-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathvector-handler.ts
More file actions
345 lines (298 loc) · 10.2 KB
/
Copy pathvector-handler.ts
File metadata and controls
345 lines (298 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import type * as http from "node:http";
import type {
VectorCollection,
VectorEntry,
VectorQuery,
QueryResult,
QueryHandler,
} from "./vector-types.js";
export interface VectorState {
collections: Map<string, VectorCollection>;
queryHandlers: Map<string, QueryHandler>;
}
interface RouteResult {
handled: boolean;
}
function jsonResponse(res: http.ServerResponse, status: number, body: unknown): void {
const payload = JSON.stringify(body);
res.writeHead(status, {
"Content-Type": "application/json",
"Content-Length": String(Buffer.byteLength(payload)),
});
res.end(payload);
}
function resolveQuery(
state: VectorState,
collectionName: string,
query: VectorQuery,
): QueryResult[] {
const handler = state.queryHandlers.get(collectionName);
if (!handler) return [];
if (typeof handler === "function") return handler(query);
return handler;
}
// ---- Pinecone-compatible endpoints ----
function handlePinecone(
state: VectorState,
req: http.IncomingMessage,
res: http.ServerResponse,
pathname: string,
body: Record<string, unknown>,
): RouteResult {
// POST /query
if (req.method === "POST" && pathname === "/query") {
const namespace = (body.namespace as string) ?? "default";
const collection = state.collections.get(namespace);
if (!collection) {
jsonResponse(res, 404, { error: { message: `Collection '${namespace}' not found` } });
return { handled: true };
}
const query: VectorQuery = {
vector: body.vector as number[] | undefined,
topK: body.topK as number | undefined,
filter: body.filter,
collection: namespace,
};
const results = resolveQuery(state, namespace, query);
const topK = query.topK ?? 10;
const matches = results.slice(0, topK).map((r) => ({
id: r.id,
score: r.score,
...(r.metadata !== undefined && { metadata: r.metadata }),
}));
jsonResponse(res, 200, { matches });
return { handled: true };
}
// POST /vectors/upsert
if (req.method === "POST" && pathname === "/vectors/upsert") {
const vectors = (body.vectors ?? []) as Array<{
id: string;
values: number[];
metadata?: Record<string, unknown>;
}>;
const namespace = (body.namespace as string) ?? "default";
let collection = state.collections.get(namespace);
if (!collection) {
const dim = vectors.length > 0 ? vectors[0].values.length : 0;
collection = { name: namespace, dimension: dim, vectors: new Map() };
state.collections.set(namespace, collection);
}
for (const v of vectors) {
const entry: VectorEntry = { id: v.id, values: v.values, metadata: v.metadata };
collection.vectors.set(v.id, entry);
}
jsonResponse(res, 200, { upsertedCount: vectors.length });
return { handled: true };
}
// POST /vectors/delete
if (req.method === "POST" && pathname === "/vectors/delete") {
const ids = (body.ids ?? []) as string[];
const namespace = (body.namespace as string) ?? "default";
const collection = state.collections.get(namespace);
if (collection) {
for (const id of ids) {
collection.vectors.delete(id);
}
}
jsonResponse(res, 200, {});
return { handled: true };
}
// GET /describe-index-stats
if (req.method === "GET" && pathname === "/describe-index-stats") {
let totalVectorCount = 0;
let dimension = 0;
for (const col of state.collections.values()) {
totalVectorCount += col.vectors.size;
if (col.dimension > 0) dimension = col.dimension;
}
jsonResponse(res, 200, { dimension, totalVectorCount });
return { handled: true };
}
return { handled: false };
}
// ---- Qdrant-compatible endpoints ----
const QDRANT_SEARCH_RE = /^\/collections\/([^/]+)\/points\/search$/;
const QDRANT_UPSERT_RE = /^\/collections\/([^/]+)\/points$/;
const QDRANT_DELETE_RE = /^\/collections\/([^/]+)\/points\/delete$/;
function handleQdrant(
state: VectorState,
req: http.IncomingMessage,
res: http.ServerResponse,
pathname: string,
body: Record<string, unknown>,
): RouteResult {
// POST /collections/{name}/points/search
let match = pathname.match(QDRANT_SEARCH_RE);
if (match && req.method === "POST") {
const name = decodeURIComponent(match[1]);
const collection = state.collections.get(name);
if (!collection) {
jsonResponse(res, 404, { status: { error: `Collection '${name}' not found` } });
return { handled: true };
}
const query: VectorQuery = {
vector: body.vector as number[] | undefined,
topK: body.limit as number | undefined,
filter: body.filter,
collection: name,
};
const results = resolveQuery(state, name, query);
const limit = (body.limit as number) ?? 10;
const result = results.slice(0, limit).map((r) => ({
id: r.id,
score: r.score,
...(r.metadata !== undefined && { payload: r.metadata }),
}));
jsonResponse(res, 200, { result });
return { handled: true };
}
// PUT /collections/{name}/points
match = pathname.match(QDRANT_UPSERT_RE);
if (match && req.method === "PUT") {
const name = decodeURIComponent(match[1]);
let collection = state.collections.get(name);
const points = (body.points ?? []) as Array<{
id: string;
vector: number[];
payload?: Record<string, unknown>;
}>;
if (!collection) {
const dim = points.length > 0 ? points[0].vector.length : 0;
collection = { name, dimension: dim, vectors: new Map() };
state.collections.set(name, collection);
}
for (const p of points) {
const entry: VectorEntry = { id: String(p.id), values: p.vector, metadata: p.payload };
collection.vectors.set(String(p.id), entry);
}
jsonResponse(res, 200, { status: "ok" });
return { handled: true };
}
// POST /collections/{name}/points/delete
match = pathname.match(QDRANT_DELETE_RE);
if (match && req.method === "POST") {
const name = decodeURIComponent(match[1]);
const collection = state.collections.get(name);
const points = (body.points ?? []) as string[];
if (collection) {
for (const id of points) {
collection.vectors.delete(String(id));
}
}
jsonResponse(res, 200, { status: "ok" });
return { handled: true };
}
return { handled: false };
}
// ---- ChromaDB-compatible endpoints ----
const CHROMA_QUERY_RE = /^\/api\/v1\/collections\/([^/]+)\/query$/;
const CHROMA_ADD_RE = /^\/api\/v1\/collections\/([^/]+)\/add$/;
const CHROMA_COLLECTION_RE = /^\/api\/v1\/collections\/([^/]+)$/;
const CHROMA_COLLECTIONS = "/api/v1/collections";
function handleChromaDB(
state: VectorState,
req: http.IncomingMessage,
res: http.ServerResponse,
pathname: string,
body: Record<string, unknown>,
): RouteResult {
// POST /api/v1/collections/{id}/query
let match = pathname.match(CHROMA_QUERY_RE);
if (match && req.method === "POST") {
const name = decodeURIComponent(match[1]);
const collection = state.collections.get(name);
if (!collection) {
jsonResponse(res, 404, { error: `Collection '${name}' not found` });
return { handled: true };
}
const queryEmbeddings = (body.query_embeddings ?? []) as number[][];
const nResults = (body.n_results as number) ?? 10;
// Process each query embedding
const allIds: string[][] = [];
const allDistances: number[][] = [];
const allMetadatas: Array<Array<Record<string, unknown> | null>> = [];
for (const embedding of queryEmbeddings) {
const query: VectorQuery = {
vector: embedding,
topK: nResults,
filter: body.where,
collection: name,
};
const results = resolveQuery(state, name, query).slice(0, nResults);
allIds.push(results.map((r) => r.id));
allDistances.push(results.map((r) => r.score));
allMetadatas.push(results.map((r) => r.metadata ?? null));
}
jsonResponse(res, 200, {
ids: allIds,
distances: allDistances,
metadatas: allMetadatas,
});
return { handled: true };
}
// POST /api/v1/collections/{id}/add
match = pathname.match(CHROMA_ADD_RE);
if (match && req.method === "POST") {
const name = decodeURIComponent(match[1]);
let collection = state.collections.get(name);
const ids = (body.ids ?? []) as string[];
const embeddings = (body.embeddings ?? []) as number[][];
const metadatas = (body.metadatas ?? []) as Array<Record<string, unknown> | undefined>;
if (!collection) {
const dim = embeddings.length > 0 ? embeddings[0].length : 0;
collection = { name, dimension: dim, vectors: new Map() };
state.collections.set(name, collection);
}
for (let i = 0; i < ids.length; i++) {
const entry: VectorEntry = {
id: ids[i],
values: embeddings[i] ?? [],
metadata: metadatas[i],
};
collection.vectors.set(ids[i], entry);
}
jsonResponse(res, 200, true);
return { handled: true };
}
// GET /api/v1/collections — list collections
if (req.method === "GET" && pathname === CHROMA_COLLECTIONS) {
const collections = Array.from(state.collections.values()).map((c) => ({
id: c.name,
name: c.name,
metadata: null,
}));
jsonResponse(res, 200, collections);
return { handled: true };
}
// DELETE /api/v1/collections/{id}
match = pathname.match(CHROMA_COLLECTION_RE);
if (match && req.method === "DELETE") {
const name = decodeURIComponent(match[1]);
if (!state.collections.has(name)) {
jsonResponse(res, 404, { error: `Collection '${name}' not found` });
return { handled: true };
}
state.collections.delete(name);
state.queryHandlers.delete(name);
jsonResponse(res, 200, { status: "ok" });
return { handled: true };
}
return { handled: false };
}
// ---- Main dispatch ----
export function createVectorRequestHandler(state: VectorState) {
return (
req: http.IncomingMessage,
res: http.ServerResponse,
pathname: string,
body: Record<string, unknown>,
): boolean => {
const pinecone = handlePinecone(state, req, res, pathname, body);
if (pinecone.handled) return true;
const qdrant = handleQdrant(state, req, res, pathname, body);
if (qdrant.handled) return true;
const chroma = handleChromaDB(state, req, res, pathname, body);
if (chroma.handled) return true;
return false;
};
}