@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.
- package/README.md +182 -154
- package/dist/autosubscribe-6EDKPBE2.js +4068 -4068
- package/dist/autosubscribe-GHO6YR5A.js +4068 -4068
- package/dist/autosubscribe-ISDETQIB.js +435 -435
- package/dist/chunk-3WGYBAYR.js +8387 -8387
- package/dist/chunk-52VJYCZ7.js +455 -455
- package/dist/chunk-5KBZQHDL.js +189 -189
- package/dist/chunk-5KIJNY6Z.js +370 -370
- package/dist/chunk-7SN3CKDK.js +1076 -1076
- package/dist/chunk-B3VWOHUA.js +271 -271
- package/dist/chunk-C57DHKTL.js +459 -459
- package/dist/chunk-EI5CE3EY.js +616 -616
- package/dist/chunk-FTWUJBAH.js +386 -386
- package/dist/chunk-H3HSKH2P.js +4841 -4841
- package/dist/chunk-JO3ORBZD.js +616 -616
- package/dist/chunk-L6DXSM2U.js +456 -456
- package/dist/chunk-LMEYV4JD.js +368 -368
- package/dist/chunk-MEFLJ4PV.js +8385 -8385
- package/dist/chunk-OBLI4FE4.js +275 -275
- package/dist/chunk-PPGYJJED.js +271 -271
- package/dist/chunk-QGM4M3NI.js +37 -37
- package/dist/chunk-T7KMSTWP.js +399 -399
- package/dist/chunk-TWEIYHI6.js +399 -399
- package/dist/chunk-UYWE7HSU.js +368 -368
- package/dist/chunk-X2DL2GWT.js +32 -32
- package/dist/chunk-X7HNNNJJ.js +1079 -1079
- package/dist/consolidation-2GCKI4RE.js +220 -220
- package/dist/consolidation-4JOPW6BG.js +220 -220
- package/dist/consolidation-FOVQTWNQ.js +222 -222
- package/dist/consolidation-IFQ52E44.js +209 -209
- package/dist/context-sharing-4ITCNKG4.js +307 -307
- package/dist/context-sharing-6CCFIAKL.js +275 -275
- package/dist/context-sharing-GYKLXHZA.js +307 -307
- package/dist/context-sharing-PH64JTXS.js +308 -308
- package/dist/context-sharing-Y6LTZZOF.js +307 -307
- package/dist/cost-optimization-6OIKRSBV.js +195 -195
- package/dist/cost-optimization-7DVSTL6R.js +307 -307
- package/dist/cost-optimization-BH5NAX33.js +286 -286
- package/dist/cost-optimization-F3L5BS5F.js +303 -303
- package/dist/ingest-2LPTWUUM.js +16 -16
- package/dist/ingest-7T5FAZNC.js +15 -15
- package/dist/ingest-EBNIE7XB.js +15 -15
- package/dist/ingest-FSHT5BCS.js +15 -15
- package/dist/ingest-QE2BTV72.js +14 -14
- package/dist/oracle-3RLQF3DP.js +259 -259
- package/dist/oracle-FKRTQUUG.js +282 -282
- package/dist/oracle-J47QCSEW.js +263 -263
- package/dist/oracle-MDP5MZRC.js +256 -256
- package/dist/search-BLVHWLWC.js +14 -14
- package/dist/search-CZ5NYL5B.js +12 -12
- package/dist/search-EG6TYWWW.js +13 -13
- package/dist/search-I22QQA7T.js +13 -13
- package/dist/search-T7H5G6DW.js +13 -13
- package/dist/server.d.ts +2 -2
- package/dist/server.js +1973 -169
- package/dist/server.js.map +1 -1
- package/package.json +51 -51
package/dist/chunk-PPGYJJED.js
CHANGED
|
@@ -1,271 +1,271 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildRelationGraph,
|
|
3
|
-
calculateTemporalRelevance,
|
|
4
|
-
parseTemporalQuery
|
|
5
|
-
} from "./chunk-LMEYV4JD.js";
|
|
6
|
-
import {
|
|
7
|
-
db,
|
|
8
|
-
embedSingle
|
|
9
|
-
} from "./chunk-3WGYBAYR.js";
|
|
10
|
-
|
|
11
|
-
// ../src/engine/memory/search.ts
|
|
12
|
-
async function searchMemories(params) {
|
|
13
|
-
const {
|
|
14
|
-
query,
|
|
15
|
-
questionDate,
|
|
16
|
-
userId,
|
|
17
|
-
projectId,
|
|
18
|
-
orgId,
|
|
19
|
-
sessionId,
|
|
20
|
-
topK = 10,
|
|
21
|
-
includeInactive = false,
|
|
22
|
-
memoryTypes
|
|
23
|
-
} = params;
|
|
24
|
-
const temporal = params.temporalFilter || await parseTemporalQuery(query, questionDate);
|
|
25
|
-
const queryEmbedding = await embedSingle(query);
|
|
26
|
-
const semanticResults = await vectorSearchMemories({
|
|
27
|
-
embedding: queryEmbedding,
|
|
28
|
-
userId,
|
|
29
|
-
projectId,
|
|
30
|
-
orgId,
|
|
31
|
-
sessionId,
|
|
32
|
-
temporal,
|
|
33
|
-
includeInactive,
|
|
34
|
-
memoryTypes,
|
|
35
|
-
limit: topK * 3
|
|
36
|
-
// Get more for reranking
|
|
37
|
-
});
|
|
38
|
-
if (semanticResults.length === 0) {
|
|
39
|
-
return [];
|
|
40
|
-
}
|
|
41
|
-
const enriched = await enrichWithRelations(semanticResults, topK * 2);
|
|
42
|
-
const scored = enriched.map((memory) => ({
|
|
43
|
-
...memory,
|
|
44
|
-
temporalScore: memory.documentDate ? calculateTemporalRelevance(memory.documentDate, questionDate) : 0.5
|
|
45
|
-
}));
|
|
46
|
-
const combined = scored.map((m) => ({
|
|
47
|
-
...m,
|
|
48
|
-
finalScore: m.similarity * 0.7 + m.temporalScore * 0.3
|
|
49
|
-
}));
|
|
50
|
-
combined.sort((a, b) => b.finalScore - a.finalScore);
|
|
51
|
-
const topMemories = combined.slice(0, topK);
|
|
52
|
-
const results = await injectSourceChunks(topMemories);
|
|
53
|
-
return results;
|
|
54
|
-
}
|
|
55
|
-
async function vectorSearchMemories(params) {
|
|
56
|
-
const {
|
|
57
|
-
embedding,
|
|
58
|
-
userId,
|
|
59
|
-
projectId,
|
|
60
|
-
orgId,
|
|
61
|
-
sessionId,
|
|
62
|
-
temporal,
|
|
63
|
-
includeInactive,
|
|
64
|
-
memoryTypes,
|
|
65
|
-
limit
|
|
66
|
-
} = params;
|
|
67
|
-
const whereConditions = [
|
|
68
|
-
{ projectId }
|
|
69
|
-
];
|
|
70
|
-
if (orgId) {
|
|
71
|
-
whereConditions.push({ orgId });
|
|
72
|
-
}
|
|
73
|
-
if (userId) {
|
|
74
|
-
whereConditions.push({ userId });
|
|
75
|
-
}
|
|
76
|
-
if (sessionId) {
|
|
77
|
-
whereConditions.push({ sessionId });
|
|
78
|
-
}
|
|
79
|
-
if (!includeInactive) {
|
|
80
|
-
whereConditions.push({ isActive: true });
|
|
81
|
-
}
|
|
82
|
-
whereConditions.push({
|
|
83
|
-
OR: [
|
|
84
|
-
{ validUntil: null },
|
|
85
|
-
{ validUntil: { gt: /* @__PURE__ */ new Date() } }
|
|
86
|
-
]
|
|
87
|
-
});
|
|
88
|
-
if (memoryTypes && memoryTypes.length > 0) {
|
|
89
|
-
whereConditions.push({
|
|
90
|
-
memoryType: { in: memoryTypes }
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
if (temporal.hasTemporalConstraint && temporal.dateRange) {
|
|
94
|
-
whereConditions.push({
|
|
95
|
-
documentDate: {
|
|
96
|
-
gte: temporal.dateRange.start,
|
|
97
|
-
lte: temporal.dateRange.end
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
const embeddingStr = `[${embedding.join(",")}]`;
|
|
102
|
-
const results = await db.$queryRaw`
|
|
103
|
-
SELECT
|
|
104
|
-
id,
|
|
105
|
-
content,
|
|
106
|
-
"memoryType",
|
|
107
|
-
"entityMentions",
|
|
108
|
-
confidence,
|
|
109
|
-
version,
|
|
110
|
-
"documentDate",
|
|
111
|
-
"eventDate",
|
|
112
|
-
"validFrom",
|
|
113
|
-
"validUntil",
|
|
114
|
-
"sourceChunkId",
|
|
115
|
-
metadata,
|
|
116
|
-
1 - (embedding <=> ${embeddingStr}::vector) as similarity
|
|
117
|
-
FROM memories
|
|
118
|
-
WHERE
|
|
119
|
-
"projectId" = ${projectId}
|
|
120
|
-
${orgId ? db.$queryRaw`AND "orgId" = ${orgId}` : db.$queryRaw``}
|
|
121
|
-
${userId ? db.$queryRaw`AND "userId" = ${userId}` : db.$queryRaw``}
|
|
122
|
-
${sessionId ? db.$queryRaw`AND "sessionId" = ${sessionId}` : db.$queryRaw``}
|
|
123
|
-
${!includeInactive ? db.$queryRaw`AND "isActive" = true` : db.$queryRaw``}
|
|
124
|
-
AND ("validUntil" IS NULL OR "validUntil" > NOW())
|
|
125
|
-
${memoryTypes && memoryTypes.length > 0 ? db.$queryRaw`AND "memoryType" = ANY(${memoryTypes})` : db.$queryRaw``}
|
|
126
|
-
${temporal.hasTemporalConstraint && temporal.dateRange ? db.$queryRaw`AND "documentDate" >= ${temporal.dateRange.start} AND "documentDate" <= ${temporal.dateRange.end}` : db.$queryRaw``}
|
|
127
|
-
ORDER BY similarity DESC
|
|
128
|
-
LIMIT ${limit}
|
|
129
|
-
`;
|
|
130
|
-
return results;
|
|
131
|
-
}
|
|
132
|
-
async function enrichWithRelations(memories, maxTotal) {
|
|
133
|
-
if (memories.length === 0) {
|
|
134
|
-
return [];
|
|
135
|
-
}
|
|
136
|
-
const memoryIds = memories.map((m) => m.id);
|
|
137
|
-
const relations = await db.memoryRelation.findMany({
|
|
138
|
-
where: {
|
|
139
|
-
OR: [
|
|
140
|
-
{ fromMemoryId: { in: memoryIds } },
|
|
141
|
-
{ toMemoryId: { in: memoryIds } }
|
|
142
|
-
]
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
if (relations.length === 0) {
|
|
146
|
-
return memories;
|
|
147
|
-
}
|
|
148
|
-
const graph = buildRelationGraph(relations);
|
|
149
|
-
const relatedIds = /* @__PURE__ */ new Set();
|
|
150
|
-
for (const memory of memories) {
|
|
151
|
-
const neighbors = graph.get(memory.id) || [];
|
|
152
|
-
for (const neighbor of neighbors.slice(0, 3)) {
|
|
153
|
-
relatedIds.add(neighbor.memoryId);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
memoryIds.forEach((id) => relatedIds.delete(id));
|
|
157
|
-
if (relatedIds.size === 0) {
|
|
158
|
-
return memories;
|
|
159
|
-
}
|
|
160
|
-
const related = await db.memory.findMany({
|
|
161
|
-
where: {
|
|
162
|
-
id: { in: Array.from(relatedIds) },
|
|
163
|
-
isActive: true
|
|
164
|
-
},
|
|
165
|
-
take: maxTotal - memories.length
|
|
166
|
-
});
|
|
167
|
-
const relatedWithScores = related.map((m) => ({
|
|
168
|
-
...m,
|
|
169
|
-
similarity: 0.6,
|
|
170
|
-
// Lower score for related but not directly matched
|
|
171
|
-
isRelated: true
|
|
172
|
-
}));
|
|
173
|
-
return [...memories, ...relatedWithScores];
|
|
174
|
-
}
|
|
175
|
-
async function injectSourceChunks(memories) {
|
|
176
|
-
const chunkIds = memories.map((m) => m.sourceChunkId).filter((id) => id !== null);
|
|
177
|
-
if (chunkIds.length === 0) {
|
|
178
|
-
return memories.map((m) => ({
|
|
179
|
-
memory: {
|
|
180
|
-
id: m.id,
|
|
181
|
-
content: m.content,
|
|
182
|
-
memoryType: m.memoryType,
|
|
183
|
-
entityMentions: m.entityMentions || [],
|
|
184
|
-
confidence: m.confidence,
|
|
185
|
-
version: m.version,
|
|
186
|
-
temporal: {
|
|
187
|
-
documentDate: m.documentDate,
|
|
188
|
-
eventDate: m.eventDate,
|
|
189
|
-
validFrom: m.validFrom,
|
|
190
|
-
validUntil: m.validUntil
|
|
191
|
-
}
|
|
192
|
-
},
|
|
193
|
-
similarity: m.similarity
|
|
194
|
-
}));
|
|
195
|
-
}
|
|
196
|
-
const chunks = await db.chunk.findMany({
|
|
197
|
-
where: {
|
|
198
|
-
id: { in: chunkIds }
|
|
199
|
-
},
|
|
200
|
-
select: {
|
|
201
|
-
id: true,
|
|
202
|
-
content: true,
|
|
203
|
-
metadata: true
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
const chunkMap = new Map(chunks.map((c) => [c.id, c]));
|
|
207
|
-
return memories.map((m) => ({
|
|
208
|
-
memory: {
|
|
209
|
-
id: m.id,
|
|
210
|
-
content: m.content,
|
|
211
|
-
memoryType: m.memoryType,
|
|
212
|
-
entityMentions: m.entityMentions || [],
|
|
213
|
-
confidence: m.confidence,
|
|
214
|
-
version: m.version,
|
|
215
|
-
temporal: {
|
|
216
|
-
documentDate: m.documentDate,
|
|
217
|
-
eventDate: m.eventDate,
|
|
218
|
-
validFrom: m.validFrom,
|
|
219
|
-
validUntil: m.validUntil
|
|
220
|
-
}
|
|
221
|
-
},
|
|
222
|
-
chunk: m.sourceChunkId && chunkMap.has(m.sourceChunkId) ? {
|
|
223
|
-
id: chunkMap.get(m.sourceChunkId).id,
|
|
224
|
-
content: chunkMap.get(m.sourceChunkId).content,
|
|
225
|
-
metadata: chunkMap.get(m.sourceChunkId).metadata
|
|
226
|
-
} : void 0,
|
|
227
|
-
similarity: m.similarity,
|
|
228
|
-
relations: m.isRelated ? [] : void 0
|
|
229
|
-
}));
|
|
230
|
-
}
|
|
231
|
-
async function getSessionMemories(params) {
|
|
232
|
-
const { sessionId, projectId, limit = 50, sinceDate } = params;
|
|
233
|
-
const where = {
|
|
234
|
-
sessionId,
|
|
235
|
-
projectId,
|
|
236
|
-
isActive: true
|
|
237
|
-
};
|
|
238
|
-
if (sinceDate) {
|
|
239
|
-
where.createdAt = { gte: sinceDate };
|
|
240
|
-
}
|
|
241
|
-
return db.memory.findMany({
|
|
242
|
-
where,
|
|
243
|
-
orderBy: {
|
|
244
|
-
createdAt: "desc"
|
|
245
|
-
},
|
|
246
|
-
take: limit
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
async function getUserProfile(params) {
|
|
250
|
-
const { userId, projectId, memoryTypes, limit = 50 } = params;
|
|
251
|
-
const where = {
|
|
252
|
-
userId,
|
|
253
|
-
projectId,
|
|
254
|
-
isActive: true,
|
|
255
|
-
scope: "USER"
|
|
256
|
-
};
|
|
257
|
-
if (memoryTypes) {
|
|
258
|
-
where.memoryType = { in: memoryTypes };
|
|
259
|
-
}
|
|
260
|
-
return db.memory.findMany({
|
|
261
|
-
where,
|
|
262
|
-
orderBy: { importance: "desc" },
|
|
263
|
-
take: Math.min(limit, 100)
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export {
|
|
268
|
-
searchMemories,
|
|
269
|
-
getSessionMemories,
|
|
270
|
-
getUserProfile
|
|
271
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
buildRelationGraph,
|
|
3
|
+
calculateTemporalRelevance,
|
|
4
|
+
parseTemporalQuery
|
|
5
|
+
} from "./chunk-LMEYV4JD.js";
|
|
6
|
+
import {
|
|
7
|
+
db,
|
|
8
|
+
embedSingle
|
|
9
|
+
} from "./chunk-3WGYBAYR.js";
|
|
10
|
+
|
|
11
|
+
// ../src/engine/memory/search.ts
|
|
12
|
+
async function searchMemories(params) {
|
|
13
|
+
const {
|
|
14
|
+
query,
|
|
15
|
+
questionDate,
|
|
16
|
+
userId,
|
|
17
|
+
projectId,
|
|
18
|
+
orgId,
|
|
19
|
+
sessionId,
|
|
20
|
+
topK = 10,
|
|
21
|
+
includeInactive = false,
|
|
22
|
+
memoryTypes
|
|
23
|
+
} = params;
|
|
24
|
+
const temporal = params.temporalFilter || await parseTemporalQuery(query, questionDate);
|
|
25
|
+
const queryEmbedding = await embedSingle(query);
|
|
26
|
+
const semanticResults = await vectorSearchMemories({
|
|
27
|
+
embedding: queryEmbedding,
|
|
28
|
+
userId,
|
|
29
|
+
projectId,
|
|
30
|
+
orgId,
|
|
31
|
+
sessionId,
|
|
32
|
+
temporal,
|
|
33
|
+
includeInactive,
|
|
34
|
+
memoryTypes,
|
|
35
|
+
limit: topK * 3
|
|
36
|
+
// Get more for reranking
|
|
37
|
+
});
|
|
38
|
+
if (semanticResults.length === 0) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const enriched = await enrichWithRelations(semanticResults, topK * 2);
|
|
42
|
+
const scored = enriched.map((memory) => ({
|
|
43
|
+
...memory,
|
|
44
|
+
temporalScore: memory.documentDate ? calculateTemporalRelevance(memory.documentDate, questionDate) : 0.5
|
|
45
|
+
}));
|
|
46
|
+
const combined = scored.map((m) => ({
|
|
47
|
+
...m,
|
|
48
|
+
finalScore: m.similarity * 0.7 + m.temporalScore * 0.3
|
|
49
|
+
}));
|
|
50
|
+
combined.sort((a, b) => b.finalScore - a.finalScore);
|
|
51
|
+
const topMemories = combined.slice(0, topK);
|
|
52
|
+
const results = await injectSourceChunks(topMemories);
|
|
53
|
+
return results;
|
|
54
|
+
}
|
|
55
|
+
async function vectorSearchMemories(params) {
|
|
56
|
+
const {
|
|
57
|
+
embedding,
|
|
58
|
+
userId,
|
|
59
|
+
projectId,
|
|
60
|
+
orgId,
|
|
61
|
+
sessionId,
|
|
62
|
+
temporal,
|
|
63
|
+
includeInactive,
|
|
64
|
+
memoryTypes,
|
|
65
|
+
limit
|
|
66
|
+
} = params;
|
|
67
|
+
const whereConditions = [
|
|
68
|
+
{ projectId }
|
|
69
|
+
];
|
|
70
|
+
if (orgId) {
|
|
71
|
+
whereConditions.push({ orgId });
|
|
72
|
+
}
|
|
73
|
+
if (userId) {
|
|
74
|
+
whereConditions.push({ userId });
|
|
75
|
+
}
|
|
76
|
+
if (sessionId) {
|
|
77
|
+
whereConditions.push({ sessionId });
|
|
78
|
+
}
|
|
79
|
+
if (!includeInactive) {
|
|
80
|
+
whereConditions.push({ isActive: true });
|
|
81
|
+
}
|
|
82
|
+
whereConditions.push({
|
|
83
|
+
OR: [
|
|
84
|
+
{ validUntil: null },
|
|
85
|
+
{ validUntil: { gt: /* @__PURE__ */ new Date() } }
|
|
86
|
+
]
|
|
87
|
+
});
|
|
88
|
+
if (memoryTypes && memoryTypes.length > 0) {
|
|
89
|
+
whereConditions.push({
|
|
90
|
+
memoryType: { in: memoryTypes }
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (temporal.hasTemporalConstraint && temporal.dateRange) {
|
|
94
|
+
whereConditions.push({
|
|
95
|
+
documentDate: {
|
|
96
|
+
gte: temporal.dateRange.start,
|
|
97
|
+
lte: temporal.dateRange.end
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
const embeddingStr = `[${embedding.join(",")}]`;
|
|
102
|
+
const results = await db.$queryRaw`
|
|
103
|
+
SELECT
|
|
104
|
+
id,
|
|
105
|
+
content,
|
|
106
|
+
"memoryType",
|
|
107
|
+
"entityMentions",
|
|
108
|
+
confidence,
|
|
109
|
+
version,
|
|
110
|
+
"documentDate",
|
|
111
|
+
"eventDate",
|
|
112
|
+
"validFrom",
|
|
113
|
+
"validUntil",
|
|
114
|
+
"sourceChunkId",
|
|
115
|
+
metadata,
|
|
116
|
+
1 - (embedding <=> ${embeddingStr}::vector) as similarity
|
|
117
|
+
FROM memories
|
|
118
|
+
WHERE
|
|
119
|
+
"projectId" = ${projectId}
|
|
120
|
+
${orgId ? db.$queryRaw`AND "orgId" = ${orgId}` : db.$queryRaw``}
|
|
121
|
+
${userId ? db.$queryRaw`AND "userId" = ${userId}` : db.$queryRaw``}
|
|
122
|
+
${sessionId ? db.$queryRaw`AND "sessionId" = ${sessionId}` : db.$queryRaw``}
|
|
123
|
+
${!includeInactive ? db.$queryRaw`AND "isActive" = true` : db.$queryRaw``}
|
|
124
|
+
AND ("validUntil" IS NULL OR "validUntil" > NOW())
|
|
125
|
+
${memoryTypes && memoryTypes.length > 0 ? db.$queryRaw`AND "memoryType" = ANY(${memoryTypes})` : db.$queryRaw``}
|
|
126
|
+
${temporal.hasTemporalConstraint && temporal.dateRange ? db.$queryRaw`AND "documentDate" >= ${temporal.dateRange.start} AND "documentDate" <= ${temporal.dateRange.end}` : db.$queryRaw``}
|
|
127
|
+
ORDER BY similarity DESC
|
|
128
|
+
LIMIT ${limit}
|
|
129
|
+
`;
|
|
130
|
+
return results;
|
|
131
|
+
}
|
|
132
|
+
async function enrichWithRelations(memories, maxTotal) {
|
|
133
|
+
if (memories.length === 0) {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
const memoryIds = memories.map((m) => m.id);
|
|
137
|
+
const relations = await db.memoryRelation.findMany({
|
|
138
|
+
where: {
|
|
139
|
+
OR: [
|
|
140
|
+
{ fromMemoryId: { in: memoryIds } },
|
|
141
|
+
{ toMemoryId: { in: memoryIds } }
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
if (relations.length === 0) {
|
|
146
|
+
return memories;
|
|
147
|
+
}
|
|
148
|
+
const graph = buildRelationGraph(relations);
|
|
149
|
+
const relatedIds = /* @__PURE__ */ new Set();
|
|
150
|
+
for (const memory of memories) {
|
|
151
|
+
const neighbors = graph.get(memory.id) || [];
|
|
152
|
+
for (const neighbor of neighbors.slice(0, 3)) {
|
|
153
|
+
relatedIds.add(neighbor.memoryId);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
memoryIds.forEach((id) => relatedIds.delete(id));
|
|
157
|
+
if (relatedIds.size === 0) {
|
|
158
|
+
return memories;
|
|
159
|
+
}
|
|
160
|
+
const related = await db.memory.findMany({
|
|
161
|
+
where: {
|
|
162
|
+
id: { in: Array.from(relatedIds) },
|
|
163
|
+
isActive: true
|
|
164
|
+
},
|
|
165
|
+
take: maxTotal - memories.length
|
|
166
|
+
});
|
|
167
|
+
const relatedWithScores = related.map((m) => ({
|
|
168
|
+
...m,
|
|
169
|
+
similarity: 0.6,
|
|
170
|
+
// Lower score for related but not directly matched
|
|
171
|
+
isRelated: true
|
|
172
|
+
}));
|
|
173
|
+
return [...memories, ...relatedWithScores];
|
|
174
|
+
}
|
|
175
|
+
async function injectSourceChunks(memories) {
|
|
176
|
+
const chunkIds = memories.map((m) => m.sourceChunkId).filter((id) => id !== null);
|
|
177
|
+
if (chunkIds.length === 0) {
|
|
178
|
+
return memories.map((m) => ({
|
|
179
|
+
memory: {
|
|
180
|
+
id: m.id,
|
|
181
|
+
content: m.content,
|
|
182
|
+
memoryType: m.memoryType,
|
|
183
|
+
entityMentions: m.entityMentions || [],
|
|
184
|
+
confidence: m.confidence,
|
|
185
|
+
version: m.version,
|
|
186
|
+
temporal: {
|
|
187
|
+
documentDate: m.documentDate,
|
|
188
|
+
eventDate: m.eventDate,
|
|
189
|
+
validFrom: m.validFrom,
|
|
190
|
+
validUntil: m.validUntil
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
similarity: m.similarity
|
|
194
|
+
}));
|
|
195
|
+
}
|
|
196
|
+
const chunks = await db.chunk.findMany({
|
|
197
|
+
where: {
|
|
198
|
+
id: { in: chunkIds }
|
|
199
|
+
},
|
|
200
|
+
select: {
|
|
201
|
+
id: true,
|
|
202
|
+
content: true,
|
|
203
|
+
metadata: true
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
const chunkMap = new Map(chunks.map((c) => [c.id, c]));
|
|
207
|
+
return memories.map((m) => ({
|
|
208
|
+
memory: {
|
|
209
|
+
id: m.id,
|
|
210
|
+
content: m.content,
|
|
211
|
+
memoryType: m.memoryType,
|
|
212
|
+
entityMentions: m.entityMentions || [],
|
|
213
|
+
confidence: m.confidence,
|
|
214
|
+
version: m.version,
|
|
215
|
+
temporal: {
|
|
216
|
+
documentDate: m.documentDate,
|
|
217
|
+
eventDate: m.eventDate,
|
|
218
|
+
validFrom: m.validFrom,
|
|
219
|
+
validUntil: m.validUntil
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
chunk: m.sourceChunkId && chunkMap.has(m.sourceChunkId) ? {
|
|
223
|
+
id: chunkMap.get(m.sourceChunkId).id,
|
|
224
|
+
content: chunkMap.get(m.sourceChunkId).content,
|
|
225
|
+
metadata: chunkMap.get(m.sourceChunkId).metadata
|
|
226
|
+
} : void 0,
|
|
227
|
+
similarity: m.similarity,
|
|
228
|
+
relations: m.isRelated ? [] : void 0
|
|
229
|
+
}));
|
|
230
|
+
}
|
|
231
|
+
async function getSessionMemories(params) {
|
|
232
|
+
const { sessionId, projectId, limit = 50, sinceDate } = params;
|
|
233
|
+
const where = {
|
|
234
|
+
sessionId,
|
|
235
|
+
projectId,
|
|
236
|
+
isActive: true
|
|
237
|
+
};
|
|
238
|
+
if (sinceDate) {
|
|
239
|
+
where.createdAt = { gte: sinceDate };
|
|
240
|
+
}
|
|
241
|
+
return db.memory.findMany({
|
|
242
|
+
where,
|
|
243
|
+
orderBy: {
|
|
244
|
+
createdAt: "desc"
|
|
245
|
+
},
|
|
246
|
+
take: limit
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async function getUserProfile(params) {
|
|
250
|
+
const { userId, projectId, memoryTypes, limit = 50 } = params;
|
|
251
|
+
const where = {
|
|
252
|
+
userId,
|
|
253
|
+
projectId,
|
|
254
|
+
isActive: true,
|
|
255
|
+
scope: "USER"
|
|
256
|
+
};
|
|
257
|
+
if (memoryTypes) {
|
|
258
|
+
where.memoryType = { in: memoryTypes };
|
|
259
|
+
}
|
|
260
|
+
return db.memory.findMany({
|
|
261
|
+
where,
|
|
262
|
+
orderBy: { importance: "desc" },
|
|
263
|
+
take: Math.min(limit, 100)
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export {
|
|
268
|
+
searchMemories,
|
|
269
|
+
getSessionMemories,
|
|
270
|
+
getUserProfile
|
|
271
|
+
};
|
package/dist/chunk-QGM4M3NI.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
-
}) : x)(function(x) {
|
|
10
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
-
});
|
|
13
|
-
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
-
};
|
|
16
|
-
var __copyProps = (to, from, except, desc) => {
|
|
17
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
-
for (let key of __getOwnPropNames(from))
|
|
19
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
-
}
|
|
22
|
-
return to;
|
|
23
|
-
};
|
|
24
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
-
mod
|
|
31
|
-
));
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
__require,
|
|
35
|
-
__commonJS,
|
|
36
|
-
__toESM
|
|
37
|
-
};
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
8
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
9
|
+
}) : x)(function(x) {
|
|
10
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
11
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
12
|
+
});
|
|
13
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
__require,
|
|
35
|
+
__commonJS,
|
|
36
|
+
__toESM
|
|
37
|
+
};
|