@carrieros/cli 0.2.0 → 0.4.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 +170 -64
- package/dist/commands/attach.js +45 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +81 -15
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +63 -3
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +1 -0
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +31 -7
- package/dist/commands/logout.js +1 -0
- 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/permissions.js +77 -0
- 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 +9 -6
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +224 -65
- package/dist/commands/settings.js +94 -0
- package/dist/commands/update.js +229 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +213 -17
- package/dist/commands/whoami.js +12 -9
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +355 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +26 -1
- package/dist/services/api.js +315 -15
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +352 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +311 -24
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +296 -23
- package/dist/services/device-auth.js +72 -35
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -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/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +55 -17
- package/dist/services/meta.js +3 -1
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +5 -2
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +60 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +113 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +311 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1297 -59
- package/dist/shell/shell-commands.js +243 -12
- package/dist/shell/shell-prompt.js +94 -11
- package/dist/shell/shell-renderer.js +8 -20
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +252 -22
- package/dist/shell/task-context-view.js +40 -0
- package/dist/types/index.js +3 -1
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +31 -48
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +207 -14
- package/dist/ui/colors.js +12 -8
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +26 -5
- package/dist/ui/command-palette.js +44 -14
- package/dist/ui/command-registry.js +92 -10
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +48 -1
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +4 -2
- 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 +19 -11
- package/dist/ui/select.js +191 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +27 -1
- package/package.json +18 -4
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { c } from "./colors.js";
|
|
2
|
+
import { MARGIN, contentWidth, visibleWidth } from "./composer-layout.js";
|
|
3
|
+
import { captureKeys } from "./screen-prompt.js";
|
|
4
|
+
import { screenScrollPage, screenScrollToBottom, screenTransient } from "./screen.js";
|
|
5
|
+
/** Remaining time as m:ss, for the auto-reject hint. */
|
|
6
|
+
function countdown(msLeft) {
|
|
7
|
+
const left = Math.max(0, Math.ceil(msLeft / 1000));
|
|
8
|
+
return `${Math.floor(left / 60)}:${String(left % 60).padStart(2, "0")}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The exact lines the prompt shows. Pure, so the layout can be tested.
|
|
12
|
+
*
|
|
13
|
+
* One line to answer, one dim line saying what the keys do and when it gives
|
|
14
|
+
* up. `(y/N)` is the convention for "anything but y means no", which is exactly
|
|
15
|
+
* the policy underneath.
|
|
16
|
+
*/
|
|
17
|
+
export function approvalFrame(question, msLeft) {
|
|
18
|
+
const hint = c.dim("Type y + Enter to approve · empty Enter or ") +
|
|
19
|
+
c.key("n") +
|
|
20
|
+
c.dim(" declines · ") +
|
|
21
|
+
c.key("Ctrl+C") +
|
|
22
|
+
c.dim(" stops the turn · declines on its own in ") +
|
|
23
|
+
c.dim(countdown(msLeft));
|
|
24
|
+
const short = c.dim("y + Enter approves · Enter/") + c.key("n") + c.dim(" declines · ") + c.key("Ctrl+C") + c.dim(" stops · ") + c.dim(countdown(msLeft));
|
|
25
|
+
const room = contentWidth() - 6;
|
|
26
|
+
if (contentWidth() < 70)
|
|
27
|
+
return [
|
|
28
|
+
MARGIN + c.title(question),
|
|
29
|
+
MARGIN + c.dim("y + Enter approves"),
|
|
30
|
+
MARGIN + c.dim("Enter/n declines"),
|
|
31
|
+
MARGIN + c.dim(`Ctrl+C stops · ${countdown(msLeft)}`)
|
|
32
|
+
];
|
|
33
|
+
return [
|
|
34
|
+
MARGIN + " " + c.warn("?") + " " + c.title(question) + " " + c.dim("(y/N)"),
|
|
35
|
+
MARGIN + " " + (visibleWidth(hint) <= room ? hint : short)
|
|
36
|
+
];
|
|
37
|
+
}
|
|
38
|
+
export function askApproval(opts) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const deadline = Date.now() + opts.timeoutMs;
|
|
41
|
+
let done = false;
|
|
42
|
+
let candidate = false;
|
|
43
|
+
let confirming = false;
|
|
44
|
+
const paint = () => screenTransient(approvalFrame(opts.question, deadline - Date.now()));
|
|
45
|
+
const finish = (value) => {
|
|
46
|
+
if (done)
|
|
47
|
+
return;
|
|
48
|
+
done = true;
|
|
49
|
+
clearInterval(timer);
|
|
50
|
+
process.stdout.removeListener("resize", paint);
|
|
51
|
+
opts.signal?.removeEventListener?.("abort", onAbort);
|
|
52
|
+
setImmediate(() => {
|
|
53
|
+
release();
|
|
54
|
+
screenTransient([]);
|
|
55
|
+
resolve(value);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
function onAbort() {
|
|
59
|
+
finish("cancel");
|
|
60
|
+
}
|
|
61
|
+
const onKey = (str, key) => {
|
|
62
|
+
if (done || (!key && !str))
|
|
63
|
+
return;
|
|
64
|
+
if (Date.now() >= deadline)
|
|
65
|
+
return finish("timeout");
|
|
66
|
+
if (opts.signal?.aborted)
|
|
67
|
+
return finish("cancel");
|
|
68
|
+
if (key?.ctrl && (key.name === "c" || key.name === "d"))
|
|
69
|
+
return finish("cancel");
|
|
70
|
+
// Never interpret a prefix of text/paste as consent. Drain this input
|
|
71
|
+
// burst before resolving so trailing characters cannot approve another
|
|
72
|
+
// prompt. No input text is retained or rendered.
|
|
73
|
+
if (confirming)
|
|
74
|
+
return finish("reject");
|
|
75
|
+
if (key?.ctrl || key?.meta)
|
|
76
|
+
return finish("reject");
|
|
77
|
+
if (!candidate && ["up", "down", "left", "right", "home"].includes(key?.name ?? ""))
|
|
78
|
+
return;
|
|
79
|
+
if (!candidate && key?.name === "pageup")
|
|
80
|
+
return screenScrollPage(1);
|
|
81
|
+
if (!candidate && key?.name === "pagedown")
|
|
82
|
+
return screenScrollPage(-1);
|
|
83
|
+
if (!candidate && key?.name === "end")
|
|
84
|
+
return screenScrollToBottom();
|
|
85
|
+
if ((str === "y" || str === "Y") && !candidate) {
|
|
86
|
+
candidate = true;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if ((key?.name === "return" || key?.name === "enter") && candidate) {
|
|
90
|
+
confirming = true;
|
|
91
|
+
setImmediate(() => finish(Date.now() >= deadline ? "timeout" : opts.signal?.aborted ? "cancel" : "approve"));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
finish("reject");
|
|
95
|
+
};
|
|
96
|
+
const release = captureKeys(onKey);
|
|
97
|
+
const timer = setInterval(() => {
|
|
98
|
+
if (Date.now() >= deadline)
|
|
99
|
+
finish("timeout");
|
|
100
|
+
else
|
|
101
|
+
paint();
|
|
102
|
+
}, 1000);
|
|
103
|
+
timer.unref?.();
|
|
104
|
+
if (opts.signal?.aborted)
|
|
105
|
+
return finish("cancel");
|
|
106
|
+
opts.signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
107
|
+
process.stdout.on("resize", paint);
|
|
108
|
+
screenScrollToBottom();
|
|
109
|
+
paint();
|
|
110
|
+
});
|
|
111
|
+
}
|
package/dist/ui/banner.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
2
|
import { line } from "../services/output.js";
|
|
3
|
+
import { branchLabel, getWorkspace } from "../services/workspace.js";
|
|
3
4
|
import { modeLabelShort, modelLabelShort } from "../shell/shell-chat.js";
|
|
4
5
|
import { c } from "./colors.js";
|
|
6
|
+
import { clip } from "./conversation.js";
|
|
7
|
+
import { contentWidth, MARGIN } from "./composer-layout.js";
|
|
5
8
|
import { WORDMARK } from "./logo.js";
|
|
6
9
|
import { BRAND_HEX } from "./theme.js";
|
|
7
|
-
const MARGIN = " ";
|
|
8
|
-
const LABEL_WIDTH = 13;
|
|
9
|
-
/** Visible width of a block of lines. */
|
|
10
|
-
function blockWidth(block) {
|
|
11
|
-
return Math.max(...block.split("\n").map((l) => [...l].length));
|
|
12
|
-
}
|
|
13
|
-
/** True when the terminal is too narrow for the block wordmark. */
|
|
14
|
-
function isNarrow() {
|
|
15
|
-
return (process.stdout.columns ?? 80) < 2 + blockWidth(WORDMARK) + 2;
|
|
16
|
-
}
|
|
17
|
-
// ── Theme-color gradient across the wordmark (subtle; follows the accent) ──────
|
|
18
10
|
function hexToRgb(h) {
|
|
19
11
|
const n = parseInt(h.slice(1), 16);
|
|
20
12
|
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
@@ -25,49 +17,40 @@ function lerpHex(a, b, t) {
|
|
|
25
17
|
const mix = (x, y) => Math.round(x + (y - x) * t);
|
|
26
18
|
return "#" + [mix(ar, br), mix(ag, bg), mix(ab, bb)].map((v) => v.toString(16).padStart(2, "0")).join("");
|
|
27
19
|
}
|
|
28
|
-
/**
|
|
29
|
-
*
|
|
30
|
-
* branding, so it stays green and does NOT follow the runtime /color accent. */
|
|
31
|
-
function gradientWordmark(lines) {
|
|
32
|
-
const from = BRAND_HEX;
|
|
33
|
-
const to = lerpHex(from, "#ffffff", 0.5); // lighten toward white on the right
|
|
34
|
-
const maxW = Math.max(1, ...lines.map((l) => [...l].length));
|
|
35
|
-
return lines.map((line) => [...line].map((ch, i) => (ch === " " ? ch : chalk.hex(lerpHex(from, to, i / maxW))(ch))).join(""));
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* The brand lockup lines (already colored): the CARRIER block wordmark with a
|
|
39
|
-
* subtle theme-color gradient. Shared by the startup banner and first-run
|
|
40
|
-
* welcome.
|
|
41
|
-
*/
|
|
20
|
+
/** The CARRIER OS block wordmark with a brand-green → lighter gradient, or a
|
|
21
|
+
* one-line mark when the terminal is too narrow for it. */
|
|
42
22
|
export function renderBrandmark() {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return MARGIN + c.label(label.padEnd(LABEL_WIDTH)) + c.value(value);
|
|
23
|
+
const rows = WORDMARK.split("\n");
|
|
24
|
+
const width = Math.max(1, ...rows.map((l) => [...l].length));
|
|
25
|
+
if ((process.stdout.columns ?? 80) < MARGIN.length + width + 2)
|
|
26
|
+
return [MARGIN + c.title("CarrierOS")];
|
|
27
|
+
const to = lerpHex(BRAND_HEX, "#ffffff", 0.5);
|
|
28
|
+
return rows.map((row) => MARGIN + [...row].map((ch, i) => (ch === " " ? ch : chalk.hex(lerpHex(BRAND_HEX, to, i / width))(ch))).join(""));
|
|
50
29
|
}
|
|
51
30
|
export function renderWelcomeBanner(ctx, opts = {}) {
|
|
52
31
|
try {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
out.push(MARGIN + c.hint("Type ") + c.key("/help") + c.hint(" for available commands."));
|
|
62
|
-
// Emit through `line()` so, in the interactive shell, the banner lands in the
|
|
63
|
-
// transcript region above the pinned composer (not written over it).
|
|
64
|
-
for (const l of out)
|
|
32
|
+
const ws = getWorkspace();
|
|
33
|
+
const branch = branchLabel(ws.repository);
|
|
34
|
+
// Where the agent will work, stated plainly before the first prompt: the
|
|
35
|
+
// signed-in state (a stored credential; the server checks it on use), the
|
|
36
|
+
// repository this folder resolves to, and its branch.
|
|
37
|
+
const row = (label, value) => MARGIN + c.dim(label.padEnd(12)) + c.value(clip(value, Math.max(8, contentWidth() - 12 - MARGIN.length)));
|
|
38
|
+
line("");
|
|
39
|
+
for (const l of renderBrandmark())
|
|
65
40
|
line(l);
|
|
41
|
+
line("");
|
|
42
|
+
line(MARGIN + c.ok("Authenticated ✓"));
|
|
43
|
+
line(row("Repository", ws.project.name));
|
|
44
|
+
if (branch)
|
|
45
|
+
line(row("Branch", branch));
|
|
46
|
+
line(row("Model", modelLabelShort(ctx)));
|
|
47
|
+
line(row("Mode", modeLabelShort(ctx)));
|
|
48
|
+
for (const notice of opts.notices ?? [])
|
|
49
|
+
line(MARGIN + clip(notice, contentWidth()));
|
|
50
|
+
line(MARGIN + c.dim("Describe a bug or a change in plain words · /help for commands"));
|
|
51
|
+
line("");
|
|
66
52
|
}
|
|
67
53
|
catch {
|
|
68
|
-
|
|
69
|
-
line();
|
|
70
|
-
line(` ${c.wordmark("Carrier OS")} ${c.subtitle("CLI")}`);
|
|
71
|
-
line();
|
|
54
|
+
line(MARGIN + c.title("CarrierOS"));
|
|
72
55
|
}
|
|
73
56
|
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import * as readline from "node:readline";
|
|
2
|
+
import { line } from "../services/output.js";
|
|
3
|
+
import { sleep } from "../services/spinner.js";
|
|
4
|
+
import { c } from "./colors.js";
|
|
5
|
+
import { renderMarkdown } from "./markdown.js";
|
|
6
|
+
import { resumeScreen, screenActive, screenCommitTransient, screenTransient, suspendScreen } from "./screen.js";
|
|
7
|
+
import { termWidth } from "./ui.js";
|
|
8
|
+
/**
|
|
9
|
+
* The "By the way" overlay — a temporary panel for a side answer.
|
|
10
|
+
*
|
|
11
|
+
* Deliberately NOT a transcript message. The answer was never persisted
|
|
12
|
+
* server-side, so printing it into the conversation would be the one place the
|
|
13
|
+
* CLI lied about what happened. It is drawn in the transient region, dismissed
|
|
14
|
+
* by a keypress, and leaves nothing behind.
|
|
15
|
+
*/
|
|
16
|
+
const MAX_ROWS = 14;
|
|
17
|
+
/** SGR runs plus OSC strings — an OSC payload occupies no columns. */
|
|
18
|
+
const ANSI = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
19
|
+
/** Truncate to `max` visible columns, closing any open colour run. */
|
|
20
|
+
function clipAnsi(s, max) {
|
|
21
|
+
let out = "";
|
|
22
|
+
let seen = 0;
|
|
23
|
+
let i = 0;
|
|
24
|
+
while (i < s.length && seen < max) {
|
|
25
|
+
if (s[i] === "\x1b") {
|
|
26
|
+
ANSI.lastIndex = i;
|
|
27
|
+
const m = ANSI.exec(s);
|
|
28
|
+
if (m && m.index === i) {
|
|
29
|
+
out += m[0];
|
|
30
|
+
i += m[0].length;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
out += s[i];
|
|
35
|
+
seen += 1;
|
|
36
|
+
i += 1;
|
|
37
|
+
}
|
|
38
|
+
return out + "\x1b[0m";
|
|
39
|
+
}
|
|
40
|
+
function wrap(text, width) {
|
|
41
|
+
const w = Math.max(20, width);
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const paragraph of text.split(/\n/)) {
|
|
44
|
+
if (!paragraph.trim()) {
|
|
45
|
+
out.push("");
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
let cur = "";
|
|
49
|
+
for (const word of paragraph.trim().split(/\s+/)) {
|
|
50
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
51
|
+
out.push(cur);
|
|
52
|
+
cur = word;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (cur)
|
|
59
|
+
out.push(cur);
|
|
60
|
+
}
|
|
61
|
+
return out.length ? out : [""];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The answer as a bordered block, with the body rendered through the SAME
|
|
65
|
+
* markdown renderer the main response uses — so headings, bold and inline code
|
|
66
|
+
* are themed identically instead of showing raw `**asterisks**`.
|
|
67
|
+
*
|
|
68
|
+
* Nothing is truncated. An earlier version capped the body at 14 rows with an
|
|
69
|
+
* ellipsis, which quietly threw away most of a long answer AND defeated the
|
|
70
|
+
* "too tall for a panel" check, since the panel could never measure tall.
|
|
71
|
+
* A left rule marks it as an aside; the transcript scrolls, so length is fine.
|
|
72
|
+
*/
|
|
73
|
+
export function btwAnswerLines(answer, note) {
|
|
74
|
+
// Interior width. The body is rendered to exactly this, so a wrapped line can
|
|
75
|
+
// never push past the right border.
|
|
76
|
+
const box = Math.max(32, Math.min(termWidth() - 4, 92));
|
|
77
|
+
const inner = box - 4; // "│ " … " │"
|
|
78
|
+
const title = " By the way ";
|
|
79
|
+
const top = "┌─" + title + "─".repeat(Math.max(1, box - title.length - 3)) + "┐";
|
|
80
|
+
const bottom = "└" + "─".repeat(box - 2) + "┘";
|
|
81
|
+
/** One boxed row, padded on the VISIBLE width so colour codes don't skew it. */
|
|
82
|
+
const row = (styled) => {
|
|
83
|
+
const visible = [...styled.replace(ANSI, "")].length;
|
|
84
|
+
const body = visible > inner ? clipAnsi(styled, inner) : styled;
|
|
85
|
+
const padTo = Math.max(0, inner - Math.min(visible, inner));
|
|
86
|
+
return " " + c.magic("│") + " " + body + " ".repeat(padTo) + " " + c.magic("│");
|
|
87
|
+
};
|
|
88
|
+
const footer = "Not added to your conversation." + (note ? ` ${note}` : "");
|
|
89
|
+
return [
|
|
90
|
+
"",
|
|
91
|
+
" " + c.magic(top),
|
|
92
|
+
...renderMarkdown(answer, inner).map(row),
|
|
93
|
+
row(""),
|
|
94
|
+
row(c.dim(footer)),
|
|
95
|
+
" " + c.magic(bottom),
|
|
96
|
+
""
|
|
97
|
+
];
|
|
98
|
+
}
|
|
99
|
+
/** Delay between revealed rows. Fast enough not to be a wait, slow enough to
|
|
100
|
+
* read as the answer arriving rather than a repaint. */
|
|
101
|
+
const REVEAL_MS = 26;
|
|
102
|
+
/**
|
|
103
|
+
* Build the box a row at a time in the transient region, then promote the whole
|
|
104
|
+
* thing to permanent transcript.
|
|
105
|
+
*
|
|
106
|
+
* Line-by-line rather than word-by-word: the box has to be closed on every
|
|
107
|
+
* frame, and a partially-drawn row inside a border reads as corruption. The
|
|
108
|
+
* screen engine diffs frames, so each step repaints only the new row.
|
|
109
|
+
*/
|
|
110
|
+
async function revealAnswer(lines) {
|
|
111
|
+
for (let i = 1; i <= lines.length; i++) {
|
|
112
|
+
screenTransient(lines.slice(0, i));
|
|
113
|
+
await sleep(REVEAL_MS);
|
|
114
|
+
}
|
|
115
|
+
// Already on screen where it will stay — promoting is invisible.
|
|
116
|
+
screenCommitTransient();
|
|
117
|
+
}
|
|
118
|
+
/** Build the bordered panel. Exported so it can be rendered without a TTY. */
|
|
119
|
+
export function btwPanelLines(panel, width, opts = {}) {
|
|
120
|
+
// Columns between the borders, including the one space of padding each side.
|
|
121
|
+
const inner = Math.max(28, Math.min(width, 92) - 4);
|
|
122
|
+
const text = inner - 2; // room actually available to content
|
|
123
|
+
const title = " By the way ";
|
|
124
|
+
const top = "┌─" + title + "─".repeat(Math.max(1, inner - title.length - 1)) + "┐";
|
|
125
|
+
const bottom = "└" + "─".repeat(inner) + "┘";
|
|
126
|
+
/** One body row. `plain` is the same text without colour — the only reliable
|
|
127
|
+
* way to know how many columns it occupies. */
|
|
128
|
+
const row = (styled, plain) => c.magic("│") + " " + styled + " ".repeat(Math.max(0, text - [...plain].length)) + " " + c.magic("│");
|
|
129
|
+
const body = [];
|
|
130
|
+
if (panel.answer === null) {
|
|
131
|
+
const message = panel.error ?? "Unable to answer this side question.";
|
|
132
|
+
for (const l of wrap(message, text - 2)) {
|
|
133
|
+
body.push(row(c.err("✗ ") + c.value(l), "✗ " + l));
|
|
134
|
+
}
|
|
135
|
+
body.push(row("", ""));
|
|
136
|
+
const footer = "Your conversation was not affected.";
|
|
137
|
+
body.push(row(c.dim(footer), footer));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
const lines = wrap(panel.answer, text);
|
|
141
|
+
// Long answers are clipped rather than allowed to fill the screen — this is
|
|
142
|
+
// an aside, and the transcript behind it has to stay visible.
|
|
143
|
+
const shown = lines.slice(0, MAX_ROWS);
|
|
144
|
+
if (lines.length > MAX_ROWS)
|
|
145
|
+
shown[MAX_ROWS - 1] = shown[MAX_ROWS - 1].slice(0, -1) + "…";
|
|
146
|
+
for (const l of shown)
|
|
147
|
+
body.push(row(c.value(l), l));
|
|
148
|
+
body.push(row("", ""));
|
|
149
|
+
const footer = "Not added to your conversation.";
|
|
150
|
+
body.push(row(c.dim(footer), footer));
|
|
151
|
+
}
|
|
152
|
+
if (panel.note)
|
|
153
|
+
body.push(row(c.dim(panel.note), panel.note));
|
|
154
|
+
return [
|
|
155
|
+
"",
|
|
156
|
+
" " + c.magic(top),
|
|
157
|
+
...body.map((l) => " " + l),
|
|
158
|
+
" " + c.magic(bottom),
|
|
159
|
+
...(opts.dismissible === false ? [] : [" " + c.dim("Esc / Enter / Space to close")]),
|
|
160
|
+
""
|
|
161
|
+
];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Show the panel and wait for a dismissal key.
|
|
165
|
+
*
|
|
166
|
+
* Resolves once dismissed. Any key closes it — Esc, Enter and Space are simply
|
|
167
|
+
* the ones advertised — because a panel you cannot get rid of is worse than one
|
|
168
|
+
* that closes too eagerly.
|
|
169
|
+
*/
|
|
170
|
+
export function showBtwPanel(panel, opts = {}) {
|
|
171
|
+
const stdin = process.stdin;
|
|
172
|
+
// A reveal needs the transient region, and during a main turn the turn owns
|
|
173
|
+
// it — two writers there is the flicker we already fixed once. So the answer
|
|
174
|
+
// builds progressively only when nothing else is rendering.
|
|
175
|
+
if (panel.answer !== null && !opts.commit && screenActive()) {
|
|
176
|
+
return revealAnswer(btwAnswerLines(panel.answer, panel.note));
|
|
177
|
+
}
|
|
178
|
+
// ANSWERS go to the transcript, always.
|
|
179
|
+
//
|
|
180
|
+
// A panel cannot scroll, so any answer longer than the terminal would have to
|
|
181
|
+
// be truncated — and these are real explanations now, not one-liners. The
|
|
182
|
+
// transcript scrolls (PgUp/PgDn, End) and renders markdown, so the whole
|
|
183
|
+
// answer survives and reads like the main response.
|
|
184
|
+
//
|
|
185
|
+
// Routed through line(), NOT process.stdout: with the screen engine running,
|
|
186
|
+
// a raw write lands wherever the cursor happens to be and corrupts the frame.
|
|
187
|
+
if (panel.answer !== null) {
|
|
188
|
+
for (const l of btwAnswerLines(panel.answer, panel.note))
|
|
189
|
+
line(l);
|
|
190
|
+
return Promise.resolve();
|
|
191
|
+
}
|
|
192
|
+
// ERRORS stay a small box — short, and worth a beat of attention.
|
|
193
|
+
if (opts.commit || !stdin.isTTY) {
|
|
194
|
+
for (const l of btwPanelLines(panel, termWidth(), { dismissible: false }))
|
|
195
|
+
line(l);
|
|
196
|
+
return Promise.resolve();
|
|
197
|
+
}
|
|
198
|
+
return new Promise((resolve) => {
|
|
199
|
+
const onScreen = screenActive();
|
|
200
|
+
const lines = () => btwPanelLines(panel, termWidth());
|
|
201
|
+
if (onScreen) {
|
|
202
|
+
screenTransient(lines());
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
suspendScreen();
|
|
206
|
+
for (const l of lines())
|
|
207
|
+
process.stdout.write(l + "\n");
|
|
208
|
+
}
|
|
209
|
+
const previous = stdin.listeners("keypress");
|
|
210
|
+
stdin.removeAllListeners("keypress");
|
|
211
|
+
readline.emitKeypressEvents(stdin);
|
|
212
|
+
const prevRaw = stdin.isRaw ?? false;
|
|
213
|
+
if (stdin.isTTY)
|
|
214
|
+
stdin.setRawMode(true);
|
|
215
|
+
stdin.resume();
|
|
216
|
+
const onKey = () => {
|
|
217
|
+
stdin.removeListener("keypress", onKey);
|
|
218
|
+
if (stdin.isTTY)
|
|
219
|
+
stdin.setRawMode(prevRaw);
|
|
220
|
+
for (const l of previous)
|
|
221
|
+
stdin.on("keypress", l);
|
|
222
|
+
if (onScreen)
|
|
223
|
+
screenTransient([]);
|
|
224
|
+
else
|
|
225
|
+
resumeScreen();
|
|
226
|
+
resolve();
|
|
227
|
+
};
|
|
228
|
+
stdin.on("keypress", onKey);
|
|
229
|
+
});
|
|
230
|
+
}
|