@sema-agent/core
Version:
Stateless, task-oriented AI agent core
114 lines (113 loc) • 7.41 kB
TypeScript
import type { PreToolUseResult } from "./hooks.js";
import type { PermissionResult, ToolCallRequest } from "./tool-policy.js";
import type { InheritedScreenRead, PreToolUseScreen, PreToolUseScreening } from "./runner/gate-exit.js";
import type { RewriteAuthor, ToolInputRewrite } from "./task-event.js";
/** One gate pass's account of its rewrites: the bytes it took at its entry, and one author per adoption that changed the bytes
* in hand (written only through {@link noteRewrite}). */
export interface RewriteAccount {
readonly entry: unknown;
readonly by: RewriteAuthor[];
}
/** An adoption of `to` in place of `from`, by `authors`: they are named when the two read as other bytes. */
export declare function noteRewrite(account: RewriteAccount, req: {
readonly face?: object | undefined;
}, from: unknown, to: unknown, authors: readonly RewriteAuthor[]): void;
/** The restriction chain's and the approver's word in an account (one author each, frozen once). */
export declare const POLICY_AUTHOR: readonly RewriteAuthor[];
export declare const APPROVER_AUTHOR: readonly RewriteAuthor[];
/** The extensions that authored a screening's rewrite, as authors. */
export declare const extensionAuthors: (names: readonly string[]) => readonly RewriteAuthor[];
/** The call's record at the allow exit: when `ran` (the bytes the gate lets go) is not the bytes it took, `snapshot` (their frozen
* plain-data projection, the exit's `finalInput`) with the authors the account holds. Bytes that moved with no author named is an
* engine defect (an adoption that did not name itself). */
export declare function rewriteRecordOf(account: RewriteAccount, req: {
readonly face?: object | undefined;
readonly toolName: string;
}, ran: unknown, snapshot: unknown): ToolInputRewrite | undefined;
/** The authors of each `classic.PreToolUse` answer one dispatch received, read by the answer's identity as the station receives
* it: a handler is an author when its answer's `updatedInput` reads as other bytes ({@link sameBytes}, under `req`'s reading) than
* what its `next()` returned (the event's `input` when that carried none, or when it never called `next`); otherwise its answer
* carries the authors of what came up. `record` is the station's answer check (it refuses nothing); `of` reads the chain's answer. */
export declare function screeningAuthorship(req: {
readonly face?: object | undefined;
}): {
readonly record: (answer: unknown, e: {
readonly input?: unknown;
}, by: {
readonly plugin: string;
}, below: readonly unknown[]) => undefined;
readonly of: (answer: unknown) => readonly string[];
};
/** The digest of the bytes a seat hands an approver, taken BEFORE it waits (a value read afterwards could have been moved to match). */
export declare const shownDigestOf: (call: ToolCallRequest) => string | undefined;
/** `resolved` read by the one reading of an answer's edit ({@link editOfAnswer}; its `updatedInput` read once) over the bytes `shown`
* digests ({@link shownDigestOf}, taken before the wait): those bytes handed back are a plain approval, its other members and record
* kept. Every seat that takes an approval from outside the engine's own resolver (which reads its approver so already) reads it here,
* synchronously on the answer it awaited (`answerAsShown(shownDigestOf(call), await seat(…), call)`), so the read adds no wait. Only
* a plain-data answer is read so (its copy runs no code and drops nothing); any other answer is left as it came, its edit re-judged. */
export declare function answerAsShown<R extends PermissionResult>(shown: string | undefined, resolved: R, call: Pick<ToolCallRequest, "toolName">): R;
/** What a judge did with the bytes it was handed: kept them (`as_is`), or handed back another form. */
export type RejudgeReading = {
readonly kind: "as_is";
} | {
readonly kind: "rewritten";
readonly form: unknown;
};
/** Do `a` and `b` read as the same bytes for the call `req` — each as the tool reads it? */
export declare function sameBytes(req: {
readonly face?: object | undefined;
}, a: unknown, b: unknown): boolean;
/** The one reading of a judge's answer about the bytes it was handed (`given`): its `updatedInput` is a rewrite only when it
* reads as other bytes; the form is returned as the tool reads it. */
export declare function readRejudge(req: {
readonly face?: object | undefined;
}, given: unknown, answer: {
readonly updatedInput?: unknown;
} | undefined): RejudgeReading;
/** What the PreToolUse screening said on its one second look at bytes rewritten after it judged: its verdict on them
* (`stands` — no opinion, an allow, an ask; never a rewrite — with the context lines and what the inherited installations
* said), or a refusal — the hook's own deny, the engine's (the screening failed, or it handed back yet another form), or an
* inherited installation's question refused where it was settled (`settled`). */
export type ScreeningRejudge = {
readonly kind: "stands";
readonly answer: PreToolUseResult | undefined;
readonly answeredBy?: string;
readonly contexts: readonly string[];
readonly inherited?: InheritedScreenRead;
} | {
readonly kind: "refused";
readonly reason: string;
readonly by: "hook" | "engine";
readonly extension?: string;
readonly judged?: unknown;
} | Extract<PreToolUseScreen, {
kind: "settled";
}>;
/** The screening's second look at rewritten `bytes` (the gate's fold after a policy rewrite; the approved-edit pass). A refusal
* names the bytes its judge judged (`judged`) when the screen moved them past `bytes`. */
export declare function screenRewrite(screening: PreToolUseScreening, req: {
readonly face?: object | undefined;
readonly toolName: string;
}, bytes: unknown, subject: string): Promise<ScreeningRejudge>;
/**
* A person's edit the restriction chain accepts only in another form: that form, as the tool reads it. Machine-readable, so
* a surface can offer "approve this form" — approving it as it stands runs exactly these bytes (they are the chain's own
* answer, so re-judging them changes nothing). Present on the approval request of the question that puts the form to the person,
* on a durable resume's refusal (`detail.editRewritten`), and on a refusal's details where nobody is left to ask.
*/
export interface EditRewritten {
readonly acceptableForm: unknown;
}
/** The question that puts a person's edit back to them, over the pass's standing verdict on the form: an allow becomes
* this question; an ask keeps its own words after the outlet sentence (and every member it carries). It goes to the
* approver whose edit it answers; its classes are the pass's, read by the one ladder like any of its questions. */
export declare function outletAsk(standing: Exclude<PermissionResult, {
action: "deny";
}>, toolName: string, form: unknown): Extract<PermissionResult, {
action: "ask";
}>;
/** The one sentence of a person's edit that a judge accepts only rewritten; each lane adds its outlet (approve the form shown /
* decide again with it / re-issue the call). */
export declare function editRewrittenSentence(toolName: string): string;
/** The one sentence of a rewrite a judge would rewrite again on its one second look. */
export declare function rewriteChasedSentence(toolName: string): string;