/** * AHP-backed implementation of {@link IHostTransport} (AHP HR8d / #1336). * * Owns one `AhpClientSocket` per `PowerLineConnection` and routes * inbound `action` notifications to per-session queues via the * {@link reverseMapAction} reverse mapper. Downstream consumers in * `@grackle-ai/core` continue to read `envelope.event` (the synthesized * AgentEventFields), so no consumer code changes — only the wire format does. * * Wire-protocol summary (per #1336): * * - `createSession` (spawn) → AHP `createSession` + `subscribe` * - `createSession` (reanimate) → same, with `config.resumeFromRuntimeSessionId` * - `dispatchInput` → AHP `dispatchAction` notification with `SessionTurnStartedAction` * - `dispose` (kill) → AHP `disposeSession` * - `listSessions` → AHP `listSessions` * - `authenticate` → AHP `authenticate` * * Drain semantics (Option E): PowerLine replays parked events as `action` * notifications immediately after the `subscribe` response, so the consumer * receives them via the same stream — no separate `drainBuffered` RPC. * * @module ahp-host-transport */ import type { ContentEncoding, ResourceListResult, ResourceReadResult, AhpNotification } from "@grackle-ai/ahp"; import { AhpClientSocket } from "@grackle-ai/ahp-transport"; import type { AuthenticateParams, CreateSessionParams, CreateSessionResult, HostSessionInfo, IHostTransport, ReanimateParams, ResourceWatchListener, ResourceWatchOptions, ResourceWatchSubscription, ServerActionEnvelope } from "./host-transport.js"; /** * AHP-backed {@link IHostTransport} for a single environment. * * Lifecycle: one instance per `PowerLineConnection`. Owns one * `AhpClientSocket`; multiplexes session subscriptions over it. * * Construct via {@link createAhpHostTransport} (in `./connect.ts`) which * handles the `socket.open()` handshake. Tests can construct directly with * a pre-opened socket. */ export declare class AhpHostTransport implements IHostTransport { private readonly socket; private readonly sessions; /** * Active resource-watch listeners keyed by their `ahp-resource-watch:/` * channel. Populated by {@link AhpHostTransport.createResourceWatch}; consulted by * {@link AhpHostTransport.handleNotification} to route `resourceWatch/changed` action batches. */ private readonly resourceWatchers; private nextClientSeq; /** * Construct an AhpHostTransport over a pre-opened `AhpClientSocket`. * The socket MUST already have completed its `initialize` handshake. * * @param socket - The AHP client socket, opened and connected. */ constructor(socket: AhpClientSocket); /** * Notification handler that must be supplied to `AhpClientSocket` at * construction time (the socket's `onNotification` option). This is a * static-bound method so it can be passed to the socket constructor. * * Routes inbound `action` notifications to the appropriate per-session * queue, running the reverse mapper to synthesize `AgentEventFields` and * wrapping each into a `ServerActionEnvelope`. Other notification methods * are ignored (no consumers in the wire-only scope of HR8d). * * @param notif - The AHP notification from the wire. */ handleNotification(notif: AhpNotification): void; /** * Create a new session via AHP `createSession` + `subscribe`. * * Returns synchronously with the session URI and the live envelope stream. * The actual AHP requests fire in the background; failures push an error * sentinel into the stream and close it (mirroring how gRPC stream errors * surface during iteration). */ createSession(params: CreateSessionParams): CreateSessionResult; /** * Reanimate a suspended session. Maps to AHP `createSession` with a * `config.resumeFromRuntimeSessionId` hint that PowerLine interprets as * "spawn a continuation runtime from this prior runtime session." */ reanimate(params: ReanimateParams): AsyncIterable; /** Send input text by dispatching a `SessionTurnStartedAction`. */ dispatchInput(sessionUri: string, text: string): Promise; /** * Deliver runtime credentials. * * AHP `authenticate` is OAuth-shaped (single `{ resource, token }`) and * Grackle's HR6 authenticate delivers multiple typed tokens (env-var or * file-backed) for one provider. HR8d preserves the AHP-spec wire method * by fanning Grackle's tokens out into N `authenticate` calls. Each call: * * - `resource`: `grackle://provider/{provider}/{name}` — encodes the * Grackle-side identity. PowerLine recognizes the `grackle://` scheme * and decodes it. * - `token`: JSON-encoded `{ type, envVar?, filePath?, value }` — carries * the delivery instructions inside the AHP token field. * * On-spec method, abused field semantics. The contortion is documented in * #1336 and bounded to this single command. */ authenticate(params: AuthenticateParams): Promise; /** * Dispose a session via AHP `disposeSession`. * * AHP's `DisposeSessionParams` doesn't carry a `reason` field; the optional * `reason` argument is logged client-side but not delivered to PowerLine. * Acceptable for HR8d — `reason` was informational in the gRPC path too. */ dispose(sessionUri: string, _reason?: string): Promise; /** List active sessions via AHP `listSessions`. */ listSessions(): Promise; /** Read a file's content via AHP `resourceRead`. */ resourceRead(uri: string, encoding?: ContentEncoding): Promise; /** List a directory's entries via AHP `resourceList`. */ resourceList(uri: string): Promise; /** * Start a watch via AHP `createResourceWatch` then `subscribe` to the * returned watch channel. The listener is registered BEFORE `subscribe` so no * change batch can race ahead of it. The watcher uses `ignoreInitial`, so no * synthetic events fire for existing files at subscribe time. */ createResourceWatch(options: ResourceWatchOptions, onChange: ResourceWatchListener): Promise; private getOrCreateSession; private closeSession; private runSpawnFlow; private surfaceErrorAndClose; } /** * Helper to wire {@link AhpHostTransport}'s notification handler into an * `AhpClientSocket` at construction time. The transport's * {@link AhpHostTransport.handleNotification} method must be bound to the * socket BEFORE `socket.open()` so the first inbound `action` notifications * are routed correctly. * * @param transport - The transport whose handler should be bound. * @returns A function suitable for passing as `onNotification` to * `AhpClientSocket`'s constructor options. */ export declare function bindNotificationHandler(transport: AhpHostTransport): (n: AhpNotification) => void; //# sourceMappingURL=ahp-host-transport.d.ts.map