@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.
@@ -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 = { namespace?: string; namespaceRef?: { namespaceId: string } };
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 ?? 'https://gcp-us-east4.memory.redis.io';
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
- options: ScopeValidationOptions = {},
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', 'namespace', 'namespaceId'] as const) {
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.namespace !== undefined || scope.namespaceId);
115
+ const hasResourceScope = Boolean(scope.userId || scope.namespaceId);
106
116
 
107
117
  const checkPlacement = (value: Placement) => {
108
- if (value.namespace !== undefined && value.namespaceRef !== undefined) fail('Supply namespace or namespaceRef, never both.');
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
- const value = { ...input };
117
- if (value.namespace !== undefined && value.namespaceRef !== undefined) fail('Supply namespace or namespaceRef, never both.');
118
- if (scope.namespace !== undefined) {
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.namespaceRef) return;
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.namespaceRef.namespaceId, options);
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.namespace && filter.namespaceRef) fail('Supply namespace or namespaceRef, never both.');
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 && input.filterOp === 'any') fail('Scoped searches require filterOp all to preserve isolation.');
187
- const fixed = (name: 'ownerId' | 'namespace' | 'namespaceRef', value: string) => {
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.namespace !== undefined || scope.namespaceId) {
194
+ if (scope.namespaceId) {
196
195
  if (filter.unnamespaced !== undefined) fail('Cannot search unnamespaced records within a namespace scope.');
197
- if (scope.namespace !== undefined) {
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, filterOp: hasResourceScope ? 'all' : input.filterOp };
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 result = await client.addSessionEvent({
261
- ...placed,
262
- sessionId,
263
- createdAt: input.createdAt ? new Date(input.createdAt) : new Date(),
264
- }, requestOptions);
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