From 3ce09c8aff085ce42538d539d515742d8650e6f0 Mon Sep 17 00:00:00 2001 From: shaw Date: Sun, 25 Oct 2026 21:30:45 +0800 Subject: [PATCH] fix(billing): keep tokens for channel per-request image tiers calculatePerRequestCost falls back to context-based tiers when the size tier misses; dropping Tokens made those tiers never match and settled images at $0 regardless of the image_count_billing switch. --- .../internal/service/gateway_usage_billing.go | 14 ++++--- .../image_count_billing_switch_test.go | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/backend/internal/service/gateway_usage_billing.go b/backend/internal/service/gateway_usage_billing.go index 903b22486a..0ece86cd3b 100644 --- a/backend/internal/service/gateway_usage_billing.go +++ b/backend/internal/service/gateway_usage_billing.go @@ -1193,15 +1193,22 @@ func (s *GatewayService) calculateImageCost( return s.billingService.CalculateImageCost(billingModel, sizeTier, result.ImageCount, groupConfig, multiplier) } if resolved != nil && resolved.Source == PricingSourceChannel { + tokens := UsageTokens{ + InputTokens: result.Usage.InputTokens, + OutputTokens: result.Usage.OutputTokens, + ImageOutputTokens: result.Usage.ImageOutputTokens, + } switch { case resolved.Mode == BillingModePerRequest || resolved.Mode == BillingModeImage: - // 按次/图片模式只按请求张数结算,与 OpenAI 流水线口径一致。 + // 按次/图片模式按请求张数结算。Tokens 不参与计价,但尺寸档未命中时 + // calculatePerRequestCost 要用它匹配按上下文分档的单价,不能省略。 gid := apiKey.Group.ID cost, err := s.billingService.CalculateCostUnified(CostInput{ Ctx: ctx, Model: billingModel, GroupID: &gid, Group: apiKey.Group, + Tokens: tokens, RequestCount: result.ImageCount, SizeTier: sizeTier, ReasoningEffort: optionalStringValue(result.ReasoningEffort), @@ -1216,11 +1223,6 @@ func (s *GatewayService) calculateImageCost( return cost case !forcePerImage: // token 模式且开关未强制按张:保持原 token 计费。 - tokens := UsageTokens{ - InputTokens: result.Usage.InputTokens, - OutputTokens: result.Usage.OutputTokens, - ImageOutputTokens: result.Usage.ImageOutputTokens, - } gid := apiKey.Group.ID cost, err := s.billingService.CalculateCostUnified(CostInput{ Ctx: ctx, diff --git a/backend/internal/service/image_count_billing_switch_test.go b/backend/internal/service/image_count_billing_switch_test.go index e2f8f1752a..31864fe785 100644 --- a/backend/internal/service/image_count_billing_switch_test.go +++ b/backend/internal/service/image_count_billing_switch_test.go @@ -184,3 +184,41 @@ func TestGroupService_ImageCountBillingValidation(t *testing.T) { require.False(t, updated.ImageCountBilling) }) } + +// 渠道按次定价只配置按上下文分档(无尺寸档)时,尺寸档未命中,必须用 token +// 上下文匹配档位;漏传 Tokens 会让档位永不命中而按 $0 结算。与开关无关。 +func TestCalculateRecordUsageCost_ChannelPerRequestContextTierUsesTokens(t *testing.T) { + for _, switchOn := range []bool{false, true} { + groupID := int64(905) + model := "gemini-3-pro-image" + resolver := newOpenAITokenImageChannelPricingResolverForTest(t, groupID, model) + cached, ok := resolver.channelService.cache.Load().(*channelCache) + require.True(t, ok) + maxTokens := 1000 + cached.pricingByGroupModel[channelModelKey{groupID: groupID, model: model}] = &ChannelModelPricing{ + BillingMode: BillingModePerRequest, + Intervals: []PricingInterval{ + {MinTokens: 0, MaxTokens: &maxTokens, PerRequestPrice: f64p(0.1)}, + {MinTokens: 1000, PerRequestPrice: f64p(0.3)}, + }, + } + group := &Group{ + ID: groupID, + Platform: PlatformComposite, + Status: StatusActive, + Hydrated: true, + AllowImageGeneration: true, + ImageCountBilling: switchOn, + } + apiKey := &APIKey{ID: 5, GroupID: &groupID, Group: group} + svc := &GatewayService{billingService: resolver.billingService, resolver: resolver} + + cost := svc.calculateRecordUsageCost(context.Background(), &ForwardResult{ + Model: model, ImageCount: 2, ImageSize: "1K", ImageEndpoint: true, + Usage: ClaudeUsage{InputTokens: 5000, OutputTokens: 100}, + }, apiKey, model, 1, 1, time.Time{}) + + require.NotNil(t, cost) + require.InDelta(t, 0.6, cost.TotalCost, 1e-12, "switch=%v: 应按上下文档 0.3 × 2 张结算", switchOn) + } +}