import { BackoffPolicy } from "./reconnect.js"; import { TransportAuth, TransportKind } from "./transport/types.js"; import "./transport/index.js"; import { ServerEventFrame } from "@graphorin/protocol"; //#region src/graphorin-client.d.ts /** * Discriminator for the subscription target. Mirrors the strict * subject grammar enforced by the server: * - `'session'`/`` ⇒ `'session:/events'` * - `'agent'`/`` + `runId` ⇒ `'agent:/runs//events'` * - `'run'`/`` ⇒ `'session:/runs//events'` * (when `sessionId` is provided) * - `'workflow'`/`` ⇒ `'workflow:/events'`, or * `'workflow:/runs//events'` when the optional `runId` * is present (the run-scoped subject advertised by the workflow * execute/resume routes) * * @stable */ type SubscriptionTarget = { readonly target: 'session'; readonly id: string; } | { readonly target: 'agent'; readonly id: string; readonly runId: string; } | { readonly target: 'run'; readonly runId: string; readonly sessionId?: string; } | { readonly target: 'workflow'; readonly id: string; /** * The server emits workflow run events ONLY on the * run-scoped subject (`workflow:/runs//events`) * advertised by `POST /v1/workflows/:id/execute` (and resume), * never on the base `workflow:/events` subject - pass the * advertised `runId` to receive them. */ readonly runId?: string; }; /** * Transport selector. `'auto'` (default) attempts a WebSocket * handshake first and falls back to SSE on failure. * * @stable */ type TransportPreference = TransportKind | 'auto'; /** * Public configuration accepted by {@link GraphorinClient}. * * @stable */ interface GraphorinClientOptions { /** * Session bound to the SSE fallback: substituted into the * `:sessionId` slot of `sseSessionPath`. Required to connect over * SSE - the old client sent the literal template and could never * receive an event. */ readonly sessionId?: string; /** * Server base URL. Examples: `'wss://graphorin.example.com'` or * `'http://localhost:8080'`. The path `/v1/ws` is appended for * the WebSocket transport. */ readonly baseUrl: string; readonly auth: TransportAuth; /** * Transport selection. Default `'auto'`: WebSocket first, SSE * fallback. Caveat for `'auto'`: SSE carries only the bound * session subject, so when a RECONNECT falls back from WS to SSE, * live WS subscriptions (agent / workflow subjects) cannot be * resumed - they are closed with a `TransportFailedError` (their * `for await` consumers reject immediately instead of hanging). * Force `'ws'` when your application depends on those subscriptions * surviving reconnects. */ readonly transport?: TransportPreference; /** * Per-subscription buffer cap. When the `for await` consumer * falls behind and the buffered queue reaches this many frames, the * subscription closes with a typed `flow-overflow` error (mirroring * the server's queue-overflow close) instead of growing the heap * without bound or silently dropping events. Default 10000 (10x the * server's default per-connection limit); `0` disables the cap and * restores the old unbounded behavior. */ readonly subscriptionQueueLimit?: number; /** Override the WS path (default `'/v1/ws'`). */ readonly wsPath?: string; /** * SSE path template. The placeholder `:sessionId` is replaced at * subscribe time. Default: `'/v1/sessions/:sessionId/events'`. * Required when the transport is `'sse'` or when the WS handshake * fails on `'auto'`. */ readonly sseSessionPath?: string; readonly reconnect?: BackoffPolicy; /** Inject a `WebSocket` constructor (Node SDKs / tests). */ readonly WebSocket?: typeof WebSocket; /** Inject an `EventSource` constructor (Node SDKs / tests). */ readonly EventSource?: typeof EventSource; /** Inject a `fetch` implementation (defaults to `globalThis.fetch`). */ readonly fetch?: typeof fetch; /** Optional client identifier surfaced on diagnostics. */ readonly clientId?: string; /** * Per-RPC reply timeout in milliseconds. When set (and > 0), an RPC * that receives no matching reply within this window rejects with a * {@link TransportFailedError} instead of hanging forever on a * non-responsive server. Default: unset - no timeout, so a legitimately * slow server reply is never aborted (opt-in). */ readonly rpcTimeoutMs?: number; } /** * Snapshot returned by {@link Subscription.metadata}. * * @stable */ interface SubscriptionMetadata { readonly id: string; readonly subject: string; readonly target: SubscriptionTarget; readonly snapshotEventId: string | undefined; readonly lastEventId: string | undefined; readonly closed: boolean; /** Current buffered (undelivered) event-frame count. */ readonly queuedEvents: number; } /** * Public surface returned by {@link GraphorinClient.subscribe}. * * @stable */ interface Subscription { readonly subscriptionId: string; readonly subject: string; events(): AsyncIterable; /** * Close the subscription on the server. Idempotent. */ unsubscribe(): Promise; metadata(): SubscriptionMetadata; } /** * @stable */ declare class GraphorinClient { #private; constructor(options: GraphorinClientOptions); /** * Open the underlying transport. Resolves once the server has * accepted the handshake (`'open'`); rejects with a typed * {@link GraphorinClientError} otherwise. * * Calling `connect()` while already connected is a no-op; calling * it during another `connect()` returns the same promise. */ connect(): Promise; /** Send a `ping` RPC and resolve when the server replies with `pong`. */ ping(): Promise; /** * Subscribe to a server-side event stream. Resolves with a * {@link Subscription} once the server confirms with the matching * `subscribed` frame; rejects when the server returns an * `error` instead. */ subscribe(target: SubscriptionTarget, opts?: { readonly sinceEventId?: string; }): Promise; /** * Cancel a server-side run. Sends the `run.cancel` RPC and * resolves with the server's `result` payload (typically * `{ cancelled: true, partialStateAvailable: true }`). */ cancel(runId: string, opts?: { readonly drain?: boolean; readonly reason?: string; readonly onPendingApprovals?: 'deny' | 'preserve'; }): Promise; /** * Resume a paused (HITL) run. The WebSocket protocol intentionally * does NOT carry a `resume` control message - resumes are durable * + idempotent + body-carrying, which maps onto the REST endpoint * `POST /v1/runs/:runId/resume`. The server resumes in-process * suspensions: pass `{ approvals: [{ toolCallId, granted }] }` as * the directive; a run whose RunState this server process does not * retain answers 409 (`run-state-unavailable`) - resume those * library-side via `agent.run(savedState, { directive })`. */ resume(runId: string, directive?: unknown, opts?: { readonly idempotencyKey?: string; }): Promise; /** * Send an MCP-compatible cancellation notification. Does not wait * for a server reply (notifications have no `id`). */ cancelNotify(requestId: string): void; /** * Disconnect the underlying transport and abort every pending RPC * + subscription. Idempotent. */ disconnect(): Promise; /** Return the active transport kind (or `undefined` if not connected). */ get transportKind(): TransportKind | undefined; } //#endregion export { GraphorinClient, GraphorinClientOptions, Subscription, SubscriptionMetadata, SubscriptionTarget, TransportPreference }; //# sourceMappingURL=graphorin-client.d.ts.map