chat/index.js cleanup
This commit is contained in:
@@ -117,7 +117,7 @@ async function getRelevantEntities(userMessage, projectId=null) {
|
|||||||
);
|
);
|
||||||
return results.map((r) => r.payload).filter(Boolean);
|
return results.map((r) => r.payload).filter(Boolean);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.warn(
|
logger.debug(
|
||||||
"[orchestration] Entity search failed, continuing without:",
|
"[orchestration] Entity search failed, continuing without:",
|
||||||
err.message,
|
err.message,
|
||||||
);
|
);
|
||||||
@@ -125,215 +125,134 @@ async function getRelevantEntities(userMessage, projectId=null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function chat(externalId, userMessage, options = {}) {
|
async function assembleContext(externalId, userMessage) {
|
||||||
const { recentEpisodeLimit, semanticLimit, scoreThreshold, temperature, repeatPenalty, topP, topK, systemPrompt} =
|
const settings = appSettings.load();
|
||||||
appSettings.load();
|
const { recentEpisodeLimit, semanticLimit, scoreThreshold,
|
||||||
// 1. Resolve or create session
|
temperature, repeatPenalty, topP, topK, systemPrompt } = settings;
|
||||||
let session = await memory.getSessionByExternalId(externalId);
|
|
||||||
if (!session) session = await memory.createSession(externalId);
|
|
||||||
|
|
||||||
let projectSessionIds = null;
|
// 1. Resolve or create session
|
||||||
let activeSystemPrompt = systemPrompt ?? ORCHESTRATION.SYSTEM_PROMPT;
|
|
||||||
if (session.project_id) {
|
|
||||||
try {
|
|
||||||
const project = await memory.getProject(session.project_id);
|
|
||||||
if (project) {
|
|
||||||
const projectSessions = await memory.getProjectSessions(session.project_id);
|
|
||||||
if (project?.system_prompt) activeSystemPrompt = project.system_prompt;
|
|
||||||
projectSessionIds = projectSessions.map((s) => s.id);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn(
|
|
||||||
"[orchestration] Failed to resolve project context:",
|
|
||||||
err.message,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 2. Fetch recent episodes for context
|
|
||||||
const recentEpisodes = await memory.getRecentEpisodes(
|
|
||||||
session.id,
|
|
||||||
recentEpisodeLimit,
|
|
||||||
);
|
|
||||||
const isFirstMessage = recentEpisodes.length === 0;
|
|
||||||
const recentIds = new Set(recentEpisodes.map((e) => e.id));
|
|
||||||
|
|
||||||
// 3. Semantic Search
|
|
||||||
const semanticEpisodes = await getSemanticEpisodes(
|
|
||||||
userMessage,
|
|
||||||
session.id,
|
|
||||||
recentIds,
|
|
||||||
projectSessionIds,
|
|
||||||
{ semanticLimit, scoreThreshold },
|
|
||||||
);
|
|
||||||
|
|
||||||
// 3b. Entity Search
|
|
||||||
const entities = await getRelevantEntities(userMessage, session.project_id ?? null);
|
|
||||||
|
|
||||||
// 4. Assemble prompt
|
|
||||||
const prompt = buildPrompt(
|
|
||||||
recentEpisodes,
|
|
||||||
semanticEpisodes,
|
|
||||||
entities,
|
|
||||||
userMessage,
|
|
||||||
activeSystemPrompt,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 5. Run inference
|
|
||||||
const result = await inference.complete(prompt, {...options, temperature, repeatPenalty, topP, topK});
|
|
||||||
|
|
||||||
// 6. Write episode back to memory
|
|
||||||
try {
|
|
||||||
await memory.createEpisode(
|
|
||||||
session.id, userMessage, result.text,
|
|
||||||
(result.evalCount || 0) + (result.promptEvalCount || 0),
|
|
||||||
session.project_id ?? null,
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error('[orchestration] Failed to save episode:', err.message);
|
|
||||||
}
|
|
||||||
const allEpisodes = await memory.getRecentEpisodes(session.id, 9999);
|
|
||||||
triggerSummary(session, allEpisodes);
|
|
||||||
|
|
||||||
|
|
||||||
// 8. Auto-name on first message
|
|
||||||
if (isFirstMessage && !session.name) {
|
|
||||||
autoNameSession(externalId, userMessage, result.text).catch(() => {}); // already logged inside autoNameSession
|
|
||||||
}
|
|
||||||
|
|
||||||
// 9. Return response
|
|
||||||
return {
|
|
||||||
sessionId: externalId,
|
|
||||||
response: result.text,
|
|
||||||
model: result.model,
|
|
||||||
tokenCount: (result.evalCount || 0) + (result.promptEvalCount || 0),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
|
||||||
const { recentEpisodeLimit, semanticLimit, scoreThreshold, temperature, repeatPenalty, topP, topK, systemPrompt } = appSettings.load();
|
|
||||||
let session = await memory.getSessionByExternalId(externalId);
|
let session = await memory.getSessionByExternalId(externalId);
|
||||||
if (!session) session = await memory.createSession(externalId);
|
if (!session) session = await memory.createSession(externalId);
|
||||||
|
|
||||||
|
// 2. Resolve project context
|
||||||
let projectSessionIds = null;
|
let projectSessionIds = null;
|
||||||
let activeSystemPrompt = systemPrompt ?? ORCHESTRATION.SYSTEM_PROMPT;
|
let activeSystemPrompt = systemPrompt ?? ORCHESTRATION.SYSTEM_PROMPT;
|
||||||
if (session.project_id) {
|
if (session.project_id) {
|
||||||
try {
|
try {
|
||||||
const project = await memory.getProject(session.project_id);
|
const project = await memory.getProject(session.project_id);
|
||||||
if (project) {
|
if (project) {
|
||||||
const projectSessions = await memory.getProjectSessions(
|
const projectSessions = await memory.getProjectSessions(session.project_id);
|
||||||
session.project_id,
|
if (project.system_prompt) activeSystemPrompt = project.system_prompt;
|
||||||
);
|
projectSessionIds = projectSessions.map(s => s.id);
|
||||||
projectSessionIds = projectSessions.map((s) => s.id);
|
}
|
||||||
if (project?.system_prompt) activeSystemPrompt = project.system_prompt;
|
} catch (err) {
|
||||||
|
logger.warn('[orchestration] Failed to resolve project context:', err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn(
|
|
||||||
"[orchestration] Failed to resolve project context:",
|
|
||||||
err.message,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const recentEpisodes = await memory.getRecentEpisodes(
|
// 3. Fetch recent episodes
|
||||||
session.id,
|
const recentEpisodes = await memory.getRecentEpisodes(session.id, recentEpisodeLimit);
|
||||||
recentEpisodeLimit,
|
|
||||||
);
|
|
||||||
const isFirstMessage = recentEpisodes.length === 0;
|
const isFirstMessage = recentEpisodes.length === 0;
|
||||||
const recentIds = new Set(recentEpisodes.map((e) => e.id));
|
const recentIds = new Set(recentEpisodes.map(e => e.id));
|
||||||
const semanticEpisodes = await getSemanticEpisodes(
|
|
||||||
userMessage,
|
|
||||||
session.id,
|
|
||||||
recentIds,
|
|
||||||
projectSessionIds,
|
|
||||||
{semanticLimit, scoreThreshold }
|
|
||||||
);
|
|
||||||
|
|
||||||
|
// 4. Semantic + entity search
|
||||||
|
const semanticEpisodes = await getSemanticEpisodes(
|
||||||
|
userMessage, session.id, recentIds, projectSessionIds, { semanticLimit, scoreThreshold }
|
||||||
|
);
|
||||||
const entities = await getRelevantEntities(userMessage, session.project_id ?? null);
|
const entities = await getRelevantEntities(userMessage, session.project_id ?? null);
|
||||||
|
|
||||||
const prompt = buildPrompt(
|
// 5. Assemble prompt
|
||||||
recentEpisodes,
|
const prompt = buildPrompt(recentEpisodes, semanticEpisodes, entities, userMessage, activeSystemPrompt);
|
||||||
semanticEpisodes,
|
|
||||||
entities,
|
|
||||||
userMessage,
|
|
||||||
activeSystemPrompt,
|
|
||||||
);
|
|
||||||
const res = await inference.completeStream(prompt, {...options, temperature, repeatPenalty, topP, topK});
|
|
||||||
|
|
||||||
let fullText = "";
|
return {
|
||||||
let model = "";
|
session,
|
||||||
let tokenCount = 0;
|
prompt,
|
||||||
let buffer = "";
|
isFirstMessage,
|
||||||
|
inferenceOptions: { temperature, repeatPenalty, topP, topK },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
for await (const chunk of res.body) {
|
async function chat(externalId, userMessage, options = {}) {
|
||||||
buffer += Buffer.from(chunk).toString("utf8");
|
const { session, prompt, isFirstMessage, inferenceOptions } = await assembleContext(externalId, userMessage);
|
||||||
|
|
||||||
const events = buffer.split("\n\n");
|
const result = await inference.complete(prompt, { ...options, ...inferenceOptions });
|
||||||
buffer = events.pop() || "";
|
|
||||||
|
|
||||||
for (const event of events) {
|
try {
|
||||||
const lines = event.split("\n");
|
await memory.createEpisode(
|
||||||
const dataLines = lines
|
session.id, userMessage, result.text,
|
||||||
.filter((line) => line.startsWith("data: "))
|
(result.evalCount || 0) + (result.promptEvalCount || 0),
|
||||||
.map((line) => line.slice(6));
|
session.project_id ?? null,
|
||||||
|
);
|
||||||
if (dataLines.length === 0) continue;
|
} catch (err) {
|
||||||
|
logger.error('[orchestration] Failed to save episode:', err.message);
|
||||||
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) {
|
|
||||||
logger.error(
|
|
||||||
"[orchestration] Failed to parse inference SSE event:",
|
|
||||||
raw,
|
|
||||||
err.message,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fullText.trim()) {
|
const allEpisodes = await memory.getRecentEpisodes(session.id, 9999);
|
||||||
await memory.createEpisode(session.id, userMessage, fullText, tokenCount, session.project_id ?? null);
|
triggerSummary(session, allEpisodes);
|
||||||
const allEpisodes = await memory.getRecentEpisodes(session.id, 9999);
|
|
||||||
triggerSummary(session, allEpisodes);
|
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
"[orchestration] Stream finished with no assistant text; episode not saved",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFirstMessage && !session.name) {
|
if (isFirstMessage && !session.name) {
|
||||||
autoNameSession(externalId, userMessage, fullText).catch(() => {});
|
autoNameSession(externalId, userMessage, result.text).catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
return { model, tokenCount };
|
return {
|
||||||
} catch (err) {
|
sessionId: externalId,
|
||||||
logger.error(
|
response: result.text,
|
||||||
"[orchestration] chatStream fatal error:",
|
model: result.model,
|
||||||
err.message,
|
tokenCount: (result.evalCount || 0) + (result.promptEvalCount || 0),
|
||||||
err.stack,
|
};
|
||||||
);
|
}
|
||||||
throw err;
|
|
||||||
}
|
async function chatStream(externalId, userMessage, onChunk, options = {}) {
|
||||||
|
try {
|
||||||
|
const { session, prompt, isFirstMessage, inferenceOptions } = await assembleContext(externalId, userMessage);
|
||||||
|
|
||||||
|
const res = await inference.completeStream(prompt, { ...options, ...inferenceOptions });
|
||||||
|
|
||||||
|
let fullText = '', model = '', tokenCount = 0, 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 dataLines = event.split('\n')
|
||||||
|
.filter(line => line.startsWith('data: '))
|
||||||
|
.map(line => line.slice(6));
|
||||||
|
|
||||||
|
if (!dataLines.length) 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) {
|
||||||
|
logger.error('[orchestration] Failed to parse SSE event:', raw, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fullText.trim()) {
|
||||||
|
await memory.createEpisode(session.id, userMessage, fullText, tokenCount, session.project_id ?? null);
|
||||||
|
const allEpisodes = await memory.getRecentEpisodes(session.id, 9999);
|
||||||
|
triggerSummary(session, allEpisodes);
|
||||||
|
} else {
|
||||||
|
logger.warn('[orchestration] Stream finished with no assistant text; episode not saved');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFirstMessage && !session.name) {
|
||||||
|
autoNameSession(externalId, userMessage, fullText).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
return { model, tokenCount };
|
||||||
|
} catch (err) {
|
||||||
|
logger.error('[orchestration] chatStream fatal error:', err.message, err.stack);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { chat, chatStream };
|
module.exports = { chat, chatStream };
|
||||||
|
|||||||
Reference in New Issue
Block a user