@trigger.dev/sdk 4.5.0-rc.5 → 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.
- package/dist/commonjs/v3/ai.d.ts +178 -5
- package/dist/commonjs/v3/ai.js +603 -119
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-client.js +3 -0
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat-react.js +10 -7
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat-server.d.ts +8 -0
- package/dist/commonjs/v3/chat-server.js +32 -10
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat-server.test.js +51 -0
- package/dist/commonjs/v3/chat-server.test.js.map +1 -1
- package/dist/commonjs/v3/chat.js +34 -6
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/chat.test.js +53 -0
- package/dist/commonjs/v3/chat.test.js.map +1 -1
- package/dist/commonjs/v3/createStartSessionAction.test.js +30 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/commonjs/v3/sessions.d.ts +11 -6
- package/dist/commonjs/v3/sessions.js +10 -5
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/test/mock-chat-agent.d.ts +6 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js +1 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +178 -5
- package/dist/esm/v3/ai.js +603 -120
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-client.js +3 -0
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat-react.js +10 -7
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat-server.d.ts +8 -0
- package/dist/esm/v3/chat-server.js +32 -10
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat-server.test.js +51 -0
- package/dist/esm/v3/chat-server.test.js.map +1 -1
- package/dist/esm/v3/chat.js +34 -6
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/chat.test.js +53 -0
- package/dist/esm/v3/chat.test.js.map +1 -1
- package/dist/esm/v3/createStartSessionAction.test.js +30 -0
- package/dist/esm/v3/createStartSessionAction.test.js.map +1 -1
- package/dist/esm/v3/sessions.d.ts +11 -6
- package/dist/esm/v3/sessions.js +10 -5
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/test/mock-chat-agent.d.ts +6 -0
- package/dist/esm/v3/test/mock-chat-agent.js +1 -0
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai/prompts.mdx +430 -0
- package/docs/ai-chat/actions.mdx +115 -0
- package/docs/ai-chat/anatomy.mdx +71 -0
- package/docs/ai-chat/backend.mdx +817 -0
- package/docs/ai-chat/background-injection.mdx +221 -0
- package/docs/ai-chat/changelog.mdx +850 -0
- package/docs/ai-chat/chat-local.mdx +174 -0
- package/docs/ai-chat/client-protocol.mdx +1081 -0
- package/docs/ai-chat/compaction.mdx +411 -0
- package/docs/ai-chat/custom-agents.mdx +364 -0
- package/docs/ai-chat/error-handling.mdx +415 -0
- package/docs/ai-chat/fast-starts.mdx +672 -0
- package/docs/ai-chat/frontend.mdx +580 -0
- package/docs/ai-chat/how-it-works.mdx +230 -0
- package/docs/ai-chat/lifecycle-hooks.mdx +530 -0
- package/docs/ai-chat/mcp.mdx +101 -0
- package/docs/ai-chat/overview.mdx +90 -0
- package/docs/ai-chat/patterns/branching-conversations.mdx +284 -0
- package/docs/ai-chat/patterns/code-sandbox.mdx +126 -0
- package/docs/ai-chat/patterns/database-persistence.mdx +414 -0
- package/docs/ai-chat/patterns/human-in-the-loop.mdx +275 -0
- package/docs/ai-chat/patterns/large-payloads.mdx +169 -0
- package/docs/ai-chat/patterns/oom-resilience.mdx +120 -0
- package/docs/ai-chat/patterns/persistence-and-replay.mdx +211 -0
- package/docs/ai-chat/patterns/recovery-boot.mdx +230 -0
- package/docs/ai-chat/patterns/skills.mdx +221 -0
- package/docs/ai-chat/patterns/sub-agents.mdx +383 -0
- package/docs/ai-chat/patterns/tool-result-auditing.mdx +148 -0
- package/docs/ai-chat/patterns/trusted-edge-signals.mdx +337 -0
- package/docs/ai-chat/patterns/version-upgrades.mdx +172 -0
- package/docs/ai-chat/pending-messages.mdx +343 -0
- package/docs/ai-chat/prompt-caching.mdx +206 -0
- package/docs/ai-chat/quick-start.mdx +161 -0
- package/docs/ai-chat/reference.mdx +909 -0
- package/docs/ai-chat/server-chat.mdx +263 -0
- package/docs/ai-chat/sessions.mdx +333 -0
- package/docs/ai-chat/testing.mdx +682 -0
- package/docs/ai-chat/tools.mdx +191 -0
- package/docs/ai-chat/types.mdx +242 -0
- package/docs/ai-chat/upgrade-guide.mdx +515 -0
- package/docs/apikeys.mdx +54 -0
- package/docs/building-with-ai.mdx +261 -0
- package/docs/bulk-actions.mdx +49 -0
- package/docs/changelog.mdx +6 -0
- package/docs/cli-deploy-commands.mdx +9 -0
- package/docs/cli-dev-commands.mdx +9 -0
- package/docs/cli-dev.mdx +8 -0
- package/docs/cli-init-commands.mdx +58 -0
- package/docs/cli-introduction.mdx +25 -0
- package/docs/cli-list-profiles-commands.mdx +42 -0
- package/docs/cli-login-commands.mdx +33 -0
- package/docs/cli-logout-commands.mdx +33 -0
- package/docs/cli-preview-archive.mdx +59 -0
- package/docs/cli-promote-commands.mdx +9 -0
- package/docs/cli-switch.mdx +43 -0
- package/docs/cli-update-commands.mdx +42 -0
- package/docs/cli-whoami-commands.mdx +33 -0
- package/docs/community.mdx +6 -0
- package/docs/config/config-file.mdx +602 -0
- package/docs/config/extensions/additionalFiles.mdx +38 -0
- package/docs/config/extensions/additionalPackages.mdx +40 -0
- package/docs/config/extensions/aptGet.mdx +34 -0
- package/docs/config/extensions/audioWaveform.mdx +20 -0
- package/docs/config/extensions/custom.mdx +380 -0
- package/docs/config/extensions/emitDecoratorMetadata.mdx +29 -0
- package/docs/config/extensions/esbuildPlugin.mdx +31 -0
- package/docs/config/extensions/ffmpeg.mdx +45 -0
- package/docs/config/extensions/lightpanda.mdx +56 -0
- package/docs/config/extensions/overview.mdx +67 -0
- package/docs/config/extensions/playwright.mdx +195 -0
- package/docs/config/extensions/prismaExtension.mdx +1014 -0
- package/docs/config/extensions/puppeteer.mdx +30 -0
- package/docs/config/extensions/pythonExtension.mdx +182 -0
- package/docs/config/extensions/syncEnvVars.mdx +291 -0
- package/docs/context.mdx +235 -0
- package/docs/database-connections.mdx +213 -0
- package/docs/deploy-environment-variables.mdx +435 -0
- package/docs/deployment/atomic-deployment.mdx +172 -0
- package/docs/deployment/overview.mdx +257 -0
- package/docs/deployment/preview-branches.mdx +224 -0
- package/docs/errors-retrying.mdx +379 -0
- package/docs/github-actions.mdx +222 -0
- package/docs/github-integration.mdx +136 -0
- package/docs/github-repo.mdx +8 -0
- package/docs/help-email.mdx +6 -0
- package/docs/help-slack.mdx +11 -0
- package/docs/hidden-tasks.mdx +56 -0
- package/docs/how-it-works.mdx +454 -0
- package/docs/how-to-reduce-your-spend.mdx +217 -0
- package/docs/idempotency.mdx +504 -0
- package/docs/introduction.mdx +223 -0
- package/docs/limits.mdx +241 -0
- package/docs/logging.mdx +195 -0
- package/docs/machines.mdx +952 -0
- package/docs/manual-setup.mdx +632 -0
- package/docs/mcp-agent-rules.mdx +41 -0
- package/docs/mcp-introduction.mdx +385 -0
- package/docs/mcp-tools.mdx +273 -0
- package/docs/migrating-from-v3.mdx +334 -0
- package/docs/observability/dashboards.mdx +102 -0
- package/docs/observability/query.mdx +585 -0
- package/docs/open-source-contributing.mdx +16 -0
- package/docs/open-source-self-hosting.mdx +541 -0
- package/docs/private-networking/aws-console-setup.mdx +304 -0
- package/docs/private-networking/overview.mdx +144 -0
- package/docs/private-networking/troubleshooting.mdx +78 -0
- package/docs/queue-concurrency.mdx +354 -0
- package/docs/quick-start.mdx +97 -0
- package/docs/realtime/auth.mdx +208 -0
- package/docs/realtime/backend/overview.mdx +45 -0
- package/docs/realtime/backend/streams.mdx +418 -0
- package/docs/realtime/backend/subscribe.mdx +225 -0
- package/docs/realtime/how-it-works.mdx +94 -0
- package/docs/realtime/overview.mdx +63 -0
- package/docs/realtime/react-hooks/overview.mdx +73 -0
- package/docs/realtime/react-hooks/streams.mdx +449 -0
- package/docs/realtime/react-hooks/subscribe.mdx +674 -0
- package/docs/realtime/react-hooks/swr.mdx +87 -0
- package/docs/realtime/react-hooks/triggering.mdx +194 -0
- package/docs/realtime/react-hooks/use-wait-token.mdx +34 -0
- package/docs/realtime/run-object.mdx +174 -0
- package/docs/replaying.mdx +72 -0
- package/docs/request-feature.mdx +6 -0
- package/docs/roadmap.mdx +6 -0
- package/docs/run-tests.mdx +20 -0
- package/docs/run-usage.mdx +113 -0
- package/docs/runs/heartbeats.mdx +38 -0
- package/docs/runs/max-duration.mdx +139 -0
- package/docs/runs/metadata.mdx +734 -0
- package/docs/runs/priority.mdx +31 -0
- package/docs/runs.mdx +396 -0
- package/docs/self-hosting/docker.mdx +458 -0
- package/docs/self-hosting/env/supervisor.mdx +74 -0
- package/docs/self-hosting/env/webapp.mdx +276 -0
- package/docs/self-hosting/kubernetes.mdx +601 -0
- package/docs/self-hosting/overview.mdx +108 -0
- package/docs/skills.mdx +85 -0
- package/docs/tags.mdx +120 -0
- package/docs/tasks/overview.mdx +697 -0
- package/docs/tasks/scheduled.mdx +382 -0
- package/docs/tasks/schemaTask.mdx +413 -0
- package/docs/tasks/streams.mdx +884 -0
- package/docs/triggering.mdx +1320 -0
- package/docs/troubleshooting-alerts.mdx +385 -0
- package/docs/troubleshooting-debugging-in-vscode.mdx +8 -0
- package/docs/troubleshooting-github-issues.mdx +6 -0
- package/docs/troubleshooting-uptime-status.mdx +6 -0
- package/docs/troubleshooting.mdx +398 -0
- package/docs/upgrading-packages.mdx +80 -0
- package/docs/vercel-integration.mdx +207 -0
- package/docs/versioning.mdx +56 -0
- package/docs/video-walkthrough.mdx +23 -0
- package/docs/wait-for-token.mdx +540 -0
- package/docs/wait-for.mdx +42 -0
- package/docs/wait-until.mdx +53 -0
- package/docs/wait.mdx +18 -0
- package/docs/writing-tasks-introduction.mdx +33 -0
- package/package.json +10 -6
- package/skills/trigger-authoring-chat-agent/SKILL.md +296 -0
- package/skills/trigger-authoring-tasks/SKILL.md +254 -0
- package/skills/trigger-chat-agent-advanced/SKILL.md +368 -0
- package/skills/trigger-cost-savings/SKILL.md +116 -0
- 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>
|