@redis/vercel-ai-sdk 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -35
- package/README.md +5 -6
- package/TOOLS.md +15 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -14
- package/dist/index.js.map +1 -1
- package/dist/instructions.d.ts +1 -1
- package/dist/instructions.d.ts.map +1 -1
- package/dist/instructions.js +2 -2
- package/dist/instructions.js.map +1 -1
- package/dist/namespaces.js +7 -7
- package/dist/namespaces.js.map +1 -1
- package/dist/provider.js +1 -1
- package/dist/provider.js.map +1 -1
- package/dist/schemas.d.ts +13 -34
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +6 -7
- package/dist/schemas.js.map +1 -1
- package/dist/scoped-memory.d.ts +2 -7
- package/dist/scoped-memory.d.ts.map +1 -1
- package/dist/scoped-memory.js +40 -54
- package/dist/scoped-memory.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +21 -15
- package/src/instructions.ts +2 -2
- package/src/namespaces.ts +7 -7
- package/src/provider.ts +1 -1
- package/src/schemas.ts +6 -7
- package/src/scoped-memory.ts +39 -45
package/src/scoped-memory.ts
CHANGED
|
@@ -9,8 +9,6 @@ export interface RedisAgentMemoryScope {
|
|
|
9
9
|
userId?: string;
|
|
10
10
|
/** Locks session operations and associates new memories; recall still spans this user's sessions. */
|
|
11
11
|
sessionId?: string;
|
|
12
|
-
/** Deprecated label; prefer namespaceId for resource-backed namespaces. */
|
|
13
|
-
namespace?: string;
|
|
14
12
|
namespaceId?: string;
|
|
15
13
|
}
|
|
16
14
|
|
|
@@ -21,7 +19,7 @@ export interface ScopeValidationOptions {
|
|
|
21
19
|
export type ScopedMemoryRequestOptions = NonNullable<Parameters<AgentMemory['getSessionMemory']>[2]>;
|
|
22
20
|
type SessionEventInput = z.infer<typeof s.event>;
|
|
23
21
|
type SearchInput = NonNullable<Parameters<AgentMemory['searchLongTermMemory']>[0]>;
|
|
24
|
-
type Placement = {
|
|
22
|
+
type Placement = { namespaceId?: string };
|
|
25
23
|
|
|
26
24
|
export interface AppendSessionEventOptions extends ScopedMemoryRequestOptions {
|
|
27
25
|
/** Reject when the named session already exists. Used by the legacy start_session tool. */
|
|
@@ -59,7 +57,8 @@ export function resolveAgentMemoryClient(
|
|
|
59
57
|
const resolvedStoreId = storeId ?? env.AGENT_MEMORY_STORE_ID;
|
|
60
58
|
if (!key?.trim()) throw new Error('Set AGENT_MEMORY_API_KEY or pass an Iris API key.');
|
|
61
59
|
if (!resolvedStoreId?.trim()) throw new Error('Set AGENT_MEMORY_STORE_ID or pass storeId.');
|
|
62
|
-
const serverURL = serverUrl ?? env.AGENT_MEMORY_URL
|
|
60
|
+
const serverURL = serverUrl ?? env.AGENT_MEMORY_URL;
|
|
61
|
+
if (!serverURL?.trim()) throw new Error('Set AGENT_MEMORY_URL or pass serverUrl; each store has its own endpoint URL.');
|
|
63
62
|
const url = new URL(serverURL);
|
|
64
63
|
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash) {
|
|
65
64
|
throw new Error('serverUrl must be an HTTP(S) URL without credentials, query, or fragment.');
|
|
@@ -68,17 +67,27 @@ export function resolveAgentMemoryClient(
|
|
|
68
67
|
return new AgentMemory({ apiKey: key, storeId: resolvedStoreId, serverURL, timeoutMs });
|
|
69
68
|
}
|
|
70
69
|
|
|
70
|
+
const SCOPE_KEYS: ReadonlySet<string> = new Set(['userId', 'sessionId', 'namespaceId']);
|
|
71
|
+
|
|
72
|
+
/** Unknown keys would be ignored, silently dropping a boundary the caller meant to enforce. */
|
|
73
|
+
function assertKnownScopeKeys(scope: object, label: string): void {
|
|
74
|
+
for (const key of Object.keys(scope)) {
|
|
75
|
+
if (SCOPE_KEYS.has(key)) continue;
|
|
76
|
+
throw new Error(key === 'namespace'
|
|
77
|
+
? `${label}.namespace was removed; use ${label}.namespaceId.`
|
|
78
|
+
: `Unknown ${label} field: ${key}.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
71
82
|
export function copyMemoryScope(
|
|
72
83
|
scope: RedisAgentMemoryScope,
|
|
73
|
-
|
|
84
|
+
_options: ScopeValidationOptions = {},
|
|
74
85
|
): Readonly<RedisAgentMemoryScope> {
|
|
86
|
+
assertKnownScopeKeys(scope, 'scope');
|
|
75
87
|
const copied = Object.freeze({ ...scope });
|
|
76
88
|
if (copied.userId !== undefined) s.ownerId.parse(copied.userId);
|
|
77
89
|
if (copied.sessionId !== undefined) s.sessionId.parse(copied.sessionId);
|
|
78
90
|
if (copied.namespaceId !== undefined) s.id.parse(copied.namespaceId);
|
|
79
|
-
if (copied.namespace !== undefined && !copied.namespace.trim()) throw new Error('scope.namespace must not be empty.');
|
|
80
|
-
if (copied.namespace !== undefined && copied.namespaceId !== undefined) throw new Error('Choose scope.namespace or scope.namespaceId.');
|
|
81
|
-
if (options.namespaceTools && copied.namespace !== undefined) throw new Error('Namespace resource tools cannot use a legacy namespace scope.');
|
|
82
91
|
return copied;
|
|
83
92
|
}
|
|
84
93
|
|
|
@@ -86,8 +95,9 @@ export function extendMemoryScope(
|
|
|
86
95
|
scope: RedisAgentMemoryScope,
|
|
87
96
|
context: RedisAgentMemoryScope,
|
|
88
97
|
): RedisAgentMemoryScope {
|
|
98
|
+
assertKnownScopeKeys(context, 'context scope');
|
|
89
99
|
const extended = { ...scope };
|
|
90
|
-
for (const name of ['userId', 'sessionId', '
|
|
100
|
+
for (const name of ['userId', 'sessionId', 'namespaceId'] as const) {
|
|
91
101
|
const value = context[name];
|
|
92
102
|
if (value === undefined) continue;
|
|
93
103
|
if (scope[name] !== undefined && scope[name] !== value) fail(`Cannot override ${name} scope.`);
|
|
@@ -102,28 +112,18 @@ export function extendMemoryScope(
|
|
|
102
112
|
*/
|
|
103
113
|
export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMemoryScope) {
|
|
104
114
|
const scope = copyMemoryScope(inputScope);
|
|
105
|
-
const hasResourceScope = Boolean(scope.userId || scope.
|
|
115
|
+
const hasResourceScope = Boolean(scope.userId || scope.namespaceId);
|
|
106
116
|
|
|
107
117
|
const checkPlacement = (value: Placement) => {
|
|
108
|
-
if (
|
|
109
|
-
if (scope.namespace !== undefined && value.namespace !== scope.namespace) fail('Resource is outside the configured namespace.');
|
|
110
|
-
if (scope.namespaceId !== undefined && value.namespaceRef?.namespaceId !== scope.namespaceId) {
|
|
118
|
+
if (scope.namespaceId !== undefined && value.namespaceId !== scope.namespaceId) {
|
|
111
119
|
fail('Resource is outside the configured namespace resource.');
|
|
112
120
|
}
|
|
113
121
|
};
|
|
114
122
|
|
|
115
123
|
const place = <T extends Placement>(input: T): T => {
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
if (value.namespaceRef || (value.namespace !== undefined && value.namespace !== scope.namespace)) fail('Cannot override namespace scope.');
|
|
120
|
-
value.namespace = scope.namespace;
|
|
121
|
-
}
|
|
122
|
-
if (scope.namespaceId !== undefined) {
|
|
123
|
-
if (value.namespace !== undefined || (value.namespaceRef && value.namespaceRef.namespaceId !== scope.namespaceId)) fail('Cannot override namespace scope.');
|
|
124
|
-
value.namespaceRef = { namespaceId: scope.namespaceId };
|
|
125
|
-
}
|
|
126
|
-
return value;
|
|
124
|
+
if (scope.namespaceId === undefined) return { ...input };
|
|
125
|
+
if (input.namespaceId !== undefined && input.namespaceId !== scope.namespaceId) fail('Cannot override namespace scope.');
|
|
126
|
+
return { ...input, namespaceId: scope.namespaceId };
|
|
127
127
|
};
|
|
128
128
|
|
|
129
129
|
const resolveSession = (value?: string) => {
|
|
@@ -164,10 +164,10 @@ export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMe
|
|
|
164
164
|
};
|
|
165
165
|
|
|
166
166
|
const assertPlacementOwner = async (value: Placement, options?: ScopedMemoryRequestOptions) => {
|
|
167
|
-
if (!scope.userId || !value.
|
|
167
|
+
if (!scope.userId || !value.namespaceId) return;
|
|
168
168
|
const signal = options?.signal ?? undefined;
|
|
169
169
|
throwIfAborted(signal);
|
|
170
|
-
const { namespace } = await client.getNamespace(value.
|
|
170
|
+
const { namespace } = await client.getNamespace(value.namespaceId, options);
|
|
171
171
|
throwIfAborted(signal);
|
|
172
172
|
const root = namespace.rootId === namespace.namespaceId ? namespace
|
|
173
173
|
: (await client.getNamespace(namespace.rootId, options)).namespace;
|
|
@@ -179,12 +179,11 @@ export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMe
|
|
|
179
179
|
|
|
180
180
|
const scopeSearch = (input: SearchInput): SearchInput => {
|
|
181
181
|
const filter: LongTermMemoryFilter = { ...input.filter };
|
|
182
|
-
if (filter.
|
|
183
|
-
if (filter.unnamespaced !== undefined && (filter.namespace || filter.namespaceRef)) {
|
|
182
|
+
if (filter.unnamespaced !== undefined && filter.namespaceId) {
|
|
184
183
|
fail('unnamespaced cannot be combined with namespace selectors.');
|
|
185
184
|
}
|
|
186
|
-
if (hasResourceScope &&
|
|
187
|
-
const fixed = (name: 'ownerId' | '
|
|
185
|
+
if (hasResourceScope && filter.op?.toUpperCase() === 'ANY') fail('Scoped searches require filter.op ALL to preserve isolation.');
|
|
186
|
+
const fixed = (name: 'ownerId' | 'namespaceId', value: string) => {
|
|
188
187
|
const existing = filter[name];
|
|
189
188
|
if (existing && (existing.eq !== value || Object.values(existing).filter(item => item !== undefined).length !== 1)) {
|
|
190
189
|
fail(`Cannot override ${name} scope.`);
|
|
@@ -192,17 +191,11 @@ export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMe
|
|
|
192
191
|
filter[name] = { eq: value };
|
|
193
192
|
};
|
|
194
193
|
if (scope.userId) fixed('ownerId', scope.userId);
|
|
195
|
-
if (scope.
|
|
194
|
+
if (scope.namespaceId) {
|
|
196
195
|
if (filter.unnamespaced !== undefined) fail('Cannot search unnamespaced records within a namespace scope.');
|
|
197
|
-
|
|
198
|
-
if (filter.namespaceRef) fail('Cannot override namespace scope.');
|
|
199
|
-
fixed('namespace', scope.namespace);
|
|
200
|
-
} else {
|
|
201
|
-
if (filter.namespace) fail('Cannot override namespace scope.');
|
|
202
|
-
fixed('namespaceRef', scope.namespaceId!);
|
|
203
|
-
}
|
|
196
|
+
fixed('namespaceId', scope.namespaceId);
|
|
204
197
|
}
|
|
205
|
-
return { ...input, filter
|
|
198
|
+
return { ...input, filter: hasResourceScope ? { ...filter, op: 'ALL' } : filter };
|
|
206
199
|
};
|
|
207
200
|
|
|
208
201
|
const searchLongTermMemory = (
|
|
@@ -215,7 +208,7 @@ export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMe
|
|
|
215
208
|
limit: number,
|
|
216
209
|
options?: ScopedMemoryRequestOptions,
|
|
217
210
|
) => {
|
|
218
|
-
const result = await searchLongTermMemory({ text, limit }, options);
|
|
211
|
+
const result = await searchLongTermMemory({ query: text, limit }, options);
|
|
219
212
|
for (const memory of result.items) {
|
|
220
213
|
if (scope.userId && memory.ownerId !== scope.userId) fail('Memory is outside the configured user scope.');
|
|
221
214
|
checkPlacement(memory);
|
|
@@ -257,11 +250,12 @@ export function createScopedMemory(client: AgentMemory, inputScope: RedisAgentMe
|
|
|
257
250
|
await assertPlacementOwner(placed, requestOptions);
|
|
258
251
|
throwIfAborted(requestSignal);
|
|
259
252
|
onPostStatus?.('started');
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
253
|
+
const { sessionId: _sessionId, ...eventFields } = placed;
|
|
254
|
+
const body = { ...eventFields, createdAt: input.createdAt ? new Date(input.createdAt) : new Date() };
|
|
255
|
+
// createSession refuses an existing ID with 409; addSessionEvent appends and creates a missing session.
|
|
256
|
+
const result = sessionId && !start
|
|
257
|
+
? await client.addSessionEvent(sessionId, body, requestOptions)
|
|
258
|
+
: await client.createSession({ ...body, sessionId }, requestOptions);
|
|
265
259
|
onPostStatus?.('acknowledged');
|
|
266
260
|
if (validateResponse && (
|
|
267
261
|
result.event.sessionId !== sessionId || result.event.actorId !== input.actorId || result.event.role !== input.role
|