sse abort
This commit is contained in:
@@ -31,7 +31,7 @@ function isPdfUpload(file) {
|
|||||||
|
|
||||||
// ─── SSE helpers ────────────────────────────────────────────────────────────────
|
// ─── SSE helpers ────────────────────────────────────────────────────────────────
|
||||||
function sseWrite(res, event, data) {
|
function sseWrite(res, event, data) {
|
||||||
if (res.writableEnded) return;
|
if (res.writableEnded || res.destroyed) return;
|
||||||
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,8 +276,21 @@ async function parseDocumentManifest({ req, res, pages, sourceLabel }) {
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const onDisconnect = () => {
|
||||||
|
if (!res.writableEnded) {
|
||||||
|
orbiter.abort();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
req.on('close', onDisconnect);
|
||||||
|
res.on('close', onDisconnect);
|
||||||
|
|
||||||
// ── Process all pages in one batch ───────────────────────────────
|
// ── Process all pages in one batch ───────────────────────────────
|
||||||
try {
|
try {
|
||||||
|
if (res.destroyed || req.destroyed) {
|
||||||
|
orbiter.abort();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const pageContent = pages
|
const pageContent = pages
|
||||||
.map((p, idx) => `--- Page ${idx + 1} of ${pages.length} ---\n${p}`)
|
.map((p, idx) => `--- Page ${idx + 1} of ${pages.length} ---\n${p}`)
|
||||||
.join('\n\n');
|
.join('\n\n');
|
||||||
@@ -301,9 +314,16 @@ ${pageContent}`;
|
|||||||
currentPage = 1;
|
currentPage = 1;
|
||||||
await orbiter.run({ prompt, model, tools: [outputManifestTool] });
|
await orbiter.run({ prompt, model, tools: [outputManifestTool] });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!res.writableEnded) {
|
if (!res.writableEnded && !res.destroyed) {
|
||||||
sseWrite(res, 'error', { message: 'LLM processing failed: ' + err.message });
|
sseWrite(res, 'error', { message: 'LLM processing failed: ' + err.message });
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
req.off('close', onDisconnect);
|
||||||
|
res.off('close', onDisconnect);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.writableEnded || res.destroyed) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Final result ─────────────────────────────────────────────────
|
// ── Final result ─────────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user