@jacks001314/codex-go 1.0.6 → 1.0.7

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 CHANGED
@@ -1,238 +1,238 @@
1
- # Codex Go
2
-
3
- The latest audited alignment target is Rust `main` at `8c076942cb70fba8e5c4732c23f1faabc4aed484`; see [the 2026-10-18 alignment record](./update/plan_2026_10_18_round142.md).
4
- [parity.json](./parity.json) records the current SDK and implementation evidence. Candidate baseline, commit, domain, and contract state live under [parity/](./parity/); only `parity/baseline.json` with `certificationReady: true` denotes a fully certified release.
5
-
6
- `codex_go` 是 Codex CLI 的 Go 实现,目标是逐步对齐 Rust 版 Codex CLI 的命令行行为、配置语义、认证、模型请求、TUI、app-server、MCP、插件、沙箱和会话能力。
7
-
8
- 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [plan.md](./plan.md) 为准。
9
-
10
- ## 环境要求
11
-
12
- - Go 1.26.2+
13
- - 推荐使用 Go toolchain 1.26.2
14
- - Windows、Linux 或 macOS
15
-
16
- 模块信息见 [go.mod](./go.mod)。
17
-
18
- ## 快速开始
19
-
20
- 构建 CLI:
21
-
22
- ```powershell
23
- go build -o .\bin\codex.exe .\cmd\codex
24
- ```
25
-
26
- 直接运行:
27
-
28
- ```powershell
29
- go run .\cmd\codex -- "解释当前目录的代码结构"
30
- ```
31
-
32
- 非交互执行:
33
-
34
- ```powershell
35
- go run .\cmd\codex exec "用一句话总结这个项目"
36
- ```
37
-
38
- 登录状态:
39
-
40
- ```powershell
41
- go run .\cmd\codex login status
42
- ```
43
-
44
- 使用 API key 登录时从 stdin 读取密钥:
45
-
46
- ```powershell
47
- $env:OPENAI_API_KEY | go run .\cmd\codex login --with-api-key
48
- ```
49
-
50
- ## 常用命令
51
-
52
- ```text
53
- codex [OPTIONS] [PROMPT] 启动交互式 TUI,或用 PROMPT 开始会话
54
- codex exec [OPTIONS] [PROMPT] 非交互执行
55
- codex review [OPTIONS] 代码审查
56
- codex login 登录 ChatGPT
57
- codex login --with-api-key 从 stdin 保存 API key
58
- codex login status 查看登录状态
59
- codex logout 退出登录
60
- codex features list 查看功能开关
61
- codex mcp ... 管理 MCP server 配置和登录
62
- codex plugin ... 管理插件和 marketplace
63
- codex app-server ... 启动或管理 app-server
64
- codex sandbox -- COMMAND 在权限配置下运行命令
65
- codex doctor 环境诊断
66
- codex completion [SHELL] 生成 shell completion
67
- ```
68
-
69
- 根级常用选项:
70
-
71
- ```text
72
- -c, --config KEY=VALUE 覆盖配置
73
- --enable FEATURE 启用功能开关
74
- --disable FEATURE 禁用功能开关
75
- -m, --model MODEL 指定模型
76
- -p, --profile PROFILE 使用配置 profile
77
- -C, --cd DIR 指定工作目录
78
- --add-dir DIR 增加可访问目录
79
- --sandbox PROFILE 指定沙箱策略
80
- --ask-for-approval POLICY 指定审批策略
81
- ```
82
-
83
- ## 测试与检查
84
-
85
- 包加载检查:
86
-
87
- ```powershell
88
- go list -buildvcs=false ./...
89
- ```
90
-
91
- 运行全部测试:
92
-
93
- ```powershell
94
- go test ./...
95
- ```
96
-
97
- 如果本机 Go 缓存目录受限,可以临时指定缓存:
98
-
99
- ```powershell
100
- $env:GOCACHE = Join-Path $env:TEMP 'codex_go_gocache'
101
- go test ./...
102
- ```
103
-
104
- 格式化:
105
-
106
- ```powershell
107
- go fmt ./...
108
- ```
109
-
110
- ## 编译、安装与发布
111
-
112
- ### npm 安装
113
-
114
- 发布后的全局安装命令:
115
-
116
- ```powershell
117
- npm install -g @jacks001314/codex-go
118
- gcode --version
119
- ```
120
-
121
- npm 对外暴露的命令名为 `gcode`,避免与官方 `codex` 命令冲突;底层原生二进制仍名为 `codex`。主包会根据当前操作系统和 CPU 架构自动安装对应的原生二进制包。发布新版本时先构建七个 tarball,再按“六个平台包、主包”的顺序发布:
122
-
123
- ```powershell
124
- node .\npm\scripts\build-packages.mjs --version 0.1.0
125
- node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm
126
- ```
127
-
128
- 如果 npm 账号要求发布时进行双因素认证,请传入认证器当前生成的 OTP:
129
-
130
- ```powershell
131
- node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm --otp 123456
132
- ```
133
-
134
- 发布需要登录 npm 账号 `jacks001314`;预发布可追加 `--tag next`。CI 发布应使用启用了 `Bypass 2FA` 的 granular access token,避免交互式 OTP 过期。
135
-
136
- 编译当前平台的开发版本:
137
-
138
- ```powershell
139
- .\scripts\build.ps1
140
- ```
141
-
142
- Windows builds also write `codex-command-runner.exe` and
143
- `codex-windows-sandbox-setup.exe` under `bin\codex-resources`. NPM platform
144
- packages use the official `vendor/<target-triple>/bin` layout with a sibling
145
- `codex-resources` directory.
146
-
147
- 编译指定版本和目标平台:
148
-
149
- ```powershell
150
- .\scripts\build.ps1 -Version 0.1.0 -GOOS linux -GOARCH amd64 -CGO off
151
- ```
152
-
153
- 从当前源码安装。Windows 默认安装到 `%LOCALAPPDATA%\Programs\CodexGo\bin`,
154
- 其他系统默认安装到 `~/.local/bin`:
155
-
156
- ```powershell
157
- .\scripts\install.ps1 -Version 0.1.0
158
- ```
159
-
160
- 也可以指定安装目录:
161
-
162
- ```powershell
163
- .\scripts\install.ps1 -InstallDir C:\Tools\CodexGo -Force
164
- ```
165
-
166
- 生成 Windows、Linux 和 macOS 的 amd64/arm64 发布包及 SHA-256 校验清单:
167
-
168
- ```powershell
169
- .\scripts\release.ps1 -Version 0.1.0
170
- ```
171
-
172
- 发布产物默认写入 `dist/v<版本号>`。可用 `-Targets windows/amd64,linux/amd64`
173
- 限制目标平台,或用 `-SkipTests` 跳过发布前的全包编译检查。
174
-
175
- Linux、macOS、WSL 或其他 POSIX Shell 环境可以使用对应的 Shell 脚本:
176
-
177
- ```sh
178
- ./scripts/build.sh --version 0.1.0
179
- ./scripts/install.sh --version 0.1.0
180
- ./scripts/release.sh --version 0.1.0
181
- ```
182
-
183
- 指定发布目标:
184
-
185
- ```sh
186
- ./scripts/release.sh --version 0.1.0 \
187
- --targets 'linux/amd64 linux/arm64 darwin/arm64'
188
- ```
189
-
190
- ## 目录结构
191
-
192
- ```text
193
- cmd/codex 可执行入口
194
- app 顶层命令分发和运行时拼装
195
- cli 命令行解析、参数校验、dispatch alias
196
- config 配置加载、profile、feature override、权限配置
197
- auth 登录凭据、auth.json、OAuth/API key/access token
198
- model 模型 provider、Responses 请求/流式事件、模型 catalog
199
- codexapi OpenAI/Codex API 客户端辅助
200
- turn agent loop、tool dispatch、turn runtime
201
- tool shell/apply_patch/MCP/tool_search/agent 工具运行时
202
- exec codex exec/review 的非交互运行
203
- appserver JSON-RPC app-server、thread/turn/runtime 服务
204
- mcp MCP client/server、OAuth、resource、tool runtime
205
- tui 终端 UI 状态、渲染、组件和 Bubble Tea adapter
206
- session 会话存储、resume/fork/archive/delete
207
- rollout rollout JSONL 记录和恢复
208
- sandbox 权限 profile、Linux/Windows 沙箱、执行计划
209
- review review 目标解析和 git diff 采集
210
- doctor 环境诊断
211
- plugin 插件 manifest、marketplace、安装流程
212
- agent 多 agent graph、身份、registry、工具
213
- utils 路径、JSON、ANSI、LRU、截断等共享工具
214
- docs 设计和技术选型文档
215
- ```
216
-
217
- ## 开发约定
218
-
219
- - 以 Rust 版 Codex 的行为和协议形状为基线。
220
- - 新功能优先放入已有功能包,避免无必要地新增目录。
221
- - 结构体参数和结构体返回值默认优先使用指针,简单标量按 Go 惯例传值。
222
- - 代码变更后至少运行相关包测试;模块收口时运行 `go list -buildvcs=false ./...` 和 `go test ./...`。
223
- - 迁移进度、验收项和工作日志维护在 [plan.md](./plan.md)。
224
-
225
- ## 参考文档
226
-
227
- - [plan.md](./plan.md):迁移进度和待办
228
- - [desgn.md](./desgn.md):迁移设计草案
229
- - [docs/tui_tech_selection.md](./docs/tui_tech_selection.md):TUI 技术选型
230
-
231
- ## 许可证
232
-
233
- 本项目基于上游 [OpenAI Codex](https://github.com/openai/codex) 的 Apache-2.0 许可实现,同样以 Apache License 2.0 发布。
234
-
235
- - [LICENSE](./LICENSE):Apache License, Version 2.0 全文
236
- - [NOTICE](./NOTICE):上游来源、修改声明,以及发布包内附带的 ripgrep、bubblewrap、Opus 等第三方组件许可信息
237
-
238
- 分发二进制或 npm 包时必须一并携带 `LICENSE` 与 `NOTICE`(`scripts/release.ps1`、`scripts/release.sh`、`npm/scripts/build-packages.mjs` 已自动打包)。
1
+ # Codex Go
2
+
3
+ The latest audited alignment target is Rust `main` at `67f0994b532b8740f14b5b738af7b7b2a915b1d6`; see [the 2026-10-31 alignment record](./update/plan_2026_10_31_round1194.md).
4
+ [parity.json](./parity.json) records the current SDK and implementation evidence. Candidate baseline, commit, domain, and contract state live under [parity/](./parity/); only `parity/baseline.json` with `certificationReady: true` denotes a fully certified release.
5
+
6
+ `codex_go` 是 Codex CLI 的 Go 实现,目标是逐步对齐 Rust 版 Codex CLI 的命令行行为、配置语义、认证、模型请求、TUI、app-server、MCP、插件、沙箱和会话能力。
7
+
8
+ 当前仓库是迁移中的工程版本。开发时以 Rust 版本为行为基线,进度和待办以 [plan.md](./plan.md) 为准。
9
+
10
+ ## 环境要求
11
+
12
+ - Go 1.26.2+
13
+ - 推荐使用 Go toolchain 1.26.2
14
+ - Windows、Linux 或 macOS
15
+
16
+ 模块信息见 [go.mod](./go.mod)。
17
+
18
+ ## 快速开始
19
+
20
+ 构建 CLI:
21
+
22
+ ```powershell
23
+ go build -o .\bin\codex.exe .\cmd\codex
24
+ ```
25
+
26
+ 直接运行:
27
+
28
+ ```powershell
29
+ go run .\cmd\codex -- "解释当前目录的代码结构"
30
+ ```
31
+
32
+ 非交互执行:
33
+
34
+ ```powershell
35
+ go run .\cmd\codex exec "用一句话总结这个项目"
36
+ ```
37
+
38
+ 登录状态:
39
+
40
+ ```powershell
41
+ go run .\cmd\codex login status
42
+ ```
43
+
44
+ 使用 API key 登录时从 stdin 读取密钥:
45
+
46
+ ```powershell
47
+ $env:OPENAI_API_KEY | go run .\cmd\codex login --with-api-key
48
+ ```
49
+
50
+ ## 常用命令
51
+
52
+ ```text
53
+ codex [OPTIONS] [PROMPT] 启动交互式 TUI,或用 PROMPT 开始会话
54
+ codex exec [OPTIONS] [PROMPT] 非交互执行
55
+ codex review [OPTIONS] 代码审查
56
+ codex login 登录 ChatGPT
57
+ codex login --with-api-key 从 stdin 保存 API key
58
+ codex login status 查看登录状态
59
+ codex logout 退出登录
60
+ codex features list 查看功能开关
61
+ codex mcp ... 管理 MCP server 配置和登录
62
+ codex plugin ... 管理插件和 marketplace
63
+ codex app-server ... 启动或管理 app-server
64
+ codex sandbox -- COMMAND 在权限配置下运行命令
65
+ codex doctor 环境诊断
66
+ codex completion [SHELL] 生成 shell completion
67
+ ```
68
+
69
+ 根级常用选项:
70
+
71
+ ```text
72
+ -c, --config KEY=VALUE 覆盖配置
73
+ --enable FEATURE 启用功能开关
74
+ --disable FEATURE 禁用功能开关
75
+ -m, --model MODEL 指定模型
76
+ -p, --profile PROFILE 使用配置 profile
77
+ -C, --cd DIR 指定工作目录
78
+ --add-dir DIR 增加可访问目录
79
+ --sandbox PROFILE 指定沙箱策略
80
+ --ask-for-approval POLICY 指定审批策略
81
+ ```
82
+
83
+ ## 测试与检查
84
+
85
+ 包加载检查:
86
+
87
+ ```powershell
88
+ go list -buildvcs=false ./...
89
+ ```
90
+
91
+ 运行全部测试:
92
+
93
+ ```powershell
94
+ go test ./...
95
+ ```
96
+
97
+ 如果本机 Go 缓存目录受限,可以临时指定缓存:
98
+
99
+ ```powershell
100
+ $env:GOCACHE = Join-Path $env:TEMP 'codex_go_gocache'
101
+ go test ./...
102
+ ```
103
+
104
+ 格式化:
105
+
106
+ ```powershell
107
+ go fmt ./...
108
+ ```
109
+
110
+ ## 编译、安装与发布
111
+
112
+ ### npm 安装
113
+
114
+ 发布后的全局安装命令:
115
+
116
+ ```powershell
117
+ npm install -g @jacks001314/codex-go
118
+ gcode --version
119
+ ```
120
+
121
+ npm 对外暴露的命令名为 `gcode`,避免与官方 `codex` 命令冲突;底层原生二进制仍名为 `codex`。主包会根据当前操作系统和 CPU 架构自动安装对应的原生二进制包。发布新版本时先构建七个 tarball,再按“六个平台包、主包”的顺序发布:
122
+
123
+ ```powershell
124
+ node .\npm\scripts\build-packages.mjs --version 0.1.0
125
+ node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm
126
+ ```
127
+
128
+ 如果 npm 账号要求发布时进行双因素认证,请传入认证器当前生成的 OTP:
129
+
130
+ ```powershell
131
+ node .\npm\scripts\publish-packages.mjs --directory .\dist\v0.1.0\npm --otp 123456
132
+ ```
133
+
134
+ 发布需要登录 npm 账号 `jacks001314`;预发布可追加 `--tag next`。CI 发布应使用启用了 `Bypass 2FA` 的 granular access token,避免交互式 OTP 过期。
135
+
136
+ 编译当前平台的开发版本:
137
+
138
+ ```powershell
139
+ .\scripts\build.ps1
140
+ ```
141
+
142
+ Windows builds also write `codex-command-runner.exe` and
143
+ `codex-windows-sandbox-setup.exe` under `bin\codex-resources`. NPM platform
144
+ packages use the official `vendor/<target-triple>/bin` layout with a sibling
145
+ `codex-resources` directory.
146
+
147
+ 编译指定版本和目标平台:
148
+
149
+ ```powershell
150
+ .\scripts\build.ps1 -Version 0.1.0 -GOOS linux -GOARCH amd64 -CGO off
151
+ ```
152
+
153
+ 从当前源码安装。Windows 默认安装到 `%LOCALAPPDATA%\Programs\CodexGo\bin`,
154
+ 其他系统默认安装到 `~/.local/bin`:
155
+
156
+ ```powershell
157
+ .\scripts\install.ps1 -Version 0.1.0
158
+ ```
159
+
160
+ 也可以指定安装目录:
161
+
162
+ ```powershell
163
+ .\scripts\install.ps1 -InstallDir C:\Tools\CodexGo -Force
164
+ ```
165
+
166
+ 生成 Windows、Linux 和 macOS 的 amd64/arm64 发布包及 SHA-256 校验清单:
167
+
168
+ ```powershell
169
+ .\scripts\release.ps1 -Version 0.1.0
170
+ ```
171
+
172
+ 发布产物默认写入 `dist/v<版本号>`。可用 `-Targets windows/amd64,linux/amd64`
173
+ 限制目标平台,或用 `-SkipTests` 跳过发布前的全包编译检查。
174
+
175
+ Linux、macOS、WSL 或其他 POSIX Shell 环境可以使用对应的 Shell 脚本:
176
+
177
+ ```sh
178
+ ./scripts/build.sh --version 0.1.0
179
+ ./scripts/install.sh --version 0.1.0
180
+ ./scripts/release.sh --version 0.1.0
181
+ ```
182
+
183
+ 指定发布目标:
184
+
185
+ ```sh
186
+ ./scripts/release.sh --version 0.1.0 \
187
+ --targets 'linux/amd64 linux/arm64 darwin/arm64'
188
+ ```
189
+
190
+ ## 目录结构
191
+
192
+ ```text
193
+ cmd/codex 可执行入口
194
+ app 顶层命令分发和运行时拼装
195
+ cli 命令行解析、参数校验、dispatch alias
196
+ config 配置加载、profile、feature override、权限配置
197
+ auth 登录凭据、auth.json、OAuth/API key/access token
198
+ model 模型 provider、Responses 请求/流式事件、模型 catalog
199
+ codexapi OpenAI/Codex API 客户端辅助
200
+ turn agent loop、tool dispatch、turn runtime
201
+ tool shell/apply_patch/MCP/tool_search/agent 工具运行时
202
+ exec codex exec/review 的非交互运行
203
+ appserver JSON-RPC app-server、thread/turn/runtime 服务
204
+ mcp MCP client/server、OAuth、resource、tool runtime
205
+ tui 终端 UI 状态、渲染、组件和 Bubble Tea adapter
206
+ session 会话存储、resume/fork/archive/delete
207
+ rollout rollout JSONL 记录和恢复
208
+ sandbox 权限 profile、Linux/Windows 沙箱、执行计划
209
+ review review 目标解析和 git diff 采集
210
+ doctor 环境诊断
211
+ plugin 插件 manifest、marketplace、安装流程
212
+ agent 多 agent graph、身份、registry、工具
213
+ utils 路径、JSON、ANSI、LRU、截断等共享工具
214
+ docs 设计和技术选型文档
215
+ ```
216
+
217
+ ## 开发约定
218
+
219
+ - 以 Rust 版 Codex 的行为和协议形状为基线。
220
+ - 新功能优先放入已有功能包,避免无必要地新增目录。
221
+ - 结构体参数和结构体返回值默认优先使用指针,简单标量按 Go 惯例传值。
222
+ - 代码变更后至少运行相关包测试;模块收口时运行 `go list -buildvcs=false ./...` 和 `go test ./...`。
223
+ - 迁移进度、验收项和工作日志维护在 [plan.md](./plan.md)。
224
+
225
+ ## 参考文档
226
+
227
+ - [plan.md](./plan.md):迁移进度和待办
228
+ - [desgn.md](./desgn.md):迁移设计草案
229
+ - [docs/tui_tech_selection.md](./docs/tui_tech_selection.md):TUI 技术选型
230
+
231
+ ## 许可证
232
+
233
+ 本项目基于上游 [OpenAI Codex](https://github.com/openai/codex) 的 Apache-2.0 许可实现,同样以 Apache License 2.0 发布。
234
+
235
+ - [LICENSE](./LICENSE):Apache License, Version 2.0 全文
236
+ - [NOTICE](./NOTICE):上游来源、修改声明,以及发布包内附带的 ripgrep、bubblewrap、Opus 等第三方组件许可信息
237
+
238
+ 分发二进制或 npm 包时必须一并携带 `LICENSE` 与 `NOTICE`(`scripts/release.ps1`、`scripts/release.sh`、`npm/scripts/build-packages.mjs` 已自动打包)。
package/bin/codex.js CHANGED
@@ -7,9 +7,17 @@ import { constants } from "node:os";
7
7
  import path from "node:path";
8
8
  import { fileURLToPath } from "node:url";
9
9
 
10
+ import {
11
+ detectPackageManager,
12
+ managedByEnvVarFor,
13
+ updateCommandFor,
14
+ } from "./package-manager.js";
15
+
10
16
  const require = createRequire(import.meta.url);
11
17
  const scriptDir = path.dirname(fileURLToPath(import.meta.url));
12
18
  const codexPackageRoot = realpathSync(path.resolve(scriptDir, ".."));
19
+ const entrypointDir = path.dirname(path.resolve(process.argv[1]));
20
+ const packageManager = detectPackageManager({ codexPackageRoot, entrypointDir, scriptDir });
13
21
  const platformPackages = {
14
22
  "linux-x64": "@jacks001314/codex-go-linux-x64",
15
23
  "linux-arm64": "@jacks001314/codex-go-linux-arm64",
@@ -39,16 +47,9 @@ let packageJsonPath;
39
47
  try {
40
48
  packageJsonPath = require.resolve(`${platformPackage}/package.json`);
41
49
  } catch {
42
- const packageManager = detectPackageManager();
43
- const updateCommand =
44
- packageManager === "bun"
45
- ? "bun install -g @jacks001314/codex-go@latest"
46
- : packageManager === "pnpm"
47
- ? "pnpm add -g @jacks001314/codex-go@latest"
48
- : "npm install -g @jacks001314/codex-go@latest";
49
50
  console.error(
50
51
  `Missing optional dependency ${platformPackage}. ` +
51
- `Reinstall with: ${updateCommand}`,
52
+ `Reinstall with: ${updateCommandFor(packageManager)}`,
52
53
  );
53
54
  process.exit(1);
54
55
  }
@@ -73,48 +74,12 @@ try {
73
74
  chmodSync(executable, 0o755);
74
75
  } catch {}
75
76
 
76
- function isPnpmOwnedCodexInstall(nodeModulesDir) {
77
- if (!existsSync(path.join(nodeModulesDir, ".modules.yaml"))) return false;
78
- try {
79
- return realpathSync(path.join(nodeModulesDir, "@jacks001314", "codex-go")) === codexPackageRoot;
80
- } catch {
81
- return false;
82
- }
83
- }
84
-
85
- function detectPackageManager() {
86
- const entrypointDir = path.dirname(path.resolve(process.argv[1]));
87
- for (const startDir of new Set([codexPackageRoot, entrypointDir])) {
88
- const filesystemRoot = path.parse(startDir).root;
89
- for (let currentDir = startDir; currentDir !== filesystemRoot; currentDir = path.dirname(currentDir)) {
90
- if (isPnpmOwnedCodexInstall(path.join(currentDir, "node_modules"))) return "pnpm";
91
- }
92
- if (isPnpmOwnedCodexInstall(path.join(filesystemRoot, "node_modules"))) return "pnpm";
93
- }
94
- const userAgent = process.env.npm_config_user_agent || "";
95
- const execPath = process.env.npm_execpath || "";
96
- if (
97
- /\bbun\//.test(userAgent) ||
98
- execPath.includes("bun") ||
99
- scriptDir.includes(".bun/install/global") ||
100
- scriptDir.includes(".bun\\install\\global")
101
- ) {
102
- return "bun";
103
- }
104
- return userAgent ? "npm" : null;
105
- }
106
-
107
- const packageManager = detectPackageManager();
108
- const packageManagerEnvVar =
109
- packageManager === "bun"
110
- ? "CODEX_MANAGED_BY_BUN"
111
- : packageManager === "pnpm"
112
- ? "CODEX_MANAGED_BY_PNPM"
113
- : "CODEX_MANAGED_BY_NPM";
77
+ const packageManagerEnvVar = managedByEnvVarFor(packageManager);
114
78
  const env = { ...process.env, CODEX_MANAGED_PACKAGE_ROOT: codexPackageRoot };
115
79
  delete env.CODEX_MANAGED_BY_NPM;
116
80
  delete env.CODEX_MANAGED_BY_BUN;
117
81
  delete env.CODEX_MANAGED_BY_PNPM;
82
+ delete env.CODEX_MANAGED_BY_VITE_PLUS;
118
83
  env[packageManagerEnvVar] = "1";
119
84
 
120
85
  // Replace Node so the native binary receives signals and exits directly.
@@ -0,0 +1,115 @@
1
+ import { existsSync, readFileSync, realpathSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ // CODEX_PACKAGE_NAME is the package this shim is published as. Keeping it in
5
+ // this module lets the shim and its tests use the same ownership path.
6
+ export const CODEX_PACKAGE_NAME = "@jacks001314/codex-go";
7
+
8
+ function sameRealPath(left, right) {
9
+ try {
10
+ return realpathSync(left) === realpathSync(right);
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+
16
+ export function isPnpmOwnedCodexInstall(nodeModulesDir, codexPackageRoot, packageName = CODEX_PACKAGE_NAME) {
17
+ if (!existsSync(path.join(nodeModulesDir, ".modules.yaml"))) return false;
18
+ return sameRealPath(path.join(nodeModulesDir, ...packageName.split("/")), codexPackageRoot);
19
+ }
20
+
21
+ // Vite+ records the active global installation in
22
+ // packages/<package-name>.json. Older installs append a #-prefixed ID to the
23
+ // package directory; newer installs put the ID in a subdirectory.
24
+ export function isVitePlusOwnedCodexInstall(packagesDir, codexPackageRoot, packageName = CODEX_PACKAGE_NAME) {
25
+ if (path.basename(packagesDir) !== "packages") return false;
26
+ try {
27
+ const metadata = JSON.parse(
28
+ readFileSync(path.join(packagesDir, `${packageName}.json`), "utf8"),
29
+ );
30
+ if (metadata.name !== packageName) return false;
31
+ const installId = typeof metadata.installId === "string" ? metadata.installId : "";
32
+ const packageDir = path.join(packagesDir, ...packageName.split("/"));
33
+ const installDir = installId.startsWith("#")
34
+ ? `${packageDir}${installId}`
35
+ : path.join(packageDir, installId);
36
+ for (const nodeModulesDir of [
37
+ path.join(installDir, "lib", "node_modules"),
38
+ path.join(installDir, "node_modules"),
39
+ ]) {
40
+ if (sameRealPath(path.join(nodeModulesDir, ...packageName.split("/")), codexPackageRoot)) {
41
+ return true;
42
+ }
43
+ }
44
+ } catch {
45
+ // Missing or unreadable ownership metadata must not prevent Codex starting.
46
+ }
47
+ return false;
48
+ }
49
+
50
+ // detectPackageManager uses owning-directory metadata from the canonical
51
+ // package root and lexical entrypoint. Vite+ is checked before pnpm because
52
+ // both can expose the same package through ancestor node_modules layouts.
53
+ export function detectPackageManager({
54
+ codexPackageRoot,
55
+ entrypointDir,
56
+ packageName = CODEX_PACKAGE_NAME,
57
+ scriptDir,
58
+ env = process.env,
59
+ }) {
60
+ for (const startDir of new Set([codexPackageRoot, entrypointDir])) {
61
+ const filesystemRoot = path.parse(startDir).root;
62
+ for (
63
+ let currentDir = startDir;
64
+ currentDir !== filesystemRoot;
65
+ currentDir = path.dirname(currentDir)
66
+ ) {
67
+ if (isVitePlusOwnedCodexInstall(currentDir, codexPackageRoot, packageName)) {
68
+ return "vite-plus";
69
+ }
70
+ if (isPnpmOwnedCodexInstall(path.join(currentDir, "node_modules"), codexPackageRoot, packageName)) {
71
+ return "pnpm";
72
+ }
73
+ }
74
+ if (isPnpmOwnedCodexInstall(path.join(filesystemRoot, "node_modules"), codexPackageRoot, packageName)) {
75
+ return "pnpm";
76
+ }
77
+ }
78
+ const userAgent = env.npm_config_user_agent || "";
79
+ const execPath = env.npm_execpath || "";
80
+ if (
81
+ /\bbun\//.test(userAgent) ||
82
+ execPath.includes("bun") ||
83
+ scriptDir.includes(".bun/install/global") ||
84
+ scriptDir.includes(".bun\\install\\global")
85
+ ) {
86
+ return "bun";
87
+ }
88
+ return userAgent ? "npm" : null;
89
+ }
90
+
91
+ export function updateCommandFor(packageManager, packageName = CODEX_PACKAGE_NAME) {
92
+ switch (packageManager) {
93
+ case "bun":
94
+ return `bun install -g ${packageName}@latest`;
95
+ case "pnpm":
96
+ return `pnpm add -g ${packageName}@latest`;
97
+ case "vite-plus":
98
+ return `vp install -g ${packageName}@latest`;
99
+ default:
100
+ return `npm install -g ${packageName}@latest`;
101
+ }
102
+ }
103
+
104
+ export function managedByEnvVarFor(packageManager) {
105
+ switch (packageManager) {
106
+ case "bun":
107
+ return "CODEX_MANAGED_BY_BUN";
108
+ case "pnpm":
109
+ return "CODEX_MANAGED_BY_PNPM";
110
+ case "vite-plus":
111
+ return "CODEX_MANAGED_BY_VITE_PLUS";
112
+ default:
113
+ return "CODEX_MANAGED_BY_NPM";
114
+ }
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacks001314/codex-go",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Codex CLI implemented in Go",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -11,18 +11,19 @@
11
11
  "node": ">=16"
12
12
  },
13
13
  "files": [
14
- "bin/codex.js"
14
+ "bin/codex.js",
15
+ "bin/package-manager.js"
15
16
  ],
16
17
  "repository": {
17
18
  "type": "git",
18
19
  "url": "git+https://github.com/jacks001314/codex_go.git"
19
20
  },
20
21
  "optionalDependencies": {
21
- "@jacks001314/codex-go-linux-x64": "1.0.6",
22
- "@jacks001314/codex-go-linux-arm64": "1.0.6",
23
- "@jacks001314/codex-go-darwin-x64": "1.0.6",
24
- "@jacks001314/codex-go-darwin-arm64": "1.0.6",
25
- "@jacks001314/codex-go-win32-x64": "1.0.6",
26
- "@jacks001314/codex-go-win32-arm64": "1.0.6"
22
+ "@jacks001314/codex-go-linux-x64": "1.0.7",
23
+ "@jacks001314/codex-go-linux-arm64": "1.0.7",
24
+ "@jacks001314/codex-go-darwin-x64": "1.0.7",
25
+ "@jacks001314/codex-go-darwin-arm64": "1.0.7",
26
+ "@jacks001314/codex-go-win32-x64": "1.0.7",
27
+ "@jacks001314/codex-go-win32-arm64": "1.0.7"
27
28
  }
28
29
  }