@usewhisper/mcp-server 0.3.0 → 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.
Files changed (57) hide show
  1. package/README.md +182 -154
  2. package/dist/autosubscribe-6EDKPBE2.js +4068 -4068
  3. package/dist/autosubscribe-GHO6YR5A.js +4068 -4068
  4. package/dist/autosubscribe-ISDETQIB.js +435 -435
  5. package/dist/chunk-3WGYBAYR.js +8387 -8387
  6. package/dist/chunk-52VJYCZ7.js +455 -455
  7. package/dist/chunk-5KBZQHDL.js +189 -189
  8. package/dist/chunk-5KIJNY6Z.js +370 -370
  9. package/dist/chunk-7SN3CKDK.js +1076 -1076
  10. package/dist/chunk-B3VWOHUA.js +271 -271
  11. package/dist/chunk-C57DHKTL.js +459 -459
  12. package/dist/chunk-EI5CE3EY.js +616 -616
  13. package/dist/chunk-FTWUJBAH.js +386 -386
  14. package/dist/chunk-H3HSKH2P.js +4841 -4841
  15. package/dist/chunk-JO3ORBZD.js +616 -616
  16. package/dist/chunk-L6DXSM2U.js +456 -456
  17. package/dist/chunk-LMEYV4JD.js +368 -368
  18. package/dist/chunk-MEFLJ4PV.js +8385 -8385
  19. package/dist/chunk-OBLI4FE4.js +275 -275
  20. package/dist/chunk-PPGYJJED.js +271 -271
  21. package/dist/chunk-QGM4M3NI.js +37 -37
  22. package/dist/chunk-T7KMSTWP.js +399 -399
  23. package/dist/chunk-TWEIYHI6.js +399 -399
  24. package/dist/chunk-UYWE7HSU.js +368 -368
  25. package/dist/chunk-X2DL2GWT.js +32 -32
  26. package/dist/chunk-X7HNNNJJ.js +1079 -1079
  27. package/dist/consolidation-2GCKI4RE.js +220 -220
  28. package/dist/consolidation-4JOPW6BG.js +220 -220
  29. package/dist/consolidation-FOVQTWNQ.js +222 -222
  30. package/dist/consolidation-IFQ52E44.js +209 -209
  31. package/dist/context-sharing-4ITCNKG4.js +307 -307
  32. package/dist/context-sharing-6CCFIAKL.js +275 -275
  33. package/dist/context-sharing-GYKLXHZA.js +307 -307
  34. package/dist/context-sharing-PH64JTXS.js +308 -308
  35. package/dist/context-sharing-Y6LTZZOF.js +307 -307
  36. package/dist/cost-optimization-6OIKRSBV.js +195 -195
  37. package/dist/cost-optimization-7DVSTL6R.js +307 -307
  38. package/dist/cost-optimization-BH5NAX33.js +286 -286
  39. package/dist/cost-optimization-F3L5BS5F.js +303 -303
  40. package/dist/ingest-2LPTWUUM.js +16 -16
  41. package/dist/ingest-7T5FAZNC.js +15 -15
  42. package/dist/ingest-EBNIE7XB.js +15 -15
  43. package/dist/ingest-FSHT5BCS.js +15 -15
  44. package/dist/ingest-QE2BTV72.js +14 -14
  45. package/dist/oracle-3RLQF3DP.js +259 -259
  46. package/dist/oracle-FKRTQUUG.js +282 -282
  47. package/dist/oracle-J47QCSEW.js +263 -263
  48. package/dist/oracle-MDP5MZRC.js +256 -256
  49. package/dist/search-BLVHWLWC.js +14 -14
  50. package/dist/search-CZ5NYL5B.js +12 -12
  51. package/dist/search-EG6TYWWW.js +13 -13
  52. package/dist/search-I22QQA7T.js +13 -13
  53. package/dist/search-T7H5G6DW.js +13 -13
  54. package/dist/server.d.ts +2 -2
  55. package/dist/server.js +1973 -169
  56. package/dist/server.js.map +1 -1
  57. package/package.json +51 -51
@@ -1,455 +1,455 @@
1
- import {
2
- detectRelations,
3
- extractEventDate,
4
- shouldInvalidateMemory
5
- } from "./chunk-LMEYV4JD.js";
6
- import {
7
- db,
8
- embedSingle
9
- } from "./chunk-3WGYBAYR.js";
10
-
11
- // ../src/engine/memory/extractor.ts
12
- import OpenAI from "openai";
13
- var openai = new OpenAI({
14
- apiKey: process.env.OPENAI_API_KEY || ""
15
- });
16
- var EXTRACTION_PROMPT = `You are an expert memory extraction system. Your job is to extract atomic, unambiguous memories from conversation chunks.
17
-
18
- **Critical Rules:**
19
- 1. Each memory must be a SINGLE fact/preference/event/relationship
20
- 2. Resolve ALL pronouns (he/she/it/they/them) to actual names using context
21
- 3. Resolve ALL ambiguous references ("the company", "that project") to specific entities
22
- 4. Extract temporal information when events occurred (not when mentioned)
23
- 5. Be conservative - only extract high-confidence memories
24
-
25
- **Memory Types:**
26
- - factual: Objective facts ("John works at Google")
27
- - preference: User preferences ("Sarah prefers dark mode")
28
- - event: Events with timestamps ("Team met on Jan 15, 2024")
29
- - relationship: Relationships ("Alex reports to Maria")
30
- - opinion: Subjective views ("User thinks Python is easier than Rust")
31
- - goal: Future intentions ("User wants to learn machine learning")
32
- - instruction: Persistent instructions ("Always use formal tone with clients")
33
-
34
- **Disambiguation:**
35
- - Replace "he" \u2192 actual name using context
36
- - Replace "she" \u2192 actual name using context
37
- - Replace "it" \u2192 specific thing using context
38
- - Replace "the company" \u2192 company name
39
- - Replace "that project" \u2192 project name
40
-
41
- **Example:**
42
- Input: "He said he prefers using React. The project will launch next week."
43
- Context: Previous message: "Alex joined the team yesterday. He's working on the dashboard project."
44
-
45
- Bad Output:
46
- - "He prefers React" \u274C (ambiguous)
47
- - "The project launches next week" \u274C (what project?)
48
-
49
- Good Output:
50
- - content: "Alex prefers using React for development"
51
- type: preference
52
- entities: ["Alex", "React"]
53
- eventDate: null
54
-
55
- - content: "Dashboard project launch scheduled for [specific date if mentioned]"
56
- type: event
57
- entities: ["Dashboard project"]
58
- eventDate: [calculated date] or null if not specific`;
59
- async function extractMemories(chunk, context) {
60
- const contextStr = buildContextString(context);
61
- const prompt = `${EXTRACTION_PROMPT}
62
-
63
- ${contextStr}
64
-
65
- **Current chunk to analyze:**
66
- ${chunk}
67
-
68
- **Document Date (when this was said):** ${context.documentDate.toISOString()}
69
-
70
- Extract memories and return a JSON array. For each memory:
71
- {
72
- "content": "clear, unambiguous statement with no pronouns",
73
- "memoryType": "factual|preference|event|relationship|opinion|goal|instruction",
74
- "entityMentions": ["list", "of", "entities", "mentioned"],
75
- "eventDate": "ISO date string or null",
76
- "confidence": 0.0-1.0,
77
- "reasoning": "brief explanation of extraction"
78
- }
79
-
80
- Return ONLY the JSON array, no other text.`;
81
- try {
82
- const response = await openai.chat.completions.create({
83
- model: "gpt-4o",
84
- max_tokens: 4096,
85
- temperature: 0,
86
- // Deterministic for extraction
87
- messages: [
88
- {
89
- role: "user",
90
- content: prompt
91
- }
92
- ],
93
- response_format: { type: "json_object" }
94
- });
95
- const text = response.choices[0]?.message?.content?.trim();
96
- if (!text) {
97
- throw new Error("No text response from OpenAI");
98
- }
99
- const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\[[\s\S]*\]/);
100
- const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
101
- const rawMemories = JSON.parse(jsonStr);
102
- if (!Array.isArray(rawMemories)) {
103
- console.error("Expected array of memories, got:", rawMemories);
104
- return [];
105
- }
106
- return rawMemories.map((m) => ({
107
- content: m.content,
108
- memoryType: m.memoryType,
109
- entityMentions: m.entityMentions || [],
110
- eventDate: m.eventDate ? new Date(m.eventDate) : null,
111
- confidence: m.confidence || 0.7,
112
- reasoning: m.reasoning
113
- }));
114
- } catch (error) {
115
- console.error("Memory extraction failed:", error);
116
- return [];
117
- }
118
- }
119
- function buildContextString(context) {
120
- const parts = [];
121
- if (context.previousMessages && context.previousMessages.length > 0) {
122
- parts.push("**Context from previous messages:**");
123
- parts.push(context.previousMessages.slice(-5).join("\n"));
124
- }
125
- if (context.entityContext && context.entityContext.size > 0) {
126
- parts.push("\n**Known entities:**");
127
- context.entityContext.forEach((name, pronoun) => {
128
- parts.push(`- "${pronoun}" refers to ${name}`);
129
- });
130
- }
131
- if (parts.length === 0) {
132
- return "**Context:** None available";
133
- }
134
- return parts.join("\n");
135
- }
136
- function buildEntityContext(recentMemories) {
137
- const entityMap = /* @__PURE__ */ new Map();
138
- for (const memory of recentMemories) {
139
- for (const entity of memory.entityMentions) {
140
- if (/^[A-Z][a-z]+(?:\s[A-Z][a-z]+)*$/.test(entity)) {
141
- entityMap.set("he", entity);
142
- entityMap.set("she", entity);
143
- entityMap.set("they", entity);
144
- }
145
- }
146
- }
147
- return entityMap;
148
- }
149
- function validateMemory(memory) {
150
- if (memory.confidence < 0.6) {
151
- return false;
152
- }
153
- if (memory.content.length < 10) {
154
- return false;
155
- }
156
- const pronouns = /\b(he|she|it|they|them|his|her|their)\b/i;
157
- if (pronouns.test(memory.content)) {
158
- console.warn("Memory contains unresolved pronouns:", memory.content);
159
- return false;
160
- }
161
- const vagueRefs = /\b(the company|that project|this thing|the system)\b/i;
162
- if (vagueRefs.test(memory.content)) {
163
- console.warn("Memory contains vague references:", memory.content);
164
- return false;
165
- }
166
- return true;
167
- }
168
-
169
- // ../src/engine/memory/ingest.ts
170
- async function ingestSession(params) {
171
- const { sessionId, projectId, orgId, userId, messages } = params;
172
- const result = {
173
- memoriesCreated: 0,
174
- relationsCreated: 0,
175
- memoriesInvalidated: 0,
176
- errors: []
177
- };
178
- if (messages.length === 0) {
179
- return result;
180
- }
181
- try {
182
- const context = {
183
- sessionId,
184
- userId: userId || "unknown",
185
- projectId,
186
- orgId,
187
- documentDate: messages[messages.length - 1].timestamp,
188
- previousMessages: messages.slice(0, -1).map((m) => `${m.role}: ${m.content}`)
189
- };
190
- const recentMemories = await db.memory.findMany({
191
- where: {
192
- sessionId,
193
- projectId,
194
- isActive: true
195
- },
196
- orderBy: {
197
- createdAt: "desc"
198
- },
199
- take: 20,
200
- select: {
201
- content: true,
202
- entityMentions: true
203
- }
204
- });
205
- context.entityContext = buildEntityContext(recentMemories);
206
- const latestMessage = messages[messages.length - 1].content;
207
- const extractedMemories = await extractMemories(latestMessage, context);
208
- const validMemories = extractedMemories.filter(validateMemory);
209
- if (validMemories.length === 0) {
210
- return result;
211
- }
212
- const existingMemories = await db.memory.findMany({
213
- where: {
214
- projectId,
215
- userId,
216
- isActive: true
217
- },
218
- orderBy: {
219
- createdAt: "desc"
220
- },
221
- take: 100,
222
- // Check against last 100 memories
223
- select: {
224
- id: true,
225
- content: true,
226
- memoryType: true,
227
- entityMentions: true,
228
- documentDate: true
229
- }
230
- });
231
- for (const extracted of validMemories) {
232
- try {
233
- const relations = await detectRelations(
234
- {
235
- content: extracted.content,
236
- memoryType: extracted.memoryType,
237
- entityMentions: extracted.entityMentions
238
- },
239
- existingMemories
240
- );
241
- const eventDate = extracted.eventDate || await extractEventDate(
242
- extracted.content,
243
- context.documentDate
244
- );
245
- const embedding = await embedSingle(extracted.content);
246
- const embeddingStr = `[${embedding.join(",")}]`;
247
- const [memory] = await db.$queryRaw`
248
- INSERT INTO "memories" (
249
- id, "projectId", "orgId", "userId", "sessionId", "memoryType",
250
- content, embedding, "entityMentions", confidence, "documentDate",
251
- "eventDate", "validFrom", metadata, "createdAt", "updatedAt"
252
- )
253
- VALUES (
254
- gen_random_uuid(), ${projectId}, ${orgId || null}, ${userId || null},
255
- ${sessionId}, ${extracted.memoryType}, ${extracted.content},
256
- ${embeddingStr}::vector, ${extracted.entityMentions}, ${extracted.confidence},
257
- ${context.documentDate}, ${eventDate}, NOW(),
258
- ${JSON.stringify({
259
- reasoning: extracted.reasoning,
260
- extractedFrom: "session_ingestion"
261
- })}::jsonb, NOW(), NOW()
262
- )
263
- RETURNING *
264
- `;
265
- result.memoriesCreated++;
266
- for (const relation of relations) {
267
- try {
268
- await db.memoryRelation.create({
269
- data: {
270
- fromMemoryId: memory.id,
271
- toMemoryId: relation.toMemoryId,
272
- relationType: relation.relationType,
273
- confidence: relation.confidence,
274
- reasoning: relation.reasoning
275
- }
276
- });
277
- result.relationsCreated++;
278
- if (shouldInvalidateMemory(relation.relationType)) {
279
- await db.memory.update({
280
- where: { id: relation.toMemoryId },
281
- data: {
282
- validUntil: /* @__PURE__ */ new Date(),
283
- supersededBy: memory.id
284
- }
285
- });
286
- const oldMemory = await db.memory.findUnique({
287
- where: { id: relation.toMemoryId },
288
- select: { version: true }
289
- });
290
- if (oldMemory) {
291
- await db.memory.update({
292
- where: { id: memory.id },
293
- data: { version: oldMemory.version + 1 }
294
- });
295
- }
296
- result.memoriesInvalidated++;
297
- }
298
- } catch (error) {
299
- result.errors.push(`Failed to create relation: ${error}`);
300
- }
301
- }
302
- } catch (error) {
303
- result.errors.push(`Failed to process memory: ${error}`);
304
- }
305
- }
306
- return result;
307
- } catch (error) {
308
- result.errors.push(`Ingestion failed: ${error}`);
309
- return result;
310
- }
311
- }
312
- async function ingestChunk(params) {
313
- const { chunkId, chunkContent, projectId, orgId, documentDate, metadata } = params;
314
- const result = {
315
- memoriesCreated: 0,
316
- relationsCreated: 0,
317
- memoriesInvalidated: 0,
318
- errors: []
319
- };
320
- try {
321
- const context = {
322
- sessionId: `chunk_${chunkId}`,
323
- userId: "system",
324
- projectId,
325
- orgId,
326
- documentDate
327
- };
328
- const extractedMemories = await extractMemories(chunkContent, context);
329
- const validMemories = extractedMemories.filter(validateMemory);
330
- for (const extracted of validMemories) {
331
- const eventDate = extracted.eventDate || await extractEventDate(
332
- extracted.content,
333
- documentDate
334
- );
335
- const embedding = await embedSingle(extracted.content);
336
- await db.memory.create({
337
- data: {
338
- projectId,
339
- orgId,
340
- memoryType: extracted.memoryType,
341
- content: extracted.content,
342
- embedding,
343
- entityMentions: extracted.entityMentions,
344
- confidence: extracted.confidence,
345
- documentDate,
346
- eventDate,
347
- validFrom: /* @__PURE__ */ new Date(),
348
- sourceChunkId: chunkId,
349
- scope: "DOCUMENT",
350
- // Document-level scope
351
- metadata: {
352
- ...metadata,
353
- reasoning: extracted.reasoning
354
- }
355
- }
356
- });
357
- result.memoriesCreated++;
358
- }
359
- return result;
360
- } catch (error) {
361
- result.errors.push(`Chunk ingestion failed: ${error}`);
362
- return result;
363
- }
364
- }
365
- async function ingestChunksBatch(params) {
366
- const { chunks, projectId, orgId, documentDate } = params;
367
- const aggregateResult = {
368
- memoriesCreated: 0,
369
- relationsCreated: 0,
370
- memoriesInvalidated: 0,
371
- errors: []
372
- };
373
- const batchSize = 10;
374
- for (let i = 0; i < chunks.length; i += batchSize) {
375
- const batch = chunks.slice(i, i + batchSize);
376
- const results = await Promise.all(
377
- batch.map(
378
- (chunk) => ingestChunk({
379
- chunkId: chunk.id,
380
- chunkContent: chunk.content,
381
- projectId,
382
- orgId,
383
- documentDate,
384
- metadata: chunk.metadata
385
- })
386
- )
387
- );
388
- for (const result of results) {
389
- aggregateResult.memoriesCreated += result.memoriesCreated;
390
- aggregateResult.relationsCreated += result.relationsCreated;
391
- aggregateResult.memoriesInvalidated += result.memoriesInvalidated;
392
- aggregateResult.errors.push(...result.errors);
393
- }
394
- }
395
- return aggregateResult;
396
- }
397
- async function updateMemory(params) {
398
- const { memoryId, newContent, reasoning } = params;
399
- const oldMemory = await db.memory.findUnique({
400
- where: { id: memoryId }
401
- });
402
- if (!oldMemory) {
403
- throw new Error("Memory not found");
404
- }
405
- const embedding = await embedSingle(newContent);
406
- const newMemory = await db.memory.create({
407
- data: {
408
- projectId: oldMemory.projectId,
409
- orgId: oldMemory.orgId,
410
- userId: oldMemory.userId,
411
- sessionId: oldMemory.sessionId,
412
- memoryType: oldMemory.memoryType,
413
- content: newContent,
414
- embedding,
415
- entityMentions: oldMemory.entityMentions,
416
- confidence: oldMemory.confidence,
417
- documentDate: oldMemory.documentDate,
418
- eventDate: oldMemory.eventDate,
419
- validFrom: /* @__PURE__ */ new Date(),
420
- version: oldMemory.version + 1,
421
- scope: oldMemory.scope,
422
- metadata: {
423
- ...oldMemory.metadata,
424
- updateReasoning: reasoning
425
- }
426
- }
427
- });
428
- await db.memory.update({
429
- where: { id: memoryId },
430
- data: {
431
- validUntil: /* @__PURE__ */ new Date(),
432
- supersededBy: newMemory.id
433
- }
434
- });
435
- await db.memoryRelation.create({
436
- data: {
437
- fromMemoryId: newMemory.id,
438
- toMemoryId: memoryId,
439
- relationType: "updates",
440
- confidence: 1,
441
- reasoning: reasoning || "Manual update"
442
- }
443
- });
444
- return {
445
- newMemoryId: newMemory.id,
446
- oldMemoryId: memoryId
447
- };
448
- }
449
-
450
- export {
451
- ingestSession,
452
- ingestChunk,
453
- ingestChunksBatch,
454
- updateMemory
455
- };
1
+ import {
2
+ detectRelations,
3
+ extractEventDate,
4
+ shouldInvalidateMemory
5
+ } from "./chunk-LMEYV4JD.js";
6
+ import {
7
+ db,
8
+ embedSingle
9
+ } from "./chunk-3WGYBAYR.js";
10
+
11
+ // ../src/engine/memory/extractor.ts
12
+ import OpenAI from "openai";
13
+ var openai = new OpenAI({
14
+ apiKey: process.env.OPENAI_API_KEY || ""
15
+ });
16
+ var EXTRACTION_PROMPT = `You are an expert memory extraction system. Your job is to extract atomic, unambiguous memories from conversation chunks.
17
+
18
+ **Critical Rules:**
19
+ 1. Each memory must be a SINGLE fact/preference/event/relationship
20
+ 2. Resolve ALL pronouns (he/she/it/they/them) to actual names using context
21
+ 3. Resolve ALL ambiguous references ("the company", "that project") to specific entities
22
+ 4. Extract temporal information when events occurred (not when mentioned)
23
+ 5. Be conservative - only extract high-confidence memories
24
+
25
+ **Memory Types:**
26
+ - factual: Objective facts ("John works at Google")
27
+ - preference: User preferences ("Sarah prefers dark mode")
28
+ - event: Events with timestamps ("Team met on Jan 15, 2024")
29
+ - relationship: Relationships ("Alex reports to Maria")
30
+ - opinion: Subjective views ("User thinks Python is easier than Rust")
31
+ - goal: Future intentions ("User wants to learn machine learning")
32
+ - instruction: Persistent instructions ("Always use formal tone with clients")
33
+
34
+ **Disambiguation:**
35
+ - Replace "he" \u2192 actual name using context
36
+ - Replace "she" \u2192 actual name using context
37
+ - Replace "it" \u2192 specific thing using context
38
+ - Replace "the company" \u2192 company name
39
+ - Replace "that project" \u2192 project name
40
+
41
+ **Example:**
42
+ Input: "He said he prefers using React. The project will launch next week."
43
+ Context: Previous message: "Alex joined the team yesterday. He's working on the dashboard project."
44
+
45
+ Bad Output:
46
+ - "He prefers React" \u274C (ambiguous)
47
+ - "The project launches next week" \u274C (what project?)
48
+
49
+ Good Output:
50
+ - content: "Alex prefers using React for development"
51
+ type: preference
52
+ entities: ["Alex", "React"]
53
+ eventDate: null
54
+
55
+ - content: "Dashboard project launch scheduled for [specific date if mentioned]"
56
+ type: event
57
+ entities: ["Dashboard project"]
58
+ eventDate: [calculated date] or null if not specific`;
59
+ async function extractMemories(chunk, context) {
60
+ const contextStr = buildContextString(context);
61
+ const prompt = `${EXTRACTION_PROMPT}
62
+
63
+ ${contextStr}
64
+
65
+ **Current chunk to analyze:**
66
+ ${chunk}
67
+
68
+ **Document Date (when this was said):** ${context.documentDate.toISOString()}
69
+
70
+ Extract memories and return a JSON array. For each memory:
71
+ {
72
+ "content": "clear, unambiguous statement with no pronouns",
73
+ "memoryType": "factual|preference|event|relationship|opinion|goal|instruction",
74
+ "entityMentions": ["list", "of", "entities", "mentioned"],
75
+ "eventDate": "ISO date string or null",
76
+ "confidence": 0.0-1.0,
77
+ "reasoning": "brief explanation of extraction"
78
+ }
79
+
80
+ Return ONLY the JSON array, no other text.`;
81
+ try {
82
+ const response = await openai.chat.completions.create({
83
+ model: "gpt-4o",
84
+ max_tokens: 4096,
85
+ temperature: 0,
86
+ // Deterministic for extraction
87
+ messages: [
88
+ {
89
+ role: "user",
90
+ content: prompt
91
+ }
92
+ ],
93
+ response_format: { type: "json_object" }
94
+ });
95
+ const text = response.choices[0]?.message?.content?.trim();
96
+ if (!text) {
97
+ throw new Error("No text response from OpenAI");
98
+ }
99
+ const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\[[\s\S]*\]/);
100
+ const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
101
+ const rawMemories = JSON.parse(jsonStr);
102
+ if (!Array.isArray(rawMemories)) {
103
+ console.error("Expected array of memories, got:", rawMemories);
104
+ return [];
105
+ }
106
+ return rawMemories.map((m) => ({
107
+ content: m.content,
108
+ memoryType: m.memoryType,
109
+ entityMentions: m.entityMentions || [],
110
+ eventDate: m.eventDate ? new Date(m.eventDate) : null,
111
+ confidence: m.confidence || 0.7,
112
+ reasoning: m.reasoning
113
+ }));
114
+ } catch (error) {
115
+ console.error("Memory extraction failed:", error);
116
+ return [];
117
+ }
118
+ }
119
+ function buildContextString(context) {
120
+ const parts = [];
121
+ if (context.previousMessages && context.previousMessages.length > 0) {
122
+ parts.push("**Context from previous messages:**");
123
+ parts.push(context.previousMessages.slice(-5).join("\n"));
124
+ }
125
+ if (context.entityContext && context.entityContext.size > 0) {
126
+ parts.push("\n**Known entities:**");
127
+ context.entityContext.forEach((name, pronoun) => {
128
+ parts.push(`- "${pronoun}" refers to ${name}`);
129
+ });
130
+ }
131
+ if (parts.length === 0) {
132
+ return "**Context:** None available";
133
+ }
134
+ return parts.join("\n");
135
+ }
136
+ function buildEntityContext(recentMemories) {
137
+ const entityMap = /* @__PURE__ */ new Map();
138
+ for (const memory of recentMemories) {
139
+ for (const entity of memory.entityMentions) {
140
+ if (/^[A-Z][a-z]+(?:\s[A-Z][a-z]+)*$/.test(entity)) {
141
+ entityMap.set("he", entity);
142
+ entityMap.set("she", entity);
143
+ entityMap.set("they", entity);
144
+ }
145
+ }
146
+ }
147
+ return entityMap;
148
+ }
149
+ function validateMemory(memory) {
150
+ if (memory.confidence < 0.6) {
151
+ return false;
152
+ }
153
+ if (memory.content.length < 10) {
154
+ return false;
155
+ }
156
+ const pronouns = /\b(he|she|it|they|them|his|her|their)\b/i;
157
+ if (pronouns.test(memory.content)) {
158
+ console.warn("Memory contains unresolved pronouns:", memory.content);
159
+ return false;
160
+ }
161
+ const vagueRefs = /\b(the company|that project|this thing|the system)\b/i;
162
+ if (vagueRefs.test(memory.content)) {
163
+ console.warn("Memory contains vague references:", memory.content);
164
+ return false;
165
+ }
166
+ return true;
167
+ }
168
+
169
+ // ../src/engine/memory/ingest.ts
170
+ async function ingestSession(params) {
171
+ const { sessionId, projectId, orgId, userId, messages } = params;
172
+ const result = {
173
+ memoriesCreated: 0,
174
+ relationsCreated: 0,
175
+ memoriesInvalidated: 0,
176
+ errors: []
177
+ };
178
+ if (messages.length === 0) {
179
+ return result;
180
+ }
181
+ try {
182
+ const context = {
183
+ sessionId,
184
+ userId: userId || "unknown",
185
+ projectId,
186
+ orgId,
187
+ documentDate: messages[messages.length - 1].timestamp,
188
+ previousMessages: messages.slice(0, -1).map((m) => `${m.role}: ${m.content}`)
189
+ };
190
+ const recentMemories = await db.memory.findMany({
191
+ where: {
192
+ sessionId,
193
+ projectId,
194
+ isActive: true
195
+ },
196
+ orderBy: {
197
+ createdAt: "desc"
198
+ },
199
+ take: 20,
200
+ select: {
201
+ content: true,
202
+ entityMentions: true
203
+ }
204
+ });
205
+ context.entityContext = buildEntityContext(recentMemories);
206
+ const latestMessage = messages[messages.length - 1].content;
207
+ const extractedMemories = await extractMemories(latestMessage, context);
208
+ const validMemories = extractedMemories.filter(validateMemory);
209
+ if (validMemories.length === 0) {
210
+ return result;
211
+ }
212
+ const existingMemories = await db.memory.findMany({
213
+ where: {
214
+ projectId,
215
+ userId,
216
+ isActive: true
217
+ },
218
+ orderBy: {
219
+ createdAt: "desc"
220
+ },
221
+ take: 100,
222
+ // Check against last 100 memories
223
+ select: {
224
+ id: true,
225
+ content: true,
226
+ memoryType: true,
227
+ entityMentions: true,
228
+ documentDate: true
229
+ }
230
+ });
231
+ for (const extracted of validMemories) {
232
+ try {
233
+ const relations = await detectRelations(
234
+ {
235
+ content: extracted.content,
236
+ memoryType: extracted.memoryType,
237
+ entityMentions: extracted.entityMentions
238
+ },
239
+ existingMemories
240
+ );
241
+ const eventDate = extracted.eventDate || await extractEventDate(
242
+ extracted.content,
243
+ context.documentDate
244
+ );
245
+ const embedding = await embedSingle(extracted.content);
246
+ const embeddingStr = `[${embedding.join(",")}]`;
247
+ const [memory] = await db.$queryRaw`
248
+ INSERT INTO "memories" (
249
+ id, "projectId", "orgId", "userId", "sessionId", "memoryType",
250
+ content, embedding, "entityMentions", confidence, "documentDate",
251
+ "eventDate", "validFrom", metadata, "createdAt", "updatedAt"
252
+ )
253
+ VALUES (
254
+ gen_random_uuid(), ${projectId}, ${orgId || null}, ${userId || null},
255
+ ${sessionId}, ${extracted.memoryType}, ${extracted.content},
256
+ ${embeddingStr}::vector, ${extracted.entityMentions}, ${extracted.confidence},
257
+ ${context.documentDate}, ${eventDate}, NOW(),
258
+ ${JSON.stringify({
259
+ reasoning: extracted.reasoning,
260
+ extractedFrom: "session_ingestion"
261
+ })}::jsonb, NOW(), NOW()
262
+ )
263
+ RETURNING *
264
+ `;
265
+ result.memoriesCreated++;
266
+ for (const relation of relations) {
267
+ try {
268
+ await db.memoryRelation.create({
269
+ data: {
270
+ fromMemoryId: memory.id,
271
+ toMemoryId: relation.toMemoryId,
272
+ relationType: relation.relationType,
273
+ confidence: relation.confidence,
274
+ reasoning: relation.reasoning
275
+ }
276
+ });
277
+ result.relationsCreated++;
278
+ if (shouldInvalidateMemory(relation.relationType)) {
279
+ await db.memory.update({
280
+ where: { id: relation.toMemoryId },
281
+ data: {
282
+ validUntil: /* @__PURE__ */ new Date(),
283
+ supersededBy: memory.id
284
+ }
285
+ });
286
+ const oldMemory = await db.memory.findUnique({
287
+ where: { id: relation.toMemoryId },
288
+ select: { version: true }
289
+ });
290
+ if (oldMemory) {
291
+ await db.memory.update({
292
+ where: { id: memory.id },
293
+ data: { version: oldMemory.version + 1 }
294
+ });
295
+ }
296
+ result.memoriesInvalidated++;
297
+ }
298
+ } catch (error) {
299
+ result.errors.push(`Failed to create relation: ${error}`);
300
+ }
301
+ }
302
+ } catch (error) {
303
+ result.errors.push(`Failed to process memory: ${error}`);
304
+ }
305
+ }
306
+ return result;
307
+ } catch (error) {
308
+ result.errors.push(`Ingestion failed: ${error}`);
309
+ return result;
310
+ }
311
+ }
312
+ async function ingestChunk(params) {
313
+ const { chunkId, chunkContent, projectId, orgId, documentDate, metadata } = params;
314
+ const result = {
315
+ memoriesCreated: 0,
316
+ relationsCreated: 0,
317
+ memoriesInvalidated: 0,
318
+ errors: []
319
+ };
320
+ try {
321
+ const context = {
322
+ sessionId: `chunk_${chunkId}`,
323
+ userId: "system",
324
+ projectId,
325
+ orgId,
326
+ documentDate
327
+ };
328
+ const extractedMemories = await extractMemories(chunkContent, context);
329
+ const validMemories = extractedMemories.filter(validateMemory);
330
+ for (const extracted of validMemories) {
331
+ const eventDate = extracted.eventDate || await extractEventDate(
332
+ extracted.content,
333
+ documentDate
334
+ );
335
+ const embedding = await embedSingle(extracted.content);
336
+ await db.memory.create({
337
+ data: {
338
+ projectId,
339
+ orgId,
340
+ memoryType: extracted.memoryType,
341
+ content: extracted.content,
342
+ embedding,
343
+ entityMentions: extracted.entityMentions,
344
+ confidence: extracted.confidence,
345
+ documentDate,
346
+ eventDate,
347
+ validFrom: /* @__PURE__ */ new Date(),
348
+ sourceChunkId: chunkId,
349
+ scope: "DOCUMENT",
350
+ // Document-level scope
351
+ metadata: {
352
+ ...metadata,
353
+ reasoning: extracted.reasoning
354
+ }
355
+ }
356
+ });
357
+ result.memoriesCreated++;
358
+ }
359
+ return result;
360
+ } catch (error) {
361
+ result.errors.push(`Chunk ingestion failed: ${error}`);
362
+ return result;
363
+ }
364
+ }
365
+ async function ingestChunksBatch(params) {
366
+ const { chunks, projectId, orgId, documentDate } = params;
367
+ const aggregateResult = {
368
+ memoriesCreated: 0,
369
+ relationsCreated: 0,
370
+ memoriesInvalidated: 0,
371
+ errors: []
372
+ };
373
+ const batchSize = 10;
374
+ for (let i = 0; i < chunks.length; i += batchSize) {
375
+ const batch = chunks.slice(i, i + batchSize);
376
+ const results = await Promise.all(
377
+ batch.map(
378
+ (chunk) => ingestChunk({
379
+ chunkId: chunk.id,
380
+ chunkContent: chunk.content,
381
+ projectId,
382
+ orgId,
383
+ documentDate,
384
+ metadata: chunk.metadata
385
+ })
386
+ )
387
+ );
388
+ for (const result of results) {
389
+ aggregateResult.memoriesCreated += result.memoriesCreated;
390
+ aggregateResult.relationsCreated += result.relationsCreated;
391
+ aggregateResult.memoriesInvalidated += result.memoriesInvalidated;
392
+ aggregateResult.errors.push(...result.errors);
393
+ }
394
+ }
395
+ return aggregateResult;
396
+ }
397
+ async function updateMemory(params) {
398
+ const { memoryId, newContent, reasoning } = params;
399
+ const oldMemory = await db.memory.findUnique({
400
+ where: { id: memoryId }
401
+ });
402
+ if (!oldMemory) {
403
+ throw new Error("Memory not found");
404
+ }
405
+ const embedding = await embedSingle(newContent);
406
+ const newMemory = await db.memory.create({
407
+ data: {
408
+ projectId: oldMemory.projectId,
409
+ orgId: oldMemory.orgId,
410
+ userId: oldMemory.userId,
411
+ sessionId: oldMemory.sessionId,
412
+ memoryType: oldMemory.memoryType,
413
+ content: newContent,
414
+ embedding,
415
+ entityMentions: oldMemory.entityMentions,
416
+ confidence: oldMemory.confidence,
417
+ documentDate: oldMemory.documentDate,
418
+ eventDate: oldMemory.eventDate,
419
+ validFrom: /* @__PURE__ */ new Date(),
420
+ version: oldMemory.version + 1,
421
+ scope: oldMemory.scope,
422
+ metadata: {
423
+ ...oldMemory.metadata,
424
+ updateReasoning: reasoning
425
+ }
426
+ }
427
+ });
428
+ await db.memory.update({
429
+ where: { id: memoryId },
430
+ data: {
431
+ validUntil: /* @__PURE__ */ new Date(),
432
+ supersededBy: newMemory.id
433
+ }
434
+ });
435
+ await db.memoryRelation.create({
436
+ data: {
437
+ fromMemoryId: newMemory.id,
438
+ toMemoryId: memoryId,
439
+ relationType: "updates",
440
+ confidence: 1,
441
+ reasoning: reasoning || "Manual update"
442
+ }
443
+ });
444
+ return {
445
+ newMemoryId: newMemory.id,
446
+ oldMemoryId: memoryId
447
+ };
448
+ }
449
+
450
+ export {
451
+ ingestSession,
452
+ ingestChunk,
453
+ ingestChunksBatch,
454
+ updateMemory
455
+ };