UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

121 lines (120 loc) • 8.42 kB
/** * The `session.append` station, and the one seat every conversation row passes on its way in ({@link RowSeat}). * * THE RULE. A row enters the conversation at one point: its first commit point, before the live context reads it again and * before the store keeps it. Every writer calls the seat there, once — the harness for the rows it commits (a row the loop * ends, the record of an API failure, a row handed to its port), every direct writer of the leg through * `appendAdmittedRow` (`row-admission.ts`). There a user row that does not yet say what it is is stamped with its origin * (`stampUserRowOrigin`, `user-row-origin.ts`: the one place every engine row gets the member the store keeps). With a handler on * `session.append` the seat raises the event once per row; with none, or before the leg's bus is bound, the row passes as made and * nothing is raised. * * `e = { door, origin, agentId?, message }` plus the identity envelope; `door`, `origin` and `agentId` are pinned. `door` and * `origin` are read off the row's own typed facts ({@link doorOf}, {@link originOf}) — no writer names them; an extension's * own note (raised through `$.session.append`) is door `note`, origin `{ kind: "plugin", name }`. `message` is a frozen data * view of the row: its role, the members that name it, and its content. * * A handler answers: * - `next(e)` — the row is kept as made; what comes up is `{ message }`, the row as kept; * - `next({ ...e, message })` — the row is kept with its content rewritten: only text changes (a text block's text, or a * string content); every other block stays, the same blocks in the same order, and every member beside `content` is * pinned. The rewrite is made on the row itself, in place — the same object, so every account kept by identity (the * live context, a frame's settlement, the prompt's receipt) settles on it — and the row records who rewrote it * (`rewrittenBy`). A rewrite that changes a pinned part, or of a row whose content another reader holds by its exact text * (a row the engine composed its frames into, an attachment's row), is not kept: the row is kept as made, and the leg's * report sink says so (the reference's form: `appendCoreOf` (cli309.js:296238)); * - `{ deny: <reason> }` — only before `next`, only of an extension's own note: the note is not kept. * The answer is the row as kept: what `next` answered, or a copy of it. Anything else is a violation (the reference's * sentences: `Nu` (cli309.js:255554)). A handler may not change `message.role`, nor the members that name the row. * * A failure skips the handler (the table's column): before `next`, what is below it runs in its place, so the row is kept as * made; after it, the row as kept stands. No row is dropped and none is kept twice: an engine row cannot be refused, an * extension's own note only by a deny before `next` (a note whose handler failed is kept — the reference refuses it: * `ee` (cli309.js:256939)). An engine fault of the dispatch keeps the row as made and is said on the report sink. * * `$.session.append({ message: { role: "user", content }, agentId? })` is the event called ({@link createSessionAppendEntry}): * the leg's harness takes the note through its own port, the seat raises it here under the caller's frames and origin, and * the call answers `{ message }` (the note as admitted) or `{ deny: <reason> }` (returned, not thrown): the note is in the * conversation the leg goes on with — stored by the port (`AgentHarness.appendMessage`), or carried by a durable park's row * (`heldRows`); once the park is taken, or may have been, the call throws `$.session.append: refused: …`. */ import type { AgentMessage, RowAdmission, RowRaising, SessionTreeEntry } from "../../internal/harness-types.js"; import type { AgentHarness } from "../../internal/harness.js"; import { type FrameSettlement } from "../frame-settlement.js"; import type { HookInvocationIdentity } from "../hooks.js"; import type { ModBottomContext, ModCallEntry } from "./kernel/index.js"; import { type LegModBus } from "./stations.js"; /** Where a row comes in: read off the row ({@link doorOf}); an extension's own note is `note`. The doors the leg's commit * points produce — a compaction's summary is kept as its own entry, not a conversation row, and the engine writes no * command row. */ export declare const SESSION_APPEND_DOORS: readonly ["prompt", "note", "delivery", "attachment", "response", "tool-result"]; export type SessionAppendDoor = (typeof SESSION_APPEND_DOORS)[number]; /** Who made a row: read off the row ({@link originOf}); an extension's own note is `plugin`. */ export declare const SESSION_APPEND_ORIGIN_KINDS: readonly ["host", "engine", "model", "tool", "plugin"]; export type SessionAppendOrigin = { readonly kind: "host"; } | { readonly kind: "engine"; } | { readonly kind: "model"; readonly model: string; } | { readonly kind: "tool"; readonly tool: string; } | { readonly kind: "plugin"; readonly name: string; }; /** A row's door, from its typed facts: a person's (or the host's) input is a `prompt`; what the engine adds for the model to * read is a `note` (a user-lane note, or a row of its own kind), a `delivery` (a frame with its delivery key) or an * `attachment`; the model's turn is a `response`, a tool's answer a `tool-result`. */ export declare function doorOf(row: AgentMessage): SessionAppendDoor; /** `$.session.turns()`: how many prompts a branch holds from its people — its message rows whose door ({@link doorOf}) is * `prompt`. A steer is the engine's note on the person's behalf (`provenance: "engine-note"`), so it is not one. */ export declare function promptCountOf(branch: ReadonlyArray<SessionTreeEntry>): number; /** A row's maker, from its typed facts: the model (or the extension that answered the turn in its place), the tool that * answered, the host (a prompt, an attachment the host declared), else the engine. */ export declare function originOf(row: AgentMessage): SessionAppendOrigin; /** What the leg binds the seat to once its bus is admitted. */ export interface RowSeatBinding { readonly bus: LegModBus; readonly identity: HookInvocationIdentity | undefined; /** The leg's report sink (handler failures and the seat's own disclosures). */ readonly report: (err: Error) => void; /** The leg's settlement seam: a tool result an extension rewrote is a host-side decision of what the model reads * (`noteHostRewrite`), so a frame the rewrite took out is withheld by the host side, never owed again. */ readonly frameSettlement?: FrameSettlement; } /** What became of an extension's own note: kept (the answer its call returns), refused by a deny, or cancelled with its call. */ export type RaisedRowOutcome = { readonly kind: "kept"; readonly answer: object; } | { readonly kind: "denied"; readonly reason: string; } | { readonly kind: "cancelled"; }; /** The leg's one admission seat. */ export interface RowSeat { /** What every writer calls at a row's first commit point (`AgentHarnessOptions.admitRows`). */ readonly admitRows: RowAdmission; /** Bind the leg's bus. Until then every row passes as made. */ bind(binding: RowSeatBinding): void; /** The token an extension's own note is handed to the harness with: its dispatch runs under `ctx` (the `$` call's frames, * origin and signal). Known by identity; a token this seat did not mint is no raising. */ raise(ctx: ModBottomContext): RowRaising; /** What became of the note a token was handed with — once the seat has admitted it. */ outcomeOf(raising: RowRaising): RaisedRowOutcome | undefined; } /** A leg's admission seat (see the module header). */ export declare function createRowSeat(): RowSeat; /** What the host entry reaches: the leg's seat, its harness's port, the leg's `agentId` (a delegated leg's), its clock. */ export interface SessionAppendEntryInput { readonly seat: RowSeat; readonly harness: Pick<AgentHarness, "appendMessage">; readonly agentId: string | undefined; readonly now: () => number; } /** `$.session.append`'s host entry: an extension's own note, through the harness's port and this station (the module header). */ export declare function createSessionAppendEntry(input: SessionAppendEntryInput): ModCallEntry;