@standardagents/react 0.8.1

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,457 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface Message {
5
+ id: string;
6
+ role: "system" | "user" | "assistant" | "tool";
7
+ content: string | null;
8
+ name?: string | null;
9
+ tool_calls?: string | null;
10
+ tool_call_id?: string | null;
11
+ log_id?: string | null;
12
+ created_at: number;
13
+ request_sent_at?: number | null;
14
+ response_completed_at?: number | null;
15
+ status?: "pending" | "completed" | "failed";
16
+ silent?: boolean;
17
+ tool_status?: "success" | "error" | null;
18
+ reasoning_content?: string | null;
19
+ reasoning_details?: string | null;
20
+ parent_id?: string | null;
21
+ depth?: number;
22
+ }
23
+ interface WorkItem {
24
+ id: string;
25
+ type: "tool_call" | "tool_result";
26
+ name?: string;
27
+ content: string | null;
28
+ status?: "pending" | "success" | "error" | null;
29
+ tool_call_id?: string;
30
+ }
31
+ interface WorkMessage {
32
+ id: string;
33
+ type: "workblock";
34
+ content: string | null;
35
+ reasoning_content?: string | null;
36
+ workItems: WorkItem[];
37
+ status: "pending" | "completed" | "failed";
38
+ created_at: number;
39
+ depth?: number;
40
+ }
41
+ type ThreadMessage = Message | WorkMessage;
42
+ interface AgentBuilderConfig {
43
+ endpoint: string;
44
+ }
45
+ interface UseThreadOptions {
46
+ preload?: boolean;
47
+ live?: boolean;
48
+ endpoint?: string;
49
+ stream?: boolean;
50
+ useWorkblocks?: boolean;
51
+ depth?: number;
52
+ }
53
+ interface GetMessagesOptions {
54
+ limit?: number;
55
+ offset?: number;
56
+ depth?: number;
57
+ includeSilent?: boolean;
58
+ }
59
+ interface MessageDataEvent {
60
+ type: "message_data";
61
+ message_id: string;
62
+ depth: number;
63
+ data: Message;
64
+ }
65
+ interface MessageChunkEvent {
66
+ type: "message_chunk";
67
+ message_id: string;
68
+ depth: number;
69
+ chunk: string;
70
+ }
71
+ interface ErrorEvent {
72
+ type: "error";
73
+ error: string;
74
+ }
75
+ /**
76
+ * Custom thread event emitted via emitThreadEvent on the backend
77
+ */
78
+ interface ThreadEvent<T = unknown> {
79
+ type: "event";
80
+ eventType: string;
81
+ data: T;
82
+ timestamp: number;
83
+ }
84
+ type MessageStreamEvent = MessageDataEvent | MessageChunkEvent | ErrorEvent | ThreadEvent;
85
+ interface LogDataEvent {
86
+ type: "log_data";
87
+ log_id: string;
88
+ data: any;
89
+ }
90
+ interface CustomEvent {
91
+ type: "custom";
92
+ customType: string;
93
+ value: any;
94
+ timestamp: number;
95
+ }
96
+ interface StoppedByUserEvent {
97
+ type: "stopped_by_user";
98
+ timestamp: number;
99
+ }
100
+ type LogStreamEvent = LogDataEvent | CustomEvent | StoppedByUserEvent;
101
+ interface SendMessagePayload {
102
+ role: "user" | "assistant" | "system";
103
+ content: string;
104
+ silent?: boolean;
105
+ }
106
+ interface Thread {
107
+ id: string;
108
+ agent_id: string;
109
+ metadata?: Record<string, any>;
110
+ created_at: number;
111
+ }
112
+ interface MessageWebSocketCallbacks {
113
+ onMessage?: (event: MessageDataEvent) => void;
114
+ onChunk?: (event: MessageChunkEvent) => void;
115
+ onEvent?: (event: ThreadEvent) => void;
116
+ onError?: (event: ErrorEvent) => void;
117
+ onOpen?: () => void;
118
+ onClose?: () => void;
119
+ }
120
+ interface LogWebSocketCallbacks {
121
+ onLog?: (event: LogDataEvent) => void;
122
+ onCustom?: (event: CustomEvent) => void;
123
+ onStopped?: (event: StoppedByUserEvent) => void;
124
+ onOpen?: () => void;
125
+ onClose?: () => void;
126
+ }
127
+ interface ThreadProviderOptions {
128
+ /** Maximum message depth to fetch/stream */
129
+ depth?: number;
130
+ /** Whether to include silent messages */
131
+ includeSilent?: boolean;
132
+ }
133
+
134
+ interface AgentBuilderProviderProps {
135
+ config: AgentBuilderConfig;
136
+ children: ReactNode;
137
+ }
138
+ /**
139
+ * AgentBuilderProvider provides the AgentBuilder client instance to all child components.
140
+ * This should wrap the part of your app where you want to use AgentBuilder functionality.
141
+ *
142
+ * @example
143
+ * ```tsx
144
+ * <AgentBuilderProvider config={{ endpoint: 'https://api.example.com' }}>
145
+ * <YourApp />
146
+ * </AgentBuilderProvider>
147
+ * ```
148
+ */
149
+ declare function AgentBuilderProvider({ config, children }: AgentBuilderProviderProps): react_jsx_runtime.JSX.Element;
150
+
151
+ /**
152
+ * Event listener callback type
153
+ */
154
+ type EventListener<T = unknown> = (data: T) => void;
155
+ /**
156
+ * Thread context value
157
+ */
158
+ interface ThreadContextValue {
159
+ /** The thread ID */
160
+ threadId: string;
161
+ /** Current messages in the thread */
162
+ messages: Message[];
163
+ /** Whether messages are currently loading */
164
+ loading: boolean;
165
+ /** Any error that occurred */
166
+ error: Error | null;
167
+ /** Subscribe to a specific event type */
168
+ subscribeToEvent: <T = unknown>(eventType: string, listener: EventListener<T>) => () => void;
169
+ /** Options passed to the provider */
170
+ options: ThreadProviderOptions;
171
+ }
172
+ interface ThreadProviderProps {
173
+ /** The thread ID to connect to */
174
+ threadId: string;
175
+ /** Provider options */
176
+ options?: ThreadProviderOptions;
177
+ /** Whether to preload messages on mount (default: true) */
178
+ preload?: boolean;
179
+ /** Whether to enable live updates via WebSocket (default: true) */
180
+ live?: boolean;
181
+ /** Maximum message depth to fetch/stream (default: 0 for top-level only) */
182
+ depth?: number;
183
+ /** Whether to include silent messages (default: false) */
184
+ includeSilent?: boolean;
185
+ /** Optional endpoint override */
186
+ endpoint?: string;
187
+ children: ReactNode;
188
+ }
189
+ /**
190
+ * ThreadProvider establishes a WebSocket connection to a thread and provides
191
+ * context for child components to access messages and events.
192
+ *
193
+ * Must be nested inside AgentBuilderProvider.
194
+ *
195
+ * @example
196
+ * ```tsx
197
+ * <AgentBuilderProvider config={{ endpoint: 'https://api.example.com' }}>
198
+ * <ThreadProvider threadId="thread-123">
199
+ * <ChatMessages />
200
+ * </ThreadProvider>
201
+ * </AgentBuilderProvider>
202
+ * ```
203
+ */
204
+ declare function ThreadProvider({ threadId, options, preload, live, depth, includeSilent, endpoint: endpointOverride, children, }: ThreadProviderProps): react_jsx_runtime.JSX.Element;
205
+ /**
206
+ * Hook to access the thread context.
207
+ * Must be used within a ThreadProvider.
208
+ *
209
+ * @throws Error if used outside of ThreadProvider
210
+ */
211
+ declare function useThreadContext(): ThreadContextValue;
212
+ /**
213
+ * Hook to get the current thread ID from context.
214
+ * Must be used within a ThreadProvider.
215
+ */
216
+ declare function useThreadId(): string;
217
+
218
+ /**
219
+ * Hook to get messages from a thread.
220
+ *
221
+ * Must be used within a ThreadProvider. The thread ID and WebSocket connection
222
+ * are managed by the ThreadProvider, so this hook simply returns the messages.
223
+ *
224
+ * @param options - Configuration options
225
+ * @returns Array of messages (raw or transformed to workblocks)
226
+ *
227
+ * @example
228
+ * ```tsx
229
+ * function ChatMessages() {
230
+ * // Basic usage - returns messages from context
231
+ * const messages = useThread()
232
+ *
233
+ * return (
234
+ * <div>
235
+ * {messages.map(msg => <Message key={msg.id} message={msg} />)}
236
+ * </div>
237
+ * )
238
+ * }
239
+ *
240
+ * // Wrap with ThreadProvider
241
+ * <ThreadProvider threadId="thread-123">
242
+ * <ChatMessages />
243
+ * </ThreadProvider>
244
+ * ```
245
+ *
246
+ * @example
247
+ * ```tsx
248
+ * // Disable workblocks transformation
249
+ * const messages = useThread({ useWorkblocks: false })
250
+ * ```
251
+ */
252
+ declare function useThread(options?: UseThreadOptions): ThreadMessage[];
253
+
254
+ /**
255
+ * Hook to listen for custom events emitted from a thread via the stream WebSocket.
256
+ * Calls the provided callback whenever an event of the specified type is received.
257
+ *
258
+ * Must be used within a ThreadProvider. Events are emitted from the backend
259
+ * using `emitThreadEvent(flow, 'event-type', data)`.
260
+ *
261
+ * @param type - The custom event type to filter for
262
+ * @param callback - Function to call when an event of this type is received
263
+ *
264
+ * @example
265
+ * ```tsx
266
+ * function GamePreview() {
267
+ * const [gameHtml, setGameHtml] = useState<string | null>(null)
268
+ *
269
+ * onThreadEvent('game_built', (data: { success: boolean }) => {
270
+ * if (data.success) {
271
+ * fetchGameHtml()
272
+ * }
273
+ * })
274
+ *
275
+ * return <iframe srcDoc={gameHtml} />
276
+ * }
277
+ * ```
278
+ *
279
+ * @example
280
+ * ```tsx
281
+ * function ProgressIndicator() {
282
+ * const [progress, setProgress] = useState(0)
283
+ *
284
+ * onThreadEvent('progress', (data: { step: number; total: number }) => {
285
+ * setProgress(data.step / data.total * 100)
286
+ * })
287
+ *
288
+ * return <ProgressBar value={progress} />
289
+ * }
290
+ * ```
291
+ */
292
+ declare function onThreadEvent<T = unknown>(type: string, callback: (data: T) => void): void;
293
+ /**
294
+ * Hook to get the latest event data for a specific event type as React state.
295
+ * Returns the most recent event data, or null if no event has been received.
296
+ *
297
+ * Must be used within a ThreadProvider. Events are emitted from the backend
298
+ * using `emitThreadEvent(flow, 'event-type', data)`.
299
+ *
300
+ * @param type - The custom event type to filter for
301
+ * @returns The latest event data matching the specified type, or null if none received
302
+ *
303
+ * @example
304
+ * ```tsx
305
+ * function ProgressIndicator() {
306
+ * const progress = useThreadEvent<{ step: number; total: number }>('progress')
307
+ *
308
+ * if (!progress) return null
309
+ *
310
+ * return (
311
+ * <div>
312
+ * Step {progress.step} of {progress.total}
313
+ * </div>
314
+ * )
315
+ * }
316
+ * ```
317
+ *
318
+ * @example
319
+ * ```tsx
320
+ * function StatusDisplay() {
321
+ * const status = useThreadEvent<{ message: string }>('status')
322
+ *
323
+ * useEffect(() => {
324
+ * if (status) {
325
+ * console.log('Status updated:', status.message)
326
+ * }
327
+ * }, [status])
328
+ *
329
+ * return <div>{status?.message ?? 'No status'}</div>
330
+ * }
331
+ * ```
332
+ */
333
+ declare function useThreadEvent<T = unknown>(type: string): T | null;
334
+
335
+ /**
336
+ * Hook that returns a function to send messages to the current thread.
337
+ * Must be used within a ThreadProvider.
338
+ *
339
+ * This hook automatically uses the thread ID from the ThreadProvider context,
340
+ * so you don't need to pass it manually.
341
+ *
342
+ * @returns A function that sends a message to the current thread
343
+ *
344
+ * @throws Error if used outside of ThreadProvider
345
+ *
346
+ * @example
347
+ * ```tsx
348
+ * function ChatInput() {
349
+ * const sendMessage = useSendMessage()
350
+ *
351
+ * const handleSubmit = async (content: string) => {
352
+ * await sendMessage({
353
+ * role: 'user',
354
+ * content,
355
+ * })
356
+ * }
357
+ *
358
+ * return <input onSubmit={handleSubmit} />
359
+ * }
360
+ * ```
361
+ */
362
+ declare function useSendMessage(): (payload: SendMessagePayload) => Promise<Message>;
363
+
364
+ /**
365
+ * Hook that returns a function to stop the current thread's execution.
366
+ * Must be used within a ThreadProvider.
367
+ *
368
+ * This hook automatically uses the thread ID from the ThreadProvider context,
369
+ * so you don't need to pass it manually.
370
+ *
371
+ * @returns A function that stops the current thread's execution
372
+ *
373
+ * @throws Error if used outside of ThreadProvider
374
+ *
375
+ * @example
376
+ * ```tsx
377
+ * function StopButton() {
378
+ * const stopThread = useStopThread()
379
+ *
380
+ * return (
381
+ * <button onClick={() => stopThread()}>
382
+ * Stop
383
+ * </button>
384
+ * )
385
+ * }
386
+ * ```
387
+ */
388
+ declare function useStopThread(): () => Promise<void>;
389
+
390
+ /**
391
+ * Send a message to a specific thread.
392
+ *
393
+ * This is a standalone function that sends messages to threads.
394
+ * It requires that an AgentBuilderProvider is mounted somewhere in your app
395
+ * to set the global endpoint configuration.
396
+ *
397
+ * @param id - The thread ID to send the message to
398
+ * @param payload - The message payload containing role, content, and optional silent flag
399
+ * @returns Promise resolving to the created message
400
+ *
401
+ * @throws Error if called before AgentBuilderProvider is mounted
402
+ *
403
+ * @example
404
+ * ```tsx
405
+ * import { sendMessage } from '@standardagents/react'
406
+ *
407
+ * await sendMessage('thread-123', {
408
+ * role: 'user',
409
+ * content: 'Hello, agent!',
410
+ * })
411
+ *
412
+ * // Send a silent message
413
+ * await sendMessage('thread-123', {
414
+ * role: 'user',
415
+ * content: 'Silent message',
416
+ * silent: true,
417
+ * })
418
+ * ```
419
+ */
420
+ declare function sendMessage(id: string, payload: SendMessagePayload): Promise<Message>;
421
+
422
+ interface StopThreadOptions {
423
+ /**
424
+ * Override the endpoint URL for this request.
425
+ * If not provided, uses the endpoint from AgentBuilderProvider.
426
+ */
427
+ endpoint?: string;
428
+ }
429
+ /**
430
+ * Stop execution of a thread.
431
+ *
432
+ * This is a standalone function that stops a running thread execution.
433
+ * It requires that an AgentBuilderProvider is mounted somewhere in your app
434
+ * to set the global endpoint configuration, or you can provide a custom endpoint.
435
+ *
436
+ * @param id - The thread ID to stop
437
+ * @param options - Optional configuration including custom endpoint
438
+ * @returns Promise that resolves when the thread is stopped
439
+ *
440
+ * @throws Error if called before AgentBuilderProvider is mounted and no endpoint is provided
441
+ *
442
+ * @example
443
+ * ```tsx
444
+ * import { stopThread } from '@standardagents/react'
445
+ *
446
+ * // Using global endpoint from AgentBuilderProvider
447
+ * await stopThread('thread-123')
448
+ *
449
+ * // Using custom endpoint
450
+ * await stopThread('thread-123', {
451
+ * endpoint: 'https://custom.example.com/api'
452
+ * })
453
+ * ```
454
+ */
455
+ declare function stopThread(id: string, options?: StopThreadOptions): Promise<void>;
456
+
457
+ export { type AgentBuilderConfig, AgentBuilderProvider, type CustomEvent, type ErrorEvent, type GetMessagesOptions, type LogDataEvent, type LogStreamEvent, type LogWebSocketCallbacks, type Message, type MessageChunkEvent, type MessageDataEvent, type MessageStreamEvent, type MessageWebSocketCallbacks, type SendMessagePayload, type StoppedByUserEvent, type Thread, type ThreadEvent, type ThreadMessage, ThreadProvider, type ThreadProviderOptions, type UseThreadOptions, type WorkItem, type WorkMessage, onThreadEvent, sendMessage, stopThread, useSendMessage, useStopThread, useThread, useThreadContext, useThreadEvent, useThreadId };