@carrieros/cli 0.1.0 → 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/README.md +142 -57
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +102 -13
- package/dist/commands/context.js +160 -0
- package/dist/commands/doctor.js +53 -8
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +3 -1
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +44 -41
- package/dist/commands/logout.js +13 -4
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +39 -3
- package/dist/commands/modes.js +4 -1
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +152 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +219 -65
- package/dist/commands/settings.js +87 -0
- package/dist/commands/update.js +159 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +212 -16
- package/dist/commands/whoami.js +2 -1
- package/dist/ide/editor-context.js +172 -0
- package/dist/ide/ide-session.js +94 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +30 -1
- package/dist/services/api.js +171 -13
- package/dist/services/chat-stream.js +145 -0
- package/dist/services/checkpoints.js +116 -0
- package/dist/services/client-tools.js +260 -0
- package/dist/services/command-coordinator.js +200 -0
- package/dist/services/command-policy.js +404 -0
- package/dist/services/config.js +249 -22
- package/dist/services/context.js +307 -6
- package/dist/services/device-auth.js +261 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/git-tools.js +248 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +64 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/job-helper.js +307 -0
- package/dist/services/local-files.js +517 -0
- package/dist/services/local-tools.js +432 -0
- package/dist/services/login-flow.js +120 -10
- package/dist/services/meta.js +3 -1
- package/dist/services/mutation-coordinator.js +161 -0
- package/dist/services/mutations.js +493 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +13 -5
- package/dist/services/packaged.js +24 -0
- package/dist/services/process-runner.js +406 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/secure-store.js +197 -0
- package/dist/services/session-resolver.js +108 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +23 -1
- package/dist/services/update.js +286 -0
- package/dist/services/workspace-registry.js +309 -0
- package/dist/services/workspace.js +573 -0
- package/dist/shell/shell-chat.js +1115 -59
- package/dist/shell/shell-commands.js +220 -19
- package/dist/shell/shell-prompt.js +89 -37
- package/dist/shell/shell-renderer.js +19 -38
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +232 -44
- package/dist/types/index.js +3 -1
- package/dist/ui/approval.js +81 -0
- package/dist/ui/banner.js +55 -13
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +87 -0
- package/dist/ui/chat-renderer.js +223 -29
- package/dist/ui/color-picker.js +21 -0
- package/dist/ui/colors.js +9 -1
- package/dist/ui/command-filter.js +40 -5
- package/dist/ui/command-palette.js +122 -62
- package/dist/ui/command-registry.js +85 -11
- package/dist/ui/command-review.js +138 -0
- package/dist/ui/composer-layout.js +41 -0
- package/dist/ui/conversation.js +398 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +18 -1
- package/dist/ui/markdown.js +63 -9
- package/dist/ui/message.js +14 -31
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +289 -0
- package/dist/ui/select.js +193 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +17 -0
- package/dist/ui/trust.js +93 -0
- package/dist/ui/ui.js +4 -1
- package/package.json +17 -6
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { displayCommand } from "../services/command-policy.js";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
4
|
+
import { clip, stripControl, wrapText } from "./conversation.js";
|
|
5
|
+
/**
|
|
6
|
+
* What a person sees before and after a local command.
|
|
7
|
+
*
|
|
8
|
+
* The card is deliberately compact — a continuation of the conversation, not a
|
|
9
|
+
* form — but it still carries EVERY fact the decision needs: the exact command,
|
|
10
|
+
* where it will run, its risk tier, whether it may use the network, the real
|
|
11
|
+
* script text behind an npm script, the model's reason, and any policy notes.
|
|
12
|
+
* Nothing is summarised away; only the labelling is lighter.
|
|
13
|
+
*
|
|
14
|
+
* It is committed to the transcript, so what was approved stays on screen.
|
|
15
|
+
*/
|
|
16
|
+
const TIER_TEXT = {
|
|
17
|
+
R0: "R0 · read-only",
|
|
18
|
+
R1: "R1 · runs project code",
|
|
19
|
+
R2: "R2 · changes files"
|
|
20
|
+
};
|
|
21
|
+
const NETWORK_TEXT = {
|
|
22
|
+
"none-expected": "no network expected",
|
|
23
|
+
"project-code-may-use-network": "not restricted",
|
|
24
|
+
"uses-network": "uses the network"
|
|
25
|
+
};
|
|
26
|
+
export const safeText = stripControl;
|
|
27
|
+
const INDENT = MARGIN + " ";
|
|
28
|
+
/** Width available inside the card. */
|
|
29
|
+
const inner = () => Math.max(24, contentWidth() - 4);
|
|
30
|
+
/** ` Label value` with continuation lines aligned under the value. */
|
|
31
|
+
function field(label, value, paint = c.dim) {
|
|
32
|
+
const pad = 8;
|
|
33
|
+
const head = label.padEnd(pad);
|
|
34
|
+
return wrapText(safeText(value), inner() - pad).map((l, i) => INDENT + c.dim(i === 0 ? head : " ".repeat(pad)) + paint(l));
|
|
35
|
+
}
|
|
36
|
+
export function renderCommandReview(d, reason, repository) {
|
|
37
|
+
const tier = d.tier === "R2" ? c.warn(TIER_TEXT[d.tier]) : d.tier === "R1" ? c.value(TIER_TEXT[d.tier]) : c.dim(TIER_TEXT[d.tier]);
|
|
38
|
+
const lines = [
|
|
39
|
+
"",
|
|
40
|
+
MARGIN + c.warn("◆") + " " + c.title("Carrier OS wants to run:"),
|
|
41
|
+
""
|
|
42
|
+
];
|
|
43
|
+
// A command can now be aimed at a repository that is not the active one, so
|
|
44
|
+
// WHICH repository is part of what is being approved. It sits above the
|
|
45
|
+
// command, before the absolute working directory that confirms it, and reads
|
|
46
|
+
// the same as the file-change card.
|
|
47
|
+
if (repository) {
|
|
48
|
+
lines.push(INDENT + c.dim("Repository ") + c.value(clip(safeText(repository), Math.max(12, inner() - 14))), "");
|
|
49
|
+
}
|
|
50
|
+
for (const l of wrapText(safeText(displayCommand(d.program, d.args)), inner()))
|
|
51
|
+
lines.push(INDENT + c.accent(l));
|
|
52
|
+
for (const l of wrapText(safeText(d.cwdAbsolute), inner()))
|
|
53
|
+
lines.push(INDENT + c.file(l));
|
|
54
|
+
lines.push(INDENT + tier + c.dim(" · network: " + NETWORK_TEXT[d.network]));
|
|
55
|
+
lines.push("");
|
|
56
|
+
if (d.script) {
|
|
57
|
+
lines.push(...field("Script", d.script.text, c.value));
|
|
58
|
+
if (d.script.pre)
|
|
59
|
+
lines.push(...field("pre", d.script.pre, c.value));
|
|
60
|
+
if (d.script.post)
|
|
61
|
+
lines.push(...field("post", d.script.post, c.value));
|
|
62
|
+
}
|
|
63
|
+
if (reason)
|
|
64
|
+
lines.push(...field("Why", reason));
|
|
65
|
+
for (const note of d.notes) {
|
|
66
|
+
if (/^Read-only|^Runs project code/.test(note))
|
|
67
|
+
continue; // already said by the tier
|
|
68
|
+
lines.push(INDENT + c.dim("· " + clip(note, inner() - 2)));
|
|
69
|
+
}
|
|
70
|
+
lines.push("");
|
|
71
|
+
return lines;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A one-line result: what ran, how it ended, how long it took — plus test
|
|
75
|
+
* counts when the output plainly states them.
|
|
76
|
+
*/
|
|
77
|
+
export function renderCommandSummary(d, r, errorCode, message) {
|
|
78
|
+
const what = d ? displayCommand(d.program, d.args) : "command";
|
|
79
|
+
if (!r) {
|
|
80
|
+
const declined = errorCode === "COMMAND_REJECTED" || errorCode === "APPROVAL_TIMEOUT" || errorCode === "COMMAND_CANCELLED";
|
|
81
|
+
const icon = declined ? c.warn("✗") : c.err("✕");
|
|
82
|
+
return [MARGIN + icon + " " + c.dim(clip(`${what} — ${safeText(message ?? errorCode ?? "not run")}`, inner()))];
|
|
83
|
+
}
|
|
84
|
+
const secs = `${(r.duration_ms / 1000).toFixed(1)}s`;
|
|
85
|
+
const extra = [
|
|
86
|
+
summariseOutput(r.output),
|
|
87
|
+
r.truncated ? "output truncated" : "",
|
|
88
|
+
r.redaction_count ? `${r.redaction_count} secret${r.redaction_count === 1 ? "" : "s"} redacted` : ""
|
|
89
|
+
].filter(Boolean);
|
|
90
|
+
const tail = c.dim([secs, ...extra].join(" · "));
|
|
91
|
+
if (r.timed_out)
|
|
92
|
+
return [MARGIN + c.err("✕") + " " + c.value(what) + c.dim(" timed out — process tree stopped · ") + tail];
|
|
93
|
+
if (r.cancelled)
|
|
94
|
+
return [MARGIN + c.warn("✗") + " " + c.value(what) + c.dim(" cancelled — process tree stopped · ") + tail];
|
|
95
|
+
if (r.exit_code === 0)
|
|
96
|
+
return [MARGIN + c.ok("✓") + " " + c.value(what) + c.dim(" · ") + tail];
|
|
97
|
+
return [MARGIN + c.err("✕") + " " + c.value(what) + c.dim(` exited ${r.exit_code} · `) + tail];
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Test counts, when a runner states them plainly. Presentation only: the exit
|
|
101
|
+
* code remains the truth about success, and an unrecognised format simply adds
|
|
102
|
+
* nothing to the line.
|
|
103
|
+
*/
|
|
104
|
+
export function summariseOutput(output) {
|
|
105
|
+
const text = safeText(output);
|
|
106
|
+
// node --test — "# pass 97" (tap reporter) or "ℹ pass 97" (spec reporter)
|
|
107
|
+
const nodePass = text.match(/^[#ℹ] pass (\d+)\s*$/m);
|
|
108
|
+
const nodeFail = text.match(/^[#ℹ] fail (\d+)\s*$/m);
|
|
109
|
+
if (nodePass && nodeFail) {
|
|
110
|
+
const skipped = text.match(/^[#ℹ] skipped (\d+)\s*$/m);
|
|
111
|
+
return [
|
|
112
|
+
`${nodePass[1]} passed`,
|
|
113
|
+
`${nodeFail[1]} failed`,
|
|
114
|
+
skipped && skipped[1] !== "0" ? `${skipped[1]} skipped` : ""
|
|
115
|
+
]
|
|
116
|
+
.filter(Boolean)
|
|
117
|
+
.join(" · ");
|
|
118
|
+
}
|
|
119
|
+
// jest / vitest: "Tests: 11 failed, 3612 passed, 3623 total"
|
|
120
|
+
const jest = text.match(/^Tests:\s+(.+?)\s*$/m);
|
|
121
|
+
if (jest) {
|
|
122
|
+
const failed = jest[1].match(/(\d+) failed/);
|
|
123
|
+
const passed = jest[1].match(/(\d+) passed/);
|
|
124
|
+
const skipped = jest[1].match(/(\d+) (?:skipped|todo)/);
|
|
125
|
+
if (passed || failed) {
|
|
126
|
+
return [passed ? `${passed[1]} passed` : "", failed ? `${failed[1]} failed` : "", skipped ? `${skipped[1]} skipped` : ""]
|
|
127
|
+
.filter(Boolean)
|
|
128
|
+
.join(" · ");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// mocha
|
|
132
|
+
const passing = text.match(/^\s*(\d+) passing/m);
|
|
133
|
+
if (passing) {
|
|
134
|
+
const failing = text.match(/^\s*(\d+) failing/m);
|
|
135
|
+
return [`${passing[1]} passed`, failing ? `${failing[1]} failed` : ""].filter(Boolean).join(" · ");
|
|
136
|
+
}
|
|
137
|
+
return "";
|
|
138
|
+
}
|
|
@@ -25,3 +25,44 @@ export function contentWidth() {
|
|
|
25
25
|
export function textWidth() {
|
|
26
26
|
return Math.max(20, Math.min(contentWidth(), READABLE_MAX));
|
|
27
27
|
}
|
|
28
|
+
// SGR colour runs plus OSC strings. An OSC payload (a hyperlink URL, a window
|
|
29
|
+
// title) occupies no columns, so a matcher that only knows SGR over-counts any
|
|
30
|
+
// line carrying one.
|
|
31
|
+
const ANSI = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
32
|
+
/** Visible column count of a styled string — escape sequences don't count. */
|
|
33
|
+
export function visibleWidth(s) {
|
|
34
|
+
return s.replace(ANSI, "").length;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Clamp a styled line to `width` visible columns, keeping its colour codes.
|
|
38
|
+
*
|
|
39
|
+
* The screen engine writes one transcript line per terminal row and does no
|
|
40
|
+
* wrap accounting, so ANY line wider than the terminal steals a second physical
|
|
41
|
+
* row and shifts everything below it — which is how model output containing a
|
|
42
|
+
* 250-character URL ends up overwriting the composer. This is the last line of
|
|
43
|
+
* defence: it runs over everything the markdown renderer emits, so a path that
|
|
44
|
+
* forgets to wrap degrades to a clipped line instead of a corrupted screen.
|
|
45
|
+
*/
|
|
46
|
+
export function clampAnsi(s, width = contentWidth()) {
|
|
47
|
+
if (visibleWidth(s) <= width)
|
|
48
|
+
return s;
|
|
49
|
+
const max = Math.max(1, width - 1); // room for the ellipsis
|
|
50
|
+
let out = "";
|
|
51
|
+
let vis = 0;
|
|
52
|
+
let i = 0;
|
|
53
|
+
while (i < s.length && vis < max) {
|
|
54
|
+
if (s[i] === "\x1b") {
|
|
55
|
+
// Copy the escape verbatim; it costs no columns.
|
|
56
|
+
const m = /^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|^\x1b\[[0-9;]*m/.exec(s.slice(i));
|
|
57
|
+
if (m) {
|
|
58
|
+
out += m[0];
|
|
59
|
+
i += m[0].length;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
out += s[i];
|
|
64
|
+
vis++;
|
|
65
|
+
i++;
|
|
66
|
+
}
|
|
67
|
+
return `${out}…\x1b[0m`;
|
|
68
|
+
}
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
import { c } from "./colors.js";
|
|
2
|
+
import { MARGIN, contentWidth, visibleWidth } from "./composer-layout.js";
|
|
3
|
+
import { renderMarkdown } from "./markdown.js";
|
|
4
|
+
/** Elapsed time is noise below this; a person only cares once it drags. */
|
|
5
|
+
const SHOW_ELAPSED_AFTER_MS = 2000;
|
|
6
|
+
/**
|
|
7
|
+
* Text that cannot drive the terminal: escape sequences and control characters
|
|
8
|
+
* removed. Everything shown from a file, a command or its output goes through
|
|
9
|
+
* this — output is untrusted input, and a stray escape sequence could move the
|
|
10
|
+
* cursor, repaint rows the renderer owns, or hide text from the person
|
|
11
|
+
* approving a change.
|
|
12
|
+
*/
|
|
13
|
+
export function stripControl(text) {
|
|
14
|
+
return text
|
|
15
|
+
.replace(/\x1b\][^\x07\x1b]*(\x07|\x1b\\)/g, "")
|
|
16
|
+
.replace(/\x1b\[[0-9;?]*[ -/]*[@-~]/g, "")
|
|
17
|
+
.replace(/\x1b[@-_]/g, "")
|
|
18
|
+
// eslint-disable-next-line no-control-regex
|
|
19
|
+
.replace(/[\x00-\x08\x0b-\x1f\x7f]/g, "");
|
|
20
|
+
}
|
|
21
|
+
/** Trim to a visible width, ellipsising rather than wrapping. */
|
|
22
|
+
export function clip(text, width) {
|
|
23
|
+
const plain = stripControl(text);
|
|
24
|
+
if (visibleWidth(plain) <= width)
|
|
25
|
+
return plain;
|
|
26
|
+
return [...plain].slice(0, Math.max(1, width - 1)).join("") + "…";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Shorten a command for an activity line, keeping both ends: `npm run build …
|
|
30
|
+
* --sourcemap` says more than the first 60 characters do.
|
|
31
|
+
*/
|
|
32
|
+
export function clipCommand(text, width) {
|
|
33
|
+
const plain = stripControl(text);
|
|
34
|
+
if (visibleWidth(plain) <= width)
|
|
35
|
+
return plain;
|
|
36
|
+
const keepEnd = Math.max(8, Math.floor((width - 3) * 0.35));
|
|
37
|
+
const keepStart = Math.max(8, width - 3 - keepEnd);
|
|
38
|
+
return [...plain].slice(0, keepStart).join("") + " … " + [...plain].slice(-keepEnd).join("");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Wrap `text` to `width`, breaking on spaces but never dropping characters —
|
|
42
|
+
* for anything a person needs in full: a command, a path, a reason.
|
|
43
|
+
*/
|
|
44
|
+
export function wrapText(text, width) {
|
|
45
|
+
const w = Math.max(16, width);
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const paragraph of text.split("\n")) {
|
|
48
|
+
let cur = "";
|
|
49
|
+
for (const word of paragraph.split(/\s+/).filter(Boolean)) {
|
|
50
|
+
if (!cur)
|
|
51
|
+
cur = word;
|
|
52
|
+
else if ([...cur].length + 1 + [...word].length <= w)
|
|
53
|
+
cur = `${cur} ${word}`;
|
|
54
|
+
else {
|
|
55
|
+
out.push(cur);
|
|
56
|
+
cur = word;
|
|
57
|
+
}
|
|
58
|
+
while ([...cur].length > w) {
|
|
59
|
+
out.push([...cur].slice(0, w).join(""));
|
|
60
|
+
cur = [...cur].slice(w).join("");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
out.push(cur);
|
|
64
|
+
}
|
|
65
|
+
return out.length ? out : [""];
|
|
66
|
+
}
|
|
67
|
+
/** Elapsed seconds, shown only once an operation has actually taken time. */
|
|
68
|
+
export function elapsedLabel(sinceMs, now = Date.now()) {
|
|
69
|
+
const ms = now - sinceMs;
|
|
70
|
+
if (ms < SHOW_ELAPSED_AFTER_MS)
|
|
71
|
+
return "";
|
|
72
|
+
return ms < 60_000 ? ` · ${Math.round(ms / 1000)}s` : ` · ${Math.floor(ms / 60_000)}m${Math.round((ms % 60_000) / 1000)}s`;
|
|
73
|
+
}
|
|
74
|
+
/** Keeps the last few lines of live command output for the transient tail. */
|
|
75
|
+
/**
|
|
76
|
+
* Lines worth NOT showing a person while a command runs.
|
|
77
|
+
*
|
|
78
|
+
* Display only — the model still receives the full bounded result from the
|
|
79
|
+
* process runner; this trims the live terminal tail so framework startup noise
|
|
80
|
+
* does not bury the test progress a person is watching for. Errors, failures,
|
|
81
|
+
* test counts and warnings that name a real problem are never matched here.
|
|
82
|
+
*/
|
|
83
|
+
export function isNoiseLine(line) {
|
|
84
|
+
const t = line.trim();
|
|
85
|
+
if (!t)
|
|
86
|
+
return true;
|
|
87
|
+
return (/^\(node:\d+\)\s+ExperimentalWarning/.test(t) ||
|
|
88
|
+
/^\(Use `node --trace-warnings/.test(t) ||
|
|
89
|
+
// Winston / pino style structured logs: "info:", "debug:", "http:", plus a
|
|
90
|
+
// trailing {"service":…,"timestamp":…} — never "error:" or "warn:".
|
|
91
|
+
/^(info|debug|verbose|silly|trace|http)\s*:/i.test(t) ||
|
|
92
|
+
/^\[\d{4}-\d\d-\d\dT[\d:.]+Z?\]\s*(info|debug|verbose|http)\b/i.test(t) ||
|
|
93
|
+
/"service"\s*:\s*"[^"]+"\s*,\s*"timestamp"/.test(t));
|
|
94
|
+
}
|
|
95
|
+
export class OutputTail {
|
|
96
|
+
max;
|
|
97
|
+
denoise;
|
|
98
|
+
lines = [""];
|
|
99
|
+
constructor(max = 5,
|
|
100
|
+
/** When true, framework/log noise is dropped from the live tail (display only). */
|
|
101
|
+
denoise = false) {
|
|
102
|
+
this.max = max;
|
|
103
|
+
this.denoise = denoise;
|
|
104
|
+
}
|
|
105
|
+
push(text) {
|
|
106
|
+
const parts = text.replace(/\r\n/g, "\n").split("\n");
|
|
107
|
+
this.lines[this.lines.length - 1] += parts[0];
|
|
108
|
+
for (const p of parts.slice(1))
|
|
109
|
+
this.lines.push(p);
|
|
110
|
+
if (this.denoise)
|
|
111
|
+
this.lines = this.dropNoise(this.lines);
|
|
112
|
+
if (this.lines.length > this.max + 1)
|
|
113
|
+
this.lines.splice(0, this.lines.length - (this.max + 1));
|
|
114
|
+
}
|
|
115
|
+
/** Drop noise lines, but keep the trailing (possibly partial) line intact. */
|
|
116
|
+
dropNoise(lines) {
|
|
117
|
+
const last = lines[lines.length - 1];
|
|
118
|
+
const kept = lines.slice(0, -1).filter((l) => !isNoiseLine(l));
|
|
119
|
+
kept.push(last);
|
|
120
|
+
return kept.length ? kept : [""];
|
|
121
|
+
}
|
|
122
|
+
clear() {
|
|
123
|
+
this.lines = [""];
|
|
124
|
+
}
|
|
125
|
+
view(width) {
|
|
126
|
+
const shown = this.lines[this.lines.length - 1] === "" ? this.lines.slice(0, -1) : this.lines;
|
|
127
|
+
return shown
|
|
128
|
+
.slice(-this.max)
|
|
129
|
+
.filter((l) => l.trim())
|
|
130
|
+
.map((l) => clip(l, width));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* "Read 2 files, ran 1 command" — the one line a finished step leaves behind.
|
|
135
|
+
* The order is fixed, so a step always reads the same way.
|
|
136
|
+
*/
|
|
137
|
+
export function summariseStep(counts) {
|
|
138
|
+
const parts = [];
|
|
139
|
+
const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
|
|
140
|
+
const verb = (word) => (parts.length ? word.toLowerCase() : word);
|
|
141
|
+
if (counts.read)
|
|
142
|
+
parts.push(`${verb("Read")} ${plural(counts.read, "file", "files")}`);
|
|
143
|
+
if (counts.list)
|
|
144
|
+
parts.push(`${verb("Listed")} ${plural(counts.list, "directory", "directories")}`);
|
|
145
|
+
if (counts.search)
|
|
146
|
+
parts.push(`${verb("Searched")} ${plural(counts.search, "pattern", "patterns")}`);
|
|
147
|
+
if (counts.inspect)
|
|
148
|
+
parts.push(`${verb("Inspected")} Git ${plural(counts.inspect, "time", "times")}`);
|
|
149
|
+
if (counts.command)
|
|
150
|
+
parts.push(`${verb("Ran")} ${plural(counts.command, "command", "commands")}`);
|
|
151
|
+
if (counts.change)
|
|
152
|
+
parts.push(`${verb("Changed")} ${plural(counts.change, "file", "files")}`);
|
|
153
|
+
if (counts.other)
|
|
154
|
+
parts.push(`${verb("Completed")} ${plural(counts.other, "step", "steps")}`);
|
|
155
|
+
return parts.join(", ");
|
|
156
|
+
}
|
|
157
|
+
export function createRenderer(live) {
|
|
158
|
+
let statusLabel = "";
|
|
159
|
+
let statusSince = Date.now();
|
|
160
|
+
let step = null;
|
|
161
|
+
let command = null;
|
|
162
|
+
let preview = "";
|
|
163
|
+
let paused = false;
|
|
164
|
+
const width = () => Math.max(32, contentWidth() - 4);
|
|
165
|
+
const dot = () => c.accentLight("•");
|
|
166
|
+
const paint = () => {
|
|
167
|
+
if (paused)
|
|
168
|
+
return;
|
|
169
|
+
const lines = [];
|
|
170
|
+
if (command) {
|
|
171
|
+
lines.push(MARGIN + " " + c.value(clip(command.label, width() - 12)) + c.dim(elapsedLabel(command.since)));
|
|
172
|
+
lines.push(MARGIN + " " + c.dim("└ $ " + clipCommand(command.command, width() - 8)));
|
|
173
|
+
for (const l of command.tail.view(width() - 6))
|
|
174
|
+
lines.push(MARGIN + " " + c.dim(l));
|
|
175
|
+
}
|
|
176
|
+
else if (step) {
|
|
177
|
+
lines.push(MARGIN + " " + c.value(clip(step.label, width() - 12)) + c.dim(elapsedLabel(step.started)));
|
|
178
|
+
if (step.running.length > 1) {
|
|
179
|
+
for (const [i, label] of step.running.entries()) {
|
|
180
|
+
const branch = i === step.running.length - 1 ? "└ " : "├ ";
|
|
181
|
+
lines.push(MARGIN + " " + c.dim(branch + clip(label, width() - 6)));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (preview) {
|
|
186
|
+
lines.push(MARGIN + dot() + " " + c.dim(clip(preview, width())));
|
|
187
|
+
}
|
|
188
|
+
else if (statusLabel) {
|
|
189
|
+
lines.push(MARGIN + " " + c.dim(clip(statusLabel, width())) + c.dim(elapsedLabel(statusSince)));
|
|
190
|
+
}
|
|
191
|
+
live.setStatus(lines);
|
|
192
|
+
};
|
|
193
|
+
// One slow timer: nothing spins, but elapsed time stays honest.
|
|
194
|
+
const timer = setInterval(paint, 500);
|
|
195
|
+
timer.unref?.();
|
|
196
|
+
return {
|
|
197
|
+
user(text) {
|
|
198
|
+
const lines = wrapText(text.trim(), contentWidth() - 3);
|
|
199
|
+
live.commit(["", ...lines.map((l, i) => MARGIN + (i === 0 ? c.brandFixed("›") : " ") + " " + c.title(l)), ""]);
|
|
200
|
+
},
|
|
201
|
+
assistant(text) {
|
|
202
|
+
const md = renderMarkdown(text.trim());
|
|
203
|
+
while (md.length && md[0].trim() === "")
|
|
204
|
+
md.shift();
|
|
205
|
+
while (md.length && md[md.length - 1].trim() === "")
|
|
206
|
+
md.pop();
|
|
207
|
+
if (!md.length)
|
|
208
|
+
return;
|
|
209
|
+
preview = "";
|
|
210
|
+
// The dot marks where a message starts. A message that opens with a list
|
|
211
|
+
// or a heading already has its own marker, so adding one stacks two
|
|
212
|
+
// bullets on the same line — indent the block instead.
|
|
213
|
+
const ownMarker = /^\s*([•\-*•]|\d+[.)]|#)/.test(md[0]);
|
|
214
|
+
live.commit([...md.map((l, i) => MARGIN + (i === 0 && !ownMarker ? dot() + " " : " ") + l), ""]);
|
|
215
|
+
},
|
|
216
|
+
writing(text) {
|
|
217
|
+
preview = text.split("\n").filter((l) => l.trim()).pop() ?? "";
|
|
218
|
+
if (!step && !command)
|
|
219
|
+
paint();
|
|
220
|
+
},
|
|
221
|
+
status(_next, label) {
|
|
222
|
+
if (label !== statusLabel)
|
|
223
|
+
statusSince = Date.now();
|
|
224
|
+
statusLabel = label;
|
|
225
|
+
if (!step && !command)
|
|
226
|
+
paint();
|
|
227
|
+
},
|
|
228
|
+
toolStart(label, kind) {
|
|
229
|
+
if (!step)
|
|
230
|
+
step = { label, started: Date.now(), running: [], counts: blankCounts() };
|
|
231
|
+
step.counts[kind] += 1;
|
|
232
|
+
step.running.push(label);
|
|
233
|
+
// One tool at a time reads better as itself than as a group.
|
|
234
|
+
step.label = step.running.length === 1 ? label : groupLabel(step.counts);
|
|
235
|
+
paint();
|
|
236
|
+
},
|
|
237
|
+
toolEnd(label, kind, summary, ok = true) {
|
|
238
|
+
if (!step)
|
|
239
|
+
return;
|
|
240
|
+
const i = step.running.indexOf(label);
|
|
241
|
+
if (i !== -1)
|
|
242
|
+
step.running.splice(i, 1);
|
|
243
|
+
if (step.running.length === 1)
|
|
244
|
+
step.label = step.running[0];
|
|
245
|
+
// A failed tool says so, concisely, right where it happened.
|
|
246
|
+
if (!ok && summary) {
|
|
247
|
+
live.commit([MARGIN + " " + c.err("✕ ") + c.dim(clip(summary, width()))]);
|
|
248
|
+
}
|
|
249
|
+
else if (ok && summary && kind === "inspect") {
|
|
250
|
+
// Git inspections each leave a result line; reads/searches aggregate
|
|
251
|
+
// into the step summary so many small reads stay quiet.
|
|
252
|
+
live.commit([MARGIN + " " + c.ok("✓ ") + c.dim(clip(summary, width()))]);
|
|
253
|
+
}
|
|
254
|
+
paint();
|
|
255
|
+
},
|
|
256
|
+
stepEnd() {
|
|
257
|
+
if (!step)
|
|
258
|
+
return;
|
|
259
|
+
// Inspections already left their own ✓ lines; the aggregate is the reads
|
|
260
|
+
// and searches that were kept quiet during the step.
|
|
261
|
+
const summary = summariseStep({ ...step.counts, inspect: 0 });
|
|
262
|
+
const took = elapsedLabel(step.started);
|
|
263
|
+
const hadInspect = step.counts.inspect > 0;
|
|
264
|
+
step = null;
|
|
265
|
+
if (summary)
|
|
266
|
+
live.commit([MARGIN + " " + c.ok("✓ ") + c.dim(summary + took), ""]);
|
|
267
|
+
else if (hadInspect)
|
|
268
|
+
live.commit([""]); // spacer after the ✓ inspection lines
|
|
269
|
+
paint();
|
|
270
|
+
},
|
|
271
|
+
commit(lines) {
|
|
272
|
+
live.commit(lines);
|
|
273
|
+
},
|
|
274
|
+
async borrow(fn) {
|
|
275
|
+
paused = true;
|
|
276
|
+
live.setStatus([]);
|
|
277
|
+
try {
|
|
278
|
+
return await fn();
|
|
279
|
+
}
|
|
280
|
+
finally {
|
|
281
|
+
paused = false;
|
|
282
|
+
paint();
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
commandStarted(label, cmd) {
|
|
286
|
+
command = { label, command: cmd, since: Date.now(), tail: new OutputTail(5, true) };
|
|
287
|
+
paint();
|
|
288
|
+
},
|
|
289
|
+
commandOutput(text) {
|
|
290
|
+
command?.tail.push(text);
|
|
291
|
+
},
|
|
292
|
+
commandEnded() {
|
|
293
|
+
command = null;
|
|
294
|
+
paint();
|
|
295
|
+
},
|
|
296
|
+
clear() {
|
|
297
|
+
step = null;
|
|
298
|
+
command = null;
|
|
299
|
+
preview = "";
|
|
300
|
+
statusLabel = "";
|
|
301
|
+
live.setStatus([]);
|
|
302
|
+
},
|
|
303
|
+
stop() {
|
|
304
|
+
clearInterval(timer);
|
|
305
|
+
live.setStatus([]);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function blankCounts() {
|
|
310
|
+
return { read: 0, list: 0, search: 0, inspect: 0, command: 0, change: 0, other: 0 };
|
|
311
|
+
}
|
|
312
|
+
/** What to call a step that is doing several things at once. */
|
|
313
|
+
function groupLabel(counts) {
|
|
314
|
+
if (counts.command > 1)
|
|
315
|
+
return "Running commands";
|
|
316
|
+
if (counts.change > 1)
|
|
317
|
+
return "Preparing changes";
|
|
318
|
+
if (counts.search && !counts.read)
|
|
319
|
+
return "Searching the project";
|
|
320
|
+
return "Inspecting the project";
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* The compact result line for one completed tool, or undefined when the step's
|
|
324
|
+
* aggregate should speak for it instead.
|
|
325
|
+
*
|
|
326
|
+
* Reads and searches return undefined on success — they are summed into the
|
|
327
|
+
* step line ("Read 3 files") rather than printed one by one. Git inspections
|
|
328
|
+
* return a real one-liner. Any failure returns a concise reason, so a broken
|
|
329
|
+
* tool reads as "✕ git_diff failed · <reason>" instead of a stack trace.
|
|
330
|
+
*
|
|
331
|
+
* `detail` is the tool's result object on success, or an error message string
|
|
332
|
+
* on failure. Nothing here exposes protocol fields.
|
|
333
|
+
*/
|
|
334
|
+
export function toolSummary(name, ok, detail) {
|
|
335
|
+
if (!ok) {
|
|
336
|
+
const reason = typeof detail === "string" && detail.trim() ? ` · ${detail.trim()}` : "";
|
|
337
|
+
return `${name} failed${reason}`;
|
|
338
|
+
}
|
|
339
|
+
const r = (detail ?? {});
|
|
340
|
+
const n = (v) => (typeof v === "number" && Number.isFinite(v) ? v : 0);
|
|
341
|
+
const arr = (v) => (Array.isArray(v) ? v.length : 0);
|
|
342
|
+
if (name === "resolve_local_file") {
|
|
343
|
+
if (r.status === "resolved")
|
|
344
|
+
return `Resolved → ${r.repository}/${r.path}`;
|
|
345
|
+
if (r.status === "ambiguous")
|
|
346
|
+
return `${Array.isArray(r.candidates) ? r.candidates.length : 0} matching files — pick one`;
|
|
347
|
+
if (r.status === "not_found")
|
|
348
|
+
return "no match found";
|
|
349
|
+
return "resolve_local_file";
|
|
350
|
+
}
|
|
351
|
+
if (name === "git_status") {
|
|
352
|
+
if (r.clean === true)
|
|
353
|
+
return `git_status · clean${r.branch ? ` · branch ${r.branch}` : ""}`;
|
|
354
|
+
const parts = [];
|
|
355
|
+
const modified = arr(r.staged) + arr(r.unstaged);
|
|
356
|
+
if (modified)
|
|
357
|
+
parts.push(`${modified} modified`);
|
|
358
|
+
if (arr(r.untracked))
|
|
359
|
+
parts.push(`${arr(r.untracked)} untracked`);
|
|
360
|
+
if (r.branch)
|
|
361
|
+
parts.push(`branch ${r.branch}`);
|
|
362
|
+
else if (r.detached)
|
|
363
|
+
parts.push(`detached ${r.head ?? ""}`.trim());
|
|
364
|
+
return `git_status · ${parts.join(" · ")}`;
|
|
365
|
+
}
|
|
366
|
+
if (name === "git_diff") {
|
|
367
|
+
if (n(r.files_changed) === 0)
|
|
368
|
+
return "git_diff · no changes";
|
|
369
|
+
return `git_diff · ${n(r.files_changed)} ${n(r.files_changed) === 1 ? "file" : "files"} · +${n(r.additions)} −${n(r.deletions)}`;
|
|
370
|
+
}
|
|
371
|
+
// read_file / list_files / search_files are aggregated by the step summary.
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
374
|
+
/** The kind of activity a tool name represents, for the step summary. */
|
|
375
|
+
export function toolKind(name) {
|
|
376
|
+
switch (name) {
|
|
377
|
+
case "read_file":
|
|
378
|
+
return "read";
|
|
379
|
+
case "list_files":
|
|
380
|
+
return "list";
|
|
381
|
+
case "search_files":
|
|
382
|
+
return "search";
|
|
383
|
+
case "resolve_local_file":
|
|
384
|
+
case "git_status":
|
|
385
|
+
case "git_diff":
|
|
386
|
+
return "inspect";
|
|
387
|
+
case "run_command":
|
|
388
|
+
return "command";
|
|
389
|
+
case "create_file":
|
|
390
|
+
case "write_file":
|
|
391
|
+
case "edit_file":
|
|
392
|
+
case "delete_file":
|
|
393
|
+
case "revert_change":
|
|
394
|
+
return "change";
|
|
395
|
+
default:
|
|
396
|
+
return "other";
|
|
397
|
+
}
|
|
398
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ctrl+C while a turn is in flight.
|
|
3
|
+
*
|
|
4
|
+
* Between prompts the terminal is deliberately left in RAW mode (see the setup
|
|
5
|
+
* note in command-palette.ts), and the composer removes its `keypress` listener
|
|
6
|
+
* as soon as it hands the line back. Raw mode means the tty does not raise
|
|
7
|
+
* SIGINT, so during a request Ctrl+C arrives as a bare 0x03 byte with nothing
|
|
8
|
+
* listening — which is why it currently does nothing at all.
|
|
9
|
+
*
|
|
10
|
+
* This attaches a listener for exactly that byte, for exactly as long as the
|
|
11
|
+
* request is running, and detaches it again so the composer's own Ctrl+C
|
|
12
|
+
* (arm-then-exit) behaviour is untouched.
|
|
13
|
+
*/
|
|
14
|
+
export function watchForInterrupt(onAbort) {
|
|
15
|
+
const stdin = process.stdin;
|
|
16
|
+
if (!stdin.isTTY)
|
|
17
|
+
return () => undefined;
|
|
18
|
+
let done = false;
|
|
19
|
+
const cleanup = () => {
|
|
20
|
+
if (done)
|
|
21
|
+
return;
|
|
22
|
+
done = true;
|
|
23
|
+
stdin.removeListener("data", onData);
|
|
24
|
+
};
|
|
25
|
+
function onData(chunk) {
|
|
26
|
+
const buf = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
|
|
27
|
+
// 0x03 = ETX, what Ctrl+C sends in raw mode.
|
|
28
|
+
if (buf.includes(0x03)) {
|
|
29
|
+
cleanup();
|
|
30
|
+
onAbort();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
stdin.on("data", onData);
|
|
34
|
+
return cleanup;
|
|
35
|
+
}
|
package/dist/ui/live.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as readline from "node:readline";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
3
|
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
4
|
+
import { screenActive, screenCommitTransient, screenPrint, screenTransient } from "./screen.js";
|
|
4
5
|
/** Non-TTY fallback: commits print normally, the bar/status are no-ops. */
|
|
5
6
|
const passthrough = {
|
|
6
7
|
setStatus: () => { },
|
|
@@ -13,11 +14,27 @@ const passthrough = {
|
|
|
13
14
|
};
|
|
14
15
|
export function startLiveComposer(footer = "") {
|
|
15
16
|
const stdout = process.stdout;
|
|
17
|
+
// In the persistent-composer shell the screen engine owns the input bar, so a
|
|
18
|
+
// live turn just streams into the transcript above it: transient lines (spinner
|
|
19
|
+
// / word-by-word reveal) go to the transient tail; finished lines are committed.
|
|
20
|
+
if (screenActive()) {
|
|
21
|
+
return {
|
|
22
|
+
setStatus: (lines) => screenTransient(lines),
|
|
23
|
+
commitStatus: () => screenCommitTransient(),
|
|
24
|
+
commit: (lines) => {
|
|
25
|
+
if (lines.length)
|
|
26
|
+
screenPrint(lines);
|
|
27
|
+
},
|
|
28
|
+
stop: () => screenTransient([])
|
|
29
|
+
};
|
|
30
|
+
}
|
|
16
31
|
if (!stdout.isTTY)
|
|
17
32
|
return passthrough;
|
|
18
33
|
let status = [];
|
|
19
34
|
let shown = []; // the transient region currently on screen (status + bar)
|
|
20
|
-
|
|
35
|
+
// SGR colour runs plus OSC strings — an OSC payload occupies no columns, so
|
|
36
|
+
// stripping only SGR would over-count the width of any line carrying one.
|
|
37
|
+
const ANSI = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
21
38
|
// Full row width (margin + fixed content column) — used only to pad rows so an
|
|
22
39
|
// overwrite fully covers the previous content; the composer itself is fixed.
|
|
23
40
|
const width = () => MARGIN.length + contentWidth() + 1;
|