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

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 (247) hide show
  1. package/dist/commonjs/v3/ai-shared.js +1 -4
  2. package/dist/commonjs/v3/ai-shared.js.map +1 -1
  3. package/dist/commonjs/v3/ai.d.ts +178 -6
  4. package/dist/commonjs/v3/ai.js +417 -64
  5. package/dist/commonjs/v3/ai.js.map +1 -1
  6. package/dist/commonjs/v3/chat-client.js +18 -14
  7. package/dist/commonjs/v3/chat-client.js.map +1 -1
  8. package/dist/commonjs/v3/chat-react.js +1 -3
  9. package/dist/commonjs/v3/chat-react.js.map +1 -1
  10. package/dist/commonjs/v3/chat-server.d.ts +76 -0
  11. package/dist/commonjs/v3/chat-server.js +194 -24
  12. package/dist/commonjs/v3/chat-server.js.map +1 -1
  13. package/dist/commonjs/v3/chat-server.test.js +201 -6
  14. package/dist/commonjs/v3/chat-server.test.js.map +1 -1
  15. package/dist/commonjs/v3/chat.js +4 -3
  16. package/dist/commonjs/v3/chat.js.map +1 -1
  17. package/dist/commonjs/v3/chat.test.js +6 -18
  18. package/dist/commonjs/v3/chat.test.js.map +1 -1
  19. package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
  20. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
  21. package/dist/commonjs/v3/idempotencyKeys.js.map +1 -1
  22. package/dist/commonjs/v3/prompt.js +2 -6
  23. package/dist/commonjs/v3/prompt.js.map +1 -1
  24. package/dist/commonjs/v3/promptManagement.js.map +1 -1
  25. package/dist/commonjs/v3/retry.js.map +1 -1
  26. package/dist/commonjs/v3/runs.d.ts +1 -1
  27. package/dist/commonjs/v3/sessions.d.ts +3 -2
  28. package/dist/commonjs/v3/sessions.js +4 -2
  29. package/dist/commonjs/v3/sessions.js.map +1 -1
  30. package/dist/commonjs/v3/shared.js.map +1 -1
  31. package/dist/commonjs/v3/skill.js.map +1 -1
  32. package/dist/commonjs/v3/streams.js +2 -3
  33. package/dist/commonjs/v3/streams.js.map +1 -1
  34. package/dist/commonjs/v3/streams.test.js.map +1 -1
  35. package/dist/commonjs/v3/test/mock-chat-agent.js +1 -4
  36. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
  37. package/dist/commonjs/v3/test/test-session-handle.js.map +1 -1
  38. package/dist/commonjs/v3/triggerClient.test.js +1 -1
  39. package/dist/commonjs/v3/triggerClient.test.js.map +1 -1
  40. package/dist/commonjs/v3/triggerClient.types.test.js +11 -11
  41. package/dist/commonjs/v3/triggerClient.types.test.js.map +1 -1
  42. package/dist/commonjs/version.js +1 -1
  43. package/dist/esm/v3/ai-shared.js +1 -4
  44. package/dist/esm/v3/ai-shared.js.map +1 -1
  45. package/dist/esm/v3/ai.d.ts +178 -6
  46. package/dist/esm/v3/ai.js +417 -64
  47. package/dist/esm/v3/ai.js.map +1 -1
  48. package/dist/esm/v3/chat-client.js +18 -14
  49. package/dist/esm/v3/chat-client.js.map +1 -1
  50. package/dist/esm/v3/chat-react.js +1 -3
  51. package/dist/esm/v3/chat-react.js.map +1 -1
  52. package/dist/esm/v3/chat-server.d.ts +76 -0
  53. package/dist/esm/v3/chat-server.js +194 -24
  54. package/dist/esm/v3/chat-server.js.map +1 -1
  55. package/dist/esm/v3/chat-server.test.js +201 -6
  56. package/dist/esm/v3/chat-server.test.js.map +1 -1
  57. package/dist/esm/v3/chat.js +4 -3
  58. package/dist/esm/v3/chat.js.map +1 -1
  59. package/dist/esm/v3/chat.test.js +6 -18
  60. package/dist/esm/v3/chat.test.js.map +1 -1
  61. package/dist/esm/v3/createStartSessionAction.test.js +30 -0
  62. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
  63. package/dist/esm/v3/idempotencyKeys.js +1 -1
  64. package/dist/esm/v3/idempotencyKeys.js.map +1 -1
  65. package/dist/esm/v3/prompt.js +2 -6
  66. package/dist/esm/v3/prompt.js.map +1 -1
  67. package/dist/esm/v3/promptManagement.js.map +1 -1
  68. package/dist/esm/v3/retry.js.map +1 -1
  69. package/dist/esm/v3/sessions.d.ts +3 -2
  70. package/dist/esm/v3/sessions.js +4 -2
  71. package/dist/esm/v3/sessions.js.map +1 -1
  72. package/dist/esm/v3/shared.js.map +1 -1
  73. package/dist/esm/v3/skill.js.map +1 -1
  74. package/dist/esm/v3/streams.js +2 -3
  75. package/dist/esm/v3/streams.js.map +1 -1
  76. package/dist/esm/v3/streams.test.js.map +1 -1
  77. package/dist/esm/v3/test/mock-chat-agent.js +4 -7
  78. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
  79. package/dist/esm/v3/test/test-session-handle.js.map +1 -1
  80. package/dist/esm/v3/triggerClient.test.js +1 -1
  81. package/dist/esm/v3/triggerClient.test.js.map +1 -1
  82. package/dist/esm/v3/triggerClient.types.test.js +11 -11
  83. package/dist/esm/v3/triggerClient.types.test.js.map +1 -1
  84. package/dist/esm/version.js +1 -1
  85. package/docs/ai/prompts.mdx +426 -0
  86. package/docs/ai-chat/actions.mdx +111 -0
  87. package/docs/ai-chat/anatomy.mdx +67 -0
  88. package/docs/ai-chat/backend.mdx +813 -0
  89. package/docs/ai-chat/background-injection.mdx +217 -0
  90. package/docs/ai-chat/changelog.mdx +958 -0
  91. package/docs/ai-chat/chat-local.mdx +170 -0
  92. package/docs/ai-chat/client-protocol.mdx +1077 -0
  93. package/docs/ai-chat/compaction.mdx +407 -0
  94. package/docs/ai-chat/custom-agents.mdx +392 -0
  95. package/docs/ai-chat/error-handling.mdx +411 -0
  96. package/docs/ai-chat/fast-starts.mdx +754 -0
  97. package/docs/ai-chat/frontend.mdx +576 -0
  98. package/docs/ai-chat/how-it-works.mdx +226 -0
  99. package/docs/ai-chat/lifecycle-hooks.mdx +526 -0
  100. package/docs/ai-chat/mcp.mdx +97 -0
  101. package/docs/ai-chat/overview.mdx +86 -0
  102. package/docs/ai-chat/patterns/branching-conversations.mdx +280 -0
  103. package/docs/ai-chat/patterns/code-sandbox.mdx +122 -0
  104. package/docs/ai-chat/patterns/database-persistence.mdx +410 -0
  105. package/docs/ai-chat/patterns/human-in-the-loop.mdx +279 -0
  106. package/docs/ai-chat/patterns/large-payloads.mdx +165 -0
  107. package/docs/ai-chat/patterns/oom-resilience.mdx +116 -0
  108. package/docs/ai-chat/patterns/persistence-and-replay.mdx +207 -0
  109. package/docs/ai-chat/patterns/recovery-boot.mdx +226 -0
  110. package/docs/ai-chat/patterns/skills.mdx +217 -0
  111. package/docs/ai-chat/patterns/sub-agents.mdx +379 -0
  112. package/docs/ai-chat/patterns/tool-result-auditing.mdx +144 -0
  113. package/docs/ai-chat/patterns/trusted-edge-signals.mdx +333 -0
  114. package/docs/ai-chat/patterns/version-upgrades.mdx +168 -0
  115. package/docs/ai-chat/pending-messages.mdx +339 -0
  116. package/docs/ai-chat/prompt-caching.mdx +206 -0
  117. package/docs/ai-chat/quick-start.mdx +157 -0
  118. package/docs/ai-chat/reference.mdx +905 -0
  119. package/docs/ai-chat/server-chat.mdx +259 -0
  120. package/docs/ai-chat/sessions.mdx +329 -0
  121. package/docs/ai-chat/testing.mdx +678 -0
  122. package/docs/ai-chat/tools.mdx +187 -0
  123. package/docs/ai-chat/types.mdx +238 -0
  124. package/docs/ai-chat/upgrade-guide.mdx +511 -0
  125. package/docs/apikeys.mdx +54 -0
  126. package/docs/building-with-ai.mdx +261 -0
  127. package/docs/bulk-actions.mdx +49 -0
  128. package/docs/changelog.mdx +6 -0
  129. package/docs/cli-deploy-commands.mdx +9 -0
  130. package/docs/cli-dev-commands.mdx +9 -0
  131. package/docs/cli-init-commands.mdx +58 -0
  132. package/docs/cli-introduction.mdx +25 -0
  133. package/docs/cli-list-profiles-commands.mdx +42 -0
  134. package/docs/cli-login-commands.mdx +33 -0
  135. package/docs/cli-logout-commands.mdx +33 -0
  136. package/docs/cli-preview-archive.mdx +59 -0
  137. package/docs/cli-promote-commands.mdx +9 -0
  138. package/docs/cli-switch.mdx +43 -0
  139. package/docs/cli-update-commands.mdx +42 -0
  140. package/docs/cli-whoami-commands.mdx +33 -0
  141. package/docs/community.mdx +6 -0
  142. package/docs/config/config-file.mdx +602 -0
  143. package/docs/config/extensions/additionalFiles.mdx +38 -0
  144. package/docs/config/extensions/additionalPackages.mdx +40 -0
  145. package/docs/config/extensions/aptGet.mdx +34 -0
  146. package/docs/config/extensions/audioWaveform.mdx +20 -0
  147. package/docs/config/extensions/custom.mdx +380 -0
  148. package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
  149. package/docs/config/extensions/esbuildPlugin.mdx +31 -0
  150. package/docs/config/extensions/ffmpeg.mdx +45 -0
  151. package/docs/config/extensions/lightpanda.mdx +56 -0
  152. package/docs/config/extensions/overview.mdx +67 -0
  153. package/docs/config/extensions/playwright.mdx +195 -0
  154. package/docs/config/extensions/prismaExtension.mdx +1014 -0
  155. package/docs/config/extensions/puppeteer.mdx +30 -0
  156. package/docs/config/extensions/pythonExtension.mdx +182 -0
  157. package/docs/config/extensions/syncEnvVars.mdx +291 -0
  158. package/docs/context.mdx +235 -0
  159. package/docs/database-connections.mdx +213 -0
  160. package/docs/deploy-environment-variables.mdx +435 -0
  161. package/docs/deployment/atomic-deployment.mdx +172 -0
  162. package/docs/deployment/dev-branches.mdx +92 -0
  163. package/docs/deployment/overview.mdx +257 -0
  164. package/docs/deployment/preview-branches.mdx +224 -0
  165. package/docs/errors-retrying.mdx +379 -0
  166. package/docs/github-actions.mdx +222 -0
  167. package/docs/github-integration.mdx +136 -0
  168. package/docs/github-repo.mdx +8 -0
  169. package/docs/help-email.mdx +6 -0
  170. package/docs/help-slack.mdx +12 -0
  171. package/docs/hidden-tasks.mdx +56 -0
  172. package/docs/how-it-works.mdx +454 -0
  173. package/docs/how-to-reduce-your-spend.mdx +224 -0
  174. package/docs/idempotency.mdx +504 -0
  175. package/docs/introduction.mdx +223 -0
  176. package/docs/limits.mdx +241 -0
  177. package/docs/logging.mdx +195 -0
  178. package/docs/machines.mdx +952 -0
  179. package/docs/manual-setup.mdx +632 -0
  180. package/docs/mcp-agent-rules.mdx +41 -0
  181. package/docs/mcp-introduction.mdx +385 -0
  182. package/docs/mcp-tools.mdx +273 -0
  183. package/docs/migrating-from-v3.mdx +334 -0
  184. package/docs/observability/dashboards.mdx +102 -0
  185. package/docs/observability/query.mdx +585 -0
  186. package/docs/open-source-contributing.mdx +16 -0
  187. package/docs/open-source-self-hosting.mdx +541 -0
  188. package/docs/private-networking/aws-console-setup.mdx +304 -0
  189. package/docs/private-networking/overview.mdx +144 -0
  190. package/docs/private-networking/troubleshooting.mdx +78 -0
  191. package/docs/queue-concurrency.mdx +354 -0
  192. package/docs/quick-start.mdx +97 -0
  193. package/docs/realtime/auth.mdx +208 -0
  194. package/docs/realtime/backend/overview.mdx +45 -0
  195. package/docs/realtime/backend/streams.mdx +418 -0
  196. package/docs/realtime/backend/subscribe.mdx +225 -0
  197. package/docs/realtime/how-it-works.mdx +94 -0
  198. package/docs/realtime/overview.mdx +63 -0
  199. package/docs/realtime/react-hooks/overview.mdx +73 -0
  200. package/docs/realtime/react-hooks/streams.mdx +449 -0
  201. package/docs/realtime/react-hooks/subscribe.mdx +674 -0
  202. package/docs/realtime/react-hooks/swr.mdx +87 -0
  203. package/docs/realtime/react-hooks/triggering.mdx +194 -0
  204. package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
  205. package/docs/realtime/run-object.mdx +174 -0
  206. package/docs/replaying.mdx +72 -0
  207. package/docs/request-feature.mdx +6 -0
  208. package/docs/roadmap.mdx +6 -0
  209. package/docs/run-tests.mdx +20 -0
  210. package/docs/run-usage.mdx +113 -0
  211. package/docs/runs/heartbeats.mdx +38 -0
  212. package/docs/runs/max-duration.mdx +139 -0
  213. package/docs/runs/metadata.mdx +734 -0
  214. package/docs/runs/priority.mdx +31 -0
  215. package/docs/runs.mdx +396 -0
  216. package/docs/self-hosting/docker.mdx +458 -0
  217. package/docs/self-hosting/env/supervisor.mdx +74 -0
  218. package/docs/self-hosting/env/webapp.mdx +276 -0
  219. package/docs/self-hosting/kubernetes.mdx +601 -0
  220. package/docs/self-hosting/overview.mdx +109 -0
  221. package/docs/skills.mdx +85 -0
  222. package/docs/tags.mdx +120 -0
  223. package/docs/tasks/overview.mdx +697 -0
  224. package/docs/tasks/scheduled.mdx +382 -0
  225. package/docs/tasks/schemaTask.mdx +413 -0
  226. package/docs/tasks/streams.mdx +884 -0
  227. package/docs/triggering.mdx +1320 -0
  228. package/docs/troubleshooting-alerts.mdx +385 -0
  229. package/docs/troubleshooting-debugging-in-vscode.mdx +9 -0
  230. package/docs/troubleshooting-github-issues.mdx +6 -0
  231. package/docs/troubleshooting-uptime-status.mdx +7 -0
  232. package/docs/troubleshooting.mdx +398 -0
  233. package/docs/upgrading-packages.mdx +80 -0
  234. package/docs/vercel-integration.mdx +207 -0
  235. package/docs/versioning.mdx +56 -0
  236. package/docs/video-walkthrough.mdx +23 -0
  237. package/docs/wait-for-token.mdx +540 -0
  238. package/docs/wait-for.mdx +42 -0
  239. package/docs/wait-until.mdx +53 -0
  240. package/docs/wait.mdx +18 -0
  241. package/docs/writing-tasks-introduction.mdx +33 -0
  242. package/package.json +8 -5
  243. package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
  244. package/skills/trigger-authoring-tasks/SKILL.md +254 -0
  245. package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
  246. package/skills/trigger-cost-savings/SKILL.md +116 -0
  247. package/skills/trigger-realtime-and-frontend/SKILL.md +276 -0
@@ -0,0 +1,217 @@
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
+ ## Overview
8
+
9
+ `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).
10
+
11
+ 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.
12
+
13
+ ## Basic usage
14
+
15
+ ```ts
16
+ import { chat } from "@trigger.dev/sdk/ai";
17
+
18
+ // Queue a system message for injection
19
+ chat.inject([
20
+ {
21
+ role: "system",
22
+ content: "The user's account was just upgraded to Pro.",
23
+ },
24
+ ]);
25
+ ```
26
+
27
+ Messages are appended to the model messages before the next LLM inference call. The LLM sees them as part of the conversation context.
28
+
29
+ ## Common pattern: defer + inject
30
+
31
+ 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.
32
+
33
+ ```ts
34
+ export const myChat = chat.agent({
35
+ id: "my-chat",
36
+ onTurnComplete: async ({ messages }) => {
37
+ // Kick off background analysis — doesn't block the turn
38
+ chat.defer(
39
+ (async () => {
40
+ const analysis = await analyzeConversation(messages);
41
+ chat.inject([
42
+ {
43
+ role: "system",
44
+ content: `[Analysis of conversation so far]\n\n${analysis}`,
45
+ },
46
+ ]);
47
+ })()
48
+ );
49
+ },
50
+ run: async ({ messages, signal }) => {
51
+ return streamText({
52
+ ...chat.toStreamTextOptions({ registry }),
53
+ messages,
54
+ abortSignal: signal,
55
+ stopWhen: stepCountIs(15),
56
+ });
57
+ },
58
+ });
59
+ ```
60
+
61
+ ### Timing
62
+
63
+ 1. Turn completes, `onTurnComplete` fires
64
+ 2. `chat.defer()` registers the background work
65
+ 3. The run immediately starts waiting for the next message (no blocking)
66
+ 4. Background work completes, `chat.inject()` queues the messages
67
+ 5. User sends next message, turn starts
68
+ 6. Injected messages are appended before `run()` executes
69
+ 7. The LLM sees the injected context alongside the new user message
70
+
71
+ If the background work finishes *during* a tool-call loop (not between turns), the messages are picked up at the next `prepareStep` boundary instead.
72
+
73
+ ## Example: self-review
74
+
75
+ 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.
76
+
77
+ ```ts
78
+ import { chat } from "@trigger.dev/sdk/ai";
79
+ import { prompts } from "@trigger.dev/sdk";
80
+ import { streamText, generateObject, createProviderRegistry, stepCountIs } from "ai";
81
+ import { anthropic } from "@ai-sdk/anthropic";
82
+ import { z } from "zod";
83
+
84
+ const registry = createProviderRegistry({ anthropic });
85
+
86
+ const selfReviewPrompt = prompts.define({
87
+ id: "self-review",
88
+ model: "anthropic:claude-haiku-4-5",
89
+ content: `You are a conversation quality reviewer. Analyze the assistant's most recent response.
90
+
91
+ Focus on:
92
+ - Whether the response answered the user's question
93
+ - Missed opportunities to use tools or provide more detail
94
+ - Tone mismatches
95
+
96
+ Be concise. Only flag issues worth fixing.`,
97
+ });
98
+
99
+ export const myChat = chat.agent({
100
+ id: "my-chat",
101
+ onTurnComplete: async ({ messages }) => {
102
+ chat.defer(
103
+ (async () => {
104
+ const resolved = await selfReviewPrompt.resolve({});
105
+
106
+ const review = await generateObject({
107
+ model: registry.languageModel(resolved.model ?? "anthropic:claude-haiku-4-5"),
108
+ ...resolved.toAISDKTelemetry(),
109
+ system: resolved.text,
110
+ prompt: messages
111
+ .filter((m) => m.role === "user" || m.role === "assistant")
112
+ .map((m) => {
113
+ const text =
114
+ typeof m.content === "string"
115
+ ? m.content
116
+ : Array.isArray(m.content)
117
+ ? m.content
118
+ .filter((p: any) => p.type === "text")
119
+ .map((p: any) => p.text)
120
+ .join("")
121
+ : "";
122
+ return `${m.role}: ${text}`;
123
+ })
124
+ .join("\n\n"),
125
+ schema: z.object({
126
+ needsImprovement: z.boolean(),
127
+ suggestions: z.array(z.string()),
128
+ }),
129
+ });
130
+
131
+ if (review.object.needsImprovement) {
132
+ chat.inject([
133
+ {
134
+ role: "system",
135
+ content: `[Self-review]\n\n${review.object.suggestions.map((s) => `- ${s}`).join("\n")}\n\nApply these naturally.`,
136
+ },
137
+ ]);
138
+ }
139
+ })()
140
+ );
141
+ },
142
+ run: async ({ messages, signal }) => {
143
+ return streamText({
144
+ ...chat.toStreamTextOptions({ registry }),
145
+ messages,
146
+ abortSignal: signal,
147
+ stopWhen: stepCountIs(15),
148
+ });
149
+ },
150
+ });
151
+ ```
152
+
153
+ 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.
154
+
155
+ ## Other use cases
156
+
157
+ - **RAG augmentation**: After each turn, fetch relevant documents and inject them as context for the next response
158
+ - **Safety checks**: Run a moderation model on the response, inject warnings if issues are detected
159
+ - **Fact-checking**: Verify claims in the response using search tools, inject corrections
160
+ - **Context enrichment**: Look up user/account data based on what was discussed, inject it as system context
161
+
162
+ ## `chat.defer` standalone
163
+
164
+ `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.
165
+
166
+ ```ts
167
+ export const myChat = chat.agent({
168
+ id: "my-chat",
169
+ onTurnStart: async ({ chatId, runId }) => {
170
+ // Analytics — fire-and-forget, irrelevant to resume.
171
+ chat.defer(analytics.track("turn_started", { chatId, runId }));
172
+ },
173
+ run: async ({ messages, signal }) => {
174
+ return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
175
+ },
176
+ });
177
+ ```
178
+
179
+ `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`.
180
+
181
+ <Warning>
182
+ **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.
183
+ </Warning>
184
+
185
+ ## How it differs from pending messages
186
+
187
+ | | `chat.inject()` | [Pending messages](/ai-chat/pending-messages) |
188
+ |---|---|---|
189
+ | **Source** | Backend task code | Frontend user input |
190
+ | **Triggered by** | Your code (e.g. `onTurnComplete` + `chat.defer()`) | User sending a message during streaming |
191
+ | **Injection point** | Start of next turn, or next `prepareStep` boundary | Next `prepareStep` boundary only |
192
+ | **Message role** | Any (`system`, `user`, `assistant`) | Typically `user` |
193
+ | **Frontend visibility** | Not visible unless you write custom `data-*` chunks | Visible via `usePendingMessages` hook |
194
+
195
+ ## API reference
196
+
197
+ ### chat.inject()
198
+
199
+ ```ts
200
+ chat.inject(messages: ModelMessage[]): void
201
+ ```
202
+
203
+ 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.
204
+
205
+ **Parameters:**
206
+
207
+ | Parameter | Type | Description |
208
+ |-----------|------|-------------|
209
+ | `messages` | `ModelMessage[]` | Model messages to inject (from the `ai` package) |
210
+
211
+ Messages are drained (consumed) when:
212
+ 1. A new turn starts — before `run()` executes
213
+ 2. A `prepareStep` boundary is reached — between tool-call steps during streaming
214
+
215
+ <Note>
216
+ `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.
217
+ </Note>