@trigger.dev/react-hooks 0.0.0-v3-prerelease-20241118124944 → 0.0.0-v3-prerelease-20250108131948

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 (63) hide show
  1. package/dist/commonjs/contexts.d.ts +2 -2
  2. package/dist/commonjs/contexts.js +3 -2
  3. package/dist/commonjs/contexts.js.map +1 -1
  4. package/dist/commonjs/hooks/useApiClient.d.ts +39 -2
  5. package/dist/commonjs/hooks/useApiClient.js +34 -6
  6. package/dist/commonjs/hooks/useApiClient.js.map +1 -1
  7. package/dist/commonjs/hooks/useRealtime.d.ts +117 -0
  8. package/dist/commonjs/hooks/useRealtime.js +409 -0
  9. package/dist/commonjs/hooks/useRealtime.js.map +1 -0
  10. package/dist/commonjs/hooks/useRun.js +7 -2
  11. package/dist/commonjs/hooks/useRun.js.map +1 -1
  12. package/dist/commonjs/hooks/useTaskTrigger.d.ts +101 -0
  13. package/dist/commonjs/hooks/useTaskTrigger.js +137 -0
  14. package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
  15. package/dist/commonjs/index.d.ts +2 -3
  16. package/dist/commonjs/index.js +2 -3
  17. package/dist/commonjs/index.js.map +1 -1
  18. package/dist/commonjs/utils/throttle.d.ts +6 -0
  19. package/dist/commonjs/utils/throttle.js +50 -0
  20. package/dist/commonjs/utils/throttle.js.map +1 -0
  21. package/dist/commonjs/utils/trigger-swr.d.ts +12 -0
  22. package/dist/commonjs/utils/trigger-swr.js.map +1 -1
  23. package/dist/esm/contexts.d.ts +2 -2
  24. package/dist/esm/contexts.js +2 -2
  25. package/dist/esm/contexts.js.map +1 -1
  26. package/dist/esm/hooks/useApiClient.d.ts +39 -2
  27. package/dist/esm/hooks/useApiClient.js +35 -7
  28. package/dist/esm/hooks/useApiClient.js.map +1 -1
  29. package/dist/esm/hooks/useRealtime.d.ts +117 -0
  30. package/dist/esm/hooks/useRealtime.js +403 -0
  31. package/dist/esm/hooks/useRealtime.js.map +1 -0
  32. package/dist/esm/hooks/useRun.js +7 -2
  33. package/dist/esm/hooks/useRun.js.map +1 -1
  34. package/dist/esm/hooks/useTaskTrigger.d.ts +101 -0
  35. package/dist/esm/hooks/useTaskTrigger.js +129 -0
  36. package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
  37. package/dist/esm/index.d.ts +2 -3
  38. package/dist/esm/index.js +2 -3
  39. package/dist/esm/index.js.map +1 -1
  40. package/dist/esm/utils/throttle.d.ts +6 -0
  41. package/dist/esm/utils/throttle.js +47 -0
  42. package/dist/esm/utils/throttle.js.map +1 -0
  43. package/dist/esm/utils/trigger-swr.d.ts +12 -0
  44. package/dist/esm/utils/trigger-swr.js.map +1 -1
  45. package/package.json +4 -4
  46. package/dist/commonjs/hooks/useRealtimeBatch.d.ts +0 -19
  47. package/dist/commonjs/hooks/useRealtimeBatch.js +0 -56
  48. package/dist/commonjs/hooks/useRealtimeBatch.js.map +0 -1
  49. package/dist/commonjs/hooks/useRealtimeRun.d.ts +0 -18
  50. package/dist/commonjs/hooks/useRealtimeRun.js +0 -40
  51. package/dist/commonjs/hooks/useRealtimeRun.js.map +0 -1
  52. package/dist/commonjs/hooks/useRealtimeRunsWithTag.d.ts +0 -5
  53. package/dist/commonjs/hooks/useRealtimeRunsWithTag.js +0 -45
  54. package/dist/commonjs/hooks/useRealtimeRunsWithTag.js.map +0 -1
  55. package/dist/esm/hooks/useRealtimeBatch.d.ts +0 -19
  56. package/dist/esm/hooks/useRealtimeBatch.js +0 -53
  57. package/dist/esm/hooks/useRealtimeBatch.js.map +0 -1
  58. package/dist/esm/hooks/useRealtimeRun.d.ts +0 -18
  59. package/dist/esm/hooks/useRealtimeRun.js +0 -37
  60. package/dist/esm/hooks/useRealtimeRun.js.map +0 -1
  61. package/dist/esm/hooks/useRealtimeRunsWithTag.d.ts +0 -5
  62. package/dist/esm/hooks/useRealtimeRunsWithTag.js +0 -42
  63. package/dist/esm/hooks/useRealtimeRunsWithTag.js.map +0 -1
@@ -0,0 +1,117 @@
1
+ import { AnyTask, RealtimeRun } from "@trigger.dev/core/v3";
2
+ import { UseApiClientOptions } from "./useApiClient.js";
3
+ export type UseRealtimeRunOptions = UseApiClientOptions & {
4
+ id?: string;
5
+ enabled?: boolean;
6
+ experimental_throttleInMs?: number;
7
+ };
8
+ export type UseRealtimeSingleRunOptions<TTask extends AnyTask = AnyTask> = UseRealtimeRunOptions & {
9
+ /**
10
+ * Callback this is called when the run completes, an error occurs, or the subscription is stopped.
11
+ *
12
+ * @param {RealtimeRun<TTask>} run - The run object
13
+ * @param {Error} [err] - The error that occurred
14
+ */
15
+ onComplete?: (run: RealtimeRun<TTask>, err?: Error) => void;
16
+ /**
17
+ * Whether to stop the subscription when the run completes
18
+ *
19
+ * @default true
20
+ *
21
+ * Set this to false if you are making updates to the run metadata after completion through child runs
22
+ */
23
+ stopOnCompletion?: boolean;
24
+ };
25
+ export type UseRealtimeRunInstance<TTask extends AnyTask = AnyTask> = {
26
+ run: RealtimeRun<TTask> | undefined;
27
+ error: Error | undefined;
28
+ /**
29
+ * Abort the current request immediately.
30
+ */
31
+ stop: () => void;
32
+ };
33
+ /**
34
+ * Hook to subscribe to realtime updates of a task run.
35
+ *
36
+ * @template TTask - The type of the task
37
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
38
+ * @param {UseRealtimeSingleRunOptions} [options] - Configuration options for the subscription
39
+ * @returns {UseRealtimeRunInstance<TTask>} An object containing the current state of the run, error handling, and control methods
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import type { myTask } from './path/to/task';
44
+ * const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
45
+ * ```
46
+ */
47
+ export declare function useRealtimeRun<TTask extends AnyTask>(runId?: string, options?: UseRealtimeSingleRunOptions<TTask>): UseRealtimeRunInstance<TTask>;
48
+ export type StreamResults<TStreams extends Record<string, any>> = {
49
+ [K in keyof TStreams]: Array<TStreams[K]>;
50
+ };
51
+ export type UseRealtimeRunWithStreamsInstance<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>> = {
52
+ run: RealtimeRun<TTask> | undefined;
53
+ streams: StreamResults<TStreams>;
54
+ error: Error | undefined;
55
+ /**
56
+ * Abort the current request immediately, keep the generated tokens if any.
57
+ */
58
+ stop: () => void;
59
+ };
60
+ /**
61
+ * Hook to subscribe to realtime updates of a task run with associated data streams.
62
+ *
63
+ * @template TTask - The type of the task
64
+ * @template TStreams - The type of the streams data
65
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
66
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
67
+ * @returns {UseRealtimeRunWithStreamsInstance<TTask, TStreams>} An object containing the current state of the run, streams data, and error handling
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * import type { myTask } from './path/to/task';
72
+ * const { run, streams, error } = useRealtimeRunWithStreams<typeof myTask, {
73
+ * output: string;
74
+ * }>('run-id-123');
75
+ * ```
76
+ */
77
+ export declare function useRealtimeRunWithStreams<TTask extends AnyTask = AnyTask, TStreams extends Record<string, any> = Record<string, any>>(runId?: string, options?: UseRealtimeSingleRunOptions<TTask>): UseRealtimeRunWithStreamsInstance<TTask, TStreams>;
78
+ export type UseRealtimeRunsInstance<TTask extends AnyTask = AnyTask> = {
79
+ runs: RealtimeRun<TTask>[];
80
+ error: Error | undefined;
81
+ /**
82
+ * Abort the current request immediately.
83
+ */
84
+ stop: () => void;
85
+ };
86
+ /**
87
+ * Hook to subscribe to realtime updates of task runs filtered by tag(s).
88
+ *
89
+ * @template TTask - The type of the task
90
+ * @param {string | string[]} tag - The tag or array of tags to filter runs by
91
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
92
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs and any error encountered
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * import type { myTask } from './path/to/task';
97
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag');
98
+ * // Or with multiple tags
99
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>(['tag1', 'tag2']);
100
+ * ```
101
+ */
102
+ export declare function useRealtimeRunsWithTag<TTask extends AnyTask>(tag: string | string[], options?: UseRealtimeRunOptions): UseRealtimeRunsInstance<TTask>;
103
+ /**
104
+ * Hook to subscribe to realtime updates of a batch of task runs.
105
+ *
106
+ * @template TTask - The type of the task
107
+ * @param {string} batchId - The unique identifier of the batch to subscribe to
108
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
109
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs, error handling, and control methods
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * import type { myTask } from './path/to/task';
114
+ * const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
115
+ * ```
116
+ */
117
+ export declare function useRealtimeBatch<TTask extends AnyTask>(batchId: string, options?: UseRealtimeRunOptions): UseRealtimeRunsInstance<TTask>;
@@ -0,0 +1,403 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
3
+ import { useSWR } from "../utils/trigger-swr.js";
4
+ import { useApiClient } from "./useApiClient.js";
5
+ import { createThrottledQueue } from "../utils/throttle.js";
6
+ /**
7
+ * Hook to subscribe to realtime updates of a task run.
8
+ *
9
+ * @template TTask - The type of the task
10
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
11
+ * @param {UseRealtimeSingleRunOptions} [options] - Configuration options for the subscription
12
+ * @returns {UseRealtimeRunInstance<TTask>} An object containing the current state of the run, error handling, and control methods
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import type { myTask } from './path/to/task';
17
+ * const { run, error } = useRealtimeRun<typeof myTask>('run-id-123');
18
+ * ```
19
+ */
20
+ export function useRealtimeRun(runId, options) {
21
+ const hookId = useId();
22
+ const idKey = options?.id ?? hookId;
23
+ // Store the streams state in SWR, using the idKey as the key to share states.
24
+ const { data: run, mutate: mutateRun } = useSWR([idKey, "run"], null);
25
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, "error"], null);
26
+ // Add state to track when the subscription is complete
27
+ const { data: isComplete = false, mutate: setIsComplete } = useSWR([idKey, "complete"], null);
28
+ // Abort controller to cancel the current API call.
29
+ const abortControllerRef = useRef(null);
30
+ const stop = useCallback(() => {
31
+ if (abortControllerRef.current) {
32
+ abortControllerRef.current.abort();
33
+ abortControllerRef.current = null;
34
+ }
35
+ }, []);
36
+ const apiClient = useApiClient(options);
37
+ const triggerRequest = useCallback(async () => {
38
+ try {
39
+ if (!runId || !apiClient) {
40
+ return;
41
+ }
42
+ const abortController = new AbortController();
43
+ abortControllerRef.current = abortController;
44
+ await processRealtimeRun(runId, apiClient, mutateRun, abortControllerRef, typeof options?.stopOnCompletion === "boolean" ? options.stopOnCompletion : true);
45
+ }
46
+ catch (err) {
47
+ // Ignore abort errors as they are expected.
48
+ if (err.name === "AbortError") {
49
+ abortControllerRef.current = null;
50
+ return;
51
+ }
52
+ setError(err);
53
+ }
54
+ finally {
55
+ if (abortControllerRef.current) {
56
+ abortControllerRef.current = null;
57
+ }
58
+ // Mark the subscription as complete
59
+ setIsComplete(true);
60
+ }
61
+ }, [runId, mutateRun, abortControllerRef, apiClient, setError]);
62
+ const hasCalledOnCompleteRef = useRef(false);
63
+ // Effect to handle onComplete callback
64
+ useEffect(() => {
65
+ if (isComplete && run && options?.onComplete && !hasCalledOnCompleteRef.current) {
66
+ options.onComplete(run, error);
67
+ hasCalledOnCompleteRef.current = true;
68
+ }
69
+ }, [isComplete, run, error, options?.onComplete]);
70
+ useEffect(() => {
71
+ if (typeof options?.enabled === "boolean" && !options.enabled) {
72
+ return;
73
+ }
74
+ if (!runId) {
75
+ return;
76
+ }
77
+ triggerRequest().finally(() => { });
78
+ return () => {
79
+ stop();
80
+ };
81
+ }, [runId, stop, options?.enabled]);
82
+ return { run, error, stop };
83
+ }
84
+ /**
85
+ * Hook to subscribe to realtime updates of a task run with associated data streams.
86
+ *
87
+ * @template TTask - The type of the task
88
+ * @template TStreams - The type of the streams data
89
+ * @param {string} [runId] - The unique identifier of the run to subscribe to
90
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
91
+ * @returns {UseRealtimeRunWithStreamsInstance<TTask, TStreams>} An object containing the current state of the run, streams data, and error handling
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * import type { myTask } from './path/to/task';
96
+ * const { run, streams, error } = useRealtimeRunWithStreams<typeof myTask, {
97
+ * output: string;
98
+ * }>('run-id-123');
99
+ * ```
100
+ */
101
+ export function useRealtimeRunWithStreams(runId, options) {
102
+ const hookId = useId();
103
+ const idKey = options?.id ?? hookId;
104
+ const [initialStreamsFallback] = useState({});
105
+ // Store the streams state in SWR, using the idKey as the key to share states.
106
+ const { data: streams, mutate: mutateStreams } = useSWR([idKey, "streams"], null, {
107
+ fallbackData: initialStreamsFallback,
108
+ });
109
+ // Keep the latest streams in a ref.
110
+ const streamsRef = useRef(streams ?? {});
111
+ useEffect(() => {
112
+ streamsRef.current = streams || {};
113
+ }, [streams]);
114
+ // Store the streams state in SWR, using the idKey as the key to share states.
115
+ const { data: run, mutate: mutateRun } = useSWR([idKey, "run"], null);
116
+ // Add state to track when the subscription is complete
117
+ const { data: isComplete = false, mutate: setIsComplete } = useSWR([idKey, "complete"], null);
118
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, "error"], null);
119
+ // Abort controller to cancel the current API call.
120
+ const abortControllerRef = useRef(null);
121
+ const stop = useCallback(() => {
122
+ if (abortControllerRef.current) {
123
+ abortControllerRef.current.abort();
124
+ abortControllerRef.current = null;
125
+ }
126
+ }, []);
127
+ const apiClient = useApiClient(options);
128
+ const triggerRequest = useCallback(async () => {
129
+ try {
130
+ if (!runId || !apiClient) {
131
+ return;
132
+ }
133
+ const abortController = new AbortController();
134
+ abortControllerRef.current = abortController;
135
+ await processRealtimeRunWithStreams(runId, apiClient, mutateRun, mutateStreams, streamsRef, abortControllerRef, typeof options?.stopOnCompletion === "boolean" ? options.stopOnCompletion : true, options?.experimental_throttleInMs);
136
+ }
137
+ catch (err) {
138
+ // Ignore abort errors as they are expected.
139
+ if (err.name === "AbortError") {
140
+ abortControllerRef.current = null;
141
+ return;
142
+ }
143
+ setError(err);
144
+ }
145
+ finally {
146
+ if (abortControllerRef.current) {
147
+ abortControllerRef.current = null;
148
+ }
149
+ // Mark the subscription as complete
150
+ setIsComplete(true);
151
+ }
152
+ }, [runId, mutateRun, mutateStreams, streamsRef, abortControllerRef, apiClient, setError]);
153
+ const hasCalledOnCompleteRef = useRef(false);
154
+ // Effect to handle onComplete callback
155
+ useEffect(() => {
156
+ if (isComplete && run && options?.onComplete && !hasCalledOnCompleteRef.current) {
157
+ options.onComplete(run, error);
158
+ hasCalledOnCompleteRef.current = true;
159
+ }
160
+ }, [isComplete, run, error, options?.onComplete]);
161
+ useEffect(() => {
162
+ if (typeof options?.enabled === "boolean" && !options.enabled) {
163
+ return;
164
+ }
165
+ if (!runId) {
166
+ return;
167
+ }
168
+ triggerRequest().finally(() => { });
169
+ return () => {
170
+ stop();
171
+ };
172
+ }, [runId, stop, options?.enabled]);
173
+ return { run, streams: streams ?? initialStreamsFallback, error, stop };
174
+ }
175
+ /**
176
+ * Hook to subscribe to realtime updates of task runs filtered by tag(s).
177
+ *
178
+ * @template TTask - The type of the task
179
+ * @param {string | string[]} tag - The tag or array of tags to filter runs by
180
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
181
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs and any error encountered
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * import type { myTask } from './path/to/task';
186
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>('my-tag');
187
+ * // Or with multiple tags
188
+ * const { runs, error } = useRealtimeRunsWithTag<typeof myTask>(['tag1', 'tag2']);
189
+ * ```
190
+ */
191
+ export function useRealtimeRunsWithTag(tag, options) {
192
+ const hookId = useId();
193
+ const idKey = options?.id ?? hookId;
194
+ // Store the streams state in SWR, using the idKey as the key to share states.
195
+ const { data: runs, mutate: mutateRuns } = useSWR([idKey, "run"], null, {
196
+ fallbackData: [],
197
+ });
198
+ // Keep the latest streams in a ref.
199
+ const runsRef = useRef([]);
200
+ useEffect(() => {
201
+ runsRef.current = runs ?? [];
202
+ }, [runs]);
203
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, "error"], null);
204
+ // Abort controller to cancel the current API call.
205
+ const abortControllerRef = useRef(null);
206
+ const stop = useCallback(() => {
207
+ if (abortControllerRef.current) {
208
+ abortControllerRef.current.abort();
209
+ abortControllerRef.current = null;
210
+ }
211
+ }, []);
212
+ const apiClient = useApiClient(options);
213
+ const triggerRequest = useCallback(async () => {
214
+ try {
215
+ if (!apiClient) {
216
+ return;
217
+ }
218
+ const abortController = new AbortController();
219
+ abortControllerRef.current = abortController;
220
+ await processRealtimeRunsWithTag(tag, apiClient, mutateRuns, runsRef, abortControllerRef);
221
+ }
222
+ catch (err) {
223
+ // Ignore abort errors as they are expected.
224
+ if (err.name === "AbortError") {
225
+ abortControllerRef.current = null;
226
+ return;
227
+ }
228
+ setError(err);
229
+ }
230
+ finally {
231
+ if (abortControllerRef.current) {
232
+ abortControllerRef.current = null;
233
+ }
234
+ }
235
+ }, [tag, mutateRuns, runsRef, abortControllerRef, apiClient, setError]);
236
+ useEffect(() => {
237
+ if (typeof options?.enabled === "boolean" && !options.enabled) {
238
+ return;
239
+ }
240
+ triggerRequest().finally(() => { });
241
+ return () => {
242
+ stop();
243
+ };
244
+ }, [tag, stop, options?.enabled]);
245
+ return { runs: runs ?? [], error, stop };
246
+ }
247
+ /**
248
+ * Hook to subscribe to realtime updates of a batch of task runs.
249
+ *
250
+ * @template TTask - The type of the task
251
+ * @param {string} batchId - The unique identifier of the batch to subscribe to
252
+ * @param {UseRealtimeRunOptions} [options] - Configuration options for the subscription
253
+ * @returns {UseRealtimeRunsInstance<TTask>} An object containing the current state of the runs, error handling, and control methods
254
+ *
255
+ * @example
256
+ * ```ts
257
+ * import type { myTask } from './path/to/task';
258
+ * const { runs, error } = useRealtimeBatch<typeof myTask>('batch-id-123');
259
+ * ```
260
+ */
261
+ export function useRealtimeBatch(batchId, options) {
262
+ const hookId = useId();
263
+ const idKey = options?.id ?? hookId;
264
+ // Store the streams state in SWR, using the idKey as the key to share states.
265
+ const { data: runs, mutate: mutateRuns } = useSWR([idKey, "run"], null, {
266
+ fallbackData: [],
267
+ });
268
+ // Keep the latest streams in a ref.
269
+ const runsRef = useRef([]);
270
+ useEffect(() => {
271
+ runsRef.current = runs ?? [];
272
+ }, [runs]);
273
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, "error"], null);
274
+ // Abort controller to cancel the current API call.
275
+ const abortControllerRef = useRef(null);
276
+ const stop = useCallback(() => {
277
+ if (abortControllerRef.current) {
278
+ abortControllerRef.current.abort();
279
+ abortControllerRef.current = null;
280
+ }
281
+ }, []);
282
+ const apiClient = useApiClient(options);
283
+ const triggerRequest = useCallback(async () => {
284
+ try {
285
+ if (!apiClient) {
286
+ return;
287
+ }
288
+ const abortController = new AbortController();
289
+ abortControllerRef.current = abortController;
290
+ await processRealtimeBatch(batchId, apiClient, mutateRuns, runsRef, abortControllerRef);
291
+ }
292
+ catch (err) {
293
+ // Ignore abort errors as they are expected.
294
+ if (err.name === "AbortError") {
295
+ abortControllerRef.current = null;
296
+ return;
297
+ }
298
+ setError(err);
299
+ }
300
+ finally {
301
+ if (abortControllerRef.current) {
302
+ abortControllerRef.current = null;
303
+ }
304
+ }
305
+ }, [batchId, mutateRuns, runsRef, abortControllerRef, apiClient, setError]);
306
+ useEffect(() => {
307
+ if (typeof options?.enabled === "boolean" && !options.enabled) {
308
+ return;
309
+ }
310
+ triggerRequest().finally(() => { });
311
+ return () => {
312
+ stop();
313
+ };
314
+ }, [batchId, stop, options?.enabled]);
315
+ return { runs: runs ?? [], error, stop };
316
+ }
317
+ async function processRealtimeBatch(batchId, apiClient, mutateRunsData, existingRunsRef, abortControllerRef) {
318
+ const subscription = apiClient.subscribeToBatch(batchId, {
319
+ signal: abortControllerRef.current?.signal,
320
+ });
321
+ for await (const part of subscription) {
322
+ mutateRunsData(insertRunShapeInOrder(existingRunsRef.current, part));
323
+ }
324
+ }
325
+ // Inserts and then orders by the run number, and ensures that the run is not duplicated
326
+ function insertRunShapeInOrder(previousRuns, run) {
327
+ const existingRun = previousRuns.find((r) => r.id === run.id);
328
+ if (existingRun) {
329
+ return previousRuns.map((r) => (r.id === run.id ? run : r));
330
+ }
331
+ const runNumber = run.number;
332
+ const index = previousRuns.findIndex((r) => r.number > runNumber);
333
+ if (index === -1) {
334
+ return [...previousRuns, run];
335
+ }
336
+ return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
337
+ }
338
+ async function processRealtimeRunsWithTag(tag, apiClient, mutateRunsData, existingRunsRef, abortControllerRef) {
339
+ const subscription = apiClient.subscribeToRunsWithTag(tag, {
340
+ signal: abortControllerRef.current?.signal,
341
+ });
342
+ for await (const part of subscription) {
343
+ mutateRunsData(insertRunShape(existingRunsRef.current, part));
344
+ }
345
+ }
346
+ // Replaces or inserts a run shape, ordered by the createdAt timestamp
347
+ function insertRunShape(previousRuns, run) {
348
+ const existingRun = previousRuns.find((r) => r.id === run.id);
349
+ if (existingRun) {
350
+ return previousRuns.map((r) => (r.id === run.id ? run : r));
351
+ }
352
+ const createdAt = run.createdAt;
353
+ const index = previousRuns.findIndex((r) => r.createdAt > createdAt);
354
+ if (index === -1) {
355
+ return [...previousRuns, run];
356
+ }
357
+ return [...previousRuns.slice(0, index), run, ...previousRuns.slice(index)];
358
+ }
359
+ async function processRealtimeRunWithStreams(runId, apiClient, mutateRunData, mutateStreamData, existingDataRef, abortControllerRef, stopOnCompletion = true, throttleInMs) {
360
+ const subscription = apiClient.subscribeToRun(runId, {
361
+ signal: abortControllerRef.current?.signal,
362
+ closeOnComplete: stopOnCompletion,
363
+ });
364
+ const streamQueue = createThrottledQueue(async (updates) => {
365
+ const nextStreamData = { ...existingDataRef.current };
366
+ // Group updates by type
367
+ const updatesByType = updates.reduce((acc, update) => {
368
+ if (!acc[update.type]) {
369
+ acc[update.type] = [];
370
+ }
371
+ acc[update.type].push(update.chunk);
372
+ return acc;
373
+ }, {});
374
+ // Apply all updates
375
+ for (const [type, chunks] of Object.entries(updatesByType)) {
376
+ // @ts-ignore
377
+ nextStreamData[type] = [...(existingDataRef.current[type] || []), ...chunks];
378
+ }
379
+ mutateStreamData(nextStreamData);
380
+ }, throttleInMs);
381
+ for await (const part of subscription.withStreams()) {
382
+ if (part.type === "run") {
383
+ mutateRunData(part.run);
384
+ }
385
+ else {
386
+ streamQueue.add({
387
+ type: part.type,
388
+ // @ts-ignore
389
+ chunk: part.chunk,
390
+ });
391
+ }
392
+ }
393
+ }
394
+ async function processRealtimeRun(runId, apiClient, mutateRunData, abortControllerRef, stopOnCompletion = true) {
395
+ const subscription = apiClient.subscribeToRun(runId, {
396
+ signal: abortControllerRef.current?.signal,
397
+ closeOnComplete: stopOnCompletion,
398
+ });
399
+ for await (const part of subscription) {
400
+ mutateRunData(part);
401
+ }
402
+ }
403
+ //# sourceMappingURL=useRealtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useRealtime.js","sourceRoot":"","sources":["../../../src/hooks/useRealtime.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAgB,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,YAAY,EAAuB,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAsC5D;;;;;;;;;;;;;GAaG;AAEH,MAAM,UAAU,cAAc,CAC5B,KAAc,EACd,OAA4C;IAE5C,MAAM,MAAM,GAAG,KAAK,EAAE,CAAC;IACvB,MAAM,KAAK,GAAG,OAAO,EAAE,EAAE,IAAI,MAAM,CAAC;IAEpC,8EAA8E;IAC9E,MAAM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aAClB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;AACH,CAAC;AAED,KAAK,UAAU,kBAAkB,CAC/B,KAAa,EACb,SAAoB,EACpB,aAA+C,EAC/C,kBAAkE,EAClE,mBAA4B,IAAI;IAEhC,MAAM,YAAY,GAAG,SAAS,CAAC,cAAc,CAAuB,KAAK,EAAE;QACzE,MAAM,EAAE,kBAAkB,CAAC,OAAO,EAAE,MAAM;QAC1C,eAAe,EAAE,gBAAgB;KAClC,CAAC,CAAC;IAEH,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,YAAY,EAAE,CAAC;QACtC,aAAa,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;AACH,CAAC"}
@@ -15,8 +15,13 @@ import { useApiClient } from "./useApiClient.js";
15
15
  * @returns {boolean} isError - Indicates if an error occurred during the retrieval of the run data.
16
16
  */
17
17
  export function useRun(runId, options) {
18
- const apiClient = useApiClient();
19
- const { data: run, error, isLoading, isValidating, } = useSWR(runId, () => apiClient.retrieveRun(runId), {
18
+ const apiClient = useApiClient(options);
19
+ const { data: run, error, isLoading, isValidating, } = useSWR(runId, () => {
20
+ if (!apiClient) {
21
+ throw new Error("Could not call useRun: Missing access token");
22
+ }
23
+ return apiClient.retrieveRun(runId);
24
+ }, {
20
25
  revalidateOnReconnect: options?.revalidateOnReconnect,
21
26
  refreshInterval: (run) => {
22
27
  if (!run)
@@ -1 +1 @@
1
- {"version":3,"file":"useRun.js","sourceRoot":"","sources":["../../../src/hooks/useRun.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAA4B,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CACpB,KAAa,EACb,OAAkC;IAQlC,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,MAAM,EACJ,IAAI,EAAE,GAAG,EACT,KAAK,EAC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,CAAC,GAAG,EAAE,EAAE;YACvB,IAAI,CAAC,GAAG;gBAAE,OAAO,OAAO,EAAE,eAAe,IAAI,CAAC,CAAC;YAE/C,IAAI,GAAG,CAAC,WAAW;gBAAE,OAAO,CAAC,CAAC;YAE9B,OAAO,OAAO,EAAE,eAAe,IAAI,CAAC,CAAC;QACvC,CAAC;QACD,iBAAiB,EAAE,OAAO,EAAE,iBAAiB;KAC9C,CACF,CAAC;IAEF,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;AACnE,CAAC"}
@@ -0,0 +1,101 @@
1
+ import { type AnyTask, type TaskIdentifier, type TaskPayload, InferRunTypes, RunHandleFromTypes, type TriggerOptions } from "@trigger.dev/core/v3";
2
+ import { UseApiClientOptions } from "./useApiClient.js";
3
+ import { UseRealtimeRunInstance, UseRealtimeRunWithStreamsInstance } from "./useRealtime.js";
4
+ /**
5
+ * Base interface for task trigger instances.
6
+ *
7
+ * @template TTask - The type of the task
8
+ */
9
+ export interface TriggerInstance<TTask extends AnyTask> {
10
+ /** Function to submit the task with a payload */
11
+ submit: (payload: TaskPayload<TTask>, options?: TriggerOptions) => void;
12
+ /** Whether the task is currently being submitted */
13
+ isLoading: boolean;
14
+ /** The handle returned after successful task submission */
15
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
16
+ /** Any error that occurred during submission */
17
+ error?: Error;
18
+ }
19
+ export type UseTaskTriggerOptions = UseApiClientOptions;
20
+ /**
21
+ * Hook to trigger a task and manage its initial execution state.
22
+ *
23
+ * @template TTask - The type of the task
24
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
25
+ * @param {UseTaskTriggerOptions} [options] - Configuration options for the task trigger
26
+ * @returns {TriggerInstance<TTask>} An object containing the submit function, loading state, handle, and any errors
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import type { myTask } from './path/to/task';
31
+ * const { submit, isLoading, handle, error } = useTaskTrigger<typeof myTask>('my-task-id');
32
+ *
33
+ * // Submit the task with payload
34
+ * submit({ foo: 'bar' });
35
+ * ```
36
+ */
37
+ export declare function useTaskTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, options?: UseTaskTriggerOptions): TriggerInstance<TTask>;
38
+ /**
39
+ * Configuration options for task triggers with realtime updates.
40
+ */
41
+ export type UseRealtimeTaskTriggerOptions = UseTaskTriggerOptions & {
42
+ /** Whether the realtime subscription is enabled */
43
+ enabled?: boolean;
44
+ /** Optional throttle time in milliseconds for stream updates */
45
+ experimental_throttleInMs?: number;
46
+ };
47
+ export type RealtimeTriggerInstanceWithStreams<TTask extends AnyTask, TStreams extends Record<string, any> = Record<string, any>> = UseRealtimeRunWithStreamsInstance<TTask, TStreams> & {
48
+ submit: (payload: TaskPayload<TTask>, options?: TriggerOptions) => void;
49
+ isLoading: boolean;
50
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
51
+ };
52
+ /**
53
+ * Hook to trigger a task and subscribe to its realtime updates including stream data.
54
+ *
55
+ * @template TTask - The type of the task
56
+ * @template TStreams - The type of the streams data
57
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
58
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
59
+ * @returns {RealtimeTriggerInstanceWithStreams<TTask, TStreams>} An object containing the submit function, loading state,
60
+ * handle, run state, streams data, and error handling
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import type { myTask } from './path/to/task';
65
+ * const { submit, run, streams, error } = useRealtimeTaskTriggerWithStreams<
66
+ * typeof myTask,
67
+ * { output: string }
68
+ * >('my-task-id');
69
+ *
70
+ * // Submit and monitor the task with streams
71
+ * submit({ foo: 'bar' });
72
+ * ```
73
+ */
74
+ export declare function useRealtimeTaskTriggerWithStreams<TTask extends AnyTask, TStreams extends Record<string, any> = Record<string, any>>(id: TaskIdentifier<TTask>, options?: UseRealtimeTaskTriggerOptions): RealtimeTriggerInstanceWithStreams<TTask, TStreams>;
75
+ export type RealtimeTriggerInstance<TTask extends AnyTask> = UseRealtimeRunInstance<TTask> & {
76
+ submit: (payload: TaskPayload<TTask>, options?: TriggerOptions) => void;
77
+ isLoading: boolean;
78
+ handle?: RunHandleFromTypes<InferRunTypes<TTask>>;
79
+ };
80
+ /**
81
+ * Hook to trigger a task and subscribe to its realtime updates.
82
+ *
83
+ * @template TTask - The type of the task
84
+ * @param {TaskIdentifier<TTask>} id - The identifier of the task to trigger
85
+ * @param {UseRealtimeTaskTriggerOptions} [options] - Configuration options for the task trigger and realtime updates
86
+ * @returns {RealtimeTriggerInstance<TTask>} An object containing the submit function, loading state,
87
+ * handle, run state, and error handling
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * import type { myTask } from './path/to/task';
92
+ * const { submit, run, error, stop } = useRealtimeTaskTrigger<typeof myTask>('my-task-id');
93
+ *
94
+ * // Submit and monitor the task
95
+ * submit({ foo: 'bar' });
96
+ *
97
+ * // Stop monitoring when needed
98
+ * stop();
99
+ * ```
100
+ */
101
+ export declare function useRealtimeTaskTrigger<TTask extends AnyTask>(id: TaskIdentifier<TTask>, options?: UseRealtimeTaskTriggerOptions): RealtimeTriggerInstance<TTask>;