agentxjs 0.0.0-dev-20260312143810

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/src/types.ts ADDED
@@ -0,0 +1,558 @@
1
+ /**
2
+ * AgentX Client SDK Types
3
+ */
4
+
5
+ import type { Message } from "@agentxjs/core/agent";
6
+ import type { AgentXError } from "@agentxjs/core/error";
7
+ import type { BusEvent, BusEventHandler, EventBus, Unsubscribe } from "@agentxjs/core/event";
8
+ import type { LLMProtocol, LLMProviderRecord } from "@agentxjs/core/persistence";
9
+ import type { AgentXPlatform } from "@agentxjs/core/runtime";
10
+ import type { Presentation, PresentationOptions } from "./presentation";
11
+
12
+ // ============================================================================
13
+ // Configuration Types
14
+ // ============================================================================
15
+
16
+ /**
17
+ * Static or dynamic value
18
+ */
19
+ export type MaybeAsync<T> = T | (() => T) | (() => Promise<T>);
20
+
21
+ /**
22
+ * Internal config passed to RemoteClient
23
+ * @internal
24
+ */
25
+ export interface RemoteClientConfig {
26
+ serverUrl: string;
27
+ headers?: MaybeAsync<Record<string, string>>;
28
+ context?: MaybeAsync<Record<string, unknown>>;
29
+ timeout?: number;
30
+ autoReconnect?: boolean;
31
+ customPlatform?: AgentXPlatform;
32
+ }
33
+
34
+ // ============================================================================
35
+ // Response Types
36
+ // ============================================================================
37
+
38
+ /**
39
+ * Agent info returned from server
40
+ */
41
+ export interface AgentInfo {
42
+ agentId: string;
43
+ imageId: string;
44
+ containerId: string;
45
+ sessionId: string;
46
+ lifecycle?: string;
47
+ }
48
+
49
+ /**
50
+ * Image record from server
51
+ */
52
+ export interface ImageRecord {
53
+ imageId: string;
54
+ containerId: string;
55
+ sessionId: string;
56
+ name?: string;
57
+ description?: string;
58
+ systemPrompt?: string;
59
+ customData?: Record<string, unknown>;
60
+ createdAt: number;
61
+ updatedAt: number;
62
+ }
63
+
64
+ /**
65
+ * Container info
66
+ */
67
+ export interface ContainerInfo {
68
+ containerId: string;
69
+ }
70
+
71
+ // ============================================================================
72
+ // Request/Response Types
73
+ // ============================================================================
74
+
75
+ /**
76
+ * Base response with requestId
77
+ */
78
+ export interface BaseResponse {
79
+ requestId: string;
80
+ error?: string;
81
+ }
82
+
83
+ /**
84
+ * Agent create response
85
+ */
86
+ export interface AgentCreateResponse extends BaseResponse {
87
+ agentId: string;
88
+ imageId: string;
89
+ containerId: string;
90
+ sessionId: string;
91
+ }
92
+
93
+ /**
94
+ * Agent get response
95
+ */
96
+ export interface AgentGetResponse extends BaseResponse {
97
+ agent: AgentInfo | null;
98
+ exists: boolean;
99
+ }
100
+
101
+ /**
102
+ * Agent list response
103
+ */
104
+ export interface AgentListResponse extends BaseResponse {
105
+ agents: AgentInfo[];
106
+ }
107
+
108
+ /**
109
+ * Image create response
110
+ */
111
+ export interface ImageCreateResponse extends BaseResponse {
112
+ record: ImageRecord;
113
+ __subscriptions?: string[];
114
+ }
115
+
116
+ /**
117
+ * Image get response
118
+ */
119
+ export interface ImageGetResponse extends BaseResponse {
120
+ record: ImageRecord | null;
121
+ __subscriptions?: string[];
122
+ }
123
+
124
+ /**
125
+ * Image list response
126
+ */
127
+ export interface ImageListResponse extends BaseResponse {
128
+ records: ImageRecord[];
129
+ __subscriptions?: string[];
130
+ }
131
+
132
+ /**
133
+ * Image update response
134
+ */
135
+ export interface ImageUpdateResponse extends BaseResponse {
136
+ record: ImageRecord;
137
+ }
138
+
139
+ /**
140
+ * Container create response
141
+ */
142
+ export interface ContainerCreateResponse extends BaseResponse {
143
+ containerId: string;
144
+ }
145
+
146
+ /**
147
+ * Container get response
148
+ */
149
+ export interface ContainerGetResponse extends BaseResponse {
150
+ containerId: string;
151
+ exists: boolean;
152
+ }
153
+
154
+ /**
155
+ * Container list response
156
+ */
157
+ export interface ContainerListResponse extends BaseResponse {
158
+ containerIds: string[];
159
+ }
160
+
161
+ /**
162
+ * Message send response
163
+ */
164
+ export interface MessageSendResponse extends BaseResponse {
165
+ agentId: string;
166
+ }
167
+
168
+ /**
169
+ * LLM provider create response
170
+ */
171
+ export interface LLMProviderCreateResponse extends BaseResponse {
172
+ record: LLMProviderRecord;
173
+ }
174
+
175
+ /**
176
+ * LLM provider get response
177
+ */
178
+ export interface LLMProviderGetResponse extends BaseResponse {
179
+ record: LLMProviderRecord | null;
180
+ }
181
+
182
+ /**
183
+ * LLM provider list response
184
+ */
185
+ export interface LLMProviderListResponse extends BaseResponse {
186
+ records: LLMProviderRecord[];
187
+ }
188
+
189
+ /**
190
+ * LLM provider update response
191
+ */
192
+ export interface LLMProviderUpdateResponse extends BaseResponse {
193
+ record: LLMProviderRecord;
194
+ }
195
+
196
+ /**
197
+ * LLM provider default response
198
+ */
199
+ export interface LLMProviderDefaultResponse extends BaseResponse {
200
+ record: LLMProviderRecord | null;
201
+ }
202
+
203
+ // ============================================================================
204
+ // Namespace Interfaces
205
+ // ============================================================================
206
+
207
+ /**
208
+ * Container operations namespace
209
+ */
210
+ export interface ContainerNamespace {
211
+ /**
212
+ * Create or get container
213
+ */
214
+ create(containerId: string): Promise<ContainerCreateResponse>;
215
+
216
+ /**
217
+ * Get container
218
+ */
219
+ get(containerId: string): Promise<ContainerGetResponse>;
220
+
221
+ /**
222
+ * List containers
223
+ */
224
+ list(): Promise<ContainerListResponse>;
225
+ }
226
+
227
+ /**
228
+ * Image operations namespace
229
+ */
230
+ export interface ImageNamespace {
231
+ /**
232
+ * Create a new image
233
+ */
234
+ create(params: {
235
+ containerId: string;
236
+ name?: string;
237
+ description?: string;
238
+ systemPrompt?: string;
239
+ mcpServers?: Record<string, unknown>;
240
+ customData?: Record<string, unknown>;
241
+ }): Promise<ImageCreateResponse>;
242
+
243
+ /**
244
+ * Get image by ID
245
+ */
246
+ get(imageId: string): Promise<ImageGetResponse>;
247
+
248
+ /**
249
+ * List images
250
+ */
251
+ list(containerId?: string): Promise<ImageListResponse>;
252
+
253
+ /**
254
+ * Update image
255
+ */
256
+ update(
257
+ imageId: string,
258
+ updates: {
259
+ name?: string;
260
+ description?: string;
261
+ customData?: Record<string, unknown>;
262
+ }
263
+ ): Promise<ImageUpdateResponse>;
264
+
265
+ /**
266
+ * Delete image
267
+ */
268
+ delete(imageId: string): Promise<BaseResponse>;
269
+
270
+ /**
271
+ * Get message history for an image
272
+ */
273
+ getMessages(imageId: string): Promise<Message[]>;
274
+ }
275
+
276
+ /**
277
+ * Agent operations namespace
278
+ */
279
+ export interface AgentNamespace {
280
+ /**
281
+ * Create a new agent
282
+ */
283
+ create(params: { imageId: string; agentId?: string }): Promise<AgentCreateResponse>;
284
+
285
+ /**
286
+ * Get agent by ID
287
+ */
288
+ get(agentId: string): Promise<AgentGetResponse>;
289
+
290
+ /**
291
+ * List agents
292
+ */
293
+ list(containerId?: string): Promise<AgentListResponse>;
294
+
295
+ /**
296
+ * Destroy an agent
297
+ */
298
+ destroy(agentId: string): Promise<BaseResponse>;
299
+ }
300
+
301
+ /**
302
+ * Session operations namespace (messaging)
303
+ */
304
+ export interface SessionNamespace {
305
+ /**
306
+ * Send message to agent
307
+ */
308
+ send(agentId: string, content: string | unknown[]): Promise<MessageSendResponse>;
309
+
310
+ /**
311
+ * Interrupt agent
312
+ */
313
+ interrupt(agentId: string): Promise<BaseResponse>;
314
+
315
+ /**
316
+ * Get message history for an agent's session
317
+ */
318
+ getMessages(agentId: string): Promise<Message[]>;
319
+ }
320
+
321
+ /**
322
+ * LLM provider operations namespace
323
+ */
324
+ export interface LLMNamespace {
325
+ /**
326
+ * Create a new LLM provider configuration
327
+ */
328
+ create(params: {
329
+ containerId: string;
330
+ name: string;
331
+ vendor: string;
332
+ protocol: LLMProtocol;
333
+ apiKey: string;
334
+ baseUrl?: string;
335
+ model?: string;
336
+ }): Promise<LLMProviderCreateResponse>;
337
+
338
+ /**
339
+ * Get LLM provider by ID
340
+ */
341
+ get(id: string): Promise<LLMProviderGetResponse>;
342
+
343
+ /**
344
+ * List LLM providers for a container
345
+ */
346
+ list(containerId: string): Promise<LLMProviderListResponse>;
347
+
348
+ /**
349
+ * Update LLM provider
350
+ */
351
+ update(
352
+ id: string,
353
+ updates: {
354
+ name?: string;
355
+ vendor?: string;
356
+ protocol?: LLMProtocol;
357
+ apiKey?: string;
358
+ baseUrl?: string;
359
+ model?: string;
360
+ }
361
+ ): Promise<LLMProviderUpdateResponse>;
362
+
363
+ /**
364
+ * Delete LLM provider
365
+ */
366
+ delete(id: string): Promise<BaseResponse>;
367
+
368
+ /**
369
+ * Set default LLM provider for a container
370
+ */
371
+ setDefault(id: string): Promise<BaseResponse>;
372
+
373
+ /**
374
+ * Get default LLM provider for a container
375
+ */
376
+ getDefault(containerId: string): Promise<LLMProviderDefaultResponse>;
377
+ }
378
+
379
+ /**
380
+ * Presentation operations namespace
381
+ */
382
+ export interface PresentationNamespace {
383
+ /**
384
+ * Create a presentation for UI integration
385
+ *
386
+ * @example
387
+ * ```typescript
388
+ * const pres = agentx.presentation.create(agentId, {
389
+ * onUpdate: (state) => renderUI(state),
390
+ * onError: (error) => console.error(error),
391
+ * });
392
+ *
393
+ * await pres.send("Hello!");
394
+ * pres.dispose();
395
+ * ```
396
+ */
397
+ create(agentId: string, options?: PresentationOptions): Promise<Presentation>;
398
+ }
399
+
400
+ // ============================================================================
401
+ // AgentX Client Interface
402
+ // ============================================================================
403
+
404
+ /**
405
+ * AgentX Client SDK — unified interface for local, remote, and server modes
406
+ */
407
+ export interface AgentX {
408
+ /**
409
+ * Check if connected/active
410
+ */
411
+ readonly connected: boolean;
412
+
413
+ /**
414
+ * Event bus for subscribing to events
415
+ */
416
+ readonly events: EventBus;
417
+
418
+ // ==================== Namespaced Operations ====================
419
+
420
+ readonly container: ContainerNamespace;
421
+ readonly image: ImageNamespace;
422
+ readonly agent: AgentNamespace;
423
+ readonly session: SessionNamespace;
424
+ readonly presentation: PresentationNamespace;
425
+ readonly llm: LLMNamespace;
426
+
427
+ // ==================== Event Subscription ====================
428
+
429
+ on<T extends string>(type: T, handler: BusEventHandler<BusEvent & { type: T }>): Unsubscribe;
430
+ onAny(handler: BusEventHandler): Unsubscribe;
431
+ subscribe(sessionId: string): void;
432
+
433
+ // ==================== Error Handling ====================
434
+
435
+ /**
436
+ * Top-level error handler — receives all AgentXError instances from any layer.
437
+ *
438
+ * Independent of `on("error", ...)` (stream events) and `presentation.onError` (UI errors).
439
+ *
440
+ * @example
441
+ * ```typescript
442
+ * ax.onError((error) => {
443
+ * console.error(`[${error.category}] ${error.code}: ${error.message}`);
444
+ * if (!error.recoverable) {
445
+ * // Circuit is open, stop sending requests
446
+ * }
447
+ * });
448
+ * ```
449
+ */
450
+ onError(handler: (error: AgentXError) => void): Unsubscribe;
451
+
452
+ // ==================== RPC ====================
453
+
454
+ /**
455
+ * Universal JSON-RPC entry point — works in all modes:
456
+ * - Local: dispatches to CommandHandler directly
457
+ * - Remote: forwards to the remote server via RPC client
458
+ *
459
+ * @example
460
+ * ```typescript
461
+ * const result = await ax.rpc("container.create", { containerId: "default" });
462
+ * const { records } = await ax.rpc<{ records: ImageRecord[] }>("image.list");
463
+ * ```
464
+ */
465
+ rpc<T = unknown>(method: string, params?: unknown): Promise<T>;
466
+
467
+ // ==================== Lifecycle ====================
468
+
469
+ disconnect(): Promise<void>;
470
+ dispose(): Promise<void>;
471
+ }
472
+
473
+ // ============================================================================
474
+ // Fluent Builder Interface
475
+ // ============================================================================
476
+
477
+ /**
478
+ * Options for connecting to a remote AgentX server
479
+ */
480
+ export interface ConnectOptions {
481
+ /** Authentication headers */
482
+ headers?: MaybeAsync<Record<string, string>>;
483
+ /** Business context injected into requests */
484
+ context?: MaybeAsync<Record<string, unknown>>;
485
+ /** Request timeout in ms (default: 30000) */
486
+ timeout?: number;
487
+ /** Auto reconnect on disconnect (default: true) */
488
+ autoReconnect?: boolean;
489
+ }
490
+
491
+ /**
492
+ * Configuration for serving as an AgentX server
493
+ */
494
+ export interface ServeConfig {
495
+ /** Port to listen on (default: 5200) */
496
+ port?: number;
497
+ /** Host to bind to (default: "0.0.0.0") */
498
+ host?: string;
499
+ /** Existing HTTP server to attach to */
500
+ server?: unknown;
501
+ /** WebSocket path when attached (default: "/ws") */
502
+ wsPath?: string;
503
+ }
504
+
505
+ /**
506
+ * Server instance returned by serve()
507
+ */
508
+ export interface AgentXServer {
509
+ listen(port?: number, host?: string): Promise<void>;
510
+ close(): Promise<void>;
511
+ dispose(): Promise<void>;
512
+ }
513
+
514
+ /**
515
+ * AgentXBuilder — fluent API entry point
516
+ *
517
+ * Created by `createAgentX(platform?)`. The builder itself is an AgentX
518
+ * instance (local mode). Call `.connect()` to get a remote client,
519
+ * or `.serve()` to start a server.
520
+ *
521
+ * @example
522
+ * ```typescript
523
+ * const ax = createAgentX(node({ createDriver }))
524
+ *
525
+ * // Local use
526
+ * await ax.agent.create({ imageId: "..." })
527
+ *
528
+ * // Connect to remote server
529
+ * const client = await ax.connect("wss://...")
530
+ *
531
+ * // Serve as server
532
+ * const server = await ax.serve({ port: 3100 })
533
+ * ```
534
+ */
535
+ export interface AgentXBuilder extends AgentX {
536
+ /**
537
+ * Connect to a remote AgentX server
538
+ */
539
+ connect(serverUrl: string, options?: ConnectOptions): Promise<AgentX>;
540
+
541
+ /**
542
+ * Start serving as an AgentX server
543
+ */
544
+ serve(config?: ServeConfig): Promise<AgentXServer>;
545
+ }
546
+
547
+ // ============================================================================
548
+ // Internal Types
549
+ // ============================================================================
550
+
551
+ /**
552
+ * Pending request entry
553
+ */
554
+ export interface PendingRequest {
555
+ resolve: (response: BusEvent) => void;
556
+ reject: (error: Error) => void;
557
+ timeout: ReturnType<typeof setTimeout>;
558
+ }