1
0

feat: 实现统一模型 ID 机制

实现统一模型 ID 格式 (provider_id/model_name),支持跨协议模型标识和 Smart Passthrough。

核心变更:
- 新增 pkg/modelid 包:解析、格式化、校验统一模型 ID
- 数据库迁移:models 表使用 UUID 主键 + UNIQUE(provider_id, model_name) 约束
- Repository 层:FindByProviderAndModelName、ListEnabled 方法
- Service 层:联合唯一校验、provider ID 字符集校验
- Conversion 层:ExtractModelName、RewriteRequestModelName/RewriteResponseModelName 方法
- Handler 层:统一模型 ID 路由、Smart Passthrough、Models API 本地聚合
- 新增 error-responses、unified-model-id 规范

测试覆盖:
- 单元测试:modelid、conversion、handler、service、repository
- 集成测试:统一模型 ID 路由、Smart Passthrough 保真性、跨协议转换
- 迁移测试:UUID 主键、UNIQUE 约束、级联删除

OpenSpec:
- 归档 unified-model-id 变更到 archive/2026-04-21-unified-model-id
- 同步 11 个 delta specs 到 main specs
- 新增 error-responses、unified-model-id 规范文件
This commit is contained in:
2026-04-21 18:14:10 +08:00
parent 7f0f831226
commit 395887667d
73 changed files with 3360 additions and 1374 deletions

View File

@@ -108,12 +108,13 @@ backend/
│ │ ├── logger.go
│ │ ├── rotate.go
│ │ └── context.go
│ ├── modelid/ # 统一模型 ID 工具包
│ │ ├── model_id.go
│ │ └── model_id_test.go
│ └── validator/ # 验证器
│ └── validator.go
├── migrations/ # 数据库迁移
── 20260401000001_initial_schema.sql
│ ├── 20260401000002_add_indexes.sql
│ └── 20260419000001_add_provider_protocol.sql
── 20260421000001_initial_schema.sql
├── tests/ # 集成测试
│ ├── helpers.go
│ └── integration/
@@ -292,6 +293,8 @@ GET /anthropic/v1/models
**协议转换**:网关支持任意协议间的双向转换。客户端使用 OpenAI 协议请求,上游供应商可以是 Anthropic 协议(反之亦然)。同协议时自动透传,零序列化开销。
**统一模型 ID**:代理请求中的 `model` 字段使用 `provider_id/model_name` 格式(如 `openai/gpt-4`),网关据此路由到对应供应商。同协议时自动改写为上游 `model_name`,跨协议时通过全量转换处理。
### 管理接口
#### 供应商管理
@@ -324,14 +327,30 @@ GET /anthropic/v1/models
- `PUT /api/models/:id` - 更新模型
- `DELETE /api/models/:id` - 删除模型
**创建请求**id 由系统自动生成 UUID
```json
{
"id": "gpt-4",
"provider_id": "openai",
"model_name": "gpt-4"
}
```
**响应示例**
```json
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"provider_id": "openai",
"model_name": "gpt-4",
"unified_id": "openai/gpt-4",
"enabled": true,
"created_at": "2026-04-21T00:00:00Z"
}
```
**统一模型 ID**`unified_id` 字段为 `provider_id/model_name` 格式,用于代理请求的 `model` 参数。
#### 统计查询
- `GET /api/stats` - 查询统计