Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

migration.md

173 lines6.0 KB

1---
2title: Migration
3id: interrupts-migration
4order: 6
5description: "Move approval and resume code from the 0.x custom events and raw resume APIs to typed AG-UI interrupts in 1.0."
6keywords:
7 - tanstack ai migration
8 - ag-ui interrupts
9 - addToolApprovalResponse
10 - pendingInterrupts
11 - resumeInterrupts
12---
13​
14In 1.0, tool approval, a generic pause, and client-tool execution are AG-UI interrupts. Resolve each one on the item. `addToolApprovalResponse` and `pendingInterrupts` are removed.
15​
16There is no codemod. Upgrade the server and the client together. For the full
17list of 1.0 changes, see [Upgrade to 1.0](../migration/v1).
18​
19## Quick migration
20​
211. Rename `pendingInterrupts` to `interrupts`.
222. Replace each `addToolApprovalResponse` call with
23 `interrupt.resolveInterrupt(approved)` on the matching item.
243. Replace approval loops with `resolveInterrupts(...)`.
254. On the server, forward `parentRunId` and `resume` into `chat()`.
265. Finish or drop the runs that paused on 0.x. 1.0 cannot resume them.
27​
28The sections below show each step.
29​
30## API mapping
31​
32| 0.x (removed in 1.0) | 1.0 |
33| --- | --- |
34| `pendingInterrupts` | `interrupts` |
35| `ChatClient.getPendingInterrupts()` | `ChatClient.getInterrupts()` |
36| `addToolApprovalResponse({ id, approved })` | Find the bound `tool-approval` item and call `interrupt.resolveInterrupt(approved)`. Removed from every framework and from `ChatClient`. |
37| Raw `resumeInterrupts(entries, state)` | Item methods, or root `resolveInterrupts(...)`. Keep `resumeInterruptsUnsafe` for validated recovery tools. |
38| `interrupt.error` | `interrupt.errors[0]` |
39| `approval-requested` custom event | `RUN_FINISHED` interrupt descriptor, reason `tool_call` |
40| `tool-input-available` custom event | `RUN_FINISHED` interrupt descriptor, reason `tanstack:client_tool_execution` |
41| Boolean denial treated as cancellation | `resolveInterrupt(false)` for a denial. `cancel()` for a cancellation with no payload. |
42​
43These stay the same:
44​
45- `addToolResult` still sends client-tool results. It hands them to the
46 matching native item.
47- `needsApproval` still turns on approval in the tool definition.
48​
49## Single approval
50​
51```ts ignore
52// 0.x
53await addToolApprovalResponse({ id: approval.id, approved: true })
54​
55// 1.0
56const interrupt = interrupts.find(
57 (item) => item.kind === 'tool-approval' && item.toolName === 'transfer',
59if (interrupt?.kind === 'tool-approval' && interrupt.toolName === 'transfer') {
60 interrupt.resolveInterrupt(true)
62```
63​
64A single valid item submits at once. For the full component, see
65[Tool Approval](./tool-approval).
66​
67## Payloads and edits
68​
69In 0.x, a boolean approval had no typed data. In 1.0, add an
70`approvalSchema` to the tool, and send data under `payload`:
71​
72```ts ignore
73interrupt.resolveInterrupt(true, {
74 editedArgs: { amount: 12, recipient: 'Ada' }, // optional, approval only, full replacement
75 payload: { note: 'Reviewed' },
76})
77interrupt.resolveInterrupt(false, { payload: { reason: 'Policy limit' } })
78```
79​
80- Rejection never accepts edits.
81- Custom fields at the top level are not valid.
82- A single `approvalSchema` (not an `{ approve, reject }` map) applies to the
83 selected decision.
84- With no schema, `resolveInterrupt(true)` and `resolveInterrupt(false)` stay
85 valid.
86​
87## Denial and cancellation
88​
89- `resolveInterrupt(false, ...)` continues the model with an explicit rejected
90 decision.
91- `cancel()` emits AG-UI `status: 'cancelled'`. It never validates or selects
92 the reject branch.
93​
94If you called `addToolApprovalResponse({ approved: false })`, use
95`resolveInterrupt(false)`. It is a denial, not a cancellation.
96​
97## Batches
98​
99Native batches are all or nothing. Replace loops over approval ids with one of
100these:
101​
102- Stage each item. The last valid item submits the batch.
103- Answer every item from one synchronous root callback:
104​
105```ts ignore
106resolveInterrupts((interrupt) => {
107 if (interrupt.kind === 'tool-approval') {
108 interrupt.resolveInterrupt(true, { payload: { note: 'Batch review' } })
109 return
110 }
111 interrupt.cancel()
112})
113```
114​
115Other root helpers:
116​
117- `resolveInterrupts(true)` or `resolveInterrupts(false)`: only for batches of
118 approvals with no payload and no edits.
119- `cancelInterrupts()`: cancel every item, with no payload.
120- `clearResolution()`: drop the draft of one item.
121- `retryInterrupts()`: only when every item is still validly staged and the
122 root error is retryable.
123​
124See [Multiple Interrupts](./multiple).
125​
126## Generic responses
127​
128Do not derive a static type from a `responseSchema` that you received. Parse
129the value as `unknown`, convert the schema with `z.fromJSONSchema`, and then
130resolve with the validated value. For typed generic interrupts, use
131`defineInterrupt`. See [Generic Interrupts](./generic).
132​
133## Server events
134​
135A native server emits these events, in this order:
136​
1371. `MESSAGES_SNAPSHOT`
1382. `STATE_SNAPSHOT` (optional)
1393. `RUN_FINISHED` with a nonempty interrupt outcome
140​
141A continuation has:
142​
143- a new `runId`
144- the same `threadId`
145- the interrupted run as `parentRunId`
146- every pending id, exactly once
147​
148Interrupts are **ephemeral**. The server rebuilds and validates the expected
149batch from the submitted history and its current tool definitions. So a
150stateless route needs no persistence. But the batch comes from client input, so
151this mode does not give authoritative recovery, exactly-once delivery, replay
152protection, or restart recovery.
153​
154`resumeInterruptsUnsafe` is a low-level escape hatch. It sends validated raw
155resume entries directly. Do not use it for approval UI.
156​
157## Old streams
158​
1591.0 does not read the `approval-requested` and `tool-input-available` custom
160events. An interrupt descriptor without a binding (only `metadata.kind`) shows
161as `kind: 'unbound'`, and you cannot resume it. Upgrade the server and the
162client together, and finish or drop the runs that paused on 0.x.
163​
164## Before you ship
165​
166The quick migration covers the client. Also do these:
167​
1681. If your server wrote the 0.x custom events itself, replace those writers
169 with the interrupt terminal.
1702. Test expired items and a failed transport.
171​
172When this is done, your approvals run on 1.0 interrupts. Start from the
173[Overview](./overview) for the current flow.