@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,219 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import { execFile } from "node:child_process";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import type { SnapshotMeta, Session, Memory, GraphNode } from "../types.js";
|
|
8
|
+
import { KV, generateId } from "../state/schema.js";
|
|
9
|
+
import type { StateKV } from "../state/kv.js";
|
|
10
|
+
import { recordAudit } from "./audit.js";
|
|
11
|
+
import { VERSION } from "../version.js";
|
|
12
|
+
|
|
13
|
+
const COMMIT_HASH_RE = /^[0-9a-f]{7,40}$/i;
|
|
14
|
+
|
|
15
|
+
const execFileAsync = promisify(execFile);
|
|
16
|
+
|
|
17
|
+
async function gitExec(dir: string, args: string[]): Promise<string> {
|
|
18
|
+
const { stdout } = await execFileAsync("git", args, { cwd: dir });
|
|
19
|
+
return stdout.trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function ensureGitRepo(dir: string): Promise<void> {
|
|
23
|
+
if (!existsSync(dir)) {
|
|
24
|
+
mkdirSync(dir, { recursive: true });
|
|
25
|
+
}
|
|
26
|
+
if (!existsSync(join(dir, ".git"))) {
|
|
27
|
+
await gitExec(dir, ["init"]);
|
|
28
|
+
await gitExec(dir, ["config", "user.email", "agentmemory@local"]);
|
|
29
|
+
await gitExec(dir, ["config", "user.name", "agentmemory"]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function registerSnapshotFunction(
|
|
34
|
+
sdk: ISdk,
|
|
35
|
+
kv: StateKV,
|
|
36
|
+
snapshotDir: string,
|
|
37
|
+
): void {
|
|
38
|
+
sdk.registerFunction(
|
|
39
|
+
{ id: "mem::snapshot-create" },
|
|
40
|
+
async (data?: { message?: string }) => {
|
|
41
|
+
const ctx = getContext();
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
await ensureGitRepo(snapshotDir);
|
|
45
|
+
|
|
46
|
+
const sessions = await kv.list<Session>(KV.sessions);
|
|
47
|
+
const memories = await kv.list<Memory>(KV.memories);
|
|
48
|
+
const graphNodes = await kv.list<GraphNode>(KV.graphNodes);
|
|
49
|
+
|
|
50
|
+
const observations: Record<string, unknown[]> = {};
|
|
51
|
+
for (const session of sessions) {
|
|
52
|
+
const obs = await kv
|
|
53
|
+
.list(KV.observations(session.id))
|
|
54
|
+
.catch(() => []);
|
|
55
|
+
if (obs.length > 0) {
|
|
56
|
+
observations[session.id] = obs;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const state = {
|
|
61
|
+
version: VERSION,
|
|
62
|
+
timestamp: new Date().toISOString(),
|
|
63
|
+
sessions,
|
|
64
|
+
memories,
|
|
65
|
+
graphNodes,
|
|
66
|
+
observations,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
writeFileSync(
|
|
70
|
+
join(snapshotDir, "state.json"),
|
|
71
|
+
JSON.stringify(state, null, 2),
|
|
72
|
+
"utf-8",
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
await gitExec(snapshotDir, ["add", "."]);
|
|
76
|
+
|
|
77
|
+
const message = data?.message || `Snapshot ${new Date().toISOString()}`;
|
|
78
|
+
try {
|
|
79
|
+
await gitExec(snapshotDir, ["commit", "-m", message]);
|
|
80
|
+
} catch (commitErr) {
|
|
81
|
+
const errMsg =
|
|
82
|
+
commitErr instanceof Error ? commitErr.message : String(commitErr);
|
|
83
|
+
if (errMsg.includes("nothing to commit")) {
|
|
84
|
+
return { success: true, message: "No changes to snapshot" };
|
|
85
|
+
}
|
|
86
|
+
throw commitErr;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const commitHash = await gitExec(snapshotDir, ["rev-parse", "HEAD"]);
|
|
90
|
+
|
|
91
|
+
const meta: SnapshotMeta = {
|
|
92
|
+
id: generateId("snap"),
|
|
93
|
+
commitHash,
|
|
94
|
+
createdAt: new Date().toISOString(),
|
|
95
|
+
message,
|
|
96
|
+
stats: {
|
|
97
|
+
sessions: sessions.length,
|
|
98
|
+
observations: Object.values(observations).reduce(
|
|
99
|
+
(sum, arr) => sum + arr.length,
|
|
100
|
+
0,
|
|
101
|
+
),
|
|
102
|
+
memories: memories.length,
|
|
103
|
+
graphNodes: graphNodes.length,
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
await recordAudit(kv, "export", "mem::snapshot-create", [meta.id], {
|
|
108
|
+
commitHash,
|
|
109
|
+
stats: meta.stats,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
ctx.logger.info("Snapshot created", { commitHash });
|
|
113
|
+
return { success: true, snapshot: meta };
|
|
114
|
+
} catch (err) {
|
|
115
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
116
|
+
ctx.logger.error("Snapshot failed", { error: msg });
|
|
117
|
+
return { success: false, error: msg };
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
sdk.registerFunction({ id: "mem::snapshot-list" }, async () => {
|
|
123
|
+
try {
|
|
124
|
+
if (!existsSync(join(snapshotDir, ".git"))) {
|
|
125
|
+
return { snapshots: [] };
|
|
126
|
+
}
|
|
127
|
+
const log = await gitExec(snapshotDir, [
|
|
128
|
+
"log",
|
|
129
|
+
"--format=%H|%aI|%s",
|
|
130
|
+
"-20",
|
|
131
|
+
]);
|
|
132
|
+
const snapshots = log
|
|
133
|
+
.split("\n")
|
|
134
|
+
.filter(Boolean)
|
|
135
|
+
.map((line) => {
|
|
136
|
+
const parts = line.split("|");
|
|
137
|
+
const [hash, date] = parts;
|
|
138
|
+
const msg = parts.slice(2).join("|");
|
|
139
|
+
return { commitHash: hash, createdAt: date, message: msg };
|
|
140
|
+
});
|
|
141
|
+
return { snapshots };
|
|
142
|
+
} catch {
|
|
143
|
+
return { snapshots: [] };
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
sdk.registerFunction(
|
|
148
|
+
{ id: "mem::snapshot-restore" },
|
|
149
|
+
async (data: { commitHash: string }) => {
|
|
150
|
+
const ctx = getContext();
|
|
151
|
+
if (!data.commitHash) {
|
|
152
|
+
return { success: false, error: "commitHash is required" };
|
|
153
|
+
}
|
|
154
|
+
if (!COMMIT_HASH_RE.test(data.commitHash)) {
|
|
155
|
+
return { success: false, error: "Invalid commitHash format" };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
await gitExec(snapshotDir, [
|
|
160
|
+
"checkout",
|
|
161
|
+
data.commitHash,
|
|
162
|
+
"--",
|
|
163
|
+
"state.json",
|
|
164
|
+
]);
|
|
165
|
+
const content = readFileSync(join(snapshotDir, "state.json"), "utf-8");
|
|
166
|
+
const state = JSON.parse(content) as {
|
|
167
|
+
sessions?: Array<{ id: string } & Record<string, unknown>>;
|
|
168
|
+
memories?: Array<{ id: string } & Record<string, unknown>>;
|
|
169
|
+
graphNodes?: Array<{ id: string } & Record<string, unknown>>;
|
|
170
|
+
observations?: Record<
|
|
171
|
+
string,
|
|
172
|
+
Array<{ id: string } & Record<string, unknown>>
|
|
173
|
+
>;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
if (state.sessions) {
|
|
177
|
+
for (const session of state.sessions) {
|
|
178
|
+
await kv.set(KV.sessions, session.id, session);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (state.memories) {
|
|
182
|
+
for (const memory of state.memories) {
|
|
183
|
+
await kv.set(KV.memories, memory.id, memory);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (state.graphNodes) {
|
|
187
|
+
for (const node of state.graphNodes) {
|
|
188
|
+
await kv.set(KV.graphNodes, node.id, node);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (state.observations) {
|
|
192
|
+
for (const [sessionId, obs] of Object.entries(state.observations)) {
|
|
193
|
+
for (const o of obs) {
|
|
194
|
+
await kv.set(KV.observations(sessionId), o.id, o);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
await gitExec(snapshotDir, ["checkout", "HEAD", "--", "state.json"]);
|
|
200
|
+
|
|
201
|
+
await recordAudit(kv, "import", "mem::snapshot-restore", [], {
|
|
202
|
+
commitHash: data.commitHash,
|
|
203
|
+
sessions: state.sessions?.length || 0,
|
|
204
|
+
memories: state.memories?.length || 0,
|
|
205
|
+
graphNodes: state.graphNodes?.length || 0,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
ctx.logger.info("Snapshot restored", {
|
|
209
|
+
commitHash: data.commitHash,
|
|
210
|
+
});
|
|
211
|
+
return { success: true, commitHash: data.commitHash };
|
|
212
|
+
} catch (err) {
|
|
213
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
214
|
+
ctx.logger.error("Snapshot restore failed", { error: msg });
|
|
215
|
+
return { success: false, error: msg };
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
);
|
|
219
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import type {
|
|
4
|
+
CompressedObservation,
|
|
5
|
+
SessionSummary,
|
|
6
|
+
MemoryProvider,
|
|
7
|
+
Session,
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
import { KV } from "../state/schema.js";
|
|
10
|
+
import { StateKV } from "../state/kv.js";
|
|
11
|
+
import { SUMMARY_SYSTEM, buildSummaryPrompt } from "../prompts/summary.js";
|
|
12
|
+
import { getXmlTag, getXmlChildren } from "../prompts/xml.js";
|
|
13
|
+
import { SummaryOutputSchema } from "../eval/schemas.js";
|
|
14
|
+
import { validateOutput } from "../eval/validator.js";
|
|
15
|
+
import { scoreSummary } from "../eval/quality.js";
|
|
16
|
+
import type { MetricsStore } from "../eval/metrics-store.js";
|
|
17
|
+
|
|
18
|
+
function parseSummaryXml(
|
|
19
|
+
xml: string,
|
|
20
|
+
sessionId: string,
|
|
21
|
+
project: string,
|
|
22
|
+
obsCount: number,
|
|
23
|
+
): SessionSummary | null {
|
|
24
|
+
const title = getXmlTag(xml, "title");
|
|
25
|
+
if (!title) return null;
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
sessionId,
|
|
29
|
+
project,
|
|
30
|
+
createdAt: new Date().toISOString(),
|
|
31
|
+
title,
|
|
32
|
+
narrative: getXmlTag(xml, "narrative"),
|
|
33
|
+
keyDecisions: getXmlChildren(xml, "decisions", "decision"),
|
|
34
|
+
filesModified: getXmlChildren(xml, "files", "file"),
|
|
35
|
+
concepts: getXmlChildren(xml, "concepts", "concept"),
|
|
36
|
+
observationCount: obsCount,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function registerSummarizeFunction(
|
|
41
|
+
sdk: ISdk,
|
|
42
|
+
kv: StateKV,
|
|
43
|
+
provider: MemoryProvider,
|
|
44
|
+
metricsStore?: MetricsStore,
|
|
45
|
+
): void {
|
|
46
|
+
sdk.registerFunction(
|
|
47
|
+
{ id: "mem::summarize", description: "Generate end-of-session summary" },
|
|
48
|
+
async (data: { sessionId: string }) => {
|
|
49
|
+
const ctx = getContext();
|
|
50
|
+
const startMs = Date.now();
|
|
51
|
+
|
|
52
|
+
const session = await kv.get<Session>(KV.sessions, data.sessionId);
|
|
53
|
+
if (!session) {
|
|
54
|
+
ctx.logger.warn("Session not found for summarize", {
|
|
55
|
+
sessionId: data.sessionId,
|
|
56
|
+
});
|
|
57
|
+
return { success: false, error: "session_not_found" };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const observations = await kv.list<CompressedObservation>(
|
|
61
|
+
KV.observations(data.sessionId),
|
|
62
|
+
);
|
|
63
|
+
const compressed = observations.filter((o) => o.title);
|
|
64
|
+
|
|
65
|
+
if (compressed.length === 0) {
|
|
66
|
+
ctx.logger.info("No observations to summarize", {
|
|
67
|
+
sessionId: data.sessionId,
|
|
68
|
+
});
|
|
69
|
+
return { success: false, error: "no_observations" };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const prompt = buildSummaryPrompt(compressed);
|
|
74
|
+
const response = await provider.summarize(SUMMARY_SYSTEM, prompt);
|
|
75
|
+
const summary = parseSummaryXml(
|
|
76
|
+
response,
|
|
77
|
+
data.sessionId,
|
|
78
|
+
session.project,
|
|
79
|
+
compressed.length,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (!summary) {
|
|
83
|
+
const latencyMs = Date.now() - startMs;
|
|
84
|
+
if (metricsStore) {
|
|
85
|
+
await metricsStore.record("mem::summarize", latencyMs, false);
|
|
86
|
+
}
|
|
87
|
+
ctx.logger.warn("Failed to parse summary XML", {
|
|
88
|
+
sessionId: data.sessionId,
|
|
89
|
+
});
|
|
90
|
+
return { success: false, error: "parse_failed" };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const summaryForValidation = {
|
|
94
|
+
title: summary.title,
|
|
95
|
+
narrative: summary.narrative,
|
|
96
|
+
keyDecisions: summary.keyDecisions,
|
|
97
|
+
filesModified: summary.filesModified,
|
|
98
|
+
concepts: summary.concepts,
|
|
99
|
+
};
|
|
100
|
+
const validation = validateOutput(
|
|
101
|
+
SummaryOutputSchema,
|
|
102
|
+
summaryForValidation,
|
|
103
|
+
"mem::summarize",
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (!validation.valid) {
|
|
107
|
+
const latencyMs = Date.now() - startMs;
|
|
108
|
+
if (metricsStore) {
|
|
109
|
+
await metricsStore.record("mem::summarize", latencyMs, false);
|
|
110
|
+
}
|
|
111
|
+
ctx.logger.warn("Summary validation failed", {
|
|
112
|
+
sessionId: data.sessionId,
|
|
113
|
+
errors: validation.result.errors,
|
|
114
|
+
});
|
|
115
|
+
return { success: false, error: "validation_failed" };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const qualityScore = scoreSummary(summaryForValidation);
|
|
119
|
+
|
|
120
|
+
await kv.set(KV.summaries, data.sessionId, summary);
|
|
121
|
+
|
|
122
|
+
const latencyMs = Date.now() - startMs;
|
|
123
|
+
if (metricsStore) {
|
|
124
|
+
await metricsStore.record(
|
|
125
|
+
"mem::summarize",
|
|
126
|
+
latencyMs,
|
|
127
|
+
true,
|
|
128
|
+
qualityScore,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
ctx.logger.info("Session summarized", {
|
|
133
|
+
sessionId: data.sessionId,
|
|
134
|
+
title: summary.title,
|
|
135
|
+
decisions: summary.keyDecisions.length,
|
|
136
|
+
qualityScore,
|
|
137
|
+
valid: validation.valid,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return { success: true, summary, qualityScore };
|
|
141
|
+
} catch (err) {
|
|
142
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
143
|
+
const latencyMs = Date.now() - startMs;
|
|
144
|
+
if (metricsStore) {
|
|
145
|
+
await metricsStore.record("mem::summarize", latencyMs, false);
|
|
146
|
+
}
|
|
147
|
+
ctx.logger.error("Summarize failed", {
|
|
148
|
+
sessionId: data.sessionId,
|
|
149
|
+
error: msg,
|
|
150
|
+
});
|
|
151
|
+
return { success: false, error: msg };
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import { getContext } from "iii-sdk";
|
|
3
|
+
import type {
|
|
4
|
+
TeamConfig,
|
|
5
|
+
TeamSharedItem,
|
|
6
|
+
TeamProfile,
|
|
7
|
+
Memory,
|
|
8
|
+
} from "../types.js";
|
|
9
|
+
import { KV, generateId } from "../state/schema.js";
|
|
10
|
+
import type { StateKV } from "../state/kv.js";
|
|
11
|
+
import { recordAudit } from "./audit.js";
|
|
12
|
+
|
|
13
|
+
const VALID_ITEM_TYPES = new Set(["memory", "pattern", "observation"]);
|
|
14
|
+
|
|
15
|
+
export function registerTeamFunction(
|
|
16
|
+
sdk: ISdk,
|
|
17
|
+
kv: StateKV,
|
|
18
|
+
config: TeamConfig,
|
|
19
|
+
): void {
|
|
20
|
+
sdk.registerFunction(
|
|
21
|
+
{ id: "mem::team-share" },
|
|
22
|
+
async (data: {
|
|
23
|
+
itemId: string;
|
|
24
|
+
itemType: "memory" | "pattern" | "observation";
|
|
25
|
+
sessionId?: string;
|
|
26
|
+
project?: string;
|
|
27
|
+
}) => {
|
|
28
|
+
const ctx = getContext();
|
|
29
|
+
if (!data.itemId || !data.itemType) {
|
|
30
|
+
return { success: false, error: "itemId and itemType are required" };
|
|
31
|
+
}
|
|
32
|
+
if (!VALID_ITEM_TYPES.has(data.itemType)) {
|
|
33
|
+
return { success: false, error: `Invalid itemType: ${data.itemType}` };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let content: unknown;
|
|
37
|
+
if (data.itemType === "observation") {
|
|
38
|
+
if (!data.sessionId) {
|
|
39
|
+
return {
|
|
40
|
+
success: false,
|
|
41
|
+
error: "sessionId is required for observations",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
content = await kv.get(KV.observations(data.sessionId), data.itemId);
|
|
45
|
+
} else {
|
|
46
|
+
content = await kv.get<Memory>(KV.memories, data.itemId);
|
|
47
|
+
}
|
|
48
|
+
if (!content) {
|
|
49
|
+
return { success: false, error: "Item not found" };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const shared: TeamSharedItem = {
|
|
53
|
+
id: generateId("ts"),
|
|
54
|
+
sharedBy: config.userId,
|
|
55
|
+
sharedAt: new Date().toISOString(),
|
|
56
|
+
type: data.itemType,
|
|
57
|
+
content,
|
|
58
|
+
project: data.project || "",
|
|
59
|
+
visibility: "shared",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
await kv.set(KV.teamShared(config.teamId), shared.id, shared);
|
|
63
|
+
|
|
64
|
+
await recordAudit(kv, "share", "mem::team-share", [data.itemId], {
|
|
65
|
+
teamId: config.teamId,
|
|
66
|
+
userId: config.userId,
|
|
67
|
+
itemType: data.itemType,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
ctx.logger.info("Team share", {
|
|
71
|
+
teamId: config.teamId,
|
|
72
|
+
itemId: data.itemId,
|
|
73
|
+
});
|
|
74
|
+
return { success: true, sharedItem: shared };
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
sdk.registerFunction(
|
|
79
|
+
{ id: "mem::team-feed" },
|
|
80
|
+
async (data?: { limit?: number }) => {
|
|
81
|
+
const limit = data?.limit || 20;
|
|
82
|
+
const items = await kv.list<TeamSharedItem>(KV.teamShared(config.teamId));
|
|
83
|
+
|
|
84
|
+
const filtered = items.filter((i) => i.visibility === "shared");
|
|
85
|
+
const sorted = filtered
|
|
86
|
+
.sort(
|
|
87
|
+
(a, b) =>
|
|
88
|
+
new Date(b.sharedAt).getTime() - new Date(a.sharedAt).getTime(),
|
|
89
|
+
)
|
|
90
|
+
.slice(0, limit);
|
|
91
|
+
|
|
92
|
+
return { items: sorted, total: filtered.length };
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
sdk.registerFunction({ id: "mem::team-profile" }, async () => {
|
|
97
|
+
const items = await kv.list<TeamSharedItem>(KV.teamShared(config.teamId));
|
|
98
|
+
|
|
99
|
+
const members = [...new Set(items.map((i) => i.sharedBy))];
|
|
100
|
+
|
|
101
|
+
const conceptCounts = new Map<string, number>();
|
|
102
|
+
const fileCounts = new Map<string, number>();
|
|
103
|
+
const patterns: string[] = [];
|
|
104
|
+
|
|
105
|
+
for (const item of items) {
|
|
106
|
+
if (item.type === "memory" || item.type === "pattern") {
|
|
107
|
+
const mem = item.content as Memory;
|
|
108
|
+
if (mem?.concepts) {
|
|
109
|
+
for (const c of mem.concepts) {
|
|
110
|
+
conceptCounts.set(c, (conceptCounts.get(c) || 0) + 1);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (mem?.files) {
|
|
114
|
+
for (const f of mem.files) {
|
|
115
|
+
fileCounts.set(f, (fileCounts.get(f) || 0) + 1);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (item.type === "pattern" && mem?.content) {
|
|
119
|
+
patterns.push(mem.content.slice(0, 100));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const topConcepts = [...conceptCounts.entries()]
|
|
125
|
+
.sort((a, b) => b[1] - a[1])
|
|
126
|
+
.slice(0, 10)
|
|
127
|
+
.map(([concept, frequency]) => ({ concept, frequency }));
|
|
128
|
+
|
|
129
|
+
const topFiles = [...fileCounts.entries()]
|
|
130
|
+
.sort((a, b) => b[1] - a[1])
|
|
131
|
+
.slice(0, 10)
|
|
132
|
+
.map(([file, frequency]) => ({ file, frequency }));
|
|
133
|
+
|
|
134
|
+
const profile: TeamProfile = {
|
|
135
|
+
teamId: config.teamId,
|
|
136
|
+
members,
|
|
137
|
+
topConcepts,
|
|
138
|
+
topFiles,
|
|
139
|
+
sharedPatterns: patterns.slice(0, 10),
|
|
140
|
+
totalSharedItems: items.length,
|
|
141
|
+
updatedAt: new Date().toISOString(),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
await kv.set(KV.teamProfile(config.teamId), "profile", profile);
|
|
145
|
+
return profile;
|
|
146
|
+
});
|
|
147
|
+
}
|