@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,117 @@
|
|
|
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 {
|
|
5
|
+
Memory,
|
|
6
|
+
CompressedObservation,
|
|
7
|
+
Session,
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
|
|
10
|
+
export function registerVerifyFunction(sdk: ISdk, kv: StateKV): void {
|
|
11
|
+
sdk.registerFunction(
|
|
12
|
+
{ id: "mem::verify" },
|
|
13
|
+
async (data: { id: string }) => {
|
|
14
|
+
if (!data.id || typeof data.id !== "string") {
|
|
15
|
+
return { success: false, error: "id is required" };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const memory = await kv.get<Memory>(KV.memories, data.id);
|
|
19
|
+
if (memory) {
|
|
20
|
+
const observationIds = memory.sourceObservationIds || [];
|
|
21
|
+
const observations: Array<{
|
|
22
|
+
observation: CompressedObservation;
|
|
23
|
+
session?: Session;
|
|
24
|
+
}> = [];
|
|
25
|
+
|
|
26
|
+
for (const obsId of observationIds) {
|
|
27
|
+
const obs = await findObservation(kv, obsId, memory.sessionIds);
|
|
28
|
+
if (obs) {
|
|
29
|
+
const session = await kv.get<Session>(KV.sessions, obs.sessionId);
|
|
30
|
+
observations.push({ observation: obs, session: session || undefined });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
success: true,
|
|
36
|
+
type: "memory",
|
|
37
|
+
memory: {
|
|
38
|
+
id: memory.id,
|
|
39
|
+
title: memory.title,
|
|
40
|
+
type: memory.type,
|
|
41
|
+
version: memory.version,
|
|
42
|
+
strength: memory.strength,
|
|
43
|
+
isLatest: memory.isLatest,
|
|
44
|
+
createdAt: memory.createdAt,
|
|
45
|
+
updatedAt: memory.updatedAt,
|
|
46
|
+
supersedes: memory.supersedes,
|
|
47
|
+
parentId: memory.parentId,
|
|
48
|
+
},
|
|
49
|
+
citations: observations.map((o) => ({
|
|
50
|
+
observationId: o.observation.id,
|
|
51
|
+
title: o.observation.title,
|
|
52
|
+
type: o.observation.type,
|
|
53
|
+
confidence: o.observation.confidence,
|
|
54
|
+
timestamp: o.observation.timestamp,
|
|
55
|
+
sessionId: o.observation.sessionId,
|
|
56
|
+
sessionProject: o.session?.project,
|
|
57
|
+
sessionStatus: o.session?.status,
|
|
58
|
+
})),
|
|
59
|
+
citationCount: observations.length,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const obs = await findObservation(kv, data.id);
|
|
64
|
+
if (obs) {
|
|
65
|
+
const session = await kv.get<Session>(KV.sessions, obs.sessionId);
|
|
66
|
+
return {
|
|
67
|
+
success: true,
|
|
68
|
+
type: "observation",
|
|
69
|
+
observation: {
|
|
70
|
+
id: obs.id,
|
|
71
|
+
title: obs.title,
|
|
72
|
+
type: obs.type,
|
|
73
|
+
confidence: obs.confidence,
|
|
74
|
+
importance: obs.importance,
|
|
75
|
+
timestamp: obs.timestamp,
|
|
76
|
+
sessionId: obs.sessionId,
|
|
77
|
+
},
|
|
78
|
+
session: session
|
|
79
|
+
? {
|
|
80
|
+
id: session.id,
|
|
81
|
+
project: session.project,
|
|
82
|
+
status: session.status,
|
|
83
|
+
startedAt: session.startedAt,
|
|
84
|
+
}
|
|
85
|
+
: null,
|
|
86
|
+
citationCount: 0,
|
|
87
|
+
citations: [],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { success: false, error: "not found" };
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function findObservation(
|
|
97
|
+
kv: StateKV,
|
|
98
|
+
obsId: string,
|
|
99
|
+
hintSessionIds?: string[],
|
|
100
|
+
): Promise<CompressedObservation | null> {
|
|
101
|
+
if (hintSessionIds) {
|
|
102
|
+
for (const sid of hintSessionIds) {
|
|
103
|
+
const obs = await kv.get<CompressedObservation>(KV.observations(sid), obsId);
|
|
104
|
+
if (obs) return obs;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const sessions = await kv.list<Session>(KV.sessions);
|
|
108
|
+
for (const session of sessions) {
|
|
109
|
+
if (hintSessionIds?.includes(session.id)) continue;
|
|
110
|
+
const obs = await kv.get<CompressedObservation>(
|
|
111
|
+
KV.observations(session.id),
|
|
112
|
+
obsId,
|
|
113
|
+
);
|
|
114
|
+
if (obs) return obs;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import type { HealthSnapshot } from "../types.js";
|
|
3
|
+
import type { StateKV } from "../state/kv.js";
|
|
4
|
+
import { KV } from "../state/schema.js";
|
|
5
|
+
import { evaluateHealth } from "./thresholds.js";
|
|
6
|
+
|
|
7
|
+
export function registerHealthMonitor(
|
|
8
|
+
sdk: ISdk,
|
|
9
|
+
kv: StateKV,
|
|
10
|
+
): { stop: () => void } {
|
|
11
|
+
let connectionState = "connected";
|
|
12
|
+
let prevCpuUsage = process.cpuUsage();
|
|
13
|
+
let prevCpuTime = Date.now();
|
|
14
|
+
|
|
15
|
+
if (typeof sdk.on === "function") {
|
|
16
|
+
sdk.on("connection_state", (state?: unknown) => {
|
|
17
|
+
connectionState = state as string;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function collectHealth(): Promise<HealthSnapshot> {
|
|
22
|
+
const mem = process.memoryUsage();
|
|
23
|
+
const currentCpu = process.cpuUsage();
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const uptime = process.uptime();
|
|
26
|
+
|
|
27
|
+
const elapsedMs = now - prevCpuTime;
|
|
28
|
+
const userDelta = currentCpu.user - prevCpuUsage.user;
|
|
29
|
+
const systemDelta = currentCpu.system - prevCpuUsage.system;
|
|
30
|
+
const cpuPercent =
|
|
31
|
+
elapsedMs > 0 ? ((userDelta + systemDelta) / 1000 / elapsedMs) * 100 : 0;
|
|
32
|
+
prevCpuUsage = currentCpu;
|
|
33
|
+
prevCpuTime = now;
|
|
34
|
+
|
|
35
|
+
const startMark = performance.now();
|
|
36
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
37
|
+
const eventLoopLagMs = performance.now() - startMark;
|
|
38
|
+
|
|
39
|
+
let workers: HealthSnapshot["workers"] = [];
|
|
40
|
+
try {
|
|
41
|
+
const result = await sdk.trigger<
|
|
42
|
+
unknown,
|
|
43
|
+
{ workers?: HealthSnapshot["workers"] }
|
|
44
|
+
>("engine::workers::list", {});
|
|
45
|
+
if (result?.workers) workers = result.workers;
|
|
46
|
+
} catch {}
|
|
47
|
+
|
|
48
|
+
const KV_PROBE_TIMEOUT = 5000;
|
|
49
|
+
let kvConnectivity: { status: string; latencyMs?: number; error?: string };
|
|
50
|
+
const kvStart = performance.now();
|
|
51
|
+
try {
|
|
52
|
+
await Promise.race([
|
|
53
|
+
(async () => {
|
|
54
|
+
await kv.set(KV.health, "_probe", { ts: Date.now() });
|
|
55
|
+
await kv.get(KV.health, "_probe");
|
|
56
|
+
})(),
|
|
57
|
+
new Promise<never>((_, reject) =>
|
|
58
|
+
setTimeout(() => reject(new Error("timeout")), KV_PROBE_TIMEOUT),
|
|
59
|
+
),
|
|
60
|
+
]);
|
|
61
|
+
kvConnectivity = { status: "ok", latencyMs: Math.round((performance.now() - kvStart) * 100) / 100 };
|
|
62
|
+
} catch {
|
|
63
|
+
kvConnectivity = { status: "error", error: "kv_probe_failed", latencyMs: Math.round((performance.now() - kvStart) * 100) / 100 };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const snapshot: HealthSnapshot = {
|
|
67
|
+
connectionState,
|
|
68
|
+
workers,
|
|
69
|
+
memory: {
|
|
70
|
+
heapUsed: mem.heapUsed,
|
|
71
|
+
heapTotal: mem.heapTotal,
|
|
72
|
+
rss: mem.rss,
|
|
73
|
+
external: mem.external,
|
|
74
|
+
},
|
|
75
|
+
cpu: {
|
|
76
|
+
userMicros: currentCpu.user,
|
|
77
|
+
systemMicros: currentCpu.system,
|
|
78
|
+
percent: Math.round(cpuPercent * 100) / 100,
|
|
79
|
+
},
|
|
80
|
+
eventLoopLagMs,
|
|
81
|
+
uptimeSeconds: uptime,
|
|
82
|
+
kvConnectivity,
|
|
83
|
+
status: "healthy",
|
|
84
|
+
alerts: [],
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const evaluated = evaluateHealth(snapshot);
|
|
88
|
+
snapshot.status = evaluated.status;
|
|
89
|
+
snapshot.alerts = evaluated.alerts;
|
|
90
|
+
|
|
91
|
+
await kv.set(KV.health, "latest", snapshot).catch(() => {});
|
|
92
|
+
return snapshot;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
collectHealth().catch(() => {});
|
|
96
|
+
const interval = setInterval(() => {
|
|
97
|
+
collectHealth().catch(() => {});
|
|
98
|
+
}, 30_000);
|
|
99
|
+
interval.unref();
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
stop: () => clearInterval(interval),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function getLatestHealth(
|
|
107
|
+
kv: StateKV,
|
|
108
|
+
): Promise<HealthSnapshot | null> {
|
|
109
|
+
return kv.get<HealthSnapshot>(KV.health, "latest");
|
|
110
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { HealthSnapshot } from "../types.js";
|
|
2
|
+
|
|
3
|
+
interface ThresholdConfig {
|
|
4
|
+
eventLoopLagWarnMs: number;
|
|
5
|
+
eventLoopLagCriticalMs: number;
|
|
6
|
+
cpuWarnPercent: number;
|
|
7
|
+
cpuCriticalPercent: number;
|
|
8
|
+
memoryWarnPercent: number;
|
|
9
|
+
memoryCriticalPercent: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULTS: ThresholdConfig = {
|
|
13
|
+
eventLoopLagWarnMs: 100,
|
|
14
|
+
eventLoopLagCriticalMs: 500,
|
|
15
|
+
cpuWarnPercent: 80,
|
|
16
|
+
cpuCriticalPercent: 90,
|
|
17
|
+
memoryWarnPercent: 80,
|
|
18
|
+
memoryCriticalPercent: 95,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function evaluateHealth(
|
|
22
|
+
snapshot: HealthSnapshot,
|
|
23
|
+
config: Partial<ThresholdConfig> = {},
|
|
24
|
+
): { status: "healthy" | "degraded" | "critical"; alerts: string[] } {
|
|
25
|
+
const cfg = { ...DEFAULTS, ...config };
|
|
26
|
+
const alerts: string[] = [];
|
|
27
|
+
let critical = false;
|
|
28
|
+
let degraded = false;
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
snapshot.connectionState === "disconnected" ||
|
|
32
|
+
snapshot.connectionState === "failed"
|
|
33
|
+
) {
|
|
34
|
+
alerts.push(`connection_${snapshot.connectionState}`);
|
|
35
|
+
critical = true;
|
|
36
|
+
} else if (snapshot.connectionState === "reconnecting") {
|
|
37
|
+
alerts.push("connection_reconnecting");
|
|
38
|
+
degraded = true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (snapshot.eventLoopLagMs > cfg.eventLoopLagCriticalMs) {
|
|
42
|
+
alerts.push(
|
|
43
|
+
`event_loop_lag_critical_${Math.round(snapshot.eventLoopLagMs)}ms`,
|
|
44
|
+
);
|
|
45
|
+
critical = true;
|
|
46
|
+
} else if (snapshot.eventLoopLagMs > cfg.eventLoopLagWarnMs) {
|
|
47
|
+
alerts.push(`event_loop_lag_warn_${Math.round(snapshot.eventLoopLagMs)}ms`);
|
|
48
|
+
degraded = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (snapshot.cpu.percent > cfg.cpuCriticalPercent) {
|
|
52
|
+
alerts.push(`cpu_critical_${Math.round(snapshot.cpu.percent)}%`);
|
|
53
|
+
critical = true;
|
|
54
|
+
} else if (snapshot.cpu.percent > cfg.cpuWarnPercent) {
|
|
55
|
+
alerts.push(`cpu_warn_${Math.round(snapshot.cpu.percent)}%`);
|
|
56
|
+
degraded = true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const memPercent =
|
|
60
|
+
snapshot.memory.heapTotal > 0
|
|
61
|
+
? (snapshot.memory.heapUsed / snapshot.memory.heapTotal) * 100
|
|
62
|
+
: 0;
|
|
63
|
+
if (memPercent > cfg.memoryCriticalPercent) {
|
|
64
|
+
alerts.push(`memory_critical_${Math.round(memPercent)}%`);
|
|
65
|
+
critical = true;
|
|
66
|
+
} else if (memPercent > cfg.memoryWarnPercent) {
|
|
67
|
+
alerts.push(`memory_warn_${Math.round(memPercent)}%`);
|
|
68
|
+
degraded = true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const status = critical ? "critical" : degraded ? "degraded" : "healthy";
|
|
72
|
+
return { status, alerts };
|
|
73
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (data.notification_type !== "permission_prompt") return;
|
|
26
|
+
|
|
27
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await fetch(`${REST_URL}/agentmemory/observe`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: authHeaders(),
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
hookType: "notification",
|
|
35
|
+
sessionId,
|
|
36
|
+
project: data.cwd || process.cwd(),
|
|
37
|
+
cwd: data.cwd || process.cwd(),
|
|
38
|
+
timestamp: new Date().toISOString(),
|
|
39
|
+
data: {
|
|
40
|
+
notification_type: data.notification_type,
|
|
41
|
+
title: data.title,
|
|
42
|
+
message: data.message,
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
signal: AbortSignal.timeout(2000),
|
|
46
|
+
});
|
|
47
|
+
} catch {
|
|
48
|
+
// fire and forget
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
main();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (data.is_interrupt) return;
|
|
26
|
+
|
|
27
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await fetch(`${REST_URL}/agentmemory/observe`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: authHeaders(),
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
hookType: "post_tool_failure",
|
|
35
|
+
sessionId,
|
|
36
|
+
project: data.cwd || process.cwd(),
|
|
37
|
+
cwd: data.cwd || process.cwd(),
|
|
38
|
+
timestamp: new Date().toISOString(),
|
|
39
|
+
data: {
|
|
40
|
+
tool_name: data.tool_name,
|
|
41
|
+
tool_input:
|
|
42
|
+
typeof data.tool_input === "string"
|
|
43
|
+
? data.tool_input.slice(0, 4000)
|
|
44
|
+
: JSON.stringify(data.tool_input ?? "").slice(0, 4000),
|
|
45
|
+
error:
|
|
46
|
+
typeof data.error === "string"
|
|
47
|
+
? data.error.slice(0, 4000)
|
|
48
|
+
: JSON.stringify(data.error ?? "").slice(0, 4000),
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
signal: AbortSignal.timeout(3000),
|
|
52
|
+
});
|
|
53
|
+
} catch {
|
|
54
|
+
// fire and forget
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
main();
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
await fetch(`${REST_URL}/agentmemory/observe`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: authHeaders(),
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
hookType: "post_tool_use",
|
|
33
|
+
sessionId,
|
|
34
|
+
project: data.cwd || process.cwd(),
|
|
35
|
+
cwd: data.cwd || process.cwd(),
|
|
36
|
+
timestamp: new Date().toISOString(),
|
|
37
|
+
data: {
|
|
38
|
+
tool_name: data.tool_name,
|
|
39
|
+
tool_input: data.tool_input,
|
|
40
|
+
tool_output: truncate(data.tool_output, 8000),
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
signal: AbortSignal.timeout(3000),
|
|
44
|
+
});
|
|
45
|
+
} catch {
|
|
46
|
+
// fire and forget
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function truncate(value: unknown, max: number): unknown {
|
|
51
|
+
if (typeof value === "string" && value.length > max) {
|
|
52
|
+
return value.slice(0, max) + "\n[...truncated]";
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "object" && value !== null) {
|
|
55
|
+
const str = JSON.stringify(value);
|
|
56
|
+
if (str.length > max) return str.slice(0, max) + "...[truncated]";
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
main();
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
26
|
+
const project = (data.cwd as string) || process.cwd();
|
|
27
|
+
|
|
28
|
+
if (process.env["CLAUDE_MEMORY_BRIDGE"] === "true") {
|
|
29
|
+
try {
|
|
30
|
+
await fetch(`${REST_URL}/agentmemory/claude-bridge/sync`, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: authHeaders(),
|
|
33
|
+
body: JSON.stringify({}),
|
|
34
|
+
signal: AbortSignal.timeout(5000),
|
|
35
|
+
});
|
|
36
|
+
} catch {
|
|
37
|
+
// best-effort
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const res = await fetch(`${REST_URL}/agentmemory/context`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: authHeaders(),
|
|
45
|
+
body: JSON.stringify({ sessionId, project, budget: 1500 }),
|
|
46
|
+
signal: AbortSignal.timeout(5000),
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (res.ok) {
|
|
50
|
+
const result = (await res.json()) as { context?: string };
|
|
51
|
+
if (result.context) {
|
|
52
|
+
process.stdout.write(result.context);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
// best effort -- don't block compaction
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
main();
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const toolName = data.tool_name as string;
|
|
26
|
+
if (!toolName) return;
|
|
27
|
+
|
|
28
|
+
const fileTools = ["Edit", "Write", "Read", "Glob", "Grep"];
|
|
29
|
+
if (!fileTools.includes(toolName)) return;
|
|
30
|
+
|
|
31
|
+
const toolInput = (data.tool_input || {}) as Record<string, unknown>;
|
|
32
|
+
const files: string[] = [];
|
|
33
|
+
const fileKeys =
|
|
34
|
+
toolName === "Grep"
|
|
35
|
+
? ["path", "file"]
|
|
36
|
+
: ["file_path", "path", "file", "pattern"];
|
|
37
|
+
for (const key of fileKeys) {
|
|
38
|
+
const val = toolInput[key];
|
|
39
|
+
if (typeof val === "string" && val.length > 0) files.push(val);
|
|
40
|
+
}
|
|
41
|
+
if (files.length === 0) return;
|
|
42
|
+
|
|
43
|
+
const terms: string[] = [];
|
|
44
|
+
if (toolName === "Grep" || toolName === "Glob") {
|
|
45
|
+
const pattern = toolInput["pattern"];
|
|
46
|
+
if (typeof pattern === "string" && pattern.length > 0) {
|
|
47
|
+
terms.push(pattern);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const res = await fetch(`${REST_URL}/agentmemory/enrich`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: authHeaders(),
|
|
57
|
+
body: JSON.stringify({ sessionId, files, terms, toolName }),
|
|
58
|
+
signal: AbortSignal.timeout(2000),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (res.ok) {
|
|
62
|
+
const result = (await res.json()) as { context?: string };
|
|
63
|
+
if (result.context) {
|
|
64
|
+
process.stdout.write(result.context);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} catch {
|
|
68
|
+
// don't block tool execution
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
main();
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
4
|
+
const SECRET = process.env["AGENTMEMORY_SECRET"] || "";
|
|
5
|
+
|
|
6
|
+
function authHeaders(): Record<string, string> {
|
|
7
|
+
const h: Record<string, string> = { "Content-Type": "application/json" };
|
|
8
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
9
|
+
return h;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
let input = "";
|
|
14
|
+
for await (const chunk of process.stdin) {
|
|
15
|
+
input += chunk;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let data: Record<string, unknown>;
|
|
19
|
+
try {
|
|
20
|
+
data = JSON.parse(input);
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const sessionId = (data.session_id as string) || "unknown";
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
await fetch(`${REST_URL}/agentmemory/observe`, {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: authHeaders(),
|
|
31
|
+
body: JSON.stringify({
|
|
32
|
+
hookType: "prompt_submit",
|
|
33
|
+
sessionId,
|
|
34
|
+
project: data.cwd || process.cwd(),
|
|
35
|
+
cwd: data.cwd || process.cwd(),
|
|
36
|
+
timestamp: new Date().toISOString(),
|
|
37
|
+
data: { prompt: data.prompt },
|
|
38
|
+
}),
|
|
39
|
+
signal: AbortSignal.timeout(3000),
|
|
40
|
+
});
|
|
41
|
+
} catch {
|
|
42
|
+
// fire and forget
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
main();
|