mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
fix(ollama): OpenAI 链定价预检对 /v1/messages 入站返回 Anthropic 错误信封
OpenAI 网关的 ollama_cloud 未定价预检一律写 OpenAI 风格 {"error":{...}},
/v1/messages 入站(原生 Anthropic、Messages→CC/Responses 桥接)缺顶层
"type":"error";generic 链同名预检已按入站协议选信封。两条链共用同一写出函数。
This commit is contained in:
1 parent
8d36844f39
commit
bc362b7dab
2 files changed
+14
-15
No files matched your search
@@ -50,14 +50,20 @@ func (e *openAIOllamaCloudUnpricedModelError) Error() string {
|
||||
// 先例由服务层直接写出 400 invalid_request_error。写前 MarkResponseCommitted 与
|
||||
// cyber_policy / grok content 拒绝同款:handler 侧 ensureForwardErrorResponse /
|
||||
// ensureAnthropicErrorResponse 检测到响应已提交便不再追加 fallback 错误;返回的
|
||||
// 错误非 UpstreamFailoverError——不换号、不写账号处置。
|
||||
// 错误非 UpstreamFailoverError——不换号、不写账号处置。错误信封按入站协议选择:
|
||||
// /messages 入站(OpenAI 族原生 Anthropic / Messages 桥接链与 generic 链)为 Anthropic
|
||||
// 风格,其余为 OpenAI 风格。
|
||||
func respondOpenAIOllamaCloudUnpricedModelError(c *gin.Context, err *openAIOllamaCloudUnpricedModelError) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
setOpsUpstreamError(c, http.StatusBadRequest, err.Error(), "")
|
||||
MarkResponseCommitted(c)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": gin.H{
|
||||
"type": "invalid_request_error",
|
||||
"message": err.Error(),
|
||||
}})
|
||||
payload := gin.H{"error": gin.H{"type": "invalid_request_error", "message": err.Error()}}
|
||||
if c.Request != nil && c.Request.URL != nil && strings.Contains(c.Request.URL.Path, "/messages") {
|
||||
payload = gin.H{"type": "error", "error": gin.H{"type": "invalid_request_error", "message": err.Error()}}
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, payload)
|
||||
}
|
||||
|
||||
// ollamaCloudBillingGroupFromContext 取认证中间件放入 ctx 的计费分组(与利润门 D
|
||||
@@ -135,7 +141,7 @@ func (s *OpenAIGatewayService) enforceOllamaCloudRequestPricingPreflight(ctx con
|
||||
|
||||
// enforceOllamaCloudRequestPricingPreflight 是 generic 网关(composite 跨族账号池经
|
||||
// Anthropic 协议链服务 ollama_cloud 账号)的同一预检:在上游 I/O 之前拒绝无价出站模型,
|
||||
// 错误信封按入站协议选择(/messages 为 Anthropic 风格,其余为 OpenAI 风格)。
|
||||
// 与 OpenAI 网关共用 respondOpenAIOllamaCloudUnpricedModelError(信封按入站协议选择)。
|
||||
func (s *GatewayService) enforceOllamaCloudRequestPricingPreflight(ctx context.Context, c *gin.Context, account *Account, body []byte, fallbackModel string) error {
|
||||
if s == nil || account == nil || !account.IsOllamaCloud() {
|
||||
return nil
|
||||
@@ -148,14 +154,6 @@ func (s *GatewayService) enforceOllamaCloudRequestPricingPreflight(ctx context.C
|
||||
return nil
|
||||
}
|
||||
err := &openAIOllamaCloudUnpricedModelError{model: model}
|
||||
if c != nil {
|
||||
setOpsUpstreamError(c, http.StatusBadRequest, err.Error(), "")
|
||||
MarkResponseCommitted(c)
|
||||
payload := gin.H{"error": gin.H{"type": "invalid_request_error", "message": err.Error()}}
|
||||
if c.Request != nil && c.Request.URL != nil && strings.Contains(c.Request.URL.Path, "/messages") {
|
||||
payload = gin.H{"type": "error", "error": gin.H{"type": "invalid_request_error", "message": err.Error()}}
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, payload)
|
||||
}
|
||||
respondOpenAIOllamaCloudUnpricedModelError(c, err)
|
||||
return err
|
||||
}
|
||||
@@ -227,4 +227,5 @@ func TestOllamaCloudPricingPreflight_AnthropicNativePathRejectsUnpriced(t *testi
|
||||
c, rec, ctx := pricingPreflightContext("/v1/messages", body, nil)
|
||||
_, err := svc.ForwardAsAnthropic(ctx, c, account, body, "", "")
|
||||
assertPricingPreflightRejected(t, c, upstream, rec, err, "totally-unpriced-model")
|
||||
require.Equal(t, "error", gjson.Get(rec.Body.String(), "type").String(), "/v1/messages 入站必须使用 Anthropic 错误信封")
|
||||
}
|
||||
Reference in new issue
Block a user