5 SDD batches archived: - Batch 1: UI Polish (10 features, 14 tasks) - Batch 2: Study System (8 features, 23 tasks) - Batch 3: Infrastructure (5 features, 22 tasks) - Batch 4: AI Advanced (5 features, 30 tasks) — RAG with @xenova/transformers - Batch 5: Core Features (5 features, 19 tasks) 37 bugs fixed from comprehensive code review (11 CRITICAL, 12 HIGH, 14 MEDIUM/LOW): - SSE streaming now works (event.token check) - API keys no longer exposed via GET /api/models - FTS5 injection sanitized - DB backup/restore with admin auth - Buddy mode wired (buddy_meta column) - Exam auto-submit stale closure fixed - CSS variables aligned with design tokens - Progress data corruption fixed - WebSocket protocol auto-detection - Tests infrastructure completed (vitest + node:test)
95 lines
2.8 KiB
JavaScript
95 lines
2.8 KiB
JavaScript
const Anthropic = require('@anthropic-ai/sdk');
|
|
const OpenAI = require('openai');
|
|
|
|
// NOTE: model objects carry api_key in memory — avoid logging full model objects.
|
|
// Use model.name or model.provider only in log statements.
|
|
|
|
/**
|
|
* Normalize Anthropic + OpenAI-compatible streams into one AsyncIterable.
|
|
* Yields: { token, done, fullText } events.
|
|
* Handles errors gracefully — emits error event, doesn't crash.
|
|
*
|
|
* Usage:
|
|
* for await (const chunk of streamCompletion(model, messages, systemPrompt)) {
|
|
* // chunk = { token: string, done: boolean, fullText: string }
|
|
* }
|
|
*/
|
|
async function* streamCompletion(model, messages, systemPrompt) {
|
|
if (!model || !model.provider) {
|
|
yield { token: '', done: true, fullText: '', error: 'Model or provider not specified' };
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (model.provider === 'anthropic') {
|
|
yield* streamAnthropic(model, messages, systemPrompt);
|
|
} else if (model.provider === 'openai') {
|
|
yield* streamOpenAI(model, messages, systemPrompt);
|
|
} else {
|
|
yield { token: '', done: true, fullText: '', error: `Unknown provider: ${model.provider}` };
|
|
}
|
|
} catch (err) {
|
|
console.error('[llm] streamCompletion error:', err.message);
|
|
yield { token: '', done: true, fullText: '', error: err.message };
|
|
}
|
|
}
|
|
|
|
async function* streamAnthropic(model, messages, systemPrompt) {
|
|
const client = new Anthropic({
|
|
apiKey: model.api_key || process.env.ANTHROPIC_API_KEY,
|
|
});
|
|
|
|
const stream = await client.messages.create({
|
|
model: model.name,
|
|
max_tokens: 4096,
|
|
system: systemPrompt || undefined,
|
|
messages: messages.map(m => ({ role: m.role, content: m.content })),
|
|
stream: true,
|
|
});
|
|
|
|
let fullText = '';
|
|
|
|
for await (const event of stream) {
|
|
if (event.type === 'content_block_delta' && event.delta?.type === 'text_delta') {
|
|
const token = event.delta.text || '';
|
|
fullText += token;
|
|
yield { token, done: false, fullText };
|
|
}
|
|
}
|
|
|
|
yield { token: '', done: true, fullText };
|
|
}
|
|
|
|
async function* streamOpenAI(model, messages, systemPrompt) {
|
|
const client = new OpenAI({
|
|
apiKey: model.api_key || process.env.OPENAI_API_KEY,
|
|
baseURL: model.api_base,
|
|
});
|
|
|
|
const openaiMessages = [];
|
|
if (systemPrompt) {
|
|
openaiMessages.push({ role: 'system', content: systemPrompt });
|
|
}
|
|
openaiMessages.push(...messages.map(m => ({ role: m.role, content: m.content })));
|
|
|
|
const stream = await client.chat.completions.create({
|
|
model: model.name,
|
|
messages: openaiMessages,
|
|
stream: true,
|
|
});
|
|
|
|
let fullText = '';
|
|
|
|
for await (const chunk of stream) {
|
|
const token = chunk.choices?.[0]?.delta?.content || '';
|
|
if (token) {
|
|
fullText += token;
|
|
yield { token, done: false, fullText };
|
|
}
|
|
}
|
|
|
|
yield { token: '', done: true, fullText };
|
|
}
|
|
|
|
module.exports = { streamCompletion };
|