import { DateTime } from 'luxon'; import type { Schema } from '@nhtio/validation'; /** * Plain input object supplied to {@link TurnGate} at construction time. * * @remarks * `turnId` and `abortSignal` are injected by the runner — callers constructing a gate via * `ctx.waitFor()` never supply them directly. * * `abortSignal` is `AbortSignal` (not `AbortController`) because the gate reacts to turn-level * cancellation but cannot trigger it. The gate owns its own internal `AbortController` for * `gate.abort()`. */ export interface RawTurnGate { /** Stable unique identifier for this gate. */ id: string; /** The ID of the turn that opened this gate. */ turnId: string; /** Human-readable label describing why this gate was opened (e.g. `'tool_approval'`). */ reason: string; /** Arbitrary data supplied to the gate opener; passed through to `turnGateOpen` listeners. */ payload: unknown; /** Optional validator schema for the resolution value. When present, `resolve()` validates before settling. */ schema?: Schema; /** Optional timeout in milliseconds. When elapsed the gate self-rejects with {@link @nhtio/adk!E_TURN_GATE_TIMEOUT}. */ timeout?: number; /** The turn's abort signal. When fired the gate self-rejects with {@link @nhtio/adk!E_TURN_GATE_ABORTED}. */ abortSignal?: AbortSignal; /** When this gate was created. */ createdAt: string | number | Date | DateTime; } /** * A cooperative suspension gate that blocks a turn's middleware pipeline until resolved, rejected, * aborted, or timed out. * * @typeParam T - The expected type of the resolution value. * * @remarks * Created exclusively via `ctx.waitFor()` — middleware never constructs a gate directly. * The gate emits `turnGateOpen` on the runner's observability bus at creation time and * `turnGateClosed` when it settles. * * Resolution is validated against an optional schema before the internal promise is settled. * A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} **synchronously in the * caller's context** — the promise is NOT settled and the gate remains open. */ export declare class TurnGate { #private; /** * Validator schema that accepts a {@link RawTurnGate} object. * * @remarks * Reusable fragment for any schema that needs to validate or nest a gate entry. */ static schema: import("@nhtio/validation").ObjectSchema; /** * Returns `true` if `value` is a {@link TurnGate} instance. * * @remarks * Uses {@link @nhtio/adk!isInstanceOf} for cross-realm safety. * * @param value - The value to test. * @returns `true` when `value` is a {@link TurnGate} instance. */ static isTurnGate(value: unknown): value is TurnGate; /** Unique identifier for this gate instance. */ readonly id: string; /** Id of the turn this gate belongs to. */ readonly turnId: string; /** Human-readable reason the gate was opened. */ readonly reason: string; /** Optional caller-supplied payload describing what the gate is waiting on. */ readonly payload: unknown; /** When the gate was created. */ readonly createdAt: DateTime; /** Whether the gate has been settled (resolved or rejected) and no longer blocks the turn. */ readonly isSettled: boolean; /** * @param raw - The raw gate input validated against `rawTurnGateSchema`. * @throws {@link @nhtio/adk!E_INVALID_INITIAL_TURN_GATE_VALUE} when `raw` does not satisfy the schema. */ constructor(raw: RawTurnGate); /** * Resolves the gate with `value`, unblocking the awaiting middleware. * * @remarks * If a schema was provided at construction, `value` is validated synchronously before the * promise is settled. A validation failure throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} * in the caller's context — the promise is NOT settled and the gate remains open. * * No-ops if the gate is already settled. * * @param value - The resolution value. Must satisfy the gate's schema when one was provided. * @throws {@link @nhtio/adk!E_INVALID_TURN_GATE_RESOLUTION} when `value` fails schema validation. */ resolve(value: unknown): void; /** * Rejects the gate with `error`, unblocking the awaiting middleware with a rejection. * * @remarks * No-ops if the gate is already settled. * * @param error - The rejection reason. */ reject(error: Error): void; /** * Aborts the gate by firing the internal `AbortController`, which rejects the promise with * {@link @nhtio/adk!E_TURN_GATE_ABORTED}. * * @remarks * No-ops if the gate is already settled. Distinct from the turn-level abort signal — this * allows callers to cancel a specific gate without aborting the whole turn. */ abort(): void; }