Commit Graph

63 Commits

Author SHA1 Message Date
Gemini Wen
87bd54d9ea fix: 修复统一客户端标识的布尔值判断
将 useUnifiedClientId 的判断从直接布尔值比较改为字符串 'true' 比较,修复配置值为字符串时的判断问题。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-30 13:01:54 +08:00
shaw
4ab91f233f refactor: 使用 claudeCodeValidator 统一验证逻辑
替换 _hasClaudeCodeSystemPrompt 方法,改用 claudeCodeValidator

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-30 11:25:35 +08:00
shaw
8b8e9703a1 fix: claude遇到5xx错误不再停止调度 2025-09-24 14:37:33 +08:00
shaw
00faa21e4b fix: 修复claude账号限流不会自动恢复的bug 2025-09-24 09:36:42 +08:00
shaw
24d1f0a494 fix: 修复统一user-agent问题 2025-09-23 17:35:10 +08:00
shaw
5a2199f9a9 fix: claude提示词检测逻辑修复 2025-09-23 16:29:17 +08:00
shaw
0eb95b3b06 refactor: 清理模型限制检查的冗余代码
优化内容:
- 删除 claudeRelayService.js 中的重复模型限制检查(82行代码)
- 保留 api.js 中的统一检查,覆盖所有服务类型(claude/console/ccr)
- 移除 /v1/messages/count_tokens 端点的模型限制(计数接口不需要限制)

架构改进:
- 模型限制逻辑现在集中在 api.js 的 handleMessagesRequest 函数中
- 避免了每个服务各自实现一遍的重复代码
- 提高了代码的可维护性和一致性

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 09:43:15 +08:00
shaw
f667a95d88 fix: 修复模型限制功能逻辑错误(从白名单改回黑名单)
问题原因:
- 在提交 7f9869ae 添加CCR支持时,错误地将模型限制从黑名单改成了白名单
- 前端UI显示"设置此API Key无法访问的模型",明确表示这是黑名单
- 后端却将其当作白名单处理,导致逻辑完全相反

修复内容:
- 将判断逻辑从 !includes 改回 includes(黑名单逻辑)
- 更新注释和日志消息,明确这是"限制列表"而非"允许列表"
- 同时修复了 api.js 和 claudeRelayService.js 中的所有相关判断

影响范围:
- src/routes/api.js: 修复 /v1/messages 和 /v1/messages/count_tokens 端点的模型限制判断
- src/services/claudeRelayService.js: 修复流式和非流式请求的模型限制判断

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 09:35:04 +08:00
Wesley Liddick
fe1f05fadd Merge pull request #411 from bottotl/main
兼容 sider 自定义 API
2025-09-10 22:36:54 +08:00
root
4fd4dbfa51 fix: 回退401错误处理逻辑到原始版本
- 恢复"遇到1次401就停止调度"的原始逻辑
- 移除"记录401错误但不停用账号"的临时修改
- 修复非流式和流式请求中的401处理逻辑
- 确保401错误会立即标记账号为异常状态

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 08:20:17 +00:00
root
bdae9d6ceb feat: 添加Chrome插件兜底支持,解决第三方插件401错误问题
• 新增browserFallback中间件,自动识别并处理Chrome插件请求
• 增强CORS支持,明确允许chrome-extension://来源
• 优化请求头过滤,移除可能触发Claude CORS检查的浏览器头信息
• 完善401错误处理逻辑,避免因临时token问题导致账号被错误停用

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-10 07:48:41 +00:00
sususu98
7f9869ae20 feat: 添加 CCR (Claude Code Router) 账户类型支持
实现通过供应商前缀语法进行 CCR 后端路由的完整支持。
用户现在可以在 Claude Code 中使用 `/model ccr,model_name` 将请求路由到 CCR 后端。
暂时没有实现`/v1/messages/count_tokens`,因为这需要在CCR后端支持。
CCR类型的账户也暂时没有考虑模型的支持情况

## 核心实现

### 供应商前缀路由

- 添加 modelHelper 工具用于解析模型名称中的 `ccr,` 供应商前缀
- 检测到前缀时自动路由到 CCR 账户池
- 转发到 CCR 后端前移除供应商前缀

### 账户管理

- 创建 ccrAccountService 实现 CCR 账户的完整 CRUD 操作
- 支持账户属性:名称、API URL、API Key、代理、优先级、配额
- 实现账户状态:active、rate_limited、unauthorized、overloaded
- 支持模型映射和支持模型配置

### 请求转发

- 实现 ccrRelayService 处理 CCR 后端通信
- 支持流式和非流式请求
- 从 SSE 流中解析和捕获使用数据
- 支持 Bearer 和 x-api-key 两种认证格式

### 统一调度

- 将 CCR 账户集成到 unifiedClaudeScheduler
- 添加 \_selectCcrAccount 方法用于 CCR 特定账户选择
- 支持 CCR 账户的会话粘性
- 防止跨类型会话映射(CCR 会话仅用于 CCR 请求)

### 错误处理

- 实现全面的错误状态管理
- 处理 401(未授权)、429(速率限制)、529(过载)错误
- 成功请求后自动从错误状态恢复
- 支持可配置的速率限制持续时间

### Web 管理界面

- 添加 CcrAccountForm 组件用于创建/编辑 CCR 账户
- 将 CCR 账户集成到 AccountsView 中,提供完整管理功能
- 支持账户切换、重置和使用统计
- 在界面中显示账户状态和错误信息

### API 端点

- POST /admin/ccr-accounts - 创建 CCR 账户
- GET /admin/ccr-accounts - 列出所有 CCR 账户
- PUT /admin/ccr-accounts/:id - 更新 CCR 账户
- DELETE /admin/ccr-accounts/:id - 删除 CCR 账户
- PUT /admin/ccr-accounts/:id/toggle - 切换账户启用状态
- PUT /admin/ccr-accounts/:id/toggle-schedulable - 切换可调度状态
- POST /admin/ccr-accounts/:id/reset-usage - 重置每日使用量
- POST /admin/ccr-accounts/:id/reset-status - 重置错误状态

## 技术细节

- CCR 账户使用 'ccr' 作为 accountType 标识符
- 带有 `ccr,` 前缀的请求绕过普通账户池
- 转发到 CCR 后端前清理模型名称内的`ccr,`
- 从流式和非流式响应中捕获使用数据
- 支持缓存令牌跟踪(创建和读取)
2025-09-10 14:21:48 +08:00
Wesley Liddick
8064bc24b9 Merge pull request #382 from Edric-Li/feat/add-account-info-to-error-logs
feat: 在错误日志中添加账号信息
2025-09-09 12:26:45 +08:00
Edric Li
f375f9f841 fix: 修复 ESLint 错误 - 解决未定义变量问题
- claudeConsoleRelayService.js: 将 account 变量声明提到更高作用域
- claudeRelayService.js: 移除 _makeClaudeStreamRequest 函数中的未定义变量引用
2025-09-09 11:10:27 +08:00
Edric Li
52820a7e49 style: 修复 Prettier 格式问题
- 格式化 src/app.js
- 格式化 src/services/claudeConsoleRelayService.js
- 格式化 src/services/claudeRelayService.js
2025-09-09 04:14:27 +08:00
Edric Li
756918b0ce feat: 在错误日志中添加账号信息
- 在 claudeRelayService.js 的所有错误日志中添加账号名称或 ID
- 在 claudeConsoleRelayService.js 的错误日志中添加账号信息
- 便于排查 529 (过载) 和 504 (超时) 错误对应的具体账号

问题背景:
用户反馈错误日志中没有账号信息,无法定位是哪个账号出现问题,
特别是 529 和 504 错误频繁出现时难以排查。

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-09 02:45:47 +08:00
Edric Li
908e323db0 feat: 为普通Claude账户添加529错误处理功能
- 添加可配置的529错误处理机制,通过CLAUDE_OVERLOAD_HANDLING_MINUTES环境变量控制
- 支持流式和非流式请求的529错误检测
- 自动标记过载账户并在指定时间后恢复
- 成功请求后自动清除过载状态
- 默认禁用,需手动配置启用(0表示禁用,>0表示过载持续分钟数)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-09 00:46:40 +08:00
shaw
fec80a16fa fix: 优化请求超时配置 2025-09-08 16:34:27 +08:00
Wesley Liddick
5c8136ddd4 Merge branch 'dev' into main 2025-09-08 16:14:54 +08:00
Edric Li
9fa7602947 feat: 优化错误处理机制和代码重构
- 将5xx错误阈值从10次降低到3次,符合行业标准(AWS ELB: 2次, K8s: 3次)
- 新增网络超时(ETIMEDOUT)错误处理,触发账户降级机制
- 重构错误处理逻辑,提取统一方法_handleServerError,消除75%重复代码
- 支持不同上下文的错误日志(Network, Request, Stream等)
- 修复流式请求中的参数作用域问题,确保错误处理一致性

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-08 16:06:12 +08:00
shaw
e824858d60 feat: claude账户支持使用统一的客户端标识 2025-09-08 11:35:44 +08:00
sczheng189
0cb58c099d 去除掉统一user-agent的冗余逻辑,增加流式处理日志打印 2025-09-07 08:41:11 +08:00
sczheng189
9d1906c0b1 Merge branch 'dev' of https://github.com/Wei-Shaw/claude-relay-service into dev 2025-09-06 23:40:10 +08:00
Wesley Liddick
d6745dbe4a Merge pull request #335 from iaineng/dev
feat: 添加Claude账户403错误处理和封禁状态支持
2025-09-04 10:46:44 +08:00
sczheng189
8f08d7843f fix: 优化Claude Code User-Agent识别逻辑
- 将字符串匹配改为正则表达式匹配,提高准确性
  - 统一版本号提取正则,支持多段版本号格式
  - 修复isRealClaudeCodeRequest中的User-Agent验证逻辑"
2025-09-04 09:19:39 +08:00
iaineng
1ff14e38cb feat: 添加Claude账户403错误处理和封禁状态支持
- 新增Claude账户403错误自动检测和处理机制
- 区分Claude账户401未授权和403封禁两种错误状态
- 支持非流式和流式请求中的401/403错误处理
- 优化Claude账户错误处理代码,减少重复逻辑
- 支持前端显示不同的Claude账户错误状态和颜色
- 完善Claude账户异常Webhook通知错误码区分
2025-09-04 00:50:28 +08:00
sczheng189
39c49fe2bb feat: 添加统一Claude Code User-Agent支持及缓存管理功能
### **核心功能**
*   **自动更新**:自动获取并使用最新的 “Claude Code” 客户端版本号。
*   **智能缓存**:获取到的版本会缓存25小时,然后自动刷新。
*   **独立开关**:每个账户都可以单独设置是否启用此功能。

### **前端界面**
*   **新增开关**:账户设置里增加了“使用统一版本”的选项。
*   **信息显示**:能直接看到当前正在使用的版本号。
*   **手动刷新**:提供“清除缓存”按钮,可手动强制更新。

### **后端技术**
*   **核心方法**:开发了新的后台功能,用于捕获、比较和管理版本号。
*   **管理接口**:为管理员提供了新的API (`/admin/claude-code-version`),方便查询和刷新。
2025-09-03 20:14:58 +08:00
Wesley Liddick
d0eef7e98e Merge pull request #314 from sczheng189/feat/5xx-error-circuit-breaker
feat: 改进5xx错误熔断机制和重置状态功能
2025-09-02 09:32:08 +08:00
sczheng189
f2c2bdf6d6 feat: 改进5xx错误熔断机制和重置状态功能
## 熔断机制优化
- 将5xx错误阈值从3次提升到10次,减少误触发
- 缩短临时错误恢复时间从60分钟到5分钟
- 支持所有5xx状态码(500-599)的统一处理

## 重置状态功能完善
后端 resetAccountStatus 新增清除:
- tempErrorAt 字段 (temp_error状态)
- sessionWindowStart/sessionWindowEnd 字段
- 5xx_errors Redis计数键

前端优化:
- 重置成功后强制刷新 loadAccounts(true)
- 避免缓存导致的状态显示不一致

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-01 21:39:39 +08:00
shaw
e84c6a5555 feat: 实现基于费用的速率限制功能
- 新增 rateLimitCost 字段,支持按费用进行速率限制
- 新增 weeklyOpusCostLimit 字段,支持 Opus 模型周费用限制
- 优化速率限制逻辑,支持费用、请求数、token多维度控制
- 更新前端界面,添加费用限制配置选项
- 增强账户管理功能,支持费用统计和限制
- 改进 Redis 数据模型,支持费用计数器
- 优化价格计算服务,支持更精确的成本核算

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-31 17:27:37 +08:00
sczheng189
f4f88091c1 feat: 扩展熔断机制支持所有5xx错误码
- 扩展错误检测从单一500错误到所有5xx错误码(500-599)
- 新增temp_error状态,连续3次5xx错误触发临时熔断
- 支持流式和非流式请求的统一5xx错误处理
- 添加定时清理机制,60分钟后自动恢复temp_error状态
- 完善错误计数和清理逻辑,提高系统可靠性

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-25 19:14:14 +08:00
shaw
b426a759a8 feat: 支持后台配置webhook 2025-08-23 20:20:32 +08:00
shaw
cb29b3f7e4 fix: 代理ip使用重构为统一方法 2025-08-20 23:21:32 +08:00
shaw
a45c832278 feat: 统一代理配置管理,支持IPv4/IPv6协议族选择
- 新增统一代理工具 ProxyHelper,支持 SOCKS5/HTTP/HTTPS 代理
- 添加 IPv4/IPv6 协议族配置选项,默认使用 IPv4 确保兼容性
- 移除 OpenAI 路由中硬编码的 family: 4 限制
- 统一 8 个服务文件中的代理创建逻辑,避免重复维护
- 支持 OAuth 和 token 交换过程中的代理使用
- 新增配置项:PROXY_USE_IPV4(默认 true)
- 向后兼容:现有配置无需手动更新

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-20 22:36:34 +08:00
shaw
77f80ef1f4 fix: claude token统计问题 2025-08-17 00:41:16 +08:00
shaw
4643e471ee fix: 修复claude转发usage统计问题 2025-08-17 00:03:17 +08:00
shaw
681cb8cd82 feat: 优化 Claude 模型缓存费用计算,支持 5 分钟和 1 小时两种缓存类型
- 在 pricingService 中硬编码 1 小时缓存价格(Opus: $30/MTok, Sonnet: $6/MTok, Haiku: $1.6/MTok)
- 更新 usage 捕获逻辑以分别记录 ephemeral_5m 和 ephemeral_1h 缓存 tokens
- 改进费用计算逻辑,正确计算两种缓存类型的费用
- 新增 recordUsageWithDetails 方法支持详细的缓存数据
- 保持向后兼容性,支持旧的数据格式
- 删除测试脚本 test-openai-refresh.js
- 修复 OpenAI token 刷新逻辑

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-16 22:31:20 +08:00
shaw
31bdb4aa8c feat: add comprehensive 401 error handling and account status management
- Add 401 error detection and automatic account suspension after 3 consecutive failures
- Implement account status reset functionality for clearing all error states
- Enhance admin interface with status reset controls and improved status display
- Upgrade service management script with backup protection and retry mechanisms
- Add mandatory code formatting requirements using Prettier
- Improve account selector with detailed status information and color coding

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-08 00:35:26 +08:00
千羽
8a74bf5afe refactor: standardize code formatting and linting configuration
- Replace .eslintrc.js with .eslintrc.cjs for better ES module compatibility
- Add .prettierrc configuration for consistent code formatting
- Update package.json with new lint and format scripts
- Add nodemon.json for development hot reloading configuration
- Standardize code formatting across all JavaScript and Vue files
- Update web admin SPA with improved linting rules and formatting
- Add prettier configuration to web admin SPA

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-07 18:19:31 +09:00
andersonby
9a9a82c86f feat: Add comprehensive Amazon Bedrock integration support
Add complete Amazon Bedrock integration to Claude Relay Service with:

## Core Features
-  Bedrock account management with encrypted AWS credential storage
-  Full request routing to AWS Bedrock with streaming support
-  Integration with unified Claude scheduler system
-  Support for Inference Profiles and Application Inference Profiles
-  Configurable default and small-fast model settings

## Backend Services
- Add bedrockAccountService.js for account management
- Add bedrockRelayService.js for request forwarding
- Integrate Bedrock accounts into unifiedClaudeScheduler.js
- Update admin and API routes to support Bedrock endpoints
- Add comprehensive configuration options to config.example.js

## Frontend Integration
- Complete Vue.js Web UI for Bedrock account management
- Account creation form with AWS credentials and model configuration
- Real-time account status monitoring and statistics
- Edit/update capabilities for existing accounts

## CLI Support
- Interactive CLI commands for Bedrock account operations
- Account creation, listing, updating, and testing
- Status monitoring and connection validation

## Security & Performance
- AES encrypted storage of AWS credentials in Redis
- Support for temporary credentials (session tokens)
- Region-specific configuration support
- Rate limiting and error handling

This integration enables the relay service to support three AI platforms:
1. Claude (OAuth) - Original Claude.ai integration
2. Gemini - Google AI integration
3. Amazon Bedrock - New AWS Bedrock integration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-06 17:41:16 +08:00
mouyong
80c6bed8c2 fix: 修复无法转发 opus 4.1 的问题 2025-08-06 15:26:49 +08:00
shaw
7fa75df1fd fix: 修复分组调度功能和API Keys统计弹窗UI问题
1. 分组调度功能修复:
   - 统一使用 unifiedClaudeScheduler 和 unifiedGeminiScheduler
   - 修复 schedulable 字段数据类型不一致问题(布尔值/字符串)
   - 添加 _isSchedulable() 辅助方法确保兼容性
   - 修复所有路由文件中的调度器调用

2. API Keys 统计弹窗UI优化:
   - 统一弹窗样式与系统UI风格
   - 添加右上角关闭按钮
   - 修复移动端宽度问题(设置为95%屏幕宽度)
   - 使用 Teleport 组件和项目通用样式

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-05 17:06:52 +08:00
shaw
8e89311dac feat: 使用API响应头中的准确时间戳修正会话窗口和限流时间
- 从429响应中提取 anthropic-ratelimit-unified-reset 响应头
- 使用准确的重置时间戳设置限流结束时间和会话窗口
- 会话窗口开始时间 = 重置时间戳 - 5小时
- 兼容旧逻辑:无响应头时使用预估的会话窗口时间

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-31 16:29:44 +08:00
leslie
5522967792 添加claude账号维度计算token费用 2025-07-25 21:27:17 +08:00
shaw
3553f5cc1f fix: 修复流式响应的 Parse Error 和缓冲问题
主要修改:
1. 从 compression 中间件中排除 SSE 流式响应,避免压缩导致的缓冲
2. 移除导致 Parse Error 的 res.flushHeaders() 调用
3. 改进流式响应的错误处理,发送 SSE 错误事件而不是破坏流
4. 在写入数据前检查流状态,避免写入已销毁的流
5. 优化响应结束时的处理逻辑,确保缓冲区数据正确处理

这些修改确保了流式请求能够正常显示打字机效果,同时保留了 usage token 收集功能。

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-23 16:13:07 +08:00
shaw
1e372dd365 fix: 修复流式响应缓冲问题,实现真正的实时流传输
- 配置 compression 中间件排除 SSE 流式响应,避免压缩导致的缓冲
- 添加 X-Accel-Buffering: no 响应头,禁用 Nginx 等代理的缓冲
- 使用 res.flushHeaders() 立即发送响应头
- 禁用 Nagle 算法确保数据立即发送
- 在每次写入流数据后调用 flush() 确保实时传输

这些修复确保了流式请求能够正常显示打字机效果,数据从上游 Claude API 接收后能够立即转发给客户端。

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-23 15:56:27 +08:00
shaw
d6675a4d8e feat: 添加 /claude/v1/messages 路由别名并优化非 Claude Code 客户端支持
- 添加 /claude 路由作为 /api 的别名,支持 /claude/v1/messages 端点
- 实现智能判断请求来源,通过 user-agent 和系统提示词识别真实的 Claude Code 请求
- 为非 Claude Code 客户端自动设置系统提示词和必要的 headers
- 优化 headers 更新逻辑,只有真实的 Claude Code 请求才更新缓存
- 确保 /api 和 /claude 路由功能完全一致

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-22 21:07:25 +08:00
shaw
e2cece6162 fix 修复x-request-id问题 2025-07-22 18:36:53 +08:00
shaw
dabf3bf7ea feat: 实现 Claude Code headers 动态管理功能
- 创建 claudeCodeHeadersService 管理各账号的 Claude Code headers
- 自动捕获成功请求的 headers 并按账号存储在 Redis
- 智能版本管理,只保留最新版本的 headers
- OpenAI 转发时根据账号动态获取对应的 headers
- 添加管理端点查看和清除各账号的 headers 信息
- 完整支持 Claude Code 必需的 beta headers

解决了 "This credential is only authorized for use with Claude Code" 错误
避免了固定版本号带来的风控问题

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-22 16:03:31 +08:00
shaw
b2ad2a4a61 fix: 修复 OpenAI 兼容路由的 Claude API 认证和 Function Calling 支持
- 添加必需的系统消息 "You are Claude Code, Anthropic's official CLI for Claude."
- 修改 anthropic-beta header 为 OAuth-only 模式 (oauth-2025-04-20)
- 不再传递客户端 headers,使用固定的 4 个必需 headers
- 增强流式响应的 Function Calling 支持,正确处理 tool_use 事件
- 支持自定义 beta header 参数,允许不同路由使用不同的认证模式

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-22 14:38:30 +08:00