This commit is contained in:
seb
2026-09-22 19:32:46 +02:00
parent d47acf5481
commit 746c4749ab

View File

@@ -67,17 +67,18 @@ class OpenRouterProvider {
onStatus?.('connected'); onStatus?.('connected');
return this._readStream(response, { onStatus, onContent, onReasoning }); return this._readStream(response, { onStatus, onContent, onReasoning }, messages);
} }
/** @private — read SSE stream and assemble the final result. */ /** @private — read SSE stream and assemble the final result. */
async _readStream(response, handlers) { async _readStream(response, handlers, messages) {
const reader = response.body.getReader(); const reader = response.body.getReader();
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buffer = ''; let buffer = '';
const result = {}; const result = {};
const state = { workingSent: false }; const state = { workingSent: false };
let firstPayload = true; let firstPayload = true;
let completeReasoning = '';
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
@@ -93,11 +94,13 @@ class OpenRouterProvider {
const payload = trimmed.slice(5).trim(); const payload = trimmed.slice(5).trim();
if (payload === '[DONE]') continue; if (payload === '[DONE]') continue;
console.log('OpenRouter SSE payload:', payload); //console.log('OpenRouter SSE payload:', payload);
const payloadObj = JSON.parse(payload); const payloadObj = JSON.parse(payload);
const reasoning = payloadObj.choices?.[0].delta.reasoning; const reasoning = payloadObj.choices?.[0].delta.reasoning;
if (reasoning) if (reasoning) {
console.log(reasoning); process.stdout.write(reasoning);
completeReasoning += reasoning;
}
if (firstPayload) { if (firstPayload) {
firstPayload = false; firstPayload = false;
@@ -108,6 +111,16 @@ class OpenRouterProvider {
} }
} }
if (completeReasoning) {
result.reasoning = completeReasoning;
if (Array.isArray(messages)) {
messages.push({
role: 'assistant',
content: completeReasoning,
});
}
}
return result; return result;
} }