nexorum 0.0.0-stage → 0.2.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/LICENSE +9 -0
- package/README.md +144 -2
- package/bin/nexorum.js +12 -0
- package/package.json +46 -4
- package/src/agent-dream.js +277 -0
- package/src/agents-md.js +80 -0
- package/src/args.js +71 -0
- package/src/cli.js +278 -0
- package/src/core/basics.js +88 -0
- package/src/core/diff.js +63 -0
- package/src/core/dream/agent.js +109 -0
- package/src/core/dream/engine.js +223 -0
- package/src/core/dream/prompts.js +112 -0
- package/src/core/dream/tools.js +348 -0
- package/src/core/estimate.js +57 -0
- package/src/core/index.js +13 -0
- package/src/core/providers/http.js +143 -0
- package/src/core/providers/index.js +179 -0
- package/src/core/scrub.js +82 -0
- package/src/core/search.js +107 -0
- package/src/core/sessions.js +171 -0
- package/src/core/text.js +72 -0
- package/src/core/wiki.js +102 -0
- package/src/doctor.js +177 -0
- package/src/dream.js +309 -0
- package/src/hook.js +101 -0
- package/src/import.js +137 -0
- package/src/init.js +266 -0
- package/src/mcp.js +376 -0
- package/src/memory.js +188 -0
- package/src/proposals.js +204 -0
- package/src/store.js +445 -0
- package/src/transcript.js +40 -0
- package/src/version.js +4 -0
package/src/cli.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { parseArgs, intFlag, CliError, UsageError } from "./args.js";
|
|
3
|
+
import { Store, userHome, assertDocPath, isSafeId } from "./store.js";
|
|
4
|
+
import { newId } from "./core/index.js";
|
|
5
|
+
import { contextFor, formatResults, recallResults, rememberChecked } from "./memory.js";
|
|
6
|
+
import { normalizeEvents } from "./transcript.js";
|
|
7
|
+
import { applyProposal, formatHistory, proposalChanges, rejectProposal, renderProposalDiff, restorePath, runRetention } from "./proposals.js";
|
|
8
|
+
import { VERSION } from "./version.js";
|
|
9
|
+
|
|
10
|
+
export const USAGE = `nexorum ${VERSION}: local-first memory for AI agents
|
|
11
|
+
|
|
12
|
+
Usage: nexorum <command> [options]
|
|
13
|
+
|
|
14
|
+
Setup
|
|
15
|
+
init [--claude-code] [--codex] [--hermes] [--cursor] [--claude-desktop] [--all] [--dry-run] [--home DIR] [--dir DIR] [--no-agents-md] [--prompt-recall]
|
|
16
|
+
mcp run the MCP server on stdio
|
|
17
|
+
hook session-start|user-prompt|stop Claude Code hook (JSON on stdin)
|
|
18
|
+
|
|
19
|
+
Memory
|
|
20
|
+
remember "text" save a note
|
|
21
|
+
recall "query" [--limit N] [--kind note|wiki] [--since DATE]
|
|
22
|
+
context [--max-chars N]
|
|
23
|
+
status
|
|
24
|
+
log [--file F | -] [--source S] [--id ID] store a transcript (JSON events)
|
|
25
|
+
import omp [--dir D] [--session FILE] [--all] [--since DATE] store omp sessions as transcripts
|
|
26
|
+
|
|
27
|
+
Dreaming
|
|
28
|
+
dream [--budget TOKENS] [--instructions "..."] [--yes] [--dry-run] [--apply]
|
|
29
|
+
[--since DATE] [--max-inputs N] only transcripts updated since DATE; at most N (1-200, default 50)
|
|
30
|
+
dream --if-due [--min-pending N] [--idle-minutes M] [--cooldown-hours H] for schedulers: dream only when
|
|
31
|
+
at least N (3) transcripts are pending, the newest is M (10) minutes old and the last dream H (6) hours old
|
|
32
|
+
doctor [--json] read-only health check (PASS/WARN/FAIL per line; exit 1 on FAIL)
|
|
33
|
+
proposals
|
|
34
|
+
diff [id]
|
|
35
|
+
apply [id] [--force]
|
|
36
|
+
reject [id]
|
|
37
|
+
restore <path> [--version N]
|
|
38
|
+
history [path]
|
|
39
|
+
retention [--days N]
|
|
40
|
+
|
|
41
|
+
Global: --space NAME (default: $NEXORUM_SPACE or the current folder name), --help, --version
|
|
42
|
+
Environment: NEXORUM_HOME (default ~/.nexorum), NEXORUM_LLM_PROVIDER, NEXORUM_LLM_API_KEY,
|
|
43
|
+
NEXORUM_LLM_BASE_URL, NEXORUM_ANALYZER_MODEL, NEXORUM_ORGANIZER_MODEL
|
|
44
|
+
Exit codes: 0 ok, 1 runtime error, 2 usage or configuration error`;
|
|
45
|
+
|
|
46
|
+
const out = (s = "") => process.stdout.write(`${s}\n`);
|
|
47
|
+
const withSpace = (spec = {}) => ({ ...spec, string: [...(spec.string ?? []), "space"] });
|
|
48
|
+
|
|
49
|
+
const SPEC = {
|
|
50
|
+
init: { bool: ["claude-code", "codex", "hermes", "cursor", "claude-desktop", "all", "dry-run", "no-agents-md", "prompt-recall"], string: ["home", "dir"] },
|
|
51
|
+
mcp: withSpace(),
|
|
52
|
+
hook: withSpace(),
|
|
53
|
+
log: withSpace({ string: ["file", "source", "id"] }),
|
|
54
|
+
remember: withSpace(),
|
|
55
|
+
recall: withSpace({ string: ["limit", "kind", "since"] }),
|
|
56
|
+
context: withSpace({ string: ["max-chars"] }),
|
|
57
|
+
status: withSpace(),
|
|
58
|
+
dream: withSpace({
|
|
59
|
+
bool: ["yes", "dry-run", "apply", "if-due"],
|
|
60
|
+
string: ["budget", "instructions", "since", "max-inputs", "min-pending", "idle-minutes", "cooldown-hours"],
|
|
61
|
+
alias: { y: "yes" },
|
|
62
|
+
}),
|
|
63
|
+
doctor: withSpace({ bool: ["json"] }),
|
|
64
|
+
proposals: withSpace(),
|
|
65
|
+
diff: withSpace(),
|
|
66
|
+
apply: withSpace({ bool: ["force"] }),
|
|
67
|
+
reject: withSpace(),
|
|
68
|
+
restore: withSpace({ string: ["version"] }),
|
|
69
|
+
import: withSpace({ bool: ["all"], string: ["dir", "session", "since"] }),
|
|
70
|
+
history: withSpace(),
|
|
71
|
+
retention: withSpace({ string: ["days"] }),
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
function readInput(file) {
|
|
75
|
+
if (file === undefined || file === "-") return fs.readFileSync(0, "utf8");
|
|
76
|
+
try {
|
|
77
|
+
return fs.readFileSync(file, "utf8");
|
|
78
|
+
} catch (e) {
|
|
79
|
+
throw new UsageError(`Cannot read ${file}: ${e.code ?? e.message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function logCommand(store, flags) {
|
|
84
|
+
let data;
|
|
85
|
+
try {
|
|
86
|
+
data = JSON.parse(readInput(flags.file));
|
|
87
|
+
} catch (e) {
|
|
88
|
+
if (e instanceof CliError) throw e;
|
|
89
|
+
throw new UsageError("Input is not valid JSON.");
|
|
90
|
+
}
|
|
91
|
+
const events = normalizeEvents(Array.isArray(data) ? data : data?.events);
|
|
92
|
+
const id = flags.id ?? newId("trn");
|
|
93
|
+
if (!isSafeId(id)) throw new UsageError("--id may only contain letters, digits, _ . - (up to 128 characters).");
|
|
94
|
+
const source = flags.source ?? (typeof data?.source === "string" ? data.source : "cli");
|
|
95
|
+
const { transcript, changed } = store.upsertTranscript({ id, source, externalId: flags.id ?? null, events });
|
|
96
|
+
out(changed ? `Logged transcript ${transcript.id} (${events.length} events, status ${transcript.status}).` : `Transcript ${transcript.id} unchanged.`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function statusCommand(store) {
|
|
100
|
+
const transcripts = store.listTranscripts();
|
|
101
|
+
const proposals = store.listProposals();
|
|
102
|
+
const count = (s) => transcripts.filter((t) => t.status === s).length;
|
|
103
|
+
out(`Home: ${store.root}`);
|
|
104
|
+
out(`Space: ${store.space}`);
|
|
105
|
+
out(`Notes: ${store.readNotes().length}`);
|
|
106
|
+
out(`Wiki docs: ${store.wikiCount()}`);
|
|
107
|
+
out(`Transcripts: ${count("pending")} pending, ${count("dreamed")} dreamed`);
|
|
108
|
+
out(`Proposals: ${proposals.filter((p) => p.status === "review").length} awaiting review, ${proposals.length} total`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function proposalsCommand(store) {
|
|
112
|
+
const list = store.listProposals();
|
|
113
|
+
if (!list.length) return out("No proposals. Run: nexorum dream");
|
|
114
|
+
for (const p of list) {
|
|
115
|
+
let n = "?";
|
|
116
|
+
try {
|
|
117
|
+
n = String(proposalChanges(store, p.id).length);
|
|
118
|
+
} catch {}
|
|
119
|
+
const what = p.status === "review" ? `${n} change(s)` : `${(p.applied_paths ?? []).length} applied`;
|
|
120
|
+
out(`${p.id} ${p.status.padEnd(8)}${p.partial ? " PARTIAL" : ""} ${p.created_at} ${what}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function dispatch(cmd, rest, env) {
|
|
125
|
+
const spec = SPEC[cmd];
|
|
126
|
+
if (!spec) throw new UsageError(`Unknown command "${cmd}". Run: nexorum --help`);
|
|
127
|
+
const { flags, positionals } = parseArgs(rest, spec);
|
|
128
|
+
const openStore = () => Store.open({ space: flags.space, env });
|
|
129
|
+
const atMostOne = (what) => {
|
|
130
|
+
if (positionals.length > 1) throw new UsageError(`Too many arguments for ${what}.`);
|
|
131
|
+
return positionals[0];
|
|
132
|
+
};
|
|
133
|
+
switch (cmd) {
|
|
134
|
+
case "init": {
|
|
135
|
+
const { initCommand } = await import("./init.js");
|
|
136
|
+
return initCommand(flags, env, flags.home ?? userHome(env));
|
|
137
|
+
}
|
|
138
|
+
case "mcp": {
|
|
139
|
+
const { mcpCommand } = await import("./mcp.js");
|
|
140
|
+
return mcpCommand(openStore());
|
|
141
|
+
}
|
|
142
|
+
case "hook": {
|
|
143
|
+
const sub = positionals[0];
|
|
144
|
+
if (!["session-start", "user-prompt", "stop"].includes(sub)) throw new UsageError("Usage: nexorum hook session-start|user-prompt|stop");
|
|
145
|
+
const { hookCommand } = await import("./hook.js");
|
|
146
|
+
return hookCommand(sub, flags, env);
|
|
147
|
+
}
|
|
148
|
+
case "log":
|
|
149
|
+
logCommand(openStore(), flags);
|
|
150
|
+
return 0;
|
|
151
|
+
case "import": {
|
|
152
|
+
const { importCommand } = await import("./import.js");
|
|
153
|
+
return importCommand(positionals, flags, env);
|
|
154
|
+
}
|
|
155
|
+
case "remember": {
|
|
156
|
+
const r = rememberChecked(openStore(), positionals.join(" "));
|
|
157
|
+
if (r.duplicateOf) out(`Already remembered as ${r.duplicateOf.id}; nothing saved.`);
|
|
158
|
+
else {
|
|
159
|
+
out(`Saved note ${r.note.id}.`);
|
|
160
|
+
if (r.similarTo) {
|
|
161
|
+
out(`Note: very similar to ${r.similarTo.id} ("${r.similarTo.content.replace(/\s+/g, " ").slice(0, 80)}"). If it replaces it, remove the old one with the MCP forget tool (or update_note).`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return 0;
|
|
165
|
+
}
|
|
166
|
+
case "recall": {
|
|
167
|
+
if (!positionals.length) throw new UsageError('Usage: nexorum recall "query" [--limit N]');
|
|
168
|
+
const kind = flags.kind;
|
|
169
|
+
if (kind !== undefined && kind !== "note" && kind !== "wiki") throw new UsageError("--kind must be note or wiki.");
|
|
170
|
+
out(formatResults(recallResults(openStore(), positionals.join(" "), intFlag(flags, "limit", { max: 50, fallback: 5 }), { kind, since: flags.since })));
|
|
171
|
+
return 0;
|
|
172
|
+
}
|
|
173
|
+
case "context": {
|
|
174
|
+
const c = contextFor(openStore(), intFlag(flags, "max-chars", { min: 100, max: 100_000, fallback: 4096 }));
|
|
175
|
+
if (c.content) out(c.content);
|
|
176
|
+
else process.stderr.write("Memory is empty for this space.\n");
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
case "status":
|
|
180
|
+
statusCommand(openStore());
|
|
181
|
+
return 0;
|
|
182
|
+
case "dream": {
|
|
183
|
+
const { dreamCommand } = await import("./dream.js");
|
|
184
|
+
return dreamCommand(openStore(), flags, env);
|
|
185
|
+
}
|
|
186
|
+
case "doctor": {
|
|
187
|
+
const { doctorCommand } = await import("./doctor.js");
|
|
188
|
+
return doctorCommand(openStore(), flags, env);
|
|
189
|
+
}
|
|
190
|
+
case "proposals":
|
|
191
|
+
proposalsCommand(openStore());
|
|
192
|
+
return 0;
|
|
193
|
+
case "diff": {
|
|
194
|
+
const store = openStore();
|
|
195
|
+
out(renderProposalDiff(store, store.pickProposal(atMostOne("diff"))));
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
case "apply": {
|
|
199
|
+
const store = openStore();
|
|
200
|
+
const p = store.pickProposal(atMostOne("apply"));
|
|
201
|
+
const r = applyProposal(store, p, { force: Boolean(flags.force) });
|
|
202
|
+
out(`Applied ${r.applied.length} path(s) from ${p.id}.`);
|
|
203
|
+
for (const a of r.applied) out(` wrote ${a}`);
|
|
204
|
+
for (const s of r.skipped) out(` skipped ${s.path}: ${s.reason}`);
|
|
205
|
+
if (r.status !== "applied") {
|
|
206
|
+
out("Nothing was applied and the proposal stays under review. Use --force to overwrite the changed live files, or run a new dream.");
|
|
207
|
+
return 1;
|
|
208
|
+
}
|
|
209
|
+
const removed = runRetention(store);
|
|
210
|
+
if (removed) out(`Retention: removed ${removed} old transcript(s).`);
|
|
211
|
+
return 0;
|
|
212
|
+
}
|
|
213
|
+
case "reject": {
|
|
214
|
+
const store = openStore();
|
|
215
|
+
const p = store.pickProposal(atMostOne("reject"));
|
|
216
|
+
rejectProposal(store, p);
|
|
217
|
+
out(`Rejected ${p.id}. Its inputs stay pending.`);
|
|
218
|
+
return 0;
|
|
219
|
+
}
|
|
220
|
+
case "restore": {
|
|
221
|
+
const p = positionals[0];
|
|
222
|
+
if (!p || positionals.length > 1) throw new UsageError("Usage: nexorum restore <path> [--version N]");
|
|
223
|
+
assertDocPath(p);
|
|
224
|
+
const t = restorePath(openStore(), p, flags.version === undefined ? undefined : intFlag(flags, "version"));
|
|
225
|
+
out(`Restored ${p} to version ${t.version} (${t.ts}).`);
|
|
226
|
+
return 0;
|
|
227
|
+
}
|
|
228
|
+
case "history":
|
|
229
|
+
out(formatHistory(openStore(), atMostOne("history")));
|
|
230
|
+
return 0;
|
|
231
|
+
case "retention": {
|
|
232
|
+
const days = intFlag(flags, "days", { min: 0, fallback: 30 });
|
|
233
|
+
out(`Removed ${runRetention(openStore(), days)} transcript(s) dreamed more than ${days} day(s) ago.`);
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
default:
|
|
237
|
+
throw new UsageError(`Unknown command "${cmd}".`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** `nexorum --space X dream` works like `nexorum dream --space X`: leading --space/--home pairs move behind the command. */
|
|
242
|
+
function hoistGlobalFlags(argv) {
|
|
243
|
+
const lead = [];
|
|
244
|
+
let i = 0;
|
|
245
|
+
while (i < argv.length && (argv[i] === "--space" || argv[i] === "--home") && i + 1 < argv.length) {
|
|
246
|
+
lead.push(argv[i], argv[i + 1]);
|
|
247
|
+
i += 2;
|
|
248
|
+
}
|
|
249
|
+
if (!lead.length || i >= argv.length) return argv;
|
|
250
|
+
return [argv[i], ...lead, ...argv.slice(i + 1)];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export async function main(rawArgv, env = process.env) {
|
|
254
|
+
const [cmd, ...rest] = hoistGlobalFlags(rawArgv);
|
|
255
|
+
if (cmd === undefined || cmd === "help" || cmd === "--help" || cmd === "-h") {
|
|
256
|
+
out(USAGE);
|
|
257
|
+
return cmd === undefined ? 2 : 0;
|
|
258
|
+
}
|
|
259
|
+
if (cmd === "--version" || cmd === "-v") {
|
|
260
|
+
out(VERSION);
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
264
|
+
out(USAGE);
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
try {
|
|
268
|
+
return (await dispatch(cmd, rest, env)) ?? 0;
|
|
269
|
+
} catch (e) {
|
|
270
|
+
if (e instanceof CliError) {
|
|
271
|
+
process.stderr.write(`nexorum: ${e.message}\n`);
|
|
272
|
+
return cmd === "hook" ? 0 : e.exitCode; // a hook never fails a session, not even on a mistyped sub-command
|
|
273
|
+
}
|
|
274
|
+
if (cmd === "hook") return 0;
|
|
275
|
+
process.stderr.write(`nexorum: ${env.NEXORUM_DEBUG === "1" ? (e?.stack ?? e) : (e?.message ?? e)}\n`);
|
|
276
|
+
return 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Small shared helpers of the core library: hashing, ids, frontmatter, input splitting.
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
|
|
4
|
+
/** Lower-case hex SHA-256 of the UTF-8 bytes of `text`. */
|
|
5
|
+
export function sha256Hex(text) {
|
|
6
|
+
return crypto.createHash("sha256").update(String(text), "utf8").digest("hex");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** `<prefix>_` followed by 16 random bytes in base64url (22 characters). */
|
|
10
|
+
export function newId(prefix) {
|
|
11
|
+
return `${prefix}_${crypto.randomBytes(16).toString("base64url")}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const FRONT_RE = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/;
|
|
15
|
+
const FIELD_RE = /^([A-Za-z_][\w-]*):[ \t]*([\s\S]*)$/;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Reads a leading `---` block of `key: value` lines. Keys are lower-cased, surrounding quotes dropped, the first
|
|
19
|
+
* occurrence of a key wins.
|
|
20
|
+
* @returns {{name:string|null, description:string|null, metadata:string|null, fields:Record<string,string>, body:string, hasFrontmatter:boolean}}
|
|
21
|
+
*/
|
|
22
|
+
export function parseFrontmatter(content) {
|
|
23
|
+
const text = String(content ?? "").replace(/^\uFEFF/, "");
|
|
24
|
+
const m = FRONT_RE.exec(text);
|
|
25
|
+
if (!m) return { name: null, description: null, metadata: null, fields: {}, body: text, hasFrontmatter: false };
|
|
26
|
+
const fields = {};
|
|
27
|
+
for (const line of m[1].split(/\r?\n/)) {
|
|
28
|
+
const kv = FIELD_RE.exec(line);
|
|
29
|
+
if (!kv) continue;
|
|
30
|
+
const key = kv[1].toLowerCase();
|
|
31
|
+
let value = kv[2].trim();
|
|
32
|
+
const q = value[0];
|
|
33
|
+
if (value.length >= 2 && (q === '"' || q === "'") && value.at(-1) === q) value = value.slice(1, -1);
|
|
34
|
+
if (!(key in fields)) fields[key] = value;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
name: fields.name || null,
|
|
38
|
+
description: fields.description || null,
|
|
39
|
+
metadata: fields.metadata || null,
|
|
40
|
+
fields,
|
|
41
|
+
body: text.slice(m[0].length),
|
|
42
|
+
hasFrontmatter: true,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Inputs longer than this are analyzed in consecutive parts. */
|
|
47
|
+
export const MAX_PART_CHARS = 120_000;
|
|
48
|
+
|
|
49
|
+
function cutLong(block, max) {
|
|
50
|
+
const pieces = [];
|
|
51
|
+
let rest = block;
|
|
52
|
+
while (rest.length > max) {
|
|
53
|
+
let cut = rest.lastIndexOf("\n", max);
|
|
54
|
+
let skip = 1;
|
|
55
|
+
if (cut < max / 2) {
|
|
56
|
+
cut = max;
|
|
57
|
+
skip = 0;
|
|
58
|
+
const c = rest.charCodeAt(cut - 1);
|
|
59
|
+
if (c >= 0xd800 && c <= 0xdbff) cut--; // keep surrogate pairs together
|
|
60
|
+
}
|
|
61
|
+
pieces.push(rest.slice(0, cut));
|
|
62
|
+
rest = rest.slice(cut + skip);
|
|
63
|
+
}
|
|
64
|
+
if (rest) pieces.push(rest);
|
|
65
|
+
return pieces;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Splits text into parts of at most `max` characters, preferring blank lines (event boundaries), then line breaks.
|
|
70
|
+
* @returns {string[]} at least one part
|
|
71
|
+
*/
|
|
72
|
+
export function splitText(text, max = MAX_PART_CHARS) {
|
|
73
|
+
if (text.length <= max) return [text];
|
|
74
|
+
const parts = [];
|
|
75
|
+
let cur = "";
|
|
76
|
+
for (const block of text.split("\n\n")) {
|
|
77
|
+
for (const piece of block.length > max ? cutLong(block, max) : [block]) {
|
|
78
|
+
if (cur === "") cur = piece;
|
|
79
|
+
else if (cur.length + 2 + piece.length <= max) cur += `\n\n${piece}`;
|
|
80
|
+
else {
|
|
81
|
+
parts.push(cur);
|
|
82
|
+
cur = piece;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (cur) parts.push(cur);
|
|
87
|
+
return parts;
|
|
88
|
+
}
|
package/src/core/diff.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Document-level and line-level diffs.
|
|
2
|
+
|
|
3
|
+
/** @returns {{path:string, kind:"added"|"modified"|"removed", before:string|null, after:string|null}[]} sorted by path */
|
|
4
|
+
export function diffDocs(before = {}, after = {}) {
|
|
5
|
+
const out = [];
|
|
6
|
+
for (const p of [...new Set([...Object.keys(before), ...Object.keys(after)])].sort()) {
|
|
7
|
+
const a = Object.hasOwn(before, p) ? before[p] : null;
|
|
8
|
+
const b = Object.hasOwn(after, p) ? after[p] : null;
|
|
9
|
+
if (a === null) out.push({ path: p, kind: "added", before: null, after: b });
|
|
10
|
+
else if (b === null) out.push({ path: p, kind: "removed", before: a, after: null });
|
|
11
|
+
else if (a !== b) out.push({ path: p, kind: "modified", before: a, after: b });
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const linesOf = (s) => (s === "" ? [] : (s.endsWith("\n") ? s.slice(0, -1) : s).split("\n"));
|
|
17
|
+
const LCS_CELL_LIMIT = 16_000_000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* LCS line diff. Every line of both sides appears once as same/del/add; one trailing newline is not a line.
|
|
21
|
+
* Shared head and tail are trimmed first; a huge middle degrades to "all deleted, then all added".
|
|
22
|
+
* @returns {{type:"same"|"add"|"del", text:string}[]}
|
|
23
|
+
*/
|
|
24
|
+
export function lineDiff(a, b) {
|
|
25
|
+
const A = linesOf(String(a ?? ""));
|
|
26
|
+
const B = linesOf(String(b ?? ""));
|
|
27
|
+
let head = 0;
|
|
28
|
+
while (head < A.length && head < B.length && A[head] === B[head]) head++;
|
|
29
|
+
let tail = 0;
|
|
30
|
+
while (tail < A.length - head && tail < B.length - head && A[A.length - 1 - tail] === B[B.length - 1 - tail]) tail++;
|
|
31
|
+
|
|
32
|
+
const out = A.slice(0, head).map((text) => ({ type: "same", text }));
|
|
33
|
+
const x = A.slice(head, A.length - tail);
|
|
34
|
+
const y = B.slice(head, B.length - tail);
|
|
35
|
+
const n = x.length;
|
|
36
|
+
const m = y.length;
|
|
37
|
+
if (n === 0 || m === 0 || (n + 1) * (m + 1) > LCS_CELL_LIMIT) {
|
|
38
|
+
for (const text of x) out.push({ type: "del", text });
|
|
39
|
+
for (const text of y) out.push({ type: "add", text });
|
|
40
|
+
} else {
|
|
41
|
+
const w = m + 1;
|
|
42
|
+
const len = new Uint32Array((n + 1) * w);
|
|
43
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
44
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
45
|
+
len[i * w + j] = x[i] === y[j] ? len[(i + 1) * w + j + 1] + 1 : Math.max(len[(i + 1) * w + j], len[i * w + j + 1]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
let i = 0;
|
|
49
|
+
let j = 0;
|
|
50
|
+
while (i < n && j < m) {
|
|
51
|
+
if (x[i] === y[j]) {
|
|
52
|
+
out.push({ type: "same", text: x[i] });
|
|
53
|
+
i++;
|
|
54
|
+
j++;
|
|
55
|
+
} else if (len[(i + 1) * w + j] >= len[i * w + j + 1]) out.push({ type: "del", text: x[i++] });
|
|
56
|
+
else out.push({ type: "add", text: y[j++] });
|
|
57
|
+
}
|
|
58
|
+
while (i < n) out.push({ type: "del", text: x[i++] });
|
|
59
|
+
while (j < m) out.push({ type: "add", text: y[j++] });
|
|
60
|
+
}
|
|
61
|
+
for (let k = A.length - tail; k < A.length; k++) out.push({ type: "same", text: A[k] });
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// One agent conversation: call the model, run every tool call of the turn, answer with one user message of
|
|
2
|
+
// tool_result blocks, repeat until submit. Messages use the Anthropic block format; providers translate.
|
|
3
|
+
import { normalizeUsage, zeroUsage } from "../providers/http.js";
|
|
4
|
+
|
|
5
|
+
export class AgentError extends Error {}
|
|
6
|
+
|
|
7
|
+
const DOC_TOOLS = new Set(["list_docs", "read_doc", "grep_docs", "write_doc", "edit_doc", "delete_doc", "rename_doc", "submit"]);
|
|
8
|
+
const clip = (s, n) => (s.length <= n ? s : `${s.slice(0, n - 1)}…`);
|
|
9
|
+
const MAX_NUDGES = 2;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Shared token counter. The cap is checked BEFORE each call starts, so a run overshoots by at most the calls
|
|
13
|
+
* already in flight. `onThreshold(pct, used, cap)` fires once each at 50, 80 and 100 percent.
|
|
14
|
+
*/
|
|
15
|
+
export class BudgetTracker {
|
|
16
|
+
constructor(cap, onThreshold) {
|
|
17
|
+
this.cap = cap;
|
|
18
|
+
this.usage = zeroUsage();
|
|
19
|
+
this.onThreshold = onThreshold;
|
|
20
|
+
this.fired = new Set();
|
|
21
|
+
}
|
|
22
|
+
get used() {
|
|
23
|
+
return this.usage.total_tokens;
|
|
24
|
+
}
|
|
25
|
+
get exhausted() {
|
|
26
|
+
return this.used >= this.cap;
|
|
27
|
+
}
|
|
28
|
+
add(raw) {
|
|
29
|
+
const u = normalizeUsage(raw);
|
|
30
|
+
for (const k of Object.keys(this.usage)) this.usage[k] += u[k];
|
|
31
|
+
for (const pct of [50, 80, 100]) {
|
|
32
|
+
if (!this.fired.has(pct) && this.used * 100 >= this.cap * pct) {
|
|
33
|
+
this.fired.add(pct);
|
|
34
|
+
this.onThreshold?.(pct, this.used, this.cap);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @returns {Promise<{summary:string, submission:object}>} */
|
|
41
|
+
export async function runAgent({ client, model, system, toolbox, firstContent, maxTurns, maxTokens, agent, emit }) {
|
|
42
|
+
const messages = [{ role: "user", content: firstContent }];
|
|
43
|
+
let nudges = 0;
|
|
44
|
+
for (let turn = 1; turn <= maxTurns; turn++) {
|
|
45
|
+
const resp = await client.send({ model, maxTokens, system, tools: toolbox.definitions, messages });
|
|
46
|
+
const content = resp.content;
|
|
47
|
+
const said = content.filter((b) => b.type === "text").map((b) => b.text).join("\n").trim();
|
|
48
|
+
if (said) emit("agent_text", clip(said.replace(/\s+/g, " "), 400), { agent });
|
|
49
|
+
|
|
50
|
+
if (resp.stopReason === "refusal") throw new AgentError("The model declined to continue (refusal).");
|
|
51
|
+
if (resp.stopReason === "max_tokens") {
|
|
52
|
+
// A cut-off response may end in a half-written tool call, so none of its calls run.
|
|
53
|
+
const kept = content.filter((b) => b.type !== "tool_use");
|
|
54
|
+
messages.push({ role: "assistant", content: kept.length ? kept : [{ type: "text", text: "(cut off)" }] });
|
|
55
|
+
messages.push({ role: "user", content: [{ type: "text", text: "Your last response hit the output limit and was cut off; no tool call in it was executed. Continue in smaller steps: fewer or shorter documents per turn." }] });
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const calls = content.filter((b) => b.type === "tool_use");
|
|
60
|
+
if (!calls.length) {
|
|
61
|
+
// Stopping without submit (or tool calls written as text through a gateway) means the job is not done.
|
|
62
|
+
if (++nudges > MAX_NUDGES) throw new AgentError("The model stopped without calling submit; it may not support tool calls through this endpoint.");
|
|
63
|
+
messages.push({ role: "assistant", content: content.length ? content : [{ type: "text", text: "(no output)" }] });
|
|
64
|
+
messages.push({ role: "user", content: [{ type: "text", text: "You have not called a tool. Work only through the provided tools. If you are finished, call submit with your summary now; otherwise continue with the next tool call." }] });
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
messages.push({ role: "assistant", content });
|
|
68
|
+
|
|
69
|
+
const results = new Array(calls.length);
|
|
70
|
+
const exec = (i) =>
|
|
71
|
+
calls[i].parse_error
|
|
72
|
+
? { isError: true, text: `The arguments of this ${calls[i].name} call were not valid JSON (${calls[i].parse_error}). Call it again with a JSON object that matches the tool schema.` }
|
|
73
|
+
: toolbox.execute(calls[i].name, calls[i].input);
|
|
74
|
+
const report = (i) => {
|
|
75
|
+
const call = calls[i];
|
|
76
|
+
if (!DOC_TOOLS.has(call.name)) return;
|
|
77
|
+
const input = call.input && typeof call.input === "object" ? call.input : {};
|
|
78
|
+
const p = [input.path, input.from, input.prefix].find((v) => typeof v === "string");
|
|
79
|
+
emit("tool", clip(results[i].text.split("\n")[0], 200), { agent, tool: call.name, ...(p ? { path: p } : {}), ok: !results[i].isError });
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// Other tools first; submit last, and only when nothing else in the turn failed.
|
|
83
|
+
const submitAt = [];
|
|
84
|
+
let failed = false;
|
|
85
|
+
calls.forEach((call, i) => {
|
|
86
|
+
if (call.name === "submit") return submitAt.push(i);
|
|
87
|
+
results[i] = exec(i);
|
|
88
|
+
if (results[i].isError) failed = true;
|
|
89
|
+
report(i);
|
|
90
|
+
});
|
|
91
|
+
let submission = null;
|
|
92
|
+
for (const i of submitAt) {
|
|
93
|
+
if (failed) results[i] = { isError: true, text: "Not accepted: another tool call in this turn failed. Fix it, check the result, then call submit again." };
|
|
94
|
+
else if (submission) results[i] = { isError: true, text: "Only one submit per turn." };
|
|
95
|
+
else {
|
|
96
|
+
results[i] = exec(i);
|
|
97
|
+
if (!results[i].isError) submission = results[i].submission;
|
|
98
|
+
}
|
|
99
|
+
report(i);
|
|
100
|
+
}
|
|
101
|
+
if (submission) return { summary: submission.summary, submission };
|
|
102
|
+
|
|
103
|
+
messages.push({
|
|
104
|
+
role: "user",
|
|
105
|
+
content: calls.map((call, i) => ({ type: "tool_result", tool_use_id: call.id, content: results[i].text, ...(results[i].isError ? { is_error: true } : {}) })),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
throw new AgentError(`Reached the limit of ${maxTurns} turns without calling submit.`);
|
|
109
|
+
}
|