@dedenlabs/claude-code-router-cli 2.0.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.md +651 -0
- package/dist/cli.js +80524 -0
- package/dist/lib/pino.js +234 -0
- package/dist/lib/worker.js +194 -0
- package/dist/reproduce_issue.js +2027 -0
- package/dist/standalone.js +81387 -0
- package/dist/tiktoken_bg.wasm +0 -0
- package/dist/transformers/glm-thinking.js +130 -0
- package/dist/transformers/index.js +167 -0
- package/examples/README-External-Rules.md +166 -0
- package/examples/config-with-external-rules.json +82 -0
- package/examples/configs/fixed-router-config.json +246 -0
- package/examples/configs/unified-router-example.json +171 -0
- package/examples/external-rules/complex-routing.js +116 -0
- package/examples/external-rules/time-based.js +51 -0
- package/examples/external-rules/user-preference.js +70 -0
- package/examples/external-rules-example.js +71 -0
- package/package.json +64 -0
- package/tsconfig.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 musistudio
|
|
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.md
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
# Claude Code Router CLI v2.0
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+
[](https://github.com/dedenlabs/claude-code-router-cli)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
[](https://nodejs.org/)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
|
|
10
|
+
**基于 [@musistudio/claude-code-router](https://github.com/musistudio/claude-code-router) 的增强版命令行路由工具**
|
|
11
|
+
|
|
12
|
+
[✨ 新功能概览](#-新功能) • [🚀 快速开始](#-快速开始) • [📖 完整文档](./docs/UNIFIED_ROUTER_GUIDE.md) • [🔧 示例配置](./examples/) • [📚 官方教程](https://code.claude.com/docs/en/quickstart)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
## 🎯 核心亮点
|
|
17
|
+
|
|
18
|
+
### ⚡ 统一路由引擎 (Unified Router Engine)
|
|
19
|
+
全新的基于规则的路由系统,支持复杂条件和优先级管理
|
|
20
|
+
|
|
21
|
+
### 🔌 外部规则支持 (External Rules)
|
|
22
|
+
动态加载自定义JavaScript规则,实现个性化路由策略
|
|
23
|
+
|
|
24
|
+
### 📊 智能日志系统 (Smart Logging)
|
|
25
|
+
可视化的路由决策过程,支持多级别调试输出
|
|
26
|
+
|
|
27
|
+
**info 级别日志示例**
|
|
28
|
+
```
|
|
29
|
+
🚀 ──────────────────────────────────────
|
|
30
|
+
📝 用户请求开始 [2025/12/07 00:35:46] 🎯 目标模型: opus
|
|
31
|
+
🔗 请求ID: b8fdsc4f-ba72-4e2c-967d-2be41577f820
|
|
32
|
+
✨ 规则触发: 提供商映射规则
|
|
33
|
+
📍 路由决策: opus → opus/glm-4.6
|
|
34
|
+
📝 请求文本: <system-reminder>...
|
|
35
|
+
As you answer the user's questions, you can use the following context:...
|
|
36
|
+
请帮我总结并commit内容
|
|
37
|
+
📊 Token使用量: 51,385 (长上下文模式)
|
|
38
|
+
🚀 ──────────────────────────────────────
|
|
39
|
+
💡 GLM思考中... 启用logical类型思维模式
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**debug 级别日志示例**
|
|
43
|
+
```
|
|
44
|
+
🚀 ──────────────────────────────────────
|
|
45
|
+
📝 用户请求开始 [2025/12/07 00:05:58] 🎯 目标模型: haiku
|
|
46
|
+
🔗 请求ID: f8fddc2d-bc3a-41e9-9ea3-3dd1338d895d
|
|
47
|
+
🔍 [DEBUG] 开始评估路由 {"requestedModel":"haiku","tokenCount":1232464}
|
|
48
|
+
🔍 [DEBUG] 🔍 开始评估路由规则 {"请求模型":"haiku","Token数量":1232464,"启用规则数":7,"总规则数":7}
|
|
49
|
+
🔍 [DEBUG] 评估条件 {"conditionType":"custom","condition":{"type":"custom","customFunction":"modelContainsComma"},"requestedModel":"haiku"}
|
|
50
|
+
🔍 [DEBUG] 规则 "用户指定规则" (优先级: 200)
|
|
51
|
+
🔍 [DEBUG] 条件: 自定义函数: modelContainsComma
|
|
52
|
+
🔍 [DEBUG] 结果: ❌ 不匹配
|
|
53
|
+
🔍 [DEBUG] 评估条件 {"conditionType":"custom","condition":{"type":"custom","customFunction":"directModelMapping"},"requestedModel":"haiku"}
|
|
54
|
+
🔍 [DEBUG] 规则 "提供商映射规则" (优先级: 190)
|
|
55
|
+
🔍 [DEBUG] 条件: 自定义函数: directModelMapping
|
|
56
|
+
🔍 [DEBUG] 结果: ✅ 匹配
|
|
57
|
+
🔍 [DEBUG] 🎯 规则 "提供商映射规则" 匹配成功,停止后续评估
|
|
58
|
+
🔍 [DEBUG] 尝试直接模型映射 {"modelName":"haiku","providersCount":3}
|
|
59
|
+
🔍 [DEBUG] 未找到直接模型映射,尝试通过 provider 名称匹配 {"modelName":"haiku"}
|
|
60
|
+
🔍 [DEBUG] 🔄 变量替换完成 {"原始路由":"${mappedModel}","最终路由":"haiku,glm-4.5-air"}
|
|
61
|
+
✨ 规则触发: 提供商映射规则
|
|
62
|
+
📍 路由决策: haiku → haiku/glm-4.5-air
|
|
63
|
+
📝 请求文本:
|
|
64
|
+
Please write a 5-10 word title for the following conversation:...
|
|
65
|
+
...
|
|
66
|
+
Respond with the title for the conversation and nothing else.
|
|
67
|
+
📊 Token使用量: 1,232,464 (长上下文模式)
|
|
68
|
+
🔍 [DEBUG] 路由决策详情 {"finalRoute":"haiku,glm-4.5-air","provider":"haiku","model":"glm-4.5-air","decisionTime":"3ms","fromCache":"否"}
|
|
69
|
+
🚀 ──────────────────────────────────────
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 🔄 配置自动迁移 (Auto Migration)
|
|
73
|
+
一键从旧版本配置迁移到新的统一路由格式
|
|
74
|
+
|
|
75
|
+
### 🎛️ GLM思考模式 (GLM Thinking)
|
|
76
|
+
内置GLM模型思考转换器,提升推理质量
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 🚀 快速开始
|
|
81
|
+
|
|
82
|
+
### 1. 安装依赖
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# 安装 Claude Code
|
|
86
|
+
npm install -g @anthropic-ai/claude-code
|
|
87
|
+
|
|
88
|
+
# 安装 Router CLI
|
|
89
|
+
npm install @dedenlabs/claude-code-router-cli
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 2. 配置环境
|
|
93
|
+
|
|
94
|
+
**启动路由服务:**
|
|
95
|
+
```bash
|
|
96
|
+
ccr start
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**配置 Claude 环境变量:**
|
|
100
|
+
```bash
|
|
101
|
+
# 编辑 Claude 配置文件
|
|
102
|
+
# Windows: C:\Users\%USERNAME%\.claude\settings.json
|
|
103
|
+
# macOS/Linux: ~/.claude/settings.json
|
|
104
|
+
|
|
105
|
+
{
|
|
106
|
+
"env": {
|
|
107
|
+
"ANTHROPIC_BASE_URL": "http://localhost:3456",
|
|
108
|
+
"ANTHROPIC_AUTH_TOKEN": "sk-anything",
|
|
109
|
+
"API_TIMEOUT_MS": "3000000",
|
|
110
|
+
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
|
|
111
|
+
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "haiku-glm",
|
|
112
|
+
"ANTHROPIC_DEFAULT_SONNET_MODEL": "sonnet-minimax",
|
|
113
|
+
"ANTHROPIC_DEFAULT_OPUS_MODEL": "opus-minimax"
|
|
114
|
+
},
|
|
115
|
+
"model": "sonnet"
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 3. 开始使用
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# 方式1: 直接使用 Claude (推荐)
|
|
123
|
+
claude
|
|
124
|
+
|
|
125
|
+
# 方式2: 通过路由启动
|
|
126
|
+
ccr code "你的提示词"
|
|
127
|
+
|
|
128
|
+
# 方式3: 通过IDE启动
|
|
129
|
+
|
|
130
|
+
#### VS Code 用户
|
|
131
|
+
- 安装 [Claude Code 官方扩展](https://marketplace.visualstudio.com/items?itemName=anthropic.claude-code)
|
|
132
|
+
- 支持版本:VS Code 1.98.0 或更高版本
|
|
133
|
+
- 扩展提供原生 IDE 体验,包括专用 Claude Code 侧边栏面板、计划模式、自动接受编辑等功能
|
|
134
|
+
|
|
135
|
+
#### ZED 编辑器用户
|
|
136
|
+
- ZED 编辑器内置了 Claude Code 作为外部代理支持(ACP - Agent Configuration Protocol)
|
|
137
|
+
- 无需安装额外插件,直接在设置中配置 Claude Code
|
|
138
|
+
- 支持为 Claude Code 设置默认模型和配置选项
|
|
139
|
+
|
|
140
|
+
# 管理服务
|
|
141
|
+
ccr status # 查看状态
|
|
142
|
+
ccr stop # 停止服务
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
> 💡 **提示**: 建议使用方式1,这样可以在VS Code、ZED等编辑器中直接使用路由功能
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## ✨ 新功能概览
|
|
150
|
+
|
|
151
|
+
### 🔄 统一路由引擎
|
|
152
|
+
|
|
153
|
+
新版本将原有的多种路由模式(默认、后台、思考、长上下文)统一为基于规则的灵活系统:
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
{
|
|
157
|
+
"Router": {
|
|
158
|
+
"engine": "unified",
|
|
159
|
+
"defaultRoute": "sonnet-minimax",
|
|
160
|
+
"rules": [
|
|
161
|
+
{
|
|
162
|
+
"name": "长上下文规则",
|
|
163
|
+
"priority": 100,
|
|
164
|
+
"condition": {
|
|
165
|
+
"type": "tokenThreshold",
|
|
166
|
+
"value": 180000,
|
|
167
|
+
"operator": "gt"
|
|
168
|
+
},
|
|
169
|
+
"action": {
|
|
170
|
+
"route": "sonnet",
|
|
171
|
+
"description": "长上下文路由:基于token阈值选择模型"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
]
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 🔌 外部规则支持
|
|
180
|
+
|
|
181
|
+
通过外部JavaScript文件定义复杂路由逻辑:
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// external-rules/user-preference.js
|
|
185
|
+
const userPreferences = {
|
|
186
|
+
'premium@company.com': { provider: 'opus-minimax', model: 'glm-4.6' },
|
|
187
|
+
'standard@company.com': { provider: 'sonnet-minimax', model: 'glm-4.5-air' }
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
function checkUserPreference(context) {
|
|
191
|
+
const userEmail = extractUserEmail(context);
|
|
192
|
+
return userPreferences[userEmail] !== undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
module.exports = { checkUserPreference };
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 📊 智能日志输出
|
|
199
|
+
|
|
200
|
+
启用调试模式查看详细的路由决策过程:
|
|
201
|
+
|
|
202
|
+
```json
|
|
203
|
+
{
|
|
204
|
+
"debug": {
|
|
205
|
+
"enabled": true,
|
|
206
|
+
"logLevel": "debug"
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 📋 配置示例
|
|
212
|
+
|
|
213
|
+
### 快速配置模板
|
|
214
|
+
|
|
215
|
+
如果你想快速开始,直接运行 `ccr start` 命令之后会自动创建类似如下的配置文件,你也可以直接手动创建或编辑 `~/.claude-code-router/config.json`:
|
|
216
|
+
需要手动替换`YOUR_MINIMAX_API_KEY`或`YOUR_GLM_API_KEY`,或则手动新增其它平台和模型.
|
|
217
|
+
```json
|
|
218
|
+
{
|
|
219
|
+
"LOG": true,
|
|
220
|
+
"LOG_LEVEL": "info",
|
|
221
|
+
"HOST": "127.0.0.1",
|
|
222
|
+
"PORT": 3456,
|
|
223
|
+
"APIKEY": "sk-anything",
|
|
224
|
+
"API_TIMEOUT_MS": 600000,
|
|
225
|
+
|
|
226
|
+
"transformers": [
|
|
227
|
+
{
|
|
228
|
+
"path": "./transformers/glm-thinking.js",
|
|
229
|
+
"options": {
|
|
230
|
+
"enabled": "false",
|
|
231
|
+
"debug": "true"
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
],
|
|
235
|
+
|
|
236
|
+
"Providers": [
|
|
237
|
+
{
|
|
238
|
+
"name": "sonnet-minimax",
|
|
239
|
+
"api_base_url": "https://api.minimaxi.com/anthropic/v1/messages",
|
|
240
|
+
"api_key": "YOUR_MINIMAX_API_KEY",
|
|
241
|
+
"models": ["MiniMax-M2"],
|
|
242
|
+
"transformer": { "use": ["Anthropic"] }
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
"name": "opus-glm",
|
|
246
|
+
"api_base_url": "https://api.z.ai/api/coding/paas/v4/chat/completions",
|
|
247
|
+
"api_key": "YOUR_GLM_API_KEY",
|
|
248
|
+
"models": ["glm-4.6"],
|
|
249
|
+
"transformer": { "use": ["reasoning", "glm-thinking"] }
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"name": "haiku-glm",
|
|
253
|
+
"api_base_url": "https://open.bigmodel.cn/api/anthropic/v1/messages",
|
|
254
|
+
"api_key": "YOUR_GLM_API_KEY",
|
|
255
|
+
"models": ["glm-4.5-air"],
|
|
256
|
+
"transformer": { "use": ["Anthropic"] }
|
|
257
|
+
}
|
|
258
|
+
],
|
|
259
|
+
|
|
260
|
+
"Router": {
|
|
261
|
+
"engine": "unified",
|
|
262
|
+
"defaultRoute": "sonnet-minimax",
|
|
263
|
+
"rules": [
|
|
264
|
+
{
|
|
265
|
+
"name": "用户指定规则",
|
|
266
|
+
"priority": 200,
|
|
267
|
+
"enabled": true,
|
|
268
|
+
"condition": {
|
|
269
|
+
"type": "custom",
|
|
270
|
+
"customFunction": "modelContainsComma"
|
|
271
|
+
},
|
|
272
|
+
"action": {
|
|
273
|
+
"route": "${userModel}",
|
|
274
|
+
"description": "用户直接指定 provider,model 格式"
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
"name": "提供商映射规则",
|
|
279
|
+
"priority": 190,
|
|
280
|
+
"enabled": true,
|
|
281
|
+
"condition": {
|
|
282
|
+
"type": "custom",
|
|
283
|
+
"customFunction": "directModelMapping"
|
|
284
|
+
},
|
|
285
|
+
"action": {
|
|
286
|
+
"route": "${mappedModel}",
|
|
287
|
+
"description": "将模型名映射到对应provider"
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
"name": "长上下文规则",
|
|
292
|
+
"priority": 100,
|
|
293
|
+
"enabled": true,
|
|
294
|
+
"condition": {
|
|
295
|
+
"type": "tokenThreshold",
|
|
296
|
+
"value": 180000,
|
|
297
|
+
"operator": "gt"
|
|
298
|
+
},
|
|
299
|
+
"action": {
|
|
300
|
+
"route": "sonnet-minimax",
|
|
301
|
+
"description": "基于token阈值选择模型"
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
"name": "后台规则",
|
|
306
|
+
"priority": 80,
|
|
307
|
+
"enabled": true,
|
|
308
|
+
"condition": {
|
|
309
|
+
"type": "modelContains",
|
|
310
|
+
"value": "haiku",
|
|
311
|
+
"operator": "contains"
|
|
312
|
+
},
|
|
313
|
+
"action": {
|
|
314
|
+
"route": "haiku-glm",
|
|
315
|
+
"description": "Haiku模型自动使用轻量级模型"
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
],
|
|
319
|
+
"cache": {
|
|
320
|
+
"enabled": true,
|
|
321
|
+
"ttl": 300000,
|
|
322
|
+
"maxSize": 1000
|
|
323
|
+
},
|
|
324
|
+
"debug": {
|
|
325
|
+
"enabled": true,
|
|
326
|
+
"logLevel": "info"
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### 🛠️ 高级配置
|
|
333
|
+
|
|
334
|
+
#### 外部规则配置示例
|
|
335
|
+
|
|
336
|
+
参考 [`examples/config-with-external-rules.json`](./examples/config-with-external-rules.json):
|
|
337
|
+
|
|
338
|
+
```json
|
|
339
|
+
{
|
|
340
|
+
"Router": {
|
|
341
|
+
"engine": "unified",
|
|
342
|
+
"rules": [
|
|
343
|
+
{
|
|
344
|
+
"name": "用户偏好路由",
|
|
345
|
+
"priority": 100,
|
|
346
|
+
"enabled": true,
|
|
347
|
+
"condition": {
|
|
348
|
+
"type": "externalFunction",
|
|
349
|
+
"externalFunction": {
|
|
350
|
+
"path": "./external-rules/user-preference.js",
|
|
351
|
+
"functionName": "checkUserPreference"
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
"action": {
|
|
355
|
+
"route": "gpt-4,openai"
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
"name": "时间路由",
|
|
360
|
+
"priority": 90,
|
|
361
|
+
"enabled": true,
|
|
362
|
+
"condition": {
|
|
363
|
+
"type": "externalFunction",
|
|
364
|
+
"externalFunction": {
|
|
365
|
+
"path": "./external-rules/time-based.js",
|
|
366
|
+
"functionName": "isBusinessHours"
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
"action": {
|
|
370
|
+
"route": "claude-3-opus,anthropic"
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
]
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
#### 固定路由配置示例
|
|
379
|
+
|
|
380
|
+
参考 [`examples/configs/fixed-router-config.json`](./examples/configs/fixed-router-config.json):
|
|
381
|
+
|
|
382
|
+
```json
|
|
383
|
+
{
|
|
384
|
+
"Router": {
|
|
385
|
+
"engine": "unified",
|
|
386
|
+
"defaultRoute": "openrouter,anthropic/claude-3.5-sonnet",
|
|
387
|
+
"rules": [
|
|
388
|
+
{
|
|
389
|
+
"name": "代码任务路由",
|
|
390
|
+
"priority": 80,
|
|
391
|
+
"enabled": true,
|
|
392
|
+
"condition": {
|
|
393
|
+
"type": "fieldExists",
|
|
394
|
+
"field": "messages.0.content",
|
|
395
|
+
"value": "代码",
|
|
396
|
+
"operator": "contains"
|
|
397
|
+
},
|
|
398
|
+
"action": {
|
|
399
|
+
"route": "openrouter,anthropic/claude-3.5-sonnet"
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
]
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
## 🔄 配置迁移
|
|
408
|
+
|
|
409
|
+
### 自动迁移 (推荐)
|
|
410
|
+
|
|
411
|
+
如果你有旧版本的配置,可以使用自动迁移工具:
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
# 自动迁移旧配置到统一路由引擎
|
|
415
|
+
ccr migrate
|
|
416
|
+
|
|
417
|
+
# 迁移前备份现有配置
|
|
418
|
+
ccr migrate --backup
|
|
419
|
+
|
|
420
|
+
# 查看迁移报告
|
|
421
|
+
ccr migrate --report
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
迁移工具会:
|
|
425
|
+
1. 备份现有配置到 `~/.claude-code-router/config.backup.json`
|
|
426
|
+
2. 自动转换为统一路由格式
|
|
427
|
+
3. 生成详细的迁移报告
|
|
428
|
+
4. 保留所有原有的路由逻辑
|
|
429
|
+
|
|
430
|
+
### 手动迁移
|
|
431
|
+
|
|
432
|
+
如果你想手动迁移或了解迁移过程,参考 [`docs/UNIFIED_ROUTER_GUIDE.md`](./docs/UNIFIED_ROUTER_GUIDE.md#配置迁移) 中的详细说明。
|
|
433
|
+
|
|
434
|
+
## 🔍 故障排查
|
|
435
|
+
|
|
436
|
+
### 常见问题
|
|
437
|
+
|
|
438
|
+
**Q: 服务启动失败,提示端口被占用**
|
|
439
|
+
```bash
|
|
440
|
+
# 检查端口占用
|
|
441
|
+
netstat -tulpn | grep 3456
|
|
442
|
+
# 或
|
|
443
|
+
lsof -i :3456
|
|
444
|
+
|
|
445
|
+
# 更换端口
|
|
446
|
+
ccr start --port 3457
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
**Q: 路由不生效,模型没有切换**
|
|
450
|
+
```bash
|
|
451
|
+
# 检查配置语法
|
|
452
|
+
ccr validate
|
|
453
|
+
|
|
454
|
+
# 查看详细日志
|
|
455
|
+
ccr logs --level debug
|
|
456
|
+
|
|
457
|
+
# 重启服务
|
|
458
|
+
ccr restart
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
**Q: 外部规则加载失败**
|
|
462
|
+
```bash
|
|
463
|
+
# 检查外部规则文件路径
|
|
464
|
+
ccr check-external-rules
|
|
465
|
+
|
|
466
|
+
# 验证JavaScript语法
|
|
467
|
+
node -c path/to/your/rule.js
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### 日志位置
|
|
471
|
+
|
|
472
|
+
- **服务日志**: `~/.claude-code-router/logs/ccr-*.log`
|
|
473
|
+
- **应用日志**: `~/.claude-code-router/claude-code-router.log`
|
|
474
|
+
- **错误日志**: `~/.claude-code-router/logs/error.log`
|
|
475
|
+
|
|
476
|
+
### 调试技巧
|
|
477
|
+
|
|
478
|
+
1. **启用详细日志**:
|
|
479
|
+
```json
|
|
480
|
+
{
|
|
481
|
+
"debug": {
|
|
482
|
+
"enabled": true,
|
|
483
|
+
"logLevel": "debug",
|
|
484
|
+
"logToConsole": true,
|
|
485
|
+
"logToFile": true
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
2. **测试路由规则**:
|
|
491
|
+
```bash
|
|
492
|
+
# 测试当前配置
|
|
493
|
+
ccr test-route --model haiku --token-count 50000
|
|
494
|
+
|
|
495
|
+
# 模拟请求
|
|
496
|
+
ccr simulate --input "你的测试提示词"
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
## 🛠️ 开发指南
|
|
500
|
+
|
|
501
|
+
### 环境准备
|
|
502
|
+
|
|
503
|
+
```bash
|
|
504
|
+
# 克隆项目
|
|
505
|
+
git clone https://github.com/dedenlabs/claude-code-router-cli.git
|
|
506
|
+
cd claude-code-router-cli
|
|
507
|
+
|
|
508
|
+
# 安装依赖
|
|
509
|
+
npm install
|
|
510
|
+
|
|
511
|
+
# 构建项目
|
|
512
|
+
npm run build
|
|
513
|
+
|
|
514
|
+
# 运行测试
|
|
515
|
+
npm test
|
|
516
|
+
|
|
517
|
+
# 开发模式
|
|
518
|
+
npm run dev
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
### 创建自定义转换器
|
|
522
|
+
|
|
523
|
+
```typescript
|
|
524
|
+
// src/transformers/my-transformer.ts
|
|
525
|
+
import { BaseTransformer } from '../types/transformer';
|
|
526
|
+
|
|
527
|
+
export class MyTransformer extends BaseTransformer {
|
|
528
|
+
name = 'my-transformer';
|
|
529
|
+
|
|
530
|
+
async transformRequest(request: any): Promise<any> {
|
|
531
|
+
// 自定义请求转换逻辑
|
|
532
|
+
return request;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
async transformResponse(response: any): Promise<any> {
|
|
536
|
+
// 自定义响应转换逻辑
|
|
537
|
+
return response;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
### 创建外部规则
|
|
543
|
+
|
|
544
|
+
参考 [`examples/external-rules/`](./examples/external-rules/) 目录下的示例。
|
|
545
|
+
|
|
546
|
+
## 📊 性能优化
|
|
547
|
+
|
|
548
|
+
### 1. 缓存配置
|
|
549
|
+
|
|
550
|
+
```json
|
|
551
|
+
{
|
|
552
|
+
"cache": {
|
|
553
|
+
"enabled": true,
|
|
554
|
+
"maxSize": 1000,
|
|
555
|
+
"ttl": 300000
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
### 2. 规则优化
|
|
561
|
+
|
|
562
|
+
- 将最常用的规则放在前面(高优先级)
|
|
563
|
+
- 避免复杂的条件判断
|
|
564
|
+
- 合理设置缓存TTL
|
|
565
|
+
|
|
566
|
+
### 3. 监控建议
|
|
567
|
+
|
|
568
|
+
```bash
|
|
569
|
+
# 查看性能统计
|
|
570
|
+
ccr stats
|
|
571
|
+
|
|
572
|
+
# 监控内存使用
|
|
573
|
+
ccr monitor --memory
|
|
574
|
+
|
|
575
|
+
# 检查规则命中情况
|
|
576
|
+
ccr stats --rules
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
## 📈 版本历史
|
|
580
|
+
|
|
581
|
+
### v2.0.0 (2025-12-14)
|
|
582
|
+
|
|
583
|
+
**🚀 核心新特性**
|
|
584
|
+
|
|
585
|
+
**✨ 统一路由引擎**
|
|
586
|
+
- 基于规则的灵活路由系统,替代原有的固定模式
|
|
587
|
+
- 支持复杂条件判断和优先级管理
|
|
588
|
+
- 可自定义路由逻辑,满足个性化需求
|
|
589
|
+
|
|
590
|
+
**🔌 外部规则支持**
|
|
591
|
+
- 动态加载外部JavaScript文件定义路由规则
|
|
592
|
+
- 支持时间路由、用户偏好路由等复杂场景
|
|
593
|
+
- 无需重启服务即可更新路由逻辑
|
|
594
|
+
|
|
595
|
+
**📊 智能日志系统**
|
|
596
|
+
- 可视化的路由决策过程展示
|
|
597
|
+
- 支持多级别调试输出(info/debug/verbose)
|
|
598
|
+
- 实时显示Token使用量和性能统计
|
|
599
|
+
|
|
600
|
+
**🔄 配置自动迁移**
|
|
601
|
+
- 一键从旧版本配置迁移到新格式
|
|
602
|
+
- 自动备份原有配置,确保数据安全
|
|
603
|
+
- 智能保留所有原有路由逻辑
|
|
604
|
+
|
|
605
|
+
**🎛️ GLM思考模式**
|
|
606
|
+
- 内置GLM模型专用思考转换器
|
|
607
|
+
- 提升推理质量和逻辑思维能力
|
|
608
|
+
- 优化长文本处理性能
|
|
609
|
+
|
|
610
|
+
**🧪 完整测试覆盖**
|
|
611
|
+
- 新增单元测试和集成测试
|
|
612
|
+
- 确保路由决策的准确性和稳定性
|
|
613
|
+
- 提供模拟测试和性能监控工具
|
|
614
|
+
|
|
615
|
+
### v1.x (旧版本)
|
|
616
|
+
- 基础路由功能
|
|
617
|
+
- 固定规则配置
|
|
618
|
+
|
|
619
|
+
## 🤝 与原版的区别
|
|
620
|
+
|
|
621
|
+
| 功能特性 | 原版 (@musistudio) | 增强版 (@dedenlabs) |
|
|
622
|
+
|---------|-------------------|-------------------|
|
|
623
|
+
| **路由引擎** | 固定模式 | ✅ 统一路由引擎 |
|
|
624
|
+
| **规则配置** | 静态配置 | ✅ 动态外部规则 |
|
|
625
|
+
| **日志系统** | 基础输出 | ✅ 可视化智能日志 |
|
|
626
|
+
| **配置迁移** | 手动 | ✅ 自动迁移工具 |
|
|
627
|
+
| **思考模式** | 无 | ✅ GLM思考转换器 |
|
|
628
|
+
| **包名** | @musistudio/... | @dedenlabs/... |
|
|
629
|
+
| **文档** | 基础 | ✅ 完整示例和指南 |
|
|
630
|
+
|
|
631
|
+
## 📄 许可证
|
|
632
|
+
|
|
633
|
+
MIT License - 与原版保持一致
|
|
634
|
+
|
|
635
|
+
## 🙏 致谢
|
|
636
|
+
|
|
637
|
+
- **musistudio** - 原版项目作者
|
|
638
|
+
- **社区贡献者** - 测试和反馈
|
|
639
|
+
- **GLM团队** - 提供优质模型支持
|
|
640
|
+
|
|
641
|
+
---
|
|
642
|
+
|
|
643
|
+
<div align="center">
|
|
644
|
+
|
|
645
|
+
**[⬆ 回到顶部](#claude-code-router-cli-v20)**
|
|
646
|
+
|
|
647
|
+
[🐛 报告问题](https://github.com/dedenlabs/claude-code-router-cli/issues) •
|
|
648
|
+
[💡 功能建议](https://github.com/dedenlabs/claude-code-router-cli/issues) •
|
|
649
|
+
[📖 完整文档](./docs/UNIFIED_ROUTER_GUIDE.md)
|
|
650
|
+
|
|
651
|
+
</div>
|