added chat streaming
This commit is contained in:
@@ -93,4 +93,60 @@ async function chat(externalId, userMessage, options = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { chat };
|
||||
async function chatStream(externalId, userMessage, onChunk, options = {} ) {
|
||||
// 1. Resolve or create session
|
||||
let session = await memory.getSessionByExternalId(externalId);
|
||||
if (!session) session = await memory.createSession(externalId);
|
||||
|
||||
// 2. Context assembly
|
||||
const recentEpisodes = await memory.getRecentEpisodes(session.id, RECENT_EPISODE_LIMIT);
|
||||
const recentIds = new Set(recentEpisodes.map(e => e.id));
|
||||
const semanticEpisodes = await getSemanticEpisodes(userMessage, session.id, recentIds)
|
||||
|
||||
// 3. Assemble Prompt
|
||||
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
||||
|
||||
// 4. Open stream to inference service
|
||||
const res = await inference.completeStream(prompt, options);
|
||||
|
||||
let fullText = '';
|
||||
let model = '';
|
||||
let tokenCount = 0;
|
||||
|
||||
// 5. Parse SSE chunks
|
||||
for await (const chunk of res.body){
|
||||
const lines = chunk.toString().split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue;
|
||||
const raw = line.slice(6).trim();
|
||||
if (raw === '[DONE]') continue //stream closed sentinel
|
||||
|
||||
try {
|
||||
const data = JSON.parse(raw);
|
||||
if (data.model) model = data.model
|
||||
|
||||
if (data.response) {
|
||||
fullText += data.response;
|
||||
onChunk(data.response);
|
||||
}
|
||||
|
||||
if (data.done && data.eval_count !== undefined) {
|
||||
tokenCount = (data.eval_count || 0) + (data.prompt_eval_count || 0)
|
||||
}
|
||||
} catch {
|
||||
//partial chunk
|
||||
//skip and wait for next
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Write Complete episode to memory
|
||||
memory.createEpisode(session.id, userMessage, fullText, tokenCount)
|
||||
.catch(err => console.error('[orchestration] Failed to save streamed episode:', err.message))
|
||||
|
||||
return {model, tokenCount};
|
||||
}
|
||||
|
||||
module.exports = { chat, chatStream };
|
||||
Reference in New Issue
Block a user