mirror of
https://github.com/sune-org/ORP.git
synced 2026-01-13 16:17:59 +00:00
Fix: Switch streamOpenRouter to fetch for images
This commit is contained in:
66
index.js
66
index.js
@@ -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'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
signal: this.controller.signal
|
||||||
|
});
|
||||||
|
|
||||||
if (delta?.reasoning && body.reasoning?.exclude !== true) {
|
if (!resp.ok) throw new Error(`OpenRouter API error: ${resp.status} ${await resp.text()}`);
|
||||||
this.queueDelta(delta.reasoning);
|
|
||||||
hasReasoning = true;
|
const reader = resp.body.getReader();
|
||||||
}
|
const decoder = new TextDecoder();
|
||||||
if (delta?.content) {
|
let buffer = '', hasReasoning = false, hasContent = false;
|
||||||
if (hasReasoning && !hasContent) this.queueDelta('\n');
|
|
||||||
this.queueDelta(delta.content);
|
while (this.phase === 'running') {
|
||||||
hasContent = true;
|
const { done, value } = await reader.read();
|
||||||
}
|
if (done) break;
|
||||||
if (images) {
|
buffer += decoder.decode(value, { stream: true });
|
||||||
this.queueDelta('', images);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user