mirror of
https://github.com/sune-org/ORP.git
synced 2026-01-13 16:17:59 +00:00
Fix: Correctly parse Google API thought stream
This commit is contained in:
21
index.js
21
index.js
@@ -222,7 +222,7 @@ export class MyDurableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mapToGoogleContents(messages) {
|
mapToGoogleContents(messages) {
|
||||||
const contents = messages.reduce((acc, m) => {
|
return messages.reduce((acc, m) => {
|
||||||
const role = m.role === 'assistant' ? 'model' : 'user';
|
const role = m.role === 'assistant' ? 'model' : 'user';
|
||||||
const msgContent = Array.isArray(m.content) ? m.content : [{ type: 'text', text: String(m.content ?? '') }];
|
const msgContent = Array.isArray(m.content) ? m.content : [{ type: 'text', text: String(m.content ?? '') }];
|
||||||
const parts = msgContent.map(p => {
|
const parts = msgContent.map(p => {
|
||||||
@@ -239,8 +239,6 @@ export class MyDurableObject {
|
|||||||
else acc.push({ role, parts });
|
else acc.push({ role, parts });
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
if (contents.at(-1)?.role !== 'user') contents.pop();
|
|
||||||
return contents;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async streamOpenAI({ apiKey, body }) {
|
async streamOpenAI({ apiKey, body }) {
|
||||||
@@ -289,11 +287,22 @@ export class MyDurableObject {
|
|||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
buffer += decoder.decode(value, { stream: true });
|
buffer += decoder.decode(value, { stream: true });
|
||||||
for (const line of buffer.split('\n')) {
|
const lines = buffer.split('\n');
|
||||||
|
buffer = lines.pop() || '';
|
||||||
|
for (const line of lines) {
|
||||||
if (!line.startsWith('data: ')) continue;
|
if (!line.startsWith('data: ')) continue;
|
||||||
try { JSON.parse(line.substring(6))?.candidates?.[0]?.content?.parts?.forEach(p => p.text && this.queueDelta(p.text)); } catch {}
|
try {
|
||||||
|
const chunk = JSON.parse(line.substring(6));
|
||||||
|
const candidate = chunk?.candidates?.[0];
|
||||||
|
if (!candidate) continue;
|
||||||
|
|
||||||
|
candidate.content?.parts?.forEach(p => p.text && this.queueDelta(p.text));
|
||||||
|
|
||||||
|
const thoughts = candidate.execution_metadata?.tool_use_signals?.map(s => s.tool_code).filter(Boolean).join('');
|
||||||
|
if (thoughts) this.queueDelta(thoughts);
|
||||||
|
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
buffer = buffer.slice(buffer.lastIndexOf('\n') + 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user