Skip to content

Commit 12ff8aa

Browse files
fix: guard embedding sync and queries against missing vec table
1 parent 9687da9 commit 12ff8aa

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

packages/memory/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ export function createMemoryPlugin(config: PluginConfig): Plugin {
270270
const initPromise = createVecService(db, dataDir, dimensions)
271271
.then(async (vec) => {
272272
memoryService.setVecService(vec)
273+
274+
if (!vec.available) {
275+
logger.log('Vec service unavailable, skipping embedding sync')
276+
return
277+
}
278+
273279
logger.log('Vec service initialized')
274280

275281
const embeddingSync = createEmbeddingSyncService(memoryService, logger)

packages/memory/src/storage/memory-queries.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,31 @@ export function createMemoryQuery(db: Database, vec: VecService, logger?: Logger
316316
projectId?: string,
317317
limit: number = 50
318318
): Memory[] {
319+
const embeddingsTableExists = db.prepare(
320+
"SELECT name FROM sqlite_master WHERE type='table' AND name='memory_embeddings'"
321+
).get()
322+
323+
if (!embeddingsTableExists) {
324+
const conditions: string[] = []
325+
const params: (string | number)[] = []
326+
327+
if (projectId) {
328+
conditions.push('project_id = ?')
329+
params.push(projectId)
330+
}
331+
332+
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
333+
const rows = db.prepare(`
334+
SELECT id, project_id, scope, content, file_path, access_count, last_accessed_at, created_at, updated_at
335+
FROM memories
336+
${where}
337+
ORDER BY created_at ASC
338+
LIMIT ? OFFSET 0
339+
`).all(...params, limit) as MemoryRow[]
340+
341+
return rows.map(mapRow)
342+
}
343+
319344
const conditions: string[] = ['e.memory_id IS NULL']
320345
const params: (string | number)[] = []
321346

@@ -337,6 +362,27 @@ export function createMemoryQuery(db: Database, vec: VecService, logger?: Logger
337362
},
338363

339364
countMemoriesWithoutEmbeddings(projectId?: string): number {
365+
const embeddingsTableExists = db.prepare(
366+
"SELECT name FROM sqlite_master WHERE type='table' AND name='memory_embeddings'"
367+
).get()
368+
369+
if (!embeddingsTableExists) {
370+
const conditions: string[] = []
371+
const params: (string | number)[] = []
372+
373+
if (projectId) {
374+
conditions.push('project_id = ?')
375+
params.push(projectId)
376+
}
377+
378+
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''
379+
const result = db.prepare(`
380+
SELECT COUNT(*) as count FROM memories ${where}
381+
`).get(...params) as { count: number }
382+
383+
return result.count
384+
}
385+
340386
const conditions: string[] = ['e.memory_id IS NULL']
341387
const params: (string | number)[] = []
342388

0 commit comments

Comments
 (0)