turno-cli 0.0.0-stage → 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/CHANGELOG.md +21 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +334 -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 +103 -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,40 @@
|
|
|
1
|
+
import { isValidProfileName } from "../../core/config.js";
|
|
2
|
+
import { markExhausted } from "../../core/exhausted.js";
|
|
3
|
+
import { formatUntil } from "../../core/resolve.js";
|
|
4
|
+
import { fail } from "../io.js";
|
|
5
|
+
import { launcherPath } from "../launch.js";
|
|
6
|
+
const USAGE = 'Usage: turno exhausted <profile> [until "YYYY-MM-DD HH:MM"]';
|
|
7
|
+
/** Local "YYYY-MM-DD HH:MM" to epoch seconds. */
|
|
8
|
+
export function parseLocalDateTime(text) {
|
|
9
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{1,2}):(\d{2})$/.exec(text.trim());
|
|
10
|
+
if (!m)
|
|
11
|
+
return undefined;
|
|
12
|
+
const [y, mo, d, h, mi] = m.slice(1).map(Number);
|
|
13
|
+
const dt = new Date(y, mo - 1, d, h, mi, 0, 0);
|
|
14
|
+
if (dt.getFullYear() !== y || dt.getMonth() !== mo - 1 || dt.getDate() !== d || dt.getHours() !== h)
|
|
15
|
+
return undefined;
|
|
16
|
+
return Math.floor(dt.getTime() / 1000);
|
|
17
|
+
}
|
|
18
|
+
export const command = {
|
|
19
|
+
summary: "send a profile's projects to the fallback (default for 5 h)",
|
|
20
|
+
usage: `${USAGE}\n\nMarks the profile exhausted until the given local time (default: now + 5 h).`,
|
|
21
|
+
async run(args, io) {
|
|
22
|
+
const name = args[0];
|
|
23
|
+
if (!name || !isValidProfileName(name) || !launcherPath(name, io.env))
|
|
24
|
+
fail(USAGE.replace("Usage: ", "usage: "));
|
|
25
|
+
let until;
|
|
26
|
+
if (args[1] === "until" && args[2]) {
|
|
27
|
+
const t = parseLocalDateTime(args[2]);
|
|
28
|
+
if (t === undefined)
|
|
29
|
+
fail(`invalid date: ${args[2]} (format YYYY-MM-DD HH:MM)`);
|
|
30
|
+
until = t;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
until = Math.floor(Date.now() / 1000) + 5 * 3600;
|
|
34
|
+
}
|
|
35
|
+
markExhausted(name, until, io.env);
|
|
36
|
+
io.out(`${name} exhausted until ${formatUntil(until)}; its projects open with the fallback, if one is set`);
|
|
37
|
+
return 0;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=exhausted.js.map
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { loadConfig } from "../../core/config.js";
|
|
4
|
+
import { paths, profileDir } from "../../core/paths.js";
|
|
5
|
+
import { listProfiles, profileFromConfigDir } from "../../core/profiles.js";
|
|
6
|
+
import { fail } from "../io.js";
|
|
7
|
+
const USAGE = `Usage:
|
|
8
|
+
turno handoff <profile|auto> [--fork] [--copy]
|
|
9
|
+
|
|
10
|
+
Prints the command that continues THIS Claude Code session under another profile:
|
|
11
|
+
default resume it there (exit this session first)
|
|
12
|
+
--fork start a new session with the conversation so far; this one keeps running
|
|
13
|
+
--copy also put the command on the clipboard
|
|
14
|
+
|
|
15
|
+
Run it from inside a Claude Code session (it reads CLAUDE_CODE_SESSION_ID). The command starts
|
|
16
|
+
with a cd to the folder the session began in, because claude --resume looks sessions up by
|
|
17
|
+
the project folder it is launched from.`;
|
|
18
|
+
function shellQuote(s) {
|
|
19
|
+
return /^[A-Za-z0-9_./:@%+=-]+$/.test(s) ? s : `'${s.replace(/'/g, "'\\''")}'`;
|
|
20
|
+
}
|
|
21
|
+
/** The first `cwd` recorded in a transcript: where the session started. */
|
|
22
|
+
export function transcriptCwd(file) {
|
|
23
|
+
const text = readFileSync(file, "utf8");
|
|
24
|
+
for (const line of text.split("\n")) {
|
|
25
|
+
if (!line.includes('"cwd"'))
|
|
26
|
+
continue;
|
|
27
|
+
try {
|
|
28
|
+
const entry = JSON.parse(line);
|
|
29
|
+
if (entry.cwd)
|
|
30
|
+
return entry.cwd;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
/* partial or foreign line */
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
export function findTranscript(configDir, sessionId) {
|
|
39
|
+
const projects = join(configDir, "projects");
|
|
40
|
+
if (!existsSync(projects))
|
|
41
|
+
return undefined;
|
|
42
|
+
for (const dir of readdirSync(projects)) {
|
|
43
|
+
const f = join(projects, dir, `${sessionId}.jsonl`);
|
|
44
|
+
if (existsSync(f))
|
|
45
|
+
return f;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
function real(p) {
|
|
50
|
+
try {
|
|
51
|
+
return realpathSync(p);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return p;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function buildHandoff(target, fork, env) {
|
|
58
|
+
const sid = env.CLAUDE_CODE_SESSION_ID;
|
|
59
|
+
if (!sid)
|
|
60
|
+
fail("CLAUDE_CODE_SESSION_ID is empty; run this from inside a Claude Code session");
|
|
61
|
+
const config = loadConfig(env);
|
|
62
|
+
// A Claude Code session continues only under another Claude Code profile.
|
|
63
|
+
const profiles = listProfiles(config, env, "claude");
|
|
64
|
+
if (target !== "auto" && !profiles.includes(target))
|
|
65
|
+
fail(`no profile '${target}'; available: ${[...profiles, "auto"].join(", ")}`);
|
|
66
|
+
const cfg = env.CLAUDE_CONFIG_DIR || paths(env).hub;
|
|
67
|
+
const transcript = findTranscript(cfg, sid);
|
|
68
|
+
if (!transcript)
|
|
69
|
+
fail(`cannot find ${sid}.jsonl under ${join(cfg, "projects")}`);
|
|
70
|
+
const cwd = transcriptCwd(transcript);
|
|
71
|
+
if (!cwd)
|
|
72
|
+
fail(`${transcript} does not say which folder the session started in`);
|
|
73
|
+
const warnings = [];
|
|
74
|
+
const current = profileFromConfigDir(env.CLAUDE_CONFIG_DIR, config, env);
|
|
75
|
+
if (!fork && target === current)
|
|
76
|
+
warnings.push(`this session already runs as '${target}'`);
|
|
77
|
+
if (target !== "auto" && real(join(cfg, "projects")) !== real(join(profileDir(target, env), "projects"))) {
|
|
78
|
+
warnings.push(`${target} does not share the projects folder with this profile, so it will not see this session (turno setup ${target})`);
|
|
79
|
+
}
|
|
80
|
+
const command = `cd ${shellQuote(cwd)} && turno ${target} --resume ${sid}${fork ? " --fork-session" : ""}`;
|
|
81
|
+
return { command, warnings };
|
|
82
|
+
}
|
|
83
|
+
async function copy(text) {
|
|
84
|
+
const { spawn } = await import("node:child_process");
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
try {
|
|
87
|
+
const p = spawn("pbcopy", [], { stdio: ["pipe", "ignore", "ignore"] });
|
|
88
|
+
p.on("error", () => resolve(false));
|
|
89
|
+
p.on("close", (code) => resolve(code === 0));
|
|
90
|
+
p.stdin.end(text);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
resolve(false);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
export const command = {
|
|
98
|
+
summary: "print the command to continue or fork this session under another profile",
|
|
99
|
+
usage: USAGE,
|
|
100
|
+
async run(args, io) {
|
|
101
|
+
const target = args.find((a) => !a.startsWith("--"));
|
|
102
|
+
if (!target) {
|
|
103
|
+
const config = loadConfig(io.env);
|
|
104
|
+
io.out(`profiles: ${[...listProfiles(config, io.env, "claude"), "auto"].join(" ")}`);
|
|
105
|
+
io.out(`current: ${profileFromConfigDir(io.env.CLAUDE_CONFIG_DIR, config, io.env) ?? "(none)"}`);
|
|
106
|
+
io.err(USAGE);
|
|
107
|
+
return 2;
|
|
108
|
+
}
|
|
109
|
+
const h = buildHandoff(target, args.includes("--fork"), io.env);
|
|
110
|
+
for (const w of h.warnings)
|
|
111
|
+
io.err(`warning: ${w}`);
|
|
112
|
+
io.out(h.command);
|
|
113
|
+
if (args.includes("--copy") && !(await copy(`${h.command}\n`)))
|
|
114
|
+
io.err("warning: could not copy to the clipboard");
|
|
115
|
+
return 0;
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=handoff.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isValidProfileName, loadConfig } from "../../core/config.js";
|
|
2
|
+
import { markExhausted } from "../../core/exhausted.js";
|
|
3
|
+
import { profileFromConfigDir } from "../../core/profiles.js";
|
|
4
|
+
import { isUsageLimit, resetTime } from "../../core/ratelimit.js";
|
|
5
|
+
import { fail } from "../io.js";
|
|
6
|
+
/**
|
|
7
|
+
* StopFailure hook (matcher rate_limit). When the failure is a real usage limit, mark the
|
|
8
|
+
* session's profile exhausted until the limit resets, so new sessions of its projects open
|
|
9
|
+
* with the fallback. Returns the profile and time marked, or undefined when nothing was done.
|
|
10
|
+
*/
|
|
11
|
+
export function handleRateLimit(input, env = process.env, now = new Date()) {
|
|
12
|
+
let payload;
|
|
13
|
+
try {
|
|
14
|
+
payload = JSON.parse(input || "{}");
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (!payload || typeof payload !== "object" || !isUsageLimit(payload))
|
|
20
|
+
return undefined; // a passing 429
|
|
21
|
+
let profile;
|
|
22
|
+
try {
|
|
23
|
+
profile = profileFromConfigDir(env.CLAUDE_CONFIG_DIR, loadConfig(env), env);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
if (!profile || !isValidProfileName(profile))
|
|
29
|
+
return undefined;
|
|
30
|
+
const until = resetTime(String(payload.error ?? ""), now);
|
|
31
|
+
markExhausted(profile, until, env);
|
|
32
|
+
return { profile, until: Math.floor(until) };
|
|
33
|
+
}
|
|
34
|
+
async function readStdin() {
|
|
35
|
+
if (process.stdin.isTTY)
|
|
36
|
+
return "";
|
|
37
|
+
const chunks = [];
|
|
38
|
+
for await (const chunk of process.stdin)
|
|
39
|
+
chunks.push(Buffer.from(chunk));
|
|
40
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
41
|
+
}
|
|
42
|
+
export const command = {
|
|
43
|
+
summary: "Claude Code hooks (rate-limit: mark the account exhausted)",
|
|
44
|
+
usage: [
|
|
45
|
+
"Usage: turno hook rate-limit",
|
|
46
|
+
"",
|
|
47
|
+
"StopFailure hook with matcher rate_limit. Reads the hook payload on stdin and, for a",
|
|
48
|
+
"usage limit, marks the session's profile exhausted until the limit resets.",
|
|
49
|
+
].join("\n"),
|
|
50
|
+
hidden: true,
|
|
51
|
+
async run(args, io) {
|
|
52
|
+
if (args[0] !== "rate-limit")
|
|
53
|
+
fail("usage: turno hook rate-limit");
|
|
54
|
+
try {
|
|
55
|
+
handleRateLimit(await readStdin(), io.env);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// a hook must never fail the session
|
|
59
|
+
}
|
|
60
|
+
return 0;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=hook.js.map
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { loadRegistry } from "../../accounts/registry.js";
|
|
2
|
+
import { tildify } from "../../core/paths.js";
|
|
3
|
+
import { JSON_FLAG, bool, parseFlags, printJson } from "../args.js";
|
|
4
|
+
import { group } from "../group.js";
|
|
5
|
+
import { fail, paint } from "../io.js";
|
|
6
|
+
import * as accounts from "./accounts.js";
|
|
7
|
+
import { dispatch, loadBrowser } from "./browser.js";
|
|
8
|
+
/**
|
|
9
|
+
* Who acts on an outside service for a session: a service account (`linear:work`, run by a
|
|
10
|
+
* profile through the accounts bridge) or a browser identity (`browser:school`, a Chrome
|
|
11
|
+
* profile with saved logins). One command for both; `accounts` and `browser` still work.
|
|
12
|
+
*/
|
|
13
|
+
const BROWSER = "browser:";
|
|
14
|
+
async function browserModule(io) {
|
|
15
|
+
const mod = await loadBrowser();
|
|
16
|
+
if (!mod) {
|
|
17
|
+
io.err("turno: the browser package (turno-browser) is not installed; browser identities need it");
|
|
18
|
+
return fail("browser identities are not available");
|
|
19
|
+
}
|
|
20
|
+
return mod;
|
|
21
|
+
}
|
|
22
|
+
async function onBrowser(mod, sub, args, io) {
|
|
23
|
+
try {
|
|
24
|
+
return await dispatch(mod, sub, args, io);
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (err.name === "IdentityError")
|
|
28
|
+
return fail(err.message);
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function list(args, io) {
|
|
33
|
+
const p = parseFlags(args, JSON_FLAG, 0);
|
|
34
|
+
const reg = loadRegistry(io.env);
|
|
35
|
+
const mod = await loadBrowser();
|
|
36
|
+
const browsers = mod ? mod.listIdentities(io.env) : [];
|
|
37
|
+
if (bool(p, "json")) {
|
|
38
|
+
printJson(io.out, {
|
|
39
|
+
services: Object.values(reg.accounts)
|
|
40
|
+
.sort((a, b) => a.id.localeCompare(b.id))
|
|
41
|
+
.map((a) => ({ id: a.id, service: a.service, profile: a.profile, tools: a.tools, description: a.description ?? null })),
|
|
42
|
+
browser: browsers.map((b) => ({ id: `${BROWSER}${b.id}`, inUse: b.inUse, dir: tildify(b.dir, io.env) })),
|
|
43
|
+
preferences: reg.prefer.map((r) => ({ path: tildify(r.path, io.env), accounts: r.accounts })),
|
|
44
|
+
});
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
io.out(paint(io, "Service accounts", "1"));
|
|
48
|
+
accounts.list(io);
|
|
49
|
+
io.out("");
|
|
50
|
+
io.out(paint(io, "Browser identities", "1"));
|
|
51
|
+
if (!mod)
|
|
52
|
+
io.out(" (the turno-browser package is not installed)");
|
|
53
|
+
else if (!browsers.length)
|
|
54
|
+
io.out(" none yet (turno identity add browser:<id>)");
|
|
55
|
+
else {
|
|
56
|
+
const w = Math.max(...browsers.map((b) => b.id.length)) + BROWSER.length;
|
|
57
|
+
for (const b of browsers) {
|
|
58
|
+
const state = b.inUse ? paint(io, "in use", "33") : paint(io, "free ", "32");
|
|
59
|
+
io.out(` ${`${BROWSER}${b.id}`.padEnd(w)} ${state} ${tildify(b.dir, io.env)}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
async function add(args, io) {
|
|
65
|
+
const id = args.find((a) => !a.startsWith("-"));
|
|
66
|
+
if (id?.startsWith(BROWSER)) {
|
|
67
|
+
if (args.includes("--dry-run")) {
|
|
68
|
+
io.out(`(dry run) would create browser identity "${id.slice(BROWSER.length)}"`);
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
return onBrowser(await browserModule(io), "add", [id.slice(BROWSER.length)], io);
|
|
72
|
+
}
|
|
73
|
+
return accounts.add(args, io);
|
|
74
|
+
}
|
|
75
|
+
async function remove(args, io) {
|
|
76
|
+
const id = args.find((a) => !a.startsWith("-"));
|
|
77
|
+
if (id?.startsWith(BROWSER)) {
|
|
78
|
+
if (args.includes("--dry-run")) {
|
|
79
|
+
io.out(`(dry run) would delete browser identity "${id.slice(BROWSER.length)}" and its saved logins`);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
return onBrowser(await browserModule(io), "remove", [id.slice(BROWSER.length)], io);
|
|
83
|
+
}
|
|
84
|
+
return accounts.remove(args, io);
|
|
85
|
+
}
|
|
86
|
+
async function login(args, io) {
|
|
87
|
+
const [id, ...rest] = args;
|
|
88
|
+
if (!id)
|
|
89
|
+
return fail("usage: turno identity login browser:<id> [--url <url>]");
|
|
90
|
+
if (!id.startsWith(BROWSER))
|
|
91
|
+
fail(`only browser identities sign in by hand; service accounts sign in through their profile (claude-<profile>)`);
|
|
92
|
+
return onBrowser(await browserModule(io), "login", [id.slice(BROWSER.length), ...rest], io);
|
|
93
|
+
}
|
|
94
|
+
export const command = group("identity", "who acts on outside services: service accounts (linear:work) and browser identities (browser:school)", {
|
|
95
|
+
list: { summary: "service accounts, browser identities and folder preferences", synopsis: "[--json]", flags: ["--json"], run: list },
|
|
96
|
+
add: {
|
|
97
|
+
summary: "add a service account, or create a browser identity (browser:<id>)",
|
|
98
|
+
synopsis: "<service:label> --profile <p> --tools <pattern>[,...] | browser:<id> [--dry-run]",
|
|
99
|
+
args: ["any"],
|
|
100
|
+
flags: ["--profile", "--tools", "--service", "--description", "--dry-run"],
|
|
101
|
+
run: add,
|
|
102
|
+
},
|
|
103
|
+
remove: { summary: "remove a service account, or delete a browser identity and its logins", synopsis: "<id> [--dry-run]", args: ["identity"], flags: ["--dry-run"], run: remove },
|
|
104
|
+
login: { summary: "open a browser identity's Chrome to sign in by hand once", synopsis: "browser:<id> [--url <url>]", args: ["identity"], flags: ["--url"], run: login },
|
|
105
|
+
prefer: {
|
|
106
|
+
summary: "pick the account of a service for a folder (empty account removes it)",
|
|
107
|
+
synopsis: "<dir> <service>=<account> [--dry-run]",
|
|
108
|
+
args: ["dir"],
|
|
109
|
+
flags: ["--dry-run"],
|
|
110
|
+
run: async (a, io) => accounts.prefer(a, io),
|
|
111
|
+
},
|
|
112
|
+
log: { summary: "recent calls to the accounts bridge", synopsis: "[-n N]", flags: ["--tail"], run: async (a, io) => accounts.log(a, io) },
|
|
113
|
+
}, { fallback: "list" });
|
|
114
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { loadConfig } from "../../core/config.js";
|
|
2
|
+
import { clearExhausted, exhaustedUntil, markExhausted } from "../../core/exhausted.js";
|
|
3
|
+
import { agentOf, listProfiles } from "../../core/profiles.js";
|
|
4
|
+
import { formatUntil } from "../../core/resolve.js";
|
|
5
|
+
import { DRY_RUN, JSON_FLAG, WATCH, bool, parseFlags, printJson, str } from "../args.js";
|
|
6
|
+
import { group } from "../group.js";
|
|
7
|
+
import { fail } from "../io.js";
|
|
8
|
+
import { launcherPath } from "../launch.js";
|
|
9
|
+
import { parseLocalDateTime } from "./exhausted.js";
|
|
10
|
+
import { command as usage, account, usageSnapshot } from "./usage.js";
|
|
11
|
+
/** "5h", "90m", "2h30m" to seconds. */
|
|
12
|
+
export function parseDuration(text) {
|
|
13
|
+
const m = /^(?:(\d+)h)?(?:(\d+)m)?$/.exec(text.trim());
|
|
14
|
+
if (!m || (!m[1] && !m[2]))
|
|
15
|
+
return undefined;
|
|
16
|
+
return Number(m[1] ?? 0) * 3600 + Number(m[2] ?? 0) * 60;
|
|
17
|
+
}
|
|
18
|
+
/** Usage of every profile as data, for --json and turno status. */
|
|
19
|
+
export async function limitsData(env, withAccounts = true) {
|
|
20
|
+
const config = loadConfig(env);
|
|
21
|
+
const profiles = listProfiles(config, env);
|
|
22
|
+
// Usage windows and accounts come from Claude Code; other agents show only exhausted marks.
|
|
23
|
+
const accounts = withAccounts ? await Promise.all(profiles.map((p) => (agentOf(p, config) === "claude" ? account(p, env) : undefined))) : [];
|
|
24
|
+
return profiles.map((p, i) => {
|
|
25
|
+
const snap = usageSnapshot(p, env);
|
|
26
|
+
const limits = snap.rate_limits ?? {};
|
|
27
|
+
const win = (w) => w ? { used: Number(w.used_percentage) || 0, resetsAt: typeof w.resets_at === "number" ? w.resets_at : null } : null;
|
|
28
|
+
return {
|
|
29
|
+
profile: p,
|
|
30
|
+
agent: agentOf(p, config),
|
|
31
|
+
...(withAccounts ? { email: accounts[i]?.email ?? null, plan: accounts[i]?.plan ?? null } : {}),
|
|
32
|
+
exhaustedUntil: exhaustedUntil(p, env) ?? null,
|
|
33
|
+
fiveHour: win(limits.five_hour),
|
|
34
|
+
week: win(limits.seven_day),
|
|
35
|
+
updatedAt: snap.updated_at ?? null,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async function show(args, io) {
|
|
40
|
+
const p = parseFlags(args, { ...JSON_FLAG, ...WATCH }, 0);
|
|
41
|
+
if (bool(p, "json")) {
|
|
42
|
+
if (bool(p, "watch"))
|
|
43
|
+
fail("--json and --watch do not go together");
|
|
44
|
+
printJson(io.out, await limitsData(io.env));
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
return usage.run(bool(p, "watch") ? ["--watch"] : [], io);
|
|
48
|
+
}
|
|
49
|
+
function profileArg(name, io, verb) {
|
|
50
|
+
if (!name)
|
|
51
|
+
return fail(`usage: turno limit ${verb} <profile>`);
|
|
52
|
+
if (!launcherPath(name, io.env))
|
|
53
|
+
fail(`no profile '${name}' (turno profile list shows them)`);
|
|
54
|
+
return name;
|
|
55
|
+
}
|
|
56
|
+
async function set(args, io) {
|
|
57
|
+
// The bash form `exhausted <profile> until "YYYY-MM-DD HH:MM"` still works.
|
|
58
|
+
const legacy = args[1] === "until" || args[1] === "hasta";
|
|
59
|
+
const p = parseFlags(legacy ? [args[0], "--until", ...args.slice(2)] : args, { until: { type: "string" }, for: { type: "string" }, ...DRY_RUN }, 1);
|
|
60
|
+
const name = profileArg(p.positionals[0], io, "set");
|
|
61
|
+
const untilText = str(p, "until");
|
|
62
|
+
const forText = str(p, "for");
|
|
63
|
+
if (untilText && forText)
|
|
64
|
+
fail("use --until or --for, not both");
|
|
65
|
+
let until = Math.floor(Date.now() / 1000) + 5 * 3600;
|
|
66
|
+
if (untilText) {
|
|
67
|
+
const t = parseLocalDateTime(untilText);
|
|
68
|
+
if (t === undefined)
|
|
69
|
+
fail(`invalid date: ${untilText} (format YYYY-MM-DD HH:MM)`);
|
|
70
|
+
until = t;
|
|
71
|
+
}
|
|
72
|
+
else if (forText) {
|
|
73
|
+
const d = parseDuration(forText);
|
|
74
|
+
if (d === undefined)
|
|
75
|
+
fail(`invalid duration: ${forText} (like 5h, 90m or 2h30m)`);
|
|
76
|
+
until = Math.floor(Date.now() / 1000) + d;
|
|
77
|
+
}
|
|
78
|
+
if (!bool(p, "dry-run"))
|
|
79
|
+
markExhausted(name, until, io.env);
|
|
80
|
+
io.out(`${name} exhausted until ${formatUntil(until)}; its projects open with the fallback, if one is set${bool(p, "dry-run") ? " (dry run)" : ""}`);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
83
|
+
async function clear(args, io) {
|
|
84
|
+
const p = parseFlags(args, DRY_RUN, 1);
|
|
85
|
+
const name = profileArg(p.positionals[0], io, "clear");
|
|
86
|
+
if (!bool(p, "dry-run"))
|
|
87
|
+
clearExhausted(name, io.env);
|
|
88
|
+
io.out(`${name} available again${bool(p, "dry-run") ? " (dry run)" : ""}`);
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
export const command = group("limit", "usage limits of each account, and the exhausted marks that move projects to the fallback", {
|
|
92
|
+
show: { summary: "5 h and weekly usage of each account, as bars", synopsis: "[--watch] [--json]", flags: ["--watch", "--json"], run: show },
|
|
93
|
+
set: {
|
|
94
|
+
summary: "mark a profile exhausted (default for 5 h); its projects open with the fallback, if one is set",
|
|
95
|
+
synopsis: '<profile> [--until "YYYY-MM-DD HH:MM" | --for 2h] [--dry-run]',
|
|
96
|
+
args: ["profile"],
|
|
97
|
+
flags: ["--until", "--for", "--dry-run"],
|
|
98
|
+
run: set,
|
|
99
|
+
},
|
|
100
|
+
clear: { summary: "clear a profile's exhausted mark", synopsis: "<profile> [--dry-run]", args: ["profile"], flags: ["--dry-run"], run: clear },
|
|
101
|
+
}, { fallback: "show" });
|
|
102
|
+
//# sourceMappingURL=limit.js.map
|