mirror of
https://github.com/sune-org/ORP.git
synced 2026-01-13 16:17:59 +00:00
Refactor: Use @openrouter/sdk for OpenRouter streaming
This commit is contained in:
10
index.js
10
index.js
@@ -1,5 +1,6 @@
|
|||||||
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;
|
||||||
@@ -327,12 +328,8 @@ export class MyDurableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async streamOpenRouter({ apiKey, body }) {
|
async streamOpenRouter({ apiKey, body }) {
|
||||||
const client = new OpenAI({
|
const client = new OpenRouter({ apiKey, defaultHeaders: { 'HTTP-Referer': 'https://sune.chat', 'X-Title': 'Sune' } });
|
||||||
apiKey,
|
const stream = await client.chat.send({ ...body, stream: true });
|
||||||
baseURL: 'https://openrouter.ai/api/v1',
|
|
||||||
defaultHeaders: { 'HTTP-Referer': 'https://sune.chat', 'X-Title': 'Sune' },
|
|
||||||
});
|
|
||||||
const stream = await client.chat.completions.create({ ...body, stream: true }, { signal: this.controller.signal });
|
|
||||||
let hasReasoning = false, hasContent = false;
|
let hasReasoning = false, hasContent = false;
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
if (this.phase !== 'running') break;
|
if (this.phase !== 'running') break;
|
||||||
@@ -445,4 +442,3 @@ export class MyDurableObject {
|
|||||||
return contents;
|
return contents;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user