2024-02-29 01:08:18 +08:00
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-31 16:51:05 +08:00
|
|
|
|
"encoding/json"
|
2025-08-14 20:05:06 +08:00
|
|
|
|
"errors"
|
2025-08-14 22:15:18 +08:00
|
|
|
|
"fmt"
|
2026-03-27 17:24:26 +08:00
|
|
|
|
"strconv"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
2024-12-15 15:52:41 +08:00
|
|
|
|
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/constant"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
|
|
|
|
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
2025-12-21 17:09:49 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/setting/model_setting"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/types"
|
|
|
|
|
|
|
2024-12-15 15:52:41 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2024-02-29 01:08:18 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-03-05 19:47:41 +08:00
|
|
|
|
type ThinkingContentInfo struct {
|
|
|
|
|
|
IsFirstThinkingContent bool
|
|
|
|
|
|
SendLastThinkingContent bool
|
2025-03-14 17:09:40 +08:00
|
|
|
|
HasSentThinkingContent bool
|
2025-03-05 19:47:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-13 19:32:08 +08:00
|
|
|
|
const (
|
2025-04-11 18:28:50 +08:00
|
|
|
|
LastMessageTypeNone = "none"
|
|
|
|
|
|
LastMessageTypeText = "text"
|
|
|
|
|
|
LastMessageTypeTools = "tools"
|
|
|
|
|
|
LastMessageTypeThinking = "thinking"
|
2025-03-13 19:32:08 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type ClaudeConvertInfo struct {
|
|
|
|
|
|
LastMessagesType string
|
|
|
|
|
|
Index int
|
2025-04-11 18:28:50 +08:00
|
|
|
|
Usage *dto.Usage
|
|
|
|
|
|
FinishReason string
|
|
|
|
|
|
Done bool
|
2026-02-05 19:32:26 +08:00
|
|
|
|
|
|
|
|
|
|
ToolCallBaseIndex int
|
|
|
|
|
|
ToolCallMaxIndexOffset int
|
2025-03-13 19:32:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-16 21:06:29 +08:00
|
|
|
|
type RerankerInfo struct {
|
2025-03-17 16:44:53 +08:00
|
|
|
|
Documents []any
|
|
|
|
|
|
ReturnDocuments bool
|
2025-03-16 21:06:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-05 00:40:16 +08:00
|
|
|
|
type BuildInToolInfo struct {
|
|
|
|
|
|
ToolName string
|
|
|
|
|
|
CallCount int
|
|
|
|
|
|
SearchContextSize string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ResponsesUsageInfo struct {
|
|
|
|
|
|
BuiltInTools map[string]*BuildInToolInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
type ChannelMeta struct {
|
2025-08-02 22:12:15 +08:00
|
|
|
|
ChannelType int
|
|
|
|
|
|
ChannelId int
|
2025-08-14 20:05:06 +08:00
|
|
|
|
ChannelIsMultiKey bool
|
|
|
|
|
|
ChannelMultiKeyIndex int
|
|
|
|
|
|
ChannelBaseUrl string
|
|
|
|
|
|
ApiType int
|
|
|
|
|
|
ApiVersion string
|
|
|
|
|
|
ApiKey string
|
|
|
|
|
|
Organization string
|
|
|
|
|
|
ChannelCreateTime int64
|
|
|
|
|
|
ParamOverride map[string]interface{}
|
2025-08-24 01:02:23 +08:00
|
|
|
|
HeadersOverride map[string]interface{}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
ChannelSetting dto.ChannelSettings
|
|
|
|
|
|
ChannelOtherSettings dto.ChannelOtherSettings
|
|
|
|
|
|
UpstreamModelName string
|
|
|
|
|
|
IsModelMapped bool
|
2025-08-14 21:10:04 +08:00
|
|
|
|
SupportStreamOptions bool // 是否支持流式选项
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 21:34:39 +08:00
|
|
|
|
type TokenCountMeta struct {
|
|
|
|
|
|
//promptTokens int
|
|
|
|
|
|
estimatePromptTokens int
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
type RelayInfo struct {
|
|
|
|
|
|
TokenId int
|
|
|
|
|
|
TokenKey string
|
2025-12-12 22:04:38 +08:00
|
|
|
|
TokenGroup string
|
2025-08-14 20:05:06 +08:00
|
|
|
|
UserId int
|
2025-12-13 01:38:12 +08:00
|
|
|
|
UsingGroup string // 使用的分组,当auto跨分组重试时,会变动
|
2025-08-14 20:05:06 +08:00
|
|
|
|
UserGroup string // 用户所在分组
|
|
|
|
|
|
TokenUnlimited bool
|
|
|
|
|
|
StartTime time.Time
|
|
|
|
|
|
FirstResponseTime time.Time
|
|
|
|
|
|
isFirstResponse bool
|
2025-03-05 19:47:41 +08:00
|
|
|
|
//SendLastReasoningResponse bool
|
2025-08-09 01:07:48 +08:00
|
|
|
|
IsStream bool
|
|
|
|
|
|
IsGeminiBatchEmbedding bool
|
|
|
|
|
|
IsPlayground bool
|
|
|
|
|
|
UsePrice bool
|
|
|
|
|
|
RelayMode int
|
|
|
|
|
|
OriginModelName string
|
2025-08-15 13:20:36 +08:00
|
|
|
|
RequestURLPath string
|
2026-02-22 00:45:49 +08:00
|
|
|
|
RequestHeaders map[string]string
|
2025-08-15 13:20:36 +08:00
|
|
|
|
ShouldIncludeUsage bool
|
|
|
|
|
|
DisablePing bool // 是否禁止向下游发送自定义 Ping
|
|
|
|
|
|
ClientWs *websocket.Conn
|
|
|
|
|
|
TargetWs *websocket.Conn
|
|
|
|
|
|
InputAudioFormat string
|
|
|
|
|
|
OutputAudioFormat string
|
|
|
|
|
|
RealtimeTools []dto.RealTimeTool
|
|
|
|
|
|
IsFirstRequest bool
|
|
|
|
|
|
AudioUsage bool
|
|
|
|
|
|
ReasoningEffort string
|
|
|
|
|
|
UserSetting dto.UserSetting
|
|
|
|
|
|
UserEmail string
|
|
|
|
|
|
UserQuota int
|
|
|
|
|
|
RelayFormat types.RelayFormat
|
|
|
|
|
|
SendResponseCount int
|
2026-02-05 15:57:17 +08:00
|
|
|
|
ReceivedResponseCount int
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
FinalPreConsumedQuota int // 最终预消耗的配额
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
// ForcePreConsume 为 true 时禁用 BillingSession 的信任额度旁路,
|
|
|
|
|
|
// 强制预扣全额。用于异步任务(视频/音乐生成等),因为请求返回后任务仍在运行,
|
|
|
|
|
|
// 必须在提交前锁定全额。
|
|
|
|
|
|
ForcePreConsume bool
|
2026-02-06 23:14:25 +08:00
|
|
|
|
// Billing 是计费会话,封装了预扣费/结算/退款的统一生命周期。
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
// 免费模型时为 nil。
|
2026-02-06 23:14:25 +08:00
|
|
|
|
Billing BillingSettler
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
// BillingSource indicates whether this request is billed from wallet quota or subscription.
|
|
|
|
|
|
// "" or "wallet" => wallet; "subscription" => subscription
|
|
|
|
|
|
BillingSource string
|
|
|
|
|
|
// SubscriptionId is the user_subscriptions.id used when BillingSource == "subscription"
|
|
|
|
|
|
SubscriptionId int
|
|
|
|
|
|
// SubscriptionPreConsumed is the amount pre-consumed on subscription item (quota units or 1)
|
|
|
|
|
|
SubscriptionPreConsumed int64
|
|
|
|
|
|
// SubscriptionPostDelta is the post-consume delta applied to amount_used (quota units; can be negative).
|
|
|
|
|
|
SubscriptionPostDelta int64
|
|
|
|
|
|
// SubscriptionPlanId / SubscriptionPlanTitle are used for logging/UI display.
|
|
|
|
|
|
SubscriptionPlanId int
|
|
|
|
|
|
SubscriptionPlanTitle string
|
|
|
|
|
|
// RequestId is used for idempotent pre-consume/refund
|
|
|
|
|
|
RequestId string
|
|
|
|
|
|
// SubscriptionAmountTotal / SubscriptionAmountUsedAfterPreConsume are used to compute remaining in logs.
|
|
|
|
|
|
SubscriptionAmountTotal int64
|
|
|
|
|
|
SubscriptionAmountUsedAfterPreConsume int64
|
|
|
|
|
|
IsClaudeBetaQuery bool // /v1/messages?beta=true
|
|
|
|
|
|
IsChannelTest bool // channel test request
|
2026-02-22 00:10:49 +08:00
|
|
|
|
RetryIndex int
|
|
|
|
|
|
LastError *types.NewAPIError
|
2026-02-22 00:45:49 +08:00
|
|
|
|
RuntimeHeadersOverride map[string]interface{}
|
|
|
|
|
|
UseRuntimeHeadersOverride bool
|
2026-03-17 15:47:05 +08:00
|
|
|
|
ParamOverrideAudit []string
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
|
|
|
|
|
PriceData types.PriceData
|
|
|
|
|
|
|
|
|
|
|
|
Request dto.Request
|
|
|
|
|
|
|
2026-01-20 23:43:29 +08:00
|
|
|
|
// RequestConversionChain records request format conversions in order, e.g.
|
|
|
|
|
|
// ["openai", "openai_responses"] or ["openai", "claude"].
|
|
|
|
|
|
RequestConversionChain []types.RelayFormat
|
2026-02-22 23:30:02 +08:00
|
|
|
|
// 最终请求到上游的格式。可由 adaptor 显式设置;
|
|
|
|
|
|
// 若为空,调用 GetFinalRequestRelayFormat 会回退到 RequestConversionChain 的最后一项或 RelayFormat。
|
2026-02-07 14:21:19 +08:00
|
|
|
|
FinalRequestRelayFormat types.RelayFormat
|
2026-01-20 23:43:29 +08:00
|
|
|
|
|
2026-03-31 16:50:24 +08:00
|
|
|
|
StreamStatus *StreamStatus
|
|
|
|
|
|
|
2025-03-05 19:47:41 +08:00
|
|
|
|
ThinkingContentInfo
|
2025-12-02 21:34:39 +08:00
|
|
|
|
TokenCountMeta
|
2025-04-15 02:32:51 +08:00
|
|
|
|
*ClaudeConvertInfo
|
2025-03-16 21:06:29 +08:00
|
|
|
|
*RerankerInfo
|
2025-05-05 00:40:16 +08:00
|
|
|
|
*ResponsesUsageInfo
|
2025-08-14 20:05:06 +08:00
|
|
|
|
*ChannelMeta
|
2025-08-25 18:01:10 +08:00
|
|
|
|
*TaskRelayInfo
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (info *RelayInfo) InitChannelMeta(c *gin.Context) {
|
|
|
|
|
|
channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
|
|
|
|
|
|
paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
|
2025-08-24 01:02:23 +08:00
|
|
|
|
headerOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelHeaderOverride)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
apiType, _ := common.ChannelType2APIType(channelType)
|
|
|
|
|
|
channelMeta := &ChannelMeta{
|
|
|
|
|
|
ChannelType: channelType,
|
|
|
|
|
|
ChannelId: common.GetContextKeyInt(c, constant.ContextKeyChannelId),
|
|
|
|
|
|
ChannelIsMultiKey: common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey),
|
|
|
|
|
|
ChannelMultiKeyIndex: common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex),
|
|
|
|
|
|
ChannelBaseUrl: common.GetContextKeyString(c, constant.ContextKeyChannelBaseUrl),
|
|
|
|
|
|
ApiType: apiType,
|
|
|
|
|
|
ApiVersion: c.GetString("api_version"),
|
|
|
|
|
|
ApiKey: common.GetContextKeyString(c, constant.ContextKeyChannelKey),
|
|
|
|
|
|
Organization: c.GetString("channel_organization"),
|
|
|
|
|
|
ChannelCreateTime: c.GetInt64("channel_create_time"),
|
|
|
|
|
|
ParamOverride: paramOverride,
|
2025-08-24 01:02:23 +08:00
|
|
|
|
HeadersOverride: headerOverride,
|
2025-08-14 20:05:06 +08:00
|
|
|
|
UpstreamModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
|
|
|
|
|
|
IsModelMapped: false,
|
2025-08-14 21:10:04 +08:00
|
|
|
|
SupportStreamOptions: false,
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-16 14:56:19 +08:00
|
|
|
|
if channelType == constant.ChannelTypeAzure {
|
|
|
|
|
|
channelMeta.ApiVersion = GetAPIVersion(c)
|
|
|
|
|
|
}
|
|
|
|
|
|
if channelType == constant.ChannelTypeVertexAi {
|
|
|
|
|
|
channelMeta.ApiVersion = c.GetString("region")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
channelSetting, ok := common.GetContextKeyType[dto.ChannelSettings](c, constant.ContextKeyChannelSetting)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
channelMeta.ChannelSetting = channelSetting
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
channelOtherSettings, ok := common.GetContextKeyType[dto.ChannelOtherSettings](c, constant.ContextKeyChannelOtherSetting)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
channelMeta.ChannelOtherSettings = channelOtherSettings
|
|
|
|
|
|
}
|
2025-08-14 21:10:04 +08:00
|
|
|
|
|
|
|
|
|
|
if streamSupportedChannels[channelMeta.ChannelType] {
|
|
|
|
|
|
channelMeta.SupportStreamOptions = true
|
|
|
|
|
|
}
|
2025-08-23 13:12:15 +08:00
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
info.ChannelMeta = channelMeta
|
2025-08-23 13:12:15 +08:00
|
|
|
|
|
|
|
|
|
|
// reset some fields based on channel meta
|
|
|
|
|
|
// 重置某些字段,例如模型名称等
|
|
|
|
|
|
if info.Request != nil {
|
|
|
|
|
|
info.Request.SetModelName(info.OriginModelName)
|
|
|
|
|
|
}
|
2024-10-04 16:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 22:15:18 +08:00
|
|
|
|
func (info *RelayInfo) ToString() string {
|
|
|
|
|
|
if info == nil {
|
|
|
|
|
|
return "RelayInfo<nil>"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Basic info
|
|
|
|
|
|
b := &strings.Builder{}
|
|
|
|
|
|
fmt.Fprintf(b, "RelayInfo{ ")
|
|
|
|
|
|
fmt.Fprintf(b, "RelayFormat: %s, ", info.RelayFormat)
|
|
|
|
|
|
fmt.Fprintf(b, "RelayMode: %d, ", info.RelayMode)
|
|
|
|
|
|
fmt.Fprintf(b, "IsStream: %t, ", info.IsStream)
|
|
|
|
|
|
fmt.Fprintf(b, "IsPlayground: %t, ", info.IsPlayground)
|
|
|
|
|
|
fmt.Fprintf(b, "RequestURLPath: %q, ", info.RequestURLPath)
|
|
|
|
|
|
fmt.Fprintf(b, "OriginModelName: %q, ", info.OriginModelName)
|
2025-12-02 21:34:39 +08:00
|
|
|
|
fmt.Fprintf(b, "EstimatePromptTokens: %d, ", info.estimatePromptTokens)
|
2025-08-14 22:15:18 +08:00
|
|
|
|
fmt.Fprintf(b, "ShouldIncludeUsage: %t, ", info.ShouldIncludeUsage)
|
|
|
|
|
|
fmt.Fprintf(b, "DisablePing: %t, ", info.DisablePing)
|
|
|
|
|
|
fmt.Fprintf(b, "SendResponseCount: %d, ", info.SendResponseCount)
|
|
|
|
|
|
fmt.Fprintf(b, "FinalPreConsumedQuota: %d, ", info.FinalPreConsumedQuota)
|
|
|
|
|
|
|
|
|
|
|
|
// User & token info (mask secrets)
|
|
|
|
|
|
fmt.Fprintf(b, "User{ Id: %d, Email: %q, Group: %q, UsingGroup: %q, Quota: %d }, ",
|
2025-08-15 12:50:27 +08:00
|
|
|
|
info.UserId, common.MaskEmail(info.UserEmail), info.UserGroup, info.UsingGroup, info.UserQuota)
|
2025-08-14 22:15:18 +08:00
|
|
|
|
fmt.Fprintf(b, "Token{ Id: %d, Unlimited: %t, Key: ***masked*** }, ", info.TokenId, info.TokenUnlimited)
|
|
|
|
|
|
|
|
|
|
|
|
// Time info
|
|
|
|
|
|
latencyMs := info.FirstResponseTime.Sub(info.StartTime).Milliseconds()
|
|
|
|
|
|
fmt.Fprintf(b, "Timing{ Start: %s, FirstResponse: %s, LatencyMs: %d }, ",
|
|
|
|
|
|
info.StartTime.Format(time.RFC3339Nano), info.FirstResponseTime.Format(time.RFC3339Nano), latencyMs)
|
|
|
|
|
|
|
|
|
|
|
|
// Audio / realtime
|
|
|
|
|
|
if info.InputAudioFormat != "" || info.OutputAudioFormat != "" || len(info.RealtimeTools) > 0 || info.AudioUsage {
|
|
|
|
|
|
fmt.Fprintf(b, "Realtime{ AudioUsage: %t, InFmt: %q, OutFmt: %q, Tools: %d }, ",
|
|
|
|
|
|
info.AudioUsage, info.InputAudioFormat, info.OutputAudioFormat, len(info.RealtimeTools))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reasoning
|
|
|
|
|
|
if info.ReasoningEffort != "" {
|
|
|
|
|
|
fmt.Fprintf(b, "ReasoningEffort: %q, ", info.ReasoningEffort)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Price data (non-sensitive)
|
|
|
|
|
|
if info.PriceData.UsePrice {
|
|
|
|
|
|
fmt.Fprintf(b, "PriceData{ %s }, ", info.PriceData.ToSetting())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Channel metadata (mask ApiKey)
|
|
|
|
|
|
if info.ChannelMeta != nil {
|
|
|
|
|
|
cm := info.ChannelMeta
|
|
|
|
|
|
fmt.Fprintf(b, "ChannelMeta{ Type: %d, Id: %d, IsMultiKey: %t, MultiKeyIndex: %d, BaseURL: %q, ApiType: %d, ApiVersion: %q, Organization: %q, CreateTime: %d, UpstreamModelName: %q, IsModelMapped: %t, SupportStreamOptions: %t, ApiKey: ***masked*** }, ",
|
|
|
|
|
|
cm.ChannelType, cm.ChannelId, cm.ChannelIsMultiKey, cm.ChannelMultiKeyIndex, cm.ChannelBaseUrl, cm.ApiType, cm.ApiVersion, cm.Organization, cm.ChannelCreateTime, cm.UpstreamModelName, cm.IsModelMapped, cm.SupportStreamOptions)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Responses usage info (non-sensitive)
|
|
|
|
|
|
if info.ResponsesUsageInfo != nil && len(info.ResponsesUsageInfo.BuiltInTools) > 0 {
|
|
|
|
|
|
fmt.Fprintf(b, "ResponsesTools{ ")
|
|
|
|
|
|
first := true
|
|
|
|
|
|
for name, tool := range info.ResponsesUsageInfo.BuiltInTools {
|
|
|
|
|
|
if !first {
|
|
|
|
|
|
fmt.Fprintf(b, ", ")
|
|
|
|
|
|
}
|
|
|
|
|
|
first = false
|
|
|
|
|
|
if tool != nil {
|
|
|
|
|
|
fmt.Fprintf(b, "%s: calls=%d", name, tool.CallCount)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
fmt.Fprintf(b, "%s: calls=0", name)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Fprintf(b, " }, ")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(b, "}")
|
|
|
|
|
|
return b.String()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-20 16:41:46 +08:00
|
|
|
|
// 定义支持流式选项的通道类型
|
|
|
|
|
|
var streamSupportedChannels = map[int]bool{
|
2026-02-11 01:39:01 +08:00
|
|
|
|
constant.ChannelTypeOpenAI: true,
|
|
|
|
|
|
constant.ChannelTypeAnthropic: true,
|
|
|
|
|
|
constant.ChannelTypeAws: true,
|
|
|
|
|
|
constant.ChannelTypeGemini: true,
|
|
|
|
|
|
constant.ChannelCloudflare: true,
|
|
|
|
|
|
constant.ChannelTypeAzure: true,
|
|
|
|
|
|
constant.ChannelTypeVolcEngine: true,
|
|
|
|
|
|
constant.ChannelTypeOllama: true,
|
|
|
|
|
|
constant.ChannelTypeXai: true,
|
|
|
|
|
|
constant.ChannelTypeDeepSeek: true,
|
|
|
|
|
|
constant.ChannelTypeBaiduV2: true,
|
|
|
|
|
|
constant.ChannelTypeZhipu_v4: true,
|
|
|
|
|
|
constant.ChannelTypeAli: true,
|
|
|
|
|
|
constant.ChannelTypeSubmodel: true,
|
|
|
|
|
|
constant.ChannelTypeCodex: true,
|
|
|
|
|
|
constant.ChannelTypeMoonshot: true,
|
|
|
|
|
|
constant.ChannelTypeMiniMax: true,
|
|
|
|
|
|
constant.ChannelTypeSiliconFlow: true,
|
2025-02-20 16:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-04 16:08:18 +08:00
|
|
|
|
func GenRelayInfoWs(c *gin.Context, ws *websocket.Conn) *RelayInfo {
|
2025-08-14 20:05:06 +08:00
|
|
|
|
info := genBaseRelayInfo(c, nil)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAIRealtime
|
2024-10-04 16:08:18 +08:00
|
|
|
|
info.ClientWs = ws
|
2024-10-06 14:13:41 +08:00
|
|
|
|
info.InputAudioFormat = "pcm16"
|
|
|
|
|
|
info.OutputAudioFormat = "pcm16"
|
|
|
|
|
|
info.IsFirstRequest = true
|
2024-10-04 16:08:18 +08:00
|
|
|
|
return info
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoClaude(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatClaude
|
2025-03-12 21:31:46 +08:00
|
|
|
|
info.ShouldIncludeUsage = false
|
2025-04-15 02:32:51 +08:00
|
|
|
|
info.ClaudeConvertInfo = &ClaudeConvertInfo{
|
2025-04-11 18:28:50 +08:00
|
|
|
|
LastMessagesType: LastMessageTypeNone,
|
2025-03-13 19:32:08 +08:00
|
|
|
|
}
|
2026-03-19 15:49:50 +08:00
|
|
|
|
info.IsClaudeBetaQuery = c.Query("beta") == "true"
|
2025-03-12 21:31:46 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoRerank(c *gin.Context, request *dto.RerankRequest) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
2025-03-16 21:06:29 +08:00
|
|
|
|
info.RelayMode = relayconstant.RelayModeRerank
|
2025-08-14 20:05:06 +08:00
|
|
|
|
info.RelayFormat = types.RelayFormatRerank
|
2025-03-16 21:06:29 +08:00
|
|
|
|
info.RerankerInfo = &RerankerInfo{
|
2025-08-14 20:05:06 +08:00
|
|
|
|
Documents: request.Documents,
|
|
|
|
|
|
ReturnDocuments: request.GetReturnDocuments(),
|
2025-03-16 21:06:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoOpenAIAudio(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAIAudio
|
2025-06-20 16:02:23 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoEmbedding(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatEmbedding
|
2025-06-20 16:02:23 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoResponses(c *gin.Context, request *dto.OpenAIResponsesRequest) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
2025-05-05 00:40:16 +08:00
|
|
|
|
info.RelayMode = relayconstant.RelayModeResponses
|
2025-08-14 20:05:06 +08:00
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAIResponses
|
2025-06-20 16:02:23 +08:00
|
|
|
|
|
2025-05-05 00:40:16 +08:00
|
|
|
|
info.ResponsesUsageInfo = &ResponsesUsageInfo{
|
|
|
|
|
|
BuiltInTools: make(map[string]*BuildInToolInfo),
|
|
|
|
|
|
}
|
2025-08-28 15:11:55 +08:00
|
|
|
|
if len(request.Tools) > 0 {
|
2025-08-26 13:17:31 +08:00
|
|
|
|
for _, tool := range request.GetToolsMap() {
|
2025-07-20 11:21:57 +08:00
|
|
|
|
toolType := common.Interface2String(tool["type"])
|
|
|
|
|
|
info.ResponsesUsageInfo.BuiltInTools[toolType] = &BuildInToolInfo{
|
|
|
|
|
|
ToolName: toolType,
|
2025-05-05 00:40:16 +08:00
|
|
|
|
CallCount: 0,
|
|
|
|
|
|
}
|
2025-07-20 11:21:57 +08:00
|
|
|
|
switch toolType {
|
2025-05-05 00:40:16 +08:00
|
|
|
|
case dto.BuildInToolWebSearchPreview:
|
2025-07-20 11:21:57 +08:00
|
|
|
|
searchContextSize := common.Interface2String(tool["search_context_size"])
|
|
|
|
|
|
if searchContextSize == "" {
|
|
|
|
|
|
searchContextSize = "medium"
|
2025-05-05 00:40:16 +08:00
|
|
|
|
}
|
2025-07-20 11:21:57 +08:00
|
|
|
|
info.ResponsesUsageInfo.BuiltInTools[toolType].SearchContextSize = searchContextSize
|
2025-05-05 00:40:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoGemini(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatGemini
|
2025-06-20 16:02:23 +08:00
|
|
|
|
info.ShouldIncludeUsage = false
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
2025-06-20 16:02:23 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoImage(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAIImage
|
2025-06-20 16:02:23 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfoOpenAI(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAI
|
|
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
|
|
|
|
|
|
|
|
|
|
|
|
//channelType := common.GetContextKeyInt(c, constant.ContextKeyChannelType)
|
|
|
|
|
|
//channelId := common.GetContextKeyInt(c, constant.ContextKeyChannelId)
|
|
|
|
|
|
//paramOverride := common.GetContextKeyStringMap(c, constant.ContextKeyChannelParamOverride)
|
2024-04-04 16:35:44 +08:00
|
|
|
|
|
2025-12-13 01:38:12 +08:00
|
|
|
|
tokenGroup := common.GetContextKeyString(c, constant.ContextKeyTokenGroup)
|
|
|
|
|
|
// 当令牌分组为空时,表示使用用户分组
|
|
|
|
|
|
if tokenGroup == "" {
|
|
|
|
|
|
tokenGroup = common.GetContextKeyString(c, constant.ContextKeyUserGroup)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-03 13:10:25 +08:00
|
|
|
|
startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
|
2025-08-06 22:58:36 +08:00
|
|
|
|
if startTime.IsZero() {
|
|
|
|
|
|
startTime = time.Now()
|
|
|
|
|
|
}
|
2024-02-29 01:08:18 +08:00
|
|
|
|
|
2025-08-14 21:10:04 +08:00
|
|
|
|
isStream := false
|
|
|
|
|
|
|
|
|
|
|
|
if request != nil {
|
|
|
|
|
|
isStream = request.IsStream(c)
|
|
|
|
|
|
}
|
2026-04-12 17:41:26 +08:00
|
|
|
|
c.Set(string(constant.ContextKeyIsStream), isStream)
|
2025-08-14 21:10:04 +08:00
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
// firstResponseTime = time.Now() - 1 second
|
2024-02-29 01:08:18 +08:00
|
|
|
|
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
reqId := common.GetContextKeyString(c, common.RequestIdKey)
|
|
|
|
|
|
if reqId == "" {
|
|
|
|
|
|
reqId = common.GetTimeString() + common.GetRandomString(8)
|
|
|
|
|
|
}
|
2024-02-29 01:08:18 +08:00
|
|
|
|
info := &RelayInfo{
|
2025-08-14 20:05:06 +08:00
|
|
|
|
Request: request,
|
|
|
|
|
|
|
✨ feat: add subscription billing system (#2808)
* ci: create docker automation
* ✨ feat: add subscription billing system with admin management and user purchase flow
Implement a new subscription-based billing model alongside existing metered/per-request billing:
Backend:
- Add subscription plan models (SubscriptionPlan, SubscriptionPlanItem, UserSubscription, etc.)
- Implement CRUD APIs for subscription plan management (admin only)
- Add user subscription queries with support for multiple active/expired subscriptions
- Integrate payment gateways (Stripe, Creem, Epay) for subscription purchases
- Implement pre-consume and post-consume billing logic for subscription quota tracking
- Add billing preference settings (subscription_first, wallet_first, etc.)
- Enhance usage logs with subscription deduction details
Frontend - Admin:
- Add subscription management page with table view and drawer-based edit form
- Match UI/UX style with existing admin pages (redemption codes, users)
- Support enabling/disabling plans, configuring payment IDs, and model quotas
- Add user subscription binding modal in user management
Frontend - Wallet:
- Add subscription plans card with current subscription status display
- Show all subscriptions (active and expired) with remaining days/usage percentage
- Display purchasable plans with pricing cards following SaaS best practices
- Extract purchase modal to separate component matching payment confirm modal style
- Add skeleton loading states with active animation
- Implement billing preference selector in card header
- Handle payment gateway availability based on admin configuration
Frontend - Usage Logs:
- Display subscription deduction details in log entries
- Show step-by-step breakdown of subscription usage (pre-consumed, delta, final, remaining)
- Add subscription deduction tag for subscription-covered requests
* ✨ feat(admin): add user subscription management and refine UI/pagination
Add admin APIs to list/create/invalidate/delete user subscriptions
Add model helpers to fetch all user subscriptions (incl. expired) and support cancel/hard-delete
Wire new admin routes for user subscription operations
Replace “Bind subscription plan” entry with a dedicated User Subscriptions SideSheet in Users table
Use CardTable with responsive layout and working client-side pagination inside the SideSheet
Improve subscription purchase modal empty-gateway state with a Banner notice
* ✨ feat(admin): streamline subscription plan benefits editor with bulk actions
Restore the avatar/icon header for the “Model Benefits” section
Replace scattered controls with a compact toolbar-style workflow
Support multi-select add with a default quota for new items
Add row selection with bulk apply-to-selected / apply-to-all quota updates
Enable delete-selected to manage benefits faster and reduce mistakes
* ✨ fix(subscription): finalize payments, log billing, and clean up dead code
Complete subscription orders by creating a matching top-up record and writing billing logs
Add Epay return handler to verify and finalize browser callbacks
Require Stripe/Creem webhook configuration before starting subscription payments
Show subscription purchases in topup history with clearer labels/methods
Remove unused subscription helper, legacy Creem webhook struct, and unused topup fields
Simplify subscription self API payload to active/all lists only
* 🎨 style: format all code with gofmt and lint:fix
Apply consistent code formatting across the entire codebase using
gofmt and lint:fix tools. This ensures adherence to Go community
standards and improves code readability and maintainability.
Changes include:
- Run gofmt on all .go files to standardize formatting
- Apply lint:fix to automatically resolve linting issues
- Fix code style inconsistencies and formatting violations
No functional changes were made in this commit.
* ✨ feat(subscription): add quota reset periods and admin configuration
- Add reset period fields on subscription plans and user items
- Apply automatic quota resets during pre-consume based on plan schedule
- Expose reset-period configuration in the admin plan editor
- Display reset cadence in subscription cards and purchase modal
- Validate custom reset seconds on plan create/update
* ✨ feat(subscription): harden subscription billing with resets, idempotency, and production-grade stability
Add plan-level quota reset periods and display/reset cadence in admin/UI
Enforce natural reset alignment with background reset task and cleanup job
Make subscription pre-consume/refund idempotent with request-scoped records and retries
Use database time for consistent resets across multi-instance deployments
Harden payment callbacks with locking and idempotent order completion
Record subscription purchases in topup history and billing logs
Optimize subscription queries and add critical composite indexes
* ✨ feat(subscription): cache plan lookups and stabilize pre-consume
Introduce hybrid caches for subscription plans, items, and plan info with explicit
invalidation on admin updates. Streamline pre-consume transactions to reduce
redundant queries while preserving idempotency and reset logic.
* 🐛 fix(subscription): avoid pre-consume lookup noise
Use a RowsAffected check for the idempotency lookup so missing records
no longer surface as "record not found" errors while preserving behavior.
* 🔧 ci: Change workflow trigger to sub branch
Update the Docker image workflow to run on pushes to the sub branch instead of main.
* 💸 chore: Align subscription pricing display with global currency settings
Unify subscription price rendering to use the site-wide currency symbol/rate on the wallet and admin views.
Make subscription plan currency read-only in the editor and force USD on create/update to avoid drift.
Use global currency display type when creating Creem checkout payloads.
* 🔧 chore: Unify subscription plan status toggle with PATCH endpoint
Replace separate enable/disable flows with a single PATCH API that updates the enabled flag.
Update frontend hooks and table actions to call the unified endpoint and keep UI behavior consistent.
Introduce a minimal admin controller handler and route for the status update.
* ✨ feat: Add subscription limits and UI tags consistency
Add per-plan purchase limits with backend enforcement and UI disable states.
Expose limit configuration in admin plan editor and show limits in plan tables/cards.
Refine subscription UI tags with unified badge style and streamlined “My Subscriptions” layout.
* 🎨 style: tag color to white
* 🚀 refactor: Simplify subscription quota to total amount model
Remove per-model subscription items and switch to a single total quota per plan and user subscription. Update billing, reset, and logging flows to operate on total quota, and refactor admin/user UI to configure and display total quota consistently.
* 🚀 chore: Remove duplicate subscription usage percentage display
Keep the usage percentage shown only in the total quota line to avoid redundant “已用 0%” text while preserving remaining days in the summary.
* ✨ feat: Add subscription upgrade group with auto downgrade
* ✨ feat: Update subscription purchase modal display
Show total quota as currency with tooltip for raw quota, hide reset cycle when never, and display upgrade group when configured to match card display rules.
* ✨ feat: Extract quota conversion helpers to shared utils
Move quota display/conversion helpers into web/src/helpers/quota.js and update the subscription plan editor to import and use the shared utilities instead of inline functions.
* ✨ chore: Add upgrade group guidance in subscription editor
Add explanatory helper text under the upgrade group field to clarify automatic group upgrades, rollback conditions, and the expected delay before downgrading takes effect.
* 🔧 chore: remove unused Creem settings state
Drop the unused originInputs state and redundant updates to keep the Creem
settings form state minimal and easier to maintain.
* 🚀 chore: Remove useless action
* ✨ Add full i18n coverage for subscription-related UI across locales
* ✨ feat: harden subscription billing and improve UI consistency
Improve subscription payment safety and data integrity by handling user/URL lookup failures, fixing Stripe subscription mode, persisting quota reset fields, and correcting subscription delta accounting and DB timestamp casting. Refine the UI with stricter custom duration validation, accurate currency rounding, conditional Epay labeling, rollback on preference update failure, and shared subscription formatting helpers plus clearer component naming.
* 🔧 fix: make epay webhook and return flow subscription-aware
Ensure Epay webhook acknowledges success only after order completion, returning fail on processing errors to allow retries. Redirect subscription payment returns to the subscription page instead of top-up for correct user flow.
* 🚦 fix: guard epay return success on order completion
Redirect subscription return flow to failure when order completion fails, preventing false success states after payment verification.
* 🔧 fix: normalize epay error handling and webhook retries
Standardize SubscriptionRequestEpay error responses via ApiErrorMsg for a consistent schema.
Return "fail" on non-success trade statuses in the epay webhook to preserve retry behavior.
* 🧾 fix: persist epay orders before purchase
Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
* 🔧 fix: harden epay callbacks and billing fallbacks
Use POST and form parsing for epay notify/return routes, persist epay orders before provider calls with expiry on failure, and ensure notify handlers retry correctly.
Restrict subscription-first fallback to insufficient-subscription errors and log refund failures after retries to avoid silent quota drift.
* 🔧 fix: harden billing flow and sidebar settings
Add missing strings import for subscription fallback checks, log failed subscription refunds after retries, and extend sidebar module settings with a subscription management toggle plus translations.
* 🛡️ fix: fail fast on epay form parse errors
Handle ParseForm errors in epay notify/return handlers by returning fail or redirecting to failure, avoiding unsafe fallback to query parameters.
* ✨ fix: refine Japanese subscription status labels
Adjust Japanese UI wording for active-count labels to read more naturally and consistently.
* ✅ fix: standardize epay success response schema
Return subscription epay pay success responses via ApiSuccess to include the consistent success field and align with error schema.
2026-02-03 17:40:43 +08:00
|
|
|
|
RequestId: reqId,
|
2025-08-14 20:05:06 +08:00
|
|
|
|
UserId: common.GetContextKeyInt(c, constant.ContextKeyUserId),
|
|
|
|
|
|
UsingGroup: common.GetContextKeyString(c, constant.ContextKeyUsingGroup),
|
|
|
|
|
|
UserGroup: common.GetContextKeyString(c, constant.ContextKeyUserGroup),
|
|
|
|
|
|
UserQuota: common.GetContextKeyInt(c, constant.ContextKeyUserQuota),
|
|
|
|
|
|
UserEmail: common.GetContextKeyString(c, constant.ContextKeyUserEmail),
|
|
|
|
|
|
|
|
|
|
|
|
OriginModelName: common.GetContextKeyString(c, constant.ContextKeyOriginalModel),
|
|
|
|
|
|
|
|
|
|
|
|
TokenId: common.GetContextKeyInt(c, constant.ContextKeyTokenId),
|
|
|
|
|
|
TokenKey: common.GetContextKeyString(c, constant.ContextKeyTokenKey),
|
|
|
|
|
|
TokenUnlimited: common.GetContextKeyBool(c, constant.ContextKeyTokenUnlimited),
|
2025-12-13 01:38:12 +08:00
|
|
|
|
TokenGroup: tokenGroup,
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
|
|
|
|
|
isFirstResponse: true,
|
|
|
|
|
|
RelayMode: relayconstant.Path2RelayMode(c.Request.URL.Path),
|
|
|
|
|
|
RequestURLPath: c.Request.URL.String(),
|
2026-02-22 00:45:49 +08:00
|
|
|
|
RequestHeaders: cloneRequestHeaders(c),
|
2025-08-14 21:10:04 +08:00
|
|
|
|
IsStream: isStream,
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
2024-06-27 00:16:39 +08:00
|
|
|
|
StartTime: startTime,
|
|
|
|
|
|
FirstResponseTime: startTime.Add(-time.Second),
|
2025-03-05 19:47:41 +08:00
|
|
|
|
ThinkingContentInfo: ThinkingContentInfo{
|
|
|
|
|
|
IsFirstThinkingContent: true,
|
|
|
|
|
|
SendLastThinkingContent: false,
|
|
|
|
|
|
},
|
2025-12-02 21:34:39 +08:00
|
|
|
|
TokenCountMeta: TokenCountMeta{
|
|
|
|
|
|
//promptTokens: common.GetContextKeyInt(c, constant.ContextKeyPromptTokens),
|
|
|
|
|
|
estimatePromptTokens: common.GetContextKeyInt(c, constant.ContextKeyEstimatedTokens),
|
|
|
|
|
|
},
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
|
2025-08-25 18:01:10 +08:00
|
|
|
|
if info.RelayMode == relayconstant.RelayModeUnknown {
|
|
|
|
|
|
info.RelayMode = c.GetInt("relay_mode")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-26 00:59:09 +08:00
|
|
|
|
if strings.HasPrefix(c.Request.URL.Path, "/pg") {
|
|
|
|
|
|
info.IsPlayground = true
|
|
|
|
|
|
info.RequestURLPath = strings.TrimPrefix(info.RequestURLPath, "/pg")
|
|
|
|
|
|
info.RequestURLPath = "/v1" + info.RequestURLPath
|
|
|
|
|
|
}
|
2025-08-10 20:21:30 +08:00
|
|
|
|
|
2025-07-07 14:26:37 +08:00
|
|
|
|
userSetting, ok := common.GetContextKeyType[dto.UserSetting](c, constant.ContextKeyUserSetting)
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
info.UserSetting = userSetting
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-29 01:08:18 +08:00
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-22 00:45:49 +08:00
|
|
|
|
func cloneRequestHeaders(c *gin.Context) map[string]string {
|
|
|
|
|
|
if c == nil || c.Request == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(c.Request.Header) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
headers := make(map[string]string, len(c.Request.Header))
|
|
|
|
|
|
for key := range c.Request.Header {
|
|
|
|
|
|
value := strings.TrimSpace(c.Request.Header.Get(key))
|
|
|
|
|
|
if value == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
headers[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(headers) == 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return headers
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 20:05:06 +08:00
|
|
|
|
func GenRelayInfo(c *gin.Context, relayFormat types.RelayFormat, request dto.Request, ws *websocket.Conn) (*RelayInfo, error) {
|
2026-01-20 23:43:29 +08:00
|
|
|
|
var info *RelayInfo
|
|
|
|
|
|
var err error
|
2025-08-14 20:05:06 +08:00
|
|
|
|
switch relayFormat {
|
|
|
|
|
|
case types.RelayFormatOpenAI:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoOpenAI(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatOpenAIAudio:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoOpenAIAudio(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatOpenAIImage:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoImage(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatOpenAIRealtime:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoWs(c, ws)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatClaude:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoClaude(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatRerank:
|
|
|
|
|
|
if request, ok := request.(*dto.RerankRequest); ok {
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoRerank(c, request)
|
|
|
|
|
|
break
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
2026-01-20 23:43:29 +08:00
|
|
|
|
err = errors.New("request is not a RerankRequest")
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatGemini:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoGemini(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatEmbedding:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoEmbedding(c, request)
|
2025-08-14 20:05:06 +08:00
|
|
|
|
case types.RelayFormatOpenAIResponses:
|
|
|
|
|
|
if request, ok := request.(*dto.OpenAIResponsesRequest); ok {
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = GenRelayInfoResponses(c, request)
|
|
|
|
|
|
break
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
2026-01-20 23:43:29 +08:00
|
|
|
|
err = errors.New("request is not a OpenAIResponsesRequest")
|
2026-01-26 20:20:16 +08:00
|
|
|
|
case types.RelayFormatOpenAIResponsesCompaction:
|
|
|
|
|
|
if request, ok := request.(*dto.OpenAIResponsesCompactionRequest); ok {
|
|
|
|
|
|
return GenRelayInfoResponsesCompaction(c, request), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, errors.New("request is not a OpenAIResponsesCompactionRequest")
|
2025-08-14 21:10:04 +08:00
|
|
|
|
case types.RelayFormatTask:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = genBaseRelayInfo(c, nil)
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
info.TaskRelayInfo = &TaskRelayInfo{}
|
2025-08-14 21:10:04 +08:00
|
|
|
|
case types.RelayFormatMjProxy:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info = genBaseRelayInfo(c, nil)
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
info.TaskRelayInfo = &TaskRelayInfo{}
|
2025-08-14 20:05:06 +08:00
|
|
|
|
default:
|
2026-01-20 23:43:29 +08:00
|
|
|
|
err = errors.New("invalid relay format")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if info == nil {
|
|
|
|
|
|
return nil, errors.New("failed to build relay info")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
info.InitRequestConversionChain()
|
|
|
|
|
|
return info, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (info *RelayInfo) InitRequestConversionChain() {
|
|
|
|
|
|
if info == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(info.RequestConversionChain) > 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if info.RelayFormat == "" {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
info.RequestConversionChain = []types.RelayFormat{info.RelayFormat}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (info *RelayInfo) AppendRequestConversion(format types.RelayFormat) {
|
|
|
|
|
|
if info == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if format == "" {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(info.RequestConversionChain) == 0 {
|
|
|
|
|
|
info.RequestConversionChain = []types.RelayFormat{format}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
last := info.RequestConversionChain[len(info.RequestConversionChain)-1]
|
|
|
|
|
|
if last == format {
|
|
|
|
|
|
return
|
2025-08-14 20:05:06 +08:00
|
|
|
|
}
|
2026-01-20 23:43:29 +08:00
|
|
|
|
info.RequestConversionChain = append(info.RequestConversionChain, format)
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-22 23:30:02 +08:00
|
|
|
|
func (info *RelayInfo) GetFinalRequestRelayFormat() types.RelayFormat {
|
|
|
|
|
|
if info == nil {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
if info.FinalRequestRelayFormat != "" {
|
|
|
|
|
|
return info.FinalRequestRelayFormat
|
|
|
|
|
|
}
|
|
|
|
|
|
if n := len(info.RequestConversionChain); n > 0 {
|
|
|
|
|
|
return info.RequestConversionChain[n-1]
|
|
|
|
|
|
}
|
|
|
|
|
|
return info.RelayFormat
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 20:20:16 +08:00
|
|
|
|
func GenRelayInfoResponsesCompaction(c *gin.Context, request *dto.OpenAIResponsesCompactionRequest) *RelayInfo {
|
|
|
|
|
|
info := genBaseRelayInfo(c, request)
|
|
|
|
|
|
if info.RelayMode == relayconstant.RelayModeUnknown {
|
|
|
|
|
|
info.RelayMode = relayconstant.RelayModeResponsesCompact
|
|
|
|
|
|
}
|
|
|
|
|
|
info.RelayFormat = types.RelayFormatOpenAIResponsesCompaction
|
|
|
|
|
|
return info
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 21:34:39 +08:00
|
|
|
|
//func (info *RelayInfo) SetPromptTokens(promptTokens int) {
|
|
|
|
|
|
// info.promptTokens = promptTokens
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
func (info *RelayInfo) SetEstimatePromptTokens(promptTokens int) {
|
|
|
|
|
|
info.estimatePromptTokens = promptTokens
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (info *RelayInfo) GetEstimatePromptTokens() int {
|
|
|
|
|
|
return info.estimatePromptTokens
|
2024-02-29 01:08:18 +08:00
|
|
|
|
}
|
2024-06-12 20:37:42 +08:00
|
|
|
|
|
2024-07-15 18:14:07 +08:00
|
|
|
|
func (info *RelayInfo) SetFirstResponseTime() {
|
2025-03-05 19:47:41 +08:00
|
|
|
|
if info.isFirstResponse {
|
2024-07-15 18:14:07 +08:00
|
|
|
|
info.FirstResponseTime = time.Now()
|
2025-03-05 19:47:41 +08:00
|
|
|
|
info.isFirstResponse = false
|
2024-07-15 18:14:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-14 19:40:23 +08:00
|
|
|
|
func (info *RelayInfo) HasSendResponse() bool {
|
|
|
|
|
|
return info.FirstResponseTime.After(info.StartTime)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-12 20:37:42 +08:00
|
|
|
|
type TaskRelayInfo struct {
|
|
|
|
|
|
Action string
|
|
|
|
|
|
OriginTaskID string
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
// PublicTaskID 是提交时预生成的 task_xxxx 格式公开 ID,
|
|
|
|
|
|
// 供 DoResponse 在返回给客户端时使用(避免暴露上游真实 ID)。
|
|
|
|
|
|
PublicTaskID string
|
2024-06-12 20:37:42 +08:00
|
|
|
|
|
|
|
|
|
|
ConsumeQuota bool
|
2026-02-21 23:47:55 +08:00
|
|
|
|
|
|
|
|
|
|
// LockedChannel holds the full channel object when the request is bound to
|
|
|
|
|
|
// a specific channel (e.g., remix on origin task's channel). Stored as any
|
|
|
|
|
|
// to avoid an import cycle with model; callers type-assert to *model.Channel.
|
|
|
|
|
|
LockedChannel any
|
2024-06-12 20:37:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 15:50:00 +08:00
|
|
|
|
type TaskSubmitReq struct {
|
2025-10-31 16:51:05 +08:00
|
|
|
|
Prompt string `json:"prompt"`
|
|
|
|
|
|
Model string `json:"model,omitempty"`
|
|
|
|
|
|
Mode string `json:"mode,omitempty"`
|
|
|
|
|
|
Image string `json:"image,omitempty"`
|
|
|
|
|
|
Images []string `json:"images,omitempty"`
|
|
|
|
|
|
Size string `json:"size,omitempty"`
|
|
|
|
|
|
Duration int `json:"duration,omitempty"`
|
|
|
|
|
|
Seconds string `json:"seconds,omitempty"`
|
|
|
|
|
|
InputReference string `json:"input_reference,omitempty"`
|
|
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2025-06-20 15:50:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 11:37:10 +08:00
|
|
|
|
func (t *TaskSubmitReq) GetPrompt() string {
|
2025-09-12 21:52:32 +08:00
|
|
|
|
return t.Prompt
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-14 11:37:10 +08:00
|
|
|
|
func (t *TaskSubmitReq) HasImage() bool {
|
2025-09-12 22:19:45 +08:00
|
|
|
|
return len(t.Images) > 0
|
2025-09-12 21:52:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-31 16:51:05 +08:00
|
|
|
|
func (t *TaskSubmitReq) UnmarshalJSON(data []byte) error {
|
|
|
|
|
|
type Alias TaskSubmitReq
|
|
|
|
|
|
aux := &struct {
|
|
|
|
|
|
Metadata json.RawMessage `json:"metadata,omitempty"`
|
2026-03-27 17:24:26 +08:00
|
|
|
|
Duration json.RawMessage `json:"duration,omitempty"`
|
2025-10-31 16:51:05 +08:00
|
|
|
|
*Alias
|
|
|
|
|
|
}{
|
|
|
|
|
|
Alias: (*Alias)(t),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := common.Unmarshal(data, &aux); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-27 17:24:26 +08:00
|
|
|
|
if len(aux.Duration) > 0 {
|
|
|
|
|
|
var durationInt int
|
|
|
|
|
|
if err := common.Unmarshal(aux.Duration, &durationInt); err == nil {
|
|
|
|
|
|
t.Duration = durationInt
|
|
|
|
|
|
} else {
|
|
|
|
|
|
var durationStr string
|
|
|
|
|
|
if err := common.Unmarshal(aux.Duration, &durationStr); err == nil && durationStr != "" {
|
|
|
|
|
|
if v, err := strconv.Atoi(durationStr); err == nil {
|
|
|
|
|
|
t.Duration = v
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-31 16:51:05 +08:00
|
|
|
|
if len(aux.Metadata) > 0 {
|
|
|
|
|
|
var metadataStr string
|
|
|
|
|
|
if err := common.Unmarshal(aux.Metadata, &metadataStr); err == nil && metadataStr != "" {
|
|
|
|
|
|
var metadataObj map[string]interface{}
|
|
|
|
|
|
if err := common.Unmarshal([]byte(metadataStr), &metadataObj); err == nil {
|
|
|
|
|
|
t.Metadata = metadataObj
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var metadataObj map[string]interface{}
|
|
|
|
|
|
if err := common.Unmarshal(aux.Metadata, &metadataObj); err == nil {
|
|
|
|
|
|
t.Metadata = metadataObj
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-11-14 11:37:10 +08:00
|
|
|
|
func (t *TaskSubmitReq) UnmarshalMetadata(v any) error {
|
|
|
|
|
|
metadata := t.Metadata
|
|
|
|
|
|
if metadata != nil {
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
metadataBytes, err := common.Marshal(metadata)
|
2025-11-14 11:37:10 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("marshal metadata failed: %w", err)
|
|
|
|
|
|
}
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
err = common.Unmarshal(metadataBytes, v)
|
2025-11-14 11:37:10 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return fmt.Errorf("unmarshal metadata to target failed: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-10-31 16:51:05 +08:00
|
|
|
|
|
2025-06-20 15:50:00 +08:00
|
|
|
|
type TaskInfo struct {
|
2025-10-02 02:46:47 +08:00
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
TaskID string `json:"task_id"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
Reason string `json:"reason,omitempty"`
|
|
|
|
|
|
Url string `json:"url,omitempty"`
|
2025-10-31 15:29:17 +08:00
|
|
|
|
RemoteUrl string `json:"remote_url,omitempty"`
|
2025-10-02 02:46:47 +08:00
|
|
|
|
Progress string `json:"progress,omitempty"`
|
|
|
|
|
|
CompletionTokens int `json:"completion_tokens,omitempty"` // 用于按倍率计费
|
|
|
|
|
|
TotalTokens int `json:"total_tokens,omitempty"` // 用于按倍率计费
|
2025-06-20 15:50:00 +08:00
|
|
|
|
}
|
2025-10-02 00:14:35 +08:00
|
|
|
|
|
2025-10-14 23:03:17 +08:00
|
|
|
|
func FailTaskInfo(reason string) *TaskInfo {
|
|
|
|
|
|
return &TaskInfo{
|
|
|
|
|
|
Status: "FAILURE",
|
|
|
|
|
|
Reason: reason,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 00:14:35 +08:00
|
|
|
|
// RemoveDisabledFields 从请求 JSON 数据中移除渠道设置中禁用的字段
|
|
|
|
|
|
// service_tier: 服务层级字段,可能导致额外计费(OpenAI、Claude、Responses API 支持)
|
2026-02-21 14:25:58 +08:00
|
|
|
|
// inference_geo: Claude 数据驻留推理区域字段(仅 Claude 支持,默认过滤)
|
2026-04-15 20:55:01 +08:00
|
|
|
|
// speed: Claude 推理速度模式字段(仅 Claude 支持,默认过滤)
|
2025-10-02 00:14:35 +08:00
|
|
|
|
// store: 数据存储授权字段,涉及用户隐私(仅 OpenAI、Responses API 支持,默认允许透传,禁用后可能导致 Codex 无法使用)
|
|
|
|
|
|
// safety_identifier: 安全标识符,用于向 OpenAI 报告违规用户(仅 OpenAI 支持,涉及用户隐私)
|
2026-02-19 14:16:07 +08:00
|
|
|
|
// stream_options.include_obfuscation: 响应流混淆控制字段(仅 OpenAI Responses API 支持)
|
2026-02-19 15:09:13 +08:00
|
|
|
|
func RemoveDisabledFields(jsonData []byte, channelOtherSettings dto.ChannelOtherSettings, channelPassThroughEnabled bool) ([]byte, error) {
|
|
|
|
|
|
if model_setting.GetGlobalSettings().PassThroughRequestEnabled || channelPassThroughEnabled {
|
|
|
|
|
|
return jsonData, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 00:14:35 +08:00
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
if err := common.Unmarshal(jsonData, &data); err != nil {
|
2025-10-02 13:57:49 +08:00
|
|
|
|
common.SysError("RemoveDisabledFields Unmarshal error :" + err.Error())
|
|
|
|
|
|
return jsonData, nil
|
2025-10-02 00:14:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 默认移除 service_tier,除非明确允许(避免额外计费风险)
|
|
|
|
|
|
if !channelOtherSettings.AllowServiceTier {
|
|
|
|
|
|
if _, exists := data["service_tier"]; exists {
|
|
|
|
|
|
delete(data, "service_tier")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-21 14:25:58 +08:00
|
|
|
|
// 默认移除 inference_geo,除非明确允许(避免在未授权情况下透传数据驻留区域)
|
|
|
|
|
|
if !channelOtherSettings.AllowInferenceGeo {
|
|
|
|
|
|
if _, exists := data["inference_geo"]; exists {
|
|
|
|
|
|
delete(data, "inference_geo")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-15 20:55:01 +08:00
|
|
|
|
// 默认移除 speed,除非明确允许(避免意外切换 Claude 推理速度模式)
|
|
|
|
|
|
if !channelOtherSettings.AllowSpeed {
|
|
|
|
|
|
if _, exists := data["speed"]; exists {
|
|
|
|
|
|
delete(data, "speed")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 00:14:35 +08:00
|
|
|
|
// 默认允许 store 透传,除非明确禁用(禁用可能影响 Codex 使用)
|
|
|
|
|
|
if channelOtherSettings.DisableStore {
|
|
|
|
|
|
if _, exists := data["store"]; exists {
|
|
|
|
|
|
delete(data, "store")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 默认移除 safety_identifier,除非明确允许(保护用户隐私,避免向 OpenAI 报告用户信息)
|
|
|
|
|
|
if !channelOtherSettings.AllowSafetyIdentifier {
|
|
|
|
|
|
if _, exists := data["safety_identifier"]; exists {
|
|
|
|
|
|
delete(data, "safety_identifier")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-19 14:16:07 +08:00
|
|
|
|
// 默认移除 stream_options.include_obfuscation,除非明确允许(避免关闭响应流混淆保护)
|
|
|
|
|
|
if !channelOtherSettings.AllowIncludeObfuscation {
|
|
|
|
|
|
if streamOptionsAny, exists := data["stream_options"]; exists {
|
|
|
|
|
|
if streamOptions, ok := streamOptionsAny.(map[string]interface{}); ok {
|
|
|
|
|
|
if _, includeExists := streamOptions["include_obfuscation"]; includeExists {
|
|
|
|
|
|
delete(streamOptions, "include_obfuscation")
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(streamOptions) == 0 {
|
|
|
|
|
|
delete(data, "stream_options")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data["stream_options"] = streamOptions
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 13:57:49 +08:00
|
|
|
|
jsonDataAfter, err := common.Marshal(data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.SysError("RemoveDisabledFields Marshal error :" + err.Error())
|
|
|
|
|
|
return jsonData, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return jsonDataAfter, nil
|
2025-10-02 00:14:35 +08:00
|
|
|
|
}
|
2025-12-21 17:09:49 +08:00
|
|
|
|
|
|
|
|
|
|
// RemoveGeminiDisabledFields removes disabled fields from Gemini request JSON data
|
|
|
|
|
|
// Currently supports removing functionResponse.id field which Vertex AI does not support
|
|
|
|
|
|
func RemoveGeminiDisabledFields(jsonData []byte) ([]byte, error) {
|
|
|
|
|
|
if !model_setting.GetGeminiSettings().RemoveFunctionResponseIdEnabled {
|
|
|
|
|
|
return jsonData, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
|
if err := common.Unmarshal(jsonData, &data); err != nil {
|
|
|
|
|
|
common.SysError("RemoveGeminiDisabledFields Unmarshal error: " + err.Error())
|
|
|
|
|
|
return jsonData, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Process contents array
|
|
|
|
|
|
// Handle both camelCase (functionResponse) and snake_case (function_response)
|
|
|
|
|
|
if contents, ok := data["contents"].([]interface{}); ok {
|
|
|
|
|
|
for _, content := range contents {
|
|
|
|
|
|
if contentMap, ok := content.(map[string]interface{}); ok {
|
|
|
|
|
|
if parts, ok := contentMap["parts"].([]interface{}); ok {
|
|
|
|
|
|
for _, part := range parts {
|
|
|
|
|
|
if partMap, ok := part.(map[string]interface{}); ok {
|
|
|
|
|
|
// Check functionResponse (camelCase)
|
|
|
|
|
|
if funcResp, ok := partMap["functionResponse"].(map[string]interface{}); ok {
|
|
|
|
|
|
delete(funcResp, "id")
|
|
|
|
|
|
}
|
|
|
|
|
|
// Check function_response (snake_case)
|
|
|
|
|
|
if funcResp, ok := partMap["function_response"].(map[string]interface{}); ok {
|
|
|
|
|
|
delete(funcResp, "id")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonDataAfter, err := common.Marshal(data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
common.SysError("RemoveGeminiDisabledFields Marshal error: " + err.Error())
|
|
|
|
|
|
return jsonData, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return jsonDataAfter, nil
|
|
|
|
|
|
}
|