Update memory plugin (#160)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-03-20 12:16:56 -04:00
1 parent be7e64b07f
commit 1248d0b9e0
50 files changed
+5363 -291

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
export { initializeDatabase, closeDatabase, resolveDataDir, resolveLogPath } from './database'
export { createVecService } from './vec'
export { createVecService, cleanupOrphanedWorkers } from './vec'
export type { VecService, VecSearchResult, TableDimensionsResult } from './vec-types'
export { createMemoryQuery } from './memory-queries'
export { createMetadataQuery } from './metadata-queries'
+26
View File
@@ -29,6 +29,9 @@ function mapRow(row: KvRowRaw): KvRow {
}
}
const CLEANUP_INTERVAL_MS = 300_000
let lastCleanupAt = 0
export function createKvQuery(db: Database) {
const getStmt = db.prepare(
`SELECT project_id, key, data, expires_at, created_at, updated_at
@@ -54,6 +57,13 @@ export function createKvQuery(db: Database) {
ORDER BY updated_at DESC`
)
const listByPrefixStmt = db.prepare(
`SELECT project_id, key, data, expires_at, created_at, updated_at
FROM project_kv
WHERE project_id = ? AND key LIKE ? AND expires_at > ?
ORDER BY updated_at DESC`
)
const deleteExpiredStmt = db.prepare(
`DELETE FROM project_kv WHERE expires_at < ?`
)
@@ -67,6 +77,17 @@ export function createKvQuery(db: Database) {
set(projectId: string, key: string, data: string, expiresAt: number): void {
const now = Date.now()
setStmt.run(projectId, key, data, expiresAt, now, now)
if (now - lastCleanupAt > CLEANUP_INTERVAL_MS) {
lastCleanupAt = now
setImmediate(() => {
try {
deleteExpiredStmt.run(now)
} catch {
// Ignore errors from cleanup (e.g., if db is closed)
}
})
}
},
delete(projectId: string, key: string): void {
@@ -78,6 +99,11 @@ export function createKvQuery(db: Database) {
return rows.map(mapRow)
},
listByPrefix(projectId: string, prefix: string): KvRow[] {
const rows = listByPrefixStmt.all(projectId, `${prefix}%`, Date.now()) as KvRowRaw[]
return rows.map(mapRow)
},
deleteExpired(): number {
const result = deleteExpiredStmt.run(Date.now())
return result.changes
+38 -4
View File
@@ -47,12 +47,13 @@ function sendRequest(socketPath: string, request: Record<string, unknown>): Prom
})
}
function isWorkerRunning(pidPath: string, socketPath: string): boolean {
async function isWorkerRunning(pidPath: string, socketPath: string): Promise<boolean> {
if (!existsSync(pidPath) || !existsSync(socketPath)) return false
try {
const pid = parseInt(readFileSync(pidPath, 'utf-8'), 10)
process.kill(pid, 0)
return true
const response = await sendRequest(socketPath, { action: 'health' })
return response.status === 'ok'
} catch {
return false
}
@@ -70,6 +71,33 @@ function cleanupStale(pidPath: string, socketPath: string): void {
} catch {}
}
export function cleanupOrphanedWorkers(pidPath: string, socketPath: string): void {
if (existsSync(socketPath) && !existsSync(pidPath)) {
try {
unlinkSync(socketPath)
} catch {}
}
if (existsSync(pidPath)) {
try {
const pid = parseInt(readFileSync(pidPath, 'utf-8'), 10)
try {
process.kill(pid, 0)
if (!existsSync(socketPath)) {
try {
process.kill(pid, 'SIGTERM')
} catch {}
}
} catch {
unlinkSync(pidPath)
if (existsSync(socketPath)) {
unlinkSync(socketPath)
}
}
} catch {}
}
}
async function startWorker(config: {
dbPath: string
dataDir: string
@@ -78,11 +106,17 @@ async function startWorker(config: {
const socketPath = join(config.dataDir, 'vec-worker.sock')
const pidPath = join(config.dataDir, 'vec-worker.pid')
if (isWorkerRunning(pidPath, socketPath)) {
cleanupOrphanedWorkers(pidPath, socketPath)
if (await isWorkerRunning(pidPath, socketPath)) {
return socketPath
}
cleanupStale(pidPath, socketPath)
if (existsSync(socketPath)) {
try {
unlinkSync(socketPath)
} catch {}
}
const workerScriptJs = join(__dirname, 'vec-worker.js')
const workerScript = existsSync(workerScriptJs)
+2
View File
@@ -2,8 +2,10 @@ import type { Database } from 'bun:sqlite'
import { join } from 'path'
import type { VecService } from './vec-types'
import type { Logger } from '../types'
import { cleanupOrphanedWorkers } from './vec-client'
export type { VecService, VecSearchResult } from './vec-types'
export { cleanupOrphanedWorkers }
export async function createVecService(_db: Database, dataDir: string, dimensions: number, logger?: Logger): Promise<VecService> {
try {