@cmdoss/memwal-sdk 0.7.0 → 0.8.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.
Files changed (192) hide show
  1. package/README.md +129 -0
  2. package/dist/client/ClientMemoryManager.js +2 -2
  3. package/dist/client/ClientMemoryManager.js.map +1 -1
  4. package/dist/client/PersonalDataWallet.d.ts.map +1 -1
  5. package/dist/client/SimplePDWClient.d.ts +28 -0
  6. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  7. package/dist/client/SimplePDWClient.js +29 -6
  8. package/dist/client/SimplePDWClient.js.map +1 -1
  9. package/dist/client/namespaces/MemoryNamespace.d.ts +4 -0
  10. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  11. package/dist/client/namespaces/MemoryNamespace.js +168 -39
  12. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  13. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts +12 -2
  14. package/dist/client/namespaces/consolidated/BlockchainNamespace.d.ts.map +1 -1
  15. package/dist/client/namespaces/consolidated/BlockchainNamespace.js +40 -2
  16. package/dist/client/namespaces/consolidated/BlockchainNamespace.js.map +1 -1
  17. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +67 -2
  18. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  19. package/dist/client/namespaces/consolidated/StorageNamespace.js +549 -16
  20. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  21. package/dist/config/ConfigurationHelper.js +61 -61
  22. package/dist/config/defaults.js +2 -2
  23. package/dist/config/defaults.js.map +1 -1
  24. package/dist/graph/GraphService.js +20 -20
  25. package/dist/infrastructure/seal/EncryptionService.d.ts +9 -5
  26. package/dist/infrastructure/seal/EncryptionService.d.ts.map +1 -1
  27. package/dist/infrastructure/seal/EncryptionService.js +37 -15
  28. package/dist/infrastructure/seal/EncryptionService.js.map +1 -1
  29. package/dist/infrastructure/seal/SealService.d.ts +13 -5
  30. package/dist/infrastructure/seal/SealService.d.ts.map +1 -1
  31. package/dist/infrastructure/seal/SealService.js +36 -34
  32. package/dist/infrastructure/seal/SealService.js.map +1 -1
  33. package/dist/langchain/createPDWRAG.js +30 -30
  34. package/dist/retrieval/MemoryDecryptionPipeline.d.ts.map +1 -1
  35. package/dist/retrieval/MemoryDecryptionPipeline.js +2 -1
  36. package/dist/retrieval/MemoryDecryptionPipeline.js.map +1 -1
  37. package/dist/services/CapabilityService.d.ts.map +1 -1
  38. package/dist/services/CapabilityService.js +30 -14
  39. package/dist/services/CapabilityService.js.map +1 -1
  40. package/dist/services/CrossContextPermissionService.d.ts.map +1 -1
  41. package/dist/services/CrossContextPermissionService.js +9 -7
  42. package/dist/services/CrossContextPermissionService.js.map +1 -1
  43. package/dist/services/EncryptionService.d.ts.map +1 -1
  44. package/dist/services/EncryptionService.js +6 -5
  45. package/dist/services/EncryptionService.js.map +1 -1
  46. package/dist/services/GeminiAIService.js +309 -309
  47. package/dist/services/StorageService.d.ts +1 -0
  48. package/dist/services/StorageService.d.ts.map +1 -1
  49. package/dist/services/StorageService.js +60 -10
  50. package/dist/services/StorageService.js.map +1 -1
  51. package/dist/services/TransactionService.d.ts +20 -0
  52. package/dist/services/TransactionService.d.ts.map +1 -1
  53. package/dist/services/TransactionService.js +43 -0
  54. package/dist/services/TransactionService.js.map +1 -1
  55. package/dist/services/ViewService.js +2 -2
  56. package/dist/services/ViewService.js.map +1 -1
  57. package/package.json +1 -1
  58. package/src/access/PermissionService.ts +635 -635
  59. package/src/access/index.ts +8 -8
  60. package/src/aggregation/AggregationService.ts +389 -389
  61. package/src/aggregation/index.ts +8 -8
  62. package/src/ai-sdk/PDWVectorStore.ts +715 -715
  63. package/src/ai-sdk/index.ts +65 -65
  64. package/src/ai-sdk/tools.ts +460 -460
  65. package/src/ai-sdk/types.ts +404 -404
  66. package/src/batch/BatchManager.ts +597 -597
  67. package/src/batch/BatchingService.ts +429 -429
  68. package/src/batch/MemoryProcessingCache.ts +492 -492
  69. package/src/batch/index.ts +30 -30
  70. package/src/browser.ts +200 -200
  71. package/src/client/ClientMemoryManager.ts +987 -987
  72. package/src/client/PersonalDataWallet.ts +345 -345
  73. package/src/client/SimplePDWClient.ts +1289 -1237
  74. package/src/client/factory.ts +154 -154
  75. package/src/client/namespaces/AnalyticsNamespace.ts +377 -377
  76. package/src/client/namespaces/BatchNamespace.ts +356 -356
  77. package/src/client/namespaces/CacheNamespace.ts +123 -123
  78. package/src/client/namespaces/CapabilityNamespace.ts +217 -217
  79. package/src/client/namespaces/ClassifyNamespace.ts +169 -169
  80. package/src/client/namespaces/ContextNamespace.ts +297 -297
  81. package/src/client/namespaces/EmbeddingsNamespace.ts +99 -99
  82. package/src/client/namespaces/EncryptionNamespace.ts +221 -221
  83. package/src/client/namespaces/GraphNamespace.ts +468 -468
  84. package/src/client/namespaces/IndexNamespace.ts +361 -361
  85. package/src/client/namespaces/MemoryNamespace.ts +1422 -1272
  86. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  87. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  88. package/src/client/namespaces/SearchNamespace.ts +1049 -1049
  89. package/src/client/namespaces/StorageNamespace.ts +458 -458
  90. package/src/client/namespaces/TxNamespace.ts +260 -260
  91. package/src/client/namespaces/WalletNamespace.ts +243 -243
  92. package/src/client/namespaces/consolidated/AINamespace.ts +449 -449
  93. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -564
  94. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  95. package/src/client/namespaces/consolidated/StorageNamespace.ts +1141 -497
  96. package/src/client/namespaces/consolidated/index.ts +39 -39
  97. package/src/client/signers/DappKitSigner.ts +207 -207
  98. package/src/client/signers/KeypairSigner.ts +108 -108
  99. package/src/client/signers/UnifiedSigner.ts +110 -110
  100. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  101. package/src/client/signers/index.ts +26 -26
  102. package/src/config/ConfigurationHelper.ts +412 -412
  103. package/src/config/defaults.ts +51 -51
  104. package/src/config/index.ts +8 -8
  105. package/src/config/validation.ts +70 -70
  106. package/src/core/index.ts +14 -14
  107. package/src/core/interfaces/IService.ts +307 -307
  108. package/src/core/interfaces/index.ts +8 -8
  109. package/src/core/types/capability.ts +297 -297
  110. package/src/core/types/index.ts +870 -870
  111. package/src/core/types/wallet.ts +270 -270
  112. package/src/core/types.ts +9 -9
  113. package/src/core/wallet.ts +222 -222
  114. package/src/embedding/index.ts +19 -19
  115. package/src/embedding/types.ts +357 -357
  116. package/src/errors/index.ts +602 -602
  117. package/src/errors/recovery.ts +461 -461
  118. package/src/errors/validation.ts +567 -567
  119. package/src/generated/pdw/capability.ts +319 -319
  120. package/src/generated/pdw/deps/sui/object.ts +12 -12
  121. package/src/generated/pdw/deps/sui/vec_map.ts +32 -32
  122. package/src/generated/pdw/memory.ts +1087 -1087
  123. package/src/generated/pdw/wallet.ts +123 -123
  124. package/src/generated/utils/index.ts +159 -159
  125. package/src/graph/GraphService.ts +887 -887
  126. package/src/graph/KnowledgeGraphManager.ts +728 -728
  127. package/src/graph/index.ts +25 -25
  128. package/src/index.ts +498 -498
  129. package/src/infrastructure/index.ts +22 -22
  130. package/src/infrastructure/seal/EncryptionService.ts +628 -603
  131. package/src/infrastructure/seal/SealService.ts +613 -615
  132. package/src/infrastructure/seal/index.ts +9 -9
  133. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  134. package/src/infrastructure/sui/SuiService.ts +888 -888
  135. package/src/infrastructure/sui/index.ts +9 -9
  136. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  137. package/src/infrastructure/walrus/WalrusStorageService.ts +612 -612
  138. package/src/infrastructure/walrus/index.ts +9 -9
  139. package/src/langchain/PDWEmbeddings.ts +145 -145
  140. package/src/langchain/PDWVectorStore.ts +456 -456
  141. package/src/langchain/createPDWRAG.ts +303 -303
  142. package/src/langchain/index.ts +47 -47
  143. package/src/permissions/ConsentRepository.browser.ts +249 -249
  144. package/src/permissions/ConsentRepository.ts +364 -364
  145. package/src/permissions/index.ts +9 -9
  146. package/src/pipeline/MemoryPipeline.ts +862 -862
  147. package/src/pipeline/PipelineManager.ts +683 -683
  148. package/src/pipeline/index.ts +26 -26
  149. package/src/retrieval/AdvancedSearchService.ts +629 -629
  150. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  151. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -824
  152. package/src/retrieval/MemoryRetrievalService.ts +904 -904
  153. package/src/retrieval/index.ts +42 -42
  154. package/src/services/BatchService.ts +352 -352
  155. package/src/services/CapabilityService.ts +464 -448
  156. package/src/services/ClassifierService.ts +465 -465
  157. package/src/services/CrossContextPermissionService.ts +486 -484
  158. package/src/services/EmbeddingService.ts +771 -771
  159. package/src/services/EncryptionService.ts +712 -711
  160. package/src/services/GeminiAIService.ts +753 -753
  161. package/src/services/IndexManager.ts +977 -977
  162. package/src/services/MemoryIndexService.ts +1003 -1003
  163. package/src/services/MemoryService.ts +369 -369
  164. package/src/services/QueryService.ts +890 -890
  165. package/src/services/StorageService.ts +1182 -1126
  166. package/src/services/TransactionService.ts +838 -790
  167. package/src/services/VectorService.ts +462 -462
  168. package/src/services/ViewService.ts +484 -484
  169. package/src/services/index.ts +25 -25
  170. package/src/services/storage/BlobAttributesManager.ts +333 -333
  171. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  172. package/src/services/storage/MemorySearchManager.ts +387 -387
  173. package/src/services/storage/QuiltBatchManager.ts +1130 -1130
  174. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  175. package/src/services/storage/WalrusStorageManager.ts +287 -287
  176. package/src/services/storage/index.ts +57 -57
  177. package/src/types/index.ts +13 -13
  178. package/src/utils/LRUCache.ts +378 -378
  179. package/src/utils/index.ts +76 -76
  180. package/src/utils/memoryIndexOnChain.ts +507 -507
  181. package/src/utils/rebuildIndex.ts +290 -290
  182. package/src/utils/rebuildIndexNode.ts +771 -771
  183. package/src/vector/BrowserHnswIndexService.ts +758 -758
  184. package/src/vector/HnswWasmService.ts +731 -731
  185. package/src/vector/IHnswService.ts +233 -233
  186. package/src/vector/NodeHnswService.ts +833 -833
  187. package/src/vector/VectorManager.ts +478 -478
  188. package/src/vector/createHnswService.ts +135 -135
  189. package/src/vector/index.ts +56 -56
  190. package/src/wallet/ContextWalletService.ts +656 -656
  191. package/src/wallet/MainWalletService.ts +317 -317
  192. package/src/wallet/index.ts +17 -17
@@ -1,460 +1,460 @@
1
- /**
2
- * PDW Tools for AI SDK - Phase 1 (MVP)
3
- *
4
- * Provides AI agents with tools to interact with Personal Data Wallet.
5
- * Tools automatically handle embeddings, storage, and search.
6
- *
7
- * @example
8
- * ```typescript
9
- * import { generateText } from 'ai';
10
- * import { google } from '@ai-sdk/google';
11
- * import { pdwTools } from 'personal-data-wallet-sdk/ai-sdk';
12
- *
13
- * const tools = pdwTools({
14
- * userId: 'user-123',
15
- * pdwConfig: {
16
- * walrus: { aggregator: '...' },
17
- * sui: { network: 'testnet', packageId: '...' },
18
- * signer,
19
- * userAddress,
20
- * dimensions: 3072 // Gemini embedding dimensions
21
- * }
22
- * });
23
- *
24
- * const result = await generateText({
25
- * model: google('gemini-2.0-flash-exp'),
26
- * tools,
27
- * prompt: "Remember that I love TypeScript"
28
- * });
29
- * ```
30
- *
31
- * @module ai-sdk/tools
32
- */
33
-
34
- import { tool, embed } from 'ai';
35
- import { z } from 'zod';
36
- import type { EmbeddingModel } from 'ai';
37
- import { PDWVectorStore } from './PDWVectorStore';
38
- import type { PDWVectorStoreConfig } from './types';
39
-
40
- /**
41
- * Configuration for pdwTools
42
- */
43
- export interface PDWToolsConfig {
44
- /**
45
- * User identifier for memory isolation
46
- */
47
- userId: string;
48
-
49
- /**
50
- * PDW Vector Store configuration
51
- */
52
- pdwConfig: PDWVectorStoreConfig;
53
-
54
- /**
55
- * Embedding model from AI SDK (e.g., google.textEmbeddingModel())
56
- * If not provided, tools that require embeddings will throw errors
57
- *
58
- * @example
59
- * ```typescript
60
- * import { google } from '@ai-sdk/google';
61
- *
62
- * const embedModel = google.textEmbeddingModel('text-embedding-004');
63
- * ```
64
- */
65
- embedModel?: EmbeddingModel<string>;
66
-
67
- /**
68
- * Which tools to enable
69
- * @default ['search_memory', 'save_memory', 'list_memories']
70
- */
71
- enabledTools?: Array<'search_memory' | 'save_memory' | 'list_memories'> | 'all';
72
-
73
- /**
74
- * Custom tool descriptions for better AI understanding
75
- */
76
- customDescriptions?: {
77
- search_memory?: string;
78
- save_memory?: string;
79
- list_memories?: string;
80
- };
81
- }
82
-
83
- /**
84
- * Internal state manager for PDW tools
85
- */
86
- class PDWToolsManager {
87
- private store: PDWVectorStore;
88
- private embedModel?: EmbeddingModel<string>;
89
- private userId: string;
90
- private config: PDWToolsConfig;
91
- private initialized: boolean = false;
92
- private initPromise: Promise<void> | null = null;
93
-
94
- constructor(config: PDWToolsConfig) {
95
- this.config = config;
96
- this.userId = config.userId;
97
- this.embedModel = config.embedModel;
98
-
99
- // Initialize PDW Vector Store
100
- this.store = new PDWVectorStore(config.pdwConfig);
101
-
102
- // Start async initialization
103
- this.initPromise = this.initialize();
104
- }
105
-
106
- /**
107
- * Initialize the tools (wait for store to be ready)
108
- */
109
- private async initialize(): Promise<void> {
110
- if (this.initialized) return;
111
-
112
- // Store initialization happens in its constructor
113
- // Just wait a bit for it to complete
114
- await new Promise(resolve => setTimeout(resolve, 100));
115
-
116
- this.initialized = true;
117
- }
118
-
119
- /**
120
- * Ensure tools are initialized before use
121
- */
122
- private async ensureInitialized(): Promise<void> {
123
- if (this.initPromise) {
124
- await this.initPromise;
125
- }
126
- }
127
-
128
- /**
129
- * Generate embedding for text using configured embed model
130
- */
131
- private async generateEmbedding(text: string): Promise<number[]> {
132
- if (!this.embedModel) {
133
- throw new Error(
134
- 'Embedding model not configured. Please provide embedModel in pdwTools config.\n' +
135
- 'Example: import { google } from "@ai-sdk/google"; const embedModel = google.textEmbeddingModel("text-embedding-004");'
136
- );
137
- }
138
-
139
- // Use AI SDK v5 embed function
140
- const result = await embed({
141
- model: this.embedModel,
142
- value: text
143
- });
144
-
145
- if (!result.embedding || result.embedding.length === 0) {
146
- throw new Error('Failed to generate embedding');
147
- }
148
-
149
- return result.embedding;
150
- }
151
-
152
- /**
153
- * Search memories by semantic similarity
154
- */
155
- async searchMemory(params: {
156
- query: string;
157
- limit?: number;
158
- minScore?: number;
159
- category?: string;
160
- startDate?: string;
161
- endDate?: string;
162
- }) {
163
- await this.ensureInitialized();
164
-
165
- try {
166
- const { query, limit = 5, minScore = 0.7, category, startDate, endDate } = params;
167
-
168
- // Generate query embedding
169
- const queryEmbedding = await this.generateEmbedding(query);
170
-
171
- // Build filters
172
- const filters: any = {};
173
- if (category) filters.category = category;
174
- // Time filters would be handled by metadata filtering
175
- // For now, we'll keep it simple
176
-
177
- // Search PDW
178
- const results = await this.store.search({
179
- vector: queryEmbedding,
180
- limit,
181
- minScore,
182
- filters,
183
- includeContent: true
184
- });
185
-
186
- if (results.length === 0) {
187
- return {
188
- found: false,
189
- message: `No memories found matching "${query}"`
190
- };
191
- }
192
-
193
- return {
194
- found: true,
195
- count: results.length,
196
- memories: results.map(r => ({
197
- id: r.id,
198
- text: r.text,
199
- score: r.score,
200
- category: r.metadata?.category || 'general',
201
- importance: r.metadata?.importance || 5,
202
- blobId: r.blobId
203
- }))
204
- };
205
- } catch (error) {
206
- console.error('search_memory error:', error);
207
- return {
208
- error: true,
209
- message: error instanceof Error ? error.message : 'Failed to search memories'
210
- };
211
- }
212
- }
213
-
214
- /**
215
- * Save new memory with automatic categorization
216
- */
217
- async saveMemory(params: {
218
- text: string;
219
- category?: 'fact' | 'preference' | 'todo' | 'note' | 'general';
220
- importance?: number;
221
- }) {
222
- await this.ensureInitialized();
223
-
224
- try {
225
- const { text, category = 'general', importance = 5 } = params;
226
-
227
- // Validate importance
228
- if (importance < 1 || importance > 10) {
229
- return {
230
- error: true,
231
- message: 'Importance must be between 1 and 10'
232
- };
233
- }
234
-
235
- // Generate embedding
236
- const embedding = await this.generateEmbedding(text);
237
-
238
- // Generate unique ID
239
- const id = `mem_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
240
-
241
- // Save to PDW
242
- const result = await this.store.add({
243
- id,
244
- vector: embedding,
245
- text,
246
- category,
247
- importance,
248
- metadata: {
249
- userId: this.userId,
250
- savedAt: new Date().toISOString(),
251
- category,
252
- importance
253
- }
254
- });
255
-
256
- return {
257
- success: true,
258
- memoryId: id,
259
- blobId: result.blobId,
260
- message: `Saved to memory: "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`
261
- };
262
- } catch (error) {
263
- console.error('save_memory error:', error);
264
- return {
265
- error: true,
266
- message: error instanceof Error ? error.message : 'Failed to save memory'
267
- };
268
- }
269
- }
270
-
271
- /**
272
- * List recent memories
273
- */
274
- async listMemories(params: {
275
- limit?: number;
276
- category?: string;
277
- }) {
278
- await this.ensureInitialized();
279
-
280
- try {
281
- const { limit = 10, category } = params;
282
-
283
- // Get store stats
284
- const stats = await this.store.stats();
285
-
286
- return {
287
- success: true,
288
- totalMemories: stats.totalVectors,
289
- message: `You have ${stats.totalVectors} stored memories` +
290
- (category ? ` in category "${category}"` : ''),
291
- stats: {
292
- totalVectors: stats.totalVectors,
293
- dimensions: stats.index.dimensions,
294
- distanceMetric: stats.index.distanceMetric
295
- }
296
- };
297
- } catch (error) {
298
- console.error('list_memories error:', error);
299
- return {
300
- error: true,
301
- message: error instanceof Error ? error.message : 'Failed to list memories'
302
- };
303
- }
304
- }
305
- }
306
-
307
- /**
308
- * Create PDW tools for AI SDK
309
- *
310
- * Returns a tools object that can be passed directly to generateText() or streamText().
311
- *
312
- * @param config - PDW tools configuration
313
- * @returns Tools object compatible with AI SDK
314
- *
315
- * @example
316
- * ```typescript
317
- * import { generateText } from 'ai';
318
- * import { google } from '@ai-sdk/google';
319
- * import { pdwTools } from 'personal-data-wallet-sdk/ai-sdk';
320
- *
321
- * // Create embedding model
322
- * const embedModel = google.textEmbeddingModel('text-embedding-004');
323
- *
324
- * // Create tools
325
- * const tools = pdwTools({
326
- * userId: 'user-123',
327
- * embedModel,
328
- * pdwConfig: {
329
- * walrus: { aggregator: process.env.WALRUS_AGGREGATOR! },
330
- * sui: { network: 'testnet', packageId: process.env.PACKAGE_ID! },
331
- * signer: keypair,
332
- * userAddress: keypair.toSuiAddress(),
333
- * dimensions: 3072 // Gemini text-embedding-004 dimensions
334
- * }
335
- * });
336
- *
337
- * // Use with AI
338
- * const result = await generateText({
339
- * model: google('gemini-2.0-flash-exp'),
340
- * tools,
341
- * prompt: "Remember that I love TypeScript and use it daily"
342
- * });
343
- * ```
344
- */
345
- export function pdwTools(config: PDWToolsConfig) {
346
- const manager = new PDWToolsManager(config);
347
-
348
- // Determine which tools to enable
349
- const enabledTools = config.enabledTools === 'all'
350
- ? ['search_memory', 'save_memory', 'list_memories']
351
- : (config.enabledTools || ['search_memory', 'save_memory', 'list_memories']);
352
-
353
- const tools: Record<string, any> = {};
354
-
355
- // Tool 1: search_memory
356
- if (enabledTools.includes('search_memory')) {
357
- tools.search_memory = tool({
358
- description: config.customDescriptions?.search_memory ||
359
- `Search through the user's personal memories using semantic similarity. ` +
360
- `Use this when the user asks about past conversations, stored facts, or wants to recall information. ` +
361
- `Examples: "What did I say about...", "Do you remember when...", "What do I know about..."`,
362
-
363
- inputSchema: z.object({
364
- query: z.string().describe('The search query text to find relevant memories'),
365
- limit: z.number().optional().describe('Maximum number of results to return (default: 5)'),
366
- minScore: z.number().optional().describe('Minimum similarity score 0-1 (default: 0.7)'),
367
- category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
368
- .describe('Filter by memory category'),
369
- startDate: z.string().optional().describe('Filter memories from this date (ISO format)'),
370
- endDate: z.string().optional().describe('Filter memories until this date (ISO format)')
371
- }),
372
-
373
- execute: async ({ query, limit, minScore, category, startDate, endDate }) =>
374
- manager.searchMemory({ query, limit, minScore, category, startDate, endDate })
375
- });
376
- }
377
-
378
- // Tool 2: save_memory
379
- if (enabledTools.includes('save_memory')) {
380
- tools.save_memory = tool({
381
- description: config.customDescriptions?.save_memory ||
382
- `Save important information to the user's personal memory. ` +
383
- `Use this when the user shares important facts, preferences, or information they want to remember. ` +
384
- `Examples: "Remember that...", "Save this...", "I prefer...", "My favorite is..."`,
385
-
386
- inputSchema: z.object({
387
- text: z.string().describe('The text content to save to memory'),
388
- category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
389
- .describe('Category of the memory (default: general)'),
390
- importance: z.number().min(1).max(10).optional()
391
- .describe('Importance level 1-10 (default: 5)')
392
- }),
393
-
394
- execute: async ({ text, category, importance }) =>
395
- manager.saveMemory({ text, category, importance })
396
- });
397
- }
398
-
399
- // Tool 3: list_memories
400
- if (enabledTools.includes('list_memories')) {
401
- tools.list_memories = tool({
402
- description: config.customDescriptions?.list_memories ||
403
- `Get information about stored memories. ` +
404
- `Use when the user asks "what do you know about me", "what have I told you", or wants a memory summary.`,
405
-
406
- inputSchema: z.object({
407
- limit: z.number().optional().describe('Number of memories to list (default: 10)'),
408
- category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
409
- .describe('Filter by category')
410
- }),
411
-
412
- execute: async ({ limit, category }) =>
413
- manager.listMemories({ limit, category })
414
- });
415
- }
416
-
417
- return tools;
418
- }
419
-
420
- /**
421
- * Type helper for extracting tool results
422
- */
423
- export type PDWToolResult = {
424
- search_memory: {
425
- found: boolean;
426
- message: string;
427
- count?: number;
428
- memories?: Array<{
429
- id: string;
430
- text: string;
431
- score: number;
432
- category: string;
433
- importance: number;
434
- blobId: string;
435
- }>;
436
- error?: boolean;
437
- };
438
- save_memory: {
439
- success: boolean;
440
- memoryId: string;
441
- blobId: string;
442
- message: string;
443
- } | {
444
- error: boolean;
445
- message: string;
446
- };
447
- list_memories: {
448
- success: boolean;
449
- totalMemories: number;
450
- message: string;
451
- stats: {
452
- totalVectors: number;
453
- dimensions: number;
454
- distanceMetric: string;
455
- };
456
- } | {
457
- error: boolean;
458
- message: string;
459
- };
460
- };
1
+ /**
2
+ * PDW Tools for AI SDK - Phase 1 (MVP)
3
+ *
4
+ * Provides AI agents with tools to interact with Personal Data Wallet.
5
+ * Tools automatically handle embeddings, storage, and search.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { generateText } from 'ai';
10
+ * import { google } from '@ai-sdk/google';
11
+ * import { pdwTools } from 'personal-data-wallet-sdk/ai-sdk';
12
+ *
13
+ * const tools = pdwTools({
14
+ * userId: 'user-123',
15
+ * pdwConfig: {
16
+ * walrus: { aggregator: '...' },
17
+ * sui: { network: 'testnet', packageId: '...' },
18
+ * signer,
19
+ * userAddress,
20
+ * dimensions: 3072 // Gemini embedding dimensions
21
+ * }
22
+ * });
23
+ *
24
+ * const result = await generateText({
25
+ * model: google('gemini-2.0-flash-exp'),
26
+ * tools,
27
+ * prompt: "Remember that I love TypeScript"
28
+ * });
29
+ * ```
30
+ *
31
+ * @module ai-sdk/tools
32
+ */
33
+
34
+ import { tool, embed } from 'ai';
35
+ import { z } from 'zod';
36
+ import type { EmbeddingModel } from 'ai';
37
+ import { PDWVectorStore } from './PDWVectorStore';
38
+ import type { PDWVectorStoreConfig } from './types';
39
+
40
+ /**
41
+ * Configuration for pdwTools
42
+ */
43
+ export interface PDWToolsConfig {
44
+ /**
45
+ * User identifier for memory isolation
46
+ */
47
+ userId: string;
48
+
49
+ /**
50
+ * PDW Vector Store configuration
51
+ */
52
+ pdwConfig: PDWVectorStoreConfig;
53
+
54
+ /**
55
+ * Embedding model from AI SDK (e.g., google.textEmbeddingModel())
56
+ * If not provided, tools that require embeddings will throw errors
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * import { google } from '@ai-sdk/google';
61
+ *
62
+ * const embedModel = google.textEmbeddingModel('text-embedding-004');
63
+ * ```
64
+ */
65
+ embedModel?: EmbeddingModel<string>;
66
+
67
+ /**
68
+ * Which tools to enable
69
+ * @default ['search_memory', 'save_memory', 'list_memories']
70
+ */
71
+ enabledTools?: Array<'search_memory' | 'save_memory' | 'list_memories'> | 'all';
72
+
73
+ /**
74
+ * Custom tool descriptions for better AI understanding
75
+ */
76
+ customDescriptions?: {
77
+ search_memory?: string;
78
+ save_memory?: string;
79
+ list_memories?: string;
80
+ };
81
+ }
82
+
83
+ /**
84
+ * Internal state manager for PDW tools
85
+ */
86
+ class PDWToolsManager {
87
+ private store: PDWVectorStore;
88
+ private embedModel?: EmbeddingModel<string>;
89
+ private userId: string;
90
+ private config: PDWToolsConfig;
91
+ private initialized: boolean = false;
92
+ private initPromise: Promise<void> | null = null;
93
+
94
+ constructor(config: PDWToolsConfig) {
95
+ this.config = config;
96
+ this.userId = config.userId;
97
+ this.embedModel = config.embedModel;
98
+
99
+ // Initialize PDW Vector Store
100
+ this.store = new PDWVectorStore(config.pdwConfig);
101
+
102
+ // Start async initialization
103
+ this.initPromise = this.initialize();
104
+ }
105
+
106
+ /**
107
+ * Initialize the tools (wait for store to be ready)
108
+ */
109
+ private async initialize(): Promise<void> {
110
+ if (this.initialized) return;
111
+
112
+ // Store initialization happens in its constructor
113
+ // Just wait a bit for it to complete
114
+ await new Promise(resolve => setTimeout(resolve, 100));
115
+
116
+ this.initialized = true;
117
+ }
118
+
119
+ /**
120
+ * Ensure tools are initialized before use
121
+ */
122
+ private async ensureInitialized(): Promise<void> {
123
+ if (this.initPromise) {
124
+ await this.initPromise;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Generate embedding for text using configured embed model
130
+ */
131
+ private async generateEmbedding(text: string): Promise<number[]> {
132
+ if (!this.embedModel) {
133
+ throw new Error(
134
+ 'Embedding model not configured. Please provide embedModel in pdwTools config.\n' +
135
+ 'Example: import { google } from "@ai-sdk/google"; const embedModel = google.textEmbeddingModel("text-embedding-004");'
136
+ );
137
+ }
138
+
139
+ // Use AI SDK v5 embed function
140
+ const result = await embed({
141
+ model: this.embedModel,
142
+ value: text
143
+ });
144
+
145
+ if (!result.embedding || result.embedding.length === 0) {
146
+ throw new Error('Failed to generate embedding');
147
+ }
148
+
149
+ return result.embedding;
150
+ }
151
+
152
+ /**
153
+ * Search memories by semantic similarity
154
+ */
155
+ async searchMemory(params: {
156
+ query: string;
157
+ limit?: number;
158
+ minScore?: number;
159
+ category?: string;
160
+ startDate?: string;
161
+ endDate?: string;
162
+ }) {
163
+ await this.ensureInitialized();
164
+
165
+ try {
166
+ const { query, limit = 5, minScore = 0.7, category, startDate, endDate } = params;
167
+
168
+ // Generate query embedding
169
+ const queryEmbedding = await this.generateEmbedding(query);
170
+
171
+ // Build filters
172
+ const filters: any = {};
173
+ if (category) filters.category = category;
174
+ // Time filters would be handled by metadata filtering
175
+ // For now, we'll keep it simple
176
+
177
+ // Search PDW
178
+ const results = await this.store.search({
179
+ vector: queryEmbedding,
180
+ limit,
181
+ minScore,
182
+ filters,
183
+ includeContent: true
184
+ });
185
+
186
+ if (results.length === 0) {
187
+ return {
188
+ found: false,
189
+ message: `No memories found matching "${query}"`
190
+ };
191
+ }
192
+
193
+ return {
194
+ found: true,
195
+ count: results.length,
196
+ memories: results.map(r => ({
197
+ id: r.id,
198
+ text: r.text,
199
+ score: r.score,
200
+ category: r.metadata?.category || 'general',
201
+ importance: r.metadata?.importance || 5,
202
+ blobId: r.blobId
203
+ }))
204
+ };
205
+ } catch (error) {
206
+ console.error('search_memory error:', error);
207
+ return {
208
+ error: true,
209
+ message: error instanceof Error ? error.message : 'Failed to search memories'
210
+ };
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Save new memory with automatic categorization
216
+ */
217
+ async saveMemory(params: {
218
+ text: string;
219
+ category?: 'fact' | 'preference' | 'todo' | 'note' | 'general';
220
+ importance?: number;
221
+ }) {
222
+ await this.ensureInitialized();
223
+
224
+ try {
225
+ const { text, category = 'general', importance = 5 } = params;
226
+
227
+ // Validate importance
228
+ if (importance < 1 || importance > 10) {
229
+ return {
230
+ error: true,
231
+ message: 'Importance must be between 1 and 10'
232
+ };
233
+ }
234
+
235
+ // Generate embedding
236
+ const embedding = await this.generateEmbedding(text);
237
+
238
+ // Generate unique ID
239
+ const id = `mem_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
240
+
241
+ // Save to PDW
242
+ const result = await this.store.add({
243
+ id,
244
+ vector: embedding,
245
+ text,
246
+ category,
247
+ importance,
248
+ metadata: {
249
+ userId: this.userId,
250
+ savedAt: new Date().toISOString(),
251
+ category,
252
+ importance
253
+ }
254
+ });
255
+
256
+ return {
257
+ success: true,
258
+ memoryId: id,
259
+ blobId: result.blobId,
260
+ message: `Saved to memory: "${text.substring(0, 50)}${text.length > 50 ? '...' : ''}"`
261
+ };
262
+ } catch (error) {
263
+ console.error('save_memory error:', error);
264
+ return {
265
+ error: true,
266
+ message: error instanceof Error ? error.message : 'Failed to save memory'
267
+ };
268
+ }
269
+ }
270
+
271
+ /**
272
+ * List recent memories
273
+ */
274
+ async listMemories(params: {
275
+ limit?: number;
276
+ category?: string;
277
+ }) {
278
+ await this.ensureInitialized();
279
+
280
+ try {
281
+ const { limit = 10, category } = params;
282
+
283
+ // Get store stats
284
+ const stats = await this.store.stats();
285
+
286
+ return {
287
+ success: true,
288
+ totalMemories: stats.totalVectors,
289
+ message: `You have ${stats.totalVectors} stored memories` +
290
+ (category ? ` in category "${category}"` : ''),
291
+ stats: {
292
+ totalVectors: stats.totalVectors,
293
+ dimensions: stats.index.dimensions,
294
+ distanceMetric: stats.index.distanceMetric
295
+ }
296
+ };
297
+ } catch (error) {
298
+ console.error('list_memories error:', error);
299
+ return {
300
+ error: true,
301
+ message: error instanceof Error ? error.message : 'Failed to list memories'
302
+ };
303
+ }
304
+ }
305
+ }
306
+
307
+ /**
308
+ * Create PDW tools for AI SDK
309
+ *
310
+ * Returns a tools object that can be passed directly to generateText() or streamText().
311
+ *
312
+ * @param config - PDW tools configuration
313
+ * @returns Tools object compatible with AI SDK
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * import { generateText } from 'ai';
318
+ * import { google } from '@ai-sdk/google';
319
+ * import { pdwTools } from 'personal-data-wallet-sdk/ai-sdk';
320
+ *
321
+ * // Create embedding model
322
+ * const embedModel = google.textEmbeddingModel('text-embedding-004');
323
+ *
324
+ * // Create tools
325
+ * const tools = pdwTools({
326
+ * userId: 'user-123',
327
+ * embedModel,
328
+ * pdwConfig: {
329
+ * walrus: { aggregator: process.env.WALRUS_AGGREGATOR! },
330
+ * sui: { network: 'testnet', packageId: process.env.PACKAGE_ID! },
331
+ * signer: keypair,
332
+ * userAddress: keypair.toSuiAddress(),
333
+ * dimensions: 3072 // Gemini text-embedding-004 dimensions
334
+ * }
335
+ * });
336
+ *
337
+ * // Use with AI
338
+ * const result = await generateText({
339
+ * model: google('gemini-2.0-flash-exp'),
340
+ * tools,
341
+ * prompt: "Remember that I love TypeScript and use it daily"
342
+ * });
343
+ * ```
344
+ */
345
+ export function pdwTools(config: PDWToolsConfig) {
346
+ const manager = new PDWToolsManager(config);
347
+
348
+ // Determine which tools to enable
349
+ const enabledTools = config.enabledTools === 'all'
350
+ ? ['search_memory', 'save_memory', 'list_memories']
351
+ : (config.enabledTools || ['search_memory', 'save_memory', 'list_memories']);
352
+
353
+ const tools: Record<string, any> = {};
354
+
355
+ // Tool 1: search_memory
356
+ if (enabledTools.includes('search_memory')) {
357
+ tools.search_memory = tool({
358
+ description: config.customDescriptions?.search_memory ||
359
+ `Search through the user's personal memories using semantic similarity. ` +
360
+ `Use this when the user asks about past conversations, stored facts, or wants to recall information. ` +
361
+ `Examples: "What did I say about...", "Do you remember when...", "What do I know about..."`,
362
+
363
+ inputSchema: z.object({
364
+ query: z.string().describe('The search query text to find relevant memories'),
365
+ limit: z.number().optional().describe('Maximum number of results to return (default: 5)'),
366
+ minScore: z.number().optional().describe('Minimum similarity score 0-1 (default: 0.7)'),
367
+ category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
368
+ .describe('Filter by memory category'),
369
+ startDate: z.string().optional().describe('Filter memories from this date (ISO format)'),
370
+ endDate: z.string().optional().describe('Filter memories until this date (ISO format)')
371
+ }),
372
+
373
+ execute: async ({ query, limit, minScore, category, startDate, endDate }) =>
374
+ manager.searchMemory({ query, limit, minScore, category, startDate, endDate })
375
+ });
376
+ }
377
+
378
+ // Tool 2: save_memory
379
+ if (enabledTools.includes('save_memory')) {
380
+ tools.save_memory = tool({
381
+ description: config.customDescriptions?.save_memory ||
382
+ `Save important information to the user's personal memory. ` +
383
+ `Use this when the user shares important facts, preferences, or information they want to remember. ` +
384
+ `Examples: "Remember that...", "Save this...", "I prefer...", "My favorite is..."`,
385
+
386
+ inputSchema: z.object({
387
+ text: z.string().describe('The text content to save to memory'),
388
+ category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
389
+ .describe('Category of the memory (default: general)'),
390
+ importance: z.number().min(1).max(10).optional()
391
+ .describe('Importance level 1-10 (default: 5)')
392
+ }),
393
+
394
+ execute: async ({ text, category, importance }) =>
395
+ manager.saveMemory({ text, category, importance })
396
+ });
397
+ }
398
+
399
+ // Tool 3: list_memories
400
+ if (enabledTools.includes('list_memories')) {
401
+ tools.list_memories = tool({
402
+ description: config.customDescriptions?.list_memories ||
403
+ `Get information about stored memories. ` +
404
+ `Use when the user asks "what do you know about me", "what have I told you", or wants a memory summary.`,
405
+
406
+ inputSchema: z.object({
407
+ limit: z.number().optional().describe('Number of memories to list (default: 10)'),
408
+ category: z.enum(['fact', 'preference', 'todo', 'note', 'general']).optional()
409
+ .describe('Filter by category')
410
+ }),
411
+
412
+ execute: async ({ limit, category }) =>
413
+ manager.listMemories({ limit, category })
414
+ });
415
+ }
416
+
417
+ return tools;
418
+ }
419
+
420
+ /**
421
+ * Type helper for extracting tool results
422
+ */
423
+ export type PDWToolResult = {
424
+ search_memory: {
425
+ found: boolean;
426
+ message: string;
427
+ count?: number;
428
+ memories?: Array<{
429
+ id: string;
430
+ text: string;
431
+ score: number;
432
+ category: string;
433
+ importance: number;
434
+ blobId: string;
435
+ }>;
436
+ error?: boolean;
437
+ };
438
+ save_memory: {
439
+ success: boolean;
440
+ memoryId: string;
441
+ blobId: string;
442
+ message: string;
443
+ } | {
444
+ error: boolean;
445
+ message: string;
446
+ };
447
+ list_memories: {
448
+ success: boolean;
449
+ totalMemories: number;
450
+ message: string;
451
+ stats: {
452
+ totalVectors: number;
453
+ dimensions: number;
454
+ distanceMetric: string;
455
+ };
456
+ } | {
457
+ error: boolean;
458
+ message: string;
459
+ };
460
+ };