@vectorize-io/hindsight-ai-sdk 0.4.10 → 0.4.12

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.
@@ -8,6 +8,15 @@ export declare const BudgetSchema: z.ZodEnum<{
8
8
  high: "high";
9
9
  }>;
10
10
  export type Budget = z.infer<typeof BudgetSchema>;
11
+ /**
12
+ * Fact types for filtering recall results.
13
+ */
14
+ export declare const FactTypeSchema: z.ZodEnum<{
15
+ world: "world";
16
+ experience: "experience";
17
+ observation: "observation";
18
+ }>;
19
+ export type FactType = z.infer<typeof FactTypeSchema>;
11
20
  /**
12
21
  * Recall result item from Hindsight
13
22
  */
@@ -100,14 +109,6 @@ export interface MentalModelResponse {
100
109
  updated_at: string;
101
110
  trigger?: MentalModelTrigger;
102
111
  }
103
- /**
104
- * Create mental model response from Hindsight
105
- */
106
- export interface CreateMentalModelResponse {
107
- mental_model_id: string;
108
- bank_id: string;
109
- created_at: string;
110
- }
111
112
  /**
112
113
  * Document response from Hindsight
113
114
  */
@@ -121,34 +122,6 @@ export interface DocumentResponse {
121
122
  memory_unit_count: number;
122
123
  tags?: string[];
123
124
  }
124
- /**
125
- * Directive response from Hindsight
126
- */
127
- export interface DirectiveResponse {
128
- id: string;
129
- bank_id: string;
130
- name: string;
131
- content: string;
132
- priority: number;
133
- is_active: boolean;
134
- tags: string[];
135
- created_at: string;
136
- updated_at: string;
137
- }
138
- /**
139
- * Create directive response from Hindsight
140
- */
141
- export interface CreateDirectiveResponse {
142
- id: string;
143
- bank_id: string;
144
- name: string;
145
- content: string;
146
- priority: number;
147
- is_active: boolean;
148
- tags: string[];
149
- created_at: string;
150
- updated_at: string;
151
- }
152
125
  /**
153
126
  * Hindsight client interface - matches @vectorize-io/hindsight-client
154
127
  */
@@ -162,7 +135,7 @@ export interface HindsightClient {
162
135
  async?: boolean;
163
136
  }): Promise<RetainResponse>;
164
137
  recall(bankId: string, query: string, options?: {
165
- types?: string[];
138
+ types?: FactType[];
166
139
  maxTokens?: number;
167
140
  budget?: Budget;
168
141
  trace?: boolean;
@@ -175,134 +148,110 @@ export interface HindsightClient {
175
148
  reflect(bankId: string, query: string, options?: {
176
149
  context?: string;
177
150
  budget?: Budget;
178
- }): Promise<ReflectResponse>;
179
- createMentalModel(bankId: string, options?: {
180
- id?: string;
181
- name?: string;
182
- sourceQuery?: string;
183
- tags?: string[];
184
151
  maxTokens?: number;
185
- trigger?: MentalModelTrigger;
186
- }): Promise<CreateMentalModelResponse>;
152
+ }): Promise<ReflectResponse>;
187
153
  getMentalModel(bankId: string, mentalModelId: string): Promise<MentalModelResponse>;
188
154
  getDocument(bankId: string, documentId: string): Promise<DocumentResponse | null>;
189
- createDirective(bankId: string, options: {
190
- name: string;
191
- content: string;
192
- priority?: number;
193
- isActive?: boolean;
194
- tags?: string[];
195
- }): Promise<CreateDirectiveResponse>;
196
- listDirectives(bankId: string, options?: {
197
- tags?: string[];
198
- tagsMatch?: 'any' | 'all' | 'exact';
199
- activeOnly?: boolean;
200
- limit?: number;
201
- offset?: number;
202
- }): Promise<{
203
- directives: DirectiveResponse[];
204
- total: number;
205
- }>;
206
155
  }
207
156
  export interface HindsightToolsOptions {
208
157
  /** Hindsight client instance */
209
158
  client: HindsightClient;
210
- /**
211
- * Custom description for the retain tool.
212
- */
213
- retainDescription?: string;
214
- /**
215
- * Custom description for the recall tool.
216
- */
217
- recallDescription?: string;
218
- /**
219
- * Custom description for the reflect tool.
220
- */
221
- reflectDescription?: string;
222
- /**
223
- * Custom description for the createMentalModel tool.
224
- */
225
- createMentalModelDescription?: string;
226
- /**
227
- * Custom description for the queryMentalModel tool.
228
- */
229
- queryMentalModelDescription?: string;
230
- /**
231
- * Custom description for the getDocument tool.
232
- */
233
- getDocumentDescription?: string;
159
+ /** Memory bank ID to use for all tool calls (e.g. the user ID) */
160
+ bankId: string;
161
+ /** Options for the retain tool */
162
+ retain?: {
163
+ /** Fire-and-forget retain without waiting for completion (default: false) */
164
+ async?: boolean;
165
+ /** Tags always attached to every retained memory (default: undefined) */
166
+ tags?: string[];
167
+ /** Metadata always attached to every retained memory (default: undefined) */
168
+ metadata?: Record<string, string>;
169
+ /** Custom tool description */
170
+ description?: string;
171
+ };
172
+ /** Options for the recall tool */
173
+ recall?: {
174
+ /** Restrict results to these fact types: 'world', 'experience', 'observation' (default: undefined = all types) */
175
+ types?: FactType[];
176
+ /** Maximum tokens to return (default: undefined = API default) */
177
+ maxTokens?: number;
178
+ /** Processing budget controlling latency vs. depth (default: 'mid') */
179
+ budget?: Budget;
180
+ /** Include entity observations in results (default: false) */
181
+ includeEntities?: boolean;
182
+ /** Include raw source chunks in results (default: false) */
183
+ includeChunks?: boolean;
184
+ /** Custom tool description */
185
+ description?: string;
186
+ };
187
+ /** Options for the reflect tool */
188
+ reflect?: {
189
+ /** Processing budget controlling latency vs. depth (default: 'mid') */
190
+ budget?: Budget;
191
+ /** Maximum tokens for the response (default: undefined = API default) */
192
+ maxTokens?: number;
193
+ /** Custom tool description */
194
+ description?: string;
195
+ };
196
+ /** Options for the getMentalModel tool */
197
+ getMentalModel?: {
198
+ /** Custom tool description */
199
+ description?: string;
200
+ };
201
+ /** Options for the getDocument tool */
202
+ getDocument?: {
203
+ /** Custom tool description */
204
+ description?: string;
205
+ };
234
206
  }
235
207
  /**
236
208
  * Creates AI SDK tools for Hindsight memory operations.
237
209
  *
238
- * Features:
239
- * - Dynamic bank ID per call (supports multi-user/multi-bank scenarios)
240
- * - Full API parameter support for retain, recall, and reflect
241
- * - Ready to use with streamText, generateText, or ToolLoopAgent
210
+ * The bank ID and all infrastructure concerns (budget, tags, async mode, etc.)
211
+ * are fixed at creation time. The agent only controls semantic inputs:
212
+ * content, queries, names, and timestamps.
242
213
  *
243
214
  * @example
244
215
  * ```ts
245
216
  * const tools = createHindsightTools({
246
217
  * client: hindsightClient,
218
+ * bankId: userId,
219
+ * recall: { budget: 'high', includeEntities: true },
220
+ * retain: { async: true, tags: ['env:prod'] },
247
221
  * });
248
222
  *
249
- * // Use with AI SDK
250
223
  * const result = await generateText({
251
- * model: openai('gpt-4'),
224
+ * model: openai('gpt-4o'),
252
225
  * tools,
253
- * prompt: 'Remember that Alice loves hiking',
226
+ * messages,
254
227
  * });
255
228
  * ```
256
229
  */
257
- export declare function createHindsightTools({ client, retainDescription, recallDescription, reflectDescription, createMentalModelDescription, queryMentalModelDescription, getDocumentDescription, }: HindsightToolsOptions): {
230
+ export declare function createHindsightTools({ client, bankId, retain: retainOpts, recall: recallOpts, reflect: reflectOpts, getMentalModel: getMentalModelOpts, getDocument: getDocumentOpts, }: HindsightToolsOptions): {
258
231
  retain: import("ai").Tool<{
259
- bankId: string;
260
232
  content: string;
261
233
  documentId?: string | undefined;
262
234
  timestamp?: string | undefined;
263
235
  context?: string | undefined;
264
- tags?: string[] | undefined;
265
- metadata?: Record<string, string> | undefined;
266
236
  }, {
267
237
  success: boolean;
268
238
  itemsCount: number;
269
239
  }>;
270
240
  recall: import("ai").Tool<{
271
- bankId: string;
272
241
  query: string;
273
- types?: string[] | undefined;
274
- maxTokens?: number | undefined;
275
- budget?: "low" | "mid" | "high" | undefined;
276
242
  queryTimestamp?: string | undefined;
277
- includeEntities?: boolean | undefined;
278
- includeChunks?: boolean | undefined;
279
243
  }, {
280
244
  results: RecallResult[];
281
245
  entities?: Record<string, EntityState> | null;
282
246
  }>;
283
247
  reflect: import("ai").Tool<{
284
- bankId: string;
285
248
  query: string;
286
249
  context?: string | undefined;
287
- budget?: "low" | "mid" | "high" | undefined;
288
250
  }, {
289
251
  text: string;
290
252
  basedOn?: ReflectFact[];
291
253
  }>;
292
- createMentalModel: import("ai").Tool<{
293
- bankId: string;
294
- mentalModelId?: string | undefined;
295
- name?: string | undefined;
296
- sourceQuery?: string | undefined;
297
- tags?: string[] | undefined;
298
- maxTokens?: number | undefined;
299
- autoRefresh?: boolean | undefined;
300
- }, {
301
- mentalModelId: string;
302
- createdAt: string;
303
- }>;
304
- queryMentalModel: import("ai").Tool<{
305
- bankId: string;
254
+ getMentalModel: import("ai").Tool<{
306
255
  mentalModelId: string;
307
256
  }, {
308
257
  content: string;
@@ -310,7 +259,6 @@ export declare function createHindsightTools({ client, retainDescription, recall
310
259
  updatedAt: string;
311
260
  }>;
312
261
  getDocument: import("ai").Tool<{
313
- bankId: string;
314
262
  documentId: string;
315
263
  }, {
316
264
  originalText: string;
@@ -318,19 +266,5 @@ export declare function createHindsightTools({ client, retainDescription, recall
318
266
  createdAt: string;
319
267
  updatedAt: string;
320
268
  } | null>;
321
- createDirective: import("ai").Tool<{
322
- bankId: string;
323
- name: string;
324
- content: string;
325
- priority?: number | undefined;
326
- isActive?: boolean | undefined;
327
- tags?: string[] | undefined;
328
- }, {
329
- id: string;
330
- name: string;
331
- content: string;
332
- tags: string[];
333
- createdAt: string;
334
- }>;
335
269
  };
336
270
  export type HindsightTools = ReturnType<typeof createHindsightTools>;
@@ -4,113 +4,91 @@ import { z } from 'zod';
4
4
  * Budget levels for recall/reflect operations.
5
5
  */
6
6
  export const BudgetSchema = z.enum(['low', 'mid', 'high']);
7
+ /**
8
+ * Fact types for filtering recall results.
9
+ */
10
+ export const FactTypeSchema = z.enum(['world', 'experience', 'observation']);
7
11
  /**
8
12
  * Creates AI SDK tools for Hindsight memory operations.
9
13
  *
10
- * Features:
11
- * - Dynamic bank ID per call (supports multi-user/multi-bank scenarios)
12
- * - Full API parameter support for retain, recall, and reflect
13
- * - Ready to use with streamText, generateText, or ToolLoopAgent
14
+ * The bank ID and all infrastructure concerns (budget, tags, async mode, etc.)
15
+ * are fixed at creation time. The agent only controls semantic inputs:
16
+ * content, queries, names, and timestamps.
14
17
  *
15
18
  * @example
16
19
  * ```ts
17
20
  * const tools = createHindsightTools({
18
21
  * client: hindsightClient,
22
+ * bankId: userId,
23
+ * recall: { budget: 'high', includeEntities: true },
24
+ * retain: { async: true, tags: ['env:prod'] },
19
25
  * });
20
26
  *
21
- * // Use with AI SDK
22
27
  * const result = await generateText({
23
- * model: openai('gpt-4'),
28
+ * model: openai('gpt-4o'),
24
29
  * tools,
25
- * prompt: 'Remember that Alice loves hiking',
30
+ * messages,
26
31
  * });
27
32
  * ```
28
33
  */
29
- export function createHindsightTools({ client, retainDescription, recallDescription, reflectDescription, createMentalModelDescription, queryMentalModelDescription, getDocumentDescription, }) {
34
+ export function createHindsightTools({ client, bankId, retain: retainOpts = {}, recall: recallOpts = {}, reflect: reflectOpts = {}, getMentalModel: getMentalModelOpts = {}, getDocument: getDocumentOpts = {}, }) {
35
+ // Agent-controlled params only: content, timestamp, documentId, context
30
36
  const retainParams = z.object({
31
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
32
37
  content: z.string().describe('Content to store in memory'),
33
38
  documentId: z.string().optional().describe('Optional document ID for grouping/upserting content'),
34
39
  timestamp: z.string().optional().describe('Optional ISO timestamp for when the memory occurred'),
35
40
  context: z.string().optional().describe('Optional context about the memory'),
36
- tags: z.array(z.string()).optional().describe('Optional tags for visibility scoping'),
37
- metadata: z.record(z.string(), z.string()).optional().describe('Optional user-defined metadata'),
38
41
  });
42
+ // Agent-controlled params only: query, queryTimestamp
39
43
  const recallParams = z.object({
40
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
41
44
  query: z.string().describe('What to search for in memory'),
42
- types: z.array(z.string()).optional().describe('Filter by fact types'),
43
- maxTokens: z.number().optional().describe('Maximum tokens to return'),
44
- budget: BudgetSchema.optional().describe('Processing budget: low, mid, or high'),
45
45
  queryTimestamp: z.string().optional().describe('Query from a specific point in time (ISO format)'),
46
- includeEntities: z.boolean().optional().describe('Include entity observations in results'),
47
- includeChunks: z.boolean().optional().describe('Include raw chunks in results'),
48
46
  });
47
+ // Agent-controlled params only: query, context
49
48
  const reflectParams = z.object({
50
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
51
49
  query: z.string().describe('Question to reflect on based on memories'),
52
50
  context: z.string().optional().describe('Additional context for the reflection'),
53
- budget: BudgetSchema.optional().describe('Processing budget: low, mid, or high'),
54
- });
55
- const createMentalModelParams = z.object({
56
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
57
- mentalModelId: z.string().optional().describe('Optional custom ID for the mental model (auto-generated if not provided)'),
58
- name: z.string().optional().describe('Optional name for the mental model'),
59
- sourceQuery: z.string().optional().describe('Query to define what memories to consolidate'),
60
- tags: z.array(z.string()).optional().describe('Optional tags for organizing mental models'),
61
- maxTokens: z.number().optional().describe('Maximum tokens for the mental model content'),
62
- autoRefresh: z.boolean().optional().describe('Auto-refresh mental model after new consolidations (default: false)'),
63
51
  });
64
- const queryMentalModelParams = z.object({
65
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
66
- mentalModelId: z.string().describe('ID of the mental model to query'),
52
+ const getMentalModelParams = z.object({
53
+ mentalModelId: z.string().describe('ID of the mental model to retrieve'),
67
54
  });
68
55
  const getDocumentParams = z.object({
69
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
70
56
  documentId: z.string().describe('ID of the document to retrieve'),
71
57
  });
72
- const createDirectiveParams = z.object({
73
- bankId: z.string().describe('Memory bank ID (usually the user ID)'),
74
- name: z.string().describe('Human-readable name for the directive'),
75
- content: z.string().describe('The directive text to inject into prompts'),
76
- priority: z.number().optional().describe('Higher priority directives are injected first (default 0)'),
77
- isActive: z.boolean().optional().describe('Whether this directive is active (default true)'),
78
- tags: z.array(z.string()).optional().describe('Tags for filtering'),
79
- });
80
58
  return {
81
59
  retain: tool({
82
- description: retainDescription ??
60
+ description: retainOpts.description ??
83
61
  `Store information in long-term memory. Use this when information should be remembered for future interactions, such as user preferences, facts, experiences, or important context.`,
84
62
  inputSchema: retainParams,
85
63
  execute: async (input) => {
86
64
  console.log('[AI SDK Tool] Retain input:', {
87
- bankId: input.bankId,
65
+ bankId,
88
66
  documentId: input.documentId,
89
- tags: input.tags,
90
67
  hasContent: !!input.content,
91
68
  });
92
- const result = await client.retain(input.bankId, input.content, {
69
+ const result = await client.retain(bankId, input.content, {
93
70
  documentId: input.documentId,
94
71
  timestamp: input.timestamp,
95
72
  context: input.context,
96
- tags: input.tags,
97
- metadata: input.metadata,
73
+ tags: retainOpts.tags,
74
+ metadata: retainOpts.metadata,
75
+ async: retainOpts.async ?? false,
98
76
  });
99
77
  return { success: result.success, itemsCount: result.items_count };
100
78
  },
101
79
  }),
102
80
  recall: tool({
103
- description: recallDescription ??
81
+ description: recallOpts.description ??
104
82
  `Search memory for relevant information. Use this to find previously stored information that can help personalize responses or provide context.`,
105
83
  inputSchema: recallParams,
106
84
  execute: async (input) => {
107
- const result = await client.recall(input.bankId, input.query, {
108
- types: input.types,
109
- maxTokens: input.maxTokens,
110
- budget: input.budget,
85
+ const result = await client.recall(bankId, input.query, {
86
+ types: recallOpts.types,
87
+ maxTokens: recallOpts.maxTokens,
88
+ budget: recallOpts.budget ?? 'mid',
111
89
  queryTimestamp: input.queryTimestamp,
112
- includeEntities: input.includeEntities,
113
- includeChunks: input.includeChunks,
90
+ includeEntities: recallOpts.includeEntities ?? false,
91
+ includeChunks: recallOpts.includeChunks ?? false,
114
92
  });
115
93
  return {
116
94
  results: result.results ?? [],
@@ -119,13 +97,14 @@ export function createHindsightTools({ client, retainDescription, recallDescript
119
97
  },
120
98
  }),
121
99
  reflect: tool({
122
- description: reflectDescription ??
100
+ description: reflectOpts.description ??
123
101
  `Analyze memories to form insights and generate contextual answers. Use this to understand patterns, synthesize information, or answer questions that require reasoning over stored memories.`,
124
102
  inputSchema: reflectParams,
125
103
  execute: async (input) => {
126
- const result = await client.reflect(input.bankId, input.query, {
104
+ const result = await client.reflect(bankId, input.query, {
127
105
  context: input.context,
128
- budget: input.budget,
106
+ budget: reflectOpts.budget ?? 'mid',
107
+ maxTokens: reflectOpts.maxTokens,
129
108
  });
130
109
  return {
131
110
  text: result.text ?? 'No insights available yet.',
@@ -133,31 +112,12 @@ export function createHindsightTools({ client, retainDescription, recallDescript
133
112
  };
134
113
  },
135
114
  }),
136
- createMentalModel: tool({
137
- description: createMentalModelDescription ??
138
- `Create a mental model that automatically consolidates memories into structured knowledge. Mental models are continuously updated as new memories are added, making them ideal for maintaining up-to-date user preferences, behavioral patterns, and accumulated wisdom.`,
139
- inputSchema: createMentalModelParams,
140
- execute: async (input) => {
141
- const result = await client.createMentalModel(input.bankId, {
142
- id: input.mentalModelId,
143
- name: input.name,
144
- sourceQuery: input.sourceQuery,
145
- tags: input.tags,
146
- maxTokens: input.maxTokens,
147
- trigger: input.autoRefresh !== undefined ? { refresh_after_consolidation: input.autoRefresh } : undefined,
148
- });
149
- return {
150
- mentalModelId: result.mental_model_id,
151
- createdAt: result.created_at,
152
- };
153
- },
154
- }),
155
- queryMentalModel: tool({
156
- description: queryMentalModelDescription ??
157
- `Query an existing mental model to retrieve consolidated knowledge. Mental models provide synthesized insights from memories, making them faster and more efficient than searching through raw memories.`,
158
- inputSchema: queryMentalModelParams,
115
+ getMentalModel: tool({
116
+ description: getMentalModelOpts.description ??
117
+ `Retrieve a mental model to get consolidated knowledge synthesized from memories. Mental models provide synthesized insights that are faster and more efficient to retrieve than searching through raw memories.`,
118
+ inputSchema: getMentalModelParams,
159
119
  execute: async (input) => {
160
- const result = await client.getMentalModel(input.bankId, input.mentalModelId);
120
+ const result = await client.getMentalModel(bankId, input.mentalModelId);
161
121
  return {
162
122
  content: result.content ?? 'No content available yet.',
163
123
  name: result.name,
@@ -166,11 +126,11 @@ export function createHindsightTools({ client, retainDescription, recallDescript
166
126
  },
167
127
  }),
168
128
  getDocument: tool({
169
- description: getDocumentDescription ??
129
+ description: getDocumentOpts.description ??
170
130
  `Retrieve a stored document by its ID. Documents are used to store structured data like application state, user profiles, or any data that needs exact retrieval.`,
171
131
  inputSchema: getDocumentParams,
172
132
  execute: async (input) => {
173
- const result = await client.getDocument(input.bankId, input.documentId);
133
+ const result = await client.getDocument(bankId, input.documentId);
174
134
  if (!result) {
175
135
  return null;
176
136
  }
@@ -182,25 +142,5 @@ export function createHindsightTools({ client, retainDescription, recallDescript
182
142
  };
183
143
  },
184
144
  }),
185
- createDirective: tool({
186
- description: `Create a directive - a hard rule that is injected into prompts during reflect operations. Directives are explicit instructions that guide agent behavior. Use tags to control when directives are applied (e.g., user-specific directives with 'user:username' tags).`,
187
- inputSchema: createDirectiveParams,
188
- execute: async (input) => {
189
- const result = await client.createDirective(input.bankId, {
190
- name: input.name,
191
- content: input.content,
192
- priority: input.priority,
193
- isActive: input.isActive,
194
- tags: input.tags,
195
- });
196
- return {
197
- id: result.id,
198
- name: result.name,
199
- content: result.content,
200
- tags: result.tags,
201
- createdAt: result.created_at,
202
- };
203
- },
204
- }),
205
145
  };
206
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectorize-io/hindsight-ai-sdk",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "Hindsight memory integration for Vercel AI SDK - Give your AI agents persistent, human-like memory",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",