@musistudio/claude-code-router 1.0.23 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -226,6 +226,45 @@ You can also switch models dynamically in Claude Code with the `/model` command:
226
226
  `/model provider_name,model_name`
227
227
  Example: `/model openrouter,anthropic/claude-3.5-sonnet`
228
228
 
229
+ #### Custom Router
230
+
231
+ For more advanced routing logic, you can specify a custom router script via the `CUSTOM_ROUTER_PATH` in your `config.json`. This allows you to implement complex routing rules beyond the default scenarios.
232
+
233
+ In your `config.json`:
234
+
235
+ ```json
236
+ {
237
+ "CUSTOM_ROUTER_PATH": "$HOME/.claude-code-router/custom-router.js"
238
+ }
239
+ ```
240
+
241
+ The custom router file must be a JavaScript module that exports an `async` function. This function receives the request object and the config object as arguments and should return the provider and model name as a string (e.g., `"provider_name,model_name"`), or `null` to fall back to the default router.
242
+
243
+ Here is an example of a `custom-router.js` based on `custom-router.example.js`:
244
+
245
+ ```javascript
246
+ // $HOME/.claude-code-router/custom-router.js
247
+
248
+ /**
249
+ * A custom router function to determine which model to use based on the request.
250
+ *
251
+ * @param {object} req - The request object from Claude Code, containing the request body.
252
+ * @param {object} config - The application's config object.
253
+ * @returns {Promise<string|null>} - A promise that resolves to the "provider,model_name" string, or null to use the default router.
254
+ */
255
+ module.exports = async function router(req, config) {
256
+ const userMessage = req.body.messages.find(m => m.role === 'user')?.content;
257
+
258
+ if (userMessage && userMessage.includes('explain this code')) {
259
+ // Use a powerful model for code explanation
260
+ return 'openrouter,anthropic/claude-3.5-sonnet';
261
+ }
262
+
263
+ // Fallback to the default router configuration
264
+ return null;
265
+ };
266
+ ```
267
+
229
268
 
230
269
  ## 🤖 GitHub Actions
231
270
 
package/README_zh.md CHANGED
@@ -222,6 +222,45 @@ Transformers 允许您修改请求和响应负载,以确保与不同提供商
222
222
  `/model provider_name,model_name`
223
223
  示例: `/model openrouter,anthropic/claude-3.5-sonnet`
224
224
 
225
+ #### 自定义路由器
226
+
227
+ 对于更高级的路由逻辑,您可以在 `config.json` 中通过 `CUSTOM_ROUTER_PATH` 字段指定一个自定义路由器脚本。这允许您实现超出默认场景的复杂路由规则。
228
+
229
+ 在您的 `config.json` 中配置:
230
+
231
+ ```json
232
+ {
233
+ "CUSTOM_ROUTER_PATH": "$HOME/.claude-code-router/custom-router.js"
234
+ }
235
+ ```
236
+
237
+ 自定义路由器文件必须是一个导出 `async` 函数的 JavaScript 模块。该函数接收请求对象和配置对象作为参数,并应返回提供商和模型名称的字符串(例如 `"provider_name,model_name"`),如果返回 `null` 则回退到默认路由。
238
+
239
+ 这是一个基于 `custom-router.example.js` 的 `custom-router.js` 示例:
240
+
241
+ ```javascript
242
+ // $HOME/.claude-code-router/custom-router.js
243
+
244
+ /**
245
+ * 一个自定义路由函数,用于根据请求确定使用哪个模型。
246
+ *
247
+ * @param {object} req - 来自 Claude Code 的请求对象,包含请求体。
248
+ * @param {object} config - 应用程序的配置对象。
249
+ * @returns {Promise<string|null>} - 一个解析为 "provider,model_name" 字符串的 Promise,如果返回 null,则使用默认路由。
250
+ */
251
+ module.exports = async function router(req, config) {
252
+ const userMessage = req.body.messages.find(m => m.role === 'user')?.content;
253
+
254
+ if (userMessage && userMessage.includes('解释这段代码')) {
255
+ // 为代码解释任务使用更强大的模型
256
+ return 'openrouter,anthropic/claude-3.5-sonnet';
257
+ }
258
+
259
+ // 回退到默认的路由配置
260
+ return null;
261
+ };
262
+ ```
263
+
225
264
 
226
265
  ## 🤖 GitHub Actions
227
266
 
@@ -0,0 +1,3 @@
1
+ module.exports = async function router(req, config) {
2
+ return "deepseek,deepseek-chat";
3
+ };