fix(billing): 文本模型 - 修复流式 Token 用量结算
为 Token 计费的流式 Chat Completions 请求开启 usage 终态块,并解析 Chat Completions 与 Responses SSE 用量。
This commit is contained in:
1 parent
c7965e7934
commit
a48b58e5f6
5 files changed
+194
-16
No files matched your search
@@ -113,6 +113,7 @@ type providerAnalyticsContext struct {
|
||||
UserID string
|
||||
TaskID string
|
||||
BillingOrderID string
|
||||
BillingMode string
|
||||
Capability string
|
||||
Operation string
|
||||
ChannelID string
|
||||
@@ -125,6 +126,12 @@ type providerAnalyticsContext struct {
|
||||
|
||||
func withProviderAnalytics(ctx context.Context, service *Service, task model.Task) context.Context {
|
||||
metadata := providerAnalyticsContext{Service: service, UserID: task.UserID, TaskID: task.ID, BillingOrderID: task.BillingOrderID, Capability: capabilityFromTaskType(task.Type), Operation: task.Operation, Model: task.Model, ProviderRequestID: task.ProviderRequestID}
|
||||
// 账单模式随请求上下文传递,流式协议据此只为 Token 计费开启 usage 终态块。
|
||||
if service != nil && task.BillingOrderID != "" {
|
||||
if order, err := service.repo.BillingOrder(task.BillingOrderID); err == nil {
|
||||
metadata.BillingMode = order.BillingMode
|
||||
}
|
||||
}
|
||||
var input struct {
|
||||
Mode string `json:"mode"`
|
||||
Config providerConfig `json:"config"`
|
||||
@@ -2417,6 +2424,12 @@ func runSeedanceAgentPlanVideoTask(ctx context.Context, input canvasGenerationIn
|
||||
|
||||
func requestTextProvider(ctx context.Context, config providerConfig, path string, body map[string]interface{}, protocol string, stream bool) (string, error) {
|
||||
if stream {
|
||||
metadata, _ := ctx.Value(providerAnalyticsKey{}).(providerAnalyticsContext)
|
||||
if protocol == "chat-completion" && metadata.BillingMode == "token" {
|
||||
if err := ensureChatCompletionStreamUsage(body); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return postStreamingText(ctx, config, path, body, protocol)
|
||||
}
|
||||
var payload map[string]interface{}
|
||||
|
||||
Reference in new issue
Block a user