@inupedia/spotlight-memory 0.1.5 → 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.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/node/cacheKey.d.ts +3 -0
- package/dist/node/cacheKey.d.ts.map +1 -1
- package/dist/node/cacheKey.js +4 -0
- package/dist/node/gate.d.ts.map +1 -1
- package/dist/node/gate.js +58 -23
- package/dist/stale.d.ts.map +1 -1
- package/dist/stale.js +6 -0
- package/dist/ttl.d.ts +4 -0
- package/dist/ttl.d.ts.map +1 -0
- package/dist/ttl.js +7 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { classifyMemoryKind, isMemoryKindAllowedForRead, type ClassifyMemoryKindInput, type ClassifyMemoryKindResult, } from "./classify.js";
|
|
2
2
|
export { normalizeQuestion, scoreQuestionSimilarity, } from "./normalize.js";
|
|
3
3
|
export { isMemoryEntryStale, pickSemanticHit } from "./stale.js";
|
|
4
|
+
export { clampMemoryTtlSec, MEMORY_TTL_MAX_SEC } from "./ttl.js";
|
|
4
5
|
export { cosineSimilarity } from "./similarity.js";
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,wBAAwB,GAC9B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,wBAAwB,GAC9B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { classifyMemoryKind, isMemoryKindAllowedForRead, } from "./classify.js";
|
|
2
2
|
export { normalizeQuestion, scoreQuestionSimilarity, } from "./normalize.js";
|
|
3
3
|
export { isMemoryEntryStale, pickSemanticHit } from "./stale.js";
|
|
4
|
+
export { clampMemoryTtlSec, MEMORY_TTL_MAX_SEC } from "./ttl.js";
|
|
4
5
|
export { cosineSimilarity } from "./similarity.js";
|
package/dist/node/cacheKey.d.ts
CHANGED
|
@@ -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;
|
|
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"}
|
package/dist/node/cacheKey.js
CHANGED
|
@@ -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
|
}
|
package/dist/node/gate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../../src/node/gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,yBAAyB,
|
|
1
|
+
{"version":3,"file":"gate.d.ts","sourceRoot":"","sources":["../../src/node/gate.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,yBAAyB,EAE9B,KAAK,0BAA0B,EAC/B,KAAK,yBAAyB,EAC9B,KAAK,0BAA0B,EAChC,MAAM,8BAA8B,CAAC;AAMtC,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,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
|
@@ -3,6 +3,32 @@ import { classifyMemoryKind, isMemoryKindAllowedForRead } from "../classify.js";
|
|
|
3
3
|
import { normalizeQuestion } from "../normalize.js";
|
|
4
4
|
import { isMemoryEntryStale } from "../stale.js";
|
|
5
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
|
+
}
|
|
6
32
|
export function createMemoryGate(reader, writer, config = {}) {
|
|
7
33
|
const enabled = config.enabled ?? true;
|
|
8
34
|
const semanticThreshold = config.semanticThreshold ?? 0.92;
|
|
@@ -20,32 +46,36 @@ export function createMemoryGate(reader, writer, config = {}) {
|
|
|
20
46
|
const questionNorm = normalizeQuestion(input.question);
|
|
21
47
|
if (!questionNorm)
|
|
22
48
|
return finishMiss("not_found");
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
+
};
|
|
35
75
|
}
|
|
36
|
-
await reader.touch(exact.id);
|
|
37
|
-
return {
|
|
38
|
-
hit: true,
|
|
39
|
-
result: {
|
|
40
|
-
source: "exact",
|
|
41
|
-
entry: exact,
|
|
42
|
-
confidence: 1,
|
|
43
|
-
lookupLatencyMs: performance.now() - started,
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
76
|
}
|
|
47
77
|
if (!input.exactOnly && semanticEnabled) {
|
|
48
|
-
const semantic = await reader.findSemantic(input.projectId, questionNorm,
|
|
78
|
+
const semantic = (await reader.findSemantic(input.projectId, questionNorm, 20)).filter((candidate) => entryMatchesLookupScope(candidate.entry, lookupScopes));
|
|
49
79
|
const top = semantic.find((c) => c.score >= semanticThreshold);
|
|
50
80
|
if (top) {
|
|
51
81
|
if (isMemoryEntryStale(top.entry, input.invalidation)) {
|
|
@@ -88,8 +118,11 @@ export function createMemoryGate(reader, writer, config = {}) {
|
|
|
88
118
|
return { written: false, skippedReason: "kind_blocked" };
|
|
89
119
|
}
|
|
90
120
|
const questionNorm = normalizeQuestion(input.question);
|
|
121
|
+
const scopedInput = input;
|
|
122
|
+
const scope = resolveWriteScope(scopedInput);
|
|
91
123
|
const cacheKey = buildCacheKey({
|
|
92
124
|
projectId: input.projectId,
|
|
125
|
+
...scope,
|
|
93
126
|
questionNorm,
|
|
94
127
|
invalidation: input.invalidation,
|
|
95
128
|
});
|
|
@@ -99,6 +132,8 @@ export function createMemoryGate(reader, writer, config = {}) {
|
|
|
99
132
|
const entry = {
|
|
100
133
|
id: createMemoryEntryId(),
|
|
101
134
|
projectId: input.projectId,
|
|
135
|
+
scope: scope.scope,
|
|
136
|
+
sessionId: scope.sessionId,
|
|
102
137
|
questionNorm,
|
|
103
138
|
questionRaw: input.question,
|
|
104
139
|
kind,
|
package/dist/stale.d.ts.map
CHANGED
|
@@ -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,
|
|
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/dist/ttl.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ttl.d.ts","sourceRoot":"","sources":["../src/ttl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAEzC,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAGxD"}
|
package/dist/ttl.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Hard upper bound for any memory entry TTL (24 hours). */
|
|
2
|
+
export const MEMORY_TTL_MAX_SEC = 86_400;
|
|
3
|
+
export function clampMemoryTtlSec(ttlSec) {
|
|
4
|
+
if (!Number.isFinite(ttlSec))
|
|
5
|
+
return 0;
|
|
6
|
+
return Math.max(0, Math.min(Math.floor(ttlSec), MEMORY_TTL_MAX_SEC));
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inupedia/spotlight-memory",
|
|
3
|
-
"version": "0.1.
|
|
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.
|
|
27
|
+
"@inupedia/spotlight-protocol": "0.1.7"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "^22.10.0",
|