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.
- package/CHANGELOG.md +293 -0
- package/README.md +143 -75
- package/dist/accounts/audit.js +137 -7
- package/dist/accounts/builtin.js +68 -21
- package/dist/accounts/delegate.js +176 -35
- package/dist/accounts/policy.js +77 -0
- package/dist/accounts/registry.js +37 -21
- package/dist/agents/adapter.js +7 -0
- package/dist/agents/claude.js +195 -0
- package/dist/agents/codex.js +98 -0
- package/dist/agents/copilot.js +39 -0
- package/dist/agents/cursor.js +41 -0
- package/dist/agents/index.js +26 -86
- package/dist/agents/snippets.js +7 -4
- package/dist/cli/args.js +19 -4
- package/dist/cli/commands/accounts.js +96 -51
- package/dist/cli/commands/auto.js +82 -32
- package/dist/cli/commands/browser.js +216 -135
- package/dist/cli/commands/complete.js +2 -1
- package/dist/cli/commands/completion.js +4 -2
- package/dist/cli/commands/config.js +103 -18
- package/dist/cli/commands/demo.js +292 -0
- package/dist/cli/commands/doctor.js +387 -119
- package/dist/cli/commands/handoff.js +33 -22
- package/dist/cli/commands/identity.js +101 -50
- package/dist/cli/commands/init.js +616 -0
- package/dist/cli/commands/limit.js +25 -13
- package/dist/cli/commands/mcp-oauth.js +194 -0
- package/dist/cli/commands/mcp-observe.js +434 -0
- package/dist/cli/commands/mcp.js +653 -185
- package/dist/cli/commands/mcpd.js +323 -75
- package/dist/cli/commands/migrate.js +6 -4
- package/dist/cli/commands/open.js +92 -27
- package/dist/cli/commands/profile.js +205 -69
- package/dist/cli/commands/resolve.js +6 -2
- package/dist/cli/commands/run.js +15 -5
- package/dist/cli/commands/secret.js +61 -42
- package/dist/cli/commands/session.js +2 -2
- package/dist/cli/commands/sessions.js +42 -12
- package/dist/cli/commands/setup.js +136 -158
- package/dist/cli/commands/status.js +172 -41
- package/dist/cli/commands/statusline.js +305 -113
- package/dist/cli/commands/ui.js +64 -66
- package/dist/cli/commands/undo.js +161 -18
- package/dist/cli/commands/uninstall.js +462 -0
- package/dist/cli/commands/upgrade.js +75 -19
- package/dist/cli/commands/usage.js +117 -44
- package/dist/cli/commands/version.js +7 -5
- package/dist/cli/commands/which.js +7 -24
- package/dist/cli/compile-cache.js +32 -0
- package/dist/cli/group.js +10 -10
- package/dist/cli/help.js +57 -0
- package/dist/cli/highlight.js +236 -0
- package/dist/cli/io.js +170 -1
- package/dist/cli/launch.js +21 -37
- package/dist/cli/launchers.js +145 -94
- package/dist/cli/main.js +163 -27
- package/dist/cli/picker.js +447 -0
- package/dist/cli/registry.js +42 -5
- package/dist/cli/security.js +539 -0
- package/dist/cli/style.js +281 -0
- package/dist/core/config.js +145 -23
- package/dist/core/exhausted.js +6 -8
- package/dist/core/fsutil.js +47 -0
- package/dist/core/journal.js +281 -11
- package/dist/core/manifest.js +126 -6
- package/dist/core/paths.js +114 -7
- package/dist/core/profiles.js +139 -9
- package/dist/core/resolve.js +129 -23
- package/dist/core/secrets.js +215 -20
- package/dist/core/sessions.js +13 -20
- package/dist/core/update.js +18 -8
- package/dist/core/version.js +6 -0
- package/dist/demo/server.js +34 -0
- package/dist/mcpd/cache.js +18 -4
- package/dist/mcpd/calllog.js +253 -0
- package/dist/mcpd/client-names.js +51 -0
- package/dist/mcpd/connectors.js +148 -17
- package/dist/mcpd/daemon.js +452 -116
- package/dist/mcpd/folder-sets.js +39 -0
- package/dist/mcpd/gateway.js +441 -47
- package/dist/mcpd/health.js +269 -0
- package/dist/mcpd/importer.js +356 -36
- package/dist/mcpd/instances.js +425 -65
- package/dist/mcpd/jsonrpc.js +8 -0
- package/dist/mcpd/launchd.js +65 -48
- package/dist/mcpd/modern.js +648 -0
- package/dist/mcpd/oauth.js +791 -0
- package/dist/mcpd/peerpid.js +122 -6
- package/dist/mcpd/pressure.js +85 -0
- package/dist/mcpd/rss.js +151 -0
- package/dist/mcpd/service.js +167 -0
- package/dist/mcpd/state.js +183 -0
- package/dist/mcpd/sync.js +55 -80
- package/dist/mcpd/systemd.js +80 -0
- package/dist/mcpd/tokens.js +89 -0
- package/dist/mcpd/upstream.js +154 -44
- package/dist/statusline/cost.js +154 -44
- package/dist/statusline/input.js +66 -0
- package/dist/statusline/render.js +9 -3
- package/dist/ui/fonts.js +8 -3
- package/dist/ui/settings.js +27 -15
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +9 -9
- package/dist/cli/commands/available.js +0 -18
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syntax highlighting for what turno shows in a terminal: TOML (config.toml, mcp.toml and their
|
|
3
|
+
* diffs) and JSON (`--json` output, agent config snippets). Both work line by line on the text as
|
|
4
|
+
* it is, never reformat it, and only add ANSI color codes: removing them gives back the input.
|
|
5
|
+
* Callers decide whether to color (see `colors` in io.ts).
|
|
6
|
+
*/
|
|
7
|
+
const RESET = "\x1b[0m";
|
|
8
|
+
/** SGR codes. Magenta reads well on dark and light backgrounds, unlike bright yellow or blue. */
|
|
9
|
+
const STYLE = {
|
|
10
|
+
header: "1",
|
|
11
|
+
key: "36",
|
|
12
|
+
string: "32",
|
|
13
|
+
literal: "35",
|
|
14
|
+
comment: "2",
|
|
15
|
+
secret: "33",
|
|
16
|
+
};
|
|
17
|
+
function render(tokens) {
|
|
18
|
+
let out = "";
|
|
19
|
+
for (const t of tokens) {
|
|
20
|
+
if (!t.text)
|
|
21
|
+
continue;
|
|
22
|
+
out += t.kind === "plain" ? t.text : `\x1b[${STYLE[t.kind]}m${t.text}${RESET}`;
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
/** The trailing comment `turno mcp import` adds to a preview line whose value it masked. */
|
|
27
|
+
const SECRET_MARK = /^#\s*secret, hidden\s*$/;
|
|
28
|
+
// TOML ----------------------------------------------------------------------------------------
|
|
29
|
+
const BARE = "[A-Za-z0-9_-]+";
|
|
30
|
+
const BASIC = '"(?:[^"\\\\]|\\\\.)*"';
|
|
31
|
+
const LITERAL = "'[^']*'";
|
|
32
|
+
const SIMPLE = `(?:${BARE}|${BASIC}|${LITERAL})`;
|
|
33
|
+
const KEY_PATH = `${SIMPLE}(?:[ \\t]*\\.[ \\t]*${SIMPLE})*`;
|
|
34
|
+
/** A key (dotted or quoted) followed by `=`, at the start of what is left of the line. */
|
|
35
|
+
const KEY = new RegExp(`^${KEY_PATH}(?=[ \\t]*=)`);
|
|
36
|
+
/** `[table]` or `[[array.of.tables]]`, possibly indented, with only a comment after it. */
|
|
37
|
+
const HEADER = new RegExp(`^([ \\t]*)(\\[\\[?[ \\t]*${KEY_PATH}[ \\t]*\\]\\]?)(?=[ \\t]*(?:#.*)?$)`);
|
|
38
|
+
/** Numbers, booleans, dates and times, inf and nan: anything bare in a value position. */
|
|
39
|
+
const WORD = /^[A-Za-z0-9_+\-.:]+/;
|
|
40
|
+
export function tomlState() {
|
|
41
|
+
return { stack: [] };
|
|
42
|
+
}
|
|
43
|
+
/** Where a multi-line string that starts at `from` ends (index after the delimiter), or -1. */
|
|
44
|
+
function closeMultiline(line, from, delim) {
|
|
45
|
+
let i = from;
|
|
46
|
+
while (i < line.length) {
|
|
47
|
+
if (delim === '"""' && line[i] === "\\") {
|
|
48
|
+
i += 2;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (line.startsWith(delim, i)) {
|
|
52
|
+
// A closing delimiter may carry up to two more quotes that belong to the string.
|
|
53
|
+
let end = i + 3;
|
|
54
|
+
while (end < line.length && end - i < 5 && line[end] === delim[0])
|
|
55
|
+
end++;
|
|
56
|
+
return end;
|
|
57
|
+
}
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
return -1;
|
|
61
|
+
}
|
|
62
|
+
/** Where a single-line string that starts at `from` (on its quote) ends, or the line end. */
|
|
63
|
+
function closeString(line, from) {
|
|
64
|
+
const quote = line[from];
|
|
65
|
+
let i = from + 1;
|
|
66
|
+
while (i < line.length) {
|
|
67
|
+
if (quote === '"' && line[i] === "\\") {
|
|
68
|
+
i += 2;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (line[i] === quote)
|
|
72
|
+
return i + 1;
|
|
73
|
+
i++;
|
|
74
|
+
}
|
|
75
|
+
return line.length;
|
|
76
|
+
}
|
|
77
|
+
function tomlTokens(line, state) {
|
|
78
|
+
const tokens = [];
|
|
79
|
+
const push = (text, kind) => tokens.push({ text, kind });
|
|
80
|
+
let i = 0;
|
|
81
|
+
if (state.multiline) {
|
|
82
|
+
const end = closeMultiline(line, 0, state.multiline);
|
|
83
|
+
if (end < 0) {
|
|
84
|
+
push(line, "string");
|
|
85
|
+
return tokens;
|
|
86
|
+
}
|
|
87
|
+
push(line.slice(0, end), "string");
|
|
88
|
+
state.multiline = undefined;
|
|
89
|
+
i = end;
|
|
90
|
+
}
|
|
91
|
+
else if (!state.stack.length) {
|
|
92
|
+
const h = HEADER.exec(line);
|
|
93
|
+
if (h) {
|
|
94
|
+
push(h[1], "plain");
|
|
95
|
+
push(h[2], "header");
|
|
96
|
+
i = h[0].length;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// A key can start the line (outside arrays) and follow `{` or `,` inside an inline table.
|
|
100
|
+
let expectKey = i === 0 && !state.stack.length && !tokens.length;
|
|
101
|
+
while (i < line.length) {
|
|
102
|
+
const rest = line.slice(i);
|
|
103
|
+
const ch = line[i];
|
|
104
|
+
if (ch === " " || ch === "\t") {
|
|
105
|
+
const ws = /^[ \t]+/.exec(rest)[0];
|
|
106
|
+
push(ws, "plain");
|
|
107
|
+
i += ws.length;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (expectKey) {
|
|
111
|
+
expectKey = false;
|
|
112
|
+
const k = KEY.exec(rest);
|
|
113
|
+
if (k) {
|
|
114
|
+
push(k[0], "key");
|
|
115
|
+
i += k[0].length;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (ch === "#") {
|
|
120
|
+
push(rest, "comment");
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
if (rest.startsWith('"""') || rest.startsWith("'''")) {
|
|
124
|
+
const delim = rest.slice(0, 3);
|
|
125
|
+
const end = closeMultiline(line, i + 3, delim);
|
|
126
|
+
if (end < 0) {
|
|
127
|
+
push(rest, "string");
|
|
128
|
+
state.multiline = delim;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
push(line.slice(i, end), "string");
|
|
132
|
+
i = end;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (ch === '"' || ch === "'") {
|
|
136
|
+
const end = closeString(line, i);
|
|
137
|
+
push(line.slice(i, end), "string");
|
|
138
|
+
i = end;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (ch === "[" || ch === "{") {
|
|
142
|
+
state.stack.push(ch);
|
|
143
|
+
push(ch, "plain");
|
|
144
|
+
expectKey = ch === "{";
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (ch === "]" || ch === "}") {
|
|
149
|
+
if (state.stack.at(-1) === (ch === "]" ? "[" : "{"))
|
|
150
|
+
state.stack.pop();
|
|
151
|
+
push(ch, "plain");
|
|
152
|
+
i++;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (ch === ",") {
|
|
156
|
+
expectKey = state.stack.at(-1) === "{";
|
|
157
|
+
push(ch, "plain");
|
|
158
|
+
i++;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const w = WORD.exec(rest);
|
|
162
|
+
if (w) {
|
|
163
|
+
push(w[0], /^(?:true|false|[+-]?(?:inf|nan)|[+-]?[0-9][A-Za-z0-9_+\-.:]*)$/.test(w[0]) ? "literal" : "plain");
|
|
164
|
+
i += w[0].length;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
push(ch, "plain");
|
|
168
|
+
i++;
|
|
169
|
+
}
|
|
170
|
+
return markSecrets(tokens);
|
|
171
|
+
}
|
|
172
|
+
/** On a line marked `# secret, hidden`, the masked value (and the mark) stand out. */
|
|
173
|
+
function markSecrets(tokens) {
|
|
174
|
+
const last = tokens.at(-1);
|
|
175
|
+
if (last?.kind !== "comment" || !SECRET_MARK.test(last.text))
|
|
176
|
+
return tokens;
|
|
177
|
+
const strings = tokens.filter((t) => t.kind === "string");
|
|
178
|
+
const masked = strings.filter((t) => t.text.includes("****"));
|
|
179
|
+
for (const t of masked.length ? masked : strings)
|
|
180
|
+
t.kind = "secret";
|
|
181
|
+
last.kind = "secret";
|
|
182
|
+
return tokens;
|
|
183
|
+
}
|
|
184
|
+
/** One TOML line, colored; `state` carries open arrays and strings to the next line. */
|
|
185
|
+
export function highlightTomlLine(line, state) {
|
|
186
|
+
return render(tomlTokens(line, state));
|
|
187
|
+
}
|
|
188
|
+
/** A TOML text, colored line by line. */
|
|
189
|
+
export function highlightToml(text) {
|
|
190
|
+
const state = tomlState();
|
|
191
|
+
return text
|
|
192
|
+
.split("\n")
|
|
193
|
+
.map((l) => highlightTomlLine(l, state))
|
|
194
|
+
.join("\n");
|
|
195
|
+
}
|
|
196
|
+
// JSON ----------------------------------------------------------------------------------------
|
|
197
|
+
const JSON_LITERAL = /^(?:-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?|true|false|null)/;
|
|
198
|
+
/** A JSON text, colored: keys, strings, and numbers with true, false and null. */
|
|
199
|
+
export function highlightJson(text) {
|
|
200
|
+
const tokens = [];
|
|
201
|
+
let i = 0;
|
|
202
|
+
let plain = "";
|
|
203
|
+
const flush = () => {
|
|
204
|
+
if (plain)
|
|
205
|
+
tokens.push({ text: plain, kind: "plain" });
|
|
206
|
+
plain = "";
|
|
207
|
+
};
|
|
208
|
+
while (i < text.length) {
|
|
209
|
+
const ch = text[i];
|
|
210
|
+
if (ch === '"') {
|
|
211
|
+
let end = i + 1;
|
|
212
|
+
while (end < text.length && text[end] !== '"' && text[end] !== "\n")
|
|
213
|
+
end += text[end] === "\\" ? 2 : 1;
|
|
214
|
+
if (text[end] === '"')
|
|
215
|
+
end++;
|
|
216
|
+
const after = /^\s*/.exec(text.slice(end))[0];
|
|
217
|
+
flush();
|
|
218
|
+
tokens.push({ text: text.slice(i, end), kind: text[end + after.length] === ":" ? "key" : "string" });
|
|
219
|
+
i = end;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const prev = text[i - 1];
|
|
223
|
+
const lit = prev === undefined || !/[A-Za-z0-9_.]/.test(prev) ? JSON_LITERAL.exec(text.slice(i)) : null;
|
|
224
|
+
if (lit) {
|
|
225
|
+
flush();
|
|
226
|
+
tokens.push({ text: lit[0], kind: "literal" });
|
|
227
|
+
i += lit[0].length;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
plain += ch;
|
|
231
|
+
i++;
|
|
232
|
+
}
|
|
233
|
+
flush();
|
|
234
|
+
return render(tokens);
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=highlight.js.map
|
package/dist/cli/io.js
CHANGED
|
@@ -5,18 +5,187 @@ export class UsageError extends Error {
|
|
|
5
5
|
this.name = "UsageError";
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
+
/** A plain error with its exit code: main prints `turno: <message>` and returns `code`. */
|
|
9
|
+
export class CliError extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(message, code = 1) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.name = "CliError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function askStdin(question) {
|
|
18
|
+
const { createInterface } = await import("node:readline/promises");
|
|
19
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
20
|
+
try {
|
|
21
|
+
return await rl.question(question);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
rl.close();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
8
27
|
export function defaultIo() {
|
|
9
28
|
return {
|
|
10
29
|
out: (line = "") => process.stdout.write(`${line}\n`),
|
|
11
30
|
err: (line = "") => process.stderr.write(`${line}\n`),
|
|
12
31
|
env: process.env,
|
|
13
32
|
isTty: Boolean(process.stdout.isTTY),
|
|
33
|
+
...(process.stdin.isTTY && process.stderr.isTTY && !noInputReason(process.env) ? { ask: askStdin } : {}),
|
|
14
34
|
};
|
|
15
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* The variables each coding agent sets for the commands it runs, by agent id: inside them nobody
|
|
38
|
+
* reads a question, so turno never asks one. Sources are in ADR 0033. Kept here rather than in
|
|
39
|
+
* the adapters because every run loads io.ts and it stays free of heavy imports; a test keeps the
|
|
40
|
+
* ids in step with src/agents.
|
|
41
|
+
*/
|
|
42
|
+
export const AGENT_SESSION_ENV = {
|
|
43
|
+
claude: ["CLAUDECODE", "CLAUDE_CODE_CHILD_SESSION"],
|
|
44
|
+
codex: ["CODEX_THREAD_ID", "CODEX_SANDBOX"],
|
|
45
|
+
copilot: ["COPILOT_CLI", "COPILOT_AGENT_SESSION_ID"],
|
|
46
|
+
cursor: ["CURSOR_AGENT"],
|
|
47
|
+
};
|
|
48
|
+
const on = (v) => v !== undefined && v !== "" && v !== "0" && v.toLowerCase() !== "false";
|
|
49
|
+
/**
|
|
50
|
+
* The variable that keeps turno from asking anything here (TURNO_NO_INPUT, or an agent's), or
|
|
51
|
+
* undefined when a person may answer on a terminal. TURNO_NO_INPUT=1 (or --no-input) turns
|
|
52
|
+
* questions off; TURNO_NO_INPUT=0 allows them inside an agent session, for a person in an
|
|
53
|
+
* editor terminal that sets an agent variable.
|
|
54
|
+
*/
|
|
55
|
+
export function noInputReason(env) {
|
|
56
|
+
const own = env.TURNO_NO_INPUT;
|
|
57
|
+
if (own !== undefined && own !== "")
|
|
58
|
+
return on(own) ? "TURNO_NO_INPUT" : undefined;
|
|
59
|
+
return Object.values(AGENT_SESSION_ENV)
|
|
60
|
+
.flat()
|
|
61
|
+
.find((name) => on(env[name]));
|
|
62
|
+
}
|
|
63
|
+
/** The same Io without a way to ask. */
|
|
64
|
+
export function withoutAsk(io) {
|
|
65
|
+
const { ask: _ask, ...rest } = io;
|
|
66
|
+
return rest;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* A question nobody can answer here (no terminal, --no-input, an agent session): exit 2 and
|
|
70
|
+
* name the flag that answers it, the same way for every command.
|
|
71
|
+
*/
|
|
72
|
+
export function needsAnswer(what, flag = "--yes") {
|
|
73
|
+
throw new UsageError(`${what} needs an answer; pass ${flag} (or run it in a terminal)`);
|
|
74
|
+
}
|
|
75
|
+
/** Ask a yes/no question; false without a person to answer. */
|
|
76
|
+
export async function confirm(io, question) {
|
|
77
|
+
if (!io.ask)
|
|
78
|
+
return false;
|
|
79
|
+
const answer = (await io.ask(`${question} [y/N] `)).trim().toLowerCase();
|
|
80
|
+
return answer === "y" || answer === "yes";
|
|
81
|
+
}
|
|
82
|
+
const COLOR_MODES = ["auto", "always", "never"];
|
|
83
|
+
export function isColorMode(v) {
|
|
84
|
+
return v !== undefined && COLOR_MODES.includes(v);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether output gets ANSI colors. The first rule that applies wins (ADR 0037):
|
|
88
|
+
*
|
|
89
|
+
* 1. `--color always|never` on the command line;
|
|
90
|
+
* 2. TURNO_COLOR=always|never (any other value counts as auto; --color auto sets it aside);
|
|
91
|
+
* 3. NO_COLOR set and not empty: off (https://no-color.org, which lets a flag or a tool's own
|
|
92
|
+
* setting override it);
|
|
93
|
+
* 4. FORCE_COLOR: 0 or false turns colors off, anything else turns them on, also in a pipe;
|
|
94
|
+
* 5. auto: on when output is a terminal.
|
|
95
|
+
*
|
|
96
|
+
* `--json` output is never colored outside a terminal, whatever this says (printJson).
|
|
97
|
+
*/
|
|
98
|
+
export function colors(io) {
|
|
99
|
+
// An explicit --color auto also sets TURNO_COLOR aside.
|
|
100
|
+
const own = io.color ?? io.env.TURNO_COLOR;
|
|
101
|
+
if (own === "always")
|
|
102
|
+
return true;
|
|
103
|
+
if (own === "never")
|
|
104
|
+
return false;
|
|
105
|
+
if (io.env.NO_COLOR)
|
|
106
|
+
return false;
|
|
107
|
+
const force = io.env.FORCE_COLOR;
|
|
108
|
+
if (force !== undefined && force !== "")
|
|
109
|
+
return force !== "0" && force !== "false";
|
|
110
|
+
return io.isTty;
|
|
111
|
+
}
|
|
16
112
|
export function paint(io, text, code) {
|
|
17
|
-
return io
|
|
113
|
+
return colors(io) ? `\x1b[${code}m${text}\x1b[0m` : text;
|
|
18
114
|
}
|
|
19
115
|
export function fail(message) {
|
|
20
116
|
throw new UsageError(message);
|
|
21
117
|
}
|
|
118
|
+
/** "1 project", "2 projects". */
|
|
119
|
+
export function plural(n, word, many = `${word}s`) {
|
|
120
|
+
return `${n} ${n === 1 ? word : many}`;
|
|
121
|
+
}
|
|
122
|
+
/** Help text width. */
|
|
123
|
+
const WIDTH = 80;
|
|
124
|
+
/** Break `text` into lines of at most `width` columns at spaces (a longer word stays whole). */
|
|
125
|
+
export function wrap(text, width) {
|
|
126
|
+
const lines = [];
|
|
127
|
+
let line = "";
|
|
128
|
+
for (const word of text.split(/\s+/).filter(Boolean)) {
|
|
129
|
+
if (line && line.length + 1 + word.length > width) {
|
|
130
|
+
lines.push(line);
|
|
131
|
+
line = word;
|
|
132
|
+
}
|
|
133
|
+
else
|
|
134
|
+
line = line ? `${line} ${word}` : word;
|
|
135
|
+
}
|
|
136
|
+
if (line)
|
|
137
|
+
lines.push(line);
|
|
138
|
+
return lines.length ? lines : [""];
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Two-column rows (` left summary`) within WIDTH columns: summaries wrap under their
|
|
142
|
+
* column, and a left side too wide for the column goes on its own line.
|
|
143
|
+
*/
|
|
144
|
+
export function columns(rows, indent = 2, maxLeft = 32, left) {
|
|
145
|
+
const fit = rows.map(([l]) => l.length).filter((n) => n <= maxLeft);
|
|
146
|
+
const w = left ?? Math.max(0, ...fit);
|
|
147
|
+
const pad = " ".repeat(indent);
|
|
148
|
+
const out = [];
|
|
149
|
+
for (const [left, summary] of rows) {
|
|
150
|
+
const col = indent + w + 2;
|
|
151
|
+
const body = wrap(summary, Math.max(20, WIDTH - col));
|
|
152
|
+
if (left.length > w) {
|
|
153
|
+
const [first, ...more] = wrap(left, WIDTH - indent - 4);
|
|
154
|
+
out.push(`${pad}${first}`);
|
|
155
|
+
for (const l of more)
|
|
156
|
+
out.push(`${pad} ${l}`);
|
|
157
|
+
for (const l of body)
|
|
158
|
+
out.push(`${" ".repeat(col)}${l}`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
out.push(`${pad}${left.padEnd(w)} ${body[0]}`);
|
|
162
|
+
for (const l of body.slice(1))
|
|
163
|
+
out.push(`${" ".repeat(col)}${l}`);
|
|
164
|
+
}
|
|
165
|
+
return out.join("\n");
|
|
166
|
+
}
|
|
167
|
+
/** Edit distance with adjacent transpositions (so "stauts" is 1 away from "status"). */
|
|
168
|
+
export function distance(a, b) {
|
|
169
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
170
|
+
for (let i = 1; i <= a.length; i++) {
|
|
171
|
+
for (let j = 1; j <= b.length; j++) {
|
|
172
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
173
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
174
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
175
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return d[a.length][b.length];
|
|
179
|
+
}
|
|
180
|
+
/** The closest of `candidates` to `word` (at most 2 edits, 1 for words of 3 letters or less). */
|
|
181
|
+
export function closest(word, candidates) {
|
|
182
|
+
const max = word.length <= 3 ? 1 : 2;
|
|
183
|
+
let best;
|
|
184
|
+
for (const c of candidates) {
|
|
185
|
+
const d = distance(word.toLowerCase(), c);
|
|
186
|
+
if (d <= max && (!best || d < best.d))
|
|
187
|
+
best = { c, d };
|
|
188
|
+
}
|
|
189
|
+
return best?.c;
|
|
190
|
+
}
|
|
22
191
|
//# sourceMappingURL=io.js.map
|
package/dist/cli/launch.js
CHANGED
|
@@ -1,35 +1,12 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import { accessSync, constants as fsConstants, statSync } from "node:fs";
|
|
3
2
|
import { constants } from "node:os";
|
|
4
|
-
import {
|
|
5
|
-
import { isValidProfileName } from "../core/config.js";
|
|
6
|
-
import { paths } from "../core/paths.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
}
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { isValidProfileName, loadConfig } from "../core/config.js";
|
|
5
|
+
import { paths, tildify } from "../core/paths.js";
|
|
6
|
+
import { listProfiles } from "../core/profiles.js";
|
|
7
|
+
import { CliError, UsageError } from "./io.js";
|
|
8
|
+
import { executable, launcherName, profileAgent } from "./launchers.js";
|
|
9
|
+
export { findOnPath } from "./launchers.js";
|
|
33
10
|
/** Path of a profile's launcher (claude-<name>, codex-<name>, ...) when it exists and is executable. */
|
|
34
11
|
export function launcherPath(name, env = process.env, agent = profileAgent(name, env)) {
|
|
35
12
|
if (!isValidProfileName(name))
|
|
@@ -80,17 +57,24 @@ export async function execLauncher(file, args, env = process.env) {
|
|
|
80
57
|
});
|
|
81
58
|
});
|
|
82
59
|
}
|
|
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
60
|
/** `turno <profile> [args]`: exec the profile's launcher. */
|
|
91
61
|
export async function launchProfile(name, args, io) {
|
|
92
62
|
const file = launcherPath(name, io.env);
|
|
93
63
|
if (!file) {
|
|
64
|
+
if (!isValidProfileName(name))
|
|
65
|
+
throw new UsageError(`unknown command '${name}' (turno --help)`);
|
|
66
|
+
let declared = false;
|
|
67
|
+
try {
|
|
68
|
+
declared = listProfiles(loadConfig(io.env), io.env).includes(name);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
declared = false;
|
|
72
|
+
}
|
|
73
|
+
if (declared) {
|
|
74
|
+
const agent = profileAgent(name, io.env);
|
|
75
|
+
const missing = tildify(join(paths(io.env).bin, launcherName(name, agent)), io.env);
|
|
76
|
+
throw new CliError(`profile ${name} has no launcher (${missing} is missing). Run: turno profile repair ${name}`);
|
|
77
|
+
}
|
|
94
78
|
throw new UsageError(`no profile '${name}' (turno profile list shows them; turno profile add ${name} creates it)`);
|
|
95
79
|
}
|
|
96
80
|
return execLauncher(file, args, io.env);
|