@yanzc/claude-pro 0.9.0 → 0.9.1
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/README.md +342 -320
- package/package.json +1 -2
- package/src/agent.js +79 -13
- package/src/agents.js +3 -2
- package/src/api.js +39 -34
- package/src/browser/broker.js +367 -0
- package/src/browser/cdp.js +114 -0
- package/src/browser/render.js +242 -0
- package/src/browser/session.js +948 -0
- package/src/browser/snapshot.js +212 -0
- package/src/cache.js +1 -1
- package/src/checkpoints.js +1 -0
- package/src/commands.js +169 -67
- package/src/compaction.js +75 -0
- package/src/config.js +143 -143
- package/src/context.js +45 -18
- package/src/filestate.js +2 -1
- package/src/gateway.js +208 -164
- package/src/hooks.js +2 -2
- package/src/keepalive.js +1 -1
- package/src/main.js +52 -40
- package/src/mcp.js +2 -2
- package/src/peers.js +1 -10
- package/src/print.js +9 -3
- package/src/prompt-profiles.js +0 -6
- package/src/prompts.js +5 -61
- package/src/ratelimit.js +2 -4
- package/src/remote.js +26 -10
- package/src/runtime.js +33 -10
- package/src/selfcheck.js +3 -3
- package/src/session.js +93 -3
- package/src/supervisor.js +10 -1
- package/src/tools/bash.js +18 -17
- package/src/tools/browser.js +365 -0
- package/src/tools/code.js +430 -0
- package/src/tools/effort.js +3 -4
- package/src/tools/files.js +92 -19
- package/src/tools/index.js +6 -0
- package/src/tools/notebook.js +11 -4
- package/src/tools/processes.js +274 -0
- package/src/tools/search.js +33 -8
- package/src/tools/web.js +316 -46
- package/src/trace.js +0 -4
- package/src/ui/ansi.js +92 -13
- package/src/ui/dialogs.js +16 -54
- package/src/ui/editor.js +231 -34
- package/src/ui/markdown.js +1 -2
- package/src/ui/render.js +1 -2
- package/src/ui/repl.js +185 -104
- package/src/ui/screen.js +139 -31
- package/src/updater.js +135 -165
- package/src/utils/clipboard.js +5 -3
- package/src/utils/paths.js +15 -17
- package/src/utils/shell.js +66 -11
- package/.env.example +0 -29
package/src/updater.js
CHANGED
|
@@ -1,165 +1,135 @@
|
|
|
1
|
-
// Manual update support for `npm i -g claude-pro`: the interactive session checks npm and installs a release only
|
|
2
|
-
// when `/update`
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
import fs from 'node:fs';
|
|
7
|
-
import path from 'node:path';
|
|
8
|
-
import https from 'node:https';
|
|
9
|
-
import { spawn } from 'node:child_process';
|
|
10
|
-
import { CONFIG_DIR, INSTALL_DIR, VERSION } from './config.js';
|
|
11
|
-
import { killTree } from './utils/shell.js';
|
|
12
|
-
|
|
13
|
-
// The name on npm (scoped: npm refused plain `claude-pro` as too close to another package's name).
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
if (!
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Install `version` with npm. Resolves with {status: 'updated'|'installed'|'locked'|'failed', version, error}:
|
|
139
|
-
// 'locked' while another session of the machine is installing.
|
|
140
|
-
export async function installUpdate(version, { onInstalling } = {}) {
|
|
141
|
-
if (!takeLock()) return { status: 'locked', version };
|
|
142
|
-
try {
|
|
143
|
-
if (!isNewer(version, installedVersion())) return { status: 'installed', version: installedVersion() };
|
|
144
|
-
return await install(version, onInstalling);
|
|
145
|
-
} finally {
|
|
146
|
-
releaseLock();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async function install(latest, onInstalling) {
|
|
151
|
-
// Only a global install is updated by `npm i -g`: one in a project's node_modules belongs to that project.
|
|
152
|
-
const root = await runNpm(['root', '-g'], { timeoutMs: 30_000 });
|
|
153
|
-
if (!root.ok || path.resolve(root.output.split(/\r?\n/).pop()) !== path.resolve(modulesDir())) {
|
|
154
|
-
return { status: 'failed', version: latest, error: `this copy (${INSTALL_DIR}) is not the global npm install; update it with npm yourself` };
|
|
155
|
-
}
|
|
156
|
-
onInstalling?.(latest);
|
|
157
|
-
// --prefer-online: not npm's own cache of the package's versions, which may not know this one yet
|
|
158
|
-
const r = await runNpm(['install', '-g', '--prefer-online', `${PACKAGE_NAME}@${latest}`]);
|
|
159
|
-
if (!r.ok) {
|
|
160
|
-
writeState({ ...readState(), checkedAt: Date.now() - CHECK_EVERY_MS + RETRY_FAILED_MS }); // again in a minute
|
|
161
|
-
return { status: 'failed', version: latest, error: r.output.split('\n').slice(-5).join('\n') };
|
|
162
|
-
}
|
|
163
|
-
writeState({ ...readState(), installed: latest });
|
|
164
|
-
return { status: 'updated', version: latest };
|
|
165
|
-
}
|
|
1
|
+
// Manual update support for `npm i -g claude-pro`: the interactive session checks npm and installs a release only
|
|
2
|
+
// when `/update` asks for it (App.updateFromNpm). Installation is serialized by a lock file and the session loads
|
|
3
|
+
// the new package with a restart (the conversation carries over). A git checkout (the development copy) is never
|
|
4
|
+
// touched.
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import https from 'node:https';
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { CONFIG_DIR, INSTALL_DIR, VERSION } from './config.js';
|
|
11
|
+
import { killTree, processAlive as alive } from './utils/shell.js';
|
|
12
|
+
|
|
13
|
+
// The name on npm (scoped: npm refused plain `claude-pro` as too close to another package's name).
|
|
14
|
+
const PACKAGE_NAME = JSON.parse(fs.readFileSync(path.join(INSTALL_DIR, 'package.json'), 'utf8')).name;
|
|
15
|
+
const LOCK_FILE = path.join(CONFIG_DIR, 'update.lock');
|
|
16
|
+
const LOCK_STALE_MS = 15 * 60_000;
|
|
17
|
+
|
|
18
|
+
// The node_modules directory a package is installed in: one level up, or two for a scoped (@scope/name) package.
|
|
19
|
+
function modulesDir(dir = INSTALL_DIR) {
|
|
20
|
+
const up = path.dirname(dir);
|
|
21
|
+
return path.basename(up).startsWith('@') ? path.dirname(up) : up;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Installed by npm (a package in some node_modules), not run from a source checkout.
|
|
25
|
+
export function installedByNpm(dir = INSTALL_DIR) {
|
|
26
|
+
return path.basename(modulesDir(dir)) === 'node_modules' && !fs.existsSync(path.join(dir, '.git')) && !/[\\/]_npx[\\/]/.test(dir);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 1.2.10 > 1.2.9; a prerelease (1.3.0-beta) is never taken for newer.
|
|
30
|
+
export function isNewer(latest, current) {
|
|
31
|
+
if (!/^\d+\.\d+\.\d+$/.test(String(latest))) return false;
|
|
32
|
+
const a = latest.split('.').map(Number);
|
|
33
|
+
const b = String(current).split('-')[0].split('.').map(Number);
|
|
34
|
+
for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] > (b[i] || 0);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The version installed on disk now (another session may have updated it since this one started).
|
|
39
|
+
export function installedVersion(dir = INSTALL_DIR) {
|
|
40
|
+
try { return JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')).version; } catch { return VERSION; }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// One session of the machine installs at a time. A lock left by a session that died, or one that is very old, is taken over.
|
|
44
|
+
function takeLock() {
|
|
45
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
46
|
+
try {
|
|
47
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
48
|
+
fs.writeFileSync(LOCK_FILE, JSON.stringify({ pid: process.pid, at: Date.now() }), { flag: 'wx' });
|
|
49
|
+
return true;
|
|
50
|
+
} catch (e) {
|
|
51
|
+
if (e.code !== 'EEXIST') return false;
|
|
52
|
+
let held = {};
|
|
53
|
+
try { held = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8')); } catch { /* unreadable: stale */ }
|
|
54
|
+
if (alive(held.pid) && Date.now() - (held.at || 0) < LOCK_STALE_MS) return false;
|
|
55
|
+
try { fs.unlinkSync(LOCK_FILE); } catch { /* someone else took it over */ }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function releaseLock() {
|
|
62
|
+
try {
|
|
63
|
+
const held = JSON.parse(fs.readFileSync(LOCK_FILE, 'utf8'));
|
|
64
|
+
if (held.pid === process.pid) fs.unlinkSync(LOCK_FILE);
|
|
65
|
+
} catch { /* gone already */ }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function latestVersion({ timeoutMs = 10_000 } = {}) {
|
|
69
|
+
return new Promise((resolve, reject) => {
|
|
70
|
+
// past the registry's CDN cache, which can answer with the version before the one just published
|
|
71
|
+
const url = `https://registry.npmjs.org/${PACKAGE_NAME.replace('/', '%2f')}/latest?t=${Date.now()}`;
|
|
72
|
+
const req = https.get(url, { headers: { accept: 'application/json', 'cache-control': 'no-cache' }, timeout: timeoutMs }, (res) => {
|
|
73
|
+
let body = '';
|
|
74
|
+
res.setEncoding('utf8');
|
|
75
|
+
res.on('data', (d) => { body += d; });
|
|
76
|
+
res.on('end', () => {
|
|
77
|
+
if (res.statusCode !== 200) return reject(new Error(`npm registry: HTTP ${res.statusCode}`));
|
|
78
|
+
try { resolve(JSON.parse(body).version); } catch (e) { reject(e); }
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
req.on('timeout', () => req.destroy(new Error('npm registry did not answer')));
|
|
82
|
+
req.on('error', reject);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function runNpm(args, { timeoutMs = 300_000 } = {}) {
|
|
87
|
+
return new Promise((resolve) => {
|
|
88
|
+
const child = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', args, { shell: process.platform === 'win32', windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
89
|
+
let out = '';
|
|
90
|
+
const take = (d) => { out = (out + d).slice(-200_000); };
|
|
91
|
+
child.stdout.on('data', take);
|
|
92
|
+
child.stderr.on('data', take);
|
|
93
|
+
// With shell: true, child.kill() ends only cmd.exe: npm kept the pipes open, 'close' never came and the
|
|
94
|
+
// timeout did nothing. The whole tree goes.
|
|
95
|
+
const timer = setTimeout(() => { killTree(child); }, timeoutMs);
|
|
96
|
+
child.on('error', (e) => { clearTimeout(timer); resolve({ ok: false, output: e.message }); });
|
|
97
|
+
child.on('close', (code) => { clearTimeout(timer); resolve({ ok: code === 0, output: out.trim() }); });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Look for a newer version on npm. Resolves with {status: 'dev'|'current'|'available'|'installed', version}:
|
|
102
|
+
// 'available' to be installed (installUpdate), 'installed' when it is on disk already (another session installed
|
|
103
|
+
// it: a restart loads it). (The background checks every few minutes, with their state file and switches, are gone:
|
|
104
|
+
// /update is the only caller.)
|
|
105
|
+
export async function checkForUpdate() {
|
|
106
|
+
if (!installedByNpm()) return { status: 'dev' };
|
|
107
|
+
const latest = await latestVersion();
|
|
108
|
+
if (!isNewer(latest, VERSION)) return { status: 'current', version: latest };
|
|
109
|
+
if (!isNewer(latest, installedVersion())) return { status: 'installed', version: installedVersion() };
|
|
110
|
+
return { status: 'available', version: latest };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Install `version` with npm. Resolves with {status: 'updated'|'installed'|'locked'|'failed', version, error}:
|
|
114
|
+
// 'locked' while another session of the machine is installing.
|
|
115
|
+
export async function installUpdate(version) {
|
|
116
|
+
if (!takeLock()) return { status: 'locked', version };
|
|
117
|
+
try {
|
|
118
|
+
if (!isNewer(version, installedVersion())) return { status: 'installed', version: installedVersion() };
|
|
119
|
+
return await install(version);
|
|
120
|
+
} finally {
|
|
121
|
+
releaseLock();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function install(latest) {
|
|
126
|
+
// Only a global install is updated by `npm i -g`: one in a project's node_modules belongs to that project.
|
|
127
|
+
const root = await runNpm(['root', '-g'], { timeoutMs: 30_000 });
|
|
128
|
+
if (!root.ok || path.resolve(root.output.split(/\r?\n/).pop()) !== path.resolve(modulesDir())) {
|
|
129
|
+
return { status: 'failed', version: latest, error: `this copy (${INSTALL_DIR}) is not the global npm install; update it with npm yourself` };
|
|
130
|
+
}
|
|
131
|
+
// --prefer-online: not npm's own cache of the package's versions, which may not know this one yet
|
|
132
|
+
const r = await runNpm(['install', '-g', '--prefer-online', `${PACKAGE_NAME}@${latest}`]);
|
|
133
|
+
if (!r.ok) return { status: 'failed', version: latest, error: r.output.split('\n').slice(-5).join('\n') };
|
|
134
|
+
return { status: 'updated', version: latest };
|
|
135
|
+
}
|
package/src/utils/clipboard.js
CHANGED
|
@@ -5,8 +5,8 @@ import fs from 'node:fs';
|
|
|
5
5
|
import os from 'node:os';
|
|
6
6
|
import path from 'node:path';
|
|
7
7
|
import { spawn } from 'node:child_process';
|
|
8
|
+
import { MAX_IMAGE_BYTES } from './images.js';
|
|
8
9
|
|
|
9
|
-
export const MAX_IMAGE_BYTES = 3_750_000; // ~5MB once base64 encoded
|
|
10
10
|
const IMAGE_EXT = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp' };
|
|
11
11
|
|
|
12
12
|
function run(cmd, args, { env, timeout = 15_000 } = {}) {
|
|
@@ -75,7 +75,8 @@ export function imageFromFile(file) {
|
|
|
75
75
|
let st;
|
|
76
76
|
try { st = fs.statSync(file); } catch { return { error: `File not found: ${file}` }; }
|
|
77
77
|
if (!st.isFile()) return { error: `Not a file: ${file}` };
|
|
78
|
-
|
|
78
|
+
// the turn scales it down to what the API takes (fitImagesIn), as it does a Read image: a 5 MB phone photo is fine
|
|
79
|
+
if (st.size > 30 * 1024 * 1024) return { error: `Image is too large (${(st.size / 1e6).toFixed(1)}MB, max 30MB)` };
|
|
79
80
|
return { image: { media_type: media, data: fs.readFileSync(file).toString('base64'), bytes: st.size, source: file } };
|
|
80
81
|
}
|
|
81
82
|
|
|
@@ -125,7 +126,8 @@ async function readLinux() {
|
|
|
125
126
|
child.on('close', (code) => { clearTimeout(timer); resolve(code === 0 ? Buffer.concat(chunks) : null); });
|
|
126
127
|
});
|
|
127
128
|
if (r?.length) {
|
|
128
|
-
|
|
129
|
+
// scaled down when it is sent (fitImagesIn), as a dropped image file is
|
|
130
|
+
if (r.length > 30 * 1024 * 1024) return { error: `Image is too large (${(r.length / 1e6).toFixed(1)}MB, max 30MB)` };
|
|
129
131
|
return { image: { media_type: 'image/png', data: r.toString('base64'), bytes: r.length } };
|
|
130
132
|
}
|
|
131
133
|
}
|
package/src/utils/paths.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import os from 'node:os';
|
|
4
|
+
import { knownShell } from './shell.js';
|
|
4
5
|
|
|
5
6
|
const isWin = process.platform === 'win32';
|
|
6
7
|
|
|
7
|
-
// Where Git for Windows is installed (its `/`), from the Git Bash the Bash tool uses
|
|
8
|
+
// Where Git for Windows is installed (its `/`), from the Git Bash the Bash tool uses (findShell: also where `where
|
|
9
|
+
// git` and the settings point, so a Git in another place than Program Files counts too); before that one is known,
|
|
10
|
+
// from the usual places. null when not found.
|
|
8
11
|
let gitRoot;
|
|
9
12
|
function gitBashRoot() {
|
|
10
13
|
if (gitRoot !== undefined) return gitRoot;
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
const shell = knownShell();
|
|
15
|
+
const candidates = shell ? [shell.kind === 'bash' ? shell.path : null] : [process.env.CLAUDE_CODE_GIT_BASH_PATH, 'C:\\Program Files\\Git\\bin\\bash.exe', 'C:\\Program Files (x86)\\Git\\bin\\bash.exe'];
|
|
16
|
+
let root = null;
|
|
17
|
+
for (const bash of candidates) {
|
|
18
|
+
if (bash && fs.existsSync(bash)) { root = path.dirname(path.dirname(bash)); break; }
|
|
15
19
|
}
|
|
16
|
-
|
|
20
|
+
if (shell) gitRoot = root; // kept once the shell is known; looked for again until then
|
|
21
|
+
return root;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
// A Git Bash path as Windows sees it, after its mount table (`mount`): /c/... is drive C:, /tmp is the Windows
|
|
@@ -45,20 +50,13 @@ export function displayPath(p, cwd) {
|
|
|
45
50
|
const rel = path.relative(cwd, p);
|
|
46
51
|
if (!rel) return '.';
|
|
47
52
|
if (!rel.startsWith('..') && !path.isAbsolute(rel)) return rel;
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
// inside the home directory (C:\Users\yzc2 is not, and the drive letter may be in either case)
|
|
54
|
+
const fromHome = path.relative(os.homedir(), p);
|
|
55
|
+
if (!fromHome) return '~';
|
|
56
|
+
if (!fromHome.startsWith('..') && !path.isAbsolute(fromHome)) return `~${path.sep}${fromHome}`;
|
|
50
57
|
return p;
|
|
51
58
|
}
|
|
52
59
|
|
|
53
|
-
export function isInside(child, parent) {
|
|
54
|
-
const rel = path.relative(parent, child);
|
|
55
|
-
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function samePath(a, b) {
|
|
59
|
-
return isWin ? path.resolve(a).toLowerCase() === path.resolve(b).toLowerCase() : path.resolve(a) === path.resolve(b);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
60
|
// `input` as an absolute directory: "" (or ~) is the home directory, "D:" is the root of D:, and forward slashes do
|
|
63
61
|
// on Windows too. null when it is not absolute.
|
|
64
62
|
export function normalizeDir(input, { platform = process.platform, home = os.homedir() } = {}) {
|
package/src/utils/shell.js
CHANGED
|
@@ -7,6 +7,14 @@ import { OutputDecoder } from './encoding.js';
|
|
|
7
7
|
const isWin = process.platform === 'win32';
|
|
8
8
|
let shellCache;
|
|
9
9
|
|
|
10
|
+
// The shell findShell found, or undefined before it was looked for.
|
|
11
|
+
export const knownShell = () => shellCache;
|
|
12
|
+
|
|
13
|
+
// Whether a process with this id runs (EPERM: it does, but is not ours to signal).
|
|
14
|
+
export function processAlive(pid) {
|
|
15
|
+
try { process.kill(pid, 0); return true; } catch (e) { return e.code === 'EPERM'; }
|
|
16
|
+
}
|
|
17
|
+
|
|
10
18
|
// Locate the shell used by the Bash tool. On Windows we need Git Bash, like Claude Code.
|
|
11
19
|
export function findShell(config) {
|
|
12
20
|
if (shellCache) return shellCache;
|
|
@@ -63,7 +71,18 @@ export function parsePsRows(text) {
|
|
|
63
71
|
// member of its process group (a child whose parent already exited is no longer linked to it).
|
|
64
72
|
export function msysTree(rows, rootPid) {
|
|
65
73
|
const root = rows.find((r) => r.pid === rootPid);
|
|
66
|
-
if (!root)
|
|
74
|
+
if (!root) {
|
|
75
|
+
// The shell has ended. What it left running in the background stays in its process group, which it led, and has
|
|
76
|
+
// no parent any more (PPID 1): those, and what they started. A group that only has the same number by now is
|
|
77
|
+
// not taken: its leader has a parent.
|
|
78
|
+
const group = rows.filter((r) => r.pgid === rootPid);
|
|
79
|
+
const take = new Set(group.filter((r) => r.ppid === 1).map((r) => r.pid));
|
|
80
|
+
for (let grew = true; grew;) {
|
|
81
|
+
grew = false;
|
|
82
|
+
for (const r of group) if (!take.has(r.pid) && take.has(r.ppid)) { take.add(r.pid); grew = true; }
|
|
83
|
+
}
|
|
84
|
+
return group.filter((r) => take.has(r.pid)).map((r) => r.winpid);
|
|
85
|
+
}
|
|
67
86
|
const children = new Map();
|
|
68
87
|
for (const r of rows) {
|
|
69
88
|
if (!children.has(r.ppid)) children.set(r.ppid, []);
|
|
@@ -118,6 +137,22 @@ function msysDescendants(child, { sync }) {
|
|
|
118
137
|
return runAsync(ps, ['-W'], { timeout: 5000 }).then((r) => (r.code === 0 ? msysTree(parsePsRows(r.out), root) : []));
|
|
119
138
|
}
|
|
120
139
|
|
|
140
|
+
// The trimmed output of a command that succeeded, or null, as run() gives it, without blocking the event loop. An
|
|
141
|
+
// output past maxChars is null too. Never rejects.
|
|
142
|
+
export function runText(cmd, args, { cwd, timeout = 5000, maxChars = 1024 * 1024 } = {}) {
|
|
143
|
+
return new Promise((resolve) => {
|
|
144
|
+
let out = '';
|
|
145
|
+
let child;
|
|
146
|
+
try { child = spawn(cmd, args, { cwd, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }); } catch { resolve(null); return; }
|
|
147
|
+
const timer = setTimeout(() => { try { child.kill(); } catch { /* gone */ } }, timeout);
|
|
148
|
+
const done = (value) => { clearTimeout(timer); resolve(value); };
|
|
149
|
+
child.stdout.setEncoding('utf8');
|
|
150
|
+
child.stdout.on('data', (d) => { out += d; if (out.length > maxChars) try { child.kill(); } catch { /* gone */ } });
|
|
151
|
+
child.on('error', () => done(null));
|
|
152
|
+
child.on('close', (code) => done(code === 0 && out.length <= maxChars ? out.trim() : null));
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
121
156
|
// A command's exit code and output, without blocking the event loop. Never rejects.
|
|
122
157
|
function runAsync(cmd, args, { timeout = 5000 } = {}) {
|
|
123
158
|
return new Promise((resolve) => {
|
|
@@ -135,11 +170,17 @@ function runAsync(cmd, args, { timeout = 5000 } = {}) {
|
|
|
135
170
|
// Kill a process and all its children, without blocking the event loop (`ps -W` and taskkill take up to seconds,
|
|
136
171
|
// and Ctrl+C, a timeout or KillShell used to freeze the interface for that long). Resolves when done; never
|
|
137
172
|
// rejects. The tree has to be looked up before anything is killed: once its parents are gone the links are too.
|
|
138
|
-
|
|
139
|
-
|
|
173
|
+
// `orphans`: also when the process itself has ended, for what it left running (a background shell whose `server &`
|
|
174
|
+
// still holds its output open): only the processes found through it then, never its own pid, which may be another
|
|
175
|
+
// process's by now. Only for callers that know something of it still runs (a background shell not closed yet).
|
|
176
|
+
export async function killTree(child, { orphans = false } = {}) {
|
|
177
|
+
if (!child || child.killed) return;
|
|
178
|
+
const ended = child.exitCode !== null || child.signalCode !== null;
|
|
179
|
+
if (ended && !orphans) return;
|
|
140
180
|
try {
|
|
141
181
|
if (isWin) {
|
|
142
|
-
const pids = new Set([child.pid, ...(child.claudeProPidFile ? await msysDescendants(child, { sync: false }) : [])]);
|
|
182
|
+
const pids = new Set([...(ended ? [] : [child.pid]), ...(child.claudeProPidFile ? await msysDescendants(child, { sync: false }) : [])]);
|
|
183
|
+
if (!pids.size) return;
|
|
143
184
|
const args = ['/F', '/T'];
|
|
144
185
|
for (const pid of pids) args.push('/PID', String(pid));
|
|
145
186
|
await runAsync('taskkill', args);
|
|
@@ -148,16 +189,19 @@ export async function killTree(child) {
|
|
|
148
189
|
process.kill(-child.pid, 'SIGKILL');
|
|
149
190
|
}
|
|
150
191
|
} catch {
|
|
151
|
-
try { child.kill('SIGKILL'); } catch { /* ignore */ }
|
|
192
|
+
if (!ended) { try { child.kill('SIGKILL'); } catch { /* ignore */ } }
|
|
152
193
|
}
|
|
153
194
|
}
|
|
154
195
|
|
|
155
196
|
// The same, done before it returns: for the exit, after which nothing would be left to finish the kill.
|
|
156
|
-
export function killTreeSync(child) {
|
|
157
|
-
if (!child || child.
|
|
197
|
+
export function killTreeSync(child, { orphans = false } = {}) {
|
|
198
|
+
if (!child || child.killed) return;
|
|
199
|
+
const ended = child.exitCode !== null || child.signalCode !== null;
|
|
200
|
+
if (ended && !orphans) return;
|
|
158
201
|
try {
|
|
159
202
|
if (isWin) {
|
|
160
|
-
const pids = new Set([child.pid, ...(child.claudeProPidFile ? msysDescendants(child, { sync: true }) : [])]);
|
|
203
|
+
const pids = new Set([...(ended ? [] : [child.pid]), ...(child.claudeProPidFile ? msysDescendants(child, { sync: true }) : [])]);
|
|
204
|
+
if (!pids.size) return;
|
|
161
205
|
const args = ['/F', '/T'];
|
|
162
206
|
for (const pid of pids) args.push('/PID', String(pid));
|
|
163
207
|
spawnSync('taskkill', args, { windowsHide: true, stdio: 'ignore' });
|
|
@@ -166,14 +210,25 @@ export function killTreeSync(child) {
|
|
|
166
210
|
process.kill(-child.pid, 'SIGKILL');
|
|
167
211
|
}
|
|
168
212
|
} catch {
|
|
169
|
-
try { child.kill('SIGKILL'); } catch { /* ignore */ }
|
|
213
|
+
if (!ended) { try { child.kill('SIGKILL'); } catch { /* ignore */ } }
|
|
170
214
|
}
|
|
171
215
|
}
|
|
172
216
|
|
|
173
217
|
// Run a quick command and return stdout (used for git info etc.). Never throws.
|
|
174
|
-
|
|
218
|
+
// The environment of what the program starts (Bash, REPL, hooks, MCP servers): non-interactive and colour free, and
|
|
219
|
+
// without what makes a process a restartable session of this program (a hook that ran `claude-pro -p` was given the
|
|
220
|
+
// Restart tool and the prompt about its own source).
|
|
221
|
+
export function childEnv() {
|
|
222
|
+
const env = { ...process.env };
|
|
223
|
+
Object.assign(env, { GIT_EDITOR: 'true', GIT_PAGER: 'cat', PAGER: 'cat', NO_COLOR: '1', CLAUDE_PRO: '1' });
|
|
224
|
+
delete env.FORCE_COLOR;
|
|
225
|
+
for (const key of ['CLAUDE_PRO_SUPERVISED', 'CLAUDE_PRO_RESTART_FILE', 'CLAUDE_PRO_RESTARTED']) delete env[key];
|
|
226
|
+
return env;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function run(cmd, args, { cwd, timeout = 5000, maxBuffer = 1024 * 1024 } = {}) {
|
|
175
230
|
try {
|
|
176
|
-
const r = spawnSync(cmd, args, { cwd, encoding: 'utf8', timeout, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
231
|
+
const r = spawnSync(cmd, args, { cwd, encoding: 'utf8', timeout, maxBuffer, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
177
232
|
if (r.status !== 0) return null;
|
|
178
233
|
return r.stdout.trim();
|
|
179
234
|
} catch {
|
package/.env.example
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# Optional gateway configuration. /login normally saves the key in
|
|
2
|
-
# ~/.claude-pro/gateway.json, so CLAUDE_PRO_GATEWAY_API_KEY is only needed for
|
|
3
|
-
# headless or automated runs.
|
|
4
|
-
CLAUDE_PRO_PROVIDER=gateway
|
|
5
|
-
CLAUDE_PRO_GATEWAY_URL=https://gw.predix.icu
|
|
6
|
-
CLAUDE_PRO_GATEWAY_API_KEY=gw-your-key
|
|
7
|
-
CLAUDE_PRO_GATEWAY_MODEL=opus
|
|
8
|
-
|
|
9
|
-
# Reasoning and context tuning
|
|
10
|
-
# CLAUDE_CODE_EFFORT_LEVEL=high # default: the model's own
|
|
11
|
-
# CLAUDE_CODE_AUTO_COMPACT_WINDOW=250000 # default: the model's whole window
|
|
12
|
-
# CLAUDE_CODE_MAX_OUTPUT_TOKENS=48000 # gateway default
|
|
13
|
-
# MAX_THINKING_TOKENS=0 # 0 = disable thinking
|
|
14
|
-
# CLAUDE_CODE_PROMPT_CACHE_TTL=1h # 5m or 1h for the main conversation
|
|
15
|
-
# CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=5m # sub-agents, compaction and side queries
|
|
16
|
-
# ENABLE_PROMPT_CACHING_1H=1 # 1h for both buckets
|
|
17
|
-
# FORCE_PROMPT_CACHING_5M=1 # force 5m for both buckets
|
|
18
|
-
# DISABLE_PROMPT_CACHING=1 # send no cache_control markers
|
|
19
|
-
# CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=90
|
|
20
|
-
# DISABLE_AUTOCOMPACT=1
|
|
21
|
-
|
|
22
|
-
# Request and tool limits
|
|
23
|
-
# API_TIMEOUT_MS=600000
|
|
24
|
-
# CLAUDE_PRO_MAX_RETRIES=6
|
|
25
|
-
# CLAUDE_PRO_RATE_LIMIT_MAX_WAIT_MS=21600000 # 0 = fail instead of waiting
|
|
26
|
-
# BASH_DEFAULT_TIMEOUT_MS=600000
|
|
27
|
-
# BASH_MAX_TIMEOUT_MS=3600000
|
|
28
|
-
# BASH_MAX_OUTPUT_LENGTH=60000
|
|
29
|
-
# CLAUDE_CODE_GIT_BASH_PATH=C:\Program Files\Git\bin\bash.exe
|