@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.
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/commonjs/contexts.d.ts +4 -0
- package/dist/commonjs/contexts.js +10 -0
- package/dist/commonjs/contexts.js.map +1 -0
- package/dist/commonjs/hooks/useApiClient.d.ts +41 -0
- package/dist/commonjs/hooks/useApiClient.js +44 -0
- package/dist/commonjs/hooks/useApiClient.js.map +1 -0
- package/dist/commonjs/hooks/useInputStreamSend.d.ts +26 -0
- package/dist/commonjs/hooks/useInputStreamSend.js +45 -0
- package/dist/commonjs/hooks/useInputStreamSend.js.map +1 -0
- package/dist/commonjs/hooks/useRealtime.d.ts +294 -0
- package/dist/commonjs/hooks/useRealtime.js +549 -0
- package/dist/commonjs/hooks/useRealtime.js.map +1 -0
- package/dist/commonjs/hooks/useRun.d.ts +22 -0
- package/dist/commonjs/hooks/useRun.js +40 -0
- package/dist/commonjs/hooks/useRun.js.map +1 -0
- package/dist/commonjs/hooks/useTaskTrigger.d.ts +107 -0
- package/dist/commonjs/hooks/useTaskTrigger.js +138 -0
- package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -0
- package/dist/commonjs/hooks/useWaitToken.d.ts +35 -0
- package/dist/commonjs/hooks/useWaitToken.js +52 -0
- package/dist/commonjs/hooks/useWaitToken.js.map +1 -0
- package/dist/commonjs/index.d.ts +7 -0
- package/dist/commonjs/index.js +24 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/utils/createContextAndHook.d.ts +15 -0
- package/dist/commonjs/utils/createContextAndHook.js +39 -0
- package/dist/commonjs/utils/createContextAndHook.js.map +1 -0
- package/dist/commonjs/utils/throttle.d.ts +6 -0
- package/dist/commonjs/utils/throttle.js +50 -0
- package/dist/commonjs/utils/throttle.js.map +1 -0
- package/dist/commonjs/utils/trigger-swr.d.ts +19 -0
- package/dist/commonjs/utils/trigger-swr.js +28 -0
- package/dist/commonjs/utils/trigger-swr.js.map +1 -0
- package/dist/esm/contexts.d.ts +4 -0
- package/dist/esm/contexts.js +5 -0
- package/dist/esm/contexts.js.map +1 -0
- package/dist/esm/hooks/useApiClient.d.ts +41 -0
- package/dist/esm/hooks/useApiClient.js +41 -0
- package/dist/esm/hooks/useApiClient.js.map +1 -0
- package/dist/esm/hooks/useInputStreamSend.d.ts +26 -0
- package/dist/esm/hooks/useInputStreamSend.js +39 -0
- package/dist/esm/hooks/useInputStreamSend.js.map +1 -0
- package/dist/esm/hooks/useRealtime.d.ts +294 -0
- package/dist/esm/hooks/useRealtime.js +542 -0
- package/dist/esm/hooks/useRealtime.js.map +1 -0
- package/dist/esm/hooks/useRun.d.ts +22 -0
- package/dist/esm/hooks/useRun.js +37 -0
- package/dist/esm/hooks/useRun.js.map +1 -0
- package/dist/esm/hooks/useTaskTrigger.d.ts +107 -0
- package/dist/esm/hooks/useTaskTrigger.js +130 -0
- package/dist/esm/hooks/useTaskTrigger.js.map +1 -0
- package/dist/esm/hooks/useWaitToken.d.ts +35 -0
- package/dist/esm/hooks/useWaitToken.js +46 -0
- package/dist/esm/hooks/useWaitToken.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/utils/createContextAndHook.d.ts +15 -0
- package/dist/esm/utils/createContextAndHook.js +31 -0
- package/dist/esm/utils/createContextAndHook.js.map +1 -0
- package/dist/esm/utils/throttle.d.ts +6 -0
- package/dist/esm/utils/throttle.js +47 -0
- package/dist/esm/utils/throttle.js.map +1 -0
- package/dist/esm/utils/trigger-swr.d.ts +19 -0
- package/dist/esm/utils/trigger-swr.js +6 -0
- package/dist/esm/utils/trigger-swr.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { ApiClient } from "@trigger.dev/core/v3";
|
|
3
|
+
import { useTriggerAuthContextOptional } from "../contexts.js";
|
|
4
|
+
/**
|
|
5
|
+
* Hook to create an API client instance using authentication context or provided options.
|
|
6
|
+
*
|
|
7
|
+
* @param {UseApiClientOptions} [options] - Configuration options for the API client
|
|
8
|
+
* @returns {ApiClient} An initialized API client instance
|
|
9
|
+
* @throws {Error} When no access token is available in either context or options
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Using context authentication
|
|
14
|
+
* const apiClient = useApiClient();
|
|
15
|
+
*
|
|
16
|
+
* // Using custom options
|
|
17
|
+
* const apiClient = useApiClient({
|
|
18
|
+
* accessToken: "your-access-token",
|
|
19
|
+
* baseURL: "https://api.my-trigger.com",
|
|
20
|
+
* requestOptions: { retry: { maxAttempts: 10 } }
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function useApiClient(options) {
|
|
25
|
+
const auth = useTriggerAuthContextOptional();
|
|
26
|
+
const baseUrl = options?.baseURL ?? auth?.baseURL ?? "https://api.trigger.dev";
|
|
27
|
+
const accessToken = options?.accessToken ?? auth?.accessToken;
|
|
28
|
+
const previewBranch = options?.previewBranch ?? auth?.previewBranch;
|
|
29
|
+
if (!accessToken) {
|
|
30
|
+
if (options?.enabled === false) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
throw new Error("Missing accessToken in TriggerAuthContext or useApiClient options");
|
|
34
|
+
}
|
|
35
|
+
const requestOptions = {
|
|
36
|
+
...auth?.requestOptions,
|
|
37
|
+
...options?.requestOptions,
|
|
38
|
+
};
|
|
39
|
+
return new ApiClient(baseUrl, accessToken, previewBranch, requestOptions);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=useApiClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useApiClient.js","sourceRoot":"","sources":["../../../src/hooks/useApiClient.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,SAAS,EAAqB,MAAM,sBAAsB,CAAC;AACpE,OAAO,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AAuB/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,YAAY,CAAC,OAA6B;IACxD,MAAM,IAAI,GAAG,6BAA6B,EAAE,CAAC;IAE7C,MAAM,OAAO,GAAG,OAAO,EAAE,OAAO,IAAI,IAAI,EAAE,OAAO,IAAI,yBAAyB,CAAC;IAC/E,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,IAAI,EAAE,WAAW,CAAC;IAC9D,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,IAAI,IAAI,EAAE,aAAa,CAAC;IACpE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,IAAI,OAAO,EAAE,OAAO,KAAK,KAAK,EAAE,CAAC;YAC/B,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IACvF,CAAC;IAED,MAAM,cAAc,GAAsB;QACxC,GAAG,IAAI,EAAE,cAAc;QACvB,GAAG,OAAO,EAAE,cAAc;KAC3B,CAAC;IAEF,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,cAAc,CAAC,CAAC;AAC5E,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { UseApiClientOptions } from "./useApiClient.js";
|
|
2
|
+
export interface InputStreamSendInstance<TData> {
|
|
3
|
+
/** Send data to the input stream */
|
|
4
|
+
send: (data: TData) => void;
|
|
5
|
+
/** Whether a send is currently in progress */
|
|
6
|
+
isLoading: boolean;
|
|
7
|
+
/** Any error that occurred during the last send */
|
|
8
|
+
error?: Error;
|
|
9
|
+
/** Whether the hook is ready to send (has runId and access token) */
|
|
10
|
+
isReady: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Hook to send data to an input stream on a running task.
|
|
14
|
+
*
|
|
15
|
+
* @template TData - The type of data to send
|
|
16
|
+
* @param streamId - The input stream identifier
|
|
17
|
+
* @param runId - The run to send input stream data to
|
|
18
|
+
* @param options - API client options (e.g. accessToken)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const { send, isLoading } = useInputStreamSend("my-stream", runId, { accessToken });
|
|
23
|
+
* send({ message: "hello" });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function useInputStreamSend<TData>(streamId: string, runId?: string, options?: UseApiClientOptions): InputStreamSendInstance<TData>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import useSWRMutation from "swr/mutation";
|
|
3
|
+
import { useApiClient } from "./useApiClient.js";
|
|
4
|
+
/**
|
|
5
|
+
* Hook to send data to an input stream on a running task.
|
|
6
|
+
*
|
|
7
|
+
* @template TData - The type of data to send
|
|
8
|
+
* @param streamId - The input stream identifier
|
|
9
|
+
* @param runId - The run to send input stream data to
|
|
10
|
+
* @param options - API client options (e.g. accessToken)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const { send, isLoading } = useInputStreamSend("my-stream", runId, { accessToken });
|
|
15
|
+
* send({ message: "hello" });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function useInputStreamSend(streamId, runId, options) {
|
|
19
|
+
const apiClient = useApiClient(options);
|
|
20
|
+
async function sendToStream(key, { arg }) {
|
|
21
|
+
if (!apiClient) {
|
|
22
|
+
throw new Error("Could not send to input stream: Missing access token");
|
|
23
|
+
}
|
|
24
|
+
if (!runId) {
|
|
25
|
+
throw new Error("Could not send to input stream: Missing run ID");
|
|
26
|
+
}
|
|
27
|
+
return await apiClient.sendInputStream(runId, streamId, arg.data);
|
|
28
|
+
}
|
|
29
|
+
const mutation = useSWRMutation(runId ? `input-stream:${runId}:${streamId}` : null, sendToStream);
|
|
30
|
+
return {
|
|
31
|
+
send: (data) => {
|
|
32
|
+
mutation.trigger({ data });
|
|
33
|
+
},
|
|
34
|
+
isLoading: mutation.isMutating,
|
|
35
|
+
isReady: !!runId && !!apiClient,
|
|
36
|
+
error: mutation.error,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=useInputStreamSend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInputStreamSend.js","sourceRoot":"","sources":["../../../src/hooks/useInputStreamSend.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,cAAc,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAuB,MAAM,mBAAmB,CAAC;AAatE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAAgB,EAChB,KAAc,EACd,OAA6B;IAE7B,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;IAExC,KAAK,UAAU,YAAY,CAAC,GAAW,EAAE,EAAE,GAAG,EAA4B;QACxE,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAC;QAC1E,CAAC;QAED,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;QACpE,CAAC;QAED,OAAO,MAAM,SAAS,CAAC,eAAe,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;IACpE,CAAC;IAED,MAAM,QAAQ,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,gBAAgB,KAAK,IAAI,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAElG,OAAO;QACL,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;YACb,QAAQ,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7B,CAAC;QACD,SAAS,EAAE,QAAQ,CAAC,UAAU;QAC9B,OAAO,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,SAAS;QAC/B,KAAK,EAAE,QAAQ,CAAC,KAAK;KACtB,CAAC;AACJ,CAAC"}
|
|
@@ -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>;
|