Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d429296
docs: ai chat.task
ericallam Mar 16, 2026
6f6ab89
docs: rename warmTimeout to idleTimeout in ai-chat docs
ericallam Mar 23, 2026
006e70c
add docs for prompts
ericallam Mar 24, 2026
6f70ab8
better compaction support in createSession and manual tasks
ericallam Mar 24, 2026
4d5ea95
docs: add prompts, compaction, and pending messages docs
ericallam Mar 25, 2026
ea74580
document the writer stuff
ericallam Mar 26, 2026
e6340ca
Add background injection docs
ericallam Mar 26, 2026
1011103
docs(ai-chat): add Types page, link toolExecute and withUIMessage, fi…
ericallam Mar 27, 2026
aaae1ab
Add run-scoped PAT renewal for chat transport
ericallam Mar 27, 2026
857fd22
patterns and the ctx thing
ericallam Mar 27, 2026
34b61bc
docs: add onChatSuspend/onChatResume, exitAfterPreloadIdle, withClien…
ericallam Mar 28, 2026
565b706
code sandbox and database patterns
ericallam Mar 28, 2026
cc04d47
docs: rename chat.task to chat.agent across all AI docs
ericallam Mar 30, 2026
a5f710d
subagents and AgentChat docs
ericallam Apr 2, 2026
838566b
remove references to the ai chat reference project from the docs
ericallam Apr 2, 2026
948d71c
agent mcp tools docs
ericallam Apr 2, 2026
32ac49e
docs for validating ui messages
ericallam Apr 2, 2026
58dd852
version upgrades
ericallam Apr 3, 2026
39af88b
docs for stopping chats after resume
ericallam Apr 11, 2026
71c3c16
docs: add tool approvals and stop-after-resume documentation
ericallam Apr 14, 2026
2e652fd
cover tool approvals in the client protocol
ericallam Apr 14, 2026
de4f93a
Cover passing a custom message ID generator
ericallam Apr 14, 2026
55ec385
docs: add chat.response API, persistent data parts, transient flag, t…
ericallam Apr 14, 2026
41af0df
add agent prerelease changelog
ericallam Apr 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,256 changes: 1,256 additions & 0 deletions docs/ai-chat/backend.mdx

Large diffs are not rendered by default.

192 changes: 192 additions & 0 deletions docs/ai-chat/background-injection.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
title: "Background injection"
sidebarTitle: "Background injection"
description: "Inject context from background work into the agent's conversation — self-review, RAG augmentation, or any async analysis."
---

## Overview

`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).

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.

## Basic usage

```ts
import { chat } from "@trigger.dev/sdk/ai";

// Queue a system message for injection
chat.inject([
{
role: "system",
content: "The user's account was just upgraded to Pro.",
},
]);
```

Messages are appended to the model messages before the next LLM inference call. The LLM sees them as part of the conversation context.

## Common pattern: defer + inject

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.

```ts
export const myChat = chat.agent({
id: "my-chat",
onTurnComplete: async ({ messages }) => {
// Kick off background analysis — doesn't block the turn
chat.defer(
(async () => {
const analysis = await analyzeConversation(messages);
chat.inject([
{
role: "system",
content: `[Analysis of conversation so far]\n\n${analysis}`,
},
]);
})()
);
},
run: async ({ messages, signal }) => {
return streamText({
...chat.toStreamTextOptions({ registry }),
messages,
abortSignal: signal,
});
},
});
```

### Timing

1. Turn completes, `onTurnComplete` fires
2. `chat.defer()` registers the background work
3. The run immediately starts waiting for the next message (no blocking)
4. Background work completes, `chat.inject()` queues the messages
5. User sends next message, turn starts
6. Injected messages are appended before `run()` executes
7. The LLM sees the injected context alongside the new user message

If the background work finishes *during* a tool-call loop (not between turns), the messages are picked up at the next `prepareStep` boundary instead.

## Example: self-review

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.

```ts
import { chat } from "@trigger.dev/sdk/ai";
import { prompts } from "@trigger.dev/sdk";
import { streamText, generateObject, createProviderRegistry } from "ai";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";

const registry = createProviderRegistry({ openai });

const selfReviewPrompt = prompts.define({
id: "self-review",
model: "openai:gpt-4o-mini",
content: `You are a conversation quality reviewer. Analyze the assistant's most recent response.

Focus on:
- Whether the response answered the user's question
- Missed opportunities to use tools or provide more detail
- Tone mismatches

Be concise. Only flag issues worth fixing.`,
});

export const myChat = chat.agent({
id: "my-chat",
onTurnComplete: async ({ messages }) => {
chat.defer(
(async () => {
const resolved = await selfReviewPrompt.resolve({});

const review = await generateObject({
model: registry.languageModel(resolved.model ?? "openai:gpt-4o-mini"),
...resolved.toAISDKTelemetry(),
system: resolved.text,
prompt: messages
.filter((m) => m.role === "user" || m.role === "assistant")
.map((m) => {
const text =
typeof m.content === "string"
? m.content
: Array.isArray(m.content)
? m.content
.filter((p: any) => p.type === "text")
.map((p: any) => p.text)
.join("")
: "";
return `${m.role}: ${text}`;
})
.join("\n\n"),
schema: z.object({
needsImprovement: z.boolean(),
suggestions: z.array(z.string()),
}),
});

if (review.object.needsImprovement) {
chat.inject([
{
role: "system",
content: `[Self-review]\n\n${review.object.suggestions.map((s) => `- ${s}`).join("\n")}\n\nApply these naturally.`,
},
]);
}
})()
);
},
run: async ({ messages, signal }) => {
return streamText({
...chat.toStreamTextOptions({ registry }),
messages,
abortSignal: signal,
});
},
});
```

The self-review runs on `gpt-4o-mini` (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.

## Other use cases

- **RAG augmentation**: After each turn, fetch relevant documents and inject them as context for the next response
- **Safety checks**: Run a moderation model on the response, inject warnings if issues are detected
- **Fact-checking**: Verify claims in the response using search tools, inject corrections
- **Context enrichment**: Look up user/account data based on what was discussed, inject it as system context

## How it differs from pending messages

| | `chat.inject()` | [Pending messages](/ai-chat/pending-messages) |
|---|---|---|
| **Source** | Backend task code | Frontend user input |
| **Triggered by** | Your code (e.g. `onTurnComplete` + `chat.defer()`) | User sending a message during streaming |
| **Injection point** | Start of next turn, or next `prepareStep` boundary | Next `prepareStep` boundary only |
| **Message role** | Any (`system`, `user`, `assistant`) | Typically `user` |
| **Frontend visibility** | Not visible unless you write custom `data-*` chunks | Visible via `usePendingMessages` hook |

## API reference

### chat.inject()

```ts
chat.inject(messages: ModelMessage[]): void
```

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.

**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `messages` | `ModelMessage[]` | Model messages to inject (from the `ai` package) |

Messages are drained (consumed) when:
1. A new turn starts — before `run()` executes
2. A `prepareStep` boundary is reached — between tool-call steps during streaming

<Note>
`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.
</Note>
64 changes: 64 additions & 0 deletions docs/ai-chat/changelog.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: "Changelog"
sidebarTitle: "Changelog"
description: "Pre-release updates for AI chat agents."
---

<Update label="April 14, 2026" description="0.0.0-chat-prerelease-20260414181032" tags={["SDK"]}>

## `chat.response` — persistent data parts

Added `chat.response.write()` for writing data parts that both stream to the frontend AND persist in `onTurnComplete`'s `responseMessage` and `uiMessages`.

```ts
// Persists to responseMessage.parts — available in onTurnComplete
chat.response.write({ type: "data-handover", data: { context: summary } });

// Transient — streams to frontend only, not in responseMessage
writer.write({ type: "data-progress", data: { percent: 50 }, transient: true });
```

Non-transient `data-*` chunks written via lifecycle hook `writer.write()` now automatically persist to the response message, matching the AI SDK's default semantics. Add `transient: true` for ephemeral chunks (progress indicators, status updates).

See [Custom data parts](/ai-chat/features#custom-data-parts).

## Tool approvals

Added support for AI SDK tool approvals (`needsApproval: true`). When the model calls a tool that needs approval, the turn completes and the frontend shows approve/deny buttons. After approval, the updated assistant message is sent back and matched by ID in the accumulator.

```ts
const sendEmail = tool({
description: "Send an email. Requires human approval.",
inputSchema: z.object({ to: z.string(), subject: z.string(), body: z.string() }),
needsApproval: true,
execute: async ({ to, subject, body }) => { /* ... */ },
});
```

Frontend setup requires `sendAutomaticallyWhen` and `addToolApprovalResponse` from `useChat`. See [Tool approvals](/ai-chat/frontend#tool-approvals).

## `transport.stopGeneration(chatId)`

Added `stopGeneration` method to `TriggerChatTransport` for reliable stop after page refresh / stream reconnect. Works regardless of whether the AI SDK passes `abortSignal` through `reconnectToStream`.

```tsx
const stop = useCallback(() => {
transport.stopGeneration(chatId);
aiStop(); // also update useChat state
}, [transport, chatId, aiStop]);
```

See [Stop generation](/ai-chat/frontend#stop-generation).

## `generateMessageId` support

`generateMessageId` can now be passed via `uiMessageStreamOptions` to control response message ID generation (e.g. UUID-v7). The backend automatically passes `originalMessages` to `toUIMessageStream` so message IDs are consistent between frontend and backend.

## Bug fixes

- **`onTurnComplete` not called**: Fixed `turnCompleteResult?.lastEventId` TypeError that silently skipped `onTurnComplete` when `writeTurnCompleteChunk` returned undefined in dev.
- **Stop during streaming**: Added 2s timeout on `onFinishPromise` so `onBeforeTurnComplete` and `onTurnComplete` fire even when the AI SDK's `onFinish` doesn't fire after abort.
- **`toStreamTextOptions` without `chat.prompt.set()`**: `prepareStep` injection (compaction, steering, background context) now works even when the user passes `system` directly to `streamText` instead of using `chat.prompt.set()`.
- **Background queue vs tool approvals**: Background context injection is now skipped when the last accumulated message is a `tool` message, preventing it from breaking `streamText`'s `collectToolApprovals`.

</Update>
Loading
Loading