@usewhisper/mcp-server 0.2.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -23
- package/dist/autosubscribe-GHO6YR5A.js +4068 -0
- package/dist/chunk-52VJYCZ7.js +455 -0
- package/dist/chunk-5KBZQHDL.js +189 -0
- package/dist/chunk-7SN3CKDK.js +1076 -0
- package/dist/chunk-EI5CE3EY.js +616 -0
- package/dist/chunk-JO3ORBZD.js +616 -0
- package/dist/chunk-LMEYV4JD.js +368 -0
- package/dist/chunk-MEFLJ4PV.js +8385 -0
- package/dist/chunk-PPGYJJED.js +271 -0
- package/dist/chunk-T7KMSTWP.js +399 -0
- package/dist/chunk-TWEIYHI6.js +399 -0
- package/dist/consolidation-2GCKI4RE.js +220 -0
- package/dist/consolidation-4JOPW6BG.js +220 -0
- package/dist/context-sharing-4ITCNKG4.js +307 -0
- package/dist/context-sharing-GYKLXHZA.js +307 -0
- package/dist/context-sharing-Y6LTZZOF.js +307 -0
- package/dist/cost-optimization-7DVSTL6R.js +307 -0
- package/dist/ingest-7T5FAZNC.js +15 -0
- package/dist/ingest-EBNIE7XB.js +15 -0
- package/dist/ingest-FSHT5BCS.js +15 -0
- package/dist/oracle-3RLQF3DP.js +259 -0
- package/dist/oracle-FKRTQUUG.js +282 -0
- package/dist/search-EG6TYWWW.js +13 -0
- package/dist/search-I22QQA7T.js +13 -0
- package/dist/search-T7H5G6DW.js +13 -0
- package/dist/server.js +1124 -1094
- package/package.json +2 -6
|
@@ -0,0 +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
|
+
};
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// ../src/engine/memory/temporal.ts
|
|
2
|
+
import OpenAI from "openai";
|
|
3
|
+
var openai = new OpenAI({
|
|
4
|
+
apiKey: process.env.OPENAI_API_KEY || ""
|
|
5
|
+
});
|
|
6
|
+
var TEMPORAL_PARSING_PROMPT = `You are an expert temporal query parser. Extract temporal constraints from user queries.
|
|
7
|
+
|
|
8
|
+
**Your job:**
|
|
9
|
+
1. Identify if the query has temporal constraints
|
|
10
|
+
2. Extract relative time references (today, yesterday, last week, etc.)
|
|
11
|
+
3. Extract absolute dates if mentioned
|
|
12
|
+
4. Calculate date ranges if applicable
|
|
13
|
+
|
|
14
|
+
**Relative Terms:**
|
|
15
|
+
- "today" \u2192 filter to documentDate = questionDate
|
|
16
|
+
- "yesterday" \u2192 documentDate = questionDate - 1 day
|
|
17
|
+
- "last week" \u2192 documentDate in range [questionDate - 7 days, questionDate]
|
|
18
|
+
- "last month" \u2192 documentDate in range [questionDate - 30 days, questionDate]
|
|
19
|
+
- "last year" \u2192 documentDate in range [questionDate - 365 days, questionDate]
|
|
20
|
+
- "this week" \u2192 current week
|
|
21
|
+
- "this month" \u2192 current month
|
|
22
|
+
|
|
23
|
+
**Examples:**
|
|
24
|
+
- "What did I say about vacation yesterday?" \u2192 relative: "yesterday"
|
|
25
|
+
- "Tell me about meetings last week" \u2192 relative: "last_week"
|
|
26
|
+
- "What happened on January 15?" \u2192 absoluteDate: "2024-01-15"
|
|
27
|
+
- "Show me everything from last month" \u2192 relative: "last_month"
|
|
28
|
+
- "What's my favorite color?" \u2192 no temporal constraint`;
|
|
29
|
+
async function parseTemporalQuery(query, questionDate) {
|
|
30
|
+
const prompt = `${TEMPORAL_PARSING_PROMPT}
|
|
31
|
+
|
|
32
|
+
**Query:** "${query}"
|
|
33
|
+
**Question asked on:** ${questionDate.toISOString()}
|
|
34
|
+
|
|
35
|
+
Extract temporal information and return JSON:
|
|
36
|
+
{
|
|
37
|
+
"hasTemporalConstraint": boolean,
|
|
38
|
+
"relative": "today|yesterday|last_week|last_month|last_year|this_week|this_month|null",
|
|
39
|
+
"absoluteDate": "ISO date string or null",
|
|
40
|
+
"dateRange": { "start": "ISO", "end": "ISO" } or null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
Return ONLY the JSON, no other text.`;
|
|
44
|
+
try {
|
|
45
|
+
const response = await openai.chat.completions.create({
|
|
46
|
+
model: "gpt-4o-mini",
|
|
47
|
+
// Faster model for parsing
|
|
48
|
+
max_tokens: 512,
|
|
49
|
+
temperature: 0,
|
|
50
|
+
messages: [{ role: "user", content: prompt }],
|
|
51
|
+
response_format: { type: "json_object" }
|
|
52
|
+
});
|
|
53
|
+
const text = response.choices[0]?.message?.content?.trim();
|
|
54
|
+
if (!text) {
|
|
55
|
+
return { hasTemporalConstraint: false };
|
|
56
|
+
}
|
|
57
|
+
const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\{[\s\S]*\}/);
|
|
58
|
+
const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
|
|
59
|
+
const parsed = JSON.parse(jsonStr);
|
|
60
|
+
if (parsed.relative) {
|
|
61
|
+
const range = calculateRelativeDateRange(parsed.relative, questionDate);
|
|
62
|
+
parsed.dateRange = range;
|
|
63
|
+
parsed.absoluteDate = range.start;
|
|
64
|
+
} else if (parsed.absoluteDate) {
|
|
65
|
+
parsed.absoluteDate = new Date(parsed.absoluteDate);
|
|
66
|
+
}
|
|
67
|
+
if (parsed.dateRange) {
|
|
68
|
+
parsed.dateRange = {
|
|
69
|
+
start: new Date(parsed.dateRange.start),
|
|
70
|
+
end: new Date(parsed.dateRange.end)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return parsed;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error("Temporal parsing failed:", error);
|
|
76
|
+
return { hasTemporalConstraint: false };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function calculateRelativeDateRange(relative, from) {
|
|
80
|
+
const start = new Date(from);
|
|
81
|
+
const end = new Date(from);
|
|
82
|
+
switch (relative) {
|
|
83
|
+
case "today":
|
|
84
|
+
start.setHours(0, 0, 0, 0);
|
|
85
|
+
end.setHours(23, 59, 59, 999);
|
|
86
|
+
break;
|
|
87
|
+
case "yesterday":
|
|
88
|
+
start.setDate(start.getDate() - 1);
|
|
89
|
+
start.setHours(0, 0, 0, 0);
|
|
90
|
+
end.setDate(end.getDate() - 1);
|
|
91
|
+
end.setHours(23, 59, 59, 999);
|
|
92
|
+
break;
|
|
93
|
+
case "last_week":
|
|
94
|
+
start.setDate(start.getDate() - 7);
|
|
95
|
+
start.setHours(0, 0, 0, 0);
|
|
96
|
+
end.setHours(23, 59, 59, 999);
|
|
97
|
+
break;
|
|
98
|
+
case "this_week":
|
|
99
|
+
const dayOfWeek = start.getDay();
|
|
100
|
+
const diff = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
|
|
101
|
+
start.setDate(start.getDate() + diff);
|
|
102
|
+
start.setHours(0, 0, 0, 0);
|
|
103
|
+
end.setHours(23, 59, 59, 999);
|
|
104
|
+
break;
|
|
105
|
+
case "last_month":
|
|
106
|
+
start.setDate(start.getDate() - 30);
|
|
107
|
+
start.setHours(0, 0, 0, 0);
|
|
108
|
+
end.setHours(23, 59, 59, 999);
|
|
109
|
+
break;
|
|
110
|
+
case "this_month":
|
|
111
|
+
start.setDate(1);
|
|
112
|
+
start.setHours(0, 0, 0, 0);
|
|
113
|
+
end.setHours(23, 59, 59, 999);
|
|
114
|
+
break;
|
|
115
|
+
case "last_year":
|
|
116
|
+
start.setFullYear(start.getFullYear() - 1);
|
|
117
|
+
start.setHours(0, 0, 0, 0);
|
|
118
|
+
end.setHours(23, 59, 59, 999);
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
start.setFullYear(1970);
|
|
122
|
+
end.setFullYear(2100);
|
|
123
|
+
}
|
|
124
|
+
return { start, end };
|
|
125
|
+
}
|
|
126
|
+
async function extractEventDate(memoryContent, documentDate) {
|
|
127
|
+
const prompt = `Extract the event date from this memory.
|
|
128
|
+
|
|
129
|
+
**Important distinction:**
|
|
130
|
+
- documentDate: When this was said/written
|
|
131
|
+
- eventDate: When the event actually occurred/will occur
|
|
132
|
+
|
|
133
|
+
**Memory:** "${memoryContent}"
|
|
134
|
+
**Document Date (when this was said):** ${documentDate.toISOString()}
|
|
135
|
+
|
|
136
|
+
**Examples:**
|
|
137
|
+
- "User said they have a meeting tomorrow" \u2192 eventDate = documentDate + 1 day
|
|
138
|
+
- "User attended conference on Jan 15" \u2192 eventDate = Jan 15 of appropriate year
|
|
139
|
+
- "User's favorite color is blue" \u2192 eventDate = null (no event, just a fact)
|
|
140
|
+
- "Meeting happened yesterday" \u2192 eventDate = documentDate - 1 day
|
|
141
|
+
|
|
142
|
+
Return JSON:
|
|
143
|
+
{
|
|
144
|
+
"hasEvent": boolean,
|
|
145
|
+
"eventDate": "ISO date string or null",
|
|
146
|
+
"reasoning": "brief explanation"
|
|
147
|
+
}`;
|
|
148
|
+
try {
|
|
149
|
+
const response = await openai.chat.completions.create({
|
|
150
|
+
model: "gpt-4o-mini",
|
|
151
|
+
max_tokens: 256,
|
|
152
|
+
temperature: 0,
|
|
153
|
+
messages: [{ role: "user", content: prompt }],
|
|
154
|
+
response_format: { type: "json_object" }
|
|
155
|
+
});
|
|
156
|
+
const text = response.choices[0]?.message?.content?.trim();
|
|
157
|
+
if (!text) {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\{[\s\S]*\}/);
|
|
161
|
+
const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
|
|
162
|
+
const result = JSON.parse(jsonStr);
|
|
163
|
+
if (result.hasEvent && result.eventDate) {
|
|
164
|
+
return new Date(result.eventDate);
|
|
165
|
+
}
|
|
166
|
+
return null;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error("Event date extraction failed:", error);
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
function calculateTemporalRelevance(memoryDate, questionDate, decayFactor = 0.1) {
|
|
173
|
+
if (!memoryDate) return 0.5;
|
|
174
|
+
const memDate = typeof memoryDate === "string" ? new Date(memoryDate) : memoryDate;
|
|
175
|
+
if (!(memDate instanceof Date) || isNaN(memDate.getTime())) {
|
|
176
|
+
return 0.5;
|
|
177
|
+
}
|
|
178
|
+
const daysDiff = Math.abs(
|
|
179
|
+
(questionDate.getTime() - memDate.getTime()) / (1e3 * 60 * 60 * 24)
|
|
180
|
+
);
|
|
181
|
+
const score = Math.exp(-decayFactor * daysDiff);
|
|
182
|
+
return score;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export {
|
|
186
|
+
parseTemporalQuery,
|
|
187
|
+
extractEventDate,
|
|
188
|
+
calculateTemporalRelevance
|
|
189
|
+
};
|