Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
8f557ee
docs: ai chat.task
ericallam Mar 16, 2026
5ea1c1d
docs: rename warmTimeout to idleTimeout in ai-chat docs
ericallam Mar 23, 2026
db951cb
add docs for prompts
ericallam Mar 24, 2026
65c1548
better compaction support in createSession and manual tasks
ericallam Mar 24, 2026
fff5e08
docs: add prompts, compaction, and pending messages docs
ericallam Mar 25, 2026
e7f577f
document the writer stuff
ericallam Mar 26, 2026
7264cc5
Add background injection docs
ericallam Mar 26, 2026
24aae42
docs(ai-chat): add Types page, link toolExecute and withUIMessage, fi…
ericallam Mar 27, 2026
0c65122
Add run-scoped PAT renewal for chat transport
ericallam Mar 27, 2026
23fb1eb
patterns and the ctx thing
ericallam Mar 27, 2026
83f0ee8
docs: add onChatSuspend/onChatResume, exitAfterPreloadIdle, withClien…
ericallam Mar 28, 2026
0439ea3
code sandbox and database patterns
ericallam Mar 28, 2026
4370927
docs: rename chat.task to chat.agent across all AI docs
ericallam Mar 30, 2026
886bbce
subagents and AgentChat docs
ericallam Apr 2, 2026
62c6c64
remove references to the ai chat reference project from the docs
ericallam Apr 2, 2026
cb8cd99
agent mcp tools docs
ericallam Apr 2, 2026
2256426
docs for validating ui messages
ericallam Apr 2, 2026
c6bab90
version upgrades
ericallam Apr 3, 2026
ae68d78
docs for stopping chats after resume
ericallam Apr 11, 2026
fecda84
docs: add tool approvals and stop-after-resume documentation
ericallam Apr 14, 2026
45fb492
cover tool approvals in the client protocol
ericallam Apr 14, 2026
47df9be
Cover passing a custom message ID generator
ericallam Apr 14, 2026
22d9855
docs: add chat.response API, persistent data parts, transient flag, t…
ericallam Apr 14, 2026
2509748
add agent prerelease changelog
ericallam Apr 14, 2026
bdb6343
docs: add hydrateMessages, chat.history, and actions documentation
ericallam Apr 15, 2026
81e7ebc
Add branching pattern
ericallam Apr 15, 2026
51bc393
new changelog for 0.0.0-chat-prerelease-20260415164455
ericallam Apr 15, 2026
fb43438
docs: multi-tab coordination, error stack truncation changelog
ericallam Apr 17, 2026
0e59236
docs(ai-chat): add testing page for chat.agent
ericallam Apr 18, 2026
d62c1bd
docs(ai-chat): changelog entry for 0.0.0-chat-prerelease-20260418083610
ericallam Apr 18, 2026
1aaf3cc
docs(ai-chat): comprehensive error handling guide
ericallam Apr 18, 2026
e23f37c
docs(ai-chat): add human-in-the-loop patterns page
ericallam Apr 18, 2026
318d9d8
docs(ai-chat): document chat.endRun()
ericallam Apr 18, 2026
b195bf8
docs(ai-chat): add user-initiated compaction pattern
ericallam Apr 18, 2026
70ad661
docs(ai-chat): changelog entry for 0.0.0-chat-prerelease-20260418174118
ericallam Apr 18, 2026
92d293c
docs(ai-chat): add Agent Skills pattern page
ericallam Apr 18, 2026
5cd512c
docs(ai-chat): changelog entry for 0.0.0-chat-prerelease-20260419173457
ericallam Apr 19, 2026
13e1bd4
docs: Sessions primitive + chat.agent on Sessions
ericallam Apr 24, 2026
0e19f27
document x-peek-settled header
ericallam Apr 25, 2026
0c010fe
docs: chat.agent on Sessions-as-run-manager
ericallam Apr 27, 2026
3b2bdf4
docs(ai-chat): add not-released-yet banner to Sessions upgrade guide
ericallam Apr 28, 2026
07d3064
docs(ai-chat): warn against non-atomic onTurnComplete persistence
ericallam Apr 28, 2026
b19e093
docs(ai-chat): tidy Sessions changelog + sidebar
ericallam Apr 29, 2026
f882cdf
docs(ai-chat): warn against chat.defer for onTurnStart message persis…
ericallam Apr 29, 2026
f82eaa1
docs(ai-chat): large-payloads pattern + ChatChunkTooLargeError reference
ericallam Apr 30, 2026
b4c29eb
docs(ai-chat): unblock check-broken-links + address PR #3226 review f…
ericallam Apr 30, 2026
d90269c
Release 0.0.0-chat-prerelease-20260501122331
ericallam May 1, 2026
d8ba4ee
docs(ai-chat): resilient SSE reconnection
ericallam May 1, 2026
27941bc
docs(ai-chat): mark SSE reconnection entry as Upcoming
ericallam May 1, 2026
02aa51e
docs(ai-chat): tag SSE reconnection entry with chat-prerelease-202605…
ericallam May 2, 2026
ebdc408
docs(ai-chat): chat.headStart guide + reference + changelog
ericallam May 3, 2026
f87e0fb
docs(ai-chat): pin chat.headStart entry to 0.0.0-chat-prerelease-2026…
ericallam May 5, 2026
c104d5b
docs(ai-chat): surface chat.toStreamTextOptions() in entry-point pages
ericallam May 5, 2026
a23ed7d
docs(ai-chat): changelog entry for suspend/resume duplicate-turn fix
ericallam May 5, 2026
c332c1a
docs(ai-chat): action turns become side-effect-only
ericallam May 6, 2026
e729269
docs(ai-chat): document actions-not-turns semantics
ericallam May 6, 2026
96d8ef3
docs(ai-chat): pin May 6 changelog entry to 0.0.0-chat-prerelease-202…
ericallam May 6, 2026
0f357ef
docs(ai-chat): split lifecycle hooks, actions, and fast starts into p…
ericallam May 7, 2026
ee34650
docs(ai-chat): add changelog entry for May 7 chat-prerelease
ericallam May 7, 2026
1f99b6d
docs(ai-chat): document oomMachine OOM-resilience pattern
ericallam May 7, 2026
b1ee852
docs(ai-chat): document delta-only wire and snapshot model
ericallam May 9, 2026
be53065
docs(ai-chat): onChatStart once-per-chat contract + continuation run …
ericallam May 12, 2026
b88776a
docs(ai-chat): document onBoot lifecycle hook
ericallam May 13, 2026
0e48546
add streamBaseUrl
ericallam May 16, 2026
5c02eec
docs(ai-chat): bounded session.out + header-form control records
ericallam May 16, 2026
967c9aa
docs(ai-chat): add how-it-works page
ericallam May 17, 2026
47a0d7f
docs(ai-chat): session-in-event-id header on turn-complete control re…
ericallam May 17, 2026
50f8bef
docs(ai-chat): functional baseURL on chat transports
ericallam May 18, 2026
3cc5297
Stop recommending run streams
ericallam May 18, 2026
ab12b88
docs(ai-chat): pin May 8 + May 16 changelog entries to 0.0.0-chat-pre…
ericallam May 19, 2026
556a04c
docs(ai-chat): recovery boot — onRecoveryBoot hook + smart context-pr…
ericallam May 19, 2026
c7d7a9d
docs(ai-chat): clarify beforeBoot bubbles while onRecoveryBoot body i…
ericallam May 19, 2026
d2a7e9a
docs(ai-chat): onRecoveryBoot fires only on mid-stream interruptions …
ericallam May 19, 2026
dd31165
docs(ai-chat): prep for v4.5 release candidate
ericallam May 20, 2026
6ceb79a
Add 0.0.0-chat-prerelease-20260520150857 to changelog
ericallam May 20, 2026
76f391c
docs(ai-chat): rewrite overview to lead with the session framing
ericallam May 21, 2026
82dad78
docs(ai-chat): swap examples to Anthropic, add stopWhen, fix transpor…
ericallam May 21, 2026
236f9a5
docs(prompts): drop duplicate anthropic import and registry key
ericallam May 21, 2026
0133dc2
docs(error-handling): describe error chunk + turn-complete control re…
ericallam May 21, 2026
84d5156
chore: remove .claude/docs-plans authoring notes from docs PR
ericallam May 21, 2026
a8df7eb
docs(ai-chat): correct action lifecycle — run() and onTurnComplete do…
ericallam May 21, 2026
58a8f2e
docs(ai-chat): add 4.5.0-rc.0 release entry to changelog
ericallam May 21, 2026
dab496a
docs(ai-chat): add 4.5.0-rc.1 changelog entry
ericallam May 21, 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
115 changes: 115 additions & 0 deletions docs/ai-chat/actions.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: "Actions"
sidebarTitle: "Actions"
description: "Custom commands sent from the frontend that mutate chat state without consuming a turn — undo, rollback, edit, regenerate."
---

import RcBanner from "/snippets/ai-chat-rc-banner.mdx";

<RcBanner />

## Overview

Custom actions let the frontend send structured commands (undo, rollback, edit, regenerate) that modify the conversation state. **Actions are not turns**: they fire `hydrateMessages` (if set) and `onAction` only. No turn lifecycle hooks (`onTurnStart` / `prepareMessages` / `onBeforeTurnComplete` / `onTurnComplete`), no `run()`, no turn-counter increment. The trace span is named `chat action`.

Actions wake the agent from suspension the same way a new message does, run their handler against the latest accumulator state, and emit a `trigger:turn-complete` chunk so the frontend's `useChat` knows the action has been applied.

## Defining an action handler

Define an `actionSchema` for validation and an `onAction` handler that uses [`chat.history`](/ai-chat/backend#chat-history) to modify state:

```ts
import { z } from "zod";

export const myChat = chat.agent({
id: "my-chat",
actionSchema: z.discriminatedUnion("type", [
z.object({ type: z.literal("undo") }),
z.object({ type: z.literal("rollback"), targetMessageId: z.string() }),
z.object({ type: z.literal("edit"), messageId: z.string(), text: z.string() }),
]),

onAction: async ({ action }) => {
switch (action.type) {
case "undo":
chat.history.slice(0, -2); // Remove last user + assistant exchange
break;
case "rollback":
chat.history.rollbackTo(action.targetMessageId);
break;
case "edit":
chat.history.replace(action.messageId, {
id: action.messageId,
role: "user",
parts: [{ type: "text", text: action.text }],
});
break;
}
// returning void → side-effect-only, no model call
},

run: async ({ messages, signal }) => {
return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
},
});
```

**Lifecycle flow:** Wake → parse action against `actionSchema` → `hydrateMessages` (if set) → **`onAction`** → apply `chat.history` mutations → emit `trigger:turn-complete` → wait for next message.

## Returning a model response from an action

`onAction` can return a `StreamTextResult`, `string`, or `UIMessage` to produce a response. The returned stream is auto-piped to the frontend just like a normal turn, but the rest of the turn machinery (`onTurnStart`, `onTurnComplete`, etc.) still does not fire.

```ts
onAction: async ({ action, messages }) => {
if (action.type === "regenerate") {
chat.history.slice(0, -1); // drop the last assistant
return streamText({
model: anthropic("claude-sonnet-4-5"),
messages,
stopWhen: stepCountIs(15),
});
}
// other actions return void → side-effect only
}
```

This is useful for actions that both mutate state and want a fresh model response (regenerate-from-here, retry-with-different-style). Persistence is your responsibility inside `onAction` itself; you have access to the streamed response object.

## Gating actions on HITL state

If you have a [human-in-the-loop](/ai-chat/patterns/human-in-the-loop) tool waiting on `addToolOutput`, you usually want to refuse competing actions like `regenerate` until the answer arrives. [`chat.history.getPendingToolCalls()`](/ai-chat/backend#chat-history) gives you exactly that signal:

```ts
onAction: async ({ action, messages, signal }) => {
if (action.type === "regenerate") {
if (chat.history.getPendingToolCalls().length > 0) return; // gated
chat.history.slice(0, -1);
return streamText({ model: anthropic("claude-sonnet-4-5"), messages, abortSignal: signal });
}
},
```

## Sending actions from the frontend

```ts
// Browser — TriggerChatTransport
const stream = await transport.sendAction(chatId, { type: "undo" });

// Server — AgentChat
const stream = await agentChat.sendAction({ type: "rollback", targetMessageId: "msg-3" });
```

The action payload is validated against `actionSchema` on the backend; invalid actions throw and surface as a stream error. The `action` parameter in `onAction` is fully typed from the schema.

<Note>
For silent state changes that should never appear as a turn (e.g. injecting background context), use [`chat.inject()`](/ai-chat/background-injection) instead. Actions are explicit user-driven mutations; injections are agent-side context updates.
</Note>

## See also

- [`chat.history`](/ai-chat/backend#chat-history) — the imperative API actions use to mutate state
- [Sending actions from the frontend](/ai-chat/frontend#sending-actions) — `transport.sendAction` ergonomics
- [`hydrateMessages`](/ai-chat/lifecycle-hooks#hydratemessages) — fires before `onAction` when set
- [Branching conversations](/ai-chat/patterns/branching-conversations) — pairs action handlers with backend-controlled history
- [Human-in-the-loop](/ai-chat/patterns/human-in-the-loop) — gating fresh actions while a tool is waiting
Loading
Loading