@miniaxolotl/grove 0.2.4 → 0.2.5
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/CHANGELOG.md +1 -1
- package/README.md +6 -6
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +8 -0
- package/dist/config.js.map +1 -1
- package/dist/index.js +253 -289
- package/dist/index.js.map +1 -1
- package/dist/repositories/entity.repository.d.ts.map +1 -1
- package/dist/repositories/entity.repository.js +24 -41
- package/dist/repositories/entity.repository.js.map +1 -1
- package/dist/repositories/memory.repository.d.ts +16 -21
- package/dist/repositories/memory.repository.d.ts.map +1 -1
- package/dist/repositories/memory.repository.js +71 -132
- package/dist/repositories/memory.repository.js.map +1 -1
- package/dist/repositories/relation.repository.d.ts.map +1 -1
- package/dist/repositories/relation.repository.js +25 -43
- package/dist/repositories/relation.repository.js.map +1 -1
- package/dist/services/embedding.d.ts +1 -0
- package/dist/services/embedding.d.ts.map +1 -1
- package/dist/services/embedding.js +8 -1
- package/dist/services/embedding.js.map +1 -1
- package/dist/services/qdrant.d.ts +5 -2
- package/dist/services/qdrant.d.ts.map +1 -1
- package/dist/services/qdrant.js +27 -10
- package/dist/services/qdrant.js.map +1 -1
- package/dist/utils/filter.d.ts +10 -0
- package/dist/utils/filter.d.ts.map +1 -0
- package/dist/utils/filter.js +15 -0
- package/dist/utils/filter.js.map +1 -0
- package/eslint.config.js +1 -1
- package/package.json +12 -12
- package/src/config.ts +5 -0
- package/src/index.ts +263 -336
- package/src/repositories/entity.repository.ts +30 -42
- package/src/repositories/memory.repository.ts +105 -166
- package/src/repositories/relation.repository.ts +33 -41
- package/src/services/embedding.ts +9 -1
- package/src/services/qdrant.ts +30 -13
- package/src/utils/filter.ts +20 -0
- package/tsconfig.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "crypto";
|
|
2
2
|
import { config } from "../config.ts";
|
|
3
|
-
import { qdrant
|
|
3
|
+
import { qdrant } from "../services/qdrant.ts";
|
|
4
|
+
import { buildFilter } from "../utils/filter.ts";
|
|
4
5
|
|
|
5
6
|
const COLLECTION = `${config.collection.prefix}_entities`;
|
|
6
7
|
|
|
@@ -62,7 +63,7 @@ export async function createEntity(
|
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
export async function getEntity(name: string): Promise<Entity | null> {
|
|
65
|
-
const points = await qdrant.scrollPoints(COLLECTION, {
|
|
66
|
+
const { points } = await qdrant.scrollPoints(COLLECTION, {
|
|
66
67
|
must: [{ key: "name", match: { value: name } }],
|
|
67
68
|
});
|
|
68
69
|
|
|
@@ -83,13 +84,11 @@ export async function searchEntities(
|
|
|
83
84
|
entityType?: string,
|
|
84
85
|
limit: number = 10,
|
|
85
86
|
): Promise<Entity[]> {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
92
|
-
const points = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
87
|
+
const filter = buildFilter([
|
|
88
|
+
query ? { key: "name", value: query } : null,
|
|
89
|
+
entityType ? { key: "entityType", value: entityType } : null,
|
|
90
|
+
]);
|
|
91
|
+
const { points } = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
93
92
|
|
|
94
93
|
return points.map((p) => ({
|
|
95
94
|
id: p.id,
|
|
@@ -109,7 +108,7 @@ export async function addObservations(
|
|
|
109
108
|
|
|
110
109
|
const updated = {
|
|
111
110
|
...entity,
|
|
112
|
-
observations: [...entity.observations, ...newObservations],
|
|
111
|
+
observations: Array.from(new Set([...entity.observations, ...newObservations])),
|
|
113
112
|
};
|
|
114
113
|
|
|
115
114
|
await qdrant.upsertPoints(COLLECTION, [
|
|
@@ -135,13 +134,13 @@ export async function deleteEntities(
|
|
|
135
134
|
entityType?: string;
|
|
136
135
|
} = {},
|
|
137
136
|
): Promise<void> {
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (
|
|
137
|
+
const qdrantFilter = buildFilter([
|
|
138
|
+
filter.name ? { key: "name", value: filter.name } : null,
|
|
139
|
+
filter.entityType ? { key: "entityType", value: filter.entityType } : null,
|
|
140
|
+
]);
|
|
141
|
+
if (!qdrantFilter) return;
|
|
143
142
|
|
|
144
|
-
await qdrant.deleteByFilter(COLLECTION,
|
|
143
|
+
await qdrant.deleteByFilter(COLLECTION, qdrantFilter);
|
|
145
144
|
}
|
|
146
145
|
|
|
147
146
|
export async function getEntityById(id: string): Promise<Entity | null> {
|
|
@@ -210,45 +209,34 @@ export async function listEntities(
|
|
|
210
209
|
limit: number = 100,
|
|
211
210
|
offset?: string,
|
|
212
211
|
): Promise<{ entities: Entity[]; offset: string | null }> {
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
217
|
-
|
|
218
|
-
const url = `${config.qdrant.url}/collections/${COLLECTION}/points/scroll`;
|
|
219
|
-
const body: Record<string, unknown> = { limit, with_payload: true };
|
|
220
|
-
if (filter) body.filter = filter;
|
|
221
|
-
if (offset) body.offset = offset;
|
|
222
|
-
|
|
223
|
-
const response = await fetch(url, {
|
|
224
|
-
method: "POST",
|
|
225
|
-
headers: {
|
|
226
|
-
"Content-Type": "application/json",
|
|
227
|
-
"api-key": config.qdrant.apiKey,
|
|
228
|
-
"User-Agent": "grove/1.0",
|
|
229
|
-
},
|
|
230
|
-
body: JSON.stringify(body),
|
|
231
|
-
});
|
|
212
|
+
const filter = buildFilter([
|
|
213
|
+
entityType ? { key: "entityType", value: entityType } : null,
|
|
214
|
+
]);
|
|
232
215
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
216
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
217
|
+
COLLECTION,
|
|
218
|
+
filter,
|
|
219
|
+
limit,
|
|
220
|
+
offset,
|
|
221
|
+
);
|
|
237
222
|
|
|
238
223
|
return {
|
|
239
|
-
entities:
|
|
224
|
+
entities: points.map((p) => ({
|
|
240
225
|
id: p.id,
|
|
241
226
|
name: p.payload.name as string,
|
|
242
227
|
entityType: p.payload.entityType as string,
|
|
243
228
|
observations: (p.payload.observations as string[]) ?? [],
|
|
244
229
|
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
245
230
|
})),
|
|
246
|
-
offset:
|
|
231
|
+
offset: nextPageOffset,
|
|
247
232
|
};
|
|
248
233
|
}
|
|
249
234
|
|
|
250
235
|
export async function initEntityCollection(): Promise<void> {
|
|
251
|
-
await qdrant.ensureCollection(COLLECTION, config.vector.dim
|
|
236
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim, [
|
|
237
|
+
"name",
|
|
238
|
+
"entityType",
|
|
239
|
+
]);
|
|
252
240
|
}
|
|
253
241
|
|
|
254
242
|
export const entityRepository = {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "crypto";
|
|
2
2
|
import { config } from "../config.ts";
|
|
3
3
|
import { qdrant, type VectorPoint } from "../services/qdrant.ts";
|
|
4
|
+
import { buildFilter } from "../utils/filter.ts";
|
|
4
5
|
|
|
5
6
|
const COLLECTION = `${config.collection.prefix}_memories`;
|
|
6
7
|
|
|
@@ -13,6 +14,8 @@ export interface Memory {
|
|
|
13
14
|
tags?: string[];
|
|
14
15
|
createdAt: string;
|
|
15
16
|
updatedAt?: string;
|
|
17
|
+
importance?: number;
|
|
18
|
+
sessionId?: string;
|
|
16
19
|
};
|
|
17
20
|
}
|
|
18
21
|
|
|
@@ -80,14 +83,11 @@ export async function searchMemories(
|
|
|
80
83
|
const embedFn = await getEmbedTexts();
|
|
81
84
|
|
|
82
85
|
if (!embedFn) {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
90
|
-
const points = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
86
|
+
const filter = buildFilter([
|
|
87
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
88
|
+
options.tags?.length ? { key: "metadata.tags", any: options.tags } : null,
|
|
89
|
+
]);
|
|
90
|
+
const { points } = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
91
91
|
return points.map((r) => ({
|
|
92
92
|
id: r.id,
|
|
93
93
|
text: r.payload.text as string,
|
|
@@ -98,13 +98,10 @@ export async function searchMemories(
|
|
|
98
98
|
|
|
99
99
|
const [queryVector] = await embedFn([query]);
|
|
100
100
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
must.push({ key: "metadata.tags", match: { any: options.tags } });
|
|
106
|
-
}
|
|
107
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
101
|
+
const filter = buildFilter([
|
|
102
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
103
|
+
options.tags?.length ? { key: "metadata.tags", any: options.tags } : null,
|
|
104
|
+
]);
|
|
108
105
|
|
|
109
106
|
const results = await qdrant.searchVectors(
|
|
110
107
|
COLLECTION,
|
|
@@ -129,15 +126,13 @@ export async function deleteMemoriesByFilter(filter: {
|
|
|
129
126
|
project?: string;
|
|
130
127
|
tags?: string[];
|
|
131
128
|
}): Promise<void> {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
if (must.length === 0) return;
|
|
129
|
+
const qdrantFilter = buildFilter([
|
|
130
|
+
filter.project ? { key: "metadata.project", value: filter.project } : null,
|
|
131
|
+
filter.tags?.length ? { key: "metadata.tags", any: filter.tags } : null,
|
|
132
|
+
]);
|
|
133
|
+
if (!qdrantFilter) return;
|
|
139
134
|
|
|
140
|
-
await qdrant.deleteByFilter(COLLECTION,
|
|
135
|
+
await qdrant.deleteByFilter(COLLECTION, qdrantFilter);
|
|
141
136
|
}
|
|
142
137
|
|
|
143
138
|
export async function getMemoryById(id: string): Promise<Memory | null> {
|
|
@@ -168,7 +163,7 @@ export async function updateMemory(
|
|
|
168
163
|
};
|
|
169
164
|
|
|
170
165
|
let vector: number[];
|
|
171
|
-
if (updates.text) {
|
|
166
|
+
if (updates.text !== undefined) {
|
|
172
167
|
const embedFn = await getEmbedTexts();
|
|
173
168
|
if (embedFn) {
|
|
174
169
|
[vector] = await embedFn([updated.text]);
|
|
@@ -191,96 +186,6 @@ export async function updateMemory(
|
|
|
191
186
|
return updated;
|
|
192
187
|
}
|
|
193
188
|
|
|
194
|
-
export async function saveMemoryBatch(
|
|
195
|
-
items: Array<{
|
|
196
|
-
text: string;
|
|
197
|
-
metadata?: Partial<Memory["metadata"]>;
|
|
198
|
-
}>,
|
|
199
|
-
): Promise<Memory[]> {
|
|
200
|
-
const embedFn = await getEmbedTexts();
|
|
201
|
-
const texts = items.map((i) => i.text);
|
|
202
|
-
|
|
203
|
-
let vectors: number[][];
|
|
204
|
-
if (embedFn) {
|
|
205
|
-
vectors = await embedFn(texts);
|
|
206
|
-
} else {
|
|
207
|
-
vectors = items.map(() => Array(config.vector.dim).fill(0));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const now = new Date().toISOString();
|
|
211
|
-
const points: VectorPoint[] = items.map((item, i) => {
|
|
212
|
-
const id = randomUUID();
|
|
213
|
-
return {
|
|
214
|
-
id,
|
|
215
|
-
vector: vectors[i],
|
|
216
|
-
payload: {
|
|
217
|
-
id,
|
|
218
|
-
text: item.text,
|
|
219
|
-
metadata: {
|
|
220
|
-
...item.metadata,
|
|
221
|
-
createdAt: item.metadata?.createdAt ?? now,
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
};
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
if (points.length > 0) {
|
|
228
|
-
await qdrant.upsertPoints(COLLECTION, points);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return points.map((p) => ({
|
|
232
|
-
id: p.id,
|
|
233
|
-
text: p.payload.text as string,
|
|
234
|
-
metadata: p.payload.metadata as Memory["metadata"],
|
|
235
|
-
}));
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export async function searchMemoriesBatch(
|
|
239
|
-
queries: Array<{
|
|
240
|
-
query: string;
|
|
241
|
-
project?: string;
|
|
242
|
-
tags?: string[];
|
|
243
|
-
limit?: number;
|
|
244
|
-
}>,
|
|
245
|
-
): Promise<Array<MemorySearchResult[]>> {
|
|
246
|
-
const embedFn = await getEmbedTexts();
|
|
247
|
-
|
|
248
|
-
if (!embedFn) {
|
|
249
|
-
return queries.map(() => []);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const queryTexts = queries.map((q) => q.query);
|
|
253
|
-
const allVectors = await embedFn(queryTexts);
|
|
254
|
-
|
|
255
|
-
const results: Array<MemorySearchResult[]> = await Promise.all(
|
|
256
|
-
queries.map(async (q, i) => {
|
|
257
|
-
const must: Record<string, unknown>[] = [];
|
|
258
|
-
if (q.project)
|
|
259
|
-
must.push({ key: "metadata.project", match: { value: q.project } });
|
|
260
|
-
if (q.tags && q.tags.length > 0) {
|
|
261
|
-
must.push({ key: "metadata.tags", match: { any: q.tags } });
|
|
262
|
-
}
|
|
263
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
264
|
-
const limit = q.limit ?? 5;
|
|
265
|
-
|
|
266
|
-
const found = await qdrant.searchVectors(
|
|
267
|
-
COLLECTION,
|
|
268
|
-
allVectors[i],
|
|
269
|
-
limit,
|
|
270
|
-
filter,
|
|
271
|
-
);
|
|
272
|
-
return found.map((r) => ({
|
|
273
|
-
id: r.id,
|
|
274
|
-
text: r.payload.text as string,
|
|
275
|
-
metadata: r.payload.metadata as Memory["metadata"],
|
|
276
|
-
score: r.score ?? 0,
|
|
277
|
-
}));
|
|
278
|
-
}),
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
return results;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
189
|
export async function scrollMemories(
|
|
285
190
|
options: {
|
|
286
191
|
project?: string;
|
|
@@ -290,35 +195,19 @@ export async function scrollMemories(
|
|
|
290
195
|
} = {},
|
|
291
196
|
): Promise<{ memories: Memory[]; offset: string | null }> {
|
|
292
197
|
const limit = options.limit ?? 100;
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
must.push({ key: "metadata.tags", match: { any: options.tags } });
|
|
298
|
-
}
|
|
299
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
300
|
-
|
|
301
|
-
const url = `${config.qdrant.url}/collections/${COLLECTION}/points/scroll`;
|
|
302
|
-
const scrollBody: Record<string, unknown> = { limit, with_payload: true };
|
|
303
|
-
if (filter) scrollBody.filter = filter;
|
|
304
|
-
if (options.offset) scrollBody.offset = options.offset;
|
|
305
|
-
|
|
306
|
-
const response = await fetch(url, {
|
|
307
|
-
method: "POST",
|
|
308
|
-
headers: {
|
|
309
|
-
"Content-Type": "application/json",
|
|
310
|
-
"api-key": config.qdrant.apiKey,
|
|
311
|
-
"User-Agent": "grove/1.0",
|
|
312
|
-
},
|
|
313
|
-
body: JSON.stringify(scrollBody),
|
|
314
|
-
});
|
|
198
|
+
const filter = buildFilter([
|
|
199
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
200
|
+
options.tags?.length ? { key: "metadata.tags", any: options.tags } : null,
|
|
201
|
+
]);
|
|
315
202
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
203
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
204
|
+
COLLECTION,
|
|
205
|
+
filter,
|
|
206
|
+
limit,
|
|
207
|
+
options.offset,
|
|
208
|
+
);
|
|
320
209
|
|
|
321
|
-
const memories: Memory[] =
|
|
210
|
+
const memories: Memory[] = points.map((p) => ({
|
|
322
211
|
id: p.id,
|
|
323
212
|
text: p.payload.text as string,
|
|
324
213
|
metadata: p.payload.metadata as Memory["metadata"],
|
|
@@ -326,7 +215,7 @@ export async function scrollMemories(
|
|
|
326
215
|
|
|
327
216
|
return {
|
|
328
217
|
memories,
|
|
329
|
-
offset:
|
|
218
|
+
offset: nextPageOffset,
|
|
330
219
|
};
|
|
331
220
|
}
|
|
332
221
|
|
|
@@ -344,35 +233,88 @@ export async function getMemoriesStats(): Promise<{
|
|
|
344
233
|
};
|
|
345
234
|
}
|
|
346
235
|
|
|
347
|
-
export async function
|
|
236
|
+
export async function compactMemories(options: {
|
|
237
|
+
project?: string;
|
|
238
|
+
sessionId?: string;
|
|
239
|
+
importanceThreshold?: number;
|
|
240
|
+
}): Promise<{ compacted: number }> {
|
|
241
|
+
const threshold = options.importanceThreshold ?? 0.6;
|
|
242
|
+
const filter = buildFilter([
|
|
243
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
244
|
+
options.sessionId ? { key: "metadata.sessionId", value: options.sessionId } : null,
|
|
245
|
+
]);
|
|
246
|
+
|
|
348
247
|
let offset: string | null = null;
|
|
349
|
-
|
|
248
|
+
let deleted = 0;
|
|
350
249
|
|
|
351
250
|
do {
|
|
352
|
-
const {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
251
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
252
|
+
COLLECTION,
|
|
253
|
+
filter,
|
|
254
|
+
100,
|
|
255
|
+
offset ?? undefined,
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const toDelete = points
|
|
259
|
+
.filter((p) => {
|
|
260
|
+
const imp = (p.payload.metadata as Memory["metadata"])?.importance;
|
|
261
|
+
return imp !== undefined && imp < threshold;
|
|
262
|
+
})
|
|
263
|
+
.map((p) => p.id);
|
|
264
|
+
|
|
265
|
+
if (toDelete.length > 0) {
|
|
266
|
+
await qdrant.deletePoints(COLLECTION, toDelete);
|
|
267
|
+
deleted += toDelete.length;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
offset = nextPageOffset;
|
|
358
271
|
} while (offset !== null);
|
|
359
272
|
|
|
360
|
-
return
|
|
273
|
+
return { compacted: deleted };
|
|
361
274
|
}
|
|
362
275
|
|
|
363
|
-
export async function
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
276
|
+
export async function pruneMemories(options: {
|
|
277
|
+
threshold: number;
|
|
278
|
+
project?: string;
|
|
279
|
+
}): Promise<{ pruned: number }> {
|
|
280
|
+
const filter = buildFilter([
|
|
281
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
282
|
+
]);
|
|
283
|
+
|
|
284
|
+
let offset: string | null = null;
|
|
285
|
+
let deleted = 0;
|
|
286
|
+
|
|
287
|
+
do {
|
|
288
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
289
|
+
COLLECTION,
|
|
290
|
+
filter,
|
|
291
|
+
100,
|
|
292
|
+
offset ?? undefined,
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
const toDelete = points
|
|
296
|
+
.filter((p) => {
|
|
297
|
+
const imp = (p.payload.metadata as Memory["metadata"])?.importance;
|
|
298
|
+
return imp !== undefined && imp < options.threshold;
|
|
299
|
+
})
|
|
300
|
+
.map((p) => p.id);
|
|
301
|
+
|
|
302
|
+
if (toDelete.length > 0) {
|
|
303
|
+
await qdrant.deletePoints(COLLECTION, toDelete);
|
|
304
|
+
deleted += toDelete.length;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
offset = nextPageOffset;
|
|
308
|
+
} while (offset !== null);
|
|
309
|
+
|
|
310
|
+
return { pruned: deleted };
|
|
372
311
|
}
|
|
373
312
|
|
|
374
313
|
export async function initMemoryCollection(): Promise<void> {
|
|
375
|
-
await qdrant.ensureCollection(COLLECTION, config.vector.dim
|
|
314
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim, [
|
|
315
|
+
"metadata.project",
|
|
316
|
+
"metadata.tags",
|
|
317
|
+
]);
|
|
376
318
|
}
|
|
377
319
|
|
|
378
320
|
export const memoryRepository = {
|
|
@@ -381,12 +323,9 @@ export const memoryRepository = {
|
|
|
381
323
|
delete: deleteMemories,
|
|
382
324
|
deleteByFilter: deleteMemoriesByFilter,
|
|
383
325
|
init: initMemoryCollection,
|
|
384
|
-
getById: getMemoryById,
|
|
385
326
|
update: updateMemory,
|
|
386
|
-
saveBatch: saveMemoryBatch,
|
|
387
|
-
searchBatch: searchMemoriesBatch,
|
|
388
327
|
scroll: scrollMemories,
|
|
389
328
|
stats: getMemoriesStats,
|
|
390
|
-
|
|
391
|
-
|
|
329
|
+
compact: compactMemories,
|
|
330
|
+
prune: pruneMemories,
|
|
392
331
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "crypto";
|
|
2
2
|
import { config } from "../config.ts";
|
|
3
|
-
import { qdrant
|
|
3
|
+
import { qdrant } from "../services/qdrant.ts";
|
|
4
|
+
import { buildFilter } from "../utils/filter.ts";
|
|
4
5
|
|
|
5
6
|
const COLLECTION = `${config.collection.prefix}_relations`;
|
|
6
7
|
|
|
@@ -55,13 +56,12 @@ export async function searchRelations(
|
|
|
55
56
|
} = {},
|
|
56
57
|
defaultLimit: number = 20,
|
|
57
58
|
): Promise<Relation[]> {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
const points = await qdrant.scrollPoints(COLLECTION, filter, options.limit ?? defaultLimit);
|
|
59
|
+
const filter = buildFilter([
|
|
60
|
+
options.from ? { key: "from", value: options.from } : null,
|
|
61
|
+
options.to ? { key: "to", value: options.to } : null,
|
|
62
|
+
options.relationType ? { key: "relationType", value: options.relationType } : null,
|
|
63
|
+
]);
|
|
64
|
+
const { points } = await qdrant.scrollPoints(COLLECTION, filter, options.limit ?? defaultLimit);
|
|
65
65
|
|
|
66
66
|
return points.map((p) => ({
|
|
67
67
|
id: p.id,
|
|
@@ -73,13 +73,14 @@ export async function searchRelations(
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
export async function deleteRelations(options: { from?: string; to?: string; relationType?: string }): Promise<void> {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
const filter = buildFilter([
|
|
77
|
+
options.from ? { key: "from", value: options.from } : null,
|
|
78
|
+
options.to ? { key: "to", value: options.to } : null,
|
|
79
|
+
options.relationType ? { key: "relationType", value: options.relationType } : null,
|
|
80
|
+
]);
|
|
81
|
+
if (!filter) return;
|
|
82
|
+
|
|
83
|
+
await qdrant.deleteByFilter(COLLECTION, filter);
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
export async function getRelationStats(): Promise<{
|
|
@@ -101,44 +102,35 @@ export async function listRelations(
|
|
|
101
102
|
limit: number = 100,
|
|
102
103
|
offset?: string,
|
|
103
104
|
): Promise<{ relations: Relation[]; offset: string | null }> {
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
method: "POST",
|
|
115
|
-
headers: {
|
|
116
|
-
"Content-Type": "application/json",
|
|
117
|
-
"api-key": config.qdrant.apiKey,
|
|
118
|
-
"User-Agent": "grove/1.0",
|
|
119
|
-
},
|
|
120
|
-
body: JSON.stringify(body),
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
if (!response.ok) throw new Error(`scroll failed: ${response.status}`);
|
|
124
|
-
const data = (await response.json()) as {
|
|
125
|
-
result: { points: VectorPoint[]; next_page_offset: string | null };
|
|
126
|
-
};
|
|
105
|
+
const filter = buildFilter([
|
|
106
|
+
relationType ? { key: "relationType", value: relationType } : null,
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
110
|
+
COLLECTION,
|
|
111
|
+
filter,
|
|
112
|
+
limit,
|
|
113
|
+
offset,
|
|
114
|
+
);
|
|
127
115
|
|
|
128
116
|
return {
|
|
129
|
-
relations:
|
|
117
|
+
relations: points.map((p) => ({
|
|
130
118
|
id: p.id,
|
|
131
119
|
from: p.payload.from as string,
|
|
132
120
|
relationType: p.payload.relationType as string,
|
|
133
121
|
to: p.payload.to as string,
|
|
134
122
|
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
135
123
|
})),
|
|
136
|
-
offset:
|
|
124
|
+
offset: nextPageOffset,
|
|
137
125
|
};
|
|
138
126
|
}
|
|
139
127
|
|
|
140
128
|
export async function initRelationCollection(): Promise<void> {
|
|
141
|
-
await qdrant.ensureCollection(COLLECTION, config.vector.dim
|
|
129
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim, [
|
|
130
|
+
"from",
|
|
131
|
+
"to",
|
|
132
|
+
"relationType",
|
|
133
|
+
]);
|
|
142
134
|
}
|
|
143
135
|
|
|
144
136
|
export const relationRepository = {
|
|
@@ -96,7 +96,8 @@ async function getLocalEmbedder(): Promise<
|
|
|
96
96
|
};
|
|
97
97
|
|
|
98
98
|
return _localEmbedder;
|
|
99
|
-
} catch {
|
|
99
|
+
} catch (err) {
|
|
100
|
+
console.warn("Failed to initialize local embedder (@huggingface/transformers):", err);
|
|
100
101
|
_localEmbedder = null;
|
|
101
102
|
return null;
|
|
102
103
|
}
|
|
@@ -140,3 +141,10 @@ export async function embedText(text: string): Promise<number[]> {
|
|
|
140
141
|
const results = await embedTexts([text]);
|
|
141
142
|
return results[0];
|
|
142
143
|
}
|
|
144
|
+
|
|
145
|
+
export async function warmup(): Promise<void> {
|
|
146
|
+
const localEmbedder = await getLocalEmbedder();
|
|
147
|
+
if (localEmbedder) {
|
|
148
|
+
await localEmbedder(["warmup"]);
|
|
149
|
+
}
|
|
150
|
+
}
|