@agentmemory/agentmemory 0.7.0 → 0.7.3
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/AGENTS.md +2 -2
- package/README.md +76 -82
- package/dist/cli.mjs +99 -32
- package/dist/cli.mjs.map +1 -1
- package/dist/index.mjs +4 -2
- package/dist/index.mjs.map +1 -1
- package/dist/{src-QxitMPfJ.mjs → src-sYZDDbiA.mjs} +5 -3
- package/dist/src-sYZDDbiA.mjs.map +1 -0
- package/dist/standalone.mjs +1 -1
- package/dist/standalone.mjs.map +1 -1
- package/package.json +11 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/scripts/notification.d.mts +1 -0
- package/plugin/scripts/notification.mjs.map +1 -0
- package/plugin/scripts/post-tool-failure.d.mts +1 -0
- package/plugin/scripts/post-tool-failure.mjs.map +1 -0
- package/plugin/scripts/post-tool-use.d.mts +1 -0
- package/plugin/scripts/post-tool-use.mjs.map +1 -0
- package/plugin/scripts/pre-compact.d.mts +1 -0
- package/plugin/scripts/pre-compact.mjs.map +1 -0
- package/plugin/scripts/pre-tool-use.d.mts +1 -0
- package/plugin/scripts/pre-tool-use.mjs.map +1 -0
- package/plugin/scripts/prompt-submit.d.mts +1 -0
- package/plugin/scripts/prompt-submit.mjs.map +1 -0
- package/plugin/scripts/session-end.d.mts +1 -0
- package/plugin/scripts/session-end.mjs.map +1 -0
- package/plugin/scripts/session-start.d.mts +1 -0
- package/plugin/scripts/session-start.mjs.map +1 -0
- package/plugin/scripts/stop.d.mts +1 -0
- package/plugin/scripts/stop.mjs.map +1 -0
- package/plugin/scripts/subagent-start.d.mts +1 -0
- package/plugin/scripts/subagent-start.mjs.map +1 -0
- package/plugin/scripts/subagent-stop.d.mts +1 -0
- package/plugin/scripts/subagent-stop.mjs.map +1 -0
- package/plugin/scripts/task-completed.d.mts +1 -0
- package/plugin/scripts/task-completed.mjs.map +1 -0
- package/.claude-plugin/marketplace.json +0 -14
- package/.github/workflows/ci.yml +0 -22
- package/.github/workflows/publish.yml +0 -28
- package/assets/banner.png +0 -0
- package/assets/demo.gif +0 -0
- package/assets/demo.mp4 +0 -0
- package/benchmark/QUALITY.md +0 -73
- package/benchmark/REAL-EMBEDDINGS.md +0 -67
- package/benchmark/SCALE.md +0 -110
- package/benchmark/dataset.ts +0 -293
- package/benchmark/quality-eval.ts +0 -643
- package/benchmark/real-embeddings-eval.ts +0 -405
- package/benchmark/scale-eval.ts +0 -398
- package/dist/src-QxitMPfJ.mjs.map +0 -1
- package/src/auth.ts +0 -12
- package/src/cli.ts +0 -159
- package/src/config.ts +0 -221
- package/src/eval/metrics-store.ts +0 -65
- package/src/eval/quality.ts +0 -51
- package/src/eval/schemas.ts +0 -124
- package/src/eval/self-correct.ts +0 -28
- package/src/eval/validator.ts +0 -31
- package/src/functions/actions.ts +0 -288
- package/src/functions/audit.ts +0 -61
- package/src/functions/auto-forget.ts +0 -169
- package/src/functions/branch-aware.ts +0 -169
- package/src/functions/cascade.ts +0 -80
- package/src/functions/checkpoints.ts +0 -209
- package/src/functions/claude-bridge.ts +0 -161
- package/src/functions/compress.ts +0 -194
- package/src/functions/consolidate.ts +0 -212
- package/src/functions/consolidation-pipeline.ts +0 -258
- package/src/functions/context.ts +0 -169
- package/src/functions/crystallize.ts +0 -293
- package/src/functions/dedup.ts +0 -57
- package/src/functions/diagnostics.ts +0 -785
- package/src/functions/enrich.ts +0 -132
- package/src/functions/evict.ts +0 -163
- package/src/functions/export-import.ts +0 -508
- package/src/functions/facets.ts +0 -248
- package/src/functions/file-index.ts +0 -106
- package/src/functions/flow-compress.ts +0 -214
- package/src/functions/frontier.ts +0 -196
- package/src/functions/governance.ts +0 -131
- package/src/functions/graph-retrieval.ts +0 -277
- package/src/functions/graph.ts +0 -275
- package/src/functions/leases.ts +0 -216
- package/src/functions/lessons.ts +0 -253
- package/src/functions/mesh.ts +0 -434
- package/src/functions/migrate.ts +0 -165
- package/src/functions/observe.ts +0 -144
- package/src/functions/obsidian-export.ts +0 -310
- package/src/functions/patterns.ts +0 -138
- package/src/functions/privacy.ts +0 -39
- package/src/functions/profile.ts +0 -155
- package/src/functions/query-expansion.ts +0 -186
- package/src/functions/relations.ts +0 -237
- package/src/functions/remember.ts +0 -162
- package/src/functions/retention.ts +0 -235
- package/src/functions/routines.ts +0 -289
- package/src/functions/search.ts +0 -80
- package/src/functions/sentinels.ts +0 -417
- package/src/functions/signals.ts +0 -186
- package/src/functions/sketches.ts +0 -274
- package/src/functions/sliding-window.ts +0 -257
- package/src/functions/smart-search.ts +0 -115
- package/src/functions/snapshot.ts +0 -219
- package/src/functions/summarize.ts +0 -155
- package/src/functions/team.ts +0 -147
- package/src/functions/temporal-graph.ts +0 -476
- package/src/functions/timeline.ts +0 -138
- package/src/functions/verify.ts +0 -117
- package/src/health/monitor.ts +0 -110
- package/src/health/thresholds.ts +0 -73
- package/src/hooks/notification.ts +0 -52
- package/src/hooks/post-tool-failure.ts +0 -58
- package/src/hooks/post-tool-use.ts +0 -62
- package/src/hooks/pre-compact.ts +0 -60
- package/src/hooks/pre-tool-use.ts +0 -72
- package/src/hooks/prompt-submit.ts +0 -46
- package/src/hooks/session-end.ts +0 -71
- package/src/hooks/session-start.ts +0 -48
- package/src/hooks/stop.ts +0 -39
- package/src/hooks/subagent-start.ts +0 -49
- package/src/hooks/subagent-stop.ts +0 -54
- package/src/hooks/task-completed.ts +0 -54
- package/src/index.ts +0 -342
- package/src/mcp/in-memory-kv.ts +0 -61
- package/src/mcp/server.ts +0 -1455
- package/src/mcp/standalone.ts +0 -177
- package/src/mcp/tools-registry.ts +0 -769
- package/src/mcp/transport.ts +0 -91
- package/src/prompts/compression.ts +0 -67
- package/src/prompts/consolidation.ts +0 -48
- package/src/prompts/graph-extraction.ts +0 -35
- package/src/prompts/summary.ts +0 -38
- package/src/prompts/xml.ts +0 -26
- package/src/providers/agent-sdk.ts +0 -34
- package/src/providers/anthropic.ts +0 -35
- package/src/providers/circuit-breaker.ts +0 -82
- package/src/providers/embedding/cohere.ts +0 -46
- package/src/providers/embedding/gemini.ts +0 -54
- package/src/providers/embedding/index.ts +0 -39
- package/src/providers/embedding/local.ts +0 -52
- package/src/providers/embedding/openai.ts +0 -45
- package/src/providers/embedding/openrouter.ts +0 -51
- package/src/providers/embedding/voyage.ts +0 -46
- package/src/providers/fallback-chain.ts +0 -31
- package/src/providers/index.ts +0 -84
- package/src/providers/openrouter.ts +0 -71
- package/src/providers/resilient.ts +0 -37
- package/src/state/hybrid-search.ts +0 -295
- package/src/state/index-persistence.ts +0 -63
- package/src/state/keyed-mutex.ts +0 -18
- package/src/state/kv.ts +0 -33
- package/src/state/schema.ts +0 -71
- package/src/state/search-index.ts +0 -245
- package/src/state/stemmer.ts +0 -104
- package/src/state/synonyms.ts +0 -63
- package/src/state/vector-index.ts +0 -130
- package/src/telemetry/setup.ts +0 -116
- package/src/triggers/api.ts +0 -1904
- package/src/triggers/events.ts +0 -71
- package/src/types.ts +0 -769
- package/src/version.ts +0 -1
- package/src/viewer/index.html +0 -2497
- package/src/viewer/server.ts +0 -207
- package/src/xenova.d.ts +0 -3
- package/test/actions.test.ts +0 -490
- package/test/audit.test.ts +0 -108
- package/test/auto-forget.test.ts +0 -188
- package/test/cascade.test.ts +0 -277
- package/test/checkpoints.test.ts +0 -493
- package/test/circuit-breaker.test.ts +0 -107
- package/test/claude-bridge.test.ts +0 -178
- package/test/confidence.test.ts +0 -247
- package/test/consistency.test.ts +0 -61
- package/test/consolidation-pipeline.test.ts +0 -251
- package/test/crystallize.test.ts +0 -521
- package/test/diagnostics.test.ts +0 -638
- package/test/embedding-provider.test.ts +0 -49
- package/test/enrich.test.ts +0 -209
- package/test/eval.test.ts +0 -300
- package/test/export-import.test.ts +0 -251
- package/test/facets.test.ts +0 -448
- package/test/fallback-chain.test.ts +0 -93
- package/test/frontier.test.ts +0 -485
- package/test/governance.test.ts +0 -147
- package/test/graph-retrieval.test.ts +0 -186
- package/test/graph.test.ts +0 -160
- package/test/helpers/mocks.ts +0 -40
- package/test/hybrid-search.test.ts +0 -145
- package/test/index-persistence.test.ts +0 -124
- package/test/integration.test.ts +0 -265
- package/test/leases.test.ts +0 -399
- package/test/mcp-prompts.test.ts +0 -218
- package/test/mcp-resources.test.ts +0 -286
- package/test/mcp-standalone.test.ts +0 -113
- package/test/mesh.test.ts +0 -700
- package/test/privacy.test.ts +0 -87
- package/test/profile.test.ts +0 -161
- package/test/query-expansion.test.ts +0 -154
- package/test/relations.test.ts +0 -198
- package/test/retention.test.ts +0 -245
- package/test/routines.test.ts +0 -497
- package/test/schema-fingerprint.test.ts +0 -81
- package/test/schema.test.ts +0 -42
- package/test/search-index.test.ts +0 -128
- package/test/sentinels.test.ts +0 -626
- package/test/signals.test.ts +0 -410
- package/test/sketches.test.ts +0 -549
- package/test/sliding-window.test.ts +0 -199
- package/test/smart-search.test.ts +0 -169
- package/test/snapshot.test.ts +0 -165
- package/test/team.test.ts +0 -156
- package/test/temporal-graph.test.ts +0 -378
- package/test/timeline.test.ts +0 -148
- package/test/vector-index.test.ts +0 -79
- package/test/verify.test.ts +0 -209
- package/test/xml.test.ts +0 -65
- package/tsconfig.json +0 -22
- package/tsdown.config.ts +0 -62
|
@@ -1,196 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,277 +0,0 @@
|
|
|
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
|
-
}
|