@anuma/sdk 1.0.0 → 1.1.0
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/README.md +54 -53
- package/dist/api/spec.cjs +30 -0
- package/dist/api/spec.d.mts +3 -0
- package/dist/api/spec.d.ts +3 -0
- package/dist/api/spec.mjs +12 -0
- package/dist/chunk-4ETHWSXJ.mjs +25 -0
- package/dist/client/index.cjs +2344 -0
- package/dist/client/index.d.mts +10368 -0
- package/dist/client/index.d.ts +10368 -0
- package/dist/client/index.mjs +2168 -0
- package/dist/clientConfig-WITO4T6L.mjs +12 -0
- package/dist/constants/index.cjs +30 -0
- package/dist/constants/index.d.mts +20 -0
- package/dist/constants/index.d.ts +20 -0
- package/dist/constants/index.mjs +5 -0
- package/dist/design/index.cjs +1523 -0
- package/dist/design/index.d.mts +561 -0
- package/dist/design/index.d.ts +561 -0
- package/dist/design/index.mjs +1468 -0
- package/dist/expo/chunk-DFVCS3QQ.mjs +246 -0
- package/dist/{react/chunk-MJJIYFAX.mjs → expo/chunk-GYKJOWEO.mjs} +13 -1
- package/dist/expo/chunk-PUKP5H4F.mjs +5278 -0
- package/dist/expo/chunk-Y7XT2VNQ.mjs +646 -0
- package/dist/expo/clientConfig-VPOGQRNP.mjs +12 -0
- package/dist/expo/consolidate-HEMQGOUD.mjs +131 -0
- package/dist/expo/index.cjs +16784 -6689
- package/dist/expo/index.d.mts +5746 -1474
- package/dist/expo/index.d.ts +5746 -1474
- package/dist/expo/index.mjs +9669 -6189
- package/dist/expo/recall-YRWAAU6K.mjs +8 -0
- package/dist/index.cjs +2992 -42
- package/dist/index.d.mts +7719 -1835
- package/dist/index.d.ts +7719 -1835
- package/dist/index.mjs +2859 -39
- package/dist/next/index.cjs +6 -2
- package/dist/next/index.d.mts +24 -1
- package/dist/next/index.d.ts +24 -1
- package/dist/next/index.mjs +6 -2
- package/dist/pii/transformers.cjs +126 -0
- package/dist/pii/transformers.d.mts +103 -0
- package/dist/pii/transformers.d.ts +103 -0
- package/dist/pii/transformers.mjs +90 -0
- package/dist/react/chunk-DFVCS3QQ.mjs +246 -0
- package/dist/{expo/chunk-MJJIYFAX.mjs → react/chunk-GYKJOWEO.mjs} +13 -1
- package/dist/react/chunk-Q2ANFWEP.mjs +5596 -0
- package/dist/react/chunk-TP6CJZ25.mjs +645 -0
- package/dist/react/clientConfig-VPOGQRNP.mjs +12 -0
- package/dist/react/consolidate-IJH7LD2T.mjs +131 -0
- package/dist/react/index.cjs +25453 -11029
- package/dist/react/index.d.mts +7388 -1406
- package/dist/react/index.d.ts +7388 -1406
- package/dist/react/index.mjs +16101 -8648
- package/dist/react/recall-YBC7YCNC.mjs +8 -0
- package/dist/server/chunk-FMGZTVXQ.mjs +62 -0
- package/dist/server/chunk-HPURS22B.mjs +19452 -0
- package/dist/server/chunk-PXN5Q3MD.mjs +643 -0
- package/dist/server/chunk-RCCKRTBI.mjs +230 -0
- package/dist/server/clientConfig-GMADOBPK.mjs +12 -0
- package/dist/server/consolidate-A2ZIX5WF.mjs +131 -0
- package/dist/server/index.cjs +32109 -0
- package/dist/server/index.d.mts +7310 -0
- package/dist/server/index.d.ts +7310 -0
- package/dist/server/index.mjs +11168 -0
- package/dist/server/recall-PFYFVYMS.mjs +8 -0
- package/dist/tools/index.cjs +12682 -34
- package/dist/tools/index.d.mts +2733 -18
- package/dist/tools/index.d.ts +2733 -18
- package/dist/tools/index.mjs +12565 -33
- package/dist/utils/index.cjs +165 -0
- package/dist/utils/index.d.mts +14 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.mjs +145 -0
- package/dist/vercel/index.d.mts +0 -3
- package/dist/vercel/index.d.ts +0 -3
- package/package.json +127 -16
- package/dist/expo/chunk-LJYAMK62.mjs +0 -25
- package/dist/expo/chunk-PJCZO4BQ.mjs +0 -25
- package/dist/expo/clientConfig-2MI4KULF.mjs +0 -10
- package/dist/expo/clientConfig-QDAXFL3W.mjs +0 -10
- package/dist/expo/clientConfig-R4IOW7I2.mjs +0 -10
- package/dist/react/chunk-LJYAMK62.mjs +0 -25
- package/dist/react/chunk-PJCZO4BQ.mjs +0 -25
- package/dist/react/clientConfig-2MI4KULF.mjs +0 -10
- package/dist/react/clientConfig-QDAXFL3W.mjs +0 -10
- package/dist/react/clientConfig-R4IOW7I2.mjs +0 -10
- package/dist/tools/chunk-KDFGY4SK.mjs +0 -13
- package/dist/tools/clientConfig-RMDOT5YM.mjs +0 -10
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolvePiiRedactor
|
|
3
|
+
} from "./chunk-TP6CJZ25.mjs";
|
|
4
|
+
import {
|
|
5
|
+
callPortalJsonCompletion,
|
|
6
|
+
getLogger
|
|
7
|
+
} from "./chunk-DFVCS3QQ.mjs";
|
|
8
|
+
import "./chunk-GYKJOWEO.mjs";
|
|
9
|
+
|
|
10
|
+
// src/lib/memory/consolidate.ts
|
|
11
|
+
var DEFAULT_MODEL = "inclusionai/ling-2.6-flash";
|
|
12
|
+
var DEFAULT_CONSOLIDATE_ATTEMPTS = 3;
|
|
13
|
+
var DEFAULT_CONSOLIDATE_TOTAL_TIMEOUT_MS = 2e4;
|
|
14
|
+
var SYSTEM_PROMPT = `You consolidate a new memory against existing memories from the same user.
|
|
15
|
+
|
|
16
|
+
A "memory" is a self-contained natural-language fact about the user. Multiple memories about the same EXACT FACET (the same dimension of the same subject) should never coexist \u2014 they should be merged into one. Different facets on the same subject (e.g. user's dog's name vs user's dog's age) are SEPARATE memories.
|
|
17
|
+
|
|
18
|
+
Decide one of three actions:
|
|
19
|
+
|
|
20
|
+
- "create": the new memory describes a distinct fact not covered by any existing memory. Write it as new.
|
|
21
|
+
- "update": the new memory describes the SAME FACET as exactly one existing memory. Either it adds detail, corrects a value, or rephrases the same fact. Return targetId of that existing memory + the consolidated content (richest version of the two).
|
|
22
|
+
- "noop": the new memory is already fully captured by an existing memory \u2014 same facet, no new information. Skip the write.
|
|
23
|
+
|
|
24
|
+
RULES (carry these strictly):
|
|
25
|
+
|
|
26
|
+
1. ONE OBSERVATION PER DISTINCT FACET. If the new memory is about "user's aunt's twins" and an existing memory is also about "user's aunt's twins", they are the same facet \u2192 update, never create.
|
|
27
|
+
2. MATCH BY FACET, NOT TOPIC. Two memories both mentioning "Zara" is not enough to merge \u2014 only merge if they describe the same property (e.g. both about "user's pending Zara boot exchange"). Two memories about Zara on different topics (e.g. "user shops at Zara" + "user returned a sweater to Zara last week") are separate facets.
|
|
28
|
+
3. NO COMPUTATION. Do not sum counts, decrement quantities, or derive new facts from existing ones. If the new memory says "user spent $200 today" and an existing memory says "user spent $150 yesterday", these are TWO SEPARATE EVENTS \u2014 create.
|
|
29
|
+
4. Events with different occurredAt dates are different memories even if the same activity ("user went to gym Monday" vs "user went to gym Friday" \u2014 separate events).
|
|
30
|
+
5. When in doubt between create and update, choose create. Over-merging loses information; under-merging is recoverable.
|
|
31
|
+
|
|
32
|
+
OUTPUT \u2014 strict JSON, no prose:
|
|
33
|
+
{
|
|
34
|
+
"action": "create" | "update" | "noop",
|
|
35
|
+
"targetId": "<existing memory id, required for update/noop, omit for create>",
|
|
36
|
+
"content": "<consolidated content, required for create/update, omit for noop>"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
For "create": content is the new memory verbatim (or a slight refinement).
|
|
40
|
+
For "update": content is the merged richest-version, \u226480 words.
|
|
41
|
+
For "noop": no content (existing memory is already correct).`;
|
|
42
|
+
async function consolidateMemory(newContent, candidates, options) {
|
|
43
|
+
const fallback = {
|
|
44
|
+
action: "create",
|
|
45
|
+
content: newContent
|
|
46
|
+
};
|
|
47
|
+
const trimmed = newContent.trim();
|
|
48
|
+
if (trimmed.length === 0) return fallback;
|
|
49
|
+
if (candidates.length === 0) return fallback;
|
|
50
|
+
const redactor = resolvePiiRedactor(options.piiRedaction);
|
|
51
|
+
const safeTrimmed = redactor ? redactor.redactText(trimmed).text : trimmed;
|
|
52
|
+
const candidateText = candidates.map((c, i) => {
|
|
53
|
+
const safeContent = redactor ? redactor.redactText(c.content).text : c.content;
|
|
54
|
+
return `[${i + 1}] (id: ${c.id}, sim: ${c.similarity.toFixed(2)})
|
|
55
|
+
${safeContent}`;
|
|
56
|
+
}).join("\n");
|
|
57
|
+
const userMessage = `New memory:
|
|
58
|
+
${safeTrimmed}
|
|
59
|
+
|
|
60
|
+
Existing memories (top ${candidates.length} by cosine):
|
|
61
|
+
${candidateText}`;
|
|
62
|
+
let parsed;
|
|
63
|
+
try {
|
|
64
|
+
parsed = await callPortalJsonCompletion({
|
|
65
|
+
...options.apiKey !== void 0 && { apiKey: options.apiKey },
|
|
66
|
+
...options.getToken !== void 0 && { getToken: options.getToken },
|
|
67
|
+
...options.baseUrl !== void 0 && { baseUrl: options.baseUrl },
|
|
68
|
+
model: options.model ?? DEFAULT_MODEL,
|
|
69
|
+
systemPrompt: SYSTEM_PROMPT,
|
|
70
|
+
userMessage,
|
|
71
|
+
tag: "memory/consolidate",
|
|
72
|
+
// Retry TRANSIENT failures only (network/timeout/5xx/429/empty) before
|
|
73
|
+
// degrading to create — a transient blip would otherwise leave a
|
|
74
|
+
// permanent below-floor paraphrase that never self-heals (see
|
|
75
|
+
// DEFAULT_CONSOLIDATE_ATTEMPTS). The happy path still resolves in one
|
|
76
|
+
// attempt; terminal failures (400/auth) and schema violations never
|
|
77
|
+
// retry. A total budget keeps a hanging portal from stalling retain.
|
|
78
|
+
maxAttempts: options.maxAttempts ?? DEFAULT_CONSOLIDATE_ATTEMPTS,
|
|
79
|
+
totalTimeoutMs: options.totalTimeoutMs ?? DEFAULT_CONSOLIDATE_TOTAL_TIMEOUT_MS,
|
|
80
|
+
...options.backoffMs && { backoffMs: options.backoffMs },
|
|
81
|
+
...options.fetchFn && { fetchFn: options.fetchFn }
|
|
82
|
+
});
|
|
83
|
+
} catch (err) {
|
|
84
|
+
return degrade("llm_error", fallback, options, err);
|
|
85
|
+
}
|
|
86
|
+
if (parsed === null) return degrade("llm_error", fallback, options);
|
|
87
|
+
const validIds = new Set(candidates.map((c) => c.id));
|
|
88
|
+
const result = validate(parsed, trimmed, validIds);
|
|
89
|
+
if (!result) return degrade("invalid_response", fallback, options);
|
|
90
|
+
if (redactor && result.content !== void 0) {
|
|
91
|
+
const restored = redactor.restoreForStorage(result.content);
|
|
92
|
+
if (restored.unresolved) {
|
|
93
|
+
return degrade("invalid_response", fallback, options);
|
|
94
|
+
}
|
|
95
|
+
return { ...result, content: restored.text };
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
function degrade(reason, fallback, options, detail) {
|
|
100
|
+
if (detail !== void 0) {
|
|
101
|
+
getLogger().warn(`memory/consolidate: degraded to create (${reason})`, detail);
|
|
102
|
+
} else {
|
|
103
|
+
getLogger().warn(`memory/consolidate: degraded to create (${reason})`);
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
options.onFallback?.(reason);
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
return { ...fallback, fallbackReason: reason };
|
|
110
|
+
}
|
|
111
|
+
function validate(parsed, newContent, validIds) {
|
|
112
|
+
if (typeof parsed !== "object" || parsed === null) return null;
|
|
113
|
+
const obj = parsed;
|
|
114
|
+
const action = obj.action;
|
|
115
|
+
if (action !== "create" && action !== "update" && action !== "noop") return null;
|
|
116
|
+
if (action === "create") {
|
|
117
|
+
const c2 = typeof obj.content === "string" ? obj.content.trim() : "";
|
|
118
|
+
return { action: "create", content: c2.length > 0 ? c2 : newContent };
|
|
119
|
+
}
|
|
120
|
+
const targetId = typeof obj.targetId === "string" ? obj.targetId : null;
|
|
121
|
+
if (!targetId || !validIds.has(targetId)) return null;
|
|
122
|
+
if (action === "noop") {
|
|
123
|
+
return { action: "noop", targetId };
|
|
124
|
+
}
|
|
125
|
+
const c = typeof obj.content === "string" ? obj.content.trim() : "";
|
|
126
|
+
if (c.length === 0) return null;
|
|
127
|
+
return { action: "update", targetId, content: c };
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
consolidateMemory
|
|
131
|
+
};
|