@sema-agent/core
Version:
Stateless, task-oriented AI agent core
120 lines (119 loc) • 9.06 kB
TypeScript
/**
* The `Artifact` tool: one tool, `action`-multiplexed, assembled from the host's capability declaration.
* The action enum, the parameter set and the description are all functions of `capabilities` — the
* implementation behind the seam is never inspected. Session state (the version tracked per URL and the
* local-path → URL binding that makes "same file path ⇒ same page" hold) lives in this factory's closure,
* fresh per run, never checkpointed. Whose a page is, is judged HERE, on every host answer before the model reads it: the owner
* record the host hands back against the run's identity (the call binding), the one judge a run's tool-result view uses too.
* What carries no owner record reaches the model as the host wrote it — a refusal's message (but `not_found`'s), a thrown
* message, a listing's `truncated`, a conflict's `currentVersion`: the host contract keeps them clean, the engine cannot.
*/
import { type AssertAllKeysHandled, type PermissionMode } from "../../core/ask-origin.js";
import { type EngineNotice } from "../../core/engine-notice.js";
import type { SharedReadClassifier } from "../../core/runner/shared-read-classifier.js";
import type { ResolvedSharedArtifactReads } from "../../core/shared-artifact-reads.js";
import { type ArtifactCallBinding, type ArtifactHost, type ArtifactHostCapabilities } from "../../core/artifact-host.js";
import { type OnAsk } from "../../core/tool-policy.js";
import type { ToolSpec } from "../../core/types.js";
import { type IdentityReadVerdict } from "../../core/owner-identity.js";
/** Why an `artifact.owner_refused` notice was told: the judge's four refusals of an owner record a host handed back, and the one
* contradiction the engine resolves instead of refusing (the host marked the caller's own page as shared; the owner record
* decides). */
declare const ARTIFACT_OWNER_REFUSAL_REASONS: readonly ["other_identity", "no_identity", "owner_unreadable", "no_owner", "own_page_marked_shared"];
type ArtifactOwnerRefusalReason = (typeof ARTIFACT_OWNER_REFUSAL_REASONS)[number];
export type OwnerRefusalReasonsCoverTheJudge = AssertAllKeysHandled<Exclude<Exclude<IdentityReadVerdict, "readable">, ArtifactOwnerRefusalReason>>;
/** What the engine's local-file reader hands back: the canonical key (the path-binding key) and the bytes, or one of
* three refusals — the fence's own (`refused`, with its text and card), a file over the byte budget the caller
* passed (judged on metadata BEFORE any read), or a path that is not a regular file. */
export type ArtifactLocalRead = {
ok: true;
key: string;
bytes: Uint8Array;
} | {
ok: false;
reason: "refused";
text: string;
details?: unknown;
} | {
ok: false;
reason: "too_large";
size: number;
limit: number;
judgedOn: "metadata" | "bytes";
} | {
ok: false;
reason: "not_a_file";
kind: string;
};
export interface ArtifactToolOptions {
/** The hosting seat. Its declaration is read from it (`capabilities()`) the one way every mount reads it,
* `readArtifactHostCapabilities` — the judgment a Runner's prepare makes; a declaration that judgment refuses refuses the mount. */
host: ArtifactHost;
runId: string;
taskId: string;
sessionId?: string;
/** The spawned name this run carries (audit attribution on every call context). */
agentName?: string;
/** Every host call's authorization binding (stamped on each call context): the run's identity — the one every owner record
* a host hands back is judged against — and the delegation tree's root conversation. A Runner mints it at prepare; a caller
* that mounts the tool itself gives the identity it acts for. Either way it is read once, the way a run's identity is read
* (`readArtifactCallBinding`): a principal naming nobody is the anonymous identity, a value of the wrong shape refuses. */
binding: ArtifactCallBinding;
/** The engine's fs-gated single read of a model-named local file (one read per call; the bytes read are the bytes
* published); `maxBytes` is the budget left for this file, judged on metadata before the read. */
readLocalFile: (path: string, maxBytes: number, signal?: AbortSignal) => Promise<ArtifactLocalRead>;
/** The run's effective approver — the same frozen seat the tool gate resolves asks at (`frozenOnAsk` =
* `spec.onAsk ?? deps.onAsk`), riding the MOUNT dep under the same rule as the workflow mount's `parentOnAsk`: a core
* mount's execute ctx is minimal, so the seat never rides `ctx`. The shared-read consent gate resolves at it. Absent
* = the run has no ask face, and a shared read whose question the setting ({@link sharedReads}), the permission mode and
* auto mode's classifier all leave for a person is then REFUSED fail-closed (`artifact.refused`, content withheld) — never silently delivered. */
onAsk?: OnAsk;
/** The run's effective permission mode, read at each shared-read consent the setting does not answer (a steer moves it). The consent ask is
* arbitrated as the gate's asks are (class `artifact_shared_read`): `bypassPermissions` RELEASES the read — delivered,
* recorded on that call's result card (`details.released`, the gate's release record), told to nobody, and NOT
* remembered as an approval (a mode release is not a person's nod; a later `default` leg asks) — `dontAsk` refuses it,
* `auto` hands it to the classifier first ({@link classifySharedRead}, the class row says it may answer), every other
* mode asks the approver. Absent ⇒ no arbitration (the approver decides, or the read is withheld with none). */
permissionMode?: () => PermissionMode;
/** Auto mode's classifier on the shared-read question, under `auto` where the class row lets it answer: an allow delivers the read
* for this call only (not remembered as a person's nod), a refusal withholds it with the classifier's reason, and a question it
* hands on goes to the approver. Absent ⇒ the approver is asked (the leg arms no classifier, or a host mounts the tool itself). */
classifySharedRead?: SharedReadClassifier;
/** The `sharedArtifactReads` setting as the leg resolved it — what `resolveSharedArtifactReads` returned, read once at mount.
* `allow` answers the tool's consent question on a shared target yes, wherever it is asked (`read`, `list_files`, `read_file`,
* a publish conflict's snapshot), with nobody asked under any permission mode; the call's result records it
* (`details.allowedBySetting: { setting: "sharedArtifactReads", layer }`) and no person's nod is remembered. Absent, or `ask`
* ⇒ the consent as described above. */
sharedReads?: ResolvedSharedArtifactReads;
}
/** Supporting files per publish (upstream cap); enforced in the schema AND in the body, for callers that bypass the schema. */
export declare const MAX_FILES_PER_PUBLISH = 255;
export declare function mediaTypeOf(path: string): string;
export declare function isTextMedia(mediaType: string | undefined): boolean;
/** One opaque key per HOST CALL (`occurrence` counts prior host calls made under that tool-call id in this run — so a
* gateway that reuses a call id across turns never replays, and neither do two host calls of ONE execution), stable
* for any retry the host itself makes of that call: the host's idempotency key. */
export declare function artifactAttemptId(runId: string, toolCallId: string, occurrence?: number): string;
/** A published path: relative, forward-slash, no dot segments, no NUL, no backslash; returns the NFC form or the refusal. */
export declare function normalizePublishedPath(raw: string): {
ok: true;
path: string;
} | {
ok: false;
reason: string;
};
/**
* The `Artifact` tool over `opts.host`. The host's declaration is read here, by the judgment every mount makes
* (`readArtifactHostCapabilities`: the closed words, the declared actions' methods, a host telling whose a page is by identity
* declaring `read`); a declaration it refuses throws a `TypeError` naming the problem — no tool is built over it.
*/
export declare function createArtifactTool(opts: ArtifactToolOptions): ToolSpec;
/**
* The tool over a declaration `readArtifactHostCapabilities` has already read off `opts.host` — the one caller is a Runner's
* prepare, which reads it once for its own judgments (the store's place, the refusal notice) and hands its verdict on, so the
* host is asked for its declaration once per mount. Not exported from the package: a caller outside it mounts through
* {@link createArtifactTool}. `onNotice` is the seat the operator's owner account (`artifact.owner_refused`) is told on — the
* Runner's; a tool built through {@link createArtifactTool} has none and tells it on the process's default notice seat.
*/
export declare function assembleArtifactTool(opts: ArtifactToolOptions, caps: ArtifactHostCapabilities, onNotice?: (notice: EngineNotice) => void): ToolSpec;
export {};