✨ feat: Enhance user settings and notification options
This commit is contained in:
@@ -6,6 +6,7 @@ var (
|
||||
UserSettingWebhookUrl = "webhook_url" // WebhookUrl webhook地址
|
||||
UserSettingWebhookSecret = "webhook_secret" // WebhookSecret webhook密钥
|
||||
UserSettingNotificationEmail = "notification_email" // NotificationEmail 通知邮箱地址
|
||||
UserAcceptUnsetRatioModel = "accept_unset_model_ratio_model" // AcceptUnsetRatioModel 是否接受未设置价格的模型
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -105,6 +105,11 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr
|
||||
request := buildTestRequest(testModel)
|
||||
common.SysLog(fmt.Sprintf("testing channel %d with model %s , info %v ", channel.Id, testModel, info))
|
||||
|
||||
priceData, err := helper.ModelPriceHelper(c, info, 0, int(request.MaxTokens))
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
adaptor.Init(info)
|
||||
|
||||
convertedRequest, err := adaptor.ConvertOpenAIRequest(c, info, request)
|
||||
@@ -143,10 +148,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr
|
||||
return err, nil
|
||||
}
|
||||
info.PromptTokens = usage.PromptTokens
|
||||
priceData, err := helper.ModelPriceHelper(c, info, usage.PromptTokens, int(request.MaxTokens))
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
quota := 0
|
||||
if !priceData.UsePrice {
|
||||
quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*priceData.CompletionRatio))
|
||||
|
||||
@@ -918,6 +918,7 @@ type UpdateUserSettingRequest struct {
|
||||
WebhookUrl string `json:"webhook_url,omitempty"`
|
||||
WebhookSecret string `json:"webhook_secret,omitempty"`
|
||||
NotificationEmail string `json:"notification_email,omitempty"`
|
||||
AcceptUnsetModelRatioModel bool `json:"accept_unset_model_ratio_model"`
|
||||
}
|
||||
|
||||
func UpdateUserSetting(c *gin.Context) {
|
||||
@@ -993,6 +994,7 @@ func UpdateUserSetting(c *gin.Context) {
|
||||
settings := map[string]interface{}{
|
||||
constant.UserSettingNotifyType: req.QuotaWarningType,
|
||||
constant.UserSettingQuotaWarningThreshold: req.QuotaWarningThreshold,
|
||||
"accept_unset_model_ratio_model": req.AcceptUnsetModelRatioModel,
|
||||
}
|
||||
|
||||
// 如果是webhook类型,添加webhook相关设置
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"one-api/common"
|
||||
constant2 "one-api/constant"
|
||||
relaycommon "one-api/relay/common"
|
||||
"one-api/setting"
|
||||
"one-api/setting/operation_setting"
|
||||
@@ -40,12 +41,21 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
|
||||
var success bool
|
||||
modelRatio, success = operation_setting.GetModelRatio(info.OriginModelName)
|
||||
if !success {
|
||||
acceptUnsetRatio := false
|
||||
if accept, ok := info.UserSetting[constant2.UserAcceptUnsetRatioModel]; ok {
|
||||
b, ok := accept.(bool)
|
||||
if ok {
|
||||
acceptUnsetRatio = b
|
||||
}
|
||||
}
|
||||
if !acceptUnsetRatio {
|
||||
if info.UserId == 1 {
|
||||
return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置,请设置或开始自用模式;Model %s ratio or price not set, please set or start self-use mode", info.OriginModelName, info.OriginModelName)
|
||||
} else {
|
||||
return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置, 请联系管理员设置;Model %s ratio or price not set, please contact administrator to set", info.OriginModelName, info.OriginModelName)
|
||||
}
|
||||
}
|
||||
}
|
||||
completionRatio = operation_setting.GetCompletionRatio(info.OriginModelName)
|
||||
cacheRatio, _ = operation_setting.GetCacheRatio(info.OriginModelName)
|
||||
cacheCreationRatio, _ = operation_setting.GetCreateCacheRatio(info.OriginModelName)
|
||||
|
||||
5185
web/pnpm-lock.yaml
generated
5185
web/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,7 @@ import {
|
||||
isRoot,
|
||||
showError,
|
||||
showInfo,
|
||||
showSuccess,
|
||||
showSuccess
|
||||
} from '../helpers';
|
||||
import Turnstile from 'react-turnstile';
|
||||
import { UserContext } from '../context/User';
|
||||
@@ -30,12 +30,15 @@ import {
|
||||
Radio,
|
||||
RadioGroup,
|
||||
AutoComplete,
|
||||
Checkbox,
|
||||
Tabs,
|
||||
TabPane
|
||||
} from '@douyinfe/semi-ui';
|
||||
import {
|
||||
getQuotaPerUnit,
|
||||
renderQuota,
|
||||
renderQuotaWithPrompt,
|
||||
stringToColor,
|
||||
stringToColor
|
||||
} from '../helpers/render';
|
||||
import TelegramLoginButton from 'react-telegram-login';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -51,7 +54,7 @@ const PersonalSetting = () => {
|
||||
email: '',
|
||||
self_account_deletion_confirmation: '',
|
||||
set_new_password: '',
|
||||
set_new_password_confirmation: '',
|
||||
set_new_password_confirmation: ''
|
||||
});
|
||||
const [status, setStatus] = useState({});
|
||||
const [showChangePasswordModal, setShowChangePasswordModal] = useState(false);
|
||||
@@ -80,7 +83,8 @@ const PersonalSetting = () => {
|
||||
warningThreshold: 100000,
|
||||
webhookUrl: '',
|
||||
webhookSecret: '',
|
||||
notificationEmail: ''
|
||||
notificationEmail: '',
|
||||
acceptUnsetModelRatioModel: false
|
||||
});
|
||||
const [showWebhookDocs, setShowWebhookDocs] = useState(false);
|
||||
|
||||
@@ -123,7 +127,8 @@ const PersonalSetting = () => {
|
||||
warningThreshold: settings.quota_warning_threshold || 500000,
|
||||
webhookUrl: settings.webhook_url || '',
|
||||
webhookSecret: settings.webhook_secret || '',
|
||||
notificationEmail: settings.notification_email || ''
|
||||
notificationEmail: settings.notification_email || '',
|
||||
acceptUnsetModelRatioModel: settings.accept_unset_model_ratio_model || false
|
||||
});
|
||||
}
|
||||
}, [userState?.user?.setting]);
|
||||
@@ -217,7 +222,7 @@ const PersonalSetting = () => {
|
||||
const bindWeChat = async () => {
|
||||
if (inputs.wechat_verification_code === '') return;
|
||||
const res = await API.get(
|
||||
`/api/oauth/wechat/bind?code=${inputs.wechat_verification_code}`,
|
||||
`/api/oauth/wechat/bind?code=${inputs.wechat_verification_code}`
|
||||
);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
@@ -234,7 +239,7 @@ const PersonalSetting = () => {
|
||||
return;
|
||||
}
|
||||
const res = await API.put(`/api/user/self`, {
|
||||
password: inputs.set_new_password,
|
||||
password: inputs.set_new_password
|
||||
});
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
@@ -252,7 +257,7 @@ const PersonalSetting = () => {
|
||||
return;
|
||||
}
|
||||
const res = await API.post(`/api/user/aff_transfer`, {
|
||||
quota: transferAmount,
|
||||
quota: transferAmount
|
||||
});
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
@@ -276,7 +281,7 @@ const PersonalSetting = () => {
|
||||
}
|
||||
setLoading(true);
|
||||
const res = await API.get(
|
||||
`/api/verification?email=${inputs.email}&turnstile=${turnstileToken}`,
|
||||
`/api/verification?email=${inputs.email}&turnstile=${turnstileToken}`
|
||||
);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
@@ -294,7 +299,7 @@ const PersonalSetting = () => {
|
||||
}
|
||||
setLoading(true);
|
||||
const res = await API.get(
|
||||
`/api/oauth/email/bind?email=${inputs.email}&code=${inputs.email_verification_code}`,
|
||||
`/api/oauth/email/bind?email=${inputs.email}&code=${inputs.email_verification_code}`
|
||||
);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
@@ -342,7 +347,8 @@ const PersonalSetting = () => {
|
||||
quota_warning_threshold: parseFloat(notificationSettings.warningThreshold),
|
||||
webhook_url: notificationSettings.webhookUrl,
|
||||
webhook_secret: notificationSettings.webhookSecret,
|
||||
notification_email: notificationSettings.notificationEmail
|
||||
notification_email: notificationSettings.notificationEmail,
|
||||
accept_unset_model_ratio_model: notificationSettings.acceptUnsetModelRatioModel
|
||||
});
|
||||
|
||||
if (res.data.success) {
|
||||
@@ -399,7 +405,7 @@ const PersonalSetting = () => {
|
||||
<Card.Meta
|
||||
avatar={
|
||||
<Avatar
|
||||
size='default'
|
||||
size="default"
|
||||
color={stringToColor(getUsername())}
|
||||
style={{ marginRight: 4 }}
|
||||
>
|
||||
@@ -410,18 +416,18 @@ const PersonalSetting = () => {
|
||||
title={<Typography.Text>{getUsername()}</Typography.Text>}
|
||||
description={
|
||||
isRoot() ? (
|
||||
<Tag color='red'>{t('管理员')}</Tag>
|
||||
<Tag color="red">{t('管理员')}</Tag>
|
||||
) : (
|
||||
<Tag color='blue'>{t('普通用户')}</Tag>
|
||||
<Tag color="blue">{t('普通用户')}</Tag>
|
||||
)
|
||||
}
|
||||
></Card.Meta>
|
||||
}
|
||||
headerExtraContent={
|
||||
<>
|
||||
<Space vertical align='start'>
|
||||
<Tag color='green'>{'ID: ' + userState?.user?.id}</Tag>
|
||||
<Tag color='blue'>{userState?.user?.group}</Tag>
|
||||
<Space vertical align="start">
|
||||
<Tag color="green">{'ID: ' + userState?.user?.id}</Tag>
|
||||
<Tag color="blue">{userState?.user?.group}</Tag>
|
||||
</Space>
|
||||
</>
|
||||
}
|
||||
@@ -436,7 +442,7 @@ const PersonalSetting = () => {
|
||||
{models.map((model) => (
|
||||
<Tag
|
||||
key={model}
|
||||
color='cyan'
|
||||
color="cyan"
|
||||
onClick={() => {
|
||||
copyText(model);
|
||||
}}
|
||||
@@ -452,7 +458,7 @@ const PersonalSetting = () => {
|
||||
{models.map((model) => (
|
||||
<Tag
|
||||
key={model}
|
||||
color='cyan'
|
||||
color="cyan"
|
||||
onClick={() => {
|
||||
copyText(model);
|
||||
}}
|
||||
@@ -461,7 +467,7 @@ const PersonalSetting = () => {
|
||||
</Tag>
|
||||
))}
|
||||
<Tag
|
||||
color='blue'
|
||||
color="blue"
|
||||
type="light"
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={() => setIsModelsExpanded(false)}
|
||||
@@ -475,7 +481,7 @@ const PersonalSetting = () => {
|
||||
{models.slice(0, MODELS_DISPLAY_COUNT).map((model) => (
|
||||
<Tag
|
||||
key={model}
|
||||
color='cyan'
|
||||
color="cyan"
|
||||
onClick={() => {
|
||||
copyText(model);
|
||||
}}
|
||||
@@ -484,7 +490,7 @@ const PersonalSetting = () => {
|
||||
</Tag>
|
||||
))}
|
||||
<Tag
|
||||
color='blue'
|
||||
color="blue"
|
||||
type="light"
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={() => setIsModelsExpanded(true)}
|
||||
@@ -691,7 +697,7 @@ const PersonalSetting = () => {
|
||||
<Button disabled={true}>{t('已绑定')}</Button>
|
||||
) : (
|
||||
<TelegramLoginButton
|
||||
dataAuthUrl='/api/oauth/telegram/bind'
|
||||
dataAuthUrl="/api/oauth/telegram/bind"
|
||||
botName={status.telegram_bot_name}
|
||||
/>
|
||||
)
|
||||
@@ -773,21 +779,38 @@ const PersonalSetting = () => {
|
||||
</p>
|
||||
</div>
|
||||
<Input
|
||||
placeholder='验证码'
|
||||
name='wechat_verification_code'
|
||||
placeholder="验证码"
|
||||
name="wechat_verification_code"
|
||||
value={inputs.wechat_verification_code}
|
||||
onChange={(v) =>
|
||||
handleInputChange('wechat_verification_code', v)
|
||||
}
|
||||
/>
|
||||
<Button color='' fluid size='large' onClick={bindWeChat}>
|
||||
<Button color="" fluid size="large" onClick={bindWeChat}>
|
||||
{t('绑定')}
|
||||
</Button>
|
||||
</Modal>
|
||||
</div>
|
||||
</Card>
|
||||
<Card style={{ marginTop: 10 }}>
|
||||
<Typography.Title heading={6}>{t('通知设置')}</Typography.Title>
|
||||
<Tabs type="line" defaultActiveKey="price">
|
||||
<TabPane tab={t('价格设置')} itemKey="price">
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Typography.Text strong>{t('接受未设置价格模型')}</Typography.Text>
|
||||
<div style={{ marginTop: 10 }}>
|
||||
<Checkbox
|
||||
checked={notificationSettings.acceptUnsetModelRatioModel}
|
||||
onChange={e => handleNotificationSettingChange('acceptUnsetModelRatioModel', e.target.checked)}
|
||||
>
|
||||
{t('接受未设置价格模型')}
|
||||
</Checkbox>
|
||||
<Typography.Text type="secondary" style={{ marginTop: 8, display: 'block' }}>
|
||||
{t('当模型没有设置价格时仍接受调用,仅当您信任该网站时使用,可能会产生高额费用')}
|
||||
</Typography.Text>
|
||||
</div>
|
||||
</div>
|
||||
</TabPane>
|
||||
<TabPane tab={t('通知设置')} itemKey="notification">
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Typography.Text strong>{t('通知方式')}</Typography.Text>
|
||||
<div style={{ marginTop: 10 }}>
|
||||
@@ -818,7 +841,12 @@ const PersonalSetting = () => {
|
||||
{t('Webhook请求结构')} {showWebhookDocs ? '▼' : '▶'}
|
||||
</div>
|
||||
<Collapsible isOpen={showWebhookDocs}>
|
||||
<pre style={{marginTop: 4, background: 'var(--semi-color-fill-0)', padding: 8, borderRadius: 4}}>
|
||||
<pre style={{
|
||||
marginTop: 4,
|
||||
background: 'var(--semi-color-fill-0)',
|
||||
padding: 8,
|
||||
borderRadius: 4
|
||||
}}>
|
||||
{`{
|
||||
"type": "quota_exceed", // 通知类型
|
||||
"title": "标题", // 通知标题
|
||||
@@ -874,7 +902,8 @@ const PersonalSetting = () => {
|
||||
</div>
|
||||
)}
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Typography.Text strong>{t('额度预警阈值')} {renderQuotaWithPrompt(notificationSettings.warningThreshold)}</Typography.Text>
|
||||
<Typography.Text
|
||||
strong>{t('额度预警阈值')} {renderQuotaWithPrompt(notificationSettings.warningThreshold)}</Typography.Text>
|
||||
<div style={{ marginTop: 10 }}>
|
||||
<AutoComplete
|
||||
value={notificationSettings.warningThreshold}
|
||||
@@ -893,6 +922,8 @@ const PersonalSetting = () => {
|
||||
{t('当剩余额度低于此数值时,系统将通过选择的方式发送通知')}
|
||||
</Typography.Text>
|
||||
</div>
|
||||
</TabPane>
|
||||
</Tabs>
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Button type="primary" onClick={saveNotificationSettings}>
|
||||
{t('保存设置')}
|
||||
@@ -912,15 +943,15 @@ const PersonalSetting = () => {
|
||||
style={{
|
||||
marginTop: 20,
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
justifyContent: 'space-between'
|
||||
}}
|
||||
>
|
||||
<Input
|
||||
fluid
|
||||
placeholder='输入邮箱地址'
|
||||
placeholder="输入邮箱地址"
|
||||
onChange={(value) => handleInputChange('email', value)}
|
||||
name='email'
|
||||
type='email'
|
||||
name="email"
|
||||
type="email"
|
||||
/>
|
||||
<Button
|
||||
onClick={sendVerificationCode}
|
||||
@@ -932,8 +963,8 @@ const PersonalSetting = () => {
|
||||
<div style={{ marginTop: 10 }}>
|
||||
<Input
|
||||
fluid
|
||||
placeholder='验证码'
|
||||
name='email_verification_code'
|
||||
placeholder="验证码"
|
||||
name="email_verification_code"
|
||||
value={inputs.email_verification_code}
|
||||
onChange={(value) =>
|
||||
handleInputChange('email_verification_code', value)
|
||||
@@ -960,20 +991,20 @@ const PersonalSetting = () => {
|
||||
>
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Banner
|
||||
type='danger'
|
||||
description='您正在删除自己的帐户,将清空所有数据且不可恢复'
|
||||
type="danger"
|
||||
description="您正在删除自己的帐户,将清空所有数据且不可恢复"
|
||||
closeIcon={null}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Input
|
||||
placeholder={`输入你的账户名 ${userState?.user?.username} 以确认删除`}
|
||||
name='self_account_deletion_confirmation'
|
||||
name="self_account_deletion_confirmation"
|
||||
value={inputs.self_account_deletion_confirmation}
|
||||
onChange={(value) =>
|
||||
handleInputChange(
|
||||
'self_account_deletion_confirmation',
|
||||
value,
|
||||
value
|
||||
)
|
||||
}
|
||||
/>
|
||||
@@ -998,7 +1029,7 @@ const PersonalSetting = () => {
|
||||
>
|
||||
<div style={{ marginTop: 20 }}>
|
||||
<Input
|
||||
name='set_new_password'
|
||||
name="set_new_password"
|
||||
placeholder={t('新密码')}
|
||||
value={inputs.set_new_password}
|
||||
onChange={(value) =>
|
||||
@@ -1007,7 +1038,7 @@ const PersonalSetting = () => {
|
||||
/>
|
||||
<Input
|
||||
style={{ marginTop: 20 }}
|
||||
name='set_new_password_confirmation'
|
||||
name="set_new_password_confirmation"
|
||||
placeholder={t('确认新密码')}
|
||||
value={inputs.set_new_password_confirmation}
|
||||
onChange={(value) =>
|
||||
|
||||
Reference in New Issue
Block a user