package service import "github.com/QuantumNous/new-api/relaykit/dto" const ( usageBillingPathLocal = "local" usageBillingPathUpstream = "upstream" usageBillingPathOpenAI = "billing-usage-openai" usageBillingPathOpenAIEstimated = "billing-usage-openai-estimated" usageBillingPathAnthropic = "billing-usage-anthropic" usageBillingPathAnthropicEstimated = "billing-usage-anthropic-estimated" usageBillingPathGemini = "billing-usage-gemini" usageBillingPathGeminiEstimated = "billing-usage-gemini-estimated" ) func effectiveBillingUsage(usage *dto.Usage) *dto.Usage { if billingUsage, ok := usageFromBillingUsage(usage); ok { return billingUsage } return usage } func usageBillingPathForLog(isLocalCountTokens bool, usage *dto.Usage) string { effectiveUsage, ok := usageFromBillingUsage(usage) if !ok { if isLocalCountTokens { return usageBillingPathLocal } return usageBillingPathUpstream } switch effectiveUsage.UsageSemantic { case dto.BillingUsageSemanticOpenAI: if usage.BillingUsage.Estimated { return usageBillingPathOpenAIEstimated } return usageBillingPathOpenAI case dto.BillingUsageSemanticAnthropic: if usage.BillingUsage.Estimated { return usageBillingPathAnthropicEstimated } return usageBillingPathAnthropic case dto.BillingUsageSemanticGemini: if usage.BillingUsage.Estimated { return usageBillingPathGeminiEstimated } return usageBillingPathGemini } return usageBillingPathUpstream } func appendUsageBillingPathForLog(other map[string]interface{}, isLocalCountTokens bool, usage *dto.Usage) { if other == nil { return } adminInfo, ok := other["admin_info"].(map[string]interface{}) if !ok || adminInfo == nil { adminInfo = make(map[string]interface{}) other["admin_info"] = adminInfo } adminInfo["usage_billing_path"] = usageBillingPathForLog(isLocalCountTokens, usage) } func usageFromBillingUsage(usage *dto.Usage) (*dto.Usage, bool) { if usage == nil || usage.BillingUsage == nil { return nil, false } return usage.BillingUsage.CanonicalUsage() }