@trigger.dev/react-hooks 0.0.0-chat-prerelease-20260401205942

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 (71) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/dist/commonjs/contexts.d.ts +4 -0
  4. package/dist/commonjs/contexts.js +10 -0
  5. package/dist/commonjs/contexts.js.map +1 -0
  6. package/dist/commonjs/hooks/useApiClient.d.ts +41 -0
  7. package/dist/commonjs/hooks/useApiClient.js +44 -0
  8. package/dist/commonjs/hooks/useApiClient.js.map +1 -0
  9. package/dist/commonjs/hooks/useInputStreamSend.d.ts +26 -0
  10. package/dist/commonjs/hooks/useInputStreamSend.js +45 -0
  11. package/dist/commonjs/hooks/useInputStreamSend.js.map +1 -0
  12. package/dist/commonjs/hooks/useRealtime.d.ts +294 -0
  13. package/dist/commonjs/hooks/useRealtime.js +549 -0
  14. package/dist/commonjs/hooks/useRealtime.js.map +1 -0
  15. package/dist/commonjs/hooks/useRun.d.ts +22 -0
  16. package/dist/commonjs/hooks/useRun.js +40 -0
  17. package/dist/commonjs/hooks/useRun.js.map +1 -0
  18. package/dist/commonjs/hooks/useTaskTrigger.d.ts +107 -0
  19. package/dist/commonjs/hooks/useTaskTrigger.js +138 -0
  20. package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
  21. package/dist/commonjs/hooks/useWaitToken.d.ts +35 -0
  22. package/dist/commonjs/hooks/useWaitToken.js +52 -0
  23. package/dist/commonjs/hooks/useWaitToken.js.map +1 -0
  24. package/dist/commonjs/index.d.ts +7 -0
  25. package/dist/commonjs/index.js +24 -0
  26. package/dist/commonjs/index.js.map +1 -0
  27. package/dist/commonjs/package.json +3 -0
  28. package/dist/commonjs/utils/createContextAndHook.d.ts +15 -0
  29. package/dist/commonjs/utils/createContextAndHook.js +39 -0
  30. package/dist/commonjs/utils/createContextAndHook.js.map +1 -0
  31. package/dist/commonjs/utils/throttle.d.ts +6 -0
  32. package/dist/commonjs/utils/throttle.js +50 -0
  33. package/dist/commonjs/utils/throttle.js.map +1 -0
  34. package/dist/commonjs/utils/trigger-swr.d.ts +19 -0
  35. package/dist/commonjs/utils/trigger-swr.js +28 -0
  36. package/dist/commonjs/utils/trigger-swr.js.map +1 -0
  37. package/dist/esm/contexts.d.ts +4 -0
  38. package/dist/esm/contexts.js +5 -0
  39. package/dist/esm/contexts.js.map +1 -0
  40. package/dist/esm/hooks/useApiClient.d.ts +41 -0
  41. package/dist/esm/hooks/useApiClient.js +41 -0
  42. package/dist/esm/hooks/useApiClient.js.map +1 -0
  43. package/dist/esm/hooks/useInputStreamSend.d.ts +26 -0
  44. package/dist/esm/hooks/useInputStreamSend.js +39 -0
  45. package/dist/esm/hooks/useInputStreamSend.js.map +1 -0
  46. package/dist/esm/hooks/useRealtime.d.ts +294 -0
  47. package/dist/esm/hooks/useRealtime.js +542 -0
  48. package/dist/esm/hooks/useRealtime.js.map +1 -0
  49. package/dist/esm/hooks/useRun.d.ts +22 -0
  50. package/dist/esm/hooks/useRun.js +37 -0
  51. package/dist/esm/hooks/useRun.js.map +1 -0
  52. package/dist/esm/hooks/useTaskTrigger.d.ts +107 -0
  53. package/dist/esm/hooks/useTaskTrigger.js +130 -0
  54. package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
  55. package/dist/esm/hooks/useWaitToken.d.ts +35 -0
  56. package/dist/esm/hooks/useWaitToken.js +46 -0
  57. package/dist/esm/hooks/useWaitToken.js.map +1 -0
  58. package/dist/esm/index.d.ts +7 -0
  59. package/dist/esm/index.js +8 -0
  60. package/dist/esm/index.js.map +1 -0
  61. package/dist/esm/package.json +3 -0
  62. package/dist/esm/utils/createContextAndHook.d.ts +15 -0
  63. package/dist/esm/utils/createContextAndHook.js +31 -0
  64. package/dist/esm/utils/createContextAndHook.js.map +1 -0
  65. package/dist/esm/utils/throttle.d.ts +6 -0
  66. package/dist/esm/utils/throttle.js +47 -0
  67. package/dist/esm/utils/throttle.js.map +1 -0
  68. package/dist/esm/utils/trigger-swr.d.ts +19 -0
  69. package/dist/esm/utils/trigger-swr.js +6 -0
  70. package/dist/esm/utils/trigger-swr.js.map +1 -0
  71. package/package.json +75 -0
@@ -0,0 +1,294 @@
1
+ import { AnyTask, InferStreamType, RealtimeDefinedStream, RealtimeRun, RealtimeRunSkipColumns } from "@trigger.dev/core/v3";
2
+ import { UseApiClientOptions } from "./useApiClient.js";
3
+ export type UseRealtimeRunOptions = UseApiClientOptions & {
4
+ id?: string;
5
+ enabled?: boolean;
6
+ /**
7
+ * The number of milliseconds to throttle the stream updates.
8
+ *
9
+ * @default 16
10
+ */
11
+ throttleInMs?: number;
12
+ };
13
+ export type UseRealtimeSingleRunOptions<TTask extends AnyTask = AnyTask> = UseRealtimeRunOptions & {
14
+ /**
15
+ * Callback this is called when the run completes, an error occurs, or the subscription is stopped.
16
+ *
17
+ * @param {RealtimeRun<TTask>} run - The run object
18
+ * @param {Error} [err] - The error that occurred
19
+ */
20
+ onComplete?: (run: RealtimeRun<TTask>, err?: Error) => void;
21
+ /**
22
+ * Whether to stop the subscription when the run completes
23
+ *
24
+ * @default true
25
+ *
26
+ * Set this to false if you are making updates to the run metadata after completion through child runs
27
+ */
28
+ stopOnCompletion?: boolean;
29
+ /**
30
+ * Skip columns from the subscription.
31
+ *
32
+ * @default []
33
+ */
34
+ skipColumns?: RealtimeRunSkipColumns;
35
+ };
36
+ export type UseRealtimeRunInstance<TTask extends AnyTask = AnyTask> = {
37
+ run: RealtimeRun<TTask> | undefined;
38
+ error: Error | undefined;
39
+ /**
40
+ * Abort the current request immediately.
41
+ */
42
+ stop: () => void;
43
+ };
44
+ /**
45
+ * Hook to subscribe to realtime updates of a task run.
46
+ *
47
+ * @template TTask - The type of the task
48
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
49
+ * @param {UseRealtimeSingleRunOptions} [options] - Configuration options for the subscription
50
+ * @returns {UseRealtimeRunInstance<TTask>} An object containing the current state of the run, error handling, and control methods
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import type { myTask } from './path/to/task';
55
+ * const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
56
+ * ```
57
+ */
58
+ export declare function useRealtimeRun<TTask extends AnyTask>(runId?: string, options?: UseRealtimeSingleRunOptions<TTask>): UseRealtimeRunInstance<TTask>;
59
+ export type StreamResults<TStreams extends Record<string, any>> = {
60
+ [K in keyof TStreams]: Array<TStreams[K]>;
61
+ };
62
+ export type UseRealtimeRunWithStreamsInstance<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>> = {
63
+ run: RealtimeRun<TTask> | undefined;
64
+ streams: StreamResults<TStreams>;
65
+ error: Error | undefined;
66
+ /**
67
+ * Abort the current request immediately, keep the generated tokens if any.
68
+ */
69
+ stop: () => void;
70
+ };
71
+ /**
72
+ * Hook to subscribe to realtime updates of a task run with associated data streams.
73
+ *
74
+ * @template TTask - The type of the task
75
+ * @template TStreams - The type of the streams data
76
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
77
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
78
+ * @returns {UseRealtimeRunWithStreamsInstance<TTask, TStreams>} An object containing the current state of the run, streams data, and error handling
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * import type { myTask } from './path/to/task';
83
+ * const { run, streams, error } = useRealtimeRunWithStreams<typeof myTask, {
84
+ * output: string;
85
+ * }>('run-id-123');
86
+ * ```
87
+ */
88
+ export declare function useRealtimeRunWithStreams<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>>(runId?: string, options?: UseRealtimeSingleRunOptions<TTask>): UseRealtimeRunWithStreamsInstance<TTask, TStreams>;
89
+ export type UseRealtimeRunsInstance<TTask extends AnyTask = AnyTask> = {
90
+ runs: RealtimeRun<TTask>[];
91
+ error: Error | undefined;
92
+ /**
93
+ * Abort the current request immediately.
94
+ */
95
+ stop: () => void;
96
+ };
97
+ export type UseRealtimeRunsWithTagOptions = UseRealtimeRunOptions & {
98
+ /**
99
+ * Filter runs by the time they were created. You must specify the duration string like "1h", "10s", "30m", etc.
100
+ *
101
+ * @example
102
+ * "1h" - 1 hour ago
103
+ * "10s" - 10 seconds ago
104
+ * "30m" - 30 minutes ago
105
+ * "1d" - 1 day ago
106
+ * "1w" - 1 week ago
107
+ *
108
+ * The maximum duration is 1 week
109
+ *
110
+ * @note The timestamp will be calculated on the server side when you first subscribe to the runs.
111
+ *
112
+ */
113
+ createdAt?: string;
114
+ /**
115
+ * Skip columns from the subscription.
116
+ *
117
+ * @default []
118
+ */
119
+ skipColumns?: RealtimeRunSkipColumns;
120
+ };
121
+ /**
122
+ * Hook to subscribe to realtime updates of task runs filtered by tag(s).
123
+ *
124
+ * @template TTask - The type of the task
125
+ * @param {string | string[]} tag - The tag or array of tags to filter runs by
126
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
127
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs and any error encountered
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import type { myTask } from './path/to/task';
132
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag');
133
+ * // Or with multiple tags
134
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>(['tag1', 'tag2']);
135
+ * // Or with a createdAt filter
136
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag', { createdAt: '1h' });
137
+ * ```
138
+ */
139
+ export declare function useRealtimeRunsWithTag<TTask extends AnyTask>(tag: string | string[], options?: UseRealtimeRunsWithTagOptions): UseRealtimeRunsInstance<TTask>;
140
+ /**
141
+ * Hook to subscribe to realtime updates of a batch of task runs.
142
+ *
143
+ * @template TTask - The type of the task
144
+ * @param {string} batchId - The unique identifier of the batch to subscribe to
145
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
146
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs, error handling, and control methods
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * import type { myTask } from './path/to/task';
151
+ * const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
152
+ * ```
153
+ */
154
+ export declare function useRealtimeBatch<TTask extends AnyTask>(batchId: string, options?: UseRealtimeRunOptions): UseRealtimeRunsInstance<TTask>;
155
+ export type UseRealtimeStreamInstance<TPart> = {
156
+ parts: Array<TPart>;
157
+ error: Error | undefined;
158
+ /**
159
+ * Abort the current request immediately, keep the generated tokens if any.
160
+ */
161
+ stop: () => void;
162
+ };
163
+ export type UseRealtimeStreamOptions<TPart> = UseApiClientOptions & {
164
+ id?: string;
165
+ enabled?: boolean;
166
+ /**
167
+ * The number of milliseconds to throttle the stream updates.
168
+ *
169
+ * @default 16
170
+ */
171
+ throttleInMs?: number;
172
+ /**
173
+ * The number of seconds to wait for new data to be available,
174
+ * If no data arrives within the timeout, the stream will be closed.
175
+ *
176
+ * @default 60 seconds
177
+ */
178
+ timeoutInSeconds?: number;
179
+ /**
180
+ * The index to start reading from.
181
+ * If not provided, the stream will start from the beginning.
182
+ * @default 0
183
+ */
184
+ startIndex?: number;
185
+ /**
186
+ * Callback this is called when new data is received.
187
+ */
188
+ onData?: (data: TPart) => void;
189
+ };
190
+ export declare function useRealtimeStream<TDefinedStream extends RealtimeDefinedStream<any>>(stream: TDefinedStream, runId: string, options?: UseRealtimeStreamOptions<InferStreamType<TDefinedStream>>): UseRealtimeStreamInstance<InferStreamType<TDefinedStream>>;
191
+ /**
192
+ * Hook to subscribe to realtime updates of a stream with a specific stream key.
193
+ *
194
+ * This hook automatically subscribes to a stream and updates the `parts` array as new data arrives.
195
+ * The stream subscription is automatically managed: it starts when the component mounts (or when
196
+ * `enabled` becomes `true`) and stops when the component unmounts or when `stop()` is called.
197
+ *
198
+ * @template TPart - The type of each chunk/part in the stream
199
+ * @param runId - The unique identifier of the run to subscribe to
200
+ * @param streamKey - The unique identifier of the stream to subscribe to. Use this overload
201
+ * when you want to read from a specific stream key.
202
+ * @param options - Optional configuration for the stream subscription
203
+ * @returns An object containing:
204
+ * - `parts`: An array of all stream chunks received so far (accumulates over time)
205
+ * - `error`: Any error that occurred during subscription
206
+ * - `stop`: A function to manually stop the subscription
207
+ *
208
+ * @example
209
+ * ```tsx
210
+ * "use client";
211
+ * import { useRealtimeStream } from "@trigger.dev/react-hooks";
212
+ *
213
+ * function StreamViewer({ runId }: { runId: string }) {
214
+ * const { parts, error } = useRealtimeStream<string>(
215
+ * runId,
216
+ * "my-stream",
217
+ * {
218
+ * accessToken: process.env.NEXT_PUBLIC_TRIGGER_PUBLIC_KEY,
219
+ * }
220
+ * );
221
+ *
222
+ * if (error) return <div>Error: {error.message}</div>;
223
+ *
224
+ * // Parts array accumulates all chunks
225
+ * const fullText = parts.join("");
226
+ *
227
+ * return <div>{fullText}</div>;
228
+ * }
229
+ * ```
230
+ *
231
+ * @example
232
+ * ```tsx
233
+ * // With custom options
234
+ * const { parts, error, stop } = useRealtimeStream<ChatChunk>(
235
+ * runId,
236
+ * "chat-stream",
237
+ * {
238
+ * accessToken: publicKey,
239
+ * timeoutInSeconds: 120,
240
+ * startIndex: 10, // Start from the 10th chunk
241
+ * throttleInMs: 50, // Throttle updates to every 50ms
242
+ * onData: (chunk) => {
243
+ * console.log("New chunk received:", chunk);
244
+ * },
245
+ * }
246
+ * );
247
+ *
248
+ * // Manually stop the subscription
249
+ * <button onClick={stop}>Stop Stream</button>
250
+ * ```
251
+ */
252
+ export declare function useRealtimeStream<TPart>(runId: string, streamKey: string, options?: UseRealtimeStreamOptions<TPart>): UseRealtimeStreamInstance<TPart>;
253
+ /**
254
+ * Hook to subscribe to realtime updates of a stream using the default stream key (`"default"`).
255
+ *
256
+ * This is a convenience overload that allows you to subscribe to the default stream without
257
+ * specifying a stream key. The stream will be accessed with the key `"default"`.
258
+ *
259
+ * @template TPart - The type of each chunk/part in the stream
260
+ * @param runId - The unique identifier of the run to subscribe to
261
+ * @param options - Optional configuration for the stream subscription
262
+ * @returns An object containing:
263
+ * - `parts`: An array of all stream chunks received so far (accumulates over time)
264
+ * - `error`: Any error that occurred during subscription
265
+ * - `stop`: A function to manually stop the subscription
266
+ *
267
+ * @example
268
+ * ```tsx
269
+ * "use client";
270
+ * import { useRealtimeStream } from "@trigger.dev/react-hooks";
271
+ *
272
+ * function DefaultStreamViewer({ runId }: { runId: string }) {
273
+ * // Subscribe to the default stream
274
+ * const { parts, error } = useRealtimeStream<string>(runId, {
275
+ * accessToken: process.env.NEXT_PUBLIC_TRIGGER_PUBLIC_KEY,
276
+ * });
277
+ *
278
+ * if (error) return <div>Error: {error.message}</div>;
279
+ *
280
+ * const fullText = parts.join("");
281
+ * return <div>{fullText}</div>;
282
+ * }
283
+ * ```
284
+ *
285
+ * @example
286
+ * ```tsx
287
+ * // Conditionally enable the stream
288
+ * const { parts } = useRealtimeStream<string>(runId, {
289
+ * accessToken: publicKey,
290
+ * enabled: !!runId && isStreaming, // Only subscribe when runId exists and isStreaming is true
291
+ * });
292
+ * ```
293
+ */
294
+ export declare function useRealtimeStream<TPart>(runId: string, options?: UseRealtimeStreamOptions<TPart>): UseRealtimeStreamInstance<TPart>;