-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector-memory-server.js
More file actions
331 lines (288 loc) · 10.4 KB
/
vector-memory-server.js
File metadata and controls
331 lines (288 loc) · 10.4 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
import Database from "better-sqlite3";
import * as sqliteVec from "sqlite-vec";
import { Worker } from "worker_threads";
import { join, dirname } from "path";
import { homedir } from "os";
import { existsSync, writeFileSync, unlinkSync, readFileSync } from "fs";
import { fileURLToPath } from "url";
import { createServer, request as httpReq } from "http";
import { execSync } from "child_process";
import { userInfo } from "os";
import { filterUnindexed, dedup, postProcessResults, isOurServer, isIndexable, DIMS, createHandler, userPort } from "./lib.js";
import { createEmbedPool } from "./embed-pool.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf-8"));
const SERVER_USER = userInfo().username;
const COPILOT_DIR = process.env.VECTOR_MEMORY_DATA_DIR || join(homedir(), ".copilot");
const SESSION_STORE_PATH = join(COPILOT_DIR, "session-store.db");
const VECTOR_INDEX_PATH = join(COPILOT_DIR, "vector-index.db");
const INDEX_INTERVAL_MS = 15 * 60 * 1000;
// Idle timeout: minutes. 0 or negative = disabled. Default 5 min.
const IDLE_TIMEOUT_MINUTES = parseFloat(process.env.VECTOR_MEMORY_IDLE_TIMEOUT || "5");
const IDLE_TIMEOUT_MS = IDLE_TIMEOUT_MINUTES > 0 ? IDLE_TIMEOUT_MINUTES * 60_000 : 0;
const IDLE_CHECK_MS = 60_000; // check every 60s
let isIndexing = false;
const pool = createEmbedPool(() => new Worker(join(__dirname, "embed-worker.js")));
function openVectorDb() {
const db = new Database(VECTOR_INDEX_PATH);
sqliteVec.load(db);
db.pragma("journal_mode = WAL");
db.pragma("busy_timeout = 5000");
db.exec(`
CREATE TABLE IF NOT EXISTS indexed_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
source_type TEXT NOT NULL,
content TEXT NOT NULL,
source_id TEXT,
UNIQUE(session_id, source_type, source_id)
);
CREATE INDEX IF NOT EXISTS idx_indexed_session ON indexed_items(session_id);
`);
const hasVec = db
.prepare("SELECT count(*) as c FROM sqlite_master WHERE type='table' AND name='vec_items'")
.get().c;
if (!hasVec) {
db.exec(`CREATE VIRTUAL TABLE vec_items USING vec0(rowid INTEGER PRIMARY KEY, embedding float[${DIMS}])`);
}
return db;
}
function openSessionStore() {
if (!existsSync(SESSION_STORE_PATH)) return null;
return new Database(SESSION_STORE_PATH, { readonly: true });
}
function runMaintenance(db) {
try {
db.pragma("wal_checkpoint(TRUNCATE)");
db.exec("ANALYZE");
} catch {}
}
function getUnindexedContent(vecDb, sessionDb) {
const allContent = sessionDb
.prepare("SELECT rowid, content, session_id, source_type, source_id FROM search_index")
.all();
const existing = vecDb.prepare("SELECT session_id, source_type, source_id FROM indexed_items").all();
return filterUnindexed(allContent, existing);
}
async function indexContent(vecDb, items) {
const insertMeta = vecDb.prepare(
"INSERT OR IGNORE INTO indexed_items (session_id, source_type, content, source_id) VALUES (?, ?, ?, ?)"
);
const insertVec = vecDb.prepare("INSERT INTO vec_items (rowid, embedding) VALUES (?, ?)");
let count = 0;
for (const item of items) {
if (!isIndexable(item)) continue;
const embedding = await pool.embed(item.content);
const result = insertMeta.run(item.session_id, item.source_type, item.content, item.source_id ?? null);
if (result.changes > 0) {
insertVec.run(BigInt(result.lastInsertRowid), embedding);
count++;
}
}
return count;
}
async function backgroundIndex() {
if (isIndexing) return;
isIndexing = true;
try {
const sessionDb = openSessionStore();
if (!sessionDb) return;
const vecDb = openVectorDb();
try {
const unindexed = getUnindexedContent(vecDb, sessionDb);
sessionDb.close();
if (unindexed.length > 0) {
await indexContent(vecDb, unindexed);
lastActivity = Date.now(); // new content = someone's using copilot
}
} finally {
vecDb.close();
}
} catch {
// Silently handle errors in background indexing
} finally {
isIndexing = false;
}
}
async function search(vecDb, query, limit = 10) {
const queryEmbedding = await pool.embed(query);
const results = vecDb
.prepare(
`SELECT v.rowid, v.distance, i.session_id, i.source_type, i.content
FROM vec_items v
JOIN indexed_items i ON i.id = v.rowid
WHERE v.embedding MATCH ? AND k = ?
ORDER BY v.distance`
)
.all(queryEmbedding, limit * 3);
const unique = dedup(results);
return postProcessResults(unique, limit);
}
// --- Startup (heavy init deferred until after singleton check) ---
// --- HTTP Server (singleton, per-user-alias port via userPort()) ---
const PORT = parseInt(process.env.VECTOR_MEMORY_PORT || String(userPort(SERVER_USER)), 10);
const handleRequest = createHandler({
openVectorDb,
openSessionStore,
getUnindexedContent,
indexContent,
search,
runMaintenance,
getIsIndexing: () => isIndexing,
setIsIndexing: (v) => { isIndexing = v; },
getIdentity: () => ({ user: SERVER_USER, version: PKG.version }),
});
let lastActivity = Date.now();
// Wrap handler to track activity on every request
function trackedHandler(req, res) {
lastActivity = Date.now();
return handleRequest(req, res);
}
// --- Port conflict resolution ---
function getPortOwnerPid() {
try {
const out = execSync(`netstat -ano`, { encoding: "utf-8", windowsHide: true });
for (const line of out.split("\n")) {
if (line.includes(`:${PORT}`) && line.includes("LISTENING")) {
const pid = parseInt(line.trim().split(/\s+/).pop());
if (!isNaN(pid) && pid > 0) return pid;
}
}
} catch {}
return null;
}
function getProcessInfo(pid) {
try {
const out = execSync(
`powershell -NoProfile -Command "(Get-CimInstance Win32_Process -Filter 'ProcessId=${pid}' | Select-Object Name,CommandLine | ConvertTo-Json -Compress)"`,
{ encoding: "utf-8", windowsHide: true, timeout: 5000 }
);
return JSON.parse(out.trim());
} catch {}
return null;
}
// More reliable than process name: check if the server speaks our protocol
async function isOurProtocol() {
const result = await httpPost("/ping", {}, 3000);
return result?.ok === true;
}
function httpPost(path, body, timeoutMs = 10000) {
return new Promise((resolve) => {
const data = JSON.stringify(body);
const req = httpReq(`http://127.0.0.1:${PORT}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(data) },
timeout: timeoutMs,
}, (res) => {
const chunks = [];
res.on("data", (c) => chunks.push(c));
res.on("end", () => {
try { resolve(JSON.parse(Buffer.concat(chunks).toString())); }
catch { resolve(null); }
});
});
req.on("error", () => resolve(null));
req.on("timeout", () => { req.destroy(); resolve(null); });
req.end(data);
});
}
function tryListen(server) {
return new Promise((resolve, reject) => {
const onError = (err) => { server.removeListener("listening", onOk); reject(err); };
const onOk = () => { server.removeListener("error", onError); resolve(); };
server.once("error", onError);
server.once("listening", onOk);
server.listen(PORT, "127.0.0.1");
});
}
function killPid(pid) {
try { process.kill(pid, "SIGTERM"); } catch {}
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
// --- Startup ---
const httpServer = createServer(trackedHandler);
try {
await tryListen(httpServer);
// We're the singleton
const pidFile = join(COPILOT_DIR, "vector-memory.pid");
writeFileSync(pidFile, process.pid.toString());
} catch (err) {
if (err.code !== "EADDRINUSE") throw err;
// Port conflict — investigate
const ownerPid = getPortOwnerPid();
const info = ownerPid ? getProcessInfo(ownerPid) : null;
// First check: does whatever's on the port speak our protocol?
if (await isOurProtocol()) {
// It's a vector-memory server — deep health check with actual search
const searchResult = await httpPost("/search", { query: "health check", limit: 1 }, 15000);
if (Array.isArray(searchResult) || (searchResult && searchResult.error == null)) {
// Singleton is alive and functional — no worker was started, just exit
process.exit(0);
}
// Responds to ping but search is broken/hung — zombie
process.stderr.write(
`[vector-memory] Existing server (PID: ${ownerPid}) responds to ping but search is unresponsive. Taking over.\n`
);
} else if (isOurServer(info)) {
// Process looks like ours but doesn't respond to ping — dead zombie
process.stderr.write(
`[vector-memory] Existing server (PID: ${ownerPid}) is not responding. Taking over.\n`
);
} else {
// Foreign process — report and bail (no worker was started)
const name = info?.Name ?? "unknown";
const cmd = info?.CommandLine ?? "(no command line)";
process.stderr.write(
`[vector-memory] FATAL: Port ${PORT} already in use by ${name} (PID: ${ownerPid ?? "unknown"})\n` +
` Command: ${cmd}\n` +
` Cannot start vector-memory server. Free the port or change PORT.\n`
);
process.exit(1);
}
killPid(ownerPid);
await sleep(3000);
// Retry
try {
const retryServer = createServer(trackedHandler);
await tryListen(retryServer);
const pidFile = join(COPILOT_DIR, "vector-memory.pid");
writeFileSync(pidFile, process.pid.toString());
retryServer.on("error", (e) => { throw e; });
} catch (retryErr) {
process.stderr.write(
`[vector-memory] FATAL: Failed to bind port ${PORT} even after killing PID ${ownerPid}.\n` +
` ${retryErr.message}\n`
);
process.exit(1);
}
}
// --- We won the singleton race — now do the heavy init ---
pool.initWorker();
{
const vecDb = openVectorDb();
runMaintenance(vecDb);
vecDb.close();
}
backgroundIndex();
setInterval(backgroundIndex, INDEX_INTERVAL_MS);
// --- Idle shutdown ---
if (IDLE_TIMEOUT_MS > 0) {
setInterval(() => {
const idle = Date.now() - lastActivity;
if (idle >= IDLE_TIMEOUT_MS) {
process.stderr.write(`[vector-memory] Idle for ${Math.round(idle / 1000)}s — shutting down.\n`);
cleanup();
}
}, IDLE_CHECK_MS);
}
// Cleanup on exit
function cleanup() {
try {
const pidFile = join(COPILOT_DIR, "vector-memory.pid");
if (existsSync(pidFile)) unlinkSync(pidFile);
} catch {}
pool.shutdown();
process.exit(0);
}
process.on("SIGTERM", cleanup);
process.on("SIGINT", cleanup);