u
This commit is contained in:
@@ -67,17 +67,18 @@ class OpenRouterProvider {
|
||||
|
||||
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. */
|
||||
async _readStream(response, handlers) {
|
||||
async _readStream(response, handlers, messages) {
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
const result = {};
|
||||
const state = { workingSent: false };
|
||||
let firstPayload = true;
|
||||
let completeReasoning = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
@@ -93,11 +94,13 @@ class OpenRouterProvider {
|
||||
const payload = trimmed.slice(5).trim();
|
||||
if (payload === '[DONE]') continue;
|
||||
|
||||
console.log('OpenRouter SSE payload:', payload);
|
||||
//console.log('OpenRouter SSE payload:', payload);
|
||||
const payloadObj = JSON.parse(payload);
|
||||
const reasoning = payloadObj.choices?.[0].delta.reasoning;
|
||||
if (reasoning)
|
||||
console.log(reasoning);
|
||||
if (reasoning) {
|
||||
process.stdout.write(reasoning);
|
||||
completeReasoning += reasoning;
|
||||
}
|
||||
|
||||
if (firstPayload) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user