fix(gateway): 修复 base_url 包含 /chat/completions 时路径拼接错误

问题:
- 当账号的 base_url 配置为 https://example.com/v1/chat/completions 时
- 代码直接追加 /responses,导致路径变成 /v1/chat/completions/responses
- 上游返回 404 错误

修复:
- 在追加 /responses 前,先移除 base_url 中的 /chat/completions 后缀
- 确保最终路径为 https://example.com/v1/responses

影响范围:
- OpenAI API Key 账号的测试接口
- OpenAI API Key 账号的实际网关请求

Related-to: #231
This commit is contained in:
ianshaw
2026-01-12 11:39:45 -08:00
parent ea699cbdc2
commit 7fdc25df3c
2 changed files with 7 additions and 1 deletions

View File

@@ -332,7 +332,10 @@ func (s *AccountTestService) testOpenAIAccountConnection(c *gin.Context, account
if err != nil {
return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
}
apiURL = strings.TrimSuffix(normalizedBaseURL, "/") + "/responses"
// Remove /chat/completions suffix if present, then add /responses
normalizedBaseURL = strings.TrimSuffix(normalizedBaseURL, "/")
normalizedBaseURL = strings.TrimSuffix(normalizedBaseURL, "/chat/completions")
apiURL = normalizedBaseURL + "/responses"
} else {
return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported account type: %s", account.Type))
}

View File

@@ -740,6 +740,9 @@ func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.
if err != nil {
return nil, err
}
// Remove /chat/completions suffix if present, then add /responses
validatedURL = strings.TrimSuffix(validatedURL, "/")
validatedURL = strings.TrimSuffix(validatedURL, "/chat/completions")
targetURL = validatedURL + "/responses"
}
default: