@trigger.dev/sdk 4.5.16 → 4.6.1

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 (159) hide show
  1. package/dist/commonjs/imports/ai-runtime-cjs.cjs.map +1 -1
  2. package/dist/commonjs/imports/ai-runtime.js +2 -0
  3. package/dist/commonjs/v3/ai-shared.d.ts +15 -0
  4. package/dist/commonjs/v3/ai-shared.js +35 -1
  5. package/dist/commonjs/v3/ai-shared.js.map +1 -1
  6. package/dist/commonjs/v3/ai.d.ts +311 -25
  7. package/dist/commonjs/v3/ai.js +1633 -380
  8. package/dist/commonjs/v3/ai.js.map +1 -1
  9. package/dist/commonjs/v3/auth.d.ts +10 -4
  10. package/dist/commonjs/v3/auth.js.map +1 -1
  11. package/dist/commonjs/v3/chat-client.d.ts +11 -2
  12. package/dist/commonjs/v3/chat-client.js +227 -111
  13. package/dist/commonjs/v3/chat-client.js.map +1 -1
  14. package/dist/commonjs/v3/chat-react.d.ts +88 -0
  15. package/dist/commonjs/v3/chat-react.js +130 -1
  16. package/dist/commonjs/v3/chat-react.js.map +1 -1
  17. package/dist/commonjs/v3/chat-server.d.ts +42 -6
  18. package/dist/commonjs/v3/chat-server.js +52 -7
  19. package/dist/commonjs/v3/chat-server.js.map +1 -1
  20. package/dist/commonjs/v3/chat.d.ts +92 -10
  21. package/dist/commonjs/v3/chat.js +328 -73
  22. package/dist/commonjs/v3/chat.js.map +1 -1
  23. package/dist/commonjs/v3/chatSnapshotIo.d.ts +2 -0
  24. package/dist/commonjs/v3/chatSnapshotIo.js +173 -0
  25. package/dist/commonjs/v3/chatSnapshotIo.js.map +1 -0
  26. package/dist/commonjs/v3/chatVersionSkew.d.ts +12 -0
  27. package/dist/commonjs/v3/chatVersionSkew.js +30 -0
  28. package/dist/commonjs/v3/chatVersionSkew.js.map +1 -0
  29. package/dist/commonjs/v3/externalDeploymentId.d.ts +23 -0
  30. package/dist/commonjs/v3/externalDeploymentId.js +43 -0
  31. package/dist/commonjs/v3/externalDeploymentId.js.map +1 -0
  32. package/dist/commonjs/v3/retry.d.ts +4 -0
  33. package/dist/commonjs/v3/retry.js +22 -13
  34. package/dist/commonjs/v3/retry.js.map +1 -1
  35. package/dist/commonjs/v3/sessions.d.ts +15 -2
  36. package/dist/commonjs/v3/sessions.js +12 -1
  37. package/dist/commonjs/v3/sessions.js.map +1 -1
  38. package/dist/commonjs/v3/shared.js +30 -36
  39. package/dist/commonjs/v3/shared.js.map +1 -1
  40. package/dist/commonjs/v3/test/index.d.ts +1 -0
  41. package/dist/commonjs/v3/test/index.js +3 -1
  42. package/dist/commonjs/v3/test/index.js.map +1 -1
  43. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +47 -4
  44. package/dist/commonjs/v3/test/mock-chat-agent.js +116 -10
  45. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  46. package/dist/commonjs/v3/test/test-session-handle.js +6 -0
  47. package/dist/commonjs/v3/test/test-session-handle.js.map +1 -1
  48. package/dist/commonjs/v3/test/transcript-storage-tests.d.ts +40 -0
  49. package/dist/commonjs/v3/test/transcript-storage-tests.js +258 -0
  50. package/dist/commonjs/v3/test/transcript-storage-tests.js.map +1 -0
  51. package/dist/commonjs/v3/transcriptStorage.d.ts +261 -0
  52. package/dist/commonjs/v3/transcriptStorage.js +374 -0
  53. package/dist/commonjs/v3/transcriptStorage.js.map +1 -0
  54. package/dist/commonjs/version.js +1 -1
  55. package/dist/esm/imports/ai-runtime.d.ts +2 -2
  56. package/dist/esm/imports/ai-runtime.js +2 -2
  57. package/dist/esm/imports/ai-runtime.js.map +1 -1
  58. package/dist/esm/v3/ai-shared.d.ts +15 -0
  59. package/dist/esm/v3/ai-shared.js +33 -0
  60. package/dist/esm/v3/ai-shared.js.map +1 -1
  61. package/dist/esm/v3/ai.d.ts +311 -25
  62. package/dist/esm/v3/ai.js +1625 -377
  63. package/dist/esm/v3/ai.js.map +1 -1
  64. package/dist/esm/v3/auth.d.ts +10 -4
  65. package/dist/esm/v3/auth.js.map +1 -1
  66. package/dist/esm/v3/chat-client.d.ts +11 -2
  67. package/dist/esm/v3/chat-client.js +228 -112
  68. package/dist/esm/v3/chat-client.js.map +1 -1
  69. package/dist/esm/v3/chat-react.d.ts +88 -0
  70. package/dist/esm/v3/chat-react.js +127 -1
  71. package/dist/esm/v3/chat-react.js.map +1 -1
  72. package/dist/esm/v3/chat-server.d.ts +42 -6
  73. package/dist/esm/v3/chat-server.js +53 -8
  74. package/dist/esm/v3/chat-server.js.map +1 -1
  75. package/dist/esm/v3/chat.d.ts +92 -10
  76. package/dist/esm/v3/chat.js +329 -74
  77. package/dist/esm/v3/chat.js.map +1 -1
  78. package/dist/esm/v3/chatSnapshotIo.d.ts +2 -0
  79. package/dist/esm/v3/chatSnapshotIo.js +165 -0
  80. package/dist/esm/v3/chatSnapshotIo.js.map +1 -0
  81. package/dist/esm/v3/chatVersionSkew.d.ts +12 -0
  82. package/dist/esm/v3/chatVersionSkew.js +27 -0
  83. package/dist/esm/v3/chatVersionSkew.js.map +1 -0
  84. package/dist/esm/v3/externalDeploymentId.d.ts +23 -0
  85. package/dist/esm/v3/externalDeploymentId.js +38 -0
  86. package/dist/esm/v3/externalDeploymentId.js.map +1 -0
  87. package/dist/esm/v3/retry.d.ts +4 -0
  88. package/dist/esm/v3/retry.js +14 -8
  89. package/dist/esm/v3/retry.js.map +1 -1
  90. package/dist/esm/v3/sessions.d.ts +15 -2
  91. package/dist/esm/v3/sessions.js +11 -1
  92. package/dist/esm/v3/sessions.js.map +1 -1
  93. package/dist/esm/v3/shared.js +17 -23
  94. package/dist/esm/v3/shared.js.map +1 -1
  95. package/dist/esm/v3/test/index.d.ts +1 -0
  96. package/dist/esm/v3/test/index.js +1 -0
  97. package/dist/esm/v3/test/index.js.map +1 -1
  98. package/dist/esm/v3/test/mock-chat-agent.d.ts +47 -4
  99. package/dist/esm/v3/test/mock-chat-agent.js +118 -12
  100. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  101. package/dist/esm/v3/test/test-session-handle.js +6 -0
  102. package/dist/esm/v3/test/test-session-handle.js.map +1 -1
  103. package/dist/esm/v3/test/transcript-storage-tests.d.ts +40 -0
  104. package/dist/esm/v3/test/transcript-storage-tests.js +255 -0
  105. package/dist/esm/v3/test/transcript-storage-tests.js.map +1 -0
  106. package/dist/esm/v3/transcriptStorage.d.ts +261 -0
  107. package/dist/esm/v3/transcriptStorage.js +360 -0
  108. package/dist/esm/v3/transcriptStorage.js.map +1 -0
  109. package/dist/esm/version.js +1 -1
  110. package/docs/ai-chat/actions.mdx +55 -23
  111. package/docs/ai-chat/anatomy.mdx +47 -27
  112. package/docs/ai-chat/backend.mdx +125 -48
  113. package/docs/ai-chat/background-injection.mdx +122 -19
  114. package/docs/ai-chat/client-protocol.mdx +5 -4
  115. package/docs/ai-chat/compaction.mdx +13 -7
  116. package/docs/ai-chat/custom-agents.mdx +23 -0
  117. package/docs/ai-chat/fast-starts.mdx +27 -20
  118. package/docs/ai-chat/frontend.mdx +19 -14
  119. package/docs/ai-chat/how-it-works.mdx +1 -1
  120. package/docs/ai-chat/lifecycle-hooks.mdx +6 -2
  121. package/docs/ai-chat/migrating-from-a-route-handler.mdx +16 -14
  122. package/docs/ai-chat/migrating-from-hydrate-messages.mdx +299 -0
  123. package/docs/ai-chat/patterns/database-persistence.mdx +24 -9
  124. package/docs/ai-chat/patterns/native-compaction.mdx +310 -0
  125. package/docs/ai-chat/patterns/persistence-and-replay.mdx +34 -23
  126. package/docs/ai-chat/patterns/skills.mdx +7 -10
  127. package/docs/ai-chat/patterns/version-upgrades.mdx +79 -6
  128. package/docs/ai-chat/pending-messages.mdx +3 -3
  129. package/docs/ai-chat/prompt-caching.mdx +23 -25
  130. package/docs/ai-chat/quick-start.mdx +11 -11
  131. package/docs/ai-chat/reference.mdx +69 -10
  132. package/docs/ai-chat/sessions.mdx +6 -1
  133. package/docs/ai-chat/side-channels.mdx +1 -1
  134. package/docs/ai-chat/testing.mdx +2 -1
  135. package/docs/ai-chat/tools.mdx +18 -13
  136. package/docs/ai-chat/transcript-storage.mdx +247 -0
  137. package/docs/ai-chat/upgrade-guide.mdx +2 -2
  138. package/docs/apikeys.mdx +27 -45
  139. package/docs/cli-env-commands.mdx +114 -0
  140. package/docs/cli-projects-commands.mdx +62 -0
  141. package/docs/cli-runs-commands.mdx +94 -0
  142. package/docs/config/extensions/syncEnvVars.mdx +6 -0
  143. package/docs/deploy-environment-variables.mdx +23 -2
  144. package/docs/deployment/overview.mdx +4 -8
  145. package/docs/deployment/preview-branches.mdx +4 -4
  146. package/docs/deployment/version-skew-protection.mdx +62 -0
  147. package/docs/introduction.mdx +58 -152
  148. package/docs/manual-setup.mdx +7 -7
  149. package/docs/mcp-tools.mdx +9 -0
  150. package/docs/quick-start.mdx +3 -3
  151. package/docs/realtime/auth.mdx +1 -1
  152. package/docs/reports.mdx +157 -0
  153. package/docs/self-hosting/security.mdx +17 -0
  154. package/docs/tasks/scheduled.mdx +24 -0
  155. package/docs/triggering.mdx +1 -1
  156. package/docs/troubleshooting-zod.mdx +158 -0
  157. package/package.json +4 -4
  158. package/skills/trigger-authoring-chat-agent/SKILL.md +41 -29
  159. package/skills/trigger-chat-agent-advanced/SKILL.md +54 -19
@@ -143,6 +143,12 @@ await waitUntilComplete();
143
143
  5s timeout, before `onTurnComplete`). `chat.inject(messages)` queues `ModelMessage[]` that drain at
144
144
  the next turn start or `prepareStep` boundary.
145
145
 
146
+ Two lanes, decided by role. A `role: "system"` message goes to the model's instructions, where it is
147
+ trusted like the system prompt, and applies to the next turn only. Any other role joins the
148
+ conversation and is untrusted by construction, so put checkable facts there and directives in the
149
+ system lane. The instructions lane reaches the model only through the managed `streamText` (or a
150
+ `chat.toStreamTextOptions()` spread), since that is where the SDK can set instructions.
151
+
146
152
  ```ts
147
153
  export const myChat = chat.agent({
148
154
  id: "my-chat",
@@ -154,8 +160,9 @@ export const myChat = chat.agent({
154
160
  })()
155
161
  );
156
162
  },
157
- run: async ({ messages, signal }) =>
158
- streamText({ ...chat.toStreamTextOptions({ registry }), messages, abortSignal: signal, stopWhen: stepCountIs(15) }),
163
+ registry,
164
+ run: async ({ messages, signal, streamText }) =>
165
+ streamText({ messages, abortSignal: signal, stopWhen: stepCountIs(15) }),
159
166
  });
160
167
  ```
161
168
 
@@ -163,8 +170,9 @@ export const myChat = chat.agent({
163
170
 
164
171
  `compaction.shouldCompact` decides when, `summarize` produces the summary that replaces the model
165
172
  messages. UI messages are preserved by default (customize via `compactUIMessages`). The `prepareStep`
166
- that performs inner-loop compaction is auto-injected by `chat.toStreamTextOptions()`; a `prepareStep`
167
- you pass after the spread wins.
173
+ that performs inner-loop compaction rides the managed `streamText`, which composes a `prepareStep`
174
+ you pass after it. Spreading `chat.toStreamTextOptions()` and then passing your own replaces it,
175
+ switching compaction off.
168
176
 
169
177
  ```ts
170
178
  compaction: {
@@ -177,29 +185,53 @@ compaction: {
177
185
  },
178
186
  ```
179
187
 
180
- ### 5. Actions: mutate state without a turn
181
-
182
- `actionSchema` validates; `onAction` mutates via `chat.history` (`slice`, `replace`, `rollbackTo`,
183
- `remove`, `getPendingToolCalls`, `extractNewToolResults`). Actions fire `hydrateMessages` and
184
- `onAction` only, never `run()` or the turn hooks. Return a `StreamTextResult`, string, or `UIMessage`
185
- to also emit a model response.
188
+ ### 5. Actions: edit state, and optionally answer
189
+
190
+ `actionSchema` validates; `onAction` edits via `chat.history` (`slice`, `replace`, `rollbackTo`,
191
+ `remove`, `getPendingToolCalls`, `extractNewToolResults`). An action fires `hydrateMessages` and
192
+ `onAction` only. Return nothing for an edit-only action: no model call, and the turn counter does not
193
+ advance. Return `chat.turn()` to answer after the edit: a turn runs on the edited history with
194
+ everything a turn has (the agent's system prompt and tools, steering, compaction, injected
195
+ instructions, `onTurnStart` and `onTurnComplete`, persistence), and `run()` receives it with
196
+ `trigger: "action-turn"`. `onAction` has no `streamText` argument, and returning a
197
+ `StreamTextResult`, string or `UIMessage` from it throws.
198
+
199
+ Persistence splits by model. With transcript storage (`storage` on `chat.agent`; the platform
200
+ snapshot by default), the runtime hands storage a changeset with `reason: "action"` after an action
201
+ that changed the conversation: an undo is one `truncateAfter`, a regenerate is a `truncateAfter`
202
+ followed by the new answer's `put` when the turn completes, and an edit is a `put` for the edited
203
+ id. With the deprecated `hydrateMessages` your store is the source of truth and the runtime does
204
+ not write, so mirror every mutation yourself: a regenerate is a delete and an insert, and the
205
+ answer that follows `chat.turn()` arrives through `onTurnComplete` like any turn's answer.
186
206
 
187
207
  ```ts
188
208
  export const myChat = chat.agent({
189
209
  id: "my-chat",
190
210
  actionSchema: z.discriminatedUnion("type", [
191
211
  z.object({ type: z.literal("undo") }),
212
+ z.object({ type: z.literal("regenerate") }),
192
213
  z.object({ type: z.literal("rollback"), targetMessageId: z.string() }),
193
214
  ]),
194
215
  onAction: async ({ action }) => {
195
- if (action.type === "undo") chat.history.slice(0, -2);
216
+ if (action.type === "undo") chat.history.slice(0, -2); // edit only
196
217
  if (action.type === "rollback") chat.history.rollbackTo(action.targetMessageId);
218
+ if (action.type === "regenerate") {
219
+ chat.history.slice(0, -1);
220
+ return chat.turn(); // answer the edited history
221
+ }
197
222
  },
198
- run: async ({ messages, signal }) => streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal }),
223
+ run: async ({ messages, signal, streamText }) =>
224
+ streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal }),
199
225
  });
200
226
  ```
201
227
 
202
- Send from the browser with `transport.sendAction(chatId, { type: "undo" })`, or server-side with
228
+ Send from the browser through `useChat`, so the answer a turn produces renders like any turn:
229
+ `sendMessage(undefined, { body: { action: { type: "regenerate" } } })`, `regenerate({ body: { action } })`,
230
+ or `useChatActions({ sendMessage })` from `@trigger.dev/sdk/chat/react`. For regeneration, use
231
+ `regenerate()` from `useChat` (Vercel AI SDK) which removes the last assistant message before
232
+ streaming the new one; calling `sendMessage` with a regenerate action appends without removal,
233
+ leaving both answers visible. `transport.sendAction(chatId, action)` returns a raw stream the
234
+ caller must read and apply itself. Server-side, use
203
235
  `agentChat.sendAction({ type: "rollback", targetMessageId: "msg-3" })`.
204
236
 
205
237
  ### 6. Fast starts: Head Start
@@ -210,18 +242,19 @@ must be **schema-only** (a module importing `ai` + `zod` only); heavy executes s
210
242
 
211
243
  ```ts
212
244
  import { chat } from "@trigger.dev/sdk/chat-server";
213
- import { streamText, stepCountIs } from "ai";
214
245
  import { anthropic } from "@ai-sdk/anthropic";
215
246
  import { headStartTools } from "@/lib/chat-tools/schemas";
216
247
 
217
248
  export const chatHandler = chat.headStart({
218
249
  agentId: "my-chat",
219
- run: async ({ chat: helper }) =>
250
+ // `streamText` from the run argument owns `messages`, `prompt`, `stopWhen`
251
+ // and `abortSignal`: the handover needs `stopWhen: stepCountIs(1)` so the agent,
252
+ // not this handler, runs step 2 onward. Passing any of them is a type error.
253
+ run: async ({ streamText }) =>
220
254
  streamText({
221
- ...helper.toStreamTextOptions({ tools: headStartTools }),
222
255
  model: anthropic("claude-sonnet-4-6"),
223
256
  system: "You are helpful.",
224
- stopWhen: stepCountIs(15),
257
+ tools: headStartTools,
225
258
  }),
226
259
  });
227
260
  // Next.js: export const POST = chatHandler; Transport: headStart: "/api/chat"
@@ -248,8 +281,10 @@ export const myChat = chat.agent({
248
281
  ### 8. Pending messages (mid-stream user input)
249
282
 
250
283
  A message sent while a turn is streaming should NOT cancel the stream. Configure
251
- `pendingMessages` (`shouldInject`, `prepare`, `onReceived`, `onInjected`) on the agent so the SDK's
252
- auto-injected `prepareStep` folds them in at the next boundary. On the frontend, `usePendingMessages`
284
+ `pendingMessages` (`shouldInject`, `prepare`, `onReceived`, `onInjected`) on the agent so the managed
285
+ `streamText`'s `prepareStep` folds them in at the next boundary. An injected steering message is part
286
+ of the conversation your hooks see, so it arrives in `uiMessages` and `newUIMessages` at
287
+ `onTurnComplete` and an app persisting from there stores it without extra work. On the frontend, `usePendingMessages`
253
288
  returns `pending`, `steer(text)`, `queue(text)`, and `promoteToSteering(id)`; send via
254
289
  `transport.sendPendingMessage(chatId, uiMessage, metadata?)`.
255
290