Merge remote-tracking branch 'upstream/main' into fix/mobile-usage-log-cost-alignment

# Conflicts:
#	web/default/src/features/usage-logs/components/usage-logs-mobile-card.tsx
This commit is contained in:
yyhhyyyyyy
2026-06-05 14:11:55 +08:00
110 changed files with 2306 additions and 3130 deletions
@@ -24,7 +24,7 @@ import {
useCallback,
useRef,
} from 'react'
import { useForm } from 'react-hook-form'
import { type SubmitErrorHandler, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
@@ -140,6 +140,7 @@ import {
hasModelConfigChanged,
findMissingModelsInMapping,
validateModelMappingJson,
hasAdvancedSettingsErrors,
} from '../../lib'
import {
collectInvalidStatusCodeEntries,
@@ -204,7 +205,6 @@ function readAdvancedSettingsPreference(): boolean {
function hasAdvancedSettingsValues(values: ChannelFormValues): boolean {
return Boolean(
values.model_mapping?.trim() ||
values.param_override?.trim() ||
values.header_override?.trim() ||
values.status_code_mapping?.trim() ||
@@ -1008,6 +1008,26 @@ export function ChannelMutateDrawer({
]
)
const handleAdvancedSettingsOpenChange = useCallback((nextOpen: boolean) => {
setAdvancedSettingsOpen(nextOpen)
if (typeof window !== 'undefined') {
window.localStorage.setItem(
ADVANCED_SETTINGS_EXPANDED_KEY,
String(nextOpen)
)
}
}, [])
const onInvalid: SubmitErrorHandler<ChannelFormValues> = useCallback(
(errors) => {
if (hasAdvancedSettingsErrors(errors)) {
handleAdvancedSettingsOpenChange(true)
}
toast.error(t('Please fix the highlighted fields before saving'))
},
[handleAdvancedSettingsOpenChange, t]
)
// Handle drawer close
const handleOpenChange = useCallback(
(v: boolean) => {
@@ -1020,16 +1040,6 @@ export function ChannelMutateDrawer({
[onOpenChange, form]
)
const handleAdvancedSettingsOpenChange = useCallback((nextOpen: boolean) => {
setAdvancedSettingsOpen(nextOpen)
if (typeof window !== 'undefined') {
window.localStorage.setItem(
ADVANCED_SETTINGS_EXPANDED_KEY,
String(nextOpen)
)
}
}, [])
return (
<>
<Sheet open={open} onOpenChange={handleOpenChange}>
@@ -1060,7 +1070,7 @@ export function ChannelMutateDrawer({
<Form {...form}>
<form
id='channel-form'
onSubmit={form.handleSubmit(onSubmit)}
onSubmit={form.handleSubmit(onSubmit, onInvalid)}
className={sideDrawerFormClassName('gap-5')}
>
{isChannelDetailLoading ? (
@@ -0,0 +1,66 @@
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import type { FieldPath } from 'react-hook-form'
import type { ChannelFormValues } from './channel-form'
type ChannelFormErrorMap = Partial<
Record<FieldPath<ChannelFormValues>, unknown>
>
const ADVANCED_SETTINGS_FIELDS = new Set<FieldPath<ChannelFormValues>>([
'priority',
'weight',
'test_model',
'auto_ban',
'tag',
'remark',
'param_override',
'header_override',
'status_code_mapping',
'force_format',
'thinking_to_content',
'pass_through_body_enabled',
'proxy',
'system_prompt',
'system_prompt_override',
'allow_service_tier',
'disable_store',
'allow_safety_identifier',
'allow_include_obfuscation',
'allow_inference_geo',
'allow_speed',
'claude_beta_query',
'upstream_model_update_check_enabled',
'upstream_model_update_auto_sync_enabled',
'upstream_model_update_ignored_models',
])
export function isAdvancedSettingsField(
fieldName: string
): fieldName is FieldPath<ChannelFormValues> {
return ADVANCED_SETTINGS_FIELDS.has(fieldName as FieldPath<ChannelFormValues>)
}
export function hasAdvancedSettingsErrors(
errors: ChannelFormErrorMap
): boolean {
return Object.keys(errors).some((fieldName) =>
isAdvancedSettingsField(fieldName)
)
}
+1
View File
@@ -18,6 +18,7 @@ For commercial licensing, please contact support@quantumnous.com
*/
// Re-export all library functions
export * from './channel-actions'
export * from './channel-form-errors'
export * from './channel-form'
export * from './channel-type-config'
export * from './channel-utils'
@@ -189,6 +189,7 @@ export function CCSwitchDialog(props: Props) {
onValueChange={setName}
placeholder={currentConfig.defaultName}
emptyText=''
allowCustomValue={true}
/>
</div>
@@ -196,6 +196,7 @@ export function ModelMutateDrawer({
'grok.violation_deduction_amount': 0,
'channel_affinity_setting.enabled': false,
'channel_affinity_setting.switch_on_success': true,
'channel_affinity_setting.keep_on_channel_disabled': false,
'channel_affinity_setting.max_entries': 100000,
'channel_affinity_setting.default_ttl_seconds': 3600,
'channel_affinity_setting.rules': '[]',
+4 -3
View File
@@ -56,8 +56,9 @@ export const ModelCard = memo(function ModelCard(props: ModelCardProps) {
const tags = parseTags(props.model.tags)
const groups = props.model.enable_groups || []
const endpoints = props.model.supported_endpoint_types || []
const vendorIcon = props.model.vendor_icon
? getLobeIcon(props.model.vendor_icon, 28)
const modelIconKey = props.model.icon || props.model.vendor_icon
const modelIcon = modelIconKey
? getLobeIcon(modelIconKey, 28)
: null
const initial = props.model.model_name?.charAt(0).toUpperCase() || '?'
const isDynamicPricing =
@@ -97,7 +98,7 @@ export const ModelCard = memo(function ModelCard(props: ModelCardProps) {
<div className='flex items-start justify-between gap-2.5 sm:gap-3'>
<div className='flex min-w-0 items-start gap-2.5 sm:gap-3'>
<div className='bg-muted/40 flex size-9 shrink-0 items-center justify-center rounded-lg sm:size-10 sm:rounded-xl'>
{vendorIcon || (
{modelIcon || (
<span className='text-muted-foreground text-sm font-bold'>
{initial}
</span>
@@ -268,8 +268,9 @@ function OverviewSummaryGrid(props: { model: PricingModel }) {
function ModelHeader(props: { model: PricingModel }) {
const { t } = useTranslation()
const model = props.model
const vendorIcon = model.vendor_icon
? getLobeIcon(model.vendor_icon, 20)
const modelIconKey = model.icon || model.vendor_icon
const modelIcon = modelIconKey
? getLobeIcon(modelIconKey, 20)
: null
const description = model.description || model.vendor_description || null
const tags = parseTags(model.tags)
@@ -281,7 +282,7 @@ function ModelHeader(props: { model: PricingModel }) {
return (
<header className='pb-4'>
<div className='flex items-center gap-2.5'>
{vendorIcon}
{modelIcon}
<h1 className='font-mono text-xl font-bold tracking-tight sm:text-2xl'>
{model.model_name}
</h1>
@@ -106,13 +106,14 @@ export function usePricingColumns(
),
cell: ({ row }) => {
const model = row.original
const vendorIcon = model.vendor_icon
? getLobeIcon(model.vendor_icon, 14)
const modelIconKey = model.icon || model.vendor_icon
const modelIcon = modelIconKey
? getLobeIcon(modelIconKey, 14)
: null
return (
<div className='flex min-w-[200px] items-center gap-2'>
{vendorIcon}
{modelIcon}
<span className='truncate font-mono text-sm font-medium'>
{model.model_name}
</span>
+1
View File
@@ -31,6 +31,7 @@ export type PricingModel = {
id: number
model_name: string
description?: string
icon?: string
vendor_id?: number
vendor_name?: string
vendor_icon?: string
@@ -111,6 +111,7 @@ export function SubscriptionPurchaseDialog(props: Props) {
Math.ceil(Number(plan.price_amount || 0) * quotaPerUnit)
)
const userQuota = Math.max(0, Number(props.userQuota || 0))
const allowBalancePay = plan.allow_balance_pay !== false
const insufficientBalance = userQuota < balanceCost
const limitReached =
(props.purchaseLimit || 0) > 0 &&
@@ -232,6 +233,10 @@ export function SubscriptionPurchaseDialog(props: Props) {
}
const handlePayBalance = async () => {
if (!allowBalancePay) {
toast.error(t('This plan does not allow balance redemption'))
return
}
setPaying(true)
try {
const res = await paySubscriptionBalance({ plan_id: plan.id })
@@ -332,15 +337,27 @@ export function SubscriptionPurchaseDialog(props: Props) {
<span className='text-muted-foreground'>{t('Available')}</span>
<span>{formatQuota(userQuota)}</span>
</div>
{insufficientBalance && (
{!allowBalancePay ? (
<Alert variant='destructive'>
<AlertDescription>{t('Insufficient balance')}</AlertDescription>
<AlertDescription>
{t('This plan does not allow balance redemption')}
</AlertDescription>
</Alert>
) : (
insufficientBalance && (
<Alert variant='destructive'>
<AlertDescription>
{t('Insufficient balance')}
</AlertDescription>
</Alert>
)
)}
<Button
variant='outline'
onClick={handlePayBalance}
disabled={paying || limitReached || insufficientBalance}
disabled={
paying || limitReached || !allowBalancePay || insufficientBalance
}
>
{t('Pay with Balance')}
</Button>
@@ -461,6 +461,24 @@ export function SubscriptionsMutateDrawer({
</FormItem>
)}
/>
<FormField
control={form.control}
name='allow_balance_pay'
render={({ field }) => (
<FormItem className={sideDrawerSwitchItemClassName()}>
<FormLabel className='!mt-0'>
{t('Allow balance redemption')}
</FormLabel>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
</div>
</SideDrawerSection>
@@ -39,6 +39,7 @@ export function getPlanFormSchema(t: TFunction) {
quota_reset_custom_seconds: z.coerce.number().min(0).optional(),
enabled: z.boolean(),
sort_order: z.coerce.number(),
allow_balance_pay: z.boolean(),
max_purchase_per_user: z.coerce.number().min(0),
total_amount: z.coerce.number().min(0),
upgrade_group: z.string().optional(),
@@ -61,6 +62,7 @@ export const PLAN_FORM_DEFAULTS: PlanFormValues = {
quota_reset_custom_seconds: 0,
enabled: true,
sort_order: 0,
allow_balance_pay: true,
max_purchase_per_user: 0,
total_amount: 0,
upgrade_group: '',
@@ -81,6 +83,7 @@ export function planToFormValues(plan: SubscriptionPlan): PlanFormValues {
quota_reset_custom_seconds: Number(plan.quota_reset_custom_seconds || 0),
enabled: plan.enabled !== false,
sort_order: Number(plan.sort_order || 0),
allow_balance_pay: plan.allow_balance_pay !== false,
max_purchase_per_user: Number(plan.max_purchase_per_user || 0),
total_amount: quotaUnitsToDollars(Number(plan.total_amount || 0)),
upgrade_group: plan.upgrade_group || '',
+1
View File
@@ -35,6 +35,7 @@ export const subscriptionPlanSchema = z.object({
quota_reset_custom_seconds: z.number().optional(),
enabled: z.boolean(),
sort_order: z.number(),
allow_balance_pay: z.boolean().optional().default(true),
max_purchase_per_user: z.number(),
total_amount: z.number(),
upgrade_group: z.string().optional(),
@@ -100,6 +100,9 @@ export function ChannelAffinitySection(props: Props) {
const [switchOnSuccess, setSwitchOnSuccess] = useState(
props.defaultValues['channel_affinity_setting.switch_on_success']
)
const [keepOnChannelDisabled, setKeepOnChannelDisabled] = useState(
props.defaultValues['channel_affinity_setting.keep_on_channel_disabled']
)
const [maxEntries, setMaxEntries] = useState(
props.defaultValues['channel_affinity_setting.max_entries']
)
@@ -136,6 +139,9 @@ export function ChannelAffinitySection(props: Props) {
setSwitchOnSuccess(
props.defaultValues['channel_affinity_setting.switch_on_success']
)
setKeepOnChannelDisabled(
props.defaultValues['channel_affinity_setting.keep_on_channel_disabled']
)
setMaxEntries(props.defaultValues['channel_affinity_setting.max_entries'])
setDefaultTtl(
props.defaultValues['channel_affinity_setting.default_ttl_seconds']
@@ -231,6 +237,14 @@ export function ChannelAffinitySection(props: Props) {
key: 'channel_affinity_setting.switch_on_success',
value: String(switchOnSuccess),
})
if (
keepOnChannelDisabled !==
props.defaultValues['channel_affinity_setting.keep_on_channel_disabled']
)
updates.push({
key: 'channel_affinity_setting.keep_on_channel_disabled',
value: String(keepOnChannelDisabled),
})
if (
maxEntries !==
props.defaultValues['channel_affinity_setting.max_entries']
@@ -397,6 +411,14 @@ export function ChannelAffinitySection(props: Props) {
'If the affinity channel fails and retry succeeds on another channel, update affinity to the successful channel.'
)}
/>
<SettingsSwitchField
checked={keepOnChannelDisabled}
onCheckedChange={setKeepOnChannelDisabled}
label={t('Keep affinity when channel is disabled')}
description={t(
'When enabled, keep the affinity entry even if the affinity channel is disabled or no longer usable for the current group/model. Leave it off to delete the entry and select another channel.'
)}
/>
<Separator />
@@ -50,6 +50,7 @@ export interface CacheStats {
export interface ChannelAffinitySettings {
'channel_affinity_setting.enabled': boolean
'channel_affinity_setting.switch_on_success': boolean
'channel_affinity_setting.keep_on_channel_disabled': boolean
'channel_affinity_setting.max_entries': number
'channel_affinity_setting.default_ttl_seconds': number
'channel_affinity_setting.rules': string
@@ -231,10 +231,10 @@ export function ClaudeSettingsCard({ defaultValues }: ClaudeSettingsCardProps) {
render={({ field }) => (
<SettingsSwitchItem>
<SettingsSwitchContent>
<FormLabel>{t('Thinking Adapter')}</FormLabel>
<FormLabel>{t('Thinking Suffix Adapter')}</FormLabel>
<FormDescription>
{t(
'Translate `-thinking` suffixes into Anthropic native thinking models while keeping pricing predictable.'
'Adapt `-thinking` suffix requests to Anthropic native thinking behavior while keeping billing predictable.'
)}
</FormDescription>
</SettingsSwitchContent>
@@ -307,7 +307,7 @@ export function GeminiSettingsCard({ defaultValues }: GeminiSettingsCardProps) {
render={({ field }) => (
<SettingsSwitchItem>
<SettingsSwitchContent>
<FormLabel>{t('Thinking Adapter')}</FormLabel>
<FormLabel>{t('Thinking Suffix Adapter')}</FormLabel>
<FormDescription>
{t('Supports `-thinking`, `-thinking-')}
{'{{budget}}'}
@@ -227,7 +227,9 @@ export function GlobalSettingsCard({ defaultValues }: GlobalSettingsCardProps) {
name='global.thinking_model_blacklist'
render={({ field }) => (
<FormItem>
<FormLabel>{t('Disable thinking processing models')}</FormLabel>
<FormLabel>
{t('Models that skip thinking suffix processing')}
</FormLabel>
<FormControl>
<Textarea
rows={4}
@@ -64,6 +64,7 @@ const defaultModelSettings: ModelSettings = {
'group_ratio_setting.group_special_usable_group': '{}',
'channel_affinity_setting.enabled': false,
'channel_affinity_setting.switch_on_success': true,
'channel_affinity_setting.keep_on_channel_disabled': false,
'channel_affinity_setting.max_entries': 100000,
'channel_affinity_setting.default_ttl_seconds': 3600,
'channel_affinity_setting.rules': '[]',
@@ -130,6 +130,8 @@ const MODELS_SECTIONS = [
settings['channel_affinity_setting.enabled'],
'channel_affinity_setting.switch_on_success':
settings['channel_affinity_setting.switch_on_success'],
'channel_affinity_setting.keep_on_channel_disabled':
settings['channel_affinity_setting.keep_on_channel_disabled'],
'channel_affinity_setting.max_entries':
settings['channel_affinity_setting.max_entries'],
'channel_affinity_setting.default_ttl_seconds':
+1
View File
@@ -176,6 +176,7 @@ export type ModelSettings = {
'group_ratio_setting.group_special_usable_group': string
'channel_affinity_setting.enabled': boolean
'channel_affinity_setting.switch_on_success': boolean
'channel_affinity_setting.keep_on_channel_disabled': boolean
'channel_affinity_setting.max_entries': number
'channel_affinity_setting.default_ttl_seconds': number
'channel_affinity_setting.rules': string
@@ -183,7 +183,7 @@ function CommonLogsCard<TData>({
const modelCell = cells.get('model_name')
const quotaCell = cells.get('quota')
const createdAtCellOriginal = cells.get('created_at')?.row.original as
const rowData = cells.get('created_at')?.row.original as
| Record<string, unknown>
| undefined
@@ -203,8 +203,8 @@ function CommonLogsCard<TData>({
{t('Time')}
</div>
<MobileLogTimeStatus
createdAt={createdAtCellOriginal?.created_at}
type={createdAtCellOriginal?.type}
createdAt={rowData?.created_at}
type={rowData?.type}
/>
</div>
<SummaryField