渠道: 模型名称映射(网关生效 + 渠道侧管理)

- 候选渠道携带 upstream_model, prepareUpstream 改写请求体 model 字段为上游名
- 新增渠道视角绑定 CRUD: GET/POST/PATCH/DELETE /admin/channels/:id/models
- 前端渠道页新增"模型映射": 列出绑定、内联改上游名、解除、添加
- rewriteModel 三种协议通用(model 均在顶层)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Sakurasan
2026-08-16 00:09:01 +08:00
co-authored by Claude
parent 8d9940ad5a
commit 4d4d09ba58
8 changed files with 331 additions and 33 deletions
+15 -6
View File
@@ -44,19 +44,28 @@ func parseBody(c *gin.Context) (*bodyReq, []byte, error) {
return br, body, nil
}
// upstreamURL 组装上游地址:base_url + 路径。
// 兼容用户填完整 base(含 /v1):去掉尾部 /v1,避免与请求路径重复。
// upstreamURL 组装上游地址,智能识别用户填写的 Base URL 形式:
// - 完整端点(以目标资源路径结尾) → 直接使用
// - 已含版本前缀(如 /v1) → 只拼资源路径(/chat/completions 等)
// - 纯域名/地址前缀 → 拼完整路径(/v1/chat/completions 等)
func upstreamURL(ch *store.Channel, path string) string {
base := strings.TrimRight(ch.BaseURL, "/")
return strings.TrimSuffix(base, "/v1") + path
if strings.HasSuffix(base, path) {
return base
}
if strings.HasSuffix(base, "/v1") {
return base + strings.TrimPrefix(path, "/v1")
}
return base + path
}
// doProxy 通用代理(M5):遍历候选渠道,按需转换;可安全重试的失败自动故障转移。
func (g *Gateway) doProxy(c *gin.Context, cands []*store.Channel, clientProto string, body []byte, stream bool, sink *usageSink) {
func (g *Gateway) doProxy(c *gin.Context, cands []channel.Candidate, clientProto string, body []byte, stream bool, sink *usageSink) {
var lastStatus = http.StatusBadGateway
var lastBody = []byte("all upstream channels failed")
for _, ch := range cands {
plan, err := prepareUpstream(ch, clientProto, body)
for _, cand := range cands {
ch := cand.Channel
plan, err := prepareUpstream(ch, clientProto, body, cand.UpstreamModel)
if err != nil {
lastStatus, lastBody = http.StatusInternalServerError, []byte("conversion error: "+err.Error())
continue