@agentuity/core 2.0.9 → 2.0.11

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.
Files changed (62) hide show
  1. package/dist/services/coder/agents.d.ts +170 -0
  2. package/dist/services/coder/agents.d.ts.map +1 -0
  3. package/dist/services/coder/agents.js +77 -0
  4. package/dist/services/coder/agents.js.map +1 -0
  5. package/dist/services/coder/api-reference.d.ts.map +1 -1
  6. package/dist/services/coder/api-reference.js +393 -41
  7. package/dist/services/coder/api-reference.js.map +1 -1
  8. package/dist/services/coder/client.d.ts +44 -2
  9. package/dist/services/coder/client.d.ts.map +1 -1
  10. package/dist/services/coder/client.js +89 -3
  11. package/dist/services/coder/client.js.map +1 -1
  12. package/dist/services/coder/close-codes.d.ts +76 -0
  13. package/dist/services/coder/close-codes.d.ts.map +1 -0
  14. package/dist/services/coder/close-codes.js +77 -0
  15. package/dist/services/coder/close-codes.js.map +1 -0
  16. package/dist/services/coder/discover.d.ts +1 -1
  17. package/dist/services/coder/discover.js +2 -2
  18. package/dist/services/coder/discover.js.map +1 -1
  19. package/dist/services/coder/index.d.ts +9 -2
  20. package/dist/services/coder/index.d.ts.map +1 -1
  21. package/dist/services/coder/index.js +6 -1
  22. package/dist/services/coder/index.js.map +1 -1
  23. package/dist/services/coder/protocol.d.ts +1855 -0
  24. package/dist/services/coder/protocol.d.ts.map +1 -0
  25. package/dist/services/coder/protocol.js +976 -0
  26. package/dist/services/coder/protocol.js.map +1 -0
  27. package/dist/services/coder/sessions.d.ts +9 -0
  28. package/dist/services/coder/sessions.d.ts.map +1 -1
  29. package/dist/services/coder/sessions.js +30 -6
  30. package/dist/services/coder/sessions.js.map +1 -1
  31. package/dist/services/coder/sse.d.ts +255 -0
  32. package/dist/services/coder/sse.d.ts.map +1 -0
  33. package/dist/services/coder/sse.js +676 -0
  34. package/dist/services/coder/sse.js.map +1 -0
  35. package/dist/services/coder/types.d.ts +1013 -0
  36. package/dist/services/coder/types.d.ts.map +1 -1
  37. package/dist/services/coder/types.js +215 -1
  38. package/dist/services/coder/types.js.map +1 -1
  39. package/dist/services/coder/websocket.d.ts +346 -0
  40. package/dist/services/coder/websocket.d.ts.map +1 -0
  41. package/dist/services/coder/websocket.js +791 -0
  42. package/dist/services/coder/websocket.js.map +1 -0
  43. package/dist/services/oauth/types.d.ts +10 -0
  44. package/dist/services/oauth/types.d.ts.map +1 -1
  45. package/dist/services/oauth/types.js +3 -0
  46. package/dist/services/oauth/types.js.map +1 -1
  47. package/dist/services/project/deploy.d.ts +1 -1
  48. package/dist/services/sandbox/run.d.ts +2 -2
  49. package/dist/services/sandbox/types.d.ts +2 -2
  50. package/package.json +2 -2
  51. package/src/services/coder/agents.ts +148 -0
  52. package/src/services/coder/api-reference.ts +411 -45
  53. package/src/services/coder/client.ts +133 -2
  54. package/src/services/coder/close-codes.ts +83 -0
  55. package/src/services/coder/discover.ts +2 -2
  56. package/src/services/coder/index.ts +29 -1
  57. package/src/services/coder/protocol.ts +1200 -0
  58. package/src/services/coder/sessions.ts +40 -10
  59. package/src/services/coder/sse.ts +796 -0
  60. package/src/services/coder/types.ts +249 -1
  61. package/src/services/coder/websocket.ts +943 -0
  62. package/src/services/oauth/types.ts +3 -0
@@ -0,0 +1,346 @@
1
+ /**
2
+ * WebSocket client for the Coder Hub real-time communication.
3
+ *
4
+ * Provides bidirectional communication between clients and the Coder Hub server,
5
+ * supporting multiple connection roles (lead, observer, controller) with
6
+ * automatic reconnection, heartbeat, and message queuing.
7
+ *
8
+ * @module coder/websocket
9
+ *
10
+ * @example Class-based API with callbacks
11
+ * ```typescript
12
+ * import { CoderHubWebSocketClient } from '@agentuity/core/coder';
13
+ *
14
+ * const client = new CoderHubWebSocketClient({
15
+ * apiKey: 'your-api-key',
16
+ * sessionId: 'session-123',
17
+ * role: 'observer',
18
+ * onInit: (init) => {
19
+ * console.log('Connected to session:', init.sessionId);
20
+ * console.log('Available agents:', init.agents);
21
+ * },
22
+ * onMessage: (msg) => {
23
+ * console.log('Received:', msg);
24
+ * },
25
+ * onStateChange: (state) => {
26
+ * console.log('Connection state:', state);
27
+ * },
28
+ * });
29
+ *
30
+ * client.connect();
31
+ *
32
+ * // Send a message
33
+ * client.send({
34
+ * type: 'ping',
35
+ * timestamp: Date.now(),
36
+ * });
37
+ *
38
+ * // Close when done
39
+ * client.close();
40
+ * ```
41
+ *
42
+ * @example Async iterator API
43
+ * ```typescript
44
+ * import { subscribeToCoderHub } from '@agentuity/core/coder';
45
+ *
46
+ * for await (const message of subscribeToCoderHub({
47
+ * sessionId: 'session-123',
48
+ * role: 'observer',
49
+ * })) {
50
+ * if (message.type === 'broadcast') {
51
+ * console.log('Event:', message.event, message.data);
52
+ * }
53
+ * }
54
+ * ```
55
+ */
56
+ import { z } from 'zod/v4';
57
+ import type { Logger } from '../../logger.ts';
58
+ import type { ClientMessage, CoderHubInitMessage, CoderHubResponse, ServerMessage } from './protocol.ts';
59
+ /**
60
+ * Connection state for the WebSocket client.
61
+ *
62
+ * - `'connecting'` - Initial WebSocket connection in progress
63
+ * - `'authenticating'` - WebSocket connected, sending auth message
64
+ * - `'connected'` - Authenticated and ready to send/receive messages
65
+ * - `'reconnecting'` - Reconnecting after disconnect
66
+ * - `'closed'` - Connection closed (manually or after max retries)
67
+ */
68
+ export type CoderHubWebSocketState = 'connecting' | 'authenticating' | 'connected' | 'reconnecting' | 'closed';
69
+ /**
70
+ * Options for the WebSocket client.
71
+ */
72
+ export declare const CoderHubWebSocketOptionsSchema: z.ZodObject<{
73
+ apiKey: z.ZodOptional<z.ZodString>;
74
+ orgId: z.ZodOptional<z.ZodString>;
75
+ url: z.ZodOptional<z.ZodString>;
76
+ region: z.ZodOptional<z.ZodString>;
77
+ sessionId: z.ZodOptional<z.ZodString>;
78
+ role: z.ZodOptional<z.ZodEnum<{
79
+ lead: "lead";
80
+ controller: "controller";
81
+ observer: "observer";
82
+ }>>;
83
+ agent: z.ZodOptional<z.ZodString>;
84
+ parentSessionId: z.ZodOptional<z.ZodString>;
85
+ task: z.ZodOptional<z.ZodString>;
86
+ label: z.ZodOptional<z.ZodString>;
87
+ origin: z.ZodOptional<z.ZodEnum<{
88
+ tui: "tui";
89
+ web: "web";
90
+ desktop: "desktop";
91
+ sdk: "sdk";
92
+ }>>;
93
+ driverMode: z.ZodOptional<z.ZodEnum<{
94
+ rpc: "rpc";
95
+ }>>;
96
+ driverInstanceId: z.ZodOptional<z.ZodString>;
97
+ driverVersion: z.ZodOptional<z.ZodString>;
98
+ logger: z.ZodOptional<z.ZodCustom<Logger, Logger>>;
99
+ autoReconnect: z.ZodOptional<z.ZodBoolean>;
100
+ maxReconnectAttempts: z.ZodOptional<z.ZodNumber>;
101
+ reconnectDelayMs: z.ZodOptional<z.ZodNumber>;
102
+ maxReconnectDelayMs: z.ZodOptional<z.ZodNumber>;
103
+ heartbeatIntervalMs: z.ZodOptional<z.ZodNumber>;
104
+ heartbeatTimeoutMs: z.ZodOptional<z.ZodNumber>;
105
+ maxMessageQueueSize: z.ZodOptional<z.ZodNumber>;
106
+ onOpen: z.ZodOptional<z.ZodCustom<() => void, () => void>>;
107
+ onClose: z.ZodOptional<z.ZodCustom<(code: number, reason: string) => void, (code: number, reason: string) => void>>;
108
+ onError: z.ZodOptional<z.ZodCustom<(error: Error) => void, (error: Error) => void>>;
109
+ onMessage: z.ZodOptional<z.ZodCustom<(message: ServerMessage) => void, (message: ServerMessage) => void>>;
110
+ onInit: z.ZodOptional<z.ZodCustom<(message: CoderHubInitMessage) => void, (message: CoderHubInitMessage) => void>>;
111
+ onStateChange: z.ZodOptional<z.ZodCustom<(state: CoderHubWebSocketState) => void, (state: CoderHubWebSocketState) => void>>;
112
+ }, z.core.$strip>;
113
+ export type CoderHubWebSocketOptions = z.infer<typeof CoderHubWebSocketOptionsSchema>;
114
+ /**
115
+ * Error type for WebSocket operations.
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * try {
120
+ * await client.sendAndWait({ type: 'tool', name: 'read', ... });
121
+ * } catch (err) {
122
+ * if (err instanceof CoderHubWebSocketError) {
123
+ * if (err.code === 'response_timeout') {
124
+ * console.log('Server did not respond in time');
125
+ * }
126
+ * }
127
+ * }
128
+ * ```
129
+ */
130
+ export declare const CoderHubWebSocketError: {
131
+ new (args?: ({
132
+ code: "connection_failed" | "auth_failed" | "connection_error" | "max_reconnects_exceeded" | "send_while_disconnected" | "response_timeout" | "invalid_response";
133
+ sessionId?: string;
134
+ } & {
135
+ message?: string;
136
+ cause?: unknown;
137
+ }) | undefined): import("../../error.ts").RichError & {
138
+ readonly _tag: "CoderHubWebSocketError";
139
+ } & Readonly<{
140
+ code: "connection_failed" | "auth_failed" | "connection_error" | "max_reconnects_exceeded" | "send_while_disconnected" | "response_timeout" | "invalid_response";
141
+ sessionId?: string;
142
+ }>;
143
+ readonly defaultMessage?: string;
144
+ };
145
+ /**
146
+ * WebSocket client for real-time Coder Hub communication.
147
+ *
148
+ * Supports multiple connection roles and provides automatic reconnection,
149
+ * heartbeat management, and message queuing for resilient connections.
150
+ *
151
+ * @example Observer connection
152
+ * ```typescript
153
+ * const client = new CoderHubWebSocketClient({
154
+ * sessionId: 'session-123',
155
+ * role: 'observer',
156
+ * onMessage: (msg) => {
157
+ * if (msg.type === 'broadcast') {
158
+ * console.log('Event:', msg.event);
159
+ * }
160
+ * },
161
+ * });
162
+ * client.connect();
163
+ * ```
164
+ *
165
+ * @example Controller connection with sendAndWait
166
+ * ```typescript
167
+ * const client = new CoderHubWebSocketClient({
168
+ * sessionId: 'session-123',
169
+ * role: 'controller',
170
+ * });
171
+ * client.connect();
172
+ *
173
+ * // Wait for connection
174
+ * await new Promise(resolve => {
175
+ * client.onInit = () => resolve(undefined);
176
+ * });
177
+ *
178
+ * // Send a request and wait for response
179
+ * const response = await client.sendAndWait({
180
+ * type: 'event',
181
+ * event: 'steer',
182
+ * data: { direction: 'continue' },
183
+ * });
184
+ * console.log('Response:', response);
185
+ * ```
186
+ *
187
+ * @example Sub-agent connection
188
+ * ```typescript
189
+ * const client = new CoderHubWebSocketClient({
190
+ * role: 'observer', // Sub-agents connect as observers to parent
191
+ * agent: 'scout',
192
+ * parentSessionId: 'parent-session-456',
193
+ * });
194
+ * client.connect();
195
+ * ```
196
+ */
197
+ export declare class CoderHubWebSocketClient {
198
+ #private;
199
+ constructor(options?: CoderHubWebSocketOptions);
200
+ /**
201
+ * The current connection state.
202
+ *
203
+ * @see CoderHubWebSocketState for state descriptions
204
+ */
205
+ get state(): CoderHubWebSocketState;
206
+ /**
207
+ * The session ID for this connection.
208
+ *
209
+ * Returns the server-assigned session ID (from init message) if available,
210
+ * otherwise returns the session ID passed in options.
211
+ */
212
+ get sessionId(): string | undefined;
213
+ /**
214
+ * The init message received from the server after authentication.
215
+ *
216
+ * Contains session configuration, available agents, tools, and other metadata.
217
+ * Only available after successful authentication.
218
+ */
219
+ get initMessage(): CoderHubInitMessage | null;
220
+ /**
221
+ * Whether the client is currently connected and authenticated.
222
+ *
223
+ * Returns `true` only when state is 'connected' AND WebSocket is open.
224
+ */
225
+ get isConnected(): boolean;
226
+ /**
227
+ * Establish the WebSocket connection and authenticate.
228
+ *
229
+ * If already connected or connecting, this is a no-op.
230
+ * Automatically reconnects on disconnection unless `close()` was called.
231
+ *
232
+ * The connection goes through these states:
233
+ * 1. `'connecting'` - WebSocket opening
234
+ * 2. `'authenticating'` - Sending auth message
235
+ * 3. `'connected'` - Received init message
236
+ */
237
+ connect(): void;
238
+ /**
239
+ * Close the WebSocket connection.
240
+ *
241
+ * After calling `close()`, you can call `connect()` again to reconnect.
242
+ * Any pending requests will be rejected with an error.
243
+ *
244
+ * @param code - Optional close code (default: 1000 for normal close)
245
+ * @param reason - Optional close reason string
246
+ */
247
+ close(code?: number, reason?: string): void;
248
+ /**
249
+ * Send a message to the server.
250
+ *
251
+ * If not connected, the message will be queued and sent when reconnected
252
+ * (up to `maxMessageQueueSize` messages). If the queue is full, an error
253
+ * is emitted via `onError`.
254
+ *
255
+ * @param message - The message to send
256
+ *
257
+ * @example
258
+ * ```typescript
259
+ * client.send({
260
+ * type: 'ping',
261
+ * timestamp: Date.now(),
262
+ * });
263
+ *
264
+ * client.send({
265
+ * type: 'session_entry',
266
+ * path: 'entries.jsonl',
267
+ * line: JSON.stringify({ type: 'message', content: 'Hello' }),
268
+ * });
269
+ * ```
270
+ */
271
+ send(message: ClientMessage): void;
272
+ /**
273
+ * Send a message and wait for a response.
274
+ *
275
+ * Automatically adds a unique `id` to the message and waits for a
276
+ * response with matching `id`. Useful for request/response patterns
277
+ * like tool calls or RPC commands.
278
+ *
279
+ * @param message - The message to send (without `id` field)
280
+ * @param timeoutMs - Timeout in milliseconds (default: 30000)
281
+ * @returns Promise that resolves with the response
282
+ * @throws {CoderHubWebSocketError} If timeout exceeded or connection closed
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * try {
287
+ * const response = await client.sendAndWait({
288
+ * type: 'tool',
289
+ * name: 'read_file',
290
+ * toolCallId: 'call-123',
291
+ * params: { path: '/src/index.ts' },
292
+ * });
293
+ * console.log('Tool result:', response.actions);
294
+ * } catch (err) {
295
+ * if (err instanceof CoderHubWebSocketError && err.code === 'response_timeout') {
296
+ * console.log('Tool call timed out');
297
+ * }
298
+ * }
299
+ * ```
300
+ */
301
+ sendAndWait(message: Omit<ClientMessage, 'id'>, timeoutMs?: number): Promise<CoderHubResponse>;
302
+ }
303
+ /**
304
+ * Subscribe to a Coder Hub session via WebSocket using async iteration.
305
+ *
306
+ * Returns an async iterator that yields server messages as they arrive.
307
+ * The connection is automatically managed (auth, reconnection, cleanup).
308
+ *
309
+ * @param options - Configuration for the WebSocket connection
310
+ * @yields Server messages as they arrive
311
+ * @throws {CoderHubWebSocketError} If connection fails or max reconnection attempts exceeded
312
+ *
313
+ * @example Basic usage
314
+ * ```typescript
315
+ * import { subscribeToCoderHub } from '@agentuity/core/coder';
316
+ *
317
+ * for await (const message of subscribeToCoderHub({
318
+ * sessionId: 'session-123',
319
+ * role: 'observer',
320
+ * })) {
321
+ * switch (message.type) {
322
+ * case 'broadcast':
323
+ * console.log('Event:', message.event);
324
+ * break;
325
+ * case 'presence':
326
+ * console.log('Participant:', message.participant);
327
+ * break;
328
+ * }
329
+ * }
330
+ * ```
331
+ *
332
+ * @example With error handling
333
+ * ```typescript
334
+ * try {
335
+ * for await (const message of subscribeToCoderHub({ sessionId: 'session-123' })) {
336
+ * console.log(message);
337
+ * }
338
+ * } catch (err) {
339
+ * if (err instanceof CoderHubWebSocketError) {
340
+ * console.log('WebSocket error:', err.code);
341
+ * }
342
+ * }
343
+ * ```
344
+ */
345
+ export declare function subscribeToCoderHub(options: CoderHubWebSocketOptions): AsyncGenerator<ServerMessage, void, unknown>;
346
+ //# sourceMappingURL=websocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/services/coder/websocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAC;AAE3B,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAO9C,OAAO,KAAK,EACX,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAEhB,aAAa,EACb,MAAM,eAAe,CAAC;AAIvB;;;;;;;;GAQG;AACH,MAAM,MAAM,sBAAsB,GAC/B,YAAY,GACZ,gBAAgB,GAChB,WAAW,GACX,cAAc,GACd,QAAQ,CAAC;AAEZ;;GAEG;AACH,eAAO,MAAM,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4CAsDnB,IAAI,QAAJ,IAAI;8CAGX,MAAM,UAAU,MAAM,KAAK,IAAI,SAA/B,MAAM,UAAU,MAAM,KAAK,IAAI;+CAIrB,KAAK,KAAK,IAAI,UAAd,KAAK,KAAK,IAAI;mDAGrB,aAAa,KAAK,IAAI,YAAtB,aAAa,KAAK,IAAI;gDAK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