import type { ReplyEffectStatus } from "../types/components.js"; import type { Memory } from "../types/memory.js"; import type { JSONSchema } from "../types/model.js"; import type { IAgentRuntime } from "../types/runtime.js"; import type { State } from "../types/state.js"; /** * Field evaluators own the schema, prompt contribution, and handler for each structured * response field. All fields remain required for stable schema bytes; shouldRun controls * prompt and handler activation, with empty defaults for inactive fields. Every active field * is parsed first; handlers then run in priority order, mutate the shared result, and may * abort further processing. Sibling parsed values remain read-only. */ /** * Sender-role classification piped through Stage 1. Re-exported here as a * narrow type so this module does not depend on agent/role internals. */ export type ResponseHandlerSenderRole = "OWNER" | "ADMIN" | "USER" | "GUEST" | "SYSTEM" | "SELF"; /** * The flat, all-required result of one Stage-1 LLM call. * * Field ownership: * shouldRespond - core * contexts - core * intents - core * candidateActionNames - core * replyText - core * facts - core (memory pipeline) * relationships - core (memory pipeline) * addressedTo - core (memory pipeline) * threadOps - app-lifeops (includes abort) * - registered by plugins * * The type is open at the top level (other plugins can contribute arbitrary * additional fields) but is keyed by `string` so all paths through the * pipeline treat unknown fields safely. */ export interface ResponseHandlerResult { shouldRespond: "RESPOND" | "IGNORE" | "STOP"; contexts: string[]; intents: string[]; candidateActionNames: string[]; replyText: string; replyEffectStatus?: ReplyEffectStatus; facts: string[]; relationships: Array<{ subject: string; predicate: string; object: string; }>; addressedTo: string[]; [extra: string]: unknown; } /** * Context passed to `shouldRun` and `handle`. Read-only view of the runtime * state plus the parsed result so far. Handlers mutate via the returned * `ResponseHandlerFieldEffect`, not by writing to `ctx`. */ export interface ResponseHandlerFieldContext { readonly runtime: IAgentRuntime; readonly message: Memory; readonly state: State; readonly senderRole: ResponseHandlerSenderRole; /** * Turn-scoped AbortSignal. Field handlers should respect it — once a * sibling handler preempts (e.g., abort), this signal fires and any * still-running handler should exit cleanly. */ readonly turnSignal: AbortSignal; } /** * Extended context only available during `handle`. Includes the parsed value * for THIS field plus the full parsed object for sibling-reads. */ export interface ResponseHandlerFieldHandleContext extends ResponseHandlerFieldContext { readonly value: TValue; readonly parsed: Readonly; } /** * What a handler can affect: * * - `mutateResult(result)` — patch the running ResponseHandlerResult. * Use sparingly; prefer letting downstream consumers read the parsed * value directly. * - `preempt: {reason}` — stop processing remaining handlers and route to * a terminal outcome. Used by abort (skip planner, skip reply send) and * by IGNORE/STOP equivalents. * - `debug` — strings recorded into the trace for observability. */ export interface ResponseHandlerFieldEffect { mutateResult?: (result: ResponseHandlerResult) => void; preempt?: { /** * What to do instead of the default route-to-planner / send-reply flow. * * - "ack-and-stop": agent emits a short ack reply and stops (used by * abort, where the abort handler has already shut down in-flight work). * - "ignore": agent emits nothing. * - "direct-reply": agent uses the current `replyText` as the final reply. */ mode: "ack-and-stop" | "ignore" | "direct-reply"; reason: string; }; debug?: string[]; } /** * A ResponseHandlerFieldEvaluator owns one top-level property of the * Stage-1 LLM's structured output. See the file header for the registration * lifecycle. * * @typeParam TValue - the parsed type for this field (matches `schema`) */ export interface ResponseHandlerFieldEvaluator { /** * The JSON property name. Becomes a top-level key on * `ResponseHandlerResult` and a field name in the composed schema. * Must be unique across all registered evaluators. */ name: string; /** * Human-readable description AND the natural-language prompt slice. This * string is included verbatim in the system prompt to tell the LLM what * this field is for and when to populate it. Should be 1-4 short * sentences. Per the user directive: "the context names and the full * descriptions must be in the prompt." */ description: string; /** Short description for stored metadata; prompt rendering uses description. */ descriptionCompressed?: string; /** * Execution order. Lower runs first. Defaults to 100. Conventions: * * 0-19 - core routing fields (shouldRespond, contexts) and plugin * routing fields that declare `directReplyValue` (threadOps, calendar, * etc.), generated before the reply * 20 - replyText * 20-49 - other plugin-contributed action surfaces * 50-79 - retrieval hints * 80-99 - extract/memory pipeline (facts, relationships, addressedTo) */ priority?: number; /** * The values of this field that keep the turn a direct answer. Declaring * it promises that, whenever it returns true for the model's raw value, * this field's parse and handle neither reroute the turn (contexts, * intents, action candidates, planner, preempt) nor change `replyText`. * A host that streams `replyText` while it is generated may then start * the stream once this field was written before it (priority below * `replyText`'s) with such a value. A field without it, or written after * `replyText`, holds that stream: its value is not known in time. */ directReplyValue?: (value: unknown) => boolean; /** * JSON schema fragment for THIS field. Must declare a deterministic * "empty" value (empty array, empty string, "IGNORE", etc.) so the LLM * can emit it when the field is N/A. Schema must support OpenAI strict * mode: no required-but-undefined, no `additionalProperties: true` * unless intentional. * * Parameter `description` strings within the schema ARE shown to the LLM * (they are part of the strict schema sent to OpenAI / Anthropic). Use * them to document subfields. */ schema: JSONSchema; /** * Per-turn activation gate. * * - When `true` (default if omitted): the evaluator's prompt slice is * included in the system prompt; the LLM is instructed to populate * the field. The field's handler runs after parse. * - When `false`: the prompt slice is omitted (no instruction to * populate); after parse, the handler is skipped. The field stays * declared in the schema for cache stability — the LLM emits the * declared empty value. * * Must be cheap. Avoid LLM calls or heavy I/O. Database lookups acceptable * if cached. */ shouldRun?(ctx: ResponseHandlerFieldContext): boolean | Promise; /** Request-scoped facts for this field, rendered outside the stable prompt/schema. * Runs only for active fields. Must not execute effects or invoke a model. */ getContext?(ctx: ResponseHandlerFieldContext): string | Promise; /** * Parse / validate the LLM's value for this field. Default: identity. * * Two failure modes (lifted from BAML's @check vs @assert): * * - Return `null` — soft fail. The field is treated as empty; the * evaluator's handler is skipped. Logged for observability. Other * fields still process. * - Throw — hard fail. The whole Stage-1 call surfaces an error to the * caller. Use this for invariants you absolutely cannot proceed past * (e.g., schema parse succeeded but the value references a forbidden * resource). */ parse?(value: unknown, ctx: ResponseHandlerFieldContext): TValue | null; /** * Run the field's effect. Called once per turn (if `shouldRun` was * truthy and `parse` did not soft-fail) with the parsed value for this * field and a read-only view of all sibling fields. * * Return a `ResponseHandlerFieldEffect` to mutate the result or preempt * the downstream routing. Return `undefined` to leave routing unchanged. */ handle?(ctx: ResponseHandlerFieldHandleContext): ResponseHandlerFieldEffect | undefined | Promise; } export interface ResponseHandlerFieldTrace { fieldName: string; active: boolean; parsed: boolean; parseOutcome: "ok" | "soft-fail" | "hard-fail" | "skipped"; handled: boolean; preempted: boolean; preemptMode?: "ack-and-stop" | "ignore" | "direct-reply"; preemptReason?: string; debug?: string[]; errorMessage?: string; } export interface ResponseHandlerFieldRunResult { parsed: ResponseHandlerResult; traces: ResponseHandlerFieldTrace[]; preempt?: { mode: "ack-and-stop" | "ignore" | "direct-reply"; reason: string; }; fieldErrors: Record; } /** * Owns the registered set of field evaluators and provides the composition * primitives (schema, prompt, dispatch) used by the Stage-1 response handler. * The registry de-dupes by `name` (first-wins, matches runtime.registerAction) * and throws when a registration would violate strict-schema rules. */ export declare class ResponseHandlerFieldRegistry { private evaluators; private cachedSchema; private cachedSchemaSignature; private cachedSorted; register(evaluator: ResponseHandlerFieldEvaluator): void; unregister(name: string): boolean; private invalidateCaches; list(options?: ResponseHandlerFieldSelectionOptions): ReadonlyArray; size(): number; /** * Build the composed HANDLE_RESPONSE schema. Cached across calls; the * cache invalidates only when registrations change. The schema is the * same bytes every turn, which is what keeps Anthropic / OpenAI prompt * caches warm. * * All fields are REQUIRED (per the user directive). The LLM emits the * declared empty value for fields that don't apply this turn. * * Canonical-source note: this is the schema the Stage-1 LLM actually * receives in production — `services/message.ts` passes it to * `createHandleResponseTool({ parameters:... })`, and `buildResponseGrammar` * (`./response-grammar.ts`) composes the GBNF skeleton from the same * registered field set. The static `HANDLE_RESPONSE_SCHEMA` in * `../actions/to-tool.ts` mirrors the builtin shape for older callers that * build the tool without passing an explicit registry-composed schema. */ composeSchema(options?: ResponseHandlerFieldSelectionOptions): JSONSchema; /** * Hash-like signature of the composed schema. Used by the cache plan to * detect "schema changed → invalidate prompt cache" situations. Stable * across boots as long as the registered set is the same. */ composeSchemaSignature(options?: ResponseHandlerFieldSelectionOptions): string; /** * Compose the per-turn system-prompt slices. Each active evaluator * contributes its complete `description` verbatim. The composition is one big * markdown block of `### {name}\n{description}` sections in priority * order — matching how the post-turn EvaluatorService composes its prompt * at services/evaluator.ts:327-333. * * Request-scoped field facts are returned separately as context; callers * must render them in dynamic input, not the stable instructions or schema. * Also returns active field names for the trace. */ composePromptSlices(ctx: ResponseHandlerFieldContext, options?: ResponseHandlerFieldSelectionOptions): Promise<{ rendered: string; context: string; activeFieldNames: string[]; skippedFieldNames: string[]; }>; /** * Parse the LLM's structured output, then dispatch each field's slice to * its handler in priority order. Every active field is parsed before any * handler runs, so a handler sees all siblings in canonical form and a * later field's parse never overwrites its mutation: a field generated * before `replyText` can still replace the reply. Handlers may preempt * downstream processing (abort, ack-and-stop, ignore, direct-reply). * * Active set is recomputed here (we don't trust the prompt-slice run to * tell us — the prompt is rendered into stable cache and may be reused * across turns where shouldRun returned different values). */ dispatch(args: { rawParsed: Record; runtime: IAgentRuntime; message: Memory; state: State; senderRole: ResponseHandlerSenderRole; turnSignal: AbortSignal; }): Promise; private sortedEvaluators; } export interface ResponseHandlerFieldSelectionOptions { includeFieldNames?: ReadonlySet | readonly string[]; }