@anthropic-ai/claude-agent-sdk 0.2.79 → 0.2.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bridge.d.ts ADDED
@@ -0,0 +1,162 @@
1
+ /**
2
+ * API surface definition for @anthropic-ai/claude-agent-sdk/bridge.
3
+ *
4
+ * This file is the source of truth for the /bridge export's public types.
5
+ * It imports ONLY from agentSdkTypes.ts so the compiled .d.ts has exactly
6
+ * one import to rewrite (./agentSdkTypes → ./sdk) for the flat package layout.
7
+ *
8
+ * Compiled by scripts/build-ant-sdk-typings.sh; see build-agent-sdk.sh for the
9
+ * copy into the package. Runtime code is in agentSdkBridge.ts (separate file,
10
+ * bun-built to bridge.mjs).
11
+ *
12
+ * The two type definitions below are copied from src/bridge/sessionHandle.ts.
13
+ * Keep in sync — sessionHandle.ts is the implementation source of truth;
14
+ * this file exists to produce a clean .d.ts without walking the implementation
15
+ * import graph.
16
+ */
17
+ import type { SDKMessage, SDKControlRequest, SDKControlResponse, PermissionMode } from './agentSdkTypes.js';
18
+ /**
19
+ * Session state reported to the CCR /worker endpoint.
20
+ * @alpha
21
+ */
22
+ export type SessionState = 'idle' | 'running' | 'requires_action';
23
+ /**
24
+ * Per-session bridge transport handle.
25
+ *
26
+ * Auth is instance-scoped — the JWT lives in this handle's closure, not a
27
+ * process-wide env var, so multiple handles can coexist without stomping
28
+ * each other.
29
+ * @alpha
30
+ */
31
+ export type BridgeSessionHandle = {
32
+ readonly sessionId: string;
33
+ /**
34
+ * Live SSE event-stream high-water mark. Updates as the underlying
35
+ * transport receives frames. Persist this and pass back as
36
+ * `initialSequenceNum` on re-attach so the server resumes instead of
37
+ * replaying full history.
38
+ */
39
+ getSequenceNum(): number;
40
+ /** True once the write path (CCRClient initialize) is ready. */
41
+ isConnected(): boolean;
42
+ /** Write a single SDKMessage. `session_id` is injected automatically. */
43
+ write(msg: SDKMessage): void;
44
+ /** Signal turn boundary — claude.ai stops the "working" spinner. */
45
+ sendResult(): void;
46
+ /** Forward a permission request (`can_use_tool`) to claude.ai. */
47
+ sendControlRequest(req: SDKControlRequest): void;
48
+ /** Forward a permission response back through the bridge. */
49
+ sendControlResponse(res: SDKControlResponse): void;
50
+ /**
51
+ * Tell claude.ai to dismiss a pending permission prompt (e.g. caller
52
+ * aborted the turn locally before the user answered).
53
+ */
54
+ sendControlCancelRequest(requestId: string): void;
55
+ /**
56
+ * Swap the underlying transport in place with a fresh JWT (and epoch).
57
+ * Carries the SSE sequence number so the server resumes the stream.
58
+ * Call this when the poll loop re-dispatches work for the same session
59
+ * with a fresh secret (JWT is 4h; backend mints a new one every dispatch).
60
+ *
61
+ * Throws if `createV2ReplTransport` fails (registerWorker error, etc).
62
+ * Caller should treat that as a close and drop this handle.
63
+ */
64
+ reconnectTransport(opts: {
65
+ ingressToken: string;
66
+ apiBaseUrl: string;
67
+ /** Omit to call registerWorker; provide if the server already bumped. */
68
+ epoch?: number;
69
+ }): Promise<void>;
70
+ /**
71
+ * PUT /worker state. Multi-session workers: `running` on turn start,
72
+ * `requires_action` on permission prompt, `idle` on turn end. Daemon
73
+ * callers don't need this — user watches the REPL locally.
74
+ */
75
+ reportState(state: SessionState): void;
76
+ /** PUT /worker external_metadata (branch, dir shown on claude.ai). */
77
+ reportMetadata(metadata: Record<string, unknown>): void;
78
+ /**
79
+ * POST /worker/events/{id}/delivery. Populates CCR's processing_at /
80
+ * processed_at columns. `received` is auto-fired internally; this
81
+ * surfaces `processing` (turn start) and `processed` (turn end).
82
+ */
83
+ reportDelivery(eventId: string, status: 'processing' | 'processed'): void;
84
+ /** Drain the write queue. Call before close() when delivery matters. */
85
+ flush(): Promise<void>;
86
+ close(): void;
87
+ };
88
+ /** @alpha */
89
+ export type AttachBridgeSessionOptions = {
90
+ /**
91
+ * Session ID (`cse_*` form). Comes from `WorkResponse.data.id` in the
92
+ * poll-loop path, or from whatever created the session.
93
+ */
94
+ sessionId: string;
95
+ /** Worker JWT. Comes from `decodeWorkSecret(work.secret).session_ingress_token`. */
96
+ ingressToken: string;
97
+ /** `WorkSecret.api_base_url` or wherever the session ingress lives. */
98
+ apiBaseUrl: string;
99
+ /**
100
+ * Worker epoch if already known (e.g. from a `/bridge` call that bumps
101
+ * epoch server-side). Omit to have `createV2ReplTransport` call
102
+ * `registerWorker` itself — correct for poll-loop callers where the
103
+ * work secret doesn't carry epoch.
104
+ */
105
+ epoch?: number;
106
+ /**
107
+ * SSE sequence-number high-water mark from a prior handle or persisted
108
+ * state. Seeds the first SSE connect's `from_sequence_num` so the server
109
+ * resumes instead of replaying full history. Omit (→ 0) for genuinely
110
+ * fresh attach.
111
+ */
112
+ initialSequenceNum?: number;
113
+ /** CCRClient heartbeat interval. Defaults to 20s (server TTL is 60s). */
114
+ heartbeatIntervalMs?: number;
115
+ /**
116
+ * User message typed on claude.ai. Echoes of outbound writes and
117
+ * re-deliveries of prompts already forwarded are filtered before this
118
+ * fires. May be async (e.g. attachment resolution).
119
+ */
120
+ onInboundMessage?: (msg: SDKMessage) => void | Promise<void>;
121
+ /**
122
+ * `control_response` from claude.ai — the user answered a `can_use_tool`
123
+ * prompt sent via `sendControlRequest`. Caller correlates by `request_id`.
124
+ */
125
+ onPermissionResponse?: (res: SDKControlResponse) => void;
126
+ /** `interrupt` control_request from claude.ai. Already auto-replied-to. */
127
+ onInterrupt?: () => void;
128
+ onSetModel?: (model: string | undefined) => void;
129
+ onSetMaxThinkingTokens?: (tokens: number | null) => void;
130
+ /**
131
+ * `set_permission_mode` from claude.ai. Return an error verdict to send
132
+ * an error control_response (vs silently false-succeeding). Omit if
133
+ * the caller doesn't support permission modes — the shared handler
134
+ * returns a "not supported in this context" error.
135
+ */
136
+ onSetPermissionMode?: (mode: PermissionMode) => {
137
+ ok: true;
138
+ } | {
139
+ ok: false;
140
+ error: string;
141
+ };
142
+ /**
143
+ * Transport died permanently. 401 = JWT expired (re-attach with fresh
144
+ * secret), 4090 = epoch superseded (409, newer worker registered),
145
+ * 4091 = CCRClient init failed. Anything else = SSE reconnect budget
146
+ * exhausted. Transient disconnects are handled transparently inside
147
+ * SSETransport and do NOT fire this.
148
+ */
149
+ onClose?: (code?: number) => void;
150
+ };
151
+ /**
152
+ * Attach to an existing bridge session. Creates the v2 transport
153
+ * (SSETransport + CCRClient), wires ingress routing and control dispatch,
154
+ * returns a handle scoped to this one session.
155
+ *
156
+ * Throws if `createV2ReplTransport` fails (registerWorker error, etc).
157
+ *
158
+ * ALPHA STABILITY. This is a separate versioning universe from the main
159
+ * `query()` surface: breaking changes here do NOT bump the package major.
160
+ * @alpha
161
+ */
162
+ export declare function attachBridgeSession(opts: AttachBridgeSessionOptions): Promise<BridgeSessionHandle>;