mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-12 22:42:34 +00:00
fix(openai): log /v1/chat/completions upstream endpoint for chat-only API-key accounts
DeriveUpstreamEndpoint maps every OpenAI-platform request to /v1/responses, but API-key accounts whose upstream only speaks Chat Completions (!ShouldUseResponsesAPI) are forwarded directly to /v1/chat/completions. The messages, responses and cyber-policy recording sites derived the endpoint via the bare GetUpstreamEndpoint, so usage/ops records mislabeled those requests as /v1/responses. Generalize the existing resolveRawCCUpstreamEndpoint into resolveOpenAIUpstreamEndpoint and use it at every OpenAI recording site, matching the already-correct chat-completions client path.
This commit is contained in:
1 parent
4a5665da5b
commit
bab8a9a93e
2 files changed
+11
-11
No files matched your search
@@ -289,7 +289,7 @@ func (h *OpenAIGatewayHandler) ChatCompletions(c *gin.Context) {
|
|||||||
userAgent := c.GetHeader("User-Agent")
|
userAgent := c.GetHeader("User-Agent")
|
||||||
clientIP := ip.GetClientIP(c)
|
clientIP := ip.GetClientIP(c)
|
||||||
inboundEndpoint := GetInboundEndpoint(c)
|
inboundEndpoint := GetInboundEndpoint(c)
|
||||||
upstreamEndpoint := resolveRawCCUpstreamEndpoint(c, account)
|
upstreamEndpoint := resolveOpenAIUpstreamEndpoint(c, account)
|
||||||
|
|
||||||
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
||||||
h.submitOpenAIUsageRecordTask(c.Request.Context(), result, func(ctx context.Context) {
|
h.submitOpenAIUsageRecordTask(c.Request.Context(), result, func(ctx context.Context) {
|
||||||
@@ -325,12 +325,12 @@ func (h *OpenAIGatewayHandler) ChatCompletions(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveRawCCUpstreamEndpoint returns the actual upstream endpoint for
|
// resolveOpenAIUpstreamEndpoint returns the actual upstream endpoint for an
|
||||||
// OpenAI Chat Completions requests. For APIKey accounts whose upstream
|
// OpenAI account, used by every OpenAI usage-recording site. APIKey accounts
|
||||||
// is forced or probed to not support the Responses API, the request is
|
// whose upstream is forced or probed to not support the Responses API are
|
||||||
// forwarded directly to /v1/chat/completions — not through the default
|
// served directly via /v1/chat/completions (the raw chat path) regardless of
|
||||||
// CC→Responses conversion path.
|
// the inbound endpoint; everything else goes through the Responses API.
|
||||||
func resolveRawCCUpstreamEndpoint(c *gin.Context, account *service.Account) string {
|
func resolveOpenAIUpstreamEndpoint(c *gin.Context, account *service.Account) string {
|
||||||
if account != nil && account.Type == service.AccountTypeAPIKey &&
|
if account != nil && account.Type == service.AccountTypeAPIKey &&
|
||||||
!openai_compat.ShouldUseResponsesAPI(account.Extra) {
|
!openai_compat.ShouldUseResponsesAPI(account.Extra) {
|
||||||
return "/v1/chat/completions"
|
return "/v1/chat/completions"
|
||||||
|
|||||||
@@ -496,7 +496,7 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
|
|||||||
clientIP := ip.GetClientIP(c)
|
clientIP := ip.GetClientIP(c)
|
||||||
requestPayloadHash := service.HashUsageRequestPayload(body)
|
requestPayloadHash := service.HashUsageRequestPayload(body)
|
||||||
inboundEndpoint := GetInboundEndpoint(c)
|
inboundEndpoint := GetInboundEndpoint(c)
|
||||||
upstreamEndpoint := GetUpstreamEndpoint(c, account.Platform)
|
upstreamEndpoint := resolveOpenAIUpstreamEndpoint(c, account)
|
||||||
|
|
||||||
// 使用量记录通过有界 worker 池提交,避免请求热路径创建无界 goroutine。
|
// 使用量记录通过有界 worker 池提交,避免请求热路径创建无界 goroutine。
|
||||||
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
||||||
@@ -901,7 +901,7 @@ func (h *OpenAIGatewayHandler) Messages(c *gin.Context) {
|
|||||||
clientIP := ip.GetClientIP(c)
|
clientIP := ip.GetClientIP(c)
|
||||||
requestPayloadHash := service.HashUsageRequestPayload(body)
|
requestPayloadHash := service.HashUsageRequestPayload(body)
|
||||||
inboundEndpoint := GetInboundEndpoint(c)
|
inboundEndpoint := GetInboundEndpoint(c)
|
||||||
upstreamEndpoint := GetUpstreamEndpoint(c, account.Platform)
|
upstreamEndpoint := resolveOpenAIUpstreamEndpoint(c, account)
|
||||||
|
|
||||||
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
||||||
h.submitOpenAIUsageRecordTask(c.Request.Context(), result, func(ctx context.Context) {
|
h.submitOpenAIUsageRecordTask(c.Request.Context(), result, func(ctx context.Context) {
|
||||||
@@ -1496,7 +1496,7 @@ func (h *OpenAIGatewayHandler) ResponsesWebSocket(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
h.gatewayService.ReportOpenAIAccountScheduleResult(account.ID, true, result.FirstTokenMs)
|
h.gatewayService.ReportOpenAIAccountScheduleResult(account.ID, true, result.FirstTokenMs)
|
||||||
inboundEndpoint := GetInboundEndpoint(c)
|
inboundEndpoint := GetInboundEndpoint(c)
|
||||||
upstreamEndpoint := GetUpstreamEndpoint(c, account.Platform)
|
upstreamEndpoint := resolveOpenAIUpstreamEndpoint(c, account)
|
||||||
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
cyberBlocked := service.GetOpsCyberPolicy(c) != nil
|
||||||
h.submitOpenAIUsageRecordTask(ctx, result, func(taskCtx context.Context) {
|
h.submitOpenAIUsageRecordTask(ctx, result, func(taskCtx context.Context) {
|
||||||
if err := h.gatewayService.RecordUsage(taskCtx, &service.OpenAIRecordUsageInput{
|
if err := h.gatewayService.RecordUsage(taskCtx, &service.OpenAIRecordUsageInput{
|
||||||
@@ -2297,7 +2297,7 @@ func (h *OpenAIGatewayHandler) recordCyberPolicyIfMarked(c *gin.Context, apiKey
|
|||||||
var accountID int64
|
var accountID int64
|
||||||
if account != nil {
|
if account != nil {
|
||||||
accountID = account.ID
|
accountID = account.ID
|
||||||
upstreamEndpoint = GetUpstreamEndpoint(c, account.Platform)
|
upstreamEndpoint = resolveOpenAIUpstreamEndpoint(c, account)
|
||||||
}
|
}
|
||||||
stream := false
|
stream := false
|
||||||
if v, ok := c.Get(opsStreamKey); ok {
|
if v, ok := c.Get(opsStreamKey); ok {
|
||||||
|
|||||||
Reference in new issue
Block a user