@ai-launchpad-v1/expert-code 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -3
- package/dist/index.js +646 -77
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -67,14 +67,38 @@ expert --manual # approve every write/edit/command (default: auto)
|
|
|
67
67
|
Type a task at the `›` prompt in plain English — the agent explores, edits, and runs
|
|
68
68
|
commands, showing diffs and live output.
|
|
69
69
|
|
|
70
|
+
At the prompt:
|
|
71
|
+
|
|
72
|
+
- **↑ / ↓** — recall your previous tasks.
|
|
73
|
+
- **Tab** — complete a `/command`, or (at an empty prompt) accept the agent's suggested
|
|
74
|
+
next step, shown as a dim `next:` hint after a task finishes.
|
|
75
|
+
- **`@path`** — attach a file to the task, e.g. `explain @src/auth.ts and compare the
|
|
76
|
+
layout to @design/home.png`. Images and PDFs are read natively; text files are
|
|
77
|
+
inlined. A mention that isn't a real file is left as plain text.
|
|
78
|
+
|
|
79
|
+
In-session commands:
|
|
80
|
+
|
|
70
81
|
- **`/init`** — have it generate an `EXPERT.md` (project orientation it auto-loads on
|
|
71
82
|
every future task). Run once per project.
|
|
83
|
+
- **`/model [provider] [model]`** — show or switch the BYOK provider/model without
|
|
84
|
+
restarting; **`/login`** / **`/logout`** — switch to or from the managed lane.
|
|
85
|
+
- **`/diff`** — review the file changes applied this task; **`/revert`** — undo the most
|
|
86
|
+
recent one (restores the file, or deletes it if the change created it).
|
|
87
|
+
- **`/compact`** — summarize the conversation into a compact briefing to shrink the
|
|
88
|
+
context (the task continues; applied changes are kept). Optionally focus it:
|
|
89
|
+
`/compact keep the auth refactor details`.
|
|
90
|
+
- **`/clear`** — start a fresh task and clear the screen.
|
|
91
|
+
- **`/status`** — workspace, model, autonomy, change count, and token usage.
|
|
72
92
|
- **Approvals** (`auto` mode, the default): reads and file edits apply automatically;
|
|
73
93
|
risky/unknown commands pause — press `y` (run), `n` (skip), `a` (always allow this
|
|
74
94
|
exact command this session), or type a note to reject with guidance.
|
|
75
95
|
- **`--manual`** / `/manual` — approve *every* write, edit, and command.
|
|
76
96
|
- **Ctrl-C** once — stop the current task and return to the prompt.
|
|
77
|
-
-
|
|
97
|
+
- Also: `/auto`, `/help`, `/exit`.
|
|
98
|
+
|
|
99
|
+
Token usage (estimated) is shown after each task — `~N task · ~N session` — and in the
|
|
100
|
+
prompt frame and `/status`. It's a ~4-chars-per-token estimate across both lanes, for
|
|
101
|
+
gauging context size and cost, not exact billing.
|
|
78
102
|
|
|
79
103
|
## Commands
|
|
80
104
|
|
|
@@ -102,5 +126,5 @@ be overridden with `EXPERT_SUPABASE_URL` / `EXPERT_SUPABASE_ANON_KEY`.
|
|
|
102
126
|
|
|
103
127
|
## Not yet included
|
|
104
128
|
|
|
105
|
-
Session persistence/resume, MCP tools, "get a human code review"
|
|
106
|
-
|
|
129
|
+
Session persistence/resume, MCP tools, and "get a human code review" (all present in
|
|
130
|
+
the ExpertAI desktop app).
|
package/dist/index.js
CHANGED
|
@@ -1342,6 +1342,13 @@ async function writeFile(relPath, content) {
|
|
|
1342
1342
|
await mkdir(dirname(file), { recursive: true });
|
|
1343
1343
|
await fsWriteFile(file, content, "utf8");
|
|
1344
1344
|
}
|
|
1345
|
+
async function deleteFile(relPath) {
|
|
1346
|
+
const file = resolveInRoot(relPath, { write: true });
|
|
1347
|
+
const info = await stat(file).catch(() => null);
|
|
1348
|
+
if (!info) return;
|
|
1349
|
+
if (!info.isFile()) throw new Error("Not a file");
|
|
1350
|
+
await unlink(file);
|
|
1351
|
+
}
|
|
1345
1352
|
function countOccurrences(haystack, needle) {
|
|
1346
1353
|
if (!needle) return 0;
|
|
1347
1354
|
let n = 0;
|
|
@@ -1943,6 +1950,35 @@ function touchedPath(t) {
|
|
|
1943
1950
|
return null;
|
|
1944
1951
|
}
|
|
1945
1952
|
|
|
1953
|
+
// tokens.ts
|
|
1954
|
+
init_esm_shims();
|
|
1955
|
+
var CHARS_PER_TOKEN = 4;
|
|
1956
|
+
var MEDIA_TOKENS = 1500;
|
|
1957
|
+
function estimateTokens(s) {
|
|
1958
|
+
return Math.ceil(s.length / CHARS_PER_TOKEN);
|
|
1959
|
+
}
|
|
1960
|
+
function estimateMessagesTokens(messages) {
|
|
1961
|
+
let total = 0;
|
|
1962
|
+
for (const m of messages) {
|
|
1963
|
+
if (typeof m.content === "string") {
|
|
1964
|
+
total += estimateTokens(m.content);
|
|
1965
|
+
continue;
|
|
1966
|
+
}
|
|
1967
|
+
for (const b of m.content) {
|
|
1968
|
+
if (b.type === "text") total += estimateTokens(b.text);
|
|
1969
|
+
else if (b.type === "tool_use") total += estimateTokens(JSON.stringify(b.input ?? ""));
|
|
1970
|
+
else if (b.type === "tool_result") total += estimateTokens(b.content);
|
|
1971
|
+
else if (b.type === "image" || b.type === "document") total += MEDIA_TOKENS;
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
return total;
|
|
1975
|
+
}
|
|
1976
|
+
function humanizeTokens(n) {
|
|
1977
|
+
if (n < 1e3) return `${n}`;
|
|
1978
|
+
if (n < 1e6) return `${(n / 1e3).toFixed(n < 1e4 ? 1 : 0)}k`;
|
|
1979
|
+
return `${(n / 1e6).toFixed(1)}M`;
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1946
1982
|
// ../apps/desktop/src/lib/approval.ts
|
|
1947
1983
|
init_esm_shims();
|
|
1948
1984
|
var RISKY_COMMAND = /(\brm\b|\brmdir\b|\bdel\b|\brd\b|remove-item|\bmv\b|\bdd\b|\bmkfs\b|\bchmod\b|\bchown\b|\bsudo\b|\bshutdown\b|\breboot\b|:\(\)\s*\{|git\s+reset\s+--hard|git\s+clean|git\s+checkout\s+--|git\s+push|--force|force-with-lease|\bnpm\s+(i|install|ci|publish)\b|\byarn\s+(add|install)\b|\bpnpm\s+(add|install|i)\b|\bpip\s+install|\bpoetry\s+add|\bgem\s+install|\bcargo\s+add|\bgo\s+get\b|\bbrew\s+install|\bapt(-get)?\b|\bnpx\b|\bcurl\b|\bwget\b|\bssh\b|\bscp\b|\btelnet\b|\bnc\b|\bvercel\b|\bdeploy\b|\btee\b)/i;
|
|
@@ -1997,14 +2033,14 @@ function isRetryableStatus(status) {
|
|
|
1997
2033
|
return status === 0 || status === 429 || status >= 500;
|
|
1998
2034
|
}
|
|
1999
2035
|
function sleep(ms, signal) {
|
|
2000
|
-
return new Promise((
|
|
2001
|
-
if (signal.aborted) return
|
|
2002
|
-
const t = setTimeout(
|
|
2036
|
+
return new Promise((resolve3) => {
|
|
2037
|
+
if (signal.aborted) return resolve3();
|
|
2038
|
+
const t = setTimeout(resolve3, ms);
|
|
2003
2039
|
signal.addEventListener(
|
|
2004
2040
|
"abort",
|
|
2005
2041
|
() => {
|
|
2006
2042
|
clearTimeout(t);
|
|
2007
|
-
|
|
2043
|
+
resolve3();
|
|
2008
2044
|
},
|
|
2009
2045
|
{ once: true }
|
|
2010
2046
|
);
|
|
@@ -2049,6 +2085,13 @@ async function runAgentTask(transcript, callbacks, signal, model) {
|
|
|
2049
2085
|
return { transcript: messages, finalText, stopped: "error", error: errorMessage(e) };
|
|
2050
2086
|
}
|
|
2051
2087
|
if (signal.aborted) return { transcript: messages, finalText, stopped: "aborted" };
|
|
2088
|
+
if (callbacks.onTurnUsage) {
|
|
2089
|
+
const outputText = turn.text + turn.toolUses.map((t) => JSON.stringify(t.input ?? "")).join("");
|
|
2090
|
+
callbacks.onTurnUsage({
|
|
2091
|
+
inputTokens: estimateMessagesTokens(messages),
|
|
2092
|
+
outputTokens: estimateTokens(outputText)
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2052
2095
|
if (turn.toolUses.length > MAX_TOOL_USES_PER_TURN) {
|
|
2053
2096
|
return {
|
|
2054
2097
|
transcript: messages,
|
|
@@ -2127,12 +2170,136 @@ function errorMessage(e) {
|
|
|
2127
2170
|
return msg || "The coding agent is unavailable right now.";
|
|
2128
2171
|
}
|
|
2129
2172
|
|
|
2173
|
+
// attachments.ts
|
|
2174
|
+
init_esm_shims();
|
|
2175
|
+
import { readFile as readFile2, stat as stat2 } from "fs/promises";
|
|
2176
|
+
import { resolve as resolve2, basename as basename2, extname } from "path";
|
|
2177
|
+
var IMAGE_TYPES = {
|
|
2178
|
+
".png": "image/png",
|
|
2179
|
+
".jpg": "image/jpeg",
|
|
2180
|
+
".jpeg": "image/jpeg",
|
|
2181
|
+
".webp": "image/webp",
|
|
2182
|
+
".gif": "image/gif"
|
|
2183
|
+
};
|
|
2184
|
+
var MAX_MEDIA_BASE64 = 8 * 1024 * 1024;
|
|
2185
|
+
var MAX_FILE_BYTES2 = 5 * 1024 * 1024;
|
|
2186
|
+
var MAX_TEXT_BYTES = 256 * 1024;
|
|
2187
|
+
var TEXT_EXTS = /* @__PURE__ */ new Set([
|
|
2188
|
+
".txt",
|
|
2189
|
+
".md",
|
|
2190
|
+
".markdown",
|
|
2191
|
+
".csv",
|
|
2192
|
+
".tsv",
|
|
2193
|
+
".json",
|
|
2194
|
+
".yaml",
|
|
2195
|
+
".yml",
|
|
2196
|
+
".log"
|
|
2197
|
+
]);
|
|
2198
|
+
function extractMentions(text) {
|
|
2199
|
+
const out2 = [];
|
|
2200
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2201
|
+
const re = /(?:^|[\s(])@([^\s()]+)/g;
|
|
2202
|
+
let m;
|
|
2203
|
+
while ((m = re.exec(text)) !== null) {
|
|
2204
|
+
const raw = m[1].replace(/[.,;:]+$/, "");
|
|
2205
|
+
if (raw && !seen.has(raw)) {
|
|
2206
|
+
seen.add(raw);
|
|
2207
|
+
out2.push(raw);
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
return out2;
|
|
2211
|
+
}
|
|
2212
|
+
function mediaTypeForPath(path2) {
|
|
2213
|
+
const ext = extname(path2).toLowerCase();
|
|
2214
|
+
if (ext === ".pdf") return "application/pdf";
|
|
2215
|
+
return IMAGE_TYPES[ext] ?? null;
|
|
2216
|
+
}
|
|
2217
|
+
function isInlineText(path2) {
|
|
2218
|
+
return TEXT_EXTS.has(extname(path2).toLowerCase());
|
|
2219
|
+
}
|
|
2220
|
+
async function buildTaskContent(text, cwd) {
|
|
2221
|
+
const mentions = extractMentions(text);
|
|
2222
|
+
if (mentions.length === 0) return { content: text, notes: [] };
|
|
2223
|
+
const blocks = [];
|
|
2224
|
+
const inlined = [];
|
|
2225
|
+
const notes = [];
|
|
2226
|
+
let mediaBase64 = 0;
|
|
2227
|
+
for (const mention of mentions) {
|
|
2228
|
+
const abs = resolve2(cwd, mention);
|
|
2229
|
+
let bytes;
|
|
2230
|
+
try {
|
|
2231
|
+
const info = await stat2(abs);
|
|
2232
|
+
if (!info.isFile()) continue;
|
|
2233
|
+
bytes = await readFile2(abs);
|
|
2234
|
+
} catch {
|
|
2235
|
+
continue;
|
|
2236
|
+
}
|
|
2237
|
+
const name = basename2(abs);
|
|
2238
|
+
const mediaType = mediaTypeForPath(abs);
|
|
2239
|
+
if (mediaType) {
|
|
2240
|
+
if (bytes.length > MAX_FILE_BYTES2) {
|
|
2241
|
+
notes.push(`skipped ${name} (too large, limit ${Math.round(MAX_FILE_BYTES2 / 1024 / 1024)}MB)`);
|
|
2242
|
+
continue;
|
|
2243
|
+
}
|
|
2244
|
+
const data = bytes.toString("base64");
|
|
2245
|
+
if (mediaBase64 + data.length > MAX_MEDIA_BASE64) {
|
|
2246
|
+
notes.push(`skipped ${name} (attachment budget exceeded)`);
|
|
2247
|
+
continue;
|
|
2248
|
+
}
|
|
2249
|
+
mediaBase64 += data.length;
|
|
2250
|
+
blocks.push({
|
|
2251
|
+
type: mediaType === "application/pdf" ? "document" : "image",
|
|
2252
|
+
source: { type: "base64", media_type: mediaType, data }
|
|
2253
|
+
});
|
|
2254
|
+
notes.push(`attached ${name}`);
|
|
2255
|
+
} else if (isInlineText(abs)) {
|
|
2256
|
+
if (bytes.length > MAX_TEXT_BYTES) {
|
|
2257
|
+
notes.push(`skipped ${name} (text too large, limit ${Math.round(MAX_TEXT_BYTES / 1024)}KB)`);
|
|
2258
|
+
continue;
|
|
2259
|
+
}
|
|
2260
|
+
const lang = extname(abs).slice(1);
|
|
2261
|
+
inlined.push(`
|
|
2262
|
+
|
|
2263
|
+
--- ${mention} ---
|
|
2264
|
+
\`\`\`${lang}
|
|
2265
|
+
${bytes.toString("utf8")}
|
|
2266
|
+
\`\`\``);
|
|
2267
|
+
notes.push(`inlined ${name}`);
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
const fullText = inlined.length ? text + inlined.join("") : text;
|
|
2271
|
+
if (blocks.length === 0) return { content: fullText, notes };
|
|
2272
|
+
return { content: [{ type: "text", text: fullText }, ...blocks], notes };
|
|
2273
|
+
}
|
|
2274
|
+
function stripStaleMedia(messages) {
|
|
2275
|
+
const lastWithMedia = (() => {
|
|
2276
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
2277
|
+
const c2 = messages[i].content;
|
|
2278
|
+
if (Array.isArray(c2) && c2.some((b) => b.type === "image" || b.type === "document")) return i;
|
|
2279
|
+
}
|
|
2280
|
+
return -1;
|
|
2281
|
+
})();
|
|
2282
|
+
if (lastWithMedia === -1) return messages;
|
|
2283
|
+
return messages.map((msg, i) => {
|
|
2284
|
+
if (i === lastWithMedia || typeof msg.content === "string") return msg;
|
|
2285
|
+
if (!msg.content.some((b) => b.type === "image" || b.type === "document")) return msg;
|
|
2286
|
+
const kept = msg.content.filter((b) => b.type !== "image" && b.type !== "document");
|
|
2287
|
+
return { ...msg, content: kept };
|
|
2288
|
+
});
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2130
2291
|
// ui.ts
|
|
2131
2292
|
init_esm_shims();
|
|
2132
2293
|
var useColor = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
2294
|
+
var truecolor = /truecolor|24bit/i.test(process.env.COLORTERM || "");
|
|
2133
2295
|
function wrap(code, s) {
|
|
2134
2296
|
return useColor ? `\x1B[${code}m${s}\x1B[0m` : s;
|
|
2135
2297
|
}
|
|
2298
|
+
function brandOrange(s) {
|
|
2299
|
+
if (!useColor) return s;
|
|
2300
|
+
const seq = truecolor ? "38;2;242;106;31" : "38;5;208";
|
|
2301
|
+
return `\x1B[${seq}m${s}\x1B[0m`;
|
|
2302
|
+
}
|
|
2136
2303
|
var c = {
|
|
2137
2304
|
dim: (s) => wrap(2, s),
|
|
2138
2305
|
bold: (s) => wrap(1, s),
|
|
@@ -2142,30 +2309,92 @@ var c = {
|
|
|
2142
2309
|
blue: (s) => wrap(34, s),
|
|
2143
2310
|
magenta: (s) => wrap(35, s),
|
|
2144
2311
|
cyan: (s) => wrap(36, s),
|
|
2145
|
-
gray: (s) => wrap(90, s)
|
|
2312
|
+
gray: (s) => wrap(90, s),
|
|
2313
|
+
/** The ExpertAI brand accent — use for the masthead, prompt, and primary actions. */
|
|
2314
|
+
orange: brandOrange
|
|
2146
2315
|
};
|
|
2316
|
+
function wordmark(suffix, prefix = "Expert") {
|
|
2317
|
+
return c.bold(prefix) + c.bold(c.orange(suffix));
|
|
2318
|
+
}
|
|
2319
|
+
function clean(s) {
|
|
2320
|
+
return s.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "");
|
|
2321
|
+
}
|
|
2147
2322
|
function out(s = "") {
|
|
2148
2323
|
process.stdout.write(s);
|
|
2149
2324
|
}
|
|
2150
2325
|
function line(s = "") {
|
|
2151
2326
|
process.stdout.write(s + "\n");
|
|
2152
2327
|
}
|
|
2328
|
+
function termWidth() {
|
|
2329
|
+
const cols = process.stdout.columns || 0;
|
|
2330
|
+
if (!cols) return 64;
|
|
2331
|
+
return Math.max(32, Math.min(cols - 1, 80));
|
|
2332
|
+
}
|
|
2333
|
+
var PROMPT = useColor ? `${c.dim("\u2502")} ${c.bold(c.orange("\u203A"))} ` : "> ";
|
|
2334
|
+
function promptTop(right = "") {
|
|
2335
|
+
const w = termWidth();
|
|
2336
|
+
const head = "\u256D\u2500 ";
|
|
2337
|
+
const label = "you";
|
|
2338
|
+
const rightText = right ? ` ${right} ` : "";
|
|
2339
|
+
const used = head.length + label.length + 1 + rightText.length;
|
|
2340
|
+
const fill = Math.max(0, w - used);
|
|
2341
|
+
const bar = head + label + " " + "\u2500".repeat(fill) + rightText;
|
|
2342
|
+
line(useColor ? c.dim(bar.slice(0, bar.length - rightText.length)) + c.gray(rightText) : bar);
|
|
2343
|
+
}
|
|
2344
|
+
function promptBottom() {
|
|
2345
|
+
line(c.dim("\u2570" + "\u2500".repeat(Math.max(1, termWidth() - 1))));
|
|
2346
|
+
}
|
|
2347
|
+
function createSpinner() {
|
|
2348
|
+
const frames = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
2349
|
+
let i = 0;
|
|
2350
|
+
let timer = null;
|
|
2351
|
+
let text = "working";
|
|
2352
|
+
return {
|
|
2353
|
+
start(label = "working") {
|
|
2354
|
+
if (!useColor || timer) return;
|
|
2355
|
+
text = label;
|
|
2356
|
+
process.stdout.write("\x1B[?25l");
|
|
2357
|
+
const render = () => {
|
|
2358
|
+
i = (i + 1) % frames.length;
|
|
2359
|
+
process.stdout.write(`\r ${c.orange(frames[i])} ${c.dim(text + "\u2026")}\x1B[K`);
|
|
2360
|
+
};
|
|
2361
|
+
render();
|
|
2362
|
+
timer = setInterval(render, 90);
|
|
2363
|
+
},
|
|
2364
|
+
stop() {
|
|
2365
|
+
if (!timer) return;
|
|
2366
|
+
clearInterval(timer);
|
|
2367
|
+
timer = null;
|
|
2368
|
+
process.stdout.write("\r\x1B[2K\x1B[?25h");
|
|
2369
|
+
}
|
|
2370
|
+
};
|
|
2371
|
+
}
|
|
2372
|
+
function showCursor() {
|
|
2373
|
+
if (useColor) process.stdout.write("\x1B[?25h");
|
|
2374
|
+
}
|
|
2375
|
+
function clearScreen() {
|
|
2376
|
+
if (process.stdout.isTTY) process.stdout.write("\x1B[2J\x1B[3J\x1B[H");
|
|
2377
|
+
}
|
|
2378
|
+
function notice(s) {
|
|
2379
|
+
line(c.dim(" " + s));
|
|
2380
|
+
}
|
|
2153
2381
|
var PLAN_GLYPH = {
|
|
2154
2382
|
pending: "\u25CB",
|
|
2155
2383
|
in_progress: "\u25D0",
|
|
2156
2384
|
done: "\u25CF"
|
|
2157
2385
|
};
|
|
2158
2386
|
function toolStartLine(t) {
|
|
2159
|
-
const label = c.
|
|
2160
|
-
const desc = describeToolUse(t);
|
|
2161
|
-
return `${c.
|
|
2387
|
+
const label = c.orange(labelForTool(t.name).padEnd(6));
|
|
2388
|
+
const desc = clean(describeToolUse(t));
|
|
2389
|
+
return `${c.orange("\xB7")} ${label} ${c.gray(desc)}`;
|
|
2162
2390
|
}
|
|
2163
2391
|
function printToolResult(t, r) {
|
|
2164
2392
|
if (t.name === "update_plan") {
|
|
2165
2393
|
const steps = parsePlan(t) ?? [];
|
|
2166
2394
|
for (const s of steps) {
|
|
2167
2395
|
const g = s.status === "done" ? c.green(PLAN_GLYPH.done) : s.status === "in_progress" ? c.yellow(PLAN_GLYPH.in_progress) : c.gray(PLAN_GLYPH.pending);
|
|
2168
|
-
|
|
2396
|
+
const title = clean(s.title);
|
|
2397
|
+
line(` ${g} ${s.status === "done" ? c.dim(title) : title}`);
|
|
2169
2398
|
}
|
|
2170
2399
|
return;
|
|
2171
2400
|
}
|
|
@@ -2174,11 +2403,11 @@ function printToolResult(t, r) {
|
|
|
2174
2403
|
return;
|
|
2175
2404
|
}
|
|
2176
2405
|
if (r.isError) {
|
|
2177
|
-
line(` ${c.red("\u2717")} ${c.red(firstLine(r.content))}`);
|
|
2406
|
+
line(` ${c.red("\u2717")} ${c.red(clean(firstLine(r.content)))}`);
|
|
2178
2407
|
return;
|
|
2179
2408
|
}
|
|
2180
2409
|
const summary = resultSummary(t, r.content);
|
|
2181
|
-
if (summary) line(` ${c.gray(summary)}`);
|
|
2410
|
+
if (summary) line(` ${c.gray(clean(summary))}`);
|
|
2182
2411
|
}
|
|
2183
2412
|
function firstLine(s) {
|
|
2184
2413
|
const i = s.indexOf("\n");
|
|
@@ -2201,12 +2430,11 @@ var MAX_DIFF_LINES = 80;
|
|
|
2201
2430
|
function printDiff(change) {
|
|
2202
2431
|
const beforeLines = change.before === null ? [] : change.before.split("\n");
|
|
2203
2432
|
const afterLines = change.after.split("\n");
|
|
2433
|
+
const path2 = clean(change.path);
|
|
2204
2434
|
if (change.before === null) {
|
|
2205
|
-
line(
|
|
2206
|
-
` ${c.green("+")} ${c.bold(change.path)} ${c.dim(`(new file, ${afterLines.length} lines)`)}`
|
|
2207
|
-
);
|
|
2435
|
+
line(` ${c.green("+")} ${c.bold(path2)} ${c.dim(`(new file, ${afterLines.length} lines)`)}`);
|
|
2208
2436
|
} else {
|
|
2209
|
-
line(` ${c.dim("\xB1")} ${c.bold(
|
|
2437
|
+
line(` ${c.dim("\xB1")} ${c.bold(path2)}`);
|
|
2210
2438
|
}
|
|
2211
2439
|
let p = 0;
|
|
2212
2440
|
while (p < beforeLines.length && p < afterLines.length && beforeLines[p] === afterLines[p]) p++;
|
|
@@ -2221,7 +2449,7 @@ function printDiff(change) {
|
|
|
2221
2449
|
line(` ${c.dim("\u2026")}`);
|
|
2222
2450
|
break;
|
|
2223
2451
|
}
|
|
2224
|
-
line(` ${c.red("- " + l)}`);
|
|
2452
|
+
line(` ${c.red("- " + clean(l))}`);
|
|
2225
2453
|
}
|
|
2226
2454
|
shown = 0;
|
|
2227
2455
|
for (const l of added) {
|
|
@@ -2229,43 +2457,60 @@ function printDiff(change) {
|
|
|
2229
2457
|
line(` ${c.dim("\u2026")}`);
|
|
2230
2458
|
break;
|
|
2231
2459
|
}
|
|
2232
|
-
line(` ${c.green("+ " + l)}`);
|
|
2460
|
+
line(` ${c.green("+ " + clean(l))}`);
|
|
2233
2461
|
}
|
|
2234
2462
|
}
|
|
2235
2463
|
function printApprovalRequest(t) {
|
|
2236
2464
|
line();
|
|
2237
2465
|
line(c.yellow(c.bold(" \u26A0 Approval needed")));
|
|
2466
|
+
const desc = clean(describeToolUse(t));
|
|
2238
2467
|
if (t.name === "run_command") {
|
|
2239
|
-
line(` ${c.bold("run")} ${c.
|
|
2468
|
+
line(` ${c.bold("run")} ${c.orange(desc)}`);
|
|
2240
2469
|
} else if (t.name === "write_file") {
|
|
2241
|
-
line(` ${c.bold("write")} ${c.
|
|
2470
|
+
line(` ${c.bold("write")} ${c.orange(desc)}`);
|
|
2242
2471
|
} else if (t.name === "edit_file") {
|
|
2243
|
-
line(` ${c.bold("edit")} ${c.
|
|
2472
|
+
line(` ${c.bold("edit")} ${c.orange(desc)}`);
|
|
2244
2473
|
} else {
|
|
2245
|
-
line(` ${c.bold(labelForTool(t.name))} ${c.
|
|
2474
|
+
line(` ${c.bold(labelForTool(t.name))} ${c.orange(desc)}`);
|
|
2246
2475
|
}
|
|
2247
2476
|
}
|
|
2248
|
-
function
|
|
2477
|
+
function masthead() {
|
|
2478
|
+
const product = c.bold("Expert") + " " + c.bold(c.orange("Code"));
|
|
2249
2479
|
line();
|
|
2250
|
-
line(c.
|
|
2480
|
+
line(` ${c.orange("\u259A\u2598")} ${product} ${c.dim("\xB7")} ${c.dim("by ") + wordmark("AI")}`);
|
|
2481
|
+
line(` ${c.orange("\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500")}`);
|
|
2482
|
+
}
|
|
2483
|
+
function banner(root, name, laneLabel, mode) {
|
|
2484
|
+
masthead();
|
|
2251
2485
|
line(c.dim(` workspace ${name} ${c.gray(root)}`));
|
|
2252
2486
|
line(c.dim(` model ${laneLabel}`));
|
|
2253
2487
|
line(c.dim(` autonomy ${mode}`));
|
|
2254
2488
|
line();
|
|
2255
|
-
line(
|
|
2256
|
-
|
|
2257
|
-
);
|
|
2489
|
+
line(c.dim(" Type a task and press Enter. \u2191 recalls past tasks \xB7 Tab completes \xB7 @path attaches"));
|
|
2490
|
+
line(c.dim(" Commands: /help /init /model /diff /compact /clear /status /exit"));
|
|
2258
2491
|
line();
|
|
2259
2492
|
}
|
|
2260
2493
|
function help() {
|
|
2261
2494
|
line();
|
|
2262
2495
|
line(c.bold(" Commands"));
|
|
2263
|
-
line(` ${c.
|
|
2264
|
-
line(` ${c.
|
|
2265
|
-
line(` ${c.
|
|
2266
|
-
line(` ${c.
|
|
2267
|
-
line(` ${c.
|
|
2268
|
-
line(` ${c.
|
|
2496
|
+
line(` ${c.orange("/help")} show this help`);
|
|
2497
|
+
line(` ${c.orange("/init")} explore the project and write (or refresh) its EXPERT.md`);
|
|
2498
|
+
line(` ${c.orange("/model")} show or switch the BYOK provider \u2014 /model <provider> [model]`);
|
|
2499
|
+
line(` ${c.orange("/login")} sign in to the hosted backend (managed lane)`);
|
|
2500
|
+
line(` ${c.orange("/logout")} sign out and switch back to your own key (BYOK)`);
|
|
2501
|
+
line(` ${c.orange("/diff")} review the file changes applied this task`);
|
|
2502
|
+
line(` ${c.orange("/revert")} undo the most recent applied change`);
|
|
2503
|
+
line(` ${c.orange("/status")} show workspace, model, autonomy, changes and token usage`);
|
|
2504
|
+
line(` ${c.orange("/auto")} autonomy: auto \u2014 safe reads + edits run; risky commands ask`);
|
|
2505
|
+
line(` ${c.orange("/manual")} autonomy: manual \u2014 every write/edit/command asks first`);
|
|
2506
|
+
line(` ${c.orange("/compact")} summarize the conversation to shrink the context (keeps the task)`);
|
|
2507
|
+
line(` ${c.orange("/clear")} start a fresh task and clear the screen`);
|
|
2508
|
+
line(` ${c.orange("/exit")} quit (or Ctrl-D)`);
|
|
2509
|
+
line();
|
|
2510
|
+
line(c.bold(" At the prompt"));
|
|
2511
|
+
line(c.dim(" \u2191 / \u2193 recall previous tasks"));
|
|
2512
|
+
line(c.dim(" Tab complete a /command, or accept the agent\u2019s suggested next step"));
|
|
2513
|
+
line(c.dim(" @path attach a file to the task (image/PDF read natively; text inlined)"));
|
|
2269
2514
|
line();
|
|
2270
2515
|
line(c.dim(" During a task, press Ctrl-C once to stop it and return to the prompt."));
|
|
2271
2516
|
line(c.dim(" At an approval prompt: y = run, n = skip, a = always allow this exact command,"));
|
|
@@ -2281,6 +2526,11 @@ function withExpertMd(expertMd, task) {
|
|
|
2281
2526
|
|
|
2282
2527
|
${expertMd}${EXPERT_MD_TASK_MARKER}${task}`;
|
|
2283
2528
|
}
|
|
2529
|
+
function applyPreamble(content, expertMd, isFirst) {
|
|
2530
|
+
if (!isFirst || !expertMd) return content;
|
|
2531
|
+
if (typeof content === "string") return withExpertMd(expertMd, content);
|
|
2532
|
+
return content.map((b) => b.type === "text" ? { ...b, text: withExpertMd(expertMd, b.text) } : b);
|
|
2533
|
+
}
|
|
2284
2534
|
function extractNextAction(text) {
|
|
2285
2535
|
const lines = text.split("\n");
|
|
2286
2536
|
let last = lines.length - 1;
|
|
@@ -2289,65 +2539,102 @@ function extractNextAction(text) {
|
|
|
2289
2539
|
const m = lines[last].match(/^NEXT:\s*(.+)$/);
|
|
2290
2540
|
return { next: m ? m[1].trim() : null };
|
|
2291
2541
|
}
|
|
2292
|
-
|
|
2542
|
+
var SLASH_COMMANDS = [
|
|
2543
|
+
"/help",
|
|
2544
|
+
"/init",
|
|
2545
|
+
"/auto",
|
|
2546
|
+
"/manual",
|
|
2547
|
+
"/clear",
|
|
2548
|
+
"/compact",
|
|
2549
|
+
"/login",
|
|
2550
|
+
"/logout",
|
|
2551
|
+
"/model",
|
|
2552
|
+
"/diff",
|
|
2553
|
+
"/revert",
|
|
2554
|
+
"/status",
|
|
2555
|
+
"/exit"
|
|
2556
|
+
];
|
|
2557
|
+
async function runRepl({ lane, workspace, manual }) {
|
|
2293
2558
|
let mode = manual ? "manual" : "auto";
|
|
2294
2559
|
const alwaysAllow = /* @__PURE__ */ new Set();
|
|
2295
|
-
const
|
|
2560
|
+
const changes = [];
|
|
2561
|
+
let pendingNext = null;
|
|
2562
|
+
let taskTokens = 0;
|
|
2563
|
+
let sessionTokens = 0;
|
|
2564
|
+
const completer = (lineStr) => {
|
|
2565
|
+
if (lineStr === "" && pendingNext) return [[pendingNext], ""];
|
|
2566
|
+
if (lineStr.startsWith("/")) {
|
|
2567
|
+
const hits = SLASH_COMMANDS.filter((cmd) => cmd.startsWith(lineStr));
|
|
2568
|
+
return [hits.length ? hits : SLASH_COMMANDS, lineStr];
|
|
2569
|
+
}
|
|
2570
|
+
return [[], lineStr];
|
|
2571
|
+
};
|
|
2572
|
+
const rl = readline.createInterface({
|
|
2573
|
+
input: process.stdin,
|
|
2574
|
+
output: process.stdout,
|
|
2575
|
+
completer,
|
|
2576
|
+
historySize: 100,
|
|
2577
|
+
removeHistoryDuplicates: true
|
|
2578
|
+
});
|
|
2296
2579
|
const ask = (q) => new Promise((res) => rl.question(q, (a) => res(a)));
|
|
2580
|
+
const askTransient = async (q) => {
|
|
2581
|
+
const a = await ask(q);
|
|
2582
|
+
const h = rl.history;
|
|
2583
|
+
if (h && h.length && h[0] === a) h.shift();
|
|
2584
|
+
return a;
|
|
2585
|
+
};
|
|
2586
|
+
const spinner = createSpinner();
|
|
2297
2587
|
let activeAbort = null;
|
|
2298
2588
|
rl.on("SIGINT", () => {
|
|
2299
2589
|
if (activeAbort) {
|
|
2300
2590
|
activeAbort.abort();
|
|
2301
2591
|
killAllCommands();
|
|
2592
|
+
spinner.stop();
|
|
2302
2593
|
line(c.yellow("\n Stopping the current task\u2026"));
|
|
2303
2594
|
} else {
|
|
2595
|
+
showCursor();
|
|
2304
2596
|
line();
|
|
2305
2597
|
rl.close();
|
|
2306
2598
|
}
|
|
2307
2599
|
});
|
|
2308
2600
|
rl.on("close", () => {
|
|
2601
|
+
spinner.stop();
|
|
2602
|
+
showCursor();
|
|
2309
2603
|
line(c.dim("\n Bye."));
|
|
2310
2604
|
process.exit(0);
|
|
2311
2605
|
});
|
|
2312
|
-
banner(workspace.root, workspace.name,
|
|
2606
|
+
banner(workspace.root, workspace.name, lane.client.label, mode);
|
|
2313
2607
|
let expertMd = await readExpertMd();
|
|
2314
2608
|
if (expertMd) line(c.dim(" Loaded EXPERT.md for project context.\n"));
|
|
2315
2609
|
else line(c.dim(" No EXPERT.md yet \u2014 type /init to generate one for this project.\n"));
|
|
2316
2610
|
let transcript = [];
|
|
2317
2611
|
for (; ; ) {
|
|
2318
|
-
|
|
2612
|
+
promptTop(sessionTokens ? `${mode} \xB7 ~${humanizeTokens(sessionTokens)} tok` : mode);
|
|
2613
|
+
const input = (await ask(PROMPT)).trim();
|
|
2614
|
+
promptBottom();
|
|
2615
|
+
pendingNext = null;
|
|
2319
2616
|
if (!input) continue;
|
|
2320
|
-
if (input
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
help();
|
|
2326
|
-
continue;
|
|
2327
|
-
}
|
|
2328
|
-
if (input === "/auto") {
|
|
2329
|
-
mode = "auto";
|
|
2330
|
-
line(c.dim(" autonomy: auto\n"));
|
|
2331
|
-
continue;
|
|
2332
|
-
}
|
|
2333
|
-
if (input === "/manual") {
|
|
2334
|
-
mode = "manual";
|
|
2335
|
-
line(c.dim(" autonomy: manual\n"));
|
|
2336
|
-
continue;
|
|
2337
|
-
}
|
|
2338
|
-
if (input === "/clear") {
|
|
2339
|
-
transcript = [];
|
|
2340
|
-
line(c.dim(" Started a fresh task.\n"));
|
|
2341
|
-
continue;
|
|
2342
|
-
}
|
|
2343
|
-
const isInit = input === "/init";
|
|
2344
|
-
if (!isInit && input.startsWith("/")) {
|
|
2617
|
+
if (input.startsWith("/") && input !== "/init") {
|
|
2618
|
+
const [cmd, ...rest] = input.split(/\s+/);
|
|
2619
|
+
const handled = await handleSlash(cmd, rest);
|
|
2620
|
+
if (handled === "exit") return;
|
|
2621
|
+
if (handled) continue;
|
|
2345
2622
|
line(c.yellow(` Unknown command: ${input}. Try /help
|
|
2346
2623
|
`));
|
|
2347
2624
|
continue;
|
|
2348
2625
|
}
|
|
2349
|
-
const
|
|
2350
|
-
|
|
2626
|
+
const isInit = input === "/init";
|
|
2627
|
+
const isFirst = transcript.length === 0;
|
|
2628
|
+
let content;
|
|
2629
|
+
if (isInit) {
|
|
2630
|
+
content = buildInitTask(expertMd);
|
|
2631
|
+
} else {
|
|
2632
|
+
const built = await buildTaskContent(input, workspace.root);
|
|
2633
|
+
if (built.notes.length) notice(built.notes.join(" \xB7 "));
|
|
2634
|
+
content = applyPreamble(built.content, expertMd, isFirst);
|
|
2635
|
+
}
|
|
2636
|
+
transcript = stripStaleMedia(transcript);
|
|
2637
|
+
transcript.push({ role: "user", content });
|
|
2351
2638
|
const abort = new AbortController();
|
|
2352
2639
|
activeAbort = abort;
|
|
2353
2640
|
let streaming = false;
|
|
@@ -2357,20 +2644,24 @@ async function runRepl({ model, workspace, manual }) {
|
|
|
2357
2644
|
streaming = false;
|
|
2358
2645
|
}
|
|
2359
2646
|
};
|
|
2647
|
+
spinner.start("working");
|
|
2360
2648
|
const result = await runAgentTask(
|
|
2361
2649
|
transcript,
|
|
2362
2650
|
{
|
|
2363
2651
|
onAssistantTextDelta: (delta) => {
|
|
2652
|
+
spinner.stop();
|
|
2364
2653
|
streaming = true;
|
|
2365
|
-
out(delta);
|
|
2654
|
+
out(clean(delta));
|
|
2366
2655
|
},
|
|
2367
2656
|
onAssistantTextReset: () => {
|
|
2657
|
+
spinner.stop();
|
|
2368
2658
|
if (streaming) {
|
|
2369
2659
|
out("\n");
|
|
2370
2660
|
streaming = false;
|
|
2371
2661
|
}
|
|
2372
2662
|
},
|
|
2373
2663
|
onAssistantText: () => {
|
|
2664
|
+
spinner.stop();
|
|
2374
2665
|
endStream();
|
|
2375
2666
|
},
|
|
2376
2667
|
requiresApproval: (tool) => {
|
|
@@ -2382,18 +2673,23 @@ async function runRepl({ model, workspace, manual }) {
|
|
|
2382
2673
|
return true;
|
|
2383
2674
|
},
|
|
2384
2675
|
onToolStart: (tool) => {
|
|
2676
|
+
spinner.stop();
|
|
2385
2677
|
endStream();
|
|
2386
2678
|
line(toolStartLine(tool));
|
|
2387
2679
|
},
|
|
2388
2680
|
requestApproval: (tool) => {
|
|
2681
|
+
spinner.stop();
|
|
2389
2682
|
endStream();
|
|
2390
2683
|
return promptApproval(tool);
|
|
2391
2684
|
},
|
|
2392
2685
|
onCommandData: (_tool, _stream, chunk) => {
|
|
2393
|
-
|
|
2686
|
+
spinner.stop();
|
|
2687
|
+
out(c.gray(clean(chunk)));
|
|
2394
2688
|
},
|
|
2395
2689
|
onToolResult: (tool, r) => {
|
|
2396
2690
|
printToolResult(tool, r);
|
|
2691
|
+
if (r.change) changes.push(r.change);
|
|
2692
|
+
spinner.start("working");
|
|
2397
2693
|
},
|
|
2398
2694
|
onFileTouched: (path2) => {
|
|
2399
2695
|
if (path2 === "EXPERT.md") {
|
|
@@ -2402,11 +2698,17 @@ async function runRepl({ model, workspace, manual }) {
|
|
|
2402
2698
|
}).catch(() => {
|
|
2403
2699
|
});
|
|
2404
2700
|
}
|
|
2701
|
+
},
|
|
2702
|
+
onTurnUsage: (u) => {
|
|
2703
|
+
const t = u.inputTokens + u.outputTokens;
|
|
2704
|
+
taskTokens += t;
|
|
2705
|
+
sessionTokens += t;
|
|
2405
2706
|
}
|
|
2406
2707
|
},
|
|
2407
2708
|
abort.signal,
|
|
2408
|
-
|
|
2709
|
+
lane.client
|
|
2409
2710
|
);
|
|
2711
|
+
spinner.stop();
|
|
2410
2712
|
endStream();
|
|
2411
2713
|
activeAbort = null;
|
|
2412
2714
|
transcript = result.transcript;
|
|
@@ -2426,20 +2728,228 @@ async function runRepl({ model, workspace, manual }) {
|
|
|
2426
2728
|
);
|
|
2427
2729
|
} else {
|
|
2428
2730
|
const { next } = extractNextAction(result.finalText);
|
|
2429
|
-
if (next)
|
|
2731
|
+
if (next) {
|
|
2732
|
+
pendingNext = next;
|
|
2733
|
+
line(c.dim(`
|
|
2430
2734
|
next: ${next}`));
|
|
2735
|
+
line(c.dim(" (press Tab at the prompt to accept)"));
|
|
2736
|
+
}
|
|
2431
2737
|
line();
|
|
2432
2738
|
}
|
|
2739
|
+
if (taskTokens > 0) {
|
|
2740
|
+
notice(
|
|
2741
|
+
`tokens ~${humanizeTokens(taskTokens)} task \xB7 ~${humanizeTokens(sessionTokens)} session
|
|
2742
|
+
`
|
|
2743
|
+
);
|
|
2744
|
+
}
|
|
2433
2745
|
}
|
|
2434
2746
|
function commandOf2(tool) {
|
|
2435
2747
|
const rec = tool.input && typeof tool.input === "object" ? tool.input : {};
|
|
2436
2748
|
return typeof rec.command === "string" ? rec.command : "";
|
|
2437
2749
|
}
|
|
2750
|
+
async function handleSlash(cmd, rest) {
|
|
2751
|
+
switch (cmd) {
|
|
2752
|
+
case "/exit":
|
|
2753
|
+
case "/quit":
|
|
2754
|
+
rl.close();
|
|
2755
|
+
return "exit";
|
|
2756
|
+
case "/help":
|
|
2757
|
+
help();
|
|
2758
|
+
return true;
|
|
2759
|
+
case "/auto":
|
|
2760
|
+
mode = "auto";
|
|
2761
|
+
notice("autonomy: auto\n");
|
|
2762
|
+
return true;
|
|
2763
|
+
case "/manual":
|
|
2764
|
+
mode = "manual";
|
|
2765
|
+
notice("autonomy: manual\n");
|
|
2766
|
+
return true;
|
|
2767
|
+
case "/clear":
|
|
2768
|
+
transcript = [];
|
|
2769
|
+
changes.length = 0;
|
|
2770
|
+
taskTokens = 0;
|
|
2771
|
+
clearScreen();
|
|
2772
|
+
banner(workspace.root, workspace.name, lane.client.label, mode);
|
|
2773
|
+
notice("Started a fresh task (screen cleared).\n");
|
|
2774
|
+
return true;
|
|
2775
|
+
case "/compact":
|
|
2776
|
+
await doCompact(rest);
|
|
2777
|
+
return true;
|
|
2778
|
+
case "/status":
|
|
2779
|
+
printStatus();
|
|
2780
|
+
return true;
|
|
2781
|
+
case "/diff":
|
|
2782
|
+
showDiff();
|
|
2783
|
+
return true;
|
|
2784
|
+
case "/revert":
|
|
2785
|
+
await revertLast();
|
|
2786
|
+
return true;
|
|
2787
|
+
case "/login":
|
|
2788
|
+
await doLogin();
|
|
2789
|
+
return true;
|
|
2790
|
+
case "/logout":
|
|
2791
|
+
doLogout();
|
|
2792
|
+
return true;
|
|
2793
|
+
case "/model":
|
|
2794
|
+
doModel(rest);
|
|
2795
|
+
return true;
|
|
2796
|
+
default:
|
|
2797
|
+
return false;
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
function printStatus() {
|
|
2801
|
+
line();
|
|
2802
|
+
notice(`workspace ${workspace.name}`);
|
|
2803
|
+
notice(`model ${lane.client.label}`);
|
|
2804
|
+
notice(`autonomy ${mode}`);
|
|
2805
|
+
notice(`changes ${changes.length} applied this task`);
|
|
2806
|
+
notice(`context ~${humanizeTokens(estimateMessagesTokens(transcript))} tokens in the conversation`);
|
|
2807
|
+
notice(`tokens ~${humanizeTokens(taskTokens)} task \xB7 ~${humanizeTokens(sessionTokens)} session (estimated)`);
|
|
2808
|
+
line();
|
|
2809
|
+
}
|
|
2810
|
+
function showDiff() {
|
|
2811
|
+
if (changes.length === 0) {
|
|
2812
|
+
notice("No file changes applied yet this task.\n");
|
|
2813
|
+
return;
|
|
2814
|
+
}
|
|
2815
|
+
line();
|
|
2816
|
+
line(c.bold(` Applied changes (${changes.length})`));
|
|
2817
|
+
for (const ch of changes) printDiff(ch);
|
|
2818
|
+
line(c.dim("\n Use /revert to undo the most recent one.\n"));
|
|
2819
|
+
}
|
|
2820
|
+
async function revertLast() {
|
|
2821
|
+
const ch = changes.pop();
|
|
2822
|
+
if (!ch) {
|
|
2823
|
+
notice("Nothing to revert.\n");
|
|
2824
|
+
return;
|
|
2825
|
+
}
|
|
2826
|
+
try {
|
|
2827
|
+
if (ch.before === null) {
|
|
2828
|
+
await deleteFile(ch.path);
|
|
2829
|
+
notice(`Reverted: deleted ${ch.path} (it was newly created).
|
|
2830
|
+
`);
|
|
2831
|
+
} else {
|
|
2832
|
+
await writeFile(ch.path, ch.before);
|
|
2833
|
+
notice(`Reverted ${ch.path} to its previous contents.
|
|
2834
|
+
`);
|
|
2835
|
+
}
|
|
2836
|
+
notice("(the agent is not told; a later turn just sees the reverted file)\n");
|
|
2837
|
+
} catch (e) {
|
|
2838
|
+
changes.push(ch);
|
|
2839
|
+
line(c.red(` Could not revert ${ch.path}: ${e instanceof Error ? e.message : "failed"}
|
|
2840
|
+
`));
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
async function doCompact(rest) {
|
|
2844
|
+
if (transcript.length === 0) {
|
|
2845
|
+
notice("Nothing to compact yet.\n");
|
|
2846
|
+
return;
|
|
2847
|
+
}
|
|
2848
|
+
const before = estimateMessagesTokens(transcript);
|
|
2849
|
+
const extra = rest.join(" ").trim();
|
|
2850
|
+
const instruction = "Summarize our conversation so far into a compact briefing I can use to continue this task with much less context. Cover: the goal, what has been done, key files and decisions, and what remains. Be thorough but concise. Reply with the summary text only \u2014 do not call any tools." + (extra ? ` Pay special attention to: ${extra}.` : "");
|
|
2851
|
+
const probe = [...transcript, { role: "user", content: instruction }];
|
|
2852
|
+
const abort = new AbortController();
|
|
2853
|
+
activeAbort = abort;
|
|
2854
|
+
line(c.bold("\n Compacting the conversation\u2026\n"));
|
|
2855
|
+
spinner.start("summarizing");
|
|
2856
|
+
let summary = "";
|
|
2857
|
+
try {
|
|
2858
|
+
const turn = await lane.client.runTurn(
|
|
2859
|
+
probe,
|
|
2860
|
+
(d) => {
|
|
2861
|
+
spinner.stop();
|
|
2862
|
+
out(clean(d));
|
|
2863
|
+
summary += d;
|
|
2864
|
+
},
|
|
2865
|
+
abort.signal
|
|
2866
|
+
);
|
|
2867
|
+
summary = turn.text || summary;
|
|
2868
|
+
} catch (e) {
|
|
2869
|
+
spinner.stop();
|
|
2870
|
+
activeAbort = null;
|
|
2871
|
+
if (abort.signal.aborted) {
|
|
2872
|
+
line(c.yellow("\n Compaction stopped.\n"));
|
|
2873
|
+
return;
|
|
2874
|
+
}
|
|
2875
|
+
line(c.red(`
|
|
2876
|
+
Compaction failed: ${e instanceof Error ? e.message : "error"}
|
|
2877
|
+
`));
|
|
2878
|
+
return;
|
|
2879
|
+
}
|
|
2880
|
+
spinner.stop();
|
|
2881
|
+
activeAbort = null;
|
|
2882
|
+
out("\n");
|
|
2883
|
+
if (!summary.trim()) {
|
|
2884
|
+
notice("The model returned no summary; the conversation is unchanged.\n");
|
|
2885
|
+
return;
|
|
2886
|
+
}
|
|
2887
|
+
transcript = [
|
|
2888
|
+
{
|
|
2889
|
+
role: "user",
|
|
2890
|
+
content: `Summary of the earlier conversation (compacted to save context):
|
|
2891
|
+
|
|
2892
|
+
${summary.trim()}`
|
|
2893
|
+
}
|
|
2894
|
+
];
|
|
2895
|
+
const after = estimateMessagesTokens(transcript);
|
|
2896
|
+
const used = estimateMessagesTokens(probe) + estimateTokens(summary);
|
|
2897
|
+
taskTokens += used;
|
|
2898
|
+
sessionTokens += used;
|
|
2899
|
+
notice(`
|
|
2900
|
+
Compacted: context ~${humanizeTokens(before)} \u2192 ~${humanizeTokens(after)} tokens.
|
|
2901
|
+
`);
|
|
2902
|
+
}
|
|
2903
|
+
async function doLogin() {
|
|
2904
|
+
if (!lane.managedAvailable()) {
|
|
2905
|
+
notice("Managed sign-in is not available in this build. Use BYOK (`expert setup`).\n");
|
|
2906
|
+
return;
|
|
2907
|
+
}
|
|
2908
|
+
line();
|
|
2909
|
+
line(" " + c.bold("Expert") + " " + c.bold(c.orange("Code")) + c.dim(" \u2014 sign in"));
|
|
2910
|
+
line(c.dim(" We will email you a one-time code.\n"));
|
|
2911
|
+
try {
|
|
2912
|
+
const email = await lane.loginInteractive(askTransient);
|
|
2913
|
+
line(c.green(`
|
|
2914
|
+
Signed in as ${email}. Now using the hosted backend.`));
|
|
2915
|
+
notice("(managed inference requires a Premium account)\n");
|
|
2916
|
+
} catch (e) {
|
|
2917
|
+
line(c.red(`
|
|
2918
|
+
${e instanceof Error ? e.message : "Sign-in failed."}
|
|
2919
|
+
`));
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
function doLogout() {
|
|
2923
|
+
try {
|
|
2924
|
+
const label = lane.logout();
|
|
2925
|
+
notice(`Signed out. Now using ${label}.
|
|
2926
|
+
`);
|
|
2927
|
+
} catch (e) {
|
|
2928
|
+
line(c.red(` ${e instanceof Error ? e.message : "Could not switch to BYOK."}
|
|
2929
|
+
`));
|
|
2930
|
+
}
|
|
2931
|
+
}
|
|
2932
|
+
function doModel(rest) {
|
|
2933
|
+
if (rest.length === 0) {
|
|
2934
|
+
line();
|
|
2935
|
+
for (const l of lane.status()) notice(l);
|
|
2936
|
+
notice("Switch with: /model <anthropic|openai|gemini|groq|openrouter> [model]\n");
|
|
2937
|
+
return;
|
|
2938
|
+
}
|
|
2939
|
+
try {
|
|
2940
|
+
const label = lane.setProvider(rest[0], rest[1]);
|
|
2941
|
+
notice(`Now using ${label}.
|
|
2942
|
+
`);
|
|
2943
|
+
} catch (e) {
|
|
2944
|
+
line(c.red(` ${e instanceof Error ? e.message : "Could not switch model."}
|
|
2945
|
+
`));
|
|
2946
|
+
}
|
|
2947
|
+
}
|
|
2438
2948
|
async function promptApproval(tool) {
|
|
2439
2949
|
printApprovalRequest(tool);
|
|
2440
2950
|
const allowAlways = tool.name === "run_command";
|
|
2441
2951
|
const hint = allowAlways ? " [y]es / [n]o / [a]lways allow this command, or type a note to reject: " : " [y]es / [n]o, or type a note to reject: ";
|
|
2442
|
-
const answer = (await
|
|
2952
|
+
const answer = (await askTransient(c.dim(hint))).trim();
|
|
2443
2953
|
const lower = answer.toLowerCase();
|
|
2444
2954
|
if (lower === "y" || lower === "yes" || answer === "") {
|
|
2445
2955
|
line();
|
|
@@ -2460,7 +2970,7 @@ async function runRepl({ model, workspace, manual }) {
|
|
|
2460
2970
|
}
|
|
2461
2971
|
|
|
2462
2972
|
// index.ts
|
|
2463
|
-
var VERSION = true ? "0.
|
|
2973
|
+
var VERSION = true ? "0.2.0" : "dev";
|
|
2464
2974
|
function withReadline(fn) {
|
|
2465
2975
|
const rl = readline2.createInterface({ input: process.stdin, output: process.stdout });
|
|
2466
2976
|
const ask = (q) => new Promise((res) => rl.question(q, (a) => res(a)));
|
|
@@ -2492,8 +3002,67 @@ function resolveModelClient(cfg) {
|
|
|
2492
3002
|
printMissingKey(provider);
|
|
2493
3003
|
return null;
|
|
2494
3004
|
}
|
|
3005
|
+
return createByokClient(byokLabel(provider, cfg));
|
|
3006
|
+
}
|
|
3007
|
+
function byokLabel(provider, cfg) {
|
|
2495
3008
|
const model = resolveModel(provider, cfg);
|
|
2496
|
-
return
|
|
3009
|
+
return `${provider}${model ? ` (${model})` : " (provider default)"}`;
|
|
3010
|
+
}
|
|
3011
|
+
function createLaneController(cfg, initial) {
|
|
3012
|
+
let client7 = initial;
|
|
3013
|
+
return {
|
|
3014
|
+
get client() {
|
|
3015
|
+
return client7;
|
|
3016
|
+
},
|
|
3017
|
+
managedAvailable: () => managedConfigured(),
|
|
3018
|
+
async loginInteractive(ask) {
|
|
3019
|
+
if (!managedConfigured()) throw new Error("Managed sign-in is not available in this build.");
|
|
3020
|
+
const email = await login(ask);
|
|
3021
|
+
client7 = createManagedClient({ apiBase: resolveApiBase(cfg), getToken: getAccessToken });
|
|
3022
|
+
return email;
|
|
3023
|
+
},
|
|
3024
|
+
logout() {
|
|
3025
|
+
logout();
|
|
3026
|
+
cfg.session = void 0;
|
|
3027
|
+
const provider = resolveProvider(cfg);
|
|
3028
|
+
if (!resolveKey(provider, cfg)) {
|
|
3029
|
+
throw new Error("Signed out, but no BYOK key is configured. Run `expert setup`.");
|
|
3030
|
+
}
|
|
3031
|
+
client7 = createByokClient(byokLabel(provider, cfg));
|
|
3032
|
+
return client7.label;
|
|
3033
|
+
},
|
|
3034
|
+
setProvider(providerArg, modelArg) {
|
|
3035
|
+
const provider = providerArg.toLowerCase();
|
|
3036
|
+
if (!PROVIDERS.includes(provider)) {
|
|
3037
|
+
throw new Error(`Unknown provider "${providerArg}". One of: ${PROVIDERS.join(", ")}`);
|
|
3038
|
+
}
|
|
3039
|
+
const p = provider;
|
|
3040
|
+
const key = resolveKey(p, cfg);
|
|
3041
|
+
if (!key) {
|
|
3042
|
+
throw new Error(
|
|
3043
|
+
`No API key for ${PROVIDER_LABELS[p]}. Run \`expert setup\` or set ${KEY_ENV[p]}.`
|
|
3044
|
+
);
|
|
3045
|
+
}
|
|
3046
|
+
cfg.provider = p;
|
|
3047
|
+
if (modelArg) cfg.models = { ...cfg.models, [p]: modelArg };
|
|
3048
|
+
saveConfig(cfg);
|
|
3049
|
+
process.env.CODE_AI_PROVIDER = p;
|
|
3050
|
+
process.env[`CODE_${KEY_ENV[p]}`] = key;
|
|
3051
|
+
const model = modelArg || resolveModel(p, cfg);
|
|
3052
|
+
if (model) process.env[`CODE_${MODEL_ENV[p]}`] = model;
|
|
3053
|
+
client7 = createByokClient(byokLabel(p, cfg));
|
|
3054
|
+
return client7.label;
|
|
3055
|
+
},
|
|
3056
|
+
status() {
|
|
3057
|
+
const lines = [`current model ${client7.label}`];
|
|
3058
|
+
const provider = resolveProvider(cfg);
|
|
3059
|
+
const keyed = PROVIDERS.map(
|
|
3060
|
+
(p) => `${p}${resolveKey(p, cfg) ? "" : " (no key)"}${p === provider ? " \u2190" : ""}`
|
|
3061
|
+
);
|
|
3062
|
+
lines.push(`providers ${keyed.join(", ")}`);
|
|
3063
|
+
return lines;
|
|
3064
|
+
}
|
|
3065
|
+
};
|
|
2497
3066
|
}
|
|
2498
3067
|
function saveByok(provider, key, model) {
|
|
2499
3068
|
const cfg = loadConfig();
|
|
@@ -2530,7 +3099,7 @@ async function cmdSetup(args) {
|
|
|
2530
3099
|
}
|
|
2531
3100
|
await withReadline(async (ask) => {
|
|
2532
3101
|
line();
|
|
2533
|
-
line(c.bold("
|
|
3102
|
+
line(" " + c.bold("Expert") + " " + c.bold(c.orange("Code")) + c.dim(" \u2014 BYOK setup"));
|
|
2534
3103
|
line(c.dim(" Use your own AI provider key. Stored in " + configPath() + "\n"));
|
|
2535
3104
|
PROVIDERS.forEach((p, i) => line(` ${i + 1}. ${PROVIDER_LABELS[p]}`));
|
|
2536
3105
|
const pick = (await ask("\n Provider [1-5, default 1]: ")).trim();
|
|
@@ -2556,7 +3125,7 @@ async function cmdLogin() {
|
|
|
2556
3125
|
}
|
|
2557
3126
|
await withReadline(async (ask) => {
|
|
2558
3127
|
line();
|
|
2559
|
-
line(c.bold("
|
|
3128
|
+
line(" " + c.bold("Expert") + " " + c.bold(c.orange("Code")) + c.dim(" \u2014 sign in"));
|
|
2560
3129
|
line(c.dim(" We will email you a one-time code.\n"));
|
|
2561
3130
|
try {
|
|
2562
3131
|
const email = await login(ask);
|
|
@@ -2640,11 +3209,11 @@ async function cmdRun(args) {
|
|
|
2640
3209
|
process.exitCode = 1;
|
|
2641
3210
|
return;
|
|
2642
3211
|
}
|
|
2643
|
-
|
|
3212
|
+
const lane = createLaneController(cfg, model);
|
|
3213
|
+
await runRepl({ lane, workspace, manual });
|
|
2644
3214
|
}
|
|
2645
3215
|
function printUsage() {
|
|
2646
|
-
|
|
2647
|
-
line(c.bold(" expert") + c.dim(" \u2014 Expert Code in your terminal"));
|
|
3216
|
+
masthead();
|
|
2648
3217
|
line();
|
|
2649
3218
|
line(" Usage:");
|
|
2650
3219
|
line(" expert [directory] [--manual] start the agent (default: current dir)");
|
|
@@ -2663,9 +3232,9 @@ function printMissingKey(provider) {
|
|
|
2663
3232
|
line(c.red(` No API key found for provider "${provider}".`));
|
|
2664
3233
|
line();
|
|
2665
3234
|
line(" Set one up once with:");
|
|
2666
|
-
line(c.
|
|
3235
|
+
line(c.orange(" expert setup"));
|
|
2667
3236
|
line(c.dim(" \u2026or sign in to use the hosted backend (no key):"));
|
|
2668
|
-
line(c.
|
|
3237
|
+
line(c.orange(" expert login"));
|
|
2669
3238
|
line(c.dim(` \u2026or set ${provider.toUpperCase()}_API_KEY in your environment for this session.`));
|
|
2670
3239
|
line();
|
|
2671
3240
|
}
|
package/package.json
CHANGED