@trigger.dev/sdk 0.0.0-prerelease-20260302145933 → 0.0.0-prerelease-20260305142821

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 (38) hide show
  1. package/dist/commonjs/v3/ai.d.ts +347 -2
  2. package/dist/commonjs/v3/ai.js +563 -1
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-constants.d.ts +10 -0
  5. package/dist/commonjs/v3/chat-constants.js +14 -0
  6. package/dist/commonjs/v3/chat-constants.js.map +1 -0
  7. package/dist/commonjs/v3/chat-react.d.ts +45 -0
  8. package/dist/commonjs/v3/chat-react.js +71 -0
  9. package/dist/commonjs/v3/chat-react.js.map +1 -0
  10. package/dist/commonjs/v3/chat.d.ts +241 -0
  11. package/dist/commonjs/v3/chat.js +343 -0
  12. package/dist/commonjs/v3/chat.js.map +1 -0
  13. package/dist/commonjs/v3/chat.test.d.ts +1 -0
  14. package/dist/commonjs/v3/chat.test.js +1557 -0
  15. package/dist/commonjs/v3/chat.test.js.map +1 -0
  16. package/dist/commonjs/v3/runs.d.ts +3 -3
  17. package/dist/commonjs/v3/streams.js +27 -17
  18. package/dist/commonjs/v3/streams.js.map +1 -1
  19. package/dist/commonjs/version.js +1 -1
  20. package/dist/esm/v3/ai.d.ts +347 -2
  21. package/dist/esm/v3/ai.js +564 -2
  22. package/dist/esm/v3/ai.js.map +1 -1
  23. package/dist/esm/v3/chat-constants.d.ts +10 -0
  24. package/dist/esm/v3/chat-constants.js +11 -0
  25. package/dist/esm/v3/chat-constants.js.map +1 -0
  26. package/dist/esm/v3/chat-react.d.ts +45 -0
  27. package/dist/esm/v3/chat-react.js +68 -0
  28. package/dist/esm/v3/chat-react.js.map +1 -0
  29. package/dist/esm/v3/chat.d.ts +241 -0
  30. package/dist/esm/v3/chat.js +338 -0
  31. package/dist/esm/v3/chat.js.map +1 -0
  32. package/dist/esm/v3/chat.test.d.ts +1 -0
  33. package/dist/esm/v3/chat.test.js +1555 -0
  34. package/dist/esm/v3/chat.test.js.map +1 -0
  35. package/dist/esm/v3/streams.js +27 -17
  36. package/dist/esm/v3/streams.js.map +1 -1
  37. package/dist/esm/version.js +1 -1
  38. package/package.json +40 -5
@@ -0,0 +1,45 @@
1
+ import { TriggerChatTransport, type TriggerChatTransportOptions } from "./chat.js";
2
+ import type { AnyTask, TaskIdentifier } from "@trigger.dev/core/v3";
3
+ /**
4
+ * Options for `useTriggerChatTransport`, with a type-safe `task` field.
5
+ *
6
+ * Pass a task type parameter to get compile-time validation of the task ID:
7
+ * ```ts
8
+ * useTriggerChatTransport<typeof myTask>({ task: "my-task", ... })
9
+ * ```
10
+ */
11
+ export type UseTriggerChatTransportOptions<TTask extends AnyTask = AnyTask> = Omit<TriggerChatTransportOptions, "task"> & {
12
+ /** The task ID. Strongly typed when a task type parameter is provided. */
13
+ task: TaskIdentifier<TTask>;
14
+ };
15
+ /**
16
+ * React hook that creates and memoizes a `TriggerChatTransport` instance.
17
+ *
18
+ * The transport is created once on first render and reused for the lifetime
19
+ * of the component. This avoids the need for `useMemo` and ensures the
20
+ * transport's internal session state (run IDs, lastEventId, etc.)
21
+ * is preserved across re-renders.
22
+ *
23
+ * For dynamic access tokens, pass a function — it will be called on each
24
+ * request without needing to recreate the transport.
25
+ *
26
+ * The `onSessionChange` callback is kept in a ref so the transport always
27
+ * calls the latest version without needing to be recreated.
28
+ *
29
+ * @example
30
+ * ```tsx
31
+ * import { useChat } from "@ai-sdk/react";
32
+ * import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
33
+ * import type { chat } from "@/trigger/chat";
34
+ *
35
+ * function Chat() {
36
+ * const transport = useTriggerChatTransport<typeof chat>({
37
+ * task: "ai-chat",
38
+ * accessToken: () => fetchToken(),
39
+ * });
40
+ *
41
+ * const { messages, sendMessage } = useChat({ transport });
42
+ * }
43
+ * ```
44
+ */
45
+ export declare function useTriggerChatTransport<TTask extends AnyTask = AnyTask>(options: UseTriggerChatTransportOptions<TTask>): TriggerChatTransport;
@@ -0,0 +1,71 @@
1
+ "use client";
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useTriggerChatTransport = useTriggerChatTransport;
5
+ /**
6
+ * @module @trigger.dev/sdk/chat/react
7
+ *
8
+ * React hooks for AI SDK chat transport integration.
9
+ * Use alongside `@trigger.dev/sdk/chat` for a type-safe, ergonomic DX.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * import { useChat } from "@ai-sdk/react";
14
+ * import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
15
+ * import type { chat } from "@/trigger/chat";
16
+ *
17
+ * function Chat() {
18
+ * const transport = useTriggerChatTransport<typeof chat>({
19
+ * task: "ai-chat",
20
+ * accessToken: () => fetchToken(),
21
+ * });
22
+ *
23
+ * const { messages, sendMessage } = useChat({ transport });
24
+ * }
25
+ * ```
26
+ */
27
+ const react_1 = require("react");
28
+ const chat_js_1 = require("./chat.js");
29
+ /**
30
+ * React hook that creates and memoizes a `TriggerChatTransport` instance.
31
+ *
32
+ * The transport is created once on first render and reused for the lifetime
33
+ * of the component. This avoids the need for `useMemo` and ensures the
34
+ * transport's internal session state (run IDs, lastEventId, etc.)
35
+ * is preserved across re-renders.
36
+ *
37
+ * For dynamic access tokens, pass a function — it will be called on each
38
+ * request without needing to recreate the transport.
39
+ *
40
+ * The `onSessionChange` callback is kept in a ref so the transport always
41
+ * calls the latest version without needing to be recreated.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * import { useChat } from "@ai-sdk/react";
46
+ * import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
47
+ * import type { chat } from "@/trigger/chat";
48
+ *
49
+ * function Chat() {
50
+ * const transport = useTriggerChatTransport<typeof chat>({
51
+ * task: "ai-chat",
52
+ * accessToken: () => fetchToken(),
53
+ * });
54
+ *
55
+ * const { messages, sendMessage } = useChat({ transport });
56
+ * }
57
+ * ```
58
+ */
59
+ function useTriggerChatTransport(options) {
60
+ const ref = (0, react_1.useRef)(null);
61
+ if (ref.current === null) {
62
+ ref.current = new chat_js_1.TriggerChatTransport(options);
63
+ }
64
+ // Keep onSessionChange up to date without recreating the transport
65
+ const { onSessionChange } = options;
66
+ (0, react_1.useEffect)(() => {
67
+ ref.current?.setOnSessionChange(onSessionChange);
68
+ }, [onSessionChange]);
69
+ return ref.current;
70
+ }
71
+ //# sourceMappingURL=chat-react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-react.js","sourceRoot":"","sources":["../../../src/v3/chat-react.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AA8Eb,0DAeC;AA3FD;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,iCAA0C;AAC1C,uCAGmB;AAmBnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,SAAgB,uBAAuB,CACrC,OAA8C;IAE9C,MAAM,GAAG,GAAG,IAAA,cAAM,EAA8B,IAAI,CAAC,CAAC;IACtD,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;QACzB,GAAG,CAAC,OAAO,GAAG,IAAI,8BAAoB,CAAC,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,mEAAmE;IACnE,MAAM,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC;IACpC,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,GAAG,CAAC,OAAO,EAAE,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACnD,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC;IAEtB,OAAO,GAAG,CAAC,OAAO,CAAC;AACrB,CAAC"}
@@ -0,0 +1,241 @@
1
+ /**
2
+ * @module @trigger.dev/sdk/chat
3
+ *
4
+ * Browser-safe module for AI SDK chat transport integration.
5
+ * Use this on the frontend with the AI SDK's `useChat` hook.
6
+ *
7
+ * For backend helpers (`chatTask`, `pipeChat`), use `@trigger.dev/sdk/ai` instead.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * import { useChat } from "@ai-sdk/react";
12
+ * import { TriggerChatTransport } from "@trigger.dev/sdk/chat";
13
+ *
14
+ * function Chat({ accessToken }: { accessToken: string }) {
15
+ * const { messages, sendMessage, status } = useChat({
16
+ * transport: new TriggerChatTransport({
17
+ * task: "my-chat-task",
18
+ * accessToken,
19
+ * }),
20
+ * });
21
+ * }
22
+ * ```
23
+ */
24
+ import type { ChatTransport, UIMessage, UIMessageChunk, ChatRequestOptions } from "ai";
25
+ /**
26
+ * Options for creating a TriggerChatTransport.
27
+ */
28
+ export type TriggerChatTransportOptions = {
29
+ /**
30
+ * The Trigger.dev task ID to trigger for chat completions.
31
+ * This task should be defined using `chatTask()` from `@trigger.dev/sdk/ai`,
32
+ * or a regular `task()` that uses `pipeChat()`.
33
+ */
34
+ task: string;
35
+ /**
36
+ * An access token for authenticating with the Trigger.dev API.
37
+ *
38
+ * This must be a token with permission to trigger the task. You can use:
39
+ * - A **trigger public token** created via `auth.createTriggerPublicToken(taskId)` (recommended for frontend use)
40
+ * - A **secret API key** (for server-side use only — never expose in the browser)
41
+ *
42
+ * Can also be a function that returns a token string (sync or async),
43
+ * useful for dynamic token refresh or passing a Next.js server action directly.
44
+ */
45
+ accessToken: string | (() => string | Promise<string>);
46
+ /**
47
+ * Base URL for the Trigger.dev API.
48
+ * @default "https://api.trigger.dev"
49
+ */
50
+ baseURL?: string;
51
+ /**
52
+ * The stream key where the task pipes UIMessageChunk data.
53
+ * When using `chatTask()` or `pipeChat()`, this is handled automatically.
54
+ * Only set this if you're using a custom stream key.
55
+ *
56
+ * @default "chat"
57
+ */
58
+ streamKey?: string;
59
+ /**
60
+ * Additional headers to include in API requests to Trigger.dev.
61
+ */
62
+ headers?: Record<string, string>;
63
+ /**
64
+ * The number of seconds to wait for the realtime stream to produce data
65
+ * before timing out.
66
+ *
67
+ * @default 120
68
+ */
69
+ streamTimeoutSeconds?: number;
70
+ /**
71
+ * Default metadata included in every request payload.
72
+ * Merged with per-call `metadata` from `sendMessage()` — per-call values
73
+ * take precedence over transport-level defaults.
74
+ *
75
+ * Useful for data that should accompany every message, like a user ID.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * new TriggerChatTransport({
80
+ * task: "my-chat",
81
+ * accessToken,
82
+ * metadata: { userId: currentUser.id },
83
+ * });
84
+ * ```
85
+ */
86
+ metadata?: Record<string, unknown>;
87
+ /**
88
+ * Restore active chat sessions from external storage (e.g. localStorage).
89
+ *
90
+ * After a page refresh, pass previously persisted sessions here so the
91
+ * transport can reconnect to existing runs instead of starting new ones.
92
+ * Use `getSession()` to retrieve session state for persistence.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * new TriggerChatTransport({
97
+ * task: "my-chat",
98
+ * accessToken,
99
+ * sessions: {
100
+ * "chat-abc": { runId: "run_123", publicAccessToken: "...", lastEventId: "42" },
101
+ * },
102
+ * });
103
+ * ```
104
+ */
105
+ sessions?: Record<string, {
106
+ runId: string;
107
+ publicAccessToken: string;
108
+ lastEventId?: string;
109
+ }>;
110
+ /**
111
+ * Called whenever a chat session's state changes.
112
+ *
113
+ * Fires when:
114
+ * - A new session is created (after triggering a task)
115
+ * - A turn completes (lastEventId updated)
116
+ * - A session is removed (run ended or input stream send failed) — `session` will be `null`
117
+ *
118
+ * Use this to persist session state for reconnection after page refreshes,
119
+ * without needing to call `getSession()` manually.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * new TriggerChatTransport({
124
+ * task: "my-chat",
125
+ * accessToken,
126
+ * onSessionChange: (chatId, session) => {
127
+ * if (session) {
128
+ * localStorage.setItem(`session:${chatId}`, JSON.stringify(session));
129
+ * } else {
130
+ * localStorage.removeItem(`session:${chatId}`);
131
+ * }
132
+ * },
133
+ * });
134
+ * ```
135
+ */
136
+ onSessionChange?: (chatId: string, session: {
137
+ runId: string;
138
+ publicAccessToken: string;
139
+ lastEventId?: string;
140
+ } | null) => void;
141
+ };
142
+ /**
143
+ * A custom AI SDK `ChatTransport` that runs chat completions as durable Trigger.dev tasks.
144
+ *
145
+ * When `sendMessages` is called, the transport:
146
+ * 1. Triggers a Trigger.dev task (or sends to an existing run via input streams)
147
+ * 2. Subscribes to the task's realtime stream to receive `UIMessageChunk` data
148
+ * 3. Returns a `ReadableStream<UIMessageChunk>` that the AI SDK processes natively
149
+ *
150
+ * Calling `stop()` from `useChat` sends a stop signal via input streams, which
151
+ * aborts the current `streamText` call in the task without ending the run.
152
+ *
153
+ * @example
154
+ * ```tsx
155
+ * import { useChat } from "@ai-sdk/react";
156
+ * import { TriggerChatTransport } from "@trigger.dev/sdk/chat";
157
+ *
158
+ * function Chat({ accessToken }: { accessToken: string }) {
159
+ * const { messages, sendMessage, stop, status } = useChat({
160
+ * transport: new TriggerChatTransport({
161
+ * task: "my-chat-task",
162
+ * accessToken,
163
+ * }),
164
+ * });
165
+ *
166
+ * // stop() sends a stop signal — the task aborts streamText but keeps the run alive
167
+ * }
168
+ * ```
169
+ */
170
+ export declare class TriggerChatTransport implements ChatTransport<UIMessage> {
171
+ private readonly taskId;
172
+ private readonly resolveAccessToken;
173
+ private readonly baseURL;
174
+ private readonly streamKey;
175
+ private readonly extraHeaders;
176
+ private readonly streamTimeoutSeconds;
177
+ private readonly defaultMetadata;
178
+ private _onSessionChange;
179
+ private sessions;
180
+ constructor(options: TriggerChatTransportOptions);
181
+ sendMessages: (options: {
182
+ trigger: "submit-message" | "regenerate-message";
183
+ chatId: string;
184
+ messageId: string | undefined;
185
+ messages: UIMessage[];
186
+ abortSignal: AbortSignal | undefined;
187
+ } & ChatRequestOptions) => Promise<ReadableStream<UIMessageChunk>>;
188
+ reconnectToStream: (options: {
189
+ chatId: string;
190
+ } & ChatRequestOptions) => Promise<ReadableStream<UIMessageChunk> | null>;
191
+ /**
192
+ * Get the current session state for a chat, suitable for external persistence.
193
+ *
194
+ * Returns `undefined` if no active session exists for this chatId.
195
+ * Persist the returned value to localStorage so it can be restored
196
+ * after a page refresh via `restoreSession()`.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const session = transport.getSession(chatId);
201
+ * if (session) {
202
+ * localStorage.setItem(`session:${chatId}`, JSON.stringify(session));
203
+ * }
204
+ * ```
205
+ */
206
+ getSession: (chatId: string) => {
207
+ runId: string;
208
+ publicAccessToken: string;
209
+ lastEventId?: string;
210
+ } | undefined;
211
+ /**
212
+ * Update the `onSessionChange` callback.
213
+ * Useful for React hooks that need to update the callback without recreating the transport.
214
+ */
215
+ setOnSessionChange(callback: ((chatId: string, session: {
216
+ runId: string;
217
+ publicAccessToken: string;
218
+ lastEventId?: string;
219
+ } | null) => void) | undefined): void;
220
+ private notifySessionChange;
221
+ private subscribeToStream;
222
+ }
223
+ /**
224
+ * Creates a new `TriggerChatTransport` instance.
225
+ *
226
+ * @example
227
+ * ```tsx
228
+ * import { useChat } from "@ai-sdk/react";
229
+ * import { createChatTransport } from "@trigger.dev/sdk/chat";
230
+ *
231
+ * const transport = createChatTransport({
232
+ * task: "my-chat-task",
233
+ * accessToken: publicAccessToken,
234
+ * });
235
+ *
236
+ * function Chat() {
237
+ * const { messages, sendMessage } = useChat({ transport });
238
+ * }
239
+ * ```
240
+ */
241
+ export declare function createChatTransport(options: TriggerChatTransportOptions): TriggerChatTransport;