claude-memory-layer 1.0.34 → 1.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3578,6 +3578,16 @@ var SQLiteEventStore = class {
3578
3578
  getDatabase() {
3579
3579
  return this.db;
3580
3580
  }
3581
+ hasTableColumn(tableName, columnName) {
3582
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(tableName))
3583
+ return false;
3584
+ try {
3585
+ const rows = sqliteAll(this.db, `PRAGMA table_info("${tableName}")`, []);
3586
+ return rows.some((row) => row.name === columnName);
3587
+ } catch {
3588
+ return false;
3589
+ }
3590
+ }
3581
3591
  async recordRetrievalTrace(input) {
3582
3592
  await this.initialize();
3583
3593
  const traceId = randomUUID5();
@@ -3643,17 +3653,18 @@ var SQLiteEventStore = class {
3643
3653
  async getRetrievalTraceStats() {
3644
3654
  await this.initialize();
3645
3655
  try {
3656
+ const rewrittenQueryRewriteKindSql = this.hasTableColumn("retrieval_traces", "query_rewrite_kind") ? REWRITTEN_QUERY_REWRITE_KIND_SQL : "0";
3646
3657
  const row = sqliteGet(
3647
3658
  this.db,
3648
3659
  `SELECT
3649
3660
  COUNT(*) as total_queries,
3650
3661
  AVG(candidate_count) as avg_candidate_count,
3651
3662
  AVG(selected_count) as avg_selected_count,
3652
- SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN 1 ELSE 0 END) as rewritten_queries,
3653
- SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} AND selected_count > 0 THEN 1 ELSE 0 END) as rewritten_queries_with_selection,
3654
- SUM(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) AND selected_count > 0 THEN 1 ELSE 0 END) as raw_queries_with_selection,
3655
- AVG(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN selected_count END) as avg_selected_count_for_rewritten_queries,
3656
- AVG(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) THEN selected_count END) as avg_selected_count_for_raw_queries,
3663
+ SUM(CASE WHEN ${rewrittenQueryRewriteKindSql} THEN 1 ELSE 0 END) as rewritten_queries,
3664
+ SUM(CASE WHEN ${rewrittenQueryRewriteKindSql} AND selected_count > 0 THEN 1 ELSE 0 END) as rewritten_queries_with_selection,
3665
+ SUM(CASE WHEN NOT (${rewrittenQueryRewriteKindSql}) AND selected_count > 0 THEN 1 ELSE 0 END) as raw_queries_with_selection,
3666
+ AVG(CASE WHEN ${rewrittenQueryRewriteKindSql} THEN selected_count END) as avg_selected_count_for_rewritten_queries,
3667
+ AVG(CASE WHEN NOT (${rewrittenQueryRewriteKindSql}) THEN selected_count END) as avg_selected_count_for_raw_queries,
3657
3668
  CASE
3658
3669
  WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
3659
3670
  ELSE 0
@@ -8056,7 +8067,7 @@ var sessionsRouter = new Hono();
8056
8067
  sessionsRouter.get("/", async (c) => {
8057
8068
  const page = parseInt(c.req.query("page") || "1", 10);
8058
8069
  const pageSize = parseInt(c.req.query("pageSize") || "20", 10);
8059
- const memoryService = getServiceFromQuery(c);
8070
+ const memoryService = getLightweightServiceFromQuery(c);
8060
8071
  try {
8061
8072
  await memoryService.initialize();
8062
8073
  const recentEvents = await memoryService.getRecentEvents(1e3);
@@ -8100,7 +8111,7 @@ sessionsRouter.get("/", async (c) => {
8100
8111
  });
8101
8112
  sessionsRouter.get("/:id", async (c) => {
8102
8113
  const { id } = c.req.param();
8103
- const memoryService = getServiceFromQuery(c);
8114
+ const memoryService = getLightweightServiceFromQuery(c);
8104
8115
  try {
8105
8116
  await memoryService.initialize();
8106
8117
  const events = await memoryService.getSessionHistory(id);
@@ -8146,7 +8157,7 @@ eventsRouter.get("/", async (c) => {
8146
8157
  const q = (c.req.query("q") || "").trim().toLowerCase();
8147
8158
  const limit = parseInt(c.req.query("limit") || "100", 10);
8148
8159
  const offset = parseInt(c.req.query("offset") || "0", 10);
8149
- const memoryService = getServiceFromQuery(c);
8160
+ const memoryService = getLightweightServiceFromQuery(c);
8150
8161
  try {
8151
8162
  await memoryService.initialize();
8152
8163
  let events;
@@ -8202,7 +8213,7 @@ eventsRouter.get("/", async (c) => {
8202
8213
  });
8203
8214
  eventsRouter.get("/:id", async (c) => {
8204
8215
  const { id } = c.req.param();
8205
- const memoryService = getServiceFromQuery(c);
8216
+ const memoryService = getLightweightServiceFromQuery(c);
8206
8217
  try {
8207
8218
  await memoryService.initialize();
8208
8219
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -8241,6 +8252,10 @@ eventsRouter.get("/:id", async (c) => {
8241
8252
  // src/apps/server/api/search.ts
8242
8253
  import { Hono as Hono3 } from "hono";
8243
8254
  var searchRouter = new Hono3();
8255
+ function isEmbeddingBackendUnavailable(error) {
8256
+ const message = error instanceof Error ? error.message : String(error);
8257
+ return /model file path or buffer|onnxruntime|transformers|embedding backend/i.test(message);
8258
+ }
8244
8259
  searchRouter.post("/", async (c) => {
8245
8260
  const memoryService = getServiceFromQuery(c);
8246
8261
  try {
@@ -8282,15 +8297,41 @@ searchRouter.post("/", async (c) => {
8282
8297
  });
8283
8298
  searchRouter.post("/disclosure", async (c) => {
8284
8299
  let memoryService;
8300
+ let body;
8285
8301
  try {
8286
- const body = await c.req.json();
8302
+ body = await c.req.json();
8287
8303
  if (!body.query) {
8288
8304
  return c.json({ error: "Query is required" }, 400);
8289
8305
  }
8290
- memoryService = body.options?.strategy === "fast" ? getLightweightServiceFromQuery(c) : getServiceFromQuery(c);
8291
- await memoryService.initialize();
8292
- const result = await memoryService.searchDisclosure(body.query, body.options);
8293
- return c.json(result);
8306
+ const useFastStrategy = body.options?.strategy === "fast";
8307
+ memoryService = useFastStrategy ? getLightweightServiceFromQuery(c) : getServiceFromQuery(c);
8308
+ try {
8309
+ await memoryService.initialize();
8310
+ const result = await memoryService.searchDisclosure(body.query, body.options);
8311
+ return c.json(result);
8312
+ } catch (error) {
8313
+ if (!useFastStrategy && isEmbeddingBackendUnavailable(error)) {
8314
+ await memoryService.shutdown();
8315
+ memoryService = getLightweightServiceFromQuery(c);
8316
+ await memoryService.initialize();
8317
+ const result = await memoryService.searchDisclosure(body.query, {
8318
+ ...body.options,
8319
+ strategy: "fast"
8320
+ });
8321
+ return c.json({
8322
+ ...result,
8323
+ meta: {
8324
+ ...result.meta,
8325
+ fallbackApplied: true,
8326
+ fallbackTrace: [
8327
+ ...result.meta.fallbackTrace || [],
8328
+ "fallback:embedding-backend-unavailable:fast"
8329
+ ]
8330
+ }
8331
+ });
8332
+ }
8333
+ throw error;
8334
+ }
8294
8335
  } catch (error) {
8295
8336
  return c.json({ error: error.message }, 500);
8296
8337
  } finally {
@@ -8298,7 +8339,7 @@ searchRouter.post("/disclosure", async (c) => {
8298
8339
  }
8299
8340
  });
8300
8341
  searchRouter.get("/disclosure/:resultId/expand", async (c) => {
8301
- const memoryService = getServiceFromQuery(c);
8342
+ const memoryService = getLightweightServiceFromQuery(c);
8302
8343
  try {
8303
8344
  const resultId = c.req.param("resultId");
8304
8345
  const rawWindowSize = c.req.query("windowSize");
@@ -8318,7 +8359,7 @@ searchRouter.get("/disclosure/:resultId/expand", async (c) => {
8318
8359
  }
8319
8360
  });
8320
8361
  searchRouter.get("/disclosure/:resultId/source", async (c) => {
8321
- const memoryService = getServiceFromQuery(c);
8362
+ const memoryService = getLightweightServiceFromQuery(c);
8322
8363
  try {
8323
8364
  const resultId = c.req.param("resultId");
8324
8365
  const result = await memoryService.sourceDisclosure(resultId);