@inupedia/spotlight-memory 0.2.6 → 0.2.7

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
@@ -1 +1 @@
1
- {"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../src/node/meta.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AAEpB,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,eAAe,GAAG,cAAc,CAOzE;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,eAAe,EACtB,IAAI,EAAE,cAAc,GACnB,IAAI,CAGN;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,KAAK,EAAE,eAAe,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;CACvB,GAAG,cAAc,CASjB"}
1
+ {"version":3,"file":"meta.d.ts","sourceRoot":"","sources":["../../src/node/meta.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,YAAY,CAAC;AAEpB,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,eAAe,GAAG,cAAc,CAOzE;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,eAAe,EACtB,IAAI,EAAE,cAAc,GACnB,IAAI,CAGN;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,KAAK,EAAE,eAAe,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,eAAe,CAAC,EAAE,qBAAqB,CAAC;IACxC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B,GAAG,cAAc,CAejB"}
package/dist/node/meta.js CHANGED
@@ -15,12 +15,18 @@ export function writePackMemoryMeta(paths, meta) {
15
15
  writeFileSync(paths.metaPath, `${JSON.stringify(meta, null, 2)}\n`, "utf8");
16
16
  }
17
17
  export function refreshPackMemoryMeta(params) {
18
+ const countsAvailable = params.countsAvailable ?? true;
18
19
  const meta = {
19
- version: "1",
20
- exactCount: params.exactCount,
21
- semanticCount: params.semanticCount,
20
+ version: "2",
22
21
  updatedAt: new Date().toISOString(),
22
+ exactBackend: params.exactBackend,
23
+ semanticBackend: params.semanticBackend,
24
+ countsAvailable,
23
25
  };
26
+ if (countsAvailable) {
27
+ meta.exactCount = params.exactCount ?? 0;
28
+ meta.semanticCount = params.semanticCount ?? 0;
29
+ }
24
30
  writePackMemoryMeta(params.paths, meta);
25
31
  return meta;
26
32
  }
@@ -1,5 +1,5 @@
1
1
  import type { MilvusClient } from "@zilliz/milvus2-sdk-node";
2
- export declare const SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION = "spotlight_gate_semantic";
2
+ export declare const SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION = "spotlight_gate_semantic_v2";
3
3
  export declare function resolveSpotlightMilvusUri(): string | null;
4
4
  /** 独立 Milvus 数据库,避免与 Yuxi 知识库 `yuxi` / `default` 混用。 */
5
5
  export declare function resolveSpotlightMilvusDb(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"milvusClient.d.ts","sourceRoot":"","sources":["../../src/node/milvusClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAK7D,eAAO,MAAM,oCAAoC,4BAA4B,CAAC;AAE9E,wBAAgB,yBAAyB,IAAI,MAAM,GAAG,IAAI,CAEzD;AAED,wDAAwD;AACxD,wBAAgB,wBAAwB,IAAI,MAAM,CAEjD;AAED,wBAAgB,2BAA2B,IAAI,MAAM,CAEpD;AAED,wBAAgB,kCAAkC,IAAI,MAAM,CAG3D;AAED,wBAAgB,2BAA2B,IAAI,OAAO,CAErD;AAoBD,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAW7E;AAED,wBAAgB,kCAAkC,IAAI,IAAI,CAGzD"}
1
+ {"version":3,"file":"milvusClient.d.ts","sourceRoot":"","sources":["../../src/node/milvusClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAK7D,eAAO,MAAM,oCAAoC,+BAA+B,CAAC;AAEjF,wBAAgB,yBAAyB,IAAI,MAAM,GAAG,IAAI,CAEzD;AAED,wDAAwD;AACxD,wBAAgB,wBAAwB,IAAI,MAAM,CAEjD;AAED,wBAAgB,2BAA2B,IAAI,MAAM,CAEpD;AAED,wBAAgB,kCAAkC,IAAI,MAAM,CAG3D;AAED,wBAAgB,2BAA2B,IAAI,OAAO,CAErD;AAoBD,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAW7E;AAED,wBAAgB,kCAAkC,IAAI,IAAI,CAGzD"}
@@ -1,6 +1,6 @@
1
1
  let client = null;
2
2
  let dbReady = false;
3
- export const SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION = "spotlight_gate_semantic";
3
+ export const SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION = "spotlight_gate_semantic_v2";
4
4
  export function resolveSpotlightMilvusUri() {
5
5
  return process.env.SPOTLIGHT_MILVUS_URI?.trim() || null;
6
6
  }
@@ -0,0 +1,16 @@
1
+ import type { MemoryEntryScope } from "./memoryEntryScope.js";
2
+ export declare function escapeMilvusExpr(value: string): string;
3
+ export declare function buildMilvusScopeFilter(scopes: Array<{
4
+ scope: MemoryEntryScope;
5
+ sessionId?: string;
6
+ }>): string;
7
+ export declare function buildMilvusSemanticFilter(params: {
8
+ projectId: string;
9
+ tenantId: string;
10
+ now: number;
11
+ scopes?: Array<{
12
+ scope: MemoryEntryScope;
13
+ sessionId?: string;
14
+ }>;
15
+ }): string;
16
+ //# sourceMappingURL=milvusExpr.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"milvusExpr.d.ts","sourceRoot":"","sources":["../../src/node/milvusExpr.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,KAAK,CAAC;IAAE,KAAK,EAAE,gBAAgB,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,GAC7D,MAAM,CAYR;AAED,wBAAgB,yBAAyB,CAAC,MAAM,EAAE;IAChD,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,KAAK,CAAC;QAAE,KAAK,EAAE,gBAAgB,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACjE,GAAG,MAAM,CAUT"}
@@ -0,0 +1,30 @@
1
+ export function escapeMilvusExpr(value) {
2
+ return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
3
+ }
4
+ export function buildMilvusScopeFilter(scopes) {
5
+ const clauses = scopes.map((scope) => {
6
+ if (scope.scope === "session") {
7
+ const sessionId = scope.sessionId?.trim();
8
+ if (!sessionId)
9
+ return `(scope == "project")`;
10
+ return `(scope == "session" && session_id == "${escapeMilvusExpr(sessionId)}")`;
11
+ }
12
+ return `(scope == "project")`;
13
+ });
14
+ if (clauses.length === 0)
15
+ return `(scope == "project")`;
16
+ if (clauses.length === 1)
17
+ return clauses[0];
18
+ return `(${clauses.join(" || ")})`;
19
+ }
20
+ export function buildMilvusSemanticFilter(params) {
21
+ const parts = [
22
+ `project_id == "${escapeMilvusExpr(params.projectId)}"`,
23
+ `tenant_id == "${escapeMilvusExpr(params.tenantId)}"`,
24
+ `expires_at > ${Math.floor(params.now)}`,
25
+ ];
26
+ if (params.scopes?.length) {
27
+ parts.push(buildMilvusScopeFilter(params.scopes));
28
+ }
29
+ return parts.join(" && ");
30
+ }
@@ -1,31 +1,36 @@
1
1
  import type { SpotlightMemoryEntry } from "@inupedia/spotlight-protocol";
2
2
  import { type EmbeddingProvider } from "./embeddingProvider.js";
3
+ import type { SemanticLookupOptions, SemanticWriteResult } from "./memoryStoreTypes.js";
3
4
  export interface MilvusSemanticMemoryStoreOptions {
4
5
  projectId: string;
5
6
  tenantId?: string;
6
7
  embeddingProvider?: EmbeddingProvider | null;
7
8
  }
8
- /** L1 semantic cache in Milvus (Spotlight 独立 DB / collection,与 Yuxi kb_* 隔离). */
9
+ export declare function isMilvusEmbeddingReady(): boolean;
10
+ export declare function validateMilvusEmbedding(embedding: number[] | null | undefined, dim?: number): boolean;
11
+ /** L1 semantic recall in Milvus (Spotlight 独立 DB / collection,与 Yuxi kb_* 隔离). */
9
12
  export declare class MilvusSemanticMemoryStore {
10
13
  private readonly projectId;
11
14
  private readonly tenantId;
12
15
  private readonly embeddingProvider;
16
+ private readonly embeddingDim;
13
17
  private collectionReady;
14
18
  constructor(options: MilvusSemanticMemoryStoreOptions);
15
19
  static isAvailable(): boolean;
16
20
  private ensureCollection;
17
21
  private initCollection;
18
- put(entry: SpotlightMemoryEntry, embedding?: number[] | null): Promise<void>;
19
- putWithOptionalEmbedding(entry: SpotlightMemoryEntry): Promise<void>;
20
- findSimilar(projectId: string, questionNorm: string, limit?: number): Promise<Array<{
22
+ put(entry: SpotlightMemoryEntry, embedding: number[]): Promise<void>;
23
+ putWithOptionalEmbedding(entry: SpotlightMemoryEntry): Promise<SemanticWriteResult>;
24
+ findSimilar(projectId: string, questionNorm: string, limit?: number, options?: SemanticLookupOptions): Promise<Array<{
21
25
  entry: SpotlightMemoryEntry;
22
26
  score: number;
23
27
  }>>;
28
+ private scoreMilvusHits;
24
29
  private findSimilarLexical;
25
30
  touch(entryId: string): Promise<void>;
26
- listEntries(limit?: number): SpotlightMemoryEntry[];
27
- deleteEntry(entryId: string): boolean;
28
- deleteAll(_projectId: string): number;
29
- count(projectId?: string): number;
31
+ listEntries(limit?: number): Promise<SpotlightMemoryEntry[]>;
32
+ deleteEntry(entryId: string): Promise<boolean>;
33
+ deleteAll(projectId: string): Promise<number>;
34
+ count(projectId?: string): Promise<number>;
30
35
  }
31
36
  //# sourceMappingURL=milvusSemanticStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"milvusSemanticStore.d.ts","sourceRoot":"","sources":["../../src/node/milvusSemanticStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEzE,OAAO,EACL,KAAK,iBAAiB,EAGvB,MAAM,wBAAwB,CAAC;AAQhC,MAAM,WAAW,gCAAgC;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAAC;CAC9C;AAED,iFAAiF;AACjF,qBAAa,yBAAyB;IACpC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA2B;IAC7D,OAAO,CAAC,eAAe,CAA8B;gBAEzC,OAAO,EAAE,gCAAgC;IASrD,MAAM,CAAC,WAAW,IAAI,OAAO;YAIf,gBAAgB;YAOhB,cAAc;IAwDtB,GAAG,CAAC,KAAK,EAAE,oBAAoB,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB5E,wBAAwB,CAAC,KAAK,EAAE,oBAAo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AS;IAClC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA2B;IAC7D,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IACtC,OAAO,CAAC,eAAe,CAA8B;gBAEzC,OAAO,EAAE,gCAAgC;IAUrD,MAAM,CAAC,WAAW,IAAI,OAAO;YAIf,gBAAgB;YAOhB,cAAc;IA+EtB,GAAG,CACP,KAAK,EAAE,oBAAoB,EAC3B,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,IAAI,CAAC;IA4BV,wBAAwB,CAC5B,KAAK,EAAE,oBAAoB,GAC1B,OAAO,CAAC,mBAAmB,CAAC;IAsCzB,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;IAoCjE,OAAO,CAAC,eAAe;YA2BT,kBAAkB;IA6B1B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmCrC,WAAW,CAAC,KAAK,SAAK,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC;IA4BxD,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAW9C,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAW7C,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAiBjD"}
@@ -1,22 +1,33 @@
1
1
  import { scoreQuestionSimilarity } from "../normalize.js";
2
2
  import { resolveEmbeddingProviderFromEnv, } from "./embeddingProvider.js";
3
+ import { resolveMemoryEntryScopeFields, } from "./memoryEntryScope.js";
4
+ import { logMemoryEvent } from "./memoryLog.js";
5
+ import { buildMilvusSemanticFilter, escapeMilvusExpr, } from "./milvusExpr.js";
3
6
  import { getSpotlightMilvusClient, isSpotlightMilvusConfigured, resolveSpotlightMilvusEmbeddingDim, SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION, } from "./milvusClient.js";
4
- /** L1 semantic cache in Milvus (Spotlight 独立 DB / collection,与 Yuxi kb_* 隔离). */
7
+ export function isMilvusEmbeddingReady() {
8
+ return (isSpotlightMilvusConfigured() && resolveEmbeddingProviderFromEnv() != null);
9
+ }
10
+ export function validateMilvusEmbedding(embedding, dim = resolveSpotlightMilvusEmbeddingDim()) {
11
+ return Array.isArray(embedding) && embedding.length === dim;
12
+ }
13
+ /** L1 semantic recall in Milvus (Spotlight 独立 DB / collection,与 Yuxi kb_* 隔离). */
5
14
  export class MilvusSemanticMemoryStore {
6
15
  projectId;
7
16
  tenantId;
8
17
  embeddingProvider;
18
+ embeddingDim;
9
19
  collectionReady = null;
10
20
  constructor(options) {
11
21
  this.projectId = options.projectId;
12
22
  this.tenantId = options.tenantId?.trim() || "default";
23
+ this.embeddingDim = resolveSpotlightMilvusEmbeddingDim();
13
24
  this.embeddingProvider =
14
25
  options.embeddingProvider === undefined
15
26
  ? resolveEmbeddingProviderFromEnv()
16
27
  : options.embeddingProvider;
17
28
  }
18
29
  static isAvailable() {
19
- return isSpotlightMilvusConfigured();
30
+ return isMilvusEmbeddingReady();
20
31
  }
21
32
  async ensureCollection() {
22
33
  if (!this.collectionReady) {
@@ -29,7 +40,7 @@ export class MilvusSemanticMemoryStore {
29
40
  if (!client)
30
41
  throw new Error("Milvus client unavailable");
31
42
  const { DataType, IndexType, MetricType } = await import("@zilliz/milvus2-sdk-node");
32
- const dim = resolveSpotlightMilvusEmbeddingDim();
43
+ const dim = this.embeddingDim;
33
44
  const name = SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION;
34
45
  const exists = await client.hasCollection({ collection_name: name });
35
46
  if (!exists.value) {
@@ -52,6 +63,29 @@ export class MilvusSemanticMemoryStore {
52
63
  data_type: DataType.VarChar,
53
64
  max_length: 128,
54
65
  },
66
+ {
67
+ name: "scope",
68
+ data_type: DataType.VarChar,
69
+ max_length: 16,
70
+ },
71
+ {
72
+ name: "session_id",
73
+ data_type: DataType.VarChar,
74
+ max_length: 128,
75
+ },
76
+ {
77
+ name: "kind",
78
+ data_type: DataType.VarChar,
79
+ max_length: 64,
80
+ },
81
+ {
82
+ name: "created_at",
83
+ data_type: DataType.Int64,
84
+ },
85
+ {
86
+ name: "expires_at",
87
+ data_type: DataType.Int64,
88
+ },
55
89
  {
56
90
  name: "question_norm",
57
91
  data_type: DataType.VarChar,
@@ -60,7 +94,7 @@ export class MilvusSemanticMemoryStore {
60
94
  {
61
95
  name: "entry_json",
62
96
  data_type: DataType.VarChar,
63
- max_length: 65_535,
97
+ max_length: 65_536,
64
98
  },
65
99
  {
66
100
  name: "embedding",
@@ -79,10 +113,14 @@ export class MilvusSemanticMemoryStore {
79
113
  await client.loadCollectionSync({ collection_name: name });
80
114
  }
81
115
  async put(entry, embedding) {
116
+ if (!validateMilvusEmbedding(embedding, this.embeddingDim)) {
117
+ throw new Error("invalid_milvus_embedding");
118
+ }
82
119
  await this.ensureCollection();
83
120
  const client = await getSpotlightMilvusClient();
84
121
  if (!client)
85
122
  return;
123
+ const scopeFields = resolveMemoryEntryScopeFields(entry);
86
124
  await client.upsert({
87
125
  collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
88
126
  data: [
@@ -90,32 +128,70 @@ export class MilvusSemanticMemoryStore {
90
128
  id: entry.id,
91
129
  project_id: entry.projectId,
92
130
  tenant_id: this.tenantId,
131
+ scope: scopeFields.scope,
132
+ session_id: scopeFields.sessionId,
133
+ kind: scopeFields.kind,
134
+ created_at: scopeFields.createdAt,
135
+ expires_at: scopeFields.expiresAt,
93
136
  question_norm: entry.questionNorm,
94
137
  entry_json: JSON.stringify(entry),
95
- embedding: embedding ?? [],
138
+ embedding,
96
139
  },
97
140
  ],
98
141
  });
99
142
  }
100
143
  async putWithOptionalEmbedding(entry) {
144
+ if (!this.embeddingProvider) {
145
+ logMemoryEvent("semantic_write_skipped", {
146
+ projectId: entry.projectId,
147
+ entryId: entry.id,
148
+ reason: "no_embedding",
149
+ backend: "milvus",
150
+ });
151
+ return { written: false, skippedReason: "no_embedding" };
152
+ }
101
153
  let embedding = null;
102
- if (this.embeddingProvider) {
103
- try {
104
- embedding = await this.embeddingProvider.embed(entry.questionNorm);
105
- }
106
- catch {
107
- embedding = null;
108
- }
154
+ try {
155
+ embedding = await this.embeddingProvider.embed(entry.questionNorm);
156
+ }
157
+ catch (error) {
158
+ logMemoryEvent("semantic_write_skipped", {
159
+ projectId: entry.projectId,
160
+ entryId: entry.id,
161
+ reason: "embedding_failed",
162
+ backend: "milvus",
163
+ error: String(error),
164
+ });
165
+ return { written: false, skippedReason: "embedding_failed" };
166
+ }
167
+ if (!validateMilvusEmbedding(embedding, this.embeddingDim)) {
168
+ logMemoryEvent("semantic_write_skipped", {
169
+ projectId: entry.projectId,
170
+ entryId: entry.id,
171
+ reason: "invalid_embedding_dim",
172
+ backend: "milvus",
173
+ dim: this.embeddingDim,
174
+ actualDim: embedding?.length ?? 0,
175
+ });
176
+ return { written: false, skippedReason: "invalid_embedding_dim" };
109
177
  }
110
178
  await this.put(entry, embedding);
179
+ return { written: true };
111
180
  }
112
- async findSimilar(projectId, questionNorm, limit = 5) {
181
+ async findSimilar(projectId, questionNorm, limit = 5, options) {
113
182
  if (projectId !== this.projectId)
114
183
  return [];
115
184
  await this.ensureCollection();
116
185
  const client = await getSpotlightMilvusClient();
117
186
  if (!client)
118
187
  return [];
188
+ const now = options?.now ?? Date.now();
189
+ const filter = buildMilvusSemanticFilter({
190
+ projectId,
191
+ tenantId: this.tenantId,
192
+ now,
193
+ scopes: options?.scopes,
194
+ });
119
195
  let queryEmbedding = null;
120
196
  if (this.embeddingProvider) {
121
197
  try {
@@ -125,46 +201,48 @@ export class MilvusSemanticMemoryStore {
125
201
  queryEmbedding = null;
126
202
  }
127
203
  }
128
- if (queryEmbedding?.length) {
204
+ if (queryEmbedding && validateMilvusEmbedding(queryEmbedding, this.embeddingDim)) {
129
205
  const search = await client.search({
130
206
  collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
131
207
  data: [queryEmbedding],
132
208
  limit: Math.max(limit, 8),
133
- filter: `project_id == "${escapeMilvusExpr(projectId)}" && tenant_id == "${escapeMilvusExpr(this.tenantId)}"`,
209
+ filter,
134
210
  output_fields: ["entry_json", "question_norm"],
135
211
  });
136
- const hits = search.results ?? [];
137
- const scored = [];
138
- for (const hit of hits) {
139
- const entryJson = String(hit.entry_json ?? "");
140
- let entry;
141
- try {
142
- entry = JSON.parse(entryJson);
143
- }
144
- catch {
145
- continue;
146
- }
147
- const lexical = scoreQuestionSimilarity(questionNorm, entry.questionNorm);
148
- const vectorScore = typeof hit.score === "number"
149
- ? Math.max(0, Math.min(1, (hit.score + 1) / 2))
150
- : 0;
151
- const score = Math.max(lexical, vectorScore);
152
- if (score <= 0)
153
- continue;
154
- scored.push({ entry, score });
212
+ return this.scoreMilvusHits(search.results ?? [], questionNorm, limit);
213
+ }
214
+ return this.findSimilarLexical(projectId, questionNorm, limit, filter);
215
+ }
216
+ scoreMilvusHits(hits, questionNorm, limit) {
217
+ const scored = [];
218
+ for (const hit of hits) {
219
+ const entryJson = String(hit.entry_json ?? "");
220
+ let entry;
221
+ try {
222
+ entry = JSON.parse(entryJson);
155
223
  }
156
- scored.sort((a, b) => b.score - a.score);
157
- return scored.slice(0, limit);
224
+ catch {
225
+ continue;
226
+ }
227
+ const lexical = scoreQuestionSimilarity(questionNorm, entry.questionNorm);
228
+ const vectorScore = typeof hit.score === "number"
229
+ ? Math.max(0, Math.min(1, (hit.score + 1) / 2))
230
+ : 0;
231
+ const score = Math.max(lexical, vectorScore);
232
+ if (score <= 0)
233
+ continue;
234
+ scored.push({ entry, score });
158
235
  }
159
- return this.findSimilarLexical(projectId, questionNorm, limit);
236
+ scored.sort((a, b) => b.score - a.score);
237
+ return scored.slice(0, limit);
160
238
  }
161
- async findSimilarLexical(projectId, questionNorm, limit) {
239
+ async findSimilarLexical(projectId, questionNorm, limit, filter) {
162
240
  const client = await getSpotlightMilvusClient();
163
241
  if (!client)
164
242
  return [];
165
243
  const query = await client.query({
166
244
  collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
167
- filter: `project_id == "${escapeMilvusExpr(projectId)}" && tenant_id == "${escapeMilvusExpr(this.tenantId)}"`,
245
+ filter,
168
246
  output_fields: ["entry_json", "question_norm"],
169
247
  limit: 200,
170
248
  });
@@ -191,7 +269,19 @@ export class MilvusSemanticMemoryStore {
191
269
  const query = await client.query({
192
270
  collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
193
271
  filter: `id == "${escapeMilvusExpr(entryId)}"`,
194
- output_fields: ["id", "project_id", "tenant_id", "question_norm", "entry_json", "embedding"],
272
+ output_fields: [
273
+ "id",
274
+ "project_id",
275
+ "tenant_id",
276
+ "scope",
277
+ "session_id",
278
+ "kind",
279
+ "created_at",
280
+ "expires_at",
281
+ "question_norm",
282
+ "entry_json",
283
+ "embedding",
284
+ ],
195
285
  limit: 1,
196
286
  });
197
287
  const row = query.data?.[0];
@@ -201,28 +291,83 @@ export class MilvusSemanticMemoryStore {
201
291
  const entry = JSON.parse(String(row.entry_json));
202
292
  entry.hitCount += 1;
203
293
  entry.lastHitAt = Date.now();
204
- await this.put(entry, row.embedding);
294
+ const embedding = row.embedding;
295
+ if (!validateMilvusEmbedding(embedding, this.embeddingDim))
296
+ return;
297
+ await this.put(entry, embedding);
205
298
  }
206
299
  catch {
207
300
  /* ignore */
208
301
  }
209
302
  }
210
- listEntries(limit = 50) {
211
- void limit;
212
- return [];
303
+ async listEntries(limit = 50) {
304
+ await this.ensureCollection();
305
+ const client = await getSpotlightMilvusClient();
306
+ if (!client)
307
+ return [];
308
+ const safeLimit = Number.isFinite(limit) && limit > 0 ? Math.min(500, Math.floor(limit)) : 50;
309
+ const filter = buildMilvusSemanticFilter({
310
+ projectId: this.projectId,
311
+ tenantId: this.tenantId,
312
+ now: Date.now(),
313
+ });
314
+ const query = await client.query({
315
+ collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
316
+ filter,
317
+ output_fields: ["entry_json", "created_at"],
318
+ limit: safeLimit,
319
+ });
320
+ const entries = [];
321
+ for (const row of query.data ?? []) {
322
+ try {
323
+ entries.push(JSON.parse(String(row.entry_json)));
324
+ }
325
+ catch {
326
+ continue;
327
+ }
328
+ }
329
+ return entries.sort((a, b) => b.createdAt - a.createdAt);
213
330
  }
214
- deleteEntry(entryId) {
215
- void entryId;
216
- return false;
331
+ async deleteEntry(entryId) {
332
+ const client = await getSpotlightMilvusClient();
333
+ if (!client)
334
+ return false;
335
+ const result = await client.delete({
336
+ collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
337
+ filter: `id == "${escapeMilvusExpr(entryId)}" && project_id == "${escapeMilvusExpr(this.projectId)}" && tenant_id == "${escapeMilvusExpr(this.tenantId)}"`,
338
+ });
339
+ const deleted = Number(result.delete_cnt ?? 0);
340
+ return deleted > 0;
217
341
  }
218
- deleteAll(_projectId) {
219
- return 0;
342
+ async deleteAll(projectId) {
343
+ if (projectId !== this.projectId)
344
+ return 0;
345
+ const client = await getSpotlightMilvusClient();
346
+ if (!client)
347
+ return 0;
348
+ const result = await client.delete({
349
+ collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
350
+ filter: `project_id == "${escapeMilvusExpr(projectId)}" && tenant_id == "${escapeMilvusExpr(this.tenantId)}"`,
351
+ });
352
+ return Number(result.delete_cnt ?? 0);
220
353
  }
221
- count(projectId) {
222
- void projectId;
223
- return 0;
354
+ async count(projectId) {
355
+ if (projectId && projectId !== this.projectId)
356
+ return 0;
357
+ const client = await getSpotlightMilvusClient();
358
+ if (!client)
359
+ return 0;
360
+ const filter = buildMilvusSemanticFilter({
361
+ projectId: this.projectId,
362
+ tenantId: this.tenantId,
363
+ now: Date.now(),
364
+ });
365
+ const stats = await client.query({
366
+ collection_name: SPOTLIGHT_MILVUS_SEMANTIC_COLLECTION,
367
+ filter,
368
+ output_fields: ["count(*)"],
369
+ });
370
+ const row = stats.data?.[0];
371
+ return Number(row?.["count(*)"] ?? 0);
224
372
  }
225
373
  }
226
- function escapeMilvusExpr(value) {
227
- return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
228
- }
@@ -10,11 +10,15 @@ export interface PackMemoryPaths {
10
10
  }
11
11
  /** Resolve pack-scoped memory directories (mirrors assets.jsonl layout). */
12
12
  export declare function resolvePackMemoryDir(packsRoot: string, projectId: string): PackMemoryPaths;
13
+ import type { ExactMemoryBackend, SemanticMemoryBackend } from "./memoryBackends.js";
13
14
  export interface PackMemoryMeta {
14
15
  version: string;
15
- exactCount: number;
16
- semanticCount: number;
16
+ exactCount?: number;
17
+ semanticCount?: number;
17
18
  updatedAt: string;
19
+ exactBackend?: ExactMemoryBackend;
20
+ semanticBackend?: SemanticMemoryBackend;
21
+ countsAvailable?: boolean;
18
22
  }
19
23
  export declare function defaultPackMemoryMeta(): PackMemoryMeta;
20
24
  export type { SpotlightMemoryEntry };
@@ -1 +1 @@
1
- {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/node/paths.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEzE,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,4EAA4E;AAC5E,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,eAAe,CAYjB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,qBAAqB,IAAI,cAAc,CAOtD;AAED,YAAY,EAAE,oBAAoB,EAAE,CAAC"}
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../../src/node/paths.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEzE,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,4EAA4E;AAC5E,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,eAAe,CAYjB;AAED,OAAO,KAAK,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAErF,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,eAAe,CAAC,EAAE,qBAAqB,CAAC;IACxC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAgB,qBAAqB,IAAI,cAAc,CAQtD;AAED,YAAY,EAAE,oBAAoB,EAAE,CAAC"}
@@ -15,9 +15,10 @@ export function resolvePackMemoryDir(packsRoot, projectId) {
15
15
  }
16
16
  export function defaultPackMemoryMeta() {
17
17
  return {
18
- version: "1",
18
+ version: "2",
19
19
  exactCount: 0,
20
20
  semanticCount: 0,
21
21
  updatedAt: new Date().toISOString(),
22
+ countsAvailable: true,
22
23
  };
23
24
  }
@@ -16,8 +16,9 @@ export declare class RedisExactMemoryStore {
16
16
  hasExact(cacheKey: string): Promise<boolean>;
17
17
  putExact(cacheKey: string, entry: SpotlightMemoryEntry): Promise<void>;
18
18
  touch(entryId: string): Promise<void>;
19
- listEntries(limit?: number): SpotlightMemoryEntry[];
20
- deleteEntry(_entryId: string): boolean;
21
- deleteAll(_projectId: string): number;
19
+ listEntries(limit?: number): Promise<SpotlightMemoryEntry[]>;
20
+ deleteEntry(entryId: string): Promise<boolean>;
21
+ deleteAll(projectId: string): Promise<number>;
22
+ count(): Promise<number>;
22
23
  }
23
24
  //# sourceMappingURL=redisExactStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"redisExactStore.d.ts","sourceRoot":"","sources":["../../src/node/redisExactStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAOzE,MAAM,WAAW,4BAA4B;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAgBD,kEAAkE;AAClE,qBAAa,qBAAqB;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;gBAEpB,OAAO,EAAE,4BAA4B;IAMjD,MAAM,CAAC,WAAW,IAAI,OAAO;IAI7B,OAAO,CAAC,MAAM;IAKR,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAC;IAoBhE,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ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