@agentmemory/agentmemory 0.7.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/.claude-plugin/marketplace.json +14 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/publish.yml +28 -0
- package/AGENTS.md +113 -0
- package/LICENSE +190 -0
- package/README.md +828 -0
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +73 -0
- package/benchmark/REAL-EMBEDDINGS.md +67 -0
- package/benchmark/SCALE.md +110 -0
- package/benchmark/dataset.ts +293 -0
- package/benchmark/quality-eval.ts +643 -0
- package/benchmark/real-embeddings-eval.ts +405 -0
- package/benchmark/scale-eval.ts +398 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +137 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/docker-compose.yml +14 -0
- package/dist/hooks/notification.d.mts +1 -0
- package/dist/hooks/notification.mjs +45 -0
- package/dist/hooks/notification.mjs.map +1 -0
- package/dist/hooks/post-tool-failure.d.mts +1 -0
- package/dist/hooks/post-tool-failure.mjs +45 -0
- package/dist/hooks/post-tool-failure.mjs.map +1 -0
- package/dist/hooks/post-tool-use.d.mts +1 -0
- package/dist/hooks/post-tool-use.mjs +53 -0
- package/dist/hooks/post-tool-use.mjs.map +1 -0
- package/dist/hooks/pre-compact.d.mts +1 -0
- package/dist/hooks/pre-compact.mjs +50 -0
- package/dist/hooks/pre-compact.mjs.map +1 -0
- package/dist/hooks/pre-tool-use.d.mts +1 -0
- package/dist/hooks/pre-tool-use.mjs +69 -0
- package/dist/hooks/pre-tool-use.mjs.map +1 -0
- package/dist/hooks/prompt-submit.d.mts +1 -0
- package/dist/hooks/prompt-submit.mjs +40 -0
- package/dist/hooks/prompt-submit.mjs.map +1 -0
- package/dist/hooks/session-end.d.mts +1 -0
- package/dist/hooks/session-end.mjs +61 -0
- package/dist/hooks/session-end.mjs.map +1 -0
- package/dist/hooks/session-start.d.mts +1 -0
- package/dist/hooks/session-start.mjs +42 -0
- package/dist/hooks/session-start.mjs.map +1 -0
- package/dist/hooks/stop.d.mts +1 -0
- package/dist/hooks/stop.mjs +33 -0
- package/dist/hooks/stop.mjs.map +1 -0
- package/dist/hooks/subagent-start.d.mts +1 -0
- package/dist/hooks/subagent-start.mjs +43 -0
- package/dist/hooks/subagent-start.mjs.map +1 -0
- package/dist/hooks/subagent-stop.d.mts +1 -0
- package/dist/hooks/subagent-stop.mjs +45 -0
- package/dist/hooks/subagent-stop.mjs.map +1 -0
- package/dist/hooks/task-completed.d.mts +1 -0
- package/dist/hooks/task-completed.mjs +46 -0
- package/dist/hooks/task-completed.mjs.map +1 -0
- package/dist/iii-config.yaml +51 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +13776 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src-QxitMPfJ.mjs +13775 -0
- package/dist/src-QxitMPfJ.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -0
- package/dist/standalone.mjs +1155 -0
- package/dist/standalone.mjs.map +1 -0
- package/dist/transformers-BX_tgxdO.mjs +38684 -0
- package/dist/transformers-BX_tgxdO.mjs.map +1 -0
- package/dist/transformers-KMm1i9no.mjs +38683 -0
- package/dist/transformers-KMm1i9no.mjs.map +1 -0
- package/docker-compose.yml +14 -0
- package/iii-config.yaml +51 -0
- package/package.json +59 -0
- package/plugin/.claude-plugin/plugin.json +10 -0
- package/plugin/hooks/hooks.json +77 -0
- package/plugin/scripts/diagnostics.mjs +551 -0
- package/plugin/scripts/notification.mjs +45 -0
- package/plugin/scripts/post-tool-failure.mjs +45 -0
- package/plugin/scripts/post-tool-use.mjs +53 -0
- package/plugin/scripts/pre-compact.mjs +50 -0
- package/plugin/scripts/pre-tool-use.mjs +69 -0
- package/plugin/scripts/prompt-submit.mjs +40 -0
- package/plugin/scripts/session-end.mjs +61 -0
- package/plugin/scripts/session-start.mjs +42 -0
- package/plugin/scripts/stop.mjs +33 -0
- package/plugin/scripts/subagent-start.mjs +43 -0
- package/plugin/scripts/subagent-stop.mjs +45 -0
- package/plugin/scripts/task-completed.mjs +46 -0
- package/plugin/skills/forget/SKILL.md +32 -0
- package/plugin/skills/recall/SKILL.md +18 -0
- package/plugin/skills/remember/SKILL.md +25 -0
- package/plugin/skills/session-history/SKILL.md +17 -0
- package/src/auth.ts +12 -0
- package/src/cli.ts +159 -0
- package/src/config.ts +221 -0
- package/src/eval/metrics-store.ts +65 -0
- package/src/eval/quality.ts +51 -0
- package/src/eval/schemas.ts +124 -0
- package/src/eval/self-correct.ts +28 -0
- package/src/eval/validator.ts +31 -0
- package/src/functions/actions.ts +288 -0
- package/src/functions/audit.ts +61 -0
- package/src/functions/auto-forget.ts +169 -0
- package/src/functions/branch-aware.ts +169 -0
- package/src/functions/cascade.ts +80 -0
- package/src/functions/checkpoints.ts +209 -0
- package/src/functions/claude-bridge.ts +161 -0
- package/src/functions/compress.ts +194 -0
- package/src/functions/consolidate.ts +212 -0
- package/src/functions/consolidation-pipeline.ts +258 -0
- package/src/functions/context.ts +169 -0
- package/src/functions/crystallize.ts +293 -0
- package/src/functions/dedup.ts +57 -0
- package/src/functions/diagnostics.ts +785 -0
- package/src/functions/enrich.ts +132 -0
- package/src/functions/evict.ts +163 -0
- package/src/functions/export-import.ts +508 -0
- package/src/functions/facets.ts +248 -0
- package/src/functions/file-index.ts +106 -0
- package/src/functions/flow-compress.ts +214 -0
- package/src/functions/frontier.ts +196 -0
- package/src/functions/governance.ts +131 -0
- package/src/functions/graph-retrieval.ts +277 -0
- package/src/functions/graph.ts +275 -0
- package/src/functions/leases.ts +216 -0
- package/src/functions/lessons.ts +253 -0
- package/src/functions/mesh.ts +434 -0
- package/src/functions/migrate.ts +165 -0
- package/src/functions/observe.ts +144 -0
- package/src/functions/obsidian-export.ts +310 -0
- package/src/functions/patterns.ts +138 -0
- package/src/functions/privacy.ts +39 -0
- package/src/functions/profile.ts +155 -0
- package/src/functions/query-expansion.ts +186 -0
- package/src/functions/relations.ts +237 -0
- package/src/functions/remember.ts +162 -0
- package/src/functions/retention.ts +235 -0
- package/src/functions/routines.ts +289 -0
- package/src/functions/search.ts +80 -0
- package/src/functions/sentinels.ts +417 -0
- package/src/functions/signals.ts +186 -0
- package/src/functions/sketches.ts +274 -0
- package/src/functions/sliding-window.ts +257 -0
- package/src/functions/smart-search.ts +115 -0
- package/src/functions/snapshot.ts +219 -0
- package/src/functions/summarize.ts +155 -0
- package/src/functions/team.ts +147 -0
- package/src/functions/temporal-graph.ts +476 -0
- package/src/functions/timeline.ts +138 -0
- package/src/functions/verify.ts +117 -0
- package/src/health/monitor.ts +110 -0
- package/src/health/thresholds.ts +73 -0
- package/src/hooks/notification.ts +52 -0
- package/src/hooks/post-tool-failure.ts +58 -0
- package/src/hooks/post-tool-use.ts +62 -0
- package/src/hooks/pre-compact.ts +60 -0
- package/src/hooks/pre-tool-use.ts +72 -0
- package/src/hooks/prompt-submit.ts +46 -0
- package/src/hooks/session-end.ts +71 -0
- package/src/hooks/session-start.ts +48 -0
- package/src/hooks/stop.ts +39 -0
- package/src/hooks/subagent-start.ts +49 -0
- package/src/hooks/subagent-stop.ts +54 -0
- package/src/hooks/task-completed.ts +54 -0
- package/src/index.ts +342 -0
- package/src/mcp/in-memory-kv.ts +61 -0
- package/src/mcp/server.ts +1455 -0
- package/src/mcp/standalone.ts +177 -0
- package/src/mcp/tools-registry.ts +769 -0
- package/src/mcp/transport.ts +91 -0
- package/src/prompts/compression.ts +67 -0
- package/src/prompts/consolidation.ts +48 -0
- package/src/prompts/graph-extraction.ts +35 -0
- package/src/prompts/summary.ts +38 -0
- package/src/prompts/xml.ts +26 -0
- package/src/providers/agent-sdk.ts +34 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/circuit-breaker.ts +82 -0
- package/src/providers/embedding/cohere.ts +46 -0
- package/src/providers/embedding/gemini.ts +54 -0
- package/src/providers/embedding/index.ts +39 -0
- package/src/providers/embedding/local.ts +52 -0
- package/src/providers/embedding/openai.ts +45 -0
- package/src/providers/embedding/openrouter.ts +51 -0
- package/src/providers/embedding/voyage.ts +46 -0
- package/src/providers/fallback-chain.ts +31 -0
- package/src/providers/index.ts +84 -0
- package/src/providers/openrouter.ts +71 -0
- package/src/providers/resilient.ts +37 -0
- package/src/state/hybrid-search.ts +295 -0
- package/src/state/index-persistence.ts +63 -0
- package/src/state/keyed-mutex.ts +18 -0
- package/src/state/kv.ts +33 -0
- package/src/state/schema.ts +71 -0
- package/src/state/search-index.ts +245 -0
- package/src/state/stemmer.ts +104 -0
- package/src/state/synonyms.ts +63 -0
- package/src/state/vector-index.ts +130 -0
- package/src/telemetry/setup.ts +116 -0
- package/src/triggers/api.ts +1904 -0
- package/src/triggers/events.ts +71 -0
- package/src/types.ts +769 -0
- package/src/version.ts +1 -0
- package/src/viewer/index.html +2497 -0
- package/src/viewer/server.ts +207 -0
- package/src/xenova.d.ts +3 -0
- package/test/actions.test.ts +490 -0
- package/test/audit.test.ts +108 -0
- package/test/auto-forget.test.ts +188 -0
- package/test/cascade.test.ts +277 -0
- package/test/checkpoints.test.ts +493 -0
- package/test/circuit-breaker.test.ts +107 -0
- package/test/claude-bridge.test.ts +178 -0
- package/test/confidence.test.ts +247 -0
- package/test/consistency.test.ts +61 -0
- package/test/consolidation-pipeline.test.ts +251 -0
- package/test/crystallize.test.ts +521 -0
- package/test/diagnostics.test.ts +638 -0
- package/test/embedding-provider.test.ts +49 -0
- package/test/enrich.test.ts +209 -0
- package/test/eval.test.ts +300 -0
- package/test/export-import.test.ts +251 -0
- package/test/facets.test.ts +448 -0
- package/test/fallback-chain.test.ts +93 -0
- package/test/frontier.test.ts +485 -0
- package/test/governance.test.ts +147 -0
- package/test/graph-retrieval.test.ts +186 -0
- package/test/graph.test.ts +160 -0
- package/test/helpers/mocks.ts +40 -0
- package/test/hybrid-search.test.ts +145 -0
- package/test/index-persistence.test.ts +124 -0
- package/test/integration.test.ts +265 -0
- package/test/leases.test.ts +399 -0
- package/test/mcp-prompts.test.ts +218 -0
- package/test/mcp-resources.test.ts +286 -0
- package/test/mcp-standalone.test.ts +113 -0
- package/test/mesh.test.ts +700 -0
- package/test/privacy.test.ts +87 -0
- package/test/profile.test.ts +161 -0
- package/test/query-expansion.test.ts +154 -0
- package/test/relations.test.ts +198 -0
- package/test/retention.test.ts +245 -0
- package/test/routines.test.ts +497 -0
- package/test/schema-fingerprint.test.ts +81 -0
- package/test/schema.test.ts +42 -0
- package/test/search-index.test.ts +128 -0
- package/test/sentinels.test.ts +626 -0
- package/test/signals.test.ts +410 -0
- package/test/sketches.test.ts +549 -0
- package/test/sliding-window.test.ts +199 -0
- package/test/smart-search.test.ts +169 -0
- package/test/snapshot.test.ts +165 -0
- package/test/team.test.ts +156 -0
- package/test/temporal-graph.test.ts +378 -0
- package/test/timeline.test.ts +148 -0
- package/test/vector-index.test.ts +79 -0
- package/test/verify.test.ts +209 -0
- package/test/xml.test.ts +65 -0
- package/tsconfig.json +22 -0
- package/tsdown.config.ts +62 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import type { StateKV } from "../state/kv.js";
|
|
3
|
+
import { KV } from "../state/schema.js";
|
|
4
|
+
import type { Action, ActionEdge, Checkpoint, Lease } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export interface FrontierItem {
|
|
7
|
+
action: Action;
|
|
8
|
+
score: number;
|
|
9
|
+
blockers: string[];
|
|
10
|
+
leased: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function registerFrontierFunction(sdk: ISdk, kv: StateKV): void {
|
|
14
|
+
sdk.registerFunction(
|
|
15
|
+
{ id: "mem::frontier" },
|
|
16
|
+
async (data: {
|
|
17
|
+
project?: string;
|
|
18
|
+
agentId?: string;
|
|
19
|
+
limit?: number;
|
|
20
|
+
includeLeasedByOthers?: boolean;
|
|
21
|
+
}) => {
|
|
22
|
+
const actions = await kv.list<Action>(KV.actions);
|
|
23
|
+
const edges = await kv.list<ActionEdge>(KV.actionEdges);
|
|
24
|
+
const leases = await kv.list<Lease>(KV.leases);
|
|
25
|
+
const checkpoints = await kv.list<Checkpoint>(KV.checkpoints);
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
|
|
28
|
+
const activeLeaseMap = new Map<string, Lease>();
|
|
29
|
+
for (const lease of leases) {
|
|
30
|
+
if (
|
|
31
|
+
lease.status === "active" &&
|
|
32
|
+
new Date(lease.expiresAt).getTime() > now
|
|
33
|
+
) {
|
|
34
|
+
activeLeaseMap.set(lease.actionId, lease);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const checkpointMap = new Map<string, Checkpoint>();
|
|
39
|
+
for (const cp of checkpoints) {
|
|
40
|
+
checkpointMap.set(cp.id, cp);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const actionMap = new Map<string, Action>();
|
|
44
|
+
for (const a of actions) actionMap.set(a.id, a);
|
|
45
|
+
|
|
46
|
+
const frontier: FrontierItem[] = [];
|
|
47
|
+
|
|
48
|
+
for (const action of actions) {
|
|
49
|
+
if (action.status === "done" || action.status === "cancelled") continue;
|
|
50
|
+
if (data.project && action.project !== data.project) continue;
|
|
51
|
+
|
|
52
|
+
const blockers: string[] = [];
|
|
53
|
+
const inEdges = edges.filter(
|
|
54
|
+
(e) => e.sourceActionId === action.id && e.type === "requires",
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
for (const edge of inEdges) {
|
|
58
|
+
const dep = actionMap.get(edge.targetActionId);
|
|
59
|
+
if (dep && dep.status !== "done") {
|
|
60
|
+
blockers.push(`requires:${dep.id}:${dep.title}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const gateEdges = edges.filter(
|
|
65
|
+
(e) => e.sourceActionId === action.id && e.type === "gated_by",
|
|
66
|
+
);
|
|
67
|
+
for (const edge of gateEdges) {
|
|
68
|
+
const cp = checkpointMap.get(edge.targetActionId);
|
|
69
|
+
if (cp && cp.status !== "passed") {
|
|
70
|
+
blockers.push(`checkpoint:${cp.id}:${cp.name}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const conflictEdges = edges.filter(
|
|
75
|
+
(e) =>
|
|
76
|
+
(e.sourceActionId === action.id ||
|
|
77
|
+
e.targetActionId === action.id) &&
|
|
78
|
+
e.type === "conflicts_with",
|
|
79
|
+
);
|
|
80
|
+
for (const edge of conflictEdges) {
|
|
81
|
+
const otherId =
|
|
82
|
+
edge.sourceActionId === action.id
|
|
83
|
+
? edge.targetActionId
|
|
84
|
+
: edge.sourceActionId;
|
|
85
|
+
const other = actionMap.get(otherId);
|
|
86
|
+
if (other && other.status === "active") {
|
|
87
|
+
blockers.push(`conflict:${other.id}:${other.title}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (blockers.length > 0) continue;
|
|
92
|
+
|
|
93
|
+
const lease = activeLeaseMap.get(action.id);
|
|
94
|
+
const leasedByOther =
|
|
95
|
+
lease && data.agentId && lease.agentId !== data.agentId;
|
|
96
|
+
if (leasedByOther && !data.includeLeasedByOthers) continue;
|
|
97
|
+
|
|
98
|
+
const score = computeScore(action, edges, now);
|
|
99
|
+
|
|
100
|
+
frontier.push({
|
|
101
|
+
action,
|
|
102
|
+
score,
|
|
103
|
+
blockers: [],
|
|
104
|
+
leased: !!lease,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
frontier.sort((a, b) => b.score - a.score);
|
|
109
|
+
const limit = data.limit || 20;
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
success: true,
|
|
113
|
+
frontier: frontier.slice(0, limit),
|
|
114
|
+
totalActions: actions.length,
|
|
115
|
+
totalUnblocked: frontier.length,
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
sdk.registerFunction(
|
|
121
|
+
{ id: "mem::next" },
|
|
122
|
+
async (data: { project?: string; agentId?: string }) => {
|
|
123
|
+
const result = await sdk.trigger<
|
|
124
|
+
{ project?: string; agentId?: string; limit?: number },
|
|
125
|
+
{
|
|
126
|
+
success: boolean;
|
|
127
|
+
frontier: FrontierItem[];
|
|
128
|
+
totalActions: number;
|
|
129
|
+
totalUnblocked: number;
|
|
130
|
+
}
|
|
131
|
+
>("mem::frontier", {
|
|
132
|
+
project: data.project,
|
|
133
|
+
agentId: data.agentId,
|
|
134
|
+
limit: 1,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (!result.success) {
|
|
138
|
+
return {
|
|
139
|
+
success: false,
|
|
140
|
+
suggestion: null,
|
|
141
|
+
message: "Failed to compute frontier",
|
|
142
|
+
totalActions: 0,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (result.frontier.length === 0) {
|
|
146
|
+
return {
|
|
147
|
+
success: true,
|
|
148
|
+
suggestion: null,
|
|
149
|
+
message: "No actionable work found",
|
|
150
|
+
totalActions: result.totalActions || 0,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const top = result.frontier[0];
|
|
155
|
+
return {
|
|
156
|
+
success: true,
|
|
157
|
+
suggestion: {
|
|
158
|
+
actionId: top.action.id,
|
|
159
|
+
title: top.action.title,
|
|
160
|
+
description: top.action.description,
|
|
161
|
+
priority: top.action.priority,
|
|
162
|
+
score: top.score,
|
|
163
|
+
tags: top.action.tags,
|
|
164
|
+
},
|
|
165
|
+
message: `Suggested: ${top.action.title} (priority ${top.action.priority}, score ${top.score.toFixed(2)})`,
|
|
166
|
+
totalActions: result.totalActions,
|
|
167
|
+
totalUnblocked: result.totalUnblocked,
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function computeScore(
|
|
174
|
+
action: Action,
|
|
175
|
+
edges: ActionEdge[],
|
|
176
|
+
now: number,
|
|
177
|
+
): number {
|
|
178
|
+
let score = action.priority * 10;
|
|
179
|
+
|
|
180
|
+
const ageHours =
|
|
181
|
+
(now - new Date(action.createdAt).getTime()) / (1000 * 60 * 60);
|
|
182
|
+
score += Math.min(ageHours * 0.5, 20);
|
|
183
|
+
|
|
184
|
+
const unlockCount = edges.filter(
|
|
185
|
+
(e) => e.sourceActionId === action.id && e.type === "unlocks",
|
|
186
|
+
).length;
|
|
187
|
+
score += unlockCount * 5;
|
|
188
|
+
|
|
189
|
+
if (edges.some((e) => e.sourceActionId === action.id && e.type === "spawned_by")) {
|
|
190
|
+
score += 3;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (action.status === "active") score += 15;
|
|
194
|
+
|
|
195
|
+
return Math.round(score * 100) / 100;
|
|
196
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import type { Memory, GovernanceFilter, AuditEntry } from "../types.js";
|
|
4
|
+
import { KV } from "../state/schema.js";
|
|
5
|
+
import type { StateKV } from "../state/kv.js";
|
|
6
|
+
import { recordAudit, queryAudit } from "./audit.js";
|
|
7
|
+
|
|
8
|
+
export function registerGovernanceFunction(sdk: ISdk, kv: StateKV): void {
|
|
9
|
+
sdk.registerFunction(
|
|
10
|
+
{ id: "mem::governance-delete" },
|
|
11
|
+
async (data: { memoryIds: string[]; reason?: string }) => {
|
|
12
|
+
const ctx = getContext();
|
|
13
|
+
if (
|
|
14
|
+
!data.memoryIds ||
|
|
15
|
+
!Array.isArray(data.memoryIds) ||
|
|
16
|
+
data.memoryIds.length === 0
|
|
17
|
+
) {
|
|
18
|
+
return { success: false, error: "memoryIds array is required" };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let deleted = 0;
|
|
22
|
+
for (const id of data.memoryIds) {
|
|
23
|
+
const mem = await kv.get<Memory>(KV.memories, id);
|
|
24
|
+
if (mem) {
|
|
25
|
+
await kv.delete(KV.memories, id);
|
|
26
|
+
deleted++;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await recordAudit(
|
|
31
|
+
kv,
|
|
32
|
+
"delete",
|
|
33
|
+
"mem::governance-delete",
|
|
34
|
+
data.memoryIds,
|
|
35
|
+
{
|
|
36
|
+
reason: data.reason || "manual deletion",
|
|
37
|
+
deleted,
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
ctx.logger.info("Governance delete", {
|
|
42
|
+
requested: data.memoryIds.length,
|
|
43
|
+
deleted,
|
|
44
|
+
});
|
|
45
|
+
return { success: true, deleted, total: data.memoryIds.length };
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
sdk.registerFunction(
|
|
50
|
+
{ id: "mem::governance-bulk" },
|
|
51
|
+
async (data: GovernanceFilter & { dryRun?: boolean }) => {
|
|
52
|
+
const ctx = getContext();
|
|
53
|
+
|
|
54
|
+
const hasFilter =
|
|
55
|
+
(data.type && data.type.length > 0) ||
|
|
56
|
+
data.dateFrom ||
|
|
57
|
+
data.dateTo ||
|
|
58
|
+
data.qualityBelow !== undefined;
|
|
59
|
+
if (!hasFilter && !data.dryRun) {
|
|
60
|
+
return {
|
|
61
|
+
success: false,
|
|
62
|
+
error: "At least one filter is required for non-dryRun bulk delete",
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const memories = await kv.list<Memory>(KV.memories);
|
|
67
|
+
let candidates = memories;
|
|
68
|
+
|
|
69
|
+
if (data.type && data.type.length > 0) {
|
|
70
|
+
candidates = candidates.filter((m) => data.type!.includes(m.type));
|
|
71
|
+
}
|
|
72
|
+
if (data.dateFrom) {
|
|
73
|
+
const from = new Date(data.dateFrom).getTime();
|
|
74
|
+
if (Number.isNaN(from)) {
|
|
75
|
+
return { success: false, error: "Invalid dateFrom format" };
|
|
76
|
+
}
|
|
77
|
+
candidates = candidates.filter(
|
|
78
|
+
(m) => new Date(m.createdAt).getTime() >= from,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (data.dateTo) {
|
|
82
|
+
const to = new Date(data.dateTo).getTime();
|
|
83
|
+
if (Number.isNaN(to)) {
|
|
84
|
+
return { success: false, error: "Invalid dateTo format" };
|
|
85
|
+
}
|
|
86
|
+
candidates = candidates.filter(
|
|
87
|
+
(m) => new Date(m.createdAt).getTime() <= to,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (data.qualityBelow !== undefined) {
|
|
91
|
+
candidates = candidates.filter((m) => m.strength < data.qualityBelow!);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (data.dryRun) {
|
|
95
|
+
return {
|
|
96
|
+
success: true,
|
|
97
|
+
dryRun: true,
|
|
98
|
+
wouldDelete: candidates.length,
|
|
99
|
+
ids: candidates.map((m) => m.id),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for (const mem of candidates) {
|
|
104
|
+
await kv.delete(KV.memories, mem.id);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
await recordAudit(
|
|
108
|
+
kv,
|
|
109
|
+
"delete",
|
|
110
|
+
"mem::governance-bulk",
|
|
111
|
+
candidates.map((m) => m.id),
|
|
112
|
+
{ filter: data, deleted: candidates.length },
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
ctx.logger.info("Governance bulk delete", { deleted: candidates.length });
|
|
116
|
+
return { success: true, deleted: candidates.length };
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
sdk.registerFunction(
|
|
121
|
+
{ id: "mem::audit-query" },
|
|
122
|
+
async (data?: {
|
|
123
|
+
operation?: AuditEntry["operation"];
|
|
124
|
+
dateFrom?: string;
|
|
125
|
+
dateTo?: string;
|
|
126
|
+
limit?: number;
|
|
127
|
+
}) => {
|
|
128
|
+
return queryAudit(kv, data);
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
GraphNode,
|
|
3
|
+
GraphEdge,
|
|
4
|
+
} from "../types.js";
|
|
5
|
+
import { KV } from "../state/schema.js";
|
|
6
|
+
import type { StateKV } from "../state/kv.js";
|
|
7
|
+
|
|
8
|
+
export interface GraphRetrievalResult {
|
|
9
|
+
obsId: string;
|
|
10
|
+
sessionId: string;
|
|
11
|
+
score: number;
|
|
12
|
+
graphContext: string;
|
|
13
|
+
pathLength: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function buildGraphContext(
|
|
17
|
+
path: Array<{ node: GraphNode; edge?: GraphEdge }>,
|
|
18
|
+
): string {
|
|
19
|
+
const parts: string[] = [];
|
|
20
|
+
for (const step of path) {
|
|
21
|
+
const props = Object.entries(step.node.properties)
|
|
22
|
+
.slice(0, 3)
|
|
23
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
24
|
+
.join(", ");
|
|
25
|
+
let line = `[${step.node.type}] ${step.node.name}`;
|
|
26
|
+
if (props) line += ` (${props})`;
|
|
27
|
+
if (step.edge) {
|
|
28
|
+
line += ` --${step.edge.type}-->`;
|
|
29
|
+
if (step.edge.context?.reasoning) {
|
|
30
|
+
line += ` [${step.edge.context.reasoning}]`;
|
|
31
|
+
}
|
|
32
|
+
if (step.edge.tvalid) {
|
|
33
|
+
line += ` @${step.edge.tvalid}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
parts.push(line);
|
|
37
|
+
}
|
|
38
|
+
return parts.join(" ");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class GraphRetrieval {
|
|
42
|
+
constructor(private kv: StateKV) {}
|
|
43
|
+
|
|
44
|
+
async searchByEntities(
|
|
45
|
+
entityNames: string[],
|
|
46
|
+
maxDepth = 2,
|
|
47
|
+
maxResults = 20,
|
|
48
|
+
): Promise<GraphRetrievalResult[]> {
|
|
49
|
+
const allNodes = (await this.kv.list<GraphNode>(KV.graphNodes)).filter((n) => !n.stale);
|
|
50
|
+
const allEdges = (await this.kv.list<GraphEdge>(KV.graphEdges)).filter((e) => !e.stale);
|
|
51
|
+
|
|
52
|
+
const matchingNodes = allNodes.filter((n) => {
|
|
53
|
+
const nameLower = n.name.toLowerCase();
|
|
54
|
+
return entityNames.some(
|
|
55
|
+
(e) =>
|
|
56
|
+
nameLower.includes(e.toLowerCase()) ||
|
|
57
|
+
e.toLowerCase().includes(nameLower),
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (matchingNodes.length === 0) return [];
|
|
62
|
+
|
|
63
|
+
const results: GraphRetrievalResult[] = [];
|
|
64
|
+
const visitedObs = new Set<string>();
|
|
65
|
+
|
|
66
|
+
for (const startNode of matchingNodes) {
|
|
67
|
+
const paths = this.bfsTraversal(
|
|
68
|
+
startNode,
|
|
69
|
+
allNodes,
|
|
70
|
+
allEdges,
|
|
71
|
+
maxDepth,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
for (const path of paths) {
|
|
75
|
+
const lastNode = path[path.length - 1].node;
|
|
76
|
+
for (const obsId of lastNode.sourceObservationIds) {
|
|
77
|
+
if (visitedObs.has(obsId)) continue;
|
|
78
|
+
visitedObs.add(obsId);
|
|
79
|
+
|
|
80
|
+
const pathLength = path.length;
|
|
81
|
+
const edgeWeights = path
|
|
82
|
+
.filter((s) => s.edge)
|
|
83
|
+
.map((s) => s.edge!.weight);
|
|
84
|
+
const avgWeight =
|
|
85
|
+
edgeWeights.length > 0
|
|
86
|
+
? edgeWeights.reduce((a, b) => a + b, 0) / edgeWeights.length
|
|
87
|
+
: 0.5;
|
|
88
|
+
const score = avgWeight * (1 / pathLength);
|
|
89
|
+
|
|
90
|
+
results.push({
|
|
91
|
+
obsId,
|
|
92
|
+
sessionId: "",
|
|
93
|
+
score,
|
|
94
|
+
graphContext: buildGraphContext(path),
|
|
95
|
+
pathLength,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const obsId of startNode.sourceObservationIds) {
|
|
101
|
+
if (visitedObs.has(obsId)) continue;
|
|
102
|
+
visitedObs.add(obsId);
|
|
103
|
+
results.push({
|
|
104
|
+
obsId,
|
|
105
|
+
sessionId: "",
|
|
106
|
+
score: 1.0,
|
|
107
|
+
graphContext: `[${startNode.type}] ${startNode.name}`,
|
|
108
|
+
pathLength: 0,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
results.sort((a, b) => b.score - a.score);
|
|
114
|
+
return results.slice(0, maxResults);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async expandFromChunks(
|
|
118
|
+
obsIds: string[],
|
|
119
|
+
maxDepth = 1,
|
|
120
|
+
maxResults = 10,
|
|
121
|
+
): Promise<GraphRetrievalResult[]> {
|
|
122
|
+
const allNodes = (await this.kv.list<GraphNode>(KV.graphNodes)).filter((n) => !n.stale);
|
|
123
|
+
const allEdges = (await this.kv.list<GraphEdge>(KV.graphEdges)).filter((e) => !e.stale);
|
|
124
|
+
|
|
125
|
+
const linkedNodes = allNodes.filter((n) =>
|
|
126
|
+
n.sourceObservationIds.some((id) => obsIds.includes(id)),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const results: GraphRetrievalResult[] = [];
|
|
130
|
+
const visitedObs = new Set<string>(obsIds);
|
|
131
|
+
|
|
132
|
+
for (const node of linkedNodes) {
|
|
133
|
+
const paths = this.bfsTraversal(node, allNodes, allEdges, maxDepth);
|
|
134
|
+
for (const path of paths) {
|
|
135
|
+
const lastNode = path[path.length - 1].node;
|
|
136
|
+
for (const obsId of lastNode.sourceObservationIds) {
|
|
137
|
+
if (visitedObs.has(obsId)) continue;
|
|
138
|
+
visitedObs.add(obsId);
|
|
139
|
+
|
|
140
|
+
const pathLength = path.length;
|
|
141
|
+
const score = 0.5 * (1 / (pathLength + 1));
|
|
142
|
+
|
|
143
|
+
results.push({
|
|
144
|
+
obsId,
|
|
145
|
+
sessionId: "",
|
|
146
|
+
score,
|
|
147
|
+
graphContext: buildGraphContext(path),
|
|
148
|
+
pathLength,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
results.sort((a, b) => b.score - a.score);
|
|
155
|
+
return results.slice(0, maxResults);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async temporalQuery(
|
|
159
|
+
entityName: string,
|
|
160
|
+
asOf?: string,
|
|
161
|
+
): Promise<{
|
|
162
|
+
entity: GraphNode | null;
|
|
163
|
+
currentState: GraphEdge[];
|
|
164
|
+
history: GraphEdge[];
|
|
165
|
+
}> {
|
|
166
|
+
const allNodes = (await this.kv.list<GraphNode>(KV.graphNodes)).filter((n) => !n.stale);
|
|
167
|
+
const allEdges = (await this.kv.list<GraphEdge>(KV.graphEdges)).filter((e) => !e.stale);
|
|
168
|
+
|
|
169
|
+
const entity = allNodes.find(
|
|
170
|
+
(n) => n.name.toLowerCase() === entityName.toLowerCase(),
|
|
171
|
+
);
|
|
172
|
+
if (!entity) return { entity: null, currentState: [], history: [] };
|
|
173
|
+
|
|
174
|
+
const relatedEdges = allEdges.filter(
|
|
175
|
+
(e) => e.sourceNodeId === entity.id || e.targetNodeId === entity.id,
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
if (!asOf) {
|
|
179
|
+
const latestEdges = this.getLatestEdges(relatedEdges);
|
|
180
|
+
const historicalEdges = relatedEdges.filter(
|
|
181
|
+
(e) => !latestEdges.some((le) => le.id === e.id),
|
|
182
|
+
);
|
|
183
|
+
return { entity, currentState: latestEdges, history: historicalEdges };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const asOfDate = new Date(asOf).getTime();
|
|
187
|
+
const validEdges = relatedEdges.filter((e) => {
|
|
188
|
+
const commitDate = new Date(e.tcommit || e.createdAt).getTime();
|
|
189
|
+
if (commitDate > asOfDate) return false;
|
|
190
|
+
if (e.tvalid) {
|
|
191
|
+
const validDate = new Date(e.tvalid).getTime();
|
|
192
|
+
if (validDate > asOfDate) return false;
|
|
193
|
+
}
|
|
194
|
+
if (e.tvalidEnd) {
|
|
195
|
+
const endDate = new Date(e.tvalidEnd).getTime();
|
|
196
|
+
if (endDate < asOfDate) return false;
|
|
197
|
+
}
|
|
198
|
+
return true;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
entity,
|
|
203
|
+
currentState: this.getLatestEdges(validEdges),
|
|
204
|
+
history: validEdges,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private getLatestEdges(edges: GraphEdge[]): GraphEdge[] {
|
|
209
|
+
const byKey = new Map<string, GraphEdge[]>();
|
|
210
|
+
for (const e of edges) {
|
|
211
|
+
const key = `${e.sourceNodeId}|${e.targetNodeId}|${e.type}`;
|
|
212
|
+
if (!byKey.has(key)) byKey.set(key, []);
|
|
213
|
+
byKey.get(key)!.push(e);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const latest: GraphEdge[] = [];
|
|
217
|
+
for (const group of byKey.values()) {
|
|
218
|
+
if (group.length === 0) continue;
|
|
219
|
+
group.sort(
|
|
220
|
+
(a, b) =>
|
|
221
|
+
new Date(b.tcommit || b.createdAt).getTime() -
|
|
222
|
+
new Date(a.tcommit || a.createdAt).getTime(),
|
|
223
|
+
);
|
|
224
|
+
const newest = group.find((e) => e.isLatest !== false) || group[0];
|
|
225
|
+
latest.push(newest);
|
|
226
|
+
}
|
|
227
|
+
return latest;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private bfsTraversal(
|
|
231
|
+
startNode: GraphNode,
|
|
232
|
+
allNodes: GraphNode[],
|
|
233
|
+
allEdges: GraphEdge[],
|
|
234
|
+
maxDepth: number,
|
|
235
|
+
): Array<Array<{ node: GraphNode; edge?: GraphEdge }>> {
|
|
236
|
+
const paths: Array<Array<{ node: GraphNode; edge?: GraphEdge }>> = [];
|
|
237
|
+
const visited = new Set<string>();
|
|
238
|
+
const queue: Array<{
|
|
239
|
+
nodeId: string;
|
|
240
|
+
depth: number;
|
|
241
|
+
path: Array<{ node: GraphNode; edge?: GraphEdge }>;
|
|
242
|
+
}> = [{ nodeId: startNode.id, depth: 0, path: [{ node: startNode }] }];
|
|
243
|
+
|
|
244
|
+
visited.add(startNode.id);
|
|
245
|
+
|
|
246
|
+
while (queue.length > 0) {
|
|
247
|
+
const { nodeId, depth, path } = queue.shift()!;
|
|
248
|
+
paths.push(path);
|
|
249
|
+
|
|
250
|
+
if (depth >= maxDepth) continue;
|
|
251
|
+
|
|
252
|
+
const neighborEdges = allEdges.filter(
|
|
253
|
+
(e) => e.sourceNodeId === nodeId || e.targetNodeId === nodeId,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
for (const edge of neighborEdges) {
|
|
257
|
+
const nextId =
|
|
258
|
+
edge.sourceNodeId === nodeId
|
|
259
|
+
? edge.targetNodeId
|
|
260
|
+
: edge.sourceNodeId;
|
|
261
|
+
if (visited.has(nextId)) continue;
|
|
262
|
+
visited.add(nextId);
|
|
263
|
+
|
|
264
|
+
const nextNode = allNodes.find((n) => n.id === nextId);
|
|
265
|
+
if (!nextNode) continue;
|
|
266
|
+
|
|
267
|
+
queue.push({
|
|
268
|
+
nodeId: nextId,
|
|
269
|
+
depth: depth + 1,
|
|
270
|
+
path: [...path, { node: nextNode, edge }],
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return paths;
|
|
276
|
+
}
|
|
277
|
+
}
|