@standardagents/client 0.1.0-dev.ffffff

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.
@@ -0,0 +1,476 @@
1
+ interface Message {
2
+ id: string;
3
+ role: "system" | "user" | "assistant" | "tool";
4
+ content: string | null;
5
+ name?: string | null;
6
+ tool_calls?: string | null;
7
+ tool_call_id?: string | null;
8
+ log_id?: string | null;
9
+ created_at: number;
10
+ request_sent_at?: number | null;
11
+ response_completed_at?: number | null;
12
+ status?: "pending" | "completed" | "failed";
13
+ silent?: boolean;
14
+ tool_status?: "success" | "error" | null;
15
+ reasoning_content?: string | null;
16
+ reasoning_details?: string | null;
17
+ parent_id?: string | null;
18
+ depth?: number;
19
+ attachments?: string | null;
20
+ }
21
+ /**
22
+ * Attachment reference stored in messages.attachments (JSON array)
23
+ * Represents a file that has been uploaded to the thread's filesystem
24
+ */
25
+ interface AttachmentRef {
26
+ id: string;
27
+ type: "file";
28
+ path: string;
29
+ name: string;
30
+ mimeType: string;
31
+ size: number;
32
+ width?: number;
33
+ height?: number;
34
+ description?: string;
35
+ }
36
+ /**
37
+ * Unified file representation for client-side state
38
+ * Represents both pending uploads and committed attachments
39
+ */
40
+ interface ThreadFile {
41
+ id: string;
42
+ name: string;
43
+ mimeType: string;
44
+ size: number;
45
+ isImage: boolean;
46
+ localPreviewUrl: string | null;
47
+ status: 'uploading' | 'ready' | 'committed' | 'error';
48
+ error?: string;
49
+ path?: string;
50
+ width?: number;
51
+ height?: number;
52
+ messageId?: string;
53
+ }
54
+ interface WorkItem {
55
+ id: string;
56
+ type: "tool_call" | "tool_result";
57
+ name?: string;
58
+ content: string | null;
59
+ status?: "pending" | "success" | "error" | null;
60
+ tool_call_id?: string;
61
+ }
62
+ interface WorkMessage {
63
+ id: string;
64
+ type: "workblock";
65
+ content: string | null;
66
+ reasoning_content?: string | null;
67
+ workItems: WorkItem[];
68
+ status: "pending" | "completed" | "failed";
69
+ created_at: number;
70
+ depth?: number;
71
+ }
72
+ type ThreadMessage = Message | WorkMessage;
73
+ interface AgentBuilderConfig {
74
+ endpoint: string;
75
+ }
76
+ interface GetMessagesOptions {
77
+ limit?: number;
78
+ offset?: number;
79
+ depth?: number;
80
+ includeSilent?: boolean;
81
+ }
82
+ interface MessageDataEvent {
83
+ type: "message_data";
84
+ message_id: string;
85
+ depth: number;
86
+ data: Message;
87
+ }
88
+ interface MessageChunkEvent {
89
+ type: "message_chunk";
90
+ message_id: string;
91
+ depth: number;
92
+ chunk: string;
93
+ }
94
+ interface ErrorEvent {
95
+ type: "error";
96
+ error: string;
97
+ }
98
+ /**
99
+ * Custom thread event emitted via emitThreadEvent on the backend
100
+ */
101
+ interface ThreadEvent<T = unknown> {
102
+ type: "event";
103
+ eventType: string;
104
+ data: T;
105
+ timestamp: number;
106
+ }
107
+ type MessageStreamEvent = MessageDataEvent | MessageChunkEvent | ErrorEvent | ThreadEvent;
108
+ interface LogDataEvent {
109
+ type: "log_data";
110
+ log_id: string;
111
+ data: any;
112
+ }
113
+ interface CustomEvent {
114
+ type: "custom";
115
+ customType: string;
116
+ value: any;
117
+ timestamp: number;
118
+ }
119
+ interface StoppedByUserEvent {
120
+ type: "stopped_by_user";
121
+ timestamp: number;
122
+ }
123
+ type LogStreamEvent = LogDataEvent | CustomEvent | StoppedByUserEvent;
124
+ interface SendMessagePayload {
125
+ role: "user" | "assistant" | "system";
126
+ content: string;
127
+ silent?: boolean;
128
+ attachments?: string[];
129
+ }
130
+ interface Thread {
131
+ id: string;
132
+ agent_id: string;
133
+ metadata?: Record<string, any>;
134
+ created_at: number;
135
+ }
136
+ interface MessageWebSocketCallbacks {
137
+ onMessage?: (event: MessageDataEvent) => void;
138
+ onChunk?: (event: MessageChunkEvent) => void;
139
+ onEvent?: (event: ThreadEvent) => void;
140
+ onError?: (event: ErrorEvent) => void;
141
+ onOpen?: () => void;
142
+ onClose?: () => void;
143
+ }
144
+ interface LogWebSocketCallbacks {
145
+ onLog?: (event: LogDataEvent) => void;
146
+ onCustom?: (event: CustomEvent) => void;
147
+ onStopped?: (event: StoppedByUserEvent) => void;
148
+ onOpen?: () => void;
149
+ onClose?: () => void;
150
+ }
151
+ type ConnectionStatus = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
152
+ interface ThreadConnectionCallbacks {
153
+ onMessage?: (event: MessageDataEvent) => void;
154
+ onChunk?: (event: MessageChunkEvent) => void;
155
+ onEvent?: (event: ThreadEvent) => void;
156
+ onError?: (event: ErrorEvent) => void;
157
+ onStatusChange?: (status: ConnectionStatus) => void;
158
+ }
159
+ interface CreateThreadPayload {
160
+ agent_id: string;
161
+ metadata?: Record<string, any>;
162
+ }
163
+
164
+ declare class AgentBuilderClient {
165
+ private endpoint;
166
+ private token;
167
+ constructor(endpoint: string);
168
+ /**
169
+ * Get the current endpoint
170
+ */
171
+ getEndpoint(): string;
172
+ /**
173
+ * Create a new thread
174
+ */
175
+ createThread(payload: CreateThreadPayload): Promise<Thread>;
176
+ /**
177
+ * Get thread metadata
178
+ */
179
+ getThread(id: string): Promise<Thread>;
180
+ /**
181
+ * Get messages from a thread with optional pagination and filtering
182
+ */
183
+ getMessages(id: string, options?: GetMessagesOptions): Promise<Message[]>;
184
+ /**
185
+ * Send a message to a thread
186
+ */
187
+ sendMessage(id: string, payload: SendMessagePayload): Promise<Message>;
188
+ /**
189
+ * Stop execution of a thread
190
+ */
191
+ stopExecution(id: string): Promise<void>;
192
+ /**
193
+ * Options for file upload
194
+ */
195
+ /**
196
+ * Upload a file to a thread's filesystem
197
+ * @param threadId - The thread ID
198
+ * @param file - The file to upload
199
+ * @param options - Optional upload options
200
+ * @param options.thumbnail - Base64-encoded thumbnail data (for images)
201
+ * @param options.width - Image width in pixels
202
+ * @param options.height - Image height in pixels
203
+ * @returns AttachmentRef with file metadata
204
+ */
205
+ uploadFile(threadId: string, file: File, options?: {
206
+ thumbnail?: string;
207
+ width?: number;
208
+ height?: number;
209
+ }): Promise<AttachmentRef>;
210
+ /**
211
+ * Convert a File to base64 string
212
+ */
213
+ private fileToBase64;
214
+ /**
215
+ * Get the full URL for a file in a thread's filesystem
216
+ */
217
+ getFileUrl(threadId: string, path: string): string;
218
+ /**
219
+ * Get the thumbnail URL for an image in a thread's filesystem
220
+ */
221
+ getThumbnailUrl(threadId: string, path: string): string;
222
+ /**
223
+ * Connect to message WebSocket for real-time message updates
224
+ */
225
+ connectMessageWebSocket(id: string, callbacks?: MessageWebSocketCallbacks, options?: {
226
+ includeSilent?: boolean;
227
+ depth?: number;
228
+ }): WebSocket;
229
+ /**
230
+ * Connect to log WebSocket for custom events
231
+ */
232
+ connectLogWebSocket(id: string, callbacks?: LogWebSocketCallbacks): WebSocket;
233
+ /**
234
+ * Get headers for HTTP requests
235
+ */
236
+ private getHeaders;
237
+ }
238
+
239
+ interface ThreadConnectionOptions {
240
+ /** Maximum message depth to stream (default: 0 for top-level only) */
241
+ depth?: number;
242
+ /** Whether to include silent messages (default: false) */
243
+ includeSilent?: boolean;
244
+ /** Heartbeat interval in milliseconds (default: 30000) */
245
+ heartbeatInterval?: number;
246
+ /** Maximum reconnection delay in milliseconds (default: 30000) */
247
+ maxReconnectDelay?: number;
248
+ }
249
+ /**
250
+ * ThreadConnectionManager handles WebSocket connection lifecycle for a thread.
251
+ * Provides automatic reconnection with exponential backoff and heartbeat keep-alive.
252
+ *
253
+ * This class is framework-agnostic. React and Vue SDKs use this internally
254
+ * and handle their own reactive state management.
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const manager = new ThreadConnectionManager(client, 'thread-123', {
259
+ * onMessage: (event) => updateMessages(event.data),
260
+ * onChunk: (event) => appendChunk(event.chunk),
261
+ * onStatusChange: (status) => setConnectionStatus(status),
262
+ * })
263
+ *
264
+ * manager.connect()
265
+ * // ... later
266
+ * manager.disconnect()
267
+ * ```
268
+ */
269
+ declare class ThreadConnectionManager {
270
+ private client;
271
+ private threadId;
272
+ private callbacks;
273
+ private options;
274
+ private ws;
275
+ private status;
276
+ private reconnectAttempts;
277
+ private reconnectTimeout;
278
+ private heartbeatInterval;
279
+ private isReconnecting;
280
+ private shouldReconnect;
281
+ constructor(client: AgentBuilderClient, threadId: string, callbacks?: ThreadConnectionCallbacks, options?: ThreadConnectionOptions);
282
+ /**
283
+ * Get current connection status
284
+ */
285
+ getStatus(): ConnectionStatus;
286
+ /**
287
+ * Get the thread ID this manager is connected to
288
+ */
289
+ getThreadId(): string;
290
+ /**
291
+ * Connect to the thread WebSocket
292
+ */
293
+ connect(): void;
294
+ /**
295
+ * Disconnect from the thread WebSocket
296
+ */
297
+ disconnect(): void;
298
+ /**
299
+ * Update callbacks without reconnecting
300
+ */
301
+ updateCallbacks(callbacks: Partial<ThreadConnectionCallbacks>): void;
302
+ /**
303
+ * Update options (requires reconnect to take effect for depth/includeSilent)
304
+ */
305
+ updateOptions(options: Partial<ThreadConnectionOptions>): void;
306
+ private setStatus;
307
+ private startHeartbeat;
308
+ private clearHeartbeat;
309
+ private clearReconnectTimeout;
310
+ private clearTimers;
311
+ private scheduleReconnect;
312
+ }
313
+
314
+ /**
315
+ * FileUploadManager - Framework-agnostic file upload management
316
+ *
317
+ * Handles the common logic for file uploads across Vue and React packages.
318
+ * Each framework integrates this manager with its own state management.
319
+ */
320
+
321
+ /**
322
+ * Callback for receiving file state updates during upload
323
+ */
324
+ type FileUpdateCallback = (updates: Partial<ThreadFile>) => void;
325
+ /**
326
+ * Options for upload execution
327
+ */
328
+ interface UploadOptions {
329
+ /** Generate and upload thumbnail for images (default: true) */
330
+ generateThumbnail?: boolean;
331
+ /** Generate local preview URL for images (default: true) */
332
+ generatePreview?: boolean;
333
+ }
334
+ /**
335
+ * FileUploadManager handles the upload lifecycle for files.
336
+ *
337
+ * Usage:
338
+ * 1. Call queueFiles() to create pending file objects for immediate UI display
339
+ * 2. Call executeUpload() for each file to start the actual upload
340
+ * 3. Receive state updates via the callback to update your framework's state
341
+ *
342
+ * @example
343
+ * ```typescript
344
+ * const manager = new FileUploadManager()
345
+ *
346
+ * // Queue files for UI display
347
+ * const pendingFiles = manager.queueFiles(selectedFiles)
348
+ * setState(prev => [...prev, ...pendingFiles])
349
+ *
350
+ * // Start uploads
351
+ * for (const pending of pendingFiles) {
352
+ * manager.executeUpload(threadId, file, pending.id, client, (updates) => {
353
+ * setState(prev => prev.map(f =>
354
+ * f.id === pending.id ? { ...f, ...updates } : f
355
+ * ))
356
+ * })
357
+ * }
358
+ * ```
359
+ */
360
+ declare class FileUploadManager {
361
+ /**
362
+ * Create a pending file object from a File.
363
+ * The returned object can be immediately added to state for UI feedback.
364
+ */
365
+ createPendingFile(file: File): ThreadFile;
366
+ /**
367
+ * Queue multiple files for upload.
368
+ * Returns an array of pending file objects paired with their source File objects.
369
+ *
370
+ * @param files - Files to queue (File array or FileList)
371
+ * @returns Array of [ThreadFile, File] tuples
372
+ */
373
+ queueFiles(files: File[] | FileList): Array<{
374
+ pending: ThreadFile;
375
+ file: File;
376
+ }>;
377
+ /**
378
+ * Execute the upload for a file.
379
+ *
380
+ * This method:
381
+ * 1. Generates a local preview for images (async, non-blocking)
382
+ * 2. Generates a thumbnail for images (if supported)
383
+ * 3. Uploads the file to the server
384
+ * 4. Calls onUpdate with state changes at each step
385
+ *
386
+ * @param threadId - The thread to upload to
387
+ * @param file - The file to upload
388
+ * @param pendingId - The ID of the pending file (from queueFiles)
389
+ * @param client - The AgentBuilderClient instance
390
+ * @param onUpdate - Callback for state updates
391
+ * @param options - Upload options
392
+ */
393
+ executeUpload(threadId: string, file: File, pendingId: string, client: AgentBuilderClient, onUpdate: FileUpdateCallback, options?: UploadOptions): Promise<AttachmentRef>;
394
+ /**
395
+ * Execute uploads for multiple files in parallel.
396
+ *
397
+ * @param threadId - The thread to upload to
398
+ * @param items - Array of pending/file pairs from queueFiles()
399
+ * @param client - The AgentBuilderClient instance
400
+ * @param onUpdate - Callback receiving (pendingId, updates) for each file
401
+ * @param options - Upload options
402
+ * @returns Array of upload results (AttachmentRef or Error for each file)
403
+ */
404
+ executeUploads(threadId: string, items: Array<{
405
+ pending: ThreadFile;
406
+ file: File;
407
+ }>, client: AgentBuilderClient, onUpdate: (pendingId: string, updates: Partial<ThreadFile>) => void, options?: UploadOptions): Promise<Array<AttachmentRef | Error>>;
408
+ }
409
+
410
+ /**
411
+ * Transform a flat list of messages into a list with workblocks.
412
+ * Groups consecutive assistant tool_calls and their tool results into WorkMessage objects.
413
+ *
414
+ * A workblock starts when an assistant message has tool_calls,
415
+ * and includes all subsequent tool messages until:
416
+ * - A non-tool message appears
417
+ * - Another assistant message without tool_calls appears
418
+ */
419
+ declare function transformToWorkblocks(messages: Message[]): ThreadMessage[];
420
+
421
+ /**
422
+ * Parse attachments JSON from a message
423
+ * Returns empty array if no attachments or invalid JSON
424
+ */
425
+ declare function parseAttachments(message: Message): AttachmentRef[];
426
+ /**
427
+ * Check if a MIME type represents an image
428
+ */
429
+ declare function isImageMimeType(mimeType: string): boolean;
430
+ /**
431
+ * Convert messages to ThreadFile array
432
+ * Extracts all committed files from message attachments
433
+ */
434
+ declare function messagesToFiles(messages: Message[]): ThreadFile[];
435
+
436
+ /**
437
+ * Image processing utilities for thumbnail generation
438
+ * These functions require a browser environment with Canvas support
439
+ */
440
+ /**
441
+ * Result of processing an image file
442
+ */
443
+ interface ImageProcessingResult {
444
+ /** Base64-encoded thumbnail data (without data URL prefix) */
445
+ thumbnail: string;
446
+ /** Original image width */
447
+ width: number;
448
+ /** Original image height */
449
+ height: number;
450
+ }
451
+ /**
452
+ * Process an image file to generate a thumbnail
453
+ * @param file - The image file to process
454
+ * @returns Promise with thumbnail data and dimensions
455
+ */
456
+ declare function generateImageThumbnail(file: File): Promise<ImageProcessingResult>;
457
+ /**
458
+ * Check if the current environment supports Canvas (for thumbnail generation)
459
+ */
460
+ declare function canGenerateThumbnails(): boolean;
461
+
462
+ /**
463
+ * File upload helper utilities
464
+ */
465
+ /**
466
+ * Generate a unique ID for pending file uploads.
467
+ * Uses timestamp + random string to ensure uniqueness.
468
+ */
469
+ declare function generatePendingFileId(): string;
470
+ /**
471
+ * Read a file as a data URL for image preview.
472
+ * Returns a base64-encoded data URL that can be used as an image src.
473
+ */
474
+ declare function readFileAsDataUrl(file: File): Promise<string>;
475
+
476
+ export { AgentBuilderClient, type AgentBuilderConfig, type AttachmentRef, type ConnectionStatus, type CreateThreadPayload, type CustomEvent, type ErrorEvent, type FileUpdateCallback, FileUploadManager, type GetMessagesOptions, type ImageProcessingResult, type LogDataEvent, type LogStreamEvent, type LogWebSocketCallbacks, type Message, type MessageChunkEvent, type MessageDataEvent, type MessageStreamEvent, type MessageWebSocketCallbacks, type SendMessagePayload, type StoppedByUserEvent, type Thread, type ThreadConnectionCallbacks, ThreadConnectionManager, type ThreadConnectionOptions, type ThreadEvent, type ThreadFile, type ThreadMessage, type UploadOptions, type WorkItem, type WorkMessage, canGenerateThumbnails, generateImageThumbnail, generatePendingFileId, isImageMimeType, messagesToFiles, parseAttachments, readFileAsDataUrl, transformToWorkblocks };