UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

58 lines (57 loc) • 4.22 kB
import { deliverEngineNotice } from "../engine-notice.js"; import { IMAGE_ADMISSION } from "../image-admission.js"; export function toolResultMediaRefusedSeat(onNotice, ids) { return ({ toolName, toolCallId, refused }) => { const reasons = [...new Set(refused.map((r) => r.reason))].join(", "); const kept = refused.filter((r) => r.ref !== undefined).length; const unkept = refused.filter((r) => r.notKept !== undefined).length; deliverEngineNotice(onNotice, { code: "tool_result.media_refused", message: `The ${JSON.stringify(toolName)} tool's result carried ${refused.length} media block(s) whose bytes the media admission refused (${reasons}). ` + `Each was replaced in the result by a text placeholder naming the refusal, so neither the model nor the event stream received those bytes; ` + `the tool, or the server behind it, returned bytes that are not what their block claims, or that are beyond what can be sent.` + (kept > 0 ? ` ${kept} refused for their size were kept in the run's tool-result store, and their placeholders name each one's ref (detail.refused[].ref).` : "") + (unkept > 0 ? ` ${unkept} refused for their size were not kept (detail.refused[].notKept says why).` : ""), detail: { toolName, toolCallId, refused: refused.map((r) => ({ ...r })), sessionId: ids.sessionId, runId: ids.runId }, }); }; } const NOT_KEPT_PHRASE = { no_byte_store: (_, offloadStore) => offloadStore ? "the run's tool-result store has no byte arm (a store with putBytes / bytesOf keeps them)" : "this run keeps no tool results in a store (its offload is off: a tool-result threshold of 0 or not finite, or ReadToolResult not among its tools)", over_limit: (pieces) => `over the keep limit (${[...new Set(pieces.map((k) => ("limit" in k ? k.limit : 0)))].join(" / ")} bytes)`, store_failed: () => "the store's write failed (detail.kept[].saveError)", }; export function mcpBinaryKeptSeat(onNotice, ids, offloadStore) { return ({ server, toolName, toolCallId, kept }) => { const stored = kept.filter((k) => "ref" in k).length; const notKept = new Map(); for (const k of kept) if ("notKept" in k) notKept.set(k.notKept, [...(notKept.get(k.notKept) ?? []), k]); const notKeptHalf = [...notKept].map(([reason, pieces]) => `${pieces.length} were not kept: ${NOT_KEPT_PHRASE[reason](pieces, offloadStore)}`).join("; "); deliverEngineNotice(onNotice, { code: "tool_result.media_persisted", message: `MCP server ${JSON.stringify(server)} sent ${kept.length} piece(s) of binary content in the ${JSON.stringify(toolName)} result that the result does not carry as media.` + (stored > 0 ? ` ${stored} were kept in the run's tool-result store, and the result names each one's ref in place of its bytes.` : "") + (notKeptHalf !== "" ? ` ${notKeptHalf}; the result says so where the bytes would have been.` : ""), detail: { server, toolName, toolCallId, kept: kept.map((k) => ({ ...k })), sessionId: ids.sessionId, runId: ids.runId }, }); }; } export function imageUnprocessedSeat(onNotice, seat, ids) { let told = false; return (bytes) => { if (told) return; told = true; const why = seat === false ? "`RunnerDeps.imageProcessor` is `false`" : "the optional sharp dependency did not resolve and `RunnerDeps.imageProcessor` sets none"; deliverEngineNotice(onNotice, { code: "config.image_processor_unavailable", message: `An image of ${bytes} bytes went to the model as it was, over the ${IMAGE_ADMISSION.soft.targetBytes}-byte target, because this run has no image processor: ${why}. ` + `Images within the hard limits are sent uncompressed (more request bytes and tokens, earlier media-budget trims); set a processor or install sharp to compress them. Said once per run.`, detail: { bytes, targetBytes: IMAGE_ADMISSION.soft.targetBytes, sessionId: ids.sessionId, runId: ids.runId }, }); }; }