@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
package/dist/ui/markdown.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import chalk from "chalk";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
3
|
import { renderCodeBlock } from "./code-block.js";
|
|
4
|
-
import { textWidth } from "./composer-layout.js";
|
|
4
|
+
import { clampAnsi, contentWidth, MARGIN, textWidth } from "./composer-layout.js";
|
|
5
5
|
/**
|
|
6
6
|
* Self-contained Markdown → ANSI renderer (no dependency). Supports headings,
|
|
7
7
|
* bold/italic, inline code, links, bullet/numbered/task lists, blockquotes,
|
|
8
8
|
* horizontal rules, tables and fenced code blocks — with soft word-wrap so long
|
|
9
9
|
* paragraphs never overflow or cut words. Returns col-0 lines; callers indent.
|
|
10
10
|
*/
|
|
11
|
-
export function renderMarkdown(md) {
|
|
12
|
-
|
|
11
|
+
export function renderMarkdown(md, maxWidth) {
|
|
12
|
+
// `maxWidth` lets a caller render into something narrower than the transcript
|
|
13
|
+
// — the /btw box, which spends 4 columns on its own borders. Without it the
|
|
14
|
+
// body would wrap to the full width and overflow the frame it sits in.
|
|
15
|
+
const wrap = Math.max(20, (maxWidth ?? textWidth()) - 4);
|
|
13
16
|
const out = [];
|
|
14
17
|
const lines = md.replace(/\r/g, "").split("\n");
|
|
15
18
|
let i = 0;
|
|
@@ -44,10 +47,12 @@ export function renderMarkdown(md) {
|
|
|
44
47
|
i++;
|
|
45
48
|
continue;
|
|
46
49
|
}
|
|
47
|
-
// Heading.
|
|
50
|
+
// Heading — wrapped like body text; a long one would otherwise overflow.
|
|
48
51
|
const h = raw.match(/^(#{1,6})\s+(.*)$/);
|
|
49
52
|
if (h) {
|
|
50
|
-
|
|
53
|
+
const paint = h[1].length === 1 ? c.brand : c.title;
|
|
54
|
+
for (const w of wrapText(h[2], wrap))
|
|
55
|
+
out.push(paint(inline(w)));
|
|
51
56
|
i++;
|
|
52
57
|
continue;
|
|
53
58
|
}
|
|
@@ -102,9 +107,24 @@ export function renderMarkdown(md) {
|
|
|
102
107
|
out.push(inline(w));
|
|
103
108
|
i++;
|
|
104
109
|
}
|
|
105
|
-
|
|
110
|
+
// Backstop. Code blocks and tables size themselves from their content, and a
|
|
111
|
+
// future block type could forget to wrap entirely — any of which would emit a
|
|
112
|
+
// line wider than the terminal and corrupt the pinned composer. Callers indent
|
|
113
|
+
// by MARGIN, so leave room for it.
|
|
114
|
+
const limit = Math.max(20, contentWidth() - MARGIN.length);
|
|
115
|
+
return out.map((l) => clampAnsi(l, limit));
|
|
106
116
|
}
|
|
107
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Word-wrap plain text to `width`, never cutting a word — EXCEPT a word that
|
|
119
|
+
* cannot fit on a line of its own.
|
|
120
|
+
*
|
|
121
|
+
* A 250-character URL, hash or base64 blob has no break point, so pure
|
|
122
|
+
* word-wrapping emits it as one over-wide line. The terminal then wraps it into
|
|
123
|
+
* several physical rows while the screen engine still counts one, and the
|
|
124
|
+
* pinned composer below gets overwritten. Splitting such a token across lines
|
|
125
|
+
* keeps every character (nothing is lost, unlike clipping) while holding the
|
|
126
|
+
* one-line-per-row invariant everything else depends on.
|
|
127
|
+
*/
|
|
108
128
|
function wrapText(text, width) {
|
|
109
129
|
const w = Math.max(20, width);
|
|
110
130
|
const words = text.trim().split(/\s+/).filter(Boolean);
|
|
@@ -113,7 +133,18 @@ function wrapText(text, width) {
|
|
|
113
133
|
const lines = [];
|
|
114
134
|
let cur = "";
|
|
115
135
|
for (const word of words) {
|
|
116
|
-
|
|
136
|
+
const len = [...word].length;
|
|
137
|
+
if (len > w) {
|
|
138
|
+
if (cur) {
|
|
139
|
+
lines.push(cur);
|
|
140
|
+
cur = "";
|
|
141
|
+
}
|
|
142
|
+
const chars = [...word];
|
|
143
|
+
for (let i = 0; i < chars.length; i += w)
|
|
144
|
+
lines.push(chars.slice(i, i + w).join(""));
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (cur && [...cur].length + 1 + len > w) {
|
|
117
148
|
lines.push(cur);
|
|
118
149
|
cur = word;
|
|
119
150
|
}
|
|
@@ -125,12 +156,35 @@ function wrapText(text, width) {
|
|
|
125
156
|
lines.push(cur);
|
|
126
157
|
return lines;
|
|
127
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* A code-review severity, coloured by weight. A review's findings arrive as
|
|
161
|
+
* bold headers like `**High — \`src/x.js:12\`**`; recognising the leading
|
|
162
|
+
* severity word lets the terminal show it as a subtle marker (red for the
|
|
163
|
+
* serious ones, dim for the quiet ones) instead of undifferentiated bold. It is
|
|
164
|
+
* deliberately narrow — only a bold segment that STARTS with one of these five
|
|
165
|
+
* words is touched, so ordinary bold text is unaffected.
|
|
166
|
+
*/
|
|
167
|
+
const SEVERITY_COLOR = {
|
|
168
|
+
critical: c.err,
|
|
169
|
+
high: c.err,
|
|
170
|
+
medium: c.warn,
|
|
171
|
+
low: c.dim,
|
|
172
|
+
info: c.accent
|
|
173
|
+
};
|
|
174
|
+
function styleBold(b) {
|
|
175
|
+
const m = b.match(/^(critical|high|medium|low|info)\b(.*)$/i);
|
|
176
|
+
if (m) {
|
|
177
|
+
const paint = SEVERITY_COLOR[m[1].toLowerCase()];
|
|
178
|
+
return chalk.bold(paint(m[1].toUpperCase())) + chalk.bold(m[2]);
|
|
179
|
+
}
|
|
180
|
+
return chalk.bold(b);
|
|
181
|
+
}
|
|
128
182
|
/** Inline spans: links, `code`, **bold**, *italic*, ~~strike~~. */
|
|
129
183
|
function inline(text) {
|
|
130
184
|
return text
|
|
131
185
|
.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, label) => c.accent(label))
|
|
132
186
|
.replace(/`([^`]+)`/g, (_m, code) => c.accent(code))
|
|
133
|
-
.replace(/\*\*([^*]+)\*\*/g, (_m, b) =>
|
|
187
|
+
.replace(/\*\*([^*]+)\*\*/g, (_m, b) => styleBold(b))
|
|
134
188
|
.replace(/~~([^~]+)~~/g, (_m, s) => chalk.strikethrough(s))
|
|
135
189
|
.replace(/(^|[^*])\*([^*]+)\*/g, (_m, pre, it) => `${pre}${chalk.italic(it)}`);
|
|
136
190
|
}
|
package/dist/ui/message.js
CHANGED
|
@@ -1,38 +1,21 @@
|
|
|
1
|
-
import chalk from "chalk";
|
|
2
1
|
import { line } from "../services/output.js";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
3
|
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
4
|
+
import { wrapText } from "./conversation.js";
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* A user turn: the accent caret the composer uses, then the message.
|
|
7
|
+
*
|
|
8
|
+
* Deliberately light. This used to be a full-width shaded band, which made
|
|
9
|
+
* every question look like a UI panel and gave the eye something heavier than
|
|
10
|
+
* the answer below it. The caret alone is enough to say "you said this", and it
|
|
11
|
+
* matches what the composer shows while typing, so a sent message reads as the
|
|
12
|
+
* same thing it was a moment ago.
|
|
8
13
|
*/
|
|
9
|
-
const BAR_BG = "#2b2b2e";
|
|
10
14
|
export function renderUserMessage(text) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
for (const [i, l] of
|
|
14
|
-
|
|
15
|
-
line(MARGIN + bar(content.padEnd(w)));
|
|
15
|
+
const lines = wrapText(text.trim(), contentWidth() - 3);
|
|
16
|
+
line();
|
|
17
|
+
for (const [i, l] of lines.entries()) {
|
|
18
|
+
line(MARGIN + (i === 0 ? c.brandFixed("›") : " ") + " " + c.title(l));
|
|
16
19
|
}
|
|
17
|
-
line();
|
|
18
|
-
}
|
|
19
|
-
function wrap(text, width) {
|
|
20
|
-
const w = Math.max(20, width);
|
|
21
|
-
const words = text.trim().split(/\s+/).filter(Boolean);
|
|
22
|
-
if (!words.length)
|
|
23
|
-
return [""];
|
|
24
|
-
const lines = [];
|
|
25
|
-
let cur = "";
|
|
26
|
-
for (const word of words) {
|
|
27
|
-
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
28
|
-
lines.push(cur);
|
|
29
|
-
cur = word;
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
cur = cur ? `${cur} ${word}` : word;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (cur)
|
|
36
|
-
lines.push(cur);
|
|
37
|
-
return lines;
|
|
20
|
+
line();
|
|
38
21
|
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import * as readline from "node:readline";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { screenActive, screenScrollPage, screenScrollToBottom, screenTransient } from "./screen.js";
|
|
4
|
+
import { selectFrame, SELECT_BACK } from "./select.js";
|
|
5
|
+
import { termWidth } from "./ui.js";
|
|
6
|
+
/**
|
|
7
|
+
* Prompts that render INSIDE the shell's transcript instead of taking the
|
|
8
|
+
* terminal over.
|
|
9
|
+
*
|
|
10
|
+
* The pickers in select.ts suspend the screen engine and paint on the raw
|
|
11
|
+
* terminal. That is correct for a one-off `cos model`, but inside the shell it
|
|
12
|
+
* swaps away the buffer holding the banner and the whole conversation — so
|
|
13
|
+
* while a picker is open there is nothing above it and nothing to scroll back
|
|
14
|
+
* to. These variants push their frame through `screenTransient`, so the
|
|
15
|
+
* transcript stays exactly where it was and the prompt appears beneath it, the
|
|
16
|
+
* way the thinking spinner already does.
|
|
17
|
+
*
|
|
18
|
+
* They only work while the screen engine is running; callers fall back to the
|
|
19
|
+
* select.ts versions when it isn't (`cos chat`, piped input).
|
|
20
|
+
*/
|
|
21
|
+
/** True when these should be preferred over the terminal-owning pickers. */
|
|
22
|
+
export function inScreen() {
|
|
23
|
+
return screenActive() && process.stdin.isTTY === true;
|
|
24
|
+
}
|
|
25
|
+
/** Take over key handling for the duration of a prompt, then hand it back. */
|
|
26
|
+
export function captureKeys(onKey) {
|
|
27
|
+
const stdin = process.stdin;
|
|
28
|
+
// The composer is not reading between turns, but a stale listener from an
|
|
29
|
+
// earlier prompt would double-handle every keystroke.
|
|
30
|
+
const previous = stdin.listeners("keypress");
|
|
31
|
+
stdin.removeAllListeners("keypress");
|
|
32
|
+
readline.emitKeypressEvents(stdin);
|
|
33
|
+
const prevRaw = stdin.isRaw ?? false;
|
|
34
|
+
if (stdin.isTTY)
|
|
35
|
+
stdin.setRawMode(true);
|
|
36
|
+
stdin.resume();
|
|
37
|
+
stdin.on("keypress", onKey);
|
|
38
|
+
return () => {
|
|
39
|
+
stdin.removeListener("keypress", onKey);
|
|
40
|
+
if (stdin.isTTY)
|
|
41
|
+
stdin.setRawMode(prevRaw);
|
|
42
|
+
for (const l of previous)
|
|
43
|
+
stdin.on("keypress", l);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** select.ts's picker, drawn into the transcript. Same keys, same result. */
|
|
47
|
+
export function pickInScreen(opts) {
|
|
48
|
+
const { choices } = opts;
|
|
49
|
+
// Only this variant can page the transcript, so only it advertises the keys.
|
|
50
|
+
const withScroll = { ...opts, scrollable: true };
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let selected = Math.min(Math.max(0, opts.initial ?? 0), choices.length - 1);
|
|
53
|
+
const paint = () => screenTransient(selectFrame(withScroll, selected));
|
|
54
|
+
const finish = (value) => {
|
|
55
|
+
release();
|
|
56
|
+
screenTransient([]); // the answer is echoed by the caller, not this frame
|
|
57
|
+
resolve(value);
|
|
58
|
+
};
|
|
59
|
+
const onKey = (str, key) => {
|
|
60
|
+
if (!key)
|
|
61
|
+
return;
|
|
62
|
+
if (key.name === "up" || (key.ctrl && key.name === "p")) {
|
|
63
|
+
selected = (selected - 1 + choices.length) % choices.length;
|
|
64
|
+
return paint();
|
|
65
|
+
}
|
|
66
|
+
if (key.name === "down" || (key.ctrl && key.name === "n")) {
|
|
67
|
+
selected = (selected + 1) % choices.length;
|
|
68
|
+
return paint();
|
|
69
|
+
}
|
|
70
|
+
// Look back over the conversation without losing your place. ↑↓ belong to
|
|
71
|
+
// the list, so paging gets its own keys.
|
|
72
|
+
//
|
|
73
|
+
// A mouse wheel can't be routed here: with the alternate buffer active,
|
|
74
|
+
// terminals translate wheel ticks into ↑↓ before the app sees them, so a
|
|
75
|
+
// scroll and a keypress are the same bytes. Separating them would mean
|
|
76
|
+
// enabling mouse tracking, which costs drag-to-select.
|
|
77
|
+
if (key.name === "pageup") {
|
|
78
|
+
screenScrollPage(-1);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (key.name === "pagedown") {
|
|
82
|
+
screenScrollPage(1);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (key.name === "end") {
|
|
86
|
+
screenScrollToBottom();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (opts.allowBack && key.name === "left")
|
|
90
|
+
return finish(SELECT_BACK);
|
|
91
|
+
if (key.name === "right" || key.name === "return" || key.name === "enter") {
|
|
92
|
+
return finish(choices[selected].value);
|
|
93
|
+
}
|
|
94
|
+
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
95
|
+
return finish(null);
|
|
96
|
+
}
|
|
97
|
+
if (str && /^[1-9]$/.test(str)) {
|
|
98
|
+
const idx = Number(str) - 1;
|
|
99
|
+
if (idx < choices.length) {
|
|
100
|
+
selected = idx;
|
|
101
|
+
if (opts.confirmOnNumber)
|
|
102
|
+
return finish(choices[idx].value);
|
|
103
|
+
paint();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const release = captureKeys(onKey);
|
|
108
|
+
paint();
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Word-wrap to `width` columns. */
|
|
112
|
+
function wrap(text, width) {
|
|
113
|
+
const w = Math.max(20, width);
|
|
114
|
+
const out = [];
|
|
115
|
+
let cur = "";
|
|
116
|
+
for (const word of text.trim().split(/\s+/).filter(Boolean)) {
|
|
117
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
118
|
+
out.push(cur);
|
|
119
|
+
cur = word;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (cur)
|
|
126
|
+
out.push(cur);
|
|
127
|
+
return out.length ? out : [""];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A single-line text prompt drawn into the transcript.
|
|
131
|
+
*
|
|
132
|
+
* Line editing is deliberately minimal — type, backspace, Enter, Esc. readline
|
|
133
|
+
* can't be used here: it owns the terminal cursor, which is precisely what the
|
|
134
|
+
* screen engine is managing. Resolves the trimmed text, or null if empty or
|
|
135
|
+
* cancelled.
|
|
136
|
+
*/
|
|
137
|
+
export function askTextInScreen(headerLines, placeholder) {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
let buffer = "";
|
|
140
|
+
const paint = () => {
|
|
141
|
+
const width = Math.min(termWidth() - 4, 92);
|
|
142
|
+
const shown = buffer
|
|
143
|
+
? c.value(buffer)
|
|
144
|
+
: c.placeholder(placeholder ?? "Type your answer");
|
|
145
|
+
screenTransient([
|
|
146
|
+
...headerLines(width),
|
|
147
|
+
"",
|
|
148
|
+
" " + c.brandFixed("❯ ") + shown + c.dim("▋"),
|
|
149
|
+
"",
|
|
150
|
+
" " + c.key("Enter") + c.dim(" confirm · ") + c.key("Esc") + c.dim(" cancel")
|
|
151
|
+
]);
|
|
152
|
+
};
|
|
153
|
+
const finish = (value) => {
|
|
154
|
+
release();
|
|
155
|
+
screenTransient([]);
|
|
156
|
+
resolve(value);
|
|
157
|
+
};
|
|
158
|
+
const onKey = (str, key) => {
|
|
159
|
+
if (!key)
|
|
160
|
+
return;
|
|
161
|
+
if (key.name === "return" || key.name === "enter")
|
|
162
|
+
return finish(buffer.trim() || null);
|
|
163
|
+
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
164
|
+
return finish(null);
|
|
165
|
+
}
|
|
166
|
+
if (key.name === "backspace") {
|
|
167
|
+
buffer = [...buffer].slice(0, -1).join("");
|
|
168
|
+
return paint();
|
|
169
|
+
}
|
|
170
|
+
// Printable input only — ignore control chords and navigation keys.
|
|
171
|
+
if (str && !key.ctrl && !key.meta && str >= " " && str !== "\x7f") {
|
|
172
|
+
buffer += str;
|
|
173
|
+
paint();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
const release = captureKeys(onKey);
|
|
177
|
+
paint();
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
export { wrap as wrapForPrompt };
|
|
181
|
+
export { askApproval as approveInScreen } from "./approval.js";
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent bottom-composer TUI engine (Claude Code–style).
|
|
3
|
+
*
|
|
4
|
+
* The terminal is a managed CANVAS, not a stream of `console.log()`s. We enter
|
|
5
|
+
* the ALTERNATE SCREEN BUFFER so the app owns the whole viewport and never
|
|
6
|
+
* pollutes the user's real scrollback — leaving TUI mode restores their terminal
|
|
7
|
+
* exactly as it was. The screen is split into two permanent regions:
|
|
8
|
+
*
|
|
9
|
+
* ┌───────────────────────────────┐
|
|
10
|
+
* │ transcript (our OWN scroll) │ history · activity · streaming · output
|
|
11
|
+
* │ … │
|
|
12
|
+
* ├───────────────────────────────┤
|
|
13
|
+
* │ ❯ Ask Carrier OS… │ the FIXED composer — never moves/scrolls
|
|
14
|
+
* └───────────────────────────────┘
|
|
15
|
+
*
|
|
16
|
+
* A virtual screen buffer is diffed against what's on-screen, so each frame only
|
|
17
|
+
* rewrites the rows that actually changed — no full repaints, no flicker. All
|
|
18
|
+
* positioning is ABSOLUTE (no DECSTBM scroll regions), so it behaves identically
|
|
19
|
+
* across Windows Terminal, PowerShell, CMD, Git Bash, macOS Terminal and Linux.
|
|
20
|
+
*
|
|
21
|
+
* Scrolling is OURS: the transcript is a virtualized window over an in-memory
|
|
22
|
+
* line buffer. `scrollOffset` counts lines up from the bottom; 0 = following the
|
|
23
|
+
* newest output ("pinned"). While pinned, new/streaming output stays in view;
|
|
24
|
+
* once the user scrolls up we HOLD their position as content grows below, so you
|
|
25
|
+
* can browse history while a reply is still generating.
|
|
26
|
+
*/
|
|
27
|
+
const out = process.stdout;
|
|
28
|
+
const ESC = "\x1b[";
|
|
29
|
+
const HIDE = `${ESC}?25l`;
|
|
30
|
+
const SHOW = `${ESC}?25h`;
|
|
31
|
+
const ALT_ON = `${ESC}?1049h`; // enter alternate screen buffer (own the canvas)
|
|
32
|
+
const ALT_OFF = `${ESC}?1049l`; // leave it → restores the user's real terminal
|
|
33
|
+
// Alternate scroll mode: in the alt-screen the terminal converts the mouse WHEEL
|
|
34
|
+
// into arrow-key presses sent to the app. This is what makes the wheel work in
|
|
35
|
+
// terminals (notably VS Code's) that DON'T forward SGR mouse reports to apps —
|
|
36
|
+
// the wheel arrives as ↑/↓ keys, which we map to scrolling (see command-palette).
|
|
37
|
+
const SCROLL_ON = `${ESC}?1007h`;
|
|
38
|
+
const SCROLL_OFF = `${ESC}?1007l`;
|
|
39
|
+
/** Fixed composer height: input rule / prompt / rule + one footer row. */
|
|
40
|
+
const CORE_H = 4;
|
|
41
|
+
/** Cap on retained transcript lines (we repaint, not native-scroll). */
|
|
42
|
+
const MAX_SCROLLBACK = 5000;
|
|
43
|
+
let active = false;
|
|
44
|
+
let committed = []; // permanent transcript lines
|
|
45
|
+
let transient = []; // in-progress tail (spinner / streaming), below `committed`
|
|
46
|
+
let palette = []; // slash-palette overlay (empty when closed)
|
|
47
|
+
let bar = []; // the fixed composer core (CORE_H lines)
|
|
48
|
+
let caretBarIndex = 1; // which composer row the caret sits on (1 = prompt line)
|
|
49
|
+
let caretCol = 1; // 1-based column of the caret
|
|
50
|
+
let showCaret = false;
|
|
51
|
+
// Scroll state — see file header. `pinned` ⇒ auto-follow the newest output.
|
|
52
|
+
let scrollOffset = 0;
|
|
53
|
+
let pinned = true;
|
|
54
|
+
let shown = []; // what is currently on each terminal row (0 = row 1)
|
|
55
|
+
const rows = () => out.rows ?? 24;
|
|
56
|
+
const write = (s) => {
|
|
57
|
+
out.write(s);
|
|
58
|
+
};
|
|
59
|
+
export function screenActive() {
|
|
60
|
+
return active;
|
|
61
|
+
}
|
|
62
|
+
/** Layout metrics for the current frame (region sizes + the full line buffer). */
|
|
63
|
+
function metrics() {
|
|
64
|
+
const h = rows();
|
|
65
|
+
const core = bar.length || CORE_H;
|
|
66
|
+
const tb = Math.max(1, h - core); // transcript occupies rows 1..tb
|
|
67
|
+
const pH = Math.min(palette.length, Math.max(0, tb - 1)); // overlay never covers the whole region
|
|
68
|
+
const contentH = Math.max(0, tb - pH); // transcript rows above the palette overlay
|
|
69
|
+
const all = transient.length ? committed.concat(transient) : committed;
|
|
70
|
+
const maxOffset = Math.max(0, all.length - contentH);
|
|
71
|
+
return { h, core, tb, pH, contentH, all, maxOffset };
|
|
72
|
+
}
|
|
73
|
+
/** Clamp the scroll offset into range and recompute the pinned flag. */
|
|
74
|
+
function clampScroll() {
|
|
75
|
+
const { maxOffset } = metrics();
|
|
76
|
+
if (scrollOffset < 0)
|
|
77
|
+
scrollOffset = 0;
|
|
78
|
+
if (scrollOffset > maxOffset)
|
|
79
|
+
scrollOffset = maxOffset;
|
|
80
|
+
pinned = scrollOffset === 0;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* When content is appended below while the user is scrolled up, grow the offset
|
|
84
|
+
* by the number of added lines so their view stays put (Claude-Code behaviour).
|
|
85
|
+
* When pinned, do nothing — flush() will render the newest tail.
|
|
86
|
+
*/
|
|
87
|
+
function onContentGrew(added) {
|
|
88
|
+
if (!pinned && added > 0) {
|
|
89
|
+
scrollOffset += added;
|
|
90
|
+
const { maxOffset } = metrics();
|
|
91
|
+
if (scrollOffset > maxOffset)
|
|
92
|
+
scrollOffset = maxOffset;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Build the full desired screen: [transcript window][palette][composer core]. */
|
|
96
|
+
function compose() {
|
|
97
|
+
const { h, tb, pH, contentH, all, maxOffset } = metrics();
|
|
98
|
+
if (scrollOffset > maxOffset)
|
|
99
|
+
scrollOffset = maxOffset; // stay valid across resizes
|
|
100
|
+
// The visible window into `all`. offset 0 = last `contentH` lines (the tail);
|
|
101
|
+
// offset maxOffset = the very top.
|
|
102
|
+
const end = all.length - scrollOffset;
|
|
103
|
+
const start = Math.max(0, end - contentH);
|
|
104
|
+
const win = all.slice(start, end);
|
|
105
|
+
const desired = new Array(h).fill("");
|
|
106
|
+
for (let i = 0; i < contentH; i++)
|
|
107
|
+
desired[i] = win[i] ?? "";
|
|
108
|
+
// A subtle "more below" hint on the last transcript row when scrolled up.
|
|
109
|
+
if (scrollOffset > 0 && contentH > 0) {
|
|
110
|
+
desired[contentH - 1] = `${ESC}2m ↓ ${scrollOffset} more line${scrollOffset === 1 ? "" : "s"} - End to jump to latest${ESC}0m`;
|
|
111
|
+
}
|
|
112
|
+
for (let i = 0; i < pH; i++)
|
|
113
|
+
desired[contentH + i] = palette[i] ?? "";
|
|
114
|
+
for (let i = 0; i < (bar.length || CORE_H); i++)
|
|
115
|
+
desired[tb + i] = bar[i] ?? "";
|
|
116
|
+
return desired;
|
|
117
|
+
}
|
|
118
|
+
/** Diff the desired screen against what's on-screen; rewrite only changed rows. */
|
|
119
|
+
function flush() {
|
|
120
|
+
if (!active)
|
|
121
|
+
return;
|
|
122
|
+
const desired = compose();
|
|
123
|
+
const n = Math.max(desired.length, shown.length);
|
|
124
|
+
let s = HIDE;
|
|
125
|
+
for (let r = 0; r < n; r++) {
|
|
126
|
+
const d = desired[r] ?? "";
|
|
127
|
+
if (d !== (shown[r] ?? ""))
|
|
128
|
+
s += `${ESC}${r + 1};1H${ESC}2K` + d;
|
|
129
|
+
}
|
|
130
|
+
shown = desired;
|
|
131
|
+
const core = bar.length || CORE_H;
|
|
132
|
+
const caretRow = rows() - core + 1 + caretBarIndex;
|
|
133
|
+
// The composer is a permanent region, so its caret shows whenever the composer
|
|
134
|
+
// is active — independent of where the transcript is scrolled.
|
|
135
|
+
s += showCaret ? `${ESC}${caretRow};${caretCol}H${SHOW}` : HIDE;
|
|
136
|
+
write(s);
|
|
137
|
+
}
|
|
138
|
+
/** Enter TUI mode: alt-screen, alt-scroll (wheel→arrows), clear, hide caret. */
|
|
139
|
+
export function startScreen() {
|
|
140
|
+
if (!out.isTTY)
|
|
141
|
+
return;
|
|
142
|
+
active = true;
|
|
143
|
+
committed = [];
|
|
144
|
+
transient = [];
|
|
145
|
+
palette = [];
|
|
146
|
+
bar = [];
|
|
147
|
+
shown = [];
|
|
148
|
+
scrollOffset = 0;
|
|
149
|
+
pinned = true;
|
|
150
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
151
|
+
flush();
|
|
152
|
+
out.on("resize", screenResize);
|
|
153
|
+
}
|
|
154
|
+
/** Leave TUI mode: disable alt-scroll, leave alt-screen (restores real terminal). */
|
|
155
|
+
export function stopScreen() {
|
|
156
|
+
if (!active)
|
|
157
|
+
return;
|
|
158
|
+
active = false;
|
|
159
|
+
out.removeListener("resize", screenResize);
|
|
160
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
161
|
+
shown = [];
|
|
162
|
+
}
|
|
163
|
+
/** Trim the transcript scrollback to its cap (drop oldest lines). */
|
|
164
|
+
function trim() {
|
|
165
|
+
if (committed.length > MAX_SCROLLBACK)
|
|
166
|
+
committed.splice(0, committed.length - MAX_SCROLLBACK);
|
|
167
|
+
}
|
|
168
|
+
/** Append permanent transcript lines (multi-line strings are split). */
|
|
169
|
+
export function screenPrint(lines) {
|
|
170
|
+
let added = 0;
|
|
171
|
+
for (const l of lines) {
|
|
172
|
+
const parts = l.includes("\n") ? l.split("\n") : [l];
|
|
173
|
+
committed.push(...parts);
|
|
174
|
+
added += parts.length;
|
|
175
|
+
}
|
|
176
|
+
trim();
|
|
177
|
+
onContentGrew(added);
|
|
178
|
+
flush();
|
|
179
|
+
}
|
|
180
|
+
/** Set the in-progress tail shown just below the committed transcript. */
|
|
181
|
+
export function screenTransient(lines) {
|
|
182
|
+
const prev = transient.length;
|
|
183
|
+
transient = lines;
|
|
184
|
+
onContentGrew(transient.length - prev);
|
|
185
|
+
flush();
|
|
186
|
+
}
|
|
187
|
+
/** Promote the current transient tail to permanent transcript (no visual jump). */
|
|
188
|
+
export function screenCommitTransient() {
|
|
189
|
+
if (transient.length) {
|
|
190
|
+
committed.push(...transient);
|
|
191
|
+
transient = [];
|
|
192
|
+
trim();
|
|
193
|
+
}
|
|
194
|
+
flush();
|
|
195
|
+
}
|
|
196
|
+
/** Clear the whole transcript (Ctrl+L) and re-pin to the bottom. */
|
|
197
|
+
export function screenClear() {
|
|
198
|
+
committed = [];
|
|
199
|
+
transient = [];
|
|
200
|
+
shown = [];
|
|
201
|
+
scrollOffset = 0;
|
|
202
|
+
pinned = true;
|
|
203
|
+
write(`${ESC}2J`);
|
|
204
|
+
flush();
|
|
205
|
+
}
|
|
206
|
+
// ── Scroll API (keyboard + wheel) ─────────────────────────────────────────────
|
|
207
|
+
/** Scroll by `delta` lines: +up (older), −down (newer). Re-pins at the bottom. */
|
|
208
|
+
export function screenScrollLines(delta) {
|
|
209
|
+
if (!active)
|
|
210
|
+
return;
|
|
211
|
+
scrollOffset += delta;
|
|
212
|
+
clampScroll();
|
|
213
|
+
flush();
|
|
214
|
+
}
|
|
215
|
+
/** Scroll a (near-)page in the given direction: +1 up, −1 down. */
|
|
216
|
+
export function screenScrollPage(dir) {
|
|
217
|
+
const { contentH } = metrics();
|
|
218
|
+
screenScrollLines(dir * Math.max(1, contentH - 1));
|
|
219
|
+
}
|
|
220
|
+
/** Jump to the oldest content. */
|
|
221
|
+
export function screenScrollToTop() {
|
|
222
|
+
if (!active)
|
|
223
|
+
return;
|
|
224
|
+
const { maxOffset } = metrics();
|
|
225
|
+
scrollOffset = maxOffset;
|
|
226
|
+
pinned = maxOffset === 0;
|
|
227
|
+
flush();
|
|
228
|
+
}
|
|
229
|
+
/** Jump to the newest content and re-pin (auto-follow). */
|
|
230
|
+
export function screenScrollToBottom() {
|
|
231
|
+
if (!active)
|
|
232
|
+
return;
|
|
233
|
+
scrollOffset = 0;
|
|
234
|
+
pinned = true;
|
|
235
|
+
flush();
|
|
236
|
+
}
|
|
237
|
+
/** True when following the newest output (used to gate auto-follow decisions). */
|
|
238
|
+
export function screenIsPinned() {
|
|
239
|
+
return pinned;
|
|
240
|
+
}
|
|
241
|
+
/** True when the transcript is taller than the viewport (i.e. can be scrolled). */
|
|
242
|
+
export function screenScrollable() {
|
|
243
|
+
return metrics().maxOffset > 0;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Draw the composer: the fixed input `core` (CORE_H lines) at the bottom, with
|
|
247
|
+
* the `palette` overlaying the transcript rows directly above it. `caretIndex`
|
|
248
|
+
* is the core row the caret sits on (1 = prompt); `col` is its 1-based column.
|
|
249
|
+
*/
|
|
250
|
+
export function screenComposer(paletteLines, coreLines, caretIndex, col) {
|
|
251
|
+
palette = paletteLines;
|
|
252
|
+
bar = coreLines;
|
|
253
|
+
caretBarIndex = caretIndex;
|
|
254
|
+
caretCol = col;
|
|
255
|
+
showCaret = true;
|
|
256
|
+
flush();
|
|
257
|
+
}
|
|
258
|
+
/** Hide the caret (e.g. while a response streams — the ▋ is drawn in the text). */
|
|
259
|
+
export function screenHideCaret() {
|
|
260
|
+
showCaret = false;
|
|
261
|
+
flush();
|
|
262
|
+
}
|
|
263
|
+
/** Recompute + full-repaint after a terminal resize (composer stays pinned). */
|
|
264
|
+
export function screenResize() {
|
|
265
|
+
if (!active)
|
|
266
|
+
return;
|
|
267
|
+
shown = []; // force every row to be rewritten at the new size
|
|
268
|
+
clampScroll();
|
|
269
|
+
write(`${ESC}2J`);
|
|
270
|
+
flush();
|
|
271
|
+
}
|
|
272
|
+
/** Temporarily hand the terminal to an external UI (inquirer picker, etc.). */
|
|
273
|
+
export function suspendScreen() {
|
|
274
|
+
if (!active)
|
|
275
|
+
return;
|
|
276
|
+
active = false;
|
|
277
|
+
out.removeListener("resize", screenResize);
|
|
278
|
+
write(`${SCROLL_OFF}${ALT_OFF}${SHOW}`);
|
|
279
|
+
}
|
|
280
|
+
/** Resume TUI mode after suspendScreen(), repainting the preserved transcript. */
|
|
281
|
+
export function resumeScreen() {
|
|
282
|
+
if (!out.isTTY)
|
|
283
|
+
return;
|
|
284
|
+
active = true;
|
|
285
|
+
shown = [];
|
|
286
|
+
write(`${ALT_ON}${SCROLL_ON}${ESC}2J${ESC}H${HIDE}`);
|
|
287
|
+
flush();
|
|
288
|
+
out.on("resize", screenResize);
|
|
289
|
+
}
|