orchestration fixes
This commit is contained in:
@@ -87,7 +87,7 @@ async function chat(externalId, userMessage, options = {}) {
|
||||
tokenCount: (result.evalCount || 0 ) + (result.promptEvalCount || 0 ),
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
async function chatStream(externalId, userMessage, onChunk, options = {} ) {
|
||||
// 1. Resolve or create session
|
||||
let session = await memory.getSessionByExternalId(externalId);
|
||||
@@ -148,5 +148,70 @@ async function chatStream(externalId, userMessage, onChunk, options = {} ) {
|
||||
}
|
||||
return {model, tokenCount};
|
||||
}
|
||||
*/
|
||||
async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
||||
let session = await memory.getSessionByExternalId(externalId);
|
||||
if (!session) session = await memory.createSession(externalId);
|
||||
|
||||
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);
|
||||
|
||||
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, userMessage);
|
||||
const res = await inference.completeStream(prompt, options);
|
||||
|
||||
let fullText = '';
|
||||
let model = '';
|
||||
let tokenCount = 0;
|
||||
let buffer = '';
|
||||
|
||||
for await (const chunk of res.body) {
|
||||
buffer += Buffer.from(chunk).toString('utf8');
|
||||
|
||||
const events = buffer.split('\n\n');
|
||||
buffer = events.pop() || '';
|
||||
|
||||
for (const event of events) {
|
||||
const lines = event.split('\n');
|
||||
const dataLines = lines
|
||||
.filter(line => line.startsWith('data: '))
|
||||
.map(line => line.slice(6));
|
||||
|
||||
if (dataLines.length === 0) continue;
|
||||
|
||||
const raw = dataLines.join('\n').trim();
|
||||
if (raw === '[DONE]') continue;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(raw);
|
||||
|
||||
if (data.response) {
|
||||
fullText += data.response;
|
||||
onChunk(data.response);
|
||||
}
|
||||
|
||||
if (data.model) model = data.model;
|
||||
if (data.done && data.tokenCount !== undefined) {
|
||||
tokenCount = data.tokenCount;
|
||||
}
|
||||
|
||||
if (data.error) {
|
||||
throw new Error(data.error);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[orchestration] Failed to parse inference SSE event:', raw, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[orchestration] final streamed text length:', fullText.length);
|
||||
|
||||
if (fullText.trim()) {
|
||||
await memory.createEpisode(session.id, userMessage, fullText, tokenCount);
|
||||
} else {
|
||||
console.warn('[orchestration] Stream finished with no assistant text; episode not saved');
|
||||
}
|
||||
|
||||
return { model, tokenCount };
|
||||
}
|
||||
module.exports = { chat, chatStream };
|
||||
Reference in New Issue
Block a user