diff --git a/extensions/memory-lancedb/index.ts b/extensions/memory-lancedb/index.ts index c5fa7051361..12899e09ab9 100644 --- a/extensions/memory-lancedb/index.ts +++ b/extensions/memory-lancedb/index.ts @@ -256,6 +256,11 @@ class MemoryDB { await this.ensureInitialized(); return this.table!.countRows(); } + + async getTable(): Promise { + await this.ensureInitialized(); + return this.table!; + } } // ============================================================================ @@ -815,6 +820,74 @@ export default definePluginEntry({ console.log(JSON.stringify(output, null, 2)); }); + memory + .command("query") + .description("Query memories (non-vector search)") + .option("--cols ", "Columns to select, comma-separated") + .option("--filter ", "Filter condition") + .option("--limit ", "Limit number of results", "10") + .option("--order-by ", "Order by column and direction (e.g., createdAt:desc)") + .action(async (opts) => { + const table = await db.getTable(); + let query = table.query(); + let sortColAdded = false; + let sortColName: string | undefined; + if (opts.cols) { + const columns = (opts.cols as string).split(",").map((c: string) => c.trim()); + if (opts.orderBy) { + const [sortCol] = opts.orderBy.split(":"); + sortColName = sortCol; + if (!columns.includes(sortCol)) { + columns.push(sortCol); + sortColAdded = true; + } + } + query = query.select(columns); + } else { + query = query.select(["id", "text", "importance", "category", "createdAt"]); + } + if (opts.filter) { + const filterCondition = String(opts.filter); + if (filterCondition.length > 200) { + throw new Error("Filter condition exceeds maximum length of 200 characters"); + } + if (!/^[a-zA-Z0-9_\-\s='"> { + if (a[col] < b[col]) { + return -1 * direction; + } + if (a[col] > b[col]) { + return 1 * direction; + } + return 0; + }); + rows = rows.slice(0, limit); + if (sortColAdded && sortColName) { + for (const row of rows) { + delete row[sortColName]; + } + } + } + console.log(JSON.stringify(rows, null, 2)); + }); + memory .command("stats") .description("Show memory statistics")