Fix: Switch streamOpenRouter to fetch for images

This commit is contained in:
2025-12-29 05:40:31 -08:00
parent 62604b42d5
commit 584f848485

View File

@@ -1,6 +1,5 @@
import OpenAI from 'openai'; import OpenAI from 'openai';
import Anthropic from '@anthropic-ai/sdk'; import Anthropic from '@anthropic-ai/sdk';
import { OpenRouter } from '@openrouter/sdk';
const TTL_MS = 20 * 60 * 1000; const TTL_MS = 20 * 60 * 1000;
const BATCH_MS = 800; const BATCH_MS = 800;
@@ -330,25 +329,53 @@ export class MyDurableObject {
} }
async streamOpenRouter({ apiKey, body }) { async streamOpenRouter({ apiKey, body }) {
const client = new OpenRouter({ apiKey, defaultHeaders: { 'HTTP-Referer': 'https://sune.chat', 'X-Title': 'Sune' } }); const resp = await fetch("https://openrouter.ai/api/v1/chat/completions", {
const stream = await client.chat.send({ ...body, stream: true }); method: 'POST',
let hasReasoning = false, hasContent = false; headers: {
for await (const chunk of stream) { 'Authorization': `Bearer ${apiKey}`,
if (this.phase !== 'running') break; 'Content-Type': 'application/json',
const delta = chunk?.choices?.[0]?.delta; 'HTTP-Referer': 'https://sune.chat',
const images = delta?.images; 'X-Title': 'Sune'
},
if (delta?.reasoning && body.reasoning?.exclude !== true) { body: JSON.stringify(body),
this.queueDelta(delta.reasoning); signal: this.controller.signal
hasReasoning = true; });
}
if (delta?.content) { if (!resp.ok) throw new Error(`OpenRouter API error: ${resp.status} ${await resp.text()}`);
if (hasReasoning && !hasContent) this.queueDelta('\n');
this.queueDelta(delta.content); const reader = resp.body.getReader();
hasContent = true; const decoder = new TextDecoder();
} let buffer = '', hasReasoning = false, hasContent = false;
if (images) {
this.queueDelta('', images); while (this.phase === 'running') {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
const data = line.substring(6).trim();
if (data === '[DONE]') break;
try {
const j = JSON.parse(data);
const delta = j.choices?.[0]?.delta;
if (!delta) continue;
if (delta.reasoning && body.reasoning?.exclude !== true) {
this.queueDelta(delta.reasoning);
hasReasoning = true;
}
if (delta.content) {
if (hasReasoning && !hasContent) this.queueDelta('\n');
this.queueDelta(delta.content);
hasContent = true;
}
if (delta.images) {
this.queueDelta('', delta.images);
}
} catch {}
} }
} }
} }
@@ -449,4 +476,3 @@ export class MyDurableObject {
return contents; return contents;
} }
} }