@miniaxolotl/grove 0.2.4 → 0.2.6
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 +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -1
- package/dist/config.js.map +1 -1
- package/dist/index.js +260 -292
- 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 -35
- 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 +14 -13
- package/src/config.ts +6 -1
- package/src/index.ts +270 -339
- package/src/repositories/entity.repository.ts +30 -42
- package/src/repositories/memory.repository.ts +105 -181
- 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,24 +1,12 @@
|
|
|
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";
|
|
5
|
+
import type { Memory, MemorySearchResult } from "@lib/shared";
|
|
4
6
|
|
|
5
7
|
const COLLECTION = `${config.collection.prefix}_memories`;
|
|
6
8
|
|
|
7
|
-
export
|
|
8
|
-
id: string;
|
|
9
|
-
text: string;
|
|
10
|
-
metadata: {
|
|
11
|
-
source?: string;
|
|
12
|
-
project?: string;
|
|
13
|
-
tags?: string[];
|
|
14
|
-
createdAt: string;
|
|
15
|
-
updatedAt?: string;
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface MemorySearchResult extends Memory {
|
|
20
|
-
score: number;
|
|
21
|
-
}
|
|
9
|
+
export type { Memory, MemorySearchResult };
|
|
22
10
|
|
|
23
11
|
let _embedTexts: ((texts: string[]) => Promise<number[][]>) | null = null;
|
|
24
12
|
|
|
@@ -80,14 +68,11 @@ export async function searchMemories(
|
|
|
80
68
|
const embedFn = await getEmbedTexts();
|
|
81
69
|
|
|
82
70
|
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);
|
|
71
|
+
const filter = buildFilter([
|
|
72
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
73
|
+
options.tags?.length ? { key: "metadata.tags", any: options.tags } : null,
|
|
74
|
+
]);
|
|
75
|
+
const { points } = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
91
76
|
return points.map((r) => ({
|
|
92
77
|
id: r.id,
|
|
93
78
|
text: r.payload.text as string,
|
|
@@ -98,13 +83,10 @@ export async function searchMemories(
|
|
|
98
83
|
|
|
99
84
|
const [queryVector] = await embedFn([query]);
|
|
100
85
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
must.push({ key: "metadata.tags", match: { any: options.tags } });
|
|
106
|
-
}
|
|
107
|
-
const filter = must.length > 0 ? { must } : undefined;
|
|
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
|
+
]);
|
|
108
90
|
|
|
109
91
|
const results = await qdrant.searchVectors(
|
|
110
92
|
COLLECTION,
|
|
@@ -129,15 +111,13 @@ export async function deleteMemoriesByFilter(filter: {
|
|
|
129
111
|
project?: string;
|
|
130
112
|
tags?: string[];
|
|
131
113
|
}): Promise<void> {
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
if (must.length === 0) return;
|
|
114
|
+
const qdrantFilter = buildFilter([
|
|
115
|
+
filter.project ? { key: "metadata.project", value: filter.project } : null,
|
|
116
|
+
filter.tags?.length ? { key: "metadata.tags", any: filter.tags } : null,
|
|
117
|
+
]);
|
|
118
|
+
if (!qdrantFilter) return;
|
|
139
119
|
|
|
140
|
-
await qdrant.deleteByFilter(COLLECTION,
|
|
120
|
+
await qdrant.deleteByFilter(COLLECTION, qdrantFilter);
|
|
141
121
|
}
|
|
142
122
|
|
|
143
123
|
export async function getMemoryById(id: string): Promise<Memory | null> {
|
|
@@ -168,7 +148,7 @@ export async function updateMemory(
|
|
|
168
148
|
};
|
|
169
149
|
|
|
170
150
|
let vector: number[];
|
|
171
|
-
if (updates.text) {
|
|
151
|
+
if (updates.text !== undefined) {
|
|
172
152
|
const embedFn = await getEmbedTexts();
|
|
173
153
|
if (embedFn) {
|
|
174
154
|
[vector] = await embedFn([updated.text]);
|
|
@@ -191,96 +171,6 @@ export async function updateMemory(
|
|
|
191
171
|
return updated;
|
|
192
172
|
}
|
|
193
173
|
|
|
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
174
|
export async function scrollMemories(
|
|
285
175
|
options: {
|
|
286
176
|
project?: string;
|
|
@@ -290,35 +180,19 @@ export async function scrollMemories(
|
|
|
290
180
|
} = {},
|
|
291
181
|
): Promise<{ memories: Memory[]; offset: string | null }> {
|
|
292
182
|
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
|
-
});
|
|
183
|
+
const filter = buildFilter([
|
|
184
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
185
|
+
options.tags?.length ? { key: "metadata.tags", any: options.tags } : null,
|
|
186
|
+
]);
|
|
315
187
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
188
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
189
|
+
COLLECTION,
|
|
190
|
+
filter,
|
|
191
|
+
limit,
|
|
192
|
+
options.offset,
|
|
193
|
+
);
|
|
320
194
|
|
|
321
|
-
const memories: Memory[] =
|
|
195
|
+
const memories: Memory[] = points.map((p) => ({
|
|
322
196
|
id: p.id,
|
|
323
197
|
text: p.payload.text as string,
|
|
324
198
|
metadata: p.payload.metadata as Memory["metadata"],
|
|
@@ -326,7 +200,7 @@ export async function scrollMemories(
|
|
|
326
200
|
|
|
327
201
|
return {
|
|
328
202
|
memories,
|
|
329
|
-
offset:
|
|
203
|
+
offset: nextPageOffset,
|
|
330
204
|
};
|
|
331
205
|
}
|
|
332
206
|
|
|
@@ -344,35 +218,88 @@ export async function getMemoriesStats(): Promise<{
|
|
|
344
218
|
};
|
|
345
219
|
}
|
|
346
220
|
|
|
347
|
-
export async function
|
|
221
|
+
export async function compactMemories(options: {
|
|
222
|
+
project?: string;
|
|
223
|
+
sessionId?: string;
|
|
224
|
+
importanceThreshold?: number;
|
|
225
|
+
}): Promise<{ compacted: number }> {
|
|
226
|
+
const threshold = options.importanceThreshold ?? 0.6;
|
|
227
|
+
const filter = buildFilter([
|
|
228
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
229
|
+
options.sessionId ? { key: "metadata.sessionId", value: options.sessionId } : null,
|
|
230
|
+
]);
|
|
231
|
+
|
|
348
232
|
let offset: string | null = null;
|
|
349
|
-
|
|
233
|
+
let deleted = 0;
|
|
350
234
|
|
|
351
235
|
do {
|
|
352
|
-
const {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
236
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
237
|
+
COLLECTION,
|
|
238
|
+
filter,
|
|
239
|
+
100,
|
|
240
|
+
offset ?? undefined,
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const toDelete = points
|
|
244
|
+
.filter((p) => {
|
|
245
|
+
const imp = (p.payload.metadata as Memory["metadata"])?.importance;
|
|
246
|
+
return imp !== undefined && imp < threshold;
|
|
247
|
+
})
|
|
248
|
+
.map((p) => p.id);
|
|
249
|
+
|
|
250
|
+
if (toDelete.length > 0) {
|
|
251
|
+
await qdrant.deletePoints(COLLECTION, toDelete);
|
|
252
|
+
deleted += toDelete.length;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
offset = nextPageOffset;
|
|
358
256
|
} while (offset !== null);
|
|
359
257
|
|
|
360
|
-
return
|
|
258
|
+
return { compacted: deleted };
|
|
361
259
|
}
|
|
362
260
|
|
|
363
|
-
export async function
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
261
|
+
export async function pruneMemories(options: {
|
|
262
|
+
threshold: number;
|
|
263
|
+
project?: string;
|
|
264
|
+
}): Promise<{ pruned: number }> {
|
|
265
|
+
const filter = buildFilter([
|
|
266
|
+
options.project ? { key: "metadata.project", value: options.project } : null,
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
let offset: string | null = null;
|
|
270
|
+
let deleted = 0;
|
|
271
|
+
|
|
272
|
+
do {
|
|
273
|
+
const { points, nextPageOffset } = await qdrant.scrollPoints(
|
|
274
|
+
COLLECTION,
|
|
275
|
+
filter,
|
|
276
|
+
100,
|
|
277
|
+
offset ?? undefined,
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
const toDelete = points
|
|
281
|
+
.filter((p) => {
|
|
282
|
+
const imp = (p.payload.metadata as Memory["metadata"])?.importance;
|
|
283
|
+
return imp !== undefined && imp < options.threshold;
|
|
284
|
+
})
|
|
285
|
+
.map((p) => p.id);
|
|
286
|
+
|
|
287
|
+
if (toDelete.length > 0) {
|
|
288
|
+
await qdrant.deletePoints(COLLECTION, toDelete);
|
|
289
|
+
deleted += toDelete.length;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
offset = nextPageOffset;
|
|
293
|
+
} while (offset !== null);
|
|
294
|
+
|
|
295
|
+
return { pruned: deleted };
|
|
372
296
|
}
|
|
373
297
|
|
|
374
298
|
export async function initMemoryCollection(): Promise<void> {
|
|
375
|
-
await qdrant.ensureCollection(COLLECTION, config.vector.dim
|
|
299
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim, [
|
|
300
|
+
"metadata.project",
|
|
301
|
+
"metadata.tags",
|
|
302
|
+
]);
|
|
376
303
|
}
|
|
377
304
|
|
|
378
305
|
export const memoryRepository = {
|
|
@@ -381,12 +308,9 @@ export const memoryRepository = {
|
|
|
381
308
|
delete: deleteMemories,
|
|
382
309
|
deleteByFilter: deleteMemoriesByFilter,
|
|
383
310
|
init: initMemoryCollection,
|
|
384
|
-
getById: getMemoryById,
|
|
385
311
|
update: updateMemory,
|
|
386
|
-
saveBatch: saveMemoryBatch,
|
|
387
|
-
searchBatch: searchMemoriesBatch,
|
|
388
312
|
scroll: scrollMemories,
|
|
389
313
|
stats: getMemoriesStats,
|
|
390
|
-
|
|
391
|
-
|
|
314
|
+
compact: compactMemories,
|
|
315
|
+
prune: pruneMemories,
|
|
392
316
|
};
|
|
@@ -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
|
+
}
|