turno-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.
Files changed (105) hide show
  1. package/CHANGELOG.md +293 -0
  2. package/README.md +143 -75
  3. package/dist/accounts/audit.js +137 -7
  4. package/dist/accounts/builtin.js +68 -21
  5. package/dist/accounts/delegate.js +176 -35
  6. package/dist/accounts/policy.js +77 -0
  7. package/dist/accounts/registry.js +37 -21
  8. package/dist/agents/adapter.js +7 -0
  9. package/dist/agents/claude.js +195 -0
  10. package/dist/agents/codex.js +98 -0
  11. package/dist/agents/copilot.js +39 -0
  12. package/dist/agents/cursor.js +41 -0
  13. package/dist/agents/index.js +26 -86
  14. package/dist/agents/snippets.js +7 -4
  15. package/dist/cli/args.js +19 -4
  16. package/dist/cli/commands/accounts.js +96 -51
  17. package/dist/cli/commands/auto.js +82 -32
  18. package/dist/cli/commands/browser.js +216 -135
  19. package/dist/cli/commands/complete.js +2 -1
  20. package/dist/cli/commands/completion.js +4 -2
  21. package/dist/cli/commands/config.js +103 -18
  22. package/dist/cli/commands/demo.js +292 -0
  23. package/dist/cli/commands/doctor.js +387 -119
  24. package/dist/cli/commands/handoff.js +33 -22
  25. package/dist/cli/commands/identity.js +101 -50
  26. package/dist/cli/commands/init.js +616 -0
  27. package/dist/cli/commands/limit.js +25 -13
  28. package/dist/cli/commands/mcp-oauth.js +194 -0
  29. package/dist/cli/commands/mcp-observe.js +434 -0
  30. package/dist/cli/commands/mcp.js +653 -185
  31. package/dist/cli/commands/mcpd.js +323 -75
  32. package/dist/cli/commands/migrate.js +6 -4
  33. package/dist/cli/commands/open.js +92 -27
  34. package/dist/cli/commands/profile.js +205 -69
  35. package/dist/cli/commands/resolve.js +6 -2
  36. package/dist/cli/commands/run.js +15 -5
  37. package/dist/cli/commands/secret.js +61 -42
  38. package/dist/cli/commands/session.js +2 -2
  39. package/dist/cli/commands/sessions.js +42 -12
  40. package/dist/cli/commands/setup.js +136 -158
  41. package/dist/cli/commands/status.js +172 -41
  42. package/dist/cli/commands/statusline.js +305 -113
  43. package/dist/cli/commands/ui.js +64 -66
  44. package/dist/cli/commands/undo.js +161 -18
  45. package/dist/cli/commands/uninstall.js +462 -0
  46. package/dist/cli/commands/upgrade.js +75 -19
  47. package/dist/cli/commands/usage.js +117 -44
  48. package/dist/cli/commands/version.js +7 -5
  49. package/dist/cli/commands/which.js +7 -24
  50. package/dist/cli/compile-cache.js +32 -0
  51. package/dist/cli/group.js +10 -10
  52. package/dist/cli/help.js +57 -0
  53. package/dist/cli/highlight.js +236 -0
  54. package/dist/cli/io.js +170 -1
  55. package/dist/cli/launch.js +21 -37
  56. package/dist/cli/launchers.js +145 -94
  57. package/dist/cli/main.js +163 -27
  58. package/dist/cli/picker.js +447 -0
  59. package/dist/cli/registry.js +42 -5
  60. package/dist/cli/security.js +539 -0
  61. package/dist/cli/style.js +281 -0
  62. package/dist/core/config.js +145 -23
  63. package/dist/core/exhausted.js +6 -8
  64. package/dist/core/fsutil.js +47 -0
  65. package/dist/core/journal.js +281 -11
  66. package/dist/core/manifest.js +126 -6
  67. package/dist/core/paths.js +114 -7
  68. package/dist/core/profiles.js +139 -9
  69. package/dist/core/resolve.js +129 -23
  70. package/dist/core/secrets.js +215 -20
  71. package/dist/core/sessions.js +13 -20
  72. package/dist/core/update.js +18 -8
  73. package/dist/core/version.js +6 -0
  74. package/dist/demo/server.js +34 -0
  75. package/dist/mcpd/cache.js +18 -4
  76. package/dist/mcpd/calllog.js +253 -0
  77. package/dist/mcpd/client-names.js +51 -0
  78. package/dist/mcpd/connectors.js +148 -17
  79. package/dist/mcpd/daemon.js +452 -116
  80. package/dist/mcpd/folder-sets.js +39 -0
  81. package/dist/mcpd/gateway.js +441 -47
  82. package/dist/mcpd/health.js +269 -0
  83. package/dist/mcpd/importer.js +356 -36
  84. package/dist/mcpd/instances.js +425 -65
  85. package/dist/mcpd/jsonrpc.js +8 -0
  86. package/dist/mcpd/launchd.js +65 -48
  87. package/dist/mcpd/modern.js +648 -0
  88. package/dist/mcpd/oauth.js +791 -0
  89. package/dist/mcpd/peerpid.js +122 -6
  90. package/dist/mcpd/pressure.js +85 -0
  91. package/dist/mcpd/rss.js +151 -0
  92. package/dist/mcpd/service.js +167 -0
  93. package/dist/mcpd/state.js +183 -0
  94. package/dist/mcpd/sync.js +55 -80
  95. package/dist/mcpd/systemd.js +80 -0
  96. package/dist/mcpd/tokens.js +89 -0
  97. package/dist/mcpd/upstream.js +154 -44
  98. package/dist/statusline/cost.js +154 -44
  99. package/dist/statusline/input.js +66 -0
  100. package/dist/statusline/render.js +9 -3
  101. package/dist/ui/fonts.js +8 -3
  102. package/dist/ui/settings.js +27 -15
  103. package/npm-shrinkwrap.json +1093 -0
  104. package/package.json +9 -9
  105. package/dist/cli/commands/available.js +0 -18
@@ -1,8 +1,9 @@
1
+ import { adapter } from "../../agents/index.js";
1
2
  import { loadConfig } from "../../core/config.js";
2
3
  import { clearExhausted, exhaustedUntil, markExhausted } from "../../core/exhausted.js";
3
4
  import { agentOf, listProfiles } from "../../core/profiles.js";
4
5
  import { formatUntil } from "../../core/resolve.js";
5
- import { DRY_RUN, JSON_FLAG, WATCH, bool, parseFlags, printJson, str } from "../args.js";
6
+ import { DRY_RUN, JSON_FLAG, WATCH, bool, flagsOf, parseFlags, printJson, str } from "../args.js";
6
7
  import { group } from "../group.js";
7
8
  import { fail } from "../io.js";
8
9
  import { launcherPath } from "../launch.js";
@@ -20,7 +21,7 @@ export async function limitsData(env, withAccounts = true) {
20
21
  const config = loadConfig(env);
21
22
  const profiles = listProfiles(config, env);
22
23
  // 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
+ const accounts = withAccounts ? await Promise.all(profiles.map((p) => (adapter(agentOf(p, config)).accountDetails ? account(p, env) : undefined))) : [];
24
25
  return profiles.map((p, i) => {
25
26
  const snap = usageSnapshot(p, env);
26
27
  const limits = snap.rate_limits ?? {};
@@ -36,28 +37,39 @@ export async function limitsData(env, withAccounts = true) {
36
37
  };
37
38
  });
38
39
  }
40
+ const SHOW_SPEC = { ...JSON_FLAG, ...WATCH };
39
41
  async function show(args, io) {
40
- const p = parseFlags(args, { ...JSON_FLAG, ...WATCH }, 0);
42
+ const p = parseFlags(args, SHOW_SPEC, 0);
41
43
  if (bool(p, "json")) {
42
44
  if (bool(p, "watch"))
43
45
  fail("--json and --watch do not go together");
44
- printJson(io.out, await limitsData(io.env));
46
+ printJson(io, await limitsData(io.env));
47
+ return 0;
48
+ }
49
+ const config = loadConfig(io.env);
50
+ if (!listProfiles(config, io.env).length) {
51
+ io.out("No profiles yet (turno profile add <name>).");
52
+ return 0;
53
+ }
54
+ if (!listProfiles(config, io.env, "claude").length) {
55
+ io.out("Usage limits come from Claude Code profiles, and there are none yet (turno profile add <name>).");
45
56
  return 0;
46
57
  }
47
58
  return usage.run(bool(p, "watch") ? ["--watch"] : [], io);
48
59
  }
49
- function profileArg(name, io, verb) {
50
- if (!name)
51
- return fail(`usage: turno limit ${verb} <profile>`);
60
+ async function profileArg(given, io, verb) {
61
+ const name = given ??
62
+ (await (await import("../picker.js")).pickProfile(io, { command: `limit ${verb}`, title: verb === "set" ? "Profile to mark exhausted" : "Profile to clear" }));
52
63
  if (!launcherPath(name, io.env))
53
64
  fail(`no profile '${name}' (turno profile list shows them)`);
54
65
  return name;
55
66
  }
67
+ const SET_SPEC = { until: { type: "string" }, for: { type: "string" }, ...DRY_RUN };
56
68
  async function set(args, io) {
57
69
  // The bash form `exhausted <profile> until "YYYY-MM-DD HH:MM"` still works.
58
70
  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");
71
+ const p = parseFlags(legacy ? [args[0], "--until", ...args.slice(2)] : args, SET_SPEC, 1);
72
+ const name = await profileArg(p.positionals[0], io, "set");
61
73
  const untilText = str(p, "until");
62
74
  const forText = str(p, "for");
63
75
  if (untilText && forText)
@@ -82,21 +94,21 @@ async function set(args, io) {
82
94
  }
83
95
  async function clear(args, io) {
84
96
  const p = parseFlags(args, DRY_RUN, 1);
85
- const name = profileArg(p.positionals[0], io, "clear");
97
+ const name = await profileArg(p.positionals[0], io, "clear");
86
98
  if (!bool(p, "dry-run"))
87
99
  clearExhausted(name, io.env);
88
100
  io.out(`${name} available again${bool(p, "dry-run") ? " (dry run)" : ""}`);
89
101
  return 0;
90
102
  }
91
103
  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 },
104
+ show: { summary: "5 h and weekly usage of each account, as bars", synopsis: "[--watch] [--json]", flags: flagsOf(SHOW_SPEC), run: show },
93
105
  set: {
94
106
  summary: "mark a profile exhausted (default for 5 h); its projects open with the fallback, if one is set",
95
107
  synopsis: '<profile> [--until "YYYY-MM-DD HH:MM" | --for 2h] [--dry-run]',
96
108
  args: ["profile"],
97
- flags: ["--until", "--for", "--dry-run"],
109
+ flags: flagsOf(SET_SPEC),
98
110
  run: set,
99
111
  },
100
- clear: { summary: "clear a profile's exhausted mark", synopsis: "<profile> [--dry-run]", args: ["profile"], flags: ["--dry-run"], run: clear },
112
+ clear: { summary: "clear a profile's exhausted mark", synopsis: "<profile> [--dry-run]", args: ["profile"], flags: flagsOf(DRY_RUN), run: clear },
101
113
  }, { fallback: "show" });
102
114
  //# sourceMappingURL=limit.js.map
@@ -0,0 +1,194 @@
1
+ import { spawn } from "node:child_process";
2
+ import { isValidProfileName } from "../../core/config.js";
3
+ import { loadManifest, servesProfile, variantKey } from "../../core/manifest.js";
4
+ import { NO_SECRET_STORE, keychainResolver, secretStore } from "../../core/secrets.js";
5
+ import { DRY_RUN, JSON_FLAG, bool, parseFlags, printJson, str } from "../args.js";
6
+ import { fail } from "../io.js";
7
+ /**
8
+ * `turno mcp login` and `turno mcp logout` (ADR 0029): mcpd is the OAuth client of remote
9
+ * servers declared with `auth = "oauth"`, one login per server variant. Agents reach the server
10
+ * through mcpd and never log in themselves.
11
+ */
12
+ export const LOGIN_FLAGS = { profile: { type: "string" }, scope: { type: "string" }, "no-browser": { type: "boolean" }, ...DRY_RUN, ...JSON_FLAG };
13
+ export const LOGOUT_FLAGS = { profile: { type: "string" }, all: { type: "boolean" }, "no-revoke": { type: "boolean" }, ...DRY_RUN };
14
+ function oauthServer(env, name, verb) {
15
+ if (!name)
16
+ fail(`usage: turno mcp ${verb} <server> [--profile <p>]`);
17
+ const def = loadManifest(env).servers[name];
18
+ if (!def)
19
+ fail(`no server "${name}" in mcp.toml`);
20
+ if (def.kind !== "remote" || !def.oauth)
21
+ fail(`${name} is not a remote server with auth = "oauth" in mcp.toml`);
22
+ return def;
23
+ }
24
+ function variantFor(server, profile) {
25
+ if (profile === undefined)
26
+ return "default";
27
+ if (!isValidProfileName(profile))
28
+ fail(`invalid profile name "${profile}"`);
29
+ if (!servesProfile(server, profile))
30
+ fail(`${server.name} is not served to profile ${profile} (profiles in mcp.toml)`);
31
+ return variantKey(server, profile);
32
+ }
33
+ function storeOf(env) {
34
+ const store = secretStore(env);
35
+ if (!store)
36
+ fail(NO_SECRET_STORE);
37
+ return store;
38
+ }
39
+ /** Who a variant's login serves, for messages. */
40
+ function whom(server, variant) {
41
+ if (variant !== "default")
42
+ return `profile ${variant}`;
43
+ return Object.keys(server.variants).length ? "every profile without its own variant" : "every profile";
44
+ }
45
+ /** Open the URL with the system browser (the profile's Chrome for claude.ai links, like turno open). */
46
+ async function openBrowser(url, profile, io) {
47
+ const { openInvocation } = await import("./open.js");
48
+ const inv = openInvocation(profile, url, io.env);
49
+ await new Promise((resolve) => {
50
+ const child = spawn(inv.command, inv.args, { stdio: "ignore", env: io.env, detached: true });
51
+ child.on("error", () => resolve());
52
+ child.on("spawn", () => {
53
+ child.unref();
54
+ resolve();
55
+ });
56
+ });
57
+ }
58
+ export async function login(args, io, deps = {}) {
59
+ const flags = parseFlags(args, LOGIN_FLAGS, 1);
60
+ const server = oauthServer(io.env, flags.positionals[0], "login");
61
+ const profile = str(flags, "profile");
62
+ const variant = variantFor(server, profile);
63
+ const store = storeOf(io.env);
64
+ const json = bool(flags, "json");
65
+ const noBrowser = bool(flags, "no-browser");
66
+ const { login: run, OAuthFlowError } = await import("../../mcpd/oauth.js");
67
+ const say = (line) => (json ? io.err(line) : io.out(line));
68
+ try {
69
+ const scope = str(flags, "scope");
70
+ const result = await run({
71
+ server,
72
+ variant,
73
+ store,
74
+ resolve: keychainResolver(io.env),
75
+ ...(scope ? { scope } : {}),
76
+ ...(deps.fetchFn ? { fetchFn: deps.fetchFn } : {}),
77
+ dryRun: bool(flags, "dry-run"),
78
+ open: async (url) => {
79
+ if (noBrowser) {
80
+ say(`Open this link in a browser to log in to ${server.name} (${whom(server, variant)}):`);
81
+ say(url);
82
+ }
83
+ else {
84
+ say(`Opening the browser to log in to ${server.name} (${whom(server, variant)}). If it does not open, visit:`);
85
+ say(url);
86
+ await (deps.open ?? openBrowser)(url, profile, io);
87
+ }
88
+ say("Waiting for the browser (Ctrl-C cancels)...");
89
+ },
90
+ });
91
+ if (json) {
92
+ printJson(io, { server: server.name, variant, ...result });
93
+ return 0;
94
+ }
95
+ if (result.planned) {
96
+ io.out(`would log in to ${server.name} (${whom(server, variant)}) through ${result.issuer}`);
97
+ io.out(` client: ${result.registration === "dynamic" ? "dynamic client registration" : result.registration === "metadata-document" ? "client ID metadata document" : "pre-registered client_id"}`);
98
+ io.out(` scope: ${result.scope ?? "(none requested)"}`);
99
+ io.out(` tokens would go to the ${store.label} as ${result.service}`);
100
+ return 0;
101
+ }
102
+ io.out(`Logged in to ${server.name} for ${whom(server, variant)} through ${result.issuer}.`);
103
+ io.out(`The tokens are in the ${store.label} as ${result.service}; mcpd refreshes them, and agents reach ${server.name} through mcpd.`);
104
+ if (!result.refreshToken)
105
+ io.out(`The server gave no refresh token: log in again when the access token expires.`);
106
+ return 0;
107
+ }
108
+ catch (err) {
109
+ if (err instanceof OAuthFlowError)
110
+ fail(`login to ${server.name} failed: ${err.message}`);
111
+ throw err;
112
+ }
113
+ }
114
+ export async function logout(args, io, deps = {}) {
115
+ const flags = parseFlags(args, LOGOUT_FLAGS, 1);
116
+ const server = oauthServer(io.env, flags.positionals[0], "logout");
117
+ const all = bool(flags, "all");
118
+ if (all && str(flags, "profile") !== undefined)
119
+ fail("use --all or --profile, not both");
120
+ const variants = all ? ["default", ...Object.keys(server.variants)] : [variantFor(server, str(flags, "profile"))];
121
+ const store = storeOf(io.env);
122
+ const dry = bool(flags, "dry-run");
123
+ const { oauthService, parseRecord, revoke } = await import("../../mcpd/oauth.js");
124
+ const removed = [];
125
+ for (const variant of variants) {
126
+ const service = oauthService(server.name, variant);
127
+ const rec = parseRecord(await store.get(service));
128
+ const label = `${server.name} (${whom(server, variant)})`;
129
+ if (!rec && !(await store.has(service))) {
130
+ io.out(`${label}: not logged in`);
131
+ continue;
132
+ }
133
+ if (dry) {
134
+ io.out(`would remove ${service} from the ${store.label}${rec?.tokens && !bool(flags, "no-revoke") ? ` and revoke the token at ${rec.issuer}` : ""}`);
135
+ continue;
136
+ }
137
+ let revoked = "";
138
+ if (rec?.tokens && !bool(flags, "no-revoke")) {
139
+ const why = await revoke(server, rec, keychainResolver(io.env), deps.fetchFn);
140
+ revoked = why ? `; could not revoke it at ${rec.issuer} (${why})` : `; revoked it at ${rec.issuer}`;
141
+ }
142
+ await store.remove(service);
143
+ removed.push(variant);
144
+ io.out(`Logged out of ${label}: removed ${service} from the ${store.label}${revoked}.`);
145
+ }
146
+ if (!dry && removed.length)
147
+ io.out(await tellMcpd(io.env, server.name, removed));
148
+ return 0;
149
+ }
150
+ /** Tell a running mcpd to drop its copy of the login now; what to print about it. */
151
+ async function tellMcpd(env, server, variants) {
152
+ const { mcpdRequest } = await import("./mcp.js");
153
+ const query = variants.map((v) => `variant=${encodeURIComponent(v)}`).join("&");
154
+ const r = await mcpdRequest(env, "POST", `/logout/${encodeURIComponent(server)}?${query}`);
155
+ if (r === "down")
156
+ return "mcpd is not running; it reads the secret store when it starts.";
157
+ // An mcpd from before 0.2 has no /logout: it notices within a minute.
158
+ return r.status === 200 ? "mcpd stopped using the old token." : "mcpd stops using the old token within a minute (now: turno mcp daemon restart).";
159
+ }
160
+ /** Variants whose login is needed: "default" when some served profile has no variant of its own. */
161
+ export function loginVariants(server) {
162
+ const own = Object.keys(server.variants);
163
+ const needsDefault = server.profiles.includes("*") || server.profiles.some((p) => !server.variants[p]);
164
+ return [...(needsDefault ? ["default"] : []), ...own];
165
+ }
166
+ /** The login state of every OAuth server variant (reads the secret store, never prints a token). */
167
+ export async function oauthLogins(env) {
168
+ const servers = Object.values(loadManifest(env).servers).filter((s) => s.kind === "remote" && s.oauth !== undefined);
169
+ if (!servers.length)
170
+ return [];
171
+ const { loginState } = await import("../../mcpd/oauth.js");
172
+ const store = secretStore(env);
173
+ const rows = [];
174
+ for (const s of servers) {
175
+ for (const variant of loginVariants(s))
176
+ rows.push({ server: s.name, variant, ...(await loginState(store, s.name, variant)) });
177
+ }
178
+ return rows;
179
+ }
180
+ /** One short phrase per state, with the command that fixes it. */
181
+ export function describeLogin(r) {
182
+ const who = r.variant === "default" ? "" : ` --profile ${r.variant}`;
183
+ switch (r.state) {
184
+ case "logged in":
185
+ return "logged in";
186
+ case "expired":
187
+ return "logged in (token refreshes on next use)";
188
+ case "unknown":
189
+ return "unknown (no secret store)";
190
+ default:
191
+ return `${r.state}: turno mcp login ${r.server}${who}`;
192
+ }
193
+ }
194
+ //# sourceMappingURL=mcp-oauth.js.map