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

Scope.md

82 lines2.7 KB

1---
2id: Scope
3title: Scope
4---
5​
6Defined in: [packages/ai/src/scope.ts:23](https://github.com/TanStack/ai/blob/main/packages/ai/src/scope.ts#L23)
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`, and `conversationId` is deprecated in favor
16of it); a subsystem must never introduce a second name (`sessionId`,
17`conversationId`, …) for the same concept.
18​
19## Security
20​
21A `Scope` is an isolation boundary. Derive every field server-side from
22trusted, validated session state — **never** from client input. `threadId`
23in particular resolves from a client-supplied value on the request, so a
24subsystem that reads/writes user-owned data (memory, per-user metadata) must
25pair it with a server-trusted `userId`/`tenantId` and must not treat a bare
26client `threadId` as sufficient isolation: thread ids are guessable, so on
27their own they let one caller read another's data.
28​
29## Properties
30​
31### namespace?
32​
33```ts
34optional namespace?: string;
35```
36​
37Defined in: [packages/ai/src/scope.ts:46](https://github.com/TanStack/ai/blob/main/packages/ai/src/scope.ts#L46)
38​
39Logical partition within a tenant/user (e.g. separating distinct memory
40banks or persistence namespaces). Reserved — no subsystem keys on it yet;
41adapters that don't understand it must ignore it rather than error.
42​
43***
44​
45### tenantId?
46​
47```ts
48optional tenantId?: string;
49```
50​
51Defined in: [packages/ai/src/scope.ts:40](https://github.com/TanStack/ai/blob/main/packages/ai/src/scope.ts#L40)
52​
53Tenant/organization boundary for multi-tenant deployments. When present,
54every read and write must be confined to it.
55​
56***
57​
58### threadId
59​
60```ts
61threadId: string;
62```
63​
64Defined in: [packages/ai/src/scope.ts:29](https://github.com/TanStack/ai/blob/main/packages/ai/src/scope.ts#L29)
65​
66The conversation this data belongs to. Required — the minimal isolation
67key both subsystems already center on. Same concept as
68`ChatMiddlewareContext.threadId`.
69​
70***
71​
72### userId?
73​
74```ts
75optional userId?: string;
76```
77​
78Defined in: [packages/ai/src/scope.ts:35](https://github.com/TanStack/ai/blob/main/packages/ai/src/scope.ts#L35)
79​
80Durable end-user identity, for cross-thread recall and per-user isolation.
81Optional, but required in practice for any multi-user deployment — a
82`threadId` alone is not an authorization boundary (see Security above).