@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,221 @@
1
+ ---
2
+ title: "Background injection"
3
+ sidebarTitle: "Background injection"
4
+ description: "Inject context from background work into the agent's conversation — self-review, RAG augmentation, or any async analysis."
5
+ ---
6
+
7
+ import RcBanner from "/snippets/ai-chat-rc-banner.mdx";
8
+
9
+ <RcBanner />
10
+
11
+ ## Overview
12
+
13
+ `chat.inject()` queues model messages for injection into the conversation. Messages are picked up at the start of the next turn or at the next `prepareStep` boundary (between tool-call steps).
14
+
15
+ This is the backend counterpart to [pending messages](/ai-chat/pending-messages) — pending messages come from the user via the frontend, while `chat.inject()` comes from your task code.
16
+
17
+ ## Basic usage
18
+
19
+ ```ts
20
+ import { chat } from "@trigger.dev/sdk/ai";
21
+
22
+ // Queue a system message for injection
23
+ chat.inject([
24
+ {
25
+ role: "system",
26
+ content: "The user's account was just upgraded to Pro.",
27
+ },
28
+ ]);
29
+ ```
30
+
31
+ Messages are appended to the model messages before the next LLM inference call. The LLM sees them as part of the conversation context.
32
+
33
+ ## Common pattern: defer + inject
34
+
35
+ The most powerful pattern combines `chat.defer()` (background work) with `chat.inject()` (inject results). Background work runs in parallel with the idle wait between turns, and results are injected before the next response.
36
+
37
+ ```ts
38
+ export const myChat = chat.agent({
39
+ id: "my-chat",
40
+ onTurnComplete: async ({ messages }) => {
41
+ // Kick off background analysis — doesn't block the turn
42
+ chat.defer(
43
+ (async () => {
44
+ const analysis = await analyzeConversation(messages);
45
+ chat.inject([
46
+ {
47
+ role: "system",
48
+ content: `[Analysis of conversation so far]\n\n${analysis}`,
49
+ },
50
+ ]);
51
+ })()
52
+ );
53
+ },
54
+ run: async ({ messages, signal }) => {
55
+ return streamText({
56
+ ...chat.toStreamTextOptions({ registry }),
57
+ messages,
58
+ abortSignal: signal,
59
+ stopWhen: stepCountIs(15),
60
+ });
61
+ },
62
+ });
63
+ ```
64
+
65
+ ### Timing
66
+
67
+ 1. Turn completes, `onTurnComplete` fires
68
+ 2. `chat.defer()` registers the background work
69
+ 3. The run immediately starts waiting for the next message (no blocking)
70
+ 4. Background work completes, `chat.inject()` queues the messages
71
+ 5. User sends next message, turn starts
72
+ 6. Injected messages are appended before `run()` executes
73
+ 7. The LLM sees the injected context alongside the new user message
74
+
75
+ If the background work finishes *during* a tool-call loop (not between turns), the messages are picked up at the next `prepareStep` boundary instead.
76
+
77
+ ## Example: self-review
78
+
79
+ A cheap model reviews the agent's response after each turn and injects coaching for the next one. Uses [Prompts](/ai/prompts) for the review prompt and `generateObject` for structured output.
80
+
81
+ ```ts
82
+ import { chat } from "@trigger.dev/sdk/ai";
83
+ import { prompts } from "@trigger.dev/sdk";
84
+ import { streamText, generateObject, createProviderRegistry, stepCountIs } from "ai";
85
+ import { anthropic } from "@ai-sdk/anthropic";
86
+ import { z } from "zod";
87
+
88
+ const registry = createProviderRegistry({ anthropic });
89
+
90
+ const selfReviewPrompt = prompts.define({
91
+ id: "self-review",
92
+ model: "anthropic:claude-haiku-4-5",
93
+ content: `You are a conversation quality reviewer. Analyze the assistant's most recent response.
94
+
95
+ Focus on:
96
+ - Whether the response answered the user's question
97
+ - Missed opportunities to use tools or provide more detail
98
+ - Tone mismatches
99
+
100
+ Be concise. Only flag issues worth fixing.`,
101
+ });
102
+
103
+ export const myChat = chat.agent({
104
+ id: "my-chat",
105
+ onTurnComplete: async ({ messages }) => {
106
+ chat.defer(
107
+ (async () => {
108
+ const resolved = await selfReviewPrompt.resolve({});
109
+
110
+ const review = await generateObject({
111
+ model: registry.languageModel(resolved.model ?? "anthropic:claude-haiku-4-5"),
112
+ ...resolved.toAISDKTelemetry(),
113
+ system: resolved.text,
114
+ prompt: messages
115
+ .filter((m) => m.role === "user" || m.role === "assistant")
116
+ .map((m) => {
117
+ const text =
118
+ typeof m.content === "string"
119
+ ? m.content
120
+ : Array.isArray(m.content)
121
+ ? m.content
122
+ .filter((p: any) => p.type === "text")
123
+ .map((p: any) => p.text)
124
+ .join("")
125
+ : "";
126
+ return `${m.role}: ${text}`;
127
+ })
128
+ .join("\n\n"),
129
+ schema: z.object({
130
+ needsImprovement: z.boolean(),
131
+ suggestions: z.array(z.string()),
132
+ }),
133
+ });
134
+
135
+ if (review.object.needsImprovement) {
136
+ chat.inject([
137
+ {
138
+ role: "system",
139
+ content: `[Self-review]\n\n${review.object.suggestions.map((s) => `- ${s}`).join("\n")}\n\nApply these naturally.`,
140
+ },
141
+ ]);
142
+ }
143
+ })()
144
+ );
145
+ },
146
+ run: async ({ messages, signal }) => {
147
+ return streamText({
148
+ ...chat.toStreamTextOptions({ registry }),
149
+ messages,
150
+ abortSignal: signal,
151
+ stopWhen: stepCountIs(15),
152
+ });
153
+ },
154
+ });
155
+ ```
156
+
157
+ The self-review runs on `claude-haiku-4-5` (fast, cheap) in the background. If the user sends another message before it completes, the coaching is still injected — `chat.inject()` persists across the idle wait.
158
+
159
+ ## Other use cases
160
+
161
+ - **RAG augmentation**: After each turn, fetch relevant documents and inject them as context for the next response
162
+ - **Safety checks**: Run a moderation model on the response, inject warnings if issues are detected
163
+ - **Fact-checking**: Verify claims in the response using search tools, inject corrections
164
+ - **Context enrichment**: Look up user/account data based on what was discussed, inject it as system context
165
+
166
+ ## `chat.defer` standalone
167
+
168
+ `chat.defer()` is also useful on its own, without `chat.inject()`. Any work whose timing has no resume implication — analytics, audit logs, search-index writes, cache warming — can run in parallel with streaming instead of in the critical path. All deferred promises are awaited (with a 5s timeout) before `onTurnComplete` fires.
169
+
170
+ ```ts
171
+ export const myChat = chat.agent({
172
+ id: "my-chat",
173
+ onTurnStart: async ({ chatId, runId }) => {
174
+ // Analytics — fire-and-forget, irrelevant to resume.
175
+ chat.defer(analytics.track("turn_started", { chatId, runId }));
176
+ },
177
+ run: async ({ messages, signal }) => {
178
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
179
+ },
180
+ });
181
+ ```
182
+
183
+ `chat.defer()` can be called from anywhere during a turn — hooks, `run()`, or nested helpers. All deferred promises are collected and awaited together before `onTurnComplete`.
184
+
185
+ <Warning>
186
+ **Don't use `chat.defer()` for the message-history write in `onTurnStart`.** That write must land *before* the model starts streaming, otherwise a mid-stream page refresh will read `[]` from your DB and lose the user's message from the rendered conversation. See [Database persistence — `onTurnStart`](/ai-chat/patterns/database-persistence#onturnstart). Reserve `chat.defer` for writes whose timing has no resume implication.
187
+ </Warning>
188
+
189
+ ## How it differs from pending messages
190
+
191
+ | | `chat.inject()` | [Pending messages](/ai-chat/pending-messages) |
192
+ |---|---|---|
193
+ | **Source** | Backend task code | Frontend user input |
194
+ | **Triggered by** | Your code (e.g. `onTurnComplete` + `chat.defer()`) | User sending a message during streaming |
195
+ | **Injection point** | Start of next turn, or next `prepareStep` boundary | Next `prepareStep` boundary only |
196
+ | **Message role** | Any (`system`, `user`, `assistant`) | Typically `user` |
197
+ | **Frontend visibility** | Not visible unless you write custom `data-*` chunks | Visible via `usePendingMessages` hook |
198
+
199
+ ## API reference
200
+
201
+ ### chat.inject()
202
+
203
+ ```ts
204
+ chat.inject(messages: ModelMessage[]): void
205
+ ```
206
+
207
+ Queue model messages for injection at the next opportunity. Messages persist across the idle wait between turns — they are not reset when a new turn starts.
208
+
209
+ **Parameters:**
210
+
211
+ | Parameter | Type | Description |
212
+ |-----------|------|-------------|
213
+ | `messages` | `ModelMessage[]` | Model messages to inject (from the `ai` package) |
214
+
215
+ Messages are drained (consumed) when:
216
+ 1. A new turn starts — before `run()` executes
217
+ 2. A `prepareStep` boundary is reached — between tool-call steps during streaming
218
+
219
+ <Note>
220
+ `chat.inject()` writes to an in-memory queue in the current process. It works from any code running in the same task — lifecycle hooks, deferred work, tool execute functions, etc. It does not work from subtasks or other runs.
221
+ </Note>