cc-provider-switcher 0.3.0 → 0.5.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/README.md +62 -24
- package/dist/cli.js +268 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ npm install -g cc-provider-switcher@latest
|
|
|
49
49
|
|
|
50
50
|
ccs sets `ANTHROPIC_DEFAULT_*_MODEL_SUPPORTED_CAPABILITIES` so Claude Code knows which thinking features a provider supports. No extra steps needed after switching.
|
|
51
51
|
|
|
52
|
-
**Providers with effort control** (DeepSeek, GLM, Qwen, Kimi, StepFun):
|
|
52
|
+
**Providers with effort control** (DeepSeek, MiniMax, GLM, Qwen, Kimi, StepFun):
|
|
53
53
|
|
|
54
54
|
```sh
|
|
55
55
|
ccs deepseek
|
|
@@ -59,15 +59,7 @@ ccs deepseek
|
|
|
59
59
|
|
|
60
60
|
```sh
|
|
61
61
|
ccs glm
|
|
62
|
-
# Same, plus max_effort tier available
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
**MiniMax — adaptive thinking:**
|
|
66
|
-
|
|
67
|
-
```sh
|
|
68
|
-
ccs minimax
|
|
69
|
-
# MiniMax decides when to think based on task complexity — nothing to configure
|
|
70
|
-
# Note shown: "Only adaptive/disabled thinking (enabled returns 400)"
|
|
62
|
+
# Same, plus max_effort tier available (also on MiniMax)
|
|
71
63
|
```
|
|
72
64
|
|
|
73
65
|
**Seed/Doubao — thinking on/off only:**
|
|
@@ -85,6 +77,7 @@ ccs seed
|
|
|
85
77
|
|---------|-------------|
|
|
86
78
|
| `ccs <provider> [region]` | Switch Claude Code to a provider (user-level) |
|
|
87
79
|
| `ccs project <provider> [region]` | Switch for current project only |
|
|
80
|
+
| `ccs <provider> --model <id>` | Use one model ID for sonnet/opus/haiku (also works with `project`) |
|
|
88
81
|
| `ccs list` | List all available providers |
|
|
89
82
|
| `ccs status` | Show current provider, model, and API key |
|
|
90
83
|
| `ccs config` | Interactive API key setup |
|
|
@@ -104,19 +97,19 @@ ccs seed
|
|
|
104
97
|
| Name | Key | Aliases | Thinking | Regions |
|
|
105
98
|
|------|-----|---------|----------|---------|
|
|
106
99
|
| DeepSeek | `deepseek` | `ds` | effort | global |
|
|
107
|
-
| MiniMax | `minimax` | `mm` |
|
|
100
|
+
| MiniMax | `minimax` | `mm` | effort | global, china |
|
|
108
101
|
| Kimi | `kimi` | `kimi2` | effort | global, china |
|
|
109
102
|
| Qwen | `qwen` | — | effort | global, china |
|
|
110
103
|
| GLM | `glm` | `glm5` | effort | global, china |
|
|
111
104
|
| Seed/Doubao | `seed` | `doubao` | on/off | global |
|
|
112
105
|
| StepFun | `stepfun` | `sf` | effort | global |
|
|
113
106
|
| OpenRouter | `openrouter` | `or` | passthrough | global |
|
|
107
|
+
| LM Studio | `lmstudio` | `lms` | — | local |
|
|
114
108
|
|
|
115
109
|
Claude (Anthropic) isn't a switchable provider — it's Claude Code's own native default. Run `ccs reset` to return to it.
|
|
116
110
|
|
|
117
111
|
**Thinking column:**
|
|
118
112
|
- `effort` — reasoning_effort levels (low/medium/high)
|
|
119
|
-
- `adaptive` — adaptive thinking only (`enabled` causes 400 on this provider)
|
|
120
113
|
- `on/off` — thinking enabled/disabled, no effort control
|
|
121
114
|
- `passthrough` — depends on the model routed through OpenRouter
|
|
122
115
|
|
|
@@ -126,14 +119,15 @@ When Claude Code picks haiku/sonnet/opus, ccs maps them to the actual model IDs
|
|
|
126
119
|
|
|
127
120
|
| Provider | Haiku | Sonnet | Opus |
|
|
128
121
|
|----------|-------|--------|------|
|
|
129
|
-
| DeepSeek | `deepseek-
|
|
130
|
-
| MiniMax | `MiniMax-M3` | `MiniMax-M3` | `MiniMax-M3` |
|
|
131
|
-
| Kimi | `kimi-k3` | `kimi-k3` | `kimi-k3` |
|
|
132
|
-
| Qwen | `qwen3.7-plus` | `qwen3.7-plus` | `qwen3.7-
|
|
133
|
-
| GLM | `glm-5.
|
|
122
|
+
| DeepSeek | `deepseek-flash` | `deepseek-flash` | `deepseek-flash` |
|
|
123
|
+
| MiniMax | `MiniMax-M3.1-Flash` | `MiniMax-M3.1-Flash` | `MiniMax-M3.1-Flash` |
|
|
124
|
+
| Kimi | `kimi-k3.1-pro-highspeed` | `kimi-k3.1-pro` | `kimi-k3.1-pro` |
|
|
125
|
+
| Qwen | `qwen3.7-plus` | `qwen3.7-plus` | `qwen3.7-plus` |
|
|
126
|
+
| GLM | `glm-5.3-flash` | `glm-5.3` | `glm-5.3` |
|
|
134
127
|
| Seed/Doubao | `ark-code-latest` | `ark-code-latest` | `ark-code-latest` |
|
|
135
128
|
| StepFun | `step-3.7-flash` | `step-3.7-flash` | `step-3.7-flash` |
|
|
136
|
-
| OpenRouter | `anthropic/claude-haiku-
|
|
129
|
+
| OpenRouter | `anthropic/claude-haiku-5.5` | `anthropic/claude-sonnet-5.5` | `anthropic/claude-opus-5.5` |
|
|
130
|
+
| LM Studio | `qwen/qwen3.8-27b` | `qwen/qwen3.8-27b` | `qwen/qwen3.8-27b` |
|
|
137
131
|
|
|
138
132
|
Override any ID via `providerOverrides` in `~/.ccs/config.json` — see [Override model IDs](#override-model-ids).
|
|
139
133
|
|
|
@@ -179,6 +173,29 @@ Update model IDs without waiting for a release:
|
|
|
179
173
|
}
|
|
180
174
|
```
|
|
181
175
|
|
|
176
|
+
### LM Studio
|
|
177
|
+
|
|
178
|
+
`ccs lmstudio` (alias `lms`) points Claude Code at `http://127.0.0.1:1234`. Start the LM Studio server first. No API key needed. To use another model, pass it with `--model`:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
ccs lmstudio --model my/model-id
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
To make it permanent, override all four model fields (overrides replace `models` as a whole):
|
|
185
|
+
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"providerOverrides": {
|
|
189
|
+
"lmstudio": {
|
|
190
|
+
"defaultModel": "my/model-id",
|
|
191
|
+
"models": { "sonnet": "my/model-id", "opus": "my/model-id", "haiku": "my/model-id" }
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Use the "API Model Identifier" shown in LM Studio. Set the context length to 32k or more.
|
|
198
|
+
|
|
182
199
|
### What ccs writes
|
|
183
200
|
|
|
184
201
|
ccs modifies only the `env` block in Claude Code settings. It never touches other fields (permissions, hooks, etc.).
|
|
@@ -191,11 +208,11 @@ ccs modifies only the `env` block in Claude Code settings. It never touches othe
|
|
|
191
208
|
"env": {
|
|
192
209
|
"ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
|
|
193
210
|
"ANTHROPIC_AUTH_TOKEN": "sk-...",
|
|
194
|
-
"ANTHROPIC_MODEL": "deepseek-
|
|
195
|
-
"ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-
|
|
196
|
-
"ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-
|
|
197
|
-
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-
|
|
198
|
-
"CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-
|
|
211
|
+
"ANTHROPIC_MODEL": "deepseek-flash",
|
|
212
|
+
"ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-flash",
|
|
213
|
+
"ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-flash",
|
|
214
|
+
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-flash",
|
|
215
|
+
"CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-flash",
|
|
199
216
|
"ANTHROPIC_DEFAULT_SONNET_MODEL_SUPPORTED_CAPABILITIES": "effort,thinking",
|
|
200
217
|
"ANTHROPIC_DEFAULT_OPUS_MODEL_SUPPORTED_CAPABILITIES": "effort,thinking",
|
|
201
218
|
"ANTHROPIC_DEFAULT_HAIKU_MODEL_SUPPORTED_CAPABILITIES": "effort,thinking"
|
|
@@ -218,7 +235,28 @@ ccs account list # list all saved accounts
|
|
|
218
235
|
ccs account delete personal # delete "personal"
|
|
219
236
|
```
|
|
220
237
|
|
|
221
|
-
|
|
238
|
+
ccs reads and writes the login where Claude Code keeps it:
|
|
239
|
+
|
|
240
|
+
- macOS: the `Claude Code-credentials` item in the login Keychain
|
|
241
|
+
- Linux and Windows: `~/.claude/.credentials.json`
|
|
242
|
+
|
|
243
|
+
A profile also saves the `oauthAccount` block from `~/.claude.json` (the account email and organization), and `switch` writes it back.
|
|
244
|
+
|
|
245
|
+
ccs keeps profiles in `~/.ccs/accounts/` with `0600` permissions.
|
|
246
|
+
|
|
247
|
+
Quit every Claude Code session before you run `ccs account switch`, then start Claude Code again. A session left open can write its old login back when it refreshes its token.
|
|
248
|
+
|
|
249
|
+
Claude Code refreshes the login after you save it, so `switch` first copies the current login into the profile with the same account and organization. If no profile matches, `switch` stops and changes nothing. Run `ccs account save <name>` first.
|
|
250
|
+
|
|
251
|
+
### When a provider is active
|
|
252
|
+
|
|
253
|
+
While a provider is active, Claude Code does not use your `/login` account. `ccs <provider>` writes `ANTHROPIC_AUTH_TOKEN`, and Claude Code ranks that token above the login. `ccs account switch` still swaps the saved login, and Claude Code uses it again once you remove the provider with `ccs reset`.
|
|
254
|
+
|
|
255
|
+
`ccs status` shows the account you are logged in to and the credential Claude Code will use. When that credential is not the login, `ccs status` and `ccs account switch` say where it comes from and how to remove it. `ccs account list` puts a `*` next to the profile you are logged in with.
|
|
256
|
+
|
|
257
|
+
With a provider active and a login saved, Claude Code shows "Both ANTHROPIC_AUTH_TOKEN and /login managed key set · auth may not work as expected". This is expected: requests go to the provider with its token. Run `ccs reset` to go back to the login.
|
|
258
|
+
|
|
259
|
+
ccs does not yet handle `CLAUDE_CONFIG_DIR` or Claude Code's opt-in Windows Credential Manager storage.
|
|
222
260
|
|
|
223
261
|
---
|
|
224
262
|
|
package/dist/cli.js
CHANGED
|
@@ -12,6 +12,7 @@ var ProviderSchema = z.object({
|
|
|
12
12
|
defaultModel: z.string(),
|
|
13
13
|
aliases: z.array(z.string()),
|
|
14
14
|
keyEnvVar: z.string(),
|
|
15
|
+
keyOptional: z.boolean().optional(),
|
|
15
16
|
models: z.object({
|
|
16
17
|
sonnet: z.string(),
|
|
17
18
|
opus: z.string(),
|
|
@@ -42,44 +43,44 @@ var providers_default = {
|
|
|
42
43
|
deepseek: {
|
|
43
44
|
name: "DeepSeek",
|
|
44
45
|
baseUrl: "https://api.deepseek.com/anthropic",
|
|
45
|
-
defaultModel: "deepseek-
|
|
46
|
+
defaultModel: "deepseek-flash",
|
|
46
47
|
aliases: ["ds"],
|
|
47
48
|
keyEnvVar: "DEEPSEEK_API_KEY",
|
|
48
|
-
models: { sonnet: "deepseek-
|
|
49
|
+
models: { sonnet: "deepseek-flash", opus: "deepseek-flash", haiku: "deepseek-flash" },
|
|
49
50
|
supportedCapabilities: ["effort", "thinking"],
|
|
50
51
|
thinkingNote: "budget_tokens ignored, uses reasoning_effort internally"
|
|
51
52
|
},
|
|
52
53
|
minimax: {
|
|
53
54
|
name: "MiniMax",
|
|
54
55
|
baseUrl: "https://api.minimax.io/anthropic",
|
|
55
|
-
defaultModel: "MiniMax-M3",
|
|
56
|
+
defaultModel: "MiniMax-M3.1-Flash",
|
|
56
57
|
aliases: ["mm"],
|
|
57
58
|
keyEnvVar: "MINIMAX_API_KEY",
|
|
58
59
|
regions: {
|
|
59
60
|
global: "https://api.minimax.io/anthropic",
|
|
60
61
|
china: "https://api.minimaxi.com/anthropic"
|
|
61
62
|
},
|
|
62
|
-
models: { sonnet: "MiniMax-M3", opus: "MiniMax-M3", haiku: "MiniMax-M3" },
|
|
63
|
-
supportedCapabilities: ["
|
|
64
|
-
thinkingNote: "
|
|
63
|
+
models: { sonnet: "MiniMax-M3.1-Flash", opus: "MiniMax-M3.1-Flash", haiku: "MiniMax-M3.1-Flash" },
|
|
64
|
+
supportedCapabilities: ["effort", "max_effort", "thinking"],
|
|
65
|
+
thinkingNote: "Always thinks (disabled returns 400). Effort: low/medium/high/xhigh/max"
|
|
65
66
|
},
|
|
66
67
|
kimi: {
|
|
67
68
|
name: "Kimi",
|
|
68
69
|
baseUrl: "https://api.moonshot.ai/anthropic",
|
|
69
|
-
defaultModel: "kimi-k3",
|
|
70
|
+
defaultModel: "kimi-k3.1-pro",
|
|
70
71
|
aliases: ["kimi2"],
|
|
71
72
|
keyEnvVar: "KIMI_API_KEY",
|
|
72
73
|
regions: {
|
|
73
74
|
global: "https://api.moonshot.ai/anthropic",
|
|
74
75
|
china: "https://api.moonshot.cn/anthropic"
|
|
75
76
|
},
|
|
76
|
-
models: { sonnet: "kimi-k3", opus: "kimi-k3", haiku: "kimi-k3" },
|
|
77
|
+
models: { sonnet: "kimi-k3.1-pro", opus: "kimi-k3.1-pro", haiku: "kimi-k3.1-pro-highspeed" },
|
|
77
78
|
supportedCapabilities: ["effort", "thinking"]
|
|
78
79
|
},
|
|
79
80
|
qwen: {
|
|
80
81
|
name: "Qwen",
|
|
81
82
|
baseUrl: "https://coding-intl.dashscope.aliyuncs.com/apps/anthropic",
|
|
82
|
-
defaultModel: "qwen3.7-
|
|
83
|
+
defaultModel: "qwen3.7-plus",
|
|
83
84
|
aliases: [],
|
|
84
85
|
keyEnvVar: "QWEN_API_KEY",
|
|
85
86
|
regions: {
|
|
@@ -88,7 +89,7 @@ var providers_default = {
|
|
|
88
89
|
},
|
|
89
90
|
models: {
|
|
90
91
|
sonnet: "qwen3.7-plus",
|
|
91
|
-
opus: "qwen3.7-
|
|
92
|
+
opus: "qwen3.7-plus",
|
|
92
93
|
haiku: "qwen3.7-plus"
|
|
93
94
|
},
|
|
94
95
|
supportedCapabilities: ["effort", "thinking"],
|
|
@@ -97,16 +98,16 @@ var providers_default = {
|
|
|
97
98
|
glm: {
|
|
98
99
|
name: "GLM",
|
|
99
100
|
baseUrl: "https://api.z.ai/api/anthropic",
|
|
100
|
-
defaultModel: "glm-5.
|
|
101
|
+
defaultModel: "glm-5.3",
|
|
101
102
|
aliases: ["glm5"],
|
|
102
103
|
keyEnvVar: "GLM_API_KEY",
|
|
103
104
|
regions: {
|
|
104
105
|
global: "https://api.z.ai/api/anthropic",
|
|
105
106
|
china: "https://open.bigmodel.cn/api/anthropic"
|
|
106
107
|
},
|
|
107
|
-
models: { sonnet: "glm-5.
|
|
108
|
+
models: { sonnet: "glm-5.3", opus: "glm-5.3", haiku: "glm-5.3-flash" },
|
|
108
109
|
supportedCapabilities: ["effort", "max_effort", "thinking"],
|
|
109
|
-
thinkingNote: "
|
|
110
|
+
thinkingNote: "Always reasons (disabled fails). Effort: low/high/max"
|
|
110
111
|
},
|
|
111
112
|
seed: {
|
|
112
113
|
name: "Seed/Doubao",
|
|
@@ -124,7 +125,7 @@ var providers_default = {
|
|
|
124
125
|
},
|
|
125
126
|
stepfun: {
|
|
126
127
|
name: "StepFun",
|
|
127
|
-
baseUrl: "https://api.stepfun.ai
|
|
128
|
+
baseUrl: "https://api.stepfun.ai",
|
|
128
129
|
defaultModel: "step-3.7-flash",
|
|
129
130
|
aliases: ["sf"],
|
|
130
131
|
keyEnvVar: "STEPFUN_API_KEY",
|
|
@@ -139,14 +140,28 @@ var providers_default = {
|
|
|
139
140
|
openrouter: {
|
|
140
141
|
name: "OpenRouter",
|
|
141
142
|
baseUrl: "https://openrouter.ai/api",
|
|
142
|
-
defaultModel: "anthropic/claude-sonnet-5",
|
|
143
|
+
defaultModel: "anthropic/claude-sonnet-5.5",
|
|
143
144
|
aliases: ["or"],
|
|
144
145
|
keyEnvVar: "OPENROUTER_API_KEY",
|
|
145
146
|
models: {
|
|
146
|
-
sonnet: "anthropic/claude-sonnet-5",
|
|
147
|
-
opus: "anthropic/claude-opus-
|
|
148
|
-
haiku: "anthropic/claude-haiku-
|
|
147
|
+
sonnet: "anthropic/claude-sonnet-5.5",
|
|
148
|
+
opus: "anthropic/claude-opus-5.5",
|
|
149
|
+
haiku: "anthropic/claude-haiku-5.5"
|
|
149
150
|
}
|
|
151
|
+
},
|
|
152
|
+
lmstudio: {
|
|
153
|
+
name: "LM Studio",
|
|
154
|
+
baseUrl: "http://127.0.0.1:1234",
|
|
155
|
+
defaultModel: "qwen/qwen3.8-27b",
|
|
156
|
+
aliases: ["lms"],
|
|
157
|
+
keyEnvVar: "LMSTUDIO_API_KEY",
|
|
158
|
+
keyOptional: true,
|
|
159
|
+
models: {
|
|
160
|
+
sonnet: "qwen/qwen3.8-27b",
|
|
161
|
+
opus: "qwen/qwen3.8-27b",
|
|
162
|
+
haiku: "qwen/qwen3.8-27b"
|
|
163
|
+
},
|
|
164
|
+
thinkingNote: "Local model. Use --model <id> to pick another one"
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
167
|
};
|
|
@@ -185,6 +200,9 @@ function getApiKey(provider, userConfig) {
|
|
|
185
200
|
if (envVal) return envVal;
|
|
186
201
|
return userConfig?.keys?.[provider.registryKey] ?? null;
|
|
187
202
|
}
|
|
203
|
+
function withModel(provider, model) {
|
|
204
|
+
return { ...provider, defaultModel: model, models: { sonnet: model, opus: model, haiku: model } };
|
|
205
|
+
}
|
|
188
206
|
|
|
189
207
|
// src/config/user-config.ts
|
|
190
208
|
import readline from "readline";
|
|
@@ -198,9 +216,13 @@ var CCS_ACCOUNTS_DIR = path.join(CCS_DIR, "accounts");
|
|
|
198
216
|
var CLAUDE_DIR = path.join(os.homedir(), ".claude");
|
|
199
217
|
var CLAUDE_SETTINGS_FILE = path.join(CLAUDE_DIR, "settings.json");
|
|
200
218
|
var CLAUDE_CREDENTIALS_FILE = path.join(CLAUDE_DIR, ".credentials.json");
|
|
219
|
+
var CLAUDE_GLOBAL_CONFIG_FILE = path.join(os.homedir(), ".claude.json");
|
|
201
220
|
function getProjectSettingsPath() {
|
|
202
221
|
return path.join(process.cwd(), ".claude", "settings.local.json");
|
|
203
222
|
}
|
|
223
|
+
function getSharedProjectSettingsPath() {
|
|
224
|
+
return path.join(process.cwd(), ".claude", "settings.json");
|
|
225
|
+
}
|
|
204
226
|
|
|
205
227
|
// src/utils/fs.ts
|
|
206
228
|
import fs from "fs/promises";
|
|
@@ -348,6 +370,9 @@ function success(msg) {
|
|
|
348
370
|
function error(msg) {
|
|
349
371
|
console.error(chalk.red("\u2717 " + msg));
|
|
350
372
|
}
|
|
373
|
+
function warn(msg) {
|
|
374
|
+
console.warn(chalk.yellow("\u26A0 " + msg));
|
|
375
|
+
}
|
|
351
376
|
function info(msg) {
|
|
352
377
|
console.log(chalk.cyan("\u2139 " + msg));
|
|
353
378
|
}
|
|
@@ -364,16 +389,17 @@ function table(rows) {
|
|
|
364
389
|
|
|
365
390
|
// src/commands/switch.ts
|
|
366
391
|
var CLAUDE_NATIVE_NAMES = /* @__PURE__ */ new Set(["claude", "anthropic", "sonnet"]);
|
|
367
|
-
async function doSwitch(providerName, region, scope) {
|
|
392
|
+
async function doSwitch(providerName, region, scope, model) {
|
|
368
393
|
if (CLAUDE_NATIVE_NAMES.has(providerName.toLowerCase())) {
|
|
369
394
|
info("Claude is Claude Code's native default \u2014 there is nothing to switch to.");
|
|
370
395
|
info('Run "ccs reset" to clear provider overrides and return to it.');
|
|
371
396
|
return;
|
|
372
397
|
}
|
|
373
398
|
const userConfig = await loadUserConfig();
|
|
374
|
-
const
|
|
399
|
+
const resolved = resolveProvider(providerName, userConfig);
|
|
400
|
+
const provider = model ? withModel(resolved, model) : resolved;
|
|
375
401
|
const baseUrl = resolveBaseUrl(provider, region);
|
|
376
|
-
const apiKey = getApiKey(provider, userConfig);
|
|
402
|
+
const apiKey = getApiKey(provider, userConfig) ?? (provider.keyOptional ? provider.registryKey : null);
|
|
377
403
|
if (!apiKey) {
|
|
378
404
|
error(
|
|
379
405
|
`No API key for ${provider.name}. Set ${provider.keyEnvVar} or run "ccs config".`
|
|
@@ -406,14 +432,85 @@ async function doSwitch(providerName, region, scope) {
|
|
|
406
432
|
if (provider.thinkingNote) info(`Thinking: ${provider.thinkingNote}`);
|
|
407
433
|
}
|
|
408
434
|
function registerSwitchCommand(program2) {
|
|
409
|
-
program2.argument("<provider>", "Provider name or alias").argument("[region]", "Optional region (global, china, etc.)").action(async (provider, region) => {
|
|
410
|
-
await doSwitch(provider, region, "user");
|
|
435
|
+
program2.argument("<provider>", "Provider name or alias").argument("[region]", "Optional region (global, china, etc.)").option("-m, --model <id>", "Use this model ID for all of sonnet/opus/haiku").action(async (provider, region, opts) => {
|
|
436
|
+
await doSwitch(provider, region, "user", opts.model);
|
|
411
437
|
});
|
|
412
|
-
program2.command("project <provider> [region]").description("Switch provider for current project only").action(async (provider, region) => {
|
|
413
|
-
await doSwitch(provider, region, "project");
|
|
438
|
+
program2.command("project <provider> [region]").description("Switch provider for current project only").option("-m, --model <id>", "Use this model ID for all of sonnet/opus/haiku").action(async (provider, region, opts) => {
|
|
439
|
+
await doSwitch(provider, region, "project", opts.model);
|
|
414
440
|
});
|
|
415
441
|
}
|
|
416
442
|
|
|
443
|
+
// src/config/auth-source.ts
|
|
444
|
+
import path4 from "path";
|
|
445
|
+
var CLOUD_FLAGS = ["CLAUDE_CODE_USE_BEDROCK", "CLAUDE_CODE_USE_VERTEX", "CLAUDE_CODE_USE_FOUNDRY"];
|
|
446
|
+
var PRECEDENCE = [
|
|
447
|
+
...CLOUD_FLAGS,
|
|
448
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
449
|
+
"ANTHROPIC_API_KEY",
|
|
450
|
+
"apiKeyHelper",
|
|
451
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
452
|
+
"ANTHROPIC_PROFILE",
|
|
453
|
+
// Federation counts only together with ANTHROPIC_ORGANIZATION_ID.
|
|
454
|
+
"ANTHROPIC_FEDERATION_RULE_ID"
|
|
455
|
+
];
|
|
456
|
+
var USER_SCOPE_ONLY = /* @__PURE__ */ new Set(["CLAUDE_CODE_OAUTH_TOKEN"]);
|
|
457
|
+
var ORIGIN_LABEL = {
|
|
458
|
+
shell: "your shell",
|
|
459
|
+
user: "~/.claude/settings.json",
|
|
460
|
+
shared: ".claude/settings.json",
|
|
461
|
+
local: ".claude/settings.local.json"
|
|
462
|
+
};
|
|
463
|
+
function resolveAuthSource(layers) {
|
|
464
|
+
const highestFirst = [...layers].reverse();
|
|
465
|
+
const lookup = (name) => {
|
|
466
|
+
for (const layer of highestFirst) {
|
|
467
|
+
if (USER_SCOPE_ONLY.has(name) && (layer.from === "shared" || layer.from === "local")) continue;
|
|
468
|
+
const value = name === "apiKeyHelper" ? layer.apiKeyHelper : layer.env?.[name];
|
|
469
|
+
if (value === void 0) continue;
|
|
470
|
+
const on = CLOUD_FLAGS.includes(name) ? typeof value === "string" && /^(1|true|yes|on)$/i.test(value) : typeof value === "string" && value !== "";
|
|
471
|
+
return on ? { name, from: layer.from } : void 0;
|
|
472
|
+
}
|
|
473
|
+
return void 0;
|
|
474
|
+
};
|
|
475
|
+
for (const name of PRECEDENCE) {
|
|
476
|
+
const source = lookup(name);
|
|
477
|
+
if (!source) continue;
|
|
478
|
+
if (name === "ANTHROPIC_FEDERATION_RULE_ID" && !lookup("ANTHROPIC_ORGANIZATION_ID")) continue;
|
|
479
|
+
return source;
|
|
480
|
+
}
|
|
481
|
+
return void 0;
|
|
482
|
+
}
|
|
483
|
+
async function readAuthState(claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
484
|
+
const sharedPath = getSharedProjectSettingsPath();
|
|
485
|
+
const sharedIsUser = path4.resolve(sharedPath) === path4.resolve(CLAUDE_SETTINGS_FILE);
|
|
486
|
+
const [user, shared, local, config] = await Promise.all([
|
|
487
|
+
readUserSettings(),
|
|
488
|
+
sharedIsUser ? {} : readProjectSettings(sharedPath),
|
|
489
|
+
readProjectSettings(getProjectSettingsPath()),
|
|
490
|
+
readJsonFile(claudeConfigPath)
|
|
491
|
+
]);
|
|
492
|
+
const source = resolveAuthSource([
|
|
493
|
+
{ from: "shell", env: process.env },
|
|
494
|
+
{ ...user, from: "user" },
|
|
495
|
+
{ ...shared, from: "shared" },
|
|
496
|
+
{ ...local, from: "local" }
|
|
497
|
+
]);
|
|
498
|
+
const account = config?.oauthAccount;
|
|
499
|
+
return { account: account && typeof account === "object" ? account : void 0, source };
|
|
500
|
+
}
|
|
501
|
+
function describeAuthSource(source) {
|
|
502
|
+
const text = `${source.name} from ${ORIGIN_LABEL[source.from]}`;
|
|
503
|
+
return source.name === "ANTHROPIC_API_KEY" ? `${text} (once approved in Claude Code)` : text;
|
|
504
|
+
}
|
|
505
|
+
function loginOverrideWarning(source) {
|
|
506
|
+
let fix;
|
|
507
|
+
if (source.from === "shell") fix = `unset ${source.name} in your shell`;
|
|
508
|
+
else if (source.name === "ANTHROPIC_AUTH_TOKEN" && source.from === "user") fix = 'run "ccs reset"';
|
|
509
|
+
else if (source.name === "ANTHROPIC_AUTH_TOKEN" && source.from === "local") fix = 'run "ccs reset --project"';
|
|
510
|
+
else fix = `remove ${source.name} from ${ORIGIN_LABEL[source.from]}`;
|
|
511
|
+
return `Claude Code uses ${describeAuthSource(source)}, not your /login account. To use the login, ${fix}.`;
|
|
512
|
+
}
|
|
513
|
+
|
|
417
514
|
// src/commands/status.ts
|
|
418
515
|
function registerStatusCommand(program2) {
|
|
419
516
|
program2.command("status").description("Show current provider, model, and key status").action(async () => {
|
|
@@ -451,6 +548,15 @@ ${scope} settings:`);
|
|
|
451
548
|
if (!hasAny) {
|
|
452
549
|
info('No provider configured. Run "ccs <provider>" to switch.');
|
|
453
550
|
}
|
|
551
|
+
const { account, source } = await readAuthState();
|
|
552
|
+
const email = typeof account?.emailAddress === "string" ? account.emailAddress : void 0;
|
|
553
|
+
const org = typeof account?.organizationName === "string" ? account.organizationName : void 0;
|
|
554
|
+
console.log("\nClaude account:");
|
|
555
|
+
table([
|
|
556
|
+
["Login", email ? org ? `${email} (${org})` : email : "not logged in"],
|
|
557
|
+
["Credential", source ? describeAuthSource(source) : email ? "/login" : "none"]
|
|
558
|
+
]);
|
|
559
|
+
if (source && email) warn(loginOverrideWarning(source));
|
|
454
560
|
});
|
|
455
561
|
}
|
|
456
562
|
|
|
@@ -589,35 +695,152 @@ function registerDoctorCommand(program2) {
|
|
|
589
695
|
import readline3 from "readline";
|
|
590
696
|
|
|
591
697
|
// src/accounts/account-manager.ts
|
|
698
|
+
import fs5 from "fs/promises";
|
|
699
|
+
import path5 from "path";
|
|
700
|
+
|
|
701
|
+
// src/accounts/credential-store.ts
|
|
592
702
|
import fs4 from "fs/promises";
|
|
593
|
-
import
|
|
594
|
-
|
|
703
|
+
import os2 from "os";
|
|
704
|
+
import { spawnSync } from "child_process";
|
|
705
|
+
function fileStore(filePath) {
|
|
706
|
+
return {
|
|
707
|
+
where: filePath,
|
|
708
|
+
async read() {
|
|
709
|
+
try {
|
|
710
|
+
return await fs4.readFile(filePath, "utf-8");
|
|
711
|
+
} catch {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
async write(data) {
|
|
716
|
+
await writeFileAtomically(filePath, data, 384);
|
|
717
|
+
}
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
var KEYCHAIN_SERVICE = "Claude Code-credentials";
|
|
721
|
+
var KEYCHAIN_NOT_FOUND = 44;
|
|
722
|
+
var SECURITY_LINE_LIMIT = 4032;
|
|
723
|
+
function keychainAccount() {
|
|
724
|
+
let name;
|
|
725
|
+
try {
|
|
726
|
+
name = process.env.USER || os2.userInfo().username;
|
|
727
|
+
} catch {
|
|
728
|
+
return "claude-code-user";
|
|
729
|
+
}
|
|
730
|
+
return /^[a-zA-Z0-9._-]+$/.test(name) ? name : "claude-code-user";
|
|
731
|
+
}
|
|
732
|
+
function keychainStore(service = KEYCHAIN_SERVICE, account = keychainAccount()) {
|
|
733
|
+
return {
|
|
734
|
+
where: `macOS Keychain item "${service}"`,
|
|
735
|
+
async read() {
|
|
736
|
+
const res = spawnSync("security", ["find-generic-password", "-a", account, "-s", service, "-w"], {
|
|
737
|
+
encoding: "utf-8"
|
|
738
|
+
});
|
|
739
|
+
if (res.status === KEYCHAIN_NOT_FOUND) return null;
|
|
740
|
+
if (res.status !== 0) {
|
|
741
|
+
throw new Error(`Keychain read failed (security exit ${res.status}): ${res.stderr.trim()}`);
|
|
742
|
+
}
|
|
743
|
+
return res.stdout.trim();
|
|
744
|
+
},
|
|
745
|
+
async write(data) {
|
|
746
|
+
const hex = Buffer.from(data, "utf-8").toString("hex");
|
|
747
|
+
const line = `add-generic-password -U -a "${account}" -s "${service}" -X "${hex}"
|
|
748
|
+
`;
|
|
749
|
+
const res = line.length <= SECURITY_LINE_LIMIT ? spawnSync("security", ["-i"], { input: line, encoding: "utf-8" }) : spawnSync("security", ["add-generic-password", "-U", "-a", account, "-s", service, "-X", hex], {
|
|
750
|
+
encoding: "utf-8"
|
|
751
|
+
});
|
|
752
|
+
if (res.status !== 0) {
|
|
753
|
+
throw new Error(`Keychain write failed (security exit ${res.status}): ${res.stderr.trim()}`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
function defaultCredentialStore(platform = process.platform) {
|
|
759
|
+
const file = fileStore(CLAUDE_CREDENTIALS_FILE);
|
|
760
|
+
if (platform !== "darwin") return file;
|
|
761
|
+
const keychain = keychainStore();
|
|
762
|
+
return {
|
|
763
|
+
where: `${keychain.where} or ${file.where}`,
|
|
764
|
+
async read() {
|
|
765
|
+
return await keychain.read() ?? await file.read();
|
|
766
|
+
},
|
|
767
|
+
write: keychain.write
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// src/accounts/account-manager.ts
|
|
772
|
+
async function saveAccount(name, store = defaultCredentialStore(), accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
773
|
+
const raw = await store.read();
|
|
595
774
|
let credentials;
|
|
596
775
|
try {
|
|
597
|
-
|
|
598
|
-
credentials = JSON.parse(raw);
|
|
776
|
+
credentials = JSON.parse(raw ?? "");
|
|
599
777
|
} catch {
|
|
600
|
-
throw new Error(`Cannot read credentials
|
|
778
|
+
throw new Error(`Cannot read credentials from ${store.where}. Are you logged in to Claude?`);
|
|
601
779
|
}
|
|
780
|
+
const claudeConfig = await readJsonFile(claudeConfigPath);
|
|
602
781
|
await ensureDir(accountsDir);
|
|
603
|
-
const snapshot = {
|
|
604
|
-
|
|
782
|
+
const snapshot = {
|
|
783
|
+
credentials,
|
|
784
|
+
oauthAccount: claudeConfig?.oauthAccount,
|
|
785
|
+
savedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
786
|
+
};
|
|
787
|
+
await writeJsonFile(path5.join(accountsDir, `${name}.json`), snapshot, 384);
|
|
605
788
|
}
|
|
606
|
-
async function switchAccount(name,
|
|
607
|
-
const accountPath =
|
|
789
|
+
async function switchAccount(name, store = defaultCredentialStore(), accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
790
|
+
const accountPath = path5.join(accountsDir, `${name}.json`);
|
|
608
791
|
const snapshot = await readJsonFile(accountPath);
|
|
609
792
|
if (!snapshot) throw new Error(`Account "${name}" not found.`);
|
|
610
|
-
await
|
|
611
|
-
await
|
|
793
|
+
const claudeConfig = await readJsonFile(claudeConfigPath);
|
|
794
|
+
await saveBackCurrent(store, accountsDir, claudeConfig?.oauthAccount);
|
|
795
|
+
await store.write(JSON.stringify(snapshot.credentials));
|
|
796
|
+
if (snapshot.oauthAccount === void 0 || !claudeConfig) return;
|
|
797
|
+
claudeConfig.oauthAccount = snapshot.oauthAccount;
|
|
798
|
+
await writeJsonFile(claudeConfigPath, claudeConfig, 384);
|
|
799
|
+
}
|
|
800
|
+
async function saveBackCurrent(store, accountsDir, currentAccount) {
|
|
801
|
+
let credentials;
|
|
802
|
+
try {
|
|
803
|
+
credentials = JSON.parse(await store.read() ?? "");
|
|
804
|
+
} catch {
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
const id = accountId(currentAccount);
|
|
808
|
+
if (!id) return;
|
|
809
|
+
for (const { name } of await listAccounts(accountsDir)) {
|
|
810
|
+
const file = path5.join(accountsDir, `${name}.json`);
|
|
811
|
+
const saved = await readJsonFile(file);
|
|
812
|
+
if (saved && accountId(saved.oauthAccount) === id) {
|
|
813
|
+
const snapshot = {
|
|
814
|
+
credentials,
|
|
815
|
+
oauthAccount: currentAccount,
|
|
816
|
+
savedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
817
|
+
};
|
|
818
|
+
await writeJsonFile(file, snapshot, 384);
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
const email = currentAccount.emailAddress ?? id;
|
|
823
|
+
throw new Error(
|
|
824
|
+
`The current login (${email}) is not saved in any profile and would be lost. Run "ccs account save <name>" first.`
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
function accountId(oauthAccount) {
|
|
828
|
+
const a = oauthAccount;
|
|
829
|
+
return typeof a?.accountUuid === "string" ? `${a.accountUuid}/${String(a.organizationUuid)}` : void 0;
|
|
612
830
|
}
|
|
613
|
-
async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR) {
|
|
831
|
+
async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR, claudeConfigPath = CLAUDE_GLOBAL_CONFIG_FILE) {
|
|
832
|
+
const current = accountId((await readJsonFile(claudeConfigPath))?.oauthAccount);
|
|
614
833
|
try {
|
|
615
|
-
const files = await
|
|
834
|
+
const files = await fs5.readdir(accountsDir);
|
|
616
835
|
const accounts = [];
|
|
617
836
|
for (const file of files) {
|
|
618
837
|
if (!file.endsWith(".json")) continue;
|
|
619
|
-
const raw = await readJsonFile(
|
|
620
|
-
accounts.push({
|
|
838
|
+
const raw = await readJsonFile(path5.join(accountsDir, file));
|
|
839
|
+
accounts.push({
|
|
840
|
+
name: file.replace(/\.json$/, ""),
|
|
841
|
+
savedAt: raw?.savedAt ?? "",
|
|
842
|
+
active: current !== void 0 && accountId(raw?.oauthAccount) === current
|
|
843
|
+
});
|
|
621
844
|
}
|
|
622
845
|
return accounts.sort((a, b) => a.name.localeCompare(b.name));
|
|
623
846
|
} catch {
|
|
@@ -625,9 +848,9 @@ async function listAccounts(accountsDir = CCS_ACCOUNTS_DIR) {
|
|
|
625
848
|
}
|
|
626
849
|
}
|
|
627
850
|
async function deleteAccount(name, accountsDir = CCS_ACCOUNTS_DIR) {
|
|
628
|
-
const accountPath =
|
|
851
|
+
const accountPath = path5.join(accountsDir, `${name}.json`);
|
|
629
852
|
try {
|
|
630
|
-
await
|
|
853
|
+
await fs5.unlink(accountPath);
|
|
631
854
|
} catch {
|
|
632
855
|
throw new Error(`Account "${name}" not found.`);
|
|
633
856
|
}
|
|
@@ -649,6 +872,8 @@ function registerAccountCommand(program2) {
|
|
|
649
872
|
try {
|
|
650
873
|
await switchAccount(name);
|
|
651
874
|
success(`Switched to account "${name}".`);
|
|
875
|
+
const { source } = await readAuthState();
|
|
876
|
+
if (source) warn(loginOverrideWarning(source));
|
|
652
877
|
} catch (err) {
|
|
653
878
|
error(String(err));
|
|
654
879
|
process.exit(1);
|
|
@@ -660,7 +885,7 @@ function registerAccountCommand(program2) {
|
|
|
660
885
|
info('No saved accounts. Run "ccs account save <name>" to save one.');
|
|
661
886
|
return;
|
|
662
887
|
}
|
|
663
|
-
table(accounts.map((a) => [a.name, a.savedAt]));
|
|
888
|
+
table(accounts.map((a) => [(a.active ? "* " : " ") + a.name, a.savedAt]));
|
|
664
889
|
});
|
|
665
890
|
account.command("delete <name>").description("Delete a saved account profile").action(async (name) => {
|
|
666
891
|
const confirmed = await confirm2(`Delete account "${name}"?`);
|