@electric-ax/agents 0.2.4 → 0.3.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/dist/entrypoint.js +434 -725
- package/dist/index.cjs +430 -721
- package/dist/index.d.cts +68 -35
- package/dist/index.d.ts +69 -36
- package/dist/index.js +451 -741
- package/docs/entities/agents/horton.md +2 -5
- package/docs/index.md +4 -2
- package/docs/quickstart.md +2 -2
- package/docs/reference/handler-context.md +0 -35
- package/docs/reference/mcp-registry.md +189 -0
- package/docs/reference/mcp-server-config.md +226 -0
- package/docs/usage/clients-and-react.md +0 -4
- package/docs/usage/embedded-builtins.md +26 -16
- package/docs/usage/mcp-servers.md +354 -0
- package/docs/usage/overview.md +1 -3
- package/docs/usage/programmatic-runtime-client.md +1 -1
- package/docs/usage/writing-handlers.md +0 -5
- package/package.json +6 -4
- package/docs/entities/agents/coder.md +0 -99
package/dist/entrypoint.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { createServer } from "node:http";
|
|
3
2
|
import path from "node:path";
|
|
4
|
-
import
|
|
3
|
+
import { createServer } from "node:http";
|
|
4
|
+
import fs from "node:fs";
|
|
5
5
|
import pino from "pino";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { homedir } from "node:os";
|
|
7
|
+
import { completeWithLowCostModel, createEntityRegistry, createRuntimeHandler, db, detectAvailableProviders, readCodexAccessToken, registerToolProvider, unregisterToolProvider } from "@electric-ax/agents-runtime";
|
|
8
|
+
import { eq, not, queryOnce } from "@durable-streams/state";
|
|
10
9
|
import { z } from "zod";
|
|
11
|
-
import { deserializeCursor, discoverSessions, importLocalSession, loadSession, resolveSession, serializeCursor, tailSession } from "agent-session-protocol";
|
|
12
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
13
10
|
import { createHash } from "node:crypto";
|
|
14
11
|
import fs$1 from "node:fs/promises";
|
|
15
12
|
import Database from "better-sqlite3";
|
|
16
13
|
import { Type } from "@sinclair/typebox";
|
|
17
14
|
import { load } from "sqlite-vec";
|
|
18
15
|
import { nanoid } from "nanoid";
|
|
19
|
-
import {
|
|
16
|
+
import { getModels } from "@mariozechner/pi-ai";
|
|
17
|
+
import { braveSearchTool, createBashTool, createEditTool, createFetchUrlTool, createReadFileTool, createWriteTool, fetchUrlTool } from "@electric-ax/agents-runtime/tools";
|
|
18
|
+
import { bridgeMcpTool, buildPromptTools, buildResourceTools, createRegistry, keychainPersistence, loadConfig, mcp, watchConfig } from "@electric-ax/agents-mcp";
|
|
20
19
|
|
|
21
20
|
//#region src/log.ts
|
|
22
21
|
const LOG_DIR = process.env.ELECTRIC_AGENTS_LOG_DIR ?? path.resolve(process.cwd(), `logs`);
|
|
@@ -71,516 +70,6 @@ const serverLog = {
|
|
|
71
70
|
}
|
|
72
71
|
};
|
|
73
72
|
|
|
74
|
-
//#endregion
|
|
75
|
-
//#region src/agents/coding-session.ts
|
|
76
|
-
const defaultCliRunner = { async run(opts) {
|
|
77
|
-
return new Promise((resolve, reject) => {
|
|
78
|
-
const isClaude = opts.agent === `claude`;
|
|
79
|
-
const bin = isClaude ? `claude` : `codex`;
|
|
80
|
-
const args = isClaude ? opts.sessionId ? [
|
|
81
|
-
`-r`,
|
|
82
|
-
opts.sessionId,
|
|
83
|
-
`--dangerously-skip-permissions`,
|
|
84
|
-
`-p`
|
|
85
|
-
] : [`--dangerously-skip-permissions`, `-p`] : opts.sessionId ? [
|
|
86
|
-
`exec`,
|
|
87
|
-
`--skip-git-repo-check`,
|
|
88
|
-
`resume`,
|
|
89
|
-
opts.sessionId,
|
|
90
|
-
opts.prompt
|
|
91
|
-
] : [
|
|
92
|
-
`exec`,
|
|
93
|
-
`--skip-git-repo-check`,
|
|
94
|
-
opts.prompt
|
|
95
|
-
];
|
|
96
|
-
const child = spawn(bin, args, {
|
|
97
|
-
cwd: opts.cwd,
|
|
98
|
-
stdio: [
|
|
99
|
-
isClaude ? `pipe` : `ignore`,
|
|
100
|
-
`pipe`,
|
|
101
|
-
`pipe`
|
|
102
|
-
]
|
|
103
|
-
});
|
|
104
|
-
const MAX_BUF_CHARS = 4096;
|
|
105
|
-
let stdout = ``;
|
|
106
|
-
let stderr = ``;
|
|
107
|
-
child.stdout?.on(`data`, (d) => {
|
|
108
|
-
if (stdout.length < MAX_BUF_CHARS) stdout += d.toString().slice(0, MAX_BUF_CHARS - stdout.length);
|
|
109
|
-
});
|
|
110
|
-
child.stderr?.on(`data`, (d) => {
|
|
111
|
-
if (stderr.length < MAX_BUF_CHARS) stderr += d.toString().slice(0, MAX_BUF_CHARS - stderr.length);
|
|
112
|
-
});
|
|
113
|
-
child.on(`error`, reject);
|
|
114
|
-
child.on(`exit`, (code) => {
|
|
115
|
-
resolve({
|
|
116
|
-
exitCode: code ?? -1,
|
|
117
|
-
stdout,
|
|
118
|
-
stderr
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
if (isClaude && child.stdin) {
|
|
122
|
-
child.stdin.write(opts.prompt);
|
|
123
|
-
child.stdin.end();
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
} };
|
|
127
|
-
async function discoverNewestSession(agent, cwd, excludeIds) {
|
|
128
|
-
const all = await discoverSessions(agent);
|
|
129
|
-
const candidates = all.filter((s) => !excludeIds.has(s.sessionId) && (!s.cwd || s.cwd === cwd));
|
|
130
|
-
if (candidates.length === 0) return null;
|
|
131
|
-
return candidates[0].sessionId;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Compute the candidate directories where Claude Code stores per-cwd
|
|
135
|
-
* session JSONL files. Claude resolves the cwd to its realpath when
|
|
136
|
-
* choosing the directory name (so /tmp/foo on macOS lands under
|
|
137
|
-
* `-private-tmp-foo`), but the entity may have been spawned with the
|
|
138
|
-
* non-realpath form. Return both candidates so the caller can union
|
|
139
|
-
* their contents.
|
|
140
|
-
*/
|
|
141
|
-
async function getClaudeProjectDirs(cwd) {
|
|
142
|
-
const home = homedir();
|
|
143
|
-
const make = (c) => path.join(home, `.claude`, `projects`, c.replace(/\//g, `-`));
|
|
144
|
-
const dirs = [make(cwd)];
|
|
145
|
-
try {
|
|
146
|
-
const real = await promises.realpath(cwd);
|
|
147
|
-
if (real !== cwd) dirs.push(make(real));
|
|
148
|
-
} catch {}
|
|
149
|
-
return dirs;
|
|
150
|
-
}
|
|
151
|
-
async function listClaudeJsonlIdsByCwd(cwd) {
|
|
152
|
-
const ids = new Set();
|
|
153
|
-
for (const dir of await getClaudeProjectDirs(cwd)) try {
|
|
154
|
-
const files = await promises.readdir(dir);
|
|
155
|
-
for (const f of files) if (f.endsWith(`.jsonl`)) ids.add(f.slice(0, -`.jsonl`.length));
|
|
156
|
-
} catch {}
|
|
157
|
-
return ids;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Deterministic-path discovery for a freshly created session. After the
|
|
161
|
-
* Claude CLI runs in `-p` mode it writes the new JSONL straight into
|
|
162
|
-
* `~/.claude/projects/<sanitize(cwd)>/<id>.jsonl` *without* leaving a
|
|
163
|
-
* `~/.claude/sessions/<pid>.json` lock file (those are interactive-only),
|
|
164
|
-
* so `discoverSessions` can miss it. Compute the expected dir directly
|
|
165
|
-
* and diff its contents against a pre-run snapshot. Returns the newest
|
|
166
|
-
* fresh sessionId or null. Codex falls back to discoverNewestSession.
|
|
167
|
-
*/
|
|
168
|
-
async function findNewSessionAfterRun(agent, cwd, preDirectIds, preDiscoveredIds) {
|
|
169
|
-
if (agent === `claude`) {
|
|
170
|
-
const dirs = await getClaudeProjectDirs(cwd);
|
|
171
|
-
let best = null;
|
|
172
|
-
for (const dir of dirs) try {
|
|
173
|
-
const files = await promises.readdir(dir);
|
|
174
|
-
for (const f of files) {
|
|
175
|
-
if (!f.endsWith(`.jsonl`)) continue;
|
|
176
|
-
const id = f.slice(0, -`.jsonl`.length);
|
|
177
|
-
if (preDirectIds.has(id)) continue;
|
|
178
|
-
const st = await promises.stat(path.join(dir, f)).catch(() => null);
|
|
179
|
-
if (!st) continue;
|
|
180
|
-
if (!best || st.mtimeMs > best.mtime) best = {
|
|
181
|
-
id,
|
|
182
|
-
mtime: st.mtimeMs
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
} catch {}
|
|
186
|
-
if (best) return best.id;
|
|
187
|
-
}
|
|
188
|
-
return discoverNewestSession(agent, cwd, preDiscoveredIds);
|
|
189
|
-
}
|
|
190
|
-
const sessionMetaRowSchema = z.object({
|
|
191
|
-
key: z.literal(`current`),
|
|
192
|
-
electricSessionId: z.string(),
|
|
193
|
-
nativeSessionId: z.string().optional(),
|
|
194
|
-
agent: z.enum([`claude`, `codex`]),
|
|
195
|
-
cwd: z.string(),
|
|
196
|
-
status: z.enum([
|
|
197
|
-
`initializing`,
|
|
198
|
-
`idle`,
|
|
199
|
-
`running`,
|
|
200
|
-
`error`
|
|
201
|
-
]),
|
|
202
|
-
error: z.string().optional(),
|
|
203
|
-
currentPromptInboxKey: z.string().optional()
|
|
204
|
-
});
|
|
205
|
-
const cursorStateRowSchema = z.object({
|
|
206
|
-
key: z.literal(`current`),
|
|
207
|
-
cursor: z.string(),
|
|
208
|
-
lastProcessedInboxKey: z.string().optional()
|
|
209
|
-
});
|
|
210
|
-
const eventRowSchema = z.object({
|
|
211
|
-
key: z.string(),
|
|
212
|
-
ts: z.number(),
|
|
213
|
-
type: z.string(),
|
|
214
|
-
callId: z.string().optional(),
|
|
215
|
-
payload: z.looseObject({})
|
|
216
|
-
});
|
|
217
|
-
const creationArgsSchema = z.object({
|
|
218
|
-
agent: z.enum([`claude`, `codex`]),
|
|
219
|
-
cwd: z.string().optional(),
|
|
220
|
-
nativeSessionId: z.string().optional(),
|
|
221
|
-
importFrom: z.object({
|
|
222
|
-
agent: z.enum([`claude`, `codex`]),
|
|
223
|
-
sessionId: z.string()
|
|
224
|
-
}).optional()
|
|
225
|
-
});
|
|
226
|
-
const promptMessageSchema = z.object({ text: z.string() });
|
|
227
|
-
/**
|
|
228
|
-
* Stable key for an events-collection row, derived from the event's content.
|
|
229
|
-
* Lets us re-insert the same event without producing duplicates — the caller
|
|
230
|
-
* (or the collection's uniqueness guard) uses this to de-dup across retries,
|
|
231
|
-
* replays, and crash recovery. Sorts chronologically by ts, then by type.
|
|
232
|
-
*/
|
|
233
|
-
function eventKey(event) {
|
|
234
|
-
const tsPart = String(event.ts).padStart(16, `0`);
|
|
235
|
-
return `${tsPart}_${event.type}_${contentHashHex(event)}`;
|
|
236
|
-
}
|
|
237
|
-
function contentHashHex(event) {
|
|
238
|
-
const json = JSON.stringify(event);
|
|
239
|
-
let h = 5381;
|
|
240
|
-
for (let i = 0; i < json.length; i++) h = (h * 33 ^ json.charCodeAt(i)) >>> 0;
|
|
241
|
-
return h.toString(16).padStart(8, `0`);
|
|
242
|
-
}
|
|
243
|
-
function buildEventRow(event) {
|
|
244
|
-
const callId = `callId` in event && typeof event.callId === `string` ? event.callId : void 0;
|
|
245
|
-
return {
|
|
246
|
-
key: eventKey(event),
|
|
247
|
-
ts: event.ts,
|
|
248
|
-
type: event.type,
|
|
249
|
-
...callId !== void 0 ? { callId } : {},
|
|
250
|
-
payload: event
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
function appendIfNew(ctx, event) {
|
|
254
|
-
const row = buildEventRow(event);
|
|
255
|
-
if (ctx.events.get(row.key) !== void 0) return;
|
|
256
|
-
ctx.actions.events_insert({ row });
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Mirror every event that lands in the JSONL file while `runWork` is
|
|
260
|
-
* executing (i.e. while the CLI is running). Returns the advanced cursor
|
|
261
|
-
* and the `runWork` result once everything has settled and every append
|
|
262
|
-
* has been persisted to the entity's durable stream.
|
|
263
|
-
*
|
|
264
|
-
* If setup fails (e.g. the session file can't be resolved), `runWork`
|
|
265
|
-
* still runs — but nothing is mirrored and `setupError` is populated so
|
|
266
|
-
* the caller can surface the condition. If `runWork` throws, the error
|
|
267
|
-
* propagates after the watcher has been cleaned up.
|
|
268
|
-
*/
|
|
269
|
-
async function runWithLiveMirror(opts) {
|
|
270
|
-
let cursor = null;
|
|
271
|
-
let setupError = void 0;
|
|
272
|
-
try {
|
|
273
|
-
const session = await resolveSession(opts.nativeSessionId, opts.agent);
|
|
274
|
-
if (opts.serializedCursor) cursor = deserializeCursor({
|
|
275
|
-
...opts.serializedCursor,
|
|
276
|
-
path: session.path
|
|
277
|
-
});
|
|
278
|
-
else {
|
|
279
|
-
const initial = await loadSession({
|
|
280
|
-
sessionId: opts.nativeSessionId,
|
|
281
|
-
agent: opts.agent
|
|
282
|
-
});
|
|
283
|
-
for (const ev of initial.events) appendIfNew(opts.ctx, ev);
|
|
284
|
-
cursor = initial.cursor;
|
|
285
|
-
}
|
|
286
|
-
} catch (e) {
|
|
287
|
-
setupError = e;
|
|
288
|
-
}
|
|
289
|
-
if (!cursor) {
|
|
290
|
-
const result$1 = await opts.runWork();
|
|
291
|
-
return {
|
|
292
|
-
cursor: opts.serializedCursor,
|
|
293
|
-
setupError,
|
|
294
|
-
result: result$1
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
let activeCursor = cursor;
|
|
298
|
-
let busy = false;
|
|
299
|
-
let pending = false;
|
|
300
|
-
let stopped = false;
|
|
301
|
-
const drainOnce = async () => {
|
|
302
|
-
if (stopped && busy) return;
|
|
303
|
-
if (busy) {
|
|
304
|
-
pending = true;
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
busy = true;
|
|
308
|
-
try {
|
|
309
|
-
const res = await tailSession({ cursor: activeCursor });
|
|
310
|
-
activeCursor = res.cursor;
|
|
311
|
-
for (const ev of res.newEvents) appendIfNew(opts.ctx, ev);
|
|
312
|
-
} catch {} finally {
|
|
313
|
-
busy = false;
|
|
314
|
-
if (pending && !stopped) {
|
|
315
|
-
pending = false;
|
|
316
|
-
drainOnce();
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
const fileWatcher = watch(activeCursor.path, () => {
|
|
321
|
-
drainOnce();
|
|
322
|
-
});
|
|
323
|
-
const pollHandle = setInterval(() => {
|
|
324
|
-
drainOnce();
|
|
325
|
-
}, 1500);
|
|
326
|
-
let result;
|
|
327
|
-
try {
|
|
328
|
-
result = await opts.runWork();
|
|
329
|
-
} finally {
|
|
330
|
-
stopped = true;
|
|
331
|
-
clearInterval(pollHandle);
|
|
332
|
-
fileWatcher.close();
|
|
333
|
-
while (busy) await new Promise((r) => setTimeout(r, 10));
|
|
334
|
-
try {
|
|
335
|
-
const final = await tailSession({ cursor: activeCursor });
|
|
336
|
-
activeCursor = final.cursor;
|
|
337
|
-
for (const ev of final.newEvents) appendIfNew(opts.ctx, ev);
|
|
338
|
-
} catch {}
|
|
339
|
-
}
|
|
340
|
-
return {
|
|
341
|
-
cursor: serializeCursor(activeCursor),
|
|
342
|
-
setupError,
|
|
343
|
-
result
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
function registerCodingSession(registry, options = {}) {
|
|
347
|
-
const runner = options.cliRunner ?? defaultCliRunner;
|
|
348
|
-
const defaultCwd = options.defaultWorkingDirectory ?? process.cwd();
|
|
349
|
-
registry.define(`coder`, {
|
|
350
|
-
description: `Runs a Claude Code / Codex CLI session and mirrors its normalized event stream into a durable store. Prompts arrive via message_received (type: "prompt") and are executed serially.`,
|
|
351
|
-
creationSchema: creationArgsSchema,
|
|
352
|
-
inboxSchemas: { prompt: promptMessageSchema },
|
|
353
|
-
state: {
|
|
354
|
-
sessionMeta: {
|
|
355
|
-
schema: sessionMetaRowSchema,
|
|
356
|
-
type: CODING_SESSION_META_COLLECTION_TYPE,
|
|
357
|
-
primaryKey: `key`
|
|
358
|
-
},
|
|
359
|
-
cursorState: {
|
|
360
|
-
schema: cursorStateRowSchema,
|
|
361
|
-
type: CODING_SESSION_CURSOR_COLLECTION_TYPE,
|
|
362
|
-
primaryKey: `key`
|
|
363
|
-
},
|
|
364
|
-
events: {
|
|
365
|
-
schema: eventRowSchema,
|
|
366
|
-
type: CODING_SESSION_EVENT_COLLECTION_TYPE,
|
|
367
|
-
primaryKey: `key`
|
|
368
|
-
}
|
|
369
|
-
},
|
|
370
|
-
async handler(ctx, _wake) {
|
|
371
|
-
const existingMeta = ctx.db.collections.sessionMeta.get(`current`);
|
|
372
|
-
if (!existingMeta) {
|
|
373
|
-
const args = creationArgsSchema.parse(ctx.args);
|
|
374
|
-
const cwd = args.cwd ?? defaultCwd;
|
|
375
|
-
const electricSessionId = ctx.entityUrl.split(`/`).pop() ?? ctx.entityUrl;
|
|
376
|
-
let resolvedNativeId = args.nativeSessionId;
|
|
377
|
-
if (args.importFrom) {
|
|
378
|
-
const result = await importLocalSession({
|
|
379
|
-
source: {
|
|
380
|
-
sessionId: args.importFrom.sessionId,
|
|
381
|
-
agent: args.importFrom.agent
|
|
382
|
-
},
|
|
383
|
-
target: {
|
|
384
|
-
agent: args.agent,
|
|
385
|
-
cwd
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
resolvedNativeId = result.sessionId;
|
|
389
|
-
}
|
|
390
|
-
const hasNative = resolvedNativeId !== void 0;
|
|
391
|
-
ctx.db.actions.sessionMeta_insert({ row: {
|
|
392
|
-
key: `current`,
|
|
393
|
-
electricSessionId,
|
|
394
|
-
...hasNative ? { nativeSessionId: resolvedNativeId } : {},
|
|
395
|
-
agent: args.agent,
|
|
396
|
-
cwd,
|
|
397
|
-
status: hasNative ? `idle` : `initializing`
|
|
398
|
-
} });
|
|
399
|
-
}
|
|
400
|
-
if (!ctx.db.collections.cursorState.get(`current`)) ctx.db.actions.cursorState_insert({ row: {
|
|
401
|
-
key: `current`,
|
|
402
|
-
cursor: ``
|
|
403
|
-
} });
|
|
404
|
-
const metaRow = ctx.db.collections.sessionMeta.get(`current`);
|
|
405
|
-
const cursorRow = ctx.db.collections.cursorState.get(`current`);
|
|
406
|
-
if (!metaRow || !cursorRow) throw new Error(`[coding-session] expected sessionMeta and cursorState rows to exist after init`);
|
|
407
|
-
if (metaRow.nativeSessionId && !cursorRow.cursor) {
|
|
408
|
-
const mirrorCtx = {
|
|
409
|
-
events: { get: (k) => ctx.db.collections.events.get(k) },
|
|
410
|
-
actions: { events_insert: ctx.db.actions.events_insert }
|
|
411
|
-
};
|
|
412
|
-
try {
|
|
413
|
-
const initial = await loadSession({
|
|
414
|
-
sessionId: metaRow.nativeSessionId,
|
|
415
|
-
agent: metaRow.agent
|
|
416
|
-
});
|
|
417
|
-
for (const ev of initial.events) appendIfNew(mirrorCtx, ev);
|
|
418
|
-
const serialized = serializeCursor(initial.cursor);
|
|
419
|
-
ctx.db.actions.cursorState_update({
|
|
420
|
-
key: `current`,
|
|
421
|
-
updater: (d) => {
|
|
422
|
-
d.cursor = JSON.stringify(serialized);
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
} catch (e) {
|
|
426
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
427
|
-
ctx.db.actions.sessionMeta_update({
|
|
428
|
-
key: `current`,
|
|
429
|
-
updater: (d) => {
|
|
430
|
-
d.error = `initial mirror failed: ${message}`;
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
const inboxRows = ctx.db.collections.inbox.toArray.slice().sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
436
|
-
const lastKey = cursorRow.lastProcessedInboxKey ?? ``;
|
|
437
|
-
const pending = inboxRows.filter((m) => m.key > lastKey);
|
|
438
|
-
if (pending.length === 0) {
|
|
439
|
-
if (metaRow.status === `running` || metaRow.status === `error`) ctx.db.actions.sessionMeta_update({
|
|
440
|
-
key: `current`,
|
|
441
|
-
updater: (d) => {
|
|
442
|
-
d.status = `idle`;
|
|
443
|
-
delete d.currentPromptInboxKey;
|
|
444
|
-
delete d.error;
|
|
445
|
-
}
|
|
446
|
-
});
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
let runningMeta = metaRow;
|
|
450
|
-
let runningCursor = cursorRow;
|
|
451
|
-
for (const inboxMsg of pending) {
|
|
452
|
-
const parsed = promptMessageSchema.safeParse(inboxMsg.payload);
|
|
453
|
-
if (!parsed.success) {
|
|
454
|
-
ctx.db.actions.cursorState_update({
|
|
455
|
-
key: `current`,
|
|
456
|
-
updater: (d) => {
|
|
457
|
-
d.lastProcessedInboxKey = inboxMsg.key;
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
runningCursor = {
|
|
461
|
-
...runningCursor,
|
|
462
|
-
lastProcessedInboxKey: inboxMsg.key
|
|
463
|
-
};
|
|
464
|
-
continue;
|
|
465
|
-
}
|
|
466
|
-
const prompt = parsed.data.text;
|
|
467
|
-
const existingTitle = ctx.tags.title;
|
|
468
|
-
if (typeof existingTitle !== `string` || existingTitle.length === 0) ctx.setTag(`title`, prompt.slice(0, 80));
|
|
469
|
-
ctx.db.actions.sessionMeta_update({
|
|
470
|
-
key: `current`,
|
|
471
|
-
updater: (d) => {
|
|
472
|
-
d.status = `running`;
|
|
473
|
-
d.currentPromptInboxKey = inboxMsg.key;
|
|
474
|
-
delete d.error;
|
|
475
|
-
}
|
|
476
|
-
});
|
|
477
|
-
const recordedRun = ctx.recordRun();
|
|
478
|
-
const eventKeysBefore = new Set(ctx.db.collections.events.toArray.map((e) => e.key));
|
|
479
|
-
try {
|
|
480
|
-
const mirrorCtx = {
|
|
481
|
-
events: { get: (k) => ctx.db.collections.events.get(k) },
|
|
482
|
-
actions: { events_insert: ctx.db.actions.events_insert }
|
|
483
|
-
};
|
|
484
|
-
let nextCursorJson = runningCursor.cursor;
|
|
485
|
-
if (!runningMeta.nativeSessionId) {
|
|
486
|
-
const preDirectIds = runningMeta.agent === `claude` ? await listClaudeJsonlIdsByCwd(runningMeta.cwd) : new Set();
|
|
487
|
-
const preDiscoveredIds = new Set((await discoverSessions(runningMeta.agent)).map((s) => s.sessionId));
|
|
488
|
-
const cliResult = await runner.run({
|
|
489
|
-
agent: runningMeta.agent,
|
|
490
|
-
cwd: runningMeta.cwd,
|
|
491
|
-
prompt
|
|
492
|
-
});
|
|
493
|
-
if (cliResult.exitCode !== 0) throw new Error(`[coding-session] ${runningMeta.agent} CLI exited ${cliResult.exitCode}. stderr=${cliResult.stderr.slice(0, 800) || `<empty>`} stdout=${cliResult.stdout.slice(0, 800) || `<empty>`}`);
|
|
494
|
-
const foundId = await findNewSessionAfterRun(runningMeta.agent, runningMeta.cwd, preDirectIds, preDiscoveredIds);
|
|
495
|
-
if (!foundId) throw new Error(`[coding-session] ${runningMeta.agent} CLI succeeded but no new session file was found`);
|
|
496
|
-
ctx.db.actions.sessionMeta_update({
|
|
497
|
-
key: `current`,
|
|
498
|
-
updater: (d) => {
|
|
499
|
-
d.nativeSessionId = foundId;
|
|
500
|
-
}
|
|
501
|
-
});
|
|
502
|
-
runningMeta = {
|
|
503
|
-
...runningMeta,
|
|
504
|
-
nativeSessionId: foundId
|
|
505
|
-
};
|
|
506
|
-
const initial = await loadSession({
|
|
507
|
-
sessionId: foundId,
|
|
508
|
-
agent: runningMeta.agent
|
|
509
|
-
});
|
|
510
|
-
for (const ev of initial.events) appendIfNew(mirrorCtx, ev);
|
|
511
|
-
nextCursorJson = JSON.stringify(serializeCursor(initial.cursor));
|
|
512
|
-
} else {
|
|
513
|
-
const serializedCursor = runningCursor.cursor ? JSON.parse(runningCursor.cursor) : null;
|
|
514
|
-
const { cursor: nextSerialized, setupError, result: cliResult } = await runWithLiveMirror({
|
|
515
|
-
agent: runningMeta.agent,
|
|
516
|
-
nativeSessionId: runningMeta.nativeSessionId,
|
|
517
|
-
serializedCursor,
|
|
518
|
-
ctx: mirrorCtx,
|
|
519
|
-
runWork: () => runner.run({
|
|
520
|
-
agent: runningMeta.agent,
|
|
521
|
-
sessionId: runningMeta.nativeSessionId,
|
|
522
|
-
cwd: runningMeta.cwd,
|
|
523
|
-
prompt
|
|
524
|
-
})
|
|
525
|
-
});
|
|
526
|
-
if (setupError) throw setupError instanceof Error ? setupError : new Error(String(setupError));
|
|
527
|
-
if (cliResult.exitCode !== 0) throw new Error(`[coding-session] ${runningMeta.agent} CLI exited ${cliResult.exitCode}. stderr=${cliResult.stderr.slice(0, 800) || `<empty>`} stdout=${cliResult.stdout.slice(0, 800) || `<empty>`}`);
|
|
528
|
-
const persistedCursor = nextSerialized ?? serializedCursor;
|
|
529
|
-
nextCursorJson = persistedCursor ? JSON.stringify(persistedCursor) : ``;
|
|
530
|
-
}
|
|
531
|
-
ctx.db.actions.cursorState_update({
|
|
532
|
-
key: `current`,
|
|
533
|
-
updater: (d) => {
|
|
534
|
-
d.cursor = nextCursorJson;
|
|
535
|
-
d.lastProcessedInboxKey = inboxMsg.key;
|
|
536
|
-
}
|
|
537
|
-
});
|
|
538
|
-
runningCursor = {
|
|
539
|
-
...runningCursor,
|
|
540
|
-
cursor: nextCursorJson,
|
|
541
|
-
lastProcessedInboxKey: inboxMsg.key
|
|
542
|
-
};
|
|
543
|
-
for (const row of ctx.db.collections.events.toArray) {
|
|
544
|
-
if (eventKeysBefore.has(row.key)) continue;
|
|
545
|
-
if (row.type !== `assistant_message`) continue;
|
|
546
|
-
const text = row.payload?.text;
|
|
547
|
-
if (typeof text === `string` && text.length > 0) recordedRun.attachResponse(text);
|
|
548
|
-
}
|
|
549
|
-
recordedRun.end({ status: `completed` });
|
|
550
|
-
} catch (e) {
|
|
551
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
552
|
-
recordedRun.end({
|
|
553
|
-
status: `failed`,
|
|
554
|
-
finishReason: `error`
|
|
555
|
-
});
|
|
556
|
-
ctx.db.actions.sessionMeta_update({
|
|
557
|
-
key: `current`,
|
|
558
|
-
updater: (d) => {
|
|
559
|
-
d.status = `error`;
|
|
560
|
-
d.error = message;
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
ctx.db.actions.cursorState_update({
|
|
564
|
-
key: `current`,
|
|
565
|
-
updater: (d) => {
|
|
566
|
-
d.lastProcessedInboxKey = inboxMsg.key;
|
|
567
|
-
}
|
|
568
|
-
});
|
|
569
|
-
throw e;
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
ctx.db.actions.sessionMeta_update({
|
|
573
|
-
key: `current`,
|
|
574
|
-
updater: (d) => {
|
|
575
|
-
d.status = `idle`;
|
|
576
|
-
delete d.currentPromptInboxKey;
|
|
577
|
-
delete d.error;
|
|
578
|
-
}
|
|
579
|
-
});
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
|
|
584
73
|
//#endregion
|
|
585
74
|
//#region src/docs/embed.ts
|
|
586
75
|
const EMBEDDING_DIMENSIONS = 128;
|
|
@@ -1385,11 +874,11 @@ const WORKER_TOOL_NAMES = [
|
|
|
1385
874
|
`read`,
|
|
1386
875
|
`write`,
|
|
1387
876
|
`edit`,
|
|
1388
|
-
`
|
|
877
|
+
`web_search`,
|
|
1389
878
|
`fetch_url`,
|
|
1390
879
|
`spawn_worker`
|
|
1391
880
|
];
|
|
1392
|
-
function createSpawnWorkerTool(ctx) {
|
|
881
|
+
function createSpawnWorkerTool(ctx, modelConfig) {
|
|
1393
882
|
return {
|
|
1394
883
|
name: `spawn_worker`,
|
|
1395
884
|
label: `Spawn Worker`,
|
|
@@ -1416,10 +905,16 @@ function createSpawnWorkerTool(ctx) {
|
|
|
1416
905
|
details: { spawned: false }
|
|
1417
906
|
};
|
|
1418
907
|
const id = nanoid(10);
|
|
908
|
+
const workerModelArgs = modelConfig ? {
|
|
909
|
+
provider: modelConfig.provider,
|
|
910
|
+
model: modelConfig.model,
|
|
911
|
+
...modelConfig.reasoningEffort && { reasoningEffort: modelConfig.reasoningEffort }
|
|
912
|
+
} : {};
|
|
1419
913
|
try {
|
|
1420
914
|
const handle = await ctx.spawn(`worker`, id, {
|
|
1421
915
|
systemPrompt,
|
|
1422
|
-
tools
|
|
916
|
+
tools,
|
|
917
|
+
...workerModelArgs
|
|
1423
918
|
}, {
|
|
1424
919
|
initialMessage,
|
|
1425
920
|
wake: {
|
|
@@ -1453,140 +948,137 @@ function createSpawnWorkerTool(ctx) {
|
|
|
1453
948
|
}
|
|
1454
949
|
|
|
1455
950
|
//#endregion
|
|
1456
|
-
//#region src/
|
|
1457
|
-
const
|
|
1458
|
-
|
|
951
|
+
//#region src/model-catalog.ts
|
|
952
|
+
const REASONING_EFFORT_VALUES = [
|
|
953
|
+
`auto`,
|
|
954
|
+
`minimal`,
|
|
955
|
+
`low`,
|
|
956
|
+
`medium`,
|
|
957
|
+
`high`
|
|
958
|
+
];
|
|
959
|
+
const DEFAULT_ANTHROPIC_MODEL = `claude-sonnet-4-6`;
|
|
960
|
+
const DEFAULT_OPENAI_MODEL = `gpt-4.1`;
|
|
961
|
+
const DEFAULT_CODEX_MODEL = `gpt-5.4`;
|
|
962
|
+
function modelValue(provider, id) {
|
|
963
|
+
return `${provider}:${id}`;
|
|
964
|
+
}
|
|
965
|
+
function providerLabel(provider) {
|
|
966
|
+
if (provider === `anthropic`) return `Anthropic`;
|
|
967
|
+
if (provider === `openai-codex`) return `OpenAI Codex`;
|
|
968
|
+
return `OpenAI`;
|
|
969
|
+
}
|
|
970
|
+
function configuredProviders() {
|
|
971
|
+
return detectAvailableProviders();
|
|
972
|
+
}
|
|
973
|
+
function mockFallbackCatalog() {
|
|
974
|
+
const fallback = {
|
|
975
|
+
provider: `anthropic`,
|
|
976
|
+
id: DEFAULT_ANTHROPIC_MODEL,
|
|
977
|
+
label: `Anthropic ${DEFAULT_ANTHROPIC_MODEL}`,
|
|
978
|
+
value: modelValue(`anthropic`, DEFAULT_ANTHROPIC_MODEL),
|
|
979
|
+
reasoning: true
|
|
980
|
+
};
|
|
1459
981
|
return {
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
description: `Spawn a coding-session subagent (a coder) that drives a Claude Code or Codex CLI session in a working directory. Use when the user asks for code changes, file edits, debugging, or any task that benefits from a real coding agent with tool access. The coder is long-lived — its URL stays valid across many turns, so you can keep prompting it via prompt_coder without re-spawning. End your turn after spawning; you'll be woken when the coder finishes its first reply.`,
|
|
1463
|
-
parameters: Type.Object({
|
|
1464
|
-
prompt: Type.String({ description: `First user message sent to the coder. This is what kicks off the run — without it the coder will idle. Be concrete: describe the task, mention the files/paths involved, and what form of answer you want back.` }),
|
|
1465
|
-
agent: Type.Optional(Type.Union(CODER_AGENT_NAMES.map((n) => Type.Literal(n)), { description: `Which coding agent to use. Defaults to "claude". Use "codex" only if the user explicitly asks for it.` })),
|
|
1466
|
-
cwd: Type.Optional(Type.String({ description: `Working directory the coder runs in. Defaults to the runtime's cwd (the same directory Horton is running in). Set this when the user wants the coder to operate on a different repo.` }))
|
|
1467
|
-
}),
|
|
1468
|
-
execute: async (_toolCallId, params) => {
|
|
1469
|
-
const { prompt, agent, cwd } = params;
|
|
1470
|
-
if (typeof prompt !== `string` || prompt.length === 0) return {
|
|
1471
|
-
content: [{
|
|
1472
|
-
type: `text`,
|
|
1473
|
-
text: `Error: prompt is required and must be a non-empty string.`
|
|
1474
|
-
}],
|
|
1475
|
-
details: { spawned: false }
|
|
1476
|
-
};
|
|
1477
|
-
const id = nanoid(10);
|
|
1478
|
-
const spawnArgs = { agent: agent ?? `claude` };
|
|
1479
|
-
if (cwd) spawnArgs.cwd = cwd;
|
|
1480
|
-
try {
|
|
1481
|
-
const handle = await ctx.spawn(`coder`, id, spawnArgs, {
|
|
1482
|
-
initialMessage: { text: prompt },
|
|
1483
|
-
wake: {
|
|
1484
|
-
on: `runFinished`,
|
|
1485
|
-
includeResponse: true
|
|
1486
|
-
}
|
|
1487
|
-
});
|
|
1488
|
-
const coderUrl = handle.entityUrl;
|
|
1489
|
-
return {
|
|
1490
|
-
content: [{
|
|
1491
|
-
type: `text`,
|
|
1492
|
-
text: `Coder dispatched at ${coderUrl}. End your turn — when the coder finishes its current reply you'll be woken with the response. To send follow-up prompts to the same coder, call prompt_coder with this URL.`
|
|
1493
|
-
}],
|
|
1494
|
-
details: {
|
|
1495
|
-
spawned: true,
|
|
1496
|
-
coderUrl
|
|
1497
|
-
}
|
|
1498
|
-
};
|
|
1499
|
-
} catch (err) {
|
|
1500
|
-
serverLog.warn(`[spawn_coder tool] failed to spawn coder ${id}: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : void 0);
|
|
1501
|
-
return {
|
|
1502
|
-
content: [{
|
|
1503
|
-
type: `text`,
|
|
1504
|
-
text: `Error spawning coder: ${err instanceof Error ? err.message : `Unknown error`}`
|
|
1505
|
-
}],
|
|
1506
|
-
details: { spawned: false }
|
|
1507
|
-
};
|
|
1508
|
-
}
|
|
1509
|
-
}
|
|
982
|
+
choices: [fallback],
|
|
983
|
+
defaultChoice: fallback
|
|
1510
984
|
};
|
|
1511
985
|
}
|
|
1512
|
-
function
|
|
986
|
+
async function fetchAvailableModelIds(provider) {
|
|
987
|
+
try {
|
|
988
|
+
const res = provider === `anthropic` ? await fetch(`https://api.anthropic.com/v1/models`, {
|
|
989
|
+
headers: {
|
|
990
|
+
"x-api-key": process.env.ANTHROPIC_API_KEY ?? ``,
|
|
991
|
+
"anthropic-version": `2023-06-01`
|
|
992
|
+
},
|
|
993
|
+
signal: AbortSignal.timeout(3e3)
|
|
994
|
+
}) : await fetch(`https://api.openai.com/v1/models`, {
|
|
995
|
+
headers: { authorization: `Bearer ${process.env.OPENAI_API_KEY ?? ``}` },
|
|
996
|
+
signal: AbortSignal.timeout(3e3)
|
|
997
|
+
});
|
|
998
|
+
if (res.status === 401 || res.status === 403) return new Set();
|
|
999
|
+
if (!res.ok) return null;
|
|
1000
|
+
const body = await res.json();
|
|
1001
|
+
const ids = new Set((body.data ?? []).map((model) => model.id).filter((id) => typeof id === `string`));
|
|
1002
|
+
return ids.size > 0 ? ids : null;
|
|
1003
|
+
} catch {
|
|
1004
|
+
return null;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
async function choicesForProvider(provider) {
|
|
1008
|
+
const knownModels = getModels(provider);
|
|
1009
|
+
if (provider === `openai-codex`) return knownModels.map((model) => ({
|
|
1010
|
+
provider,
|
|
1011
|
+
id: model.id,
|
|
1012
|
+
label: `${providerLabel(provider)} ${model.name}`,
|
|
1013
|
+
value: modelValue(provider, model.id),
|
|
1014
|
+
reasoning: model.reasoning
|
|
1015
|
+
}));
|
|
1016
|
+
const availableIds = await fetchAvailableModelIds(provider);
|
|
1017
|
+
const models = availableIds === null ? knownModels : knownModels.filter((model) => availableIds.has(model.id));
|
|
1018
|
+
return models.map((model) => ({
|
|
1019
|
+
provider,
|
|
1020
|
+
id: model.id,
|
|
1021
|
+
label: `${providerLabel(provider)} ${model.name}`,
|
|
1022
|
+
value: modelValue(provider, model.id),
|
|
1023
|
+
reasoning: model.reasoning
|
|
1024
|
+
}));
|
|
1025
|
+
}
|
|
1026
|
+
function withProviderPayloadDefaults(config, choice, reasoningEffort) {
|
|
1027
|
+
if (choice.provider !== `openai` && choice.provider !== `openai-codex` || !choice.reasoning) return config;
|
|
1028
|
+
const defaultEffort = choice.provider === `openai-codex` ? `low` : `minimal`;
|
|
1029
|
+
const effort = reasoningEffort === `minimal` && choice.provider === `openai-codex` ? `low` : reasoningEffort ?? defaultEffort;
|
|
1513
1030
|
return {
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
type: `text`,
|
|
1526
|
-
text: `Error: coder_url must be a path like "/coder/<id>".`
|
|
1527
|
-
}],
|
|
1528
|
-
details: { sent: false }
|
|
1529
|
-
};
|
|
1530
|
-
if (typeof prompt !== `string` || prompt.length === 0) return {
|
|
1531
|
-
content: [{
|
|
1532
|
-
type: `text`,
|
|
1533
|
-
text: `Error: prompt is required and must be a non-empty string.`
|
|
1534
|
-
}],
|
|
1535
|
-
details: { sent: false }
|
|
1031
|
+
...config,
|
|
1032
|
+
onPayload: (payload) => {
|
|
1033
|
+
if (typeof payload !== `object` || payload === null) return void 0;
|
|
1034
|
+
const body = payload;
|
|
1035
|
+
const existingReasoning = typeof body.reasoning === `object` && body.reasoning !== null ? body.reasoning : {};
|
|
1036
|
+
return {
|
|
1037
|
+
...body,
|
|
1038
|
+
reasoning: {
|
|
1039
|
+
...existingReasoning,
|
|
1040
|
+
effort
|
|
1041
|
+
}
|
|
1536
1042
|
};
|
|
1537
|
-
try {
|
|
1538
|
-
ctx.send(coder_url, { text: prompt });
|
|
1539
|
-
return {
|
|
1540
|
-
content: [{
|
|
1541
|
-
type: `text`,
|
|
1542
|
-
text: `Prompt queued for ${coder_url}. End your turn — you'll be woken when the coder's reply lands.`
|
|
1543
|
-
}],
|
|
1544
|
-
details: {
|
|
1545
|
-
sent: true,
|
|
1546
|
-
coderUrl: coder_url
|
|
1547
|
-
}
|
|
1548
|
-
};
|
|
1549
|
-
} catch (err) {
|
|
1550
|
-
serverLog.warn(`[prompt_coder tool] failed to send to ${coder_url}: ${err instanceof Error ? err.message : String(err)}`, err instanceof Error ? err : void 0);
|
|
1551
|
-
return {
|
|
1552
|
-
content: [{
|
|
1553
|
-
type: `text`,
|
|
1554
|
-
text: `Error sending prompt to coder: ${err instanceof Error ? err.message : `Unknown error`}`
|
|
1555
|
-
}],
|
|
1556
|
-
details: { sent: false }
|
|
1557
|
-
};
|
|
1558
|
-
}
|
|
1559
1043
|
}
|
|
1560
1044
|
};
|
|
1561
1045
|
}
|
|
1046
|
+
function parseReasoningEffort(value) {
|
|
1047
|
+
return value === `minimal` || value === `low` || value === `medium` || value === `high` ? value : null;
|
|
1048
|
+
}
|
|
1049
|
+
async function createBuiltinModelCatalog(options = {}) {
|
|
1050
|
+
const providers = configuredProviders();
|
|
1051
|
+
if (providers.length === 0 && options.allowMockFallback) return mockFallbackCatalog();
|
|
1052
|
+
const choices = (await Promise.all(providers.map((provider) => choicesForProvider(provider)))).flat();
|
|
1053
|
+
if (choices.length === 0) return options.allowMockFallback ? mockFallbackCatalog() : null;
|
|
1054
|
+
const defaultChoice = choices.find((choice) => choice.provider === `anthropic` && choice.id === DEFAULT_ANTHROPIC_MODEL) ?? choices.find((choice) => choice.provider === `openai` && choice.id === DEFAULT_OPENAI_MODEL) ?? choices.find((choice) => choice.provider === `openai-codex` && choice.id === DEFAULT_CODEX_MODEL) ?? choices[0];
|
|
1055
|
+
return {
|
|
1056
|
+
choices,
|
|
1057
|
+
defaultChoice
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
function resolveBuiltinModelConfig(catalog, args) {
|
|
1061
|
+
const modelArg = args.model;
|
|
1062
|
+
const providerArg = args.provider;
|
|
1063
|
+
const reasoningEffort = parseReasoningEffort(args.reasoningEffort);
|
|
1064
|
+
const selected = typeof modelArg === `string` ? catalog.choices.find((choice$1) => choice$1.value === modelArg || choice$1.id === modelArg && choice$1.provider === providerArg) : void 0;
|
|
1065
|
+
const choice = selected ?? catalog.defaultChoice;
|
|
1066
|
+
const config = {
|
|
1067
|
+
provider: choice.provider,
|
|
1068
|
+
model: choice.id,
|
|
1069
|
+
...reasoningEffort && { reasoningEffort },
|
|
1070
|
+
...choice.provider === `openai-codex` && { getApiKey: () => readCodexAccessToken() }
|
|
1071
|
+
};
|
|
1072
|
+
return withProviderPayloadDefaults(config, choice, reasoningEffort);
|
|
1073
|
+
}
|
|
1074
|
+
function modelChoiceValues(catalog) {
|
|
1075
|
+
return catalog.choices.map((choice) => choice.value);
|
|
1076
|
+
}
|
|
1562
1077
|
|
|
1563
1078
|
//#endregion
|
|
1564
1079
|
//#region src/agents/horton.ts
|
|
1565
|
-
const
|
|
1566
|
-
const
|
|
1567
|
-
let anthropic = null;
|
|
1568
|
-
function getClient() {
|
|
1569
|
-
if (!anthropic) anthropic = new Anthropic();
|
|
1570
|
-
return anthropic;
|
|
1571
|
-
}
|
|
1572
|
-
async function defaultHaikuCall(prompt) {
|
|
1573
|
-
const client = getClient();
|
|
1574
|
-
const res = await client.messages.create({
|
|
1575
|
-
model: TITLE_MODEL,
|
|
1576
|
-
max_tokens: 64,
|
|
1577
|
-
messages: [{
|
|
1578
|
-
role: `user`,
|
|
1579
|
-
content: prompt
|
|
1580
|
-
}]
|
|
1581
|
-
});
|
|
1582
|
-
const block = res.content[0];
|
|
1583
|
-
return block?.type === `text` ? block.text : ``;
|
|
1584
|
-
}
|
|
1585
|
-
const TITLE_PROMPT = (userMessage) => `Summarize the following user request in 3-5 words for use as a chat session title.
|
|
1586
|
-
Respond with only the title, no quotes, no punctuation, no preamble.
|
|
1587
|
-
|
|
1588
|
-
User request:
|
|
1589
|
-
${userMessage}`;
|
|
1080
|
+
const TITLE_SYSTEM_PROMPT = "You generate concise chat session titles in 3-5 words. Respond with only the title, no quotes, no punctuation, no preamble.";
|
|
1081
|
+
const TITLE_USER_PROMPT = (userMessage) => `User request:\n${userMessage}`;
|
|
1590
1082
|
const TITLE_STOP_WORDS = new Set([
|
|
1591
1083
|
`a`,
|
|
1592
1084
|
`an`,
|
|
@@ -1654,19 +1146,34 @@ function buildFallbackTitle(userMessage) {
|
|
|
1654
1146
|
const selected = informativeWords.length >= 2 ? informativeWords.slice(0, 5) : backupWords;
|
|
1655
1147
|
return selected.join(` `).slice(0, 80).trim() || `Untitled Chat`;
|
|
1656
1148
|
}
|
|
1657
|
-
|
|
1149
|
+
function createConfiguredTitleCall(catalog, modelConfig, logPrefix) {
|
|
1150
|
+
return (prompt) => completeWithLowCostModel({
|
|
1151
|
+
catalog,
|
|
1152
|
+
modelConfig,
|
|
1153
|
+
log: (message) => serverLog.info(message),
|
|
1154
|
+
logPrefix,
|
|
1155
|
+
purpose: `title generation`,
|
|
1156
|
+
systemPrompt: TITLE_SYSTEM_PROMPT,
|
|
1157
|
+
prompt,
|
|
1158
|
+
maxTokens: 64
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
async function generateTitle(userMessage, llmCall, onFallback) {
|
|
1658
1162
|
try {
|
|
1659
|
-
const raw = await llmCall(
|
|
1163
|
+
const raw = await llmCall(TITLE_USER_PROMPT(userMessage));
|
|
1660
1164
|
const title = raw.trim();
|
|
1661
|
-
|
|
1662
|
-
|
|
1165
|
+
if (title.length > 0) return title;
|
|
1166
|
+
onFallback?.(`empty LLM title response`);
|
|
1167
|
+
return buildFallbackTitle(userMessage);
|
|
1168
|
+
} catch (err) {
|
|
1169
|
+
onFallback?.(err instanceof Error ? err.message : String(err));
|
|
1663
1170
|
return buildFallbackTitle(userMessage);
|
|
1664
1171
|
}
|
|
1665
1172
|
}
|
|
1666
1173
|
function buildHortonSystemPrompt(workingDirectory, opts = {}) {
|
|
1667
1174
|
const docsTools = opts.hasDocsSupport ? `\n- search_durable_agents_docs: hybrid search over the built-in Durable Agents docs index` : ``;
|
|
1668
1175
|
const skillsTools = opts.hasSkills ? `\n- use_skill: load a skill (knowledge, instructions, or a tutorial) into your context to help with the user's request\n- remove_skill: unload a skill from context when you're done with it` : ``;
|
|
1669
|
-
const docsGuidance = opts.hasDocsSupport ? `\n- For ANY question about Electric Agents, Durable Agents, or this framework, ALWAYS use search_durable_agents_docs FIRST. Do not use
|
|
1176
|
+
const docsGuidance = opts.hasDocsSupport ? `\n- For ANY question about Electric Agents, Durable Agents, or this framework, ALWAYS use search_durable_agents_docs FIRST. Do not use web_search or fetch_url for Electric Agents topics unless the docs search returns no useful results.\n- The search tool returns chunk content directly — you do not need to read the source files.\n- Use repo read/bash tools only for non-doc files or when you need to inspect exact implementation code in the workspace.` : ``;
|
|
1670
1177
|
const skillsGuidance = opts.hasSkills ? `\n# Skills\nYou have access to skills — specialized knowledge and guided workflows you can load on demand. Your context includes a skills catalog listing what's available. When the user's request matches a skill's description or keywords, load it with use_skill.
|
|
1671
1178
|
|
|
1672
1179
|
Some skills are user-invocable — the user can trigger them with a slash command like \`/quickstart\`. When you see a message starting with \`/\` followed by a skill name, load that skill immediately with use_skill. Pass any text after the skill name as args.
|
|
@@ -1702,7 +1209,9 @@ Don't force onboarding. If someone just wants to chat or code, let them. When in
|
|
|
1702
1209
|
- ${opts.hasDocsSupport ? `If search_durable_agents_docs is available, use it first (faster, hybrid search).` : `Use fetch_url to look up documentation pages.`}
|
|
1703
1210
|
- The Electric Agents docs site is at ${opts.docsUrl}
|
|
1704
1211
|
- The docs site covers: Usage (entity definition, handlers, tools, state, spawning, coordination, waking, shared state, client integration, app setup), Reference (handler context, entity definitions, configurations, tools, state proxies, wake events, registries), Entities (Horton, Worker), and Patterns (Manager-Worker, Pipeline, Map-Reduce, Dispatcher, Blackboard, Reactive Observers).
|
|
1705
|
-
- For general coding questions unrelated to Electric Agents, use
|
|
1212
|
+
- For general coding questions unrelated to Electric Agents, use web_search or your own knowledge.` : ``;
|
|
1213
|
+
const modelGuidance = opts.modelProvider && opts.modelId ? `\n# Runtime model
|
|
1214
|
+
You are currently running via provider "${opts.modelProvider}" with model "${opts.modelId}". If the user asks what model or provider you are using, answer with these exact runtime values. Do not infer your model identity from training data or from the name of another coding tool.` : ``;
|
|
1706
1215
|
return `You are Horton, a friendly and capable assistant. You can chat, research the web, read and edit code, run shell commands, and dispatch subagents (workers) for isolated subtasks. Be warm and engaging in conversation; be precise and concrete when working with code.
|
|
1707
1216
|
|
|
1708
1217
|
# Greetings
|
|
@@ -1713,18 +1222,16 @@ When a user opens with a greeting ("hi", "hello", "hey", etc.) or a broad statem
|
|
|
1713
1222
|
- read: read a file
|
|
1714
1223
|
- write: create or overwrite a file
|
|
1715
1224
|
- edit: targeted string replacement in an existing file (you must read the file first)
|
|
1716
|
-
-
|
|
1225
|
+
- web_search: search the web
|
|
1717
1226
|
- fetch_url: fetch and convert a URL to markdown
|
|
1718
1227
|
- spawn_worker: dispatch a subagent for an isolated task
|
|
1719
|
-
- spawn_coder: spawn a long-lived coding agent (Claude Code or Codex CLI) for code changes, file edits, debugging
|
|
1720
|
-
- prompt_coder: send a follow-up prompt to a coder you previously spawned
|
|
1721
1228
|
${docsTools}${skillsTools}
|
|
1722
1229
|
|
|
1723
1230
|
# Working with files
|
|
1724
1231
|
- Prefer edit over write when modifying existing files.
|
|
1725
1232
|
- You must read a file before you can edit it.
|
|
1726
1233
|
- Use absolute paths or paths relative to the current working directory.
|
|
1727
|
-
${docsGuidance}${skillsGuidance}${onboardingGuidance}${docsUrlGuidance}
|
|
1234
|
+
${modelGuidance}${docsGuidance}${skillsGuidance}${onboardingGuidance}${docsUrlGuidance}
|
|
1728
1235
|
|
|
1729
1236
|
# Risky actions
|
|
1730
1237
|
Pause and confirm with the user before:
|
|
@@ -1745,13 +1252,6 @@ When you spawn a worker, write its system prompt the way you'd brief a colleague
|
|
|
1745
1252
|
|
|
1746
1253
|
After spawning, end your turn (optionally with a brief "I've dispatched a worker for X; I'll respond when it finishes"). When the worker finishes, you'll receive a message describing which worker completed and what it returned. Multiple workers may finish at different times — check the message for the worker URL to know which one you're hearing about.
|
|
1747
1254
|
|
|
1748
|
-
# When to spawn a coder
|
|
1749
|
-
Spawn a coder when the user asks for code changes, file edits, debugging, or any task that benefits from a real coding agent with full tool access (bash, file edits, etc.). A coder runs Claude Code or Codex CLI under the hood.
|
|
1750
|
-
|
|
1751
|
-
Unlike a worker, a coder is **long-lived**: its URL stays valid across many turns. Spawn once with spawn_coder, then keep prompting it via prompt_coder for follow-ups — don't spawn a new coder for each turn. Treat the coder URL like a chat handle.
|
|
1752
|
-
|
|
1753
|
-
After calling spawn_coder or prompt_coder, end your turn. When the coder's reply lands, you'll be woken with the response in the wake message — relay it (or a summary) back to the user, and call prompt_coder again if there's a follow-up.
|
|
1754
|
-
|
|
1755
1255
|
# Reporting
|
|
1756
1256
|
Report outcomes faithfully. If a command failed, say so with the relevant output. If you didn't run a verification step, say that rather than implying you did. Don't hedge confirmed results with unnecessary disclaimers.
|
|
1757
1257
|
|
|
@@ -1765,34 +1265,82 @@ function createHortonTools(workingDirectory, ctx, readSet, opts = {}) {
|
|
|
1765
1265
|
createWriteTool(workingDirectory, readSet),
|
|
1766
1266
|
createEditTool(workingDirectory, readSet),
|
|
1767
1267
|
braveSearchTool,
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1268
|
+
...opts.modelCatalog && opts.modelConfig ? [createFetchUrlTool({
|
|
1269
|
+
catalog: opts.modelCatalog,
|
|
1270
|
+
modelConfig: opts.modelConfig,
|
|
1271
|
+
log: (message) => serverLog.info(message),
|
|
1272
|
+
logPrefix: opts.logPrefix ?? `[horton]`
|
|
1273
|
+
})] : [fetchUrlTool],
|
|
1274
|
+
createSpawnWorkerTool(ctx, opts.modelConfig),
|
|
1772
1275
|
...opts.docsSearchTool ? [opts.docsSearchTool] : []
|
|
1773
1276
|
];
|
|
1774
1277
|
}
|
|
1775
|
-
function
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1278
|
+
function payloadToTitleText(payload) {
|
|
1279
|
+
if (typeof payload === `string`) return payload;
|
|
1280
|
+
if (payload == null) return ``;
|
|
1281
|
+
if (typeof payload === `object`) {
|
|
1282
|
+
const text = payload.text;
|
|
1283
|
+
return typeof text === `string` ? text : JSON.stringify(payload);
|
|
1284
|
+
}
|
|
1285
|
+
return String(payload);
|
|
1286
|
+
}
|
|
1287
|
+
async function extractFirstUserMessage(ctx) {
|
|
1288
|
+
const firstMessage = await queryOnce((q) => q.from({ inbox: ctx.db.collections.inbox }).where(({ inbox }) => not(eq(inbox.from, `system`))).orderBy(({ inbox }) => inbox._seq, `asc`).findOne());
|
|
1289
|
+
if (!firstMessage) return null;
|
|
1290
|
+
const text = payloadToTitleText(firstMessage.payload);
|
|
1291
|
+
return text.length > 0 ? text : null;
|
|
1292
|
+
}
|
|
1293
|
+
function readAgentsMd(workingDirectory) {
|
|
1294
|
+
const agentsMdPath = path.join(workingDirectory, `AGENTS.md`);
|
|
1295
|
+
try {
|
|
1296
|
+
if (!fs.existsSync(agentsMdPath) || !fs.statSync(agentsMdPath).isFile()) return null;
|
|
1297
|
+
const content = fs.readFileSync(agentsMdPath, `utf8`);
|
|
1298
|
+
return [
|
|
1299
|
+
`<context_file kind="instructions" path="${agentsMdPath}">`,
|
|
1300
|
+
content,
|
|
1301
|
+
`</context_file>`
|
|
1302
|
+
].join(`\n`);
|
|
1303
|
+
} catch {
|
|
1304
|
+
return null;
|
|
1783
1305
|
}
|
|
1784
|
-
return null;
|
|
1785
1306
|
}
|
|
1786
1307
|
function createAssistantHandler(options) {
|
|
1787
|
-
const { workingDirectory, streamFn, docsSupport, docsSearchTool, skillsRegistry, docsUrl } = options;
|
|
1308
|
+
const { workingDirectory, streamFn, docsSupport, docsSearchTool, skillsRegistry, modelCatalog, docsUrl } = options;
|
|
1788
1309
|
const hasSkills = Boolean(skillsRegistry && skillsRegistry.catalog.size > 0);
|
|
1789
1310
|
return async function assistantHandler(ctx, wake) {
|
|
1790
1311
|
const readSet = new Set();
|
|
1312
|
+
const effectiveCwd = typeof ctx.args.workingDirectory === `string` && ctx.args.workingDirectory.trim().length > 0 ? ctx.args.workingDirectory : workingDirectory;
|
|
1313
|
+
const modelConfig = resolveBuiltinModelConfig(modelCatalog, ctx.args);
|
|
1314
|
+
const agentsMd = readAgentsMd(effectiveCwd);
|
|
1791
1315
|
const tools = [
|
|
1792
1316
|
...ctx.electricTools,
|
|
1793
|
-
...createHortonTools(
|
|
1794
|
-
|
|
1317
|
+
...createHortonTools(effectiveCwd, ctx, readSet, {
|
|
1318
|
+
docsSearchTool,
|
|
1319
|
+
modelConfig,
|
|
1320
|
+
modelCatalog,
|
|
1321
|
+
logPrefix: `[horton ${ctx.entityUrl}]`
|
|
1322
|
+
}),
|
|
1323
|
+
...skillsRegistry && skillsRegistry.catalog.size > 0 ? createSkillTools(skillsRegistry, ctx) : [],
|
|
1324
|
+
...mcp.tools()
|
|
1795
1325
|
];
|
|
1326
|
+
const titlePromise = ctx.firstWake && !ctx.tags.title ? (async () => {
|
|
1327
|
+
const firstUserMessage = await extractFirstUserMessage(ctx);
|
|
1328
|
+
if (!firstUserMessage) return;
|
|
1329
|
+
let title = null;
|
|
1330
|
+
try {
|
|
1331
|
+
const result = await generateTitle(firstUserMessage, createConfiguredTitleCall(modelCatalog, modelConfig, `[horton ${ctx.entityUrl}]`), (reason) => {
|
|
1332
|
+
serverLog.warn(`[horton ${ctx.entityUrl}] title generation fell back to local title: ${reason}`);
|
|
1333
|
+
});
|
|
1334
|
+
if (result.length > 0) title = result;
|
|
1335
|
+
} catch (err) {
|
|
1336
|
+
serverLog.warn(`[horton ${ctx.entityUrl}] title generation failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1337
|
+
}
|
|
1338
|
+
if (title !== null) try {
|
|
1339
|
+
await ctx.setTag(`title`, title);
|
|
1340
|
+
} catch (err) {
|
|
1341
|
+
serverLog.warn(`[horton ${ctx.entityUrl}] setTag failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1342
|
+
}
|
|
1343
|
+
})() : Promise.resolve();
|
|
1796
1344
|
if (docsSupport) ctx.useContext({
|
|
1797
1345
|
sourceBudget: 1e5,
|
|
1798
1346
|
sources: {
|
|
@@ -1810,6 +1358,11 @@ function createAssistantHandler(options) {
|
|
|
1810
1358
|
content: () => ctx.timelineMessages(),
|
|
1811
1359
|
cache: `volatile`
|
|
1812
1360
|
},
|
|
1361
|
+
...agentsMd ? { agents_md: {
|
|
1362
|
+
content: () => agentsMd,
|
|
1363
|
+
max: 2e4,
|
|
1364
|
+
cache: `stable`
|
|
1365
|
+
} } : {},
|
|
1813
1366
|
...skillsRegistry && skillsRegistry.catalog.size > 0 ? { skills_catalog: {
|
|
1814
1367
|
content: () => skillsRegistry.renderCatalog(2e3),
|
|
1815
1368
|
max: 2e3,
|
|
@@ -1828,41 +1381,46 @@ function createAssistantHandler(options) {
|
|
|
1828
1381
|
conversation: {
|
|
1829
1382
|
content: () => ctx.timelineMessages(),
|
|
1830
1383
|
cache: `volatile`
|
|
1384
|
+
},
|
|
1385
|
+
...agentsMd ? { agents_md: {
|
|
1386
|
+
content: () => agentsMd,
|
|
1387
|
+
max: 2e4,
|
|
1388
|
+
cache: `stable`
|
|
1389
|
+
} } : {}
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
else if (agentsMd) ctx.useContext({
|
|
1393
|
+
sourceBudget: 1e5,
|
|
1394
|
+
sources: {
|
|
1395
|
+
conversation: {
|
|
1396
|
+
content: () => ctx.timelineMessages(),
|
|
1397
|
+
cache: `volatile`
|
|
1398
|
+
},
|
|
1399
|
+
agents_md: {
|
|
1400
|
+
content: () => agentsMd,
|
|
1401
|
+
max: 2e4,
|
|
1402
|
+
cache: `stable`
|
|
1831
1403
|
}
|
|
1832
1404
|
}
|
|
1833
1405
|
});
|
|
1834
1406
|
ctx.useAgent({
|
|
1835
|
-
systemPrompt: buildHortonSystemPrompt(
|
|
1407
|
+
systemPrompt: buildHortonSystemPrompt(effectiveCwd, {
|
|
1836
1408
|
hasDocsSupport: Boolean(docsSupport),
|
|
1837
1409
|
hasSkills,
|
|
1838
|
-
docsUrl
|
|
1410
|
+
docsUrl,
|
|
1411
|
+
modelProvider: modelConfig.provider,
|
|
1412
|
+
modelId: String(modelConfig.model)
|
|
1839
1413
|
}),
|
|
1840
|
-
|
|
1414
|
+
...modelConfig,
|
|
1841
1415
|
tools,
|
|
1842
1416
|
...streamFn && { streamFn }
|
|
1843
1417
|
});
|
|
1844
1418
|
await ctx.agent.run();
|
|
1845
|
-
|
|
1846
|
-
const firstUserMessage = extractFirstUserMessage(ctx.events);
|
|
1847
|
-
if (firstUserMessage) {
|
|
1848
|
-
let title = null;
|
|
1849
|
-
try {
|
|
1850
|
-
const result = await generateTitle(firstUserMessage);
|
|
1851
|
-
if (result.length > 0) title = result;
|
|
1852
|
-
} catch (err) {
|
|
1853
|
-
serverLog.warn(`[horton ${ctx.entityUrl}] title generation failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1854
|
-
}
|
|
1855
|
-
if (title !== null) try {
|
|
1856
|
-
await ctx.setTag(`title`, title);
|
|
1857
|
-
} catch (err) {
|
|
1858
|
-
serverLog.warn(`[horton ${ctx.entityUrl}] setTag failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1419
|
+
await titlePromise;
|
|
1862
1420
|
};
|
|
1863
1421
|
}
|
|
1864
1422
|
function registerHorton(registry, options) {
|
|
1865
|
-
const { workingDirectory, streamFn, skillsRegistry = null } = options;
|
|
1423
|
+
const { workingDirectory, streamFn, skillsRegistry = null, modelCatalog } = options;
|
|
1866
1424
|
const docsUrl = options.docsUrl ?? process.env.HORTON_DOCS_URL;
|
|
1867
1425
|
if (process.env.BRAVE_SEARCH_API_KEY) serverLog.info(`[horton] Web search: using Brave Search API`);
|
|
1868
1426
|
else serverLog.warn(`[horton] BRAVE_SEARCH_API_KEY not set — web search will fall back to Anthropic built-in search (uses your ANTHROPIC_API_KEY)`);
|
|
@@ -1877,10 +1435,17 @@ function registerHorton(registry, options) {
|
|
|
1877
1435
|
docsSupport,
|
|
1878
1436
|
docsSearchTool,
|
|
1879
1437
|
skillsRegistry,
|
|
1438
|
+
modelCatalog,
|
|
1880
1439
|
docsUrl
|
|
1881
1440
|
});
|
|
1441
|
+
const hortonCreationSchema = z.object({
|
|
1442
|
+
model: z.enum(modelChoiceValues(modelCatalog)).default(modelCatalog.defaultChoice.value),
|
|
1443
|
+
reasoningEffort: z.enum(REASONING_EFFORT_VALUES).default(`auto`).describe(`Reasoning effort for compatible reasoning models. Auto uses a safe provider default.`),
|
|
1444
|
+
workingDirectory: z.string().optional().describe(`Working directory for file operations. Defaults to the server's configured cwd.`)
|
|
1445
|
+
});
|
|
1882
1446
|
registry.define(`horton`, {
|
|
1883
1447
|
description: `Friendly capable assistant — chat, code, research, dispatch`,
|
|
1448
|
+
creationSchema: hortonCreationSchema,
|
|
1884
1449
|
handler: assistantHandler
|
|
1885
1450
|
});
|
|
1886
1451
|
const typeNames = [`horton`];
|
|
@@ -1925,6 +1490,9 @@ function parseWorkerArgs(value) {
|
|
|
1925
1490
|
};
|
|
1926
1491
|
}
|
|
1927
1492
|
if (tools.length === 0 && !args.sharedDb) throw new Error(`[worker] must provide tools and/or sharedDb`);
|
|
1493
|
+
if (typeof value.model === `string`) args.model = value.model;
|
|
1494
|
+
if (typeof value.provider === `string`) args.provider = value.provider;
|
|
1495
|
+
if (typeof value.reasoningEffort === `string` && REASONING_EFFORT_VALUES.includes(value.reasoningEffort)) args.reasoningEffort = value.reasoningEffort;
|
|
1928
1496
|
return args;
|
|
1929
1497
|
}
|
|
1930
1498
|
function buildToolsForWorker(tools, workingDirectory, ctx, readSet) {
|
|
@@ -1942,7 +1510,7 @@ function buildToolsForWorker(tools, workingDirectory, ctx, readSet) {
|
|
|
1942
1510
|
case `edit`:
|
|
1943
1511
|
out.push(createEditTool(workingDirectory, readSet));
|
|
1944
1512
|
break;
|
|
1945
|
-
case `
|
|
1513
|
+
case `web_search`:
|
|
1946
1514
|
out.push(braveSearchTool);
|
|
1947
1515
|
break;
|
|
1948
1516
|
case `fetch_url`:
|
|
@@ -2051,13 +1619,14 @@ function buildSharedStateTools(shared, schema, mode) {
|
|
|
2051
1619
|
return tools;
|
|
2052
1620
|
}
|
|
2053
1621
|
function registerWorker(registry, options) {
|
|
2054
|
-
const { workingDirectory, streamFn } = options;
|
|
1622
|
+
const { workingDirectory, streamFn, modelCatalog } = options;
|
|
2055
1623
|
registry.define(`worker`, {
|
|
2056
1624
|
description: `Internal — generic worker spawned by other agents. Configure via spawn args (systemPrompt + tools + optional sharedDb).`,
|
|
2057
1625
|
async handler(ctx) {
|
|
2058
1626
|
const args = parseWorkerArgs(ctx.args);
|
|
2059
1627
|
const readSet = new Set();
|
|
2060
1628
|
const builtinTools = buildToolsForWorker(args.tools, workingDirectory, ctx, readSet);
|
|
1629
|
+
const modelConfig = resolveBuiltinModelConfig(modelCatalog, args);
|
|
2061
1630
|
const sharedStateTools = [];
|
|
2062
1631
|
if (args.sharedDb) {
|
|
2063
1632
|
const shared = await ctx.observe(db(args.sharedDb.id, args.sharedDb.schema));
|
|
@@ -2065,7 +1634,7 @@ function registerWorker(registry, options) {
|
|
|
2065
1634
|
}
|
|
2066
1635
|
ctx.useAgent({
|
|
2067
1636
|
systemPrompt: `${args.systemPrompt}${WORKER_PROMPT_FOOTER}`,
|
|
2068
|
-
|
|
1637
|
+
...modelConfig,
|
|
2069
1638
|
tools: [...builtinTools, ...sharedStateTools],
|
|
2070
1639
|
...streamFn && { streamFn }
|
|
2071
1640
|
});
|
|
@@ -2155,7 +1724,6 @@ function stripQuotes(value) {
|
|
|
2155
1724
|
|
|
2156
1725
|
//#endregion
|
|
2157
1726
|
//#region src/skills/extract-meta.ts
|
|
2158
|
-
const EXTRACT_MODEL = `claude-haiku-4-5-20251001`;
|
|
2159
1727
|
const DEFAULT_MAX = 1e4;
|
|
2160
1728
|
async function extractSkillMeta(name, content) {
|
|
2161
1729
|
const preamble = parsePreamble(content);
|
|
@@ -2168,7 +1736,7 @@ async function extractSkillMeta(name, content) {
|
|
|
2168
1736
|
...preamble.userInvocable && { userInvocable: true },
|
|
2169
1737
|
max: preamble.max ?? DEFAULT_MAX
|
|
2170
1738
|
};
|
|
2171
|
-
|
|
1739
|
+
try {
|
|
2172
1740
|
return await llmExtract(name, content, preamble);
|
|
2173
1741
|
} catch (err) {
|
|
2174
1742
|
serverLog.warn(`[skills] LLM metadata extraction failed for "${name}": ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -2181,7 +1749,6 @@ async function extractSkillMeta(name, content) {
|
|
|
2181
1749
|
};
|
|
2182
1750
|
}
|
|
2183
1751
|
async function llmExtract(name, content, partial) {
|
|
2184
|
-
const client = new Anthropic();
|
|
2185
1752
|
const truncated = content.slice(0, 8e3);
|
|
2186
1753
|
const prompt = `Analyze this skill document and extract metadata. The skill is named "${name}".
|
|
2187
1754
|
|
|
@@ -2195,15 +1762,14 @@ Return ONLY a JSON object with these fields:
|
|
|
2195
1762
|
- "keywords": array of 3-8 relevant keywords
|
|
2196
1763
|
|
|
2197
1764
|
Return raw JSON, no markdown fences.`;
|
|
2198
|
-
const
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
1765
|
+
const text = await completeWithLowCostModel({
|
|
1766
|
+
purpose: `skill metadata extraction`,
|
|
1767
|
+
systemPrompt: `Extract metadata from skill documents. Return only valid JSON that matches the requested schema.`,
|
|
1768
|
+
prompt,
|
|
1769
|
+
maxTokens: 256,
|
|
1770
|
+
log: (message) => serverLog.info(message),
|
|
1771
|
+
logPrefix: `[skills]`
|
|
2205
1772
|
});
|
|
2206
|
-
const text = res.content[0]?.type === `text` ? res.content[0].text : ``;
|
|
2207
1773
|
const parsed = JSON.parse(text);
|
|
2208
1774
|
return {
|
|
2209
1775
|
description: partial.description ?? parsed.description ?? humanize(name),
|
|
@@ -2334,9 +1900,10 @@ function truncate(str, max) {
|
|
|
2334
1900
|
//#region src/bootstrap.ts
|
|
2335
1901
|
const DEFAULT_BUILTIN_AGENT_HANDLER_PATH = `/_electric/builtin-agent-handler`;
|
|
2336
1902
|
async function createBuiltinAgentHandler(options) {
|
|
2337
|
-
const { agentServerUrl, serveEndpoint = `${agentServerUrl}${DEFAULT_BUILTIN_AGENT_HANDLER_PATH}`, workingDirectory, streamFn, createElectricTools } = options;
|
|
2338
|
-
|
|
2339
|
-
|
|
1903
|
+
const { agentServerUrl, serveEndpoint = `${agentServerUrl}${DEFAULT_BUILTIN_AGENT_HANDLER_PATH}`, workingDirectory, streamFn, createElectricTools, publicUrl, runtimeName } = options;
|
|
1904
|
+
const modelCatalog = await createBuiltinModelCatalog({ allowMockFallback: Boolean(streamFn) });
|
|
1905
|
+
if (!modelCatalog) {
|
|
1906
|
+
serverLog.warn(`[builtin-agents] no supported model provider API key found — set ANTHROPIC_API_KEY or OPENAI_API_KEY`);
|
|
2340
1907
|
return null;
|
|
2341
1908
|
}
|
|
2342
1909
|
const cwd = workingDirectory ?? process.cwd();
|
|
@@ -2357,22 +1924,24 @@ async function createBuiltinAgentHandler(options) {
|
|
|
2357
1924
|
const typeNames = registerHorton(registry, {
|
|
2358
1925
|
workingDirectory: cwd,
|
|
2359
1926
|
streamFn,
|
|
2360
|
-
skillsRegistry
|
|
1927
|
+
skillsRegistry,
|
|
1928
|
+
modelCatalog
|
|
2361
1929
|
});
|
|
2362
1930
|
registerWorker(registry, {
|
|
2363
1931
|
workingDirectory: cwd,
|
|
2364
|
-
streamFn
|
|
1932
|
+
streamFn,
|
|
1933
|
+
modelCatalog
|
|
2365
1934
|
});
|
|
2366
1935
|
typeNames.push(`worker`);
|
|
2367
|
-
registerCodingSession(registry, { defaultWorkingDirectory: cwd });
|
|
2368
|
-
typeNames.push(`coder`);
|
|
2369
1936
|
const runtime = createRuntimeHandler({
|
|
2370
1937
|
baseUrl: agentServerUrl,
|
|
2371
1938
|
serveEndpoint,
|
|
2372
1939
|
registry,
|
|
2373
1940
|
subscriptionPathForType: (name) => `/${name}/*/main`,
|
|
2374
1941
|
idleTimeout: 5e3,
|
|
2375
|
-
createElectricTools
|
|
1942
|
+
createElectricTools,
|
|
1943
|
+
publicUrl,
|
|
1944
|
+
name: runtimeName ?? `builtin-agents`
|
|
2376
1945
|
});
|
|
2377
1946
|
return {
|
|
2378
1947
|
handler: runtime.onEnter,
|
|
@@ -2394,10 +1963,19 @@ var BuiltinAgentsServer = class {
|
|
|
2394
1963
|
bootstrap = null;
|
|
2395
1964
|
_url = null;
|
|
2396
1965
|
publicBaseUrl = null;
|
|
1966
|
+
_mcpRegistry = null;
|
|
1967
|
+
mcpWatcherCloser = null;
|
|
1968
|
+
mcpToolProviderName = null;
|
|
1969
|
+
mcpApplyInFlight = new Set();
|
|
1970
|
+
mcpStopping = false;
|
|
2397
1971
|
options;
|
|
2398
1972
|
constructor(options) {
|
|
2399
1973
|
this.options = options;
|
|
2400
1974
|
}
|
|
1975
|
+
/** Embedded MCP registry. `null` until `start()` has run. */
|
|
1976
|
+
get mcpRegistry() {
|
|
1977
|
+
return this._mcpRegistry;
|
|
1978
|
+
}
|
|
2401
1979
|
get url() {
|
|
2402
1980
|
if (!this._url) throw new Error(`Builtin agents server not started`);
|
|
2403
1981
|
return this._url;
|
|
@@ -2431,14 +2009,124 @@ var BuiltinAgentsServer = class {
|
|
|
2431
2009
|
this.publicBaseUrl = this.options.baseUrl ?? this._url;
|
|
2432
2010
|
const webhookPath = this.options.webhookPath ?? DEFAULT_BUILTIN_AGENT_HANDLER_PATH;
|
|
2433
2011
|
const serveEndpoint = new URL(webhookPath, this.publicBaseUrl.endsWith(`/`) ? this.publicBaseUrl : `${this.publicBaseUrl}/`).toString();
|
|
2012
|
+
const publicUrl = this.options.mcpOAuthRedirectBase ?? this.publicBaseUrl;
|
|
2013
|
+
const mcpRegistry = createRegistry({
|
|
2014
|
+
publicUrl,
|
|
2015
|
+
openAuthorizeUrl: this.options.openAuthorizeUrl
|
|
2016
|
+
});
|
|
2017
|
+
this._mcpRegistry = mcpRegistry;
|
|
2018
|
+
const mcpConfigPath = this.options.loadProjectMcpConfig ? path.resolve(this.options.workingDirectory ?? process.cwd(), `mcp.json`) : null;
|
|
2019
|
+
const extras = this.options.extraMcpServers ?? [];
|
|
2020
|
+
const wirePersistence = async (cfg) => {
|
|
2021
|
+
const servers = [];
|
|
2022
|
+
for (const s of cfg.servers) if (s.transport === `http` && s.auth?.mode === `authorizationCode`) {
|
|
2023
|
+
const persist = await keychainPersistence({ server: s.name });
|
|
2024
|
+
servers.push({
|
|
2025
|
+
...s,
|
|
2026
|
+
auth: {
|
|
2027
|
+
...s.auth,
|
|
2028
|
+
...persist
|
|
2029
|
+
}
|
|
2030
|
+
});
|
|
2031
|
+
} else servers.push(s);
|
|
2032
|
+
return {
|
|
2033
|
+
...cfg,
|
|
2034
|
+
servers
|
|
2035
|
+
};
|
|
2036
|
+
};
|
|
2037
|
+
const merge = (jsonCfg) => {
|
|
2038
|
+
const jsonServers = jsonCfg?.servers ?? [];
|
|
2039
|
+
const jsonNames = new Set(jsonServers.map((s) => s.name));
|
|
2040
|
+
const filteredExtras = extras.filter((s) => !jsonNames.has(s.name));
|
|
2041
|
+
return {
|
|
2042
|
+
servers: [...filteredExtras, ...jsonServers],
|
|
2043
|
+
raw: jsonCfg?.raw
|
|
2044
|
+
};
|
|
2045
|
+
};
|
|
2046
|
+
const onConfigError = this.options.onConfigError;
|
|
2047
|
+
const runApply = async (jsonCfg) => {
|
|
2048
|
+
if (this.mcpStopping) return;
|
|
2049
|
+
try {
|
|
2050
|
+
const wired = await wirePersistence(merge(jsonCfg));
|
|
2051
|
+
if (this.mcpStopping) return;
|
|
2052
|
+
await mcpRegistry.applyConfig(wired);
|
|
2053
|
+
} catch (e) {
|
|
2054
|
+
serverLog.error(`[mcp] applyConfig:`, e);
|
|
2055
|
+
try {
|
|
2056
|
+
onConfigError?.(e);
|
|
2057
|
+
} catch (cbErr) {
|
|
2058
|
+
serverLog.error(`[mcp] onConfigError callback failed:`, cbErr);
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
};
|
|
2062
|
+
const applyMerged = (jsonCfg) => {
|
|
2063
|
+
const p = runApply(jsonCfg);
|
|
2064
|
+
this.mcpApplyInFlight.add(p);
|
|
2065
|
+
p.finally(() => this.mcpApplyInFlight.delete(p));
|
|
2066
|
+
return p;
|
|
2067
|
+
};
|
|
2068
|
+
if (mcpConfigPath) {
|
|
2069
|
+
try {
|
|
2070
|
+
const cfg = await loadConfig(mcpConfigPath, process.env);
|
|
2071
|
+
applyMerged(cfg);
|
|
2072
|
+
} catch (err) {
|
|
2073
|
+
if (err.code !== `ENOENT`) throw err;
|
|
2074
|
+
if (extras.length === 0) serverLog.info(`[mcp] no ${mcpConfigPath} — starting with no servers`);
|
|
2075
|
+
else serverLog.info(`[mcp] no ${mcpConfigPath} — starting with ${extras.length} server(s) from extras`);
|
|
2076
|
+
applyMerged(null);
|
|
2077
|
+
}
|
|
2078
|
+
try {
|
|
2079
|
+
this.mcpWatcherCloser = await watchConfig(mcpConfigPath, {
|
|
2080
|
+
onChange: (cfg) => void applyMerged(cfg),
|
|
2081
|
+
onError: (e) => serverLog.error(`[mcp] config error:`, e)
|
|
2082
|
+
});
|
|
2083
|
+
} catch (e) {
|
|
2084
|
+
serverLog.error(`[mcp] config watcher failed to start:`, e);
|
|
2085
|
+
}
|
|
2086
|
+
} else {
|
|
2087
|
+
if (extras.length > 0) serverLog.info(`[mcp] starting with ${extras.length} server(s) from extras`);
|
|
2088
|
+
applyMerged(null);
|
|
2089
|
+
}
|
|
2090
|
+
this.mcpToolProviderName = `mcp`;
|
|
2091
|
+
registerToolProvider({
|
|
2092
|
+
name: `mcp`,
|
|
2093
|
+
tools: () => {
|
|
2094
|
+
const tools = [];
|
|
2095
|
+
for (const entry of mcpRegistry.list()) {
|
|
2096
|
+
if (entry.status !== `ready`) continue;
|
|
2097
|
+
const live = mcpRegistry.get(entry.name);
|
|
2098
|
+
if (!live?.transport) continue;
|
|
2099
|
+
for (const t of entry.tools) tools.push(bridgeMcpTool({
|
|
2100
|
+
server: entry.name,
|
|
2101
|
+
tool: t,
|
|
2102
|
+
client: live.transport.client,
|
|
2103
|
+
timeoutMs: live.config.timeoutMs
|
|
2104
|
+
}));
|
|
2105
|
+
const caps = live.transport.client.getServerCapabilities?.();
|
|
2106
|
+
if (caps?.resources) tools.push(...buildResourceTools({
|
|
2107
|
+
server: entry.name,
|
|
2108
|
+
client: live.transport.client,
|
|
2109
|
+
timeoutMs: live.config.timeoutMs
|
|
2110
|
+
}));
|
|
2111
|
+
if (caps?.prompts) tools.push(...buildPromptTools({
|
|
2112
|
+
server: entry.name,
|
|
2113
|
+
client: live.transport.client,
|
|
2114
|
+
timeoutMs: live.config.timeoutMs
|
|
2115
|
+
}));
|
|
2116
|
+
}
|
|
2117
|
+
return tools;
|
|
2118
|
+
}
|
|
2119
|
+
});
|
|
2434
2120
|
this.bootstrap = await createBuiltinAgentHandler({
|
|
2435
2121
|
agentServerUrl: this.options.agentServerUrl,
|
|
2436
2122
|
serveEndpoint,
|
|
2437
2123
|
workingDirectory: this.options.workingDirectory,
|
|
2438
2124
|
streamFn: this.options.mockStreamFn,
|
|
2439
|
-
createElectricTools: this.options.createElectricTools
|
|
2125
|
+
createElectricTools: this.options.createElectricTools,
|
|
2126
|
+
publicUrl,
|
|
2127
|
+
runtimeName: `builtin-agents`
|
|
2440
2128
|
});
|
|
2441
|
-
if (!this.bootstrap) throw new Error(`ANTHROPIC_API_KEY must be set before starting builtin agents`);
|
|
2129
|
+
if (!this.bootstrap) throw new Error(`ANTHROPIC_API_KEY or OPENAI_API_KEY must be set before starting builtin agents`);
|
|
2442
2130
|
await registerBuiltinAgentTypes(this.bootstrap);
|
|
2443
2131
|
serverLog.info(`[builtin-agents] webhook handler listening at ${serveEndpoint}`);
|
|
2444
2132
|
resolve(this._url);
|
|
@@ -2455,6 +2143,26 @@ var BuiltinAgentsServer = class {
|
|
|
2455
2143
|
await Promise.race([this.bootstrap.runtime.drainWakes().catch(() => {}), new Promise((resolve) => setTimeout(resolve, 5e3))]);
|
|
2456
2144
|
this.bootstrap = null;
|
|
2457
2145
|
}
|
|
2146
|
+
this.mcpStopping = true;
|
|
2147
|
+
if (this.mcpWatcherCloser) {
|
|
2148
|
+
try {
|
|
2149
|
+
this.mcpWatcherCloser();
|
|
2150
|
+
} catch (e) {
|
|
2151
|
+
serverLog.error(`[mcp] watcher close failed:`, e);
|
|
2152
|
+
}
|
|
2153
|
+
this.mcpWatcherCloser = null;
|
|
2154
|
+
}
|
|
2155
|
+
if (this.mcpApplyInFlight.size > 0) await Promise.allSettled([...this.mcpApplyInFlight]);
|
|
2156
|
+
if (this.mcpToolProviderName) {
|
|
2157
|
+
unregisterToolProvider(this.mcpToolProviderName);
|
|
2158
|
+
this.mcpToolProviderName = null;
|
|
2159
|
+
}
|
|
2160
|
+
if (this._mcpRegistry) {
|
|
2161
|
+
await this._mcpRegistry.close().catch((e) => {
|
|
2162
|
+
serverLog.error(`[mcp] registry close failed:`, e);
|
|
2163
|
+
});
|
|
2164
|
+
this._mcpRegistry = null;
|
|
2165
|
+
}
|
|
2458
2166
|
if (this.server) {
|
|
2459
2167
|
const server = this.server;
|
|
2460
2168
|
await new Promise((resolve) => {
|
|
@@ -2462,19 +2170,20 @@ var BuiltinAgentsServer = class {
|
|
|
2462
2170
|
});
|
|
2463
2171
|
this.server = null;
|
|
2464
2172
|
}
|
|
2173
|
+
this.mcpStopping = false;
|
|
2465
2174
|
this._url = null;
|
|
2466
2175
|
this.publicBaseUrl = null;
|
|
2467
2176
|
}
|
|
2468
2177
|
async handleRequest(req, res) {
|
|
2469
2178
|
const method = req.method?.toUpperCase();
|
|
2470
|
-
const
|
|
2179
|
+
const pathname = new URL(req.url ?? `/`, `http://localhost`).pathname;
|
|
2471
2180
|
const webhookPath = this.options.webhookPath ?? DEFAULT_BUILTIN_AGENT_HANDLER_PATH;
|
|
2472
|
-
if (
|
|
2181
|
+
if (pathname === `/_electric/health` && method === `GET`) {
|
|
2473
2182
|
res.writeHead(200, { "content-type": `application/json` });
|
|
2474
2183
|
res.end(JSON.stringify({ status: `ok` }));
|
|
2475
2184
|
return;
|
|
2476
2185
|
}
|
|
2477
|
-
if (
|
|
2186
|
+
if (pathname === webhookPath && method === `POST` && this.bootstrap) {
|
|
2478
2187
|
await this.bootstrap.handler(req, res);
|
|
2479
2188
|
return;
|
|
2480
2189
|
}
|