@sema-agent/core
Version:
Stateless, task-oriented AI agent core
123 lines (122 loc) • 7.8 kB
TypeScript
/**
* The advisor seat's two answers, each minted here once and read by every reader: whether the advisor tool attaches for
* a (main model, advisor model) pair on a deployment ({@link advisorAttachVerdict} — the prepare mount, a host's advisor
* query and its echo all ask this one function), and whether a conversation has switched the advisor off
* ({@link advisorHoldOf} — read off the transcript, its only source of truth, at the mount, at every call and by a host).
* The closed sets the answers are spelled in live beside them.
*/
import type { SessionTreeEntry } from "../internal/harness-types.js";
import type { AssistantMessage } from "../internal/llm.js";
import { type BrainErrorCode } from "../brain/errors.js";
import type { AssertAllKeysHandled } from "./ask-origin.js";
import type { EngineNotice, Model, ModelRef, RunnerDeps } from "./types.js";
/** The tool's wire name. */
export declare const ADVISOR_TOOL_NAME = "advisor";
/** What a fork is told when it inherits the advisor tool with its parent's prefix — the reference's sentence. */
export declare const ADVISOR_FORK_SENTENCE = "Never consult the advisor tool.";
/** Why the advisor tool is not attached on a leg whose spec names an advisor model. The first six are the static verdict's
* answers; the last three are the mount's (the tool selector unmounted it, the deployment's route judgment refused it, the
* transcript could not be read). */
export declare const ADVISOR_NOT_ATTACHED_REASONS: readonly ["disabled", "unknown_model", "advisor_unranked", "weakest_tier", "main_unranked", "pairing", "route_refused", "excluded", "hold_unknown"];
export type AdvisorNotAttachedReason = (typeof ADVISOR_NOT_ATTACHED_REASONS)[number];
/**
* Each reason's sentence to the person (`advisor.not_attached`, once per conversation per pair) — `null` for a reason that
* tells nobody: the deployment switched the advisor off (it is ignored), the deployment's tool selector unmounted it
* (`excluded`), or the deployment's route judgment refused it — its credentials cannot pair, or the deployment does not serve
* the model (`route_refused`, the refusal's code on the manifest's `routeCode`; told to the operator by `route.seat_detached` —
* only an operator can fix it).
*/
declare const NOT_ATTACHED_SENTENCE: {
readonly disabled: null;
readonly unknown_model: (a: string) => string;
readonly advisor_unranked: (a: string) => string;
readonly weakest_tier: (a: string) => string;
readonly main_unranked: (_a: string, m: string) => string;
readonly pairing: (a: string, m: string) => string;
readonly route_refused: null;
readonly excluded: null;
readonly hold_unknown: () => string;
};
export type NotAttachedSentenceCoversEveryReason = AssertAllKeysHandled<Exclude<AdvisorNotAttachedReason, keyof typeof NOT_ATTACHED_SENTENCE>>;
/** The shape door of `TaskSpec.advisorModel` (read only while the deployment switch is on): absent, a non-empty ref, or a
* Model object; anything else refuses (`config.advisor_model_invalid`) — a model the engine cannot read names no advisor. */
export declare function assertAdvisorModelShape(ref: unknown): void;
/** The three faces of one advisor call: guidance came back, the advisor model refused, or no guidance could be had. */
export declare const ADVISOR_OUTCOMES: readonly ["reviewed", "declined", "unavailable"];
export type AdvisorOutcome = (typeof ADVISOR_OUTCOMES)[number];
/** The static verdict: the advisor attaches on `model`, or does not and says why. */
export type AdvisorAttachVerdict = {
readonly state: "on";
readonly model: Model;
} | {
readonly state: "not_attached";
readonly reason: Exclude<AdvisorNotAttachedReason, "route_refused" | "excluded" | "hold_unknown">;
};
/**
* Whether the advisor tool attaches for `advisor` beside the run's `main` model, in command order: the deployment switch,
* then the advisor itself (a model the catalog cannot resolve, one on no declared tier, one on the weakest tier — `lite`,
* the tier the `haiku` alias names), then the main model (on no declared tier, or on a WEAKER tier than the advisor's:
* an advisor at least as capable as the main model attaches). Tiers are read by the classifier seat's own reading
* ({@link tierIndexOf}) over a catalog {@link normalizeSeatDeps} expanded, so a host holding raw deps gets the engine's
* answer. No tiers declared ⇒ no ranks ⇒ not attached.
*/
export declare function advisorAttachVerdict(args: {
deps: Pick<RunnerDeps, "models" | "tiers" | "advisorTool">;
main: Model;
advisor: ModelRef;
}): AdvisorAttachVerdict;
/**
* Which failure of an advisor request switches the advisor off for the conversation: the advisor model's credential refused
* (`auth`), or a provider 400 / 422 its minter ruled out as an over-window request (`invalid_request` with
* `inputTooLongRuledOut`) — deterministic client faults that repeat until the conversation is compacted. Every other code,
* an over-window refusal and an unclassified 400 pass (the next call may succeed). Total over the brain codes by its fence.
*/
export declare const ADVISOR_HOLD_CAUSES: Readonly<{
readonly auth: "hold";
readonly invalid_request: "hold_unless_window";
readonly rate_limit: "pass";
readonly server: "pass";
readonly network: "pass";
readonly http: "pass";
readonly stream_torn: "pass";
readonly refusal: "pass";
readonly length_empty: "pass";
}>;
/** What one failure's code does to the conversation's advisor ({@link ADVISOR_HOLD_CAUSES}). */
export type AdvisorHoldDisposition = "hold" | "hold_unless_window" | "pass";
export type HoldCausesCoverEveryBrainCode = AssertAllKeysHandled<Exclude<BrainErrorCode, keyof typeof ADVISOR_HOLD_CAUSES>>;
/** What a failed advisor request said about itself — the fields the hold reads and the card carries. */
export interface AdvisorFailure {
readonly code?: BrainErrorCode;
readonly errorKind?: AssistantMessage["errorKind"];
readonly inputTooLongRuledOut?: true;
}
/** Whether this failure switches the advisor off for the conversation ({@link ADVISOR_HOLD_CAUSES}). */
export declare function advisorFailureHolds(f: AdvisorFailure): boolean;
/** The conversation's advisor switch-off, with the cause of the call that switched it. */
export interface AdvisorHold {
readonly code?: BrainErrorCode;
readonly errorKind?: AdvisorFailure["errorKind"];
}
/**
* Whether the conversation on this branch has switched the advisor off, read off the transcript: an advisor tool result
* recorded after the latest compaction whose card says `hold: true` — the latest such card's cause. A compaction clears it
* (the card lands before it); a new conversation has none; a fork copies its parent's branch and with it the hold.
* `undefined` = not held. Members of a recorded card outside their closed sets are not carried.
*/
export declare function advisorHoldOf(entries: ReadonlyArray<SessionTreeEntry>): AdvisorHold | undefined;
/** A not-attached reason the person is told about — its own fact or one they can act on — as a type. */
export type AdvisorToldReason = {
[R in AdvisorNotAttachedReason]: (typeof NOT_ATTACHED_SENTENCE)[R] extends null ? never : R;
}[AdvisorNotAttachedReason];
/** Whether the person is told about this reason ({@link NOT_ATTACHED_SENTENCE}: the reasons with a sentence). */
export declare function advisorReasonTold(reason: AdvisorNotAttachedReason): reason is AdvisorToldReason;
/** The `advisor.not_attached` notice for a reason the person is told about. */
export declare function advisorNotAttachedNotice(args: {
reason: AdvisorToldReason;
advisorModel: string;
mainModel: string;
sessionId: string;
runId?: string;
}): EngineNotice;
export {};