@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,289 @@
|
|
|
1
|
+
import type { ISdk } from "iii-sdk";
|
|
2
|
+
import type { StateKV } from "../state/kv.js";
|
|
3
|
+
import { KV, generateId } from "../state/schema.js";
|
|
4
|
+
import { withKeyedLock } from "../state/keyed-mutex.js";
|
|
5
|
+
import type { Action, Routine, RoutineStep, RoutineRun } from "../types.js";
|
|
6
|
+
|
|
7
|
+
export function registerRoutinesFunction(sdk: ISdk, kv: StateKV): void {
|
|
8
|
+
sdk.registerFunction(
|
|
9
|
+
{ id: "mem::routine-create" },
|
|
10
|
+
async (data: {
|
|
11
|
+
name: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
steps: RoutineStep[];
|
|
14
|
+
tags?: string[];
|
|
15
|
+
frozen?: boolean;
|
|
16
|
+
sourceProceduralIds?: string[];
|
|
17
|
+
}) => {
|
|
18
|
+
if (!data.name || !Array.isArray(data.steps) || data.steps.length === 0) {
|
|
19
|
+
return { success: false, error: "name and steps are required" };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < data.steps.length; i++) {
|
|
23
|
+
if (!data.steps[i].title?.trim()) {
|
|
24
|
+
return { success: false, error: `step ${i} must have a title` };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const orders = data.steps.map((s, i) => s.order ?? i);
|
|
29
|
+
const uniqueOrders = new Set(orders);
|
|
30
|
+
if (uniqueOrders.size !== orders.length) {
|
|
31
|
+
return { success: false, error: "duplicate step orders" };
|
|
32
|
+
}
|
|
33
|
+
for (const step of data.steps) {
|
|
34
|
+
if (step.dependsOn) {
|
|
35
|
+
for (const dep of step.dependsOn) {
|
|
36
|
+
if (!uniqueOrders.has(dep)) {
|
|
37
|
+
return { success: false, error: `step ${step.order ?? data.steps.indexOf(step)} depends on unknown order ${dep}` };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const now = new Date().toISOString();
|
|
44
|
+
const routine: Routine = {
|
|
45
|
+
id: generateId("rtn"),
|
|
46
|
+
name: data.name.trim(),
|
|
47
|
+
description: (data.description || "").trim(),
|
|
48
|
+
steps: data.steps.map((s, i) => ({
|
|
49
|
+
order: s.order ?? i,
|
|
50
|
+
title: s.title,
|
|
51
|
+
description: s.description || "",
|
|
52
|
+
actionTemplate: s.actionTemplate || {},
|
|
53
|
+
dependsOn: s.dependsOn || [],
|
|
54
|
+
})),
|
|
55
|
+
createdAt: now,
|
|
56
|
+
updatedAt: now,
|
|
57
|
+
frozen: data.frozen ?? true,
|
|
58
|
+
tags: data.tags || [],
|
|
59
|
+
sourceProceduralIds: data.sourceProceduralIds || [],
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
await kv.set(KV.routines, routine.id, routine);
|
|
63
|
+
return { success: true, routine };
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
sdk.registerFunction(
|
|
68
|
+
{ id: "mem::routine-list" },
|
|
69
|
+
async (data: { frozen?: boolean; tags?: string[] }) => {
|
|
70
|
+
let routines = await kv.list<Routine>(KV.routines);
|
|
71
|
+
if (data.frozen !== undefined) {
|
|
72
|
+
routines = routines.filter((r) => r.frozen === data.frozen);
|
|
73
|
+
}
|
|
74
|
+
if (data.tags && data.tags.length > 0) {
|
|
75
|
+
routines = routines.filter((r) =>
|
|
76
|
+
data.tags!.some((t) => r.tags.includes(t)),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
routines.sort(
|
|
80
|
+
(a, b) =>
|
|
81
|
+
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(),
|
|
82
|
+
);
|
|
83
|
+
return { success: true, routines };
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
sdk.registerFunction(
|
|
88
|
+
{ id: "mem::routine-run" },
|
|
89
|
+
async (data: {
|
|
90
|
+
routineId: string;
|
|
91
|
+
initiatedBy?: string;
|
|
92
|
+
project?: string;
|
|
93
|
+
overrides?: Record<number, Partial<Action>>;
|
|
94
|
+
}) => {
|
|
95
|
+
if (!data.routineId) {
|
|
96
|
+
return { success: false, error: "routineId is required" };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return withKeyedLock(`mem:routine:${data.routineId}`, async () => {
|
|
100
|
+
const routine = await kv.get<Routine>(KV.routines, data.routineId);
|
|
101
|
+
if (!routine) {
|
|
102
|
+
return { success: false, error: "routine not found" };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const now = new Date().toISOString();
|
|
106
|
+
const stepOrderToActionId = new Map<number, string>();
|
|
107
|
+
const actionIds: string[] = [];
|
|
108
|
+
const stepStatus: Record<number, "pending" | "active" | "done" | "failed"> = {};
|
|
109
|
+
|
|
110
|
+
for (const step of routine.steps) {
|
|
111
|
+
const template = step.actionTemplate || {};
|
|
112
|
+
const override = data.overrides?.[step.order] || {};
|
|
113
|
+
|
|
114
|
+
const hasDeps = (step.dependsOn || []).length > 0;
|
|
115
|
+
const action: Action = {
|
|
116
|
+
id: generateId("act"),
|
|
117
|
+
title: override.title || template.title || step.title,
|
|
118
|
+
description:
|
|
119
|
+
override.description ||
|
|
120
|
+
template.description ||
|
|
121
|
+
step.description,
|
|
122
|
+
status: hasDeps ? "blocked" : "pending",
|
|
123
|
+
priority:
|
|
124
|
+
override.priority ?? template.priority ?? 5,
|
|
125
|
+
createdAt: now,
|
|
126
|
+
updatedAt: now,
|
|
127
|
+
createdBy: data.initiatedBy || "routine",
|
|
128
|
+
project: data.project || template.project,
|
|
129
|
+
tags: [
|
|
130
|
+
...(template.tags || []),
|
|
131
|
+
...(override.tags || []),
|
|
132
|
+
`routine:${routine.id}`,
|
|
133
|
+
],
|
|
134
|
+
sourceObservationIds: [],
|
|
135
|
+
sourceMemoryIds: [],
|
|
136
|
+
metadata: { routineId: routine.id, stepOrder: step.order },
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
await kv.set(KV.actions, action.id, action);
|
|
140
|
+
stepOrderToActionId.set(step.order, action.id);
|
|
141
|
+
actionIds.push(action.id);
|
|
142
|
+
stepStatus[step.order] = "pending";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
for (const step of routine.steps) {
|
|
146
|
+
const actionId = stepOrderToActionId.get(step.order);
|
|
147
|
+
if (!actionId) continue;
|
|
148
|
+
|
|
149
|
+
for (const depOrder of step.dependsOn) {
|
|
150
|
+
const depActionId = stepOrderToActionId.get(depOrder);
|
|
151
|
+
if (!depActionId) continue;
|
|
152
|
+
const edge = {
|
|
153
|
+
id: generateId("ae"),
|
|
154
|
+
type: "requires" as const,
|
|
155
|
+
sourceActionId: actionId,
|
|
156
|
+
targetActionId: depActionId,
|
|
157
|
+
createdAt: now,
|
|
158
|
+
};
|
|
159
|
+
await kv.set(KV.actionEdges, edge.id, edge);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const run: RoutineRun = {
|
|
164
|
+
id: generateId("run"),
|
|
165
|
+
routineId: routine.id,
|
|
166
|
+
status: "running",
|
|
167
|
+
startedAt: now,
|
|
168
|
+
actionIds,
|
|
169
|
+
stepStatus,
|
|
170
|
+
initiatedBy: data.initiatedBy || "unknown",
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
await kv.set(KV.routineRuns, run.id, run);
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
success: true,
|
|
177
|
+
run,
|
|
178
|
+
actionsCreated: actionIds.length,
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
sdk.registerFunction(
|
|
185
|
+
{ id: "mem::routine-status" },
|
|
186
|
+
async (data: { runId: string }) => {
|
|
187
|
+
if (!data.runId) {
|
|
188
|
+
return { success: false, error: "runId is required" };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const run = await kv.get<RoutineRun>(KV.routineRuns, data.runId);
|
|
192
|
+
if (!run) {
|
|
193
|
+
return { success: false, error: "run not found" };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const actionStates: Array<{
|
|
197
|
+
actionId: string;
|
|
198
|
+
status: string;
|
|
199
|
+
title: string;
|
|
200
|
+
}> = [];
|
|
201
|
+
let allDone = true;
|
|
202
|
+
let anyFailed = false;
|
|
203
|
+
|
|
204
|
+
let statusChanged = false;
|
|
205
|
+
for (const actionId of run.actionIds) {
|
|
206
|
+
const action = await kv.get<Action>(KV.actions, actionId);
|
|
207
|
+
if (action) {
|
|
208
|
+
actionStates.push({
|
|
209
|
+
actionId: action.id,
|
|
210
|
+
status: action.status,
|
|
211
|
+
title: action.title,
|
|
212
|
+
});
|
|
213
|
+
if (action.status !== "done") allDone = false;
|
|
214
|
+
if (action.status === "cancelled") anyFailed = true;
|
|
215
|
+
|
|
216
|
+
const stepOrder = (action.metadata as { stepOrder?: number })?.stepOrder;
|
|
217
|
+
if (stepOrder !== undefined && stepOrder in run.stepStatus) {
|
|
218
|
+
let mapped: "pending" | "active" | "done" | "failed";
|
|
219
|
+
if (action.status === "cancelled") {
|
|
220
|
+
mapped = "failed";
|
|
221
|
+
} else if (action.status === "blocked") {
|
|
222
|
+
mapped = "pending";
|
|
223
|
+
} else {
|
|
224
|
+
mapped = action.status as "pending" | "active" | "done";
|
|
225
|
+
}
|
|
226
|
+
if (run.stepStatus[stepOrder] !== mapped) {
|
|
227
|
+
run.stepStatus[stepOrder] = mapped;
|
|
228
|
+
statusChanged = true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
actionStates.push({
|
|
233
|
+
actionId,
|
|
234
|
+
status: "cancelled",
|
|
235
|
+
title: "(missing)",
|
|
236
|
+
});
|
|
237
|
+
allDone = false;
|
|
238
|
+
anyFailed = true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (allDone && run.status === "running") {
|
|
243
|
+
run.status = "completed";
|
|
244
|
+
run.completedAt = new Date().toISOString();
|
|
245
|
+
statusChanged = true;
|
|
246
|
+
} else if (anyFailed && run.status === "running") {
|
|
247
|
+
run.status = "failed";
|
|
248
|
+
statusChanged = true;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (statusChanged) {
|
|
252
|
+
await kv.set(KV.routineRuns, run.id, run);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
success: true,
|
|
257
|
+
run,
|
|
258
|
+
actions: actionStates,
|
|
259
|
+
progress: {
|
|
260
|
+
total: run.actionIds.length,
|
|
261
|
+
done: actionStates.filter((a) => a.status === "done").length,
|
|
262
|
+
active: actionStates.filter((a) => a.status === "active").length,
|
|
263
|
+
pending: actionStates.filter((a) => a.status === "pending").length,
|
|
264
|
+
blocked: actionStates.filter((a) => a.status === "blocked").length,
|
|
265
|
+
cancelled: actionStates.filter((a) => a.status === "cancelled").length,
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
},
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
sdk.registerFunction(
|
|
272
|
+
{ id: "mem::routine-freeze" },
|
|
273
|
+
async (data: { routineId: string }) => {
|
|
274
|
+
if (!data.routineId) {
|
|
275
|
+
return { success: false, error: "routineId is required" };
|
|
276
|
+
}
|
|
277
|
+
return withKeyedLock(`mem:routine:${data.routineId}`, async () => {
|
|
278
|
+
const routine = await kv.get<Routine>(KV.routines, data.routineId);
|
|
279
|
+
if (!routine) {
|
|
280
|
+
return { success: false, error: "routine not found" };
|
|
281
|
+
}
|
|
282
|
+
routine.frozen = true;
|
|
283
|
+
routine.updatedAt = new Date().toISOString();
|
|
284
|
+
await kv.set(KV.routines, routine.id, routine);
|
|
285
|
+
return { success: true, routine };
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
);
|
|
289
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { ISdk } from 'iii-sdk'
|
|
2
|
+
import { getContext } from 'iii-sdk'
|
|
3
|
+
import type { CompressedObservation, SearchResult, Session } from '../types.js'
|
|
4
|
+
import { KV } from '../state/schema.js'
|
|
5
|
+
import { StateKV } from '../state/kv.js'
|
|
6
|
+
import { SearchIndex } from '../state/search-index.js'
|
|
7
|
+
|
|
8
|
+
let index: SearchIndex | null = null
|
|
9
|
+
|
|
10
|
+
export function getSearchIndex(): SearchIndex {
|
|
11
|
+
if (!index) index = new SearchIndex()
|
|
12
|
+
return index
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function rebuildIndex(kv: StateKV): Promise<number> {
|
|
16
|
+
const idx = getSearchIndex()
|
|
17
|
+
idx.clear()
|
|
18
|
+
|
|
19
|
+
const sessions = await kv.list<Session>(KV.sessions)
|
|
20
|
+
if (!sessions.length) return 0
|
|
21
|
+
|
|
22
|
+
let count = 0
|
|
23
|
+
const obsPerSession: CompressedObservation[][] = []
|
|
24
|
+
const failedSessions: string[] = []
|
|
25
|
+
for (let batch = 0; batch < sessions.length; batch += 10) {
|
|
26
|
+
const chunk = sessions.slice(batch, batch + 10)
|
|
27
|
+
const results = await Promise.all(
|
|
28
|
+
chunk.map(async (s) => {
|
|
29
|
+
try {
|
|
30
|
+
return await kv.list<CompressedObservation>(KV.observations(s.id))
|
|
31
|
+
} catch {
|
|
32
|
+
failedSessions.push(s.id)
|
|
33
|
+
return [] as CompressedObservation[]
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
obsPerSession.push(...results)
|
|
38
|
+
}
|
|
39
|
+
if (failedSessions.length > 0) {
|
|
40
|
+
const ctx = getContext()
|
|
41
|
+
ctx.logger.warn('rebuildIndex: failed to load observations for sessions', { failedSessions })
|
|
42
|
+
}
|
|
43
|
+
for (const observations of obsPerSession) {
|
|
44
|
+
for (const obs of observations) {
|
|
45
|
+
if (obs.title && obs.narrative) {
|
|
46
|
+
idx.add(obs)
|
|
47
|
+
count++
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return count
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function registerSearchFunction(sdk: ISdk, kv: StateKV): void {
|
|
55
|
+
sdk.registerFunction(
|
|
56
|
+
{ id: 'mem::search', description: 'Search observations by keyword' },
|
|
57
|
+
async (data: { query: string; limit?: number }) => {
|
|
58
|
+
const ctx = getContext()
|
|
59
|
+
const idx = getSearchIndex()
|
|
60
|
+
|
|
61
|
+
if (idx.size === 0) {
|
|
62
|
+
const count = await rebuildIndex(kv)
|
|
63
|
+
ctx.logger.info('Search index rebuilt', { entries: count })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const results = idx.search(data.query, data.limit || 20)
|
|
67
|
+
|
|
68
|
+
const enriched: SearchResult[] = []
|
|
69
|
+
for (const r of results) {
|
|
70
|
+
const obs = await kv.get<CompressedObservation>(KV.observations(r.sessionId), r.obsId)
|
|
71
|
+
if (obs) {
|
|
72
|
+
enriched.push({ observation: obs, score: r.score, sessionId: r.sessionId })
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
ctx.logger.info('Search completed', { query: data.query, results: enriched.length })
|
|
77
|
+
return { results: enriched }
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
}
|