codex-mux 0.1.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/LICENSE +21 -0
- package/README.ko.md +218 -0
- package/README.md +222 -0
- package/dist/index.js +332 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ko.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# codex-mux
|
|
2
|
+
|
|
3
|
+
`codex-mux`는 Codex CLI(`@openai/codex`)를 여러 계정 프로필로 분리해 쓰기 위한 비공식 래퍼 CLI입니다.
|
|
4
|
+
|
|
5
|
+
핵심 목적:
|
|
6
|
+
- 터미널 A는 계정 A, 터미널 B는 계정 B로 동시에 사용
|
|
7
|
+
- 매번 `codex logout` -> `codex login` 반복하는 불편 최소화
|
|
8
|
+
- `CODEX_HOME`을 프로필별로 자동 분리
|
|
9
|
+
|
|
10
|
+
## Important Notice (Unofficial)
|
|
11
|
+
|
|
12
|
+
- 이 프로젝트는 OpenAI의 공식 제품이 아닙니다.
|
|
13
|
+
- OpenAI/Codex의 이름은 해당 권리자에게 있습니다.
|
|
14
|
+
- 이 도구는 설치된 `codex` 실행 파일을 호출하는 래퍼이며, 계정 제한 우회 기능을 제공하지 않습니다.
|
|
15
|
+
|
|
16
|
+
## Prerequisites
|
|
17
|
+
|
|
18
|
+
- Node.js + npm
|
|
19
|
+
- Codex CLI 설치
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i -g @openai/codex
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Codex CLI가 PATH에 있어야 `mux login`, `mux logout`, `mux run`이 동작합니다.
|
|
26
|
+
|
|
27
|
+
## Install (Local Development)
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install
|
|
31
|
+
npm run build
|
|
32
|
+
npm link
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
설치 후:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
mux --help
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Why This Exists
|
|
42
|
+
|
|
43
|
+
Codex CLI 기본 동작은 단일 `~/.codex`를 사용합니다. 그래서 계정 전환이 자주 필요할 때 로그인/로그아웃이 번거롭습니다.
|
|
44
|
+
|
|
45
|
+
`codex-mux`는 아래처럼 프로필별 저장소를 분리합니다:
|
|
46
|
+
|
|
47
|
+
- `~/.codex-mux/state.json`
|
|
48
|
+
- `~/.codex-mux/profiles/<profile>/codex-home`
|
|
49
|
+
|
|
50
|
+
각 프로필은 서로 다른 `auth.json`을 가지므로 로그인 상태가 분리됩니다.
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
1. 프로필 선택
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
mux use a
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
2. 로그인
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
mux login
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
3. Codex 실행
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
mux run
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
다른 터미널에서는:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
mux use b
|
|
76
|
+
mux login
|
|
77
|
+
mux run
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## A/B Account Example
|
|
81
|
+
|
|
82
|
+
아래 예시는 터미널 2개를 열고, 각각 다른 계정을 유지하는 가장 일반적인 흐름입니다.
|
|
83
|
+
|
|
84
|
+
Terminal 1 (Account A):
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
mux use a
|
|
88
|
+
mux login
|
|
89
|
+
mux run
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Terminal 2 (Account B):
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
mux use b
|
|
96
|
+
mux login
|
|
97
|
+
mux run
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
상태 확인:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
mux current
|
|
104
|
+
mux list
|
|
105
|
+
mux list --json
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
필요할 때 로그아웃:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
mux logout -p a
|
|
112
|
+
mux logout -p b
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
프로필 지정으로 한 번에 실행하고 싶을 때:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
mux login -p a
|
|
119
|
+
mux run -p a
|
|
120
|
+
mux login -p b
|
|
121
|
+
mux run -p b
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Commands
|
|
125
|
+
|
|
126
|
+
### `mux use <profile>`
|
|
127
|
+
현재 터미널 세션의 활성 프로필을 설정합니다.
|
|
128
|
+
|
|
129
|
+
옵션:
|
|
130
|
+
- `--global`: 세션이 아닌 전역 기본 프로필로 설정
|
|
131
|
+
- `--json`: JSON 출력
|
|
132
|
+
- `--verbose`: 디버그 정보 출력
|
|
133
|
+
|
|
134
|
+
예시:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
mux use a
|
|
138
|
+
mux use team-b --global
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### `mux current`
|
|
142
|
+
현재 활성 프로필을 확인합니다.
|
|
143
|
+
|
|
144
|
+
옵션:
|
|
145
|
+
- `-p, --profile <name>`: 특정 프로필 기준으로 확인
|
|
146
|
+
- `--json`
|
|
147
|
+
- `--verbose`
|
|
148
|
+
|
|
149
|
+
예시:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
mux current
|
|
153
|
+
mux current --json
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `mux login [codexArgs...]`
|
|
157
|
+
선택된 프로필의 `CODEX_HOME`으로 `codex login`을 실행합니다.
|
|
158
|
+
|
|
159
|
+
옵션:
|
|
160
|
+
- `-p, --profile <name>`
|
|
161
|
+
- `--verbose`
|
|
162
|
+
|
|
163
|
+
예시:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
mux login
|
|
167
|
+
mux login -p b
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### `mux logout [codexArgs...]`
|
|
171
|
+
선택된 프로필의 `CODEX_HOME`으로 `codex logout`을 실행합니다.
|
|
172
|
+
|
|
173
|
+
옵션:
|
|
174
|
+
- `-p, --profile <name>`
|
|
175
|
+
- `--verbose`
|
|
176
|
+
|
|
177
|
+
예시:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
mux logout
|
|
181
|
+
mux logout -p a
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `mux run [codexArgs...]`
|
|
185
|
+
선택된 프로필의 `CODEX_HOME`으로 `codex`를 실행합니다.
|
|
186
|
+
|
|
187
|
+
옵션:
|
|
188
|
+
- `-p, --profile <name>`
|
|
189
|
+
- `--verbose`
|
|
190
|
+
|
|
191
|
+
예시:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
mux run
|
|
195
|
+
mux run -p b -- --help
|
|
196
|
+
mux run -p a chat
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### `mux list`
|
|
200
|
+
등록된 프로필 목록과 로그인 상태를 보여줍니다.
|
|
201
|
+
|
|
202
|
+
옵션:
|
|
203
|
+
- `--json`
|
|
204
|
+
- `--verbose`
|
|
205
|
+
|
|
206
|
+
예시:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
mux list
|
|
210
|
+
mux list --json
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Recommended Usage Pattern
|
|
214
|
+
|
|
215
|
+
- 터미널 1: `mux use a` -> `mux run`
|
|
216
|
+
- 터미널 2: `mux use b` -> `mux run`
|
|
217
|
+
|
|
218
|
+
필요할 때만 각 터미널에서 `mux login`/`mux logout`을 실행하세요.
|
package/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# codex-mux
|
|
2
|
+
|
|
3
|
+
`codex-mux` is an unofficial wrapper CLI that lets you use Codex CLI (`@openai/codex`) with multiple account profiles.
|
|
4
|
+
|
|
5
|
+
Primary goals:
|
|
6
|
+
- Use account A in terminal A and account B in terminal B at the same time
|
|
7
|
+
- Avoid repetitive `codex logout` -> `codex login` switching
|
|
8
|
+
- Isolate `CODEX_HOME` per profile automatically
|
|
9
|
+
|
|
10
|
+
## Important Notice (Unofficial)
|
|
11
|
+
|
|
12
|
+
- This project is not an official OpenAI product.
|
|
13
|
+
- OpenAI and Codex names are trademarks of their respective owners.
|
|
14
|
+
- This tool wraps an installed `codex` binary and does not provide account-limit bypass features.
|
|
15
|
+
|
|
16
|
+
## Prerequisites
|
|
17
|
+
|
|
18
|
+
- Node.js + npm
|
|
19
|
+
- Codex CLI installed
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i -g @openai/codex
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`codex` must be available in your PATH for `mux login`, `mux logout`, and `mux run`.
|
|
26
|
+
|
|
27
|
+
## Install (Local Development)
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install
|
|
31
|
+
npm run build
|
|
32
|
+
npm link
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then check:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
mux --help
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Why This Exists
|
|
42
|
+
|
|
43
|
+
Codex CLI uses a single default home (`~/.codex`). If you switch accounts often, repeated login/logout becomes tedious.
|
|
44
|
+
|
|
45
|
+
`codex-mux` isolates profile storage under:
|
|
46
|
+
|
|
47
|
+
- `~/.codex-mux/state.json`
|
|
48
|
+
- `~/.codex-mux/profiles/<profile>/codex-home`
|
|
49
|
+
|
|
50
|
+
Each profile has its own `auth.json`, so login sessions remain separated.
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
1. Select a profile
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
mux use a
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
2. Login for that profile
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
mux login
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
3. Run Codex with that profile
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
mux run
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
In another terminal:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
mux use b
|
|
76
|
+
mux login
|
|
77
|
+
mux run
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## A/B Account Example
|
|
81
|
+
|
|
82
|
+
Use two terminals and keep different accounts active.
|
|
83
|
+
|
|
84
|
+
Terminal 1 (Account A):
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
mux use a
|
|
88
|
+
mux login
|
|
89
|
+
mux run
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Terminal 2 (Account B):
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
mux use b
|
|
96
|
+
mux login
|
|
97
|
+
mux run
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Check status:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
mux current
|
|
104
|
+
mux list
|
|
105
|
+
mux list --json
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Logout when needed:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
mux logout -p a
|
|
112
|
+
mux logout -p b
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
If you prefer explicit profile flags:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
mux login -p a
|
|
119
|
+
mux run -p a
|
|
120
|
+
mux login -p b
|
|
121
|
+
mux run -p b
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Commands
|
|
125
|
+
|
|
126
|
+
### `mux use <profile>`
|
|
127
|
+
Set the active profile for the current terminal session.
|
|
128
|
+
|
|
129
|
+
Options:
|
|
130
|
+
- `--global`: set global default profile instead of session-only
|
|
131
|
+
- `--json`: JSON output
|
|
132
|
+
- `--verbose`: debug output
|
|
133
|
+
|
|
134
|
+
Examples:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
mux use a
|
|
138
|
+
mux use team-b --global
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### `mux current`
|
|
142
|
+
Show the active profile for this terminal.
|
|
143
|
+
|
|
144
|
+
Options:
|
|
145
|
+
- `-p, --profile <name>`: resolve/show a specific profile
|
|
146
|
+
- `--json`
|
|
147
|
+
- `--verbose`
|
|
148
|
+
|
|
149
|
+
Examples:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
mux current
|
|
153
|
+
mux current --json
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `mux login [codexArgs...]`
|
|
157
|
+
Run `codex login` with the selected profile's `CODEX_HOME`.
|
|
158
|
+
|
|
159
|
+
Options:
|
|
160
|
+
- `-p, --profile <name>`
|
|
161
|
+
- `--verbose`
|
|
162
|
+
|
|
163
|
+
Examples:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
mux login
|
|
167
|
+
mux login -p b
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### `mux logout [codexArgs...]`
|
|
171
|
+
Run `codex logout` with the selected profile's `CODEX_HOME`.
|
|
172
|
+
|
|
173
|
+
Options:
|
|
174
|
+
- `-p, --profile <name>`
|
|
175
|
+
- `--verbose`
|
|
176
|
+
|
|
177
|
+
Examples:
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
mux logout
|
|
181
|
+
mux logout -p a
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `mux run [codexArgs...]`
|
|
185
|
+
Run `codex` with the selected profile's `CODEX_HOME`.
|
|
186
|
+
|
|
187
|
+
Options:
|
|
188
|
+
- `-p, --profile <name>`
|
|
189
|
+
- `--verbose`
|
|
190
|
+
|
|
191
|
+
Examples:
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
mux run
|
|
195
|
+
mux run -p b -- --help
|
|
196
|
+
mux run -p a chat
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### `mux list`
|
|
200
|
+
List registered profiles and login status.
|
|
201
|
+
|
|
202
|
+
Options:
|
|
203
|
+
- `--json`
|
|
204
|
+
- `--verbose`
|
|
205
|
+
|
|
206
|
+
Examples:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
mux list
|
|
210
|
+
mux list --json
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Recommended Usage Pattern
|
|
214
|
+
|
|
215
|
+
- Terminal 1: `mux use a` -> `mux run`
|
|
216
|
+
- Terminal 2: `mux use b` -> `mux run`
|
|
217
|
+
|
|
218
|
+
Use `mux login`/`mux logout` only when needed.
|
|
219
|
+
|
|
220
|
+
## Korean README
|
|
221
|
+
|
|
222
|
+
For Korean documentation, see `README.ko.md`.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command, Option } from 'commander';
|
|
3
|
+
import { execa } from 'execa';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import which from 'which';
|
|
8
|
+
const DEFAULT_STATE = {
|
|
9
|
+
version: 1,
|
|
10
|
+
sessionProfiles: {},
|
|
11
|
+
profiles: {}
|
|
12
|
+
};
|
|
13
|
+
const MUX_HOME = process.env.CODEX_MUX_HOME ?? path.join(homedir(), '.codex-mux');
|
|
14
|
+
const STATE_FILE = path.join(MUX_HOME, 'state.json');
|
|
15
|
+
const PROFILES_DIR = path.join(MUX_HOME, 'profiles');
|
|
16
|
+
function ensureDir(dirPath) {
|
|
17
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
function readState() {
|
|
20
|
+
ensureDir(MUX_HOME);
|
|
21
|
+
if (!fs.existsSync(STATE_FILE)) {
|
|
22
|
+
return { ...DEFAULT_STATE };
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
const raw = fs.readFileSync(STATE_FILE, 'utf8');
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
return {
|
|
28
|
+
version: 1,
|
|
29
|
+
globalProfile: parsed.globalProfile,
|
|
30
|
+
sessionProfiles: parsed.sessionProfiles ?? {},
|
|
31
|
+
profiles: parsed.profiles ?? {}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return { ...DEFAULT_STATE };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function writeState(state) {
|
|
39
|
+
ensureDir(MUX_HOME);
|
|
40
|
+
fs.writeFileSync(STATE_FILE, `${JSON.stringify(state, null, 2)}\n`, 'utf8');
|
|
41
|
+
}
|
|
42
|
+
function sanitizeProfile(name) {
|
|
43
|
+
const trimmed = name.trim();
|
|
44
|
+
if (!trimmed) {
|
|
45
|
+
throw new Error('Profile name cannot be empty.');
|
|
46
|
+
}
|
|
47
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(trimmed)) {
|
|
48
|
+
throw new Error('Profile name must match /^[a-zA-Z0-9_-]+$/.');
|
|
49
|
+
}
|
|
50
|
+
return trimmed;
|
|
51
|
+
}
|
|
52
|
+
function profileCodexHome(name) {
|
|
53
|
+
return path.join(PROFILES_DIR, name, 'codex-home');
|
|
54
|
+
}
|
|
55
|
+
function ensureProfile(state, profile) {
|
|
56
|
+
if (!state.profiles[profile]) {
|
|
57
|
+
state.profiles[profile] = { codexHome: profileCodexHome(profile) };
|
|
58
|
+
}
|
|
59
|
+
ensureDir(state.profiles[profile].codexHome);
|
|
60
|
+
}
|
|
61
|
+
function getSessionKey() {
|
|
62
|
+
const envSession = process.env.CODEX_MUX_SESSION ??
|
|
63
|
+
process.env.TERM_SESSION_ID ??
|
|
64
|
+
process.env.ITERM_SESSION_ID ??
|
|
65
|
+
process.env.TMUX_PANE;
|
|
66
|
+
if (envSession) {
|
|
67
|
+
return envSession;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const ttyPath = fs.realpathSync('/dev/fd/0');
|
|
71
|
+
if (ttyPath && ttyPath !== '/dev/null') {
|
|
72
|
+
return ttyPath;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// ignore
|
|
77
|
+
}
|
|
78
|
+
return `pid:${process.ppid}`;
|
|
79
|
+
}
|
|
80
|
+
function resolveProfile(state, options) {
|
|
81
|
+
if (options.profile) {
|
|
82
|
+
const selected = sanitizeProfile(options.profile);
|
|
83
|
+
ensureProfile(state, selected);
|
|
84
|
+
return selected;
|
|
85
|
+
}
|
|
86
|
+
const sessionKey = getSessionKey();
|
|
87
|
+
const sessionProfile = state.sessionProfiles[sessionKey];
|
|
88
|
+
if (sessionProfile) {
|
|
89
|
+
ensureProfile(state, sessionProfile);
|
|
90
|
+
return sessionProfile;
|
|
91
|
+
}
|
|
92
|
+
if (state.globalProfile) {
|
|
93
|
+
ensureProfile(state, state.globalProfile);
|
|
94
|
+
return state.globalProfile;
|
|
95
|
+
}
|
|
96
|
+
throw new Error('No active profile. Run `mux use <profile>` first.');
|
|
97
|
+
}
|
|
98
|
+
function codexExecutable() {
|
|
99
|
+
try {
|
|
100
|
+
return which.sync('codex');
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
throw new Error('`codex` command not found in PATH. Install Codex CLI first.');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function runCodexWithProfile(profile, args, verbose = false) {
|
|
107
|
+
const state = readState();
|
|
108
|
+
ensureProfile(state, profile);
|
|
109
|
+
writeState(state);
|
|
110
|
+
const codexHome = state.profiles[profile].codexHome;
|
|
111
|
+
const codexBin = codexExecutable();
|
|
112
|
+
if (verbose) {
|
|
113
|
+
console.error(`[mux] profile=${profile} CODEX_HOME=${codexHome}`);
|
|
114
|
+
}
|
|
115
|
+
const child = execa(codexBin, args, {
|
|
116
|
+
stdio: 'inherit',
|
|
117
|
+
env: {
|
|
118
|
+
...process.env,
|
|
119
|
+
CODEX_HOME: codexHome
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
try {
|
|
123
|
+
await child;
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
const execaError = error;
|
|
128
|
+
return execaError.exitCode ?? 1;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function authSummary(codexHome) {
|
|
132
|
+
const authFile = path.join(codexHome, 'auth.json');
|
|
133
|
+
if (!fs.existsSync(authFile)) {
|
|
134
|
+
return { loggedIn: false };
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const raw = fs.readFileSync(authFile, 'utf8');
|
|
138
|
+
const parsed = JSON.parse(raw);
|
|
139
|
+
return {
|
|
140
|
+
loggedIn: true,
|
|
141
|
+
accountId: parsed.tokens?.account_id,
|
|
142
|
+
authMode: parsed.auth_mode
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return { loggedIn: true };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function printJson(value) {
|
|
150
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
151
|
+
}
|
|
152
|
+
async function main() {
|
|
153
|
+
const program = new Command();
|
|
154
|
+
const commonProfileOption = new Option('-p, --profile <name>', 'profile name');
|
|
155
|
+
const commonVerboseOption = new Option('--verbose', 'show debug details');
|
|
156
|
+
program
|
|
157
|
+
.name('mux')
|
|
158
|
+
.description('Multi-account profile switcher for Codex CLI')
|
|
159
|
+
.version('0.1.0');
|
|
160
|
+
program
|
|
161
|
+
.command('use <profile>')
|
|
162
|
+
.description('set active profile for this terminal session (or globally)')
|
|
163
|
+
.addOption(new Option('--global', 'set global default profile'))
|
|
164
|
+
.addOption(new Option('--json', 'output as JSON'))
|
|
165
|
+
.addOption(commonVerboseOption)
|
|
166
|
+
.action((profileArg, options) => {
|
|
167
|
+
const profile = sanitizeProfile(profileArg);
|
|
168
|
+
const state = readState();
|
|
169
|
+
ensureProfile(state, profile);
|
|
170
|
+
const sessionKey = getSessionKey();
|
|
171
|
+
if (options.global) {
|
|
172
|
+
state.globalProfile = profile;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
state.sessionProfiles[sessionKey] = profile;
|
|
176
|
+
}
|
|
177
|
+
writeState(state);
|
|
178
|
+
const codexHome = state.profiles[profile].codexHome;
|
|
179
|
+
if (options.json) {
|
|
180
|
+
printJson({
|
|
181
|
+
ok: true,
|
|
182
|
+
profile,
|
|
183
|
+
scope: options.global ? 'global' : 'session',
|
|
184
|
+
sessionKey: options.global ? undefined : sessionKey,
|
|
185
|
+
codexHome
|
|
186
|
+
});
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (options.verbose) {
|
|
190
|
+
console.log(`profile=${profile}`);
|
|
191
|
+
console.log(`scope=${options.global ? 'global' : 'session'}`);
|
|
192
|
+
console.log(`session=${sessionKey}`);
|
|
193
|
+
console.log(`CODEX_HOME=${codexHome}`);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
console.log(`${profile}`);
|
|
197
|
+
});
|
|
198
|
+
program
|
|
199
|
+
.command('current')
|
|
200
|
+
.description('show active profile for this terminal')
|
|
201
|
+
.addOption(commonProfileOption)
|
|
202
|
+
.addOption(new Option('--json', 'output as JSON'))
|
|
203
|
+
.addOption(commonVerboseOption)
|
|
204
|
+
.action((options) => {
|
|
205
|
+
const state = readState();
|
|
206
|
+
const sessionKey = getSessionKey();
|
|
207
|
+
const profile = resolveProfile(state, options);
|
|
208
|
+
ensureProfile(state, profile);
|
|
209
|
+
writeState(state);
|
|
210
|
+
const codexHome = state.profiles[profile].codexHome;
|
|
211
|
+
if (options.json) {
|
|
212
|
+
printJson({
|
|
213
|
+
profile,
|
|
214
|
+
codexHome,
|
|
215
|
+
from: options.profile ? 'option' : state.sessionProfiles[sessionKey] ? 'session' : 'global',
|
|
216
|
+
sessionKey
|
|
217
|
+
});
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (options.verbose) {
|
|
221
|
+
console.log(`profile=${profile}`);
|
|
222
|
+
console.log(`session=${sessionKey}`);
|
|
223
|
+
console.log(`CODEX_HOME=${codexHome}`);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
console.log(`${profile}`);
|
|
227
|
+
});
|
|
228
|
+
program
|
|
229
|
+
.command('login')
|
|
230
|
+
.description('run `codex login` with selected profile')
|
|
231
|
+
.allowUnknownOption(true)
|
|
232
|
+
.addOption(commonProfileOption)
|
|
233
|
+
.addOption(commonVerboseOption)
|
|
234
|
+
.argument('[codexArgs...]', 'extra args forwarded to codex login')
|
|
235
|
+
.action(async (codexArgs, options) => {
|
|
236
|
+
const state = readState();
|
|
237
|
+
const profile = resolveProfile(state, options);
|
|
238
|
+
writeState(state);
|
|
239
|
+
const exitCode = await runCodexWithProfile(profile, ['login', ...codexArgs], options.verbose);
|
|
240
|
+
process.exitCode = exitCode;
|
|
241
|
+
});
|
|
242
|
+
program
|
|
243
|
+
.command('logout')
|
|
244
|
+
.description('run `codex logout` with selected profile')
|
|
245
|
+
.allowUnknownOption(true)
|
|
246
|
+
.addOption(commonProfileOption)
|
|
247
|
+
.addOption(commonVerboseOption)
|
|
248
|
+
.argument('[codexArgs...]', 'extra args forwarded to codex logout')
|
|
249
|
+
.action(async (codexArgs, options) => {
|
|
250
|
+
const state = readState();
|
|
251
|
+
const profile = resolveProfile(state, options);
|
|
252
|
+
writeState(state);
|
|
253
|
+
const exitCode = await runCodexWithProfile(profile, ['logout', ...codexArgs], options.verbose);
|
|
254
|
+
process.exitCode = exitCode;
|
|
255
|
+
});
|
|
256
|
+
program
|
|
257
|
+
.command('run')
|
|
258
|
+
.description('run `codex ...` with selected profile')
|
|
259
|
+
.allowUnknownOption(true)
|
|
260
|
+
.addOption(commonProfileOption)
|
|
261
|
+
.addOption(commonVerboseOption)
|
|
262
|
+
.argument('[codexArgs...]', 'args forwarded to codex')
|
|
263
|
+
.action(async (codexArgs, options) => {
|
|
264
|
+
const state = readState();
|
|
265
|
+
const profile = resolveProfile(state, options);
|
|
266
|
+
writeState(state);
|
|
267
|
+
const exitCode = await runCodexWithProfile(profile, codexArgs, options.verbose);
|
|
268
|
+
process.exitCode = exitCode;
|
|
269
|
+
});
|
|
270
|
+
program
|
|
271
|
+
.command('list')
|
|
272
|
+
.description('list profiles and login status')
|
|
273
|
+
.addOption(new Option('--json', 'output as JSON'))
|
|
274
|
+
.addOption(commonVerboseOption)
|
|
275
|
+
.action((options) => {
|
|
276
|
+
const state = readState();
|
|
277
|
+
const sessionKey = getSessionKey();
|
|
278
|
+
const activeSession = state.sessionProfiles[sessionKey];
|
|
279
|
+
const rows = Object.keys(state.profiles)
|
|
280
|
+
.sort()
|
|
281
|
+
.map((name) => {
|
|
282
|
+
const codexHome = state.profiles[name].codexHome;
|
|
283
|
+
const auth = authSummary(codexHome);
|
|
284
|
+
return {
|
|
285
|
+
profile: name,
|
|
286
|
+
codexHome,
|
|
287
|
+
sessionActive: activeSession === name,
|
|
288
|
+
globalDefault: state.globalProfile === name,
|
|
289
|
+
loggedIn: auth.loggedIn,
|
|
290
|
+
accountId: auth.accountId,
|
|
291
|
+
authMode: auth.authMode
|
|
292
|
+
};
|
|
293
|
+
});
|
|
294
|
+
if (options.json) {
|
|
295
|
+
printJson({
|
|
296
|
+
sessionKey,
|
|
297
|
+
activeSession,
|
|
298
|
+
globalProfile: state.globalProfile,
|
|
299
|
+
profiles: rows
|
|
300
|
+
});
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (rows.length === 0) {
|
|
304
|
+
console.log('No profiles yet. Run `mux use <profile>` first.');
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
for (const row of rows) {
|
|
308
|
+
const tags = [];
|
|
309
|
+
if (row.sessionActive) {
|
|
310
|
+
tags.push('session');
|
|
311
|
+
}
|
|
312
|
+
if (row.globalDefault) {
|
|
313
|
+
tags.push('global');
|
|
314
|
+
}
|
|
315
|
+
const status = row.loggedIn ? `logged-in${row.accountId ? `:${row.accountId}` : ''}` : 'logged-out';
|
|
316
|
+
const tagText = tags.length > 0 ? ` [${tags.join(',')}]` : '';
|
|
317
|
+
const base = `${row.profile}${tagText} ${status}`;
|
|
318
|
+
if (options.verbose) {
|
|
319
|
+
console.log(`${base} CODEX_HOME=${row.codexHome}`);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
console.log(base);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
await program.parseAsync(process.argv);
|
|
327
|
+
}
|
|
328
|
+
main().catch((error) => {
|
|
329
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
330
|
+
console.error(`Error: ${message}`);
|
|
331
|
+
process.exit(1);
|
|
332
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "codex-mux",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Unofficial wrapper CLI for using Codex CLI with multiple account profiles",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"bin": {
|
|
12
|
+
"mux": "dist/index.js",
|
|
13
|
+
"cx": "dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "tsx src/index.ts",
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"prepublishOnly": "npm run build"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/node": "^25.1.0",
|
|
22
|
+
"@types/which": "^3.0.4",
|
|
23
|
+
"tsx": "^4.21.0",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"commander": "^14.0.3",
|
|
28
|
+
"execa": "^9.6.1",
|
|
29
|
+
"which": "^6.0.0"
|
|
30
|
+
}
|
|
31
|
+
}
|