@vectorize-io/hindsight-ai-sdk 0.4.19 → 0.5.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/README.md +8 -7
- package/dist/index.d.ts +26 -11
- package/dist/index.js.map +1 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -9,14 +9,14 @@ npm install @vectorize-io/hindsight-ai-sdk @vectorize-io/hindsight-client ai zod
|
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
```typescript
|
|
12
|
-
import { HindsightClient } from
|
|
13
|
-
import { createHindsightTools } from
|
|
14
|
-
import { generateText } from
|
|
15
|
-
import { anthropic } from
|
|
12
|
+
import { HindsightClient } from "@vectorize-io/hindsight-client";
|
|
13
|
+
import { createHindsightTools } from "@vectorize-io/hindsight-ai-sdk";
|
|
14
|
+
import { generateText } from "ai";
|
|
15
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
16
16
|
|
|
17
17
|
// 1. Initialize Hindsight client
|
|
18
18
|
const hindsightClient = new HindsightClient({
|
|
19
|
-
apiUrl:
|
|
19
|
+
apiUrl: "http://localhost:8000",
|
|
20
20
|
});
|
|
21
21
|
|
|
22
22
|
// 2. Create memory tools
|
|
@@ -24,13 +24,13 @@ const tools = createHindsightTools({ client: hindsightClient });
|
|
|
24
24
|
|
|
25
25
|
// 3. Use with AI SDK
|
|
26
26
|
const result = await generateText({
|
|
27
|
-
model: anthropic(
|
|
27
|
+
model: anthropic("claude-sonnet-4-20250514"),
|
|
28
28
|
tools,
|
|
29
29
|
system: `You have long-term memory. Use:
|
|
30
30
|
- 'recall' to search past conversations
|
|
31
31
|
- 'retain' to remember important information
|
|
32
32
|
- 'reflect' to synthesize insights from memories`,
|
|
33
|
-
prompt:
|
|
33
|
+
prompt: "Remember that Alice loves hiking and prefers spicy food",
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
console.log(result.text);
|
|
@@ -49,6 +49,7 @@ console.log(result.text);
|
|
|
49
49
|
📖 **[Full Documentation](https://vectorize.io/hindsight/sdks/integrations/ai-sdk)**
|
|
50
50
|
|
|
51
51
|
The complete documentation includes:
|
|
52
|
+
|
|
52
53
|
- Detailed tool descriptions and parameters
|
|
53
54
|
- Advanced usage patterns (streaming, multi-user, ToolLoopAgent)
|
|
54
55
|
- HTTP client example (no dependencies)
|
package/dist/index.d.ts
CHANGED
|
@@ -75,12 +75,27 @@ interface ReflectFact {
|
|
|
75
75
|
occurred_start?: string | null;
|
|
76
76
|
occurred_end?: string | null;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Nested based_on structure from Hindsight reflect
|
|
80
|
+
*/
|
|
81
|
+
interface ReflectBasedOn {
|
|
82
|
+
memories?: ReflectFact[];
|
|
83
|
+
mental_models?: Array<{
|
|
84
|
+
id: string;
|
|
85
|
+
name: string;
|
|
86
|
+
content?: string | null;
|
|
87
|
+
}>;
|
|
88
|
+
directives?: Array<{
|
|
89
|
+
id: string;
|
|
90
|
+
content: string;
|
|
91
|
+
}>;
|
|
92
|
+
}
|
|
78
93
|
/**
|
|
79
94
|
* Reflect response from Hindsight
|
|
80
95
|
*/
|
|
81
96
|
interface ReflectResponse {
|
|
82
97
|
text: string;
|
|
83
|
-
based_on?:
|
|
98
|
+
based_on?: ReflectBasedOn | null;
|
|
84
99
|
}
|
|
85
100
|
/**
|
|
86
101
|
* Retain response from Hindsight
|
|
@@ -101,15 +116,15 @@ interface MentalModelTrigger {
|
|
|
101
116
|
* Mental model response from Hindsight
|
|
102
117
|
*/
|
|
103
118
|
interface MentalModelResponse {
|
|
104
|
-
|
|
119
|
+
id: string;
|
|
105
120
|
bank_id: string;
|
|
106
|
-
name
|
|
107
|
-
content?: string;
|
|
108
|
-
source_query?: string;
|
|
121
|
+
name: string;
|
|
122
|
+
content?: string | null;
|
|
123
|
+
source_query?: string | null;
|
|
109
124
|
tags?: string[];
|
|
110
|
-
created_at
|
|
111
|
-
updated_at
|
|
112
|
-
trigger?: MentalModelTrigger;
|
|
125
|
+
created_at?: string | null;
|
|
126
|
+
updated_at?: string | null;
|
|
127
|
+
trigger?: MentalModelTrigger | null;
|
|
113
128
|
}
|
|
114
129
|
/**
|
|
115
130
|
* Document response from Hindsight
|
|
@@ -251,14 +266,14 @@ declare function createHindsightTools({ client, bankId, retain: retainOpts, reca
|
|
|
251
266
|
context?: string | undefined;
|
|
252
267
|
}, {
|
|
253
268
|
text: string;
|
|
254
|
-
basedOn?:
|
|
269
|
+
basedOn?: ReflectBasedOn | null;
|
|
255
270
|
}>;
|
|
256
271
|
getMentalModel: ai.Tool<{
|
|
257
272
|
mentalModelId: string;
|
|
258
273
|
}, {
|
|
259
274
|
content: string;
|
|
260
|
-
name
|
|
261
|
-
updatedAt
|
|
275
|
+
name: string;
|
|
276
|
+
updatedAt?: string | null;
|
|
262
277
|
}>;
|
|
263
278
|
getDocument: ai.Tool<{
|
|
264
279
|
documentId: string;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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":";AAAA,SAAS,YAAY;AACrB,SAAS,SAAS;AAKX,IAAM,eAAe,EAAE,KAAK,CAAC,OAAO,OAAO,MAAM,CAAC;AAMlD,IAAM,iBAAiB,EAAE,KAAK,CAAC,SAAS,cAAc,aAAa,CAAC;AAyPpE,SAAS,qBAAqB;AAAA,EACnC;AAAA,EACA;AAAA,EACA,QAAQ,aAAa,CAAC;AAAA,EACtB,QAAQ,aAAa,CAAC;AAAA,EACtB,SAAS,cAAc,CAAC;AAAA,EACxB,gBAAgB,qBAAqB,CAAC;AAAA,EACtC,aAAa,kBAAkB,CAAC;AAClC,GAA0B;AAExB,QAAM,eAAe,EAAE,OAAO;AAAA,IAC5B,SAAS,EAAE,OAAO,EAAE,SAAS,4BAA4B;AAAA,IACzD,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,qDAAqD;AAAA,IAChG,WAAW,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,qDAAqD;AAAA,IAC/F,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,mCAAmC;AAAA,EAC7E,CAAC;AAGD,QAAM,eAAe,EAAE,OAAO;AAAA,IAC5B,OAAO,EAAE,OAAO,EAAE,SAAS,8BAA8B;AAAA,IACzD,gBAAgB,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,kDAAkD;AAAA,EACnG,CAAC;AAGD,QAAM,gBAAgB,EAAE,OAAO;AAAA,IAC7B,OAAO,EAAE,OAAO,EAAE,SAAS,0CAA0C;AAAA,IACrE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,uCAAuC;AAAA,EACjF,CAAC;AAED,QA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,eACZ;AAAA,MACF,aAAa;AAAA,MACb,SAAS,OAAO,UAAU;AACxB,cAAM,SAAS,MAAM,OAAO,QAAQ,QAAQ,MAAM,OAAO;AAAA,UACvD,SAAS,MAAM;AAAA,UACf,QAAQ,YAAY,UAAU;AAAA,UAC9B,WAAW,YAAY;AAAA,QACzB,CAAC;AACD,eAAO;AAAA,UACL,MAAM,OAAO,QAAQ;AAAA,UACrB,SAAS,OAAO;AAAA,QAClB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,IAED,gBAAgB,KAAgD;AAAA,MAC9D,aACE,mBAAmB,eACnB;AAAA,MACF,aAAa;AAAA,MACb,SAAS,OAAO,UAAU;AACxB,cAAM,SAAS,MAAM,OAAO,eAAe,QAAQ,MAAM,aAAa;AACtE,eAAO;AAAA,UACL,SAAS,OAAO,WAAW;AAAA,UAC3B,MAAM,OAAO;AAAA,UACb,WAAW,OAAO;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,IAED,aAAa,KAA0C;AAAA,MACrD,aACE,gBAAgB,eAChB;AAAA,MACF,aAAa;AAAA,MACb,SAAS,OAAO,UAAU;AACxB,cAAM,SAAS,MAAM,OAAO,YAAY,QAAQ,MAAM,UAAU;AAChE,YAAI,CAAC,QAAQ;AACX,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,UACL,cAAc,OAAO;AAAA,UACrB,IAAI,OAAO;AAAA,UACX,WAAW,OAAO;AAAA,UAClB,WAAW,OAAO;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EAEH;AACF;","names":[]}
|
|
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 * Nested based_on structure from Hindsight reflect\n */\nexport interface ReflectBasedOn {\n memories?: ReflectFact[];\n mental_models?: Array<{ id: string; name: string; content?: string | null }>;\n directives?: Array<{ id: string; content: string }>;\n}\n\n/**\n * Reflect response from Hindsight\n */\nexport interface ReflectResponse {\n text: string;\n based_on?: ReflectBasedOn | null;\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 id: string;\n bank_id: string;\n name: string;\n content?: string | null;\n source_query?: string | null;\n tags?: string[];\n created_at?: string | null;\n updated_at?: string | null;\n trigger?: MentalModelTrigger | null;\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(bankId: string, mentalModelId: string): Promise<MentalModelResponse>;\n\n getDocument(bankId: string, documentId: string): 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\n .string()\n .optional()\n .describe(\"Optional document ID for grouping/upserting content\"),\n timestamp: z\n .string()\n .optional()\n .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\n .string()\n .optional()\n .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?: ReflectBasedOn | null };\n\n type GetMentalModelInput = z.infer<typeof getMentalModelParams>;\n type GetMentalModelOutput = { content: string; name: string; updatedAt?: string | null };\n\n type GetDocumentInput = z.infer<typeof getDocumentParams>;\n type GetDocumentOutput = {\n originalText: string;\n id: string;\n createdAt: string;\n updatedAt: string;\n } | 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\nexport type HindsightTools = ReturnType<typeof createHindsightTools>;\n"],"mappings":";AAAA,SAAS,YAAY;AACrB,SAAS,SAAS;AAKX,IAAM,eAAe,EAAE,KAAK,CAAC,OAAO,OAAO,MAAM,CAAC;AAMlD,IAAM,iBAAiB,EAAE,KAAK,CAAC,SAAS,cAAc,aAAa,CAAC;AA4PpE,SAAS,qBAAqB;AAAA,EACnC;AAAA,EACA;AAAA,EACA,QAAQ,aAAa,CAAC;AAAA,EACtB,QAAQ,aAAa,CAAC;AAAA,EACtB,SAAS,cAAc,CAAC;AAAA,EACxB,gBAAgB,qBAAqB,CAAC;AAAA,EACtC,aAAa,kBAAkB,CAAC;AAClC,GAA0B;AAExB,QAAM,eAAe,EAAE,OAAO;AAAA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ChB;AAAA,MACF,aAAa;AAAA,MACb,SAAS,OAAO,UAAU;AACxB,cAAM,SAAS,MAAM,OAAO,YAAY,QAAQ,MAAM,UAAU;AAChE,YAAI,CAAC,QAAQ;AACX,iBAAO;AAAA,QACT;AACA,eAAO;AAAA,UACL,cAAc,OAAO;AAAA,UACrB,IAAI,OAAO;AAAA,UACX,WAAW,OAAO;AAAA,UAClB,WAAW,OAAO;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vectorize-io/hindsight-ai-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
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",
|
|
@@ -47,17 +47,19 @@
|
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^22.0.0",
|
|
50
|
-
"@vitest/ui": "^4.
|
|
50
|
+
"@vitest/ui": "^4.1.2",
|
|
51
51
|
"ai": "^6.0.2",
|
|
52
52
|
"tsup": "^8.5.1",
|
|
53
53
|
"typescript": "^5.7.0",
|
|
54
|
-
"vitest": "^4.
|
|
54
|
+
"vitest": "^4.1.2",
|
|
55
55
|
"zod": "^4.2.0"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=22"
|
|
59
59
|
},
|
|
60
60
|
"overrides": {
|
|
61
|
-
"rollup": "^4.59.0"
|
|
61
|
+
"rollup": "^4.59.0",
|
|
62
|
+
"picomatch": ">=2.3.2 <3.0.0 || >=4.0.4",
|
|
63
|
+
"vite": ">=8.0.5"
|
|
62
64
|
}
|
|
63
65
|
}
|