forked from musistudio/claude-code-router
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Implement error handling, config validation, and enhanced logging #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3b8429f
feat: implement error handling, config validation, and enhanced logging
e2bea7d
fix: address CodeRabbit review comments
d2bd764
fix: resolve merge conflicts from stash
0f7da3a
fix: replace p-retry with custom implementation and add ajv-formats
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
node_modules | ||
npm-debug.log | ||
node_modules | ||
npm-debug.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Publish to npm | ||
|
||
on: | ||
push: | ||
tags: | ||
- 'v*' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '18' | ||
registry-url: 'https://registry.npmjs.org' | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Build | ||
run: npm run build | ||
|
||
- name: Publish to npm | ||
run: npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
node_modules | ||
.env | ||
log.txt | ||
.idea | ||
node_modules | ||
.env | ||
log.txt | ||
.idea | ||
dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
src | ||
node_modules | ||
.claude | ||
CLAUDE.md | ||
screenshoots | ||
.DS_Store | ||
.vscode | ||
.idea | ||
.env | ||
.blog | ||
docs | ||
.log | ||
blog | ||
config.json | ||
src | ||
node_modules | ||
.claude | ||
CLAUDE.md | ||
screenshoots | ||
.DS_Store | ||
.vscode | ||
.idea | ||
.env | ||
.blog | ||
docs | ||
.log | ||
blog | ||
config.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Registry configuration | ||
registry=https://registry.npmjs.org/ | ||
|
||
# Authentication token (will be set by npm login or CI/CD) | ||
# //registry.npmjs.org/:_authToken=${NPM_TOKEN} | ||
|
||
# Package access level | ||
access=public | ||
|
||
# Ignore optional dependencies during install | ||
omit=optional | ||
|
||
# Save exact versions | ||
save-exact=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
# CLAUDE.md | ||
|
||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
||
## Commands | ||
|
||
- **Build the project**: | ||
```bash | ||
npm run build | ||
``` | ||
- **Start the router server**: | ||
```bash | ||
ccr start | ||
``` | ||
- **Stop the router server**: | ||
```bash | ||
ccr stop | ||
``` | ||
- **Check the server status**: | ||
```bash | ||
ccr status | ||
``` | ||
- **Run Claude Code through the router**: | ||
```bash | ||
ccr code "<your prompt>" | ||
``` | ||
- **Release a new version**: | ||
```bash | ||
npm run release | ||
``` | ||
|
||
## Architecture | ||
|
||
This project is a TypeScript-based router for Claude Code requests. It allows routing requests to different large language models (LLMs) from various providers based on custom rules. | ||
|
||
- **Entry Point**: The main command-line interface logic is in `src/cli.ts`. It handles parsing commands like `start`, `stop`, and `code`. | ||
- **Server**: The `ccr start` command launches a server that listens for requests from Claude Code. The server logic is initiated from `src/index.ts`. | ||
- **Configuration**: The router is configured via a JSON file located at `~/.claude-code-router/config.json`. This file defines API providers, routing rules, and custom transformers. An example can be found in `config.example.json`. | ||
- **Routing**: The core routing logic determines which LLM provider and model to use for a given request. It supports default routes for different scenarios (`default`, `background`, `think`, `longContext`, `webSearch`) and can be extended with a custom JavaScript router file. The router logic is likely in `src/utils/router.ts`. | ||
- **Providers and Transformers**: The application supports multiple LLM providers. Transformers adapt the request and response formats for different provider APIs. | ||
- **Claude Code Integration**: When a user runs `ccr code`, the command is forwarded to the running router service. The service then processes the request, applies routing rules, and sends it to the configured LLM. If the service isn't running, `ccr code` will attempt to start it automatically. | ||
- **Dependencies**: The project is built with `esbuild`. It has a key local dependency `@musistudio/llms`, which probably contains the core logic for interacting with different LLM APIs. | ||
# CLAUDE.md | ||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
## Commands | ||
- **Build the project**: | ||
```bash | ||
npm run build | ||
``` | ||
- **Start the router server**: | ||
```bash | ||
ccr start | ||
``` | ||
- **Stop the router server**: | ||
```bash | ||
ccr stop | ||
``` | ||
- **Check the server status**: | ||
```bash | ||
ccr status | ||
``` | ||
- **Run Claude Code through the router**: | ||
```bash | ||
ccr code "<your prompt>" | ||
``` | ||
- **Release a new version**: | ||
```bash | ||
npm run release | ||
``` | ||
## Architecture | ||
This project is a TypeScript-based router for Claude Code requests. It allows routing requests to different large language models (LLMs) from various providers based on custom rules. | ||
- **Entry Point**: The main command-line interface logic is in `src/cli.ts`. It handles parsing commands like `start`, `stop`, and `code`. | ||
- **Server**: The `ccr start` command launches a server that listens for requests from Claude Code. The server logic is initiated from `src/index.ts`. | ||
- **Configuration**: The router is configured via a JSON file located at `~/.claude-code-router/config.json`. This file defines API providers, routing rules, and custom transformers. An example can be found in `config.example.json`. | ||
- **Routing**: The core routing logic determines which LLM provider and model to use for a given request. It supports default routes for different scenarios (`default`, `background`, `think`, `longContext`, `webSearch`) and can be extended with a custom JavaScript router file. The router logic is likely in `src/utils/router.ts`. | ||
- **Providers and Transformers**: The application supports multiple LLM providers. Transformers adapt the request and response formats for different provider APIs. | ||
- **Claude Code Integration**: When a user runs `ccr code`, the command is forwarded to the running router service. The service then processes the request, applies routing rules, and sends it to the configured LLM. If the service isn't running, `ccr code` will attempt to start it automatically. | ||
- **Dependencies**: The project is built with `esbuild`. It has a key local dependency `@musistudio/llms`, which probably contains the core logic for interacting with different LLM APIs. | ||
- `@musistudio/llms` is implemented based on `fastify` and exposes `fastify`'s hook and middleware interfaces, allowing direct use of `server.addHook`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2025 musistudio | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
MIT License | ||
Copyright (c) 2025 musistudio | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# ccr-next | ||
|
||
An enhanced fork of [claude-code-router](https://github.com/musistudio/claude-code-router) with additional features for dynamic provider configuration. | ||
|
||
## Features | ||
|
||
- Route Claude Code requests to different LLM providers | ||
- Configure providers and transformers via command line | ||
- Support for multiple LLM providers (OpenRouter, DeepSeek, Ollama, Gemini, etc.) | ||
- Custom routing rules based on request context | ||
- Background processing for non-critical tasks | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install -g ccr-next | ||
``` | ||
|
||
## Quick Start | ||
|
||
1. Start the router service: | ||
```bash | ||
ccr start | ||
``` | ||
|
||
2. Configure providers on the fly: | ||
```bash | ||
ccr start --provider openrouter https://openrouter.ai/api/v1/chat/completions sk-xxx claude-3.5-sonnet,gpt-4 | ||
ccr start --transformer openrouter openrouter | ||
``` | ||
|
||
3. Use Claude Code with the router: | ||
```bash | ||
ccr code "Write a Hello World program" | ||
``` | ||
|
||
## Command Line Options | ||
|
||
### Start Server with Provider Configuration | ||
|
||
```bash | ||
ccr start --provider <name> <url> <key> <models> --transformer <provider> <transformer> | ||
``` | ||
|
||
Options: | ||
- `--provider`: Add or update a provider | ||
- `name`: Provider name (e.g., openrouter, deepseek) | ||
- `url`: API base URL | ||
- `key`: API key | ||
- `models`: Comma-separated list of model names | ||
- `--transformer`: Set transformer for a provider | ||
- `provider`: Provider name | ||
- `transformer`: Transformer name | ||
|
||
Examples: | ||
```bash | ||
# Add OpenRouter provider | ||
ccr start --provider openrouter https://openrouter.ai/api/v1/chat/completions sk-xxx claude-3.5-sonnet | ||
|
||
# Add DeepSeek provider with transformer | ||
ccr start --provider deepseek https://api.deepseek.com/chat/completions sk-xxx deepseek-chat --transformer deepseek deepseek | ||
|
||
# Add multiple providers | ||
ccr start \ | ||
--provider openrouter https://openrouter.ai/api/v1/chat/completions sk-xxx claude-3.5-sonnet \ | ||
--provider deepseek https://api.deepseek.com/chat/completions sk-xxx deepseek-chat | ||
``` | ||
|
||
### Other Commands | ||
|
||
- `ccr stop`: Stop the router service | ||
- `ccr restart`: Restart the service (supports same options as start) | ||
- `ccr status`: Show service status | ||
- `ccr code "<prompt>"`: Execute Claude command through the router | ||
- `ccr -v`: Show version | ||
- `ccr -h`: Show help | ||
|
||
## Configuration | ||
|
||
The router uses a configuration file at `~/.claude-code-router/config.json`. You can either: | ||
1. Edit this file directly | ||
2. Use command line options to configure providers dynamically | ||
|
||
See [config.example.json](https://github.com/yourusername/ccr-direct/blob/main/config.example.json) for configuration examples. | ||
|
||
## License | ||
|
||
MIT |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace placeholder URL with actual repository
The configuration example link contains a placeholder URL that should be updated to match the actual repository.
📝 Committable suggestion
🤖 Prompt for AI Agents