@carrieros/cli 0.1.0 → 0.2.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/dist/commands/auth.js +60 -0
- package/dist/commands/chat.js +27 -4
- package/dist/commands/doctor.js +7 -7
- package/dist/commands/help.js +2 -1
- package/dist/commands/login.js +19 -40
- package/dist/commands/logout.js +13 -4
- package/dist/commands/scope.js +152 -0
- package/dist/commands/whoami.js +2 -1
- package/dist/main.js +4 -0
- package/dist/services/api.js +33 -9
- package/dist/services/config.js +3 -1
- package/dist/services/context.js +59 -3
- package/dist/services/device-auth.js +249 -0
- package/dist/services/login-flow.js +113 -10
- package/dist/services/output.js +8 -3
- package/dist/services/spinner.js +23 -1
- package/dist/shell/shell-chat.js +102 -17
- package/dist/shell/shell-commands.js +21 -9
- package/dist/shell/shell-prompt.js +17 -38
- package/dist/shell/shell-renderer.js +14 -21
- package/dist/shell/shell.js +52 -32
- package/dist/ui/banner.js +16 -13
- package/dist/ui/chat-renderer.js +17 -16
- package/dist/ui/color-picker.js +21 -0
- package/dist/ui/colors.js +5 -1
- package/dist/ui/command-filter.js +15 -1
- package/dist/ui/command-palette.js +107 -60
- package/dist/ui/command-registry.js +4 -5
- package/dist/ui/live.js +15 -0
- package/dist/ui/screen.js +289 -0
- package/dist/ui/select.js +8 -0
- package/dist/ui/theme.js +17 -0
- package/package.json +5 -5
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ApiError } from "../services/api.js";
|
|
2
|
+
import { DEFAULT_API_URL } from "../services/config.js";
|
|
3
|
+
import { getCliContext } from "../services/device-auth.js";
|
|
4
|
+
import { heading, info, kv, line, warn } from "../services/output.js";
|
|
5
|
+
import { withSpinner } from "../services/spinner.js";
|
|
6
|
+
/**
|
|
7
|
+
* `cos auth status` — report whether this machine has a valid session and, if
|
|
8
|
+
* so, the account + active workspace/project it is bound to. Verifies live
|
|
9
|
+
* against the server, falling back to the cached context when offline.
|
|
10
|
+
*/
|
|
11
|
+
export function registerAuth(program, ctx) {
|
|
12
|
+
const auth = program.command("auth").description("Inspect the current session");
|
|
13
|
+
auth
|
|
14
|
+
.command("status")
|
|
15
|
+
.description("Show whether you are signed in, and the active context")
|
|
16
|
+
.action(async () => {
|
|
17
|
+
const apiUrl = ctx.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
18
|
+
if (!ctx.config.isAuthenticated) {
|
|
19
|
+
heading("Authentication");
|
|
20
|
+
kv("Status", "Signed out");
|
|
21
|
+
kv("Endpoint", apiUrl);
|
|
22
|
+
line();
|
|
23
|
+
info("Run `cos login` to sign in through your browser.");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const c = await withSpinner("Checking session…", () => getCliContext(apiUrl, ctx.authProvider()));
|
|
28
|
+
heading("Authentication");
|
|
29
|
+
kv("Status", "Signed in");
|
|
30
|
+
kv("Endpoint", apiUrl);
|
|
31
|
+
if (c.account)
|
|
32
|
+
kv("Account", c.account.email);
|
|
33
|
+
if (c.workspace)
|
|
34
|
+
kv("Workspace", `${c.workspace.name} (${c.workspace.slug})`);
|
|
35
|
+
if (c.project)
|
|
36
|
+
kv("Project", `${c.project.name} · ${c.project.environment}`);
|
|
37
|
+
line();
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
const auth = ctx.config.get("auth");
|
|
41
|
+
heading("Authentication");
|
|
42
|
+
if (err instanceof ApiError && err.status === 401) {
|
|
43
|
+
kv("Status", "Session expired");
|
|
44
|
+
line();
|
|
45
|
+
info("Run `cos login` to sign in again.");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
warn(`Could not reach the server (${err instanceof ApiError ? err.code : "error"}); showing cached details.`);
|
|
49
|
+
kv("Status", "Signed in (cached)");
|
|
50
|
+
kv("Endpoint", apiUrl);
|
|
51
|
+
if (auth?.account)
|
|
52
|
+
kv("Account", auth.account.email);
|
|
53
|
+
if (auth?.workspace)
|
|
54
|
+
kv("Workspace", auth.workspace.name);
|
|
55
|
+
if (auth?.project)
|
|
56
|
+
kv("Project", auth.project.name);
|
|
57
|
+
line();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
package/dist/commands/chat.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { input } from "@inquirer/prompts";
|
|
2
2
|
import chalk from "chalk";
|
|
3
3
|
import { info, line, printBanner } from "../services/output.js";
|
|
4
|
-
import { chatTurn, loadModels, modelLabelShort, pickMode, pickModel, setMode, setModel } from "../shell/shell-chat.js";
|
|
4
|
+
import { chatTurn, chatTypeLabelShort, loadModels, loadModes, modeLabelShort, modelLabelShort, pickChatType, pickMode, pickModel, setChatType, setMode, setModel } from "../shell/shell-chat.js";
|
|
5
5
|
/**
|
|
6
6
|
* `cos chat` — a focused interactive REPL over POST /v1/chat. (The full
|
|
7
7
|
* shell — `cos` with no command — is a superset with every slash command.)
|
|
@@ -13,19 +13,29 @@ export function registerChat(program, ctx) {
|
|
|
13
13
|
.description("Start a focused interactive chat session")
|
|
14
14
|
.option("-m, --mode <mode>", "Carrier mode (general, code, research, …)")
|
|
15
15
|
.option("--model <model>", "provider model override (e.g. gpt-5.5); 'auto' to clear")
|
|
16
|
+
.option("--standard", "start in Standard chat instead of Carrier Chat")
|
|
16
17
|
.action(async (opts) => {
|
|
18
|
+
// Default this REPL to the Carrier OS mode (Carrier Chat) like the shell;
|
|
19
|
+
// an explicit -m flag overrides it for this run.
|
|
17
20
|
if (opts.mode)
|
|
18
21
|
ctx.config.set("mode", opts.mode);
|
|
22
|
+
else
|
|
23
|
+
ctx.setSessionMode("general");
|
|
19
24
|
if (opts.model !== undefined)
|
|
20
25
|
ctx.config.set("model", opts.model === "auto" ? "" : opts.model);
|
|
26
|
+
// Chat type defaults to Carrier Chat (the persisted default is carrier);
|
|
27
|
+
// --standard opts into a plain chat for this run onward.
|
|
28
|
+
if (opts.standard)
|
|
29
|
+
ctx.setChatType("standard");
|
|
21
30
|
await runChat(ctx);
|
|
22
31
|
});
|
|
23
32
|
}
|
|
24
33
|
export async function runChat(ctx) {
|
|
25
34
|
const client = ctx.requireAuth();
|
|
26
|
-
// Preload models so the banner can show
|
|
27
|
-
await loadModels(client).catch(() => []);
|
|
28
|
-
printBanner(ctx.config.get("scope"), ctx
|
|
35
|
+
// Preload modes + models so the banner can show friendly labels (best-effort).
|
|
36
|
+
await Promise.all([loadModels(client).catch(() => []), loadModes(client).catch(() => [])]);
|
|
37
|
+
printBanner(ctx.config.get("scope"), modeLabelShort(ctx), modelLabelShort(ctx));
|
|
38
|
+
info(`${chatTypeLabelShort(ctx)} · type /chat to switch (carrier / standard)`);
|
|
29
39
|
for (;;) {
|
|
30
40
|
let message;
|
|
31
41
|
try {
|
|
@@ -59,6 +69,17 @@ export async function runChat(ctx) {
|
|
|
59
69
|
info(sid ? `Current session: ${sid}` : "No active session yet (a new one starts on your next message).");
|
|
60
70
|
continue;
|
|
61
71
|
}
|
|
72
|
+
if (cmdWord === "/chat") {
|
|
73
|
+
setChatType(ctx, argStr);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (cmdWord === "/switch") {
|
|
77
|
+
if (argStr.trim())
|
|
78
|
+
setChatType(ctx, argStr);
|
|
79
|
+
else
|
|
80
|
+
await pickChatType(ctx);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
62
83
|
if (cmdWord === "/modes") {
|
|
63
84
|
await pickMode(ctx, client);
|
|
64
85
|
continue;
|
|
@@ -81,6 +102,8 @@ export async function runChat(ctx) {
|
|
|
81
102
|
function printSlashHelp() {
|
|
82
103
|
line();
|
|
83
104
|
line(chalk.dim(" In-chat commands:"));
|
|
105
|
+
line(` ${chalk.cyan("/switch")} switch chat: Carrier ⇄ Standard (picker)`);
|
|
106
|
+
line(` ${chalk.cyan("/chat")} switch experience: /chat carrier | /chat standard`);
|
|
84
107
|
line(` ${chalk.cyan("/modes")} list Carrier modes (behavior)`);
|
|
85
108
|
line(` ${chalk.cyan("/mode <id>")} switch Carrier mode (e.g. /mode code)`);
|
|
86
109
|
line(` ${chalk.cyan("/models")} list provider models (GPT, Grok, …)`);
|
package/dist/commands/doctor.js
CHANGED
|
@@ -31,19 +31,19 @@ export function registerDoctor(program, ctx) {
|
|
|
31
31
|
catch (err) {
|
|
32
32
|
row("err", "API reachable", err instanceof ApiError ? err.message : "unreachable");
|
|
33
33
|
}
|
|
34
|
-
// 3)
|
|
34
|
+
// 3) Signed in?
|
|
35
35
|
if (!ctx.config.isAuthenticated) {
|
|
36
|
-
row("warn", "
|
|
36
|
+
row("warn", "Session", "not signed in — run `cos login`");
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
row("ok", "
|
|
40
|
-
// 4)
|
|
39
|
+
row("ok", "Session", "present");
|
|
40
|
+
// 4) Session valid?
|
|
41
41
|
try {
|
|
42
|
-
const scope = await withSpinner("Verifying
|
|
43
|
-
row("ok", "
|
|
42
|
+
const scope = await withSpinner("Verifying session…", () => client.verify());
|
|
43
|
+
row("ok", "Session valid", `${scope.workspace.name} / ${scope.project.name}`);
|
|
44
44
|
}
|
|
45
45
|
catch (err) {
|
|
46
|
-
row("err", "
|
|
46
|
+
row("err", "Session valid", err instanceof ApiError ? `${err.message} (${err.code})` : "failed");
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
// 5) Versions
|
package/dist/commands/help.js
CHANGED
|
@@ -13,7 +13,8 @@ export function registerHelp(program, _ctx) {
|
|
|
13
13
|
const item = (cmd, desc) => line(` ${chalk.cyan(cmd.padEnd(26))} ${chalk.dim(desc)}`);
|
|
14
14
|
line("\n" + chalk.bold.cyan(" Carrier OS CLI") + chalk.dim(" — developer workflow\n"));
|
|
15
15
|
line(chalk.bold(" Getting started"));
|
|
16
|
-
item("cos login", "
|
|
16
|
+
item("cos login", "sign in through your browser");
|
|
17
|
+
item("cos workspace use", "switch workspace · cos project use");
|
|
17
18
|
item("cos doctor", "verify configuration & connectivity");
|
|
18
19
|
item("cos whoami", "show workspace / project / account");
|
|
19
20
|
line("\n" + chalk.bold(" Chat"));
|
package/dist/commands/login.js
CHANGED
|
@@ -1,58 +1,37 @@
|
|
|
1
|
-
import { password } from "@inquirer/prompts";
|
|
2
|
-
import { ApiError } from "../services/api.js";
|
|
3
1
|
import { DEFAULT_API_URL } from "../services/config.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { withSpinner } from "../services/spinner.js";
|
|
2
|
+
import { deviceLogin, renderLoginFailure } from "../services/login-flow.js";
|
|
3
|
+
import { info, line } from "../services/output.js";
|
|
7
4
|
/**
|
|
8
|
-
* `cos login
|
|
9
|
-
*
|
|
10
|
-
*
|
|
5
|
+
* `cos login` — browser-based sign-in via the OAuth 2.0 Device Authorization
|
|
6
|
+
* flow (like Claude Code / GitHub CLI). Opens the platform, you approve the
|
|
7
|
+
* device and pick a workspace + project, and the CLI stores the issued session.
|
|
8
|
+
* No API key to paste.
|
|
11
9
|
*/
|
|
12
10
|
export function registerLogin(program, ctx) {
|
|
13
11
|
program
|
|
14
12
|
.command("login")
|
|
15
|
-
.description("
|
|
16
|
-
.argument("[key]", "carrier_sk_… secret key (prompted if omitted)")
|
|
13
|
+
.description("Sign in through your browser (device authorization)")
|
|
17
14
|
.option("--url <url>", "override the API base URL (e.g. http://localhost:5000/v1)")
|
|
18
|
-
.
|
|
15
|
+
.option("--platform <url>", "override the platform URL for the browser step (e.g. http://localhost:3002)")
|
|
16
|
+
.action(async (opts) => {
|
|
19
17
|
if (opts.url)
|
|
20
18
|
ctx.config.set("apiUrl", opts.url.replace(/\/+$/, ""));
|
|
19
|
+
if (opts.platform)
|
|
20
|
+
ctx.config.set("platformUrl", opts.platform.replace(/\/+$/, ""));
|
|
21
21
|
const apiUrl = ctx.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
22
|
-
|
|
23
|
-
(
|
|
24
|
-
|
|
25
|
-
mask: "•",
|
|
26
|
-
validate: (v) => (v.trim().length > 0 ? true : "A key is required.")
|
|
27
|
-
})).trim();
|
|
28
|
-
if (!key.startsWith("carrier_sk_")) {
|
|
29
|
-
warn("That does not look like a secret key (expected a carrier_sk_… value).");
|
|
22
|
+
if (ctx.config.isAuthenticated) {
|
|
23
|
+
info("You are already logged in. Run `cos logout` first to switch accounts.");
|
|
24
|
+
return;
|
|
30
25
|
}
|
|
31
26
|
try {
|
|
32
|
-
|
|
33
|
-
success("Logged in.");
|
|
27
|
+
await deviceLogin(ctx, apiUrl);
|
|
34
28
|
line();
|
|
35
|
-
|
|
36
|
-
kv("Project", scope.project.name);
|
|
37
|
-
kv("Key", `${scope.apiKey.name} (${scope.apiKey.type})`);
|
|
38
|
-
if (scope.user)
|
|
39
|
-
kv("Account", scope.user.email);
|
|
40
|
-
line();
|
|
41
|
-
info("Try `cos chat` to start talking, or `cos doctor` to check your setup.");
|
|
29
|
+
info("Run `cos` to open the shell, or `cos chat` to start talking.");
|
|
42
30
|
}
|
|
43
31
|
catch (err) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// (e.g. still pointed at production while the API runs locally).
|
|
48
|
-
if (err.code === "NETWORK_ERROR" || err.code.startsWith("HTTP_")) {
|
|
49
|
-
warn(`Endpoint: ${apiUrl}`);
|
|
50
|
-
info("For local development, run: cos login --url http://localhost:5000/v1");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
errorLine(`Login failed: ${err.message}`);
|
|
55
|
-
}
|
|
32
|
+
const kind = renderLoginFailure(err, apiUrl);
|
|
33
|
+
if (kind === "denied")
|
|
34
|
+
line(" " + "Run `cos login` to retry.");
|
|
56
35
|
process.exitCode = 1;
|
|
57
36
|
}
|
|
58
37
|
});
|
package/dist/commands/logout.js
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
+
import { DEFAULT_API_URL } from "../services/config.js";
|
|
2
|
+
import { logoutCliSession } from "../services/device-auth.js";
|
|
1
3
|
import { info, success } from "../services/output.js";
|
|
2
|
-
|
|
4
|
+
import { withSpinner } from "../services/spinner.js";
|
|
5
|
+
/** `cos logout` — revoke the session server-side, then forget it locally. */
|
|
3
6
|
export function registerLogout(program, ctx) {
|
|
4
7
|
program
|
|
5
8
|
.command("logout")
|
|
6
|
-
.description("
|
|
7
|
-
.action(() => {
|
|
9
|
+
.description("Sign out and revoke this device's session")
|
|
10
|
+
.action(async () => {
|
|
8
11
|
if (!ctx.config.isAuthenticated) {
|
|
9
12
|
info("You are already logged out.");
|
|
10
13
|
return;
|
|
11
14
|
}
|
|
15
|
+
const apiUrl = ctx.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
16
|
+
const refreshToken = ctx.config.get("auth")?.refreshToken;
|
|
17
|
+
if (refreshToken) {
|
|
18
|
+
// Best-effort server revoke so the refresh token can't be reused.
|
|
19
|
+
await withSpinner("Signing out…", () => logoutCliSession(apiUrl, refreshToken));
|
|
20
|
+
}
|
|
12
21
|
ctx.config.clearAuth();
|
|
13
|
-
success("Logged out.
|
|
22
|
+
success("Logged out. This device's session has been removed.");
|
|
14
23
|
});
|
|
15
24
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { select } from "@inquirer/prompts";
|
|
2
|
+
import { DEFAULT_API_URL } from "../services/config.js";
|
|
3
|
+
import { listCliProjects, listCliWorkspaces, setCliScope } from "../services/device-auth.js";
|
|
4
|
+
import { errorLine, heading, info, kv, line, success } from "../services/output.js";
|
|
5
|
+
import { withSpinner } from "../services/spinner.js";
|
|
6
|
+
import { screenActive } from "../ui/screen.js";
|
|
7
|
+
/**
|
|
8
|
+
* `cos workspace use [target]` / `cos project use [target]` — switch the active
|
|
9
|
+
* developer context. The chosen workspace + project are re-bound server-side
|
|
10
|
+
* (which rotates the session), so every subsequent request carries the new
|
|
11
|
+
* scope. `target` may be an id or a name; omit it for an interactive picker.
|
|
12
|
+
*/
|
|
13
|
+
export function registerScope(program, ctx) {
|
|
14
|
+
const workspace = program.command("workspace").description("Manage the active workspace");
|
|
15
|
+
workspace
|
|
16
|
+
.command("list")
|
|
17
|
+
.description("List your workspaces")
|
|
18
|
+
.action(async () => {
|
|
19
|
+
const { apiUrl, provider } = auth(ctx);
|
|
20
|
+
const { workspaces } = await withSpinner("Loading workspaces…", () => listCliWorkspaces(apiUrl, provider));
|
|
21
|
+
printList("Workspaces", workspaces, activeWorkspaceId(ctx), (w) => `${w.name} ${dim(w.slug)}`);
|
|
22
|
+
});
|
|
23
|
+
workspace
|
|
24
|
+
.command("use [target]")
|
|
25
|
+
.description("Switch the active workspace (and pick a project in it)")
|
|
26
|
+
.action(async (target) => {
|
|
27
|
+
await useWorkspace(ctx, target);
|
|
28
|
+
});
|
|
29
|
+
const project = program.command("project").description("Manage the active project");
|
|
30
|
+
project
|
|
31
|
+
.command("list")
|
|
32
|
+
.description("List projects in the active workspace")
|
|
33
|
+
.action(async () => {
|
|
34
|
+
const { apiUrl, provider } = auth(ctx);
|
|
35
|
+
const wsId = requireActiveWorkspace(ctx);
|
|
36
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, wsId));
|
|
37
|
+
printList("Projects", projects, activeProjectId(ctx), (p) => `${p.name} ${dim(p.environment)}`);
|
|
38
|
+
});
|
|
39
|
+
project
|
|
40
|
+
.command("use [target]")
|
|
41
|
+
.description("Switch the active project within the current workspace")
|
|
42
|
+
.action(async (target) => {
|
|
43
|
+
await useProject(ctx, target);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// ── flows ─────────────────────────────────────────────────────────────────────
|
|
47
|
+
async function useWorkspace(ctx, target) {
|
|
48
|
+
const { apiUrl, provider } = auth(ctx);
|
|
49
|
+
const { workspaces } = await withSpinner("Loading workspaces…", () => listCliWorkspaces(apiUrl, provider));
|
|
50
|
+
const ws = await resolve(workspaces, target, "Select a workspace:", (w) => `${w.name} (${w.slug})`);
|
|
51
|
+
if (!ws)
|
|
52
|
+
return unresolved("workspace", workspaces, activeWorkspaceId(ctx), (w) => `${w.name} ${dim(w.slug)}`);
|
|
53
|
+
// A workspace switch needs a project in that workspace — pick its default, or
|
|
54
|
+
// prompt when interactive.
|
|
55
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, ws.id));
|
|
56
|
+
if (projects.length === 0) {
|
|
57
|
+
errorLine(`Workspace "${ws.name}" has no projects. Create one in the dashboard first.`);
|
|
58
|
+
process.exitCode = 1;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const proj = (await resolve(projects, undefined, `Select a project in ${ws.name}:`, (p) => `${p.name} (${p.environment})`)) ??
|
|
62
|
+
projects[0];
|
|
63
|
+
await applyScope(ctx, apiUrl, provider, ws.id, proj.id);
|
|
64
|
+
}
|
|
65
|
+
async function useProject(ctx, target) {
|
|
66
|
+
const { apiUrl, provider } = auth(ctx);
|
|
67
|
+
const wsId = requireActiveWorkspace(ctx);
|
|
68
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, wsId));
|
|
69
|
+
const proj = await resolve(projects, target, "Select a project:", (p) => `${p.name} (${p.environment})`);
|
|
70
|
+
if (!proj)
|
|
71
|
+
return unresolved("project", projects, activeProjectId(ctx), (p) => `${p.name} ${dim(p.environment)}`);
|
|
72
|
+
await applyScope(ctx, apiUrl, provider, wsId, proj.id);
|
|
73
|
+
}
|
|
74
|
+
async function applyScope(ctx, apiUrl, provider, workspaceId, projectId) {
|
|
75
|
+
const tokens = await withSpinner("Switching context…", () => setCliScope(apiUrl, provider, workspaceId, projectId));
|
|
76
|
+
persist(ctx, tokens);
|
|
77
|
+
success("Context updated.");
|
|
78
|
+
line();
|
|
79
|
+
kv("Workspace", tokens.workspace?.name ?? "—");
|
|
80
|
+
kv("Project", tokens.project?.name ?? "—");
|
|
81
|
+
line();
|
|
82
|
+
}
|
|
83
|
+
// ── helpers ───────────────────────────────────────────────────────────────────
|
|
84
|
+
function auth(ctx) {
|
|
85
|
+
ctx.requireAuth();
|
|
86
|
+
return { apiUrl: ctx.config.get("apiUrl") ?? DEFAULT_API_URL, provider: ctx.authProvider() };
|
|
87
|
+
}
|
|
88
|
+
/** Resolve a target (id or name) or prompt interactively; returns undefined when
|
|
89
|
+
* no match and no picker is available (caller prints the list). */
|
|
90
|
+
async function resolve(items, target, message, labelFn) {
|
|
91
|
+
if (target) {
|
|
92
|
+
const q = target.trim().toLowerCase();
|
|
93
|
+
return items.find((i) => i.id.toLowerCase() === q || i.name.toLowerCase() === q);
|
|
94
|
+
}
|
|
95
|
+
// Interactive select only when we own a real TTY (not inside the shell's TUI).
|
|
96
|
+
if (!process.stdout.isTTY || screenActive())
|
|
97
|
+
return undefined;
|
|
98
|
+
if (items.length === 0)
|
|
99
|
+
return undefined;
|
|
100
|
+
const id = await select({
|
|
101
|
+
message,
|
|
102
|
+
choices: items.map((i) => ({ name: labelFn(i), value: i.id }))
|
|
103
|
+
});
|
|
104
|
+
return items.find((i) => i.id === id);
|
|
105
|
+
}
|
|
106
|
+
function unresolved(kind, items, activeId, labelFn) {
|
|
107
|
+
errorLine(`No matching ${kind}. Available ${kind}s:`);
|
|
108
|
+
printList(cap(kind) + "s", items, activeId, labelFn);
|
|
109
|
+
info(`Run \`cos ${kind} use <name-or-id>\` to switch.`);
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
}
|
|
112
|
+
function persist(ctx, tokens) {
|
|
113
|
+
const current = ctx.config.get("auth");
|
|
114
|
+
if (current) {
|
|
115
|
+
ctx.config.update({
|
|
116
|
+
auth: {
|
|
117
|
+
...current,
|
|
118
|
+
accessToken: tokens.access_token,
|
|
119
|
+
refreshToken: tokens.refresh_token,
|
|
120
|
+
workspace: tokens.workspace ?? current.workspace,
|
|
121
|
+
project: tokens.project ?? current.project
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const user = ctx.config.get("scope")?.user ?? null;
|
|
126
|
+
ctx.config.set("scope", {
|
|
127
|
+
apiKey: null,
|
|
128
|
+
workspace: tokens.workspace ?? { id: "", name: "—", slug: "" },
|
|
129
|
+
project: tokens.project ?? { id: "", name: "—", projectId: "", environment: "" },
|
|
130
|
+
user
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function printList(title, items, activeId, labelFn) {
|
|
134
|
+
heading(title);
|
|
135
|
+
for (const it of items) {
|
|
136
|
+
const marker = it.id === activeId ? "● " : " ";
|
|
137
|
+
line(` ${marker}${labelFn(it)}`);
|
|
138
|
+
}
|
|
139
|
+
line();
|
|
140
|
+
}
|
|
141
|
+
function requireActiveWorkspace(ctx) {
|
|
142
|
+
const wsId = activeWorkspaceId(ctx);
|
|
143
|
+
if (!wsId) {
|
|
144
|
+
errorLine("No active workspace. Run `cos workspace use` first.");
|
|
145
|
+
throw new Error("no active workspace");
|
|
146
|
+
}
|
|
147
|
+
return wsId;
|
|
148
|
+
}
|
|
149
|
+
const activeWorkspaceId = (ctx) => ctx.config.get("auth")?.workspace?.id;
|
|
150
|
+
const activeProjectId = (ctx) => ctx.config.get("auth")?.project?.id;
|
|
151
|
+
const dim = (s) => `· ${s}`;
|
|
152
|
+
const cap = (s) => (s ? s[0].toUpperCase() + s.slice(1) : s);
|
package/dist/commands/whoami.js
CHANGED
|
@@ -33,7 +33,8 @@ function render(scope, cached) {
|
|
|
33
33
|
kv("Workspace", `${scope.workspace.name} (${scope.workspace.slug})`);
|
|
34
34
|
kv("Project", scope.project.name);
|
|
35
35
|
kv("Environment", scope.project.environment);
|
|
36
|
-
|
|
36
|
+
// apiKey is null for a device-flow (browser) session.
|
|
37
|
+
kv("Signed in via", scope.apiKey ? `API key · ${scope.apiKey.name}` : "Browser session");
|
|
37
38
|
if (scope.user)
|
|
38
39
|
kv("Account", scope.user.email);
|
|
39
40
|
line();
|
package/dist/main.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command, CommanderError } from "commander";
|
|
3
|
+
import { registerAuth } from "./commands/auth.js";
|
|
3
4
|
import { registerChat } from "./commands/chat.js";
|
|
4
5
|
import { registerDoctor } from "./commands/doctor.js";
|
|
5
6
|
import { registerFiles } from "./commands/files.js";
|
|
@@ -10,6 +11,7 @@ import { registerLogout } from "./commands/logout.js";
|
|
|
10
11
|
import { registerModels } from "./commands/models.js";
|
|
11
12
|
import { registerModes } from "./commands/modes.js";
|
|
12
13
|
import { registerModeModel } from "./commands/mode-model.js";
|
|
14
|
+
import { registerScope } from "./commands/scope.js";
|
|
13
15
|
import { registerSessions } from "./commands/sessions.js";
|
|
14
16
|
import { registerUpload } from "./commands/upload.js";
|
|
15
17
|
import { registerUsage } from "./commands/usage.js";
|
|
@@ -39,7 +41,9 @@ async function main() {
|
|
|
39
41
|
// Phase A — auth & diagnostics
|
|
40
42
|
registerLogin(program, ctx);
|
|
41
43
|
registerLogout(program, ctx);
|
|
44
|
+
registerAuth(program, ctx);
|
|
42
45
|
registerWhoami(program, ctx);
|
|
46
|
+
registerScope(program, ctx);
|
|
43
47
|
registerVersion(program, ctx);
|
|
44
48
|
registerDoctor(program, ctx);
|
|
45
49
|
// Phase B — chat, sessions, history
|
package/dist/services/api.js
CHANGED
|
@@ -21,18 +21,32 @@ export class ApiError extends Error {
|
|
|
21
21
|
*/
|
|
22
22
|
export class ApiClient {
|
|
23
23
|
baseUrl;
|
|
24
|
+
auth;
|
|
24
25
|
http;
|
|
25
|
-
|
|
26
|
+
/**
|
|
27
|
+
* @param baseUrl the /v1 base
|
|
28
|
+
* @param auth an AuthProvider (device-flow) OR a legacy static key string.
|
|
29
|
+
*/
|
|
30
|
+
constructor(baseUrl, auth) {
|
|
26
31
|
this.baseUrl = baseUrl;
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
this.auth = auth;
|
|
33
|
+
this.http = axios.create({ baseURL: baseUrl, timeout: 60_000 });
|
|
34
|
+
}
|
|
35
|
+
/** Current bearer token, whether from a provider or a legacy static key. */
|
|
36
|
+
token() {
|
|
37
|
+
return typeof this.auth === "string" ? this.auth : this.auth?.token();
|
|
38
|
+
}
|
|
39
|
+
async request(config, retry = true) {
|
|
40
|
+
const token = this.token();
|
|
41
|
+
const withAuth = {
|
|
42
|
+
...config,
|
|
43
|
+
headers: {
|
|
44
|
+
...(config.headers ?? {}),
|
|
45
|
+
...(token ? { Authorization: `Bearer ${token}` } : {})
|
|
46
|
+
}
|
|
47
|
+
};
|
|
34
48
|
try {
|
|
35
|
-
const res = await this.http.request(
|
|
49
|
+
const res = await this.http.request(withAuth);
|
|
36
50
|
const body = res.data;
|
|
37
51
|
if (body?.success && body.data !== undefined)
|
|
38
52
|
return body.data;
|
|
@@ -41,6 +55,16 @@ export class ApiClient {
|
|
|
41
55
|
catch (err) {
|
|
42
56
|
if (err instanceof ApiError)
|
|
43
57
|
throw err;
|
|
58
|
+
// Silent refresh: on a 401, renew the token once and retry the request.
|
|
59
|
+
if (isAxiosError(err) &&
|
|
60
|
+
err.response?.status === 401 &&
|
|
61
|
+
retry &&
|
|
62
|
+
typeof this.auth === "object" &&
|
|
63
|
+
this.auth.refresh) {
|
|
64
|
+
const renewed = await this.auth.refresh();
|
|
65
|
+
if (renewed)
|
|
66
|
+
return this.request(config, false);
|
|
67
|
+
}
|
|
44
68
|
if (isAxiosError(err)) {
|
|
45
69
|
const status = err.response?.status;
|
|
46
70
|
const body = err.response?.data;
|
package/dist/services/config.js
CHANGED
|
@@ -55,10 +55,12 @@ export class ConfigStore {
|
|
|
55
55
|
this.write();
|
|
56
56
|
}
|
|
57
57
|
get isAuthenticated() {
|
|
58
|
-
|
|
58
|
+
// Device-flow session is the primary credential; a legacy key still counts.
|
|
59
|
+
return Boolean(this.data.auth?.accessToken || this.data.secretKey);
|
|
59
60
|
}
|
|
60
61
|
/** Wipe credentials + derived state on logout (keeps apiUrl). */
|
|
61
62
|
clearAuth() {
|
|
63
|
+
delete this.data.auth;
|
|
62
64
|
delete this.data.secretKey;
|
|
63
65
|
delete this.data.sessionId;
|
|
64
66
|
delete this.data.scope;
|
package/dist/services/context.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApiClient } from "./api.js";
|
|
2
2
|
import { DEFAULT_API_URL } from "./config.js";
|
|
3
|
+
import { refreshCliSession } from "./device-auth.js";
|
|
3
4
|
/**
|
|
4
5
|
* Thrown by requireAuth when no key is configured. Unlike a process.exit, this
|
|
5
6
|
* can be caught — so the interactive shell can keep running after a failed
|
|
@@ -21,9 +22,46 @@ export class CommandContext {
|
|
|
21
22
|
constructor(config) {
|
|
22
23
|
this.config = config;
|
|
23
24
|
}
|
|
25
|
+
/** In-process mode override. The interactive shell always OPENS in the Carrier
|
|
26
|
+
* OS mode (the core persona) regardless of the persisted default; setting the
|
|
27
|
+
* mode mid-session updates this. It is never written to disk, so standalone
|
|
28
|
+
* commands keep using the persisted `mode`. */
|
|
29
|
+
overrideMode;
|
|
24
30
|
/** A client built from current config (anonymous if not logged in). */
|
|
25
31
|
client() {
|
|
26
|
-
return new ApiClient(this.config.get("apiUrl") ?? DEFAULT_API_URL, this.
|
|
32
|
+
return new ApiClient(this.config.get("apiUrl") ?? DEFAULT_API_URL, this.authProvider());
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The credential + silent-refresh strategy for every authenticated call.
|
|
36
|
+
* Prefers the device-flow session; falls back to a legacy static key. On a
|
|
37
|
+
* 401 it rotates the refresh token and persists the new pair; if the session
|
|
38
|
+
* is dead it clears local auth so the next command prompts a fresh login.
|
|
39
|
+
*/
|
|
40
|
+
authProvider() {
|
|
41
|
+
return {
|
|
42
|
+
token: () => this.config.get("auth")?.accessToken ?? this.config.get("secretKey"),
|
|
43
|
+
refresh: async () => {
|
|
44
|
+
const auth = this.config.get("auth");
|
|
45
|
+
if (!auth?.refreshToken)
|
|
46
|
+
return undefined;
|
|
47
|
+
const apiUrl = this.config.get("apiUrl") ?? DEFAULT_API_URL;
|
|
48
|
+
const renewed = await refreshCliSession(apiUrl, auth.refreshToken);
|
|
49
|
+
if (!renewed) {
|
|
50
|
+
this.config.clearAuth();
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
this.config.update({
|
|
54
|
+
auth: {
|
|
55
|
+
...auth,
|
|
56
|
+
accessToken: renewed.access_token,
|
|
57
|
+
refreshToken: renewed.refresh_token,
|
|
58
|
+
workspace: renewed.workspace ?? auth.workspace,
|
|
59
|
+
project: renewed.project ?? auth.project
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return renewed.access_token;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
27
65
|
}
|
|
28
66
|
/**
|
|
29
67
|
* A client guaranteed to carry a key, or a clean exit with guidance. Use this
|
|
@@ -35,8 +73,26 @@ export class CommandContext {
|
|
|
35
73
|
}
|
|
36
74
|
return this.client();
|
|
37
75
|
}
|
|
38
|
-
/**
|
|
76
|
+
/** Active Carrier mode: the session override if set (the shell), otherwise the
|
|
77
|
+
* persisted default, falling back to "general" (the Carrier OS persona). */
|
|
39
78
|
get mode() {
|
|
40
|
-
return this.config.get("mode") ?? "general";
|
|
79
|
+
return this.overrideMode ?? this.config.get("mode") ?? "general";
|
|
80
|
+
}
|
|
81
|
+
/** Set the mode for this process only (interactive shell), without persisting. */
|
|
82
|
+
setSessionMode(id) {
|
|
83
|
+
this.overrideMode = id;
|
|
84
|
+
}
|
|
85
|
+
/** Active chat experience: "carrier" (flagship Carrier Chat) by default — you
|
|
86
|
+
* always land in Carrier Chat, matching the web app. Persisted so a chosen
|
|
87
|
+
* type survives across invocations; reset to carrier on login. */
|
|
88
|
+
get chatType() {
|
|
89
|
+
return this.config.get("chatType") ?? "carrier";
|
|
90
|
+
}
|
|
91
|
+
/** Switch chat type. Because a conversation's type is fixed at creation, this
|
|
92
|
+
* clears the active session so the NEXT message starts a fresh conversation of
|
|
93
|
+
* the new type. */
|
|
94
|
+
setChatType(type) {
|
|
95
|
+
this.config.set("chatType", type);
|
|
96
|
+
this.config.set("sessionId", undefined);
|
|
41
97
|
}
|
|
42
98
|
}
|