@hexdecimal16/nexo 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +75 -0
- package/README.md +75 -2
- package/cli/src/bin.js +12 -0
- package/cli/src/common.js +77 -0
- package/cli/src/doctor.js +27 -0
- package/cli/src/eval-cases.js +65 -0
- package/cli/src/eval.js +67 -0
- package/cli/src/init.js +186 -0
- package/cli/src/interactive.js +171 -0
- package/cli/src/learn.js +178 -0
- package/cli/src/main.js +261 -0
- package/cli/src/optimize.js +289 -0
- package/cli/src/prompter.js +47 -0
- package/cli/src/review.js +120 -0
- package/cli/src/runtime.js +2 -0
- package/cli/src/sessions.js +34 -0
- package/cli/src/settings.js +169 -0
- package/cli/src/ui.js +112 -0
- package/core/src/approval/classifier.js +152 -0
- package/core/src/config/load.js +283 -0
- package/core/src/config/schema.js +186 -0
- package/core/src/cost/admission.js +90 -0
- package/core/src/cost/budget.js +67 -0
- package/core/src/cost/pricing.js +53 -0
- package/core/src/engine/charged-completion.js +79 -0
- package/core/src/engine/errors.js +77 -0
- package/core/src/engine/mcp.js +11 -0
- package/core/src/engine/messages.js +86 -0
- package/core/src/engine/prompts.js +32 -0
- package/core/src/engine/provider-gate.js +33 -0
- package/core/src/engine/recorder.js +61 -0
- package/core/src/engine/run.js +1115 -0
- package/core/src/engine/semaphore.js +45 -0
- package/core/src/engine/spawn-tool.js +119 -0
- package/core/src/engine/task-graph.js +153 -0
- package/core/src/index.js +32 -0
- package/core/src/learn/caller.js +171 -0
- package/core/src/learn/context.js +112 -0
- package/core/src/learn/embedder.js +216 -0
- package/core/src/learn/job.js +196 -0
- package/core/src/learn/wiki.js +116 -0
- package/core/src/learn/worker.js +141 -0
- package/core/src/optimize/adapter.js +250 -0
- package/core/src/optimize/climb.js +338 -0
- package/core/src/optimize/mine.js +95 -0
- package/core/src/optimize/optimize.js +377 -0
- package/core/src/optimize/optimizer-run.js +67 -0
- package/core/src/optimize/reflect.js +100 -0
- package/core/src/optimize/scratchpad-wiki.js +69 -0
- package/core/src/providers/claude-p.js +363 -0
- package/core/src/providers/kiro-p.js +408 -0
- package/core/src/providers/models.js +10 -0
- package/core/src/scoring/calibration.js +31 -0
- package/core/src/scoring/continuation.js +24 -0
- package/core/src/scoring/runner.js +322 -0
- package/core/src/scoring/sources.js +88 -0
- package/core/src/session/lock.js +88 -0
- package/core/src/session/session.js +647 -0
- package/core/src/session/shell.js +26 -0
- package/core/src/session/transcript.js +86 -0
- package/core/src/settings/config-edit.js +472 -0
- package/core/src/settings/credentials.js +120 -0
- package/core/src/settings/doctor.js +103 -0
- package/core/src/settings/settings.js +265 -0
- package/mcp/src/hub.js +184 -0
- package/mcp/src/index.js +1 -0
- package/memory/src/curate.js +121 -0
- package/memory/src/index.js +4 -0
- package/memory/src/prompts.js +143 -0
- package/memory/src/retrieval.js +91 -0
- package/memory/src/similarity.js +47 -0
- package/optimize/src/engine.js +206 -0
- package/optimize/src/index.js +8 -0
- package/optimize/src/merge.js +73 -0
- package/optimize/src/novelty.js +37 -0
- package/optimize/src/pareto.js +83 -0
- package/optimize/src/proposer.js +148 -0
- package/optimize/src/report.js +107 -0
- package/optimize/src/rng.js +34 -0
- package/optimize/src/target.js +34 -0
- package/package.json +47 -4
- package/router/src/effort.js +5 -0
- package/router/src/escalation.js +171 -0
- package/router/src/index.js +4 -0
- package/router/src/registry.js +21 -0
- package/router/src/resolve.js +117 -0
- package/scorers/src/deterministic.js +194 -0
- package/scorers/src/family.js +48 -0
- package/scorers/src/index.js +6 -0
- package/scorers/src/judge.js +284 -0
- package/scorers/src/regex-worker.js +3 -0
- package/scorers/src/spec.js +147 -0
- package/scorers/src/trajectory.js +22 -0
- package/scorers/src/types.js +1 -0
- package/sdk/src/index.js +3 -0
- package/sdk/src/nexo.js +94 -0
- package/sdk/src/runtime.js +39 -0
- package/skills/bundled/browser/SKILL.md +57 -0
- package/skills/bundled/wiki/SKILL.md +73 -0
- package/skills/src/frontmatter.js +25 -0
- package/skills/src/index.js +3 -0
- package/skills/src/load-skill-tool.js +41 -0
- package/skills/src/loader.js +95 -0
- package/store/src/artifacts.js +124 -0
- package/store/src/budget-ledger.js +18 -0
- package/store/src/cost-report.js +46 -0
- package/store/src/cursor.js +35 -0
- package/store/src/db.js +83 -0
- package/store/src/errors.js +9 -0
- package/store/src/eval-cases.js +255 -0
- package/store/src/feedback.js +154 -0
- package/store/src/goldens.js +61 -0
- package/store/src/ids.js +6 -0
- package/store/src/index.js +25 -0
- package/store/src/judge-calibration.js +182 -0
- package/store/src/learn-jobs.js +145 -0
- package/store/src/memories.js +261 -0
- package/store/src/migrations/001_init.sql +81 -0
- package/store/src/migrations/002_feedback.sql +9 -0
- package/store/src/migrations/003_sessions.sql +19 -0
- package/store/src/migrations/004_scoring.sql +22 -0
- package/store/src/migrations/005_judge_calibration.sql +27 -0
- package/store/src/migrations/006_learning.sql +77 -0
- package/store/src/migrations/007_optimize.sql +55 -0
- package/store/src/migrations/008_mined_cases_and_climb.sql +46 -0
- package/store/src/mine.js +51 -0
- package/store/src/optim.js +291 -0
- package/store/src/paths.js +22 -0
- package/store/src/playbooks.js +199 -0
- package/store/src/reindex.js +143 -0
- package/store/src/run-dir.js +151 -0
- package/store/src/runs.js +310 -0
- package/store/src/scores.js +92 -0
- package/store/src/sessions.js +304 -0
- package/store/src/span-tree.js +98 -0
- package/store/src/spans.js +84 -0
- package/store/src/sql.js +13 -0
- package/store/src/workspace-copy.js +80 -0
- package/telemetry/src/errors.js +32 -0
- package/telemetry/src/index.js +3 -0
- package/telemetry/src/logger.js +39 -0
- package/telemetry/src/redact.js +106 -0
- package/tools/src/builtin.js +269 -0
- package/tools/src/change-tracker.js +57 -0
- package/tools/src/confine.js +101 -0
- package/tools/src/index.js +7 -0
- package/tools/src/leases.js +57 -0
- package/tools/src/policy.js +162 -0
- package/tools/src/read-only.js +338 -0
- package/tools/src/session-rules.js +140 -0
- package/tui/src/app.js +948 -0
- package/tui/src/approvals.js +96 -0
- package/tui/src/commands.js +38 -0
- package/tui/src/components.js +248 -0
- package/tui/src/index.js +5 -0
- package/tui/src/picker.js +78 -0
- package/tui/src/session-picker.js +100 -0
- package/tui/src/settings-screen.js +346 -0
- package/tui/src/text.js +70 -0
- package/tui/src/theme.js +44 -0
- package/web/public/cost.html +25 -0
- package/web/public/fonts/GeistMono-400.woff2 +0 -0
- package/web/public/fonts/GeistMono-600.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-400.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-600.woff2 +0 -0
- package/web/public/index.html +39 -0
- package/web/public/judge.html +21 -0
- package/web/public/learning.html +46 -0
- package/web/public/lib/api-types.ts +616 -0
- package/web/public/lib/common-view.ts +47 -0
- package/web/public/lib/cost-view.ts +35 -0
- package/web/public/lib/files-view.ts +90 -0
- package/web/public/lib/format.ts +35 -0
- package/web/public/lib/html.ts +51 -0
- package/web/public/lib/judge-view.ts +62 -0
- package/web/public/lib/learning-view.ts +102 -0
- package/web/public/lib/optim-view.ts +148 -0
- package/web/public/lib/pager.ts +56 -0
- package/web/public/lib/poller.ts +79 -0
- package/web/public/lib/run-view.ts +61 -0
- package/web/public/lib/runs-view.ts +87 -0
- package/web/public/lib/score-view.ts +94 -0
- package/web/public/lib/sessions-view.ts +28 -0
- package/web/public/lib/span-view.ts +90 -0
- package/web/public/lib/theme.ts +60 -0
- package/web/public/lib/timeline.ts +97 -0
- package/web/public/lib/wikis-view.ts +257 -0
- package/web/public/optim.html +30 -0
- package/web/public/pages/api.ts +60 -0
- package/web/public/pages/cost.ts +36 -0
- package/web/public/pages/judge.ts +15 -0
- package/web/public/pages/learning.ts +92 -0
- package/web/public/pages/optim.ts +68 -0
- package/web/public/pages/paging.ts +18 -0
- package/web/public/pages/run.ts +221 -0
- package/web/public/pages/runs.ts +91 -0
- package/web/public/pages/sessions.ts +25 -0
- package/web/public/pages/wikis.ts +103 -0
- package/web/public/run.html +36 -0
- package/web/public/sessions.html +28 -0
- package/web/public/style.css +288 -0
- package/web/public/wikis.html +25 -0
- package/web/src/api.js +682 -0
- package/web/src/http-errors.js +60 -0
- package/web/src/index.js +5 -0
- package/web/src/server.js +292 -0
- package/web/src/static.js +86 -0
- package/web/src/strip.js +22 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Container, SelectList, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
2
|
+
import { fitLines } from "./components.js";
|
|
3
|
+
import { escapeControl } from "./text.js";
|
|
4
|
+
import { selectListTheme, style } from "./theme.js";
|
|
5
|
+
/** The question, the optional note, the Allow once / Always allow / Deny list and a key hint, in a bordered box. */
|
|
6
|
+
class ApprovalBox extends Container {
|
|
7
|
+
question;
|
|
8
|
+
note;
|
|
9
|
+
list;
|
|
10
|
+
acceptInputAt;
|
|
11
|
+
now;
|
|
12
|
+
constructor(question, note, list, guardMs, now) {
|
|
13
|
+
super();
|
|
14
|
+
this.question = question;
|
|
15
|
+
this.note = note;
|
|
16
|
+
this.list = list;
|
|
17
|
+
this.now = now;
|
|
18
|
+
this.acceptInputAt = now() + guardMs;
|
|
19
|
+
}
|
|
20
|
+
/** Keys typed ahead (for example an Enter meant for the editor) are dropped until the guard time has passed. */
|
|
21
|
+
handleInput(data) {
|
|
22
|
+
if (this.now() < this.acceptInputAt)
|
|
23
|
+
return;
|
|
24
|
+
this.list.handleInput(data);
|
|
25
|
+
}
|
|
26
|
+
render(width) {
|
|
27
|
+
return fitLines(this.renderBox(width), width);
|
|
28
|
+
}
|
|
29
|
+
renderBox(width) {
|
|
30
|
+
const inner = Math.max(1, width - 4);
|
|
31
|
+
const border = (s) => style.yellow(s);
|
|
32
|
+
const row = (s) => `${border("│")} ${truncateToWidth(s, inner, "…", true)} ${border("│")}`;
|
|
33
|
+
const lines = [border(`╭${"─".repeat(inner + 2)}╮`)];
|
|
34
|
+
lines.push(row(style.bold(style.yellow("Approval needed"))));
|
|
35
|
+
for (const line of wrapTextWithAnsi(this.question, inner))
|
|
36
|
+
lines.push(row(line));
|
|
37
|
+
if (this.note)
|
|
38
|
+
for (const line of wrapTextWithAnsi(this.note, inner))
|
|
39
|
+
lines.push(row(style.gray(line)));
|
|
40
|
+
lines.push(row(""));
|
|
41
|
+
for (const line of this.list.render(inner))
|
|
42
|
+
lines.push(row(line));
|
|
43
|
+
for (const line of wrapTextWithAnsi("↑↓ choose · enter select · esc deny · ctrl-c deny and stop the turn", inner))
|
|
44
|
+
lines.push(row(style.gray(line)));
|
|
45
|
+
lines.push(border(`╰${"─".repeat(inner + 2)}╯`));
|
|
46
|
+
return lines;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A policy Prompter that asks in a bordered box with Allow once, Always allow (when the request offers it) and Deny, drawn by the host in place of the editor. Questions are
|
|
51
|
+
* queued, so concurrent subagents get one box each in turn. An aborted signal (Ctrl-C, turn end) closes the box and
|
|
52
|
+
* denies. Deny is preselected and input is ignored for `guardMs`, so a keystroke typed ahead cannot approve a tool.
|
|
53
|
+
*/
|
|
54
|
+
export function createApprovalPrompter(host, options = {}) {
|
|
55
|
+
const guardMs = options.guardMs ?? 400;
|
|
56
|
+
const now = options.now ?? Date.now;
|
|
57
|
+
const redact = options.redact ?? ((text) => text);
|
|
58
|
+
let queue = Promise.resolve();
|
|
59
|
+
const ask = (request, signal) => new Promise((resolve) => {
|
|
60
|
+
if (signal?.aborted)
|
|
61
|
+
return resolve("deny");
|
|
62
|
+
if (request.settled?.())
|
|
63
|
+
return resolve("once");
|
|
64
|
+
const options = [
|
|
65
|
+
{ value: "once", label: "Allow once" },
|
|
66
|
+
...(request.alwaysLabel ? [{ value: "always", label: escapeControl(redact(request.alwaysLabel)) }] : []),
|
|
67
|
+
{ value: "deny", label: "Deny" },
|
|
68
|
+
];
|
|
69
|
+
const list = new SelectList(options, options.length, selectListTheme);
|
|
70
|
+
list.setSelectedIndex(options.length - 1);
|
|
71
|
+
const note = request.note ? escapeControl(redact(request.note)) : undefined;
|
|
72
|
+
const box = new ApprovalBox(escapeControl(redact(request.question)), note, list, guardMs, now);
|
|
73
|
+
const hide = host.show(box);
|
|
74
|
+
let settled = false;
|
|
75
|
+
const finish = (answer) => {
|
|
76
|
+
if (settled)
|
|
77
|
+
return;
|
|
78
|
+
settled = true;
|
|
79
|
+
signal?.removeEventListener("abort", onAbort);
|
|
80
|
+
hide();
|
|
81
|
+
resolve(answer);
|
|
82
|
+
};
|
|
83
|
+
const onAbort = () => finish("deny");
|
|
84
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
85
|
+
list.onSelect = (item) => finish(item.value);
|
|
86
|
+
list.onCancel = () => finish("deny");
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
interactive: true,
|
|
90
|
+
approve(request, signal) {
|
|
91
|
+
const next = queue.then(() => ask(request, signal));
|
|
92
|
+
queue = next.catch(() => undefined);
|
|
93
|
+
return next;
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { EFFORTS } from "../../router/src/index.js";
|
|
2
|
+
/** `/name args…` → { name, args }; null when the text is not a slash command. */
|
|
3
|
+
export function parseSlashCommand(text) {
|
|
4
|
+
const trimmed = text.trim();
|
|
5
|
+
if (!trimmed.startsWith("/"))
|
|
6
|
+
return null;
|
|
7
|
+
const match = /^\/(\S*)\s*([\s\S]*)$/.exec(trimmed);
|
|
8
|
+
return { name: match?.[1] ?? "", args: (match?.[2] ?? "").trim() };
|
|
9
|
+
}
|
|
10
|
+
const items = (values) => (prefix) => {
|
|
11
|
+
const matches = values.filter((v) => v.startsWith(prefix)).map((v) => ({ value: v, label: v }));
|
|
12
|
+
return matches.length > 0 ? matches : null;
|
|
13
|
+
};
|
|
14
|
+
/** Every slash command, in /help order. */
|
|
15
|
+
export const SLASH_COMMANDS = [
|
|
16
|
+
{ name: "help", description: "List the commands" },
|
|
17
|
+
{ name: "done", description: "Mark the last turn done" },
|
|
18
|
+
{ name: "notdone", description: "Mark the last turn not done", argumentHint: "[comment]" },
|
|
19
|
+
{ name: "cost", description: "Show the session's cost and budget" },
|
|
20
|
+
{ name: "budget", description: "Set this session's budget cap", argumentHint: "<usd>" },
|
|
21
|
+
{ name: "model", description: "Set this session's model (default clears it)", argumentHint: "<s1|s2|provider/id|default>", getArgumentCompletions: items(["s1", "s2", "default"]) },
|
|
22
|
+
{ name: "effort", description: "Set this session's effort (default clears it)", argumentHint: "<level|default>", getArgumentCompletions: items([...EFFORTS, "default"]) },
|
|
23
|
+
{ name: "auto", description: "Auto mode for this session: an approver model allows in-scope actions", argumentHint: "[on|off|default]", getArgumentCompletions: items(["on", "off", "default"]) },
|
|
24
|
+
{ name: "permissions", description: "List this session's Always-allow rules", argumentHint: "[remove <n>|clear]", getArgumentCompletions: items(["remove", "clear"]) },
|
|
25
|
+
{ name: "compact", description: "Summarize older turns at the start of the next turn" },
|
|
26
|
+
{ name: "clear", description: "Start a new conversation in this session" },
|
|
27
|
+
{ name: "sessions", description: "List recent sessions" },
|
|
28
|
+
{ name: "resume", description: "Switch to another session (a picker when no id is given)", argumentHint: "[id]" },
|
|
29
|
+
{ name: "settings", description: "Open settings (models, keys, budgets, tool policy)" },
|
|
30
|
+
{ name: "exit", description: "Leave nexo (the session can be resumed)" },
|
|
31
|
+
];
|
|
32
|
+
export function helpText() {
|
|
33
|
+
const width = Math.max(...SLASH_COMMANDS.map((c) => `/${c.name}${c.argumentHint ? ` ${c.argumentHint}` : ""}`.length));
|
|
34
|
+
const lines = SLASH_COMMANDS.map((c) => `${`/${c.name}${c.argumentHint ? ` ${c.argumentHint}` : ""}`.padEnd(width)} ${c.description ?? ""}`);
|
|
35
|
+
lines.push("", `${"!<command>".padEnd(width)} Run a shell command; its output goes to the model with your next message`);
|
|
36
|
+
lines.push("", "enter send · shift+enter newline · ↑↓ history · ctrl+o expand tool output · esc/ctrl-c stop a turn · ctrl-c twice or ctrl-d quit");
|
|
37
|
+
return lines.join("\n");
|
|
38
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Container, CURSOR_MARKER, Markdown, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
|
|
2
|
+
import { duration, escapeControl, oneLine, previewLines, resultLines, usd } from "./text.js";
|
|
3
|
+
import { style } from "./theme.js";
|
|
4
|
+
export const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
5
|
+
/** Shared animation frame for every running line; the chat advances it while a turn runs. */
|
|
6
|
+
export class Ticker {
|
|
7
|
+
frame = 0;
|
|
8
|
+
get spinner() {
|
|
9
|
+
return SPINNER_FRAMES[this.frame % SPINNER_FRAMES.length];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Cuts every line to the width, so no child can make pi-tui throw on an over-wide line. */
|
|
13
|
+
export function fitLines(lines, width) {
|
|
14
|
+
return lines.map((line) => (visibleWidth(line) > width ? truncateToWidth(line, width, "…") : line));
|
|
15
|
+
}
|
|
16
|
+
/** A Container whose rendered lines never exceed the width. */
|
|
17
|
+
export class SafeContainer extends Container {
|
|
18
|
+
render(width) {
|
|
19
|
+
return fitLines(super.render(width), width);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Markdown whose rendered lines never exceed the width. */
|
|
23
|
+
export class SafeMarkdown extends Markdown {
|
|
24
|
+
render(width) {
|
|
25
|
+
return fitLines(super.render(width), width);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** One line whose text is computed at render time, cut to the width. */
|
|
29
|
+
export class DynamicLine {
|
|
30
|
+
text;
|
|
31
|
+
constructor(text) {
|
|
32
|
+
this.text = text;
|
|
33
|
+
}
|
|
34
|
+
render(width) {
|
|
35
|
+
return [truncateToWidth(this.text(), width)];
|
|
36
|
+
}
|
|
37
|
+
invalidate() { }
|
|
38
|
+
}
|
|
39
|
+
/** Most queued messages listed before the rest collapse into `+N more`. */
|
|
40
|
+
export const QUEUE_SHOWN = 3;
|
|
41
|
+
/** Messages waiting for the running turn to end, one dim line each; renders nothing when empty. */
|
|
42
|
+
export class QueuedList {
|
|
43
|
+
items;
|
|
44
|
+
constructor(items) {
|
|
45
|
+
this.items = items;
|
|
46
|
+
}
|
|
47
|
+
render(width) {
|
|
48
|
+
const items = this.items();
|
|
49
|
+
if (items.length === 0)
|
|
50
|
+
return [];
|
|
51
|
+
const lines = [""];
|
|
52
|
+
items.slice(0, QUEUE_SHOWN).forEach((item, i) => {
|
|
53
|
+
lines.push(truncateToWidth(style.dim(` ⏵ ${i + 1} ${escapeControl(oneLine(item, 200))}`), width));
|
|
54
|
+
});
|
|
55
|
+
if (items.length > QUEUE_SHOWN)
|
|
56
|
+
lines.push(truncateToWidth(style.gray(` … +${items.length - QUEUE_SHOWN} more`), width));
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
invalidate() { }
|
|
60
|
+
}
|
|
61
|
+
/** Static text, wrapped to the width (text must already be control-escaped). */
|
|
62
|
+
export class Block {
|
|
63
|
+
text;
|
|
64
|
+
color;
|
|
65
|
+
prefix;
|
|
66
|
+
constructor(text, color = (s) => s, prefix = "") {
|
|
67
|
+
this.text = text;
|
|
68
|
+
this.color = color;
|
|
69
|
+
this.prefix = prefix;
|
|
70
|
+
}
|
|
71
|
+
setText(text) {
|
|
72
|
+
this.text = text;
|
|
73
|
+
}
|
|
74
|
+
render(width) {
|
|
75
|
+
const pad = visibleWidth(this.prefix);
|
|
76
|
+
const out = [""];
|
|
77
|
+
const lines = this.text.split("\n");
|
|
78
|
+
lines.forEach((line, i) => {
|
|
79
|
+
const wrapped = wrapTextWithAnsi(line, Math.max(1, width - pad));
|
|
80
|
+
wrapped.forEach((w, j) => {
|
|
81
|
+
const lead = i === 0 && j === 0 ? this.prefix : " ".repeat(pad);
|
|
82
|
+
out.push(truncateToWidth(this.color(lead + w), width));
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
invalidate() { }
|
|
88
|
+
}
|
|
89
|
+
/** Whether tool lines show their full arguments and output (ctrl+o); shared by every line in the chat. */
|
|
90
|
+
export class ViewMode {
|
|
91
|
+
verbose = false;
|
|
92
|
+
}
|
|
93
|
+
/** `● tool(args)` with a spinner while running, then `✓`/`✗`, duration and a short result preview, or everything when verbose. */
|
|
94
|
+
export class ToolLine {
|
|
95
|
+
state = "running";
|
|
96
|
+
durationMs = 0;
|
|
97
|
+
preview = [];
|
|
98
|
+
output = [];
|
|
99
|
+
summary;
|
|
100
|
+
fullSummary;
|
|
101
|
+
ticker;
|
|
102
|
+
view;
|
|
103
|
+
constructor(summary, fullSummary, ticker, view) {
|
|
104
|
+
this.summary = summary;
|
|
105
|
+
this.fullSummary = fullSummary;
|
|
106
|
+
this.ticker = ticker;
|
|
107
|
+
this.view = view;
|
|
108
|
+
}
|
|
109
|
+
finish(isError, durationMs, output) {
|
|
110
|
+
this.state = isError ? "error" : "ok";
|
|
111
|
+
this.durationMs = durationMs;
|
|
112
|
+
this.preview = previewLines(output);
|
|
113
|
+
this.output = resultLines(output);
|
|
114
|
+
}
|
|
115
|
+
/** Marks a line still running when its turn ended (aborted or failed). */
|
|
116
|
+
interrupt() {
|
|
117
|
+
if (this.state === "running")
|
|
118
|
+
this.state = "error";
|
|
119
|
+
}
|
|
120
|
+
render(width) {
|
|
121
|
+
const mark = this.state === "running" ? style.cyan(this.ticker.spinner) : this.state === "ok" ? style.green("✓") : style.red("✗");
|
|
122
|
+
const tail = this.state === "running" ? "" : style.gray(` ${duration(this.durationMs)}`);
|
|
123
|
+
if (!this.view.verbose) {
|
|
124
|
+
const lines = ["", truncateToWidth(`${mark} ${style.bold(this.summary)}${tail}`, width)];
|
|
125
|
+
this.preview.forEach((line, i) => {
|
|
126
|
+
lines.push(truncateToWidth(style.gray(`${i === 0 ? " ⎿ " : " "}${line}`), width));
|
|
127
|
+
});
|
|
128
|
+
return lines;
|
|
129
|
+
}
|
|
130
|
+
const lines = [""];
|
|
131
|
+
wrapTextWithAnsi(`${style.bold(this.fullSummary)}${tail}`, Math.max(1, width - 2)).forEach((w, i) => {
|
|
132
|
+
lines.push(truncateToWidth(`${i === 0 ? `${mark} ` : " "}${w}`, width));
|
|
133
|
+
});
|
|
134
|
+
this.output.forEach((line, i) => {
|
|
135
|
+
wrapTextWithAnsi(line, Math.max(1, width - 4)).forEach((w, j) => {
|
|
136
|
+
lines.push(truncateToWidth(style.gray(`${i === 0 && j === 0 ? " ⎿ " : " "}${w}`), width));
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
return lines;
|
|
140
|
+
}
|
|
141
|
+
invalidate() { }
|
|
142
|
+
}
|
|
143
|
+
/** One line per spawned subagent: status and cost. */
|
|
144
|
+
export class SubagentLine {
|
|
145
|
+
state = "running";
|
|
146
|
+
costUsd = 0;
|
|
147
|
+
status = "";
|
|
148
|
+
summary;
|
|
149
|
+
ticker;
|
|
150
|
+
depth;
|
|
151
|
+
constructor(summary, depth, ticker) {
|
|
152
|
+
this.summary = summary;
|
|
153
|
+
this.depth = depth;
|
|
154
|
+
this.ticker = ticker;
|
|
155
|
+
}
|
|
156
|
+
finish(status, costUsd) {
|
|
157
|
+
this.state = status === "succeeded" ? "ok" : "error";
|
|
158
|
+
this.status = status;
|
|
159
|
+
this.costUsd = costUsd;
|
|
160
|
+
}
|
|
161
|
+
interrupt() {
|
|
162
|
+
if (this.state === "running") {
|
|
163
|
+
this.state = "error";
|
|
164
|
+
this.status = "interrupted";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
render(width) {
|
|
168
|
+
const indent = " ".repeat(Math.max(0, this.depth - 1));
|
|
169
|
+
const mark = this.state === "running" ? style.cyan(this.ticker.spinner) : this.state === "ok" ? style.green("✓") : style.red("✗");
|
|
170
|
+
const tail = this.state === "running" ? style.gray(" running") : style.gray(` ${this.status} · ${usd(this.costUsd)}`);
|
|
171
|
+
return [truncateToWidth(`${indent} ↳ ${mark} subagent ${this.summary}${tail}`, width)];
|
|
172
|
+
}
|
|
173
|
+
invalidate() { }
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Single-line secret entry. Shows one bullet per character and nothing else; the value is handed to `onSubmit` and
|
|
177
|
+
* dropped. It keeps no history, undo or kill ring.
|
|
178
|
+
*/
|
|
179
|
+
export class MaskedInput {
|
|
180
|
+
focused = false;
|
|
181
|
+
onSubmit;
|
|
182
|
+
onCancel;
|
|
183
|
+
value = "";
|
|
184
|
+
inPaste = false;
|
|
185
|
+
pasteBuffer = "";
|
|
186
|
+
prompt;
|
|
187
|
+
constructor(prompt) {
|
|
188
|
+
this.prompt = prompt;
|
|
189
|
+
}
|
|
190
|
+
get length() {
|
|
191
|
+
return this.value.length;
|
|
192
|
+
}
|
|
193
|
+
handleInput(data) {
|
|
194
|
+
if (this.inPaste && (matchesKey(data, "escape") || matchesKey(data, "ctrl+c"))) {
|
|
195
|
+
this.clear();
|
|
196
|
+
this.onCancel?.();
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (data.includes("\x1b[200~")) {
|
|
200
|
+
this.inPaste = true;
|
|
201
|
+
this.pasteBuffer = "";
|
|
202
|
+
data = data.replace("\x1b[200~", "");
|
|
203
|
+
}
|
|
204
|
+
if (this.inPaste) {
|
|
205
|
+
this.pasteBuffer += data;
|
|
206
|
+
const end = this.pasteBuffer.indexOf("\x1b[201~");
|
|
207
|
+
if (end === -1)
|
|
208
|
+
return;
|
|
209
|
+
this.value += this.pasteBuffer.slice(0, end).replace(/[\r\n]/g, "");
|
|
210
|
+
this.pasteBuffer = "";
|
|
211
|
+
this.inPaste = false;
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (matchesKey(data, "enter")) {
|
|
215
|
+
const value = this.value;
|
|
216
|
+
this.clear();
|
|
217
|
+
this.onSubmit?.(value);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
|
|
221
|
+
this.clear();
|
|
222
|
+
this.onCancel?.();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (matchesKey(data, "backspace")) {
|
|
226
|
+
this.value = this.value.slice(0, -1);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (matchesKey(data, "ctrl+u")) {
|
|
230
|
+
this.value = "";
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
// Printable characters only; escape sequences (arrows, function keys) are ignored.
|
|
234
|
+
if (!data.startsWith("\x1b") && !/[\u0000-\u001f\u007f]/.test(data))
|
|
235
|
+
this.value += data;
|
|
236
|
+
}
|
|
237
|
+
clear() {
|
|
238
|
+
this.value = "";
|
|
239
|
+
this.pasteBuffer = "";
|
|
240
|
+
this.inPaste = false;
|
|
241
|
+
}
|
|
242
|
+
render(width) {
|
|
243
|
+
const bullets = "•".repeat(Math.min(this.value.length, Math.max(0, width - visibleWidth(this.prompt) - 2)));
|
|
244
|
+
const cursor = this.focused ? CURSOR_MARKER : "";
|
|
245
|
+
return [truncateToWidth(`${this.prompt}${bullets}${cursor}`, width)];
|
|
246
|
+
}
|
|
247
|
+
invalidate() { }
|
|
248
|
+
}
|
package/tui/src/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { restoreTerminal, runInteractive, runSettingsScreen } from "./app.js";
|
|
2
|
+
export { createApprovalPrompter } from "./approvals.js";
|
|
3
|
+
export { runSessionPicker, sessionPickerItems } from "./session-picker.js";
|
|
4
|
+
export { helpText, parseSlashCommand, SLASH_COMMANDS } from "./commands.js";
|
|
5
|
+
export { escapeControl, oneLine } from "./text.js";
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { fuzzyFilter, matchesKey, SelectList, truncateToWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
import { fitLines } from "./components.js";
|
|
3
|
+
import { escapeControl } from "./text.js";
|
|
4
|
+
import { selectListTheme, style } from "./theme.js";
|
|
5
|
+
/** A list filtered by typing. With `freeText`, Enter on a filter that matches nothing picks the filter text. */
|
|
6
|
+
export class PickerView {
|
|
7
|
+
title;
|
|
8
|
+
hint;
|
|
9
|
+
items;
|
|
10
|
+
freeText;
|
|
11
|
+
maxVisible;
|
|
12
|
+
onPick;
|
|
13
|
+
onCancel;
|
|
14
|
+
labelWidth;
|
|
15
|
+
filter = "";
|
|
16
|
+
list;
|
|
17
|
+
filtered;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.title = options.title;
|
|
20
|
+
this.items = options.items;
|
|
21
|
+
this.freeText = options.freeText ?? false;
|
|
22
|
+
this.maxVisible = options.maxVisible;
|
|
23
|
+
this.onPick = options.onPick;
|
|
24
|
+
this.onCancel = options.onCancel;
|
|
25
|
+
this.hint = options.hint ?? "type to filter · ↑↓ move · enter select · esc back";
|
|
26
|
+
this.labelWidth = options.labelWidth ?? { min: 20, max: 32 };
|
|
27
|
+
this.filtered = this.items;
|
|
28
|
+
this.list = this.buildList();
|
|
29
|
+
}
|
|
30
|
+
buildList() {
|
|
31
|
+
const items = this.filtered.length > 0 ? this.filtered : [];
|
|
32
|
+
const list = new SelectList(items, Math.max(3, this.maxVisible()), selectListTheme, { minPrimaryColumnWidth: this.labelWidth.min, maxPrimaryColumnWidth: this.labelWidth.max });
|
|
33
|
+
list.onSelect = (item) => this.onPick(item.value);
|
|
34
|
+
list.onCancel = () => this.onCancel();
|
|
35
|
+
return list;
|
|
36
|
+
}
|
|
37
|
+
refilter() {
|
|
38
|
+
this.filtered = fuzzyFilter(this.items, this.filter, (item) => `${item.label} ${item.value.startsWith("\u0000") ? "" : item.value}`);
|
|
39
|
+
this.list = this.buildList();
|
|
40
|
+
}
|
|
41
|
+
handleInput(data) {
|
|
42
|
+
if (matchesKey(data, "backspace")) {
|
|
43
|
+
if (this.filter.length > 0) {
|
|
44
|
+
this.filter = this.filter.slice(0, -1);
|
|
45
|
+
this.refilter();
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (matchesKey(data, "enter") && this.filtered.length === 0) {
|
|
50
|
+
if (this.freeText && this.filter.trim())
|
|
51
|
+
this.onPick(this.filter.trim());
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (matchesKey(data, "escape") && this.filter.length > 0) {
|
|
55
|
+
this.filter = "";
|
|
56
|
+
this.refilter();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!data.startsWith("\x1b") && !/[\u0000-\u001f\u007f]/.test(data) && data.length > 0) {
|
|
60
|
+
this.filter += data;
|
|
61
|
+
this.refilter();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.list.handleInput(data);
|
|
65
|
+
}
|
|
66
|
+
render(width) {
|
|
67
|
+
const filterLine = this.filter ? `${style.gray("filter:")} ${escapeControl(this.filter)}` : style.gray("filter: (type to filter)");
|
|
68
|
+
const lines = [truncateToWidth(filterLine, width)];
|
|
69
|
+
if (this.filtered.length === 0) {
|
|
70
|
+
lines.push(style.gray(this.freeText && this.filter.trim() ? ` enter uses "${escapeControl(this.filter.trim())}"` : " no matches"));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
lines.push(...this.list.render(width));
|
|
74
|
+
}
|
|
75
|
+
return fitLines(lines, width);
|
|
76
|
+
}
|
|
77
|
+
invalidate() { }
|
|
78
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { matchesKey, ProcessTerminal, TuiMainScreen, truncateToWidth } from "@earendil-works/pi-tui";
|
|
2
|
+
import { startTui, stopTui } from "./app.js";
|
|
3
|
+
import { fitLines, SafeContainer } from "./components.js";
|
|
4
|
+
import { PickerView } from "./picker.js";
|
|
5
|
+
import { escapeControl, oneLine, usd } from "./text.js";
|
|
6
|
+
import { style } from "./theme.js";
|
|
7
|
+
function ago(iso, now) {
|
|
8
|
+
const minutes = Math.floor((now - Date.parse(iso)) / 60_000);
|
|
9
|
+
if (!Number.isFinite(minutes) || minutes < 1)
|
|
10
|
+
return "just now";
|
|
11
|
+
if (minutes < 60)
|
|
12
|
+
return `${minutes}m ago`;
|
|
13
|
+
if (minutes < 24 * 60)
|
|
14
|
+
return `${Math.floor(minutes / 60)}h ago`;
|
|
15
|
+
return `${Math.floor(minutes / (24 * 60))}d ago`;
|
|
16
|
+
}
|
|
17
|
+
/** One list row per session: the title as the label; turns, cost, age and id as the description (the id makes it searchable). */
|
|
18
|
+
export function sessionPickerItems(sessions, now) {
|
|
19
|
+
return sessions.map((s) => ({
|
|
20
|
+
value: s.id,
|
|
21
|
+
label: oneLine(escapeControl(s.title), 60) || "(untitled)",
|
|
22
|
+
description: `${s.turns} turn${s.turns === 1 ? "" : "s"} · ${usd(s.costUsd)} · ${ago(s.updatedAt, now)} · ${escapeControl(s.id)}`,
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
/** The picker screen: a heading, the searchable list (the settings screen's picker) and its key hint. */
|
|
26
|
+
class SessionPickerScreen {
|
|
27
|
+
picker;
|
|
28
|
+
count;
|
|
29
|
+
onCancel;
|
|
30
|
+
constructor(picker, count, onCancel) {
|
|
31
|
+
this.picker = picker;
|
|
32
|
+
this.count = count;
|
|
33
|
+
this.onCancel = onCancel;
|
|
34
|
+
}
|
|
35
|
+
handleInput(data) {
|
|
36
|
+
if (matchesKey(data, "ctrl+c"))
|
|
37
|
+
return this.onCancel();
|
|
38
|
+
this.picker.handleInput(data);
|
|
39
|
+
}
|
|
40
|
+
render(width) {
|
|
41
|
+
const heading = `${style.bold("Resume a session")} ${style.gray(`(${this.count} recent)`)}`;
|
|
42
|
+
return fitLines([truncateToWidth(heading, width), ...this.picker.render(width), style.gray(truncateToWidth(this.picker.hint, width))], width);
|
|
43
|
+
}
|
|
44
|
+
invalidate() { }
|
|
45
|
+
}
|
|
46
|
+
/** The picker screen as a component, for `nexo --resume` and for `/resume` inside the chat. */
|
|
47
|
+
export function createSessionPickerScreen(options) {
|
|
48
|
+
const items = sessionPickerItems(options.sessions, options.now);
|
|
49
|
+
const picker = new PickerView({
|
|
50
|
+
title: "Resume a session",
|
|
51
|
+
items,
|
|
52
|
+
maxVisible: () => Math.max(3, Math.min(20, options.rows() - 9)),
|
|
53
|
+
onPick: options.onPick,
|
|
54
|
+
onCancel: options.onCancel,
|
|
55
|
+
hint: "type to filter · ↑↓ move · enter resume · esc cancel",
|
|
56
|
+
labelWidth: { min: 24, max: 48 },
|
|
57
|
+
});
|
|
58
|
+
return new SessionPickerScreen(picker, items.length, options.onCancel);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Shows a searchable list of sessions for `nexo --resume`: type to filter by title or id, ↑↓ to move, enter to resume,
|
|
62
|
+
* esc (with an empty filter) or ctrl-c to cancel. Resolves the picked session id, or null when cancelled. The terminal
|
|
63
|
+
* is restored before it resolves.
|
|
64
|
+
*/
|
|
65
|
+
export async function runSessionPicker(options) {
|
|
66
|
+
const terminal = options.terminal ?? new ProcessTerminal();
|
|
67
|
+
const tui = new TuiMainScreen(terminal);
|
|
68
|
+
let resolveDone = () => { };
|
|
69
|
+
const done = new Promise((resolve) => {
|
|
70
|
+
resolveDone = resolve;
|
|
71
|
+
});
|
|
72
|
+
const screen = createSessionPickerScreen({
|
|
73
|
+
sessions: options.sessions,
|
|
74
|
+
now: (options.now ?? Date.now)(),
|
|
75
|
+
rows: () => terminal.rows,
|
|
76
|
+
onPick: (id) => resolveDone(id),
|
|
77
|
+
onCancel: () => resolveDone(null),
|
|
78
|
+
});
|
|
79
|
+
const root = new SafeContainer();
|
|
80
|
+
root.addChild(screen);
|
|
81
|
+
tui.addChild(root);
|
|
82
|
+
tui.setFocus(screen);
|
|
83
|
+
const onAbort = () => resolveDone(null);
|
|
84
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
85
|
+
startTui(tui);
|
|
86
|
+
try {
|
|
87
|
+
if (options.signal?.aborted)
|
|
88
|
+
resolveDone(null);
|
|
89
|
+
return await done;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
93
|
+
try {
|
|
94
|
+
await terminal.drainInput(1000, 50);
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
stopTui(tui);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|