@trigger.dev/react-hooks 0.0.0-process-keep-alive-20250626135929 → 0.0.0-rc-4-8-0-20261021151928
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/dist/commonjs/contexts.js +1 -1
- package/dist/commonjs/contexts.js.map +1 -1
- package/dist/commonjs/hooks/useApiClient.d.ts +7 -1
- package/dist/commonjs/hooks/useApiClient.js +15 -2
- package/dist/commonjs/hooks/useApiClient.js.map +1 -1
- 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 +190 -3
- package/dist/commonjs/hooks/useRealtime.js +241 -26
- package/dist/commonjs/hooks/useRealtime.js.map +1 -1
- package/dist/commonjs/hooks/useRun.d.ts +2 -2
- package/dist/commonjs/hooks/useRun.js +1 -1
- package/dist/commonjs/hooks/useRun.js.map +1 -1
- package/dist/commonjs/hooks/useSessionStream.d.ts +130 -0
- package/dist/commonjs/hooks/useSessionStream.js +235 -0
- package/dist/commonjs/hooks/useSessionStream.js.map +1 -0
- package/dist/commonjs/hooks/useSessionStreamChannel.d.ts +86 -0
- package/dist/commonjs/hooks/useSessionStreamChannel.js +209 -0
- package/dist/commonjs/hooks/useSessionStreamChannel.js.map +1 -0
- package/dist/commonjs/hooks/useTaskTrigger.d.ts +3 -3
- package/dist/commonjs/hooks/useTaskTrigger.js +13 -2
- package/dist/commonjs/hooks/useTaskTrigger.js.map +1 -1
- package/dist/commonjs/hooks/useWaitToken.d.ts +1 -1
- package/dist/commonjs/hooks/useWaitToken.js +1 -1
- package/dist/commonjs/hooks/useWaitToken.js.map +1 -1
- package/dist/commonjs/index.d.ts +3 -0
- package/dist/commonjs/index.js +3 -0
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/utils/createContextAndHook.d.ts +0 -1
- package/dist/commonjs/utils/createContextAndHook.js +1 -2
- package/dist/commonjs/utils/createContextAndHook.js.map +1 -1
- package/dist/commonjs/utils/throttle.js.map +1 -1
- package/dist/commonjs/utils/trigger-swr.d.ts +2 -2
- package/dist/commonjs/utils/trigger-swr.js +2 -3
- package/dist/commonjs/utils/trigger-swr.js.map +1 -1
- package/dist/commonjs/utils/useStableRequestCallback.d.ts +5 -0
- package/dist/commonjs/utils/useStableRequestCallback.js +17 -0
- package/dist/commonjs/utils/useStableRequestCallback.js.map +1 -0
- package/dist/esm/hooks/useApiClient.d.ts +7 -1
- package/dist/esm/hooks/useApiClient.js +15 -2
- package/dist/esm/hooks/useApiClient.js.map +1 -1
- 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 +190 -3
- package/dist/esm/hooks/useRealtime.js +239 -25
- package/dist/esm/hooks/useRealtime.js.map +1 -1
- package/dist/esm/hooks/useRun.d.ts +2 -2
- package/dist/esm/hooks/useRun.js.map +1 -1
- package/dist/esm/hooks/useSessionStream.d.ts +130 -0
- package/dist/esm/hooks/useSessionStream.js +232 -0
- package/dist/esm/hooks/useSessionStream.js.map +1 -0
- package/dist/esm/hooks/useSessionStreamChannel.d.ts +86 -0
- package/dist/esm/hooks/useSessionStreamChannel.js +206 -0
- package/dist/esm/hooks/useSessionStreamChannel.js.map +1 -0
- package/dist/esm/hooks/useTaskTrigger.d.ts +3 -3
- package/dist/esm/hooks/useTaskTrigger.js +12 -1
- package/dist/esm/hooks/useTaskTrigger.js.map +1 -1
- package/dist/esm/hooks/useWaitToken.d.ts +1 -1
- package/dist/esm/hooks/useWaitToken.js.map +1 -1
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/createContextAndHook.d.ts +0 -1
- package/dist/esm/utils/createContextAndHook.js +1 -1
- package/dist/esm/utils/createContextAndHook.js.map +1 -1
- package/dist/esm/utils/trigger-swr.d.ts +2 -2
- package/dist/esm/utils/trigger-swr.js +1 -1
- package/dist/esm/utils/trigger-swr.js.map +1 -1
- package/dist/esm/utils/useStableRequestCallback.d.ts +5 -0
- package/dist/esm/utils/useStableRequestCallback.js +14 -0
- package/dist/esm/utils/useStableRequestCallback.js.map +1 -0
- package/package.json +5 -5
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use client";
|
|
2
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.useTriggerAuthContextOptional = exports.useTriggerAuthContext = exports.TriggerAuthContext = void 0;
|
|
5
5
|
const createContextAndHook_js_1 = require("./utils/createContextAndHook.js");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../src/contexts.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC
|
|
1
|
+
{"version":3,"file":"contexts.js","sourceRoot":"","sources":["../../src/contexts.tsx"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;AAGb,6EAAuE;AAGvE,MAAM,CAAC,kBAAkB,EAAE,qBAAqB,EAAE,6BAA6B,CAAC,GAC9E,IAAA,8CAAoB,EAAyB,oBAAoB,CAAC,CAAC;QAE5D,kBAAkB;QAAE,qBAAqB;QAAE,6BAA6B"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ApiRequestOptions } from "@trigger.dev/core/v3";
|
|
2
|
+
import { ApiClient } from "@trigger.dev/core/v3";
|
|
2
3
|
/**
|
|
3
4
|
* Configuration options for creating an API client instance.
|
|
4
5
|
*/
|
|
@@ -11,6 +12,11 @@ export type UseApiClientOptions = {
|
|
|
11
12
|
previewBranch?: string;
|
|
12
13
|
/** Optional additional request configuration */
|
|
13
14
|
requestOptions?: ApiRequestOptions;
|
|
15
|
+
/**
|
|
16
|
+
* Optional callback that mints a fresh access token. Used to reconnect a
|
|
17
|
+
* realtime stream that the server rejected because its token expired.
|
|
18
|
+
*/
|
|
19
|
+
refreshAccessToken?: () => Promise<string>;
|
|
14
20
|
/**
|
|
15
21
|
* Enable or disable the API client instance.
|
|
16
22
|
*
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
"use client";
|
|
2
1
|
"use strict";
|
|
2
|
+
"use client";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.useApiClient = useApiClient;
|
|
5
5
|
const v3_1 = require("@trigger.dev/core/v3");
|
|
6
|
+
const react_1 = require("react");
|
|
6
7
|
const contexts_js_1 = require("../contexts.js");
|
|
7
8
|
/**
|
|
8
9
|
* Hook to create an API client instance using authentication context or provided options.
|
|
@@ -29,6 +30,18 @@ function useApiClient(options) {
|
|
|
29
30
|
const baseUrl = options?.baseURL ?? auth?.baseURL ?? "https://api.trigger.dev";
|
|
30
31
|
const accessToken = options?.accessToken ?? auth?.accessToken;
|
|
31
32
|
const previewBranch = options?.previewBranch ?? auth?.previewBranch;
|
|
33
|
+
const refreshAccessToken = options?.refreshAccessToken ?? auth?.refreshAccessToken;
|
|
34
|
+
const refreshAccessTokenRef = (0, react_1.useRef)(refreshAccessToken);
|
|
35
|
+
(0, react_1.useEffect)(() => {
|
|
36
|
+
refreshAccessTokenRef.current = refreshAccessToken;
|
|
37
|
+
}, [refreshAccessToken]);
|
|
38
|
+
const stableRefreshAccessToken = (0, react_1.useCallback)(async () => {
|
|
39
|
+
const refresh = refreshAccessTokenRef.current;
|
|
40
|
+
if (!refresh) {
|
|
41
|
+
throw new Error("Missing refreshAccessToken in TriggerAuthContext or useApiClient options");
|
|
42
|
+
}
|
|
43
|
+
return (0, v3_1.refreshAccessTokenOnce)(refresh);
|
|
44
|
+
}, []);
|
|
32
45
|
if (!accessToken) {
|
|
33
46
|
if (options?.enabled === false) {
|
|
34
47
|
return undefined;
|
|
@@ -39,6 +52,6 @@ function useApiClient(options) {
|
|
|
39
52
|
...auth?.requestOptions,
|
|
40
53
|
...options?.requestOptions,
|
|
41
54
|
};
|
|
42
|
-
return new v3_1.ApiClient(baseUrl, accessToken, previewBranch, requestOptions);
|
|
55
|
+
return new v3_1.ApiClient(baseUrl, accessToken, previewBranch, requestOptions, undefined, refreshAccessToken ? stableRefreshAccessToken : undefined);
|
|
43
56
|
}
|
|
44
57
|
//# sourceMappingURL=useApiClient.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useApiClient.js","sourceRoot":"","sources":["../../../src/hooks/useApiClient.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;
|
|
1
|
+
{"version":3,"file":"useApiClient.js","sourceRoot":"","sources":["../../../src/hooks/useApiClient.ts"],"names":[],"mappings":";AAAA,YAAY,CAAC;;;AAGb,6CAAyE;AACzE,iCAAuD;AACvD,gDAA+D;AA4B/D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,sBAA6B,OAA6B;IACxD,MAAM,IAAI,GAAG,IAAA,2CAA6B,GAAE,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,MAAM,kBAAkB,GAAG,OAAO,EAAE,kBAAkB,IAAI,IAAI,EAAE,kBAAkB,CAAC;IAEnF,MAAM,qBAAqB,GAAG,IAAA,cAAM,EAAC,kBAAkB,CAAC,CAAC;IACzD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,qBAAqB,CAAC,OAAO,GAAG,kBAAkB,CAAC;IACrD,CAAC,EAAE,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACzB,MAAM,wBAAwB,GAAG,IAAA,mBAAW,EAAC,KAAK,IAAI,EAAE;QACtD,MAAM,OAAO,GAAG,qBAAqB,CAAC,OAAO,CAAC;QAE9C,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAC;QAC9F,CAAC;QAED,OAAO,IAAA,2BAAsB,EAAC,OAAO,CAAC,CAAC;IACzC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,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,cAAS,CAClB,OAAO,EACP,WAAW,EACX,aAAa,EACb,cAAc,EACd,SAAS,EACT,kBAAkB,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,SAAS,CAC1D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { 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,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.useInputStreamSend = useInputStreamSend;
|
|
8
|
+
const mutation_1 = __importDefault(require("swr/mutation"));
|
|
9
|
+
const useApiClient_js_1 = require("./useApiClient.js");
|
|
10
|
+
/**
|
|
11
|
+
* Hook to send data to an input stream on a running task.
|
|
12
|
+
*
|
|
13
|
+
* @template TData - The type of data to send
|
|
14
|
+
* @param streamId - The input stream identifier
|
|
15
|
+
* @param runId - The run to send input stream data to
|
|
16
|
+
* @param options - API client options (e.g. accessToken)
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* const { send, isLoading } = useInputStreamSend("my-stream", runId, { accessToken });
|
|
21
|
+
* send({ message: "hello" });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function useInputStreamSend(streamId, runId, options) {
|
|
25
|
+
const apiClient = (0, useApiClient_js_1.useApiClient)(options);
|
|
26
|
+
async function sendToStream(key, { arg }) {
|
|
27
|
+
if (!apiClient) {
|
|
28
|
+
throw new Error("Could not send to input stream: Missing access token");
|
|
29
|
+
}
|
|
30
|
+
if (!runId) {
|
|
31
|
+
throw new Error("Could not send to input stream: Missing run ID");
|
|
32
|
+
}
|
|
33
|
+
return await apiClient.sendInputStream(runId, streamId, arg.data);
|
|
34
|
+
}
|
|
35
|
+
const mutation = (0, mutation_1.default)(runId ? `input-stream:${runId}:${streamId}` : null, sendToStream);
|
|
36
|
+
return {
|
|
37
|
+
send: (data) => {
|
|
38
|
+
mutation.trigger({ data });
|
|
39
|
+
},
|
|
40
|
+
isLoading: mutation.isMutating,
|
|
41
|
+
isReady: !!runId && !!apiClient,
|
|
42
|
+
error: mutation.error,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# 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,4DAA0C;AAE1C,uDAAiD;AAajD;;;;;;;;;;;;;GAaG;AACH,4BACE,QAAgB,EAChB,KAAc,EACd,OAA6B;IAE7B,MAAM,SAAS,GAAG,IAAA,8BAAY,EAAC,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,IAAA,kBAAc,EAAC,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"}
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { AnyTask, RealtimeRun, RealtimeRunSkipColumns } from "@trigger.dev/core/v3";
|
|
2
|
-
import { UseApiClientOptions } from "./useApiClient.js";
|
|
1
|
+
import type { AnyTask, InferStreamType, RealtimeDefinedStream, RealtimeRun, RealtimeRunSkipColumns, SSEStreamPart } from "@trigger.dev/core/v3";
|
|
2
|
+
import type { UseApiClientOptions } from "./useApiClient.js";
|
|
3
3
|
export type UseRealtimeRunOptions = UseApiClientOptions & {
|
|
4
4
|
id?: string;
|
|
5
5
|
enabled?: boolean;
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The number of milliseconds to throttle the stream updates.
|
|
8
|
+
*
|
|
9
|
+
* @default 16
|
|
10
|
+
*/
|
|
11
|
+
throttleInMs?: number;
|
|
7
12
|
};
|
|
8
13
|
export type UseRealtimeSingleRunOptions<TTask extends AnyTask = AnyTask> = UseRealtimeRunOptions & {
|
|
9
14
|
/**
|
|
@@ -147,3 +152,185 @@ export declare function useRealtimeRunsWithTag<TTask extends AnyTask>(tag: strin
|
|
|
147
152
|
* ```
|
|
148
153
|
*/
|
|
149
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>;
|