@cmdoss/memwal-sdk 0.9.0 → 1.0.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 (174) hide show
  1. package/README.md +157 -52
  2. package/dist/client/ClientMemoryManager.d.ts.map +1 -1
  3. package/dist/client/ClientMemoryManager.js +25 -8
  4. package/dist/client/ClientMemoryManager.js.map +1 -1
  5. package/dist/client/PersonalDataWallet.d.ts.map +1 -1
  6. package/dist/client/SimplePDWClient.d.ts +2 -1
  7. package/dist/client/SimplePDWClient.d.ts.map +1 -1
  8. package/dist/client/SimplePDWClient.js +23 -6
  9. package/dist/client/SimplePDWClient.js.map +1 -1
  10. package/dist/client/namespaces/MemoryNamespace.d.ts +6 -0
  11. package/dist/client/namespaces/MemoryNamespace.d.ts.map +1 -1
  12. package/dist/client/namespaces/MemoryNamespace.js +131 -18
  13. package/dist/client/namespaces/MemoryNamespace.js.map +1 -1
  14. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts +3 -1
  15. package/dist/client/namespaces/consolidated/StorageNamespace.d.ts.map +1 -1
  16. package/dist/client/namespaces/consolidated/StorageNamespace.js.map +1 -1
  17. package/dist/config/ConfigurationHelper.js +61 -61
  18. package/dist/config/index.d.ts +1 -0
  19. package/dist/config/index.d.ts.map +1 -1
  20. package/dist/config/index.js +2 -0
  21. package/dist/config/index.js.map +1 -1
  22. package/dist/config/modelDefaults.d.ts +67 -0
  23. package/dist/config/modelDefaults.d.ts.map +1 -0
  24. package/dist/config/modelDefaults.js +91 -0
  25. package/dist/config/modelDefaults.js.map +1 -0
  26. package/dist/graph/GraphService.d.ts.map +1 -1
  27. package/dist/graph/GraphService.js +22 -21
  28. package/dist/graph/GraphService.js.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/langchain/createPDWRAG.js +30 -30
  34. package/dist/pipeline/MemoryPipeline.d.ts.map +1 -1
  35. package/dist/pipeline/MemoryPipeline.js +2 -1
  36. package/dist/pipeline/MemoryPipeline.js.map +1 -1
  37. package/dist/services/GeminiAIService.d.ts.map +1 -1
  38. package/dist/services/GeminiAIService.js +311 -310
  39. package/dist/services/GeminiAIService.js.map +1 -1
  40. package/dist/services/StorageService.d.ts +4 -1
  41. package/dist/services/StorageService.d.ts.map +1 -1
  42. package/dist/services/StorageService.js.map +1 -1
  43. package/dist/services/storage/QuiltBatchManager.d.ts +7 -0
  44. package/dist/services/storage/QuiltBatchManager.d.ts.map +1 -1
  45. package/dist/services/storage/QuiltBatchManager.js +24 -5
  46. package/dist/services/storage/QuiltBatchManager.js.map +1 -1
  47. package/dist/services/storage/WalrusStorageManager.d.ts +10 -1
  48. package/dist/services/storage/WalrusStorageManager.d.ts.map +1 -1
  49. package/dist/services/storage/WalrusStorageManager.js +53 -12
  50. package/dist/services/storage/WalrusStorageManager.js.map +1 -1
  51. package/dist/vector/BrowserHnswIndexService.js +2 -2
  52. package/dist/vector/BrowserHnswIndexService.js.map +1 -1
  53. package/dist/vector/NodeHnswService.js +4 -4
  54. package/dist/vector/NodeHnswService.js.map +1 -1
  55. package/dist/vector/createHnswService.d.ts +4 -0
  56. package/dist/vector/createHnswService.d.ts.map +1 -1
  57. package/dist/vector/createHnswService.js +15 -3
  58. package/dist/vector/createHnswService.js.map +1 -1
  59. package/package.json +1 -1
  60. package/src/access/PermissionService.ts +635 -635
  61. package/src/aggregation/AggregationService.ts +389 -389
  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 +1004 -987
  72. package/src/client/PersonalDataWallet.ts +345 -345
  73. package/src/client/SimplePDWClient.ts +1387 -1369
  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/EncryptionNamespace.ts +221 -221
  82. package/src/client/namespaces/GraphNamespace.ts +468 -468
  83. package/src/client/namespaces/IndexNamespace.ts +364 -364
  84. package/src/client/namespaces/MemoryNamespace.ts +1704 -1569
  85. package/src/client/namespaces/PermissionsNamespace.ts +254 -254
  86. package/src/client/namespaces/PipelineNamespace.ts +220 -220
  87. package/src/client/namespaces/StorageNamespace.ts +458 -458
  88. package/src/client/namespaces/TxNamespace.ts +260 -260
  89. package/src/client/namespaces/WalletNamespace.ts +243 -243
  90. package/src/client/namespaces/consolidated/BlockchainNamespace.ts +607 -607
  91. package/src/client/namespaces/consolidated/SecurityNamespace.ts +648 -648
  92. package/src/client/namespaces/consolidated/StorageNamespace.ts +1143 -1141
  93. package/src/client/namespaces/consolidated/index.ts +41 -41
  94. package/src/client/signers/KeypairSigner.ts +108 -108
  95. package/src/client/signers/UnifiedSigner.ts +110 -110
  96. package/src/client/signers/WalletAdapterSigner.ts +159 -159
  97. package/src/client/signers/index.ts +26 -26
  98. package/src/config/ConfigurationHelper.ts +412 -412
  99. package/src/config/defaults.ts +56 -56
  100. package/src/config/index.ts +16 -9
  101. package/src/config/modelDefaults.ts +103 -0
  102. package/src/config/validation.ts +70 -70
  103. package/src/core/index.ts +14 -14
  104. package/src/core/interfaces/IService.ts +307 -307
  105. package/src/core/interfaces/index.ts +8 -8
  106. package/src/core/types/capability.ts +297 -297
  107. package/src/core/types/index.ts +874 -874
  108. package/src/core/types/wallet.ts +270 -270
  109. package/src/core/types.ts +9 -9
  110. package/src/core/wallet.ts +222 -222
  111. package/src/embedding/index.ts +19 -19
  112. package/src/embedding/types.ts +357 -357
  113. package/src/errors/index.ts +602 -602
  114. package/src/errors/recovery.ts +461 -461
  115. package/src/errors/validation.ts +567 -567
  116. package/src/generated/pdw/capability.ts +319 -319
  117. package/src/graph/GraphService.ts +888 -887
  118. package/src/graph/KnowledgeGraphManager.ts +728 -728
  119. package/src/graph/index.ts +25 -25
  120. package/src/index.ts +498 -498
  121. package/src/infrastructure/index.ts +22 -22
  122. package/src/infrastructure/seal/EncryptionService.ts +628 -628
  123. package/src/infrastructure/seal/SealService.ts +613 -613
  124. package/src/infrastructure/seal/index.ts +9 -9
  125. package/src/infrastructure/sui/BlockchainManager.ts +627 -627
  126. package/src/infrastructure/sui/SuiService.ts +888 -888
  127. package/src/infrastructure/sui/index.ts +9 -9
  128. package/src/infrastructure/walrus/StorageManager.ts +604 -604
  129. package/src/infrastructure/walrus/WalrusStorageService.ts +637 -637
  130. package/src/infrastructure/walrus/index.ts +9 -9
  131. package/src/langchain/createPDWRAG.ts +303 -303
  132. package/src/langchain/index.ts +47 -47
  133. package/src/permissions/ConsentRepository.browser.ts +249 -249
  134. package/src/permissions/ConsentRepository.ts +364 -364
  135. package/src/pipeline/MemoryPipeline.ts +863 -862
  136. package/src/pipeline/PipelineManager.ts +683 -683
  137. package/src/pipeline/index.ts +26 -26
  138. package/src/retrieval/AdvancedSearchService.ts +629 -629
  139. package/src/retrieval/MemoryAnalyticsService.ts +711 -711
  140. package/src/retrieval/MemoryDecryptionPipeline.ts +825 -825
  141. package/src/retrieval/index.ts +42 -42
  142. package/src/services/BatchService.ts +352 -352
  143. package/src/services/CapabilityService.ts +464 -464
  144. package/src/services/ClassifierService.ts +465 -465
  145. package/src/services/CrossContextPermissionService.ts +486 -486
  146. package/src/services/EmbeddingService.ts +796 -796
  147. package/src/services/EncryptionService.ts +712 -712
  148. package/src/services/GeminiAIService.ts +754 -753
  149. package/src/services/MemoryIndexService.ts +1009 -1009
  150. package/src/services/MemoryService.ts +369 -369
  151. package/src/services/QueryService.ts +890 -890
  152. package/src/services/StorageService.ts +1185 -1182
  153. package/src/services/TransactionService.ts +838 -838
  154. package/src/services/VectorService.ts +462 -462
  155. package/src/services/ViewService.ts +484 -484
  156. package/src/services/index.ts +25 -25
  157. package/src/services/storage/BlobAttributesManager.ts +333 -333
  158. package/src/services/storage/KnowledgeGraphManager.ts +425 -425
  159. package/src/services/storage/MemorySearchManager.ts +387 -387
  160. package/src/services/storage/QuiltBatchManager.ts +1157 -1130
  161. package/src/services/storage/WalrusMetadataManager.ts +268 -268
  162. package/src/services/storage/WalrusStorageManager.ts +333 -287
  163. package/src/services/storage/index.ts +57 -57
  164. package/src/types/index.ts +13 -13
  165. package/src/utils/index.ts +76 -76
  166. package/src/utils/memoryIndexOnChain.ts +507 -507
  167. package/src/vector/BrowserHnswIndexService.ts +758 -758
  168. package/src/vector/HnswWasmService.ts +731 -731
  169. package/src/vector/IHnswService.ts +233 -233
  170. package/src/vector/NodeHnswService.ts +833 -833
  171. package/src/vector/createHnswService.ts +147 -135
  172. package/src/vector/index.ts +56 -56
  173. package/src/wallet/ContextWalletService.ts +656 -656
  174. package/src/wallet/MainWalletService.ts +317 -317
@@ -9,6 +9,7 @@
9
9
  * Refactored to use official @openrouter/sdk instead of raw fetch calls.
10
10
  */
11
11
  import { OpenRouter } from '@openrouter/sdk';
12
+ import { getChatModel } from '../config/modelDefaults.js';
12
13
  /**
13
14
  * AI service for advanced text analysis and knowledge extraction
14
15
  * Uses OpenRouter SDK for maximum flexibility and model choice
@@ -21,7 +22,7 @@ export class GeminiAIService {
21
22
  throw new Error('API key is required. Set OPENROUTER_API_KEY environment variable or provide apiKey in config.');
22
23
  }
23
24
  this.config = {
24
- model: config.model || process.env.AI_CHAT_MODEL || 'google/gemini-2.5-flash',
25
+ model: getChatModel(config.model),
25
26
  temperature: config.temperature || 0.1,
26
27
  maxTokens: config.maxTokens || 4096,
27
28
  timeout: config.timeout || 30000,
@@ -130,15 +131,15 @@ export class GeminiAIService {
130
131
  */
131
132
  async analyzeContent(content) {
132
133
  try {
133
- const prompt = `
134
- Analyze the following text and provide a JSON response with:
135
- - "categories": array of relevant categories (max 3)
136
- - "sentiment": "positive", "negative", or "neutral"
137
- - "topics": array of main topics/themes (max 5)
138
- - "confidence": overall analysis confidence (0.0-1.0)
139
-
140
- TEXT: ${content}
141
-
134
+ const prompt = `
135
+ Analyze the following text and provide a JSON response with:
136
+ - "categories": array of relevant categories (max 3)
137
+ - "sentiment": "positive", "negative", or "neutral"
138
+ - "topics": array of main topics/themes (max 5)
139
+ - "confidence": overall analysis confidence (0.0-1.0)
140
+
141
+ TEXT: ${content}
142
+
142
143
  JSON:`;
143
144
  const text = await this.callOpenRouter(prompt);
144
145
  return this.parseAnalysisResponse(text);
@@ -156,291 +157,291 @@ JSON:`;
156
157
  // ==================== PRIVATE METHODS ====================
157
158
  buildExtractionPrompt(content, context) {
158
159
  const contextSection = context ? `\nCONTEXT: ${context}\n` : '';
159
- return `
160
- You are a knowledge graph extraction system for a Personal Data Wallet application. Your task is to extract meaningful entities and relationships from personal memories, notes, and user statements.
161
-
162
- ## CRITICAL RULE: User Entity
163
- ALWAYS include a "user" entity to represent the person who wrote this memory:
164
- {
165
- "id": "user",
166
- "label": "User",
167
- "type": "person",
168
- "confidence": 1.0
169
- }
170
- This "user" entity should be the source or target of relationships describing personal preferences, attributes, or experiences.
171
-
172
- ## Entity Types (Comprehensive List)
173
-
174
- ### People & Social
175
- - **person**: Individual people, including the user themselves
176
- - Examples: "user", "john_doe", "my_mother", "boss"
177
- - Properties: name, role, relationship_to_user
178
-
179
- ### Organizations & Groups
180
- - **organization**: Companies, institutions, teams, communities
181
- - Examples: "google", "harvard_university", "local_gym"
182
- - Properties: industry, size, location
183
-
184
- ### Locations & Places
185
- - **location**: Geographic places, addresses, venues
186
- - Examples: "ho_chi_minh_city", "vietnam", "my_office", "central_park"
187
- - Properties: type (city/country/venue), coordinates
188
-
189
- ### Food & Dining
190
- - **food**: Foods, dishes, cuisines, beverages, ingredients
191
- - Examples: "spaghetti", "vietnamese_cuisine", "coffee", "chocolate"
192
- - Properties: cuisine_type, meal_type, dietary_info
193
- - **restaurant**: Eating establishments
194
- - Examples: "starbucks", "local_pho_shop"
195
- - Properties: cuisine, price_range
196
-
197
- ### Preferences & Interests
198
- - **preference**: General likes, dislikes, favorites
199
- - Examples: "blue_color", "morning_routine", "minimalist_style"
200
- - Properties: sentiment (positive/negative/neutral), intensity (1-10)
201
- - **hobby**: Recreational activities, pastimes
202
- - Examples: "playing_guitar", "photography", "hiking", "gaming"
203
- - Properties: frequency, skill_level
204
- - **interest**: Topics of curiosity or passion
205
- - Examples: "artificial_intelligence", "history", "cooking"
206
- - Properties: depth (casual/moderate/deep)
207
-
208
- ### Skills & Abilities
209
- - **skill**: Technical or soft skills, expertise areas
210
- - Examples: "python_programming", "public_speaking", "cooking"
211
- - Properties: proficiency (beginner/intermediate/expert)
212
- - **language**: Languages known or being learned
213
- - Examples: "english", "vietnamese", "japanese"
214
- - Properties: proficiency, native (true/false)
215
-
216
- ### Objects & Possessions
217
- - **object**: Physical items, products, tools
218
- - Examples: "macbook_pro", "my_car", "guitar"
219
- - Properties: brand, model, acquisition_date
220
- - **digital_product**: Software, apps, digital services
221
- - Examples: "spotify", "notion", "chatgpt"
222
- - Properties: category, usage_frequency
223
-
224
- ### Time & Events
225
- - **event**: Occasions, milestones, meetings
226
- - Examples: "birthday_2024", "job_interview", "vacation_trip"
227
- - Properties: date, duration, importance
228
- - **routine**: Regular activities or habits
229
- - Examples: "morning_workout", "weekly_meeting", "daily_meditation"
230
- - Properties: frequency, time_of_day
231
-
232
- ### Abstract & Conceptual
233
- - **concept**: Ideas, topics, abstract things
234
- - Examples: "work_life_balance", "productivity", "happiness"
235
- - **goal**: Objectives, aspirations, plans
236
- - Examples: "learn_japanese", "run_marathon", "save_money"
237
- - Properties: deadline, priority, status
238
- - **emotion**: Feelings, moods, emotional states
239
- - Examples: "happiness", "stress", "excitement"
240
- - Properties: intensity, trigger
241
-
242
- ### Health & Wellness
243
- - **health_condition**: Medical conditions, allergies
244
- - Examples: "lactose_intolerance", "migraine", "allergy_to_peanuts"
245
- - **medication**: Medicines, supplements
246
- - Examples: "vitamin_d", "aspirin"
247
- - **exercise**: Physical activities for health
248
- - Examples: "running", "yoga", "weight_training"
249
-
250
- ### Media & Entertainment
251
- - **music**: Songs, artists, genres, albums
252
- - Examples: "jazz_music", "beatles", "classical_piano"
253
- - **movie**: Films, TV shows, documentaries
254
- - Examples: "inception", "game_of_thrones"
255
- - **book**: Books, authors, genres
256
- - Examples: "atomic_habits", "fiction_genre"
257
- - **game**: Video games, board games
258
- - Examples: "chess", "minecraft"
259
-
260
- ## Relationship Types (Comprehensive List)
261
-
262
- ### Preference Relationships (source: usually "user")
263
- - **loves**: Strong positive preference (intensity 9-10)
264
- - **likes**: Moderate positive preference (intensity 6-8)
265
- - **enjoys**: Positive experience with something
266
- - **prefers**: Comparative preference
267
- - **favorite**: Top choice in a category
268
- - **interested_in**: Curiosity or engagement
269
- - **dislikes**: Moderate negative preference
270
- - **hates**: Strong negative preference (intensity 9-10)
271
- - **avoids**: Intentionally stays away from
272
- - **allergic_to**: Medical/physical aversion
273
-
274
- ### Affiliation Relationships
275
- - **works_at**: Employment relationship
276
- - **studies_at**: Educational institution
277
- - **member_of**: Group membership
278
- - **belongs_to**: General affiliation
279
- - **founded**: Created an organization
280
- - **leads**: Leadership role
281
-
282
- ### Location Relationships
283
- - **lives_in**: Current residence
284
- - **from**: Origin/hometown
285
- - **located_in**: Physical location
286
- - **visited**: Past travel
287
- - **wants_to_visit**: Travel aspiration
288
-
289
- ### Social Relationships
290
- - **knows**: Acquaintance
291
- - **friends_with**: Friendship
292
- - **family_of**: Family relationship (specify: parent, sibling, child, spouse)
293
- - **works_with**: Professional relationship
294
- - **mentored_by**: Learning relationship
295
-
296
- ### Skill & Knowledge Relationships
297
- - **has_skill**: Possesses ability
298
- - **expert_in**: High proficiency
299
- - **learning**: Currently acquiring
300
- - **wants_to_learn**: Aspiration to learn
301
- - **teaches**: Instructing others
302
- - **certified_in**: Formal qualification
303
-
304
- ### Possession & Usage
305
- - **owns**: Ownership
306
- - **uses**: Regular usage
307
- - **wants**: Desire to acquire
308
- - **recommends**: Positive endorsement
309
-
310
- ### Temporal Relationships
311
- - **started_on**: Beginning date
312
- - **ended_on**: Ending date
313
- - **scheduled_for**: Future event
314
- - **happens_during**: Temporal context
315
-
316
- ### Causal & Descriptive
317
- - **causes**: Causal relationship
318
- - **related_to**: General association
319
- - **part_of**: Component relationship
320
- - **similar_to**: Similarity
321
- - **opposite_of**: Contrast
322
-
323
- ## Output Format
324
-
325
- Return ONLY valid JSON with this structure:
326
- {
327
- "entities": [
328
- {
329
- "id": "snake_case_identifier",
330
- "label": "Human Readable Name",
331
- "type": "entity_type_from_list_above",
332
- "confidence": 0.0-1.0,
333
- "properties": { "optional": "attributes" }
334
- }
335
- ],
336
- "relationships": [
337
- {
338
- "source": "source_entity_id",
339
- "target": "target_entity_id",
340
- "label": "relationship_type_from_list_above",
341
- "confidence": 0.0-1.0,
342
- "type": "optional_category"
343
- }
344
- ]
345
- }
346
-
347
- ## Examples
348
-
349
- ### Example 1: Food Preference
350
- Input: "i love spaghetti"
351
- Output:
352
- {
353
- "entities": [
354
- {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
355
- {"id": "spaghetti", "label": "Spaghetti", "type": "food", "confidence": 0.95, "properties": {"cuisine": "italian", "meal_type": "main_course"}}
356
- ],
357
- "relationships": [
358
- {"source": "user", "target": "spaghetti", "label": "loves", "confidence": 0.95, "type": "preference"}
359
- ]
360
- }
361
-
362
- ### Example 2: Multiple Preferences
363
- Input: "i like hamburgers but hate vegetables"
364
- Output:
365
- {
366
- "entities": [
367
- {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
368
- {"id": "hamburgers", "label": "Hamburgers", "type": "food", "confidence": 0.95},
369
- {"id": "vegetables", "label": "Vegetables", "type": "food", "confidence": 0.95}
370
- ],
371
- "relationships": [
372
- {"source": "user", "target": "hamburgers", "label": "likes", "confidence": 0.9, "type": "preference"},
373
- {"source": "user", "target": "vegetables", "label": "dislikes", "confidence": 0.9, "type": "preference"}
374
- ]
375
- }
376
-
377
- ### Example 3: Work Information
378
- Input: "i work at Google as a software engineer in Mountain View"
379
- Output:
380
- {
381
- "entities": [
382
- {"id": "user", "label": "User", "type": "person", "confidence": 1.0, "properties": {"role": "software_engineer"}},
383
- {"id": "google", "label": "Google", "type": "organization", "confidence": 0.98, "properties": {"industry": "technology"}},
384
- {"id": "software_engineer", "label": "Software Engineer", "type": "skill", "confidence": 0.9},
385
- {"id": "mountain_view", "label": "Mountain View", "type": "location", "confidence": 0.95, "properties": {"type": "city"}}
386
- ],
387
- "relationships": [
388
- {"source": "user", "target": "google", "label": "works_at", "confidence": 0.98, "type": "affiliation"},
389
- {"source": "user", "target": "software_engineer", "label": "has_skill", "confidence": 0.9, "type": "skill"},
390
- {"source": "google", "target": "mountain_view", "label": "located_in", "confidence": 0.85, "type": "location"}
391
- ]
392
- }
393
-
394
- ### Example 4: Personal Life
395
- Input: "my name is Aaron and I live in Ho Chi Minh City with my wife"
396
- Output:
397
- {
398
- "entities": [
399
- {"id": "user", "label": "Aaron", "type": "person", "confidence": 1.0, "properties": {"name": "Aaron"}},
400
- {"id": "ho_chi_minh_city", "label": "Ho Chi Minh City", "type": "location", "confidence": 0.98, "properties": {"type": "city", "country": "Vietnam"}},
401
- {"id": "wife", "label": "Wife", "type": "person", "confidence": 0.9, "properties": {"relationship": "spouse"}}
402
- ],
403
- "relationships": [
404
- {"source": "user", "target": "ho_chi_minh_city", "label": "lives_in", "confidence": 0.98, "type": "location"},
405
- {"source": "user", "target": "wife", "label": "family_of", "confidence": 0.95, "type": "social", "properties": {"relationship_type": "spouse"}}
406
- ]
407
- }
408
-
409
- ### Example 5: Hobbies and Interests
410
- Input: "i enjoy playing guitar and listening to jazz music on weekends"
411
- Output:
412
- {
413
- "entities": [
414
- {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
415
- {"id": "playing_guitar", "label": "Playing Guitar", "type": "hobby", "confidence": 0.95},
416
- {"id": "guitar", "label": "Guitar", "type": "object", "confidence": 0.9, "properties": {"category": "musical_instrument"}},
417
- {"id": "jazz_music", "label": "Jazz Music", "type": "music", "confidence": 0.95, "properties": {"genre": "jazz"}},
418
- {"id": "weekends", "label": "Weekends", "type": "routine", "confidence": 0.8, "properties": {"frequency": "weekly"}}
419
- ],
420
- "relationships": [
421
- {"source": "user", "target": "playing_guitar", "label": "enjoys", "confidence": 0.95, "type": "hobby"},
422
- {"source": "playing_guitar", "target": "guitar", "label": "uses", "confidence": 0.9, "type": "activity"},
423
- {"source": "user", "target": "jazz_music", "label": "enjoys", "confidence": 0.9, "type": "preference"},
424
- {"source": "playing_guitar", "target": "weekends", "label": "happens_during", "confidence": 0.8, "type": "temporal"}
425
- ]
426
- }
427
-
428
- ## Guidelines
429
-
430
- 1. **Always include "user" entity** for personal statements (first-person: "I", "my", "me")
431
- 2. **Extract implicit information**: "i'm a doctor" implies medical skill
432
- 3. **Handle negations properly**: "don't like" = dislikes relationship
433
- 4. **Capture intensity**: "love" vs "like" vs "enjoy" = different confidence/intensity
434
- 5. **Include relevant properties**: cuisine type for food, location type for places
435
- 6. **Create bidirectional relationships when applicable**: if A works_at B, B employs A
436
- 7. **Minimum confidence threshold**: 0.5 for entities, 0.5 for relationships
437
- 8. **Be comprehensive**: Extract ALL meaningful entities, even from short texts
438
- 9. **Handle multilingual content**: Vietnamese, English, etc.
439
- 10. **Infer entity types from context**: "spaghetti" = food, "Python" = skill/language based on context
440
- ${contextSection}
441
- ## TEXT TO ANALYZE:
442
- ${content}
443
-
160
+ return `
161
+ You are a knowledge graph extraction system for a Personal Data Wallet application. Your task is to extract meaningful entities and relationships from personal memories, notes, and user statements.
162
+
163
+ ## CRITICAL RULE: User Entity
164
+ ALWAYS include a "user" entity to represent the person who wrote this memory:
165
+ {
166
+ "id": "user",
167
+ "label": "User",
168
+ "type": "person",
169
+ "confidence": 1.0
170
+ }
171
+ This "user" entity should be the source or target of relationships describing personal preferences, attributes, or experiences.
172
+
173
+ ## Entity Types (Comprehensive List)
174
+
175
+ ### People & Social
176
+ - **person**: Individual people, including the user themselves
177
+ - Examples: "user", "john_doe", "my_mother", "boss"
178
+ - Properties: name, role, relationship_to_user
179
+
180
+ ### Organizations & Groups
181
+ - **organization**: Companies, institutions, teams, communities
182
+ - Examples: "google", "harvard_university", "local_gym"
183
+ - Properties: industry, size, location
184
+
185
+ ### Locations & Places
186
+ - **location**: Geographic places, addresses, venues
187
+ - Examples: "ho_chi_minh_city", "vietnam", "my_office", "central_park"
188
+ - Properties: type (city/country/venue), coordinates
189
+
190
+ ### Food & Dining
191
+ - **food**: Foods, dishes, cuisines, beverages, ingredients
192
+ - Examples: "spaghetti", "vietnamese_cuisine", "coffee", "chocolate"
193
+ - Properties: cuisine_type, meal_type, dietary_info
194
+ - **restaurant**: Eating establishments
195
+ - Examples: "starbucks", "local_pho_shop"
196
+ - Properties: cuisine, price_range
197
+
198
+ ### Preferences & Interests
199
+ - **preference**: General likes, dislikes, favorites
200
+ - Examples: "blue_color", "morning_routine", "minimalist_style"
201
+ - Properties: sentiment (positive/negative/neutral), intensity (1-10)
202
+ - **hobby**: Recreational activities, pastimes
203
+ - Examples: "playing_guitar", "photography", "hiking", "gaming"
204
+ - Properties: frequency, skill_level
205
+ - **interest**: Topics of curiosity or passion
206
+ - Examples: "artificial_intelligence", "history", "cooking"
207
+ - Properties: depth (casual/moderate/deep)
208
+
209
+ ### Skills & Abilities
210
+ - **skill**: Technical or soft skills, expertise areas
211
+ - Examples: "python_programming", "public_speaking", "cooking"
212
+ - Properties: proficiency (beginner/intermediate/expert)
213
+ - **language**: Languages known or being learned
214
+ - Examples: "english", "vietnamese", "japanese"
215
+ - Properties: proficiency, native (true/false)
216
+
217
+ ### Objects & Possessions
218
+ - **object**: Physical items, products, tools
219
+ - Examples: "macbook_pro", "my_car", "guitar"
220
+ - Properties: brand, model, acquisition_date
221
+ - **digital_product**: Software, apps, digital services
222
+ - Examples: "spotify", "notion", "chatgpt"
223
+ - Properties: category, usage_frequency
224
+
225
+ ### Time & Events
226
+ - **event**: Occasions, milestones, meetings
227
+ - Examples: "birthday_2024", "job_interview", "vacation_trip"
228
+ - Properties: date, duration, importance
229
+ - **routine**: Regular activities or habits
230
+ - Examples: "morning_workout", "weekly_meeting", "daily_meditation"
231
+ - Properties: frequency, time_of_day
232
+
233
+ ### Abstract & Conceptual
234
+ - **concept**: Ideas, topics, abstract things
235
+ - Examples: "work_life_balance", "productivity", "happiness"
236
+ - **goal**: Objectives, aspirations, plans
237
+ - Examples: "learn_japanese", "run_marathon", "save_money"
238
+ - Properties: deadline, priority, status
239
+ - **emotion**: Feelings, moods, emotional states
240
+ - Examples: "happiness", "stress", "excitement"
241
+ - Properties: intensity, trigger
242
+
243
+ ### Health & Wellness
244
+ - **health_condition**: Medical conditions, allergies
245
+ - Examples: "lactose_intolerance", "migraine", "allergy_to_peanuts"
246
+ - **medication**: Medicines, supplements
247
+ - Examples: "vitamin_d", "aspirin"
248
+ - **exercise**: Physical activities for health
249
+ - Examples: "running", "yoga", "weight_training"
250
+
251
+ ### Media & Entertainment
252
+ - **music**: Songs, artists, genres, albums
253
+ - Examples: "jazz_music", "beatles", "classical_piano"
254
+ - **movie**: Films, TV shows, documentaries
255
+ - Examples: "inception", "game_of_thrones"
256
+ - **book**: Books, authors, genres
257
+ - Examples: "atomic_habits", "fiction_genre"
258
+ - **game**: Video games, board games
259
+ - Examples: "chess", "minecraft"
260
+
261
+ ## Relationship Types (Comprehensive List)
262
+
263
+ ### Preference Relationships (source: usually "user")
264
+ - **loves**: Strong positive preference (intensity 9-10)
265
+ - **likes**: Moderate positive preference (intensity 6-8)
266
+ - **enjoys**: Positive experience with something
267
+ - **prefers**: Comparative preference
268
+ - **favorite**: Top choice in a category
269
+ - **interested_in**: Curiosity or engagement
270
+ - **dislikes**: Moderate negative preference
271
+ - **hates**: Strong negative preference (intensity 9-10)
272
+ - **avoids**: Intentionally stays away from
273
+ - **allergic_to**: Medical/physical aversion
274
+
275
+ ### Affiliation Relationships
276
+ - **works_at**: Employment relationship
277
+ - **studies_at**: Educational institution
278
+ - **member_of**: Group membership
279
+ - **belongs_to**: General affiliation
280
+ - **founded**: Created an organization
281
+ - **leads**: Leadership role
282
+
283
+ ### Location Relationships
284
+ - **lives_in**: Current residence
285
+ - **from**: Origin/hometown
286
+ - **located_in**: Physical location
287
+ - **visited**: Past travel
288
+ - **wants_to_visit**: Travel aspiration
289
+
290
+ ### Social Relationships
291
+ - **knows**: Acquaintance
292
+ - **friends_with**: Friendship
293
+ - **family_of**: Family relationship (specify: parent, sibling, child, spouse)
294
+ - **works_with**: Professional relationship
295
+ - **mentored_by**: Learning relationship
296
+
297
+ ### Skill & Knowledge Relationships
298
+ - **has_skill**: Possesses ability
299
+ - **expert_in**: High proficiency
300
+ - **learning**: Currently acquiring
301
+ - **wants_to_learn**: Aspiration to learn
302
+ - **teaches**: Instructing others
303
+ - **certified_in**: Formal qualification
304
+
305
+ ### Possession & Usage
306
+ - **owns**: Ownership
307
+ - **uses**: Regular usage
308
+ - **wants**: Desire to acquire
309
+ - **recommends**: Positive endorsement
310
+
311
+ ### Temporal Relationships
312
+ - **started_on**: Beginning date
313
+ - **ended_on**: Ending date
314
+ - **scheduled_for**: Future event
315
+ - **happens_during**: Temporal context
316
+
317
+ ### Causal & Descriptive
318
+ - **causes**: Causal relationship
319
+ - **related_to**: General association
320
+ - **part_of**: Component relationship
321
+ - **similar_to**: Similarity
322
+ - **opposite_of**: Contrast
323
+
324
+ ## Output Format
325
+
326
+ Return ONLY valid JSON with this structure:
327
+ {
328
+ "entities": [
329
+ {
330
+ "id": "snake_case_identifier",
331
+ "label": "Human Readable Name",
332
+ "type": "entity_type_from_list_above",
333
+ "confidence": 0.0-1.0,
334
+ "properties": { "optional": "attributes" }
335
+ }
336
+ ],
337
+ "relationships": [
338
+ {
339
+ "source": "source_entity_id",
340
+ "target": "target_entity_id",
341
+ "label": "relationship_type_from_list_above",
342
+ "confidence": 0.0-1.0,
343
+ "type": "optional_category"
344
+ }
345
+ ]
346
+ }
347
+
348
+ ## Examples
349
+
350
+ ### Example 1: Food Preference
351
+ Input: "i love spaghetti"
352
+ Output:
353
+ {
354
+ "entities": [
355
+ {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
356
+ {"id": "spaghetti", "label": "Spaghetti", "type": "food", "confidence": 0.95, "properties": {"cuisine": "italian", "meal_type": "main_course"}}
357
+ ],
358
+ "relationships": [
359
+ {"source": "user", "target": "spaghetti", "label": "loves", "confidence": 0.95, "type": "preference"}
360
+ ]
361
+ }
362
+
363
+ ### Example 2: Multiple Preferences
364
+ Input: "i like hamburgers but hate vegetables"
365
+ Output:
366
+ {
367
+ "entities": [
368
+ {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
369
+ {"id": "hamburgers", "label": "Hamburgers", "type": "food", "confidence": 0.95},
370
+ {"id": "vegetables", "label": "Vegetables", "type": "food", "confidence": 0.95}
371
+ ],
372
+ "relationships": [
373
+ {"source": "user", "target": "hamburgers", "label": "likes", "confidence": 0.9, "type": "preference"},
374
+ {"source": "user", "target": "vegetables", "label": "dislikes", "confidence": 0.9, "type": "preference"}
375
+ ]
376
+ }
377
+
378
+ ### Example 3: Work Information
379
+ Input: "i work at Google as a software engineer in Mountain View"
380
+ Output:
381
+ {
382
+ "entities": [
383
+ {"id": "user", "label": "User", "type": "person", "confidence": 1.0, "properties": {"role": "software_engineer"}},
384
+ {"id": "google", "label": "Google", "type": "organization", "confidence": 0.98, "properties": {"industry": "technology"}},
385
+ {"id": "software_engineer", "label": "Software Engineer", "type": "skill", "confidence": 0.9},
386
+ {"id": "mountain_view", "label": "Mountain View", "type": "location", "confidence": 0.95, "properties": {"type": "city"}}
387
+ ],
388
+ "relationships": [
389
+ {"source": "user", "target": "google", "label": "works_at", "confidence": 0.98, "type": "affiliation"},
390
+ {"source": "user", "target": "software_engineer", "label": "has_skill", "confidence": 0.9, "type": "skill"},
391
+ {"source": "google", "target": "mountain_view", "label": "located_in", "confidence": 0.85, "type": "location"}
392
+ ]
393
+ }
394
+
395
+ ### Example 4: Personal Life
396
+ Input: "my name is Aaron and I live in Ho Chi Minh City with my wife"
397
+ Output:
398
+ {
399
+ "entities": [
400
+ {"id": "user", "label": "Aaron", "type": "person", "confidence": 1.0, "properties": {"name": "Aaron"}},
401
+ {"id": "ho_chi_minh_city", "label": "Ho Chi Minh City", "type": "location", "confidence": 0.98, "properties": {"type": "city", "country": "Vietnam"}},
402
+ {"id": "wife", "label": "Wife", "type": "person", "confidence": 0.9, "properties": {"relationship": "spouse"}}
403
+ ],
404
+ "relationships": [
405
+ {"source": "user", "target": "ho_chi_minh_city", "label": "lives_in", "confidence": 0.98, "type": "location"},
406
+ {"source": "user", "target": "wife", "label": "family_of", "confidence": 0.95, "type": "social", "properties": {"relationship_type": "spouse"}}
407
+ ]
408
+ }
409
+
410
+ ### Example 5: Hobbies and Interests
411
+ Input: "i enjoy playing guitar and listening to jazz music on weekends"
412
+ Output:
413
+ {
414
+ "entities": [
415
+ {"id": "user", "label": "User", "type": "person", "confidence": 1.0},
416
+ {"id": "playing_guitar", "label": "Playing Guitar", "type": "hobby", "confidence": 0.95},
417
+ {"id": "guitar", "label": "Guitar", "type": "object", "confidence": 0.9, "properties": {"category": "musical_instrument"}},
418
+ {"id": "jazz_music", "label": "Jazz Music", "type": "music", "confidence": 0.95, "properties": {"genre": "jazz"}},
419
+ {"id": "weekends", "label": "Weekends", "type": "routine", "confidence": 0.8, "properties": {"frequency": "weekly"}}
420
+ ],
421
+ "relationships": [
422
+ {"source": "user", "target": "playing_guitar", "label": "enjoys", "confidence": 0.95, "type": "hobby"},
423
+ {"source": "playing_guitar", "target": "guitar", "label": "uses", "confidence": 0.9, "type": "activity"},
424
+ {"source": "user", "target": "jazz_music", "label": "enjoys", "confidence": 0.9, "type": "preference"},
425
+ {"source": "playing_guitar", "target": "weekends", "label": "happens_during", "confidence": 0.8, "type": "temporal"}
426
+ ]
427
+ }
428
+
429
+ ## Guidelines
430
+
431
+ 1. **Always include "user" entity** for personal statements (first-person: "I", "my", "me")
432
+ 2. **Extract implicit information**: "i'm a doctor" implies medical skill
433
+ 3. **Handle negations properly**: "don't like" = dislikes relationship
434
+ 4. **Capture intensity**: "love" vs "like" vs "enjoy" = different confidence/intensity
435
+ 5. **Include relevant properties**: cuisine type for food, location type for places
436
+ 6. **Create bidirectional relationships when applicable**: if A works_at B, B employs A
437
+ 7. **Minimum confidence threshold**: 0.5 for entities, 0.5 for relationships
438
+ 8. **Be comprehensive**: Extract ALL meaningful entities, even from short texts
439
+ 9. **Handle multilingual content**: Vietnamese, English, etc.
440
+ 10. **Infer entity types from context**: "spaghetti" = food, "Python" = skill/language based on context
441
+ ${contextSection}
442
+ ## TEXT TO ANALYZE:
443
+ ${content}
444
+
444
445
  ## JSON OUTPUT:`;
445
446
  }
446
447
  parseExtractionResponse(response) {
@@ -534,21 +535,21 @@ ${content}
534
535
  */
535
536
  async extractRichMetadata(content, categoryHint) {
536
537
  try {
537
- const prompt = `
538
- Analyze the following text and extract rich metadata in JSON format:
539
- - "importance": relevance/importance score from 1-10 (1=trivial, 10=critical)
540
- - "topic": concise topic/title (max 100 chars)
541
- - "summary": brief summary (max 200 chars)
542
- - "category": best-fit category (personal, work, education, health, finance, travel, family, hobbies, goals, ideas)
543
-
544
- Consider:
545
- - Importance: How valuable is this information for future recall?
546
- - Topic: What's the main subject or theme?
547
- - Summary: Key points in 1-2 sentences
548
- ${categoryHint ? `- Prefer category: ${categoryHint}` : ''}
549
-
550
- TEXT: ${content}
551
-
538
+ const prompt = `
539
+ Analyze the following text and extract rich metadata in JSON format:
540
+ - "importance": relevance/importance score from 1-10 (1=trivial, 10=critical)
541
+ - "topic": concise topic/title (max 100 chars)
542
+ - "summary": brief summary (max 200 chars)
543
+ - "category": best-fit category (personal, work, education, health, finance, travel, family, hobbies, goals, ideas)
544
+
545
+ Consider:
546
+ - Importance: How valuable is this information for future recall?
547
+ - Topic: What's the main subject or theme?
548
+ - Summary: Key points in 1-2 sentences
549
+ ${categoryHint ? `- Prefer category: ${categoryHint}` : ''}
550
+
551
+ TEXT: ${content}
552
+
552
553
  JSON:`;
553
554
  const text = await this.callOpenRouter(prompt);
554
555
  return this.parseRichMetadataResponse(text, content, categoryHint);