UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

139 lines (138 loc) • 10.4 kB
/** * Bytes a tool result carried that the model cannot receive as content, kept as an entry of the run's offload store (its byte * arm, `ToolResultStore.putBytes`) and named to the model by ref: the same holder, the same lifetime (the session's deletion, the * store's age bound, its orphan leg) and the same reference form as an offloaded text result. Two suppliers reach the one keep: * a media block the tool-result seat refused for its SIZE (`image-admission.ts`), and an MCP server's binary content that is not * media the model reads (an audio block, a resource blob of another type; `mcp.ts`). * * A keep answers one of two things: kept (the ref), or not kept for one reason of a closed set. The line the model reads is made * of those words only — never a store's error text, which names the store's own paths; that text goes to the operator's account. * Nothing falls back to a directory of the engine's host: a second holder is a second lifetime nobody deletes. * * A leaf (no value import): the media admission reaches the keep through a {@link ToolResultBytesKeeper} it is handed, so its * evaluation stays below the store, the tools and the prompt assembly the store's own tool face loads. */ import type { ToolResultSeat } from "./tool-result-store.js"; /** The longest claimed media type a sentence repeats; a longer one is named by its length. Long enough for the registered names a * binary result really carries (office document types run to 73 characters, their part types past 80), short enough that a * "type" carries no paragraph. */ export declare const MEDIA_TYPE_ECHO_MAX = 100; /** * A claimed media type as a sentence may repeat it: only when it can be one (RFC 6838 restricted names: no space, at most * 127 characters a side) and is at most {@link MEDIA_TYPE_ECHO_MAX} characters — a "type" is never a carrier for text. * `undefined` otherwise. */ export declare function mediaTypeEcho(mime: unknown): string | undefined; /** A claimed media type in a sentence: the type itself when {@link mediaTypeEcho} repeats it, else its length, else * `unknown type` when none was claimed. */ export declare function mediaTypePhrase(mime: unknown): string; /** The largest single piece of bytes a run keeps (100 MiB, the reference's per-blob persist bound); a larger one is not kept. */ export declare const TOOL_RESULT_BYTES_KEEP_LIMIT = 104857600; /** The tool that writes kept bytes into a file of the run's environment (mounted beside the run's write tools). */ export declare const SAVE_TOOL_RESULT_NAME = "SaveToolResult"; /** Why bytes were not kept — the closed set every model-visible "not kept" line is made of: * `no_byte_store` (the run has no offload store, or its store has no byte arm), `over_limit` (over a bound on one piece: the * run's {@link TOOL_RESULT_BYTES_KEEP_LIMIT}, or the store's own — an in-memory store takes no single piece larger than its * whole bound), `store_failed` (the store's write threw anything else). */ export type BytesNotKeptReason = "no_byte_store" | "over_limit" | "store_failed"; /** What keeping one piece of bytes came to, as the operator's account carries it: its size, its claimed type when * {@link mediaTypeEcho} repeats it, and either the ref it is kept under or why it is not — with the bound it was over (`limit`, * in bytes) for `over_limit`, and the store's own words (`saveError`, the text it threw) whenever the store threw: always on * `store_failed`, and on `over_limit` when the bound is the store's own refusal — never on the run's own bound, where no store * spoke. Those words are the operator's, never the model's: a face of the result carries {@link keptBytesShown}. */ export type KeptBytes = { readonly bytes: number; readonly mimeType?: string; readonly ref: string; } | { readonly bytes: number; readonly mimeType?: string; readonly notKept: "no_byte_store"; } | { readonly bytes: number; readonly mimeType?: string; readonly notKept: "over_limit"; readonly limit: number; readonly saveError?: string; } | { readonly bytes: number; readonly mimeType?: string; readonly notKept: "store_failed"; readonly saveError: string; }; /** A {@link KeptBytes} without the store's words: the members the line the model reads is made of ({@link notKeptClause}, and the * ref of the kept clause). */ export type KeptBytesShown = KeptBytes extends infer K ? (K extends KeptBytes ? Omit<K, "saveError"> : never) : never; /** What a face of the result carries of one keep (an MCP result's `details` stands it where the piece's bytes were): the record * without the store's words, which only the operator's account carries. */ export declare function keptBytesShown(kept: KeptBytes): KeptBytesShown; /** Index one piece by the base64 string it was sent as; the first piece of a string decides it (a later one is the same bytes). */ export declare function noteKeptPiece(pieces: Map<string, KeptBytes>, base64: string, kept: KeptBytes): void; /** * The `details` face of an answer that carried binary pieces as base64 (an MCP tool call's result, its structured content, a resource * read's contents): the answer as it was sent, except that a string of it that is the base64 a piece was sent as — a piece the * result's line already accounts for, kept in the run's tool-result store or judged not to keep (`pieces`) — stands as `{ kept }`: * that piece's record as the line reads it ({@link keptBytesShown}: its size, its type when it can be one, its ref or why it was not * kept; never the store's words). The bytes are then held once — by the store, or nowhere when they were not kept — instead of * riding the session row and being written to the store a second time as text. Everything else is as sent, by identity where nothing * under it changed: with no piece the answer is returned as it came. Plain objects and arrays are walked, each member under its own * key (`__proto__` included), an array keeping its length and its holes; a container met again inside itself is carried as it is, * and one met again elsewhere (an answer a host built without JSON on the way can share a subtree) is the same rebuilt container, * walked once; a value of any other kind is carried as it is. A walk that cannot finish (a member whose read throws) carries the * whole answer as it came — the call already ran, and its result is never failed for this. */ export declare function withKeptPieces(answer: unknown, pieces: ReadonlyMap<string, KeptBytes>): unknown; /** Whether the run's model can write kept bytes into a file: {@link SAVE_TOOL_RESULT_NAME} callable now, mounted but not active yet * (behind the tool search), or not mounted at all (no offload store with a byte arm, no write hands, or the leg's valve). */ export type SaveToolResultReach = "callable" | "deferred" | "absent"; /** The reach of {@link SAVE_TOOL_RESULT_NAME} over a run's tools as its valve leaves them (`mounted`) and the set callable now * (`callable`; `undefined` when the run defers nothing, so every mounted tool is callable). */ export declare function saveToolResultReach(mounted: readonly { readonly name: string; }[], callable: ReadonlySet<string> | undefined): SaveToolResultReach; /** * Where a run keeps bytes: the offload store and the session its refs are minted under (absent store ⇒ nothing is kept), and how * the run's model can write kept bytes to a file (absent ⇒ it cannot). Plain data — a library caller wiring a tool surface itself * hands its own store here; a seat it does not hand over keeps nothing. */ export interface ToolResultBytesSeat { readonly store?: ToolResultSeat; readonly sessionId: string; readonly saveReach?: () => SaveToolResultReach; } /** The facts every {@link KeptBytes} carries for one piece: its size, and its claimed type when {@link mediaTypeEcho} repeats it. */ export declare function keptBytesFacts(bytes: Uint8Array, mimeType: unknown): { readonly bytes: number; readonly mimeType?: string; }; /** * The way out every model-visible sentence takes when it says output cannot be read back here — a ref that expired or was displaced * (`ReadToolResult`, `SaveToolResult`: `gone`), bytes that were not kept ({@link notKeptClause}: `notKept`). Never an unconditional * "run it again": a repeated call with an effect (a command, a write, a request) repeats the effect. `gone`: what is left here is the * preview; fetch again only where that repeats no action, otherwise tell the person. `notKept`: no fetch is offered — a second fetch * of the piece meets the same keep (the run's or the store's reason, not the piece's) — so the person is told. */ export declare const UNRETRIEVABLE_OUTPUT: Readonly<{ readonly gone: string; readonly notKept: "they cannot be retrieved here — if you need them, do not repeat the call to get them back; tell the user they could not be kept"; }>; /** The clause a model-visible line ends with for bytes that were NOT kept — the reason's closed words and numbers (and, for * `over_limit`, that the limit belongs to this run's tool-result storage, not the tool, and to ask for a smaller result), * then the way out ({@link UNRETRIEVABLE_OUTPUT}`.notKept`) (the kept clause, which names the ref and how the run can write * it to a file, is `keptBytesClause` in `tool-result-store.ts`). */ export declare function notKeptClause(kept: Exclude<KeptBytes, { readonly ref: string; }>): string; /** What keeping one piece of bytes came to, and the clause the line naming it ends with. */ export interface KeptBytesAnswer { readonly kept: KeptBytes; readonly clause: string; } /** * One call's keep, as a supplier of bytes reaches it: keep a piece (claimed `mimeType`) and answer what came of it. The one * keeper is `toolResultBytesKeeper(seat, toolCallId)` (`tool-result-store.ts`), which binds the one keep and the one wording to a * run's seat and the call the bytes came back from; a supplier handed none keeps nothing ({@link keepBytesWith}). */ export type ToolResultBytesKeeper = (bytes: Uint8Array, mimeType: unknown) => Promise<KeptBytesAnswer>; /** What `keeper` answers for `bytes` — or, with no keeper, that they were not kept for want of a byte store. */ export declare function keepBytesWith(keeper: ToolResultBytesKeeper | undefined, bytes: Uint8Array, mimeType: unknown): Promise<KeptBytesAnswer>;