@carrieros/cli 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -57
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +102 -13
- package/dist/commands/context.js +160 -0
- package/dist/commands/doctor.js +53 -8
- package/dist/commands/files.js +84 -23
- package/dist/commands/help.js +3 -1
- package/dist/commands/history.js +88 -23
- package/dist/commands/login.js +44 -41
- package/dist/commands/logout.js +13 -4
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +39 -3
- package/dist/commands/modes.js +4 -1
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +152 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +219 -65
- package/dist/commands/settings.js +87 -0
- package/dist/commands/update.js +159 -0
- package/dist/commands/upload.js +204 -31
- package/dist/commands/usage.js +212 -16
- package/dist/commands/whoami.js +2 -1
- package/dist/ide/editor-context.js +172 -0
- package/dist/ide/ide-session.js +94 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +30 -1
- package/dist/services/api.js +171 -13
- package/dist/services/chat-stream.js +145 -0
- package/dist/services/checkpoints.js +116 -0
- package/dist/services/client-tools.js +260 -0
- package/dist/services/command-coordinator.js +200 -0
- package/dist/services/command-policy.js +404 -0
- package/dist/services/config.js +249 -22
- package/dist/services/context.js +307 -6
- package/dist/services/device-auth.js +261 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/git-tools.js +248 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +64 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/job-helper.js +307 -0
- package/dist/services/local-files.js +517 -0
- package/dist/services/local-tools.js +432 -0
- package/dist/services/login-flow.js +120 -10
- package/dist/services/meta.js +3 -1
- package/dist/services/mutation-coordinator.js +161 -0
- package/dist/services/mutations.js +493 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +13 -5
- package/dist/services/packaged.js +24 -0
- package/dist/services/process-runner.js +406 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/secure-store.js +197 -0
- package/dist/services/session-resolver.js +108 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +23 -1
- package/dist/services/update.js +286 -0
- package/dist/services/workspace-registry.js +309 -0
- package/dist/services/workspace.js +573 -0
- package/dist/shell/shell-chat.js +1115 -59
- package/dist/shell/shell-commands.js +220 -19
- package/dist/shell/shell-prompt.js +89 -37
- package/dist/shell/shell-renderer.js +19 -38
- package/dist/shell/shell-router.js +19 -3
- package/dist/shell/shell.js +232 -44
- package/dist/types/index.js +3 -1
- package/dist/ui/approval.js +81 -0
- package/dist/ui/banner.js +55 -13
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +87 -0
- package/dist/ui/chat-renderer.js +223 -29
- package/dist/ui/color-picker.js +21 -0
- package/dist/ui/colors.js +9 -1
- package/dist/ui/command-filter.js +40 -5
- package/dist/ui/command-palette.js +122 -62
- package/dist/ui/command-registry.js +85 -11
- package/dist/ui/command-review.js +138 -0
- package/dist/ui/composer-layout.js +41 -0
- package/dist/ui/conversation.js +398 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +18 -1
- package/dist/ui/markdown.js +63 -9
- package/dist/ui/message.js +14 -31
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +289 -0
- package/dist/ui/select.js +193 -29
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +17 -0
- package/dist/ui/trust.js +93 -0
- package/dist/ui/ui.js +4 -1
- package/package.json +17 -6
- package/dist/ui/activity.js +0 -77
|
@@ -0,0 +1,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
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { c } from "./colors.js";
|
|
2
|
+
import { MARGIN, contentWidth } from "./composer-layout.js";
|
|
3
|
+
import { clip, stripControl, wrapText } from "./conversation.js";
|
|
4
|
+
/**
|
|
5
|
+
* How a proposed file change is shown before anyone approves it.
|
|
6
|
+
*
|
|
7
|
+
* Compact header, then the diff — committed to the transcript rather than drawn
|
|
8
|
+
* in the prompt, so it stays on screen as the record of what was approved, and
|
|
9
|
+
* a long one can be scrolled back to. The limits that decide whether a change
|
|
10
|
+
* is reviewable at all live in the mutation layer, not here.
|
|
11
|
+
*/
|
|
12
|
+
const ACTION = {
|
|
13
|
+
create_file: "create",
|
|
14
|
+
write_file: "overwrite",
|
|
15
|
+
edit_file: "edit",
|
|
16
|
+
delete_file: "delete",
|
|
17
|
+
revert_change: "revert"
|
|
18
|
+
};
|
|
19
|
+
const RISK = { low: "low risk", medium: "medium risk", high: "high risk" };
|
|
20
|
+
export function riskOf(plan) {
|
|
21
|
+
if (plan.name === "delete_file")
|
|
22
|
+
return "high";
|
|
23
|
+
if (plan.name === "create_file")
|
|
24
|
+
return "low";
|
|
25
|
+
return "medium";
|
|
26
|
+
}
|
|
27
|
+
const safe = stripControl;
|
|
28
|
+
const INDENT = MARGIN + " ";
|
|
29
|
+
const inner = () => Math.max(24, contentWidth() - 4);
|
|
30
|
+
function planLines(plan, index, total) {
|
|
31
|
+
const risk = riskOf(plan);
|
|
32
|
+
const riskText = risk === "high" ? c.err(RISK[risk]) : risk === "medium" ? c.warn(RISK[risk]) : c.dim(RISK[risk]);
|
|
33
|
+
const counter = total > 1 ? c.dim(`${index + 1}/${total} `) : "";
|
|
34
|
+
const out = [
|
|
35
|
+
INDENT +
|
|
36
|
+
counter +
|
|
37
|
+
c.file(clip(plan.rel, Math.max(16, inner() - 28))) +
|
|
38
|
+
c.dim(` +${plan.diff.added} −${plan.diff.removed} · `) +
|
|
39
|
+
riskText
|
|
40
|
+
];
|
|
41
|
+
// WHICH repository, above the path — a change can now target a repository
|
|
42
|
+
// that is not the active one, so the name is part of what is being approved
|
|
43
|
+
// rather than context the user is assumed to be holding.
|
|
44
|
+
if (plan.repositoryName) {
|
|
45
|
+
out.unshift(INDENT + c.dim("Repository ") + c.value(clip(plan.repositoryName, Math.max(12, inner() - 14))));
|
|
46
|
+
}
|
|
47
|
+
if (plan.reverts)
|
|
48
|
+
out.push(INDENT + c.dim(`undoes ${plan.reverts}`));
|
|
49
|
+
for (const [i, l] of wrapText(safe(plan.reason), inner() - 8).entries()) {
|
|
50
|
+
out.push(INDENT + c.dim(i === 0 ? "Why " : " ") + c.dim(l));
|
|
51
|
+
}
|
|
52
|
+
out.push("");
|
|
53
|
+
if (plan.name === "delete_file") {
|
|
54
|
+
out.push(INDENT + c.err(`The whole file (${plan.diff.removed} lines) will be removed.`));
|
|
55
|
+
}
|
|
56
|
+
for (const raw of plan.unified.split("\n")) {
|
|
57
|
+
const l = safe(raw);
|
|
58
|
+
if (l.startsWith("@@"))
|
|
59
|
+
out.push(INDENT + c.accent(l));
|
|
60
|
+
else if (l.startsWith("+++") || l.startsWith("---"))
|
|
61
|
+
continue; // the path is in the header
|
|
62
|
+
else if (l.startsWith("+"))
|
|
63
|
+
out.push(INDENT + c.ok(l));
|
|
64
|
+
else if (l.startsWith("-"))
|
|
65
|
+
out.push(INDENT + c.err(l));
|
|
66
|
+
else
|
|
67
|
+
out.push(INDENT + c.dim(l));
|
|
68
|
+
}
|
|
69
|
+
if (plan.diff.hunks.length === 0)
|
|
70
|
+
out.push(INDENT + c.dim("(no line changes)"));
|
|
71
|
+
out.push("");
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
export function renderGroup(plans) {
|
|
75
|
+
const verb = plans.length === 1 ? ACTION[plans[0].name] ?? "change" : "change";
|
|
76
|
+
const what = plans.length === 1 ? `${verb}:` : `change ${plans.length} files:`;
|
|
77
|
+
return [
|
|
78
|
+
"",
|
|
79
|
+
MARGIN + c.warn("◆") + " " + c.title(`Carrier OS wants to ${what}`),
|
|
80
|
+
"",
|
|
81
|
+
...plans.flatMap((p, i) => planLines(p, i, plans.length))
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
/** One line per applied or refused change, for after the decision. */
|
|
85
|
+
export function renderChangeSummary(lines) {
|
|
86
|
+
return lines.map((l) => MARGIN + (l.includes("✓") ? c.ok(l.trim()) : c.warn(l.trim())));
|
|
87
|
+
}
|
package/dist/ui/chat-renderer.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { line } from "../services/output.js";
|
|
2
2
|
import { sleep } from "../services/spinner.js";
|
|
3
3
|
import { c } from "./colors.js";
|
|
4
|
+
import { contentWidth } from "./composer-layout.js";
|
|
4
5
|
import { renderMarkdown } from "./markdown.js";
|
|
5
6
|
import { renderUserMessage } from "./message.js";
|
|
6
7
|
import { thinking } from "./spinner.js";
|
|
@@ -34,9 +35,66 @@ const THINKING_PHRASES = [
|
|
|
34
35
|
];
|
|
35
36
|
let phraseIndex = 0;
|
|
36
37
|
let tipIndex = 0;
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Completion footers, rotated like the thinking phrases so a long session
|
|
40
|
+
* doesn't read as the same line stamped over and over.
|
|
41
|
+
*
|
|
42
|
+
* Each entry carries its own preposition — "Baked FOR 3s" but "Generated IN
|
|
43
|
+
* 3s" — because a shared connector makes half the list ungrammatical. "in"
|
|
44
|
+
* suits verbs about producing a result, "for" verbs about time spent.
|
|
45
|
+
*/
|
|
46
|
+
const COMPLETION_PHRASES = [
|
|
47
|
+
"Generated in",
|
|
48
|
+
"Brewed for",
|
|
49
|
+
"Baked for",
|
|
50
|
+
"Churned for",
|
|
51
|
+
"Simmered for",
|
|
52
|
+
"Percolated for",
|
|
53
|
+
"Distilled in",
|
|
54
|
+
"Composed in",
|
|
55
|
+
"Crafted in",
|
|
56
|
+
"Forged in",
|
|
57
|
+
"Assembled in",
|
|
58
|
+
"Synthesized in"
|
|
59
|
+
];
|
|
60
|
+
let completionIndex = 0;
|
|
61
|
+
/** Completion footer text, Claude-style: "Brewed for 1.2s". Advances the
|
|
62
|
+
* rotation, so call it once per finished turn. */
|
|
63
|
+
function completionLabel(seconds, spend) {
|
|
64
|
+
const phrase = COMPLETION_PHRASES[completionIndex % COMPLETION_PHRASES.length];
|
|
65
|
+
completionIndex++;
|
|
66
|
+
return `${phrase} ${Math.max(0.1, seconds).toFixed(1)}s${spendSuffix(spend)}`;
|
|
67
|
+
}
|
|
68
|
+
/** Format a number with thousands separators, trimming pointless decimals. */
|
|
69
|
+
const num = (n, dp = 0) => n.toLocaleString("en-US", { minimumFractionDigits: dp, maximumFractionDigits: dp });
|
|
70
|
+
/**
|
|
71
|
+
* " · 412.6 credits left"
|
|
72
|
+
*
|
|
73
|
+
* Balance only. Tokens and dollar cost are still returned by the API (and used
|
|
74
|
+
* for request logging), but three numbers on a completion line is a bill, not a
|
|
75
|
+
* status — the one that answers "can I keep going?" is what's left.
|
|
76
|
+
*
|
|
77
|
+
* Omitted entirely when the API didn't send it, rather than shown as 0: an
|
|
78
|
+
* older API build has no balance field, and a wrong number is worse than none
|
|
79
|
+
* when it's money.
|
|
80
|
+
*/
|
|
81
|
+
export function spendSuffix(spend) {
|
|
82
|
+
// An unlimited account's balance is a running total of real spend, not a
|
|
83
|
+
// limit — so "3,600 credits overdrawn" is true and alarming and answers the
|
|
84
|
+
// wrong question. What such an account wants to know is what THIS session
|
|
85
|
+
// cost. The balance itself is unchanged and still shown by /usage.
|
|
86
|
+
if (spend?.unlimited === true) {
|
|
87
|
+
return typeof spend.sessionSpent === "number"
|
|
88
|
+
? ` · Unlimited · session ${num(spend.sessionSpent, 2)} credits`
|
|
89
|
+
: " · Unlimited";
|
|
90
|
+
}
|
|
91
|
+
const credits = spend?.creditsRemaining;
|
|
92
|
+
if (typeof credits !== "number")
|
|
93
|
+
return "";
|
|
94
|
+
// A negative balance is an overdraft, not "-556 left" — say what it is.
|
|
95
|
+
return credits < 0
|
|
96
|
+
? ` · ${num(Math.abs(credits), 1)} credits overdrawn`
|
|
97
|
+
: ` · ${num(credits, 1)} credits left`;
|
|
40
98
|
}
|
|
41
99
|
// Rotating tips shown beneath the spinner while Carrier is responding — a chance
|
|
42
100
|
// to surface a handy shortcut, the way Claude Code does.
|
|
@@ -102,7 +160,10 @@ export function startThinkingLive(live) {
|
|
|
102
160
|
// SSE), so we surface it progressively — word by word at a natural cadence — so
|
|
103
161
|
// it reads like Claude typing the response in, rather than one instant block.
|
|
104
162
|
const STREAM_DELAY = 22; // ms between words
|
|
105
|
-
|
|
163
|
+
// SGR colour runs plus OSC strings (\x1b] … BEL / ST). OSC carries a payload —
|
|
164
|
+
// a hyperlink's URL, a title — that occupies no columns, so a regex that only
|
|
165
|
+
// knows SGR silently over-counts the width of any line containing one.
|
|
166
|
+
const ANSI = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b\[[0-9;]*m/g;
|
|
106
167
|
const stripAnsi = (s) => s.replace(ANSI, "");
|
|
107
168
|
/** Styled prefix of `s` showing its first `n` visible characters (ANSI kept). */
|
|
108
169
|
function ansiSlice(s, n) {
|
|
@@ -143,20 +204,22 @@ function wordStops(styled) {
|
|
|
143
204
|
return stops;
|
|
144
205
|
}
|
|
145
206
|
const CURSOR = "▋"; // streaming caret shown at the end of the in-progress line
|
|
207
|
+
/** Claude-style lead-in for an assistant reply: a soft theme-light dot before the
|
|
208
|
+
* first line, with continuation lines indented to align under the text. */
|
|
209
|
+
const LEAD = (i) => (i === 0 ? c.accentLight("●") + " " : " ");
|
|
146
210
|
/** Stream the answer above a PINNED input bar via a scroll region (flicker-free):
|
|
147
|
-
* a
|
|
148
|
-
*
|
|
211
|
+
* led by a neutral dot, each line revealed word-by-word on the bottom content
|
|
212
|
+
* row (with the streaming cursor) before scrolling up to the next. */
|
|
149
213
|
export async function streamAssistantSticky(sticky, text) {
|
|
150
214
|
const md = renderMarkdown(text);
|
|
151
215
|
while (md.length && md[0].trim() === "")
|
|
152
216
|
md.shift();
|
|
153
217
|
while (md.length && md[md.length - 1].trim() === "")
|
|
154
218
|
md.pop();
|
|
155
|
-
|
|
156
|
-
sticky.setLine(MARGIN + c.brand.bold("Carrier")); // response header, once
|
|
157
|
-
for (const raw of md) {
|
|
219
|
+
for (let i = 0; i < md.length; i++) {
|
|
158
220
|
sticky.scroll();
|
|
159
|
-
const
|
|
221
|
+
const raw = md[i];
|
|
222
|
+
const full = MARGIN + LEAD(i) + raw;
|
|
160
223
|
if (raw.trim()) {
|
|
161
224
|
for (const n of wordStops(full)) {
|
|
162
225
|
sticky.setLine(ansiSlice(full, n) + c.dim(CURSOR));
|
|
@@ -167,25 +230,25 @@ export async function streamAssistantSticky(sticky, text) {
|
|
|
167
230
|
}
|
|
168
231
|
sticky.scroll(); // trailing blank
|
|
169
232
|
}
|
|
170
|
-
/** Completion footer on the scroll region: ●
|
|
233
|
+
/** Completion footer on the scroll region: ● Brewed for 1.2s */
|
|
171
234
|
export function renderCompleteSticky(sticky, seconds) {
|
|
172
235
|
sticky.scroll();
|
|
173
236
|
sticky.setLine(MARGIN + c.dim(`● ${completionLabel(seconds)}`));
|
|
174
237
|
sticky.scroll();
|
|
175
238
|
}
|
|
176
|
-
/** Stream the assistant's answer above the pinned bar, led
|
|
177
|
-
*
|
|
178
|
-
* the transient status region (so the cursor can trail it) and is
|
|
179
|
-
* permanent once fully revealed. */
|
|
239
|
+
/** Stream the assistant's answer above the pinned bar, led by a neutral dot
|
|
240
|
+
* (Claude-style), word by word with a streaming cursor. Each in-progress line
|
|
241
|
+
* lives in the transient status region (so the cursor can trail it) and is
|
|
242
|
+
* promoted to permanent once fully revealed. */
|
|
180
243
|
export async function streamAssistantLive(live, text) {
|
|
181
244
|
const md = renderMarkdown(text);
|
|
182
245
|
while (md.length && md[0].trim() === "")
|
|
183
246
|
md.shift();
|
|
184
247
|
while (md.length && md[md.length - 1].trim() === "")
|
|
185
248
|
md.pop();
|
|
186
|
-
live.commit([MARGIN + c.brand.bold("Carrier")]); // response header, once
|
|
187
249
|
for (let i = 0; i < md.length; i++) {
|
|
188
|
-
|
|
250
|
+
// No name header — the reply is led by a neutral dot (Claude-style).
|
|
251
|
+
const full = MARGIN + LEAD(i) + md[i];
|
|
189
252
|
if (md[i].trim()) {
|
|
190
253
|
for (const n of wordStops(full)) {
|
|
191
254
|
live.setStatus([ansiSlice(full, n) + c.dim(CURSOR)]);
|
|
@@ -197,9 +260,9 @@ export async function streamAssistantLive(live, text) {
|
|
|
197
260
|
}
|
|
198
261
|
live.commit([""]); // single trailing blank
|
|
199
262
|
}
|
|
200
|
-
/** Dim completion footer committed above the pinned bar: ●
|
|
201
|
-
export function renderCompleteLive(live, seconds) {
|
|
202
|
-
live.commit([MARGIN + c.dim(`● ${completionLabel(seconds)}`), ""]);
|
|
263
|
+
/** Dim completion footer committed above the pinned bar: ● Brewed for 1.2s */
|
|
264
|
+
export function renderCompleteLive(live, seconds, spend) {
|
|
265
|
+
live.commit([MARGIN + c.dim(`● ${completionLabel(seconds, spend)}`), ""]);
|
|
203
266
|
}
|
|
204
267
|
/** Error block committed above the pinned bar. */
|
|
205
268
|
export function renderErrorLive(live, title, detail, hintCmd) {
|
|
@@ -209,8 +272,8 @@ export function renderErrorLive(live, title, detail, hintCmd) {
|
|
|
209
272
|
out.push("");
|
|
210
273
|
live.commit(out);
|
|
211
274
|
}
|
|
212
|
-
/** Stream the assistant's answer led
|
|
213
|
-
* with a streaming cursor in a TTY (in-place line rewrite); plain when piped. */
|
|
275
|
+
/** Stream the assistant's answer led by a neutral dot (Claude-style), word by
|
|
276
|
+
* word with a streaming cursor in a TTY (in-place line rewrite); plain when piped. */
|
|
214
277
|
export async function streamAssistant(text) {
|
|
215
278
|
const md = renderMarkdown(text);
|
|
216
279
|
while (md.length && md[0].trim() === "")
|
|
@@ -220,9 +283,8 @@ export async function streamAssistant(text) {
|
|
|
220
283
|
const tty = process.stdout.isTTY;
|
|
221
284
|
if (tty)
|
|
222
285
|
process.stdout.write("\x1b[?25l"); // hide the caret; ▋ is the cursor
|
|
223
|
-
line(MARGIN + c.brand.bold("Carrier"));
|
|
224
286
|
for (let i = 0; i < md.length; i++) {
|
|
225
|
-
const full = MARGIN + md[i];
|
|
287
|
+
const full = MARGIN + LEAD(i) + md[i];
|
|
226
288
|
if (tty && md[i].trim()) {
|
|
227
289
|
for (const n of wordStops(full)) {
|
|
228
290
|
process.stdout.write("\r\x1b[K" + ansiSlice(full, n) + c.dim(CURSOR));
|
|
@@ -238,15 +300,147 @@ export async function streamAssistant(text) {
|
|
|
238
300
|
process.stdout.write("\x1b[?25h");
|
|
239
301
|
line(); // single trailing blank
|
|
240
302
|
}
|
|
241
|
-
/** Dim completion footer: ●
|
|
242
|
-
export function renderComplete(seconds) {
|
|
243
|
-
line(MARGIN + c.dim(`● ${completionLabel(seconds)}`));
|
|
303
|
+
/** Dim completion footer: ● Brewed for 1.2s */
|
|
304
|
+
export function renderComplete(seconds, spend) {
|
|
305
|
+
line(MARGIN + c.dim(`● ${completionLabel(seconds, spend)}`));
|
|
306
|
+
line();
|
|
307
|
+
}
|
|
308
|
+
// ── Web-search citations ─────────────────────────────────────────────────────
|
|
309
|
+
// The web app renders these as hoverable chips. A terminal can't hover, so the
|
|
310
|
+
// list is numbered and the numbers are addressable: /open 2, /sources.
|
|
311
|
+
//
|
|
312
|
+
// Publisher leads, not the URL. Raw article URLs are long, visually noisy, and
|
|
313
|
+
// (for Google News results) not even human-readable — so the domain is shown
|
|
314
|
+
// bare and the full URL is attached as a terminal hyperlink where that works.
|
|
315
|
+
/** Citations shown inline under an answer before collapsing into a count. */
|
|
316
|
+
const INLINE_SOURCES = 4;
|
|
317
|
+
/** Indent under the "[n] " marker that title/domain lines sit at. */
|
|
318
|
+
const CITE_INDENT = " ";
|
|
319
|
+
/**
|
|
320
|
+
* Clip to the composer's content column.
|
|
321
|
+
*
|
|
322
|
+
* The screen engine writes one transcript line per terminal row and does no
|
|
323
|
+
* wrap accounting, so a line wider than the column steals a second row and
|
|
324
|
+
* shifts everything below it — which is how a URL tail ends up floating above
|
|
325
|
+
* the prompt. Titles and URLs are the only unbounded strings in the transcript,
|
|
326
|
+
* so they get clipped here. Plain text in, styled after, so this never has to
|
|
327
|
+
* count escape sequences.
|
|
328
|
+
*/
|
|
329
|
+
function fit(text, indent = CITE_INDENT.length) {
|
|
330
|
+
const max = Math.max(8, contentWidth() - indent);
|
|
331
|
+
return text.length <= max ? text : text.slice(0, max - 1) + "…";
|
|
332
|
+
}
|
|
333
|
+
/** Bare hostname for a citation — "bbc.com", never "www.bbc.com/path?utm=…". */
|
|
334
|
+
export function sourceDomain(s) {
|
|
335
|
+
try {
|
|
336
|
+
return new URL(s.url).hostname.replace(/^www\./, "");
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
return s.url;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/** Publisher name for a citation — the API's `source`, else the domain. */
|
|
343
|
+
export function sourceLabel(s) {
|
|
344
|
+
return s.source?.trim() || sourceDomain(s);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* One citation: publisher, then title, then domain — publisher first because
|
|
348
|
+
* that is what tells you whether to trust the line. `tail` carries the domain
|
|
349
|
+
* inline; the full list overrides it with the complete URL.
|
|
350
|
+
*/
|
|
351
|
+
function citation(s, i, tail) {
|
|
352
|
+
const marker = `[${i + 1}] `;
|
|
353
|
+
const head = `${MARGIN}${c.dim(`[${i + 1}]`)} ${c.value(fit(sourceLabel(s), marker.length))}`;
|
|
354
|
+
const tailLine = `${MARGIN}${CITE_INDENT}${c.dim(fit(tail))}`;
|
|
355
|
+
const title = s.title?.trim();
|
|
356
|
+
if (!title)
|
|
357
|
+
return [head, tailLine];
|
|
358
|
+
return [head, `${MARGIN}${CITE_INDENT}${c.dim(fit(title))}`, tailLine];
|
|
359
|
+
}
|
|
360
|
+
/** The compact block printed under an answer: first few, then a pointer. */
|
|
361
|
+
function sourceLines(sources) {
|
|
362
|
+
if (sources.length === 0)
|
|
363
|
+
return [];
|
|
364
|
+
const shown = sources.slice(0, INLINE_SOURCES);
|
|
365
|
+
const out = ["", MARGIN + c.dim(`Sources · ${sources.length}`), ""];
|
|
366
|
+
shown.forEach((s, i) => {
|
|
367
|
+
out.push(...citation(s, i, sourceDomain(s)));
|
|
368
|
+
out.push(""); // one blank between entries so the block scans as a list
|
|
369
|
+
});
|
|
370
|
+
const hidden = sources.length - shown.length;
|
|
371
|
+
if (hidden > 0)
|
|
372
|
+
out.push(`${MARGIN}${c.dim(`+ ${hidden} more · `)}${c.cmd("/sources")}`, "");
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
/** Numbered citation list printed under the answer. No-op without sources. */
|
|
376
|
+
export function renderSources(sources) {
|
|
377
|
+
for (const l of sourceLines(sources))
|
|
378
|
+
line(l);
|
|
379
|
+
}
|
|
380
|
+
/** Citation list committed above the pinned composer bar. */
|
|
381
|
+
export function renderSourcesLive(live, sources) {
|
|
382
|
+
const out = sourceLines(sources);
|
|
383
|
+
if (out.length)
|
|
384
|
+
live.commit(out);
|
|
385
|
+
}
|
|
386
|
+
/** The full list, every entry — `/sources`. Printed outside a turn. */
|
|
387
|
+
export function renderAllSources(sources, agoLabel) {
|
|
244
388
|
line();
|
|
389
|
+
line(MARGIN + c.title(`Sources · ${sources.length}`) + (agoLabel ? c.dim(` ${agoLabel}`) : ""));
|
|
390
|
+
line();
|
|
391
|
+
// The full list is where someone goes to copy a link, so the tail is the
|
|
392
|
+
// complete URL here even though the compact block deliberately hides it.
|
|
393
|
+
sources.forEach((s, i) => {
|
|
394
|
+
for (const l of citation(s, i, s.url))
|
|
395
|
+
line(l);
|
|
396
|
+
line();
|
|
397
|
+
});
|
|
398
|
+
line(MARGIN + c.dim("Open one with ") + c.cmd("/open <n>"));
|
|
399
|
+
line();
|
|
400
|
+
}
|
|
401
|
+
// ── Memory-engine markers ────────────────────────────────────────────────────
|
|
402
|
+
// The engine compresses a growing conversation on its own. The web draws two
|
|
403
|
+
// deliberately different marks and this mirrors that weighting: a snapshot is
|
|
404
|
+
// routine short-term housekeeping (muted, hairline rules), a restructure is a
|
|
405
|
+
// long-term rebuild that COSTS the account money (themed, heavier rule). Both
|
|
406
|
+
// carry the number you pass to `/memory <n>`.
|
|
407
|
+
function centred(inner, rule, char) {
|
|
408
|
+
const width = Math.max(20, contentWidth() - MARGIN.length);
|
|
409
|
+
const visible = inner.replace(ANSI, "").length;
|
|
410
|
+
const side = Math.max(2, Math.floor((width - visible - 2) / 2));
|
|
411
|
+
const bar = char.repeat(side);
|
|
412
|
+
return `${MARGIN}${rule(bar)} ${inner} ${rule(bar)}`;
|
|
413
|
+
}
|
|
414
|
+
/** `──── ⛁ Memory snapshot 3 · 229 tok ────` — understated, like the web. */
|
|
415
|
+
export function memoryMarkerLine(m) {
|
|
416
|
+
const n = m.number ?? "?";
|
|
417
|
+
const tok = m.tokens ? c.dim(` · ${m.tokens.toLocaleString()} tok`) : "";
|
|
418
|
+
const ref = m.index ? c.dim(` [${m.index}]`) : "";
|
|
419
|
+
if (m.failed) {
|
|
420
|
+
const label = c.warn(`! ${m.kind === "snapshot" ? "Snapshot" : "Restructure"} ${n} failed`);
|
|
421
|
+
return centred(label + ref, c.dim, "─");
|
|
422
|
+
}
|
|
423
|
+
if (m.kind === "restructure") {
|
|
424
|
+
// Themed and heavier — this one was billed.
|
|
425
|
+
return centred(c.brand(`⟐ Chat restructured v${n}`) + tok + ref, c.brand, "─");
|
|
426
|
+
}
|
|
427
|
+
return centred(c.dim(`⛁ Memory snapshot ${n}`) + tok + ref, c.dim, "─");
|
|
428
|
+
}
|
|
429
|
+
/** Completion footer for a turn that searched: ✓ Web search · 5 sources · 3.2s */
|
|
430
|
+
export function searchCompletionLine(sourceCount, seconds) {
|
|
431
|
+
const secs = seconds < 10 ? seconds.toFixed(1) : Math.round(seconds).toString();
|
|
432
|
+
const plural = sourceCount === 1 ? "source" : "sources";
|
|
433
|
+
return MARGIN + c.ok("✓") + c.dim(` Web search · ${sourceCount} ${plural} · ${secs}s`);
|
|
245
434
|
}
|
|
246
435
|
/** A premium error block: × Title / detail / hint. */
|
|
247
|
-
export function renderError(title, detail, hintCmd
|
|
436
|
+
export function renderError(title, detail, hintCmd,
|
|
437
|
+
/** "warn" for states the user can act on (out of credits) rather than
|
|
438
|
+
* failures — red reads as "something broke", which this isn't. */
|
|
439
|
+
tone = "error") {
|
|
440
|
+
const mark = tone === "warn" ? c.warn("⚠") : c.err("×");
|
|
441
|
+
const heading = tone === "warn" ? c.warn.bold(title) : c.err.bold(title);
|
|
248
442
|
line();
|
|
249
|
-
line(` ${
|
|
443
|
+
line(` ${mark} ${heading}`);
|
|
250
444
|
line();
|
|
251
445
|
line(" " + c.value(detail));
|
|
252
446
|
if (hintCmd) {
|