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

migration.md

142 lines5.8 KB

1---
2title: Migration
3id: interrupts-migration
4order: 6
5description: "Migrate approval and resume code from legacy custom events and raw resume APIs to typed, atomic AG-UI interrupts."
6keywords:
7 - tanstack ai migration
8 - ag-ui interrupts
9 - addToolApprovalResponse
10 - pendingInterrupts
11 - resumeInterrupts
12---
13​
14TanStack AI now models approvals, generic pauses, and client-tool execution as
15AG-UI interrupt descriptors. Native runs end with
16`RUN_FINISHED.outcome.type === 'interrupt'`, and the continuation is a new run
17whose `parentRunId` is the interrupted run.
18​
19There is no codemod. Migrate the server lifecycle and client rendering together.
20Legacy readers stay temporarily for old streams but cannot provide the full
21native contract. Start from [Overview](./overview).
22​
23## API mapping
24​
25| Deprecated / legacy | Current |
26| --- | --- |
27| `pendingInterrupts` | `interrupts` (`pendingInterrupts` is a deprecated alias of the same array) |
28| `ChatClient.getPendingInterrupts()` | `ChatClient.getInterrupts()` |
29| `addToolApprovalResponse({ id, approved })` | Find the bound `tool-approval` item, call `interrupt.resolveInterrupt(approved)` |
30| Raw `resumeInterrupts(entries, state)` | Bound item methods or root `resolveInterrupts(...)`; reserve `resumeInterruptsUnsafe` for validated recovery tooling |
31| `approval-requested` custom event | `RUN_FINISHED` interrupt descriptor, reason `tool_call` |
32| `tool-input-available` custom event | `RUN_FINISHED` interrupt descriptor, reason `tanstack:client_tool_execution` |
33| Boolean denial treated as cancellation | `resolveInterrupt(false)` for denial; `cancel()` for payloadless cancellation |
34​
35`addToolResult` is **not** removed. It still handles client-tool results and
36delegates to a matching native item. `needsApproval` remains the tool-definition
37switch for approvals.
38​
39## Single approval
40​
41```ts ignore
42// Before
43await addToolApprovalResponse({ id: approval.id, approved: true })
44​
45// After
46const interrupt = interrupts.find(
47 (item) => item.kind === 'tool-approval' && item.toolName === 'transfer',
49if (interrupt?.kind === 'tool-approval' && interrupt.toolName === 'transfer') {
50 interrupt.resolveInterrupt(true)
52```
53​
54A valid singleton submits automatically. For the full render/resolve component
55see [Tool Approval](./tool-approval).
56​
57## Branch payloads and edits
58​
59Legacy boolean approvals couldn't carry typed data. Add `approvalSchema` and
60resolve the selected branch with data under `payload`:
61​
62```ts ignore
63interrupt.resolveInterrupt(true, {
64 editedArgs: { amount: 12, recipient: 'Ada' }, // optional, approval-only, full replacement
65 payload: { note: 'Reviewed' },
66})
67interrupt.resolveInterrupt(false, { payload: { reason: 'Policy limit' } })
68```
69​
70Rejection never accepts edits; top-level custom fields are invalid. A single
71`approvalSchema` (not `{ approve, reject }`) applies to the selected decision;
72with no schema the boolean shorthand stays valid.
73​
74## Denial vs cancellation
75​
76`resolveInterrupt(false, ...)` continues the model with an explicit rejected
77decision. `cancel()` emits AG-UI `status: 'cancelled'` and never validates or
78selects the reject branch. Deprecated `addToolApprovalResponse({ approved: false })`
79maps to denial, not cancellation.
80​
81## Batches
82​
83Native batches are all-or-nothing. Replace approval-ID loops with staged items
84(the last valid item auto-submits) or one synchronous root callback:
85​
86```ts ignore
87resolveInterrupts((interrupt) => {
88 if (interrupt.kind === 'tool-approval') {
89 interrupt.resolveInterrupt(true, { payload: { note: 'Batch review' } })
90 return
91 }
92 interrupt.cancel()
93})
94```
95​
96`resolveInterrupts(true|false)` is shorthand only for all-approval batches with
97no payload/edits. Use `cancelInterrupts()` for payloadless all-items cancel,
98`clearResolution()` to drop one draft, `retryInterrupts()` only when every item
99is still validly staged and the root error is retryable. See
100[Multiple Interrupts](./multiple).
101​
102## Generic responses
103​
104Don't derive a static type from a received `responseSchema`. Parse as
105`unknown`, convert with `z.fromJSONSchema`, then resolve the validated value.
106Full form example in [Generic Interrupts](./generic).
107​
108## Server events
109​
110A native server emits, in order: `MESSAGES_SNAPSHOT` → optional
111`STATE_SNAPSHOT` → `RUN_FINISHED` with a nonempty interrupt outcome.
112Continuations use a fresh `runId`, the same `threadId`, and the interrupted run
113as `parentRunId`, with every pending ID present exactly once.
114​
115Interrupts run **ephemerally**: the server reconstructs and validates the
116expected batch from the submitted history and its current tool definitions, so
117a stateless route needs no persistence. Because the batch is rebuilt from
118client-provided input, this mode does not provide authoritative recovery,
119exactly-once, replay protection, or restart recovery.
120​
121`resumeInterruptsUnsafe` is a low-level escape hatch for submitting validated
122raw resume entries directly, not the normal target for approval UI.
123​
124## Legacy limits
125​
126Deprecated readers recognize well-formed historical `approval-requested` and
127`tool-input-available` events and convert a fully-covered legacy batch into one
128cloned-history follow-up. They do **not** support edited arguments, custom
129approval payloads, generic responses, payloadless cancellation, or expiry/
130schema-hash reconciliation; those fail with `legacy-unsupported`. Native and
131legacy items cannot mix in one batch; a failed legacy transport keeps staged
132decisions and reports `legacy-submit-failed`.
133​
134## Checklist
135​
1361. Replace native custom-event writers with the interrupt terminal.
1372. Render bound `interrupts` instead of `pendingInterrupts`.
1383. Replace boolean approval helpers with `resolveInterrupt` + explicit
139 denial/cancellation.
1404. Replace approval loops with atomic batch staging or root `resolveInterrupts`.
1415. Keep `addToolResult` for client-tool results where useful.
1426. Test expired items and failed transport before removing legacy support.