tmuxes 0.1.7 → 0.1.9
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/.node-version +1 -0
- package/.nvmrc +1 -0
- package/.tmp-npm-cache/_cacache/content-v2/sha512/43/27/5e000b8b9c56a6ccc66f709485499f4304e2cb1982582ba571321c07b3ef56fcabd2c671898cc8003365a0485b6fd8e73e7b17b073cec0f7d1628c1a99df +0 -0
- package/.tmp-npm-cache/_cacache/content-v2/sha512/51/cf/4301295d74559ed494bae160d54d8741077f89faebb311882ac065019246951e7b53f3dcb913793c42b331e14c7070c4810c3cdc27a427d103a7db4614e0 +0 -0
- package/.tmp-npm-cache/_cacache/content-v2/sha512/c3/4d/d68a454a916e74c2617f586fbf770981b33811d667c2547eb0e9fc21938f4ee7e98f1ceee4bde8ad8815b5f6efe21b60eee798837d68f51a3340d7e5bb7a +0 -0
- package/.tmp-npm-cache/_cacache/content-v2/sha512/fe/40/2abfbefc96299e8bf714aa91d62607190ae299e102cf5933db2e2904640d65d25d67dbbb6fa2ddc92a17f00b9dbfdf2e37487f67d96ec36c64a285b59a7d +0 -0
- package/.tmp-npm-cache/_cacache/index-v5/27/fe/81a3de6ce7ae3d1e41a3421de20c5629998c4ee5d0ffe2037630f03b03b2 +4 -0
- package/.tmp-npm-cache/_cacache/index-v5/65/22/dd66711f62681fce09aabb2357a2907b4a0c778ac5227c4baf9603fd86e8 +4 -0
- package/.tmp-npm-cache/_update-notifier-last-checked +0 -0
- package/AGENTS.md +15 -0
- package/CLAUDE.md +3 -0
- package/LICENSE +21 -21
- package/README.en.md +304 -0
- package/README.md +301 -289
- package/SECURITY.md +31 -0
- package/{public → client}/index.html +12 -13
- package/client/package.json +29 -0
- package/client/src/App.tsx +123 -0
- package/client/src/activity.ts +5 -0
- package/client/src/api.ts +130 -0
- package/client/src/attention.tsx +157 -0
- package/client/src/components/FileExplorer.tsx +156 -0
- package/client/src/components/FileViewer.tsx +194 -0
- package/client/src/components/SessionRow.tsx +108 -0
- package/client/src/components/SessionTree.tsx +197 -0
- package/client/src/components/SettingsButton.tsx +122 -0
- package/client/src/components/Sidebar.tsx +96 -0
- package/client/src/components/StatusBanner.tsx +31 -0
- package/client/src/components/TargetGroup.tsx +275 -0
- package/client/src/components/TerminalPanel.tsx +192 -0
- package/client/src/folders.ts +245 -0
- package/client/src/hooks/useTerminal.ts +67 -0
- package/client/src/hooks/useTmuxSocket.ts +65 -0
- package/client/src/i18n.ts +213 -0
- package/client/src/main.tsx +17 -0
- package/client/src/settings.tsx +87 -0
- package/client/src/styles.css +723 -0
- package/client/src/types.ts +93 -0
- package/client/src/util.ts +65 -0
- package/client/tsconfig.json +13 -0
- package/client/vite.config.ts +15 -0
- package/fig/fig1.png +0 -0
- package/package.json +28 -61
- package/scripts/prepack.mjs +35 -0
- package/{bin → server/bin}/tmuxes.js +36 -36
- package/server/package.json +61 -0
- package/server/src/agentHooks.ts +120 -0
- package/server/src/agentOutput.ts +36 -0
- package/server/src/agentState.ts +70 -0
- package/server/src/config.ts +31 -0
- package/server/src/exe.ts +34 -0
- package/server/src/exec.ts +61 -0
- package/server/src/files.ts +330 -0
- package/server/src/foldersStore.ts +114 -0
- package/server/src/index.ts +114 -0
- package/server/src/logger.ts +16 -0
- package/{dist/monitor.js → server/src/monitor.ts} +10 -9
- package/server/src/openBrowser.ts +28 -0
- package/{dist/platform.js → server/src/platform.ts} +4 -5
- package/server/src/rest/router.ts +290 -0
- package/server/src/targetCommand.ts +79 -0
- package/server/src/targets.ts +152 -0
- package/server/src/tmux/builder.ts +198 -0
- package/server/src/tmux/formats.ts +95 -0
- package/server/src/tmux/sessions.ts +204 -0
- package/server/src/validate.ts +79 -0
- package/server/src/windowsSsh.ts +239 -0
- package/server/src/winshell/manager.ts +296 -0
- package/server/src/ws/protocol.ts +15 -0
- package/server/src/ws/sshState.ts +36 -0
- package/server/src/ws/terminalSession.ts +207 -0
- package/server/src/ws/wsServer.ts +153 -0
- package/server/src/wsl.ts +38 -0
- package/server/test/agentHooks.test.ts +66 -0
- package/server/test/agentOutput.test.ts +26 -0
- package/server/test/agentState.test.ts +24 -0
- package/server/test/builder.test.ts +162 -0
- package/server/test/files.test.ts +81 -0
- package/server/test/formats.test.ts +123 -0
- package/server/test/monitor.test.ts +25 -0
- package/server/test/validate.test.ts +71 -0
- package/server/test/wsl.test.ts +18 -0
- package/server/tsconfig.json +9 -0
- package/server/vitest.config.ts +12 -0
- package/start.cmd +30 -0
- package/start.command +20 -0
- package/start.sh +20 -0
- package/tsconfig.base.json +19 -0
- package/dist/agentHooks.js +0 -91
- package/dist/agentHooks.js.map +0 -1
- package/dist/agentOutput.js +0 -30
- package/dist/agentOutput.js.map +0 -1
- package/dist/agentState.js +0 -45
- package/dist/agentState.js.map +0 -1
- package/dist/config.js +0 -32
- package/dist/config.js.map +0 -1
- package/dist/exe.js +0 -37
- package/dist/exe.js.map +0 -1
- package/dist/exec.js +0 -43
- package/dist/exec.js.map +0 -1
- package/dist/files.js +0 -243
- package/dist/files.js.map +0 -1
- package/dist/foldersStore.js +0 -103
- package/dist/foldersStore.js.map +0 -1
- package/dist/index.js +0 -117
- package/dist/index.js.map +0 -1
- package/dist/logger.js +0 -16
- package/dist/logger.js.map +0 -1
- package/dist/monitor.js.map +0 -1
- package/dist/openBrowser.js +0 -31
- package/dist/openBrowser.js.map +0 -1
- package/dist/platform.js.map +0 -1
- package/dist/rest/router.js +0 -190
- package/dist/rest/router.js.map +0 -1
- package/dist/targetCommand.js +0 -41
- package/dist/targetCommand.js.map +0 -1
- package/dist/targets.js +0 -131
- package/dist/targets.js.map +0 -1
- package/dist/tmux/builder.js +0 -173
- package/dist/tmux/builder.js.map +0 -1
- package/dist/tmux/formats.js +0 -61
- package/dist/tmux/formats.js.map +0 -1
- package/dist/tmux/sessions.js +0 -157
- package/dist/tmux/sessions.js.map +0 -1
- package/dist/validate.js +0 -65
- package/dist/validate.js.map +0 -1
- package/dist/winshell/manager.js +0 -267
- package/dist/winshell/manager.js.map +0 -1
- package/dist/ws/protocol.js +0 -4
- package/dist/ws/protocol.js.map +0 -1
- package/dist/ws/sshState.js +0 -35
- package/dist/ws/sshState.js.map +0 -1
- package/dist/ws/terminalSession.js +0 -204
- package/dist/ws/terminalSession.js.map +0 -1
- package/dist/ws/wsServer.js +0 -151
- package/dist/ws/wsServer.js.map +0 -1
- package/dist/wsl.js +0 -35
- package/dist/wsl.js.map +0 -1
- package/public/assets/index-BpVrfoZw.js +0 -44
- package/public/assets/index-D_X5SnGx.css +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { isValidHostAlias, parseHostSpec } from './validate.js';
|
|
5
|
+
import { isWindows } from './platform.js';
|
|
6
|
+
import { listWslDistros } from './wsl.js';
|
|
7
|
+
import { winShell } from './winshell/manager.js';
|
|
8
|
+
import { log } from './logger.js';
|
|
9
|
+
|
|
10
|
+
export interface Target {
|
|
11
|
+
/** Stable, URL-safe id used in REST paths and the WS query. */
|
|
12
|
+
id: string;
|
|
13
|
+
kind: 'local' | 'ssh' | 'wsl' | 'winlocal';
|
|
14
|
+
label: string;
|
|
15
|
+
/** ssh destination host or config-alias (ssh targets only). */
|
|
16
|
+
host?: string;
|
|
17
|
+
/** ssh user (undefined when a ~/.ssh/config alias supplies it). */
|
|
18
|
+
user?: string;
|
|
19
|
+
/** ssh port (undefined when default / config supplies it). */
|
|
20
|
+
port?: number;
|
|
21
|
+
/** WSL distro name (wsl targets only). */
|
|
22
|
+
distro?: string;
|
|
23
|
+
/** Launchable shells (winlocal target only). */
|
|
24
|
+
shells?: { id: string; label: string }[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Native Windows shells (PowerShell/cmd via ConPTY). Enabled on Windows, or
|
|
28
|
+
* anywhere via TMUXES_FAKE_WINSHELL for testing the path on Linux/macOS. */
|
|
29
|
+
const WINSHELL_ENABLED = isWindows || !!process.env.TMUXES_FAKE_WINSHELL;
|
|
30
|
+
|
|
31
|
+
function winlocalTarget(): Target {
|
|
32
|
+
return {
|
|
33
|
+
id: 'winlocal',
|
|
34
|
+
kind: 'winlocal',
|
|
35
|
+
label: isWindows ? 'Windows (local)' : 'Local shell (fake)',
|
|
36
|
+
shells: winShell.listShells(),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const ID_RE = /^[A-Za-z0-9._-]{1,128}$/;
|
|
41
|
+
|
|
42
|
+
export function isValidTargetId(id: unknown): id is string {
|
|
43
|
+
return typeof id === 'string' && ID_RE.test(id);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function slug(s: string): string {
|
|
47
|
+
return s.replace(/[^A-Za-z0-9._-]/g, '-');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const LOCAL: Target = { id: 'local', kind: 'local', label: 'Local' };
|
|
51
|
+
|
|
52
|
+
/** Best-effort, read-only parse of ~/.ssh/config Host aliases (no wildcards). */
|
|
53
|
+
function parseSshConfig(): Target[] {
|
|
54
|
+
const path = join(homedir(), '.ssh', 'config');
|
|
55
|
+
let text: string;
|
|
56
|
+
try {
|
|
57
|
+
text = readFileSync(path, 'utf8');
|
|
58
|
+
} catch {
|
|
59
|
+
return []; // no config file is normal
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const targets: Target[] = [];
|
|
63
|
+
const seen = new Set<string>();
|
|
64
|
+
for (const rawLine of text.split('\n')) {
|
|
65
|
+
const line = rawLine.trim();
|
|
66
|
+
if (!line || line.startsWith('#')) continue;
|
|
67
|
+
const m = /^Host\s+(.+)$/i.exec(line);
|
|
68
|
+
if (!m) continue;
|
|
69
|
+
for (const token of m[1].split(/\s+/)) {
|
|
70
|
+
// Skip wildcard / negated patterns — they aren't concrete hosts.
|
|
71
|
+
if (token.includes('*') || token.includes('?') || token.startsWith('!')) continue;
|
|
72
|
+
if (!isValidHostAlias(token) || seen.has(token)) continue;
|
|
73
|
+
seen.add(token);
|
|
74
|
+
targets.push({ id: `cfg-${slug(token)}`, kind: 'ssh', label: token, host: token });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return targets;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Parse the TMUXES_HOSTS="alice@web1,bob@db2:2222" override. */
|
|
81
|
+
function parseEnvHosts(): Target[] {
|
|
82
|
+
const env = process.env.TMUXES_HOSTS;
|
|
83
|
+
if (!env) return [];
|
|
84
|
+
const targets: Target[] = [];
|
|
85
|
+
for (const part of env.split(',')) {
|
|
86
|
+
const spec = parseHostSpec(part);
|
|
87
|
+
if (!spec) {
|
|
88
|
+
log.warn(`ignoring invalid TMUXES_HOSTS entry: ${part.trim()}`);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const label = `${spec.user ? `${spec.user}@` : ''}${spec.host}${spec.port ? `:${spec.port}` : ''}`;
|
|
92
|
+
targets.push({
|
|
93
|
+
id: `env-${slug(label)}`,
|
|
94
|
+
kind: 'ssh',
|
|
95
|
+
label,
|
|
96
|
+
host: spec.host,
|
|
97
|
+
user: spec.user,
|
|
98
|
+
port: spec.port,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return targets;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function sshTargets(): Target[] {
|
|
105
|
+
const all = [...parseEnvHosts(), ...parseSshConfig()];
|
|
106
|
+
const byId = new Map<string, Target>();
|
|
107
|
+
for (const t of all) if (!byId.has(t.id)) byId.set(t.id, t);
|
|
108
|
+
return [...byId.values()];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The synchronous part of the target list (everything except WSL discovery). */
|
|
112
|
+
function baseTargets(): Target[] {
|
|
113
|
+
const base: Target[] = [];
|
|
114
|
+
if (WINSHELL_ENABLED) base.push(winlocalTarget());
|
|
115
|
+
// Windows has no native tmux; its "local" machine is reached through WSL.
|
|
116
|
+
if (!isWindows) base.push(LOCAL);
|
|
117
|
+
return [...base, ...sshTargets()];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Cache so the (async, Windows-only) WSL discovery doesn't run on the WS
|
|
121
|
+
// upgrade path. The client always GETs /api/targets first, which refreshes it.
|
|
122
|
+
let cachedTargets: Target[] = baseTargets();
|
|
123
|
+
|
|
124
|
+
/** Recompute the full target list, including WSL distros on Windows. */
|
|
125
|
+
export async function refreshTargets(): Promise<Target[]> {
|
|
126
|
+
if (isWindows) {
|
|
127
|
+
const distros = await listWslDistros();
|
|
128
|
+
const wsl: Target[] = distros.map((name) => ({
|
|
129
|
+
id: `wsl-${slug(name)}`,
|
|
130
|
+
kind: 'wsl',
|
|
131
|
+
label: name,
|
|
132
|
+
distro: name,
|
|
133
|
+
}));
|
|
134
|
+
cachedTargets = [winlocalTarget(), ...wsl, ...sshTargets()];
|
|
135
|
+
} else {
|
|
136
|
+
cachedTargets = baseTargets();
|
|
137
|
+
}
|
|
138
|
+
return cachedTargets;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function listTargets(): Target[] {
|
|
142
|
+
return cachedTargets;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function getTarget(id: string): Target | undefined {
|
|
146
|
+
return cachedTargets.find((t) => t.id === id);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** ssh destination string: "user@host" or just the config alias / host. */
|
|
150
|
+
export function sshDestination(t: Target): string {
|
|
151
|
+
return t.user ? `${t.user}@${t.host}` : `${t.host}`;
|
|
152
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdirSync } from 'node:fs';
|
|
3
|
+
import { homedir, tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { config } from '../config.js';
|
|
6
|
+
import { sshDestination, type Target } from '../targets.js';
|
|
7
|
+
import { isWindows } from '../platform.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Single-quote a string for a POSIX shell. Needed ONLY for the remote ssh path:
|
|
11
|
+
* ssh concatenates the remote command words with spaces and re-parses them
|
|
12
|
+
* through the remote login shell, so each tmux arg must survive that re-parse.
|
|
13
|
+
* Local commands use pure argv (no shell) and need no quoting.
|
|
14
|
+
*/
|
|
15
|
+
export function sshQuote(arg: string): string {
|
|
16
|
+
return `'${arg.replace(/'/g, `'\\''`)}'`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Local tmux management/attach argv — passed straight to spawn (shell:false). */
|
|
20
|
+
export function localTmux(sub: string[]): string[] {
|
|
21
|
+
return ['tmux', ...sub];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function sshControlDir(): string | null {
|
|
25
|
+
// OpenSSH connection sharing avoids repeated TCP/auth handshakes for sidebar
|
|
26
|
+
// polling and file operations. Windows OpenSSH accepts the config but fails
|
|
27
|
+
// at runtime on mux sockets, so Windows management uses an app-owned SSH
|
|
28
|
+
// session instead of ControlMaster.
|
|
29
|
+
if (isWindows) return null;
|
|
30
|
+
|
|
31
|
+
const uid = process.getuid?.() ?? 'user';
|
|
32
|
+
const dir = join(tmpdir(), `tmuxes-ssh-${uid}`);
|
|
33
|
+
try {
|
|
34
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
return dir;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function sshControlPath(t: Target): string | null {
|
|
42
|
+
const dir = sshControlDir();
|
|
43
|
+
if (!dir) return null;
|
|
44
|
+
const key = `${sshDestination(t)}:${t.port ?? 22}:${t.id}`;
|
|
45
|
+
return join(dir, createHash('sha1').update(key).digest('hex'));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function sshMultiplexArgs(t: Target): string[] {
|
|
49
|
+
const path = sshControlPath(t);
|
|
50
|
+
if (!path) return [];
|
|
51
|
+
|
|
52
|
+
return [
|
|
53
|
+
'-o',
|
|
54
|
+
'ControlMaster=auto',
|
|
55
|
+
'-o',
|
|
56
|
+
`ControlPath=${path}`,
|
|
57
|
+
'-o',
|
|
58
|
+
`ControlPersist=${config.ssh.controlPersist}`,
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function sshClientArgs(
|
|
63
|
+
t: Target,
|
|
64
|
+
opts: { tty?: boolean; batchMode?: boolean; connectTimeout: number; multiplex?: boolean },
|
|
65
|
+
): string[] {
|
|
66
|
+
const portArgs = t.port ? ['-p', String(t.port)] : [];
|
|
67
|
+
return [
|
|
68
|
+
'ssh',
|
|
69
|
+
...(opts.tty ? ['-tt'] : []),
|
|
70
|
+
...(opts.batchMode ? ['-o', 'BatchMode=yes'] : []),
|
|
71
|
+
'-o',
|
|
72
|
+
`ConnectTimeout=${opts.connectTimeout}`,
|
|
73
|
+
...(opts.multiplex === false ? ['-o', 'ControlMaster=no'] : sshMultiplexArgs(t)),
|
|
74
|
+
...portArgs,
|
|
75
|
+
sshDestination(t),
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Remote tmux argv via the system ssh binary.
|
|
81
|
+
* - tty:false (management) → BatchMode + short ConnectTimeout so it fails fast
|
|
82
|
+
* and never hangs on a prompt. Remote args are sshQuote'd.
|
|
83
|
+
* - tty:true (interactive attach) → -tt forces a remote PTY (and SIGWINCH
|
|
84
|
+
* propagation). BatchMode is left default so host-key/agent prompts surface
|
|
85
|
+
* in the terminal. tmuxes does not force ServerAliveInterval; users can set
|
|
86
|
+
* keepalives in ~/.ssh/config if their site allows them. Remote args are NOT
|
|
87
|
+
* quoted here: this argv is handed to a PTY where the remote command words go
|
|
88
|
+
* to ssh as separate argv elements and our inputs are already allowlist-
|
|
89
|
+
* validated.
|
|
90
|
+
*/
|
|
91
|
+
export function remoteTmux(
|
|
92
|
+
t: Target,
|
|
93
|
+
sub: string[],
|
|
94
|
+
opts: { tty: boolean; multiplex?: boolean },
|
|
95
|
+
): string[] {
|
|
96
|
+
if (opts.tty) {
|
|
97
|
+
return [
|
|
98
|
+
...sshClientArgs(t, {
|
|
99
|
+
tty: true,
|
|
100
|
+
connectTimeout: config.ssh.connectTimeoutTty,
|
|
101
|
+
multiplex: opts.multiplex,
|
|
102
|
+
}),
|
|
103
|
+
'tmux',
|
|
104
|
+
...sub,
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return [
|
|
109
|
+
...sshClientArgs(t, {
|
|
110
|
+
batchMode: true,
|
|
111
|
+
connectTimeout: config.ssh.connectTimeoutMgmt,
|
|
112
|
+
multiplex: opts.multiplex,
|
|
113
|
+
}),
|
|
114
|
+
'tmux',
|
|
115
|
+
...sub.map(sshQuote),
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* tmux inside a WSL distro (Windows host). We use `--exec` (NOT `--`): `--`
|
|
121
|
+
* runs the command through the distro's login shell, which would re-parse tmux
|
|
122
|
+
* format strings (`#{...}` is a comment, `|` a pipe) and mangle them; `--exec`
|
|
123
|
+
* execs the binary directly with no shell, so argv maps straight through with
|
|
124
|
+
* no quoting — like the local path. The interactive case sets TERM via `env`
|
|
125
|
+
* because WSL does not inherit the Windows TERM.
|
|
126
|
+
*/
|
|
127
|
+
export function wslTmux(distro: string, sub: string[], opts: { tty: boolean }): string[] {
|
|
128
|
+
const prefix = opts.tty ? ['env', 'TERM=xterm-256color', 'tmux'] : ['tmux'];
|
|
129
|
+
return ['wsl.exe', '-d', distro, '--exec', ...prefix, ...sub];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Build a non-PTY argv to run an ARBITRARY command on a target (tmux, ls, cat,
|
|
134
|
+
* display-message, …). Same transport rules as management tmux: local runs the
|
|
135
|
+
* argv directly, wsl uses `--exec` (no shell), ssh uses BatchMode + sshQuote.
|
|
136
|
+
*/
|
|
137
|
+
export function commandArgv(
|
|
138
|
+
t: Target,
|
|
139
|
+
argv: string[],
|
|
140
|
+
opts: { multiplex?: boolean } = {},
|
|
141
|
+
): { file: string; args: string[] } {
|
|
142
|
+
let full: string[];
|
|
143
|
+
if (t.kind === 'local') {
|
|
144
|
+
full = argv;
|
|
145
|
+
} else if (t.kind === 'wsl') {
|
|
146
|
+
full = ['wsl.exe', '-d', t.distro ?? '', '--exec', ...argv];
|
|
147
|
+
} else {
|
|
148
|
+
full = [
|
|
149
|
+
...sshClientArgs(t, {
|
|
150
|
+
batchMode: true,
|
|
151
|
+
connectTimeout: config.ssh.connectTimeoutMgmt,
|
|
152
|
+
multiplex: opts.multiplex,
|
|
153
|
+
}),
|
|
154
|
+
...argv.map(sshQuote),
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
return { file: full[0], args: full.slice(1) };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Build a management argv (no PTY) for a local / ssh / wsl target. */
|
|
161
|
+
export function managementArgv(
|
|
162
|
+
t: Target,
|
|
163
|
+
sub: string[],
|
|
164
|
+
opts: { multiplex?: boolean } = {},
|
|
165
|
+
): { file: string; args: string[] } {
|
|
166
|
+
return commandArgv(t, ['tmux', ...sub], opts);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Build the create-a-new-session argv, always starting in the user's HOME:
|
|
171
|
+
* - local → tmux `-c <homedir>` (explicit, regardless of the server's cwd),
|
|
172
|
+
* - wsl → `wsl --cd ~` runs the command from the distro's home; tmux inherits,
|
|
173
|
+
* - ssh → the remote command already runs from the remote `$HOME`; tmux inherits.
|
|
174
|
+
* `sub` is the new-session subcommand (named or auto-named `-P` form).
|
|
175
|
+
*/
|
|
176
|
+
export function newSessionArgv(
|
|
177
|
+
t: Target,
|
|
178
|
+
sub: string[],
|
|
179
|
+
opts: { multiplex?: boolean } = {},
|
|
180
|
+
): { file: string; args: string[] } {
|
|
181
|
+
if (t.kind === 'local') {
|
|
182
|
+
return { file: 'tmux', args: [...sub, '-c', homedir()] };
|
|
183
|
+
}
|
|
184
|
+
if (t.kind === 'wsl') {
|
|
185
|
+
return { file: 'wsl.exe', args: ['-d', t.distro ?? '', '--cd', '~', '--exec', 'tmux', ...sub] };
|
|
186
|
+
}
|
|
187
|
+
return commandArgv(t, ['tmux', ...sub], opts);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Build an interactive attach argv (run inside a PTY) for a local / ssh / wsl target. */
|
|
191
|
+
export function attachArgv(t: Target, session: string): { file: string; args: string[] } {
|
|
192
|
+
const sub = ['new-session', '-A', '-s', session]; // NO -d: the PTY supplies the terminal
|
|
193
|
+
let argv: string[];
|
|
194
|
+
if (t.kind === 'local') argv = localTmux(sub);
|
|
195
|
+
else if (t.kind === 'wsl') argv = wslTmux(t.distro ?? '', sub, { tty: true });
|
|
196
|
+
else argv = remoteTmux(t, sub, { tty: true });
|
|
197
|
+
return { file: argv[0], args: argv.slice(1) };
|
|
198
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** tmux -F format strings + parsers.
|
|
2
|
+
*
|
|
3
|
+
* tmux ESCAPES control characters in -F output (e.g. 0x1f comes back as the
|
|
4
|
+
* literal text "\037"), so we cannot use a control byte as a separator. We use
|
|
5
|
+
* a printable "|" and place the only free-form field (the name) LAST, then
|
|
6
|
+
* rejoin the remainder — robust even if a name itself contains "|", because
|
|
7
|
+
* the leading numeric fields never do. */
|
|
8
|
+
|
|
9
|
+
import { AGENT_OPTION, parseAgentValue, type AgentKind, type AgentState, type AttentionReason } from '../agentState.js';
|
|
10
|
+
|
|
11
|
+
const SEP = '|';
|
|
12
|
+
|
|
13
|
+
export const SESSION_FORMAT = [
|
|
14
|
+
'#{session_windows}',
|
|
15
|
+
'#{session_attached}',
|
|
16
|
+
'#{session_created}',
|
|
17
|
+
'#{session_activity}', // epoch from tmux; kept for display/debug
|
|
18
|
+
`#{${AGENT_OPTION}}`, // agent hook state: "<kind>:<state>:<reason>:<event>:<nonce>"
|
|
19
|
+
'#{session_name}', // free-form → must be last
|
|
20
|
+
].join(SEP);
|
|
21
|
+
|
|
22
|
+
export const WINDOW_FORMAT = [
|
|
23
|
+
'#{window_index}',
|
|
24
|
+
'#{window_panes}',
|
|
25
|
+
'#{window_active}',
|
|
26
|
+
'#{window_name}', // free-form → must be last
|
|
27
|
+
].join(SEP);
|
|
28
|
+
|
|
29
|
+
export interface SessionInfo {
|
|
30
|
+
name: string;
|
|
31
|
+
windows: number;
|
|
32
|
+
attached: boolean;
|
|
33
|
+
/** unix epoch seconds */
|
|
34
|
+
created: number;
|
|
35
|
+
/** unix epoch seconds from tmux; kept for display/debug. */
|
|
36
|
+
lastActivity: number;
|
|
37
|
+
/** Recognized agent whose hooks are driving status. */
|
|
38
|
+
agentKind?: AgentKind;
|
|
39
|
+
/** Agent lifecycle state from official hooks. */
|
|
40
|
+
agentState?: AgentState;
|
|
41
|
+
/** Why this session is asking for attention, when known. */
|
|
42
|
+
attentionReason?: AttentionReason;
|
|
43
|
+
/** Hook event that last updated the state. */
|
|
44
|
+
agentEvent?: string;
|
|
45
|
+
/** Monotonic-ish event token for client edge detection. */
|
|
46
|
+
agentNonce?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WindowInfo {
|
|
50
|
+
index: number;
|
|
51
|
+
name: string;
|
|
52
|
+
panes: number;
|
|
53
|
+
active: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function parseSessions(stdout: string): SessionInfo[] {
|
|
57
|
+
return stdout
|
|
58
|
+
.split('\n')
|
|
59
|
+
.filter((l) => l.length > 0)
|
|
60
|
+
.map((line) => {
|
|
61
|
+
const parts = line.split(SEP);
|
|
62
|
+
const agent = parseAgentValue(parts[4] || '');
|
|
63
|
+
return {
|
|
64
|
+
windows: Number(parts[0]) || 0,
|
|
65
|
+
attached: Number(parts[1]) > 0,
|
|
66
|
+
created: Number(parts[2]) || 0,
|
|
67
|
+
lastActivity: Number(parts[3]) || 0,
|
|
68
|
+
...agent,
|
|
69
|
+
name: parts.slice(5).join(SEP), // name may legitimately contain "|"
|
|
70
|
+
};
|
|
71
|
+
})
|
|
72
|
+
.filter((s) => s.name);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function parseWindows(stdout: string): WindowInfo[] {
|
|
76
|
+
return stdout
|
|
77
|
+
.split('\n')
|
|
78
|
+
.filter((l) => l.length > 0)
|
|
79
|
+
.map((line) => {
|
|
80
|
+
const parts = line.split(SEP);
|
|
81
|
+
return {
|
|
82
|
+
index: Number(parts[0]) || 0,
|
|
83
|
+
panes: Number(parts[1]) || 0,
|
|
84
|
+
active: Number(parts[2]) > 0,
|
|
85
|
+
name: parts.slice(3).join(SEP),
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** stderr patterns that mean "no sessions" rather than a real error. */
|
|
91
|
+
const EMPTY_RE = /no server running|no sessions|error connecting to/i;
|
|
92
|
+
|
|
93
|
+
export function isEmptySessionsError(stderr: string): boolean {
|
|
94
|
+
return EMPTY_RE.test(stderr);
|
|
95
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { managementArgv, newSessionArgv } from './builder.js';
|
|
2
|
+
import type { Target } from '../targets.js';
|
|
3
|
+
import { runTargetCommand } from '../targetCommand.js';
|
|
4
|
+
import {
|
|
5
|
+
AGENT_OPTION,
|
|
6
|
+
agentValue,
|
|
7
|
+
agentInitialValue,
|
|
8
|
+
parseAgentValue,
|
|
9
|
+
} from '../agentState.js';
|
|
10
|
+
import { augmentAgentCommand } from '../agentHooks.js';
|
|
11
|
+
import { classifyAgentTerminalError } from '../agentOutput.js';
|
|
12
|
+
import {
|
|
13
|
+
SESSION_FORMAT,
|
|
14
|
+
WINDOW_FORMAT,
|
|
15
|
+
parseSessions,
|
|
16
|
+
parseWindows,
|
|
17
|
+
isEmptySessionsError,
|
|
18
|
+
type SessionInfo,
|
|
19
|
+
type WindowInfo,
|
|
20
|
+
} from './formats.js';
|
|
21
|
+
import { isValidSessionName } from '../validate.js';
|
|
22
|
+
|
|
23
|
+
export type LaunchAgent = 'claude' | 'codex';
|
|
24
|
+
|
|
25
|
+
/** A management error carrying the HTTP status the router should return. */
|
|
26
|
+
export class TmuxError extends Error {
|
|
27
|
+
constructor(
|
|
28
|
+
public status: number,
|
|
29
|
+
message: string,
|
|
30
|
+
) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'TmuxError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Hard ceiling so a wedged ssh can never hang a request (ssh also has ConnectTimeout).
|
|
37
|
+
const REMOTE_TIMEOUT_MS = 15_000;
|
|
38
|
+
|
|
39
|
+
function timeoutFor(target: Target): number | undefined {
|
|
40
|
+
// Bound ssh (network) and wsl (possible cold start) so a request can't hang.
|
|
41
|
+
return target.kind === 'local' ? undefined : REMOTE_TIMEOUT_MS;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function run(target: Target, sub: string[]) {
|
|
45
|
+
return runTargetCommand(target, (opts) => managementArgv(target, sub, opts), {
|
|
46
|
+
timeoutMs: timeoutFor(target),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function firstStderrLine(stderr: string): string {
|
|
51
|
+
return stderr.trim().split('\n')[0] || 'command failed';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function listSessions(target: Target): Promise<SessionInfo[]> {
|
|
55
|
+
const r = await run(target, ['list-sessions', '-F', SESSION_FORMAT]);
|
|
56
|
+
if (r.code === 0) return reconcileAgentTerminalErrors(target, parseSessions(r.stdout));
|
|
57
|
+
// "no server running" / "no sessions" is the normal empty case.
|
|
58
|
+
if (isEmptySessionsError(r.stderr)) return [];
|
|
59
|
+
throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function reconcileAgentTerminalErrors(
|
|
63
|
+
target: Target,
|
|
64
|
+
sessions: SessionInfo[],
|
|
65
|
+
): Promise<SessionInfo[]> {
|
|
66
|
+
return Promise.all(sessions.map((session) => reconcileAgentTerminalError(target, session)));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function reconcileAgentTerminalError(
|
|
70
|
+
target: Target,
|
|
71
|
+
session: SessionInfo,
|
|
72
|
+
): Promise<SessionInfo> {
|
|
73
|
+
if (!session.agentKind || session.agentState !== 'running') return session;
|
|
74
|
+
|
|
75
|
+
const pane = await run(target, ['capture-pane', '-t', session.name, '-p', '-S', '-20']);
|
|
76
|
+
if (pane.code !== 0) return session;
|
|
77
|
+
|
|
78
|
+
const event = classifyAgentTerminalError(pane.stdout, session.agentKind);
|
|
79
|
+
if (!event) return session;
|
|
80
|
+
|
|
81
|
+
const value = agentValue(session.agentKind, 'idle', 'error', event, String(Date.now()));
|
|
82
|
+
const set = await run(target, ['set-option', '-t', session.name, '-q', AGENT_OPTION, value]);
|
|
83
|
+
if (set.code !== 0) return session;
|
|
84
|
+
|
|
85
|
+
const snapshot = parseAgentValue(value);
|
|
86
|
+
return snapshot ? { ...session, ...snapshot } : session;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function createSession(
|
|
90
|
+
target: Target,
|
|
91
|
+
opts: { name?: string; command?: string },
|
|
92
|
+
): Promise<{ name: string }> {
|
|
93
|
+
let name = opts.name;
|
|
94
|
+
|
|
95
|
+
// New sessions always start in the user's home directory (newSessionArgv).
|
|
96
|
+
if (name) {
|
|
97
|
+
if (!isValidSessionName(name)) throw new TmuxError(400, 'invalid session name');
|
|
98
|
+
const sessionName = name;
|
|
99
|
+
const r = await runTargetCommand(
|
|
100
|
+
target,
|
|
101
|
+
(opts) => newSessionArgv(target, ['new-session', '-d', '-s', sessionName], opts),
|
|
102
|
+
{ timeoutMs: timeoutFor(target) },
|
|
103
|
+
);
|
|
104
|
+
if (r.code !== 0) {
|
|
105
|
+
if (/duplicate session/i.test(r.stderr)) {
|
|
106
|
+
throw new TmuxError(409, `session "${name}" already exists`);
|
|
107
|
+
}
|
|
108
|
+
throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
// Let tmux assign a numeric name and report it back.
|
|
112
|
+
const r = await runTargetCommand(
|
|
113
|
+
target,
|
|
114
|
+
(opts) => newSessionArgv(target, ['new-session', '-d', '-P', '-F', '#{session_name}'], opts),
|
|
115
|
+
{ timeoutMs: timeoutFor(target) },
|
|
116
|
+
);
|
|
117
|
+
if (r.code !== 0) throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
118
|
+
name = r.stdout.trim();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (opts.command && opts.command.length > 0) {
|
|
122
|
+
const augmented = augmentAgentCommand(opts.command);
|
|
123
|
+
if (augmented.kind) {
|
|
124
|
+
await run(target, [
|
|
125
|
+
'set-option',
|
|
126
|
+
'-t',
|
|
127
|
+
name,
|
|
128
|
+
'-q',
|
|
129
|
+
AGENT_OPTION,
|
|
130
|
+
agentInitialValue(augmented.kind),
|
|
131
|
+
]);
|
|
132
|
+
}
|
|
133
|
+
// Type the command literally, then press Enter. Two send-keys calls so the
|
|
134
|
+
// command text can never be misparsed as a key name.
|
|
135
|
+
await run(target, ['send-keys', '-t', name, '-l', augmented.command]);
|
|
136
|
+
await run(target, ['send-keys', '-t', name, 'Enter']);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return { name };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function launchAgentInSession(
|
|
143
|
+
target: Target,
|
|
144
|
+
name: string,
|
|
145
|
+
agent: LaunchAgent,
|
|
146
|
+
): Promise<void> {
|
|
147
|
+
const augmented = augmentAgentCommand(agent);
|
|
148
|
+
if (!augmented.kind) throw new TmuxError(400, 'unsupported agent');
|
|
149
|
+
|
|
150
|
+
const set = await run(target, [
|
|
151
|
+
'set-option',
|
|
152
|
+
'-t',
|
|
153
|
+
name,
|
|
154
|
+
'-q',
|
|
155
|
+
AGENT_OPTION,
|
|
156
|
+
agentInitialValue(augmented.kind),
|
|
157
|
+
]);
|
|
158
|
+
if (set.code !== 0) {
|
|
159
|
+
if (/can't find session|session not found|no server running/i.test(set.stderr)) {
|
|
160
|
+
throw new TmuxError(404, `session "${name}" not found`);
|
|
161
|
+
}
|
|
162
|
+
throw new TmuxError(502, firstStderrLine(set.stderr));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const send = await run(target, ['send-keys', '-t', name, '-l', augmented.command]);
|
|
166
|
+
if (send.code !== 0) throw new TmuxError(502, firstStderrLine(send.stderr));
|
|
167
|
+
const enter = await run(target, ['send-keys', '-t', name, 'Enter']);
|
|
168
|
+
if (enter.code !== 0) throw new TmuxError(502, firstStderrLine(enter.stderr));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function renameSession(
|
|
172
|
+
target: Target,
|
|
173
|
+
name: string,
|
|
174
|
+
newName: string,
|
|
175
|
+
): Promise<void> {
|
|
176
|
+
if (!isValidSessionName(newName)) throw new TmuxError(400, 'invalid new session name');
|
|
177
|
+
const r = await run(target, ['rename-session', '-t', name, newName]);
|
|
178
|
+
if (r.code === 0) return;
|
|
179
|
+
if (/can't find session|session not found/i.test(r.stderr)) {
|
|
180
|
+
throw new TmuxError(404, `session "${name}" not found`);
|
|
181
|
+
}
|
|
182
|
+
if (/duplicate session/i.test(r.stderr)) {
|
|
183
|
+
throw new TmuxError(409, `session "${newName}" already exists`);
|
|
184
|
+
}
|
|
185
|
+
throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export async function killSession(target: Target, name: string): Promise<void> {
|
|
189
|
+
const r = await run(target, ['kill-session', '-t', name]);
|
|
190
|
+
if (r.code === 0) return;
|
|
191
|
+
if (/can't find session|session not found|no server running/i.test(r.stderr)) {
|
|
192
|
+
throw new TmuxError(404, `session "${name}" not found`);
|
|
193
|
+
}
|
|
194
|
+
throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export async function listWindows(target: Target, name: string): Promise<WindowInfo[]> {
|
|
198
|
+
const r = await run(target, ['list-windows', '-t', name, '-F', WINDOW_FORMAT]);
|
|
199
|
+
if (r.code === 0) return parseWindows(r.stdout);
|
|
200
|
+
if (/can't find session|session not found|no server running/i.test(r.stderr)) {
|
|
201
|
+
throw new TmuxError(404, `session "${name}" not found`);
|
|
202
|
+
}
|
|
203
|
+
throw new TmuxError(502, firstStderrLine(r.stderr));
|
|
204
|
+
}
|