turno-cli 0.0.0-stage → 0.1.1
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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +335 -2
- package/dist/accounts/audit.js +54 -0
- package/dist/accounts/builtin.js +214 -0
- package/dist/accounts/delegate.js +185 -0
- package/dist/accounts/registry.js +190 -0
- package/dist/agents/index.js +101 -0
- package/dist/agents/snippets.js +64 -0
- package/dist/cli/args.js +47 -0
- package/dist/cli/commands/accounts.js +216 -0
- package/dist/cli/commands/auto.js +44 -0
- package/dist/cli/commands/available.js +18 -0
- package/dist/cli/commands/browser.js +175 -0
- package/dist/cli/commands/complete.js +112 -0
- package/dist/cli/commands/completion.js +57 -0
- package/dist/cli/commands/config.js +168 -0
- package/dist/cli/commands/doctor.js +324 -0
- package/dist/cli/commands/exhausted.js +40 -0
- package/dist/cli/commands/handoff.js +118 -0
- package/dist/cli/commands/hook.js +63 -0
- package/dist/cli/commands/identity.js +114 -0
- package/dist/cli/commands/limit.js +102 -0
- package/dist/cli/commands/mcp.js +350 -0
- package/dist/cli/commands/mcpd.js +161 -0
- package/dist/cli/commands/migrate.js +176 -0
- package/dist/cli/commands/open.js +60 -0
- package/dist/cli/commands/profile.js +259 -0
- package/dist/cli/commands/resolve.js +23 -0
- package/dist/cli/commands/run.js +15 -0
- package/dist/cli/commands/secret.js +123 -0
- package/dist/cli/commands/session.js +36 -0
- package/dist/cli/commands/sessions.js +27 -0
- package/dist/cli/commands/setup.js +224 -0
- package/dist/cli/commands/status.js +101 -0
- package/dist/cli/commands/statusline.js +275 -0
- package/dist/cli/commands/ui.js +276 -0
- package/dist/cli/commands/undo.js +67 -0
- package/dist/cli/commands/update-check.js +22 -0
- package/dist/cli/commands/upgrade.js +162 -0
- package/dist/cli/commands/usage.js +167 -0
- package/dist/cli/commands/version.js +27 -0
- package/dist/cli/commands/which.js +28 -0
- package/dist/cli/group.js +50 -0
- package/dist/cli/io.js +22 -0
- package/dist/cli/launch.js +98 -0
- package/dist/cli/launchers.js +200 -0
- package/dist/cli/main.js +94 -0
- package/dist/cli/registry.js +77 -0
- package/dist/core/config-edit.js +42 -0
- package/dist/core/config.js +137 -0
- package/dist/core/exhausted.js +30 -0
- package/dist/core/journal.js +185 -0
- package/dist/core/manifest.js +177 -0
- package/dist/core/paths.js +54 -0
- package/dist/core/profiles.js +61 -0
- package/dist/core/ratelimit.js +39 -0
- package/dist/core/resolve.js +78 -0
- package/dist/core/secrets.js +44 -0
- package/dist/core/sessions.js +173 -0
- package/dist/core/toml-edit.js +152 -0
- package/dist/core/update.js +102 -0
- package/dist/core/version.js +109 -0
- package/dist/mcpd/cache.js +41 -0
- package/dist/mcpd/connectors.js +63 -0
- package/dist/mcpd/daemon.js +163 -0
- package/dist/mcpd/gateway.js +308 -0
- package/dist/mcpd/importer.js +160 -0
- package/dist/mcpd/instances.js +303 -0
- package/dist/mcpd/jsonrpc.js +19 -0
- package/dist/mcpd/launchd.js +86 -0
- package/dist/mcpd/peerpid.js +25 -0
- package/dist/mcpd/sync.js +208 -0
- package/dist/mcpd/upstream.js +232 -0
- package/dist/statusline/cost.js +135 -0
- package/dist/statusline/render.js +246 -0
- package/dist/ui/cmap.js +57 -0
- package/dist/ui/fonts.js +107 -0
- package/dist/ui/settings.js +69 -0
- package/dist/ui/terminal-id.js +30 -0
- package/dist/ui/terminals.js +163 -0
- package/package.json +69 -3
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { loadConfig } from "../../core/config.js";
|
|
5
|
+
import { exhaustedUntil } from "../../core/exhausted.js";
|
|
6
|
+
import { paths, profileDir } from "../../core/paths.js";
|
|
7
|
+
import { listProfiles } from "../../core/profiles.js";
|
|
8
|
+
import { paint } from "../io.js";
|
|
9
|
+
/**
|
|
10
|
+
* Run `claude auth status --json` with the profile's config dir (20 s timeout). Records the
|
|
11
|
+
* plan in the state plan dir, where the status line reads it. Undefined when it fails.
|
|
12
|
+
*/
|
|
13
|
+
export function authStatus(profile, env = process.env) {
|
|
14
|
+
return new Promise((resolve) => {
|
|
15
|
+
execFile("claude", ["auth", "status", "--json"], { env: { ...env, CLAUDE_CONFIG_DIR: profileDir(profile, env) }, timeout: 20_000, encoding: "utf8" }, (_err, stdout) => {
|
|
16
|
+
let s;
|
|
17
|
+
try {
|
|
18
|
+
s = JSON.parse(stdout);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
resolve(undefined);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!s || typeof s !== "object") {
|
|
25
|
+
resolve(undefined);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (s.loggedIn && s.subscriptionType) {
|
|
29
|
+
try {
|
|
30
|
+
const dir = paths(env).planDir;
|
|
31
|
+
mkdirSync(dir, { recursive: true });
|
|
32
|
+
writeFileSync(join(dir, profile), `${s.subscriptionType}\n`);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// the plan file is a cache for the status line
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
resolve(s);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function readSnapshot(file) {
|
|
43
|
+
try {
|
|
44
|
+
const v = JSON.parse(readFileSync(file, "utf8"));
|
|
45
|
+
return v && typeof v === "object" ? v : undefined;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The newer of the snapshot in the state dir and the one the bash tool's status line wrote. */
|
|
52
|
+
export function usageSnapshot(profile, env = process.env) {
|
|
53
|
+
const p = paths(env);
|
|
54
|
+
const current = readSnapshot(join(p.usageDir, `${profile}.json`));
|
|
55
|
+
const legacy = readSnapshot(join(p.legacyConfig, "uso", `${profile}.json`));
|
|
56
|
+
if (!current)
|
|
57
|
+
return legacy ?? {};
|
|
58
|
+
if (!legacy)
|
|
59
|
+
return current;
|
|
60
|
+
return (legacy.updated_at ?? -Infinity) > (current.updated_at ?? -Infinity) ? legacy : current;
|
|
61
|
+
}
|
|
62
|
+
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
63
|
+
const pad2 = (n) => String(n).padStart(2, "0");
|
|
64
|
+
/** "Thu 12 14:30" in local time. */
|
|
65
|
+
export function shortTime(epochSeconds) {
|
|
66
|
+
const d = new Date(epochSeconds * 1000);
|
|
67
|
+
return `${DAYS[d.getDay()]} ${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`;
|
|
68
|
+
}
|
|
69
|
+
export function bar(pct, width = 30) {
|
|
70
|
+
const filled = Math.round((Math.min(Math.max(pct, 0), 100) * width) / 100);
|
|
71
|
+
return "█".repeat(filled) + "░".repeat(width - filled);
|
|
72
|
+
}
|
|
73
|
+
export function resetText(ts, now = Date.now() / 1000) {
|
|
74
|
+
const d = Math.floor(ts - now);
|
|
75
|
+
if (d <= 0)
|
|
76
|
+
return "already reset";
|
|
77
|
+
const h = Math.floor(d / 3600);
|
|
78
|
+
const m = Math.floor((d % 3600) / 60);
|
|
79
|
+
const rel = h >= 24 ? `${Math.floor(h / 24)}d ${h % 24}h` : h ? `${h}h${pad2(m)}m` : `${m}m`;
|
|
80
|
+
return `resets in ${rel} (${shortTime(ts)})`;
|
|
81
|
+
}
|
|
82
|
+
export function agoText(ts, now = Date.now() / 1000) {
|
|
83
|
+
const d = Math.floor(now - ts);
|
|
84
|
+
if (d < 60)
|
|
85
|
+
return "seconds ago";
|
|
86
|
+
if (d < 3600)
|
|
87
|
+
return `${Math.floor(d / 60)} min ago`;
|
|
88
|
+
if (d < 86400)
|
|
89
|
+
return `${Math.floor(d / 3600)} h ago`;
|
|
90
|
+
return `${Math.floor(d / 86400)} d ago`;
|
|
91
|
+
}
|
|
92
|
+
export async function account(profile, env) {
|
|
93
|
+
const s = await authStatus(profile, env);
|
|
94
|
+
if (!s)
|
|
95
|
+
return { email: "?", plan: "?" };
|
|
96
|
+
if (!s.loggedIn)
|
|
97
|
+
return { email: "not logged in", plan: "" };
|
|
98
|
+
return { email: s.email || "?", plan: s.subscriptionType || s.authMethod || "?" };
|
|
99
|
+
}
|
|
100
|
+
export function renderUsage(profiles, accounts, io) {
|
|
101
|
+
const now = Date.now() / 1000;
|
|
102
|
+
const out = [];
|
|
103
|
+
for (const p of profiles) {
|
|
104
|
+
const { email, plan } = accounts[p] ?? { email: "?", plan: "?" };
|
|
105
|
+
out.push(paint(io, `${p} ${email}${plan ? ` · ${plan}` : ""}`, "1"));
|
|
106
|
+
const exhausted = exhaustedUntil(p, io.env);
|
|
107
|
+
if (exhausted !== undefined && exhausted > now) {
|
|
108
|
+
out.push(paint(io, ` marked exhausted: its projects use the fallback (if set) until ${shortTime(exhausted)}`, "2"));
|
|
109
|
+
}
|
|
110
|
+
const data = usageSnapshot(p, io.env);
|
|
111
|
+
const limits = data.rate_limits ?? {};
|
|
112
|
+
const hasLimits = Object.keys(limits).length > 0;
|
|
113
|
+
if (!hasLimits) {
|
|
114
|
+
const why = plan === "team" ? "the Team plan does not report these limits" : "shows up once a session of this account gets a response";
|
|
115
|
+
out.push(paint(io, ` no data yet: ${why}`, "2"));
|
|
116
|
+
}
|
|
117
|
+
for (const [key, label] of [
|
|
118
|
+
["five_hour", "5 hours"],
|
|
119
|
+
["seven_day", "week"],
|
|
120
|
+
]) {
|
|
121
|
+
const w = limits[key];
|
|
122
|
+
if (!w)
|
|
123
|
+
continue;
|
|
124
|
+
let pct = Number(w.used_percentage) || 0;
|
|
125
|
+
const reset = typeof w.resets_at === "number" ? w.resets_at : undefined;
|
|
126
|
+
if (reset && reset < now)
|
|
127
|
+
pct = 0;
|
|
128
|
+
const code = pct < 50 ? "32" : pct < 80 ? "33" : "31";
|
|
129
|
+
out.push(` ${label.padEnd(8)}${paint(io, bar(pct), code)} ${pct.toFixed(0).padStart(3)}% ${reset ? resetText(reset, now) : ""}`);
|
|
130
|
+
}
|
|
131
|
+
if (hasLimits && typeof data.updated_at === "number")
|
|
132
|
+
out.push(paint(io, ` data from ${agoText(data.updated_at, now)}`, "2"));
|
|
133
|
+
out.push("");
|
|
134
|
+
}
|
|
135
|
+
return `${out.join("\n").trimEnd()}\n`;
|
|
136
|
+
}
|
|
137
|
+
export const command = {
|
|
138
|
+
summary: "5 h and weekly usage of each account, as bars",
|
|
139
|
+
usage: [
|
|
140
|
+
"Usage: turno usage [--watch]",
|
|
141
|
+
"",
|
|
142
|
+
"Shows the rate limits the status line records for each profile. --watch redraws",
|
|
143
|
+
"every 30 s until Ctrl+C.",
|
|
144
|
+
].join("\n"),
|
|
145
|
+
async run(args, io) {
|
|
146
|
+
const watch = args.includes("--watch");
|
|
147
|
+
const profiles = listProfiles(loadConfig(io.env), io.env, "claude");
|
|
148
|
+
const list = await Promise.all(profiles.map((p) => account(p, io.env)));
|
|
149
|
+
const accounts = Object.fromEntries(profiles.map((p, i) => [p, list[i]]));
|
|
150
|
+
if (!watch) {
|
|
151
|
+
io.out(renderUsage(profiles, accounts, io).trimEnd());
|
|
152
|
+
return 0;
|
|
153
|
+
}
|
|
154
|
+
return new Promise((resolve) => {
|
|
155
|
+
const draw = () => {
|
|
156
|
+
io.out(`\x1b[2J\x1b[H${renderUsage(profiles, accounts, io)}\n(refreshes every 30 s · Ctrl+C to quit)`);
|
|
157
|
+
};
|
|
158
|
+
draw();
|
|
159
|
+
const timer = setInterval(draw, 30_000);
|
|
160
|
+
process.once("SIGINT", () => {
|
|
161
|
+
clearInterval(timer);
|
|
162
|
+
resolve(0);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=usage.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { tildify } from "../../core/paths.js";
|
|
2
|
+
import { availableUpdate } from "../../core/update.js";
|
|
3
|
+
import { detectInstall, packageInfo } from "../../core/version.js";
|
|
4
|
+
import { JSON_FLAG, bool, parseFlags, printJson } from "../args.js";
|
|
5
|
+
export const command = {
|
|
6
|
+
summary: "the turno version, how it was installed, and whether a newer one is known",
|
|
7
|
+
usage: "Usage: turno version [--json]\n\nturno --version prints only the number.",
|
|
8
|
+
flags: ["--json"],
|
|
9
|
+
async run(args, io) {
|
|
10
|
+
const p = parseFlags(args, JSON_FLAG, 0);
|
|
11
|
+
const pkg = packageInfo();
|
|
12
|
+
const install = detectInstall();
|
|
13
|
+
const latest = availableUpdate(io.env);
|
|
14
|
+
const data = { name: pkg.name, version: pkg.version, node: process.versions.node, method: install.method, path: tildify(install.root, io.env), latest: latest ?? null };
|
|
15
|
+
if (bool(p, "json")) {
|
|
16
|
+
printJson(io.out, data);
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
19
|
+
io.out(`turno ${pkg.version}`);
|
|
20
|
+
io.out(` installed with ${install.method} at ${data.path}`);
|
|
21
|
+
io.out(` node ${data.node}`);
|
|
22
|
+
if (latest)
|
|
23
|
+
io.out(` ${latest} is available: turno upgrade`);
|
|
24
|
+
return 0;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { resolve as resolvePath } from "node:path";
|
|
3
|
+
import { loadConfig } from "../../core/config.js";
|
|
4
|
+
import { tildify } from "../../core/paths.js";
|
|
5
|
+
import { resolveProfile } from "../../core/resolve.js";
|
|
6
|
+
import { fail } from "../io.js";
|
|
7
|
+
export const command = {
|
|
8
|
+
summary: "which account auto would use in a folder, and why",
|
|
9
|
+
usage: "Usage: turno which [dir]\n\nExplains the profile resolution for a folder (default: the current one).",
|
|
10
|
+
async run(args, io) {
|
|
11
|
+
const dir = resolvePath(args[0] ?? process.cwd());
|
|
12
|
+
let isDir = false;
|
|
13
|
+
try {
|
|
14
|
+
isDir = statSync(dir).isDirectory();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
isDir = false;
|
|
18
|
+
}
|
|
19
|
+
if (!isDir)
|
|
20
|
+
fail(`no such folder: ${args[0] ?? dir}`);
|
|
21
|
+
const r = resolveProfile(dir, loadConfig(io.env), io.env);
|
|
22
|
+
io.out(tildify(dir, io.env));
|
|
23
|
+
io.out(` assigned: ${r.assigned || "none"} (${r.reason})`);
|
|
24
|
+
io.out(r.note ? ` using now: ${r.chosen} (${r.assigned} ${r.note})` : ` using now: ${r.chosen || "plain claude"}`);
|
|
25
|
+
return 0;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=which.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { fail } from "./io.js";
|
|
2
|
+
/** Usage text of a group: one line per visible subcommand, then an optional footer. */
|
|
3
|
+
export function groupUsage(name, subs, footer) {
|
|
4
|
+
const rows = Object.entries(subs)
|
|
5
|
+
.filter(([, s]) => !s.hidden)
|
|
6
|
+
.map(([n, s]) => [`turno ${name} ${n}${s.synopsis ? ` ${s.synopsis}` : ""}`, s.summary]);
|
|
7
|
+
const w = Math.min(60, Math.max(...rows.map(([l]) => l.length)));
|
|
8
|
+
const lines = rows.map(([l, s]) => (l.length > w ? ` ${l}\n ${" ".repeat(w)} ${s}` : ` ${l.padEnd(w)} ${s}`));
|
|
9
|
+
return `Usage:\n${lines.join("\n")}${footer ? `\n\n${footer}` : ""}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A command made of subcommands: `turno <name> <sub> [args]`. `fallback` runs when no
|
|
13
|
+
* subcommand is given (usually `list` or `show`). `--help` after a subcommand prints the group usage.
|
|
14
|
+
*/
|
|
15
|
+
export function group(name, summary, subs, opts = {}) {
|
|
16
|
+
const usage = groupUsage(name, subs, opts.footer);
|
|
17
|
+
const subcommands = Object.fromEntries(Object.entries(subs)
|
|
18
|
+
.filter(([, s]) => !s.hidden)
|
|
19
|
+
.map(([n, s]) => [n, { ...(s.args ? { args: s.args } : {}), ...(s.flags ? { flags: s.flags } : {}) }]));
|
|
20
|
+
return {
|
|
21
|
+
summary,
|
|
22
|
+
usage,
|
|
23
|
+
subcommands,
|
|
24
|
+
async run(args, io) {
|
|
25
|
+
let [sub, ...rest] = args;
|
|
26
|
+
if (sub === undefined || (sub.startsWith("-") && sub !== "-h" && sub !== "--help" && opts.fallback)) {
|
|
27
|
+
if (!opts.fallback) {
|
|
28
|
+
io.out(usage);
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
if (sub !== undefined)
|
|
32
|
+
rest = args;
|
|
33
|
+
sub = opts.fallback;
|
|
34
|
+
}
|
|
35
|
+
if (sub === "-h" || sub === "--help" || sub === "help") {
|
|
36
|
+
io.out(usage);
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
const s = subs[sub];
|
|
40
|
+
if (!s)
|
|
41
|
+
return fail(`unknown ${name} command '${sub}'\n\n${usage}`);
|
|
42
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
43
|
+
io.out(groupUsage(name, { [sub]: s }));
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
return s.run(rest, io);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=group.js.map
|
package/dist/cli/io.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Small helpers shared by command modules. Keep this file free of heavy imports. */
|
|
2
|
+
export class UsageError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "UsageError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function defaultIo() {
|
|
9
|
+
return {
|
|
10
|
+
out: (line = "") => process.stdout.write(`${line}\n`),
|
|
11
|
+
err: (line = "") => process.stderr.write(`${line}\n`),
|
|
12
|
+
env: process.env,
|
|
13
|
+
isTty: Boolean(process.stdout.isTTY),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export function paint(io, text, code) {
|
|
17
|
+
return io.isTty && !io.env.NO_COLOR ? `\x1b[${code}m${text}\x1b[0m` : text;
|
|
18
|
+
}
|
|
19
|
+
export function fail(message) {
|
|
20
|
+
throw new UsageError(message);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=io.js.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { accessSync, constants as fsConstants, statSync } from "node:fs";
|
|
3
|
+
import { constants } from "node:os";
|
|
4
|
+
import { delimiter, join } from "node:path";
|
|
5
|
+
import { isValidProfileName } from "../core/config.js";
|
|
6
|
+
import { paths } from "../core/paths.js";
|
|
7
|
+
import { UsageError } from "./io.js";
|
|
8
|
+
import { launcherName, profileAgent } from "./launchers.js";
|
|
9
|
+
function executable(file) {
|
|
10
|
+
try {
|
|
11
|
+
if (!statSync(file).isFile())
|
|
12
|
+
return false;
|
|
13
|
+
accessSync(file, fsConstants.X_OK);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Absolute path of a command found on PATH (or the command itself when it has a slash). */
|
|
21
|
+
export function findOnPath(cmd, env = process.env) {
|
|
22
|
+
if (cmd.includes("/"))
|
|
23
|
+
return executable(cmd) ? cmd : undefined;
|
|
24
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
25
|
+
if (!dir)
|
|
26
|
+
continue;
|
|
27
|
+
const file = join(dir, cmd);
|
|
28
|
+
if (executable(file))
|
|
29
|
+
return file;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
/** Path of a profile's launcher (claude-<name>, codex-<name>, ...) when it exists and is executable. */
|
|
34
|
+
export function launcherPath(name, env = process.env, agent = profileAgent(name, env)) {
|
|
35
|
+
if (!isValidProfileName(name))
|
|
36
|
+
return undefined;
|
|
37
|
+
const file = join(paths(env).bin, launcherName(name, agent));
|
|
38
|
+
return executable(file) ? file : undefined;
|
|
39
|
+
}
|
|
40
|
+
/** Terminal signals already reach the child (same process group); these are passed on. */
|
|
41
|
+
const FORWARDED = ["SIGTERM", "SIGHUP"];
|
|
42
|
+
const IGNORED = ["SIGINT", "SIGQUIT"];
|
|
43
|
+
/**
|
|
44
|
+
* Replace this process with `file args...` (the PID is kept, which run records rely on).
|
|
45
|
+
* Without `process.execve` (Node < 22.15) the program runs as a child with inherited stdio,
|
|
46
|
+
* and its exit code or signal is passed on.
|
|
47
|
+
*/
|
|
48
|
+
export async function execLauncher(file, args, env = process.env) {
|
|
49
|
+
if (typeof process.execve === "function") {
|
|
50
|
+
process.execve(file, [file, ...args], env);
|
|
51
|
+
}
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
const child = spawn(file, args, { stdio: "inherit", env });
|
|
54
|
+
const forward = (sig) => {
|
|
55
|
+
child.kill(sig);
|
|
56
|
+
};
|
|
57
|
+
const ignore = () => { };
|
|
58
|
+
for (const sig of FORWARDED)
|
|
59
|
+
process.on(sig, forward);
|
|
60
|
+
for (const sig of IGNORED)
|
|
61
|
+
process.on(sig, ignore);
|
|
62
|
+
const cleanup = () => {
|
|
63
|
+
for (const sig of FORWARDED)
|
|
64
|
+
process.off(sig, forward);
|
|
65
|
+
for (const sig of IGNORED)
|
|
66
|
+
process.off(sig, ignore);
|
|
67
|
+
};
|
|
68
|
+
child.on("error", (err) => {
|
|
69
|
+
cleanup();
|
|
70
|
+
reject(err);
|
|
71
|
+
});
|
|
72
|
+
child.on("exit", (code, signal) => {
|
|
73
|
+
cleanup();
|
|
74
|
+
if (signal) {
|
|
75
|
+
process.kill(process.pid, signal);
|
|
76
|
+
resolve(128 + (constants.signals[signal] ?? 1));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
resolve(code ?? 1);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/** Exec a command looked up on PATH, as the shell's `exec cmd "$@"` would. */
|
|
84
|
+
export async function execCommand(cmd, args, env = process.env) {
|
|
85
|
+
const file = findOnPath(cmd, env);
|
|
86
|
+
if (!file)
|
|
87
|
+
throw new UsageError(`${cmd}: command not found on PATH`);
|
|
88
|
+
return execLauncher(file, args, env);
|
|
89
|
+
}
|
|
90
|
+
/** `turno <profile> [args]`: exec the profile's launcher. */
|
|
91
|
+
export async function launchProfile(name, args, io) {
|
|
92
|
+
const file = launcherPath(name, io.env);
|
|
93
|
+
if (!file) {
|
|
94
|
+
throw new UsageError(`no profile '${name}' (turno profile list shows them; turno profile add ${name} creates it)`);
|
|
95
|
+
}
|
|
96
|
+
return execLauncher(file, args, io.env);
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=launch.js.map
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { accessSync, chmodSync, constants, existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { adapter, agentHub, agentProfileDir } from "../agents/index.js";
|
|
5
|
+
import { loadConfig } from "../core/config.js";
|
|
6
|
+
import { paths } from "../core/paths.js";
|
|
7
|
+
import { agentOf } from "../core/profiles.js";
|
|
8
|
+
import { UsageError } from "./io.js";
|
|
9
|
+
/** Every launcher turno writes carries this line; only marked files are ever overwritten. */
|
|
10
|
+
export const MARKER = "Generated by turno";
|
|
11
|
+
/** Markers of the bash claudio, of its predecessor, and of turno while it was called claudio. */
|
|
12
|
+
export const LEGACY_MARKERS = ["Generado por claudio", "Generado por claude-profile", "Generated by claudio"];
|
|
13
|
+
/** Helper launchers of the bash claudio, kept working (Orca runs claudio-auto) by pointing them at turno. */
|
|
14
|
+
const LEGACY_HELPERS = [
|
|
15
|
+
["claudio-auto", "auto"],
|
|
16
|
+
["claudio-open", "open"],
|
|
17
|
+
["claudio-navegador", "open"],
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Hub folders shared by every profile. Launchers recreate them, so a cleanup that removed
|
|
21
|
+
* an empty hub folder never leaves a profile with a dangling symlink.
|
|
22
|
+
*/
|
|
23
|
+
export const SHARED_DIRS = [
|
|
24
|
+
"projects",
|
|
25
|
+
"file-history",
|
|
26
|
+
"plans",
|
|
27
|
+
"tasks",
|
|
28
|
+
"paste-cache",
|
|
29
|
+
"session-env",
|
|
30
|
+
"uploads",
|
|
31
|
+
"dev-mods",
|
|
32
|
+
"sessions",
|
|
33
|
+
"skills",
|
|
34
|
+
"commands",
|
|
35
|
+
"agents",
|
|
36
|
+
"hooks",
|
|
37
|
+
"plugins",
|
|
38
|
+
"output-styles",
|
|
39
|
+
"workflows",
|
|
40
|
+
"agent-memory",
|
|
41
|
+
"rules",
|
|
42
|
+
"themes",
|
|
43
|
+
"state",
|
|
44
|
+
"usage-data",
|
|
45
|
+
"cache",
|
|
46
|
+
];
|
|
47
|
+
export function isGenerated(file) {
|
|
48
|
+
try {
|
|
49
|
+
const text = readFileSync(file, "utf8");
|
|
50
|
+
return [MARKER, ...LEGACY_MARKERS].some((m) => text.includes(m));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Double-quoted POSIX shell word; a path under HOME is written as "$HOME/...". */
|
|
57
|
+
export function shPath(p, env = process.env) {
|
|
58
|
+
const home = paths(env).home;
|
|
59
|
+
const esc = (s) => s.replace(/(["\\$`])/g, "\\$1");
|
|
60
|
+
if (p === home)
|
|
61
|
+
return '"$HOME"';
|
|
62
|
+
if (p.startsWith(home + "/"))
|
|
63
|
+
return `"$HOME/${esc(p.slice(home.length + 1))}"`;
|
|
64
|
+
return `"${esc(p)}"`;
|
|
65
|
+
}
|
|
66
|
+
/** The agent of a profile according to config.toml (Claude Code when the config is missing or broken). */
|
|
67
|
+
export function profileAgent(name, env = process.env) {
|
|
68
|
+
try {
|
|
69
|
+
return agentOf(name, loadConfig(env));
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return "claude";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** File name of a profile's launcher in ~/.local/bin: claude-<name>, codex-<name>, ... */
|
|
76
|
+
export function launcherName(name, agent = "claude") {
|
|
77
|
+
return `${adapter(agent).launcherPrefix}${name}`;
|
|
78
|
+
}
|
|
79
|
+
/** Text of the launcher of an agent other than Claude Code. */
|
|
80
|
+
function agentLauncherText(name, agent, env) {
|
|
81
|
+
const a = adapter(agent);
|
|
82
|
+
const p = paths(env);
|
|
83
|
+
const dir = shPath(agentProfileDir(name, agent, env), env);
|
|
84
|
+
const hubDirs = a.sharedDirs.map((d) => shPath(join(agentHub(agent, env), d), env));
|
|
85
|
+
const run = shPath(p.runDir, env);
|
|
86
|
+
const runFile = run.slice(0, -1) + '/$$"';
|
|
87
|
+
return [
|
|
88
|
+
"#!/bin/sh",
|
|
89
|
+
`# ${MARKER}: ${a.label} with the "${name}" account.`,
|
|
90
|
+
`export ${a.homeEnv}=${dir}`,
|
|
91
|
+
`export TURNO_PROFILE="${name}"`,
|
|
92
|
+
`export TURNO_AGENT="${agent}"`,
|
|
93
|
+
"export TURNO_SESSION_PID=$$",
|
|
94
|
+
...a.launcherEnv,
|
|
95
|
+
...(a.tokenFromEnv
|
|
96
|
+
? ["# The MCP gateway token, read by the agent from the environment (never written to its config).", `TURNO_MCPD_TOKEN="$(cat ${shPath(p.mcpdToken, env)} 2>/dev/null)"`, "export TURNO_MCPD_TOKEN"]
|
|
97
|
+
: []),
|
|
98
|
+
...(hubDirs.length ? ["# If a cleanup removed an empty hub folder, the profile symlink would dangle.", `mkdir -p ${hubDirs.join(" ")}`] : []),
|
|
99
|
+
`export BROWSER=${shPath(join(p.bin, "turno-open"), env)}`,
|
|
100
|
+
"# exec keeps the PID, so turno sessions knows which account each session uses.",
|
|
101
|
+
`mkdir -p ${run} && echo ${name} >${runFile} 2>/dev/null`,
|
|
102
|
+
`exec ${a.command} "$@"`,
|
|
103
|
+
"",
|
|
104
|
+
].join("\n");
|
|
105
|
+
}
|
|
106
|
+
/** Text of a profile's launcher: claude-<name> by default, or the launcher of `agent`. */
|
|
107
|
+
export function launcherText(name, env = process.env, agent = "claude") {
|
|
108
|
+
if (agent !== "claude")
|
|
109
|
+
return agentLauncherText(name, agent, env);
|
|
110
|
+
const p = paths(env);
|
|
111
|
+
const dirs = SHARED_DIRS.map((d) => shPath(join(p.hub, d), env)).join(" ");
|
|
112
|
+
const run = shPath(p.runDir, env);
|
|
113
|
+
const runFile = run.slice(0, -1) + '/$$"';
|
|
114
|
+
return [
|
|
115
|
+
"#!/bin/sh",
|
|
116
|
+
`# ${MARKER}: Claude Code with the "${name}" account, sharing ${shPath(p.hub, env).slice(1, -1)}.`,
|
|
117
|
+
`export CLAUDE_CONFIG_DIR="$HOME/.claude-${name}"`,
|
|
118
|
+
`export TURNO_PROFILE="${name}"`,
|
|
119
|
+
"export TURNO_SESSION_PID=$$",
|
|
120
|
+
"# If a cleanup removed an empty hub folder, the profile symlink would dangle.",
|
|
121
|
+
`mkdir -p ${dirs}`,
|
|
122
|
+
"# claude.ai links (artifacts, login) open with this account: turno open.",
|
|
123
|
+
`export BROWSER=${shPath(join(p.bin, "turno-open"), env)}`,
|
|
124
|
+
"# exec keeps the PID, so turno sessions knows which account each session uses.",
|
|
125
|
+
`mkdir -p ${run} && echo ${name} >${runFile} 2>/dev/null`,
|
|
126
|
+
'exec claude "$@"',
|
|
127
|
+
"",
|
|
128
|
+
].join("\n");
|
|
129
|
+
}
|
|
130
|
+
function writeScript(file, text) {
|
|
131
|
+
if (existsSync(file) && !isGenerated(file)) {
|
|
132
|
+
throw new UsageError(`${file} already exists and was not generated by turno; leaving it alone`);
|
|
133
|
+
}
|
|
134
|
+
mkdirSync(join(file, ".."), { recursive: true });
|
|
135
|
+
writeFileSync(file, text);
|
|
136
|
+
chmodSync(file, 0o755);
|
|
137
|
+
}
|
|
138
|
+
/** Write the profile's launcher (claude-<name>, codex-<name>, ...). Refuses to overwrite a file turno did not generate. */
|
|
139
|
+
export function writeLauncher(name, env = process.env, agent = profileAgent(name, env)) {
|
|
140
|
+
const file = join(paths(env).bin, launcherName(name, agent));
|
|
141
|
+
writeScript(file, launcherText(name, env, agent));
|
|
142
|
+
return file;
|
|
143
|
+
}
|
|
144
|
+
function sameFile(a, b) {
|
|
145
|
+
try {
|
|
146
|
+
return realpathSync(a) === realpathSync(b);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Shell words that run this turno: the real path of the running CLI entry when it is
|
|
154
|
+
* this package's main module, else `turno` from PATH.
|
|
155
|
+
*/
|
|
156
|
+
export function turnoCommand(argv1 = process.argv[1]) {
|
|
157
|
+
const main = fileURLToPath(new URL("./main.js", import.meta.url));
|
|
158
|
+
if (!argv1 || !sameFile(argv1, main))
|
|
159
|
+
return "turno";
|
|
160
|
+
const real = realpathSync(argv1);
|
|
161
|
+
const q = (s) => `"${s.replace(/(["\\$`])/g, "\\$1")}"`;
|
|
162
|
+
try {
|
|
163
|
+
accessSync(real, constants.X_OK);
|
|
164
|
+
return q(real);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return `${q(process.execPath)} ${q(real)}`;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function helperText(sub, why, turno) {
|
|
171
|
+
return ["#!/bin/sh", `# ${MARKER}: ${why}`, `exec ${turno} ${sub} "$@"`, ""].join("\n");
|
|
172
|
+
}
|
|
173
|
+
export function autoLauncherText(turno = turnoCommand()) {
|
|
174
|
+
return helperText("auto", "Claude Code with the account assigned to the project (Orca uses it as its Claude command).", turno);
|
|
175
|
+
}
|
|
176
|
+
export function openLauncherText(turno = turnoCommand()) {
|
|
177
|
+
return helperText("open", "Claude Code uses it as $BROWSER (turno open).", turno);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Write turno-auto and turno-open. The claudio-auto and claudio-navegador left by the bash
|
|
181
|
+
* tool are rewritten to run turno, so Orca and sessions started before the switch keep working.
|
|
182
|
+
* Returns the names written.
|
|
183
|
+
*/
|
|
184
|
+
export function writeHelperLaunchers(env = process.env, turno = turnoCommand()) {
|
|
185
|
+
const bin = paths(env).bin;
|
|
186
|
+
const written = [];
|
|
187
|
+
writeScript(join(bin, "turno-auto"), autoLauncherText(turno));
|
|
188
|
+
written.push("turno-auto");
|
|
189
|
+
writeScript(join(bin, "turno-open"), openLauncherText(turno));
|
|
190
|
+
written.push("turno-open");
|
|
191
|
+
for (const [name, sub] of LEGACY_HELPERS) {
|
|
192
|
+
const legacy = join(bin, name);
|
|
193
|
+
if (!existsSync(legacy) || !isGenerated(legacy))
|
|
194
|
+
continue;
|
|
195
|
+
writeScript(legacy, sub === "auto" ? autoLauncherText(turno) : openLauncherText(turno));
|
|
196
|
+
written.push(name);
|
|
197
|
+
}
|
|
198
|
+
return written;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=launchers.js.map
|