@vectorize-io/hindsight-ai-sdk 0.4.18 → 0.4.19
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/dist/index.d.ts +274 -1
- package/dist/index.js +123 -1
- package/dist/index.js.map +1 -0
- package/package.json +4 -2
- package/dist/tools/index.d.ts +0 -270
- package/dist/tools/index.js +0 -146
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,274 @@
|
|
|
1
|
-
|
|
1
|
+
import * as ai from 'ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Budget levels for recall/reflect operations.
|
|
6
|
+
*/
|
|
7
|
+
declare const BudgetSchema: z.ZodEnum<{
|
|
8
|
+
low: "low";
|
|
9
|
+
mid: "mid";
|
|
10
|
+
high: "high";
|
|
11
|
+
}>;
|
|
12
|
+
type Budget = z.infer<typeof BudgetSchema>;
|
|
13
|
+
/**
|
|
14
|
+
* Fact types for filtering recall results.
|
|
15
|
+
*/
|
|
16
|
+
declare const FactTypeSchema: z.ZodEnum<{
|
|
17
|
+
world: "world";
|
|
18
|
+
experience: "experience";
|
|
19
|
+
observation: "observation";
|
|
20
|
+
}>;
|
|
21
|
+
type FactType = z.infer<typeof FactTypeSchema>;
|
|
22
|
+
/**
|
|
23
|
+
* Recall result item from Hindsight
|
|
24
|
+
*/
|
|
25
|
+
interface RecallResult {
|
|
26
|
+
id: string;
|
|
27
|
+
text: string;
|
|
28
|
+
type?: string | null;
|
|
29
|
+
entities?: string[] | null;
|
|
30
|
+
context?: string | null;
|
|
31
|
+
occurred_start?: string | null;
|
|
32
|
+
occurred_end?: string | null;
|
|
33
|
+
mentioned_at?: string | null;
|
|
34
|
+
document_id?: string | null;
|
|
35
|
+
metadata?: Record<string, string> | null;
|
|
36
|
+
chunk_id?: string | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Entity state with observations
|
|
40
|
+
*/
|
|
41
|
+
interface EntityState {
|
|
42
|
+
entity_id: string;
|
|
43
|
+
canonical_name: string;
|
|
44
|
+
observations: Array<{
|
|
45
|
+
text: string;
|
|
46
|
+
mentioned_at?: string | null;
|
|
47
|
+
}>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Chunk data
|
|
51
|
+
*/
|
|
52
|
+
interface ChunkData {
|
|
53
|
+
id: string;
|
|
54
|
+
text: string;
|
|
55
|
+
chunk_index: number;
|
|
56
|
+
truncated?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Recall response from Hindsight
|
|
60
|
+
*/
|
|
61
|
+
interface RecallResponse {
|
|
62
|
+
results: RecallResult[];
|
|
63
|
+
trace?: Record<string, unknown> | null;
|
|
64
|
+
entities?: Record<string, EntityState> | null;
|
|
65
|
+
chunks?: Record<string, ChunkData> | null;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reflect fact
|
|
69
|
+
*/
|
|
70
|
+
interface ReflectFact {
|
|
71
|
+
id?: string | null;
|
|
72
|
+
text: string;
|
|
73
|
+
type?: string | null;
|
|
74
|
+
context?: string | null;
|
|
75
|
+
occurred_start?: string | null;
|
|
76
|
+
occurred_end?: string | null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Reflect response from Hindsight
|
|
80
|
+
*/
|
|
81
|
+
interface ReflectResponse {
|
|
82
|
+
text: string;
|
|
83
|
+
based_on?: ReflectFact[];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Retain response from Hindsight
|
|
87
|
+
*/
|
|
88
|
+
interface RetainResponse {
|
|
89
|
+
success: boolean;
|
|
90
|
+
bank_id: string;
|
|
91
|
+
items_count: number;
|
|
92
|
+
async: boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Mental model trigger configuration
|
|
96
|
+
*/
|
|
97
|
+
interface MentalModelTrigger {
|
|
98
|
+
refresh_after_consolidation?: boolean;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Mental model response from Hindsight
|
|
102
|
+
*/
|
|
103
|
+
interface MentalModelResponse {
|
|
104
|
+
mental_model_id: string;
|
|
105
|
+
bank_id: string;
|
|
106
|
+
name?: string;
|
|
107
|
+
content?: string;
|
|
108
|
+
source_query?: string;
|
|
109
|
+
tags?: string[];
|
|
110
|
+
created_at: string;
|
|
111
|
+
updated_at: string;
|
|
112
|
+
trigger?: MentalModelTrigger;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Document response from Hindsight
|
|
116
|
+
*/
|
|
117
|
+
interface DocumentResponse {
|
|
118
|
+
id: string;
|
|
119
|
+
bank_id: string;
|
|
120
|
+
original_text: string;
|
|
121
|
+
content_hash: string | null;
|
|
122
|
+
created_at: string;
|
|
123
|
+
updated_at: string;
|
|
124
|
+
memory_unit_count: number;
|
|
125
|
+
tags?: string[];
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Hindsight client interface - matches @vectorize-io/hindsight-client
|
|
129
|
+
*/
|
|
130
|
+
interface HindsightClient {
|
|
131
|
+
retain(bankId: string, content: string, options?: {
|
|
132
|
+
timestamp?: Date | string;
|
|
133
|
+
context?: string;
|
|
134
|
+
metadata?: Record<string, string>;
|
|
135
|
+
documentId?: string;
|
|
136
|
+
tags?: string[];
|
|
137
|
+
async?: boolean;
|
|
138
|
+
}): Promise<RetainResponse>;
|
|
139
|
+
recall(bankId: string, query: string, options?: {
|
|
140
|
+
types?: FactType[];
|
|
141
|
+
maxTokens?: number;
|
|
142
|
+
budget?: Budget;
|
|
143
|
+
trace?: boolean;
|
|
144
|
+
queryTimestamp?: string;
|
|
145
|
+
includeEntities?: boolean;
|
|
146
|
+
maxEntityTokens?: number;
|
|
147
|
+
includeChunks?: boolean;
|
|
148
|
+
maxChunkTokens?: number;
|
|
149
|
+
}): Promise<RecallResponse>;
|
|
150
|
+
reflect(bankId: string, query: string, options?: {
|
|
151
|
+
context?: string;
|
|
152
|
+
budget?: Budget;
|
|
153
|
+
maxTokens?: number;
|
|
154
|
+
}): Promise<ReflectResponse>;
|
|
155
|
+
getMentalModel(bankId: string, mentalModelId: string): Promise<MentalModelResponse>;
|
|
156
|
+
getDocument(bankId: string, documentId: string): Promise<DocumentResponse | null>;
|
|
157
|
+
}
|
|
158
|
+
interface HindsightToolsOptions {
|
|
159
|
+
/** Hindsight client instance */
|
|
160
|
+
client: HindsightClient;
|
|
161
|
+
/** Memory bank ID to use for all tool calls (e.g. the user ID) */
|
|
162
|
+
bankId: string;
|
|
163
|
+
/** Options for the retain tool */
|
|
164
|
+
retain?: {
|
|
165
|
+
/** Fire-and-forget retain without waiting for completion (default: false) */
|
|
166
|
+
async?: boolean;
|
|
167
|
+
/** Tags always attached to every retained memory (default: undefined) */
|
|
168
|
+
tags?: string[];
|
|
169
|
+
/** Metadata always attached to every retained memory (default: undefined) */
|
|
170
|
+
metadata?: Record<string, string>;
|
|
171
|
+
/** Custom tool description */
|
|
172
|
+
description?: string;
|
|
173
|
+
};
|
|
174
|
+
/** Options for the recall tool */
|
|
175
|
+
recall?: {
|
|
176
|
+
/** Restrict results to these fact types: 'world', 'experience', 'observation' (default: undefined = all types) */
|
|
177
|
+
types?: FactType[];
|
|
178
|
+
/** Maximum tokens to return (default: undefined = API default) */
|
|
179
|
+
maxTokens?: number;
|
|
180
|
+
/** Processing budget controlling latency vs. depth (default: 'mid') */
|
|
181
|
+
budget?: Budget;
|
|
182
|
+
/** Include entity observations in results (default: false) */
|
|
183
|
+
includeEntities?: boolean;
|
|
184
|
+
/** Include raw source chunks in results (default: false) */
|
|
185
|
+
includeChunks?: boolean;
|
|
186
|
+
/** Custom tool description */
|
|
187
|
+
description?: string;
|
|
188
|
+
};
|
|
189
|
+
/** Options for the reflect tool */
|
|
190
|
+
reflect?: {
|
|
191
|
+
/** Processing budget controlling latency vs. depth (default: 'mid') */
|
|
192
|
+
budget?: Budget;
|
|
193
|
+
/** Maximum tokens for the response (default: undefined = API default) */
|
|
194
|
+
maxTokens?: number;
|
|
195
|
+
/** Custom tool description */
|
|
196
|
+
description?: string;
|
|
197
|
+
};
|
|
198
|
+
/** Options for the getMentalModel tool */
|
|
199
|
+
getMentalModel?: {
|
|
200
|
+
/** Custom tool description */
|
|
201
|
+
description?: string;
|
|
202
|
+
};
|
|
203
|
+
/** Options for the getDocument tool */
|
|
204
|
+
getDocument?: {
|
|
205
|
+
/** Custom tool description */
|
|
206
|
+
description?: string;
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Creates AI SDK tools for Hindsight memory operations.
|
|
211
|
+
*
|
|
212
|
+
* The bank ID and all infrastructure concerns (budget, tags, async mode, etc.)
|
|
213
|
+
* are fixed at creation time. The agent only controls semantic inputs:
|
|
214
|
+
* content, queries, names, and timestamps.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* const tools = createHindsightTools({
|
|
219
|
+
* client: hindsightClient,
|
|
220
|
+
* bankId: userId,
|
|
221
|
+
* recall: { budget: 'high', includeEntities: true },
|
|
222
|
+
* retain: { async: true, tags: ['env:prod'] },
|
|
223
|
+
* });
|
|
224
|
+
*
|
|
225
|
+
* const result = await generateText({
|
|
226
|
+
* model: openai('gpt-4o'),
|
|
227
|
+
* tools,
|
|
228
|
+
* messages,
|
|
229
|
+
* });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
declare function createHindsightTools({ client, bankId, retain: retainOpts, recall: recallOpts, reflect: reflectOpts, getMentalModel: getMentalModelOpts, getDocument: getDocumentOpts, }: HindsightToolsOptions): {
|
|
233
|
+
retain: ai.Tool<{
|
|
234
|
+
content: string;
|
|
235
|
+
documentId?: string | undefined;
|
|
236
|
+
timestamp?: string | undefined;
|
|
237
|
+
context?: string | undefined;
|
|
238
|
+
}, {
|
|
239
|
+
success: boolean;
|
|
240
|
+
itemsCount: number;
|
|
241
|
+
}>;
|
|
242
|
+
recall: ai.Tool<{
|
|
243
|
+
query: string;
|
|
244
|
+
queryTimestamp?: string | undefined;
|
|
245
|
+
}, {
|
|
246
|
+
results: RecallResult[];
|
|
247
|
+
entities?: Record<string, EntityState> | null;
|
|
248
|
+
}>;
|
|
249
|
+
reflect: ai.Tool<{
|
|
250
|
+
query: string;
|
|
251
|
+
context?: string | undefined;
|
|
252
|
+
}, {
|
|
253
|
+
text: string;
|
|
254
|
+
basedOn?: ReflectFact[];
|
|
255
|
+
}>;
|
|
256
|
+
getMentalModel: ai.Tool<{
|
|
257
|
+
mentalModelId: string;
|
|
258
|
+
}, {
|
|
259
|
+
content: string;
|
|
260
|
+
name?: string;
|
|
261
|
+
updatedAt: string;
|
|
262
|
+
}>;
|
|
263
|
+
getDocument: ai.Tool<{
|
|
264
|
+
documentId: string;
|
|
265
|
+
}, {
|
|
266
|
+
originalText: string;
|
|
267
|
+
id: string;
|
|
268
|
+
createdAt: string;
|
|
269
|
+
updatedAt: string;
|
|
270
|
+
} | null>;
|
|
271
|
+
};
|
|
272
|
+
type HindsightTools = ReturnType<typeof createHindsightTools>;
|
|
273
|
+
|
|
274
|
+
export { type Budget, BudgetSchema, type ChunkData, type EntityState, type HindsightClient, type HindsightTools, type HindsightToolsOptions, type RecallResponse, type RecallResult, type ReflectFact, type ReflectResponse, type RetainResponse, createHindsightTools };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,123 @@
|
|
|
1
|
-
|
|
1
|
+
// src/tools/index.ts
|
|
2
|
+
import { tool } from "ai";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
var BudgetSchema = z.enum(["low", "mid", "high"]);
|
|
5
|
+
var FactTypeSchema = z.enum(["world", "experience", "observation"]);
|
|
6
|
+
function createHindsightTools({
|
|
7
|
+
client,
|
|
8
|
+
bankId,
|
|
9
|
+
retain: retainOpts = {},
|
|
10
|
+
recall: recallOpts = {},
|
|
11
|
+
reflect: reflectOpts = {},
|
|
12
|
+
getMentalModel: getMentalModelOpts = {},
|
|
13
|
+
getDocument: getDocumentOpts = {}
|
|
14
|
+
}) {
|
|
15
|
+
const retainParams = z.object({
|
|
16
|
+
content: z.string().describe("Content to store in memory"),
|
|
17
|
+
documentId: z.string().optional().describe("Optional document ID for grouping/upserting content"),
|
|
18
|
+
timestamp: z.string().optional().describe("Optional ISO timestamp for when the memory occurred"),
|
|
19
|
+
context: z.string().optional().describe("Optional context about the memory")
|
|
20
|
+
});
|
|
21
|
+
const recallParams = z.object({
|
|
22
|
+
query: z.string().describe("What to search for in memory"),
|
|
23
|
+
queryTimestamp: z.string().optional().describe("Query from a specific point in time (ISO format)")
|
|
24
|
+
});
|
|
25
|
+
const reflectParams = z.object({
|
|
26
|
+
query: z.string().describe("Question to reflect on based on memories"),
|
|
27
|
+
context: z.string().optional().describe("Additional context for the reflection")
|
|
28
|
+
});
|
|
29
|
+
const getMentalModelParams = z.object({
|
|
30
|
+
mentalModelId: z.string().describe("ID of the mental model to retrieve")
|
|
31
|
+
});
|
|
32
|
+
const getDocumentParams = z.object({
|
|
33
|
+
documentId: z.string().describe("ID of the document to retrieve")
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
retain: tool({
|
|
37
|
+
description: retainOpts.description ?? `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.`,
|
|
38
|
+
inputSchema: retainParams,
|
|
39
|
+
execute: async (input) => {
|
|
40
|
+
console.log("[AI SDK Tool] Retain input:", {
|
|
41
|
+
bankId,
|
|
42
|
+
documentId: input.documentId,
|
|
43
|
+
hasContent: !!input.content
|
|
44
|
+
});
|
|
45
|
+
const result = await client.retain(bankId, input.content, {
|
|
46
|
+
documentId: input.documentId,
|
|
47
|
+
timestamp: input.timestamp,
|
|
48
|
+
context: input.context,
|
|
49
|
+
tags: retainOpts.tags,
|
|
50
|
+
metadata: retainOpts.metadata,
|
|
51
|
+
async: retainOpts.async ?? false
|
|
52
|
+
});
|
|
53
|
+
return { success: result.success, itemsCount: result.items_count };
|
|
54
|
+
}
|
|
55
|
+
}),
|
|
56
|
+
recall: tool({
|
|
57
|
+
description: recallOpts.description ?? `Search memory for relevant information. Use this to find previously stored information that can help personalize responses or provide context.`,
|
|
58
|
+
inputSchema: recallParams,
|
|
59
|
+
execute: async (input) => {
|
|
60
|
+
const result = await client.recall(bankId, input.query, {
|
|
61
|
+
types: recallOpts.types,
|
|
62
|
+
maxTokens: recallOpts.maxTokens,
|
|
63
|
+
budget: recallOpts.budget ?? "mid",
|
|
64
|
+
queryTimestamp: input.queryTimestamp,
|
|
65
|
+
includeEntities: recallOpts.includeEntities ?? false,
|
|
66
|
+
includeChunks: recallOpts.includeChunks ?? false
|
|
67
|
+
});
|
|
68
|
+
return {
|
|
69
|
+
results: result.results ?? [],
|
|
70
|
+
entities: result.entities
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}),
|
|
74
|
+
reflect: tool({
|
|
75
|
+
description: reflectOpts.description ?? `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.`,
|
|
76
|
+
inputSchema: reflectParams,
|
|
77
|
+
execute: async (input) => {
|
|
78
|
+
const result = await client.reflect(bankId, input.query, {
|
|
79
|
+
context: input.context,
|
|
80
|
+
budget: reflectOpts.budget ?? "mid",
|
|
81
|
+
maxTokens: reflectOpts.maxTokens
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
text: result.text ?? "No insights available yet.",
|
|
85
|
+
basedOn: result.based_on
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}),
|
|
89
|
+
getMentalModel: tool({
|
|
90
|
+
description: getMentalModelOpts.description ?? `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.`,
|
|
91
|
+
inputSchema: getMentalModelParams,
|
|
92
|
+
execute: async (input) => {
|
|
93
|
+
const result = await client.getMentalModel(bankId, input.mentalModelId);
|
|
94
|
+
return {
|
|
95
|
+
content: result.content ?? "No content available yet.",
|
|
96
|
+
name: result.name,
|
|
97
|
+
updatedAt: result.updated_at
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}),
|
|
101
|
+
getDocument: tool({
|
|
102
|
+
description: getDocumentOpts.description ?? `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.`,
|
|
103
|
+
inputSchema: getDocumentParams,
|
|
104
|
+
execute: async (input) => {
|
|
105
|
+
const result = await client.getDocument(bankId, input.documentId);
|
|
106
|
+
if (!result) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
originalText: result.original_text,
|
|
111
|
+
id: result.id,
|
|
112
|
+
createdAt: result.created_at,
|
|
113
|
+
updatedAt: result.updated_at
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export {
|
|
120
|
+
BudgetSchema,
|
|
121
|
+
createHindsightTools
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/tools/index.ts"],"sourcesContent":["import { tool } from 'ai';\nimport { z } from 'zod';\n\n/**\n * Budget levels for recall/reflect operations.\n */\nexport const BudgetSchema = z.enum(['low', 'mid', 'high']);\nexport type Budget = z.infer<typeof BudgetSchema>;\n\n/**\n * Fact types for filtering recall results.\n */\nexport const FactTypeSchema = z.enum(['world', 'experience', 'observation']);\nexport type FactType = z.infer<typeof FactTypeSchema>;\n\n/**\n * Recall result item from Hindsight\n */\nexport interface RecallResult {\n id: string;\n text: string;\n type?: string | null;\n entities?: string[] | null;\n context?: string | null;\n occurred_start?: string | null;\n occurred_end?: string | null;\n mentioned_at?: string | null;\n document_id?: string | null;\n metadata?: Record<string, string> | null;\n chunk_id?: string | null;\n}\n\n/**\n * Entity state with observations\n */\nexport interface EntityState {\n entity_id: string;\n canonical_name: string;\n observations: Array<{ text: string; mentioned_at?: string | null }>;\n}\n\n/**\n * Chunk data\n */\nexport interface ChunkData {\n id: string;\n text: string;\n chunk_index: number;\n truncated?: boolean;\n}\n\n/**\n * Recall response from Hindsight\n */\nexport interface RecallResponse {\n results: RecallResult[];\n trace?: Record<string, unknown> | null;\n entities?: Record<string, EntityState> | null;\n chunks?: Record<string, ChunkData> | null;\n}\n\n/**\n * Reflect fact\n */\nexport interface ReflectFact {\n id?: string | null;\n text: string;\n type?: string | null;\n context?: string | null;\n occurred_start?: string | null;\n occurred_end?: string | null;\n}\n\n/**\n * Reflect response from Hindsight\n */\nexport interface ReflectResponse {\n text: string;\n based_on?: ReflectFact[];\n}\n\n/**\n * Retain response from Hindsight\n */\nexport interface RetainResponse {\n success: boolean;\n bank_id: string;\n items_count: number;\n async: boolean;\n}\n\n/**\n * Mental model trigger configuration\n */\nexport interface MentalModelTrigger {\n refresh_after_consolidation?: boolean;\n}\n\n/**\n * Mental model response from Hindsight\n */\nexport interface MentalModelResponse {\n mental_model_id: string;\n bank_id: string;\n name?: string;\n content?: string;\n source_query?: string;\n tags?: string[];\n created_at: string;\n updated_at: string;\n trigger?: MentalModelTrigger;\n}\n\n/**\n * Document response from Hindsight\n */\nexport interface DocumentResponse {\n id: string;\n bank_id: string;\n original_text: string;\n content_hash: string | null;\n created_at: string;\n updated_at: string;\n memory_unit_count: number;\n tags?: string[];\n}\n\n/**\n * Hindsight client interface - matches @vectorize-io/hindsight-client\n */\nexport interface HindsightClient {\n retain(\n bankId: string,\n content: string,\n options?: {\n timestamp?: Date | string;\n context?: string;\n metadata?: Record<string, string>;\n documentId?: string;\n tags?: string[];\n async?: boolean;\n }\n ): Promise<RetainResponse>;\n\n recall(\n bankId: string,\n query: string,\n options?: {\n types?: FactType[];\n maxTokens?: number;\n budget?: Budget;\n trace?: boolean;\n queryTimestamp?: string;\n includeEntities?: boolean;\n maxEntityTokens?: number;\n includeChunks?: boolean;\n maxChunkTokens?: number;\n }\n ): Promise<RecallResponse>;\n\n reflect(\n bankId: string,\n query: string,\n options?: {\n context?: string;\n budget?: Budget;\n maxTokens?: number;\n }\n ): Promise<ReflectResponse>;\n\n getMentalModel(\n bankId: string,\n mentalModelId: string\n ): Promise<MentalModelResponse>;\n\n getDocument(\n bankId: string,\n documentId: string\n ): Promise<DocumentResponse | null>;\n}\n\nexport interface HindsightToolsOptions {\n /** Hindsight client instance */\n client: HindsightClient;\n /** Memory bank ID to use for all tool calls (e.g. the user ID) */\n bankId: string;\n\n /** Options for the retain tool */\n retain?: {\n /** Fire-and-forget retain without waiting for completion (default: false) */\n async?: boolean;\n /** Tags always attached to every retained memory (default: undefined) */\n tags?: string[];\n /** Metadata always attached to every retained memory (default: undefined) */\n metadata?: Record<string, string>;\n /** Custom tool description */\n description?: string;\n };\n\n /** Options for the recall tool */\n recall?: {\n /** Restrict results to these fact types: 'world', 'experience', 'observation' (default: undefined = all types) */\n types?: FactType[];\n /** Maximum tokens to return (default: undefined = API default) */\n maxTokens?: number;\n /** Processing budget controlling latency vs. depth (default: 'mid') */\n budget?: Budget;\n /** Include entity observations in results (default: false) */\n includeEntities?: boolean;\n /** Include raw source chunks in results (default: false) */\n includeChunks?: boolean;\n /** Custom tool description */\n description?: string;\n };\n\n /** Options for the reflect tool */\n reflect?: {\n /** Processing budget controlling latency vs. depth (default: 'mid') */\n budget?: Budget;\n /** Maximum tokens for the response (default: undefined = API default) */\n maxTokens?: number;\n /** Custom tool description */\n description?: string;\n };\n\n /** Options for the getMentalModel tool */\n getMentalModel?: {\n /** Custom tool description */\n description?: string;\n };\n\n /** Options for the getDocument tool */\n getDocument?: {\n /** Custom tool description */\n description?: string;\n };\n}\n\n/**\n * Creates AI SDK tools for Hindsight memory operations.\n *\n * The bank ID and all infrastructure concerns (budget, tags, async mode, etc.)\n * are fixed at creation time. The agent only controls semantic inputs:\n * content, queries, names, and timestamps.\n *\n * @example\n * ```ts\n * const tools = createHindsightTools({\n * client: hindsightClient,\n * bankId: userId,\n * recall: { budget: 'high', includeEntities: true },\n * retain: { async: true, tags: ['env:prod'] },\n * });\n *\n * const result = await generateText({\n * model: openai('gpt-4o'),\n * tools,\n * messages,\n * });\n * ```\n */\nexport function createHindsightTools({\n client,\n bankId,\n retain: retainOpts = {},\n recall: recallOpts = {},\n reflect: reflectOpts = {},\n getMentalModel: getMentalModelOpts = {},\n getDocument: getDocumentOpts = {},\n}: HindsightToolsOptions) {\n // Agent-controlled params only: content, timestamp, documentId, context\n const retainParams = z.object({\n content: z.string().describe('Content to store in memory'),\n documentId: z.string().optional().describe('Optional document ID for grouping/upserting content'),\n timestamp: z.string().optional().describe('Optional ISO timestamp for when the memory occurred'),\n context: z.string().optional().describe('Optional context about the memory'),\n });\n\n // Agent-controlled params only: query, queryTimestamp\n const recallParams = z.object({\n query: z.string().describe('What to search for in memory'),\n queryTimestamp: z.string().optional().describe('Query from a specific point in time (ISO format)'),\n });\n\n // Agent-controlled params only: query, context\n const reflectParams = z.object({\n query: z.string().describe('Question to reflect on based on memories'),\n context: z.string().optional().describe('Additional context for the reflection'),\n });\n\n const getMentalModelParams = z.object({\n mentalModelId: z.string().describe('ID of the mental model to retrieve'),\n });\n\n const getDocumentParams = z.object({\n documentId: z.string().describe('ID of the document to retrieve'),\n });\n\n type RetainInput = z.infer<typeof retainParams>;\n type RetainOutput = { success: boolean; itemsCount: number };\n\n type RecallInput = z.infer<typeof recallParams>;\n type RecallOutput = { results: RecallResult[]; entities?: Record<string, EntityState> | null };\n\n type ReflectInput = z.infer<typeof reflectParams>;\n type ReflectOutput = { text: string; basedOn?: ReflectFact[] };\n\n type GetMentalModelInput = z.infer<typeof getMentalModelParams>;\n type GetMentalModelOutput = { content: string; name?: string; updatedAt: string };\n\n type GetDocumentInput = z.infer<typeof getDocumentParams>;\n type GetDocumentOutput = { originalText: string; id: string; createdAt: string; updatedAt: string } | null;\n\n return {\n retain: tool<RetainInput, RetainOutput>({\n description:\n retainOpts.description ??\n `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.`,\n inputSchema: retainParams,\n execute: async (input) => {\n console.log('[AI SDK Tool] Retain input:', {\n bankId,\n documentId: input.documentId,\n hasContent: !!input.content,\n });\n const result = await client.retain(bankId, input.content, {\n documentId: input.documentId,\n timestamp: input.timestamp,\n context: input.context,\n tags: retainOpts.tags,\n metadata: retainOpts.metadata,\n async: retainOpts.async ?? false,\n });\n return { success: result.success, itemsCount: result.items_count };\n },\n }),\n\n recall: tool<RecallInput, RecallOutput>({\n description:\n recallOpts.description ??\n `Search memory for relevant information. Use this to find previously stored information that can help personalize responses or provide context.`,\n inputSchema: recallParams,\n execute: async (input) => {\n const result = await client.recall(bankId, input.query, {\n types: recallOpts.types,\n maxTokens: recallOpts.maxTokens,\n budget: recallOpts.budget ?? 'mid',\n queryTimestamp: input.queryTimestamp,\n includeEntities: recallOpts.includeEntities ?? false,\n includeChunks: recallOpts.includeChunks ?? false,\n });\n return {\n results: result.results ?? [],\n entities: result.entities,\n };\n },\n }),\n\n reflect: tool<ReflectInput, ReflectOutput>({\n description:\n reflectOpts.description ??\n `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.`,\n inputSchema: reflectParams,\n execute: async (input) => {\n const result = await client.reflect(bankId, input.query, {\n context: input.context,\n budget: reflectOpts.budget ?? 'mid',\n maxTokens: reflectOpts.maxTokens,\n });\n return {\n text: result.text ?? 'No insights available yet.',\n basedOn: result.based_on,\n };\n },\n }),\n\n getMentalModel: tool<GetMentalModelInput, GetMentalModelOutput>({\n description:\n getMentalModelOpts.description ??\n `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.`,\n inputSchema: getMentalModelParams,\n execute: async (input) => {\n const result = await client.getMentalModel(bankId, input.mentalModelId);\n return {\n content: result.content ?? 'No content available yet.',\n name: result.name,\n updatedAt: result.updated_at,\n };\n },\n }),\n\n getDocument: tool<GetDocumentInput, GetDocumentOutput>({\n description:\n getDocumentOpts.description ??\n `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.`,\n inputSchema: getDocumentParams,\n execute: async (input) => {\n const result = await client.getDocument(bankId, input.documentId);\n if (!result) {\n return null;\n }\n return {\n originalText: result.original_text,\n id: result.id,\n createdAt: result.created_at,\n updatedAt: result.updated_at,\n };\n },\n }),\n\n };\n}\n\nexport type HindsightTools = ReturnType<typeof createHindsightTools>;\n"],"mappings":"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","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vectorize-io/hindsight-ai-sdk",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.19",
|
|
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",
|
|
@@ -33,11 +33,12 @@
|
|
|
33
33
|
"README.md"
|
|
34
34
|
],
|
|
35
35
|
"scripts": {
|
|
36
|
-
"build": "
|
|
36
|
+
"build": "tsup",
|
|
37
37
|
"dev": "tsc --watch",
|
|
38
38
|
"clean": "rm -rf dist",
|
|
39
39
|
"test": "vitest run",
|
|
40
40
|
"test:watch": "vitest",
|
|
41
|
+
"test:deno": "deno test --no-check --allow-all --unstable-sloppy-imports src/tools/index.test.ts",
|
|
41
42
|
"prepublishOnly": "npm run clean && npm run build"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
@@ -48,6 +49,7 @@
|
|
|
48
49
|
"@types/node": "^22.0.0",
|
|
49
50
|
"@vitest/ui": "^4.0.18",
|
|
50
51
|
"ai": "^6.0.2",
|
|
52
|
+
"tsup": "^8.5.1",
|
|
51
53
|
"typescript": "^5.7.0",
|
|
52
54
|
"vitest": "^4.0.18",
|
|
53
55
|
"zod": "^4.2.0"
|
package/dist/tools/index.d.ts
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
/**
|
|
3
|
-
* Budget levels for recall/reflect operations.
|
|
4
|
-
*/
|
|
5
|
-
export declare const BudgetSchema: z.ZodEnum<{
|
|
6
|
-
low: "low";
|
|
7
|
-
mid: "mid";
|
|
8
|
-
high: "high";
|
|
9
|
-
}>;
|
|
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>;
|
|
20
|
-
/**
|
|
21
|
-
* Recall result item from Hindsight
|
|
22
|
-
*/
|
|
23
|
-
export interface RecallResult {
|
|
24
|
-
id: string;
|
|
25
|
-
text: string;
|
|
26
|
-
type?: string | null;
|
|
27
|
-
entities?: string[] | null;
|
|
28
|
-
context?: string | null;
|
|
29
|
-
occurred_start?: string | null;
|
|
30
|
-
occurred_end?: string | null;
|
|
31
|
-
mentioned_at?: string | null;
|
|
32
|
-
document_id?: string | null;
|
|
33
|
-
metadata?: Record<string, string> | null;
|
|
34
|
-
chunk_id?: string | null;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Entity state with observations
|
|
38
|
-
*/
|
|
39
|
-
export interface EntityState {
|
|
40
|
-
entity_id: string;
|
|
41
|
-
canonical_name: string;
|
|
42
|
-
observations: Array<{
|
|
43
|
-
text: string;
|
|
44
|
-
mentioned_at?: string | null;
|
|
45
|
-
}>;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Chunk data
|
|
49
|
-
*/
|
|
50
|
-
export interface ChunkData {
|
|
51
|
-
id: string;
|
|
52
|
-
text: string;
|
|
53
|
-
chunk_index: number;
|
|
54
|
-
truncated?: boolean;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Recall response from Hindsight
|
|
58
|
-
*/
|
|
59
|
-
export interface RecallResponse {
|
|
60
|
-
results: RecallResult[];
|
|
61
|
-
trace?: Record<string, unknown> | null;
|
|
62
|
-
entities?: Record<string, EntityState> | null;
|
|
63
|
-
chunks?: Record<string, ChunkData> | null;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Reflect fact
|
|
67
|
-
*/
|
|
68
|
-
export interface ReflectFact {
|
|
69
|
-
id?: string | null;
|
|
70
|
-
text: string;
|
|
71
|
-
type?: string | null;
|
|
72
|
-
context?: string | null;
|
|
73
|
-
occurred_start?: string | null;
|
|
74
|
-
occurred_end?: string | null;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Reflect response from Hindsight
|
|
78
|
-
*/
|
|
79
|
-
export interface ReflectResponse {
|
|
80
|
-
text: string;
|
|
81
|
-
based_on?: ReflectFact[];
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Retain response from Hindsight
|
|
85
|
-
*/
|
|
86
|
-
export interface RetainResponse {
|
|
87
|
-
success: boolean;
|
|
88
|
-
bank_id: string;
|
|
89
|
-
items_count: number;
|
|
90
|
-
async: boolean;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Mental model trigger configuration
|
|
94
|
-
*/
|
|
95
|
-
export interface MentalModelTrigger {
|
|
96
|
-
refresh_after_consolidation?: boolean;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Mental model response from Hindsight
|
|
100
|
-
*/
|
|
101
|
-
export interface MentalModelResponse {
|
|
102
|
-
mental_model_id: string;
|
|
103
|
-
bank_id: string;
|
|
104
|
-
name?: string;
|
|
105
|
-
content?: string;
|
|
106
|
-
source_query?: string;
|
|
107
|
-
tags?: string[];
|
|
108
|
-
created_at: string;
|
|
109
|
-
updated_at: string;
|
|
110
|
-
trigger?: MentalModelTrigger;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Document response from Hindsight
|
|
114
|
-
*/
|
|
115
|
-
export interface DocumentResponse {
|
|
116
|
-
id: string;
|
|
117
|
-
bank_id: string;
|
|
118
|
-
original_text: string;
|
|
119
|
-
content_hash: string | null;
|
|
120
|
-
created_at: string;
|
|
121
|
-
updated_at: string;
|
|
122
|
-
memory_unit_count: number;
|
|
123
|
-
tags?: string[];
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Hindsight client interface - matches @vectorize-io/hindsight-client
|
|
127
|
-
*/
|
|
128
|
-
export interface HindsightClient {
|
|
129
|
-
retain(bankId: string, content: string, options?: {
|
|
130
|
-
timestamp?: Date | string;
|
|
131
|
-
context?: string;
|
|
132
|
-
metadata?: Record<string, string>;
|
|
133
|
-
documentId?: string;
|
|
134
|
-
tags?: string[];
|
|
135
|
-
async?: boolean;
|
|
136
|
-
}): Promise<RetainResponse>;
|
|
137
|
-
recall(bankId: string, query: string, options?: {
|
|
138
|
-
types?: FactType[];
|
|
139
|
-
maxTokens?: number;
|
|
140
|
-
budget?: Budget;
|
|
141
|
-
trace?: boolean;
|
|
142
|
-
queryTimestamp?: string;
|
|
143
|
-
includeEntities?: boolean;
|
|
144
|
-
maxEntityTokens?: number;
|
|
145
|
-
includeChunks?: boolean;
|
|
146
|
-
maxChunkTokens?: number;
|
|
147
|
-
}): Promise<RecallResponse>;
|
|
148
|
-
reflect(bankId: string, query: string, options?: {
|
|
149
|
-
context?: string;
|
|
150
|
-
budget?: Budget;
|
|
151
|
-
maxTokens?: number;
|
|
152
|
-
}): Promise<ReflectResponse>;
|
|
153
|
-
getMentalModel(bankId: string, mentalModelId: string): Promise<MentalModelResponse>;
|
|
154
|
-
getDocument(bankId: string, documentId: string): Promise<DocumentResponse | null>;
|
|
155
|
-
}
|
|
156
|
-
export interface HindsightToolsOptions {
|
|
157
|
-
/** Hindsight client instance */
|
|
158
|
-
client: HindsightClient;
|
|
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
|
-
};
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Creates AI SDK tools for Hindsight memory operations.
|
|
209
|
-
*
|
|
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.
|
|
213
|
-
*
|
|
214
|
-
* @example
|
|
215
|
-
* ```ts
|
|
216
|
-
* const tools = createHindsightTools({
|
|
217
|
-
* client: hindsightClient,
|
|
218
|
-
* bankId: userId,
|
|
219
|
-
* recall: { budget: 'high', includeEntities: true },
|
|
220
|
-
* retain: { async: true, tags: ['env:prod'] },
|
|
221
|
-
* });
|
|
222
|
-
*
|
|
223
|
-
* const result = await generateText({
|
|
224
|
-
* model: openai('gpt-4o'),
|
|
225
|
-
* tools,
|
|
226
|
-
* messages,
|
|
227
|
-
* });
|
|
228
|
-
* ```
|
|
229
|
-
*/
|
|
230
|
-
export declare function createHindsightTools({ client, bankId, retain: retainOpts, recall: recallOpts, reflect: reflectOpts, getMentalModel: getMentalModelOpts, getDocument: getDocumentOpts, }: HindsightToolsOptions): {
|
|
231
|
-
retain: import("ai").Tool<{
|
|
232
|
-
content: string;
|
|
233
|
-
documentId?: string | undefined;
|
|
234
|
-
timestamp?: string | undefined;
|
|
235
|
-
context?: string | undefined;
|
|
236
|
-
}, {
|
|
237
|
-
success: boolean;
|
|
238
|
-
itemsCount: number;
|
|
239
|
-
}>;
|
|
240
|
-
recall: import("ai").Tool<{
|
|
241
|
-
query: string;
|
|
242
|
-
queryTimestamp?: string | undefined;
|
|
243
|
-
}, {
|
|
244
|
-
results: RecallResult[];
|
|
245
|
-
entities?: Record<string, EntityState> | null;
|
|
246
|
-
}>;
|
|
247
|
-
reflect: import("ai").Tool<{
|
|
248
|
-
query: string;
|
|
249
|
-
context?: string | undefined;
|
|
250
|
-
}, {
|
|
251
|
-
text: string;
|
|
252
|
-
basedOn?: ReflectFact[];
|
|
253
|
-
}>;
|
|
254
|
-
getMentalModel: import("ai").Tool<{
|
|
255
|
-
mentalModelId: string;
|
|
256
|
-
}, {
|
|
257
|
-
content: string;
|
|
258
|
-
name?: string;
|
|
259
|
-
updatedAt: string;
|
|
260
|
-
}>;
|
|
261
|
-
getDocument: import("ai").Tool<{
|
|
262
|
-
documentId: string;
|
|
263
|
-
}, {
|
|
264
|
-
originalText: string;
|
|
265
|
-
id: string;
|
|
266
|
-
createdAt: string;
|
|
267
|
-
updatedAt: string;
|
|
268
|
-
} | null>;
|
|
269
|
-
};
|
|
270
|
-
export type HindsightTools = ReturnType<typeof createHindsightTools>;
|
package/dist/tools/index.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { tool } from 'ai';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
/**
|
|
4
|
-
* Budget levels for recall/reflect operations.
|
|
5
|
-
*/
|
|
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']);
|
|
11
|
-
/**
|
|
12
|
-
* Creates AI SDK tools for Hindsight memory operations.
|
|
13
|
-
*
|
|
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.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const tools = createHindsightTools({
|
|
21
|
-
* client: hindsightClient,
|
|
22
|
-
* bankId: userId,
|
|
23
|
-
* recall: { budget: 'high', includeEntities: true },
|
|
24
|
-
* retain: { async: true, tags: ['env:prod'] },
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* const result = await generateText({
|
|
28
|
-
* model: openai('gpt-4o'),
|
|
29
|
-
* tools,
|
|
30
|
-
* messages,
|
|
31
|
-
* });
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
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
|
|
36
|
-
const retainParams = z.object({
|
|
37
|
-
content: z.string().describe('Content to store in memory'),
|
|
38
|
-
documentId: z.string().optional().describe('Optional document ID for grouping/upserting content'),
|
|
39
|
-
timestamp: z.string().optional().describe('Optional ISO timestamp for when the memory occurred'),
|
|
40
|
-
context: z.string().optional().describe('Optional context about the memory'),
|
|
41
|
-
});
|
|
42
|
-
// Agent-controlled params only: query, queryTimestamp
|
|
43
|
-
const recallParams = z.object({
|
|
44
|
-
query: z.string().describe('What to search for in memory'),
|
|
45
|
-
queryTimestamp: z.string().optional().describe('Query from a specific point in time (ISO format)'),
|
|
46
|
-
});
|
|
47
|
-
// Agent-controlled params only: query, context
|
|
48
|
-
const reflectParams = z.object({
|
|
49
|
-
query: z.string().describe('Question to reflect on based on memories'),
|
|
50
|
-
context: z.string().optional().describe('Additional context for the reflection'),
|
|
51
|
-
});
|
|
52
|
-
const getMentalModelParams = z.object({
|
|
53
|
-
mentalModelId: z.string().describe('ID of the mental model to retrieve'),
|
|
54
|
-
});
|
|
55
|
-
const getDocumentParams = z.object({
|
|
56
|
-
documentId: z.string().describe('ID of the document to retrieve'),
|
|
57
|
-
});
|
|
58
|
-
return {
|
|
59
|
-
retain: tool({
|
|
60
|
-
description: retainOpts.description ??
|
|
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.`,
|
|
62
|
-
inputSchema: retainParams,
|
|
63
|
-
execute: async (input) => {
|
|
64
|
-
console.log('[AI SDK Tool] Retain input:', {
|
|
65
|
-
bankId,
|
|
66
|
-
documentId: input.documentId,
|
|
67
|
-
hasContent: !!input.content,
|
|
68
|
-
});
|
|
69
|
-
const result = await client.retain(bankId, input.content, {
|
|
70
|
-
documentId: input.documentId,
|
|
71
|
-
timestamp: input.timestamp,
|
|
72
|
-
context: input.context,
|
|
73
|
-
tags: retainOpts.tags,
|
|
74
|
-
metadata: retainOpts.metadata,
|
|
75
|
-
async: retainOpts.async ?? false,
|
|
76
|
-
});
|
|
77
|
-
return { success: result.success, itemsCount: result.items_count };
|
|
78
|
-
},
|
|
79
|
-
}),
|
|
80
|
-
recall: tool({
|
|
81
|
-
description: recallOpts.description ??
|
|
82
|
-
`Search memory for relevant information. Use this to find previously stored information that can help personalize responses or provide context.`,
|
|
83
|
-
inputSchema: recallParams,
|
|
84
|
-
execute: async (input) => {
|
|
85
|
-
const result = await client.recall(bankId, input.query, {
|
|
86
|
-
types: recallOpts.types,
|
|
87
|
-
maxTokens: recallOpts.maxTokens,
|
|
88
|
-
budget: recallOpts.budget ?? 'mid',
|
|
89
|
-
queryTimestamp: input.queryTimestamp,
|
|
90
|
-
includeEntities: recallOpts.includeEntities ?? false,
|
|
91
|
-
includeChunks: recallOpts.includeChunks ?? false,
|
|
92
|
-
});
|
|
93
|
-
return {
|
|
94
|
-
results: result.results ?? [],
|
|
95
|
-
entities: result.entities,
|
|
96
|
-
};
|
|
97
|
-
},
|
|
98
|
-
}),
|
|
99
|
-
reflect: tool({
|
|
100
|
-
description: reflectOpts.description ??
|
|
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.`,
|
|
102
|
-
inputSchema: reflectParams,
|
|
103
|
-
execute: async (input) => {
|
|
104
|
-
const result = await client.reflect(bankId, input.query, {
|
|
105
|
-
context: input.context,
|
|
106
|
-
budget: reflectOpts.budget ?? 'mid',
|
|
107
|
-
maxTokens: reflectOpts.maxTokens,
|
|
108
|
-
});
|
|
109
|
-
return {
|
|
110
|
-
text: result.text ?? 'No insights available yet.',
|
|
111
|
-
basedOn: result.based_on,
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
}),
|
|
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,
|
|
119
|
-
execute: async (input) => {
|
|
120
|
-
const result = await client.getMentalModel(bankId, input.mentalModelId);
|
|
121
|
-
return {
|
|
122
|
-
content: result.content ?? 'No content available yet.',
|
|
123
|
-
name: result.name,
|
|
124
|
-
updatedAt: result.updated_at,
|
|
125
|
-
};
|
|
126
|
-
},
|
|
127
|
-
}),
|
|
128
|
-
getDocument: tool({
|
|
129
|
-
description: getDocumentOpts.description ??
|
|
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.`,
|
|
131
|
-
inputSchema: getDocumentParams,
|
|
132
|
-
execute: async (input) => {
|
|
133
|
-
const result = await client.getDocument(bankId, input.documentId);
|
|
134
|
-
if (!result) {
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
originalText: result.original_text,
|
|
139
|
-
id: result.id,
|
|
140
|
-
createdAt: result.created_at,
|
|
141
|
-
updatedAt: result.updated_at,
|
|
142
|
-
};
|
|
143
|
-
},
|
|
144
|
-
}),
|
|
145
|
-
};
|
|
146
|
-
}
|