@sema-agent/core
Version:
Stateless, task-oriented AI agent core
93 lines (92 loc) • 4.88 kB
JavaScript
import { deliverEngineNotice } from "../engine-notice.js";
import { inlineUntrusted } from "../untrusted-text.js";
import { emitTrace } from "../trace.js";
import { announceDigest, withAnnounceDigests } from "./announce-once-ledger.js";
const MEDIA_REMOVED = "context.media_removed";
const counted = (n, noun) => (n === 1 ? `${noun === "image" ? "an" : "a"} ${noun}` : `${n} ${noun}s`);
function announceMediaRemoved(ids, reason, images, documents) {
const what = [images > 0 ? counted(images, "image") : undefined, documents > 0 ? counted(documents, "document") : undefined].filter((s) => s !== undefined).join(" and ");
const one = images + documents === 1;
const it = one ? "it" : "them";
const message = reason === "rejected_by_provider"
? `The model provider could not process ${what} in this conversation, so ${one ? "it was" : "they were"} removed ` +
`from what the model sees (each replaced by a line saying so) and the request was sent again without ${it}. ` +
`The session still holds the original; to show the model this content again, send it in another format.`
: `${what[0].toUpperCase()}${what.slice(1)} in this conversation no longer fit the request's media budget, so ${one ? "it was" : "they were"} removed ` +
`from what the model sees (the oldest go first; each is replaced by a line saying so). ` +
`The session still holds the original${one ? "" : "s"}; to show the model ${one ? "it" : "one of them"} again, send or ask for ${one ? "it" : "only that one"} — ` +
`showing all the removed media again would push other media out. Said once per image or document per conversation.`;
deliverEngineNotice(ids.onNotice, { code: MEDIA_REMOVED, message, detail: { reason, images, documents, sessionId: ids.sessionId, runId: ids.runId } });
}
export function createProviderRefusalSeat(ids, onNewlyRemoved) {
const announced = new Set();
return (removals) => {
for (const { refusalAt, images } of removals) {
if (announced.has(refusalAt))
continue;
announced.add(refusalAt);
onNewlyRemoved?.();
announceMediaRemoved(ids, "rejected_by_provider", images, 0);
}
};
}
export function createToolSchemaRemovedSeat(onNotice, ids, onNewlyRemoved) {
const announced = new Set();
return ({ tool, refusalAt }) => {
if (announced.has(refusalAt))
return;
announced.add(refusalAt);
onNewlyRemoved?.();
const name = inlineUntrusted(tool, 160);
deliverEngineNotice(onNotice, {
code: "context.tool_schema_removed",
message: `Tool "${name}": the model provider refused this tool's input schema, so the request was sent again with the tool's parameters ` +
`taken out of what the model sees. The model can still call the tool in this conversation, but does not see its parameters. ` +
`Fix the schema on the server that provides the tool, then start a new conversation.`,
detail: { tool, sessionId: ids.sessionId, runId: ids.runId },
});
};
}
export function createMediaBudgetSeat(ids, record, trace, onNewlyRemoved) {
const lookedAt = new WeakSet();
return ({ removed, removedBase64Chars, items, base64Chars }, { maxItems, budgetBase64Chars }) => {
const cutImages = removed.filter((r) => r.type === "image").length;
emitTrace(trace.tracer, () => ({
kind: MEDIA_REMOVED,
version: 1,
taskId: trace.taskId,
reason: "media_budget",
images: cutImages,
documents: removed.length - cutImages,
removedBase64Chars,
items,
base64Chars,
maxItems,
...(budgetBase64Chars !== Infinity ? { budgetBase64Chars } : {}),
ts: Date.now(),
}));
const told = new Set(record.announcedListingsRef.advisories ?? []);
const fresh = new Set();
let images = 0;
let documents = 0;
for (const { block, type, data } of removed) {
if (lookedAt.has(block))
continue;
lookedAt.add(block);
const digest = announceDigest(MEDIA_REMOVED, `${type}\n${data}`);
if (told.has(digest) || fresh.has(digest))
continue;
fresh.add(digest);
if (type === "image")
images++;
else
documents++;
}
if (fresh.size === 0)
return;
onNewlyRemoved?.();
announceMediaRemoved(ids, "media_budget", images, documents);
record.announcedListingsRef.advisories = withAnnounceDigests(record.announcedListingsRef.advisories, [...fresh]);
record.frameReceipts.listingSnapshotOwed = true;
};
}