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

controls.md

115 lines4.3 KB

1---
2title: Persistence Controls
3id: controls
4---
5​
6Persistence has no feature flags. What you persist is decided by which **state**
7stores the backend provides, and you compose backends per store. Supply only the
8stores your workflow needs.
9​
10Need a mutex across instances? See [Locks](#locks-coordination) below.
11​
12## Named shapes (prefer these)
13​
14| Type | Required stores | Use |
15| --- | --- | --- |
16| `ChatTranscriptStores` / `ChatTranscriptPersistence` | `messages` (optional runs/interrupts/metadata) | Floor for `withPersistence` / `reconstructChat` |
17| `ChatPersistenceStores` / `ChatPersistence` | `messages` + `runs` + `interrupts` + `metadata` | Packaged backends (`memoryPersistence`, Drizzle, Prisma, D1) |
18| `ChatWithInterruptsStores` / `ChatWithInterruptsPersistence` | `messages` + `runs` + `interrupts` | HITL without requiring metadata |
19​
20There is no public sparse `AIPersistenceStores` export, so use a named shape or
21`AIPersistence<{ messages: MessageStore, … }>` for custom maps.
22`defineAIPersistence` / `composePersistence` still accept sparse maps by
23inference.
24​
25## What each state store gives you
26​
27| Requirement | Store |
28| --- | --- |
29| Authoritative server transcript | `messages` (**required** by `withPersistence` / `reconstructChat`) |
30| Run status and usage | `runs` (required on `ChatPersistence`; required when `interrupts` is set) |
31| Durable approvals or human input | `interrupts` (requires `runs`) |
32| App or integration checkpoints | `metadata` (always optional) |
33​
34`withPersistence(persistence)` inspects the stores that are present. Store
35presence is the capability selection mechanism for optional chat features.
36​
37## Entrypoint requirements
38​
39| Entrypoint | Shape | Notes |
40| --- | --- | --- |
41| `withPersistence` | `ChatTranscriptStores` floor | `interrupts` ⇒ `runs` |
42| `reconstructChat` | `ChatTranscriptStores` | `runs` / `interrupts` enrich the response when present |
43| Packaged `*Persistence()` | `ChatPersistence` | messages + runs (+ interrupts + metadata) |
44| `defineAIPersistence` / `composePersistence` | sparse by inference | Prefer a named shape for the result |
45​
46## Compose and override stores
47​
48`composePersistence` takes the base backend first and an overrides object
49second. Here it starts from the in-memory reference backend and swaps in custom
50`interrupts` / `runs` stores:
51​
52```ts
53import { composePersistence, memoryPersistence } from '@tanstack/ai-persistence'
54// Your own store implementations of the InterruptStore / RunStore contracts.
55import { interruptStore, runStore } from './stores'
56​
57const persistence = composePersistence(memoryPersistence(), {
58 overrides: {
59 interrupts: interruptStore,
60 runs: runStore,
61 },
62})
63```
64​
65Each override is independent:
66​
67| Override value | Result |
68| --- | --- |
69| key omitted | Inherit the base store. |
70| `undefined` | Inherit the base store. |
71| a store object | Replace that store only. |
72| `false` | Remove that store. |
73​
74```ts
75import { composePersistence, memoryPersistence } from '@tanstack/ai-persistence'
76​
77// Drop metadata; the resulting type has no `metadata` key.
78const withoutMetadata = composePersistence(memoryPersistence(), {
79 overrides: { metadata: false },
80})
81```
82​
83Unknown store names fail type checking, and are also rejected at runtime when
84values arrive from untyped JavaScript.
85​
86## Valid store combinations
87​
88- `withPersistence` requires `messages`.
89- `interrupts` requires `runs`: an interrupt record is scoped to a run.
90- `withGenerationPersistence` requires `generationRuns`.
91- Portable sandbox snapshots require `messages`, `artifacts`, and `blobs` on
92 the same persistence object. See
93 [Keep Files After Reload](../sandbox/portable-snapshots-configure).
94​
95To define a partial backend directly rather than by composing, use
96`defineAIPersistence({ stores: { ... } })` and pass only the stores you have.
97See the
98[store reference](./store-reference)
99for the store contracts.
100​
101## Locks (coordination)
102​
103Locks coordinate work across instances (a distributed mutex). They live in
104`@tanstack/ai/locks` and apply as their own middleware with `withLocks`,
105alongside `withPersistence`. Full guide: [Locks](../advanced/locks).
106​
107```ts
108import { withLocks, InMemoryLockStore } from '@tanstack/ai/locks'
109import { withPersistence, memoryPersistence } from '@tanstack/ai-persistence'
110​
111const middleware = [
112 withPersistence(memoryPersistence()),
113 withLocks(new InMemoryLockStore()), // multi-instance: distributed LockStore
115```