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.
@@ -3590,6 +3590,16 @@ var SQLiteEventStore = class {
3590
3590
  getDatabase() {
3591
3591
  return this.db;
3592
3592
  }
3593
+ hasTableColumn(tableName, columnName) {
3594
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(tableName))
3595
+ return false;
3596
+ try {
3597
+ const rows = sqliteAll(this.db, `PRAGMA table_info("${tableName}")`, []);
3598
+ return rows.some((row) => row.name === columnName);
3599
+ } catch {
3600
+ return false;
3601
+ }
3602
+ }
3593
3603
  async recordRetrievalTrace(input) {
3594
3604
  await this.initialize();
3595
3605
  const traceId = randomUUID5();
@@ -3655,17 +3665,18 @@ var SQLiteEventStore = class {
3655
3665
  async getRetrievalTraceStats() {
3656
3666
  await this.initialize();
3657
3667
  try {
3668
+ const rewrittenQueryRewriteKindSql = this.hasTableColumn("retrieval_traces", "query_rewrite_kind") ? REWRITTEN_QUERY_REWRITE_KIND_SQL : "0";
3658
3669
  const row = sqliteGet(
3659
3670
  this.db,
3660
3671
  `SELECT
3661
3672
  COUNT(*) as total_queries,
3662
3673
  AVG(candidate_count) as avg_candidate_count,
3663
3674
  AVG(selected_count) as avg_selected_count,
3664
- SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN 1 ELSE 0 END) as rewritten_queries,
3665
- SUM(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} AND selected_count > 0 THEN 1 ELSE 0 END) as rewritten_queries_with_selection,
3666
- SUM(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) AND selected_count > 0 THEN 1 ELSE 0 END) as raw_queries_with_selection,
3667
- AVG(CASE WHEN ${REWRITTEN_QUERY_REWRITE_KIND_SQL} THEN selected_count END) as avg_selected_count_for_rewritten_queries,
3668
- AVG(CASE WHEN NOT (${REWRITTEN_QUERY_REWRITE_KIND_SQL}) THEN selected_count END) as avg_selected_count_for_raw_queries,
3675
+ SUM(CASE WHEN ${rewrittenQueryRewriteKindSql} THEN 1 ELSE 0 END) as rewritten_queries,
3676
+ SUM(CASE WHEN ${rewrittenQueryRewriteKindSql} AND selected_count > 0 THEN 1 ELSE 0 END) as rewritten_queries_with_selection,
3677
+ SUM(CASE WHEN NOT (${rewrittenQueryRewriteKindSql}) AND selected_count > 0 THEN 1 ELSE 0 END) as raw_queries_with_selection,
3678
+ AVG(CASE WHEN ${rewrittenQueryRewriteKindSql} THEN selected_count END) as avg_selected_count_for_rewritten_queries,
3679
+ AVG(CASE WHEN NOT (${rewrittenQueryRewriteKindSql}) THEN selected_count END) as avg_selected_count_for_raw_queries,
3669
3680
  CASE
3670
3681
  WHEN SUM(candidate_count) > 0 THEN (SUM(selected_count) * 1.0 / SUM(candidate_count))
3671
3682
  ELSE 0
@@ -8068,7 +8079,7 @@ var sessionsRouter = new Hono();
8068
8079
  sessionsRouter.get("/", async (c) => {
8069
8080
  const page = parseInt(c.req.query("page") || "1", 10);
8070
8081
  const pageSize = parseInt(c.req.query("pageSize") || "20", 10);
8071
- const memoryService = getServiceFromQuery(c);
8082
+ const memoryService = getLightweightServiceFromQuery(c);
8072
8083
  try {
8073
8084
  await memoryService.initialize();
8074
8085
  const recentEvents = await memoryService.getRecentEvents(1e3);
@@ -8112,7 +8123,7 @@ sessionsRouter.get("/", async (c) => {
8112
8123
  });
8113
8124
  sessionsRouter.get("/:id", async (c) => {
8114
8125
  const { id } = c.req.param();
8115
- const memoryService = getServiceFromQuery(c);
8126
+ const memoryService = getLightweightServiceFromQuery(c);
8116
8127
  try {
8117
8128
  await memoryService.initialize();
8118
8129
  const events = await memoryService.getSessionHistory(id);
@@ -8158,7 +8169,7 @@ eventsRouter.get("/", async (c) => {
8158
8169
  const q = (c.req.query("q") || "").trim().toLowerCase();
8159
8170
  const limit = parseInt(c.req.query("limit") || "100", 10);
8160
8171
  const offset = parseInt(c.req.query("offset") || "0", 10);
8161
- const memoryService = getServiceFromQuery(c);
8172
+ const memoryService = getLightweightServiceFromQuery(c);
8162
8173
  try {
8163
8174
  await memoryService.initialize();
8164
8175
  let events;
@@ -8214,7 +8225,7 @@ eventsRouter.get("/", async (c) => {
8214
8225
  });
8215
8226
  eventsRouter.get("/:id", async (c) => {
8216
8227
  const { id } = c.req.param();
8217
- const memoryService = getServiceFromQuery(c);
8228
+ const memoryService = getLightweightServiceFromQuery(c);
8218
8229
  try {
8219
8230
  await memoryService.initialize();
8220
8231
  const recentEvents = await memoryService.getRecentEvents(1e4);
@@ -8253,6 +8264,10 @@ eventsRouter.get("/:id", async (c) => {
8253
8264
  // src/apps/server/api/search.ts
8254
8265
  import { Hono as Hono3 } from "hono";
8255
8266
  var searchRouter = new Hono3();
8267
+ function isEmbeddingBackendUnavailable(error) {
8268
+ const message = error instanceof Error ? error.message : String(error);
8269
+ return /model file path or buffer|onnxruntime|transformers|embedding backend/i.test(message);
8270
+ }
8256
8271
  searchRouter.post("/", async (c) => {
8257
8272
  const memoryService = getServiceFromQuery(c);
8258
8273
  try {
@@ -8294,15 +8309,41 @@ searchRouter.post("/", async (c) => {
8294
8309
  });
8295
8310
  searchRouter.post("/disclosure", async (c) => {
8296
8311
  let memoryService;
8312
+ let body;
8297
8313
  try {
8298
- const body = await c.req.json();
8314
+ body = await c.req.json();
8299
8315
  if (!body.query) {
8300
8316
  return c.json({ error: "Query is required" }, 400);
8301
8317
  }
8302
- memoryService = body.options?.strategy === "fast" ? getLightweightServiceFromQuery(c) : getServiceFromQuery(c);
8303
- await memoryService.initialize();
8304
- const result = await memoryService.searchDisclosure(body.query, body.options);
8305
- return c.json(result);
8318
+ const useFastStrategy = body.options?.strategy === "fast";
8319
+ memoryService = useFastStrategy ? getLightweightServiceFromQuery(c) : getServiceFromQuery(c);
8320
+ try {
8321
+ await memoryService.initialize();
8322
+ const result = await memoryService.searchDisclosure(body.query, body.options);
8323
+ return c.json(result);
8324
+ } catch (error) {
8325
+ if (!useFastStrategy && isEmbeddingBackendUnavailable(error)) {
8326
+ await memoryService.shutdown();
8327
+ memoryService = getLightweightServiceFromQuery(c);
8328
+ await memoryService.initialize();
8329
+ const result = await memoryService.searchDisclosure(body.query, {
8330
+ ...body.options,
8331
+ strategy: "fast"
8332
+ });
8333
+ return c.json({
8334
+ ...result,
8335
+ meta: {
8336
+ ...result.meta,
8337
+ fallbackApplied: true,
8338
+ fallbackTrace: [
8339
+ ...result.meta.fallbackTrace || [],
8340
+ "fallback:embedding-backend-unavailable:fast"
8341
+ ]
8342
+ }
8343
+ });
8344
+ }
8345
+ throw error;
8346
+ }
8306
8347
  } catch (error) {
8307
8348
  return c.json({ error: error.message }, 500);
8308
8349
  } finally {
@@ -8310,7 +8351,7 @@ searchRouter.post("/disclosure", async (c) => {
8310
8351
  }
8311
8352
  });
8312
8353
  searchRouter.get("/disclosure/:resultId/expand", async (c) => {
8313
- const memoryService = getServiceFromQuery(c);
8354
+ const memoryService = getLightweightServiceFromQuery(c);
8314
8355
  try {
8315
8356
  const resultId = c.req.param("resultId");
8316
8357
  const rawWindowSize = c.req.query("windowSize");
@@ -8330,7 +8371,7 @@ searchRouter.get("/disclosure/:resultId/expand", async (c) => {
8330
8371
  }
8331
8372
  });
8332
8373
  searchRouter.get("/disclosure/:resultId/source", async (c) => {
8333
- const memoryService = getServiceFromQuery(c);
8374
+ const memoryService = getLightweightServiceFromQuery(c);
8334
8375
  try {
8335
8376
  const resultId = c.req.param("resultId");
8336
8377
  const result = await memoryService.sourceDisclosure(resultId);