/** * AHP `StateAction` → PowerLine `AgentEvent` reverse mapper (AHP HR8d / #1336). * * The inverse of {@link mapAgentEvent}. Consumes AHP action envelopes as they * arrive on the wire and synthesizes the `AgentEventFields` shape that * downstream Grackle consumers (`event-processor.ts`, JSONL writer, * lifecycle handlers) expect. * * Lives in `@grackle-ai/common` next to `ahp-mapper.ts` because both mappers * are pure functions over the AHP action vocabulary and the AgentEvent shape. * Used by `AhpHostTransport` (in `@grackle-ai/adapter-sdk`) on every * inbound `action` notification. * * Tool calls produce a coalesced single `tool_use` AgentEvent only after * BOTH `SessionToolCallStart` and `SessionToolCallReady` have arrived * (the forward mapper emits them as a pair). Other actions produce zero or * one event each. * * @module ahp-reverse-mapper */ import { type ActionEnvelope } from "@grackle-ai/ahp"; import type { AgentEventFields } from "./ahp-mapper.js"; /** * Pending half of a tool call (waiting for the matching `SessionToolCallReady` * to complete the pair before emitting one `tool_use` event). Exported so * the public `ReverseMapperContext` type doesn't leak an unexported symbol. */ export interface PendingToolCall { readonly turnId: string; readonly toolName: string; readonly displayName: string; } /** * Per-session context maintained by `AhpHostTransport`. Tracks the half * of a tool-call pair that has arrived (and is awaiting its mate) plus the * meta accumulator that lets cost/runtime-session-id ride alongside the * action stream via `SessionMetaChangedAction`. */ export interface ReverseMapperContext { /** ID of the currently active turn, or `undefined` between turns. */ turnId?: string; /** Pending `SessionToolCallStart` envelopes awaiting their `SessionToolCallReady`, keyed by toolCallId. */ readonly pendingToolCalls: Map; /** Accumulated meta carried from `SessionMetaChangedAction` envelopes. */ readonly metaAccumulator: { costMillicents?: number; /** Last-seen total — used to compute deltas for the rehydrated `usage` event. HR8d follow-up #1355. */ inputTokens?: number; /** Last-seen total — used to compute deltas for the rehydrated `usage` event. HR8d follow-up #1355. */ outputTokens?: number; runtimeSessionId?: string; }; } /** * Construct a fresh per-session reverse-mapper context. * * @returns A new {@link ReverseMapperContext} with empty state. */ export declare function newReverseMapperContext(): ReverseMapperContext; /** * Result of reverse-mapping a single `ActionEnvelope`. * * Zero events: the action was buffered (e.g. `SessionToolCallStart` waiting * for its `Ready`) or has no AgentEvent representation (e.g. a root-channel * action arriving on a session subscription). * * One event: the typical case. * * Two events: errors emit both `error` and `status: failed` so the * lifecycle-handling code in `event-processor.ts` (which keys off * `status: failed` for end-reason) still fires. */ export interface ReverseMapResult { /** Synthesized AgentEventFields, in emission order. May be empty. */ readonly events: AgentEventFields[]; /** Brief disposition note (for diagnostics; never affects behavior). */ readonly disposition: "mapped" | "carried" | "dropped" | "buffered"; /** Detail string accompanying the disposition. */ readonly detail: string; } /** * Reverse-map one AHP action envelope into 0..N PowerLine AgentEvents. * * @param envelope - The inbound `ActionEnvelope` carrying the AHP action. * @param context - Per-session context (mutated to track turn state and * pending tool-call pairs). * @returns A {@link ReverseMapResult} with the synthesized events. */ export declare function reverseMapAction(envelope: ActionEnvelope, context: ReverseMapperContext): ReverseMapResult; //# sourceMappingURL=ahp-reverse-mapper.d.ts.map