claude-ai-switcher 1.1.4

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.
Files changed (83) hide show
  1. package/AGENTS.md +265 -0
  2. package/ARCHITECTURE.md +162 -0
  3. package/CLAUDE.md +267 -0
  4. package/LICENSE +21 -0
  5. package/QWEN.md +429 -0
  6. package/README.md +833 -0
  7. package/dist/clients/claude-code.d.ts +92 -0
  8. package/dist/clients/claude-code.d.ts.map +1 -0
  9. package/dist/clients/claude-code.js +312 -0
  10. package/dist/clients/claude-code.js.map +1 -0
  11. package/dist/clients/opencode.d.ts +71 -0
  12. package/dist/clients/opencode.d.ts.map +1 -0
  13. package/dist/clients/opencode.js +604 -0
  14. package/dist/clients/opencode.js.map +1 -0
  15. package/dist/config.d.ts +37 -0
  16. package/dist/config.d.ts.map +1 -0
  17. package/dist/config.js +122 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/display.d.ts +51 -0
  20. package/dist/display.d.ts.map +1 -0
  21. package/dist/display.js +118 -0
  22. package/dist/display.js.map +1 -0
  23. package/dist/hooks/index.d.ts +60 -0
  24. package/dist/hooks/index.d.ts.map +1 -0
  25. package/dist/hooks/index.js +223 -0
  26. package/dist/hooks/index.js.map +1 -0
  27. package/dist/hooks/token-tracker.js +280 -0
  28. package/dist/hooks/visual-enhancements.js +364 -0
  29. package/dist/index.d.ts +9 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +1091 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/models.d.ts +34 -0
  34. package/dist/models.d.ts.map +1 -0
  35. package/dist/models.js +343 -0
  36. package/dist/models.js.map +1 -0
  37. package/dist/providers/alibaba.d.ts +25 -0
  38. package/dist/providers/alibaba.d.ts.map +1 -0
  39. package/dist/providers/alibaba.js +37 -0
  40. package/dist/providers/alibaba.js.map +1 -0
  41. package/dist/providers/anthropic.d.ts +14 -0
  42. package/dist/providers/anthropic.d.ts.map +1 -0
  43. package/dist/providers/anthropic.js +19 -0
  44. package/dist/providers/anthropic.js.map +1 -0
  45. package/dist/providers/gemini.d.ts +44 -0
  46. package/dist/providers/gemini.d.ts.map +1 -0
  47. package/dist/providers/gemini.js +156 -0
  48. package/dist/providers/gemini.js.map +1 -0
  49. package/dist/providers/glm.d.ts +25 -0
  50. package/dist/providers/glm.d.ts.map +1 -0
  51. package/dist/providers/glm.js +89 -0
  52. package/dist/providers/glm.js.map +1 -0
  53. package/dist/providers/ollama.d.ts +48 -0
  54. package/dist/providers/ollama.d.ts.map +1 -0
  55. package/dist/providers/ollama.js +174 -0
  56. package/dist/providers/ollama.js.map +1 -0
  57. package/dist/providers/openrouter.d.ts +24 -0
  58. package/dist/providers/openrouter.d.ts.map +1 -0
  59. package/dist/providers/openrouter.js +36 -0
  60. package/dist/providers/openrouter.js.map +1 -0
  61. package/dist/verify.d.ts +24 -0
  62. package/dist/verify.d.ts.map +1 -0
  63. package/dist/verify.js +262 -0
  64. package/dist/verify.js.map +1 -0
  65. package/package.json +57 -0
  66. package/scripts/copy-hooks.js +15 -0
  67. package/src/clients/claude-code.ts +340 -0
  68. package/src/clients/opencode.ts +618 -0
  69. package/src/config.ts +101 -0
  70. package/src/display.ts +151 -0
  71. package/src/hooks/index.ts +208 -0
  72. package/src/hooks/token-tracker.js +280 -0
  73. package/src/hooks/visual-enhancements.js +364 -0
  74. package/src/index.ts +1263 -0
  75. package/src/models.ts +366 -0
  76. package/src/providers/alibaba.ts +43 -0
  77. package/src/providers/anthropic.ts +23 -0
  78. package/src/providers/gemini.ts +136 -0
  79. package/src/providers/glm.ts +60 -0
  80. package/src/providers/ollama.ts +146 -0
  81. package/src/providers/openrouter.ts +42 -0
  82. package/src/verify.ts +258 -0
  83. package/tsconfig.json +19 -0
package/AGENTS.md ADDED
@@ -0,0 +1,265 @@
1
+ # AGENTS.md
2
+
3
+ This file provides guidance to Codex (Codex.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Codex AI Switcher is a TypeScript CLI tool that enables seamless switching between AI providers (Anthropic, Alibaba Coding Plan, GLM/Z.AI, OpenRouter, Ollama, Gemini) for Codex and OpenCode clients. It manages configuration files, API keys, environment variables, and model alias env vars so users always know what model is active in Codex.
8
+
9
+ ## Project Structure
10
+
11
+ ```
12
+ Codex-ai-switcher/
13
+ ── src/
14
+ │ ├── index.ts # Main CLI entry point (Commander.js)
15
+ │ ├── config.ts # API key and config management
16
+ │ ├── models.ts # Provider/model definitions + ModelTierMap
17
+ │ ├── verify.ts # API key verification (lightweight HTTP checks)
18
+ │ ├── display.ts # Console output utilities (chalk)
19
+ │ ├── hooks/
20
+ │ │ ├── index.ts # Hook manager (install/remove hooks)
21
+ │ │ ├── token-tracker.js # Token tracking script
22
+ │ │ └── visual-enhancements.js # Visual enhancements script
23
+ │ ├── clients/
24
+ │ │ ├── Codex.ts # Codex config handler (~/.Codex/)
25
+ │ │ ── opencode.ts # OpenCode config handler (~/.config/opencode/opencode.json)
26
+ │ └── providers/
27
+ │ ├── anthropic.ts # Anthropic provider config
28
+ │ ├── alibaba.ts # Alibaba Coding Plan config
29
+ │ ├── glm.ts # GLM/Z.AI provider (coding-helper)
30
+ │ ├── openrouter.ts # OpenRouter provider config
31
+ │ ├── ollama.ts # Ollama provider (local, LiteLLM proxy on :4000)
32
+ │ └── gemini.ts # Gemini provider (Google, LiteLLM proxy on :4001)
33
+ ├── dist/ # Compiled JavaScript output
34
+ ├── package.json # Dependencies and scripts
35
+ ── tsconfig.json # TypeScript configuration
36
+ └── README.md # User documentation
37
+ ```
38
+
39
+ ## Build and Development Commands
40
+
41
+ ### Build Commands
42
+ ```bash
43
+ # Install dependencies (reproducible install from package-lock.json; does NOT modify it)
44
+ npm ci
45
+
46
+ # Build TypeScript to JavaScript
47
+ npm run build
48
+
49
+ # Run development version (ts-node)
50
+ npm run dev
51
+
52
+ # Run built version
53
+ npm start
54
+
55
+ # Clean build output
56
+ npm run clean
57
+ ```
58
+
59
+ ### Global Installation
60
+ ```bash
61
+ npm link # Install CLI globally as 'Codex-switch'
62
+ ```
63
+
64
+ ## Key Architecture Patterns
65
+
66
+ ### Separation of Concerns
67
+ - `clients/` handles file I/O for specific AI clients
68
+ - `providers/` contains provider-specific configuration logic
69
+ - `models.ts` centralizes model definitions, tier maps, and ModelTierMap interface
70
+ - `display.ts` handles all console output formatting
71
+
72
+ ### Model Alias Environment Variables
73
+ When switching Codex to a non-Anthropic provider, the tool writes into `~/.Codex/settings.json`:
74
+ ```json
75
+ {
76
+ "env": {
77
+ "ANTHROPIC_DEFAULT_OPUS_MODEL": "<model>",
78
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "<model>",
79
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "<model>"
80
+ }
81
+ }
82
+ ```
83
+
84
+ Default tier maps per provider:
85
+ | Provider | opus | sonnet | haiku |
86
+ |----------|------|--------|-------|
87
+ | Alibaba | qwen3.7-plus (default), selected model (when specific model chosen) | qwen3.6-plus (default), qwen3.7-plus (when specific model chosen) | kimi-k2.5 (default), qwen3.6-plus (when specific model chosen) |
88
+ | GLM | glm-5.2[1m] | glm-5-turbo | glm-5v-turbo |
89
+ | OpenRouter | qwen/qwen3.6-plus:free | openrouter/free | openrouter/free |
90
+ | Ollama | deepseek-r1:latest | qwen2.5-coder:latest | llama3.1:latest |
91
+ | Gemini | gemini-2.5-pro | gemini-2.5-flash | gemini-2.5-flash-lite |
92
+ | Anthropic | (cleared) | (cleared) | (cleared) |
93
+
94
+ ### Status Command
95
+ `Codex-switch status` — Shows current provider/model for both clients, displays masked API keys, and verifies each key by making a lightweight API call. Uses `src/verify.ts` which performs:
96
+ - Alibaba: GET to DashScope models endpoint
97
+ - OpenRouter: GET to OpenRouter models endpoint
98
+ - Anthropic: GET to Anthropic models endpoint (uses `ANTHROPIC_API_KEY` env var)
99
+ - GLM: Checks if `coding-helper` CLI is installed
100
+ - Ollama: Checks LiteLLM proxy on port 4000, then Ollama on port 11434
101
+ - Gemini: Checks LiteLLM proxy on port 4001, then validates Google API key
102
+
103
+ ### Type Definitions
104
+ ```typescript
105
+ // ModelTierMap defines which model maps to each Anthropic tier
106
+ interface ModelTierMap {
107
+ opus: string;
108
+ sonnet: string;
109
+ haiku: string;
110
+ }
111
+ ```
112
+
113
+ ## Common Tasks
114
+
115
+ ### Switch Providers
116
+ ```bash
117
+ # Switch both Codex and OpenCode
118
+ Codex-switch anthropic
119
+ Codex-switch alibaba
120
+ Codex-switch glm
121
+
122
+ # Switch Codex only
123
+ Codex-switch Codex anthropic
124
+ Codex-switch Codex alibaba
125
+ Codex-switch Codex glm
126
+ Codex-switch Codex openrouter
127
+ Codex-switch Codex ollama
128
+ Codex-switch Codex gemini
129
+
130
+ # Switch OpenCode only
131
+ Codex-switch opencode anthropic
132
+ Codex-switch opencode alibaba
133
+ Codex-switch opencode glm
134
+ Codex-switch opencode openrouter
135
+ Codex-switch opencode add ollama
136
+ Codex-switch opencode add gemini
137
+ Codex-switch opencode remove ollama
138
+ Codex-switch opencode remove gemini
139
+ ```
140
+
141
+ ### Configure Model Tiers
142
+ ```bash
143
+ # Custom model tier aliases (Codex only)
144
+ Codex-switch Codex alibaba --opus qwen3-max-2026-01-23 --sonnet qwen3-coder-plus --haiku qwen3.6-plus
145
+ Codex-switch glm --opus glm-5.2[1m] --sonnet glm-5-turbo --haiku glm-5v-turbo
146
+
147
+ # Specific configuration with qwen3.7-plus for opus, qwen3.6-plus for sonnet, kimi-k2.5 for haiku
148
+ Codex-switch Codex alibaba --opus qwen3.7-plus --sonnet qwen3.6-plus --haiku kimi-k2.5
149
+ ```
150
+
151
+ ### View Information
152
+ ```bash
153
+ Codex-switch status # Show current config + verify API keys
154
+ Codex-switch current # Show current configuration (both clients)
155
+ Codex-switch list # List all providers and models
156
+ Codex-switch models alibaba # Show models for specific provider
157
+ ```
158
+
159
+ ### API Key Management
160
+ ```bash
161
+ Codex-switch key alibaba # Check if API key is set
162
+ Codex-switch key alibaba <key> # Set API key
163
+ Codex-switch setup # Interactive setup wizard
164
+ ```
165
+
166
+ ### Hooks - Token Tracking & Visual Enhancements
167
+ ```bash
168
+ # Install all hooks (token tracker + visual enhancements)
169
+ Codex-switch hooks install
170
+
171
+ # Install individual hooks
172
+ Codex-switch hooks install-token # Token tracker only
173
+ Codex-switch hooks install-visual # Visual enhancements only
174
+
175
+ # View status and manage hooks
176
+ Codex-switch hooks status # Show current token usage and visual status
177
+ Codex-switch hooks reset # Reset token usage counters
178
+ Codex-switch hooks remove # Remove all hooks
179
+ Codex-switch hooks remove-token # Remove token tracker
180
+ Codex-switch hooks remove-visual # Remove visual enhancements
181
+ ```
182
+
183
+ **Hook Files:**
184
+ - `~/.Codex/token-tracker.js` - Tracks input/output tokens with visual context bar
185
+ - `~/.Codex/visual-enhancements.js` - Shows active model, provider, context window, capabilities
186
+ - `~/.Codex/hooks-config.json` - Hook installation status
187
+ - `~/.Codex/token-usage.json` - Session token usage data
188
+
189
+ ## Configuration Files
190
+
191
+ | Client | Config File | Purpose |
192
+ |--------|-------------|---------|
193
+ | Codex | `~/.Codex/settings.json` | Environment variables for provider config + model alias env vars |
194
+ | Codex | `~/.Codex.json` | Onboarding flag (`hasCompletedOnboarding`) |
195
+ | OpenCode | `~/.config/opencode/opencode.json` | Provider and agent configuration |
196
+ | API Keys | `~/.Codex-ai-switcher/config.json` | Secure API key storage |
197
+ | Hooks | `~/.Codex/hooks-config.json` | Hook installation status and configuration |
198
+ | Token Tracker | `~/.Codex/token-tracker.js` | Token tracking script (installed via hooks) |
199
+ | Visual Enhancements | `~/.Codex/visual-enhancements.js` | Visual enhancements script (installed via hooks) |
200
+ | Token Usage Data | `~/.Codex/token-usage.json` | Session token usage tracking |
201
+
202
+ ## Safety Features
203
+ 1. **Backup Before Modify**: All config file modifications create timestamped backups
204
+ 2. **Directory Creation**: Uses `fs.ensureDir()` to safely create directories
205
+ 3. **Onboarding Auto-Complete**: Sets `hasCompletedOnboarding: true` to prevent connection errors
206
+ 4. **Local-Only Storage**: No cloud sync of API keys or configurations
207
+ 5. **Existence Checks**: Validates config files before reading/writing
208
+ 6. **Env Var Cleanup**: Clears provider-specific env vars when switching between providers
209
+ 7. **Hook Safety**: Hooks are optional and can be removed at any time without affecting core functionality
210
+ 8. **Token Usage Privacy**: Token usage data stored locally only, never transmitted
211
+
212
+ ## External Dependencies
213
+ - Codex and/or OpenCode must be installed separately
214
+ - `@z_ai/coding-helper` package required for GLM/Z.AI provider support
215
+ - API keys required for Alibaba (from Alibaba Cloud Model Studio)
216
+ - [LiteLLM](https://github.com/BerriAI/litellm) with proxy support required for Ollama and Gemini (`pip install 'litellm[proxy]'`)
217
+ - [Ollama](https://ollama.com) must be installed and running for local model support
218
+ - Google API key (from [AI Studio](https://aistudio.google.com/apikey)) required for Gemini
219
+
220
+ ## Zread Project Wiki
221
+
222
+ The `.zread/` directory contains an AI-generated project wiki maintained with [Zread CLI](https://zread.ai/cli).
223
+
224
+ ### Structure
225
+ ```
226
+ .zread/
227
+ wiki/
228
+ current → pointer to latest version (e.g., "versions/2026-04-24-203206")
229
+ versions/
230
+ <timestamp>/ ← markdown files (the actual wiki content)
231
+ drafts/ ← in-progress generation (gitignored)
232
+ ```
233
+
234
+ ### Workflow
235
+ 1. After meaningful code changes, run `zread generate` to update the wiki
236
+ 2. Prune old version snapshots before committing: delete all but the latest dated folder under `.zread/wiki/versions/`
237
+ 3. Commit `current` (the pointer) + the single latest version folder
238
+ 4. Git history tracks what changed over time — no need for multiple dated snapshots in the repo
239
+
240
+ ### Guidelines
241
+ - Never commit `.zread/wiki/drafts/` (already in `.gitignore`)
242
+ - Keep only one versioned snapshot in the repo at a time to avoid accumulation
243
+ - The wiki is for developer reference and AI context — it does not affect build or runtime
244
+
245
+ ## Cross-Platform Development
246
+
247
+ ### Platform Support
248
+ - **macOS**: Full support (primary development platform)
249
+ - **Linux**: Full support
250
+ - **Windows 11**: Full support (use Git Bash, WSL, or PowerShell for build commands)
251
+
252
+ ### Cross-Platform Code Guidelines
253
+
254
+ **Shell Commands**:
255
+ - Avoid Unix-specific commands like `which`, `rm -rf`
256
+ - Use `platform()` from Node.js `os` module for platform detection
257
+ - Example: `platform() === "win32" ? "where coding-helper" : "which coding-helper"`
258
+
259
+ **Path Handling**:
260
+ - Always use `path.join()` and `os.homedir()` for cross-platform paths
261
+ - The tool writes to each client's native config path (`~/.Codex/`, `~/.config/opencode/opencode.json`) for cross-platform compatibility
262
+
263
+ **Build Scripts**:
264
+ - `rimraf` is used instead of `rm -rf` for cross-platform directory deletion
265
+ - All npm scripts work on Windows, macOS, and Linux
@@ -0,0 +1,162 @@
1
+ # Architecture
2
+
3
+ ## High-Level Overview
4
+
5
+ ```
6
+ claude-switch CLI
7
+ |
8
+ +-- src/index.ts (Commander.js routing)
9
+ |
10
+ +-- switch functions (switchAlibaba, switchGLM, switchOpenRouter, switchOllama, switchGemini, switchAnthropic)
11
+ |
12
+ +-- helper functions (promptApiKey, buildTierMap, displayTierMap)
13
+ |
14
+ +-------+-------------------------------+
15
+ | | |
16
+ Claude Code OpenCode Config Storage
17
+ Client Client (~/.claude-ai-switcher/)
18
+ | | |
19
+ ~/.claude/ ~/.config/opencode/ config.json
20
+ settings.json opencode.json (API keys)
21
+ ~/.claude.json
22
+ ```
23
+
24
+ ## Provider Architecture
25
+
26
+ ```
27
+ Claude Code / OpenCode
28
+ |
29
+ +-------+-------+
30
+ | ANTHROPIC_ |
31
+ | BASE_URL |
32
+ +-------+-------+
33
+ |
34
+ +-------------------+-----------------------+
35
+ | | |
36
+ Direct Providers LiteLLM Proxy Layer No Config
37
+ (Anthropic API) (OpenAI -> Anthropic) (coding-helper)
38
+ | | |
39
+ +----+----+ +----+----+ +---+---+
40
+ |Alibaba | | :4000 | | GLM |
41
+ |OpenRouter| | | | |(MCP) |
42
+ |Anthropic | | Ollama | +-------+
43
+ +---------+ | :4001 |
44
+ | | |
45
+ | Gemini |
46
+ +---------+
47
+ ```
48
+
49
+ Anthropic, Alibaba, and OpenRouter speak the Anthropic Messages API natively.
50
+ Ollama and Gemini only speak OpenAI format, so LiteLLM translates between protocols.
51
+ GLM uses the coding-helper MCP server which handles its own configuration.
52
+
53
+ ## Provider Switch Flow
54
+
55
+ When switching providers, the tool writes environment variables to `~/.claude/settings.json`:
56
+
57
+ ```
58
+ +------------------+
59
+ | switch command |
60
+ +--------+---------+
61
+ |
62
+ +--------v---------+
63
+ | Validate API key |
64
+ | (if required) |
65
+ +--------+---------+
66
+ |
67
+ +--------v---------+
68
+ | Validate model |
69
+ | (if specified) |
70
+ +--------+---------+
71
+ |
72
+ +--------v---------+
73
+ | Build tier map |
74
+ | (opus/sonnet/haiku)|
75
+ +--------+---------+
76
+ |
77
+ +--------v---------+
78
+ | Start LiteLLM | <-- Ollama/Gemini only
79
+ | proxy if needed |
80
+ +--------+---------+
81
+ |
82
+ +--------v---------+
83
+ | Write settings |
84
+ | (backup first) |
85
+ +--------+---------+
86
+ ```
87
+
88
+ ## Model Tier Alias System
89
+
90
+ Claude Code uses three env vars to alias model tiers:
91
+
92
+ ```
93
+ ANTHROPIC_DEFAULT_OPUS_MODEL -- maps to provider's opus model
94
+ ANTHROPIC_DEFAULT_SONNET_MODEL -- maps to provider's sonnet model
95
+ ANTHROPIC_DEFAULT_HAIKU_MODEL -- maps to provider's haiku model
96
+ ```
97
+
98
+ ### Default Tier Maps
99
+
100
+ ```
101
+ Provider | opus | sonnet | haiku
102
+ ------------+---------------------------+---------------------------+---------------------------
103
+ Alibaba | qwen3.7-plus / selected | qwen3.6-plus / qwen3.7-plus| kimi-k2.5 / qwen3.6-plus
104
+ GLM | glm-5.2[1m] | glm-5-turbo | glm-5v-turbo
105
+ OpenRouter | qwen/qwen3.6-plus:free | openrouter/free | openrouter/free
106
+ Ollama | deepseek-r1:latest | qwen2.5-coder:latest | llama3.1:latest
107
+ Gemini | gemini-2.5-pro | gemini-2.5-flash | gemini-2.5-flash-lite
108
+ Anthropic | (cleared) | (cleared) | (cleared)
109
+ ```
110
+
111
+ Users can override tiers at switch time with `--opus`, `--sonnet`, `--haiku` flags.
112
+
113
+ ## Provider Detection
114
+
115
+ `getCurrentProvider()` detects the active provider by inspecting `~/.claude/settings.json`:
116
+
117
+ ```
118
+ +-------------------------------------------+
119
+ | ANTHROPIC_BASE_URL contains |
120
+ | "coding-intl.dashscope.aliyuncs.com" |--> Alibaba
121
+ | "openrouter.ai" |--> OpenRouter
122
+ | "localhost:4000" |--> Ollama (via LiteLLM)
123
+ | "localhost:4001" |--> Gemini (via LiteLLM)
124
+ | "z.ai" |--> GLM (via coding-helper)
125
+ | (absent) |
126
+ | +- mcpServers["glm-coding-plan"] |--> GLM
127
+ | +- tier map set but no BASE_URL |--> GLM
128
+ | +- (none of the above) |--> Anthropic
129
+ +-------------------------------------------+
130
+ ```
131
+
132
+ ## LiteLLM Proxy Layer
133
+
134
+ Ollama and Gemini require a LiteLLM proxy to translate between Claude Code's Anthropic Messages API format and their native OpenAI Chat Completions format.
135
+
136
+ ```
137
+ Claude Code (Anthropic API)
138
+ |
139
+ v
140
+ LiteLLM Proxy (translation layer)
141
+ |
142
+ +-- Port 4000 --> Ollama (localhost:11434)
143
+ +-- Port 4001 --> Gemini API (generativelanguage.googleapis.com)
144
+ ```
145
+
146
+ The proxy is automatically started as a detached background process when switching to Ollama or Gemini. Health checks poll `/health` on the respective port for up to 5 seconds.
147
+
148
+ ## Config File Locations
149
+
150
+ | File | Purpose | Managed By |
151
+ |------|---------|------------|
152
+ | `~/.claude/settings.json` | Env vars for provider routing + model aliases | `claude-code.ts` |
153
+ | `~/.claude.json` | Onboarding flag (`hasCompletedOnboarding`) | `claude-code.ts` |
154
+ | `~/.config/opencode/opencode.json` | OpenCode provider and model config | `opencode.ts` |
155
+ | `~/.claude-ai-switcher/config.json` | API keys per provider (alibaba, openrouter, gemini) | `config.ts` |
156
+
157
+ ## Cross-Platform Support
158
+
159
+ - All paths use `path.join()` + `os.homedir()` (no hardcoded `/home/`)
160
+ - `rimraf` for clean (no `rm -rf`)
161
+ - Platform detection for `which`/`where` commands (`platform() === "win32"`)
162
+ - Works on macOS, Linux, and Windows 11
package/CLAUDE.md ADDED
@@ -0,0 +1,267 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Claude AI Switcher is a TypeScript CLI tool that enables seamless switching between AI providers (Anthropic, Alibaba Coding Plan, GLM/Z.AI, OpenRouter, Ollama, Gemini) for Claude Code and OpenCode clients. It manages configuration files, API keys, environment variables, and model alias env vars so users always know what model is active in Claude Code.
8
+
9
+ ## Project Structure
10
+
11
+ ```
12
+ claude-ai-switcher/
13
+ ── src/
14
+ │ ├── index.ts # Main CLI entry point (Commander.js)
15
+ │ ├── config.ts # API key and config management
16
+ │ ├── models.ts # Provider/model definitions + ModelTierMap
17
+ │ ├── verify.ts # API key verification (lightweight HTTP checks)
18
+ │ ├── display.ts # Console output utilities (chalk)
19
+ │ ├── hooks/
20
+ │ │ ├── index.ts # Hook manager (install/remove hooks)
21
+ │ │ ├── token-tracker.js # Token tracking script
22
+ │ │ └── visual-enhancements.js # Visual enhancements script
23
+ │ ├── clients/
24
+ │ │ ├── claude-code.ts # Claude Code config handler (~/.claude/)
25
+ │ │ ── opencode.ts # OpenCode config handler (~/.config/opencode/opencode.json)
26
+ │ └── providers/
27
+ │ ├── anthropic.ts # Anthropic provider config
28
+ │ ├── alibaba.ts # Alibaba Coding Plan config
29
+ │ ├── glm.ts # GLM/Z.AI provider (coding-helper)
30
+ │ ├── openrouter.ts # OpenRouter provider config
31
+ │ ├── ollama.ts # Ollama provider (local, LiteLLM proxy on :4000)
32
+ │ └── gemini.ts # Gemini provider (Google, LiteLLM proxy on :4001)
33
+ ├── dist/ # Compiled JavaScript output
34
+ ├── package.json # Dependencies and scripts
35
+ ── tsconfig.json # TypeScript configuration
36
+ └── README.md # User documentation
37
+ ```
38
+
39
+ ## Build and Development Commands
40
+
41
+ ### Build Commands
42
+ ```bash
43
+ # Install dependencies (reproducible install from package-lock.json; does NOT modify it)
44
+ npm ci
45
+
46
+ # Build TypeScript to JavaScript
47
+ npm run build
48
+
49
+ # Run development version (ts-node)
50
+ npm run dev
51
+
52
+ # Run built version
53
+ npm start
54
+
55
+ # Clean build output
56
+ npm run clean
57
+ ```
58
+
59
+ ### Global Installation
60
+ ```bash
61
+ npm link # Install CLI globally as 'claude-switch'
62
+ ```
63
+
64
+ ## Key Architecture Patterns
65
+
66
+ ### Separation of Concerns
67
+ - `clients/` handles file I/O for specific AI clients
68
+ - `providers/` contains provider-specific configuration logic
69
+ - `models.ts` centralizes model definitions, tier maps, and ModelTierMap interface
70
+ - `display.ts` handles all console output formatting
71
+
72
+ ### Model Alias Environment Variables
73
+ When switching Claude Code to a non-Anthropic provider, the tool writes into `~/.claude/settings.json`:
74
+ ```json
75
+ {
76
+ "env": {
77
+ "ANTHROPIC_DEFAULT_OPUS_MODEL": "<model>",
78
+ "ANTHROPIC_DEFAULT_SONNET_MODEL": "<model>",
79
+ "ANTHROPIC_DEFAULT_HAIKU_MODEL": "<model>"
80
+ }
81
+ }
82
+ ```
83
+
84
+ Default tier maps per provider:
85
+ | Provider | opus | sonnet | haiku |
86
+ |----------|------|--------|-------|
87
+ | Alibaba | qwen3.7-plus (default), selected model (when specific model chosen) | qwen3.6-plus (default), qwen3.7-plus (when specific model chosen) | kimi-k2.5 (default), qwen3.6-plus (when specific model chosen) |
88
+ | GLM | glm-5.2[1m] | glm-5-turbo | glm-5v-turbo |
89
+ | OpenRouter | qwen/qwen3.6-plus:free | openrouter/free | openrouter/free |
90
+ | Ollama | deepseek-r1:latest | qwen2.5-coder:latest | llama3.1:latest |
91
+ | Gemini | gemini-2.5-pro | gemini-2.5-flash | gemini-2.5-flash-lite |
92
+ | Anthropic | (cleared) | (cleared) | (cleared) |
93
+
94
+ ### Status Command
95
+ `claude-switch status` — Shows current provider/model for both clients, displays masked API keys, and verifies each key by making a lightweight API call. Uses `src/verify.ts` which performs:
96
+ - Alibaba: GET to DashScope models endpoint
97
+ - OpenRouter: GET to OpenRouter models endpoint
98
+ - Anthropic: GET to Anthropic models endpoint (uses `ANTHROPIC_API_KEY` env var)
99
+ - GLM: Checks if `coding-helper` CLI is installed
100
+ - Ollama: Checks LiteLLM proxy on port 4000, then Ollama on port 11434
101
+ - Gemini: Checks LiteLLM proxy on port 4001, then validates Google API key
102
+
103
+ ### Type Definitions
104
+ ```typescript
105
+ // ModelTierMap defines which model maps to each Anthropic tier
106
+ interface ModelTierMap {
107
+ opus: string;
108
+ sonnet: string;
109
+ haiku: string;
110
+ }
111
+ ```
112
+
113
+ ## Common Tasks
114
+
115
+ ### Switch Providers
116
+ ```bash
117
+ # Switch both Claude Code and OpenCode
118
+ claude-switch anthropic
119
+ claude-switch alibaba
120
+ claude-switch glm
121
+
122
+ # Switch Claude Code only
123
+ claude-switch claude anthropic
124
+ claude-switch claude alibaba
125
+ claude-switch claude glm
126
+ claude-switch claude openrouter
127
+ claude-switch claude ollama
128
+ claude-switch claude gemini
129
+
130
+ # Switch OpenCode only
131
+ claude-switch opencode anthropic
132
+ claude-switch opencode alibaba
133
+ claude-switch opencode glm
134
+ claude-switch opencode openrouter
135
+ claude-switch opencode add ollama
136
+ claude-switch opencode add gemini
137
+ claude-switch opencode add glm
138
+ claude-switch opencode remove ollama
139
+ claude-switch opencode remove gemini
140
+ claude-switch opencode remove glm
141
+ ```
142
+
143
+ ### Configure Model Tiers
144
+ ```bash
145
+ # Custom model tier aliases (Claude Code only)
146
+ claude-switch claude alibaba --opus qwen3-max-2026-01-23 --sonnet qwen3-coder-plus --haiku qwen3.6-plus
147
+ claude-switch glm --opus glm-5.2[1m] --sonnet glm-5-turbo --haiku glm-5v-turbo
148
+
149
+ # Specific configuration with qwen3.7-plus for opus, qwen3.6-plus for sonnet, kimi-k2.5 for haiku
150
+ claude-switch claude alibaba --opus qwen3.7-plus --sonnet qwen3.6-plus --haiku kimi-k2.5
151
+ ```
152
+
153
+ ### View Information
154
+ ```bash
155
+ claude-switch status # Show current config + verify API keys
156
+ claude-switch current # Show current configuration (both clients)
157
+ claude-switch list # List all providers and models
158
+ claude-switch models alibaba # Show models for specific provider
159
+ ```
160
+
161
+ ### API Key Management
162
+ ```bash
163
+ claude-switch key alibaba # Check if API key is set
164
+ claude-switch key alibaba <key> # Set API key
165
+ claude-switch setup # Interactive setup wizard
166
+ ```
167
+
168
+ ### Hooks - Token Tracking & Visual Enhancements
169
+ ```bash
170
+ # Install all hooks (token tracker + visual enhancements)
171
+ claude-switch hooks install
172
+
173
+ # Install individual hooks
174
+ claude-switch hooks install-token # Token tracker only
175
+ claude-switch hooks install-visual # Visual enhancements only
176
+
177
+ # View status and manage hooks
178
+ claude-switch hooks status # Show current token usage and visual status
179
+ claude-switch hooks reset # Reset token usage counters
180
+ claude-switch hooks remove # Remove all hooks
181
+ claude-switch hooks remove-token # Remove token tracker
182
+ claude-switch hooks remove-visual # Remove visual enhancements
183
+ ```
184
+
185
+ **Hook Files:**
186
+ - `~/.claude/token-tracker.js` - Tracks input/output tokens with visual context bar
187
+ - `~/.claude/visual-enhancements.js` - Shows active model, provider, context window, capabilities
188
+ - `~/.claude/hooks-config.json` - Hook installation status
189
+ - `~/.claude/token-usage.json` - Session token usage data
190
+
191
+ ## Configuration Files
192
+
193
+ | Client | Config File | Purpose |
194
+ |--------|-------------|---------|
195
+ | Claude Code | `~/.claude/settings.json` | Environment variables for provider config + model alias env vars |
196
+ | Claude Code | `~/.claude.json` | Onboarding flag (`hasCompletedOnboarding`) |
197
+ | OpenCode | `~/.config/opencode/opencode.json` | Provider and agent configuration |
198
+ | API Keys | `~/.claude-ai-switcher/config.json` | Secure API key storage |
199
+ | Hooks | `~/.claude/hooks-config.json` | Hook installation status and configuration |
200
+ | Token Tracker | `~/.claude/token-tracker.js` | Token tracking script (installed via hooks) |
201
+ | Visual Enhancements | `~/.claude/visual-enhancements.js` | Visual enhancements script (installed via hooks) |
202
+ | Token Usage Data | `~/.claude/token-usage.json` | Session token usage tracking |
203
+
204
+ ## Safety Features
205
+ 1. **Backup Before Modify**: All config file modifications create timestamped backups
206
+ 2. **Directory Creation**: Uses `fs.ensureDir()` to safely create directories
207
+ 3. **Onboarding Auto-Complete**: Sets `hasCompletedOnboarding: true` to prevent connection errors
208
+ 4. **Local-Only Storage**: No cloud sync of API keys or configurations
209
+ 5. **Existence Checks**: Validates config files before reading/writing
210
+ 6. **Env Var Cleanup**: Clears provider-specific env vars when switching between providers
211
+ 7. **Hook Safety**: Hooks are optional and can be removed at any time without affecting core functionality
212
+ 8. **Token Usage Privacy**: Token usage data stored locally only, never transmitted
213
+
214
+ ## External Dependencies
215
+ - Claude Code and/or OpenCode must be installed separately
216
+ - `@z_ai/coding-helper` package required for GLM/Z.AI provider support
217
+ - API keys required for Alibaba (from Alibaba Cloud Model Studio)
218
+ - [LiteLLM](https://github.com/BerriAI/litellm) with proxy support required for Ollama and Gemini (`pip install 'litellm[proxy]'`)
219
+ - [Ollama](https://ollama.com) must be installed and running for local model support
220
+ - Google API key (from [AI Studio](https://aistudio.google.com/apikey)) required for Gemini
221
+
222
+ ## Zread Project Wiki
223
+
224
+ The `.zread/` directory contains an AI-generated project wiki maintained with [Zread CLI](https://zread.ai/cli).
225
+
226
+ ### Structure
227
+ ```
228
+ .zread/
229
+ wiki/
230
+ current → pointer to latest version (e.g., "versions/2026-04-24-203206")
231
+ versions/
232
+ <timestamp>/ ← markdown files (the actual wiki content)
233
+ drafts/ ← in-progress generation (gitignored)
234
+ ```
235
+
236
+ ### Workflow
237
+ 1. After meaningful code changes, run `zread generate` to update the wiki
238
+ 2. Prune old version snapshots before committing: delete all but the latest dated folder under `.zread/wiki/versions/`
239
+ 3. Commit `current` (the pointer) + the single latest version folder
240
+ 4. Git history tracks what changed over time — no need for multiple dated snapshots in the repo
241
+
242
+ ### Guidelines
243
+ - Never commit `.zread/wiki/drafts/` (already in `.gitignore`)
244
+ - Keep only one versioned snapshot in the repo at a time to avoid accumulation
245
+ - The wiki is for developer reference and AI context — it does not affect build or runtime
246
+
247
+ ## Cross-Platform Development
248
+
249
+ ### Platform Support
250
+ - **macOS**: Full support (primary development platform)
251
+ - **Linux**: Full support
252
+ - **Windows 11**: Full support (use Git Bash, WSL, or PowerShell for build commands)
253
+
254
+ ### Cross-Platform Code Guidelines
255
+
256
+ **Shell Commands**:
257
+ - Avoid Unix-specific commands like `which`, `rm -rf`
258
+ - Use `platform()` from Node.js `os` module for platform detection
259
+ - Example: `platform() === "win32" ? "where coding-helper" : "which coding-helper"`
260
+
261
+ **Path Handling**:
262
+ - Always use `path.join()` and `os.homedir()` for cross-platform paths
263
+ - The tool writes to each client's native config path (`~/.claude/`, `~/.config/opencode/opencode.json`) for cross-platform compatibility
264
+
265
+ **Build Scripts**:
266
+ - `rimraf` is used instead of `rm -rf` for cross-platform directory deletion
267
+ - All npm scripts work on Windows, macOS, and Linux