feat(system-settings): add user token limit configuration section (#5678)
This commit is contained in:
+131
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
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 { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { useEffect } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import * as z from 'zod'
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from '@/components/ui/form'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { SettingsForm } from '../components/settings-form-layout'
|
||||
import { SettingsPageFormActions } from '../components/settings-page-context'
|
||||
import { SettingsSection } from '../components/settings-section'
|
||||
import { useUpdateOption } from '../hooks/use-update-option'
|
||||
|
||||
const tokenLimitSchema = z.object({
|
||||
token_setting: z.object({
|
||||
max_user_tokens: z.number().min(1),
|
||||
}),
|
||||
})
|
||||
|
||||
type TokenLimitFormValues = z.output<typeof tokenLimitSchema>
|
||||
type TokenLimitFormInput = z.input<typeof tokenLimitSchema>
|
||||
|
||||
type NormalizedTokenLimitValues = {
|
||||
'token_setting.max_user_tokens': number
|
||||
}
|
||||
|
||||
type TokenLimitSectionProps = {
|
||||
defaultValues: NormalizedTokenLimitValues
|
||||
}
|
||||
|
||||
const buildFormDefaults = (
|
||||
defaults: TokenLimitSectionProps['defaultValues']
|
||||
): TokenLimitFormInput => ({
|
||||
token_setting: {
|
||||
max_user_tokens: defaults['token_setting.max_user_tokens'],
|
||||
},
|
||||
})
|
||||
|
||||
const normalizeFormValues = (
|
||||
values: TokenLimitFormValues
|
||||
): NormalizedTokenLimitValues => ({
|
||||
'token_setting.max_user_tokens': values.token_setting.max_user_tokens,
|
||||
})
|
||||
|
||||
export function TokenLimitSection({ defaultValues }: TokenLimitSectionProps) {
|
||||
const { t } = useTranslation()
|
||||
const updateOption = useUpdateOption()
|
||||
const form = useForm<TokenLimitFormInput, unknown, TokenLimitFormValues>({
|
||||
resolver: zodResolver(tokenLimitSchema),
|
||||
mode: 'onChange',
|
||||
defaultValues: buildFormDefaults(defaultValues),
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
form.reset(buildFormDefaults(defaultValues))
|
||||
}, [defaultValues, form])
|
||||
|
||||
const onSubmit = async (values: TokenLimitFormValues) => {
|
||||
const key = 'token_setting.max_user_tokens' as const
|
||||
const normalized = normalizeFormValues(values)
|
||||
const value = normalized[key]
|
||||
if (value !== defaultValues[key]) {
|
||||
await updateOption.mutateAsync({ key, value })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<SettingsSection title={t('Token Limits')}>
|
||||
<Form {...form}>
|
||||
<SettingsForm onSubmit={form.handleSubmit(onSubmit)}>
|
||||
<SettingsPageFormActions
|
||||
onSave={form.handleSubmit(onSubmit)}
|
||||
isSaving={updateOption.isPending}
|
||||
saveLabel='Save token limits'
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name='token_setting.max_user_tokens'
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t('Maximum tokens per user')}</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type='number'
|
||||
min={1}
|
||||
step={1}
|
||||
{...field}
|
||||
onChange={(e) =>
|
||||
field.onChange(Number.parseInt(e.target.value) || 1)
|
||||
}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
{t(
|
||||
'Maximum number of tokens each user can create. Default 1000. Setting too large may affect performance.'
|
||||
)}
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</SettingsForm>
|
||||
</Form>
|
||||
</SettingsSection>
|
||||
)
|
||||
}
|
||||
@@ -41,6 +41,7 @@ const defaultSecuritySettings: SecuritySettings = {
|
||||
'fetch_setting.ip_list': [],
|
||||
'fetch_setting.allowed_ports': [],
|
||||
'fetch_setting.apply_ip_filter_for_domain': false,
|
||||
'token_setting.max_user_tokens': 1000,
|
||||
}
|
||||
|
||||
export function SecuritySettings() {
|
||||
|
||||
@@ -19,6 +19,7 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
import { RateLimitSection } from '../request-limits/rate-limit-section'
|
||||
import { SensitiveWordsSection } from '../request-limits/sensitive-words-section'
|
||||
import { SSRFSection } from '../request-limits/ssrf-section'
|
||||
import { TokenLimitSection } from '../request-limits/token-limit-section'
|
||||
import type { SecuritySettings } from '../types'
|
||||
import { createSectionRegistry } from '../utils/section-registry'
|
||||
|
||||
@@ -77,6 +78,18 @@ const SECURITY_SECTIONS = [
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
id: 'token-limits',
|
||||
titleKey: 'Token Limits',
|
||||
build: (settings: SecuritySettings) => (
|
||||
<TokenLimitSection
|
||||
defaultValues={{
|
||||
'token_setting.max_user_tokens':
|
||||
settings['token_setting.max_user_tokens'],
|
||||
}}
|
||||
/>
|
||||
),
|
||||
},
|
||||
] as const
|
||||
|
||||
export type SecuritySectionId = (typeof SECURITY_SECTIONS)[number]['id']
|
||||
|
||||
@@ -378,6 +378,7 @@ export type SecuritySettings = {
|
||||
'fetch_setting.ip_list': string[]
|
||||
'fetch_setting.allowed_ports': number[]
|
||||
'fetch_setting.apply_ip_filter_for_domain': boolean
|
||||
'token_setting.max_user_tokens': number
|
||||
}
|
||||
|
||||
export type UpstreamChannel = {
|
||||
|
||||
Reference in New Issue
Block a user