@agent-dock/cli 0.1.1 → 0.1.3

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.
@@ -5,13 +5,47 @@ import { spawnSync } from 'node:child_process';
5
5
  import { access, stat } from 'node:fs/promises';
6
6
  import { constants } from 'node:fs';
7
7
  import { delimiter, resolve } from 'node:path';
8
+ import { homedir } from 'node:os';
9
+
10
+ function codexVersion(file) {
11
+ const result = spawnSync(file, ['--version'], { encoding: 'utf8', timeout: 1500 });
12
+ const match = result.status === 0 && /^codex-cli (\d+)\.(\d+)\.(\d+)(\S*)/m.exec(result.stdout ?? '');
13
+ return match ? { numbers: match.slice(1, 4).map(Number), prerelease: match[4] } : null;
14
+ }
15
+
16
+ function newerVersion(candidate, current) {
17
+ for (let i = 0; i < 3; i++) {
18
+ if (candidate.numbers[i] !== current.numbers[i]) return candidate.numbers[i] > current.numbers[i];
19
+ }
20
+ return !candidate.prerelease && Boolean(current.prerelease);
21
+ }
22
+
23
+ async function preferCurrentDesktopCodex(pathCodex) {
24
+ if (process.platform !== 'darwin') return pathCodex;
25
+ let selected = pathCodex;
26
+ let version = codexVersion(selected);
27
+ // Preserve wrappers and custom executables whose version cannot be identified.
28
+ if (!version) return selected;
29
+ for (const root of ['/Applications', resolve(homedir(), 'Applications')]) {
30
+ for (const app of ['ChatGPT.app', 'Codex.app']) {
31
+ const candidate = resolve(root, app, 'Contents/Resources/codex-cli/bin/codex');
32
+ try { await access(candidate, constants.X_OK); } catch { continue; }
33
+ const candidateVersion = codexVersion(candidate);
34
+ if (candidateVersion && newerVersion(candidateVersion, version)) {
35
+ selected = candidate;
36
+ version = candidateVersion;
37
+ }
38
+ }
39
+ }
40
+ return selected;
41
+ }
8
42
 
9
43
  async function localCodexPath() {
10
44
  if (process.env.CODEX_PATH) return process.env.CODEX_PATH;
11
45
  for (const directory of (process.env.PATH ?? '').split(delimiter).filter(Boolean)) {
12
46
  for (const name of process.platform === 'win32' ? ['codex.exe', 'codex.cmd', 'codex.bat'] : ['codex']) {
13
47
  const candidate = resolve(directory, name);
14
- try { if (!(await stat(candidate)).isFile()) continue; await access(candidate, constants.X_OK); return candidate; } catch {}
48
+ try { if (!(await stat(candidate)).isFile()) continue; await access(candidate, constants.X_OK); return await preferCurrentDesktopCodex(candidate); } catch {}
15
49
  }
16
50
  }
17
51
  throw new Error('未找到本机 Codex,请将 Codex CLI 加入 PATH 或设置 CODEX_PATH。');