change doc

This commit is contained in:
musistudio
2025-12-28 13:43:25 +08:00
parent aa18a354bb
commit bd55450b1d
33 changed files with 4807 additions and 592 deletions

View File

@@ -0,0 +1,208 @@
# CLI 基础配置
CLI 使用与 Server 相同的配置文件:`~/.claude-code-router/config.json`
## 配置文件位置
```bash
~/.claude-code-router/config.json
```
## 快速配置
使用交互式命令配置:
```bash
ccr model
```
这将引导你完成:
1. 选择 LLM 提供商
2. 配置 API Key
3. 选择模型
4. 设置路由规则
## 手动配置
### 编辑配置文件
```bash
# 打开配置文件
nano ~/.claude-code-router/config.json
```
### 最小配置示例
```json5
{
// API 密钥(可选,用于保护服务)
"APIKEY": "your-api-key-here",
// LLM 提供商
"Providers": [
{
"name": "openai",
"baseUrl": "https://api.openai.com/v1",
"apiKey": "$OPENAI_API_KEY",
"models": ["gpt-4", "gpt-3.5-turbo"]
}
],
// 默认路由
"Router": {
"default": "openai,gpt-4"
}
}
```
## 环境变量
配置支持环境变量插值:
```json5
{
"Providers": [
{
"apiKey": "$OPENAI_API_KEY" // 从环境变量读取
}
]
}
```
`.bashrc``.zshrc` 中设置:
```bash
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
```
## 常用配置项
### HOST 和 PORT
```json5
{
"HOST": "127.0.0.1", // 监听地址
"PORT": 3456 // 监听端口
}
```
### 日志配置
```json5
{
"LOG": true, // 启用日志
"LOG_LEVEL": "info" // 日志级别
}
```
### 路由配置
```json5
{
"Router": {
"default": "openai,gpt-4",
"background": "openai,gpt-3.5-turbo",
"think": "openai,gpt-4",
"longContext": "anthropic,claude-3-opus"
}
}
```
## 配置验证
配置文件会自动验证。常见错误:
- **缺少 Providers**:必须至少配置一个提供商
- **API Key 缺失**:如果配置了 Providers必须提供 API Key
- **模型不存在**:确保模型在提供商的 models 列表中
## 配置备份
每次更新配置时会自动备份:
```
~/.claude-code-router/config.backup.{timestamp}.json
```
## 重新加载配置
修改配置后需要重启服务:
```bash
ccr restart
```
## 查看当前配置
```bash
# 通过 API 查看
curl http://localhost:3456/api/config
# 或查看配置文件
cat ~/.claude-code-router/config.json
```
## 示例配置
### OpenAI
```json5
{
"Providers": [
{
"name": "openai",
"baseUrl": "https://api.openai.com/v1",
"apiKey": "$OPENAI_API_KEY",
"models": ["gpt-4", "gpt-3.5-turbo"]
}
],
"Router": {
"default": "openai,gpt-4"
}
}
```
### Anthropic
```json5
{
"Providers": [
{
"name": "anthropic",
"baseUrl": "https://api.anthropic.com/v1",
"apiKey": "$ANTHROPIC_API_KEY",
"models": ["claude-3-5-sonnet-20241022", "claude-3-opus-20240229"]
}
],
"Router": {
"default": "anthropic,claude-3-5-sonnet-20241022"
}
}
```
### 多提供商
```json5
{
"Providers": [
{
"name": "openai",
"baseUrl": "https://api.openai.com/v1",
"apiKey": "$OPENAI_API_KEY",
"models": ["gpt-4", "gpt-3.5-turbo"]
},
{
"name": "anthropic",
"baseUrl": "https://api.anthropic.com/v1",
"apiKey": "$ANTHROPIC_API_KEY",
"models": ["claude-3-5-sonnet-20241022", "claude-3-opus-20240229"]
}
],
"Router": {
"default": "openai,gpt-4",
"think": "anthropic,claude-3-5-sonnet-20241022",
"background": "openai,gpt-3.5-turbo"
}
}
```

View File

@@ -0,0 +1,213 @@
# 项目级配置
除了全局配置,`ccr` 还支持为特定项目设置不同的路由规则。
## 项目配置文件
项目配置文件位于:
```
~/.claude/projects/<project-id>/claude-code-router.json
```
其中 `<project-id>` 是 Claude Code 项目的唯一标识符。
## 项目配置结构
```json5
{
"Router": {
"default": "openai,gpt-4",
"background": "openai,gpt-3.5-turbo"
}
}
```
## 查找项目 ID
### 方法一:使用 CLI
```bash
# 在项目目录中运行
ccr status
```
输出会显示当前项目 ID
```
Project: my-project (abc123def456)
```
### 方法二:查看 Claude Code 配置
```bash
cat ~/.claude.json
```
找到你的项目 ID
```json
{
"projects": {
"abc123def456": {
"path": "/path/to/your/project",
"name": "my-project"
}
}
}
```
## 创建项目配置
### 手动创建
```bash
# 创建项目配置目录
mkdir -p ~/.claude/projects/abc123def456
# 创建配置文件
cat > ~/.claude/projects/abc123def456/claude-code-router.json << 'EOF'
{
"Router": {
"default": "anthropic,claude-3-5-sonnet-20241022",
"background": "openai,gpt-3.5-turbo"
}
}
EOF
```
### 使用 ccr model 命令
```bash
# 在项目目录中运行
cd /path/to/your/project
ccr model --project
```
## 配置优先级
路由配置的优先级(从高到低):
1. **自定义路由函数** (`CUSTOM_ROUTER_PATH`)
2. **项目级配置** (`~/.claude/projects/<id>/claude-code-router.json`)
3. **全局配置** (`~/.claude-code-router/config.json`)
4. **内置路由规则**
## 使用场景
### 场景一:不同项目使用不同模型
```json5
// Web 项目使用 GPT-4
~/.claude/projects/web-project-id/claude-code-router.json:
{
"Router": {
"default": "openai,gpt-4"
}
}
// AI 项目使用 Claude
~/.claude/projects/ai-project-id/claude-code-router.json:
{
"Router": {
"default": "anthropic,claude-3-5-sonnet-20241022"
}
}
```
### 场景二:测试项目使用低成本模型
```json5
~/.claude/projects/test-project-id/claude-code-router.json:
{
"Router": {
"default": "openai,gpt-3.5-turbo",
"background": "openai,gpt-3.5-turbo"
}
}
```
### 场景三:长上下文项目
```json5
~/.claude/projects/long-context-project-id/claude-code-router.json:
{
"Router": {
"default": "anthropic,claude-3-opus-20240229",
"longContext": "anthropic,claude-3-opus-20240229"
}
}
```
## 验证项目配置
```bash
# 查看当前项目使用的路由
ccr status
# 查看日志确认路由决策
tail -f ~/.claude-code-router/claude-code-router.log
```
## 删除项目配置
```bash
rm ~/.claude/projects/<project-id>/claude-code-router.json
```
删除后会回退到全局配置。
## 完整示例
假设你有两个项目:
### 全局配置(`~/.claude-code-router/config.json`
```json5
{
"Providers": [
{
"name": "openai",
"baseUrl": "https://api.openai.com/v1",
"apiKey": "$OPENAI_API_KEY",
"models": ["gpt-4", "gpt-3.5-turbo"]
},
{
"name": "anthropic",
"baseUrl": "https://api.anthropic.com/v1",
"apiKey": "$ANTHROPIC_API_KEY",
"models": ["claude-3-5-sonnet-20241022"]
}
],
"Router": {
"default": "openai,gpt-4",
"background": "openai,gpt-3.5-turbo"
}
}
```
### Web 项目配置
```json5
{
"Router": {
"default": "openai,gpt-4"
}
}
```
### AI 项目配置
```json5
{
"Router": {
"default": "anthropic,claude-3-5-sonnet-20241022",
"think": "anthropic,claude-3-5-sonnet-20241022"
}
}
```
这样:
- Web 项目使用 GPT-4
- AI 项目使用 Claude
- 所有项目的后台任务使用 GPT-3.5-turbo继承全局配置

View File

@@ -0,0 +1,83 @@
# CLI 简介
Claude Code Router CLI (`ccr`) 是一个命令行工具,用于管理和控制 Claude Code Router 服务。
## 功能概述
`ccr` 提供以下功能:
- **服务管理**:启动、停止、重启服务
- **配置管理**:交互式配置模型选择
- **状态查看**:查看服务运行状态
- **代码执行**:直接执行 `claude` 命令
- **环境集成**:输出环境变量用于 shell 集成
- **Web UI**:打开 Web 管理界面
- **状态栏**:集成到编辑器状态栏
## 安装
```bash
npm install -g @musistudio/claude-code-router-cli
```
或使用项目别名:
```bash
npm install -g claude-code-router
```
## 基本使用
### 启动服务
```bash
ccr start
```
### 查看状态
```bash
ccr status
```
### 停止服务
```bash
ccr stop
```
### 查看模型
```bash
ccr model
```
## 与 Claude Code 集成
`ccr` 可以与 Claude Code 无缝集成,将请求路由到你选择的 LLM 提供商。
### 方式一:设置 API 地址
```bash
export ANTHROPIC_BASE_URL="http://localhost:3456/v1"
export ANTHROPIC_API_KEY="your-api-key"
```
### 方式二:使用 activate 命令
```bash
eval "$(ccr activate)"
```
## 配置文件
`ccr` 使用与 Server 相同的配置文件:`~/.claude-code-router/config.json`
配置一次CLI 和 Server 都会使用。
## 下一步
- [安装指南](/docs/cli/installation) - 详细安装说明
- [快速开始](/docs/cli/quick-start) - 5 分钟上手
- [命令参考](/docs/category/cli-commands) - 完整命令列表
- [配置说明](/docs/category/cli-config) - 配置文件详解