@sema-agent/core
Version:
Stateless, task-oriented AI agent core
83 lines (82 loc) • 5.96 kB
TypeScript
/**
* A leg's lifecycle stations over its bus, all observations (an answer changes nothing, a failure skips the handler). Each
* entry reads first, synchronously, whether its event (or the classic event raised in its bottom) is heard — none ⇒ nothing
* is raised and nothing is awaited.
* `session.start`: the leg opened (prepared, before its first model request); `e = { cwd, sessionId, agentId? }`; its bottom
* raises `classic.SessionStart` with the same `e` and comes up as `{ cwd }`.
* `turn.start`: a model turn began, before its first model request; `e = { turn, agentId? }` (`turn` = 1 for the leg's first);
* the bottom comes up as `{ turn }`.
* `turn.complete`: the turn's end records it ({@link noteCompletedTurn}); the next await point raises it once
* ({@link raiseCompletedTurn}) — the turn boundary after it, or the leg's settle for a turn whose boundary never ran.
* `e = { answer, reason, turn, isAborted, agentId?, usage? }`; the bottom comes up as `{ text: answer, usage? }`.
* `session.measure`: a turn boundary measured the leg's context occupancy and it moved since the last measurement
* ({@link measureSession} — the one write `$.session.usage` reads); `e = { context, changed, agentId? }`; the bottom comes up as
* `{ changed }`.
* `session.end`: a leg's terminal settled; `e = { reason, sessionId, agentId? }`, `reason` the terminal status word; its bottom
* raises `classic.SessionEnd` with the same `e` and comes up as `{ sessionId }`.
*/
import { type AssistantMessage } from "../../internal/llm.js";
import type { maybeCompact } from "../auto-compaction.js";
import type { HookInvocationIdentity } from "../hooks.js";
import { type LegModBus } from "./stations.js";
/** Raise `session.start` as the leg opens; `undefined` (nothing raised, nothing to await) when neither it nor
* `classic.SessionStart` is heard. */
export declare function raiseSessionStart(bus: LegModBus, identity: HookInvocationIdentity | undefined, start: {
readonly cwd: string;
readonly sessionId: string;
}, signal: AbortSignal | undefined): Promise<void> | undefined;
/** Raise `turn.start` for the turn beginning (`turn` = its number); `undefined` when no handler hangs on it. */
export declare function raiseStartedTurn(bus: LegModBus, identity: HookInvocationIdentity | undefined, turn: number, signal: AbortSignal | undefined): Promise<void> | undefined;
/** A leg's context occupancy as a turn boundary measured it: the compaction pass's own `contextUsage` — the three numbers the
* boundary's `context_usage` frame carries. */
export type ContextOccupancy = Readonly<NonNullable<Awaited<ReturnType<typeof maybeCompact>>["contextUsage"]>>;
/** What `session.measure`'s `changed` names: the units that moved (the leg measures one). */
export declare const SESSION_MEASURE_UNITS: readonly ["context"];
export type SessionMeasureUnit = (typeof SESSION_MEASURE_UNITS)[number];
/** Where a leg keeps its latest measurement: written once per boundary by {@link measureSession}, read by `$.session.usage`. */
export interface SessionMeasureSeat {
context?: ContextOccupancy;
}
/** Record a boundary's measurement on the seat (always), then raise `session.measure` when the occupancy moved since the
* last one (the first measurement moved from none); `undefined` when nothing is raised. */
export declare function measureSession(bus: LegModBus, seat: SessionMeasureSeat, measured: ContextOccupancy, identity: HookInvocationIdentity | undefined, signal: AbortSignal | undefined): Promise<void> | undefined;
/** Why a model turn ended. */
export type CompletedTurnReason = "answer" | "aborted" | "error";
/** A model turn that ended, as `turn.complete` names it (raised once, then gone). */
export interface CompletedTurn {
readonly answer: string;
readonly reason: CompletedTurnReason;
/** The leg's turn count at the turn's end (1 = its first turn). */
readonly turn: number;
readonly usage?: {
readonly inputTokens: number;
readonly outputTokens: number;
readonly cacheReadTokens: number;
readonly cacheWriteTokens: number;
};
/** The extension whose `prompt.submit` `{ reply }` answered the turn in the model's place (`usage` then absent). */
readonly answeredBy?: string;
}
/** Where the leg keeps the turns it has not raised yet, in order (a turn can end with no boundary after it — a recovery
* re-prompt, a failure's own `turn_end` — so the next await point may find more than one). */
export interface CompletedTurnSeat {
completed?: readonly CompletedTurn[];
}
/** Record the turn that just ended for `turn.complete` (nothing when no handler hangs on it); `aborted` = the leg's cancel was
* already in force when the turn ended, `usage` absent when none was reported — and on a turn an extension answered, which
* got no response to measure. */
export declare function noteCompletedTurn(bus: LegModBus, seat: CompletedTurnSeat, message: AssistantMessage | undefined, turn: number, aborted: boolean, usage: CompletedTurn["usage"] | undefined): void;
/** Raise `turn.complete` for each turn the seat holds, in order, each once (the seat is emptied first). An engine-side
* rejection is swallowed: an observation never changes what it observes. */
export declare function raiseCompletedTurn(bus: LegModBus, seat: CompletedTurnSeat, identity: HookInvocationIdentity | undefined, signal: AbortSignal | undefined): Promise<void>;
/**
* The leg's terminal: its bus's lifetime ends (its timers stop; a `$` call made after its handler settled is refused from
* here), then `session.end` is raised; `undefined` when neither it nor `classic.SessionEnd` is heard.
*/
export declare function raiseSessionEnd(leg: {
readonly modBus: LegModBus;
readonly endModLife: () => void;
}, identity: HookInvocationIdentity | undefined, end: {
readonly reason: string;
readonly sessionId: string;
}): Promise<void> | undefined;