feat(platform): 模型管理与后台能力 - 新增逻辑模型路由和响应拦截

This commit is contained in:
ddCat
2026-08-18 20:42:25 +08:00
parent 0002cdd2cb
commit 8c5f56a551
89 changed files with 21451 additions and 1170 deletions

View File

@@ -46,7 +46,7 @@ func RegisterCustomRelayRoutes(r *gin.RouterGroup, svc *service.Service) {
return
}
defer release()
proxyCustomRelayRequestWithCapabilities(c, policy.Request, svc.DesktopLocalChannelsEnabled())
proxyCustomRelayRequestWithService(c, policy.Request, svc.DesktopLocalChannelsEnabled(), svc)
})
}
@@ -55,6 +55,10 @@ func proxyCustomRelayRequest(c *gin.Context, policy service.RuntimeRequestPolicy
}
func proxyCustomRelayRequestWithCapabilities(c *gin.Context, policy service.RuntimeRequestPolicy, desktopLocalChannelsEnabled bool) {
proxyCustomRelayRequestWithService(c, policy, desktopLocalChannelsEnabled, nil)
}
func proxyCustomRelayRequestWithService(c *gin.Context, policy service.RuntimeRequestPolicy, desktopLocalChannelsEnabled bool, svc *service.Service) {
requestedAllowLocal := strings.TrimSpace(c.GetHeader(service.LocalChannelRequestHeader)) == "1"
target, err := service.ValidateCustomRelayChannelURL(c.GetHeader("X-Canvas-Upstream-URL"), c.GetHeader(service.LocalChannelBaseURLHeader), requestedAllowLocal, desktopLocalChannelsEnabled)
if err != nil {
@@ -122,20 +126,20 @@ func proxyCustomRelayRequestWithCapabilities(c *gin.Context, policy service.Runt
resp, err := customRelayClient(time.Duration(policy.CustomRelayTimeoutMinutes)*time.Minute, target, requestedAllowLocal, desktopLocalChannelsEnabled).Do(upstreamReq)
if err != nil {
fail(c, http.StatusBadGateway, errors.New("自定义渠道上游连接失败"))
fail(c, http.StatusBadGateway, errors.New(userFacingRelayError(svc, errors.New("自定义渠道上游连接失败"))))
return
}
defer resp.Body.Close()
allowBinary := customVideoContentPath.MatchString(target.EscapedPath()) || strings.HasSuffix(target.Path, "/audio/speech")
writeCustomRelayResponse(c, resp, apiKey, policy.CustomRelayResponseMB<<20, allowBinary)
writeCustomRelayResponse(c, svc, resp, apiKey, policy.CustomRelayResponseMB<<20, allowBinary)
}
func writeCustomRelayResponse(c *gin.Context, resp *http.Response, apiKey string, responseLimit int64, allowBinary bool) {
func writeCustomRelayResponse(c *gin.Context, svc *service.Service, resp *http.Response, apiKey string, responseLimit int64, allowBinary bool) {
c.Header("Cache-Control", "no-store")
c.Header("X-Content-Type-Options", "nosniff")
mediaType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
writeCustomRelayError(c, resp, apiKey, mediaType)
writeCustomRelayError(c, svc, resp, apiKey, mediaType)
return
}
if mediaType == "text/event-stream" {
@@ -149,33 +153,38 @@ func writeCustomRelayResponse(c *gin.Context, resp *http.Response, apiKey string
if allowBinary && (strings.HasPrefix(mediaType, "video/") || strings.HasPrefix(mediaType, "audio/") || mediaType == "application/octet-stream") {
body, err := readLimitedRelayBody(resp.Body, responseLimit)
if err != nil {
fail(c, http.StatusBadGateway, errors.New("自定义渠道上游返回了过大的媒体文件"))
fail(c, http.StatusBadGateway, errors.New(userFacingRelayError(svc, errors.New("自定义渠道上游返回了过大的媒体文件"))))
return
}
c.Data(resp.StatusCode, mediaType, body)
return
}
if mediaType != "application/json" && !strings.HasSuffix(mediaType, "+json") {
fail(c, http.StatusBadGateway, errors.New("自定义渠道上游返回了不支持的内容类型"))
fail(c, http.StatusBadGateway, errors.New(userFacingRelayError(svc, errors.New("自定义渠道上游返回了不支持的内容类型"))))
return
}
limit := responseLimit
body, err := readLimitedRelayBody(resp.Body, limit)
if err != nil || !json.Valid(body) {
fail(c, http.StatusBadGateway, errors.New("自定义渠道上游返回无效或过大的 JSON"))
fail(c, http.StatusBadGateway, errors.New(userFacingRelayError(svc, errors.New("自定义渠道上游返回无效或过大的 JSON"))))
return
}
body = redactRelaySecret(body, apiKey)
c.Data(resp.StatusCode, "application/json; charset=utf-8", body)
}
func writeCustomRelayError(c *gin.Context, resp *http.Response, apiKey string, mediaType string) {
func writeCustomRelayError(c *gin.Context, svc *service.Service, resp *http.Response, apiKey string, mediaType string) {
body, err := readLimitedRelayBody(resp.Body, maxCustomRelayErrorResponseBytes)
if err != nil {
fail(c, http.StatusBadGateway, errors.New("自定义渠道上游请求失败"))
fail(c, http.StatusBadGateway, errors.New(userFacingRelayError(svc, errors.New("自定义渠道上游请求失败"))))
return
}
body = redactRelaySecret(body, apiKey)
rawMessage := strings.TrimSpace(string(body))
if intercepted := interceptedRelayText(svc, rawMessage); intercepted != rawMessage {
fail(c, resp.StatusCode, errors.New(intercepted))
return
}
if (mediaType == "application/json" || strings.HasSuffix(mediaType, "+json")) && json.Valid(body) {
c.Data(resp.StatusCode, "application/json; charset=utf-8", body)
return
@@ -184,7 +193,21 @@ func writeCustomRelayError(c *gin.Context, resp *http.Response, apiKey string, m
if len(snippet) > 200 {
snippet = snippet[:200] + "..."
}
fail(c, resp.StatusCode, fmt.Errorf("自定义渠道上游请求失败(%s)%s", resp.Status, snippet))
fail(c, resp.StatusCode, errors.New(userFacingRelayError(svc, fmt.Errorf("自定义渠道上游请求失败(%s)%s", resp.Status, snippet))))
}
func userFacingRelayError(svc *service.Service, err error) string {
if svc == nil {
return err.Error()
}
return svc.UserFacingErrorMessage(err)
}
func interceptedRelayText(svc *service.Service, raw string) string {
if svc == nil {
return raw
}
return svc.InterceptResponseText(raw)
}
func copyCustomRelayStream(c *gin.Context, source io.Reader, apiKey string, maxBytes int64) {