@inupedia/spotlight-memory 0.1.6 → 0.1.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.
@@ -1,9 +1,12 @@
1
1
  export declare function buildCacheKey(params: {
2
2
  projectId: string;
3
+ scope?: "project" | "session";
4
+ sessionId?: string | null;
3
5
  questionNorm: string;
4
6
  invalidation: {
5
7
  assetsVersion?: string | null;
6
8
  catalogVersion?: string | null;
9
+ knowledgeIndexVersion?: string | null;
7
10
  };
8
11
  }): string;
9
12
  export declare function createMemoryEntryId(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"cacheKey.d.ts","sourceRoot":"","sources":["../../src/node/cacheKey.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,CAAC,MAAM,EAAE;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE;QACZ,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KAChC,CAAC;CACH,GAAG,MAAM,CAQT;AAED,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C"}
1
+ {"version":3,"file":"cacheKey.d.ts","sourceRoot":"","sources":["../../src/node/cacheKey.ts"],"names":[],"mappings":"AAEA,wBAAgB,aAAa,CAAC,MAAM,EAAE;IACpC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAC9B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE;QACZ,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC/B,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;KACvC,CAAC;CACH,GAAG,MAAM,CAYT;AAED,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C"}
@@ -1,10 +1,14 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
2
  export function buildCacheKey(params) {
3
+ const scope = params.scope ?? "project";
3
4
  const payload = [
4
5
  params.projectId,
6
+ scope,
7
+ scope === "session" ? (params.sessionId ?? "") : "",
5
8
  params.questionNorm,
6
9
  params.invalidation.assetsVersion ?? "",
7
10
  params.invalidation.catalogVersion ?? "",
11
+ params.invalidation.knowledgeIndexVersion ?? "",
8
12
  ].join("\0");
9
13
  return createHash("sha256").update(payload).digest("hex");
10
14
  }
@@ -1 +1 @@
1
- {"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../../src/node/gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAC/B,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAChC,MAAM,8BAA8B,CAAC;AAOtC,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,8BAA8B,EAAE,oBAAoB,GAAG,IAAI,CAAC,CAAC;IACxG,YAAY,CACV,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,KAAK,CAAC,EAAE,MAAM,GACb,OAAO,CACR,KAAK,CAAC;QACJ,KAAK,EAAE,OAAO,8BAA8B,EAAE,oBAAoB,CAAC;QACnE,KAAK,EAAE,MAAM,CAAC;KACf,CAAC,CACH,CAAC;IACF,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CACN,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,OAAO,8BAA8B,EAAE,oBAAoB,GACjE,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,WAAW,CACT,KAAK,EAAE,OAAO,8BAA8B,EAAE,oBAAoB,GACjE,OAAO,CAAC,I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AAM,WAAW,iBAAiB;IAChC,QAAQ,CACN,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,OAAO,8BAA8B,EAAE,oBAAoB,GACjE,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,WAAW,CACT,KAAK,EAAE,OAAO,8BAA8B,EAAE,oBAAoB,GACjE,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9C;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,MAAM,WAAW,UAAU;IACzB,MAAM,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAAC;IAC9E,KAAK,CAAC,KAAK,EAAE,yBAAyB,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;CAC9E;AAkDD,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,iBAAiB,EACzB,MAAM,EAAE,iBAAiB,EACzB,MAAM,GAAE,gBAAqB,GAC5B,UAAU,CA4IZ"}
package/dist/node/gate.js CHANGED
@@ -2,8 +2,33 @@ import { SPOTLIGHT_MEMORY_DEFAULT_TTL_SEC, } from "@inupedia/spotlight-protocol"
2
2
  import { classifyMemoryKind, isMemoryKindAllowedForRead } from "../classify.js";
3
3
  import { normalizeQuestion } from "../normalize.js";
4
4
  import { isMemoryEntryStale } from "../stale.js";
5
- import { clampMemoryTtlSec } from "../ttl.js";
6
5
  import { buildCacheKey, createMemoryEntryId } from "./cacheKey.js";
6
+ function resolveWriteScope(input) {
7
+ if (input.scope === "project")
8
+ return { scope: "project" };
9
+ if (input.scope === "session" || input.sessionId?.trim()) {
10
+ const sessionId = input.sessionId?.trim();
11
+ return sessionId ? { scope: "session", sessionId } : { scope: "project" };
12
+ }
13
+ return { scope: "project" };
14
+ }
15
+ function resolveLookupScopes(input) {
16
+ const sessionId = input.sessionId?.trim();
17
+ if (input.scope === "project" || !sessionId)
18
+ return [{ scope: "project" }];
19
+ return [{ scope: "session", sessionId }, { scope: "project" }];
20
+ }
21
+ function entryMatchesLookupScope(entry, scopes) {
22
+ const scopedEntry = entry;
23
+ const entryScope = scopedEntry.scope ?? "project";
24
+ return scopes.some((scope) => {
25
+ if (scope.scope !== entryScope)
26
+ return false;
27
+ if (entryScope === "session")
28
+ return scopedEntry.sessionId === scope.sessionId;
29
+ return true;
30
+ });
31
+ }
7
32
  export function createMemoryGate(reader, writer, config = {}) {
8
33
  const enabled = config.enabled ?? true;
9
34
  const semanticThreshold = config.semanticThreshold ?? 0.92;
@@ -21,32 +46,36 @@ export function createMemoryGate(reader, writer, config = {}) {
21
46
  const questionNorm = normalizeQuestion(input.question);
22
47
  if (!questionNorm)
23
48
  return finishMiss("not_found");
24
- const cacheKey = buildCacheKey({
25
- projectId: input.projectId,
26
- questionNorm,
27
- invalidation: input.invalidation,
28
- });
29
- const exact = await reader.getExact(cacheKey);
30
- if (exact) {
31
- if (isMemoryEntryStale(exact, input.invalidation)) {
32
- return finishMiss("stale");
33
- }
34
- if (!isMemoryKindAllowedForRead(exact.kind)) {
35
- return finishMiss("kind_blocked");
49
+ const lookupScopes = resolveLookupScopes(input);
50
+ for (const scope of lookupScopes) {
51
+ const cacheKey = buildCacheKey({
52
+ projectId: input.projectId,
53
+ ...scope,
54
+ questionNorm,
55
+ invalidation: input.invalidation,
56
+ });
57
+ const exact = await reader.getExact(cacheKey);
58
+ if (exact) {
59
+ if (isMemoryEntryStale(exact, input.invalidation)) {
60
+ return finishMiss("stale");
61
+ }
62
+ if (!isMemoryKindAllowedForRead(exact.kind)) {
63
+ return finishMiss("kind_blocked");
64
+ }
65
+ await reader.touch(exact.id);
66
+ return {
67
+ hit: true,
68
+ result: {
69
+ source: scope.scope === "session" ? "session" : "exact",
70
+ entry: exact,
71
+ confidence: 1,
72
+ lookupLatencyMs: performance.now() - started,
73
+ },
74
+ };
36
75
  }
37
- await reader.touch(exact.id);
38
- return {
39
- hit: true,
40
- result: {
41
- source: "exact",
42
- entry: exact,
43
- confidence: 1,
44
- lookupLatencyMs: performance.now() - started,
45
- },
46
- };
47
76
  }
48
77
  if (!input.exactOnly && semanticEnabled) {
49
- const semantic = await reader.findSemantic(input.projectId, questionNorm, 5);
78
+ const semantic = (await reader.findSemantic(input.projectId, questionNorm, 20)).filter((candidate) => entryMatchesLookupScope(candidate.entry, lookupScopes));
50
79
  const top = semantic.find((c) => c.score >= semanticThreshold);
51
80
  if (top) {
52
81
  if (isMemoryEntryStale(top.entry, input.invalidation)) {
@@ -89,8 +118,11 @@ export function createMemoryGate(reader, writer, config = {}) {
89
118
  return { written: false, skippedReason: "kind_blocked" };
90
119
  }
91
120
  const questionNorm = normalizeQuestion(input.question);
121
+ const scopedInput = input;
122
+ const scope = resolveWriteScope(scopedInput);
92
123
  const cacheKey = buildCacheKey({
93
124
  projectId: input.projectId,
125
+ ...scope,
94
126
  questionNorm,
95
127
  invalidation: input.invalidation,
96
128
  });
@@ -100,13 +132,15 @@ export function createMemoryGate(reader, writer, config = {}) {
100
132
  const entry = {
101
133
  id: createMemoryEntryId(),
102
134
  projectId: input.projectId,
135
+ scope: scope.scope,
136
+ sessionId: scope.sessionId,
103
137
  questionNorm,
104
138
  questionRaw: input.question,
105
139
  kind,
106
140
  answer: input.answer,
107
141
  plan: input.plan,
108
142
  invalidation: input.invalidation,
109
- ttlSec: clampMemoryTtlSec(input.ttlSec ?? SPOTLIGHT_MEMORY_DEFAULT_TTL_SEC[kind]),
143
+ ttlSec: input.ttlSec ?? SPOTLIGHT_MEMORY_DEFAULT_TTL_SEC[kind],
110
144
  createdAt: Date.now(),
111
145
  hitCount: 0,
112
146
  confidence: input.confidence,
@@ -1 +1 @@
1
- {"version":3,"file":"stale.d.ts","sourceRoot":"","sources":["../src/stale.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,kCAAkC,EACnC,MAAM,8BAA8B,CAAC;AAEtC,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,oBAAoB,EAC3B,GAAG,EAAE,kCAAkC,EACvC,GAAG,SAAa,GACf,OAAO,CAqBT;AAED,wBAAgB,eAAe,CAAC,CAAC,SAAS;IAAE,KAAK,EAAE,oBAAoB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EACtF,UAAU,EAAE,CAAC,EAAE,EACf,SAAS,EAAE,MAAM,GAChB,CAAC,GAAG,IAAI,CAQV"}
1
+ {"version":3,"file":"stale.d.ts","sourceRoot":"","sources":["../src/stale.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,kCAAkC,EACnC,MAAM,8BAA8B,CAAC;AAEtC,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,oBAAoB,EAC3B,GAAG,EAAE,kCAAkC,EACvC,GAAG,SAAa,GACf,OAAO,CA6BT;AAED,wBAAgB,eAAe,CAAC,CAAC,SAAS;IAAE,KAAK,EAAE,oBAAoB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EACtF,UAAU,EAAE,CAAC,EAAE,EACf,SAAS,EAAE,MAAM,GAChB,CAAC,GAAG,IAAI,CAQV"}
package/dist/stale.js CHANGED
@@ -8,11 +8,17 @@ export function isMemoryEntryStale(entry, ctx, now = Date.now()) {
8
8
  const catalogChanged = entry.invalidation.catalogVersion != null &&
9
9
  ctx.catalogVersion != null &&
10
10
  entry.invalidation.catalogVersion !== ctx.catalogVersion;
11
+ const knowledgeChanged = entry.invalidation.knowledgeIndexVersion != null &&
12
+ ctx.knowledgeIndexVersion != null &&
13
+ entry.invalidation.knowledgeIndexVersion !== ctx.knowledgeIndexVersion;
11
14
  if (assetsChanged && entry.kind === "data_snapshot")
12
15
  return true;
13
16
  if (catalogChanged && (entry.kind === "action_plan" || entry.kind === "routing_hint")) {
14
17
  return true;
15
18
  }
19
+ if (knowledgeChanged && (entry.kind === "qa_answer" || entry.kind === "routing_hint")) {
20
+ return true;
21
+ }
16
22
  return false;
17
23
  }
18
24
  export function pickSemanticHit(candidates, threshold) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inupedia/spotlight-memory",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Memory Gate, exact/semantic cache stores for Inupedia Spotlight",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "dist"
25
25
  ],
26
26
  "dependencies": {
27
- "@inupedia/spotlight-protocol": "0.1.6"
27
+ "@inupedia/spotlight-protocol": "0.1.7"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^22.10.0",