UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

53 lines (52 loc) • 4.82 kB
import { deliverEngineNotice } from "../engine-notice.js"; const ACCOUNT = { landed: { why: "Compaction ran but did not bring this conversation under what the model can read in one request", out: "The newest messages alone are too large: start a new conversation, or send less at once (large tool results, attachments)." }, noop: { why: "Compaction found nothing older to summarize, and this conversation does not fit what the model can read in one request", out: "The newest messages alone are too large: start a new conversation, or send less at once (large tool results, attachments)." }, failed: { why: "Compaction failed (its summary did not land), and this conversation does not fit what the model can read in one request", out: "Compact the conversation again, or start a new conversation." }, paused: { why: "Compaction is paused after failing several times in a row, and this conversation does not fit what the model can read in one request", out: "Compact the conversation by hand, or start a new conversation." }, off: { why: "Automatic compaction is off for this run, and this conversation does not fit what the model can read in one request", out: "Compact the conversation by hand, or turn automatic compaction on." }, suppressed: { why: "Compaction ran recently and freed too little to run again yet, and this conversation does not fit what the model can read in one request", out: "It runs again once the conversation grows further; compact it by hand to run it now." }, cut: { why: "Compaction was stopped, and this conversation does not fit what the model can read in one request", out: "Compact the conversation, or start a new conversation." }, unknown: { why: "This conversation no longer fits what the model can read in one request, and compaction did not make it fit", out: "Compact the conversation, or start a new conversation." }, }; export function createMessagesOmittedSeat(ids) { let toldTrim = false; let toldShort = false; let toldYours = 0; return (omitted, short, yours = 0, compaction) => { if (omitted <= 0 && short === undefined) { toldTrim = toldShort = false; toldYours = 0; return; } const newTrim = omitted > 0 && !toldTrim; const newYours = yours > toldYours; const newShort = short !== undefined && !toldShort; if (!newTrim && !newYours && !newShort) return; const more = newYours && !newTrim ? yours - toldYours : 0; toldTrim ||= omitted > 0; toldShort ||= short !== undefined; toldYours = Math.max(toldYours, yours); const what = omitted === 1 ? "1 earlier message was" : `${omitted} earlier messages were`; const mine = (n) => (n === 1 ? "1 of your messages" : `${n} of your messages`); const { why, out } = ACCOUNT[compaction ?? "unknown"]; const trimmed = newTrim ? `${why}, so ${what} left out of what the model sees${yours > 0 ? `, ${mine(yours)} among them` : ""} (the oldest go first; the model is told ` + `that earlier messages were omitted). ` : more > 0 ? `This request leaves out ${mine(more)} more than the requests before it (${yours} of yours, ${omitted} messages in all; the oldest go first). ` : ""; const room = short === undefined || !newShort ? "" : `The fixed prompt (system prompt and tool schemas, about ${short.fixedTokens} tokens) leaves about ${short.roomTokens} of the ${short.lineTokens} tokens ` + `a request can hold on this model for the conversation, less than the newest turn and what every request carries (about ${short.neededTokens} tokens), ` + (omitted > 0 ? "so each request keeps only that and the model does not see the results before it" : "so this request goes out larger than the model's window and the provider is likely to refuse it") + ` — shrink the system prompt or the tool set, or use a model with a larger window. `; deliverEngineNotice(ids.onNotice, { code: "context.messages_omitted", message: `${trimmed}${room}The session still holds every message. ${newTrim ? `${out} ` : ""}Said again only when more of your messages are left out, when messages are first left out or a request's room first falls short, and anew after a request fits or a compaction.`, detail: { omitted, ...(compaction !== undefined ? { compaction } : {}), ...(yours > 0 ? { userMessages: yours } : {}), ...(short !== undefined && newShort ? { fixedTokens: short.fixedTokens, lineTokens: short.lineTokens, roomTokens: short.roomTokens, neededTokens: short.neededTokens } : {}), sessionId: ids.sessionId, runId: ids.runId }, }); }; }