@trigger.dev/react-hooks 4.5.13 → 4.5.15

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 (37) hide show
  1. package/dist/commonjs/hooks/useApiClient.d.ts +5 -0
  2. package/dist/commonjs/hooks/useApiClient.js +14 -1
  3. package/dist/commonjs/hooks/useApiClient.js.map +1 -1
  4. package/dist/commonjs/hooks/useRealtime.d.ts +43 -1
  5. package/dist/commonjs/hooks/useRealtime.js +65 -22
  6. package/dist/commonjs/hooks/useRealtime.js.map +1 -1
  7. package/dist/commonjs/hooks/useSessionStream.d.ts +131 -0
  8. package/dist/commonjs/hooks/useSessionStream.js +236 -0
  9. package/dist/commonjs/hooks/useSessionStream.js.map +1 -0
  10. package/dist/commonjs/hooks/useSessionStreamChannel.d.ts +86 -0
  11. package/dist/commonjs/hooks/useSessionStreamChannel.js +209 -0
  12. package/dist/commonjs/hooks/useSessionStreamChannel.js.map +1 -0
  13. package/dist/commonjs/index.d.ts +2 -0
  14. package/dist/commonjs/index.js +2 -0
  15. package/dist/commonjs/index.js.map +1 -1
  16. package/dist/commonjs/utils/useStableRequestCallback.d.ts +5 -0
  17. package/dist/commonjs/utils/useStableRequestCallback.js +17 -0
  18. package/dist/commonjs/utils/useStableRequestCallback.js.map +1 -0
  19. package/dist/esm/hooks/useApiClient.d.ts +5 -0
  20. package/dist/esm/hooks/useApiClient.js +15 -2
  21. package/dist/esm/hooks/useApiClient.js.map +1 -1
  22. package/dist/esm/hooks/useRealtime.d.ts +43 -1
  23. package/dist/esm/hooks/useRealtime.js +60 -17
  24. package/dist/esm/hooks/useRealtime.js.map +1 -1
  25. package/dist/esm/hooks/useSessionStream.d.ts +131 -0
  26. package/dist/esm/hooks/useSessionStream.js +233 -0
  27. package/dist/esm/hooks/useSessionStream.js.map +1 -0
  28. package/dist/esm/hooks/useSessionStreamChannel.d.ts +86 -0
  29. package/dist/esm/hooks/useSessionStreamChannel.js +206 -0
  30. package/dist/esm/hooks/useSessionStreamChannel.js.map +1 -0
  31. package/dist/esm/index.d.ts +2 -0
  32. package/dist/esm/index.js +2 -0
  33. package/dist/esm/index.js.map +1 -1
  34. package/dist/esm/utils/useStableRequestCallback.d.ts +5 -0
  35. package/dist/esm/utils/useStableRequestCallback.js +14 -0
  36. package/dist/esm/utils/useStableRequestCallback.js.map +1 -0
  37. package/package.json +2 -2
@@ -0,0 +1,233 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
3
+ import { createThrottledQueue } from "../utils/throttle.js";
4
+ import { useSWR } from "../utils/trigger-swr.js";
5
+ import { useStableRequestCallback } from "../utils/useStableRequestCallback.js";
6
+ import { useApiClient } from "./useApiClient.js";
7
+ /**
8
+ * Hook to read one channel of a Session's realtime stream.
9
+ *
10
+ * This hook subscribes to one of the session's channels (`out` by default, or `in`) and
11
+ * updates the `records` array as new records arrive. It is read-only: use `useSession` for
12
+ * two-way (read and write) communication. The subscription is automatically managed: it
13
+ * starts when the component mounts (or when `enabled` becomes `true`) and stops when the
14
+ * component unmounts or when `stop()` is called.
15
+ *
16
+ * Requires a Public Access Token with the `read:sessions:{id}` scope.
17
+ *
18
+ * @template TRecord - The type of each record on the channel
19
+ * @param sessionIdOrExternalId - The id or external id of the session to subscribe to
20
+ * @param options - Optional configuration for the subscription
21
+ * @returns An object containing:
22
+ * - `records`: An array of all the records received so far (accumulates over time)
23
+ * - `lastEventId`: The cursor of the last record seen, for resuming later
24
+ * - `lastControl`: The last control record seen
25
+ * - `error`: Any error that occurred during subscription
26
+ * - `stop`: A function to manually stop the subscription
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * "use client";
31
+ * import { useSessionStream } from "@trigger.dev/react-hooks";
32
+ *
33
+ * function SessionViewer({ sessionId }: { sessionId: string }) {
34
+ * const { records, error } = useSessionStream<string>(sessionId, {
35
+ * accessToken: publicAccessToken,
36
+ * });
37
+ *
38
+ * if (error) return <div>Error: {error.message}</div>;
39
+ *
40
+ * return <div>{records.join("")}</div>;
41
+ * }
42
+ * ```
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * // Read the input channel, resuming from a persisted cursor
47
+ * const { records, lastEventId, stop } = useSessionStream<MyRecord>(sessionId, {
48
+ * accessToken: publicAccessToken,
49
+ * io: "in",
50
+ * lastEventId: persistedCursor,
51
+ * onControl: (event) => {
52
+ * if (event.subtype === "turn-complete") {
53
+ * console.log("The turn is complete");
54
+ * }
55
+ * },
56
+ * });
57
+ * ```
58
+ */
59
+ export function useSessionStream(sessionIdOrExternalId, options) {
60
+ const hookId = useId();
61
+ const idKey = options?.id ?? hookId;
62
+ const io = options?.io ?? "out";
63
+ const [initialRecordsFallback] = useState([]);
64
+ const { data: records, mutate: mutateRecords } = useSWR([idKey, sessionIdOrExternalId, io, "records"], null, {
65
+ fallbackData: initialRecordsFallback,
66
+ });
67
+ const recordsRef = useRef(records ?? []);
68
+ useEffect(() => {
69
+ recordsRef.current = records || [];
70
+ }, [records]);
71
+ const { data: lastEventId = undefined, mutate: setLastEventId } = useSWR([idKey, sessionIdOrExternalId, io, "lastEventId"], null);
72
+ const lastEventIdRef = useRef(lastEventId);
73
+ const channelIdentityRef = useRef(`${idKey}:${sessionIdOrExternalId}:${io}`);
74
+ useEffect(() => {
75
+ const identity = `${idKey}:${sessionIdOrExternalId}:${io}`;
76
+ if (channelIdentityRef.current !== identity) {
77
+ channelIdentityRef.current = identity;
78
+ lastEventIdRef.current = lastEventId;
79
+ }
80
+ }, [idKey, sessionIdOrExternalId, io, lastEventId]);
81
+ const { data: lastControl = undefined, mutate: setLastControl } = useSWR([idKey, sessionIdOrExternalId, io, "lastControl"], null);
82
+ const { data: _isComplete = false, mutate: setIsComplete } = useSWR([idKey, sessionIdOrExternalId, io, "complete"], null);
83
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, sessionIdOrExternalId, io, "error"], null);
84
+ const abortControllerRef = useRef(null);
85
+ const stop = useCallback(() => {
86
+ if (abortControllerRef.current) {
87
+ abortControllerRef.current.abort();
88
+ abortControllerRef.current = null;
89
+ }
90
+ }, []);
91
+ const onRecordsCallback = options?.onRecords;
92
+ const onRecords = useCallback((recordsBatch) => {
93
+ if (onRecordsCallback) {
94
+ onRecordsCallback(recordsBatch);
95
+ }
96
+ }, [onRecordsCallback]);
97
+ const onControlCallback = options?.onControl;
98
+ const onControl = useCallback((event) => {
99
+ if (onControlCallback) {
100
+ onControlCallback(event);
101
+ }
102
+ }, [onControlCallback]);
103
+ const apiClient = useApiClient(options);
104
+ const timeoutInSeconds = options?.timeoutInSeconds;
105
+ const startEventId = options?.lastEventId;
106
+ const throttleInMs = options?.throttleInMs;
107
+ const from = options?.from;
108
+ const maxRecords = options?.maxRecords;
109
+ useEffect(() => {
110
+ if (maxRecords != null && maxRecords >= 0) {
111
+ const current = recordsRef.current;
112
+ if (current.length > maxRecords) {
113
+ mutateRecords(current.slice(current.length - maxRecords));
114
+ }
115
+ }
116
+ }, [maxRecords, mutateRecords]);
117
+ const triggerRequest = useCallback(async () => {
118
+ try {
119
+ if (!sessionIdOrExternalId || !apiClient) {
120
+ return;
121
+ }
122
+ const abortController = new AbortController();
123
+ abortControllerRef.current = abortController;
124
+ await processSessionStream(sessionIdOrExternalId, io, apiClient, mutateRecords, recordsRef, setLastEventId, setLastControl, setError, onRecords, onControl, abortControllerRef, timeoutInSeconds, startEventId !== undefined ? String(startEventId) : lastEventIdRef.current, throttleInMs ?? 16, from, maxRecords);
125
+ }
126
+ catch (err) {
127
+ if (err.name === "AbortError") {
128
+ abortControllerRef.current = null;
129
+ return;
130
+ }
131
+ setError(err);
132
+ }
133
+ finally {
134
+ if (abortControllerRef.current) {
135
+ abortControllerRef.current = null;
136
+ }
137
+ setIsComplete(true);
138
+ }
139
+ }, [
140
+ sessionIdOrExternalId,
141
+ io,
142
+ apiClient,
143
+ mutateRecords,
144
+ setLastEventId,
145
+ setLastControl,
146
+ setError,
147
+ setIsComplete,
148
+ onRecords,
149
+ onControl,
150
+ timeoutInSeconds,
151
+ startEventId,
152
+ throttleInMs,
153
+ from,
154
+ maxRecords,
155
+ ]);
156
+ const requestSubscription = useStableRequestCallback(triggerRequest);
157
+ useEffect(() => {
158
+ if (typeof options?.enabled === "boolean" && !options.enabled) {
159
+ return;
160
+ }
161
+ if (!sessionIdOrExternalId) {
162
+ return;
163
+ }
164
+ requestSubscription().finally(() => { });
165
+ return () => {
166
+ stop();
167
+ };
168
+ }, [sessionIdOrExternalId, io, stop, options?.enabled, requestSubscription]);
169
+ return { records: records ?? initialRecordsFallback, lastEventId, lastControl, error, stop };
170
+ }
171
+ async function processSessionStream(sessionIdOrExternalId, io, apiClient, mutateRecordsData, existingRecordsRef, setLastEventId, setLastControl, onError, onRecords, onControl, abortControllerRef, timeoutInSeconds, lastEventId, throttleInMs, from, maxRecords) {
172
+ let lastSeenEventId;
173
+ let publishedEventId;
174
+ let partsBatch = [];
175
+ const publishLastEventId = () => {
176
+ if (lastSeenEventId !== publishedEventId) {
177
+ publishedEventId = lastSeenEventId;
178
+ setLastEventId(lastSeenEventId);
179
+ }
180
+ };
181
+ const flushParts = () => {
182
+ if (partsBatch.length === 0)
183
+ return;
184
+ const batch = partsBatch;
185
+ partsBatch = [];
186
+ onRecords(batch);
187
+ };
188
+ try {
189
+ const stream = await apiClient.subscribeToSessionStream(sessionIdOrExternalId, io, {
190
+ signal: abortControllerRef.current?.signal,
191
+ timeoutInSeconds,
192
+ lastEventId,
193
+ from,
194
+ onPart: (part) => {
195
+ lastSeenEventId = part.id;
196
+ partsBatch.push(part);
197
+ },
198
+ onControl: (event) => {
199
+ setLastControl(event);
200
+ onControl(event);
201
+ },
202
+ });
203
+ const recordsQueue = createThrottledQueue(async (newRecords) => {
204
+ const combined = [...existingRecordsRef.current, ...newRecords];
205
+ const bounded = maxRecords != null && maxRecords >= 0 && combined.length > maxRecords
206
+ ? combined.slice(combined.length - maxRecords)
207
+ : combined;
208
+ existingRecordsRef.current = bounded;
209
+ mutateRecordsData(bounded);
210
+ publishLastEventId();
211
+ flushParts();
212
+ }, throttleInMs);
213
+ for await (const record of stream) {
214
+ recordsQueue.add(record);
215
+ }
216
+ await recordsQueue.flush();
217
+ publishLastEventId();
218
+ flushParts();
219
+ }
220
+ catch (err) {
221
+ if (err.name === "AbortError") {
222
+ return;
223
+ }
224
+ if (err instanceof Error) {
225
+ onError(err);
226
+ }
227
+ else {
228
+ onError(new Error(String(err)));
229
+ }
230
+ throw err;
231
+ }
232
+ }
233
+ //# sourceMappingURL=useSessionStream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSessionStream.js","sourceRoot":"","sources":["../../../src/hooks/useSessionStream.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAGb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACjD,OAAO,EAAE,wBAAwB,EAAE,MAAM,sCAAsC,CAAC;AAEhF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAoFjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAM,UAAU,gBAAgB,CAC9B,qBAA8B,EAC9B,OAA0C;IAE1C,MAAM,MAAM,GAAG,KAAK,EAAE,CAAC;IACvB,MAAM,KAAK,GAAG,OAAO,EAAE,EAAE,IAAI,MAAM,CAAC;IACpC,MAAM,EAAE,GAAG,OAAO,EAAE,EAAE,IAAI,KAAK,CAAC;IAEhC,MAAM,CAAC,sBAAsB,CAAC,GAAG,QAAQ,CAAC,EAAoB,CAAC,CAAC;IAEhE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,GAAG,MAAM,CACrD,CAAC,KAAK,EAAE,qBAAqB,EAAE,EAAE,EA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IAAI,IAAI,UAAU,IAAI,CAAC,IAAI,QAAQ,CAAC,MAAM,GAAG,UAAU;gBACnE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,GAAG,UAAU,CAAC;gBAC9C,CAAC,CAAC,QAAQ,CAAC;YACf,kBAAkB,CAAC,OAAO,GAAG,OAAO,CAAC;YACrC,iBAAiB,CAAC,OAAO,CAAC,CAAC;YAC3B,kBAAkB,EAAE,CAAC;YACrB,UAAU,EAAE,CAAC;QACf,CAAC,EAAE,YAAY,CAAC,CAAC;QAEjB,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;YAClC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC3B,CAAC;QAED,MAAM,YAAY,CAAC,KAAK,EAAE,CAAC;QAC3B,kBAAkB,EAAE,CAAC;QACrB,UAAU,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAK,GAAW,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YACvC,OAAO;QACT,CAAC;QAED,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;YACzB,OAAO,CAAC,GAAG,CAAC,CAAC;QACf,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAClC,CAAC;QAED,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC"}
@@ -0,0 +1,86 @@
1
+ import type { AnySessionChannel, ControlEvent, SessionChannelIn, SessionChannelName, SessionChannelOut, SSEStreamPart } from "@trigger.dev/core/v3";
2
+ import type { UseApiClientOptions } from "./useApiClient.js";
3
+ type ChannelRecord<TChannel extends AnySessionChannel, S extends "in" | "out"> = S extends "out" ? SessionChannelOut<TChannel> : SessionChannelIn<TChannel>;
4
+ export type UseSessionStreamChannelInstance<TRecord> = {
5
+ /** The records received so far on the channel, in arrival order. */
6
+ records: Array<TRecord>;
7
+ /** The cursor of the last record seen; pass back as `lastEventId` to resume. */
8
+ lastEventId: string | undefined;
9
+ /** The last control record seen on the channel. */
10
+ lastControl: ControlEvent | undefined;
11
+ error: Error | undefined;
12
+ /** Abort the current request immediately, keep the records received so far. */
13
+ stop: () => void;
14
+ };
15
+ export type UseSessionStreamChannelOptions<TChannel extends AnySessionChannel, S extends "in" | "out"> = UseApiClientOptions & {
16
+ /**
17
+ * The id or external id of the session that owns the channel. May be
18
+ * undefined while it resolves; the subscription starts once it is set.
19
+ */
20
+ sessionId?: string;
21
+ id?: string;
22
+ enabled?: boolean;
23
+ /**
24
+ * Which side of the channel to read.
25
+ *
26
+ * @default "out"
27
+ */
28
+ io?: S;
29
+ /**
30
+ * The number of milliseconds to throttle the record updates.
31
+ *
32
+ * @default 16
33
+ */
34
+ throttleInMs?: number;
35
+ /**
36
+ * The number of seconds to wait for new data before the stream closes.
37
+ *
38
+ * @default 60 seconds
39
+ */
40
+ timeoutInSeconds?: number;
41
+ /** The cursor to resume from. If not provided, reads per `from`. */
42
+ lastEventId?: string | number;
43
+ /**
44
+ * Where a fresh subscription (no `lastEventId`) starts reading.
45
+ *
46
+ * - `"beginning"` (default): replay the full channel history, then live-tail.
47
+ * - `"latest"`: start at the current tail, for a last-value / live view.
48
+ *
49
+ * Ignored when `lastEventId` is set.
50
+ */
51
+ from?: "beginning" | "latest";
52
+ /**
53
+ * Cap the number of records kept in `records`. Use `maxRecords: 1` with
54
+ * `from: "latest"` for a bounded last-value view.
55
+ */
56
+ maxRecords?: number;
57
+ /** Invoked once per throttled flush with the batch of records (control records included). */
58
+ onRecords?: (records: Array<SSEStreamPart<ChannelRecord<TChannel, S>>>) => void;
59
+ /** Called when a control record is received on the channel. */
60
+ onControl?: (event: ControlEvent) => void;
61
+ };
62
+ /**
63
+ * Read one side of a named Session side channel, with record types inferred
64
+ * from a `defineSessionChannel` declaration passed as the type argument.
65
+ *
66
+ * The channel name is typesafe (`SessionChannelName<TChannel>`) and `records`
67
+ * is typed from the channel's `.out` / `.in` record type. Called without the
68
+ * type argument, the channel name is any string and `records` is `unknown`.
69
+ *
70
+ * Requires a Public Access Token scoped to the session (or to the channel).
71
+ *
72
+ * @example
73
+ * ```tsx
74
+ * import type { screenshotsChannel } from "./shared/channels";
75
+ *
76
+ * const { records } = useSessionStreamChannel<typeof screenshotsChannel>("screenshots", {
77
+ * sessionId,
78
+ * accessToken,
79
+ * io: "out",
80
+ * from: "latest",
81
+ * maxRecords: 1,
82
+ * });
83
+ * ```
84
+ */
85
+ export declare function useSessionStreamChannel<TChannel extends AnySessionChannel = AnySessionChannel, S extends "in" | "out" = "out">(channel: SessionChannelName<TChannel>, options: UseSessionStreamChannelOptions<TChannel, S>): UseSessionStreamChannelInstance<ChannelRecord<TChannel, S>>;
86
+ export {};
@@ -0,0 +1,206 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useId, useRef, useState } from "react";
3
+ import { createThrottledQueue } from "../utils/throttle.js";
4
+ import { useSWR } from "../utils/trigger-swr.js";
5
+ import { useStableRequestCallback } from "../utils/useStableRequestCallback.js";
6
+ import { useApiClient } from "./useApiClient.js";
7
+ /**
8
+ * Read one side of a named Session side channel, with record types inferred
9
+ * from a `defineSessionChannel` declaration passed as the type argument.
10
+ *
11
+ * The channel name is typesafe (`SessionChannelName<TChannel>`) and `records`
12
+ * is typed from the channel's `.out` / `.in` record type. Called without the
13
+ * type argument, the channel name is any string and `records` is `unknown`.
14
+ *
15
+ * Requires a Public Access Token scoped to the session (or to the channel).
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * import type { screenshotsChannel } from "./shared/channels";
20
+ *
21
+ * const { records } = useSessionStreamChannel<typeof screenshotsChannel>("screenshots", {
22
+ * sessionId,
23
+ * accessToken,
24
+ * io: "out",
25
+ * from: "latest",
26
+ * maxRecords: 1,
27
+ * });
28
+ * ```
29
+ */
30
+ export function useSessionStreamChannel(channel, options) {
31
+ const hookId = useId();
32
+ const idKey = options.id ?? hookId;
33
+ const io = (options.io ?? "out");
34
+ const sessionId = options.sessionId;
35
+ const channelName = channel;
36
+ const [initialRecordsFallback] = useState([]);
37
+ const { data: records, mutate: mutateRecords } = useSWR([idKey, sessionId, channelName, io, "records"], null, { fallbackData: initialRecordsFallback });
38
+ const recordsRef = useRef(records ?? []);
39
+ useEffect(() => {
40
+ recordsRef.current = records || [];
41
+ }, [records]);
42
+ const { data: lastEventId = undefined, mutate: setLastEventId } = useSWR([idKey, sessionId, channelName, io, "lastEventId"], null);
43
+ const lastEventIdRef = useRef(lastEventId);
44
+ const channelIdentityRef = useRef(`${idKey}:${sessionId}:${channelName}:${io}`);
45
+ useEffect(() => {
46
+ const identity = `${idKey}:${sessionId}:${channelName}:${io}`;
47
+ if (channelIdentityRef.current !== identity) {
48
+ channelIdentityRef.current = identity;
49
+ lastEventIdRef.current = lastEventId;
50
+ }
51
+ }, [idKey, sessionId, channelName, io, lastEventId]);
52
+ const { data: lastControl = undefined, mutate: setLastControl } = useSWR([idKey, sessionId, channelName, io, "lastControl"], null);
53
+ const { data: _isComplete = false, mutate: setIsComplete } = useSWR([idKey, sessionId, channelName, io, "complete"], null);
54
+ const { data: error = undefined, mutate: setError } = useSWR([idKey, sessionId, channelName, io, "error"], null);
55
+ const abortControllerRef = useRef(null);
56
+ const stop = useCallback(() => {
57
+ if (abortControllerRef.current) {
58
+ abortControllerRef.current.abort();
59
+ abortControllerRef.current = null;
60
+ }
61
+ }, []);
62
+ const onRecordsCallback = options.onRecords;
63
+ const onRecords = useCallback((recordsBatch) => {
64
+ if (onRecordsCallback) {
65
+ onRecordsCallback(recordsBatch);
66
+ }
67
+ }, [onRecordsCallback]);
68
+ const onControlCallback = options.onControl;
69
+ const onControl = useCallback((event) => {
70
+ if (onControlCallback) {
71
+ onControlCallback(event);
72
+ }
73
+ }, [onControlCallback]);
74
+ const apiClient = useApiClient(options);
75
+ const timeoutInSeconds = options.timeoutInSeconds;
76
+ const startEventId = options.lastEventId;
77
+ const throttleInMs = options.throttleInMs;
78
+ const from = options.from;
79
+ const maxRecords = options.maxRecords;
80
+ useEffect(() => {
81
+ if (maxRecords != null && maxRecords >= 0) {
82
+ const current = recordsRef.current;
83
+ if (current.length > maxRecords) {
84
+ mutateRecords(current.slice(current.length - maxRecords));
85
+ }
86
+ }
87
+ }, [maxRecords, mutateRecords]);
88
+ const triggerRequest = useCallback(async () => {
89
+ let abortController = null;
90
+ try {
91
+ if (!sessionId || !apiClient) {
92
+ return;
93
+ }
94
+ abortController = new AbortController();
95
+ abortControllerRef.current = abortController;
96
+ await processSessionChannelStream(sessionId, io, channelName, apiClient, mutateRecords, recordsRef, setLastEventId, setLastControl, setError, onRecords, onControl, abortControllerRef, timeoutInSeconds, startEventId !== undefined ? String(startEventId) : lastEventIdRef.current, throttleInMs ?? 16, from, maxRecords);
97
+ }
98
+ catch (err) {
99
+ if (err.name === "AbortError") {
100
+ return;
101
+ }
102
+ setError(err);
103
+ }
104
+ finally {
105
+ if (abortControllerRef.current === abortController) {
106
+ abortControllerRef.current = null;
107
+ }
108
+ setIsComplete(true);
109
+ }
110
+ }, [
111
+ sessionId,
112
+ io,
113
+ channelName,
114
+ apiClient,
115
+ mutateRecords,
116
+ setLastEventId,
117
+ setLastControl,
118
+ setError,
119
+ setIsComplete,
120
+ onRecords,
121
+ onControl,
122
+ timeoutInSeconds,
123
+ startEventId,
124
+ throttleInMs,
125
+ from,
126
+ maxRecords,
127
+ ]);
128
+ const requestSubscription = useStableRequestCallback(triggerRequest);
129
+ useEffect(() => {
130
+ if (typeof options.enabled === "boolean" && !options.enabled) {
131
+ return;
132
+ }
133
+ if (!sessionId) {
134
+ return;
135
+ }
136
+ requestSubscription().finally(() => { });
137
+ return () => {
138
+ stop();
139
+ };
140
+ }, [sessionId, channelName, io, stop, options.enabled, requestSubscription]);
141
+ return { records: records ?? initialRecordsFallback, lastEventId, lastControl, error, stop };
142
+ }
143
+ async function processSessionChannelStream(sessionIdOrExternalId, io, channel, apiClient, mutateRecordsData, existingRecordsRef, setLastEventId, setLastControl, onError, onRecords, onControl, abortControllerRef, timeoutInSeconds, lastEventId, throttleInMs, from, maxRecords) {
144
+ let lastSeenEventId;
145
+ let publishedEventId;
146
+ let partsBatch = [];
147
+ const publishLastEventId = () => {
148
+ if (lastSeenEventId !== publishedEventId) {
149
+ publishedEventId = lastSeenEventId;
150
+ setLastEventId(lastSeenEventId);
151
+ }
152
+ };
153
+ const flushParts = () => {
154
+ if (partsBatch.length === 0)
155
+ return;
156
+ const batch = partsBatch;
157
+ partsBatch = [];
158
+ onRecords(batch);
159
+ };
160
+ try {
161
+ const stream = await apiClient.subscribeToSessionStream(sessionIdOrExternalId, io, {
162
+ signal: abortControllerRef.current?.signal,
163
+ channel,
164
+ timeoutInSeconds,
165
+ lastEventId,
166
+ from,
167
+ onPart: (part) => {
168
+ lastSeenEventId = part.id;
169
+ partsBatch.push(part);
170
+ },
171
+ onControl: (event) => {
172
+ setLastControl(event);
173
+ onControl(event);
174
+ },
175
+ });
176
+ const recordsQueue = createThrottledQueue(async (newRecords) => {
177
+ const combined = [...existingRecordsRef.current, ...newRecords];
178
+ const bounded = maxRecords != null && maxRecords >= 0 && combined.length > maxRecords
179
+ ? combined.slice(combined.length - maxRecords)
180
+ : combined;
181
+ existingRecordsRef.current = bounded;
182
+ mutateRecordsData(bounded);
183
+ publishLastEventId();
184
+ flushParts();
185
+ }, throttleInMs);
186
+ for await (const record of stream) {
187
+ recordsQueue.add(record);
188
+ }
189
+ await recordsQueue.flush();
190
+ publishLastEventId();
191
+ flushParts();
192
+ }
193
+ catch (err) {
194
+ if (err.name === "AbortError") {
195
+ return;
196
+ }
197
+ if (err instanceof Error) {
198
+ onError(err);
199
+ }
200
+ else {
201
+ onError(new Error(String(err)));
202
+ }
203
+ throw err;
204
+ }
205
+ }
206
+ //# sourceMappingURL=useSessionStreamChannel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSessionStreamChannel.js","sourceRoot":"","sources":["../../../src/hooks/useSessionStreamChann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CAAC;YACrC,iBAAiB,CAAC,OAAO,CAAC,CAAC;YAC3B,kBAAkB,EAAE,CAAC;YACrB,UAAU,EAAE,CAAC;QACf,CAAC,EAAE,YAAY,CAAC,CAAC;QAEjB,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,MAAM,EAAE,CAAC;YAClC,YAAY,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAC3B,CAAC;QAED,MAAM,YAAY,CAAC,KAAK,EAAE,CAAC;QAC3B,kBAAkB,EAAE,CAAC;QACrB,UAAU,EAAE,CAAC;IACf,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAK,GAAW,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YACvC,OAAO;QACT,CAAC;QAED,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;YACzB,OAAO,CAAC,GAAG,CAAC,CAAC;QACf,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAClC,CAAC;QAED,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC"}
@@ -5,3 +5,5 @@ export * from "./hooks/useRealtime.js";
5
5
  export * from "./hooks/useTaskTrigger.js";
6
6
  export * from "./hooks/useWaitToken.js";
7
7
  export * from "./hooks/useInputStreamSend.js";
8
+ export * from "./hooks/useSessionStream.js";
9
+ export * from "./hooks/useSessionStreamChannel.js";
package/dist/esm/index.js CHANGED
@@ -5,4 +5,6 @@ export * from "./hooks/useRealtime.js";
5
5
  export * from "./hooks/useTaskTrigger.js";
6
6
  export * from "./hooks/useWaitToken.js";
7
7
  export * from "./hooks/useInputStreamSend.js";
8
+ export * from "./hooks/useSessionStream.js";
9
+ export * from "./hooks/useSessionStreamChannel.js";
8
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oCAAoC,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Keep subscription lifecycles controlled by their effects while using the
3
+ * latest request inputs.
4
+ */
5
+ export declare function useStableRequestCallback(callback: () => Promise<void>): () => Promise<void>;
@@ -0,0 +1,14 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useRef } from "react";
3
+ /**
4
+ * Keep subscription lifecycles controlled by their effects while using the
5
+ * latest request inputs.
6
+ */
7
+ export function useStableRequestCallback(callback) {
8
+ const callbackRef = useRef(callback);
9
+ useEffect(() => {
10
+ callbackRef.current = callback;
11
+ }, [callback]);
12
+ return useCallback(() => callbackRef.current(), []);
13
+ }
14
+ //# sourceMappingURL=useStableRequestCallback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStableRequestCallback.js","sourceRoot":"","sources":["../../../src/utils/useStableRequestCallback.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEvD;;;GAGG;AACH,MAAM,UAAU,wBAAwB,CAAC,QAA6B;IACpE,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;IAErC,SAAS,CAAC,GAAG,EAAE;QACb,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;IACjC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,OAAO,WAAW,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;AACtD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trigger.dev/react-hooks",
3
- "version": "4.5.13",
3
+ "version": "4.5.15",
4
4
  "description": "trigger.dev react hooks",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -29,7 +29,7 @@
29
29
  ]
30
30
  },
31
31
  "dependencies": {
32
- "@trigger.dev/core": "^4.5.13",
32
+ "@trigger.dev/core": "^4.5.15",
33
33
  "swr": "^2.2.5"
34
34
  },
35
35
  "devDependencies": {