Open History to see the last commit
1---
2id: Scope
3title: Scope
4---
5
6Defined in: packages/ai/src/scope.ts:22
7
8Shared identity/isolation scope for the TanStack AI subsystems that persist
9or recall per-conversation data — `@tanstack/ai-persistence` (keyed CRUD over
10threads, runs, interrupts) and `@tanstack/ai-memory` (ranked recall/save).
11
12Both subsystems answer the same underlying question — "whose data is this?" —
13so they share ONE identity vocabulary defined here rather than each inventing
14its own. `threadId` is the single conversation key across the codebase (it is
15`ChatMiddlewareContext.threadId`); a subsystem must never introduce a second
16name (`sessionId`, `conversationId`, …) for the same concept.
17
18## Security
19
20A `Scope` is an isolation boundary. Derive every field server-side from
21trusted, validated session state — **never** from client input. `threadId`
22in particular resolves from a client-supplied value on the request, so a
23subsystem that reads/writes user-owned data (memory, per-user metadata) must
24pair it with a server-trusted `userId`/`tenantId` and must not treat a bare
25client `threadId` as sufficient isolation: thread ids are guessable, so on
26their own they let one caller read another's data.
27
28## Properties
29
30### namespace?
31
32```ts
33optional namespace?: string;
34```
35
36Defined in: packages/ai/src/scope.ts:45
37
38Logical partition within a tenant/user (e.g. separating distinct memory
39banks or persistence namespaces). Reserved — no subsystem keys on it yet;
40adapters that don't understand it must ignore it rather than error.
41
42***
43
44### tenantId?
45
46```ts
47optional tenantId?: string;
48```
49
50Defined in: packages/ai/src/scope.ts:39
51
52Tenant/organization boundary for multi-tenant deployments. When present,
53every read and write must be confined to it.
54
55***
56
57### threadId
58
59```ts
60threadId: string;
61```
62
63Defined in: packages/ai/src/scope.ts:28
64
65The conversation this data belongs to. Required — the minimal isolation
66key both subsystems already center on. Same concept as
67`ChatMiddlewareContext.threadId`.
68
69***
70
71### userId?
72
73```ts
74optional userId?: string;
75```
76
77Defined in: packages/ai/src/scope.ts:34
78
79Durable end-user identity, for cross-thread recall and per-user isolation.
80Optional, but required in practice for any multi-user deployment — a
81`threadId` alone is not an authorization boundary (see Security above).