@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,368 @@
|
|
|
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
|
+
const daysDiff = Math.abs(
|
|
174
|
+
(questionDate.getTime() - memoryDate.getTime()) / (1e3 * 60 * 60 * 24)
|
|
175
|
+
);
|
|
176
|
+
const score = Math.exp(-decayFactor * daysDiff);
|
|
177
|
+
return score;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ../src/engine/memory/relations.ts
|
|
181
|
+
import OpenAI2 from "openai";
|
|
182
|
+
var openai2 = new OpenAI2({
|
|
183
|
+
apiKey: process.env.OPENAI_API_KEY || ""
|
|
184
|
+
});
|
|
185
|
+
var RELATION_DETECTION_PROMPT = `You are an expert at detecting relationships between memories in a knowledge graph.
|
|
186
|
+
|
|
187
|
+
**Relation Types:**
|
|
188
|
+
|
|
189
|
+
1. **updates** - New memory supersedes/replaces old memory (state mutation)
|
|
190
|
+
Example:
|
|
191
|
+
- Old: "User's favorite color is blue"
|
|
192
|
+
- New: "User's favorite color is green"
|
|
193
|
+
- Relation: updates (green replaces blue)
|
|
194
|
+
|
|
195
|
+
2. **extends** - New memory adds detail to existing memory without contradiction (refinement)
|
|
196
|
+
Example:
|
|
197
|
+
- Old: "John works at Google"
|
|
198
|
+
- New: "John works at Google as a Senior Engineer"
|
|
199
|
+
- Relation: extends (adds job title)
|
|
200
|
+
|
|
201
|
+
3. **derives** - New memory is inferred from existing memory/memories (inference)
|
|
202
|
+
Example:
|
|
203
|
+
- Memory 1: "User prefers dark mode"
|
|
204
|
+
- Memory 2: "User prefers high contrast"
|
|
205
|
+
- New: "User likely has vision preferences for accessibility"
|
|
206
|
+
- Relation: derives (inferred from both)
|
|
207
|
+
|
|
208
|
+
4. **contradicts** - New memory conflicts with existing memory (conflict detection)
|
|
209
|
+
Example:
|
|
210
|
+
- Old: "Meeting scheduled for 3pm"
|
|
211
|
+
- New: "Meeting scheduled for 4pm"
|
|
212
|
+
- Relation: contradicts (should trigger update)
|
|
213
|
+
|
|
214
|
+
5. **supports** - New memory provides evidence/support for existing memory
|
|
215
|
+
Example:
|
|
216
|
+
- Memory 1: "User is interested in ML"
|
|
217
|
+
- New: "User enrolled in ML course"
|
|
218
|
+
- Relation: supports (confirms interest)
|
|
219
|
+
|
|
220
|
+
**Important:**
|
|
221
|
+
- Only detect relations when there's a clear, meaningful connection
|
|
222
|
+
- Be conservative - if unsure, don't create a relation
|
|
223
|
+
- "updates" should invalidate the old memory (set validUntil)
|
|
224
|
+
- "extends" keeps the old memory valid but adds information
|
|
225
|
+
- "contradicts" should flag for review/resolution`;
|
|
226
|
+
async function detectRelations(newMemory, existingMemories) {
|
|
227
|
+
if (existingMemories.length === 0) {
|
|
228
|
+
return [];
|
|
229
|
+
}
|
|
230
|
+
const relevantMemories = filterRelevantMemories(newMemory, existingMemories);
|
|
231
|
+
if (relevantMemories.length === 0) {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
const prompt = `${RELATION_DETECTION_PROMPT}
|
|
235
|
+
|
|
236
|
+
**New memory:**
|
|
237
|
+
"${newMemory.content}"
|
|
238
|
+
Type: ${newMemory.memoryType}
|
|
239
|
+
Entities: ${newMemory.entityMentions.join(", ")}
|
|
240
|
+
|
|
241
|
+
**Existing memories to check against:**
|
|
242
|
+
${relevantMemories.map((m, i) => `${i}. "${m.content}" (Type: ${m.memoryType}, Date: ${m.documentDate?.toISOString() || "unknown"})`).join("\n")}
|
|
243
|
+
|
|
244
|
+
Analyze if the new memory relates to any existing memories.
|
|
245
|
+
|
|
246
|
+
Return a JSON array of relations:
|
|
247
|
+
[{
|
|
248
|
+
"toMemoryIndex": 0,
|
|
249
|
+
"relationType": "updates|extends|derives|contradicts|supports",
|
|
250
|
+
"confidence": 0.0-1.0,
|
|
251
|
+
"reasoning": "brief explanation why this relation exists"
|
|
252
|
+
}]
|
|
253
|
+
|
|
254
|
+
Return ONLY the JSON array. If no relations found, return [].`;
|
|
255
|
+
try {
|
|
256
|
+
const response = await openai2.chat.completions.create({
|
|
257
|
+
model: "gpt-4o",
|
|
258
|
+
max_tokens: 2048,
|
|
259
|
+
temperature: 0,
|
|
260
|
+
messages: [{ role: "user", content: prompt }],
|
|
261
|
+
response_format: { type: "json_object" }
|
|
262
|
+
});
|
|
263
|
+
const text = response.choices[0]?.message?.content?.trim();
|
|
264
|
+
if (!text) {
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
const jsonMatch = text.match(/```json\n?([\s\S]*?)\n?```/) || text.match(/\[[\s\S]*\]/);
|
|
268
|
+
const jsonStr = jsonMatch ? jsonMatch[1] || jsonMatch[0] : text;
|
|
269
|
+
const relations = JSON.parse(jsonStr);
|
|
270
|
+
if (!Array.isArray(relations)) {
|
|
271
|
+
return [];
|
|
272
|
+
}
|
|
273
|
+
return relations.filter((r) => r.confidence >= 0.7).map((r) => ({
|
|
274
|
+
toMemoryId: relevantMemories[r.toMemoryIndex].id,
|
|
275
|
+
relationType: r.relationType,
|
|
276
|
+
confidence: r.confidence,
|
|
277
|
+
reasoning: r.reasoning
|
|
278
|
+
}));
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("Relation detection failed:", error);
|
|
281
|
+
return [];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function filterRelevantMemories(newMemory, existingMemories) {
|
|
285
|
+
return existingMemories.filter((existing) => {
|
|
286
|
+
const sharedEntities = newMemory.entityMentions.some(
|
|
287
|
+
(entity) => existing.entityMentions.includes(entity)
|
|
288
|
+
);
|
|
289
|
+
if (sharedEntities) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
const newWords = extractKeywords(newMemory.content);
|
|
293
|
+
const existingWords = extractKeywords(existing.content);
|
|
294
|
+
const overlap = newWords.filter((w) => existingWords.includes(w));
|
|
295
|
+
return overlap.length >= 2;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
function extractKeywords(text) {
|
|
299
|
+
const stopWords = /* @__PURE__ */ new Set([
|
|
300
|
+
"the",
|
|
301
|
+
"a",
|
|
302
|
+
"an",
|
|
303
|
+
"is",
|
|
304
|
+
"are",
|
|
305
|
+
"was",
|
|
306
|
+
"were",
|
|
307
|
+
"be",
|
|
308
|
+
"been",
|
|
309
|
+
"being",
|
|
310
|
+
"have",
|
|
311
|
+
"has",
|
|
312
|
+
"had",
|
|
313
|
+
"do",
|
|
314
|
+
"does",
|
|
315
|
+
"did",
|
|
316
|
+
"will",
|
|
317
|
+
"would",
|
|
318
|
+
"could",
|
|
319
|
+
"should",
|
|
320
|
+
"may",
|
|
321
|
+
"might",
|
|
322
|
+
"must",
|
|
323
|
+
"can",
|
|
324
|
+
"to",
|
|
325
|
+
"of",
|
|
326
|
+
"in",
|
|
327
|
+
"for",
|
|
328
|
+
"on",
|
|
329
|
+
"at",
|
|
330
|
+
"by",
|
|
331
|
+
"from",
|
|
332
|
+
"with",
|
|
333
|
+
"about"
|
|
334
|
+
]);
|
|
335
|
+
return text.toLowerCase().split(/\W+/).filter((word) => word.length > 3 && !stopWords.has(word)).slice(0, 10);
|
|
336
|
+
}
|
|
337
|
+
function shouldInvalidateMemory(relationType) {
|
|
338
|
+
return relationType === "updates" || relationType === "contradicts";
|
|
339
|
+
}
|
|
340
|
+
function buildRelationGraph(relations) {
|
|
341
|
+
const graph = /* @__PURE__ */ new Map();
|
|
342
|
+
for (const relation of relations) {
|
|
343
|
+
if (!graph.has(relation.fromMemoryId)) {
|
|
344
|
+
graph.set(relation.fromMemoryId, []);
|
|
345
|
+
}
|
|
346
|
+
graph.get(relation.fromMemoryId).push({
|
|
347
|
+
memoryId: relation.toMemoryId,
|
|
348
|
+
relationType: relation.relationType
|
|
349
|
+
});
|
|
350
|
+
if (!graph.has(relation.toMemoryId)) {
|
|
351
|
+
graph.set(relation.toMemoryId, []);
|
|
352
|
+
}
|
|
353
|
+
graph.get(relation.toMemoryId).push({
|
|
354
|
+
memoryId: relation.fromMemoryId,
|
|
355
|
+
relationType: `inverse_${relation.relationType}`
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return graph;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export {
|
|
362
|
+
parseTemporalQuery,
|
|
363
|
+
extractEventDate,
|
|
364
|
+
calculateTemporalRelevance,
|
|
365
|
+
detectRelations,
|
|
366
|
+
shouldInvalidateMemory,
|
|
367
|
+
buildRelationGraph
|
|
368
|
+
};
|