Skip to content
Snippets Groups Projects
Unverified Commit ba8e4e5d authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

handle OpenRouter exceptions on streaming (#2033)

parent 9a09bac2
No related branches found
No related tags found
No related merge requests found
...@@ -254,35 +254,48 @@ class OpenRouterLLM { ...@@ -254,35 +254,48 @@ class OpenRouterLLM {
} }
}, 500); }, 500);
for await (const chunk of stream) { try {
const message = chunk?.choices?.[0]; for await (const chunk of stream) {
const token = message?.delta?.content; const message = chunk?.choices?.[0];
lastChunkTime = Number(new Date()); const token = message?.delta?.content;
lastChunkTime = Number(new Date());
if (token) {
fullText += token; if (token) {
writeResponseChunk(response, { fullText += token;
uuid, writeResponseChunk(response, {
sources: [], uuid,
type: "textResponseChunk", sources: [],
textResponse: token, type: "textResponseChunk",
close: false, textResponse: token,
error: false, close: false,
}); error: false,
} });
}
if (message.finish_reason !== null) {
writeResponseChunk(response, { if (message.finish_reason !== null) {
uuid, writeResponseChunk(response, {
sources, uuid,
type: "textResponseChunk", sources,
textResponse: "", type: "textResponseChunk",
close: true, textResponse: "",
error: false, close: true,
}); error: false,
response.removeListener("close", handleAbort); });
resolve(fullText); response.removeListener("close", handleAbort);
resolve(fullText);
}
} }
} catch (e) {
writeResponseChunk(response, {
uuid,
sources,
type: "abort",
textResponse: null,
close: true,
error: e.message,
});
response.removeListener("close", handleAbort);
resolve(fullText);
} }
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment