feat(channel): add channel management with multi-mode pricing and billing integration#1455
Merged
Wei-Shaw merged 67 commits intoWei-Shaw:mainfrom Apr 4, 2026
Merged
Conversation
Cherry-picked from release/custom-0.1.106: a911760
- 模型输入改为标签列表(输入回车添加,支持粘贴批量导入) - 价格显示单位改为 $/MTok(每百万 token),提交时自动转换 - Token 模式增加图片输出价格字段(适配 Gemini 图片模型按 token 计费) - 区间边界改为左开右闭 (min, max],右边界包含 - 默认价格作为未命中区间时的回退价格 - 添加完整中英文 i18n 翻译
- 渠道模型映射:新增 model_mapping JSONB 字段,在账号映射之前执行 - 分组选择:添加搜索过滤 + 平台图标 - 定价卡片:支持折叠/展开,已有数据默认折叠 - 模型冲突校验:前后端均禁止同一渠道内重复模型 - 迁移 083: channels 表添加 model_mapping 列
- 缓存重构为 O(1) 哈希结构 (pricingByGroupModel, mappingByGroupModel) - 渠道模型映射接入网关流程 (Forward 前应用, a→b→c 映射链) - 新增 billing_model_source 配置 (请求模型/最终模型计费) - usage_logs 新增 channel_id, model_mapping_chain, billing_tier 字段 - 每种计费模式统一支持默认价格 + 区间定价 - 渠道模型限制开关 (restrict_models) - 分组按平台分类展示 + 彩色图标 - 必填字段红色星号 + 模型映射 UI - 去除模型通配符支持
- 缓存按 (groupID, platform, model) 三维 key 扁平化,避免跨平台同名模型冲突
- buildCache 批量查询 group platform,按平台过滤展开定价和映射
- model_mapping 改为嵌套格式 {platform: {src: dst}}
- channel_model_pricing 新增 platform 列
- 前端按平台维度重构:每个平台独立配置分组/映射/定价
- 迁移 086: platform 列 + model_mapping 嵌套格式迁移
- 新增 GET /admin/channels/model-pricing?model=xxx API - 从 BillingService 查询 LiteLLM/Fallback 默认定价 - 前端添加模型时自动查询并填充价格($/MTok) - 仅在所有价格字段为空时才自动填充,不覆盖手动配置
- DB: usage_logs 表新增 billing_mode VARCHAR(20) 列 - 后端: RecordUsage 写入时根据 image_count 判定计费模式 - 前端: 使用记录表格新增计费模式 badge 列 + 筛选下拉
- GatewayService/OpenAIGatewayService 注入 ModelPricingResolver - RecordUsage 从旧路径迁移到 CalculateCostUnified(支持 per_request/image 模式) - 无渠道时自动回退旧路径,保持原有行为 - 长上下文双倍计费仅在无渠道定价时生效 - CostBreakdown 新增 BillingMode 字段,使用日志记录实际计费模式 - 模型限制错误改为与"无可用账号"相同的 503 响应
ResolvedPricing 新增 DefaultPerRequestPrice,当无层级匹配时使用渠道的默认按次价格
- 定价查找支持通配符(suffix *),最长前缀优先匹配 - 模型限制(restrict_models)同样支持通配符匹配 - OpenAI 网关接入渠道映射/BillingModelSource/模型限制 - 按次/图片计费模式创建时强制要求价格或层级(前后端) - 用户使用记录列表增加计费模式 badge 列
- OpenAI 网关三处 IsModelRestricted 改用 channelMapping.MappedModel - 前端平台勾选改为 enabled 开关,取消勾选不清空配置数据 - formToAPI/校验只处理 enabled 的平台
- 抽取 ResolveChannelMappingAndRestrict 统一入口(5处→1个方法) - 抽取 BuildModelMappingChain 到 ChannelMappingResult 方法(5处→1行调用) - OpenAI 三入口 Forward 前应用渠道映射到请求体 - OpenAI Responses/Messages 限制检查添加错误响应 - 清理前端 3 处 console.log 调试日志
- 4个缺失handler入口添加渠道映射+限制检查(ChatCompletions/Responses/Gemini) - 模型限制错误信息优化,区分"模型不可用"和"无账号" - OpenAI RecordUsage RequestedModel 改用 OriginalModel - ResolveChannelMappingAndRestrict/ReplaceModelInBody 抽取到 ChannelService 消除跨service重复 - validateNoDuplicateModels 按 platform:model 去重 - 删除 Channel.ResolveMappedModel 死代码和 CalculateCostWithChannel Deprecated方法 - 移除冗余nil检查,抽取 validatePricingBillingMode 公共校验
invalidateCache 存入 (*channelCache)(nil),类型断言 ok=true 但 指针为 nil,后续 cached.loadedAt 导致 nil pointer dereference。 在 loadCache 双重检查处增加 cached != nil 防御。
- 保存时校验:定价条目有但模型列表为空时报错并跳转到对应平台 tab, 不再静默跳过导致数据丢失 - 保存时校验:启用的平台必须至少选择一个分组 - 分组关联标签增加红色 * 必填标记
与定价通配符一致,映射源支持 * 后缀通配符(最长前缀优先): - `*` 匹配所有模型 - `claude-*` 匹配 claude- 开头的模型 - 精确匹配优先于通配符
后端 validateNoDuplicateModels 使用 platform:model 复合键, 前端之前跨平台扁平化检查导致不同平台下的同名模型误报重复。
开启 restrict_models 时,应用原始模型名查定价列表; 定价列表未命中即拒绝,不因通配符映射而绕过限制。
去掉 max-w-[120px] truncate 限制,改用 flex-wrap 允许换行, 充分利用空白区域展示完整模型名。
- PricingEntryCard 折叠态模型 tag 按平台着色 - ModelTagInput 模型标签按平台着色 - 模型映射输入框边框按平台着色
Antigravity platform serves both Claude and Gemini models, but channel pricing/mapping is configured under Anthropic/Gemini tabs. The cache builder was using strict platform equality, causing antigravity groups to miss all channel pricing entries, resulting in $0 billing. Add isPlatformPricingMatch() to treat antigravity as superset of anthropic+gemini for pricing and mapping cache indexing.
- Backend: reject intervals with all-null price fields on save - Backend: filterValidIntervals skips empty intervals in pricing resolver - Frontend: red border + asterisk on empty interval rows - Backend: antigravity groups now match anthropic/gemini channel pricing
…on, credits check - TestIsPlatformPricingMatch: 12 cases covering all platform combinations - TestMatchingPlatforms: 4 cases for platform expansion - TestGetChannelModelPricing_AntigravityCrossPlatform: antigravity sees anthropic pricing - TestGetChannelModelPricing_AnthropicCannotSeeAntigravityPricing: no reverse leakage - TestResolveChannelMapping_AntigravityCrossPlatform: antigravity uses anthropic mapping - TestFilterValidIntervals: 8 cases for empty interval filtering - TestHasEnoughCredits: 10 cases for credits balance threshold logic - Extract hasEnoughCredits() pure function for testability
- clearCreditsExhausted: sync Redis scheduler cache after DB update - Image billing mode UI: write to per_request_price instead of image_output_price - OpenAI RecordUsage: use BillingModelSourceRequested constant, add s.cfg nil guard - Fix i18n key path: admin.channels.perRequestPriceRequired → admin.channels.form.perRequestPriceRequired
When ImageCount > 0, check if channel has token pricing configured: - YES (source=channel, mode=token) → use token billing with image_output_tokens - NO → fall back to CalculateImageCost (original per-image billing) This allows channels to configure $/MTok pricing for image generation models while maintaining backward compatibility for setups without channel pricing.
Instead of hardcoding BillingMode="image" when ImageCount>0, let cost.BillingMode (set by CalculateCostUnified/CalculateImageCost) take priority. This ensures channel token pricing shows "token" mode.
Only display image count format when billing_mode is "image". When channel has token pricing, show input/output/cache token details.
- PricingSourceChannel/LiteLLM/Fallback for resolver source - MediaTypeImage/Video/Prompt for result.MediaType - Reuse BillingModeToken/BillingModeImage for billing mode - Reuse BillingModelSourceChannelMapped/PlatformAnthropic in handler
- Extract newEmptyChannelCache() factory to deduplicate map init - Extract expandPricingToCache() for model pricing expansion - Extract expandMappingToCache() for model mapping expansion - buildCache reduced from 110 to 50 lines
Replace response.BadRequest with response.ErrorFrom + infraerrors.BadRequest to provide machine-readable reason codes (VALIDATION_ERROR, INVALID_CHANNEL_ID, MISSING_PARAMETER) for frontend i18n support.
- calculateTokenCost reduced from 80 to 15 lines - calculateCostInternal reduced from 91 to 15 lines - Shared logic in computeTokenBreakdown + computeCacheCreationCost - Unified rateMultiplier <= 0 protection in both paths
…d core - Extract recordUsageCore with recordUsageOpts for parameterized differences - RecordUsage (276 lines) → thin wrapper (~40 lines) - RecordUsageWithLongContext (251 lines) → thin wrapper (~20 lines) - Split billing logic into calculateSoraMediaCost, calculateImageCost, calculateTokenCost sub-functions - Extract buildRecordUsageLog for usage log construction - Net reduction: -79 lines, eliminated ~170 lines of duplication
…n gateway billing - Extract resolveChannelPricing to DRY the resolver pattern shared by calculateImageCost/calculateTokenCost - Remove unnecessary IIFE wrapper and pass accountRateMultiplier as parameter - Extract resolveBillingMode, resolveMediaType, optionalSubscriptionID to simplify buildRecordUsageLog (104→65 lines) - Extract shouldDeductAPIKeyQuota/shouldUpdateRateLimits/shouldUpdateAccountQuota methods on postUsageBillingParams to unify duplicated billing conditions
…hase Move the model pricing restriction check from 8 handler entry points to the account scheduling phase (SelectAccountForModelWithExclusions / SelectAccountWithLoadAwareness), aligning restriction with billing: - requested: check original request model against pricing list - channel_mapped: check channel-mapped model against pricing list - upstream: per-account check using account-mapped model Handler layer now only resolves channel mapping (no restriction). Scheduling layer performs pre-check for requested/channel_mapped, and per-account filtering for upstream billing source.
- Fix 7 stale comments still mentioning "限制检查" in handlers/services - Make billingModelForRestriction explicitly list channel_mapped case - Add slog.Warn for error swallowing in ResolveChannelMapping and needsUpstreamChannelRestrictionCheck - Document sticky session upstream check exemption
20 test cases covering: - billingModelForRestriction: 4 cases (requested/channel_mapped/upstream/empty) - resolveAccountUpstreamModel: 3 cases (antigravity/unsupported/non-antigravity) - checkChannelPricingRestriction: 10 cases (nil guards, 3 billing sources, RestrictModels disabled, no channel) - isUpstreamModelRestrictedByChannel: 3 cases (restricted/allowed/unsupported)
P0-1: Credits degraded response retry + fail-open - Add isAntigravityDegradedResponse() to detect transient API failures - Retry up to 3 times with exponential backoff (500ms/1s/2s) - Invalidate singleflight cache between retries - Fail-open after exhausting retries instead of 5h circuit break P1-1: Fix channel restriction pre-check timing conflict - Swap checkClaudeCodeRestriction before checkChannelPricingRestriction - Ensures channel restriction is checked against final fallback groupID P1-2: Add interval pricing validation (frontend + backend) - Backend: ValidateIntervals() with boundary, price, overlap checks - Frontend: validateIntervals() with Chinese error messages - Rules: MinTokens>=0, MaxTokens>MinTokens, prices>=0, no overlap P2: Fix cross-platform same-model pricing/mapping override - Store cache keys using original platform instead of group platform - Lookup across matching platforms (antigravity→anthropic→gemini) - Prevents anthropic/gemini same-name models from overwriting each other
…tus toggle stale data - GetGroupPlatforms failure now stores error-TTL cache and returns error (fail-close) - Frontend group-to-channel conflict map loads all channels instead of current page only - Toggle channel status reloads list when active filter would hide the changed item
- Change channel cache TTL from 60s to 10min (reduce unnecessary DB queries) - Actively rebuild cache after CRUD instead of lazy invalidation - Add slog.Warn logging for channel pricing restriction blocks (4 places)
- Add int64(0) param to SelectAccountWithLoadAwareness callers (signature change from channel scheduling refactor) - Add UsageMapHook type and struct field to StreamingProcessor - Revert Claude Max cache billing code to upstream/main (not part of channel feature) - Revert credits overages logic to upstream/main (non-channel change) - Remove Instructions field reference (non-channel OpenAI feature) - Restore sora_client_handler_test.go from upstream + add channel service nil params
…directly Eliminates unnecessary indirection layer. The wrapper function only called normalizeCodexModel with a special case for "gpt 5.3 codex spark" (space-separated variant) that is no longer needed. All call sites now use normalizeCodexModel directly.
…l feature) Restore account_usage_service.go, antigravity_gateway_service.go, antigravity_credits_overages.go and its test to upstream/main state. These credits balance precheck changes were accidentally included during cherry-pick of channel management commits.
…ions, fix gofmt - Remove unused claudeMax*Tokens constants (Claude Max feature not included) - Remove unused UsageMapHook type, SetUsageMapHook method, and usageToMap function - Fix gofmt formatting in channel_service.go, openai_model_mapping_test.go, chatcompletions_to_responses.go
- applyRequestTierOverrides now uses filterValidIntervals consistently with applyTokenOverrides (per_request/image modes were not filtering) - CostInput accepts optional pre-resolved pricing via Resolved field, eliminating duplicate Resolver.Resolve() calls in gateway billing paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景 / Background
目前系统的计费和模型定价完全依赖上游默认定价(LiteLLM),无法按分组/渠道配置差异化定价策略。管理员无法为不同客户分组设置独立的模型价格、计费模式和模型访问限制。
Currently the system relies entirely on upstream default pricing (LiteLLM) for billing. Administrators cannot configure differentiated pricing strategies per customer group — no custom model prices, billing modes, or model access restrictions.
目的 / Purpose
引入完整的渠道管理系统,支持按分组配置独立的模型定价(token/按次/图片三种模式)、模型映射、模型访问限制,并将渠道定价与网关计费流程深度集成。
Introduce a complete channel management system supporting per-group model pricing (token/per-request/image billing modes), model mapping, model access restriction, with deep integration into the gateway billing pipeline.
改动内容 / Changes
后端 / Backend
CalculateCostUnified替代分散的计费逻辑,支持渠道定价覆盖restrict_models开关,限制分组仅可访问定价列表中的模型*通配符(如claude-*)CalculateCostUnifiedreplaces scattered billing logic, supports channel pricing overriderestrict_modelstoggle limits groups to models in the pricing list only*wildcards (e.g.claude-*)前端 / Frontend