sumo-agents 0.0.0-stage → 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/AGENTS.md +38 -0
- package/LICENSE +21 -0
- package/README.md +111 -2
- package/bin/sumo.mjs +33 -0
- package/guides/delegation.md +33 -0
- package/guides/feature.md +14 -0
- package/guides/fix.md +14 -0
- package/guides/memory.md +23 -0
- package/guides/projects.md +27 -0
- package/guides/review.md +16 -0
- package/guides/workflows.md +26 -0
- package/logo.txt +19 -0
- package/package.json +42 -4
- package/prompts/agent.md +7 -0
- package/prompts/dream.md +13 -0
- package/prompts/scribe.md +21 -0
- package/spinner.txt +90 -0
- package/src/advisor.mjs +59 -0
- package/src/apply.mjs +368 -0
- package/src/auth.mjs +80 -0
- package/src/card.mjs +72 -0
- package/src/catalog.mjs +167 -0
- package/src/chat.mjs +1162 -0
- package/src/cli.mjs +815 -0
- package/src/cost.mjs +128 -0
- package/src/db.mjs +384 -0
- package/src/dream.mjs +97 -0
- package/src/editor.mjs +198 -0
- package/src/export.mjs +132 -0
- package/src/guard.mjs +121 -0
- package/src/guides.mjs +86 -0
- package/src/herdr.mjs +117 -0
- package/src/hooks.mjs +270 -0
- package/src/images.mjs +89 -0
- package/src/jobs.mjs +1184 -0
- package/src/jobtools.mjs +82 -0
- package/src/local-server.mjs +169 -0
- package/src/lock.mjs +45 -0
- package/src/loop.mjs +838 -0
- package/src/mcp.mjs +604 -0
- package/src/memory.mjs +400 -0
- package/src/model.mjs +270 -0
- package/src/page.html +304 -0
- package/src/page.mjs +135 -0
- package/src/paths.mjs +33 -0
- package/src/prime.mjs +177 -0
- package/src/projects.mjs +232 -0
- package/src/recall.mjs +45 -0
- package/src/redact.mjs +173 -0
- package/src/render.mjs +45 -0
- package/src/route.mjs +287 -0
- package/src/sandbox.mjs +173 -0
- package/src/scan.mjs +204 -0
- package/src/scribe.mjs +349 -0
- package/src/sessions.mjs +371 -0
- package/src/setup.mjs +528 -0
- package/src/sqlite.mjs +66 -0
- package/src/text.mjs +97 -0
- package/src/tools.mjs +987 -0
- package/src/transcript.mjs +106 -0
- package/src/tty.mjs +529 -0
- package/src/ui.mjs +1363 -0
- package/src/verify.mjs +369 -0
- package/src/web.mjs +49 -0
- package/src/workflows.mjs +270 -0
- package/src/worktree.mjs +128 -0
package/src/chat.mjs
ADDED
|
@@ -0,0 +1,1162 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
4
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { createInterface } from 'node:readline/promises';
|
|
6
|
+
import { isAdvisorTool } from './advisor.mjs';
|
|
7
|
+
import { ago } from './card.mjs';
|
|
8
|
+
import { getMeta } from './db.mjs';
|
|
9
|
+
import { runDream } from './dream.mjs';
|
|
10
|
+
import { openWatchTab, runHerdr } from './herdr.mjs';
|
|
11
|
+
import { appendLive, attemptsSettled, endLive, getJob, newJob, parseJobId, reportOf, runFollowed, startLive, tell as tellJob, treeOf } from './jobs.mjs';
|
|
12
|
+
import { handleEvent } from './hooks.mjs';
|
|
13
|
+
import { contextFor, converse, networkOn, paramsFor, runJob, runLines, sandboxOn, sendToApi, stopReason, TAMPER_REFUSED, tampersMarker, textOf, thinkingFor, withMcp, withServers } from './loop.mjs';
|
|
14
|
+
import { closeMcp, compactInput, gateMcp, isMcpTool, mcpLines, mcpReady, splitMcpName } from './mcp.mjs';
|
|
15
|
+
import { assertOn, MODEL_IDS, MODELS, modelId, usableModels } from './catalog.mjs';
|
|
16
|
+
import { takeLock } from './lock.mjs';
|
|
17
|
+
import { paths, REPO_ROOT } from './paths.mjs';
|
|
18
|
+
import { UsageError } from './memory.mjs';
|
|
19
|
+
import { openInBrowser, servePage } from './page.mjs';
|
|
20
|
+
import { getProject, scratchDir } from './projects.mjs';
|
|
21
|
+
import { redact } from './redact.mjs';
|
|
22
|
+
import { chooseChatRoute, EFFORTS } from './route.mjs';
|
|
23
|
+
import { verifyCommands } from './scan.mjs';
|
|
24
|
+
import { outcomeLines, runScribe } from './scribe.mjs';
|
|
25
|
+
import { currentProject, describeSaved, findSaved, loadState, lockFile, recordTurn, saveState, savedSessions, sessionLine, shortId, withoutThinking } from './sessions.mjs';
|
|
26
|
+
import { ASK_TOOL, BASH_TOOL, cap, capRedacted, DELEGATE_TOOL, EDITOR_TOOL, INTERRUPTED, nextUndoTurn, runCommand, undoLast } from './tools.mjs';
|
|
27
|
+
import { CONFIG_DEFAULTS } from './setup.mjs';
|
|
28
|
+
import { estimateTokens, head } from './text.mjs';
|
|
29
|
+
import { colourEnabled, createRenderer, header, pathFrom, prompt, renderBlock, safeForTerminal, styles, tilde, widthOf } from './tty.mjs';
|
|
30
|
+
import { hasCommit } from './verify.mjs';
|
|
31
|
+
import { sourcesOf, WEB_FETCH_TOOL, WEB_SEARCH_TOOL, WEB_TOOLS } from './web.mjs';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The conversation the user has with Sumo: the same loop a job runs in, with
|
|
35
|
+
* the session policy raised in process — the memory block first, a project's
|
|
36
|
+
* card when it comes up, a taught workflow before the command it gates, memory
|
|
37
|
+
* before a question goes to the user, the size of the context said once per
|
|
38
|
+
* band — and the scribe woken when a turn ends. Work is handed to sub-agents
|
|
39
|
+
* with the `delegate` tool: each job runs here, in this process, on its own
|
|
40
|
+
* route and in a context of its own, so the API key never enters a shell; its
|
|
41
|
+
* work is shown as it happens, the user can talk to it while it runs, and its
|
|
42
|
+
* report is the tool's result. Several delegated in one reply run at once.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/** Models that take operator instructions as `role: "system"` messages mid-conversation; the others get them as text in the user turn. */
|
|
46
|
+
const SYSTEM_MESSAGES = new Set([MODEL_IDS.opus, MODEL_IDS.sonnet, MODEL_IDS.fable]);
|
|
47
|
+
|
|
48
|
+
// The guide comes with the command, by name, through the prompt hook — the same way it comes when the user's words ask for
|
|
49
|
+
// it: from inside a project the tools cannot reach guides/ by a relative path, and the editor is jailed away from it.
|
|
50
|
+
const COMMANDS = {
|
|
51
|
+
fix: (args) => ({ text: `Fix this: ${args}`, guide: 'fix' }),
|
|
52
|
+
feature: (args) => ({ text: `Build this: ${args}`, guide: 'feature' }),
|
|
53
|
+
review: (args) => ({ text: `Review this: ${args}`, guide: 'review' }),
|
|
54
|
+
dream: () => ({ text: 'Run `sumo dream run` and tell me, in a few lines, what it changed and what it wants me to confirm.' }),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* What the command menu shows, in the order it shows it; the models `/model` offers are the ones that are on as it is typed.
|
|
59
|
+
* The effort a model would keep comes first (`effortNow`), so Enter on the open menu keeps it rather than dropping to the lowest.
|
|
60
|
+
*/
|
|
61
|
+
export function commandList(db, { effortNow = () => null, saved = () => [], running = () => [] } = {}) {
|
|
62
|
+
const efforts = () => {
|
|
63
|
+
const now = effortNow();
|
|
64
|
+
return EFFORTS.includes(now) ? [now, ...EFFORTS.filter((e) => e !== now)] : EFFORTS;
|
|
65
|
+
};
|
|
66
|
+
const modelChoices = (given) => {
|
|
67
|
+
const usable = usableModels(db);
|
|
68
|
+
return given.length === 0 ? ['auto', ...usable] : given.length === 1 && usable.includes(given[0]) && given[0] !== 'haiku' ? efforts() : [];
|
|
69
|
+
};
|
|
70
|
+
return [
|
|
71
|
+
{ name: 'fix', hint: 'fix a bug, by guides/fix.md' },
|
|
72
|
+
{ name: 'feature', hint: 'build something, by guides/feature.md' },
|
|
73
|
+
{ name: 'review', hint: 'have a reviewer job judge something' },
|
|
74
|
+
{ name: 'dream', hint: 'tidy the memory and say what changed' },
|
|
75
|
+
{ name: 'model', hint: 'the route, or set it: auto, or a model and an effort', choices: modelChoices },
|
|
76
|
+
{ name: 'memory', hint: 'see the memory in the browser: say yes or no, edit, forget' },
|
|
77
|
+
{ name: 'mcp', hint: 'the MCP servers: connected or not, their tools, what may run unasked' },
|
|
78
|
+
{ name: 'resume', hint: 'pick a saved session up where it stopped', choices: (given) => (given.length === 0 ? saved().map((s) => ({ name: shortId(s.id), hint: s.hint })) : []) },
|
|
79
|
+
{ name: 'copy', hint: 'the last reply to the clipboard, as it was written; `code` for its last code block', choices: (given) => (given.length === 0 ? ['code'] : []) },
|
|
80
|
+
{ name: 'undo', hint: "put back the files the editor changed in the last turn that changed any; again for the turn before — bash's changes are not tracked" },
|
|
81
|
+
{ name: 'stop', hint: 'stop a job at work in the background', choices: (given) => (given.length === 0 ? running().map((id) => `j${id}`) : []) },
|
|
82
|
+
{ name: 'help', hint: 'the commands and the keys' },
|
|
83
|
+
{ name: 'new', hint: 'start a fresh session' },
|
|
84
|
+
{ name: 'exit', hint: 'leave' },
|
|
85
|
+
];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A job is run with `delegate`, never from the shell: there it would be an agent in a shell of its own, holding no credential. */
|
|
89
|
+
const JOB_RUN = /(?:^|[\n;&|(`]|\$\()\s*(?:\w+=\S*\s+|nohup\s+|exec\s+)*sumo\s+job\s+(run|new)\b/;
|
|
90
|
+
/** A cheap-model pass the model asks for: run here too, since the shell it would start in holds no credential. */
|
|
91
|
+
const PASS_RUN = /^\s*sumo\s+(scribe|dream)\s+run\s*$/;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Operator text for the model, placed so the cached prefix is untouched: a
|
|
95
|
+
* system message where the model takes one, a text block otherwise. The API
|
|
96
|
+
* accepts a mid-conversation system message only right after a user message,
|
|
97
|
+
* so one that would follow the assistant's own turn goes as text too.
|
|
98
|
+
*/
|
|
99
|
+
export function injection(text, model, messages = []) {
|
|
100
|
+
const afterUser = messages.at(-1)?.role === 'user';
|
|
101
|
+
return SYSTEM_MESSAGES.has(model) && afterUser ? { role: 'system', content: text } : asText(text);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const asText = (text) => ({ role: 'user', content: [{ type: 'text', text: `<sumo>\n${text}\n</sumo>` }] });
|
|
105
|
+
/** Something that happened — a background job's report — as a block of a user turn: labelled as an event, never wrapped as operator text. */
|
|
106
|
+
const eventBlock = (text) => ({ type: 'text', text: `<event>\n${text}\n</event>` });
|
|
107
|
+
|
|
108
|
+
/** Automated turns in a row — a report read, which delegates, which reports — before the reports wait for the user's next message instead. */
|
|
109
|
+
const MAX_WOKEN = 3;
|
|
110
|
+
/** Jobs at work here at once: each report can start a turn, and each turn can delegate again. */
|
|
111
|
+
const MAX_JOBS = 4;
|
|
112
|
+
/** About how much of a job's report the chat's model is given; the rest is a command away, and the screen shows it whole. */
|
|
113
|
+
const REPORT_TOKENS = 1500;
|
|
114
|
+
/** The sections code adds at the end of a report: the checks' verdict, and where a worktree's change went. */
|
|
115
|
+
const ADDED_BY_CODE = /\n## (?:Verified by code|Accepted without verification|Worktree|Attempts)\b/;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A job's report as the chat's model is given it: whole when it fits in REPORT_TOKENS; otherwise the head of what its
|
|
119
|
+
* author wrote, what code added after it whole — the verdict, whether the change was brought back — and a last line
|
|
120
|
+
* that says where the whole report is.
|
|
121
|
+
*/
|
|
122
|
+
function reportForModel(report, id) {
|
|
123
|
+
if (estimateTokens(report) <= REPORT_TOKENS) return report;
|
|
124
|
+
const at = report.search(ADDED_BY_CODE);
|
|
125
|
+
const added = at === -1 ? '' : report.slice(at);
|
|
126
|
+
const last = `The report is cut to fit; the whole of it: sumo job show ${id}`;
|
|
127
|
+
const room = Math.max(0, REPORT_TOKENS * 4 - added.length - last.length - 40);
|
|
128
|
+
return `${head(at === -1 ? report : report.slice(0, at), room).trimEnd()}\n[… cut]${added}\n${last}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** One line per tool call, so the user can watch the work: the command, or the edit and its file. */
|
|
132
|
+
export function describeCall(call) {
|
|
133
|
+
if (call.name === BASH_TOOL.name) return `$ ${String(call.input?.command ?? '').split('\n')[0].slice(0, 120)}`;
|
|
134
|
+
if (call.name === DELEGATE_TOOL.name) return call.input?.job !== undefined ? `delegate j${call.input.job}` : `delegate ${call.input?.agent ?? 'worker'} — ${call.input?.title ?? ''}`;
|
|
135
|
+
if (call.name === EDITOR_TOOL.name) return `${call.input?.command ?? 'edit'} ${tilde(call.input?.path ?? '')}${Array.isArray(call.input?.view_range) ? `:${call.input.view_range.join('-')}` : ''}`;
|
|
136
|
+
if (isMcpTool(call.name)) {
|
|
137
|
+
const { server, tool } = splitMcpName(call.name);
|
|
138
|
+
return `${server} · ${tool}(${compactInput(call.input)})`;
|
|
139
|
+
}
|
|
140
|
+
if (call.name === WEB_SEARCH_TOOL.name) return `search: ${String(call.input?.query ?? '').slice(0, 120)}`;
|
|
141
|
+
if (call.name === WEB_FETCH_TOOL.name) return `fetch: ${String(call.input?.url ?? '').slice(0, 120)}`;
|
|
142
|
+
if (isAdvisorTool(call)) return `advisor: ${call.input?.asked ?? ''}`;
|
|
143
|
+
return call.name;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The branch a directory's repository is on, read from its HEAD — no process is started for it. A worktree's `.git` is a
|
|
148
|
+
* file that names where its HEAD is. A detached head is the start of its commit; a directory in no repository has none.
|
|
149
|
+
*/
|
|
150
|
+
export function branchOf(dir) {
|
|
151
|
+
for (let at = resolve(dir); ; at = dirname(at)) {
|
|
152
|
+
const git = join(at, '.git');
|
|
153
|
+
try {
|
|
154
|
+
const home = statSync(git).isDirectory() ? git : resolve(at, /^gitdir: (.+)$/m.exec(readFileSync(git, 'utf8'))[1]);
|
|
155
|
+
const head = readFileSync(join(home, 'HEAD'), 'utf8').trim();
|
|
156
|
+
return /^ref: refs\/heads\/(.+)$/.exec(head)?.[1] ?? head.slice(0, 8);
|
|
157
|
+
} catch {
|
|
158
|
+
if (dirname(at) === at) return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The session's route for the screen's margins: `opus · high`, `haiku`, or on auto what the router last chose. */
|
|
164
|
+
export function routeLine(session) {
|
|
165
|
+
const dot = ({ model, effort }) => (effort && effort !== 'none' ? `${model} · ${effort}` : model);
|
|
166
|
+
if (session.model !== 'auto') return dot(session);
|
|
167
|
+
return session.routed ? `auto → ${dot(session.routed)}` : 'auto';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A route as the screen says it: the model, and the effort when it has one. */
|
|
171
|
+
export const routeOf = ({ model, effort }) => (effort && effort !== 'none' ? `${model}/${effort}` : model);
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* What `/model` was given: a model that is on, a model and an effort, or `auto`. The model alone keeps
|
|
175
|
+
* the effort in hand; haiku takes none. Anything else is said back, and nothing changes.
|
|
176
|
+
*/
|
|
177
|
+
export function parseRoute(args, current, db) {
|
|
178
|
+
const [model, effort, ...rest] = args.split(/\s+/).filter(Boolean);
|
|
179
|
+
if (model === 'auto') {
|
|
180
|
+
if (effort) throw new UsageError('auto takes no effort — the router picks it each turn');
|
|
181
|
+
return { model: 'auto', effort: null };
|
|
182
|
+
}
|
|
183
|
+
if (!MODELS.includes(model)) throw new UsageError(`no such model "${model}" — one of: auto, ${usableModels(db).join(', ')}`);
|
|
184
|
+
assertOn(db, model);
|
|
185
|
+
if (rest.length > 0) throw new UsageError(`/model takes a model and an effort, not "${args}"`);
|
|
186
|
+
if (model === 'haiku') {
|
|
187
|
+
if (effort) throw new UsageError('haiku takes no effort setting');
|
|
188
|
+
return { model, effort: 'none' };
|
|
189
|
+
}
|
|
190
|
+
if (effort !== undefined && !EFFORTS.includes(effort)) throw new UsageError(`no such effort "${effort}" — one of: ${EFFORTS.join(', ')}`);
|
|
191
|
+
const kept = current.effort && current.effort !== 'none' ? current.effort : CONFIG_DEFAULTS['chat.effort'];
|
|
192
|
+
return { model, effort: effort ?? kept };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* What a job looks like while it works — on the terminal of `sumo job run`, and in the record `sumo job watch` shows:
|
|
197
|
+
* the job's line, each call with the first line of what came back under it, and what the job says drawn as the chat
|
|
198
|
+
* draws a reply, set in under its line. `clean` is done to the words before they are drawn: a wrapped line must not
|
|
199
|
+
* break a secret apart where the redaction after it would no longer see it whole.
|
|
200
|
+
*/
|
|
201
|
+
export function jobPrinter(write, s, { width = 98, clean = (text) => text } = {}) {
|
|
202
|
+
return {
|
|
203
|
+
onStart: (job) => write(`⏺ ${s.bold(`j${job.id}`)} ${job.agent} · ${routeOf(job)} — ${job.title}\n`),
|
|
204
|
+
onTool: (call) => write(` ⏺ ${describeCall(call)}\n`),
|
|
205
|
+
onRetry: (r) => write(` ${s.dim(retryLine(r))}\n`),
|
|
206
|
+
onCompact: (c) => write(` ${s.dim(compactLine(c))}\n`),
|
|
207
|
+
onResult: (call, result) => {
|
|
208
|
+
const first = String(result.content ?? '').split('\n').find((l) => l.trim()) ?? '';
|
|
209
|
+
write(` ⎿ ${s.dim(clean(first).slice(0, 160))}\n`);
|
|
210
|
+
},
|
|
211
|
+
onTurn: (response) => {
|
|
212
|
+
const text = textOf(response.content);
|
|
213
|
+
if (!text) return;
|
|
214
|
+
const drawn = [];
|
|
215
|
+
const reply = createRenderer((t) => drawn.push(t), s, { width });
|
|
216
|
+
reply.write(clean(text));
|
|
217
|
+
reply.flush();
|
|
218
|
+
write(`${drawn.join('').split('\n').map((l) => (l ? ` ${l}` : l)).join('\n')}\n`);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** A request being sent again, in one line: which try, how long until it, and why. */
|
|
224
|
+
export const retryLine = ({ attempt, limit, delayMs, reason }) => `retrying in ${Math.max(1, Math.round(delayMs / 1000))}s (${attempt} of ${limit}) — ${reason}`;
|
|
225
|
+
/** A conversation summarized to make room, in one line: how big it had grown. */
|
|
226
|
+
export const compactLine = ({ before }) => `the conversation was summarized at ${Math.round(before / 1000)}k tokens to make room, and goes on from the summary`;
|
|
227
|
+
/** A session past its budget, in one line: what it has cost, how to start afresh, and how to move the line. */
|
|
228
|
+
export const budgetLine = (cost) => `this session has cost $${cost.toFixed(2)} — /new starts a fresh one; sumo config chat.budget changes when this is said`;
|
|
229
|
+
|
|
230
|
+
/** The session log, one JSON line per message, the shape the scribe reads assistant replies from and the gauge reads usage from. */
|
|
231
|
+
function logLine(file, entry) {
|
|
232
|
+
appendFileSync(file, `${JSON.stringify({ timestamp: new Date().toISOString(), ...entry })}\n`, { mode: 0o600 });
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function configured(db, key) {
|
|
236
|
+
return getMeta(db, `config.${key}`) ?? CONFIG_DEFAULTS[key];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The chat's own tools, in a fixed order so its cached prefix never moves; the MCP servers' come after them. */
|
|
240
|
+
export const CHAT_TOOLS = [BASH_TOOL, EDITOR_TOOL, DELEGATE_TOOL, ASK_TOOL, ...WEB_TOOLS];
|
|
241
|
+
/**
|
|
242
|
+
* The chat's system prompt: AGENTS.md. The guides it names are read from wherever the chat was started: their paths are
|
|
243
|
+
* made absolute, or the model goes looking for them.
|
|
244
|
+
*/
|
|
245
|
+
export const chatSystem = () => readFileSync(join(REPO_ROOT, 'AGENTS.md'), 'utf8').trim().replaceAll(/(?<![\w/])guides\//g, `${join(REPO_ROOT, 'guides')}/`);
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* One chat session's state and policy, apart from the terminal, so it can be
|
|
249
|
+
* driven by tests with canned responses. `say(text)` is one user turn;
|
|
250
|
+
* `interrupt()` stops the one in progress. `watch` is told each tool call, its
|
|
251
|
+
* result, and the running totals, for a screen that shows the work — and, for
|
|
252
|
+
* a job run here, the job as it starts, its calls and results (marked with its
|
|
253
|
+
* id), what it says, and its end. `tell(text)` talks to the job running here;
|
|
254
|
+
* `tell(text, id)` to any open job, through its inbox on disk if it runs
|
|
255
|
+
* elsewhere. Inside Herdr (`env`), each job also gets a tab that shows its work,
|
|
256
|
+
* opened through `herdr` — both swapped in tests. `route` is the router asked
|
|
257
|
+
* for each turn's model when the model is `auto`. `approve` puts an MCP call
|
|
258
|
+
* that is not on the allow list to the user — the chat's own, or a job's run
|
|
259
|
+
* here — and answers `once`, `always` or `no`; without it such a call is refused.
|
|
260
|
+
* `ask` puts a question of the model's to the user, with its options, and
|
|
261
|
+
* answers `{ label }` for the one picked or `{ text }` for words of their own —
|
|
262
|
+
* anything else is no answer; without it the model is told nobody is there.
|
|
263
|
+
* With `background`, a delegated job does not hold the turn: `delegate` answers
|
|
264
|
+
* at once, the job runs beside the conversation with a stop of its own, and its
|
|
265
|
+
* report reaches the model later as an automated event — with its next request
|
|
266
|
+
* if it is at work, else in a turn of its own (`wake`), which whoever watches
|
|
267
|
+
* starts when told `job-report`.
|
|
268
|
+
*/
|
|
269
|
+
export function createChat(db, { model, effort, cwd = process.cwd(), send = sendToApi, out = () => {}, activity = () => {}, watch = () => {}, herdr = runHerdr, env = process.env, route = chooseChatRoute, approve = null, ask = null, background = false, now = () => new Date().toISOString() } = {}) {
|
|
270
|
+
// A route the chat was started with holds through a resumed session; one it was not, the saved session brings back.
|
|
271
|
+
const given = { model: model !== undefined, effort: effort !== undefined };
|
|
272
|
+
model ??= configured(db, 'chat.model');
|
|
273
|
+
// A model the user turned off is not chatted on, however the chat came to be on it; auto reads the switches each turn.
|
|
274
|
+
// A full API id is that model too (claude-opus-5-5 is opus), switched off or on with it.
|
|
275
|
+
const named = MODELS.find((name) => MODEL_IDS[name] === model) ?? model;
|
|
276
|
+
if (named !== 'auto' && MODELS.includes(named)) assertOn(db, named, 'sumo config chat.model <name>');
|
|
277
|
+
effort ??= model === 'auto' ? null : configured(db, 'chat.effort');
|
|
278
|
+
// Haiku takes no effort setting, however the chat came to be on it: by flag, by configuration, or by /model.
|
|
279
|
+
if (modelId(model) === MODEL_IDS.haiku) effort = 'none';
|
|
280
|
+
/** On auto, the route the last turn ran on; null otherwise. */
|
|
281
|
+
let routed = null;
|
|
282
|
+
const system = chatSystem();
|
|
283
|
+
const own = CHAT_TOOLS;
|
|
284
|
+
/** The MCP servers of this process, once connected: their tools join the request, deferred, and their names the system prompt. */
|
|
285
|
+
let registry = null;
|
|
286
|
+
const connecting = mcpReady().then((r) => {
|
|
287
|
+
registry = r;
|
|
288
|
+
});
|
|
289
|
+
const toolsNow = () => withMcp(own, registry?.tools() ?? []);
|
|
290
|
+
const systemNow = () => withServers(system, registry);
|
|
291
|
+
let sessionId;
|
|
292
|
+
let params;
|
|
293
|
+
let transcript;
|
|
294
|
+
let lastContext = 0;
|
|
295
|
+
let project = null;
|
|
296
|
+
let stopper = null;
|
|
297
|
+
/** The file that says this session is open here, until it ends. */
|
|
298
|
+
let lock = null;
|
|
299
|
+
/** Whether there is anything to save yet: a session nobody has spoken to is not kept. */
|
|
300
|
+
let spoken = false;
|
|
301
|
+
/** Said to the model with the next turn: that the session was picked up again, or that /undo put files back. */
|
|
302
|
+
let note = null;
|
|
303
|
+
/** The files the model has viewed or written in this session, as they stood then: an edit to one changed on disk since says so. */
|
|
304
|
+
let seen = new Map();
|
|
305
|
+
/** Whether the screen has said what this session cost: once a session, when it first passes `chat.budget`. */
|
|
306
|
+
let costSaid = false;
|
|
307
|
+
/** The jobs running here, each with what the user has said to it that it has not read yet. */
|
|
308
|
+
const jobs = new Map();
|
|
309
|
+
/** The projects a worker is running in: two in one working tree overwrite each other. */
|
|
310
|
+
const workers = new Set();
|
|
311
|
+
/** What the user said to the chat while it worked, not read yet: it goes in with the model's next request of the same turn. */
|
|
312
|
+
const steers = [];
|
|
313
|
+
/** The jobs at work in the background, each with the stop that is its own: Esc stops the chat's turn, not the work handed off. */
|
|
314
|
+
const stops = new Map();
|
|
315
|
+
/** The background jobs' runs, so a chat that closes can wait for the ones it stopped to let go. */
|
|
316
|
+
const runs = new Set();
|
|
317
|
+
/** The jobs the user stopped: their end is said, and wakes nothing. */
|
|
318
|
+
const stoppedByUser = new Set();
|
|
319
|
+
/** What background jobs reported and the model has not read yet: `{ id, text, quiet }`. */
|
|
320
|
+
const reports = [];
|
|
321
|
+
/** Turns nobody typed since the user last did: past MAX_WOKEN, what the jobs report waits for the user. */
|
|
322
|
+
let woken = 0;
|
|
323
|
+
/** The memory page, once /memory has asked for it: the promise of its address. */
|
|
324
|
+
let page = null;
|
|
325
|
+
const ledger = { kind: 'chat', sessionId: null };
|
|
326
|
+
// What /model with a model and no effort keeps: the effort in hand, or the configured one.
|
|
327
|
+
// Worked out as setRoute and parseRoute do: off auto, the effort kept is the user's configured one.
|
|
328
|
+
const others = () => savedSessions(db, { now: now(), except: sessionId ?? null });
|
|
329
|
+
const commands = commandList(db, {
|
|
330
|
+
effortNow: () => {
|
|
331
|
+
const kept = effort ?? configured(db, 'chat.effort');
|
|
332
|
+
return kept && kept !== 'none' ? kept : CONFIG_DEFAULTS['chat.effort'];
|
|
333
|
+
},
|
|
334
|
+
saved: () => others().map((s) => ({ id: s.id, hint: describeSaved(s, now()) })),
|
|
335
|
+
running: () => [...stops.keys()],
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const emit = (text) => out(text);
|
|
339
|
+
/** The conversation as the model in hand can take it: one that takes no operator messages is sent them as text. The history itself is left alone, so a model that does take them still reads it from its cache. */
|
|
340
|
+
const sendAs = (p, options) => send(SYSTEM_MESSAGES.has(p.model) ? p : { ...p, messages: p.messages.map((m) => (m.role === 'system' ? asText(m.content) : m)) }, options);
|
|
341
|
+
|
|
342
|
+
const busy = (id) => (holder) => `session ${shortId(id)} is open in another terminal (process ${holder})`;
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* A fresh session, or a saved one picked up again (`saved`, with its lock already `held`). A fresh one carries
|
|
346
|
+
* nothing over but the route: it has read nothing yet, and no card has named a project in it. A resumed one carries
|
|
347
|
+
* its whole conversation, the size it had reached, and its route unless the chat was started with one; the memory
|
|
348
|
+
* block it opened with is already its first turn, so the one made now is not sent again.
|
|
349
|
+
*/
|
|
350
|
+
function start(source, saved = null, held = null) {
|
|
351
|
+
sessionId = saved?.id ?? randomUUID();
|
|
352
|
+
mkdirSync(join(paths().logs, 'sessions'), { recursive: true, mode: 0o700 });
|
|
353
|
+
lock = held ?? takeLock(lockFile(sessionId), busy(sessionId));
|
|
354
|
+
ledger.sessionId = sessionId;
|
|
355
|
+
lastContext = saved?.contextTokens ?? 0;
|
|
356
|
+
costSaid = false;
|
|
357
|
+
project = null;
|
|
358
|
+
seen = new Map();
|
|
359
|
+
spoken = Boolean(saved);
|
|
360
|
+
transcript = join(paths().logs, 'sessions', `${sessionId}.jsonl`);
|
|
361
|
+
const block = handleEvent(db, 'session-start', { session_id: sessionId, cwd, transcript_path: transcript, source }, now());
|
|
362
|
+
if (saved && !given.model) {
|
|
363
|
+
model = saved.model;
|
|
364
|
+
routed = saved.routed ?? null;
|
|
365
|
+
if (!given.effort) effort = saved.effort;
|
|
366
|
+
if (modelId(model) === MODEL_IDS.haiku) effort = 'none';
|
|
367
|
+
}
|
|
368
|
+
params = paramsFor({ model: model === 'auto' ? (routed?.model ?? CONFIG_DEFAULTS['chat.model']) : model, effort: model === 'auto' ? routed?.effort : effort, tools: toolsNow(), system: systemNow(), text: saved ? undefined : block });
|
|
369
|
+
if (saved) {
|
|
370
|
+
params.messages = withoutThinking(saved.messages);
|
|
371
|
+
note = `resumed: this session was saved ${ago(saved.savedAt, now())} and picked up again now, in ${cwd}. Carry on where it stopped; memory may have changed since this session's first block — search it before assuming.`;
|
|
372
|
+
}
|
|
373
|
+
return block;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* `/resume <id>`: a saved session picked up where it stopped, in place of this one, which ends. Everything is checked
|
|
378
|
+
* before anything changes: the id names one session, it can be read, its model is on, and no other terminal has it.
|
|
379
|
+
*/
|
|
380
|
+
function resume(ref) {
|
|
381
|
+
const found = findSaved(db, ref, { now: now(), except: sessionId ?? null });
|
|
382
|
+
const saved = loadState(found.id);
|
|
383
|
+
if (!saved) throw new UsageError(`the saved session ${shortId(found.id)} is gone`);
|
|
384
|
+
// Its model is checked as the chat's own was: a full API id is that model too, and one turned off since is not chatted on.
|
|
385
|
+
const named = MODELS.find((name) => MODEL_IDS[name] === saved.model) ?? saved.model;
|
|
386
|
+
if (!given.model && named !== 'auto' && MODELS.includes(named)) assertOn(db, named, '/model <name>');
|
|
387
|
+
const held = takeLock(lockFile(found.id), busy(found.id));
|
|
388
|
+
if (sessionId) end();
|
|
389
|
+
start('resume', saved, held);
|
|
390
|
+
return `resumed ${sessionLine(found, now())} — the first reply pays one uncached turn`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** The session as it stands, kept after every turn. A save that fails is said as the turn's error: the turn itself went fine. */
|
|
394
|
+
function persist() {
|
|
395
|
+
if (!spoken) return;
|
|
396
|
+
try {
|
|
397
|
+
saveState({ id: sessionId, messages: params.messages, model, effort, routed, cwd, contextTokens: lastContext, now: now() });
|
|
398
|
+
} catch (cause) {
|
|
399
|
+
throw new UsageError(`the session could not be saved: ${cause.message}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** The request from here on goes to this model at this effort, thinking as that model does; the conversation so far stays. */
|
|
404
|
+
function apply({ model: m, effort: e }) {
|
|
405
|
+
params.model = modelId(m);
|
|
406
|
+
if (e && e !== 'none') params.output_config = { effort: e };
|
|
407
|
+
else delete params.output_config;
|
|
408
|
+
const thinking = thinkingFor(m);
|
|
409
|
+
if (thinking) params.thinking = thinking;
|
|
410
|
+
else delete params.thinking;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* `/model`: with nothing, says the route; with `auto`, a model, or a model and an effort,
|
|
415
|
+
* sets it for the turns from here on and says what it is now. A bad one is thrown and changes nothing.
|
|
416
|
+
*/
|
|
417
|
+
function setRoute(args) {
|
|
418
|
+
if (args.trim()) {
|
|
419
|
+
// Off auto there is no effort in hand: the one kept is the user's configured one.
|
|
420
|
+
const next = parseRoute(args.trim(), { model, effort: effort ?? configured(db, 'chat.effort') }, db);
|
|
421
|
+
({ model, effort } = next);
|
|
422
|
+
routed = null;
|
|
423
|
+
if (model !== 'auto') apply(next);
|
|
424
|
+
persist();
|
|
425
|
+
}
|
|
426
|
+
return model === 'auto' ? `auto${routed ? ` → ${routeOf(routed)}` : ''}` : routeOf({ model, effort });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const event = (name, payload) => handleEvent(db, name, { session_id: sessionId, cwd, transcript_path: transcript, ...payload }, now());
|
|
430
|
+
|
|
431
|
+
/** Where this session's turns keep what the editor changed, for /undo. */
|
|
432
|
+
const undoDir = () => join(paths().logs, 'undo', sessionId);
|
|
433
|
+
|
|
434
|
+
/** The project the work is in: the one whose card came in last, else none (tools then run in scratch, wherever the chat was started). */
|
|
435
|
+
function workspace() {
|
|
436
|
+
const slug = currentProject(db, sessionId);
|
|
437
|
+
if (slug && project?.slug !== slug) project = getProject(db, slug);
|
|
438
|
+
return contextFor(project ?? { path: scratchDir() }, { sandbox: sandboxOn(db), network: networkOn(db) });
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* A question for the user. Memory is read first, and holds it back once if it may hold the answer. Then it waits on
|
|
443
|
+
* the user: a pick is kept as their decision, with the question; what they typed instead is their words, kept as a turn is.
|
|
444
|
+
*/
|
|
445
|
+
async function askUser(input) {
|
|
446
|
+
const line = (v) => safeForTerminal(String(v ?? '')).replace(/\s+/g, ' ').trim();
|
|
447
|
+
const question = safeForTerminal(String(input.question ?? '')).trim();
|
|
448
|
+
const options = (Array.isArray(input.options) ? input.options : []).map((o) => ({ label: line(o?.label ?? o), ...(line(o?.detail) ? { detail: line(o.detail) } : {}) }));
|
|
449
|
+
if (!question || options.length < 2 || options.length > 4 || options.some((o) => !o.label)) return { content: 'not asked: give `question` and 2–4 `options`, each with a `label`.', isError: true };
|
|
450
|
+
const gate = event('pre-tool', { tool_name: ASK_TOOL.name, tool_input: { question } });
|
|
451
|
+
if (gate) return { content: JSON.parse(gate).deny, isError: true };
|
|
452
|
+
if (!ask) return { content: 'not asked: nobody is here to answer. Decide it yourself and say what you assumed, or end your turn on the question.', isError: true };
|
|
453
|
+
const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve('no'), { once: true }));
|
|
454
|
+
const answer = await Promise.race([ask({ question, options, signal: stopper.signal }), stopped]);
|
|
455
|
+
const picked = options.find((o) => o.label === answer?.label);
|
|
456
|
+
if (picked) {
|
|
457
|
+
// A pick is the user's decision in the model's wording: kept beside what they typed, with the question it answers.
|
|
458
|
+
recordTurn(db, { sessionId, text: `${question} → ${picked.label}`, now: now() });
|
|
459
|
+
return { content: `The user chose: ${picked.label}` };
|
|
460
|
+
}
|
|
461
|
+
const text = typeof answer?.text === 'string' ? answer.text.trim() : '';
|
|
462
|
+
if (!text) return { content: 'no answer: the user turned the question away. Do not ask it again; decide it yourself and say what you assumed, or carry on another way.', isError: true };
|
|
463
|
+
const brought = event('prompt', { prompt: text, context_tokens: lastContext });
|
|
464
|
+
noteGuides(brought);
|
|
465
|
+
logLine(transcript, { type: 'user', message: { role: 'user', content: redact(text).text } });
|
|
466
|
+
return { content: `The user answered: ${text}${brought ? `\n<sumo>\n${brought}\n</sumo>` : ''}` };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** The workflow gate, the cheap-model passes, `delegate` and `ask`; anything else is the tool's own business. */
|
|
470
|
+
async function beforeTool(call) {
|
|
471
|
+
if (call.name === DELEGATE_TOOL.name) return delegate(call.input ?? {}, call.id);
|
|
472
|
+
if (call.name === ASK_TOOL.name) return askUser(call.input ?? {});
|
|
473
|
+
if (isMcpTool(call.name)) return gateMcp(call, { approve, signal: stopper.signal });
|
|
474
|
+
if (call.name !== BASH_TOOL.name) return null;
|
|
475
|
+
const command = String(call.input?.command ?? '');
|
|
476
|
+
if (tampersMarker(command)) return { content: TAMPER_REFUSED, isError: true };
|
|
477
|
+
const gate = event('pre-tool', { tool_name: 'bash', tool_input: { command } });
|
|
478
|
+
if (gate) return { content: JSON.parse(gate).deny, isError: true };
|
|
479
|
+
const pass = PASS_RUN.exec(command);
|
|
480
|
+
if (pass) {
|
|
481
|
+
activity(`running the ${pass[1]} pass…`);
|
|
482
|
+
return { content: outcomeLines(pass[1] === 'dream' ? await runDream(db, { force: true }) : await runScribe(db)).join('\n') };
|
|
483
|
+
}
|
|
484
|
+
const job = JOB_RUN.exec(command);
|
|
485
|
+
if (job) return { content: `jobs are not ${job[1] === 'new' ? 'made' : 'run'} from the shell: use the delegate tool${job[1] === 'run' ? ' (only `job` for one already made)' : ''}`, isError: true };
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* `delegate`: a job is made from the brief, or an open one picked up, and run here on its own route. The chat waits
|
|
491
|
+
* for its report — or, working in the background, is answered at once and reads the report when it comes. Calls in
|
|
492
|
+
* one reply run side by side, but a worker has its project's working tree to itself — in a git project, a second one
|
|
493
|
+
* is given a worktree of its own. Several `attempts` run side by side too, and are answered for once, together.
|
|
494
|
+
* `callId` is the delegate call's own, so a screen can show the job under the call that started it.
|
|
495
|
+
*/
|
|
496
|
+
async function delegate(input, callId = null) {
|
|
497
|
+
const asked = (input.job === undefined || input.job === null) && [2, 3].includes(input.attempts) ? input.attempts : 1;
|
|
498
|
+
// Counted where a run begins, before anything is awaited: five calls in one reply are judged one after another, not all at once.
|
|
499
|
+
if (jobs.size + asked > MAX_JOBS) throw new UsageError(`${[...jobs.keys()].map((j) => `j${j}`).join(', ')} are at work already — ${MAX_JOBS} at once is the most; wait for a report`);
|
|
500
|
+
const own = background ? new AbortController() : null;
|
|
501
|
+
const signal = own?.signal ?? stopper.signal;
|
|
502
|
+
let claimed = null;
|
|
503
|
+
const claim = (agent, slug, shared) => {
|
|
504
|
+
if (agent !== 'worker' || !shared) return;
|
|
505
|
+
if (workers.has(slug)) throw new UsageError(`a worker is already running in ${slug} — two in one working tree overwrite each other; wait for its report`);
|
|
506
|
+
workers.add(slug);
|
|
507
|
+
claimed = slug;
|
|
508
|
+
};
|
|
509
|
+
const release = () => {
|
|
510
|
+
if (claimed) workers.delete(claimed);
|
|
511
|
+
};
|
|
512
|
+
let id;
|
|
513
|
+
/** The other attempts made beside the first, when several were asked for. */
|
|
514
|
+
let siblings = [];
|
|
515
|
+
let warnings = [];
|
|
516
|
+
try {
|
|
517
|
+
if (input.job !== undefined && input.job !== null) {
|
|
518
|
+
const job = getJob(db, parseJobId(input.job));
|
|
519
|
+
claim(job.agent, job.project, treeOf(db, job.id) === null);
|
|
520
|
+
id = job.id;
|
|
521
|
+
} else {
|
|
522
|
+
const agent = input.agent ?? 'worker';
|
|
523
|
+
const project = getProject(db, input.project ?? '');
|
|
524
|
+
if (!project) throw new UsageError(`no project "${input.project ?? ''}" — the delegate call names one by its slug`);
|
|
525
|
+
claim(agent, project.slug, !hasCommit(project.path));
|
|
526
|
+
activity(`briefing a ${agent}…`);
|
|
527
|
+
const made = await newJob(db, { project: project.slug, title: input.title, agent, task: input.task, guide: input.guide, reviews: input.reviews ?? undefined, testsMayChange: Boolean(input.tests_may_change), attempts: input.attempts ?? 1, now: now() });
|
|
528
|
+
({ job: { id }, warnings } = made);
|
|
529
|
+
siblings = (made.jobs ?? []).slice(1).map((job) => job.id);
|
|
530
|
+
}
|
|
531
|
+
} catch (cause) {
|
|
532
|
+
release();
|
|
533
|
+
throw cause;
|
|
534
|
+
}
|
|
535
|
+
let begun;
|
|
536
|
+
const started = new Promise((resolve) => (begun = resolve));
|
|
537
|
+
const ids = [id, ...siblings];
|
|
538
|
+
// A job made to follow this one — its retry, or its review — is at work under the same call, and stopped with it.
|
|
539
|
+
const follow = (next) => {
|
|
540
|
+
ids.push(next);
|
|
541
|
+
if (own) stops.set(next, own);
|
|
542
|
+
};
|
|
543
|
+
const work = (siblings.length === 0 ? runFollowedHere(id, warnings, { signal, callId, begun }, follow) : runAttempts(ids, warnings, { signal, callId, begun })).finally(release);
|
|
544
|
+
if (!own) return work;
|
|
545
|
+
|
|
546
|
+
// In the background. A job that cannot start says so as the call's own answer; one that has started answers later.
|
|
547
|
+
// Attempts share one stop: stopping one stops them all.
|
|
548
|
+
for (const each of ids) stops.set(each, own);
|
|
549
|
+
const first = await Promise.race([started, work.then(() => null, (cause) => ({ cause }))]);
|
|
550
|
+
if (first?.cause) {
|
|
551
|
+
for (const each of ids) stops.delete(each);
|
|
552
|
+
throw first.cause;
|
|
553
|
+
}
|
|
554
|
+
const run = work
|
|
555
|
+
.then((result) => result, (cause) => ({ content: `j${id} stopped on an error: ${cause.message}`, isError: true }))
|
|
556
|
+
.then((result) => {
|
|
557
|
+
for (const each of ids) stops.delete(each);
|
|
558
|
+
runs.delete(run);
|
|
559
|
+
const quiet = ids.filter((each) => stoppedByUser.delete(each)).length > 0;
|
|
560
|
+
reports.push({ id, quiet, text: `An automated event, not a message from the user: ${ids.map((each) => `j${each}`).join(', ')} ${ids.length === 1 ? 'has' : 'have'} reported.\n${result.content}` });
|
|
561
|
+
watch({ type: 'job-report', job: id, result, quiet });
|
|
562
|
+
});
|
|
563
|
+
runs.add(run);
|
|
564
|
+
const job = getJob(db, id);
|
|
565
|
+
const beside = siblings.length === 0 ? '' : `, with ${siblings.map((each) => `j${each}`).join(' and ')} beside it: attempts at the same task, reported on together`;
|
|
566
|
+
return {
|
|
567
|
+
content:
|
|
568
|
+
`j${id} is running in the background (${job.agent} · ${routeOf(job)} — ${job.title})${beside}. Its report will reach you later, as an automated event marked as ` +
|
|
569
|
+
'not from the user: with your next request if you are still at work, otherwise in a turn of its own. Until then you know nothing of what it found — ' +
|
|
570
|
+
'do not guess its result, and do not do its work yourself. Carry on with what does not depend on it, or end your turn and say it is running.',
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** Attempts at one task, run side by side and answered for once, when all have ended: what was kept first, then how each went. */
|
|
575
|
+
async function runAttempts(ids, warnings, options) {
|
|
576
|
+
const ended = await Promise.allSettled(ids.map((each) => runHere(each, [], options)));
|
|
577
|
+
const kept = attemptsSettled(db, ids[0]) ?? ['Not every attempt closed, so none was picked or brought back yet.'];
|
|
578
|
+
const each = ended.map((e, i) => (e.status === 'fulfilled' ? e.value.content : `j${ids[i]} stopped on an error: ${e.reason?.message}`));
|
|
579
|
+
return { content: capRedacted([[...warnings, `Attempts ${ids.map((j) => `j${j}`).join(', ')}:`, ...kept].join('\n'), ...each].join('\n\n')) };
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* One job run here, and what follows it unasked — a retry one step up of a first attempt that failed, a review of
|
|
584
|
+
* risky work done (`runFollowed`, src/jobs.mjs) — answered for once. A retry stands in for the attempt that failed,
|
|
585
|
+
* after the line that says so; a review comes after the work it judged. `onFollow` hears each job made, before it runs.
|
|
586
|
+
*/
|
|
587
|
+
async function runFollowedHere(id, warnings, options, onFollow) {
|
|
588
|
+
const ran = await runFollowed(db, id, (each, said = warnings) => runHere(each, said, options), { signal: options.signal, onFollow, now });
|
|
589
|
+
if (ran.length === 1) return ran[0].result;
|
|
590
|
+
let parts = [];
|
|
591
|
+
let shown = [];
|
|
592
|
+
let job = null;
|
|
593
|
+
for (const step of ran) {
|
|
594
|
+
// The attempt that failed is one line now; its report is a command away.
|
|
595
|
+
if (step.kind === 'retry') [parts, shown, job] = [[], [], null];
|
|
596
|
+
parts.push([step.line, step.result?.content ?? (step.error ? `j${step.id} stopped on an error: ${step.error}` : '')].filter(Boolean).join('\n'));
|
|
597
|
+
shown.push([step.line, step.result?.job?.report].filter(Boolean).join('\n'));
|
|
598
|
+
job ??= step.result?.job ?? null;
|
|
599
|
+
}
|
|
600
|
+
return { content: parts.join('\n\n'), job: job && { ...job, report: shown.join('\n\n') } };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** One job, run here to its end: watched as it works, written down for `sumo job watch`, and answered with how it went and what it reported. */
|
|
604
|
+
async function runHere(id, warnings, { signal, callId, begun }) {
|
|
605
|
+
const inbox = [];
|
|
606
|
+
jobs.set(id, inbox);
|
|
607
|
+
const began = Date.now();
|
|
608
|
+
// Its work is written down as it happens, for `sumo job watch` — in a Herdr tab of its own, or any terminal —
|
|
609
|
+
// and without its secrets, through the same redaction as what the model sees.
|
|
610
|
+
const record = (t) => appendLive(id, redact(t).text);
|
|
611
|
+
const live = jobPrinter(record, styles(false), { clean: (t) => redact(t).text });
|
|
612
|
+
let ended = [];
|
|
613
|
+
try {
|
|
614
|
+
const outcome = await runJob(db, id, {
|
|
615
|
+
send: (p) => send(p, { signal }),
|
|
616
|
+
signal,
|
|
617
|
+
approve,
|
|
618
|
+
inbox: () => inbox.splice(0),
|
|
619
|
+
onStart: (job) => {
|
|
620
|
+
startLive(id);
|
|
621
|
+
live.onStart(job);
|
|
622
|
+
watch({ type: 'job', job, call: callId, tab: env.HERDR_ENV ? watchTab(job) : null });
|
|
623
|
+
begun();
|
|
624
|
+
},
|
|
625
|
+
onTool: (call) => {
|
|
626
|
+
live.onTool(call);
|
|
627
|
+
activity(`j${id} ${describeCall(call)}`);
|
|
628
|
+
watch({ type: 'tool', call, job: id });
|
|
629
|
+
},
|
|
630
|
+
onResult: (call, result) => {
|
|
631
|
+
live.onResult(call, result);
|
|
632
|
+
watch({ type: 'result', call, result, job: id });
|
|
633
|
+
},
|
|
634
|
+
onTurn: (response, totals) => {
|
|
635
|
+
live.onTurn(response, totals);
|
|
636
|
+
overBudget();
|
|
637
|
+
},
|
|
638
|
+
onThinking: (_delta, snapshot) => watch({ type: 'thinking', job: id, text: snapshot }),
|
|
639
|
+
onRetry: (r) => {
|
|
640
|
+
live.onRetry(r);
|
|
641
|
+
watch({ type: 'retry', job: id, ...r });
|
|
642
|
+
},
|
|
643
|
+
onCompact: (c) => {
|
|
644
|
+
live.onCompact(c);
|
|
645
|
+
watch({ type: 'compact', job: id, ...c });
|
|
646
|
+
},
|
|
647
|
+
});
|
|
648
|
+
ended = runLines(outcome);
|
|
649
|
+
const report = reportOf(id);
|
|
650
|
+
// Model-written, like any tool output: redacted before it reaches the chat's context, and cut to fit there. `job` is how it went, for the screen only.
|
|
651
|
+
const { job, turns, totals } = outcome;
|
|
652
|
+
return {
|
|
653
|
+
content: capRedacted([...warnings, ...ended, ...(report ? ['', reportForModel(report, id)] : [])].join('\n')),
|
|
654
|
+
job: { id, status: job.status, seconds: Math.round((Date.now() - began) / 1000), turns, toolCalls: totals.toolCalls, costUsd: totals.costUsd, model: job.model, effort: job.effort, report: report ? redact(report).text : '' },
|
|
655
|
+
};
|
|
656
|
+
} catch (cause) {
|
|
657
|
+
ended = [cause.message];
|
|
658
|
+
throw cause;
|
|
659
|
+
} finally {
|
|
660
|
+
jobs.delete(id);
|
|
661
|
+
endLive(id, ended.map((l) => redact(l).text));
|
|
662
|
+
watch({ type: 'job-end', job: id });
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** Stops a job at work in the background, for the user: it ends where it is, and its end is said to the model with whatever comes next. */
|
|
667
|
+
function stop(id) {
|
|
668
|
+
const own = stops.get(id);
|
|
669
|
+
if (!own) return false;
|
|
670
|
+
stoppedByUser.add(id);
|
|
671
|
+
own.abort();
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/** The chat is closing: the jobs still at work are stopped — each can be picked up again from its notes — and waited for, so none is cut off mid-write. */
|
|
676
|
+
async function settle() {
|
|
677
|
+
for (const id of [...stops.keys()]) stop(id);
|
|
678
|
+
await Promise.allSettled([...runs]);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* A turn nobody typed: what background jobs have reported since the model last looked, put to it as an automated event.
|
|
683
|
+
* Null when nothing is waiting — a job the user stopped waits for the next message instead.
|
|
684
|
+
*/
|
|
685
|
+
async function wake() {
|
|
686
|
+
if (waiting() === 0) return null;
|
|
687
|
+
woken++;
|
|
688
|
+
return say(reports.splice(0).map((r) => r.text).join('\n\n'), '', [], { automated: true });
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/** How many reports wait for a turn of their own: none once MAX_WOKEN automated turns have run in a row — they wait for the user's next message. */
|
|
692
|
+
const waiting = () => (woken >= MAX_WOKEN ? 0 : reports.filter((r) => !r.quiet).length);
|
|
693
|
+
|
|
694
|
+
/** The job's tab in Herdr, showing its work: its pane, or why there is none — the job runs here either way. A watcher that never starts in it is said when it gives up. */
|
|
695
|
+
function watchTab(job) {
|
|
696
|
+
try {
|
|
697
|
+
return { pane: openWatchTab(herdr, { job, project: getProject(db, job.project), env, onFail: (reason) => watch({ type: 'tab-failed', job: job.id, reason }) }) };
|
|
698
|
+
} catch (cause) {
|
|
699
|
+
return { error: cause.message };
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* Something for a job. Without an id: the one job running here, or false when there is none, so the caller can
|
|
705
|
+
* treat the words as its own; with several running, it must be named. With an id: that job, through its inbox on
|
|
706
|
+
* disk if it is not running here; a job that is not open is refused. What comes back is the job it reached.
|
|
707
|
+
*/
|
|
708
|
+
function tell(text, id) {
|
|
709
|
+
if (id === undefined || id === null) {
|
|
710
|
+
if (jobs.size === 0) return false;
|
|
711
|
+
if (jobs.size > 1) throw new UsageError(`${[...jobs.keys()].map((j) => `j${j}`).join(', ')} are running — name the one: @j<id> …`);
|
|
712
|
+
[id] = jobs.keys();
|
|
713
|
+
}
|
|
714
|
+
if (jobs.has(id)) jobs.get(id).push(text);
|
|
715
|
+
else tellJob(db, id, text, now());
|
|
716
|
+
return id;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** A turn that ended before the model answered left its operator message at the tail; the API takes one only where the model answers next, so before anything follows it, it becomes text. */
|
|
720
|
+
function settleTail() {
|
|
721
|
+
const tail = params.messages.at(-1);
|
|
722
|
+
if (tail?.role === 'system') params.messages[params.messages.length - 1] = asText(tail.content);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* One turn. `text` is what the model is sent; `said` is what the user typed of it — for a slash command, the words after the command — and is all that memory keeps as theirs.
|
|
727
|
+
* `images` go before the words, each under the label the text names it by: `{ label, mediaType, data }`, the data in base64.
|
|
728
|
+
* `guide` is the one a slash command named, which rides in whether or not the words would have brought it.
|
|
729
|
+
*/
|
|
730
|
+
async function say(text, said = text, images = [], { automated = false, guide = null } = {}) {
|
|
731
|
+
try {
|
|
732
|
+
const outcome = await turn(text, said, images, automated, guide);
|
|
733
|
+
persist();
|
|
734
|
+
return outcome;
|
|
735
|
+
} finally {
|
|
736
|
+
// What was said too late to be read in this turn is the caller's to send as a turn of its own, not this one's to keep.
|
|
737
|
+
steers.length = 0;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* What was said while the turn worked, handed to the model with its next request. Each is the user's own words, so it is
|
|
743
|
+
* kept as a turn is — stored, logged, and able to bring a project's card in with it — and whoever watches is told it was read.
|
|
744
|
+
*/
|
|
745
|
+
function heard() {
|
|
746
|
+
const said = steers.splice(0);
|
|
747
|
+
if (said.length > 0) watch({ type: 'heard', said });
|
|
748
|
+
return [
|
|
749
|
+
...said.map((text) => {
|
|
750
|
+
const brought = event('prompt', { prompt: text, context_tokens: lastContext });
|
|
751
|
+
noteGuides(brought);
|
|
752
|
+
logLine(transcript, { type: 'user', message: { role: 'user', content: redact(text).text } });
|
|
753
|
+
return brought ? `${text}\n<sumo>\n${brought}\n</sumo>` : text;
|
|
754
|
+
}),
|
|
755
|
+
// What a background job reported while the model worked: read now, as something that happened, not as the user's words.
|
|
756
|
+
...reports.splice(0).map((report) => ({ event: report.text })),
|
|
757
|
+
];
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** Takes back something said while the turn worked, if the model has not read it yet. */
|
|
761
|
+
function unsteer(text) {
|
|
762
|
+
const at = steers.lastIndexOf(text);
|
|
763
|
+
if (at === -1) return false;
|
|
764
|
+
steers.splice(at, 1);
|
|
765
|
+
return true;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
/** The screen is told which guide rode in: one dim line, so the user knows the model was handed a way of working. */
|
|
769
|
+
function noteGuides(brought) {
|
|
770
|
+
for (const [, name] of brought.matchAll(/^<guide (\w+)>/gm)) watch({ type: 'guide', name });
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
async function turn(text, said, images, automated = false, guide = null) {
|
|
774
|
+
stopper = new AbortController();
|
|
775
|
+
// Edits nothing has checked: an editor call that writes adds its file; one of the project's declared checks running
|
|
776
|
+
// clears them — any command does when the project declares none. The gate at the end of the turn is told what is
|
|
777
|
+
// left. The shell can edit too (`sed -i`, a redirect); that is not seen.
|
|
778
|
+
const unchecked = new Set();
|
|
779
|
+
const declared = () => verifyCommands(workspace().cwd).map((c) => c.command);
|
|
780
|
+
const noteCall = (call) => {
|
|
781
|
+
if (call.name === EDITOR_TOOL.name && call.input?.command !== 'view' && call.input?.path) unchecked.add(resolve(workspace().cwd, String(call.input.path)));
|
|
782
|
+
if (call.name !== BASH_TOOL.name) return;
|
|
783
|
+
const checks = declared();
|
|
784
|
+
if (checks.length === 0 || checks.some((c) => String(call.input?.command ?? '').includes(c))) unchecked.clear();
|
|
785
|
+
};
|
|
786
|
+
// The servers connect while the first turn is typed; the first request waits for them, and Esc does not.
|
|
787
|
+
if (!registry) {
|
|
788
|
+
const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve('stopped'), { once: true }));
|
|
789
|
+
if ((await Promise.race([connecting, stopped])) === 'stopped') return { stop: 'interrupted', error: null, text: '' };
|
|
790
|
+
params.tools = toolsNow();
|
|
791
|
+
params.system[0].text = systemNow();
|
|
792
|
+
if (registry.servers().length > 0 || registry.error) watch({ type: 'mcp', servers: registry.servers(), error: registry.error });
|
|
793
|
+
}
|
|
794
|
+
// On auto the router names this turn's model first; a router that cannot answer refuses the turn, and nothing of it reaches the conversation.
|
|
795
|
+
// A turn nobody typed runs on the route the last one had: there is no message to size it by.
|
|
796
|
+
if (model === 'auto' && !(automated && routed)) {
|
|
797
|
+
let chosen;
|
|
798
|
+
try {
|
|
799
|
+
workspace();
|
|
800
|
+
// Esc is heard while the router thinks: the turn ends now, and the router's late answer is let go unread.
|
|
801
|
+
const routing = route(db, { project, text });
|
|
802
|
+
routing.catch(() => {});
|
|
803
|
+
const stopped = new Promise((resolve) => stopper.signal.addEventListener('abort', () => resolve(null), { once: true }));
|
|
804
|
+
chosen = await Promise.race([routing, stopped]);
|
|
805
|
+
} catch (cause) {
|
|
806
|
+
return { stop: 'error', error: cause.message, text: '' };
|
|
807
|
+
}
|
|
808
|
+
// Stopped while the router was still choosing: nothing has been sent, and nothing of the turn is kept.
|
|
809
|
+
if (stopper.signal.aborted) return { stop: 'interrupted', error: null, text: '' };
|
|
810
|
+
routed = chosen;
|
|
811
|
+
apply(routed);
|
|
812
|
+
logLine(transcript, { type: 'route', ...routed });
|
|
813
|
+
watch({ type: 'route', ...routed });
|
|
814
|
+
}
|
|
815
|
+
settleTail();
|
|
816
|
+
// A resumed session is told so with its first turn. An automated turn is a report itself: nothing in it is the user's, so nothing of it is kept as theirs.
|
|
817
|
+
const injected = [note, ...(automated ? [] : [event('prompt', { prompt: said, context_tokens: lastContext, guide })])].filter(Boolean).join('\n');
|
|
818
|
+
noteGuides(injected);
|
|
819
|
+
note = null;
|
|
820
|
+
if (!automated) woken = 0;
|
|
821
|
+
const pictures = images.flatMap(({ label, mediaType, data }) => [{ type: 'text', text: label }, { type: 'image', source: { type: 'base64', media_type: mediaType, data } }]);
|
|
822
|
+
// What background jobs reported while nobody was looking rides in with the message, as blocks of its own: an event in the user's turn,
|
|
823
|
+
// never operator text — a report repeats what a page or a file said, and that is nobody's instruction.
|
|
824
|
+
const events = reports.splice(0).map((r) => eventBlock(r.text));
|
|
825
|
+
const at = params.messages.push(automated ? { role: 'user', content: [eventBlock(text)] } : { role: 'user', content: [...pictures, { type: 'text', text }, ...events] }) - 1;
|
|
826
|
+
spoken = true;
|
|
827
|
+
if (injected) params.messages.push(injection(injected, params.model, params.messages));
|
|
828
|
+
// The log keeps what memory keeps: the user's words without their secrets — and an event as an event.
|
|
829
|
+
logLine(transcript, automated ? { type: 'event', text: redact(text).text } : { type: 'user', message: { role: 'user', content: redact(text).text } });
|
|
830
|
+
|
|
831
|
+
// What the editor changes in this turn is kept for /undo in one folder, however many requests the turn takes.
|
|
832
|
+
const undoFolder = nextUndoTurn(undoDir());
|
|
833
|
+
let continued = false;
|
|
834
|
+
for (;;) {
|
|
835
|
+
const outcome = await converse(db, params, {
|
|
836
|
+
send: sendAs,
|
|
837
|
+
// A command the sandbox kept from writing is put to the user as an MCP call off the allow list is, for one run outside it.
|
|
838
|
+
ctx: { ...workspace(), seen, undo: undoFolder, approve },
|
|
839
|
+
ledger,
|
|
840
|
+
beforeTool,
|
|
841
|
+
concurrent: (call) => call.name === DELEGATE_TOOL.name,
|
|
842
|
+
signal: stopper.signal,
|
|
843
|
+
inbox: heard,
|
|
844
|
+
onOutput: (call, chunk) => watch({ type: 'output', call, chunk }),
|
|
845
|
+
onText: emit,
|
|
846
|
+
onThinking: (_delta, snapshot) => watch({ type: 'thinking', text: snapshot }),
|
|
847
|
+
onRetry: (r) => {
|
|
848
|
+
activity(retryLine(r));
|
|
849
|
+
watch({ type: 'retry', ...r });
|
|
850
|
+
},
|
|
851
|
+
onTool: (call) => {
|
|
852
|
+
activity(describeCall(call));
|
|
853
|
+
watch({ type: 'tool', call });
|
|
854
|
+
noteCall(call);
|
|
855
|
+
},
|
|
856
|
+
onResult: (call, result) => watch({ type: 'result', call, result }),
|
|
857
|
+
onTurn: (response, totals) => {
|
|
858
|
+
lastContext = totals.contextTokens;
|
|
859
|
+
logLine(transcript, { type: 'assistant', message: { role: 'assistant', model: response.model ?? params.model, usage: response.usage, content: response.content } });
|
|
860
|
+
watch({ type: 'usage', totals });
|
|
861
|
+
overBudget();
|
|
862
|
+
// What the reply rests on, said under it: the pages it cites.
|
|
863
|
+
const sources = sourcesOf(response.content);
|
|
864
|
+
if (sources.length > 0) watch({ type: 'sources', sources });
|
|
865
|
+
},
|
|
866
|
+
});
|
|
867
|
+
// Pictures the API refused would ride along with every later turn and be refused again: a turn refused before the model saw it keeps only its words.
|
|
868
|
+
// Only a refusal of the request itself (a 4xx); a rate limit, an overload or a dropped connection says nothing against the pictures.
|
|
869
|
+
const rejected = outcome.status >= 400 && outcome.status < 500 && outcome.status !== 429;
|
|
870
|
+
if (outcome.stop === 'error' && rejected && pictures.length > 0 && !params.messages.slice(at + 1).some((m) => m.role === 'assistant')) params.messages[at].content = [{ type: 'text', text }, ...events];
|
|
871
|
+
// Stopped by the user: the turn still ended, so the scribe is woken, but nothing is asked of memory and nothing continues.
|
|
872
|
+
if (outcome.stop === 'interrupted') {
|
|
873
|
+
event('stop', { stop_hook_active: true });
|
|
874
|
+
return outcome;
|
|
875
|
+
}
|
|
876
|
+
// The check before the user, and memory before the user: a turn ending on an edit nothing checked, or on a question
|
|
877
|
+
// memory can answer, is told so and continued, once. Only a turn the model ended itself: after an error or a cut-off
|
|
878
|
+
// reply the turn is simply over, and the scribe still hears of it.
|
|
879
|
+
const asked = outcome.stop === 'end_turn';
|
|
880
|
+
const edited = [...unchecked].map((path) => relative(workspace().cwd, path));
|
|
881
|
+
const checks = declared();
|
|
882
|
+
const held = continued ? '' : event('stop', asked ? { last_assistant_message: outcome.text, stop_hook_active: false, edited, checks } : { stop_hook_active: true });
|
|
883
|
+
if (!held) return outcome;
|
|
884
|
+
const { context } = JSON.parse(held);
|
|
885
|
+
if (context.includes('<check>')) watch({ type: 'check', edited, checks });
|
|
886
|
+
params.messages.push(injection(context, params.model, params.messages));
|
|
887
|
+
continued = true;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* `/undo`: the files the editor changed in the last turn that changed any, put back as they were before it — one it
|
|
893
|
+
* created is deleted — and the model told so with its next turn. Another walks back one turn more. The shell's changes
|
|
894
|
+
* are not seen, so they are not undone.
|
|
895
|
+
*/
|
|
896
|
+
function undo() {
|
|
897
|
+
const done = undoLast(undoDir());
|
|
898
|
+
if (!done) return 'nothing to undo: the editor has changed no file in this session — changes made through bash are not tracked';
|
|
899
|
+
const { cwd: at } = workspace();
|
|
900
|
+
const names = (list) => {
|
|
901
|
+
const shown = list.map((path) => pathFrom(path, at));
|
|
902
|
+
return shown.length === 1 ? shown[0] : `${shown.slice(0, -1).join(', ')} and ${shown.at(-1)}`;
|
|
903
|
+
};
|
|
904
|
+
const what = [done.restored.length > 0 && `restored ${names(done.restored)}`, done.deleted.length > 0 && `deleted ${names(done.deleted)}, which that turn created`].filter(Boolean).join('; ');
|
|
905
|
+
note = [note, `the user ran /undo: ${what}. View a file again before you edit it.`].filter(Boolean).join('\n');
|
|
906
|
+
return `undid the last turn that changed files: ${what} — changes made through bash are not tracked, so they stay`;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
/** `! <command>`: the user runs it themselves, where the model would be refused; the model sees what it printed. */
|
|
910
|
+
async function shell(command) {
|
|
911
|
+
stopper = new AbortController();
|
|
912
|
+
const run = await runCommand(command, { cwd: workspace().cwd, env: process.env, signal: stopper.signal });
|
|
913
|
+
const stopped = run.stopped === 'interrupted' ? `\n(${INTERRUPTED})` : '';
|
|
914
|
+
const output = run.error ?? cap(run.output.trimEnd(), undefined, run.omitted).toWellFormed() + stopped;
|
|
915
|
+
// The user sees what their command printed; the model is given what a tool would have given it — without secrets, and well-formed.
|
|
916
|
+
const told = run.error ?? capRedacted(run.output.trimEnd(), undefined, run.omitted).toWellFormed() + stopped;
|
|
917
|
+
settleTail();
|
|
918
|
+
params.messages.push({ role: 'user', content: [{ type: 'text', text: `I ran \`${redact(command).text}\` myself:\n${told || '(no output)'}` }] });
|
|
919
|
+
spoken = true;
|
|
920
|
+
persist();
|
|
921
|
+
return output;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/** A slash command is a sentence the user did not have to type; unknown ones are said back. A path is not one: the word after the slash stands alone. */
|
|
925
|
+
function expand(line) {
|
|
926
|
+
const m = /^\/(\w+)(?:\s+(.*))?$/s.exec(line);
|
|
927
|
+
if (!m) return { text: line };
|
|
928
|
+
const [, name, args = ''] = m;
|
|
929
|
+
if (name === 'exit' || name === 'quit') return { control: 'quit' };
|
|
930
|
+
if (name === 'new' || name === 'memory' || name === 'undo') return { control: name };
|
|
931
|
+
if (name === 'model' || name === 'resume' || name === 'mcp' || name === 'copy' || name === 'help' || name === 'stop') return { control: name, args: args.trim() };
|
|
932
|
+
const make = Object.hasOwn(COMMANDS, name) ? COMMANDS[name] : undefined;
|
|
933
|
+
return make ? { ...make(args.trim()), said: args.trim() } : { error: `no such command /${name} — one of: ${commands.map((c) => `/${c.name}`).join(' ')}` };
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/** `/mcp`: the servers as `sumo mcp` lists them. They connect once, while the first turn is typed, so an early look waits for them. */
|
|
937
|
+
async function mcp() {
|
|
938
|
+
await connecting;
|
|
939
|
+
return mcpLines(registry);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/** `/memory`: one page per chat, on this chat's store, gone when the chat is; asked for again, it is opened again. */
|
|
943
|
+
async function memoryPage() {
|
|
944
|
+
page ??= servePage(db, { unref: true }).catch((cause) => {
|
|
945
|
+
page = null;
|
|
946
|
+
throw cause;
|
|
947
|
+
});
|
|
948
|
+
const { url } = await page;
|
|
949
|
+
openInBrowser(url);
|
|
950
|
+
return `your memory, in the browser: ${url}`;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/** Where the work is, for the screen's margin: the directory the tools run in, the project's name for it, and the branch it is on. */
|
|
954
|
+
function where() {
|
|
955
|
+
const { cwd: at } = workspace();
|
|
956
|
+
return { cwd: at, name: project?.slug ?? basename(at), branch: branchOf(at) };
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/** What this session's requests have cost so far, the jobs it delegated included: the ledger's own sum. */
|
|
960
|
+
const spent = () => (sessionId ? db.prepare('SELECT COALESCE(SUM(cost_usd), 0) AS cost FROM model_runs WHERE session_id = ?').get(sessionId).cost : 0);
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* The session's cost said to the user the first time it passes `chat.budget` — on the screen, where a retry is said,
|
|
964
|
+
* and never to the model: what to spend is the user's to weigh. Asked after every reply, a delegated job's included.
|
|
965
|
+
*/
|
|
966
|
+
function overBudget() {
|
|
967
|
+
const budget = Number(configured(db, 'chat.budget'));
|
|
968
|
+
if (costSaid || !(budget > 0)) return;
|
|
969
|
+
const cost = spent();
|
|
970
|
+
if (cost < budget) return;
|
|
971
|
+
costSaid = true;
|
|
972
|
+
activity(budgetLine(cost));
|
|
973
|
+
watch({ type: 'budget', text: budgetLine(cost) });
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
function end() {
|
|
977
|
+
event('session-end', {});
|
|
978
|
+
if (lock) rmSync(lock, { force: true });
|
|
979
|
+
lock = null;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
return {
|
|
983
|
+
start,
|
|
984
|
+
say,
|
|
985
|
+
interrupt: () => stopper?.abort(),
|
|
986
|
+
/** Said to the chat while it works: read with the model's next request of this turn. */
|
|
987
|
+
steer: (text) => void steers.push(text),
|
|
988
|
+
unsteer,
|
|
989
|
+
wake,
|
|
990
|
+
stop,
|
|
991
|
+
settle,
|
|
992
|
+
/** The jobs at work in the background, by id. */
|
|
993
|
+
running: () => [...stops.keys()],
|
|
994
|
+
/** How many background jobs have reported and wait for a turn to read them. */
|
|
995
|
+
waiting,
|
|
996
|
+
where,
|
|
997
|
+
spent,
|
|
998
|
+
tell,
|
|
999
|
+
shell,
|
|
1000
|
+
undo,
|
|
1001
|
+
expand,
|
|
1002
|
+
memoryPage,
|
|
1003
|
+
mcp,
|
|
1004
|
+
route: setRoute,
|
|
1005
|
+
resume,
|
|
1006
|
+
/** The sessions that can be picked up, one line each, newest first. */
|
|
1007
|
+
saved: () => others().map((s) => sessionLine(s, now())),
|
|
1008
|
+
end,
|
|
1009
|
+
commands,
|
|
1010
|
+
get sessionId() {
|
|
1011
|
+
return sessionId;
|
|
1012
|
+
},
|
|
1013
|
+
get model() {
|
|
1014
|
+
return model;
|
|
1015
|
+
},
|
|
1016
|
+
get effort() {
|
|
1017
|
+
return effort;
|
|
1018
|
+
},
|
|
1019
|
+
get routed() {
|
|
1020
|
+
return routed;
|
|
1021
|
+
},
|
|
1022
|
+
get params() {
|
|
1023
|
+
return params;
|
|
1024
|
+
},
|
|
1025
|
+
get contextTokens() {
|
|
1026
|
+
return lastContext;
|
|
1027
|
+
},
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** The chat on a real terminal: the screen in ui.mjs, loaded only here so no other `sumo` command pays for it. */
|
|
1032
|
+
async function screen(db, { model, effort, resume }) {
|
|
1033
|
+
const { runUi } = await import('./ui.mjs');
|
|
1034
|
+
const events = new EventEmitter();
|
|
1035
|
+
const session = createChat(db, {
|
|
1036
|
+
model,
|
|
1037
|
+
effort,
|
|
1038
|
+
// On the screen a delegated job does not hold the turn: the user goes on working, and the report is read when it comes.
|
|
1039
|
+
background: true,
|
|
1040
|
+
out: (t) => events.emit('text', t),
|
|
1041
|
+
watch: (e) => events.emit(e.type, e),
|
|
1042
|
+
// A call waiting on the user's word goes to the screen, which answers it with a key.
|
|
1043
|
+
approve: (ask) => new Promise((resolve) => events.emit('approve', { ...ask, resolve })),
|
|
1044
|
+
// So does a question of the model's: its options are shown, and a pick or what is typed in the box answers it.
|
|
1045
|
+
ask: (question) => new Promise((resolve) => events.emit('ask', { ...question, resolve })),
|
|
1046
|
+
});
|
|
1047
|
+
try {
|
|
1048
|
+
await runUi({ session, events, resume, notify: configured(db, 'chat.notify') !== 'off' }).waitUntilExit();
|
|
1049
|
+
} finally {
|
|
1050
|
+
// Jobs still at work in the background are stopped with the chat, and let go of what they hold before it closes.
|
|
1051
|
+
await session.settle();
|
|
1052
|
+
session.end();
|
|
1053
|
+
await closeMcp();
|
|
1054
|
+
}
|
|
1055
|
+
return 0;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
/**
|
|
1059
|
+
* The terminal. With a keyboard and a screen it is the full-screen chat; piped,
|
|
1060
|
+
* it is plain lines: a header, the prompt with the model in it, the reply
|
|
1061
|
+
* rendered as it streams, tool lines dim, errors red, a rule between turns.
|
|
1062
|
+
*/
|
|
1063
|
+
export async function chat(db, { model, effort, resume = null, send, input = process.stdin, output = process.stdout } = {}) {
|
|
1064
|
+
if (input.isTTY && output.isTTY) return screen(db, { model, effort, resume });
|
|
1065
|
+
const rl = createInterface({ input, output });
|
|
1066
|
+
// Every line is a turn. Lines that arrive while one is being answered wait here for theirs; a question would hear only the next.
|
|
1067
|
+
const lines = rl[Symbol.asyncIterator]();
|
|
1068
|
+
const s = styles(colourEnabled(output));
|
|
1069
|
+
const width = widthOf(output);
|
|
1070
|
+
// Piped in is not piped out: what the model or a command prints may reach a terminal, and only colour gets through to it.
|
|
1071
|
+
// A carriage return redraws a progress bar in place; as plain lines each redraw is a line of its own, not run into the next.
|
|
1072
|
+
const write = (t) => output.write(safeForTerminal(String(t).replace(/\r(?!\n)/g, '\n')));
|
|
1073
|
+
const reply = createRenderer(write, s, { width });
|
|
1074
|
+
const session = createChat(db, {
|
|
1075
|
+
model,
|
|
1076
|
+
effort,
|
|
1077
|
+
send,
|
|
1078
|
+
out: (t) => reply.write(t),
|
|
1079
|
+
activity: (line) => {
|
|
1080
|
+
reply.flush();
|
|
1081
|
+
write(`\n${s.dim(` › ${line}`)}\n`);
|
|
1082
|
+
},
|
|
1083
|
+
// Piped, nobody answers a question: one of the model's is not asked, an MCP call not on the allow list is refused, and a server that failed is said.
|
|
1084
|
+
watch: (e) => {
|
|
1085
|
+
if (e.type === 'retry' && e.job === undefined) return write(`${s.dim(` › ${retryLine(e)}`)}\n`);
|
|
1086
|
+
if (e.type === 'sources') return write(`\n${e.sources.map((page) => s.dim(` ⎿ ${safeForTerminal(`${page.title} — ${page.url}`).replace(/\s+/g, ' ')}`)).join('\n')}\n`);
|
|
1087
|
+
if (e.type !== 'mcp') return;
|
|
1088
|
+
if (e.error) write(`${s.red(`mcp: ${e.error}`)}\n`);
|
|
1089
|
+
for (const server of e.servers.filter((x) => !x.ok)) write(`${s.red(`mcp ${server.name}: ${server.error}`)}\n`);
|
|
1090
|
+
},
|
|
1091
|
+
});
|
|
1092
|
+
const rule = () => write(`\n${s.dim('─'.repeat(width))}\n\n`);
|
|
1093
|
+
/** A line under what the user typed, as a tool's result is drawn: what a command did. */
|
|
1094
|
+
const result = (text) => write(`${s.dim(` ⎿ ${text}`)}\n`);
|
|
1095
|
+
/** The header, then the memory block of a fresh session or the line that says what a resumed one is. */
|
|
1096
|
+
const show = (block, after = '') => {
|
|
1097
|
+
write(`${header({ route: routeLine(session), cwd: process.cwd() }, s)}\n\n`);
|
|
1098
|
+
if (block) write(`${renderBlock(block, s, { width })}\n`);
|
|
1099
|
+
if (after) result(after);
|
|
1100
|
+
rule();
|
|
1101
|
+
};
|
|
1102
|
+
if (resume) show('', session.resume(resume));
|
|
1103
|
+
else show(session.start('startup'));
|
|
1104
|
+
try {
|
|
1105
|
+
for (;;) {
|
|
1106
|
+
write(prompt({ route: routeLine(session), contextTokens: session.contextTokens }, s));
|
|
1107
|
+
const next = await lines.next();
|
|
1108
|
+
if (next.done) break;
|
|
1109
|
+
const line = next.value.trim();
|
|
1110
|
+
if (!line) continue;
|
|
1111
|
+
// What a line cannot do is said in red, and the next line is read: a refused command does not end the chat.
|
|
1112
|
+
try {
|
|
1113
|
+
if (line.startsWith('!')) {
|
|
1114
|
+
write(`${s.dim(await session.shell(line.slice(1).trim()))}\n`);
|
|
1115
|
+
rule();
|
|
1116
|
+
continue;
|
|
1117
|
+
}
|
|
1118
|
+
const { text, said, guide, control, args, error } = session.expand(line);
|
|
1119
|
+
if (error) {
|
|
1120
|
+
write(`${s.red(error)}\n`);
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
if (control === 'quit') break;
|
|
1124
|
+
if (control === 'new') {
|
|
1125
|
+
session.end();
|
|
1126
|
+
write('\n');
|
|
1127
|
+
show(session.start('new'));
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
if (control === 'resume') {
|
|
1131
|
+
if (!args) {
|
|
1132
|
+
const saved = session.saved();
|
|
1133
|
+
write(`${s.dim(saved.length > 0 ? saved.map((l) => ` ${l}`).join('\n') : ' no saved sessions')}\n`);
|
|
1134
|
+
continue;
|
|
1135
|
+
}
|
|
1136
|
+
const resumed = session.resume(args);
|
|
1137
|
+
write('\n');
|
|
1138
|
+
show('', resumed);
|
|
1139
|
+
continue;
|
|
1140
|
+
}
|
|
1141
|
+
if (control === 'model' || control === 'memory' || control === 'mcp' || control === 'undo') {
|
|
1142
|
+
result(control === 'model' ? session.route(args) : control === 'undo' ? session.undo() : control === 'mcp' ? (await session.mcp()).join('\n ') : await session.memoryPage());
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
write('\n');
|
|
1146
|
+
const outcome = await session.say(text, said, [], { guide });
|
|
1147
|
+
reply.flush();
|
|
1148
|
+
if (outcome.stop === 'error') write(`\n${s.red(`error: ${outcome.error}`)}\n`);
|
|
1149
|
+
if (stopReason(outcome.stop)) write(`\n${s.dim(stopReason(outcome.stop))}\n`);
|
|
1150
|
+
rule();
|
|
1151
|
+
} catch (cause) {
|
|
1152
|
+
reply.flush();
|
|
1153
|
+
write(`${s.red(cause.message)}\n`);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
} finally {
|
|
1157
|
+
session.end();
|
|
1158
|
+
rl.close();
|
|
1159
|
+
await closeMcp();
|
|
1160
|
+
}
|
|
1161
|
+
return 0;
|
|
1162
|
+
}
|