@trigger.dev/sdk 4.5.0-rc.6 → 4.5.0-rc.7

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 (191) hide show
  1. package/dist/commonjs/v3/ai.d.ts +171 -5
  2. package/dist/commonjs/v3/ai.js +309 -22
  3. package/dist/commonjs/v3/ai.js.map +1 -1
  4. package/dist/commonjs/v3/chat-server.d.ts +8 -0
  5. package/dist/commonjs/v3/chat-server.js +32 -10
  6. package/dist/commonjs/v3/chat-server.js.map +1 -1
  7. package/dist/commonjs/v3/chat-server.test.js +51 -0
  8. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  9. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  10. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  11. package/dist/commonjs/v3/sessions.d.ts +3 -2
  12. package/dist/commonjs/v3/sessions.js +3 -2
  13. package/dist/commonjs/v3/sessions.js.map +1 -1
  14. package/dist/commonjs/version.js +1 -1
  15. package/dist/esm/v3/ai.d.ts +171 -5
  16. package/dist/esm/v3/ai.js +309 -22
  17. package/dist/esm/v3/ai.js.map +1 -1
  18. package/dist/esm/v3/chat-server.d.ts +8 -0
  19. package/dist/esm/v3/chat-server.js +32 -10
  20. package/dist/esm/v3/chat-server.js.map +1 -1
  21. package/dist/esm/v3/chat-server.test.js +51 -0
  22. package/dist/esm/v3/chat-server.test.js.map +1 -1
  23. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  24. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  25. package/dist/esm/v3/sessions.d.ts +3 -2
  26. package/dist/esm/v3/sessions.js +3 -2
  27. package/dist/esm/v3/sessions.js.map +1 -1
  28. package/dist/esm/version.js +1 -1
  29. package/docs/ai/prompts.mdx +430 -0
  30. package/docs/ai-chat/actions.mdx +115 -0
  31. package/docs/ai-chat/anatomy.mdx +71 -0
  32. package/docs/ai-chat/backend.mdx +817 -0
  33. package/docs/ai-chat/background-injection.mdx +221 -0
  34. package/docs/ai-chat/changelog.mdx +850 -0
  35. package/docs/ai-chat/chat-local.mdx +174 -0
  36. package/docs/ai-chat/client-protocol.mdx +1081 -0
  37. package/docs/ai-chat/compaction.mdx +411 -0
  38. package/docs/ai-chat/custom-agents.mdx +364 -0
  39. package/docs/ai-chat/error-handling.mdx +415 -0
  40. package/docs/ai-chat/fast-starts.mdx +672 -0
  41. package/docs/ai-chat/frontend.mdx +580 -0
  42. package/docs/ai-chat/how-it-works.mdx +230 -0
  43. package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
  44. package/docs/ai-chat/mcp.mdx +101 -0
  45. package/docs/ai-chat/overview.mdx +90 -0
  46. package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
  47. package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
  48. package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
  49. package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
  50. package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
  51. package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
  52. package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
  53. package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
  54. package/docs/ai-chat/patterns/skills.mdx +221 -0
  55. package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
  56. package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
  57. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
  58. package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
  59. package/docs/ai-chat/pending-messages.mdx +343 -0
  60. package/docs/ai-chat/prompt-caching.mdx +206 -0
  61. package/docs/ai-chat/quick-start.mdx +161 -0
  62. package/docs/ai-chat/reference.mdx +909 -0
  63. package/docs/ai-chat/server-chat.mdx +263 -0
  64. package/docs/ai-chat/sessions.mdx +333 -0
  65. package/docs/ai-chat/testing.mdx +682 -0
  66. package/docs/ai-chat/tools.mdx +191 -0
  67. package/docs/ai-chat/types.mdx +242 -0
  68. package/docs/ai-chat/upgrade-guide.mdx +515 -0
  69. package/docs/apikeys.mdx +54 -0
  70. package/docs/building-with-ai.mdx +261 -0
  71. package/docs/bulk-actions.mdx +49 -0
  72. package/docs/changelog.mdx +6 -0
  73. package/docs/cli-deploy-commands.mdx +9 -0
  74. package/docs/cli-dev-commands.mdx +9 -0
  75. package/docs/cli-dev.mdx +8 -0
  76. package/docs/cli-init-commands.mdx +58 -0
  77. package/docs/cli-introduction.mdx +25 -0
  78. package/docs/cli-list-profiles-commands.mdx +42 -0
  79. package/docs/cli-login-commands.mdx +33 -0
  80. package/docs/cli-logout-commands.mdx +33 -0
  81. package/docs/cli-preview-archive.mdx +59 -0
  82. package/docs/cli-promote-commands.mdx +9 -0
  83. package/docs/cli-switch.mdx +43 -0
  84. package/docs/cli-update-commands.mdx +42 -0
  85. package/docs/cli-whoami-commands.mdx +33 -0
  86. package/docs/community.mdx +6 -0
  87. package/docs/config/config-file.mdx +602 -0
  88. package/docs/config/extensions/additionalFiles.mdx +38 -0
  89. package/docs/config/extensions/additionalPackages.mdx +40 -0
  90. package/docs/config/extensions/aptGet.mdx +34 -0
  91. package/docs/config/extensions/audioWaveform.mdx +20 -0
  92. package/docs/config/extensions/custom.mdx +380 -0
  93. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  94. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  95. package/docs/config/extensions/ffmpeg.mdx +45 -0
  96. package/docs/config/extensions/lightpanda.mdx +56 -0
  97. package/docs/config/extensions/overview.mdx +67 -0
  98. package/docs/config/extensions/playwright.mdx +195 -0
  99. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  100. package/docs/config/extensions/puppeteer.mdx +30 -0
  101. package/docs/config/extensions/pythonExtension.mdx +182 -0
  102. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  103. package/docs/context.mdx +235 -0
  104. package/docs/database-connections.mdx +213 -0
  105. package/docs/deploy-environment-variables.mdx +435 -0
  106. package/docs/deployment/atomic-deployment.mdx +172 -0
  107. package/docs/deployment/overview.mdx +257 -0
  108. package/docs/deployment/preview-branches.mdx +224 -0
  109. package/docs/errors-retrying.mdx +379 -0
  110. package/docs/github-actions.mdx +222 -0
  111. package/docs/github-integration.mdx +136 -0
  112. package/docs/github-repo.mdx +8 -0
  113. package/docs/help-email.mdx +6 -0
  114. package/docs/help-slack.mdx +11 -0
  115. package/docs/hidden-tasks.mdx +56 -0
  116. package/docs/how-it-works.mdx +454 -0
  117. package/docs/how-to-reduce-your-spend.mdx +217 -0
  118. package/docs/idempotency.mdx +504 -0
  119. package/docs/introduction.mdx +223 -0
  120. package/docs/limits.mdx +241 -0
  121. package/docs/logging.mdx +195 -0
  122. package/docs/machines.mdx +952 -0
  123. package/docs/manual-setup.mdx +632 -0
  124. package/docs/mcp-agent-rules.mdx +41 -0
  125. package/docs/mcp-introduction.mdx +385 -0
  126. package/docs/mcp-tools.mdx +273 -0
  127. package/docs/migrating-from-v3.mdx +334 -0
  128. package/docs/observability/dashboards.mdx +102 -0
  129. package/docs/observability/query.mdx +585 -0
  130. package/docs/open-source-contributing.mdx +16 -0
  131. package/docs/open-source-self-hosting.mdx +541 -0
  132. package/docs/private-networking/aws-console-setup.mdx +304 -0
  133. package/docs/private-networking/overview.mdx +144 -0
  134. package/docs/private-networking/troubleshooting.mdx +78 -0
  135. package/docs/queue-concurrency.mdx +354 -0
  136. package/docs/quick-start.mdx +97 -0
  137. package/docs/realtime/auth.mdx +208 -0
  138. package/docs/realtime/backend/overview.mdx +45 -0
  139. package/docs/realtime/backend/streams.mdx +418 -0
  140. package/docs/realtime/backend/subscribe.mdx +225 -0
  141. package/docs/realtime/how-it-works.mdx +94 -0
  142. package/docs/realtime/overview.mdx +63 -0
  143. package/docs/realtime/react-hooks/overview.mdx +73 -0
  144. package/docs/realtime/react-hooks/streams.mdx +449 -0
  145. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  146. package/docs/realtime/react-hooks/swr.mdx +87 -0
  147. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  148. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  149. package/docs/realtime/run-object.mdx +174 -0
  150. package/docs/replaying.mdx +72 -0
  151. package/docs/request-feature.mdx +6 -0
  152. package/docs/roadmap.mdx +6 -0
  153. package/docs/run-tests.mdx +20 -0
  154. package/docs/run-usage.mdx +113 -0
  155. package/docs/runs/heartbeats.mdx +38 -0
  156. package/docs/runs/max-duration.mdx +139 -0
  157. package/docs/runs/metadata.mdx +734 -0
  158. package/docs/runs/priority.mdx +31 -0
  159. package/docs/runs.mdx +396 -0
  160. package/docs/self-hosting/docker.mdx +458 -0
  161. package/docs/self-hosting/env/supervisor.mdx +74 -0
  162. package/docs/self-hosting/env/webapp.mdx +276 -0
  163. package/docs/self-hosting/kubernetes.mdx +601 -0
  164. package/docs/self-hosting/overview.mdx +108 -0
  165. package/docs/skills.mdx +85 -0
  166. package/docs/tags.mdx +120 -0
  167. package/docs/tasks/overview.mdx +697 -0
  168. package/docs/tasks/scheduled.mdx +382 -0
  169. package/docs/tasks/schemaTask.mdx +413 -0
  170. package/docs/tasks/streams.mdx +884 -0
  171. package/docs/triggering.mdx +1320 -0
  172. package/docs/troubleshooting-alerts.mdx +385 -0
  173. package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
  174. package/docs/troubleshooting-github-issues.mdx +6 -0
  175. package/docs/troubleshooting-uptime-status.mdx +6 -0
  176. package/docs/troubleshooting.mdx +398 -0
  177. package/docs/upgrading-packages.mdx +80 -0
  178. package/docs/vercel-integration.mdx +207 -0
  179. package/docs/versioning.mdx +56 -0
  180. package/docs/video-walkthrough.mdx +23 -0
  181. package/docs/wait-for-token.mdx +540 -0
  182. package/docs/wait-for.mdx +42 -0
  183. package/docs/wait-until.mdx +53 -0
  184. package/docs/wait.mdx +18 -0
  185. package/docs/writing-tasks-introduction.mdx +33 -0
  186. package/package.json +8 -5
  187. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  188. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  189. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  190. package/skills/trigger-cost-savings/SKILL.md +116 -0
  191. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,580 @@
1
+ ---
2
+ title: "Frontend"
3
+ sidebarTitle: "Frontend"
4
+ description: "Transport setup, session management, client data, and frontend patterns for AI Chat."
5
+ ---
6
+
7
+ import RcBanner from "/snippets/ai-chat-rc-banner.mdx";
8
+
9
+ <RcBanner />
10
+
11
+ ## How the transport works
12
+
13
+ Vanilla `useChat` expects an `api` URL — it POSTs the conversation to your own Next.js route handler, which terminates the stream. `useTriggerChatTransport` replaces that round-trip: instead of an `api` URL, you pass a custom [`ChatTransport`](https://ai-sdk.dev/docs/ai-sdk-ui/transport) that talks directly to the Trigger.dev cloud (or your self-hosted webapp) on behalf of `useChat`.
14
+
15
+ There's no API route to maintain. The browser uses a short-lived session-scoped PAT (minted by your `accessToken` server action) to:
16
+
17
+ - **Create the session** via your `startSession` action on the first message (or `transport.preload(chatId)`).
18
+ - **Append the new user message** to the session's durable `.in` stream.
19
+ - **Subscribe to the `.out` SSE stream** for the agent's response chunks (text, tool calls, reasoning, custom `data-*` parts).
20
+
21
+ The transport handles the auth refresh, reconnect, `Last-Event-ID` resume, and stop-signal plumbing transparently. `useChat` sees the result as `UIMessageChunk`s and renders them unchanged.
22
+
23
+ ## Transport setup
24
+
25
+ Use the `useTriggerChatTransport` hook from `@trigger.dev/sdk/chat/react` to create a memoized transport instance, then pass it to `useChat`:
26
+
27
+ ```tsx
28
+ import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
29
+ import { useChat } from "@ai-sdk/react";
30
+ import type { myChat } from "@/trigger/chat";
31
+ import { mintChatAccessToken, startChatSession } from "@/app/actions";
32
+
33
+ export function Chat() {
34
+ const transport = useTriggerChatTransport<typeof myChat>({
35
+ task: "my-chat",
36
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
37
+ startSession: ({ chatId, clientData }) =>
38
+ startChatSession({ chatId, clientData }),
39
+ });
40
+
41
+ const { messages, sendMessage, stop, status } = useChat({ transport });
42
+ // ... render UI
43
+ }
44
+ ```
45
+
46
+ The transport is created once on first render and reused across re-renders. Pass a type parameter for compile-time validation of the task ID.
47
+
48
+ The two callbacks have distinct responsibilities:
49
+
50
+ - **`accessToken`** is a *pure* PAT mint — the transport invokes it on a 401/403 to refresh the session-scoped token. Customer wraps `auth.createPublicToken({ scopes: { read: { sessions: chatId }, write: { sessions: chatId } } })`, which resolves to a `Promise<string>` (the JWT). Return that string from your `accessToken` callback.
51
+ - **`startSession`** wraps `chat.createStartSessionAction(taskId)` and is called when the transport needs to *create* the session (`transport.preload(chatId)`, or lazily on the first `sendMessage` for a chatId without a cached PAT). The customer's server controls authorization here, alongside any DB writes paired with session creation.
52
+
53
+ See [Quick start](/ai-chat/quick-start) for the matching server actions.
54
+
55
+ <Tip>
56
+ The hook keeps `onSessionChange` and `clientData` up to date via internal refs, so you don't need
57
+ to memoize callbacks or worry about stale closures when those options change between renders.
58
+ </Tip>
59
+
60
+ ## Typed messages (`chat.withUIMessage`)
61
+
62
+ If your chat agent is defined with [`chat.withUIMessage<YourUIMessage>()`](/ai-chat/types) (custom `data-*` parts, typed tools, etc.), pass the same message type through `useChat` so `messages` and `message.parts` are narrowed on the client:
63
+
64
+ ```tsx
65
+ import { useChat } from "@ai-sdk/react";
66
+ import { useTriggerChatTransport, type InferChatUIMessage } from "@trigger.dev/sdk/chat/react";
67
+ import type { myChat } from "./myChat";
68
+
69
+ type Msg = InferChatUIMessage<typeof myChat>;
70
+
71
+ const transport = useTriggerChatTransport<typeof myChat>({
72
+ task: "my-chat",
73
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
74
+ startSession: ({ chatId, clientData }) =>
75
+ startChatSession({ chatId, clientData }),
76
+ });
77
+ const { messages } = useChat<Msg>({ transport });
78
+ ```
79
+
80
+ See the [Types](/ai-chat/types) guide for defining `YourUIMessage`, default stream options, and backend examples.
81
+
82
+ ### Calling a fetch endpoint instead of a server action
83
+
84
+ If you want to mint tokens via a REST endpoint instead of a Next.js server action, the same callbacks accept any async function. Import `AccessTokenParams` and `StartSessionParams` from `@trigger.dev/sdk/chat` to type your fetch handler.
85
+
86
+ ```ts
87
+ import type { AccessTokenParams, StartSessionParams } from "@trigger.dev/sdk/chat";
88
+
89
+ const transport = useTriggerChatTransport({
90
+ task: "my-chat",
91
+ accessToken: async ({ chatId }: AccessTokenParams) => {
92
+ const res = await fetch(`/api/chat/${chatId}/access-token`, { method: "POST" });
93
+ return res.text();
94
+ },
95
+ startSession: async ({ chatId, taskId, clientData }: StartSessionParams) => {
96
+ const res = await fetch(`/api/chat/${chatId}/start`, {
97
+ method: "POST",
98
+ body: JSON.stringify({ taskId, clientData }),
99
+ });
100
+ return res.json(); // { publicAccessToken: string }
101
+ },
102
+ });
103
+ ```
104
+
105
+ The fetch handlers on the server side wrap the same SDK helpers as the server-action variant: `auth.createPublicToken({ scopes: { read: { sessions: chatId }, write: { sessions: chatId } } })` for refresh and `chat.createStartSessionAction(taskId)` for create.
106
+
107
+ ## Session management
108
+
109
+ Every chat is backed by a durable Session — the row that owns the chat's runs, persists across run lifecycles, and orchestrates handoffs. The transport manages the session for you; what you persist on your side is a small piece of state per chat that lets a fresh tab resume without a round-trip to create a new session.
110
+
111
+ ### What the transport persists per chat
112
+
113
+ | Field | Type | Notes |
114
+ | --- | --- | --- |
115
+ | `publicAccessToken` | `string` | Session-scoped JWT (`read:sessions:{chatId} + write:sessions:{chatId}`). Refreshed automatically on 401/403 via `accessToken`. |
116
+ | `lastEventId` | `string \| undefined` | Last SSE event received on `.out`. **Valid for the lifetime of the Session** — keep it across `endRun` / `requestUpgrade` / continuation-run boundaries; only clear when the Session itself closes. The cursor lets the next subscription open past the prior turn's stale `turn-complete` record. |
117
+ | `isStreaming` | `boolean \| undefined` | **Optional.** The transport sets it internally, but you don't have to persist it — the server decides "nothing is streaming" via the session's [`X-Session-Settled`](/ai-chat/client-protocol#x-session-settled-fast-close-on-idle-reconnects) signal on reconnect. If you do persist it, the transport keeps the fast-path short-circuit. If you drop it, reconnects open the SSE and close fast on settled sessions. |
118
+
119
+ ### Session cleanup (frontend)
120
+
121
+ Since session creation and updates are handled server-side, the frontend only needs to handle session deletion when a run ends:
122
+
123
+ ```tsx
124
+ const transport = useTriggerChatTransport<typeof myChat>({
125
+ task: "my-chat",
126
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
127
+ startSession: ({ chatId, clientData }) =>
128
+ startChatSession({ chatId, clientData }),
129
+ sessions: loadedSessions, // Restored from DB on page load
130
+ onSessionChange: (chatId, session) => {
131
+ if (!session) {
132
+ deleteSession(chatId); // Server action — run ended
133
+ }
134
+ },
135
+ });
136
+ ```
137
+
138
+ ### Restoring on page load
139
+
140
+ On page load, fetch both the messages and the session state from your database, then pass them to `useChat` and the transport. Pass `resume: true` to `useChat` when there's an existing conversation — this tells the AI SDK to reconnect to the stream via the transport.
141
+
142
+ Because the underlying Session row outlives individual runs, a chat you were in yesterday resumes against the same chat — even if the original run has long since exited. The transport hydrates from the persisted state and uses `lastEventId` to resubscribe; if the client tries to send a new message and no run is alive, the server triggers a fresh continuation run on the same session before the message is appended.
143
+
144
+ ```tsx app/chat/[chatId]/ChatPage.tsx
145
+ "use client";
146
+
147
+ import { useEffect, useState } from "react";
148
+ import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
149
+ import { useChat } from "@ai-sdk/react";
150
+ import {
151
+ mintChatAccessToken,
152
+ startChatSession,
153
+ getChatMessages,
154
+ getSession,
155
+ deleteSession,
156
+ } from "@/app/actions";
157
+
158
+ // Rendered from `app/chat/[chatId]/page.tsx`, which awaits `params`
159
+ // and forwards `chatId` into this client component:
160
+ //
161
+ // export default async function Page({ params }: { params: Promise<{ chatId: string }> }) {
162
+ // const { chatId } = await params;
163
+ // return <ChatPage chatId={chatId} />;
164
+ // }
165
+ export default function ChatPage({ chatId }: { chatId: string }) {
166
+ const [initialMessages, setInitialMessages] = useState([]);
167
+ const [initialSession, setInitialSession] = useState(undefined);
168
+ const [loaded, setLoaded] = useState(false);
169
+
170
+ useEffect(() => {
171
+ async function load() {
172
+ const [messages, session] = await Promise.all([getChatMessages(chatId), getSession(chatId)]);
173
+ setInitialMessages(messages);
174
+ setInitialSession(session ? { [chatId]: session } : undefined);
175
+ setLoaded(true);
176
+ }
177
+ load();
178
+ }, [chatId]);
179
+
180
+ if (!loaded) return null;
181
+
182
+ return (
183
+ <ChatClient
184
+ chatId={chatId}
185
+ initialMessages={initialMessages}
186
+ initialSessions={initialSession}
187
+ />
188
+ );
189
+ }
190
+
191
+ function ChatClient({ chatId, initialMessages, initialSessions }) {
192
+ const transport = useTriggerChatTransport({
193
+ task: "my-chat",
194
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
195
+ startSession: ({ chatId, clientData }) =>
196
+ startChatSession({ chatId, clientData }),
197
+ sessions: initialSessions,
198
+ onSessionChange: (id, session) => {
199
+ if (!session) deleteSession(id);
200
+ },
201
+ });
202
+
203
+ const { messages, sendMessage, stop, status } = useChat({
204
+ id: chatId,
205
+ messages: initialMessages,
206
+ transport,
207
+ resume: initialMessages.length > 0, // Resume if there's an existing conversation
208
+ });
209
+
210
+ // ... render UI
211
+ }
212
+ ```
213
+
214
+ <Info>
215
+ `resume: true` causes `useChat` to call `reconnectToStream` on the transport when the component
216
+ mounts. The transport uses the session's `lastEventId` to skip past already-seen stream events, so
217
+ the frontend only receives new data. Only enable `resume` when there are existing messages — for
218
+ brand new chats, there's nothing to reconnect to.
219
+ </Info>
220
+
221
+ <Note>
222
+ After resuming, `useChat`'s built-in `stop()` won't send the stop signal to the backend because
223
+ the AI SDK doesn't pass its abort signal through `reconnectToStream`. Use
224
+ `transport.stopGeneration(chatId)` for reliable stop behavior after resume — see
225
+ [Stop generation](#stop-generation) for the recommended pattern.
226
+ </Note>
227
+
228
+ <Warning>
229
+ In React strict mode (enabled by default in Next.js dev), you may see a `TypeError: Cannot read
230
+ properties of undefined (reading 'state')` in the console when using `resume`. This is a [known
231
+ bug in the AI SDK](https://github.com/vercel/ai/issues/8477) caused by React strict mode
232
+ double-firing the resume effect. The error is caught internally and **does not affect
233
+ functionality** — streaming and message display work correctly. It only appears in development and
234
+ will not occur in production builds.
235
+ </Warning>
236
+
237
+ ### Network resilience
238
+
239
+ You don't need to handle network drops, mobile background-kills, or Safari bfcache restores. The transport retries indefinitely with bounded backoff, reconnects on `online` / tab refocus / `pageshow` with `event.persisted`, and uses `Last-Event-ID` to resume without dropping chunks. See the [changelog entry](/ai-chat/changelog) for the gory details.
240
+
241
+ ## Client data and metadata
242
+
243
+ ### Transport-level client data
244
+
245
+ Set default client data on the transport that's included in every request. When the task uses `clientDataSchema`, this is type-checked to match:
246
+
247
+ ```ts
248
+ const transport = useTriggerChatTransport<typeof myChat>({
249
+ task: "my-chat",
250
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
251
+ startSession: ({ chatId, clientData }) =>
252
+ startChatSession({ chatId, clientData }),
253
+ clientData: { userId: currentUser.id },
254
+ });
255
+ ```
256
+
257
+ The transport threads `clientData` through three places automatically: into `startSession`'s `params.clientData` for the first run's `payload.metadata`, into per-turn `metadata` on every `.in/append` chunk, and live-updates if the option value changes between renders (so React-driven values like the current user work without reconstructing the transport).
258
+
259
+ ### Per-message metadata
260
+
261
+ Pass metadata with individual messages via `sendMessage`. Per-message values are merged with transport-level client data (per-message wins on conflicts):
262
+
263
+ ```ts
264
+ sendMessage({ text: "Hello" }, { metadata: { model: "gpt-4o", priority: "high" } });
265
+ ```
266
+
267
+ ### Typed client data with clientDataSchema
268
+
269
+ Instead of manually parsing `clientData` with Zod in every hook, pass a `clientDataSchema` to `chat.agent`. The schema validates the data once per turn, and `clientData` is typed in all hooks and `run`:
270
+
271
+ ```ts
272
+ import { chat } from "@trigger.dev/sdk/ai";
273
+ import { streamText, stepCountIs } from "ai";
274
+ import { anthropic } from "@ai-sdk/anthropic";
275
+ import { z } from "zod";
276
+
277
+ export const myChat = chat.agent({
278
+ id: "my-chat",
279
+ clientDataSchema: z.object({
280
+ model: z.string().optional(),
281
+ userId: z.string(),
282
+ }),
283
+ onChatStart: async ({ chatId, clientData }) => {
284
+ // clientData is typed as { model?: string; userId: string }
285
+ await db.chat.create({
286
+ data: { id: chatId, userId: clientData.userId },
287
+ });
288
+ },
289
+ run: async ({ messages, clientData, signal }) => {
290
+ // Same typed clientData — no manual parsing needed
291
+ return streamText({
292
+ model: openai(clientData?.model ?? "gpt-4o"),
293
+ messages,
294
+ abortSignal: signal,
295
+ stopWhen: stepCountIs(15),
296
+ });
297
+ },
298
+ });
299
+ ```
300
+
301
+ The schema also types the `clientData` option on the frontend transport:
302
+
303
+ ```ts
304
+ // TypeScript enforces that clientData matches the schema
305
+ const transport = useTriggerChatTransport<typeof myChat>({
306
+ task: "my-chat",
307
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
308
+ startSession: ({ chatId, clientData }) =>
309
+ startChatSession({ chatId, clientData }),
310
+ clientData: { userId: currentUser.id },
311
+ });
312
+ ```
313
+
314
+ Supports Zod, ArkType, Valibot, and other schema libraries supported by the SDK.
315
+
316
+ ## Stop generation
317
+
318
+ Use `transport.stopGeneration(chatId)` to stop the current generation. This sends a stop signal to the running task via input streams, aborting the current `streamText` call while keeping the run alive for the next message.
319
+
320
+ `stopGeneration` works in all scenarios — including after a page refresh when the stream was reconnected via `resume`. Call it alongside `useChat`'s `stop()` to also update the frontend state:
321
+
322
+ ```tsx
323
+ const { messages, sendMessage, stop: aiStop, status } = useChat({ transport });
324
+
325
+ // Wrap both calls in a single stop handler
326
+ const stop = useCallback(() => {
327
+ transport.stopGeneration(chatId);
328
+ aiStop();
329
+ }, [transport, chatId, aiStop]);
330
+
331
+ {
332
+ status === "streaming" && (
333
+ <button type="button" onClick={stop}>
334
+ Stop
335
+ </button>
336
+ );
337
+ }
338
+ ```
339
+
340
+ <Info>
341
+ `transport.stopGeneration(chatId)` handles the backend stop signal and closes
342
+ the SSE connection, while `aiStop()` (from `useChat`) updates the frontend
343
+ status to `"ready"` and fires the `onFinish` callback.
344
+ </Info>
345
+
346
+ <Tip>
347
+ A [PR to the AI SDK](https://github.com/vercel/ai/pull/14350) has been
348
+ submitted to pass `abortSignal` through `reconnectToStream`, which would make
349
+ `useChat`'s built-in `stop()` work after resume without needing
350
+ `stopGeneration`. Until that lands, use the pattern above for reliable stop
351
+ behavior after page refresh.
352
+ </Tip>
353
+
354
+ See [Stop generation](/ai-chat/backend#stop-generation) in the backend docs for how to handle stop signals in your task.
355
+
356
+ ## Tool approvals
357
+
358
+ The AI SDK supports tools that require human approval before execution. To use this with `chat.agent`, define a tool with `needsApproval: true` on the backend, then handle the approval UI and configure `sendAutomaticallyWhen` on the frontend.
359
+
360
+ ### Backend: define an approval-required tool
361
+
362
+ ```ts
363
+ import { tool } from "ai";
364
+ import { z } from "zod";
365
+
366
+ const sendEmail = tool({
367
+ description: "Send an email. Requires human approval before sending.",
368
+ inputSchema: z.object({
369
+ to: z.string(),
370
+ subject: z.string(),
371
+ body: z.string(),
372
+ }),
373
+ needsApproval: true,
374
+ execute: async ({ to, subject, body }) => {
375
+ await emailService.send({ to, subject, body });
376
+ return { sent: true, to, subject };
377
+ },
378
+ });
379
+ ```
380
+
381
+ Pass the tool to `streamText` in your `run` function as usual. When the model calls the tool, `chat.agent` streams a `tool-approval-request` chunk. The turn completes and the run waits for the next message.
382
+
383
+ ### Frontend: approval UI
384
+
385
+ Import `lastAssistantMessageIsCompleteWithApprovalResponses` from the AI SDK and pass it to `sendAutomaticallyWhen`. This tells `useChat` to automatically re-send messages once all approvals have been responded to.
386
+
387
+ Destructure `addToolApprovalResponse` from `useChat` and wire it to your approval buttons:
388
+
389
+ ```tsx
390
+ import { useChat } from "@ai-sdk/react";
391
+ import { lastAssistantMessageIsCompleteWithApprovalResponses } from "ai";
392
+
393
+ function Chat({ chatId, transport }) {
394
+ const { messages, sendMessage, addToolApprovalResponse, status } = useChat({
395
+ id: chatId,
396
+ transport,
397
+ sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
398
+ });
399
+
400
+ const handleApprove = (approvalId: string) => {
401
+ addToolApprovalResponse({ id: approvalId, approved: true });
402
+ };
403
+
404
+ const handleDeny = (approvalId: string) => {
405
+ addToolApprovalResponse({ id: approvalId, approved: false, reason: "User denied" });
406
+ };
407
+
408
+ return (
409
+ <div>
410
+ {messages.map((msg) =>
411
+ msg.parts.map((part, i) => {
412
+ if (part.state === "approval-requested") {
413
+ return (
414
+ <div key={i}>
415
+ <p>Tool "{part.type}" wants to run with input:</p>
416
+ <pre>{JSON.stringify(part.input, null, 2)}</pre>
417
+ <button onClick={() => handleApprove(part.approval.id)}>Approve</button>
418
+ <button onClick={() => handleDeny(part.approval.id)}>Deny</button>
419
+ </div>
420
+ );
421
+ }
422
+ // ... render other parts
423
+ })
424
+ )}
425
+ </div>
426
+ );
427
+ }
428
+ ```
429
+
430
+ ### How it works
431
+
432
+ 1. Model calls a tool with `needsApproval: true` — the turn completes with the tool in `approval-requested` state
433
+ 2. Frontend shows Approve/Deny buttons
434
+ 3. User clicks Approve — `addToolApprovalResponse` updates the tool part to `approval-responded`
435
+ 4. `sendAutomaticallyWhen` returns `true` — `useChat` re-sends the updated assistant message
436
+ 5. The transport sends the message via input streams — the backend matches it by ID and replaces the existing assistant message in the accumulator
437
+ 6. `streamText` sees the approved tool, executes it, and streams the result
438
+
439
+ <Info>
440
+ Message IDs are kept in sync between frontend and backend automatically. The backend always
441
+ includes a `generateMessageId` function when streaming responses, ensuring the `start` chunk
442
+ carries a `messageId` that the frontend uses. This makes the ID-based matching reliable
443
+ for tool approval updates.
444
+ </Info>
445
+
446
+ ## Sending actions
447
+
448
+ Send custom actions (undo, rollback, edit) to the agent via `transport.sendAction()`. Actions wake the agent and fire only `hydrateMessages` (if configured) and `onAction` — they're not turns, so `onTurnStart` / `prepareMessages` / `onBeforeTurnComplete` / `onTurnComplete` and `run()` do not fire.
449
+
450
+ For optimistic UI, mirror the action's effect on the `useChat` state via `setMessages` while the request is in flight:
451
+
452
+ ```tsx
453
+ function ChatControls({ chatId }: { chatId: string }) {
454
+ const transport = useTriggerChatTransport({
455
+ task: "my-chat",
456
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
457
+ startSession: ({ chatId, clientData }) =>
458
+ startChatSession({ chatId, clientData }),
459
+ });
460
+
461
+ const { setMessages } = useChat({ transport });
462
+
463
+ return (
464
+ <div>
465
+ <button
466
+ onClick={() => {
467
+ void transport.sendAction(chatId, { type: "undo" });
468
+ setMessages((prev) => prev.slice(0, -2));
469
+ }}
470
+ >
471
+ Undo last exchange
472
+ </button>
473
+ <button
474
+ onClick={() => transport.sendAction(chatId, { type: "rollback", targetMessageId: "msg-5" })}
475
+ >
476
+ Rollback to message
477
+ </button>
478
+ </div>
479
+ );
480
+ }
481
+ ```
482
+
483
+ The action payload is validated against the agent's `actionSchema` on the backend — invalid actions are rejected. See [Actions](/ai-chat/actions) for the backend setup.
484
+
485
+ <Note>
486
+ `sendAction` returns a `ReadableStream<UIMessageChunk>`. For side-effect-only actions (where `onAction` returns `void`), the stream completes immediately with `trigger:turn-complete`. For actions where `onAction` returns a `StreamTextResult`, the stream carries the assistant chunks the same way `sendMessages` does — `useChat` consumes them automatically.
487
+ </Note>
488
+
489
+ For server-to-server usage, `AgentChat` has the same method:
490
+
491
+ ```ts
492
+ const stream = await agentChat.sendAction({ type: "undo" });
493
+ for await (const chunk of stream) {
494
+ if (chunk.type === "text-delta") process.stdout.write(chunk.delta);
495
+ }
496
+ ```
497
+
498
+ ## Multi-tab coordination
499
+
500
+ When the same chat is open in multiple browser tabs, `multiTab: true` prevents duplicate messages and syncs conversation state across tabs. Only one tab can send at a time. Other tabs enter read-only mode with real-time message updates.
501
+
502
+ ```tsx
503
+ import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
504
+ import { useMultiTabChat } from "@trigger.dev/sdk/chat/react";
505
+ import { useChat } from "@ai-sdk/react";
506
+
507
+ function Chat({ chatId }: { chatId: string }) {
508
+ const transport = useTriggerChatTransport({
509
+ task: "my-chat",
510
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
511
+ startSession: ({ chatId, clientData }) =>
512
+ startChatSession({ chatId, clientData }),
513
+ multiTab: true,
514
+ });
515
+
516
+ const { messages, setMessages, sendMessage } = useChat({
517
+ id: chatId,
518
+ transport,
519
+ });
520
+
521
+ const { isReadOnly } = useMultiTabChat(transport, chatId, messages, setMessages);
522
+
523
+ return (
524
+ <div>
525
+ {isReadOnly && (
526
+ <div className="bg-amber-50 text-amber-700 p-2 text-sm">
527
+ This chat is active in another tab. Messages are read-only.
528
+ </div>
529
+ )}
530
+ {/* message list */}
531
+ <input
532
+ disabled={isReadOnly}
533
+ placeholder={isReadOnly ? "Active in another tab" : "Type a message..."}
534
+ />
535
+ </div>
536
+ );
537
+ }
538
+ ```
539
+
540
+ ### How it works
541
+
542
+ 1. When a tab sends a message, the transport "claims" the chatId via `BroadcastChannel`
543
+ 2. Other tabs detect the claim and enter read-only mode (`isReadOnly: true`)
544
+ 3. The active tab broadcasts its messages so read-only tabs see updates in real-time
545
+ 4. When the turn completes, the claim is released. Any tab can send next.
546
+ 5. Heartbeats detect crashed tabs (10s timeout clears stale claims)
547
+
548
+ ### What `useMultiTabChat` does
549
+
550
+ - Returns `{ isReadOnly }` for disabling the input UI
551
+ - Broadcasts `messages` from the active tab to other tabs
552
+ - Calls `setMessages` on read-only tabs when messages arrive from the active tab
553
+ - Tracks read-only state via the transport's `BroadcastChannel` coordinator
554
+
555
+ <Note>
556
+ Multi-tab coordination is same-browser only (`BroadcastChannel` is a browser API). It gracefully degrades to a no-op in Node.js, SSR, or browsers without `BroadcastChannel` support. Cross-device coordination requires server-side involvement.
557
+ </Note>
558
+
559
+ ## Self-hosting
560
+
561
+ If you're self-hosting Trigger.dev, pass the `baseURL` option:
562
+
563
+ ```ts
564
+ const transport = useTriggerChatTransport({
565
+ task: "my-chat",
566
+ accessToken: ({ chatId }) => mintChatAccessToken(chatId),
567
+ startSession: ({ chatId, clientData }) =>
568
+ startChatSession({ chatId, clientData }),
569
+ baseURL: "https://your-trigger-instance.com",
570
+ });
571
+ ```
572
+
573
+ `baseURL` also accepts a function so you can route per endpoint — useful when fronting `.in/append` with an edge proxy (e.g. to inject server-trusted signal into the wire) while keeping `.out` SSE direct:
574
+
575
+ ```ts
576
+ baseURL: ({ endpoint }) =>
577
+ endpoint === "out" ? "https://api.trigger.dev" : "https://chat-proxy.example.com",
578
+ ```
579
+
580
+ For per-request control beyond URL routing (header injection, custom retries, tracing), pass a `fetch` override. See [Trusted edge signals](/ai-chat/patterns/trusted-edge-signals) for a full proxy walkthrough.