turno-cli 0.3.0 → 0.5.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 +140 -0
- package/README.md +9 -5
- package/dist/cli/args.js +2 -78
- package/dist/cli/commands/browser-do-session.js +69 -0
- package/dist/cli/commands/browser-do.js +241 -0
- package/dist/cli/commands/browser.js +134 -303
- package/dist/cli/commands/identity.js +101 -94
- package/dist/cli/commands/mcp-call.js +4 -40
- package/dist/cli/exit.js +2 -64
- package/dist/cli/group.js +2 -61
- package/dist/cli/highlight.js +2 -235
- package/dist/cli/hints.js +5 -12
- package/dist/cli/io.js +2 -206
- package/dist/cli/output.js +2 -122
- package/dist/cli/registry.js +3 -2
- package/dist/cli/secret-input.js +2 -62
- package/dist/cli/style.js +2 -289
- package/dist/cli/topics.js +17 -0
- package/dist/core/paths.js +2 -9
- package/dist/mcpd/health.js +1 -1
- package/dist/mcpd/instances.js +11 -1
- package/dist/mcpd/middleware.js +15 -3
- package/dist/mcpd/peerpid.js +52 -18
- package/npm-shrinkwrap.json +5 -5
- package/package.json +3 -3
- package/dist/cli/commands/browser-identity.js +0 -514
- package/dist/cli/commands/browser-session.js +0 -102
- package/dist/cli/commands/browser-vault.js +0 -193
package/dist/cli/style.js
CHANGED
|
@@ -1,57 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
* The look of turno's own screens: semantic colors, status icons, section headers, key/value
|
|
3
|
-
* rows, tables sized to the terminal, usage bars, a spinner and timings. Every command draws with
|
|
4
|
-
* this so the CLI reads as one tool. Without colors (a pipe, NO_COLOR) the same layout comes out
|
|
5
|
-
* as plain text with ASCII icons, stable for scripts and tests.
|
|
6
|
-
*
|
|
7
|
-
* Keep this file free of heavy imports: `turno --help`, `turno status` and the status line load it.
|
|
8
|
-
*/
|
|
1
|
+
// The look moved to turno-core so turno-browser shares it; profile colors stay with the status line.
|
|
9
2
|
import { NAMED_COLORS, PILL_DEFAULTS } from "../statusline/render.js";
|
|
10
|
-
|
|
11
|
-
const cp = (n) => String.fromCodePoint(n);
|
|
12
|
-
export const ICONS = {
|
|
13
|
-
nerd: {
|
|
14
|
-
ok: cp(0xf00c),
|
|
15
|
-
warn: cp(0xf071),
|
|
16
|
-
err: cp(0xf00d),
|
|
17
|
-
info: cp(0xf05a),
|
|
18
|
-
add: "+",
|
|
19
|
-
remove: "-",
|
|
20
|
-
on: cp(0xf111),
|
|
21
|
-
off: cp(0xf10c),
|
|
22
|
-
arrow: cp(0xf061),
|
|
23
|
-
next: cp(0xf0a9),
|
|
24
|
-
skip: cp(0xf05e),
|
|
25
|
-
},
|
|
26
|
-
unicode: {
|
|
27
|
-
ok: "✓",
|
|
28
|
-
warn: "!",
|
|
29
|
-
err: "✗",
|
|
30
|
-
info: "•",
|
|
31
|
-
add: "+",
|
|
32
|
-
remove: "-",
|
|
33
|
-
on: "●",
|
|
34
|
-
off: "○",
|
|
35
|
-
arrow: "→",
|
|
36
|
-
next: "➜",
|
|
37
|
-
skip: "–",
|
|
38
|
-
},
|
|
39
|
-
ascii: {
|
|
40
|
-
ok: "ok",
|
|
41
|
-
warn: "!",
|
|
42
|
-
err: "x",
|
|
43
|
-
info: "-",
|
|
44
|
-
add: "+",
|
|
45
|
-
remove: "-",
|
|
46
|
-
on: "",
|
|
47
|
-
off: "",
|
|
48
|
-
arrow: "->",
|
|
49
|
-
next: "",
|
|
50
|
-
skip: "-",
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
/** SGR codes of the semantic colors: the terminal's own palette, so dark and light themes both work. */
|
|
54
|
-
const CODES = { ok: "32", warn: "33", err: "31", accent: "36", muted: "2", faint: "90", bold: "1" };
|
|
3
|
+
export * from "turno-core/cli/style";
|
|
55
4
|
/**
|
|
56
5
|
* The 256-color code of a profile: its `color` in config.toml, or one of the defaults by its
|
|
57
6
|
* place in the config. The one rule for every profile pill: the status line (`pillColor` in
|
|
@@ -72,240 +21,4 @@ export function profileColor(profiles, name) {
|
|
|
72
21
|
const idx = names.includes(name) ? names.indexOf(name) : [...name].reduce((a, ch) => a + ch.charCodeAt(0), 0);
|
|
73
22
|
return PILL_DEFAULTS[idx % PILL_DEFAULTS.length];
|
|
74
23
|
}
|
|
75
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI escapes is the point
|
|
76
|
-
const ANSI = /\x1b\[[0-9;]*m/g;
|
|
77
|
-
/** Text without its ANSI color codes. */
|
|
78
|
-
export function stripAnsi(text) {
|
|
79
|
-
return text.replace(ANSI, "");
|
|
80
|
-
}
|
|
81
|
-
/** Columns a string takes on screen (ANSI codes take none). */
|
|
82
|
-
export function visibleWidth(text) {
|
|
83
|
-
return [...stripAnsi(text)].length;
|
|
84
|
-
}
|
|
85
|
-
/** Pad to `width` visible columns. */
|
|
86
|
-
export function padEnd(text, width) {
|
|
87
|
-
return text + " ".repeat(Math.max(0, width - visibleWidth(text)));
|
|
88
|
-
}
|
|
89
|
-
function padStart(text, width) {
|
|
90
|
-
return " ".repeat(Math.max(0, width - visibleWidth(text))) + text;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Columns of the terminal: COLUMNS, then the real terminal, then 80 (a pipe, a test). Never
|
|
94
|
-
* below 40, so layouts keep a sane minimum.
|
|
95
|
-
*/
|
|
96
|
-
export function termWidth(io) {
|
|
97
|
-
const env = Number(io.env.COLUMNS);
|
|
98
|
-
if (Number.isInteger(env) && env > 0)
|
|
99
|
-
return Math.max(40, env);
|
|
100
|
-
const real = io.isTty ? process.stdout.columns : undefined;
|
|
101
|
-
return real && real > 0 ? Math.max(40, real) : 80;
|
|
102
|
-
}
|
|
103
|
-
/** "420ms", "1.2s", "1m05s". */
|
|
104
|
-
export function duration(ms) {
|
|
105
|
-
if (ms < 1000)
|
|
106
|
-
return `${Math.max(0, Math.round(ms))}ms`;
|
|
107
|
-
if (ms < 60_000)
|
|
108
|
-
return `${(ms / 1000).toFixed(1)}s`;
|
|
109
|
-
const s = Math.round(ms / 1000);
|
|
110
|
-
return `${Math.floor(s / 60)}m${String(s % 60).padStart(2, "0")}s`;
|
|
111
|
-
}
|
|
112
|
-
/** Level of a usage percentage: below 60 fine, below 85 a warning, then a problem. */
|
|
113
|
-
export function level(pct) {
|
|
114
|
-
return pct < 60 ? "ok" : pct < 85 ? "warn" : "err";
|
|
115
|
-
}
|
|
116
|
-
/** Cut a word that may hold ANSI codes into pieces of at most `width` visible columns. */
|
|
117
|
-
function chop(word, width) {
|
|
118
|
-
const pieces = [];
|
|
119
|
-
let piece = "";
|
|
120
|
-
let w = 0;
|
|
121
|
-
// ANSI codes take no columns and stay with the character after them.
|
|
122
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: matching ANSI escapes is the point
|
|
123
|
-
for (const m of word.matchAll(/\x1b\[[0-9;]*m|[^\x1b]/gu)) {
|
|
124
|
-
const t = m[0];
|
|
125
|
-
if (t.startsWith("\x1b")) {
|
|
126
|
-
piece += t;
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
if (w === width) {
|
|
130
|
-
pieces.push(piece);
|
|
131
|
-
piece = "";
|
|
132
|
-
w = 0;
|
|
133
|
-
}
|
|
134
|
-
piece += t;
|
|
135
|
-
w++;
|
|
136
|
-
}
|
|
137
|
-
if (piece)
|
|
138
|
-
pieces.push(piece);
|
|
139
|
-
return pieces;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Break text that may hold ANSI codes into lines of at most `width` visible columns, at spaces.
|
|
143
|
-
* A word longer than the width (a long path) is cut at the width. Colors carry over: a terminal
|
|
144
|
-
* keeps the active SGR across a line break.
|
|
145
|
-
*/
|
|
146
|
-
export function wrapAnsi(text, width) {
|
|
147
|
-
if (visibleWidth(text) <= width)
|
|
148
|
-
return [text];
|
|
149
|
-
const words = text.split(" ").flatMap((word) => (visibleWidth(word) > width ? chop(word, width) : [word]));
|
|
150
|
-
const [first = "", ...rest] = words;
|
|
151
|
-
const lines = [];
|
|
152
|
-
let line = first;
|
|
153
|
-
let w = visibleWidth(first);
|
|
154
|
-
for (const word of rest) {
|
|
155
|
-
const ww = visibleWidth(word);
|
|
156
|
-
// Break before a word that does not fit; spaces inside a line (alignment) stay as they are.
|
|
157
|
-
if (w > 0 && ww > 0 && w + 1 + ww > width) {
|
|
158
|
-
lines.push(line);
|
|
159
|
-
line = word;
|
|
160
|
-
w = ww;
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
line += ` ${word}`;
|
|
164
|
-
w += 1 + ww;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
lines.push(line);
|
|
168
|
-
return lines;
|
|
169
|
-
}
|
|
170
|
-
/** Columns a wrapping last table column keeps before other columns are dropped for it. */
|
|
171
|
-
const LAST_MIN = 24;
|
|
172
|
-
export function style(io, opts = {}) {
|
|
173
|
-
const color = colors(io);
|
|
174
|
-
const icons = !color ? "ascii" : opts.nerd ? "nerd" : "unicode";
|
|
175
|
-
const width = termWidth(io);
|
|
176
|
-
const sgr = (code) => (s) => (color && s ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
177
|
-
const ok = sgr(CODES.ok);
|
|
178
|
-
const warn = sgr(CODES.warn);
|
|
179
|
-
const err = sgr(CODES.err);
|
|
180
|
-
const accent = sgr(CODES.accent);
|
|
181
|
-
const muted = sgr(CODES.muted);
|
|
182
|
-
const faint = sgr(CODES.faint);
|
|
183
|
-
const bold = sgr(CODES.bold);
|
|
184
|
-
const ICON_COLOR = {
|
|
185
|
-
ok,
|
|
186
|
-
warn,
|
|
187
|
-
err,
|
|
188
|
-
info: accent,
|
|
189
|
-
add: ok,
|
|
190
|
-
remove: err,
|
|
191
|
-
on: ok,
|
|
192
|
-
off: faint,
|
|
193
|
-
arrow: faint,
|
|
194
|
-
next: accent,
|
|
195
|
-
skip: faint,
|
|
196
|
-
};
|
|
197
|
-
const icon = (name) => (ICON_COLOR[name] ?? ((s) => s))(ICONS[icons][name]);
|
|
198
|
-
const iconWidth = Math.max(...["ok", "warn", "err", "skip", "info"].map((n) => visibleWidth(ICONS[icons][n])));
|
|
199
|
-
const wrapLine = (prefix, text) => {
|
|
200
|
-
if (!color)
|
|
201
|
-
return `${prefix}${text}`;
|
|
202
|
-
const hang = visibleWidth(prefix);
|
|
203
|
-
const body = wrapAnsi(text, Math.max(20, width - hang));
|
|
204
|
-
return [`${prefix}${body[0]}`, ...body.slice(1).map((l) => `${" ".repeat(hang)}${l}`)].join("\n");
|
|
205
|
-
};
|
|
206
|
-
const tableRows = (rows, o = {}) => {
|
|
207
|
-
const indent = o.indent ?? 2;
|
|
208
|
-
const gap = o.gap ?? 2;
|
|
209
|
-
const cols = o.columns ?? [];
|
|
210
|
-
const n = Math.max(0, ...rows.map((r) => r.length));
|
|
211
|
-
let keep = Array.from({ length: n }, (_, i) => i);
|
|
212
|
-
const widthOf = (i) => Math.max(0, ...rows.map((r) => visibleWidth(r[i] ?? "")));
|
|
213
|
-
// The last column wraps under itself, so it only needs LAST_MIN columns to fit.
|
|
214
|
-
const total = (ks) => indent +
|
|
215
|
-
ks.reduce((a, i, k) => a + (k === ks.length - 1 ? Math.min(widthOf(i), LAST_MIN) : widthOf(i)), 0) +
|
|
216
|
-
gap * Math.max(0, ks.length - 1);
|
|
217
|
-
// Only a terminal drops columns: a pipe keeps everything for whoever reads it.
|
|
218
|
-
while (color && total(keep) > width) {
|
|
219
|
-
const droppable = keep.filter((i) => cols[i]?.drop !== undefined).sort((a, b) => cols[b].drop - cols[a].drop);
|
|
220
|
-
if (!droppable.length)
|
|
221
|
-
break;
|
|
222
|
-
keep = keep.filter((i) => i !== droppable[0]);
|
|
223
|
-
}
|
|
224
|
-
const widths = new Map(keep.map((i) => [i, widthOf(i)]));
|
|
225
|
-
return rows.map((r) => {
|
|
226
|
-
const cells = keep.map((i, k) => {
|
|
227
|
-
const cell = r[i] ?? "";
|
|
228
|
-
const w = widths.get(i);
|
|
229
|
-
if (cols[i]?.align === "right")
|
|
230
|
-
return padStart(cell, w);
|
|
231
|
-
return k === keep.length - 1 ? cell : padEnd(cell, w);
|
|
232
|
-
});
|
|
233
|
-
const head = " ".repeat(indent) +
|
|
234
|
-
cells
|
|
235
|
-
.slice(0, -1)
|
|
236
|
-
.map((c) => `${c}${" ".repeat(gap)}`)
|
|
237
|
-
.join("");
|
|
238
|
-
return wrapLine(head, cells.at(-1) ?? "")
|
|
239
|
-
.split("\n")
|
|
240
|
-
.map((l) => l.trimEnd());
|
|
241
|
-
});
|
|
242
|
-
};
|
|
243
|
-
const table = (rows, o) => tableRows(rows, o).flat();
|
|
244
|
-
return {
|
|
245
|
-
color,
|
|
246
|
-
icons,
|
|
247
|
-
width,
|
|
248
|
-
bold,
|
|
249
|
-
muted,
|
|
250
|
-
accent,
|
|
251
|
-
ok,
|
|
252
|
-
warn,
|
|
253
|
-
err,
|
|
254
|
-
icon,
|
|
255
|
-
lead: (name, text) => (ICONS[icons][name] ? `${icon(name)} ${text}` : text),
|
|
256
|
-
profile: (name, c) => color && c !== undefined ? `\x1b[48;5;${c};1;38;5;255m ${name} \x1b[0m` : color ? bold(name) : name,
|
|
257
|
-
header: (title) => {
|
|
258
|
-
if (!color)
|
|
259
|
-
return title;
|
|
260
|
-
const rule = Math.max(0, Math.min(width, 80) - visibleWidth(title) - 1);
|
|
261
|
-
return `${bold(title)} ${faint("─".repeat(rule))}`;
|
|
262
|
-
},
|
|
263
|
-
kv: (key, value, keyWidth, indent = 2) => wrapLine(`${" ".repeat(indent)}${muted(padEnd(key, keyWidth))} `, value).trimEnd(),
|
|
264
|
-
table,
|
|
265
|
-
tableRows,
|
|
266
|
-
wrap: wrapLine,
|
|
267
|
-
note: (state, text, indent = 2) => wrapLine(`${" ".repeat(indent)}${padEnd(icon(state), iconWidth)} `, text),
|
|
268
|
-
bar: (pct, cells = 10) => {
|
|
269
|
-
const p = Math.min(Math.max(pct, 0), 100);
|
|
270
|
-
const filled = Math.round((p * cells) / 100);
|
|
271
|
-
const paint = { ok, warn, err }[level(p)];
|
|
272
|
-
// A heavy rule on a terminal (colored, the rest faint); blocks in plain text, where only shape tells.
|
|
273
|
-
const [on, off] = color ? ["━", "━"] : ["█", "░"];
|
|
274
|
-
return `${paint(on.repeat(filled))}${faint(off.repeat(cells - filled))} ${paint(`${p.toFixed(0)}%`.padStart(4))}`;
|
|
275
|
-
},
|
|
276
|
-
check: (state, text) => `${padEnd(icon(state), iconWidth)} ${text}`,
|
|
277
|
-
};
|
|
278
|
-
}
|
|
279
|
-
const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
280
|
-
/**
|
|
281
|
-
* A spinner with the elapsed time on stderr, for steps that wait (the gateway starting). Only on
|
|
282
|
-
* a terminal with colors: anywhere else it draws nothing, and the caller prints the result line.
|
|
283
|
-
*/
|
|
284
|
-
export function spinner(io, text, opts = {}) {
|
|
285
|
-
const now = opts.now ?? Date.now;
|
|
286
|
-
const start = now();
|
|
287
|
-
const tty = opts.tty ?? Boolean(process.stderr.isTTY);
|
|
288
|
-
const write = opts.write ?? ((s) => process.stderr.write(s));
|
|
289
|
-
if (!tty || !colors(io))
|
|
290
|
-
return { update: () => { }, stop: () => now() - start };
|
|
291
|
-
let current = text;
|
|
292
|
-
let frame = 0;
|
|
293
|
-
const draw = () => {
|
|
294
|
-
const secs = ((now() - start) / 1000).toFixed(1);
|
|
295
|
-
write(`\r\x1b[K\x1b[36m${FRAMES[frame++ % FRAMES.length]}\x1b[0m ${current} \x1b[2m${secs}s\x1b[0m`);
|
|
296
|
-
};
|
|
297
|
-
draw();
|
|
298
|
-
const timer = setInterval(draw, 80);
|
|
299
|
-
timer.unref?.();
|
|
300
|
-
return {
|
|
301
|
-
update: (t) => {
|
|
302
|
-
current = t;
|
|
303
|
-
},
|
|
304
|
-
stop: () => {
|
|
305
|
-
clearInterval(timer);
|
|
306
|
-
write("\r\x1b[K");
|
|
307
|
-
return now() - start;
|
|
308
|
-
},
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
24
|
//# sourceMappingURL=style.js.map
|
package/dist/cli/topics.js
CHANGED
|
@@ -44,6 +44,23 @@ const ENVIRONMENT = [
|
|
|
44
44
|
["TURNO_NO_UPDATE_NOTIFIER", "1: same as TURNO_NO_UPDATE_CHECK (the name gh uses)"],
|
|
45
45
|
["TURNO_REGISTRY", "npm registry for update checks (default: npm_config_registry, else registry.npmjs.org)"],
|
|
46
46
|
["TURNO_BROWSER_IDLE", "turno-browser closes Chrome after this long without a tool call (default: 15m)"],
|
|
47
|
+
[
|
|
48
|
+
"TURNO_BROWSER_BACKGROUND_IDLE",
|
|
49
|
+
"the background server of a browser identity exits after this long without a request (default: 15m; 0: never)",
|
|
50
|
+
],
|
|
51
|
+
["TURNO_LIGHTPANDA_PATH", "the Lightpanda binary turno-browser's read mode runs (default: lightpanda on PATH)"],
|
|
52
|
+
[
|
|
53
|
+
"TURNO_BROWSER_SESSION",
|
|
54
|
+
"the session name of turno browser do (same as --session; default: default), to keep several apart",
|
|
55
|
+
],
|
|
56
|
+
[
|
|
57
|
+
"TURNO_CAMOUFOX_PATH",
|
|
58
|
+
"the Camoufox executable turno-browser's camoufox engine runs (default: the newest in Camoufox's cache)",
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
"TURNO_PLAYWRIGHT_CORE",
|
|
62
|
+
"the playwright-core folder the camoufox engine runs on (default: the one next to turno-browser)",
|
|
63
|
+
],
|
|
47
64
|
],
|
|
48
65
|
},
|
|
49
66
|
{
|
package/dist/core/paths.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { join } from "node:path";
|
|
2
2
|
import { browserPaths, turnoRoots } from "turno-core/paths";
|
|
3
3
|
// The roots, private folders and PATH lookup are shared with turno-browser.
|
|
4
|
-
export { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE, ensurePrivateDir, findOnPath, isPrivatePath, privateRoots, resetPrivateDirCache, writePrivateFile, } from "turno-core/paths";
|
|
4
|
+
export { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE, ensurePrivateDir, findOnPath, isPrivatePath, privateRoots, resetPrivateDirCache, tildify, writePrivateFile, } from "turno-core/paths";
|
|
5
5
|
export function paths(env) {
|
|
6
6
|
const { home, config, state, data } = turnoRoots(env);
|
|
7
7
|
return {
|
|
@@ -36,13 +36,6 @@ export function paths(env) {
|
|
|
36
36
|
export function profileDir(name, env) {
|
|
37
37
|
return join(paths(env).home, `.claude-${name}`);
|
|
38
38
|
}
|
|
39
|
-
/** Replace the home prefix with ~ for display. */
|
|
40
|
-
export function tildify(p, env) {
|
|
41
|
-
// HOME may come with a trailing slash or doubled slashes (HOME=/Users/a/): compare normalized.
|
|
42
|
-
const home = normalize(paths(env).home).replace(/(.)\/+$/, "$1");
|
|
43
|
-
const q = isAbsolute(p) ? normalize(p) : p;
|
|
44
|
-
return q === home ? "~" : home !== "/" && q.startsWith(`${home}/`) ? `~${q.slice(home.length)}` : p;
|
|
45
|
-
}
|
|
46
39
|
/** Expand a leading ~ to the home directory. */
|
|
47
40
|
export function expandHome(p, env) {
|
|
48
41
|
const home = paths(env).home;
|
package/dist/mcpd/health.js
CHANGED
|
@@ -97,7 +97,7 @@ async function portChecks(env) {
|
|
|
97
97
|
return out;
|
|
98
98
|
}
|
|
99
99
|
const ours = await answersAsMcpd(port, token);
|
|
100
|
-
const owner = await portOwner(port, { env });
|
|
100
|
+
const owner = await portOwner(port, { env, ...(ours?.pid !== undefined ? { pid: ours.pid } : {}) });
|
|
101
101
|
const uid = process.getuid?.();
|
|
102
102
|
if (ours) {
|
|
103
103
|
if (owner?.uid !== undefined && uid !== undefined && owner.uid !== uid) {
|
package/dist/mcpd/instances.js
CHANGED
|
@@ -6,6 +6,7 @@ import { promisify } from "node:util";
|
|
|
6
6
|
import { effectiveLaunch, variantKey } from "../core/manifest.js";
|
|
7
7
|
import { fingerprint } from "./cache.js";
|
|
8
8
|
import { ErrorCode, rpcError } from "./jsonrpc.js";
|
|
9
|
+
import { SESSION_CAPABILITY, SESSION_ENDED, sessionIdOf } from "./middleware.js";
|
|
9
10
|
import { LoginRequiredError } from "./oauth.js";
|
|
10
11
|
import { effectiveIdle } from "./pressure.js";
|
|
11
12
|
import { Upstream } from "./upstream.js";
|
|
@@ -97,6 +98,13 @@ export function descriptionFingerprint(server, variant) {
|
|
|
97
98
|
b: server.builtin ?? null,
|
|
98
99
|
});
|
|
99
100
|
}
|
|
101
|
+
/** A server that declared `SESSION_CAPABILITY` hears that a session it served ended. */
|
|
102
|
+
function tellSessionEnded(inst, session) {
|
|
103
|
+
const experimental = inst.upstream.init?.capabilities.experimental;
|
|
104
|
+
if (typeof experimental !== "object" || experimental === null || !(SESSION_CAPABILITY in experimental))
|
|
105
|
+
return;
|
|
106
|
+
inst.upstream.notify(SESSION_ENDED, { session });
|
|
107
|
+
}
|
|
100
108
|
/**
|
|
101
109
|
* Fingerprint of the whole definition (launch, scope, cwd, exclusive, idle, lease, timeouts),
|
|
102
110
|
* except who gets it and which tools they see: tool filters apply per request, so a
|
|
@@ -157,11 +165,13 @@ export class InstanceManager {
|
|
|
157
165
|
/** A client session ended: cancel its calls, drop it from every instance and release its leases. */
|
|
158
166
|
detach(client) {
|
|
159
167
|
this.clients.delete(client);
|
|
168
|
+
const session = sessionIdOf(client);
|
|
160
169
|
for (const inst of this.instances.values()) {
|
|
161
170
|
const cancelled = inst.upstream.forgetClient(client);
|
|
162
171
|
if (cancelled)
|
|
163
172
|
this.opts.log?.(`cancelled ${cancelled} call(s) of a closed session on ${inst.upstream.label}`);
|
|
164
|
-
inst.clients.delete(client)
|
|
173
|
+
if (inst.clients.delete(client) && session)
|
|
174
|
+
tellSessionEnded(inst, session);
|
|
165
175
|
for (const [uri, subs] of inst.subscriptions) {
|
|
166
176
|
if (subs.delete(client) && subs.size === 0) {
|
|
167
177
|
inst.subscriptions.delete(uri);
|
package/dist/mcpd/middleware.js
CHANGED
|
@@ -23,6 +23,19 @@ import { ErrorCode, rpcError } from "./jsonrpc.js";
|
|
|
23
23
|
* import, erased at build): if the two ever differ, the typecheck fails.
|
|
24
24
|
*/
|
|
25
25
|
export const SESSION_META = "io.turno/session";
|
|
26
|
+
/**
|
|
27
|
+
* A server that declares this experimental capability in `initialize` hears, with
|
|
28
|
+
* `SESSION_ENDED`, when a client session that made a call to it ends (`InstanceManager.detach`).
|
|
29
|
+
* turno-browser uses it to free that session's tabs at once (`"sharing": "tabs"`).
|
|
30
|
+
*/
|
|
31
|
+
export const SESSION_CAPABILITY = "io.turno/sessions";
|
|
32
|
+
export const SESSION_ENDED = "notifications/io.turno/session_ended";
|
|
33
|
+
/** The session id of each client handle, made on its first call. */
|
|
34
|
+
const sessionIds = new WeakMap();
|
|
35
|
+
/** The id the `session` stage gave this client, or undefined when it made no call. */
|
|
36
|
+
export function sessionIdOf(client) {
|
|
37
|
+
return sessionIds.get(client);
|
|
38
|
+
}
|
|
26
39
|
const BUILT_IN = ["filter", "calllog", "session"];
|
|
27
40
|
const STAGE_NAME = /^[a-z][a-z0-9-]*$/;
|
|
28
41
|
/** A profile's tool filter for a client's server, or undefined when it filters nothing. */
|
|
@@ -84,14 +97,13 @@ function callLogStage(sink) {
|
|
|
84
97
|
* against mcpd. A value the client sent under the same key is replaced.
|
|
85
98
|
*/
|
|
86
99
|
function sessionStage() {
|
|
87
|
-
const ids = new WeakMap();
|
|
88
100
|
return {
|
|
89
101
|
name: "session",
|
|
90
102
|
run(call, next) {
|
|
91
|
-
let id =
|
|
103
|
+
let id = sessionIds.get(call.client);
|
|
92
104
|
if (!id) {
|
|
93
105
|
id = randomUUID();
|
|
94
|
-
|
|
106
|
+
sessionIds.set(call.client, id);
|
|
95
107
|
}
|
|
96
108
|
const meta = typeof call.params._meta === "object" && call.params._meta !== null
|
|
97
109
|
? call.params._meta
|
package/dist/mcpd/peerpid.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import { readFileSync, readdirSync, readlinkSync } from "node:fs";
|
|
3
|
+
import { connect } from "node:net";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { promisify } from "node:util";
|
|
5
6
|
const run = promisify(execFile);
|
|
@@ -104,7 +105,39 @@ export async function peerPid(remotePort, self = process.pid, opts = {}) {
|
|
|
104
105
|
}
|
|
105
106
|
return lsofPeer(remotePort, self, opts.env ?? process.env);
|
|
106
107
|
}
|
|
107
|
-
/**
|
|
108
|
+
/** Whether something accepts TCP connections on 127.0.0.1:`port`, the address mcpd binds. */
|
|
109
|
+
function listensOn(port, timeoutMs = 1000) {
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
const socket = connect({ host: "127.0.0.1", port });
|
|
112
|
+
const done = (yes) => {
|
|
113
|
+
socket.destroy();
|
|
114
|
+
resolve(yes);
|
|
115
|
+
};
|
|
116
|
+
socket.setTimeout(timeoutMs, () => done(true)); // accepted the SYN but slow to finish: busy
|
|
117
|
+
socket.once("connect", () => done(true));
|
|
118
|
+
socket.once("error", () => done(false));
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
function parseLsof(stdout) {
|
|
122
|
+
const owner = {};
|
|
123
|
+
for (const line of stdout.split("\n")) {
|
|
124
|
+
if (line.startsWith("p") && owner.pid === undefined)
|
|
125
|
+
owner.pid = Number(line.slice(1));
|
|
126
|
+
else if (line.startsWith("c") && owner.command === undefined)
|
|
127
|
+
owner.command = line.slice(1);
|
|
128
|
+
else if (line.startsWith("u") && owner.uid === undefined)
|
|
129
|
+
owner.uid = Number(line.slice(1));
|
|
130
|
+
}
|
|
131
|
+
return owner.pid !== undefined ? owner : undefined;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Which process listens on 127.0.0.1:`port`, as far as this user can tell. Undefined when the
|
|
135
|
+
* port is free; an empty object when something listens but the system cannot say what.
|
|
136
|
+
*
|
|
137
|
+
* On macOS lsof scans every process and can take many seconds on a busy machine, so a free port
|
|
138
|
+
* is told by a refused connection without lsof, and `opts.pid` (a process expected to hold the
|
|
139
|
+
* port) is checked alone first.
|
|
140
|
+
*/
|
|
108
141
|
export async function portOwner(port, opts = {}) {
|
|
109
142
|
const platform = opts.platform ?? process.platform;
|
|
110
143
|
if (platform === "linux") {
|
|
@@ -123,25 +156,26 @@ export async function portOwner(port, opts = {}) {
|
|
|
123
156
|
return { uid: row.uid, ...(pid ? { pid } : {}), ...(command ? { command } : {}) };
|
|
124
157
|
}
|
|
125
158
|
}
|
|
126
|
-
|
|
127
|
-
const { stdout } = await run("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-Fpcu"], {
|
|
128
|
-
timeout: 3000,
|
|
129
|
-
env: opts.env ?? process.env,
|
|
130
|
-
});
|
|
131
|
-
const owner = {};
|
|
132
|
-
for (const line of stdout.split("\n")) {
|
|
133
|
-
if (line.startsWith("p") && owner.pid === undefined)
|
|
134
|
-
owner.pid = Number(line.slice(1));
|
|
135
|
-
else if (line.startsWith("c") && owner.command === undefined)
|
|
136
|
-
owner.command = line.slice(1);
|
|
137
|
-
else if (line.startsWith("u") && owner.uid === undefined)
|
|
138
|
-
owner.uid = Number(line.slice(1));
|
|
139
|
-
}
|
|
140
|
-
return owner.pid !== undefined ? owner : undefined;
|
|
141
|
-
}
|
|
142
|
-
catch {
|
|
159
|
+
if (!(await listensOn(port)))
|
|
143
160
|
return undefined;
|
|
161
|
+
const lsof = async (args) => {
|
|
162
|
+
try {
|
|
163
|
+
const { stdout } = await run("lsof", ["-nP", ...args, `-iTCP:${port}`, "-sTCP:LISTEN", "-Fpcu"], {
|
|
164
|
+
timeout: 3000,
|
|
165
|
+
env: opts.env ?? process.env,
|
|
166
|
+
});
|
|
167
|
+
return parseLsof(stdout);
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return undefined; // lsof exits 1 when it finds nothing; also missing or too slow
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
if (opts.pid !== undefined) {
|
|
174
|
+
const own = await lsof(["-a", "-p", String(opts.pid)]);
|
|
175
|
+
if (own)
|
|
176
|
+
return own;
|
|
144
177
|
}
|
|
178
|
+
return (await lsof([])) ?? {};
|
|
145
179
|
}
|
|
146
180
|
/** "node (pid 123, uid 501)" for messages. */
|
|
147
181
|
export function describeOwner(o) {
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "turno-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "turno-cli",
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.5.0",
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@modelcontextprotocol/sdk": "1.32.2",
|
|
13
13
|
"smol-toml": "1.9.1",
|
|
14
|
-
"turno-core": "0.
|
|
14
|
+
"turno-core": "0.2.0"
|
|
15
15
|
},
|
|
16
16
|
"bin": {
|
|
17
17
|
"turno": "dist/cli/main.js"
|
|
@@ -1014,8 +1014,8 @@
|
|
|
1014
1014
|
}
|
|
1015
1015
|
},
|
|
1016
1016
|
"node_modules/turno-core": {
|
|
1017
|
-
"version": "0.
|
|
1018
|
-
"resolved": "https://registry.npmjs.org/turno-core/-/turno-core-0.
|
|
1017
|
+
"version": "0.2.0",
|
|
1018
|
+
"resolved": "https://registry.npmjs.org/turno-core/-/turno-core-0.2.0.tgz",
|
|
1019
1019
|
"license": "Apache-2.0",
|
|
1020
1020
|
"engines": {
|
|
1021
1021
|
"node": ">=22.11"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "turno-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Switch accounts in your coding agents and keep the conversation, the MCP sign-ins and the browser session.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude-code",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@modelcontextprotocol/sdk": "1.32.2",
|
|
49
49
|
"smol-toml": "1.9.1",
|
|
50
|
-
"turno-core": "0.
|
|
50
|
+
"turno-core": "0.2.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/node": "^26.6.6",
|
|
54
|
-
"turno-browser": "0.
|
|
54
|
+
"turno-browser": "0.5.0",
|
|
55
55
|
"turno-fonts": "0.1.2",
|
|
56
56
|
"typescript": "^7.0.2",
|
|
57
57
|
"vitest": "^5.1.0"
|