@inupedia/spotlight-memory 0.2.6 → 0.2.8

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.
Files changed (43) hide show
  1. package/dist/node/exactStore.d.ts +9 -3
  2. package/dist/node/exactStore.d.ts.map +1 -1
  3. package/dist/node/exactStore.js +20 -4
  4. package/dist/node/gate.d.ts +5 -1
  5. package/dist/node/gate.d.ts.map +1 -1
  6. package/dist/node/gate.js +37 -29
  7. package/dist/node/index.d.ts +19 -12
  8. package/dist/node/index.d.ts.map +1 -1
  9. package/dist/node/index.js +93 -40
  10. package/dist/node/memoryBackends.d.ts +6 -0
  11. package/dist/node/memoryBackends.d.ts.map +1 -1
  12. package/dist/node/memoryBackends.js +19 -0
  13. package/dist/node/memoryEntryScope.d.ts +19 -0
  14. package/dist/node/memoryEntryScope.d.ts.map +1 -0
  15. package/dist/node/memoryEntryScope.js +28 -0
  16. package/dist/node/memoryLog.d.ts +2 -0
  17. package/dist/node/memoryLog.d.ts.map +1 -0
  18. package/dist/node/memoryLog.js +7 -0
  19. package/dist/node/memoryStoreTypes.d.ts +15 -0
  20. package/dist/node/memoryStoreTypes.d.ts.map +1 -0
  21. package/dist/node/memoryStoreTypes.js +1 -0
  22. package/dist/node/meta.d.ts +6 -2
  23. package/dist/node/meta.d.ts.map +1 -1
  24. package/dist/node/meta.js +9 -3
  25. package/dist/node/milvusClient.d.ts +1 -1
  26. package/dist/node/milvusClient.d.ts.map +1 -1
  27. package/dist/node/milvusClient.js +1 -1
  28. package/dist/node/milvusExpr.d.ts +16 -0
  29. package/dist/node/milvusExpr.d.ts.map +1 -0
  30. package/dist/node/milvusExpr.js +30 -0
  31. package/dist/node/milvusSemanticStore.d.ts +13 -8
  32. package/dist/node/milvusSemanticStore.d.ts.map +1 -1
  33. package/dist/node/milvusSemanticStore.js +200 -55
  34. package/dist/node/paths.d.ts +6 -2
  35. package/dist/node/paths.d.ts.map +1 -1
  36. package/dist/node/paths.js +2 -1
  37. package/dist/node/redisExactStore.d.ts +4 -3
  38. package/dist/node/redisExactStore.d.ts.map +1 -1
  39. package/dist/node/redisExactStore.js +72 -11
  40. package/dist/node/semanticStore.d.ts +7 -6
  41. package/dist/node/semanticStore.d.ts.map +1 -1
  42. package/dist/node/semanticStore.js +14 -5
  43. package/package.json +2 -2
@@ -61,9 +61,9 @@ export class RedisExactMemoryStore {
61
61
  const staleKeys = await redis.zrange(index, 0, evictCount - 1);
62
62
  if (staleKeys.length) {
63
63
  const pipeline = redis.pipeline();
64
- for (const cacheKey of staleKeys) {
65
- pipeline.del(exactKey(this.projectId, this.tenantId, cacheKey));
66
- pipeline.zrem(index, cacheKey);
64
+ for (const staleCacheKey of staleKeys) {
65
+ pipeline.del(exactKey(this.projectId, this.tenantId, staleCacheKey));
66
+ pipeline.zrem(index, staleCacheKey);
67
67
  }
68
68
  await pipeline.exec();
69
69
  }
@@ -73,8 +73,6 @@ export class RedisExactMemoryStore {
73
73
  const redis = await getSpotlightRedisClient();
74
74
  if (!redis)
75
75
  return;
76
- const pattern = spotlightRedisKey("gate", "exact", "*");
77
- void pattern;
78
76
  const index = indexKey(this.projectId, this.tenantId);
79
77
  const cacheKeys = await redis.zrange(index, 0, -1);
80
78
  for (const cacheKey of cacheKeys) {
@@ -95,6 +93,7 @@ export class RedisExactMemoryStore {
95
93
  else {
96
94
  await redis.set(key, JSON.stringify(entry), "EX", this.ttlSec(entry));
97
95
  }
96
+ await redis.zadd(index, Date.now(), cacheKey);
98
97
  return;
99
98
  }
100
99
  catch {
@@ -102,14 +101,76 @@ export class RedisExactMemoryStore {
102
101
  }
103
102
  }
104
103
  }
105
- listEntries(limit = 50) {
106
- void limit;
107
- return [];
104
+ async listEntries(limit = 50) {
105
+ const redis = await getSpotlightRedisClient();
106
+ if (!redis)
107
+ return [];
108
+ const safeLimit = Number.isFinite(limit) && limit > 0 ? Math.min(500, Math.floor(limit)) : 50;
109
+ const index = indexKey(this.projectId, this.tenantId);
110
+ const cacheKeys = await redis.zrevrange(index, 0, safeLimit - 1);
111
+ const entries = [];
112
+ for (const cacheKey of cacheKeys) {
113
+ const raw = await redis.get(exactKey(this.projectId, this.tenantId, cacheKey));
114
+ if (!raw)
115
+ continue;
116
+ try {
117
+ entries.push(JSON.parse(raw));
118
+ }
119
+ catch {
120
+ continue;
121
+ }
122
+ }
123
+ return entries.sort((a, b) => b.createdAt - a.createdAt);
108
124
  }
109
- deleteEntry(_entryId) {
125
+ async deleteEntry(entryId) {
126
+ const redis = await getSpotlightRedisClient();
127
+ if (!redis)
128
+ return false;
129
+ const index = indexKey(this.projectId, this.tenantId);
130
+ const cacheKeys = await redis.zrange(index, 0, -1);
131
+ for (const cacheKey of cacheKeys) {
132
+ const key = exactKey(this.projectId, this.tenantId, cacheKey);
133
+ const raw = await redis.get(key);
134
+ if (!raw) {
135
+ await redis.zrem(index, cacheKey);
136
+ continue;
137
+ }
138
+ try {
139
+ const entry = JSON.parse(raw);
140
+ if (entry.id !== entryId)
141
+ continue;
142
+ await redis.del(key);
143
+ await redis.zrem(index, cacheKey);
144
+ return true;
145
+ }
146
+ catch {
147
+ continue;
148
+ }
149
+ }
110
150
  return false;
111
151
  }
112
- deleteAll(_projectId) {
113
- return 0;
152
+ async deleteAll(projectId) {
153
+ if (projectId !== this.projectId)
154
+ return 0;
155
+ const redis = await getSpotlightRedisClient();
156
+ if (!redis)
157
+ return 0;
158
+ const index = indexKey(this.projectId, this.tenantId);
159
+ const cacheKeys = await redis.zrange(index, 0, -1);
160
+ if (cacheKeys.length === 0)
161
+ return 0;
162
+ const pipeline = redis.pipeline();
163
+ for (const cacheKey of cacheKeys) {
164
+ pipeline.del(exactKey(this.projectId, this.tenantId, cacheKey));
165
+ pipeline.zrem(index, cacheKey);
166
+ }
167
+ await pipeline.exec();
168
+ return cacheKeys.length;
169
+ }
170
+ async count() {
171
+ const redis = await getSpotlightRedisClient();
172
+ if (!redis)
173
+ return 0;
174
+ return redis.zcard(indexKey(this.projectId, this.tenantId));
114
175
  }
115
176
  }
@@ -1,5 +1,6 @@
1
1
  import type { SpotlightMemoryEntry } from "@inupedia/spotlight-protocol";
2
2
  import type { PackMemoryPaths } from "./paths.js";
3
+ import type { SemanticLookupOptions, SemanticWriteResult } from "./memoryStoreTypes.js";
3
4
  import { type EmbeddingProvider } from "./embeddingProvider.js";
4
5
  export interface SemanticMemoryStoreOptions {
5
6
  paths: PackMemoryPaths;
@@ -11,15 +12,15 @@ export declare class SemanticMemoryStore {
11
12
  private readonly embeddingProvider;
12
13
  constructor(options: SemanticMemoryStoreOptions);
13
14
  put(entry: SpotlightMemoryEntry, embedding?: number[] | null): void;
14
- putWithOptionalEmbedding(entry: SpotlightMemoryEntry): Promise<void>;
15
- findSimilar(projectId: string, questionNorm: string, limit?: number): Promise<Array<{
15
+ putWithOptionalEmbedding(entry: SpotlightMemoryEntry): Promise<SemanticWriteResult>;
16
+ findSimilar(projectId: string, questionNorm: string, limit?: number, options?: SemanticLookupOptions): Promise<Array<{
16
17
  entry: SpotlightMemoryEntry;
17
18
  score: number;
18
19
  }>>;
19
20
  touch(entryId: string): void;
20
- listEntries(limit?: number): SpotlightMemoryEntry[];
21
- deleteEntry(entryId: string): boolean;
22
- deleteAll(projectId: string): number;
23
- count(projectId?: string): number;
21
+ listEntries(limit?: number): Promise<SpotlightMemoryEntry[]>;
22
+ deleteEntry(entryId: string): Promise<boolean>;
23
+ deleteAll(projectId: string): Promise<number>;
24
+ count(projectId?: string): Promise<number>;
24
25
  }
25
26
  //# sourceMappingURL=semanticStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"semanticStore.d.ts","sourceRoot":"","sources":["../../src/node/semanticStore.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EACL,KAAK,iBAAiB,EAGvB,MAAM,wBAAwB,CAAC;AAOhC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AM,WAAW,0BAA0B;IACzC,KAAK,EAAE,eAAe,CAAC;IACvB,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAAC;CAC9C;AAED,qFAAqF;AACrF,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAe;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA2B;gBAEjD,OAAO,EAAE,0BAA0B;IAoB/C,GAAG,CAAC,KAAK,EAAE,oBAAoB,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,IAAI;IAc7D,wBAAwB,CAC5B,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,mBAAmB,CAAC;IAazB,WAAW,CACf,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,KAAK,SAAI,EACT,OAAO,CAAC,EAAE,qBAAqB,GAC9B,OAAO,CAAC,KAAK,CAAC;QAAE,KAAK,EAAE,oBAAoB,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAmDjE,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAiBtB,WAAW,CAAC,KAAK,SAAK,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC;IAiBxD,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAO9C,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAO7C,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAYjD"}
@@ -1,6 +1,7 @@
1
1
  import { mkdirSync } from "node:fs";
2
2
  import { DatabaseSync } from "node:sqlite";
3
3
  import { scoreQuestionSimilarity } from "../normalize.js";
4
+ import { entryMatchesLookupScopes, isMemoryEntryExpired, } from "./memoryEntryScope.js";
4
5
  import { resolveEmbeddingProviderFromEnv, scoreEmbeddingPair, } from "./embeddingProvider.js";
5
6
  /** L1 semantic cache backed by SQLite; BM25/bigram fallback, optional embeddings. */
6
7
  export class SemanticMemoryStore {
@@ -43,8 +44,9 @@ export class SemanticMemoryStore {
43
44
  }
44
45
  }
45
46
  this.put(entry, embedding);
47
+ return { written: true };
46
48
  }
47
- async findSimilar(projectId, questionNorm, limit = 5) {
49
+ async findSimilar(projectId, questionNorm, limit = 5, options) {
48
50
  const rows = this.db
49
51
  .prepare(`SELECT entry_json, embedding_json FROM entries WHERE project_id = ?`)
50
52
  .all(projectId);
@@ -57,6 +59,7 @@ export class SemanticMemoryStore {
57
59
  queryEmbedding = null;
58
60
  }
59
61
  }
62
+ const now = options?.now ?? Date.now();
60
63
  const scored = [];
61
64
  for (const row of rows) {
62
65
  let entry;
@@ -66,6 +69,12 @@ export class SemanticMemoryStore {
66
69
  catch {
67
70
  continue;
68
71
  }
72
+ if (isMemoryEntryExpired(entry, now))
73
+ continue;
74
+ if (options?.scopes?.length &&
75
+ !entryMatchesLookupScopes(entry, options.scopes)) {
76
+ continue;
77
+ }
69
78
  const lexical = scoreQuestionSimilarity(questionNorm, entry.questionNorm);
70
79
  let embeddingScore = 0;
71
80
  if (queryEmbedding && row.embedding_json) {
@@ -103,7 +112,7 @@ export class SemanticMemoryStore {
103
112
  /* ignore corrupt row */
104
113
  }
105
114
  }
106
- listEntries(limit = 50) {
115
+ async listEntries(limit = 50) {
107
116
  const safeLimit = Number.isFinite(limit) && limit > 0 ? Math.min(500, Math.floor(limit)) : 50;
108
117
  const rows = this.db
109
118
  .prepare(`SELECT entry_json FROM entries ORDER BY json_extract(entry_json, '$.createdAt') DESC LIMIT ?`)
@@ -119,19 +128,19 @@ export class SemanticMemoryStore {
119
128
  }
120
129
  return entries;
121
130
  }
122
- deleteEntry(entryId) {
131
+ async deleteEntry(entryId) {
123
132
  const result = this.db
124
133
  .prepare(`DELETE FROM entries WHERE id = ?`)
125
134
  .run(entryId);
126
135
  return Number(result.changes) > 0;
127
136
  }
128
- deleteAll(projectId) {
137
+ async deleteAll(projectId) {
129
138
  const result = this.db
130
139
  .prepare(`DELETE FROM entries WHERE project_id = ?`)
131
140
  .run(projectId);
132
141
  return Number(result.changes);
133
142
  }
134
- count(projectId) {
143
+ async count(projectId) {
135
144
  if (projectId) {
136
145
  const row = this.db
137
146
  .prepare(`SELECT COUNT(*) AS total FROM entries WHERE project_id = ?`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inupedia/spotlight-memory",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "Memory Gate, exact/semantic cache stores for Inupedia Spotlight",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "@zilliz/milvus2-sdk-node": "^2.5.5",
28
28
  "ioredis": "^5.6.1",
29
- "@inupedia/spotlight-protocol": "0.2.6"
29
+ "@inupedia/spotlight-protocol": "0.2.8"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/node": "^22.10.0",