@miniaxolotl/grove 0.2.4
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 +7 -0
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +27 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +580 -0
- package/dist/index.js.map +1 -0
- package/dist/repositories/entity.repository.d.ts +44 -0
- package/dist/repositories/entity.repository.d.ts.map +1 -0
- package/dist/repositories/entity.repository.js +203 -0
- package/dist/repositories/entity.repository.js.map +1 -0
- package/dist/repositories/memory.repository.d.ts +78 -0
- package/dist/repositories/memory.repository.d.ts.map +1 -0
- package/dist/repositories/memory.repository.js +283 -0
- package/dist/repositories/memory.repository.js.map +1 -0
- package/dist/repositories/relation.repository.d.ts +38 -0
- package/dist/repositories/relation.repository.d.ts.map +1 -0
- package/dist/repositories/relation.repository.js +113 -0
- package/dist/repositories/relation.repository.js.map +1 -0
- package/dist/services/embedding.d.ts +3 -0
- package/dist/services/embedding.d.ts.map +1 -0
- package/dist/services/embedding.js +107 -0
- package/dist/services/embedding.js.map +1 -0
- package/dist/services/qdrant.d.ts +35 -0
- package/dist/services/qdrant.d.ts.map +1 -0
- package/dist/services/qdrant.js +100 -0
- package/dist/services/qdrant.js.map +1 -0
- package/dist/services/reranking.d.ts +6 -0
- package/dist/services/reranking.d.ts.map +1 -0
- package/dist/services/reranking.js +24 -0
- package/dist/services/reranking.js.map +1 -0
- package/eslint.config.js +1 -0
- package/package.json +56 -0
- package/src/config.ts +27 -0
- package/src/index.ts +663 -0
- package/src/repositories/entity.repository.ts +265 -0
- package/src/repositories/memory.repository.ts +392 -0
- package/src/repositories/relation.repository.ts +151 -0
- package/src/services/embedding.ts +142 -0
- package/src/services/qdrant.ts +184 -0
- package/src/services/reranking.ts +40 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
import { config } from "../config.ts";
|
|
3
|
+
import { qdrant, type VectorPoint } from "../services/qdrant.ts";
|
|
4
|
+
|
|
5
|
+
const COLLECTION = `${config.collection.prefix}_entities`;
|
|
6
|
+
|
|
7
|
+
export interface Entity {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
entityType: string;
|
|
11
|
+
observations: string[];
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function createEntity(
|
|
16
|
+
name: string,
|
|
17
|
+
entityType: string,
|
|
18
|
+
observations: string[] = [],
|
|
19
|
+
metadata?: Record<string, unknown>,
|
|
20
|
+
): Promise<Entity> {
|
|
21
|
+
const existing = await getEntity(name);
|
|
22
|
+
if (existing) {
|
|
23
|
+
const mergedObservations = [
|
|
24
|
+
...existing.observations,
|
|
25
|
+
...observations.filter((o) => !existing.observations.includes(o)),
|
|
26
|
+
];
|
|
27
|
+
const mergedMetadata = { ...existing.metadata, ...metadata };
|
|
28
|
+
|
|
29
|
+
await qdrant.upsertPoints(COLLECTION, [
|
|
30
|
+
{
|
|
31
|
+
id: existing.id,
|
|
32
|
+
vector: Array(config.vector.dim).fill(0),
|
|
33
|
+
payload: {
|
|
34
|
+
id: existing.id,
|
|
35
|
+
name,
|
|
36
|
+
entityType,
|
|
37
|
+
observations: mergedObservations,
|
|
38
|
+
metadata: mergedMetadata,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
id: existing.id,
|
|
45
|
+
name,
|
|
46
|
+
entityType,
|
|
47
|
+
observations: mergedObservations,
|
|
48
|
+
metadata: mergedMetadata,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const id = randomUUID();
|
|
53
|
+
const point = {
|
|
54
|
+
id,
|
|
55
|
+
vector: Array(config.vector.dim).fill(0),
|
|
56
|
+
payload: { id, name, entityType, observations, metadata },
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
await qdrant.upsertPoints(COLLECTION, [point]);
|
|
60
|
+
|
|
61
|
+
return { id, name, entityType, observations, metadata };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function getEntity(name: string): Promise<Entity | null> {
|
|
65
|
+
const points = await qdrant.scrollPoints(COLLECTION, {
|
|
66
|
+
must: [{ key: "name", match: { value: name } }],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
if (points.length === 0) return null;
|
|
70
|
+
|
|
71
|
+
const p = points[0];
|
|
72
|
+
return {
|
|
73
|
+
id: p.id,
|
|
74
|
+
name: p.payload.name as string,
|
|
75
|
+
entityType: p.payload.entityType as string,
|
|
76
|
+
observations: (p.payload.observations as string[]) ?? [],
|
|
77
|
+
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function searchEntities(
|
|
82
|
+
query?: string,
|
|
83
|
+
entityType?: string,
|
|
84
|
+
limit: number = 10,
|
|
85
|
+
): Promise<Entity[]> {
|
|
86
|
+
const must: Record<string, unknown>[] = [];
|
|
87
|
+
if (query) must.push({ key: "name", match: { value: query } });
|
|
88
|
+
if (entityType)
|
|
89
|
+
must.push({ key: "entityType", match: { value: entityType } });
|
|
90
|
+
|
|
91
|
+
const filter = must.length > 0 ? { must } : undefined;
|
|
92
|
+
const points = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
93
|
+
|
|
94
|
+
return points.map((p) => ({
|
|
95
|
+
id: p.id,
|
|
96
|
+
name: p.payload.name as string,
|
|
97
|
+
entityType: p.payload.entityType as string,
|
|
98
|
+
observations: (p.payload.observations as string[]) ?? [],
|
|
99
|
+
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function addObservations(
|
|
104
|
+
name: string,
|
|
105
|
+
newObservations: string[],
|
|
106
|
+
): Promise<Entity | null> {
|
|
107
|
+
const entity = await getEntity(name);
|
|
108
|
+
if (!entity) return null;
|
|
109
|
+
|
|
110
|
+
const updated = {
|
|
111
|
+
...entity,
|
|
112
|
+
observations: [...entity.observations, ...newObservations],
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
await qdrant.upsertPoints(COLLECTION, [
|
|
116
|
+
{
|
|
117
|
+
id: entity.id,
|
|
118
|
+
vector: Array(config.vector.dim).fill(0),
|
|
119
|
+
payload: {
|
|
120
|
+
id: updated.id,
|
|
121
|
+
name: updated.name,
|
|
122
|
+
entityType: updated.entityType,
|
|
123
|
+
observations: updated.observations,
|
|
124
|
+
metadata: updated.metadata,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
return updated;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function deleteEntities(
|
|
133
|
+
filter: {
|
|
134
|
+
name?: string;
|
|
135
|
+
entityType?: string;
|
|
136
|
+
} = {},
|
|
137
|
+
): Promise<void> {
|
|
138
|
+
const must: Record<string, unknown>[] = [];
|
|
139
|
+
if (filter.name) must.push({ key: "name", match: { value: filter.name } });
|
|
140
|
+
if (filter.entityType)
|
|
141
|
+
must.push({ key: "entityType", match: { value: filter.entityType } });
|
|
142
|
+
if (must.length === 0) return;
|
|
143
|
+
|
|
144
|
+
await qdrant.deleteByFilter(COLLECTION, { must });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export async function getEntityById(id: string): Promise<Entity | null> {
|
|
148
|
+
const point = await qdrant.getPoint(COLLECTION, id);
|
|
149
|
+
if (!point) return null;
|
|
150
|
+
return {
|
|
151
|
+
id: point.id,
|
|
152
|
+
name: point.payload.name as string,
|
|
153
|
+
entityType: point.payload.entityType as string,
|
|
154
|
+
observations: (point.payload.observations as string[]) ?? [],
|
|
155
|
+
metadata: point.payload.metadata as Record<string, unknown> | undefined,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function updateEntity(
|
|
160
|
+
id: string,
|
|
161
|
+
updates: {
|
|
162
|
+
name?: string;
|
|
163
|
+
entityType?: string;
|
|
164
|
+
metadata?: Record<string, unknown>;
|
|
165
|
+
},
|
|
166
|
+
): Promise<Entity | null> {
|
|
167
|
+
const existing = await getEntityById(id);
|
|
168
|
+
if (!existing) return null;
|
|
169
|
+
|
|
170
|
+
const updated: Entity = {
|
|
171
|
+
...existing,
|
|
172
|
+
name: updates.name ?? existing.name,
|
|
173
|
+
entityType: updates.entityType ?? existing.entityType,
|
|
174
|
+
metadata: updates.metadata ?? existing.metadata,
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
await qdrant.upsertPoints(COLLECTION, [
|
|
178
|
+
{
|
|
179
|
+
id,
|
|
180
|
+
vector: Array(config.vector.dim).fill(0),
|
|
181
|
+
payload: {
|
|
182
|
+
id,
|
|
183
|
+
name: updated.name,
|
|
184
|
+
entityType: updated.entityType,
|
|
185
|
+
observations: updated.observations,
|
|
186
|
+
metadata: updated.metadata,
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
return updated;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export async function getEntityStats(): Promise<{
|
|
195
|
+
count: number;
|
|
196
|
+
vectorsCount: number;
|
|
197
|
+
indexedVectorsCount: number;
|
|
198
|
+
}> {
|
|
199
|
+
const info = await qdrant.getCollectionInfo(COLLECTION);
|
|
200
|
+
if (!info) return { count: 0, vectorsCount: 0, indexedVectorsCount: 0 };
|
|
201
|
+
return {
|
|
202
|
+
count: info.pointsCount,
|
|
203
|
+
vectorsCount: info.vectorsCount,
|
|
204
|
+
indexedVectorsCount: info.indexedVectorsCount,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export async function listEntities(
|
|
209
|
+
entityType?: string,
|
|
210
|
+
limit: number = 100,
|
|
211
|
+
offset?: string,
|
|
212
|
+
): Promise<{ entities: Entity[]; offset: string | null }> {
|
|
213
|
+
const must: Record<string, unknown>[] = [];
|
|
214
|
+
if (entityType)
|
|
215
|
+
must.push({ key: "entityType", match: { value: entityType } });
|
|
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
|
+
});
|
|
232
|
+
|
|
233
|
+
if (!response.ok) throw new Error(`scroll failed: ${response.status}`);
|
|
234
|
+
const data = (await response.json()) as {
|
|
235
|
+
result: { points: VectorPoint[]; next_page_offset: string | null };
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
entities: data.result.points.map((p) => ({
|
|
240
|
+
id: p.id,
|
|
241
|
+
name: p.payload.name as string,
|
|
242
|
+
entityType: p.payload.entityType as string,
|
|
243
|
+
observations: (p.payload.observations as string[]) ?? [],
|
|
244
|
+
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
245
|
+
})),
|
|
246
|
+
offset: data.result.next_page_offset ?? null,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export async function initEntityCollection(): Promise<void> {
|
|
251
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export const entityRepository = {
|
|
255
|
+
create: createEntity,
|
|
256
|
+
get: getEntity,
|
|
257
|
+
search: searchEntities,
|
|
258
|
+
addObservations,
|
|
259
|
+
delete: deleteEntities,
|
|
260
|
+
init: initEntityCollection,
|
|
261
|
+
getById: getEntityById,
|
|
262
|
+
update: updateEntity,
|
|
263
|
+
stats: getEntityStats,
|
|
264
|
+
list: listEntities,
|
|
265
|
+
};
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
import { config } from "../config.ts";
|
|
3
|
+
import { qdrant, type VectorPoint } from "../services/qdrant.ts";
|
|
4
|
+
|
|
5
|
+
const COLLECTION = `${config.collection.prefix}_memories`;
|
|
6
|
+
|
|
7
|
+
export interface Memory {
|
|
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
|
+
}
|
|
22
|
+
|
|
23
|
+
let _embedTexts: ((texts: string[]) => Promise<number[][]>) | null = null;
|
|
24
|
+
|
|
25
|
+
async function getEmbedTexts(): Promise<
|
|
26
|
+
((texts: string[]) => Promise<number[][]>) | null
|
|
27
|
+
> {
|
|
28
|
+
if (_embedTexts === null) {
|
|
29
|
+
try {
|
|
30
|
+
const { embedTexts } = await import("../services/embedding.ts");
|
|
31
|
+
_embedTexts = embedTexts;
|
|
32
|
+
} catch {
|
|
33
|
+
_embedTexts = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return _embedTexts;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function saveMemory(
|
|
40
|
+
text: string,
|
|
41
|
+
metadata: Partial<Memory["metadata"]> = {},
|
|
42
|
+
): Promise<Memory> {
|
|
43
|
+
const id = randomUUID();
|
|
44
|
+
const embedFn = await getEmbedTexts();
|
|
45
|
+
|
|
46
|
+
let vector: number[];
|
|
47
|
+
if (embedFn) {
|
|
48
|
+
[vector] = await embedFn([text]);
|
|
49
|
+
} else {
|
|
50
|
+
vector = Array(config.vector.dim).fill(0);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const point: VectorPoint = {
|
|
54
|
+
id,
|
|
55
|
+
vector,
|
|
56
|
+
payload: {
|
|
57
|
+
id,
|
|
58
|
+
text,
|
|
59
|
+
metadata: {
|
|
60
|
+
...metadata,
|
|
61
|
+
createdAt: metadata.createdAt ?? new Date().toISOString(),
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
await qdrant.upsertPoints(COLLECTION, [point]);
|
|
67
|
+
|
|
68
|
+
return { id, text, metadata: point.payload.metadata as Memory["metadata"] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function searchMemories(
|
|
72
|
+
query: string,
|
|
73
|
+
options: {
|
|
74
|
+
project?: string;
|
|
75
|
+
tags?: string[];
|
|
76
|
+
limit?: number;
|
|
77
|
+
} = {},
|
|
78
|
+
): Promise<MemorySearchResult[]> {
|
|
79
|
+
const limit = options.limit ?? 5;
|
|
80
|
+
const embedFn = await getEmbedTexts();
|
|
81
|
+
|
|
82
|
+
if (!embedFn) {
|
|
83
|
+
const must: Record<string, unknown>[] = [];
|
|
84
|
+
if (options.project)
|
|
85
|
+
must.push({ key: "metadata.project", match: { value: options.project } });
|
|
86
|
+
if (options.tags && options.tags.length > 0) {
|
|
87
|
+
must.push({ key: "metadata.tags", match: { any: options.tags } });
|
|
88
|
+
}
|
|
89
|
+
const filter = must.length > 0 ? { must } : undefined;
|
|
90
|
+
const points = await qdrant.scrollPoints(COLLECTION, filter, limit);
|
|
91
|
+
return points.map((r) => ({
|
|
92
|
+
id: r.id,
|
|
93
|
+
text: r.payload.text as string,
|
|
94
|
+
metadata: r.payload.metadata as Memory["metadata"],
|
|
95
|
+
score: 0,
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const [queryVector] = await embedFn([query]);
|
|
100
|
+
|
|
101
|
+
const must: Record<string, unknown>[] = [];
|
|
102
|
+
if (options.project)
|
|
103
|
+
must.push({ key: "metadata.project", match: { value: options.project } });
|
|
104
|
+
if (options.tags && options.tags.length > 0) {
|
|
105
|
+
must.push({ key: "metadata.tags", match: { any: options.tags } });
|
|
106
|
+
}
|
|
107
|
+
const filter = must.length > 0 ? { must } : undefined;
|
|
108
|
+
|
|
109
|
+
const results = await qdrant.searchVectors(
|
|
110
|
+
COLLECTION,
|
|
111
|
+
queryVector,
|
|
112
|
+
limit,
|
|
113
|
+
filter,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return results.map((r) => ({
|
|
117
|
+
id: r.id,
|
|
118
|
+
text: r.payload.text as string,
|
|
119
|
+
metadata: r.payload.metadata as Memory["metadata"],
|
|
120
|
+
score: r.score ?? 0,
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function deleteMemories(ids: string[]): Promise<void> {
|
|
125
|
+
await qdrant.deletePoints(COLLECTION, ids);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function deleteMemoriesByFilter(filter: {
|
|
129
|
+
project?: string;
|
|
130
|
+
tags?: string[];
|
|
131
|
+
}): Promise<void> {
|
|
132
|
+
const must: Record<string, unknown>[] = [];
|
|
133
|
+
if (filter.project)
|
|
134
|
+
must.push({ key: "metadata.project", match: { value: filter.project } });
|
|
135
|
+
if (filter.tags && filter.tags.length > 0) {
|
|
136
|
+
must.push({ key: "metadata.tags", match: { any: filter.tags } });
|
|
137
|
+
}
|
|
138
|
+
if (must.length === 0) return;
|
|
139
|
+
|
|
140
|
+
await qdrant.deleteByFilter(COLLECTION, { must });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function getMemoryById(id: string): Promise<Memory | null> {
|
|
144
|
+
const point = await qdrant.getPoint(COLLECTION, id);
|
|
145
|
+
if (!point) return null;
|
|
146
|
+
return {
|
|
147
|
+
id: point.id,
|
|
148
|
+
text: point.payload.text as string,
|
|
149
|
+
metadata: point.payload.metadata as Memory["metadata"],
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export async function updateMemory(
|
|
154
|
+
id: string,
|
|
155
|
+
updates: { text?: string; metadata?: Partial<Memory["metadata"]> },
|
|
156
|
+
): Promise<Memory | null> {
|
|
157
|
+
const existing = await getMemoryById(id);
|
|
158
|
+
if (!existing) return null;
|
|
159
|
+
|
|
160
|
+
const updated: Memory = {
|
|
161
|
+
...existing,
|
|
162
|
+
text: updates.text ?? existing.text,
|
|
163
|
+
metadata: {
|
|
164
|
+
...existing.metadata,
|
|
165
|
+
...updates.metadata,
|
|
166
|
+
updatedAt: new Date().toISOString(),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
let vector: number[];
|
|
171
|
+
if (updates.text) {
|
|
172
|
+
const embedFn = await getEmbedTexts();
|
|
173
|
+
if (embedFn) {
|
|
174
|
+
[vector] = await embedFn([updated.text]);
|
|
175
|
+
} else {
|
|
176
|
+
vector = Array(config.vector.dim).fill(0);
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
const point = await qdrant.getPoint(COLLECTION, id);
|
|
180
|
+
vector = point?.vector ?? Array(config.vector.dim).fill(0);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
await qdrant.upsertPoints(COLLECTION, [
|
|
184
|
+
{
|
|
185
|
+
id,
|
|
186
|
+
vector,
|
|
187
|
+
payload: { id, text: updated.text, metadata: updated.metadata },
|
|
188
|
+
},
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
return updated;
|
|
192
|
+
}
|
|
193
|
+
|
|
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
|
+
export async function scrollMemories(
|
|
285
|
+
options: {
|
|
286
|
+
project?: string;
|
|
287
|
+
tags?: string[];
|
|
288
|
+
limit?: number;
|
|
289
|
+
offset?: string;
|
|
290
|
+
} = {},
|
|
291
|
+
): Promise<{ memories: Memory[]; offset: string | null }> {
|
|
292
|
+
const limit = options.limit ?? 100;
|
|
293
|
+
const must: Record<string, unknown>[] = [];
|
|
294
|
+
if (options.project)
|
|
295
|
+
must.push({ key: "metadata.project", match: { value: options.project } });
|
|
296
|
+
if (options.tags && options.tags.length > 0) {
|
|
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
|
+
});
|
|
315
|
+
|
|
316
|
+
if (!response.ok) throw new Error(`scroll failed: ${response.status}`);
|
|
317
|
+
const scrollData = (await response.json()) as {
|
|
318
|
+
result: { points: VectorPoint[]; next_page_offset: string | null };
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const memories: Memory[] = scrollData.result.points.map((p) => ({
|
|
322
|
+
id: p.id,
|
|
323
|
+
text: p.payload.text as string,
|
|
324
|
+
metadata: p.payload.metadata as Memory["metadata"],
|
|
325
|
+
}));
|
|
326
|
+
|
|
327
|
+
return {
|
|
328
|
+
memories,
|
|
329
|
+
offset: scrollData.result.next_page_offset ?? null,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export async function getMemoriesStats(): Promise<{
|
|
334
|
+
count: number;
|
|
335
|
+
vectorsCount: number;
|
|
336
|
+
indexedVectorsCount: number;
|
|
337
|
+
}> {
|
|
338
|
+
const info = await qdrant.getCollectionInfo(COLLECTION);
|
|
339
|
+
if (!info) return { count: 0, vectorsCount: 0, indexedVectorsCount: 0 };
|
|
340
|
+
return {
|
|
341
|
+
count: info.pointsCount,
|
|
342
|
+
vectorsCount: info.vectorsCount,
|
|
343
|
+
indexedVectorsCount: info.indexedVectorsCount,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export async function exportMemories(): Promise<Memory[]> {
|
|
348
|
+
let offset: string | null = null;
|
|
349
|
+
const allMemories: Memory[] = [];
|
|
350
|
+
|
|
351
|
+
do {
|
|
352
|
+
const { memories, offset: nextOffset } = await scrollMemories({
|
|
353
|
+
limit: 1000,
|
|
354
|
+
offset: offset ?? undefined,
|
|
355
|
+
});
|
|
356
|
+
allMemories.push(...memories);
|
|
357
|
+
offset = nextOffset;
|
|
358
|
+
} while (offset !== null);
|
|
359
|
+
|
|
360
|
+
return allMemories;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export async function importMemories(
|
|
364
|
+
items: Array<{
|
|
365
|
+
text: string;
|
|
366
|
+
metadata?: Partial<Memory["metadata"]>;
|
|
367
|
+
}>,
|
|
368
|
+
): Promise<{ imported: number }> {
|
|
369
|
+
if (items.length === 0) return { imported: 0 };
|
|
370
|
+
const saved = await saveMemoryBatch(items);
|
|
371
|
+
return { imported: saved.length };
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export async function initMemoryCollection(): Promise<void> {
|
|
375
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export const memoryRepository = {
|
|
379
|
+
save: saveMemory,
|
|
380
|
+
search: searchMemories,
|
|
381
|
+
delete: deleteMemories,
|
|
382
|
+
deleteByFilter: deleteMemoriesByFilter,
|
|
383
|
+
init: initMemoryCollection,
|
|
384
|
+
getById: getMemoryById,
|
|
385
|
+
update: updateMemory,
|
|
386
|
+
saveBatch: saveMemoryBatch,
|
|
387
|
+
searchBatch: searchMemoriesBatch,
|
|
388
|
+
scroll: scrollMemories,
|
|
389
|
+
stats: getMemoriesStats,
|
|
390
|
+
export: exportMemories,
|
|
391
|
+
import: importMemories,
|
|
392
|
+
};
|