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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# kiro-rs
|
| 2 |
+
|
| 3 |
+
一个用 Rust 编写的 Anthropic Claude API 兼容代理服务,将 Anthropic API 请求转换为 Kiro API 请求。
|
| 4 |
+
|
| 5 |
+
## 功能特性
|
| 6 |
+
|
| 7 |
+
- **Anthropic API 兼容**: 完整支持 Anthropic Claude API 格式
|
| 8 |
+
- **流式响应**: 支持 SSE (Server-Sent Events) 流式输出
|
| 9 |
+
- **Token 自动刷新**: 自动管理和刷新 OAuth Token
|
| 10 |
+
- **Thinking 模式**: 支持 Claude 的 extended thinking 功能
|
| 11 |
+
- **工具调用**: 完整支持 function calling / tool use
|
| 12 |
+
- **多模型支持**: 支持 Sonnet、Opus、Haiku 系列模型
|
| 13 |
+
|
| 14 |
+
## 支持的 API 端点
|
| 15 |
+
|
| 16 |
+
| 端点 | 方法 | 描述 |
|
| 17 |
+
|------|------|-------------|
|
| 18 |
+
| `/v1/models` | GET | 获取可用模型列表 |
|
| 19 |
+
| `/v1/messages` | POST | 创建消息(对话) |
|
| 20 |
+
| `/v1/messages/count_tokens` | POST | 估算 Token 数量 |
|
| 21 |
+
|
| 22 |
+
## 快速开始
|
| 23 |
+
|
| 24 |
+
### 1. 编译项目
|
| 25 |
+
|
| 26 |
+
```bash
|
| 27 |
+
cargo build --release
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
### 2. 配置文件
|
| 31 |
+
|
| 32 |
+
创建 `config.json` 配置文件:
|
| 33 |
+
|
| 34 |
+
```json
|
| 35 |
+
{
|
| 36 |
+
"host": "127.0.0.1",
|
| 37 |
+
"port": 8990,
|
| 38 |
+
"apiKey": "sk-your-custom-api-key",
|
| 39 |
+
"region": "us-east-1",
|
| 40 |
+
"kiroVersion": "0.8.0",
|
| 41 |
+
"systemVersion": "darwin#24.6.0",
|
| 42 |
+
"nodeVersion": "22.21.1"
|
| 43 |
+
}
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
### 3. 凭证文件
|
| 47 |
+
|
| 48 |
+
创建 `credentials.json` 凭证文件(从 Kiro IDE 获取):
|
| 49 |
+
|
| 50 |
+
```json
|
| 51 |
+
{
|
| 52 |
+
"accessToken": "your-access-token",
|
| 53 |
+
"refreshToken": "your-refresh-token",
|
| 54 |
+
"profileArn": "arn:aws:sso::123456789:profile/xxx", // 登录时返回
|
| 55 |
+
"expiresAt": "2024-01-01T00:00:00Z",
|
| 56 |
+
"authMethod": "social",
|
| 57 |
+
"provider": "Google"
|
| 58 |
+
}
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### 4. 启动服务
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
./target/release/kiro-rs
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
或指定配置文件路径:
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
./target/release/kiro-rs -c /path/to/config.json --credentials /path/to/credentials.json
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### 5. 使用 API
|
| 74 |
+
|
| 75 |
+
```bash
|
| 76 |
+
curl http://127.0.0.1:8990/v1/messages \
|
| 77 |
+
-H "Content-Type: application/json" \
|
| 78 |
+
-H "x-api-key: sk-your-custom-api-key" \
|
| 79 |
+
-d '{
|
| 80 |
+
"model": "claude-sonnet-4-20250514",
|
| 81 |
+
"max_tokens": 1024,
|
| 82 |
+
"messages": [
|
| 83 |
+
{"role": "user", "content": "Hello, Claude!"}
|
| 84 |
+
]
|
| 85 |
+
}'
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## 配置说明
|
| 89 |
+
|
| 90 |
+
### config.json
|
| 91 |
+
|
| 92 |
+
| 字段 | 类型 | 默认值 | 描述 |
|
| 93 |
+
|------|------|--------|-------------------------|
|
| 94 |
+
| `host` | string | `127.0.0.1` | 服务监听地址 |
|
| 95 |
+
| `port` | number | `8080` | 服务监听端口 |
|
| 96 |
+
| `apiKey` | string | - | 自定义 API Key(用于客户端认证) |
|
| 97 |
+
| `region` | string | `us-east-1` | AWS 区域 |
|
| 98 |
+
| `kiroVersion` | string | `0.8.0` | Kiro 版本号 |
|
| 99 |
+
| `machineId` | string | - | 自定义机器码(64位十六进制)不定义则自动生成 |
|
| 100 |
+
| `systemVersion` | string | 随机 | 系统版本标识 |
|
| 101 |
+
| `nodeVersion` | string | `22.21.1` | Node.js 版本标识 |
|
| 102 |
+
|
| 103 |
+
### credentials.json
|
| 104 |
+
|
| 105 |
+
| 字段 | 类型 | 描述 |
|
| 106 |
+
|------|------|-------------------------|
|
| 107 |
+
| `accessToken` | string | OAuth 访问令牌 |
|
| 108 |
+
| `refreshToken` | string | OAuth 刷新令牌 |
|
| 109 |
+
| `profileArn` | string | AWS Profile ARN (登录时返回) |
|
| 110 |
+
| `expiresAt` | string | Token 过期时间 (RFC3339) |
|
| 111 |
+
| `authMethod` | string | 认证方式 |
|
| 112 |
+
| `provider` | string | 认证提供者 |
|
| 113 |
+
|
| 114 |
+
## 模型映射
|
| 115 |
+
|
| 116 |
+
| Anthropic 模型 | Kiro 模型 |
|
| 117 |
+
|----------------|-----------|
|
| 118 |
+
| `*sonnet*` | `claude-sonnet-4.5` |
|
| 119 |
+
| `*opus*` | `claude-opus-4.5` |
|
| 120 |
+
| `*haiku*` | `claude-haiku-4.5` |
|
| 121 |
+
|
| 122 |
+
## 项目结构
|
| 123 |
+
|
| 124 |
+
```
|
| 125 |
+
kiro-rs/
|
| 126 |
+
├── src/
|
| 127 |
+
│ ├── main.rs # 程序入口
|
| 128 |
+
│ ├── model/ # 配置和参数模型
|
| 129 |
+
│ │ ├── config.rs # 应用配置
|
| 130 |
+
│ │ └── arg.rs # 命令行参数
|
| 131 |
+
│ ├── anthropic/ # Anthropic API 兼容层
|
| 132 |
+
│ │ ├── router.rs # 路由配置
|
| 133 |
+
│ │ ├── handlers.rs # 请求处理器
|
| 134 |
+
│ │ ├── middleware.rs # 认证中间件
|
| 135 |
+
│ │ ├── types.rs # 类型定义
|
| 136 |
+
│ │ ├── converter.rs # 协议转换器
|
| 137 |
+
│ │ ├── stream.rs # 流式响应处理
|
| 138 |
+
│ │ └── token.rs # Token 估算
|
| 139 |
+
│ └── kiro/ # Kiro API 客户端
|
| 140 |
+
│ ├── provider.rs # API 提供者
|
| 141 |
+
│ ├── token_manager.rs # Token 管理
|
| 142 |
+
│ ├── machine_id.rs # 设备指纹生成
|
| 143 |
+
│ ├── model/ # 数据模型
|
| 144 |
+
│ │ ├── credentials.rs # OAuth 凭证
|
| 145 |
+
│ │ ├── events/ # 响应事件类型
|
| 146 |
+
│ │ ├── requests/ # 请求类型
|
| 147 |
+
│ │ └── common/ # 共享类型
|
| 148 |
+
│ └── parser/ # AWS Event Stream 解析器
|
| 149 |
+
│ ├── decoder.rs # 流式解码器
|
| 150 |
+
│ ├── frame.rs # 帧解析
|
| 151 |
+
│ ├── header.rs # 头部解析
|
| 152 |
+
│ └── crc.rs # CRC 校验
|
| 153 |
+
├── Cargo.toml # 项目配置
|
| 154 |
+
├── config.example.json # 配置示例
|
| 155 |
+
└── credentials.json # 凭证文件
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
## 技术栈
|
| 159 |
+
|
| 160 |
+
- **Web 框架**: [Axum](https://github.com/tokio-rs/axum) 0.8
|
| 161 |
+
- **异步运行时**: [Tokio](https://tokio.rs/)
|
| 162 |
+
- **HTTP 客户端**: [Reqwest](https://github.com/seanmonstar/reqwest)
|
| 163 |
+
- **序列化**: [Serde](https://serde.rs/)
|
| 164 |
+
- **日志**: [tracing](https://github.com/tokio-rs/tracing)
|
| 165 |
+
- **命令行**: [Clap](https://github.com/clap-rs/clap)
|
| 166 |
+
|
| 167 |
+
## 高级功能
|
| 168 |
+
|
| 169 |
+
### Thinking 模式
|
| 170 |
+
|
| 171 |
+
支持 Claude 的 extended thinking 功能:
|
| 172 |
+
|
| 173 |
+
```json
|
| 174 |
+
{
|
| 175 |
+
"model": "claude-sonnet-4-20250514",
|
| 176 |
+
"max_tokens": 16000,
|
| 177 |
+
"thinking": {
|
| 178 |
+
"type": "enabled",
|
| 179 |
+
"budget_tokens": 10000
|
| 180 |
+
},
|
| 181 |
+
"messages": [...]
|
| 182 |
+
}
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
### 工具调用
|
| 186 |
+
|
| 187 |
+
完整支持 Anthropic 的 tool use 功能:
|
| 188 |
+
|
| 189 |
+
```json
|
| 190 |
+
{
|
| 191 |
+
"model": "claude-sonnet-4-20250514",
|
| 192 |
+
"max_tokens": 1024,
|
| 193 |
+
"tools": [
|
| 194 |
+
{
|
| 195 |
+
"name": "get_weather",
|
| 196 |
+
"description": "获取指定城市的天气",
|
| 197 |
+
"input_schema": {
|
| 198 |
+
"type": "object",
|
| 199 |
+
"properties": {
|
| 200 |
+
"city": {"type": "string"}
|
| 201 |
+
},
|
| 202 |
+
"required": ["city"]
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
],
|
| 206 |
+
"messages": [...]
|
| 207 |
+
}
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
### 流式响应
|
| 211 |
+
|
| 212 |
+
设置 `stream: true` 启用 SSE 流式响应:
|
| 213 |
+
|
| 214 |
+
```json
|
| 215 |
+
{
|
| 216 |
+
"model": "claude-sonnet-4-20250514",
|
| 217 |
+
"max_tokens": 1024,
|
| 218 |
+
"stream": true,
|
| 219 |
+
"messages": [...]
|
| 220 |
+
}
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
## 认证方式
|
| 224 |
+
|
| 225 |
+
支持两种 API Key 认证方式:
|
| 226 |
+
|
| 227 |
+
1. **x-api-key Header**
|
| 228 |
+
```
|
| 229 |
+
x-api-key: sk-your-api-key
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
2. **Authorization Bearer**
|
| 233 |
+
```
|
| 234 |
+
Authorization: Bearer sk-your-api-key
|
| 235 |
+
```
|
| 236 |
+
|
| 237 |
+
## 环境变量
|
| 238 |
+
|
| 239 |
+
可通过环境变量配置日志级别:
|
| 240 |
+
|
| 241 |
+
```bash
|
| 242 |
+
RUST_LOG=debug ./target/release/kiro-rs
|
| 243 |
+
```
|
| 244 |
+
|
| 245 |
+
## 注意事项
|
| 246 |
+
|
| 247 |
+
1. **凭证安全**: 请妥善保管 `credentials.json` 文件,不要提交到版本控制
|
| 248 |
+
2. **Token 刷新**: 服务会自动刷新过期的 Token,无需手动干预
|
| 249 |
+
3. **不支持的工具**: `web_search` 和 `websearch` 工具会被自动过滤
|
| 250 |
+
|
| 251 |
+
## License
|
| 252 |
+
|
| 253 |
+
MIT
|