@carrieros/cli 0.1.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 +305 -0
- package/dist/commands/chat.js +93 -0
- package/dist/commands/doctor.js +54 -0
- package/dist/commands/files.js +33 -0
- package/dist/commands/help.js +35 -0
- package/dist/commands/history.js +47 -0
- package/dist/commands/login.js +59 -0
- package/dist/commands/logout.js +15 -0
- package/dist/commands/mode-model.js +28 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +27 -0
- package/dist/commands/sessions.js +123 -0
- package/dist/commands/upload.js +50 -0
- package/dist/commands/usage.js +31 -0
- package/dist/commands/version.js +28 -0
- package/dist/commands/whoami.js +40 -0
- package/dist/main.js +94 -0
- package/dist/services/api.js +115 -0
- package/dist/services/config.js +72 -0
- package/dist/services/context.js +42 -0
- package/dist/services/login-flow.js +15 -0
- package/dist/services/meta.js +13 -0
- package/dist/services/output.js +54 -0
- package/dist/services/spinner.js +18 -0
- package/dist/shell/shell-chat.js +280 -0
- package/dist/shell/shell-commands.js +168 -0
- package/dist/shell/shell-prompt.js +48 -0
- package/dist/shell/shell-renderer.js +70 -0
- package/dist/shell/shell-router.js +21 -0
- package/dist/shell/shell.js +139 -0
- package/dist/types/index.js +4 -0
- package/dist/ui/activity.js +77 -0
- package/dist/ui/banner.js +70 -0
- package/dist/ui/chat-renderer.js +269 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +79 -0
- package/dist/ui/colors.js +47 -0
- package/dist/ui/command-filter.js +57 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +306 -0
- package/dist/ui/command-registry.js +47 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/composer-layout.js +27 -0
- package/dist/ui/live.js +124 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +151 -0
- package/dist/ui/message.js +38 -0
- package/dist/ui/select.js +114 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/theme.js +63 -0
- package/dist/ui/ui.js +75 -0
- package/package.json +64 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { password } from "@inquirer/prompts";
|
|
2
|
+
import { ApiError } from "../services/api.js";
|
|
3
|
+
import { DEFAULT_API_URL } from "../services/config.js";
|
|
4
|
+
import { verifyAndSaveKey } from "../services/login-flow.js";
|
|
5
|
+
import { errorLine, info, line, warn } from "../services/output.js";
|
|
6
|
+
import { withSpinner } from "../services/spinner.js";
|
|
7
|
+
import { renderWelcomeBanner } from "../ui/banner.js";
|
|
8
|
+
import { renderExit } from "../ui/chat-renderer.js";
|
|
9
|
+
import { c } from "../ui/colors.js";
|
|
10
|
+
import { readCommand } from "../ui/command-palette.js";
|
|
11
|
+
import { loadModels } from "./shell-chat.js";
|
|
12
|
+
import { CARET, buildFooter } from "./shell-prompt.js";
|
|
13
|
+
import { dispatchLine } from "./shell-router.js";
|
|
14
|
+
import { printAuthSuccess, printWelcome } from "./shell-renderer.js";
|
|
15
|
+
/**
|
|
16
|
+
* The interactive Carrier OS shell — the default experience when you run
|
|
17
|
+
* `cos` with no command. It sits on top of the existing commands: chat is
|
|
18
|
+
* handled by the shared chat turn, and every other slash command is routed
|
|
19
|
+
* through the same Commander program, so no logic is duplicated.
|
|
20
|
+
*/
|
|
21
|
+
export async function runShell(ctx, program) {
|
|
22
|
+
// Returning users see the branded banner once; a fresh login already showed
|
|
23
|
+
// the branding + scope in the welcome / auth-success screens, so we don't
|
|
24
|
+
// repeat it after logging in.
|
|
25
|
+
const returning = ctx.config.isAuthenticated;
|
|
26
|
+
if (!returning) {
|
|
27
|
+
const ok = await firstRunLogin(ctx);
|
|
28
|
+
if (!ok)
|
|
29
|
+
return; // user chose /exit or Ctrl+C
|
|
30
|
+
}
|
|
31
|
+
const client = ctx.requireAuth();
|
|
32
|
+
// Preload the model list so the prompt/banner can show friendly labels.
|
|
33
|
+
await loadModels(client).catch(() => []);
|
|
34
|
+
if (returning) {
|
|
35
|
+
// Branded startup banner (rendered once). Notices can be passed here later —
|
|
36
|
+
// e.g. renderWelcomeBanner(ctx, { notices: ["Update available: v0.2.0"] }).
|
|
37
|
+
renderWelcomeBanner(ctx);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Just logged in — go straight to the shell with a slim hint (the prompt
|
|
41
|
+
// already shows workspace / project / mode / model).
|
|
42
|
+
line();
|
|
43
|
+
line(" " + c.dim("Type ") + c.key("/help") + c.dim(" for available commands."));
|
|
44
|
+
}
|
|
45
|
+
line();
|
|
46
|
+
// Interactive loop driven by the composer/reader. Each iteration reads one
|
|
47
|
+
// line (with the slash palette overlay, the dim status footer, and a
|
|
48
|
+
// placeholder inside the box), then dispatches it. Enter in the palette only
|
|
49
|
+
// inserts a command — a second Enter submits it — exactly like Claude Code.
|
|
50
|
+
const history = [];
|
|
51
|
+
let sigintArmed = false;
|
|
52
|
+
let conversationStarted = false; // only the first empty session shows the full welcome
|
|
53
|
+
// Leaving the shell: restore cooked mode + a visible cursor (the reader keeps
|
|
54
|
+
// raw mode on for the session), then print the farewell.
|
|
55
|
+
const leave = () => {
|
|
56
|
+
if (process.stdin.isTTY)
|
|
57
|
+
process.stdin.setRawMode(false);
|
|
58
|
+
process.stdout.write("\x1b[?25h");
|
|
59
|
+
renderExit(ctx.config.get("sessionId"));
|
|
60
|
+
};
|
|
61
|
+
for (;;) {
|
|
62
|
+
const placeholder = conversationStarted ? COMPACT_PLACEHOLDER : WELCOME_PLACEHOLDER;
|
|
63
|
+
// One Ctrl+C swaps the status footer for a dim "again to exit" hint beneath
|
|
64
|
+
// the bar (no separate info line); a second Ctrl+C quits.
|
|
65
|
+
const footer = sigintArmed ? " " + c.dim("Press Ctrl-C again to exit") : buildFooter(ctx);
|
|
66
|
+
const res = await readCommand(CARET, history, footer, placeholder);
|
|
67
|
+
if (res.type === "exit") {
|
|
68
|
+
leave();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (res.type === "sigint") {
|
|
72
|
+
if (sigintArmed) {
|
|
73
|
+
leave();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
sigintArmed = true;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
sigintArmed = false;
|
|
80
|
+
let message = res.value.trim();
|
|
81
|
+
if (!message)
|
|
82
|
+
continue;
|
|
83
|
+
history.push(message);
|
|
84
|
+
// /new is a friendly alias for /clear. A cleared/new session drops back to
|
|
85
|
+
// the welcome placeholder; sending a real message starts the conversation
|
|
86
|
+
// and switches to the compact placeholder.
|
|
87
|
+
if (message === "/new")
|
|
88
|
+
message = "/clear";
|
|
89
|
+
if (message === "/clear")
|
|
90
|
+
conversationStarted = false;
|
|
91
|
+
else if (!message.startsWith("/"))
|
|
92
|
+
conversationStarted = true;
|
|
93
|
+
const { exit } = await dispatchLine(ctx, program, client, message);
|
|
94
|
+
if (exit) {
|
|
95
|
+
leave();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** First empty session — the welcome placeholder doubles as the input area. */
|
|
101
|
+
const WELCOME_PLACEHOLDER = "What can I help you build today?";
|
|
102
|
+
/** Once the conversation is underway — a compact, unobtrusive prompt. */
|
|
103
|
+
const COMPACT_PLACEHOLDER = "Ask Carrier OS…";
|
|
104
|
+
/** Prompt for and verify a secret key until it succeeds or the user exits. */
|
|
105
|
+
async function firstRunLogin(ctx) {
|
|
106
|
+
printWelcome();
|
|
107
|
+
const apiUrl = ctx.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
108
|
+
for (;;) {
|
|
109
|
+
let key;
|
|
110
|
+
try {
|
|
111
|
+
key = (await password({ message: "carrier_sk_…", mask: "•" })).trim();
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false; // Ctrl+C at the prompt
|
|
115
|
+
}
|
|
116
|
+
if (key === "/exit" || key === "/quit")
|
|
117
|
+
return false;
|
|
118
|
+
if (!key)
|
|
119
|
+
continue;
|
|
120
|
+
try {
|
|
121
|
+
const scope = await withSpinner("Verifying key…", () => verifyAndSaveKey(ctx, apiUrl, key));
|
|
122
|
+
printAuthSuccess(scope);
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
if (err instanceof ApiError) {
|
|
127
|
+
errorLine(`${err.message} (${err.code})`);
|
|
128
|
+
if (err.code === "NETWORK_ERROR" || err.code.startsWith("HTTP_")) {
|
|
129
|
+
warn(`Endpoint: ${apiUrl}`);
|
|
130
|
+
info("Set a different endpoint with: cos login --url http://localhost:5000/v1");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
errorLine(err.message);
|
|
135
|
+
}
|
|
136
|
+
// Stay in the loop and let the user try again.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { c } from "./colors.js";
|
|
2
|
+
import { MARGIN } from "./composer-layout.js";
|
|
3
|
+
/**
|
|
4
|
+
* A live activity timeline — the "thinking" phase, Claude-style but TRUTHFUL to
|
|
5
|
+
* the Carrier OS architecture. The CLI only talks to the public API, so steps
|
|
6
|
+
* reflect what actually happens (understand the request → select the Carrier
|
|
7
|
+
* mode → route to the API → wait for the model), never fabricated file/tool
|
|
8
|
+
* actions.
|
|
9
|
+
*
|
|
10
|
+
* It renders into the composer's transient status region: completed steps dim to
|
|
11
|
+
* a ●, and only the current step spins. The shell drives it by emitting phase
|
|
12
|
+
* events (request_started → mode_selected → request_sent → waiting → done),
|
|
13
|
+
* which map 1:1 to `step()` calls — new backend capabilities (search, files,
|
|
14
|
+
* vision, agents…) plug in as extra steps without changing this renderer.
|
|
15
|
+
*/
|
|
16
|
+
const FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏";
|
|
17
|
+
export function startActivity(live) {
|
|
18
|
+
const steps = [];
|
|
19
|
+
let frame = 0;
|
|
20
|
+
const paint = () => {
|
|
21
|
+
live.setStatus(steps.map((s, i) => {
|
|
22
|
+
const active = !s.done && i === steps.length - 1;
|
|
23
|
+
const marker = s.done
|
|
24
|
+
? c.dim("●")
|
|
25
|
+
: active
|
|
26
|
+
? c.brand(FRAMES[frame % FRAMES.length])
|
|
27
|
+
: c.dim("○");
|
|
28
|
+
return MARGIN + marker + " " + c.dim(active ? `${s.label}…` : s.label);
|
|
29
|
+
}));
|
|
30
|
+
};
|
|
31
|
+
const timer = setInterval(() => {
|
|
32
|
+
frame++;
|
|
33
|
+
paint();
|
|
34
|
+
}, 90);
|
|
35
|
+
timer.unref?.();
|
|
36
|
+
return {
|
|
37
|
+
step(label) {
|
|
38
|
+
if (steps.length)
|
|
39
|
+
steps[steps.length - 1].done = true;
|
|
40
|
+
steps.push({ label, done: false });
|
|
41
|
+
paint();
|
|
42
|
+
},
|
|
43
|
+
done() {
|
|
44
|
+
for (const s of steps)
|
|
45
|
+
s.done = true;
|
|
46
|
+
paint();
|
|
47
|
+
},
|
|
48
|
+
stop() {
|
|
49
|
+
clearInterval(timer);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Truthful activity steps for a chat turn, flavored by the Carrier mode. The
|
|
55
|
+
* FIRST steps are brief client-side phases; the LAST step is the one that spins
|
|
56
|
+
* while genuinely awaiting the model's response.
|
|
57
|
+
*/
|
|
58
|
+
export function chatActivitySteps(modeLabel) {
|
|
59
|
+
const lower = modeLabel.toLowerCase();
|
|
60
|
+
if (lower === "code" || lower === "debug") {
|
|
61
|
+
return ["Detecting coding task", `Selecting ${modeLabel} mode`, "Routing to Carrier OS", "Generating solution"];
|
|
62
|
+
}
|
|
63
|
+
if (lower === "research") {
|
|
64
|
+
return [
|
|
65
|
+
"Understanding your request",
|
|
66
|
+
`Selecting ${modeLabel} mode`,
|
|
67
|
+
"Routing to Carrier OS",
|
|
68
|
+
"Synthesizing response"
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
return [
|
|
72
|
+
"Understanding your request",
|
|
73
|
+
`Selecting ${modeLabel} mode`,
|
|
74
|
+
"Routing to Carrier OS",
|
|
75
|
+
"Waiting for the model"
|
|
76
|
+
];
|
|
77
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { capitalize } from "../services/output.js";
|
|
3
|
+
import { modelLabelShort } from "../shell/shell-chat.js";
|
|
4
|
+
import { c } from "./colors.js";
|
|
5
|
+
import { WORDMARK } from "./logo.js";
|
|
6
|
+
import { accentHex } from "./theme.js";
|
|
7
|
+
const MARGIN = " ";
|
|
8
|
+
const LABEL_WIDTH = 13;
|
|
9
|
+
/** Visible width of a block of lines. */
|
|
10
|
+
function blockWidth(block) {
|
|
11
|
+
return Math.max(...block.split("\n").map((l) => [...l].length));
|
|
12
|
+
}
|
|
13
|
+
/** True when the terminal is too narrow for the block wordmark. */
|
|
14
|
+
function isNarrow() {
|
|
15
|
+
return (process.stdout.columns ?? 80) < 2 + blockWidth(WORDMARK) + 2;
|
|
16
|
+
}
|
|
17
|
+
// ── Theme-color gradient across the wordmark (subtle; follows the accent) ──────
|
|
18
|
+
function hexToRgb(h) {
|
|
19
|
+
const n = parseInt(h.slice(1), 16);
|
|
20
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
21
|
+
}
|
|
22
|
+
function lerpHex(a, b, t) {
|
|
23
|
+
const [ar, ag, ab] = hexToRgb(a);
|
|
24
|
+
const [br, bg, bb] = hexToRgb(b);
|
|
25
|
+
const mix = (x, y) => Math.round(x + (y - x) * t);
|
|
26
|
+
return "#" + [mix(ar, br), mix(ag, bg), mix(ab, bb)].map((v) => v.toString(16).padStart(2, "0")).join("");
|
|
27
|
+
}
|
|
28
|
+
/** Color the wordmark with a gentle left→right gradient from the accent to a
|
|
29
|
+
* lighter tint of it (spaces left untouched). Re-reads the live accent. */
|
|
30
|
+
function gradientWordmark(lines) {
|
|
31
|
+
const from = accentHex();
|
|
32
|
+
const to = lerpHex(from, "#ffffff", 0.5); // lighten toward white on the right
|
|
33
|
+
const maxW = Math.max(1, ...lines.map((l) => [...l].length));
|
|
34
|
+
return lines.map((line) => [...line].map((ch, i) => (ch === " " ? ch : chalk.hex(lerpHex(from, to, i / maxW))(ch))).join(""));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The brand lockup lines (already colored): the CARRIER block wordmark with a
|
|
38
|
+
* subtle theme-color gradient. Shared by the startup banner and first-run
|
|
39
|
+
* welcome.
|
|
40
|
+
*/
|
|
41
|
+
export function renderBrandmark() {
|
|
42
|
+
if (isNarrow()) {
|
|
43
|
+
return [MARGIN + c.brand("Carrier OS") + " " + c.subtitle("CLI")];
|
|
44
|
+
}
|
|
45
|
+
return gradientWordmark(WORDMARK.split("\n")).map((l) => MARGIN + l);
|
|
46
|
+
}
|
|
47
|
+
function kvLine(label, value) {
|
|
48
|
+
return MARGIN + c.label(label.padEnd(LABEL_WIDTH)) + c.value(value);
|
|
49
|
+
}
|
|
50
|
+
export function renderWelcomeBanner(ctx, opts = {}) {
|
|
51
|
+
try {
|
|
52
|
+
const scope = ctx.config.get("scope");
|
|
53
|
+
const out = ["", ...renderBrandmark(), ""];
|
|
54
|
+
out.push(kvLine("Workspace", scope?.workspace.name ?? "—"));
|
|
55
|
+
out.push(kvLine("Project", scope?.project.name ?? "—"));
|
|
56
|
+
out.push(kvLine("Mode", capitalize(ctx.mode)));
|
|
57
|
+
out.push(kvLine("Model", modelLabelShort(ctx)));
|
|
58
|
+
out.push("");
|
|
59
|
+
for (const n of opts.notices ?? [])
|
|
60
|
+
out.push(MARGIN + c.accent("•") + " " + n);
|
|
61
|
+
if (opts.notices?.length)
|
|
62
|
+
out.push("");
|
|
63
|
+
out.push(MARGIN + c.hint("Type ") + c.key("/help") + c.hint(" for available commands."));
|
|
64
|
+
process.stdout.write(out.join("\n") + "\n");
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// The banner must never break startup — degrade to a bare header.
|
|
68
|
+
process.stdout.write(`\n ${c.wordmark("Carrier OS")} ${c.subtitle("CLI")}\n\n`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { line } from "../services/output.js";
|
|
2
|
+
import { sleep } from "../services/spinner.js";
|
|
3
|
+
import { c } from "./colors.js";
|
|
4
|
+
import { renderMarkdown } from "./markdown.js";
|
|
5
|
+
import { renderUserMessage } from "./message.js";
|
|
6
|
+
import { thinking } from "./spinner.js";
|
|
7
|
+
/**
|
|
8
|
+
* Presentation layer for the conversation, styled after Claude Code:
|
|
9
|
+
* ❯ <your message> (full-width bar — see message.ts)
|
|
10
|
+
* ● <assistant reply> (dot marker, continuation indented)
|
|
11
|
+
* ✳ Brewed for 3s (dim completion footer)
|
|
12
|
+
* The chat logic only emits events (renderUserMessage / startThinking /
|
|
13
|
+
* streamAssistant / renderComplete / renderError), keeping rendering separate.
|
|
14
|
+
*/
|
|
15
|
+
export { renderUserMessage };
|
|
16
|
+
// Thinking phrases that cycle while Carrier is responding — the same set the web
|
|
17
|
+
// chat animates through. Rotated so a different one leads each turn.
|
|
18
|
+
const THINKING_PHRASES = [
|
|
19
|
+
"Thinking",
|
|
20
|
+
"Processing",
|
|
21
|
+
"Analyzing",
|
|
22
|
+
"Computing",
|
|
23
|
+
"Reasoning",
|
|
24
|
+
"Synthesizing",
|
|
25
|
+
"Exploring",
|
|
26
|
+
"Connecting dots",
|
|
27
|
+
"Deep dive",
|
|
28
|
+
"Unraveling",
|
|
29
|
+
"Decoding",
|
|
30
|
+
"Pattern matching",
|
|
31
|
+
"Quantum thinking",
|
|
32
|
+
"Neural pathways",
|
|
33
|
+
"Emerging insights"
|
|
34
|
+
];
|
|
35
|
+
let phraseIndex = 0;
|
|
36
|
+
let tipIndex = 0;
|
|
37
|
+
/** Completion footer text, Claude-style: "Generated in 1.2s". */
|
|
38
|
+
function completionLabel(seconds) {
|
|
39
|
+
return `Generated in ${Math.max(0.1, seconds).toFixed(1)}s`;
|
|
40
|
+
}
|
|
41
|
+
// Rotating tips shown beneath the spinner while Carrier is responding — a chance
|
|
42
|
+
// to surface a handy shortcut, the way Claude Code does.
|
|
43
|
+
const TIPS = [
|
|
44
|
+
"Type / to browse commands.",
|
|
45
|
+
"Switch the model any time with /model.",
|
|
46
|
+
"Use /mode to change how Carrier responds.",
|
|
47
|
+
"Press ↑ to reuse a previous message.",
|
|
48
|
+
"Run /clear to start a fresh conversation.",
|
|
49
|
+
"Give Carrier context by attaching files with /upload.",
|
|
50
|
+
"Check your usage and limits with /usage.",
|
|
51
|
+
"Tab completes the highlighted command."
|
|
52
|
+
];
|
|
53
|
+
/** Start the thinking spinner: a phrase that cycles through THINKING_PHRASES
|
|
54
|
+
* every ~1.5s, with a rotating tip on the line beneath. Returns a stop handle. */
|
|
55
|
+
export function startThinking() {
|
|
56
|
+
const tip = TIPS[tipIndex % TIPS.length];
|
|
57
|
+
tipIndex++;
|
|
58
|
+
let idx = phraseIndex % THINKING_PHRASES.length;
|
|
59
|
+
phraseIndex++;
|
|
60
|
+
const label = (i) => c.dim(`${THINKING_PHRASES[i]}…`) + "\n " + c.dim(`└ Tip: ${tip}`);
|
|
61
|
+
const spinner = thinking(label(idx));
|
|
62
|
+
const timer = setInterval(() => {
|
|
63
|
+
idx = (idx + 1) % THINKING_PHRASES.length;
|
|
64
|
+
spinner.setText(label(idx));
|
|
65
|
+
}, 1500);
|
|
66
|
+
timer.unref?.(); // don't keep the process alive on the cycling timer
|
|
67
|
+
return {
|
|
68
|
+
stop: () => {
|
|
69
|
+
clearInterval(timer);
|
|
70
|
+
spinner.stop();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// ── Sticky-composer variants: same visuals, but rendered around a pinned input
|
|
75
|
+
// bar so the composer never disappears while a turn is processing ──────────
|
|
76
|
+
const FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏";
|
|
77
|
+
/** Cycle the thinking spinner + tip into the live region above the pinned bar.
|
|
78
|
+
* Returns a stop handle (the caller clears the region afterwards). */
|
|
79
|
+
export function startThinkingLive(live) {
|
|
80
|
+
const tip = TIPS[tipIndex % TIPS.length];
|
|
81
|
+
tipIndex++;
|
|
82
|
+
let idx = phraseIndex % THINKING_PHRASES.length;
|
|
83
|
+
phraseIndex++;
|
|
84
|
+
let frame = 0;
|
|
85
|
+
let ticks = 0;
|
|
86
|
+
const paint = () => live.setStatus([
|
|
87
|
+
" " + c.brand(FRAMES[frame % FRAMES.length]) + " " + c.dim(`${THINKING_PHRASES[idx]}…`),
|
|
88
|
+
" " + c.dim(`└ Tip: ${tip}`)
|
|
89
|
+
]);
|
|
90
|
+
paint();
|
|
91
|
+
const timer = setInterval(() => {
|
|
92
|
+
frame++;
|
|
93
|
+
ticks++;
|
|
94
|
+
if (ticks % 18 === 0)
|
|
95
|
+
idx = (idx + 1) % THINKING_PHRASES.length; // advance phrase ~1.5s
|
|
96
|
+
paint();
|
|
97
|
+
}, 80);
|
|
98
|
+
timer.unref?.();
|
|
99
|
+
return { stop: () => clearInterval(timer) };
|
|
100
|
+
}
|
|
101
|
+
// Word-streaming reveal. The API returns the full answer at once (no backend
|
|
102
|
+
// SSE), so we surface it progressively — word by word at a natural cadence — so
|
|
103
|
+
// it reads like Claude typing the response in, rather than one instant block.
|
|
104
|
+
const STREAM_DELAY = 22; // ms between words
|
|
105
|
+
const ANSI = /\x1b\[[0-9;]*m/g;
|
|
106
|
+
const stripAnsi = (s) => s.replace(ANSI, "");
|
|
107
|
+
/** Styled prefix of `s` showing its first `n` visible characters (ANSI kept). */
|
|
108
|
+
function ansiSlice(s, n) {
|
|
109
|
+
let out = "";
|
|
110
|
+
let vis = 0;
|
|
111
|
+
let i = 0;
|
|
112
|
+
while (i < s.length && vis < n) {
|
|
113
|
+
const m = s[i] === "\x1b" ? /^\x1b\[[0-9;]*m/.exec(s.slice(i)) : null;
|
|
114
|
+
if (m) {
|
|
115
|
+
out += m[0];
|
|
116
|
+
i += m[0].length;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
out += s[i];
|
|
120
|
+
vis++;
|
|
121
|
+
i++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
while (i < s.length && s[i] === "\x1b") {
|
|
125
|
+
const m = /^\x1b\[[0-9;]*m/.exec(s.slice(i));
|
|
126
|
+
if (!m)
|
|
127
|
+
break;
|
|
128
|
+
out += m[0];
|
|
129
|
+
i += m[0].length;
|
|
130
|
+
}
|
|
131
|
+
return out + "\x1b[0m";
|
|
132
|
+
}
|
|
133
|
+
/** Cumulative visible-char counts at each word boundary of a styled line. */
|
|
134
|
+
function wordStops(styled) {
|
|
135
|
+
const visible = stripAnsi(styled);
|
|
136
|
+
const stops = [];
|
|
137
|
+
const re = /\S+\s*/g;
|
|
138
|
+
let m;
|
|
139
|
+
while ((m = re.exec(visible)) !== null)
|
|
140
|
+
stops.push(m.index + m[0].length);
|
|
141
|
+
if (!stops.length || stops[stops.length - 1] < visible.length)
|
|
142
|
+
stops.push(visible.length);
|
|
143
|
+
return stops;
|
|
144
|
+
}
|
|
145
|
+
const CURSOR = "▋"; // streaming caret shown at the end of the in-progress line
|
|
146
|
+
/** Stream the answer above a PINNED input bar via a scroll region (flicker-free):
|
|
147
|
+
* a "Carrier" header once, then each line revealed word-by-word on the bottom
|
|
148
|
+
* content row (with the streaming cursor) before scrolling up to the next. */
|
|
149
|
+
export async function streamAssistantSticky(sticky, text) {
|
|
150
|
+
const md = renderMarkdown(text);
|
|
151
|
+
while (md.length && md[0].trim() === "")
|
|
152
|
+
md.shift();
|
|
153
|
+
while (md.length && md[md.length - 1].trim() === "")
|
|
154
|
+
md.pop();
|
|
155
|
+
sticky.scroll();
|
|
156
|
+
sticky.setLine(MARGIN + c.brand.bold("Carrier")); // response header, once
|
|
157
|
+
for (const raw of md) {
|
|
158
|
+
sticky.scroll();
|
|
159
|
+
const full = MARGIN + raw;
|
|
160
|
+
if (raw.trim()) {
|
|
161
|
+
for (const n of wordStops(full)) {
|
|
162
|
+
sticky.setLine(ansiSlice(full, n) + c.dim(CURSOR));
|
|
163
|
+
await sleep(STREAM_DELAY);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
sticky.setLine(full); // final line, cursor gone
|
|
167
|
+
}
|
|
168
|
+
sticky.scroll(); // trailing blank
|
|
169
|
+
}
|
|
170
|
+
/** Completion footer on the scroll region: ● Generated in 1.2s */
|
|
171
|
+
export function renderCompleteSticky(sticky, seconds) {
|
|
172
|
+
sticky.scroll();
|
|
173
|
+
sticky.setLine(MARGIN + c.dim(`● ${completionLabel(seconds)}`));
|
|
174
|
+
sticky.scroll();
|
|
175
|
+
}
|
|
176
|
+
/** Stream the assistant's answer above the pinned bar, led once by a "Carrier"
|
|
177
|
+
* header, word by word with a streaming cursor. Each in-progress line lives in
|
|
178
|
+
* the transient status region (so the cursor can trail it) and is promoted to
|
|
179
|
+
* permanent once fully revealed. */
|
|
180
|
+
export async function streamAssistantLive(live, text) {
|
|
181
|
+
const md = renderMarkdown(text);
|
|
182
|
+
while (md.length && md[0].trim() === "")
|
|
183
|
+
md.shift();
|
|
184
|
+
while (md.length && md[md.length - 1].trim() === "")
|
|
185
|
+
md.pop();
|
|
186
|
+
live.commit([MARGIN + c.brand.bold("Carrier")]); // response header, once
|
|
187
|
+
for (let i = 0; i < md.length; i++) {
|
|
188
|
+
const full = MARGIN + md[i];
|
|
189
|
+
if (md[i].trim()) {
|
|
190
|
+
for (const n of wordStops(full)) {
|
|
191
|
+
live.setStatus([ansiSlice(full, n) + c.dim(CURSOR)]);
|
|
192
|
+
await sleep(STREAM_DELAY);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
live.setStatus([full]); // final line, cursor gone
|
|
196
|
+
live.commitStatus(); // lock the finished line in place (no flicker)
|
|
197
|
+
}
|
|
198
|
+
live.commit([""]); // single trailing blank
|
|
199
|
+
}
|
|
200
|
+
/** Dim completion footer committed above the pinned bar: ● Generated in 1.2s */
|
|
201
|
+
export function renderCompleteLive(live, seconds) {
|
|
202
|
+
live.commit([MARGIN + c.dim(`● ${completionLabel(seconds)}`), ""]);
|
|
203
|
+
}
|
|
204
|
+
/** Error block committed above the pinned bar. */
|
|
205
|
+
export function renderErrorLive(live, title, detail, hintCmd) {
|
|
206
|
+
const out = ["", ` ${c.err("×")} ${c.err.bold(title)}`, "", " " + c.value(detail)];
|
|
207
|
+
if (hintCmd)
|
|
208
|
+
out.push("", " " + c.dim("Run ") + c.cmd(hintCmd) + c.dim(" to continue."));
|
|
209
|
+
out.push("");
|
|
210
|
+
live.commit(out);
|
|
211
|
+
}
|
|
212
|
+
/** Stream the assistant's answer led once by a "Carrier" header, word by word
|
|
213
|
+
* with a streaming cursor in a TTY (in-place line rewrite); plain when piped. */
|
|
214
|
+
export async function streamAssistant(text) {
|
|
215
|
+
const md = renderMarkdown(text);
|
|
216
|
+
while (md.length && md[0].trim() === "")
|
|
217
|
+
md.shift();
|
|
218
|
+
while (md.length && md[md.length - 1].trim() === "")
|
|
219
|
+
md.pop();
|
|
220
|
+
const tty = process.stdout.isTTY;
|
|
221
|
+
if (tty)
|
|
222
|
+
process.stdout.write("\x1b[?25l"); // hide the caret; ▋ is the cursor
|
|
223
|
+
line(MARGIN + c.brand.bold("Carrier"));
|
|
224
|
+
for (let i = 0; i < md.length; i++) {
|
|
225
|
+
const full = MARGIN + md[i];
|
|
226
|
+
if (tty && md[i].trim()) {
|
|
227
|
+
for (const n of wordStops(full)) {
|
|
228
|
+
process.stdout.write("\r\x1b[K" + ansiSlice(full, n) + c.dim(CURSOR));
|
|
229
|
+
await sleep(STREAM_DELAY);
|
|
230
|
+
}
|
|
231
|
+
process.stdout.write("\r\x1b[K" + full + "\n");
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
line(full);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (tty)
|
|
238
|
+
process.stdout.write("\x1b[?25h");
|
|
239
|
+
line(); // single trailing blank
|
|
240
|
+
}
|
|
241
|
+
/** Dim completion footer: ● Generated in 1.2s */
|
|
242
|
+
export function renderComplete(seconds) {
|
|
243
|
+
line(MARGIN + c.dim(`● ${completionLabel(seconds)}`));
|
|
244
|
+
line();
|
|
245
|
+
}
|
|
246
|
+
/** A premium error block: × Title / detail / hint. */
|
|
247
|
+
export function renderError(title, detail, hintCmd) {
|
|
248
|
+
line();
|
|
249
|
+
line(` ${c.err("×")} ${c.err.bold(title)}`);
|
|
250
|
+
line();
|
|
251
|
+
line(" " + c.value(detail));
|
|
252
|
+
if (hintCmd) {
|
|
253
|
+
line();
|
|
254
|
+
line(" " + c.dim("Run ") + c.cmd(hintCmd) + c.dim(" to continue."));
|
|
255
|
+
}
|
|
256
|
+
line();
|
|
257
|
+
}
|
|
258
|
+
/** Farewell shown on exit — with a resume hint when a session is in flight. */
|
|
259
|
+
export function renderExit(sessionId) {
|
|
260
|
+
line();
|
|
261
|
+
if (sessionId) {
|
|
262
|
+
line(MARGIN + c.dim("Resume this session with:"));
|
|
263
|
+
line(MARGIN + c.cmd(`cos session use ${sessionId}`));
|
|
264
|
+
line();
|
|
265
|
+
}
|
|
266
|
+
line(MARGIN + c.dim("Thanks for using Carrier OS. See you soon."));
|
|
267
|
+
line();
|
|
268
|
+
}
|
|
269
|
+
const MARGIN = " ";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { c } from "./colors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Renders a fenced code block as a subtle, rounded left-framed section with a
|
|
4
|
+
* language label and light syntax highlighting. No ASCII boxes — a single
|
|
5
|
+
* rounded gutter for a premium, uncluttered look. Returns col-0 lines; callers
|
|
6
|
+
* add any base indent.
|
|
7
|
+
*/
|
|
8
|
+
const LANG_LABEL = {
|
|
9
|
+
ts: "TypeScript",
|
|
10
|
+
tsx: "TypeScript",
|
|
11
|
+
typescript: "TypeScript",
|
|
12
|
+
js: "JavaScript",
|
|
13
|
+
jsx: "JavaScript",
|
|
14
|
+
javascript: "JavaScript",
|
|
15
|
+
py: "Python",
|
|
16
|
+
python: "Python",
|
|
17
|
+
json: "JSON",
|
|
18
|
+
sh: "Shell",
|
|
19
|
+
bash: "Shell",
|
|
20
|
+
shell: "Shell",
|
|
21
|
+
go: "Go",
|
|
22
|
+
rs: "Rust",
|
|
23
|
+
rust: "Rust",
|
|
24
|
+
sql: "SQL",
|
|
25
|
+
html: "HTML",
|
|
26
|
+
css: "CSS",
|
|
27
|
+
yaml: "YAML",
|
|
28
|
+
yml: "YAML",
|
|
29
|
+
md: "Markdown"
|
|
30
|
+
};
|
|
31
|
+
export function renderCodeBlock(code, lang) {
|
|
32
|
+
const label = LANG_LABEL[lang.toLowerCase()] ?? (lang || "code");
|
|
33
|
+
const lines = code.replace(/\n+$/, "").split("\n");
|
|
34
|
+
const out = [c.rule("╭─ ") + c.accent(label)];
|
|
35
|
+
for (const l of lines)
|
|
36
|
+
out.push(c.rule("│ ") + highlight(l));
|
|
37
|
+
out.push(c.rule("╰─"));
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
const KEYWORDS = /\b(function|const|let|var|return|if|else|for|while|switch|case|break|continue|import|from|export|default|class|extends|new|await|async|try|catch|finally|throw|typeof|instanceof|def|lambda|print|public|private|protected|static|void|int|float|double|str|string|bool|boolean|true|false|null|None|nil|undefined|this|self|package|func|type|struct|interface|enum|select|where|and|or|not)\b/g;
|
|
41
|
+
/** Tokenizer-free, cross-language light highlighting (keywords, strings, comments). */
|
|
42
|
+
export function highlight(text) {
|
|
43
|
+
const t = text.replace(/\t/g, " ");
|
|
44
|
+
const comment = t.match(/^(\s*)((\/\/|#|--).*)$/);
|
|
45
|
+
if (comment)
|
|
46
|
+
return comment[1] + c.dim(comment[2]);
|
|
47
|
+
// Split on string literals so keywords inside strings aren't recolored.
|
|
48
|
+
const parts = t.split(/("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|`(?:[^`\\]|\\.)*`)/g);
|
|
49
|
+
return parts
|
|
50
|
+
.map((p, i) => (i % 2 === 1 ? c.arg(p) : p.replace(KEYWORDS, (m) => c.cmd(m))))
|
|
51
|
+
.join("");
|
|
52
|
+
}
|