fix(chat): reduce system spam and cap history

This commit is contained in:
Peter Steinberger
2025-12-16 20:35:03 +01:00
parent 49a9f74753
commit e1e3da946f
4 changed files with 202 additions and 46 deletions

View File

@@ -676,23 +676,24 @@ export async function getReplyFromConfig(
}
}
// Prepend queued system events and (for new main sessions) a provider snapshot.
// For new main sessions, prepend a provider snapshot.
// Note: We intentionally do NOT prepend queued system events to the user prompt,
// since that bloats session logs (token cost) and clutters chat history.
const isGroupSession =
typeof ctx.From === "string" &&
(ctx.From.includes("@g.us") || ctx.From.startsWith("group:"));
const isMainSession =
!isGroupSession && sessionKey === (sessionCfg?.mainKey ?? "main");
if (isMainSession) {
const systemLines: string[] = [];
const queued = drainSystemEvents();
systemLines.push(...queued);
// Drain (discard) queued system events so they remain ephemeral.
// They are still available via presence/health in the gateway UI.
drainSystemEvents();
if (isNewSession) {
const summary = await buildProviderSummary(cfg);
if (summary.length > 0) systemLines.unshift(...summary);
}
if (systemLines.length > 0) {
const block = systemLines.map((l) => `System: ${l}`).join("\n");
prefixedBodyBase = `${block}\n\n${prefixedBodyBase}`;
if (summary.length > 0) {
const block = summary.map((l) => `System: ${l}`).join("\n");
prefixedBodyBase = `${block}\n\n${prefixedBodyBase}`;
}
}
}
if (