@trigger.dev/react-hooks 0.0.0-bun-runtimes-20260909131057

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 (89) 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 +47 -0
  7. package/dist/commonjs/hooks/useApiClient.js +57 -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 +336 -0
  13. package/dist/commonjs/hooks/useRealtime.js +642 -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/useSessionStream.d.ts +131 -0
  19. package/dist/commonjs/hooks/useSessionStream.js +236 -0
  20. package/dist/commonjs/hooks/useSessionStream.js.map +1 -0
  21. package/dist/commonjs/hooks/useSessionStreamChannel.d.ts +86 -0
  22. package/dist/commonjs/hooks/useSessionStreamChannel.js +209 -0
  23. package/dist/commonjs/hooks/useSessionStreamChannel.js.map +1 -0
  24. package/dist/commonjs/hooks/useTaskTrigger.d.ts +107 -0
  25. package/dist/commonjs/hooks/useTaskTrigger.js +139 -0
  26. package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
  27. package/dist/commonjs/hooks/useWaitToken.d.ts +35 -0
  28. package/dist/commonjs/hooks/useWaitToken.js +52 -0
  29. package/dist/commonjs/hooks/useWaitToken.js.map +1 -0
  30. package/dist/commonjs/index.d.ts +9 -0
  31. package/dist/commonjs/index.js +26 -0
  32. package/dist/commonjs/index.js.map +1 -0
  33. package/dist/commonjs/package.json +3 -0
  34. package/dist/commonjs/utils/createContextAndHook.d.ts +14 -0
  35. package/dist/commonjs/utils/createContextAndHook.js +38 -0
  36. package/dist/commonjs/utils/createContextAndHook.js.map +1 -0
  37. package/dist/commonjs/utils/throttle.d.ts +6 -0
  38. package/dist/commonjs/utils/throttle.js +50 -0
  39. package/dist/commonjs/utils/throttle.js.map +1 -0
  40. package/dist/commonjs/utils/trigger-swr.d.ts +19 -0
  41. package/dist/commonjs/utils/trigger-swr.js +27 -0
  42. package/dist/commonjs/utils/trigger-swr.js.map +1 -0
  43. package/dist/commonjs/utils/useStableRequestCallback.d.ts +5 -0
  44. package/dist/commonjs/utils/useStableRequestCallback.js +17 -0
  45. package/dist/commonjs/utils/useStableRequestCallback.js.map +1 -0
  46. package/dist/esm/contexts.d.ts +4 -0
  47. package/dist/esm/contexts.js +5 -0
  48. package/dist/esm/contexts.js.map +1 -0
  49. package/dist/esm/hooks/useApiClient.d.ts +47 -0
  50. package/dist/esm/hooks/useApiClient.js +54 -0
  51. package/dist/esm/hooks/useApiClient.js.map +1 -0
  52. package/dist/esm/hooks/useInputStreamSend.d.ts +26 -0
  53. package/dist/esm/hooks/useInputStreamSend.js +39 -0
  54. package/dist/esm/hooks/useInputStreamSend.js.map +1 -0
  55. package/dist/esm/hooks/useRealtime.d.ts +336 -0
  56. package/dist/esm/hooks/useRealtime.js +635 -0
  57. package/dist/esm/hooks/useRealtime.js.map +1 -0
  58. package/dist/esm/hooks/useRun.d.ts +22 -0
  59. package/dist/esm/hooks/useRun.js +37 -0
  60. package/dist/esm/hooks/useRun.js.map +1 -0
  61. package/dist/esm/hooks/useSessionStream.d.ts +131 -0
  62. package/dist/esm/hooks/useSessionStream.js +233 -0
  63. package/dist/esm/hooks/useSessionStream.js.map +1 -0
  64. package/dist/esm/hooks/useSessionStreamChannel.d.ts +86 -0
  65. package/dist/esm/hooks/useSessionStreamChannel.js +206 -0
  66. package/dist/esm/hooks/useSessionStreamChannel.js.map +1 -0
  67. package/dist/esm/hooks/useTaskTrigger.d.ts +107 -0
  68. package/dist/esm/hooks/useTaskTrigger.js +131 -0
  69. package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
  70. package/dist/esm/hooks/useWaitToken.d.ts +35 -0
  71. package/dist/esm/hooks/useWaitToken.js +46 -0
  72. package/dist/esm/hooks/useWaitToken.js.map +1 -0
  73. package/dist/esm/index.d.ts +9 -0
  74. package/dist/esm/index.js +10 -0
  75. package/dist/esm/index.js.map +1 -0
  76. package/dist/esm/package.json +3 -0
  77. package/dist/esm/utils/createContextAndHook.d.ts +14 -0
  78. package/dist/esm/utils/createContextAndHook.js +31 -0
  79. package/dist/esm/utils/createContextAndHook.js.map +1 -0
  80. package/dist/esm/utils/throttle.d.ts +6 -0
  81. package/dist/esm/utils/throttle.js +47 -0
  82. package/dist/esm/utils/throttle.js.map +1 -0
  83. package/dist/esm/utils/trigger-swr.d.ts +19 -0
  84. package/dist/esm/utils/trigger-swr.js +6 -0
  85. package/dist/esm/utils/trigger-swr.js.map +1 -0
  86. package/dist/esm/utils/useStableRequestCallback.d.ts +5 -0
  87. package/dist/esm/utils/useStableRequestCallback.js +14 -0
  88. package/dist/esm/utils/useStableRequestCallback.js.map +1 -0
  89. package/package.json +75 -0
@@ -0,0 +1,336 @@
1
+ import type { AnyTask, InferStreamType, RealtimeDefinedStream, RealtimeRun, RealtimeRunSkipColumns, SSEStreamPart } from "@trigger.dev/core/v3";
2
+ import type { 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
+ /**
158
+ * The event id of the last part seen. Persist this (e.g. to localStorage) and
159
+ * pass it back as the `lastEventId` option to resume the stream where you left
160
+ * off after a page reload. Updated on each throttled flush.
161
+ */
162
+ lastEventId: string | undefined;
163
+ error: Error | undefined;
164
+ /**
165
+ * Abort the current request immediately, keep the generated tokens if any.
166
+ */
167
+ stop: () => void;
168
+ };
169
+ export type UseRealtimeStreamOptions<TPart> = UseApiClientOptions & {
170
+ id?: string;
171
+ enabled?: boolean;
172
+ /**
173
+ * The number of milliseconds to throttle the stream updates.
174
+ *
175
+ * @default 16
176
+ */
177
+ throttleInMs?: number;
178
+ /**
179
+ * The number of seconds to wait for new data to be available,
180
+ * If no data arrives within the timeout, the stream will be closed.
181
+ *
182
+ * @default 60 seconds
183
+ */
184
+ timeoutInSeconds?: number;
185
+ /**
186
+ * The index to start reading from.
187
+ * If not provided, the stream will start from the beginning.
188
+ * @default 0
189
+ */
190
+ startIndex?: number;
191
+ /**
192
+ * The event id to resume from, as returned in `lastEventId`. Persist it across
193
+ * a page reload and pass it back to continue where the previous session left
194
+ * off, with no replay and no gap. Takes precedence over `startIndex` and
195
+ * `from`.
196
+ */
197
+ lastEventId?: string | number;
198
+ /**
199
+ * Where a fresh subscription starts reading.
200
+ *
201
+ * - `"beginning"` (default): replay the full stream history, then live-tail.
202
+ * - `"latest"`: start at the current tail (the latest record, then live
203
+ * updates) instead of replaying history, for a last-value / live view. On
204
+ * reconnect or remount the subscription resumes from the last record it
205
+ * saw, so no frames are missed and none are replayed.
206
+ *
207
+ * Ignored when `startIndex` is set (which pins an absolute start position).
208
+ */
209
+ from?: "beginning" | "latest";
210
+ /**
211
+ * Cap the number of parts kept in the accumulated `parts` array. When more
212
+ * than `maxParts` parts have been received, only the most recent `maxParts`
213
+ * are retained (older parts are dropped). Use `maxParts: 1` together with
214
+ * `from: "latest"` for a pure last-value view with bounded memory.
215
+ *
216
+ * When unset, `parts` accumulates every record for the lifetime of the
217
+ * subscription (the default).
218
+ */
219
+ maxParts?: number;
220
+ /**
221
+ * Callback this is called when new data is received.
222
+ */
223
+ onData?: (data: TPart) => void;
224
+ /**
225
+ * Callback invoked once per throttled flush with the batch of parts in that
226
+ * flush, each carrying its event `id`, `chunk` and `timestamp`. Use it to
227
+ * track the resume cursor without re-rendering on every record. Fires at the
228
+ * `throttleInMs` cadence, not per record.
229
+ */
230
+ onParts?: (parts: Array<SSEStreamPart<TPart>>) => void;
231
+ };
232
+ export declare function useRealtimeStream<TDefinedStream extends RealtimeDefinedStream<any>>(stream: TDefinedStream, runId: string, options?: UseRealtimeStreamOptions<InferStreamType<TDefinedStream>>): UseRealtimeStreamInstance<InferStreamType<TDefinedStream>>;
233
+ /**
234
+ * Hook to subscribe to realtime updates of a stream with a specific stream key.
235
+ *
236
+ * This hook automatically subscribes to a stream and updates the `parts` array as new data arrives.
237
+ * The stream subscription is automatically managed: it starts when the component mounts (or when
238
+ * `enabled` becomes `true`) and stops when the component unmounts or when `stop()` is called.
239
+ *
240
+ * @template TPart - The type of each chunk/part in the stream
241
+ * @param runId - The unique identifier of the run to subscribe to
242
+ * @param streamKey - The unique identifier of the stream to subscribe to. Use this overload
243
+ * when you want to read from a specific stream key.
244
+ * @param options - Optional configuration for the stream subscription
245
+ * @returns An object containing:
246
+ * - `parts`: An array of all stream chunks received so far (accumulates over time)
247
+ * - `error`: Any error that occurred during subscription
248
+ * - `stop`: A function to manually stop the subscription
249
+ *
250
+ * @example
251
+ * ```tsx
252
+ * "use client";
253
+ * import { useRealtimeStream } from "@trigger.dev/react-hooks";
254
+ *
255
+ * function StreamViewer({ runId }: { runId: string }) {
256
+ * const { parts, error } = useRealtimeStream<string>(
257
+ * runId,
258
+ * "my-stream",
259
+ * {
260
+ * accessToken: process.env.NEXT_PUBLIC_TRIGGER_PUBLIC_KEY,
261
+ * }
262
+ * );
263
+ *
264
+ * if (error) return <div>Error: {error.message}</div>;
265
+ *
266
+ * // Parts array accumulates all chunks
267
+ * const fullText = parts.join("");
268
+ *
269
+ * return <div>{fullText}</div>;
270
+ * }
271
+ * ```
272
+ *
273
+ * @example
274
+ * ```tsx
275
+ * // With custom options
276
+ * const { parts, error, stop } = useRealtimeStream<ChatChunk>(
277
+ * runId,
278
+ * "chat-stream",
279
+ * {
280
+ * accessToken: publicKey,
281
+ * timeoutInSeconds: 120,
282
+ * startIndex: 10, // Start from the 10th chunk
283
+ * throttleInMs: 50, // Throttle updates to every 50ms
284
+ * onData: (chunk) => {
285
+ * console.log("New chunk received:", chunk);
286
+ * },
287
+ * }
288
+ * );
289
+ *
290
+ * // Manually stop the subscription
291
+ * <button onClick={stop}>Stop Stream</button>
292
+ * ```
293
+ */
294
+ export declare function useRealtimeStream<TPart>(runId: string, streamKey: string, options?: UseRealtimeStreamOptions<TPart>): UseRealtimeStreamInstance<TPart>;
295
+ /**
296
+ * Hook to subscribe to realtime updates of a stream using the default stream key (`"default"`).
297
+ *
298
+ * This is a convenience overload that allows you to subscribe to the default stream without
299
+ * specifying a stream key. The stream will be accessed with the key `"default"`.
300
+ *
301
+ * @template TPart - The type of each chunk/part in the stream
302
+ * @param runId - The unique identifier of the run to subscribe to
303
+ * @param options - Optional configuration for the stream subscription
304
+ * @returns An object containing:
305
+ * - `parts`: An array of all stream chunks received so far (accumulates over time)
306
+ * - `error`: Any error that occurred during subscription
307
+ * - `stop`: A function to manually stop the subscription
308
+ *
309
+ * @example
310
+ * ```tsx
311
+ * "use client";
312
+ * import { useRealtimeStream } from "@trigger.dev/react-hooks";
313
+ *
314
+ * function DefaultStreamViewer({ runId }: { runId: string }) {
315
+ * // Subscribe to the default stream
316
+ * const { parts, error } = useRealtimeStream<string>(runId, {
317
+ * accessToken: process.env.NEXT_PUBLIC_TRIGGER_PUBLIC_KEY,
318
+ * });
319
+ *
320
+ * if (error) return <div>Error: {error.message}</div>;
321
+ *
322
+ * const fullText = parts.join("");
323
+ * return <div>{fullText}</div>;
324
+ * }
325
+ * ```
326
+ *
327
+ * @example
328
+ * ```tsx
329
+ * // Conditionally enable the stream
330
+ * const { parts } = useRealtimeStream<string>(runId, {
331
+ * accessToken: publicKey,
332
+ * enabled: !!runId && isStreaming, // Only subscribe when runId exists and isStreaming is true
333
+ * });
334
+ * ```
335
+ */
336
+ export declare function useRealtimeStream<TPart>(runId: string, options?: UseRealtimeStreamOptions<TPart>): UseRealtimeStreamInstance<TPart>;