fix(billing): 文本模型 - 修复流式 Token 用量结算

为 Token 计费的流式 Chat Completions 请求开启 usage 终态块,并解析 Chat Completions 与 Responses SSE 用量。
This commit is contained in:
MasterChiefCN
2026-08-16 13:12:41 +08:00
committed by GitHub
parent c7965e7934
commit a48b58e5f6
5 changed files with 194 additions and 16 deletions

View File

@@ -715,8 +715,10 @@ func proxySystemRequest(c *gin.Context, svc *service.Service, user *model.User,
modelName := proxyRequestModelForPath(path, c.GetHeader("Content-Type"), body)
protocol := model.ChannelInterfaceType("")
capability := "text"
var channelModel *model.ChannelModel
if !(c.Request.Method == http.MethodGet && path == "/models") {
channelModel, modelErr := svc.SystemChannelModel(channel.ID, modelName)
var modelErr error
channelModel, modelErr = svc.SystemChannelModel(channel.ID, modelName)
if modelErr != nil || channelModel.Protocol == "" {
fail(c, http.StatusForbidden, errors.New("当前系统渠道未授权该模型或模型协议尚未配置"))
return
@@ -728,6 +730,13 @@ func proxySystemRequest(c *gin.Context, svc *service.Service, user *model.User,
fail(c, http.StatusForbidden, err)
return
}
if channelModel != nil && channelModel.BillingMode == "token" && protocol == model.ChannelInterfaceChatCompletion {
body, err = service.EnsureChatCompletionStreamUsageRequest(body)
if err != nil {
fail(c, http.StatusBadRequest, err)
return
}
}
billingOrderID := ""
query := c.Request.URL.Query()
for _, key := range []string{"key", "api_key", "access_token", "token"} {