@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/select.js
CHANGED
|
@@ -1,14 +1,156 @@
|
|
|
1
1
|
import * as readline from "node:readline";
|
|
2
2
|
import { c } from "./colors.js";
|
|
3
|
+
import { resumeScreen, screenActive, suspendScreen } from "./screen.js";
|
|
3
4
|
import { termWidth } from "./ui.js";
|
|
5
|
+
/** Resolved when the user pressed ← on a picker opened with `allowBack`. */
|
|
6
|
+
export const SELECT_BACK = "\u0000back";
|
|
7
|
+
/**
|
|
8
|
+
* One painted frame of the picker, clipped to the terminal.
|
|
9
|
+
*
|
|
10
|
+
* Split out because the same frame is drawn two ways: straight to the terminal
|
|
11
|
+
* (below), or into the shell's transcript via the screen engine — which is what
|
|
12
|
+
* keeps the banner and conversation on screen while a picker is open.
|
|
13
|
+
*
|
|
14
|
+
* Rows are hard-clipped rather than wrapped: callers count LOGICAL lines to
|
|
15
|
+
* rewind or size a region, so a row the terminal wraps would occupy two
|
|
16
|
+
* physical rows and leave half the previous frame behind.
|
|
17
|
+
*/
|
|
18
|
+
export function selectFrame(opts, selected) {
|
|
19
|
+
const width = Math.min(termWidth() - 4, 92);
|
|
20
|
+
const build = (descriptionsFor) => {
|
|
21
|
+
const lines = headerLines(opts, width);
|
|
22
|
+
lines.push("");
|
|
23
|
+
lines.push(...optionLines(opts, selected, width, descriptionsFor));
|
|
24
|
+
lines.push("");
|
|
25
|
+
lines.push(...hintLines(opts, width));
|
|
26
|
+
return lines;
|
|
27
|
+
};
|
|
28
|
+
// Full descriptions cost 2-3 rows each, which on a short terminal would push
|
|
29
|
+
// the conversation off screen entirely — so fall back to describing only the
|
|
30
|
+
// highlighted option, which is still readable in full as you arrow through.
|
|
31
|
+
let lines = build("all");
|
|
32
|
+
if (opts.stacked && lines.length > roomForPrompt())
|
|
33
|
+
lines = build("selected");
|
|
34
|
+
const cols = termWidth();
|
|
35
|
+
return lines.map((l) => (visibleWidth(l) > cols ? clipAnsi(l, cols) : l));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Transcript rows a prompt may occupy — terminal height minus the 4-row
|
|
39
|
+
* composer, minus one so the frame never sits flush against it.
|
|
40
|
+
*
|
|
41
|
+
* Deliberately not reserving space for the conversation: the descriptions are
|
|
42
|
+
* the reason the prompt exists, and PgUp/PgDn can reach anything they cover.
|
|
43
|
+
*/
|
|
44
|
+
function roomForPrompt() {
|
|
45
|
+
return Math.max(8, (process.stdout.rows ?? 24) - 5);
|
|
46
|
+
}
|
|
47
|
+
function headerLines(opts, width) {
|
|
48
|
+
if (opts.header)
|
|
49
|
+
return opts.header(width);
|
|
50
|
+
const lines = [" " + c.brand("─".repeat(width)), " " + c.brand.bold(opts.title)];
|
|
51
|
+
if (opts.description) {
|
|
52
|
+
for (const l of wrap(opts.description, width))
|
|
53
|
+
lines.push(" " + c.dim(l));
|
|
54
|
+
}
|
|
55
|
+
return lines;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The key legend, packed into as many lines as it needs.
|
|
59
|
+
*
|
|
60
|
+
* Wrapping rather than clipping: the last chip is what Esc does, and on this
|
|
61
|
+
* prompt that is "discard message" — the one hint you cannot afford to lose to
|
|
62
|
+
* a narrow terminal.
|
|
63
|
+
*
|
|
64
|
+
* ←→ rather than ⇄: it names both keys instead of half the pair, and it is the
|
|
65
|
+
* same arrow family as ↑↓ (U+2190/2192 beside U+2191/2193), so the two chips
|
|
66
|
+
* match in weight. ⇄ falls back to a different font in many terminals and
|
|
67
|
+
* rendered noticeably heavier than the arrows above it.
|
|
68
|
+
*/
|
|
69
|
+
function hintLines(opts, width) {
|
|
70
|
+
const chips = [c.dim("↑↓ navigate")];
|
|
71
|
+
if (opts.allowBack)
|
|
72
|
+
chips.push(c.key("←→") + c.dim(` ${opts.backHint ?? "back"}`));
|
|
73
|
+
if (opts.scrollable)
|
|
74
|
+
chips.push(c.key("PgUp/PgDn") + c.dim(" scroll"));
|
|
75
|
+
chips.push(c.key("Enter") + c.dim(" confirm"));
|
|
76
|
+
chips.push(c.key("Esc") + c.dim(opts.cancelHint ? ` ${opts.cancelHint}` : " cancel"));
|
|
77
|
+
const SEP = " · ";
|
|
78
|
+
const lines = [];
|
|
79
|
+
let cur = "";
|
|
80
|
+
let curW = 0;
|
|
81
|
+
for (const chip of chips) {
|
|
82
|
+
const w = visibleWidth(chip);
|
|
83
|
+
if (cur && curW + SEP.length + w > width) {
|
|
84
|
+
lines.push(" " + cur);
|
|
85
|
+
cur = chip;
|
|
86
|
+
curW = w;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
cur = cur ? cur + c.dim(SEP) + chip : chip;
|
|
90
|
+
curW = curW ? curW + SEP.length + w : w;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (cur)
|
|
94
|
+
lines.push(" " + cur);
|
|
95
|
+
return lines;
|
|
96
|
+
}
|
|
97
|
+
function optionLines(opts, selected, width, descriptionsFor) {
|
|
98
|
+
const { choices } = opts;
|
|
99
|
+
const numW = String(choices.length).length;
|
|
100
|
+
const lines = [];
|
|
101
|
+
if (!opts.stacked) {
|
|
102
|
+
// Column layout: label padded to a shared width, description clipped to fit.
|
|
103
|
+
const labelW = Math.min(30, Math.max(...choices.map((ch) => ch.label.length)));
|
|
104
|
+
const gutter = 2 + numW + 1 + 1 + labelW + 2;
|
|
105
|
+
choices.forEach((ch, i) => {
|
|
106
|
+
const on = i === selected;
|
|
107
|
+
const pointer = on ? c.brand("❯ ") : " ";
|
|
108
|
+
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
109
|
+
const label = (on ? c.brand.bold : c.value)(ch.label.padEnd(labelW));
|
|
110
|
+
const mark = ch.current ? c.brand(" ✓") : "";
|
|
111
|
+
const room = width - gutter - (ch.current ? 2 : 0);
|
|
112
|
+
const desc = ch.description ? " " + c.dim(clip(ch.description, room)) : "";
|
|
113
|
+
lines.push(" " + pointer + num + " " + label + desc + mark);
|
|
114
|
+
});
|
|
115
|
+
return lines;
|
|
116
|
+
}
|
|
117
|
+
// Stacked layout: label on its own row, description wrapped beneath it and
|
|
118
|
+
// aligned under the label rather than the number.
|
|
119
|
+
const indent = " ".repeat(2 + 2 + numW + 1 + 1);
|
|
120
|
+
choices.forEach((ch, i) => {
|
|
121
|
+
const on = i === selected;
|
|
122
|
+
const pointer = on ? c.brand("❯ ") : " ";
|
|
123
|
+
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
124
|
+
const label = (on ? c.brand.bold : c.value)(ch.label);
|
|
125
|
+
const mark = ch.current ? c.brand(" ✓") : "";
|
|
126
|
+
lines.push(" " + pointer + num + " " + label + mark);
|
|
127
|
+
const show = ch.description && (descriptionsFor === "all" || on);
|
|
128
|
+
if (show) {
|
|
129
|
+
// Two lines is the most a description needs; beyond that it is prose the
|
|
130
|
+
// list can't carry, and every extra row is one less of the conversation.
|
|
131
|
+
const wrapped = wrap(ch.description, width - indent.length + 2);
|
|
132
|
+
const shown = wrapped.slice(0, 2);
|
|
133
|
+
if (wrapped.length > 2)
|
|
134
|
+
shown[1] = shown[1].replace(/.{0,2}$/, "…");
|
|
135
|
+
for (const l of shown)
|
|
136
|
+
lines.push(indent + c.dim(l));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return lines;
|
|
140
|
+
}
|
|
4
141
|
export function pickFromList(opts) {
|
|
5
|
-
const {
|
|
142
|
+
const { choices, confirmOnNumber = false } = opts;
|
|
6
143
|
if (!process.stdin.isTTY || choices.length === 0) {
|
|
7
144
|
return Promise.resolve(choices[opts.initial ?? 0]?.value ?? null);
|
|
8
145
|
}
|
|
9
146
|
return new Promise((resolve) => {
|
|
10
147
|
const stdin = process.stdin;
|
|
11
148
|
const stdout = process.stdout;
|
|
149
|
+
// Hand the terminal over from the persistent-composer engine for the picker,
|
|
150
|
+
// then repaint the transcript + composer when it closes.
|
|
151
|
+
const suspended = screenActive();
|
|
152
|
+
if (suspended)
|
|
153
|
+
suspendScreen();
|
|
12
154
|
stdin.removeAllListeners("keypress");
|
|
13
155
|
readline.emitKeypressEvents(stdin);
|
|
14
156
|
const prevRaw = stdin.isRaw ?? false;
|
|
@@ -17,38 +159,14 @@ export function pickFromList(opts) {
|
|
|
17
159
|
stdin.resume();
|
|
18
160
|
let selected = Math.min(Math.max(0, opts.initial ?? 0), choices.length - 1);
|
|
19
161
|
let prevUp = 0;
|
|
20
|
-
const labelW = Math.min(30, Math.max(...choices.map((ch) => ch.label.length)));
|
|
21
|
-
const numW = String(choices.length).length;
|
|
22
162
|
const render = () => {
|
|
23
163
|
if (prevUp > 0)
|
|
24
164
|
readline.moveCursor(stdout, 0, -prevUp);
|
|
25
165
|
readline.cursorTo(stdout, 0);
|
|
26
166
|
readline.clearScreenDown(stdout);
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
for (const l of wrap(description, width))
|
|
31
|
-
lines.push(" " + c.dim(l));
|
|
32
|
-
}
|
|
33
|
-
lines.push("");
|
|
34
|
-
choices.forEach((ch, i) => {
|
|
35
|
-
const on = i === selected;
|
|
36
|
-
const pointer = on ? c.brand("❯ ") : " ";
|
|
37
|
-
const num = c.dim(`${String(i + 1).padStart(numW)}.`);
|
|
38
|
-
const label = (on ? c.brand.bold : c.value)(ch.label.padEnd(labelW));
|
|
39
|
-
const desc = ch.description ? " " + c.dim(ch.description) : "";
|
|
40
|
-
const mark = ch.current ? c.brand(" ✓") : "";
|
|
41
|
-
lines.push(" " + pointer + num + " " + label + desc + mark);
|
|
42
|
-
});
|
|
43
|
-
lines.push("");
|
|
44
|
-
lines.push(" " +
|
|
45
|
-
c.dim("↑↓ navigate · ") +
|
|
46
|
-
c.key("Enter") +
|
|
47
|
-
c.dim(" confirm · ") +
|
|
48
|
-
c.key("Esc") +
|
|
49
|
-
c.dim(" cancel"));
|
|
50
|
-
stdout.write(lines.join("\n"));
|
|
51
|
-
prevUp = lines.length - 1; // cursor rests on the last line; rewind from there next time
|
|
167
|
+
const painted = selectFrame(opts, selected);
|
|
168
|
+
stdout.write(painted.join("\n"));
|
|
169
|
+
prevUp = painted.length - 1; // cursor rests on the last line; rewind from there next time
|
|
52
170
|
};
|
|
53
171
|
const finish = (val) => {
|
|
54
172
|
if (prevUp > 0)
|
|
@@ -57,6 +175,8 @@ export function pickFromList(opts) {
|
|
|
57
175
|
readline.clearScreenDown(stdout);
|
|
58
176
|
stdin.removeListener("keypress", onKey);
|
|
59
177
|
stdin.setRawMode(prevRaw);
|
|
178
|
+
if (suspended)
|
|
179
|
+
resumeScreen(); // repaint the pinned composer + transcript
|
|
60
180
|
resolve(val);
|
|
61
181
|
};
|
|
62
182
|
const onKey = (str, key) => {
|
|
@@ -71,16 +191,25 @@ export function pickFromList(opts) {
|
|
|
71
191
|
selected = (selected + 1) % choices.length;
|
|
72
192
|
return render();
|
|
73
193
|
}
|
|
194
|
+
// ← steps back a question, → confirms and moves on (same as Enter), so
|
|
195
|
+
// a multi-step flow reads as one horizontal track.
|
|
196
|
+
if (opts.allowBack && key.name === "left")
|
|
197
|
+
return finish(SELECT_BACK);
|
|
198
|
+
if (key.name === "right")
|
|
199
|
+
return finish(choices[selected].value);
|
|
74
200
|
if (key.name === "return" || key.name === "enter")
|
|
75
201
|
return finish(choices[selected].value);
|
|
76
202
|
if (key.name === "escape" || (key.ctrl && (key.name === "c" || key.name === "d"))) {
|
|
77
203
|
return finish(null);
|
|
78
204
|
}
|
|
79
|
-
// Number keys jump to an option
|
|
205
|
+
// Number keys jump to an option — and confirm it directly when the
|
|
206
|
+
// caller asked for that (Yes/No style prompts).
|
|
80
207
|
if (str && /^[1-9]$/.test(str)) {
|
|
81
208
|
const idx = Number(str) - 1;
|
|
82
209
|
if (idx < choices.length) {
|
|
83
210
|
selected = idx;
|
|
211
|
+
if (confirmOnNumber)
|
|
212
|
+
return finish(choices[idx].value);
|
|
84
213
|
render();
|
|
85
214
|
}
|
|
86
215
|
}
|
|
@@ -93,6 +222,41 @@ export function pickFromList(opts) {
|
|
|
93
222
|
render();
|
|
94
223
|
});
|
|
95
224
|
}
|
|
225
|
+
const ANSI_RE = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
226
|
+
/** Visible columns, ignoring colour codes. */
|
|
227
|
+
function visibleWidth(s) {
|
|
228
|
+
return [...s.replace(ANSI_RE, "")].length;
|
|
229
|
+
}
|
|
230
|
+
/** Truncate to `max` visible columns, keeping colour codes intact and closing
|
|
231
|
+
* any open run so the cut can't bleed into the next line. */
|
|
232
|
+
function clipAnsi(s, max) {
|
|
233
|
+
let out = "";
|
|
234
|
+
let seen = 0;
|
|
235
|
+
let i = 0;
|
|
236
|
+
while (i < s.length && seen < max) {
|
|
237
|
+
if (s[i] === "\x1b") {
|
|
238
|
+
ANSI_RE.lastIndex = i;
|
|
239
|
+
const m = ANSI_RE.exec(s);
|
|
240
|
+
if (m && m.index === i) {
|
|
241
|
+
out += m[0];
|
|
242
|
+
i += m[0].length;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
out += s[i];
|
|
247
|
+
seen += 1;
|
|
248
|
+
i += 1;
|
|
249
|
+
}
|
|
250
|
+
return out + "\x1b[0m";
|
|
251
|
+
}
|
|
252
|
+
/** Trim to `max` columns, marking the cut with an ellipsis. Returns "" when
|
|
253
|
+
* there is no room worth using. */
|
|
254
|
+
function clip(text, max) {
|
|
255
|
+
if (max < 4)
|
|
256
|
+
return "";
|
|
257
|
+
const chars = [...text];
|
|
258
|
+
return chars.length <= max ? text : chars.slice(0, max - 1).join("").trimEnd() + "…";
|
|
259
|
+
}
|
|
96
260
|
/** Minimal word wrap for the description line. */
|
|
97
261
|
function wrap(text, width) {
|
|
98
262
|
const w = Math.max(20, width);
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { ApiError } from "../services/api.js";
|
|
2
|
+
import { buildStructuredIntent } from "../services/sia.js";
|
|
3
|
+
import { line } from "../services/output.js";
|
|
4
|
+
import { withSpinner } from "../services/spinner.js";
|
|
5
|
+
import { c } from "./colors.js";
|
|
6
|
+
import { pickFromList, SELECT_BACK } from "./select.js";
|
|
7
|
+
import { askTextInScreen, inScreen, pickInScreen } from "./screen-prompt.js";
|
|
8
|
+
import { askText } from "./text-prompt.js";
|
|
9
|
+
/**
|
|
10
|
+
* Inside the shell, prompts draw into the transcript so the banner and the
|
|
11
|
+
* conversation stay visible above them. Outside it (`cos chat`, a pipe) there
|
|
12
|
+
* is no screen engine, so the terminal-owning versions are used instead.
|
|
13
|
+
*/
|
|
14
|
+
const ask = (opts) => inScreen() ? pickInScreen(opts) : pickFromList(opts);
|
|
15
|
+
/**
|
|
16
|
+
* SIA clarification — the terminal counterpart of the website's Q&A overlay.
|
|
17
|
+
*
|
|
18
|
+
* Before an ambiguous prompt is sent, the API is asked what it would need to
|
|
19
|
+
* know; the answers are appended to the prompt and THAT is what gets sent. The
|
|
20
|
+
* questions are a way to write a better prompt, not a separate request — see
|
|
21
|
+
* services/sia.ts for the merge format both clients share.
|
|
22
|
+
*
|
|
23
|
+
* Failures are non-blocking but NOT silent. If the API is unreachable or
|
|
24
|
+
* refuses, the original prompt still goes out — clarification must never be the
|
|
25
|
+
* reason a message can't be sent — but one dim line says so, because with SIA
|
|
26
|
+
* on by default a swallowed error is indistinguishable from the backend
|
|
27
|
+
* deciding no clarification was needed.
|
|
28
|
+
*/
|
|
29
|
+
/** Sentinel values that can't collide with a real option value. */
|
|
30
|
+
const OTHER = "\u0000other";
|
|
31
|
+
const SKIP = "\u0000skip";
|
|
32
|
+
const unchanged = (prompt) => ({ prompt, cancelled: false });
|
|
33
|
+
/** Options for one question, plus the two escapes every question offers. */
|
|
34
|
+
function choicesFor(q) {
|
|
35
|
+
const base = q.type === "yn"
|
|
36
|
+
? [
|
|
37
|
+
{ value: "true", label: "Yes" },
|
|
38
|
+
{ value: "false", label: "No" }
|
|
39
|
+
]
|
|
40
|
+
: (q.options ?? []).map((o) => ({
|
|
41
|
+
value: o.value,
|
|
42
|
+
label: o.label,
|
|
43
|
+
description: o.description
|
|
44
|
+
}));
|
|
45
|
+
// No descriptions on these two: the labels already say it, and every
|
|
46
|
+
// description is a row taken from the answers that need one.
|
|
47
|
+
return [...base, { value: OTHER, label: "Other…" }, { value: SKIP, label: "Skip this question" }];
|
|
48
|
+
}
|
|
49
|
+
/** Word-wrap to `width` columns. */
|
|
50
|
+
function wrap(text, width) {
|
|
51
|
+
const w = Math.max(20, width);
|
|
52
|
+
const out = [];
|
|
53
|
+
let cur = "";
|
|
54
|
+
for (const word of text.trim().split(/\s+/).filter(Boolean)) {
|
|
55
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
56
|
+
out.push(cur);
|
|
57
|
+
cur = word;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (cur)
|
|
64
|
+
out.push(cur);
|
|
65
|
+
return out.length ? out : [""];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The block above the options.
|
|
69
|
+
*
|
|
70
|
+
* It restates the prompt on EVERY question, which is not redundancy: a picker
|
|
71
|
+
* takes over the terminal from the shell's screen engine, so the banner and the
|
|
72
|
+
* echoed message are in a buffer the user cannot scroll back to while answering.
|
|
73
|
+
* Without this, the questions appear with no visible sign of what they are about.
|
|
74
|
+
*/
|
|
75
|
+
/** Wrap, then hard-limit to `max` lines, marking the cut on the last one. */
|
|
76
|
+
function clampLines(text, width, max) {
|
|
77
|
+
const lines = wrap(text, width);
|
|
78
|
+
if (lines.length <= max)
|
|
79
|
+
return lines;
|
|
80
|
+
const shown = lines.slice(0, max);
|
|
81
|
+
shown[max - 1] = shown[max - 1].replace(/.{0,2}$/, "…");
|
|
82
|
+
return shown;
|
|
83
|
+
}
|
|
84
|
+
function siaHeader(prompt, q, index, total) {
|
|
85
|
+
return (width) => {
|
|
86
|
+
// Every row here is a row of your conversation pushed off screen, and the
|
|
87
|
+
// transcript only gets terminal-height minus the composer. So both blocks
|
|
88
|
+
// are capped: one line to recognise your own message, three for the
|
|
89
|
+
// question — the model pads these to 1-3 sentences and the ask is at the
|
|
90
|
+
// front. The options and their descriptions are what the rows are for.
|
|
91
|
+
const promptLine = clampLines(prompt, width - 2, 1);
|
|
92
|
+
return [
|
|
93
|
+
" " + c.brand("─".repeat(width)),
|
|
94
|
+
" " + c.dim("❯ ") + c.dim(promptLine[0]),
|
|
95
|
+
"",
|
|
96
|
+
" " + c.brand.bold(`Question ${index + 1} of ${total}`),
|
|
97
|
+
...clampLines(q.text, width, 3).map((l) => " " + c.value(l))
|
|
98
|
+
];
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Index of the choice matching an already-given answer, so stepping back lands
|
|
102
|
+
* on what was picked last time rather than resetting to the top. */
|
|
103
|
+
function initialIndex(choices, previous) {
|
|
104
|
+
if (previous === undefined)
|
|
105
|
+
return 0;
|
|
106
|
+
const want = typeof previous === "boolean" ? String(previous) : previous;
|
|
107
|
+
const at = choices.findIndex((ch) => ch.value === want);
|
|
108
|
+
return at === -1 ? 0 : at;
|
|
109
|
+
}
|
|
110
|
+
/** Ask one question. null = cancelled, "back" = step to the previous question. */
|
|
111
|
+
async function askOne(prompt, q, index, total, previous, canGoBack) {
|
|
112
|
+
const heading = `Question ${index + 1} of ${total}`;
|
|
113
|
+
// An "open" question has nothing to pick from — go straight to free text.
|
|
114
|
+
// There is no list to press ← in, so it is the one step without back.
|
|
115
|
+
if (q.type === "open") {
|
|
116
|
+
const typed = inScreen()
|
|
117
|
+
? await askTextInScreen(siaHeader(prompt, q, index, total), q.placeholder)
|
|
118
|
+
: await askText(`${heading} ${q.text}`, q.placeholder ?? "Enter to skip");
|
|
119
|
+
return { answer: typed };
|
|
120
|
+
}
|
|
121
|
+
const choices = choicesFor(q);
|
|
122
|
+
const picked = await ask({
|
|
123
|
+
title: heading,
|
|
124
|
+
header: siaHeader(prompt, q, index, total),
|
|
125
|
+
choices,
|
|
126
|
+
initial: initialIndex(choices, previous),
|
|
127
|
+
allowBack: canGoBack,
|
|
128
|
+
// "prev/next" rather than "question": it says you can move BOTH ways
|
|
129
|
+
// through the set, which the noun alone left you to guess.
|
|
130
|
+
backHint: "prev/next question",
|
|
131
|
+
// The descriptions ARE the decision here — a clipped one hides what's
|
|
132
|
+
// being chosen between.
|
|
133
|
+
stacked: true,
|
|
134
|
+
// Esc abandons the message, which "cancel" badly understates when the row
|
|
135
|
+
// above it says "Skip this question".
|
|
136
|
+
cancelHint: "discard message"
|
|
137
|
+
});
|
|
138
|
+
// Esc / Ctrl+C. The website treats Esc as "send it anyway", but in a terminal
|
|
139
|
+
// that gesture means abort — and sending a prompt someone tried to cancel
|
|
140
|
+
// spends real money. So it stops the turn, and "Skip this question" is the
|
|
141
|
+
// explicit way to move past one.
|
|
142
|
+
if (picked === null)
|
|
143
|
+
return null;
|
|
144
|
+
if (picked === SELECT_BACK)
|
|
145
|
+
return "back";
|
|
146
|
+
if (picked === SKIP)
|
|
147
|
+
return { answer: null };
|
|
148
|
+
if (picked === OTHER) {
|
|
149
|
+
const typed = inScreen()
|
|
150
|
+
? await askTextInScreen(siaHeader(prompt, q, index, total), q.placeholder)
|
|
151
|
+
: await askText(q.text, q.placeholder ?? "Your answer");
|
|
152
|
+
return { answer: typed };
|
|
153
|
+
}
|
|
154
|
+
if (q.type === "yn")
|
|
155
|
+
return { answer: picked === "true" };
|
|
156
|
+
return { answer: picked };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Run the clarification pass for `message`.
|
|
160
|
+
*
|
|
161
|
+
* @param onLookup called when the API round-trip starts, so a caller with a
|
|
162
|
+
* pinned composer can clear its own status area first.
|
|
163
|
+
*/
|
|
164
|
+
export async function clarifyIntent(client, message, onLookup) {
|
|
165
|
+
onLookup?.();
|
|
166
|
+
let questions;
|
|
167
|
+
try {
|
|
168
|
+
const res = await withSpinner(c.dim("Clarifying intent…"), () => client.siaQuestions(message));
|
|
169
|
+
if (res.skip || res.questions.length === 0)
|
|
170
|
+
return unchanged(message);
|
|
171
|
+
questions = res.questions;
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
// Unreachable, unauthorized, rate-limited — none of it should block a chat,
|
|
175
|
+
// so the prompt still goes out unchanged.
|
|
176
|
+
//
|
|
177
|
+
// But it must not be SILENT. With SIA on by default, a swallowed failure is
|
|
178
|
+
// indistinguishable from the backend deciding no clarification was needed —
|
|
179
|
+
// so an expired token looks exactly like SIA working correctly, forever.
|
|
180
|
+
// One dim line is enough to tell the two apart.
|
|
181
|
+
line(" " +
|
|
182
|
+
c.dim("↳ SIA unavailable — sending as typed. ") +
|
|
183
|
+
c.dim(err instanceof ApiError ? "(" + err.code + ")" : ""));
|
|
184
|
+
return unchanged(message);
|
|
185
|
+
}
|
|
186
|
+
// No "a few questions first" banner: it would be written into the shell's
|
|
187
|
+
// transcript, which the picker is about to suspend — so it would be invisible
|
|
188
|
+
// during the Q&A and a stray line afterwards. The count lives in each
|
|
189
|
+
// question's own header instead.
|
|
190
|
+
//
|
|
191
|
+
// A cursor rather than a for-loop: ← steps back, and answers survive the
|
|
192
|
+
// round trip so revisiting a question shows what you already chose.
|
|
193
|
+
const answers = {};
|
|
194
|
+
let i = 0;
|
|
195
|
+
while (i < questions.length) {
|
|
196
|
+
const q = questions[i];
|
|
197
|
+
const result = await askOne(message, q, i, questions.length, answers[q.key], i > 0);
|
|
198
|
+
if (result === null)
|
|
199
|
+
return { prompt: message, cancelled: true };
|
|
200
|
+
if (result === "back") {
|
|
201
|
+
i -= 1;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
// Skipping clears any earlier answer — otherwise going back to un-answer a
|
|
205
|
+
// question would silently keep the old value in the prompt.
|
|
206
|
+
if (result.answer === null)
|
|
207
|
+
delete answers[q.key];
|
|
208
|
+
else
|
|
209
|
+
answers[q.key] = result.answer;
|
|
210
|
+
i += 1;
|
|
211
|
+
}
|
|
212
|
+
return { prompt: buildStructuredIntent(message, answers), cancelled: false };
|
|
213
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import * as readline from "node:readline";
|
|
2
|
+
import { c } from "./colors.js";
|
|
3
|
+
import { resumeScreen, screenActive, suspendScreen } from "./screen.js";
|
|
4
|
+
import { termWidth } from "./ui.js";
|
|
5
|
+
/** Word-wrap to `width` columns. */
|
|
6
|
+
function wrap(text, width) {
|
|
7
|
+
const w = Math.max(20, width);
|
|
8
|
+
const lines = [];
|
|
9
|
+
let cur = "";
|
|
10
|
+
for (const word of text.trim().split(/\s+/).filter(Boolean)) {
|
|
11
|
+
if (cur && [...cur].length + 1 + [...word].length > w) {
|
|
12
|
+
lines.push(cur);
|
|
13
|
+
cur = word;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
cur = cur ? `${cur} ${word}` : word;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (cur)
|
|
20
|
+
lines.push(cur);
|
|
21
|
+
return lines.length ? lines : [""];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A one-line free-text prompt, for the places a list can't answer the question
|
|
25
|
+
* — SIA's "open" questions and the "Other…" escape on every other type.
|
|
26
|
+
*
|
|
27
|
+
* Deliberately NOT @inquirer: the picker next door avoids it because handing
|
|
28
|
+
* stdin back and forth froze the composer, and this has to interleave with the
|
|
29
|
+
* same pinned-composer engine. Cooked mode is restored for the duration so
|
|
30
|
+
* readline owns line editing (backspace, arrows, paste), then handed back.
|
|
31
|
+
*
|
|
32
|
+
* Resolves the trimmed answer, or null if it was empty or cancelled.
|
|
33
|
+
*/
|
|
34
|
+
export function askText(label, placeholder) {
|
|
35
|
+
const stdin = process.stdin;
|
|
36
|
+
const stdout = process.stdout;
|
|
37
|
+
if (!stdin.isTTY)
|
|
38
|
+
return Promise.resolve(null);
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
const suspended = screenActive();
|
|
41
|
+
if (suspended)
|
|
42
|
+
suspendScreen();
|
|
43
|
+
// The picker leaves keypress listeners and raw mode behind; readline needs
|
|
44
|
+
// neither, and a stale listener would eat the typed characters.
|
|
45
|
+
stdin.removeAllListeners("keypress");
|
|
46
|
+
const prevRaw = stdin.isRaw ?? false;
|
|
47
|
+
if (stdin.isTTY)
|
|
48
|
+
stdin.setRawMode(false);
|
|
49
|
+
stdin.resume();
|
|
50
|
+
// Wrap to the terminal, at word boundaries. A SIA question is a full
|
|
51
|
+
// sentence or three, and left raw the terminal breaks it mid-word.
|
|
52
|
+
const width = Math.min(termWidth() - 4, 92);
|
|
53
|
+
stdout.write("\n");
|
|
54
|
+
for (const l of wrap(label, width))
|
|
55
|
+
stdout.write(" " + c.title(l) + "\n");
|
|
56
|
+
if (placeholder)
|
|
57
|
+
stdout.write(" " + c.dim(placeholder) + "\n");
|
|
58
|
+
const rl = readline.createInterface({ input: stdin, output: stdout, terminal: true });
|
|
59
|
+
let settled = false;
|
|
60
|
+
const finish = (value) => {
|
|
61
|
+
if (settled)
|
|
62
|
+
return;
|
|
63
|
+
settled = true;
|
|
64
|
+
rl.close();
|
|
65
|
+
if (stdin.isTTY)
|
|
66
|
+
stdin.setRawMode(prevRaw);
|
|
67
|
+
if (suspended)
|
|
68
|
+
resumeScreen();
|
|
69
|
+
resolve(value);
|
|
70
|
+
};
|
|
71
|
+
rl.question(" " + c.brandFixed("❯ "), (answer) => finish(answer.trim() || null));
|
|
72
|
+
rl.on("SIGINT", () => finish(null));
|
|
73
|
+
});
|
|
74
|
+
}
|
package/dist/ui/theme.js
CHANGED
|
@@ -9,6 +9,12 @@ import chalk from "chalk";
|
|
|
9
9
|
* Future-ready: this is the seam a fuller theme (background, font, links) would
|
|
10
10
|
* plug into — add fields here, not scattered Chalk calls across the app.
|
|
11
11
|
*/
|
|
12
|
+
/**
|
|
13
|
+
* The Carrier OS brand green — FIXED. Brand elements (the "CARRIER OS" wordmark,
|
|
14
|
+
* the prompt marker) always render in this and never follow the runtime accent,
|
|
15
|
+
* so `/color` only re-themes the accent surfaces (rules, selection, etc.).
|
|
16
|
+
*/
|
|
17
|
+
export const BRAND_HEX = "#9ef56b";
|
|
12
18
|
/** Named accent colors and their hex. `default` is Carrier OS green. */
|
|
13
19
|
export const THEME_COLORS = {
|
|
14
20
|
green: "#9ef56b",
|
|
@@ -46,6 +52,17 @@ export function accentName() {
|
|
|
46
52
|
export function accentHex(name = current) {
|
|
47
53
|
return THEME_COLORS[name] ?? THEME_COLORS.green;
|
|
48
54
|
}
|
|
55
|
+
/** Mix a hex color toward white by `t` (0 = unchanged, 1 = white). */
|
|
56
|
+
function lighten(hex, t) {
|
|
57
|
+
const n = parseInt(hex.slice(1), 16);
|
|
58
|
+
const ch = [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
59
|
+
return "#" + ch.map((v) => Math.round(v + (255 - v) * t).toString(16).padStart(2, "0")).join("");
|
|
60
|
+
}
|
|
61
|
+
/** A light tint of the current accent — for soft accent marks (e.g. the
|
|
62
|
+
* assistant's lead dot), so they read as "theme-light" rather than pure white. */
|
|
63
|
+
export function accentLightHex(name = current) {
|
|
64
|
+
return lighten(accentHex(name), 0.5);
|
|
65
|
+
}
|
|
49
66
|
/** Switch the live accent. Takes effect immediately across the UI. */
|
|
50
67
|
export function setAccent(name) {
|
|
51
68
|
if (isThemeColor(name))
|