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

InMemoryRunStore.md

268 lines6.1 KB

1---
2id: InMemoryRunStore
3title: InMemoryRunStore
4---
5​
6Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:423
7​
8In-memory [RunStore](../interfaces/RunStore.md). Single process only.
9​
10## Implements
11​
12- [`RunStore`](../interfaces/RunStore.md)
13​
14## Constructors
15​
16### Constructor
17​
18```ts
19new InMemoryRunStore(): InMemoryRunStore;
20```
21​
22#### Returns
23​
24`InMemoryRunStore`
25​
26## Methods
27​
28### createOrResume()
29​
30```ts
31createOrResume(input): Promise<RunRecord>;
32```
33​
34Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:426
35​
36Create a run record, or return the existing one unchanged if `runId` is
37already present.
38​
39INVARIANT (idempotency): an existing record is returned **unchanged** and
40the passed `threadId`, `startedAt`, `status`, `parentRunId`,
41`subagentRunId`, and `name` are ignored. This is what makes resuming a
42run safe. `status` defaults to `'running'` on first creation. The three
43link fields are copied only on the first insert.
44​
45#### Parameters
46​
47##### input
48​
49`Pick`\<[`RunRecord`](../interfaces/RunRecord.md), `"threadId"` \| `"runId"` \| `"startedAt"`\> & `object` & `Partial`\<`Pick`\<[`RunRecord`](../interfaces/RunRecord.md), `"activity"` \| `"kind"` \| `"agent"` \| `"principal"`\>\>
50​
51#### Returns
52​
53`Promise`\<[`RunRecord`](../interfaces/RunRecord.md)\>
54​
55#### Implementation of
56​
57[`RunStore`](../interfaces/RunStore.md).[`createOrResume`](../interfaces/RunStore.md#createorresume)
58​
59***
60​
61### findActiveRun()
62​
63```ts
64findActiveRun(threadId): Promise<RunRecord | null>;
65```
66​
67Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:493
68​
69The most recent `'running'` run for `threadId`, or `null` if none is active.
70​
71REQUIRED. This resolves "does this thread have a live run to attach to?"
72from the STABLE thread id, which is the durable basis for reconnecting a
73client (a reload, or the same thread opened on another device) — independent
74of the ephemeral run id, which a single turn may mint several of. When more
75than one run is `'running'`, the one with the greatest `startedAt` wins.
76​
77A backend that stubs this to `null` turns reconnect off silently, because
78`null` is also the correct answer for an idle thread. A backend with no run
79lifecycle at all should omit the whole `runs` store instead — capability
80tiers belong at the store level, not the method level.
81​
82#### Parameters
83​
84##### threadId
85​
86`string`
87​
88#### Returns
89​
90`Promise`\<[`RunRecord`](../interfaces/RunRecord.md) \| `null`\>
91​
92#### Implementation of
93​
94[`RunStore`](../interfaces/RunStore.md).[`findActiveRun`](../interfaces/RunStore.md#findactiverun)
95​
96***
97​
98### get()
99​
100```ts
101get(runId): Promise<RunRecord | null>;
102```
103​
104Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:461
105​
106Current record, or null when unknown.
107​
108#### Parameters
109​
110##### runId
111​
112`string`
113​
114#### Returns
115​
116`Promise`\<[`RunRecord`](../interfaces/RunRecord.md) \| `null`\>
117​
118#### Implementation of
119​
120[`RunStore`](../interfaces/RunStore.md).[`get`](../interfaces/RunStore.md#get)
121​
122***
123​
124### listByParentRun()
125​
126```ts
127listByParentRun(parentRunId): Promise<RunRecord[]>;
128```
129​
130Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:472
131​
132Child runs started by `parentRunId`, ascending by `startedAt`.
133OPTIONAL. `reconstructChat` uses this to put subagent cards back
134on the parent assistant message. A store that omits it reloads the
135text and not the cards.
136​
137#### Parameters
138​
139##### parentRunId
140​
141`string`
142​
143#### Returns
144​
145`Promise`\<[`RunRecord`](../interfaces/RunRecord.md)[]\>
146​
147#### Implementation of
148​
149[`RunStore`](../interfaces/RunStore.md).[`listByParentRun`](../interfaces/RunStore.md#listbyparentrun)
150​
151***
152​
153### listByThread()
154​
155```ts
156listByThread(threadId): Promise<RunRecord[]>;
157```
158​
159Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:465
160​
161Every run in a conversation, ascending by `startedAt`. OPTIONAL.
162`reconstructChat` calls it to find the parent runs of children that a
163tool call started. Without it those cards stay absent on reload.
164Consumers feature-detect.
165​
166#### Parameters
167​
168##### threadId
169​
170`string`
171​
172#### Returns
173​
174`Promise`\<[`RunRecord`](../interfaces/RunRecord.md)[]\>
175​
176#### Implementation of
177​
178[`RunStore`](../interfaces/RunStore.md).[`listByThread`](../interfaces/RunStore.md#listbythread)
179​
180***
181​
182### listReclaimable()
183​
184```ts
185listReclaimable(opts): Promise<RunRecord[]>;
186```
187​
188Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:479
189​
190Runs that may be reclaimed: ALL THREE of `status === 'running'`,
191`detachedSince` is set, and `detachedSince <= now - ttlMs`. The cutoff is
192**inclusive** — a run detached at exactly `now - ttlMs` IS reclaimable.
193​
194OPTIONAL: only needed by a reaper. Consumers feature-detect.
195​
196`detachedSince` is populated by `withSandbox`'s detach path (see
197[RunRecord.detachedSince](../interfaces/RunRecord.md#detachedsince)). The sweep over the candidates this
198surfaces is `@tanstack/ai-sandbox`'s `reapDetachedRuns`: it finalizes a run
199whose agent already finished, expires one past its TTL, and reclaims the
200sandbox. That is a function, not a scheduler — the application invokes it
201(cron, queue, `alarm()`, `waitUntil`) — and a backend that omits this
202method cannot be reaped at all.
203​
204#### Parameters
205​
206##### opts
207​
208###### now
209​
210`number`
211​
212###### ttlMs
213​
214`number`
215​
216#### Returns
217​
218`Promise`\<[`RunRecord`](../interfaces/RunRecord.md)[]\>
219​
220#### Implementation of
221​
222[`RunStore`](../interfaces/RunStore.md).[`listReclaimable`](../interfaces/RunStore.md#listreclaimable)
223​
224***
225​
226### update()
227​
228```ts
229update(runId, patch): Promise<void>;
230```
231​
232Defined in: packages/ai/src/activities/chat/middleware/run-store.ts:452
233​
234Patch a record's mutable fields.
235​
236INVARIANT: updating an unknown `runId` is a **no-op** — it must not throw
237and must not create a record.
238​
239#### Parameters
240​
241##### runId
242​
243`string`
244​
245##### patch
246​
247`Partial`\<`Pick`\<[`RunRecord`](../interfaces/RunRecord.md),
248 \| `"error"`
249 \| `"status"`
250 \| `"result"`
251 \| `"usage"`
252 \| `"finishedAt"`
253 \| `"sandboxKey"`
254 \| `"detachedSince"`
255 \| `"cancelRequested"`
256 \| `"driverEpoch"`
257 \| `"artifacts"`
258 \| `"leaseOwner"`
259 \| `"leaseExpiresAt"`
260 \| `"checkpoint"`\>\>
261​
262#### Returns
263​
264`Promise`\<`void`\>
265​
266#### Implementation of
267​
268[`RunStore`](../interfaces/RunStore.md).[`update`](../interfaces/RunStore.md#update)