Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,272 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useState, useRef } from 'react';
|
||||
import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
|
||||
import {
|
||||
compareObjects,
|
||||
API,
|
||||
showError,
|
||||
showSuccess,
|
||||
showWarning,
|
||||
verifyJSON,
|
||||
} from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Text from '@douyinfe/semi-ui/lib/es/typography/text';
|
||||
|
||||
const CLAUDE_HEADER = {
|
||||
'claude-3-7-sonnet-20250219-thinking': {
|
||||
'anthropic-beta': [
|
||||
'output-128k-2025-02-19',
|
||||
'token-efficient-tools-2025-02-19',
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const CLAUDE_DEFAULT_MAX_TOKENS = {
|
||||
default: 8192,
|
||||
'claude-3-haiku-20240307': 4096,
|
||||
'claude-3-opus-20240229': 4096,
|
||||
'claude-3-7-sonnet-20250219-thinking': 8192,
|
||||
};
|
||||
|
||||
export default function SettingClaudeModel(props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inputs, setInputs] = useState({
|
||||
'claude.model_headers_settings': '',
|
||||
'claude.thinking_adapter_enabled': true,
|
||||
'claude.default_max_tokens': '',
|
||||
'claude.thinking_adapter_budget_tokens_percentage': 0.8,
|
||||
'claude.cache_creation_5m_ratio': 1.25,
|
||||
'claude.cache_creation_1h_ratio': 2,
|
||||
});
|
||||
const refForm = useRef();
|
||||
const [inputsRow, setInputsRow] = useState(inputs);
|
||||
|
||||
function onSubmit() {
|
||||
const updateArray = compareObjects(inputs, inputsRow);
|
||||
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
|
||||
const requestQueue = updateArray.map((item) => {
|
||||
let value = String(inputs[item.key]);
|
||||
|
||||
return API.put('/api/option/', {
|
||||
key: item.key,
|
||||
value,
|
||||
});
|
||||
});
|
||||
setLoading(true);
|
||||
Promise.all(requestQueue)
|
||||
.then((res) => {
|
||||
if (requestQueue.length === 1) {
|
||||
if (res.includes(undefined)) return;
|
||||
} else if (requestQueue.length > 1) {
|
||||
if (res.includes(undefined))
|
||||
return showError(t('部分保存失败,请重试'));
|
||||
}
|
||||
// 后端校验失败返回 200 + success:false(如倍率填了负数),不能误报保存成功
|
||||
const failed = res.find((r) => !r?.data?.success);
|
||||
if (failed) {
|
||||
showError(failed?.data?.message || t('保存失败,请重试'));
|
||||
props.refresh();
|
||||
return;
|
||||
}
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
})
|
||||
.catch(() => {
|
||||
showError(t('保存失败,请重试'));
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const currentInputs = {};
|
||||
for (let key in props.options) {
|
||||
if (Object.keys(inputs).includes(key)) {
|
||||
currentInputs[key] = props.options[key];
|
||||
}
|
||||
}
|
||||
setInputs(currentInputs);
|
||||
setInputsRow(structuredClone(currentInputs));
|
||||
refForm.current.setValues(currentInputs);
|
||||
}, [props.options]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Spin spinning={loading}>
|
||||
<Form
|
||||
values={inputs}
|
||||
getFormApi={(formAPI) => (refForm.current = formAPI)}
|
||||
style={{ marginBottom: 15 }}
|
||||
>
|
||||
<Form.Section text={t('Claude设置')}>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.TextArea
|
||||
label={t('Claude请求头覆盖')}
|
||||
field={'claude.model_headers_settings'}
|
||||
placeholder={
|
||||
t('为一个 JSON 文本,例如:') +
|
||||
'\n' +
|
||||
JSON.stringify(CLAUDE_HEADER, null, 2)
|
||||
}
|
||||
extraText={
|
||||
t('示例') + '\n' + JSON.stringify(CLAUDE_HEADER, null, 2)
|
||||
}
|
||||
autosize={{ minRows: 6, maxRows: 12 }}
|
||||
trigger='blur'
|
||||
stopValidateWithError
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => verifyJSON(value),
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'claude.model_headers_settings': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.TextArea
|
||||
label={t('缺省 MaxTokens')}
|
||||
field={'claude.default_max_tokens'}
|
||||
placeholder={
|
||||
t('为一个 JSON 文本,例如:') +
|
||||
'\n' +
|
||||
JSON.stringify(CLAUDE_DEFAULT_MAX_TOKENS, null, 2)
|
||||
}
|
||||
extraText={
|
||||
t('示例') +
|
||||
'\n' +
|
||||
JSON.stringify(CLAUDE_DEFAULT_MAX_TOKENS, null, 2)
|
||||
}
|
||||
autosize={{ minRows: 6, maxRows: 12 }}
|
||||
trigger='blur'
|
||||
stopValidateWithError
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => verifyJSON(value),
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
onChange={(value) =>
|
||||
setInputs({ ...inputs, 'claude.default_max_tokens': value })
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
<Form.Switch
|
||||
label={t('启用Claude思考适配(-thinking后缀)')}
|
||||
field={'claude.thinking_adapter_enabled'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'claude.thinking_adapter_enabled': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
{/*//展示MaxTokens和BudgetTokens的计算公式, 并展示实际数字*/}
|
||||
<Text>
|
||||
{t(
|
||||
'Claude思考适配 BudgetTokens = MaxTokens * BudgetTokens 百分比',
|
||||
)}
|
||||
</Text>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.InputNumber
|
||||
label={t('思考适配 BudgetTokens 百分比')}
|
||||
field={'claude.thinking_adapter_budget_tokens_percentage'}
|
||||
initValue={''}
|
||||
extraText={t('0.1以上的小数')}
|
||||
min={0.1}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'claude.thinking_adapter_budget_tokens_percentage': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row gutter={16}>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.InputNumber
|
||||
label={t('5分钟缓存创建倍率')}
|
||||
field={'claude.cache_creation_5m_ratio'}
|
||||
initValue={''}
|
||||
extraText={t(
|
||||
'仅作用于 Claude 模型,相对输入价格的倍率;未区分时长的缓存创建也按此倍率计费',
|
||||
)}
|
||||
min={0}
|
||||
step={0.05}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'claude.cache_creation_5m_ratio': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.InputNumber
|
||||
label={t('1小时缓存创建倍率')}
|
||||
field={'claude.cache_creation_1h_ratio'}
|
||||
initValue={''}
|
||||
extraText={t('仅作用于 Claude 模型,相对输入价格的倍率')}
|
||||
min={0}
|
||||
step={0.05}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'claude.cache_creation_1h_ratio': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<Row>
|
||||
<Button size='default' onClick={onSubmit}>
|
||||
{t('保存')}
|
||||
</Button>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
</Form>
|
||||
</Spin>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useState, useRef } from 'react';
|
||||
import { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
|
||||
import {
|
||||
compareObjects,
|
||||
API,
|
||||
showError,
|
||||
showSuccess,
|
||||
showWarning,
|
||||
verifyJSON,
|
||||
} from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import Text from '@douyinfe/semi-ui/lib/es/typography/text';
|
||||
|
||||
const GEMINI_SETTING_EXAMPLE = {
|
||||
default: 'OFF',
|
||||
HARM_CATEGORY_CIVIC_INTEGRITY: 'BLOCK_NONE',
|
||||
};
|
||||
|
||||
const GEMINI_VERSION_EXAMPLE = {
|
||||
default: 'v1beta',
|
||||
};
|
||||
|
||||
const DEFAULT_GEMINI_INPUTS = {
|
||||
'gemini.safety_settings': '',
|
||||
'gemini.version_settings': '',
|
||||
'gemini.supported_imagine_models': '',
|
||||
'gemini.thinking_adapter_enabled': false,
|
||||
'gemini.thinking_adapter_budget_tokens_percentage': 0.6,
|
||||
'gemini.function_call_thought_signature_enabled': true,
|
||||
};
|
||||
|
||||
export default function SettingGeminiModel(props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inputs, setInputs] = useState(DEFAULT_GEMINI_INPUTS);
|
||||
const refForm = useRef();
|
||||
const [inputsRow, setInputsRow] = useState(DEFAULT_GEMINI_INPUTS);
|
||||
|
||||
async function onSubmit() {
|
||||
await refForm.current
|
||||
.validate()
|
||||
.then(() => {
|
||||
const updateArray = compareObjects(inputs, inputsRow);
|
||||
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
|
||||
const requestQueue = updateArray.map((item) => {
|
||||
let value = String(inputs[item.key]);
|
||||
return API.put('/api/option/', {
|
||||
key: item.key,
|
||||
value,
|
||||
});
|
||||
});
|
||||
setLoading(true);
|
||||
Promise.all(requestQueue)
|
||||
.then((res) => {
|
||||
if (requestQueue.length === 1) {
|
||||
if (res.includes(undefined)) return;
|
||||
} else if (requestQueue.length > 1) {
|
||||
if (res.includes(undefined))
|
||||
return showError(t('部分保存失败,请重试'));
|
||||
}
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
})
|
||||
.catch(() => {
|
||||
showError(t('保存失败,请重试'));
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Validation failed:', error);
|
||||
showError(t('请检查输入'));
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const currentInputs = { ...DEFAULT_GEMINI_INPUTS };
|
||||
for (let key in props.options) {
|
||||
if (Object.prototype.hasOwnProperty.call(DEFAULT_GEMINI_INPUTS, key)) {
|
||||
currentInputs[key] = props.options[key];
|
||||
}
|
||||
}
|
||||
setInputs(currentInputs);
|
||||
setInputsRow(structuredClone(currentInputs));
|
||||
refForm.current.setValues(currentInputs);
|
||||
}, [props.options]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Spin spinning={loading}>
|
||||
<Form
|
||||
values={inputs}
|
||||
getFormApi={(formAPI) => (refForm.current = formAPI)}
|
||||
style={{ marginBottom: 15 }}
|
||||
>
|
||||
<Form.Section text={t('Gemini设置')}>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.TextArea
|
||||
label={t('Gemini安全设置')}
|
||||
placeholder={
|
||||
t('为一个 JSON 文本,例如:') +
|
||||
'\n' +
|
||||
JSON.stringify(GEMINI_SETTING_EXAMPLE, null, 2)
|
||||
}
|
||||
field={'gemini.safety_settings'}
|
||||
extraText={t(
|
||||
'default为默认设置,可单独设置每个分类的安全等级',
|
||||
)}
|
||||
autosize={{ minRows: 6, maxRows: 12 }}
|
||||
trigger='blur'
|
||||
stopValidateWithError
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => verifyJSON(value),
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
onChange={(value) =>
|
||||
setInputs({ ...inputs, 'gemini.safety_settings': value })
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.TextArea
|
||||
label={t('Gemini版本设置')}
|
||||
placeholder={
|
||||
t('为一个 JSON 文本,例如:') +
|
||||
'\n' +
|
||||
JSON.stringify(GEMINI_VERSION_EXAMPLE, null, 2)
|
||||
}
|
||||
field={'gemini.version_settings'}
|
||||
extraText={t('default为默认设置,可单独设置每个模型的版本')}
|
||||
autosize={{ minRows: 6, maxRows: 12 }}
|
||||
trigger='blur'
|
||||
stopValidateWithError
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => verifyJSON(value),
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
onChange={(value) =>
|
||||
setInputs({ ...inputs, 'gemini.version_settings': value })
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
<Form.Switch
|
||||
label={t('启用FunctionCall思维签名填充')}
|
||||
field={'gemini.function_call_thought_signature_enabled'}
|
||||
extraText={t(
|
||||
'仅为使用OpenAI格式的Gemini/Vertex渠道填充thoughtSignature',
|
||||
)}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'gemini.function_call_thought_signature_enabled': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.TextArea
|
||||
field={'gemini.supported_imagine_models'}
|
||||
label={t('支持的图像模型')}
|
||||
placeholder={
|
||||
t('例如:') +
|
||||
'\n' +
|
||||
JSON.stringify(
|
||||
['gemini-2.0-flash-exp-image-generation'],
|
||||
null,
|
||||
2,
|
||||
)
|
||||
}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'gemini.supported_imagine_models': value,
|
||||
})
|
||||
}
|
||||
trigger='blur'
|
||||
stopValidateWithError
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => verifyJSON(value),
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
|
||||
<Form.Section text={t('Gemini思考适配设置')}>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
<Text>
|
||||
{t(
|
||||
'和Claude不同,默认情况下Gemini的思考模型会自动决定要不要思考,就算不开启适配模型也可以正常使用,' +
|
||||
'如果您需要计费,推荐设置无后缀模型价格按思考价格设置。' +
|
||||
'支持使用 gemini-2.5-pro-preview-06-05-thinking-128 格式来精确传递思考预算。',
|
||||
)}
|
||||
</Text>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
<Form.Switch
|
||||
label={t('启用Gemini思考后缀适配')}
|
||||
field={'gemini.thinking_adapter_enabled'}
|
||||
extraText={t(
|
||||
'适配 -thinking、-thinking-预算数字 和 -nothinking 后缀',
|
||||
)}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'gemini.thinking_adapter_enabled': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={16}>
|
||||
<Text>
|
||||
{t(
|
||||
'Gemini思考适配 BudgetTokens = MaxTokens * BudgetTokens 百分比',
|
||||
)}
|
||||
</Text>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.InputNumber
|
||||
label={t('思考预算占比')}
|
||||
field={'gemini.thinking_adapter_budget_tokens_percentage'}
|
||||
initValue={''}
|
||||
extraText={t('0.002-1之间的小数')}
|
||||
min={0.002}
|
||||
max={1}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'gemini.thinking_adapter_budget_tokens_percentage': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
|
||||
<Row>
|
||||
<Button size='default' onClick={onSubmit}>
|
||||
{t('保存')}
|
||||
</Button>
|
||||
</Row>
|
||||
</Form>
|
||||
</Spin>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useState, useRef } from 'react';
|
||||
import { Button, Col, Form, Row, Spin, Banner } from '@douyinfe/semi-ui';
|
||||
import {
|
||||
compareObjects,
|
||||
API,
|
||||
showError,
|
||||
showSuccess,
|
||||
showWarning,
|
||||
verifyJSON,
|
||||
} from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const thinkingExample = JSON.stringify(
|
||||
['moonshotai/kimi-k2-thinking', 'kimi-k2-thinking'],
|
||||
null,
|
||||
2,
|
||||
);
|
||||
|
||||
const defaultGlobalSettingInputs = {
|
||||
'global.pass_through_request_enabled': false,
|
||||
'global.thinking_model_blacklist': '[]',
|
||||
'general_setting.ping_interval_enabled': false,
|
||||
'general_setting.ping_interval_seconds': 60,
|
||||
'general_setting.force_responses_models': '',
|
||||
'general_setting.force_responses_reasoning_summary_enabled': true,
|
||||
'general_setting.hide_price_enabled': false,
|
||||
'general_setting.market_curated_only': false,
|
||||
};
|
||||
|
||||
const forceResponsesExample =
|
||||
'gpt-5-codex\ngpt-5.5\ngpt-5.5-codex\no3-pro\no3-deep-research';
|
||||
|
||||
export default function SettingGlobalModel(props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inputs, setInputs] = useState(defaultGlobalSettingInputs);
|
||||
const refForm = useRef();
|
||||
const [inputsRow, setInputsRow] = useState(defaultGlobalSettingInputs);
|
||||
|
||||
const normalizeValueBeforeSave = (key, value) => {
|
||||
if (key === 'global.thinking_model_blacklist') {
|
||||
const text = typeof value === 'string' ? value.trim() : '';
|
||||
return text === '' ? '[]' : value;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
function onSubmit() {
|
||||
const updateArray = compareObjects(inputs, inputsRow);
|
||||
if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
|
||||
const requestQueue = updateArray.map((item) => {
|
||||
const normalizedValue = normalizeValueBeforeSave(
|
||||
item.key,
|
||||
inputs[item.key],
|
||||
);
|
||||
let value = String(normalizedValue);
|
||||
|
||||
return API.put('/api/option/', {
|
||||
key: item.key,
|
||||
value,
|
||||
});
|
||||
});
|
||||
setLoading(true);
|
||||
Promise.all(requestQueue)
|
||||
.then((res) => {
|
||||
if (requestQueue.length === 1) {
|
||||
if (res.includes(undefined)) return;
|
||||
} else if (requestQueue.length > 1) {
|
||||
if (res.includes(undefined))
|
||||
return showError(t('部分保存失败,请重试'));
|
||||
}
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
})
|
||||
.catch(() => {
|
||||
showError(t('保存失败,请重试'));
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const currentInputs = {};
|
||||
for (const key of Object.keys(defaultGlobalSettingInputs)) {
|
||||
if (props.options[key] !== undefined) {
|
||||
let value = props.options[key];
|
||||
if (key === 'global.thinking_model_blacklist') {
|
||||
try {
|
||||
value =
|
||||
value && String(value).trim() !== ''
|
||||
? JSON.stringify(JSON.parse(value), null, 2)
|
||||
: defaultGlobalSettingInputs[key];
|
||||
} catch (error) {
|
||||
value = defaultGlobalSettingInputs[key];
|
||||
}
|
||||
}
|
||||
currentInputs[key] = value;
|
||||
} else {
|
||||
currentInputs[key] = defaultGlobalSettingInputs[key];
|
||||
}
|
||||
}
|
||||
|
||||
setInputs(currentInputs);
|
||||
setInputsRow(structuredClone(currentInputs));
|
||||
if (refForm.current) {
|
||||
refForm.current.setValues(currentInputs);
|
||||
}
|
||||
}, [props.options]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Spin spinning={loading}>
|
||||
<Form
|
||||
values={inputs}
|
||||
getFormApi={(formAPI) => (refForm.current = formAPI)}
|
||||
style={{ marginBottom: 15 }}
|
||||
>
|
||||
<Form.Section text={t('全局设置')}>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.Switch
|
||||
label={t('启用请求透传')}
|
||||
field={'global.pass_through_request_enabled'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'global.pass_through_request_enabled': value,
|
||||
})
|
||||
}
|
||||
extraText={t(
|
||||
'开启后,所有请求将直接透传给上游,不会进行任何处理(重定向和渠道适配也将失效),请谨慎开启',
|
||||
)}
|
||||
/>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.Switch
|
||||
label={t('隐藏模型价格')}
|
||||
field={'general_setting.hide_price_enabled'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.hide_price_enabled': value,
|
||||
})
|
||||
}
|
||||
extraText={t(
|
||||
'开启后,定价页与模型详情弹窗将对普通用户和访客隐藏所有模型价格,管理员仍可见',
|
||||
)}
|
||||
/>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.Switch
|
||||
label={t('模型超市仅展示已建档模型')}
|
||||
field={'general_setting.market_curated_only'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.market_curated_only': value,
|
||||
})
|
||||
}
|
||||
extraText={t(
|
||||
'开启后,模型超市仅对普通用户展示已在「模型管理」建档且启用的模型,新增渠道模型建档后才上架,管理员仍可见全部',
|
||||
)}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Form.TextArea
|
||||
label={t('禁用思考处理的模型列表')}
|
||||
field={'global.thinking_model_blacklist'}
|
||||
placeholder={t('例如:') + '\n' + thinkingExample}
|
||||
rows={4}
|
||||
rules={[
|
||||
{
|
||||
validator: (rule, value) => {
|
||||
if (!value || value.trim() === '') return true;
|
||||
return verifyJSON(value);
|
||||
},
|
||||
message: t('不是合法的 JSON 字符串'),
|
||||
},
|
||||
]}
|
||||
extraText={t(
|
||||
'列出的模型将不会自动添加或移除-thinking/-nothinking 后缀',
|
||||
)}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'global.thinking_model_blacklist': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Banner
|
||||
type='info'
|
||||
description={
|
||||
<div>
|
||||
<p>
|
||||
<strong>{t('强制 Responses 协议路由说明:')}</strong>
|
||||
</p>
|
||||
<ul style={{ marginLeft: 20, marginTop: 4 }}>
|
||||
<li>
|
||||
{t(
|
||||
'配置后,外部 SDK / Cursor / Codex CLI / Cline 等以 /v1/chat/completions 请求这些模型时,后端会自动透明转换为 /v1/responses 调用上游',
|
||||
)}
|
||||
</li>
|
||||
<li>
|
||||
{t(
|
||||
'响应会反向翻译回标准 Chat Completion 格式,客户端零感知',
|
||||
)}
|
||||
</li>
|
||||
<li>
|
||||
{t('体验中心选用这些模型也会自动走 Responses 协议')}
|
||||
</li>
|
||||
<li>
|
||||
{t(
|
||||
'匹配规则:模型名包含列表中的任一关键字即命中(与 OpenAIResponseOnlyModels 一致)',
|
||||
)}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
bordered
|
||||
fullMode={false}
|
||||
closeIcon={null}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Form.TextArea
|
||||
label={t('强制 Responses 协议的模型列表')}
|
||||
field={'general_setting.force_responses_models'}
|
||||
placeholder={t('例如:') + '\n' + forceResponsesExample}
|
||||
rows={4}
|
||||
extraText={t('多个模型用逗号或换行分隔。留空则该功能不生效')}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.force_responses_models': value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Form.Switch
|
||||
label={t('转换时返回思考摘要')}
|
||||
field={
|
||||
'general_setting.force_responses_reasoning_summary_enabled'
|
||||
}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.force_responses_reasoning_summary_enabled':
|
||||
value,
|
||||
})
|
||||
}
|
||||
extraText={t(
|
||||
'开启后,带推理参数的 Chat 请求转为 Responses 协议时会代为请求思考摘要,并以 reasoning_content 返回;推理力度始终透传,不受此开关影响。渠道不支持思考摘要时(如 OpenAI 组织未认证)会自动去掉摘要重试,并对该渠道暂停请求摘要 1 小时',
|
||||
)}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<Form.Section text={t('连接保活设置')}>
|
||||
<Row style={{ marginTop: 10 }}>
|
||||
<Col span={24}>
|
||||
<Banner
|
||||
type='warning'
|
||||
description={t(
|
||||
'警告:启用保活后,如果已经写入保活数据后渠道出错,系统无法重试,如果必须开启,推荐设置尽可能大的Ping间隔',
|
||||
)}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.Switch
|
||||
label={t('启用Ping间隔')}
|
||||
field={'general_setting.ping_interval_enabled'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.ping_interval_enabled': value,
|
||||
})
|
||||
}
|
||||
extraText={t('开启后,将定期发送ping数据保持连接活跃')}
|
||||
/>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} md={8} lg={8} xl={8}>
|
||||
<Form.InputNumber
|
||||
label={t('Ping间隔(秒)')}
|
||||
field={'general_setting.ping_interval_seconds'}
|
||||
onChange={(value) =>
|
||||
setInputs({
|
||||
...inputs,
|
||||
'general_setting.ping_interval_seconds': value,
|
||||
})
|
||||
}
|
||||
min={1}
|
||||
disabled={!inputs['general_setting.ping_interval_enabled']}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
|
||||
<Row>
|
||||
<Button size='default' onClick={onSubmit}>
|
||||
{t('保存')}
|
||||
</Button>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
</Form>
|
||||
</Spin>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,569 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
Button,
|
||||
Col,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
Modal,
|
||||
Popconfirm,
|
||||
Row,
|
||||
Space,
|
||||
Spin,
|
||||
Switch,
|
||||
Table,
|
||||
Tag,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconDelete, IconEdit, IconPlus } from '@douyinfe/semi-icons';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, showError, showSuccess } from '../../../helpers';
|
||||
|
||||
const OPTION_KEY = 'image.image_resolution_pricing';
|
||||
const MATCH_MODE_OPTIONS = [
|
||||
{ value: 'exact', label: '精确匹配' },
|
||||
{ value: 'prefix', label: '前缀匹配' },
|
||||
];
|
||||
const RESOLUTION_RULE_OPTIONS = [
|
||||
{
|
||||
value: 'manual',
|
||||
label: '手工别名',
|
||||
requiredTiers: [],
|
||||
help: '仅按档位 key 和别名匹配;未命中时使用默认档位。',
|
||||
},
|
||||
{
|
||||
value: 'gpt_image_2',
|
||||
label: 'GPT Image 2',
|
||||
requiredTiers: ['1k', '2k', '4k'],
|
||||
help: '识别官方尺寸及合法自定义宽高,自定义尺寸按总像素向上取档。',
|
||||
},
|
||||
{
|
||||
value: 'gemini',
|
||||
label: 'Gemini',
|
||||
requiredTiers: ['1k', '2k', '4k'],
|
||||
help: '识别 imageSize、官方比例尺寸及自定义宽高。0.5K 按 1K 档计费。',
|
||||
},
|
||||
{
|
||||
value: 'seedream_4_0',
|
||||
label: 'Seedream 4.0',
|
||||
requiredTiers: ['1k', '2k', '4k'],
|
||||
help: '按 Seedream 4.0 官方 1K、2K、4K 尺寸和自定义宽高识别。',
|
||||
},
|
||||
{
|
||||
value: 'seedream_4_5',
|
||||
label: 'Seedream 4.5',
|
||||
requiredTiers: ['2k', '4k'],
|
||||
help: '按 Seedream 4.5 官方 2K、4K 尺寸和自定义宽高识别。',
|
||||
},
|
||||
{
|
||||
value: 'seedream_5_0_pro',
|
||||
label: 'Seedream 5.0 Pro',
|
||||
requiredTiers: ['1k', '2k'],
|
||||
help: '识别 1K、1.5K、2K,1.5K 按 1K 档计费。',
|
||||
},
|
||||
{
|
||||
value: 'seedream_5_0_lite',
|
||||
label: 'Seedream 5.0 Lite',
|
||||
requiredTiers: ['2k', '3k', '4k'],
|
||||
help: '按 Seedream 5.0 Lite 官方 2K、3K、4K 尺寸和自定义宽高识别。',
|
||||
},
|
||||
];
|
||||
|
||||
const emptyTier = () => ({
|
||||
key: '',
|
||||
label: '',
|
||||
price_per_image: 0,
|
||||
aliases: '',
|
||||
});
|
||||
|
||||
const normalize = (value) =>
|
||||
String(value || '')
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replaceAll(' ', '');
|
||||
|
||||
const parsePricing = (value) => {
|
||||
try {
|
||||
const parsed = JSON.parse(value || '[]');
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const resolutionRuleOption = (value) =>
|
||||
RESOLUTION_RULE_OPTIONS.find((option) => option.value === value) ||
|
||||
RESOLUTION_RULE_OPTIONS[0];
|
||||
|
||||
export default function SettingImagePricing({ options, refresh }) {
|
||||
const { t } = useTranslation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [configs, setConfigs] = useState([]);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [editingIndex, setEditingIndex] = useState(-1);
|
||||
const [resolutionRule, setResolutionRule] = useState('manual');
|
||||
const [tierRows, setTierRows] = useState([emptyTier()]);
|
||||
const formRef = useRef();
|
||||
|
||||
useEffect(() => {
|
||||
setConfigs(parsePricing(options?.[OPTION_KEY]));
|
||||
}, [options]);
|
||||
|
||||
const save = async (next) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const response = await API.put('/api/option/', {
|
||||
key: OPTION_KEY,
|
||||
value: JSON.stringify(next),
|
||||
});
|
||||
if (!response.data.success) {
|
||||
showError(response.data.message || t('保存失败'));
|
||||
return false;
|
||||
}
|
||||
setConfigs(next);
|
||||
showSuccess(t('保存成功'));
|
||||
refresh();
|
||||
return true;
|
||||
} catch (error) {
|
||||
showError(error?.response?.data?.message || t('保存失败,请重试'));
|
||||
return false;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const openAdd = () => {
|
||||
setEditingIndex(-1);
|
||||
setResolutionRule('manual');
|
||||
setTierRows([emptyTier()]);
|
||||
setModalVisible(true);
|
||||
setTimeout(() => {
|
||||
formRef.current?.setValues({
|
||||
model_name: '',
|
||||
match_mode: 'exact',
|
||||
resolution_rule: 'manual',
|
||||
default_tier: '',
|
||||
enabled: true,
|
||||
});
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const openEdit = (record, index) => {
|
||||
const nextResolutionRule = record.resolution_rule || 'manual';
|
||||
setEditingIndex(index);
|
||||
setResolutionRule(nextResolutionRule);
|
||||
setTierRows(
|
||||
(record.tiers || []).map((tier) => ({
|
||||
key: tier.key || '',
|
||||
label: tier.label || '',
|
||||
price_per_image: tier.price_per_image || 0,
|
||||
aliases: Array.isArray(tier.aliases) ? tier.aliases.join(',') : '',
|
||||
})),
|
||||
);
|
||||
setModalVisible(true);
|
||||
setTimeout(() => {
|
||||
formRef.current?.setValues({
|
||||
model_name: record.model_name || '',
|
||||
match_mode: record.match_mode || 'exact',
|
||||
resolution_rule: nextResolutionRule,
|
||||
default_tier: record.default_tier || '',
|
||||
enabled: record.enabled !== false,
|
||||
});
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const updateTier = (index, patch) => {
|
||||
setTierRows((rows) =>
|
||||
rows.map((row, rowIndex) =>
|
||||
rowIndex === index ? { ...row, ...patch } : row,
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const buildConfig = (values) => {
|
||||
const modelName = String(values.model_name || '').trim();
|
||||
if (
|
||||
configs.some(
|
||||
(config, index) =>
|
||||
index !== editingIndex &&
|
||||
normalize(config.model_name) === normalize(modelName),
|
||||
)
|
||||
) {
|
||||
showError(t('该模型已配置图片分档计费'));
|
||||
return null;
|
||||
}
|
||||
if (tierRows.length === 0 || tierRows.some((row) => !normalize(row.key))) {
|
||||
showError(t('每个档位都必须填写档位 key'));
|
||||
return null;
|
||||
}
|
||||
|
||||
const tiers = tierRows.map((row) => ({
|
||||
key: String(row.key).trim(),
|
||||
label: String(row.label || row.key).trim(),
|
||||
price_per_image: Number(row.price_per_image),
|
||||
aliases: String(row.aliases || '')
|
||||
.split(',')
|
||||
.map((alias) => alias.trim())
|
||||
.filter(Boolean),
|
||||
}));
|
||||
if (tiers.some((tier) => !(tier.price_per_image > 0))) {
|
||||
showError(t('每个档位的每张单价必须大于 0'));
|
||||
return null;
|
||||
}
|
||||
|
||||
const seen = new Set();
|
||||
for (const tier of tiers) {
|
||||
for (const matchValue of [tier.key, ...tier.aliases]) {
|
||||
const key = normalize(matchValue);
|
||||
if (seen.has(key)) {
|
||||
showError(t('同一模型内的档位 key 和别名不能重复'));
|
||||
return null;
|
||||
}
|
||||
seen.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
const selectedRule = values.resolution_rule || 'manual';
|
||||
const missingTier = resolutionRuleOption(selectedRule).requiredTiers.find(
|
||||
(requiredTier) =>
|
||||
!tiers.some((tier) => normalize(tier.key) === requiredTier),
|
||||
);
|
||||
if (missingTier) {
|
||||
showError(t('当前识别规则要求配置 {{tier}} 档位', { tier: missingTier }));
|
||||
return null;
|
||||
}
|
||||
|
||||
const defaultTier = String(values.default_tier || tiers[0].key).trim();
|
||||
if (!tiers.some((tier) => normalize(tier.key) === normalize(defaultTier))) {
|
||||
showError(t('默认档位必须填写一个已有的档位 key'));
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
model_name: modelName,
|
||||
match_mode: values.match_mode || 'exact',
|
||||
resolution_rule: selectedRule,
|
||||
enabled: values.enabled !== false,
|
||||
default_tier: defaultTier,
|
||||
tiers,
|
||||
};
|
||||
};
|
||||
|
||||
const submit = () => {
|
||||
formRef.current
|
||||
?.validate()
|
||||
.then(async (values) => {
|
||||
const config = buildConfig(values);
|
||||
if (!config) return;
|
||||
const next = [...configs];
|
||||
if (editingIndex >= 0) {
|
||||
next[editingIndex] = config;
|
||||
} else {
|
||||
next.push(config);
|
||||
}
|
||||
if (await save(next)) setModalVisible(false);
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
|
||||
const remove = async (index) => {
|
||||
const next = configs.filter((_, configIndex) => configIndex !== index);
|
||||
await save(next);
|
||||
};
|
||||
|
||||
const toggle = async (index, enabled) => {
|
||||
const next = configs.map((config, configIndex) =>
|
||||
configIndex === index ? { ...config, enabled } : config,
|
||||
);
|
||||
await save(next);
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: t('模型名称'),
|
||||
dataIndex: 'model_name',
|
||||
key: 'model_name',
|
||||
},
|
||||
{
|
||||
title: t('匹配方式'),
|
||||
dataIndex: 'match_mode',
|
||||
key: 'match_mode',
|
||||
render: (matchMode) =>
|
||||
t(matchMode === 'prefix' ? '前缀匹配' : '精确匹配'),
|
||||
},
|
||||
{
|
||||
title: t('识别规则'),
|
||||
dataIndex: 'resolution_rule',
|
||||
key: 'resolution_rule',
|
||||
render: (rule) => t(resolutionRuleOption(rule || 'manual').label),
|
||||
},
|
||||
{
|
||||
title: t('默认档位'),
|
||||
dataIndex: 'default_tier',
|
||||
key: 'default_tier',
|
||||
},
|
||||
{
|
||||
title: t('分辨率档位'),
|
||||
dataIndex: 'tiers',
|
||||
key: 'tiers',
|
||||
render: (tiers) => (
|
||||
<Space wrap>
|
||||
{(tiers || []).map((tier) => (
|
||||
<Tag key={tier.key} color='blue'>
|
||||
{`${tier.label || tier.key}: $${tier.price_per_image}/张`}
|
||||
</Tag>
|
||||
))}
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('启用'),
|
||||
dataIndex: 'enabled',
|
||||
key: 'enabled',
|
||||
render: (enabled, _record, index) => (
|
||||
<Switch
|
||||
checked={enabled !== false}
|
||||
onChange={(checked) => toggle(index, checked)}
|
||||
aria-label={t('启用图片分档计费')}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
key: 'action',
|
||||
width: 110,
|
||||
render: (_text, record, index) => (
|
||||
<Space>
|
||||
<Button
|
||||
icon={<IconEdit />}
|
||||
size='small'
|
||||
theme='borderless'
|
||||
aria-label={t('编辑')}
|
||||
onClick={() => openEdit(record, index)}
|
||||
/>
|
||||
<Popconfirm
|
||||
title={t('确定删除此图片定价配置吗?')}
|
||||
onConfirm={() => remove(index)}
|
||||
>
|
||||
<Button
|
||||
icon={<IconDelete />}
|
||||
size='small'
|
||||
type='danger'
|
||||
theme='borderless'
|
||||
aria-label={t('删除')}
|
||||
/>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
const currentRule = resolutionRuleOption(resolutionRule);
|
||||
|
||||
return (
|
||||
<Spin spinning={loading}>
|
||||
<Form>
|
||||
<Form.Section text={t('图片分辨率分档计费')}>
|
||||
<Typography.Text type='tertiary'>
|
||||
{t(
|
||||
'仅已启用且匹配的模型按分辨率档位计费,未配置模型继续使用现有固定价格或模型倍率。单价单位为美元/张,最终费用仍乘分组倍率。',
|
||||
)}
|
||||
</Typography.Text>
|
||||
<div style={{ marginTop: 12, marginBottom: 12 }}>
|
||||
<Button icon={<IconPlus />} onClick={openAdd}>
|
||||
{t('添加图片计费模型')}
|
||||
</Button>
|
||||
</div>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={configs}
|
||||
rowKey={(record, index) => `${record.model_name}-${index}`}
|
||||
pagination={false}
|
||||
empty={t('暂无图片分档计费配置')}
|
||||
/>
|
||||
</Form.Section>
|
||||
</Form>
|
||||
|
||||
<Modal
|
||||
title={editingIndex >= 0 ? t('编辑图片定价') : t('添加图片定价')}
|
||||
visible={modalVisible}
|
||||
onOk={submit}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
okText={t('保存')}
|
||||
cancelText={t('取消')}
|
||||
width={760}
|
||||
confirmLoading={loading}
|
||||
>
|
||||
<Form
|
||||
getFormApi={(formApi) => (formRef.current = formApi)}
|
||||
initValues={{
|
||||
model_name: '',
|
||||
match_mode: 'exact',
|
||||
resolution_rule: 'manual',
|
||||
default_tier: '',
|
||||
enabled: true,
|
||||
}}
|
||||
>
|
||||
<Form.Input
|
||||
field='model_name'
|
||||
label={t('模型名称')}
|
||||
placeholder={t('例如 gpt-image-2-count')}
|
||||
rules={[{ required: true, message: t('请输入模型名称') }]}
|
||||
extraText={t('填写平台对外暴露的模型名称。')}
|
||||
/>
|
||||
<Row gutter={12}>
|
||||
<Col span={12}>
|
||||
<Form.Select
|
||||
field='match_mode'
|
||||
label={t('模型匹配方式')}
|
||||
extraText={t('精确匹配不会影响同名前缀的其他模型。')}
|
||||
>
|
||||
{MATCH_MODE_OPTIONS.map((option) => (
|
||||
<Form.Select.Option key={option.value} value={option.value}>
|
||||
{t(option.label)}
|
||||
</Form.Select.Option>
|
||||
))}
|
||||
</Form.Select>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Select
|
||||
field='resolution_rule'
|
||||
label={t('分辨率识别规则')}
|
||||
onChange={(value) => setResolutionRule(value)}
|
||||
extraText={t(currentRule.help)}
|
||||
>
|
||||
{RESOLUTION_RULE_OPTIONS.map((option) => (
|
||||
<Form.Select.Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Form.Select.Option>
|
||||
))}
|
||||
</Form.Select>
|
||||
</Col>
|
||||
</Row>
|
||||
<Form.Input
|
||||
field='default_tier'
|
||||
label={t('默认档位')}
|
||||
placeholder={t('留空时使用第一个档位')}
|
||||
extraText={t(
|
||||
'请求未提供尺寸或传入 auto 时使用;自动规则下,非法的显式尺寸会直接返回 invalid_size。',
|
||||
)}
|
||||
/>
|
||||
<Form.Switch field='enabled' label={t('启用')} />
|
||||
</Form>
|
||||
|
||||
<div
|
||||
style={{
|
||||
marginTop: 8,
|
||||
padding: '8px 12px',
|
||||
borderRadius: 6,
|
||||
background: 'var(--semi-color-fill-0)',
|
||||
color: 'var(--semi-color-text-2)',
|
||||
fontSize: 12,
|
||||
lineHeight: 1.6,
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
{currentRule.requiredTiers.length > 0
|
||||
? t('当前规则必须配置档位:{{tiers}}', {
|
||||
tiers: currentRule.requiredTiers.join('、'),
|
||||
})
|
||||
: t('手工规则可自定义档位 key,并通过别名匹配请求尺寸。')}
|
||||
</div>
|
||||
<div>
|
||||
{t('别名始终优先于自动识别,可用于覆盖个别尺寸的计费档位。')}
|
||||
</div>
|
||||
<div>{t('自定义宽高按总像素向上取最近档位,避免低档计费。')}</div>
|
||||
</div>
|
||||
|
||||
<div style={{ marginTop: 14 }}>
|
||||
<Typography.Text strong>{t('档位列表')}</Typography.Text>
|
||||
{tierRows.map((row, index) => (
|
||||
<Row
|
||||
key={index}
|
||||
gutter={8}
|
||||
type='flex'
|
||||
align='middle'
|
||||
style={{ marginTop: 8 }}
|
||||
>
|
||||
<Col span={5}>
|
||||
<Input
|
||||
value={row.key}
|
||||
placeholder={t('档位 key')}
|
||||
onChange={(value) => updateTier(index, { key: value })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={5}>
|
||||
<Input
|
||||
value={row.label}
|
||||
placeholder={t('显示名称')}
|
||||
onChange={(value) => updateTier(index, { label: value })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={5}>
|
||||
<InputNumber
|
||||
value={row.price_per_image}
|
||||
placeholder={t('单价/张')}
|
||||
prefix='$'
|
||||
min={0}
|
||||
step={0.01}
|
||||
style={{ width: '100%' }}
|
||||
onChange={(value) =>
|
||||
updateTier(index, { price_per_image: value })
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={7}>
|
||||
<Input
|
||||
value={row.aliases}
|
||||
placeholder={t('别名,英文逗号分隔')}
|
||||
onChange={(value) => updateTier(index, { aliases: value })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={2}>
|
||||
<Button
|
||||
icon={<IconDelete />}
|
||||
size='small'
|
||||
type='danger'
|
||||
theme='borderless'
|
||||
disabled={tierRows.length === 1}
|
||||
aria-label={t('删除档位')}
|
||||
onClick={() =>
|
||||
setTierRows((rows) =>
|
||||
rows.filter((_, rowIndex) => rowIndex !== index),
|
||||
)
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
))}
|
||||
<Button
|
||||
icon={<IconPlus />}
|
||||
size='small'
|
||||
style={{ marginTop: 10 }}
|
||||
onClick={() => setTierRows((rows) => [...rows, emptyTier()])}
|
||||
>
|
||||
{t('添加档位')}
|
||||
</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
</Spin>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Button,
|
||||
Table,
|
||||
Modal,
|
||||
Form,
|
||||
Space,
|
||||
Typography,
|
||||
Tabs,
|
||||
TabPane,
|
||||
Popconfirm,
|
||||
Select,
|
||||
Empty,
|
||||
Spin,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconPlus } from '@douyinfe/semi-icons';
|
||||
import { API, showError, showSuccess } from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const { Title, Text } = Typography;
|
||||
|
||||
const SettingModelCategory = () => {
|
||||
const { t } = useTranslation();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [level1Categories, setLevel1Categories] = useState([]);
|
||||
const [level2Categories, setLevel2Categories] = useState([]);
|
||||
const [allLevel2Categories, setAllLevel2Categories] = useState([]);
|
||||
|
||||
// 模态框相关
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [editingCategory, setEditingCategory] = useState(null);
|
||||
const [isLevel1Modal, setIsLevel1Modal] = useState(true);
|
||||
|
||||
// 二级分类选择的一级分类
|
||||
const [selectedLevel1Id, setSelectedLevel1Id] = useState(null);
|
||||
|
||||
const loadCategories = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.get('/api/model_categories/flat');
|
||||
const { success, data, message } = res.data;
|
||||
if (success) {
|
||||
const categoriesData = data || [];
|
||||
|
||||
// 分离一级和二级分类
|
||||
const level1 = categoriesData.filter((cat) => cat.parent_id === 0);
|
||||
const level2 = categoriesData.filter((cat) => cat.parent_id !== 0);
|
||||
|
||||
setLevel1Categories(level1);
|
||||
setAllLevel2Categories(level2);
|
||||
|
||||
// 如果已选择一级分类,更新二级分类列表
|
||||
if (selectedLevel1Id) {
|
||||
const filteredLevel2 = level2.filter(
|
||||
(cat) => cat.parent_id === selectedLevel1Id,
|
||||
);
|
||||
setLevel2Categories(filteredLevel2);
|
||||
}
|
||||
} else {
|
||||
showError(message || t('加载分类失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('加载分类失败: ') + error.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadCategories();
|
||||
}, []);
|
||||
|
||||
// 选择一级分类时更新二级分类列表
|
||||
const handleLevel1Select = (value) => {
|
||||
setSelectedLevel1Id(value);
|
||||
const filteredLevel2 = allLevel2Categories.filter(
|
||||
(cat) => cat.parent_id === value,
|
||||
);
|
||||
setLevel2Categories(filteredLevel2);
|
||||
};
|
||||
|
||||
// 打开新建一级分类模态框
|
||||
const handleAddLevel1 = () => {
|
||||
setIsLevel1Modal(true);
|
||||
setEditingCategory({
|
||||
id: 0,
|
||||
parent_id: 0,
|
||||
category_name: '',
|
||||
category_key: '',
|
||||
description: '',
|
||||
sort: 0,
|
||||
status: 1,
|
||||
});
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 打开编辑一级分类模态框
|
||||
const handleEditLevel1 = (category) => {
|
||||
setIsLevel1Modal(true);
|
||||
setEditingCategory({ ...category });
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 打开新建二级分类模态框
|
||||
const handleAddLevel2 = () => {
|
||||
if (!selectedLevel1Id) {
|
||||
showError(t('请先选择一级分类'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 找到选中的一级分类名称
|
||||
const selectedLevel1 = level1Categories.find(
|
||||
(cat) => cat.id === selectedLevel1Id,
|
||||
);
|
||||
|
||||
setIsLevel1Modal(false);
|
||||
setEditingCategory({
|
||||
id: 0,
|
||||
parent_id: selectedLevel1Id,
|
||||
parent_category_name: selectedLevel1?.category_name || '',
|
||||
category_name: '',
|
||||
category_key: '',
|
||||
description: '',
|
||||
sort: 0,
|
||||
status: 1,
|
||||
});
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 打开编辑二级分类模态框
|
||||
const handleEditLevel2 = (category) => {
|
||||
setIsLevel1Modal(false);
|
||||
setEditingCategory({ ...category });
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 保存分类
|
||||
const handleSave = async (values) => {
|
||||
try {
|
||||
let res;
|
||||
if (editingCategory.id === 0) {
|
||||
// 创建
|
||||
res = await API.post('/api/model_categories/', values);
|
||||
} else {
|
||||
// 更新
|
||||
res = await API.put('/api/model_categories/', {
|
||||
...values,
|
||||
id: editingCategory.id,
|
||||
});
|
||||
}
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess(editingCategory.id === 0 ? t('创建成功') : t('保存成功'));
|
||||
setModalVisible(false);
|
||||
await loadCategories();
|
||||
} else {
|
||||
showError(
|
||||
message || (editingCategory.id === 0 ? t('创建失败') : t('保存失败')),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
showError(
|
||||
(editingCategory.id === 0 ? t('创建失败: ') : t('保存失败: ')) +
|
||||
error.message,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// 删除分类
|
||||
const handleDelete = async (id) => {
|
||||
try {
|
||||
const res = await API.delete(`/api/model_categories/${id}`);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess(t('删除成功'));
|
||||
await loadCategories();
|
||||
} else {
|
||||
showError(message || t('删除失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('删除失败: ') + error.message);
|
||||
}
|
||||
};
|
||||
|
||||
// 重置分类
|
||||
const handleReset = async () => {
|
||||
try {
|
||||
const res = await API.post('/api/model_categories/reset');
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess(t('分类已重置为默认配置'));
|
||||
await loadCategories();
|
||||
} else {
|
||||
showError(message || t('重置失败'));
|
||||
}
|
||||
} catch (error) {
|
||||
showError(t('重置失败: ') + error.message);
|
||||
}
|
||||
};
|
||||
|
||||
// 一级分类表格列
|
||||
const level1Columns = [
|
||||
{
|
||||
title: t('分类名称'),
|
||||
dataIndex: 'category_name',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: t('分类标识'),
|
||||
dataIndex: 'category_key',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: t('描述'),
|
||||
dataIndex: 'description',
|
||||
},
|
||||
{
|
||||
title: t('排序'),
|
||||
dataIndex: 'sort',
|
||||
width: 80,
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
dataIndex: 'operate',
|
||||
width: 150,
|
||||
render: (text, record) => (
|
||||
<Space>
|
||||
<Button size='small' onClick={() => handleEditLevel1(record)}>
|
||||
{t('修改')}
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title={t('确定要删除这个分类吗?')}
|
||||
content={t('删除前请确保已删除其下的所有二级分类')}
|
||||
onConfirm={() => handleDelete(record.id)}
|
||||
okText={t('确定')}
|
||||
cancelText={t('取消')}
|
||||
>
|
||||
<Button size='small' type='danger'>
|
||||
{t('删除')}
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 二级分类表格列
|
||||
const level2Columns = [
|
||||
{
|
||||
title: t('分类名称'),
|
||||
dataIndex: 'category_name',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: t('分类标识'),
|
||||
dataIndex: 'category_key',
|
||||
width: 150,
|
||||
},
|
||||
{
|
||||
title: t('描述'),
|
||||
dataIndex: 'description',
|
||||
},
|
||||
{
|
||||
title: t('排序'),
|
||||
dataIndex: 'sort',
|
||||
width: 80,
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
dataIndex: 'operate',
|
||||
width: 150,
|
||||
render: (text, record) => (
|
||||
<Space>
|
||||
<Button size='small' onClick={() => handleEditLevel2(record)}>
|
||||
{t('修改')}
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title={t('确定要删除这个分类吗?')}
|
||||
content={t('此操作不可恢复')}
|
||||
onConfirm={() => handleDelete(record.id)}
|
||||
okText={t('确定')}
|
||||
cancelText={t('取消')}
|
||||
>
|
||||
<Button size='small' type='danger'>
|
||||
{t('删除')}
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<Spin spinning={loading}>
|
||||
<div>
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
marginBottom: 16,
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
<Title heading={5}>{t('模型分类管理')}</Title>
|
||||
<Text type='tertiary'>
|
||||
{t('管理模型的一级分类和二级分类,用于模型标签化管理')}
|
||||
</Text>
|
||||
</div>
|
||||
<Popconfirm
|
||||
title={t('确定要重置分类吗?')}
|
||||
content={t('这将删除所有自定义分类并恢复为默认配置')}
|
||||
onConfirm={handleReset}
|
||||
okText={t('确定')}
|
||||
cancelText={t('取消')}
|
||||
>
|
||||
<Button type='tertiary'>{t('重置为默认分类')}</Button>
|
||||
</Popconfirm>
|
||||
</div>
|
||||
|
||||
<Tabs type='line'>
|
||||
{/* 一级分类 */}
|
||||
<TabPane tab={t('一级分类')} itemKey='level1'>
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'flex-end',
|
||||
marginBottom: 16,
|
||||
}}
|
||||
>
|
||||
<Button
|
||||
type='primary'
|
||||
icon={<IconPlus />}
|
||||
onClick={handleAddLevel1}
|
||||
>
|
||||
{t('新建')}
|
||||
</Button>
|
||||
</div>
|
||||
<Table
|
||||
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
|
||||
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
|
||||
scroll={{ x: 'max-content' }}
|
||||
columns={level1Columns}
|
||||
dataSource={level1Categories}
|
||||
rowKey='id'
|
||||
pagination={false}
|
||||
empty={<Empty description={t('暂无数据')} />}
|
||||
/>
|
||||
</TabPane>
|
||||
|
||||
{/* 二级分类 */}
|
||||
<TabPane tab={t('二级分类')} itemKey='level2'>
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'center',
|
||||
marginBottom: 16,
|
||||
}}
|
||||
>
|
||||
<div style={{ flex: 1, maxWidth: 300 }}>
|
||||
<Select
|
||||
placeholder={t('选择一级分类')}
|
||||
value={selectedLevel1Id}
|
||||
onChange={handleLevel1Select}
|
||||
style={{ width: '100%' }}
|
||||
>
|
||||
{level1Categories.map((cat) => (
|
||||
<Select.Option key={cat.id} value={cat.id}>
|
||||
{cat.category_name}
|
||||
</Select.Option>
|
||||
))}
|
||||
</Select>
|
||||
</div>
|
||||
<Button
|
||||
type='primary'
|
||||
icon={<IconPlus />}
|
||||
onClick={handleAddLevel2}
|
||||
disabled={!selectedLevel1Id}
|
||||
>
|
||||
{t('新建二级分类')}
|
||||
</Button>
|
||||
</div>
|
||||
{selectedLevel1Id ? (
|
||||
<Table
|
||||
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
|
||||
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
|
||||
scroll={{ x: 'max-content' }}
|
||||
columns={level2Columns}
|
||||
dataSource={level2Categories}
|
||||
rowKey='id'
|
||||
pagination={false}
|
||||
empty={<Empty description={t('暂无数据')} />}
|
||||
/>
|
||||
) : (
|
||||
<Empty description={t('请先选择一级分类')} />
|
||||
)}
|
||||
</TabPane>
|
||||
</Tabs>
|
||||
|
||||
{/* 编辑/新建模态框 */}
|
||||
<Modal
|
||||
title={
|
||||
editingCategory?.id === 0
|
||||
? isLevel1Modal
|
||||
? t('新建一级分类')
|
||||
: t('新建二级分类')
|
||||
: isLevel1Modal
|
||||
? t('编辑一级分类')
|
||||
: t('编辑二级分类')
|
||||
}
|
||||
visible={modalVisible}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
footer={null}
|
||||
width={600}
|
||||
>
|
||||
<Form
|
||||
initValues={editingCategory}
|
||||
onSubmit={(values) => handleSave(values)}
|
||||
labelPosition='left'
|
||||
labelWidth='120px'
|
||||
>
|
||||
{!isLevel1Modal && (
|
||||
<Form.Input
|
||||
field='parent_category_name'
|
||||
label={t('一级分类')}
|
||||
disabled
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
)}
|
||||
<Form.Input
|
||||
field='category_name'
|
||||
label={t('分类名称')}
|
||||
placeholder={
|
||||
isLevel1Modal ? t('例如:文本模型') : t('例如:文本生成')
|
||||
}
|
||||
rules={[{ required: true, message: t('请输入分类名称') }]}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
<Form.Input
|
||||
field='category_key'
|
||||
label={t('分类标识')}
|
||||
placeholder={
|
||||
isLevel1Modal ? t('例如:text') : t('例如:text_generation')
|
||||
}
|
||||
rules={[{ required: true, message: t('请输入分类标识') }]}
|
||||
extraText={t('唯一标识,只能包含字母、数字和下划线')}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
<Form.TextArea
|
||||
field='description'
|
||||
label={t('描述')}
|
||||
placeholder={t('分类的详细描述')}
|
||||
autosize={{ minRows: 2, maxRows: 4 }}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
<Form.InputNumber
|
||||
field='sort'
|
||||
label={t('排序')}
|
||||
placeholder={t('数字越小越靠前')}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
<Form.Input field='parent_id' noLabel style={{ display: 'none' }} />
|
||||
<div
|
||||
style={{ display: 'flex', justifyContent: 'flex-end', gap: 8 }}
|
||||
>
|
||||
<Button onClick={() => setModalVisible(false)}>
|
||||
{t('取消')}
|
||||
</Button>
|
||||
<Button type='primary' htmlType='submit'>
|
||||
{editingCategory?.id === 0 ? t('创建') : t('保存')}
|
||||
</Button>
|
||||
</div>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
</Spin>
|
||||
);
|
||||
};
|
||||
|
||||
export default SettingModelCategory;
|
||||
@@ -0,0 +1,905 @@
|
||||
/*
|
||||
Copyright (C) 2025 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 React, { useEffect, useState, useRef } from 'react';
|
||||
import {
|
||||
Button,
|
||||
Col,
|
||||
Form,
|
||||
Row,
|
||||
Spin,
|
||||
Table,
|
||||
Modal,
|
||||
Tag,
|
||||
Popconfirm,
|
||||
Space,
|
||||
Input,
|
||||
InputNumber,
|
||||
Switch,
|
||||
Typography,
|
||||
} from '@douyinfe/semi-ui';
|
||||
import { IconPlus, IconDelete, IconEdit } from '@douyinfe/semi-icons';
|
||||
import {
|
||||
API,
|
||||
showError,
|
||||
showSuccess,
|
||||
} from '../../../helpers';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
// 视频相关的渠道类型
|
||||
const VIDEO_CHANNEL_TYPES = [
|
||||
{ value: 1, label: 'OpenAI', provider: 'openai' },
|
||||
{ value: 17, label: '阿里通义', provider: 'ali' },
|
||||
{ value: 35, label: 'MiniMax/Hailuo', provider: 'hailuo' },
|
||||
{ value: 54, label: '豆包视频', provider: 'doubao' },
|
||||
{ value: 55, label: 'Sora', provider: 'sora' },
|
||||
];
|
||||
|
||||
// 提供商选项
|
||||
const PROVIDER_OPTIONS = [
|
||||
{ value: 'openai', label: 'OpenAI 兼容' },
|
||||
{ value: 'ali', label: '阿里云' },
|
||||
{ value: 'hailuo', label: 'Hailuo/MiniMax' },
|
||||
{ value: 'doubao', label: '豆包' },
|
||||
{ value: 'sora', label: 'Sora' },
|
||||
];
|
||||
|
||||
const defaultVideoModelInputs = {
|
||||
'video.custom_video_models': '[]',
|
||||
'video.video_second_pricing': '[]',
|
||||
'video.asset_library_user_limit': 50,
|
||||
'video.real_portrait_enabled': false,
|
||||
};
|
||||
|
||||
// 按秒分档:选档字段选项
|
||||
const TIER_PARAM_OPTIONS = [
|
||||
{ value: 'resolution', label: '分辨率 (resolution)' },
|
||||
{ value: 'mode', label: '模式 (mode)' },
|
||||
{ value: 'size', label: '尺寸 (size)' },
|
||||
];
|
||||
|
||||
export default function SettingVideoModel(props) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [, setInputs] = useState(defaultVideoModelInputs);
|
||||
const [models, setModels] = useState([]);
|
||||
const [modalVisible, setModalVisible] = useState(false);
|
||||
const [editingModel, setEditingModel] = useState(null);
|
||||
const [editingIndex, setEditingIndex] = useState(-1);
|
||||
const modalFormRef = useRef();
|
||||
|
||||
// 解析模型列表
|
||||
const parseModels = (jsonStr) => {
|
||||
try {
|
||||
if (!jsonStr || jsonStr === '[]') return [];
|
||||
return JSON.parse(jsonStr);
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// 序列化模型列表
|
||||
const serializeModels = (modelList) => {
|
||||
return JSON.stringify(modelList);
|
||||
};
|
||||
|
||||
// 保存到服务器
|
||||
const saveToServer = async (modelList) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const value = serializeModels(modelList);
|
||||
const res = await API.put('/api/option/', {
|
||||
key: 'video.custom_video_models',
|
||||
value,
|
||||
});
|
||||
if (res.data.success) {
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
} else {
|
||||
showError(res.data.message || t('保存失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
showError(t('保存失败,请重试'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 添加模型
|
||||
const handleAddModel = () => {
|
||||
setEditingModel(null);
|
||||
setEditingIndex(-1);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 编辑模型
|
||||
const handleEditModel = (record, index) => {
|
||||
setEditingModel(record);
|
||||
setEditingIndex(index);
|
||||
setModalVisible(true);
|
||||
};
|
||||
|
||||
// 删除模型
|
||||
const handleDeleteModel = (index) => {
|
||||
const newModels = [...models];
|
||||
newModels.splice(index, 1);
|
||||
setModels(newModels);
|
||||
saveToServer(newModels);
|
||||
};
|
||||
|
||||
// 切换启用状态
|
||||
const handleToggleEnabled = (index) => {
|
||||
const newModels = [...models];
|
||||
newModels[index].enabled = !newModels[index].enabled;
|
||||
setModels(newModels);
|
||||
saveToServer(newModels);
|
||||
};
|
||||
|
||||
// 提交模型表单
|
||||
const handleModalOk = () => {
|
||||
modalFormRef.current.validate().then((values) => {
|
||||
const newModel = {
|
||||
model_name: values.model_name,
|
||||
provider: values.provider,
|
||||
channel_type: values.channel_type,
|
||||
supports_i2v: values.supports_i2v || false,
|
||||
enabled: values.enabled !== false,
|
||||
endpoint: values.endpoint || '',
|
||||
};
|
||||
|
||||
const newModels = [...models];
|
||||
if (editingIndex >= 0) {
|
||||
newModels[editingIndex] = newModel;
|
||||
} else {
|
||||
newModels.push(newModel);
|
||||
}
|
||||
|
||||
setModels(newModels);
|
||||
saveToServer(newModels);
|
||||
setModalVisible(false);
|
||||
});
|
||||
};
|
||||
|
||||
// 根据 provider 自动设置 channel_type
|
||||
const handleProviderChange = (value) => {
|
||||
const channelType = VIDEO_CHANNEL_TYPES.find((c) => c.provider === value);
|
||||
if (channelType && modalFormRef.current) {
|
||||
modalFormRef.current.setValue('channel_type', channelType.value);
|
||||
}
|
||||
};
|
||||
|
||||
// ===== 视频按秒分档定价 =====
|
||||
const [pricingConfigs, setPricingConfigs] = useState([]);
|
||||
const [pricingModalVisible, setPricingModalVisible] = useState(false);
|
||||
const [editingPricingIndex, setEditingPricingIndex] = useState(-1);
|
||||
const [tierRows, setTierRows] = useState([]);
|
||||
const pricingFormRef = useRef();
|
||||
|
||||
const savePricingToServer = async (list) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.put('/api/option/', {
|
||||
key: 'video.video_second_pricing',
|
||||
value: JSON.stringify(list),
|
||||
});
|
||||
if (res.data.success) {
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
} else {
|
||||
showError(res.data.message || t('保存失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
showError(t('保存失败,请重试'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleAddPricing = () => {
|
||||
setEditingPricingIndex(-1);
|
||||
setTierRows([{ key: '', label: '', price_per_second: 0, aliases: '' }]);
|
||||
setPricingModalVisible(true);
|
||||
};
|
||||
|
||||
const handleEditPricing = (record, index) => {
|
||||
setEditingPricingIndex(index);
|
||||
setTierRows(
|
||||
(record.tiers || []).map((tier) => ({
|
||||
key: tier.key || '',
|
||||
label: tier.label || '',
|
||||
price_per_second: tier.price_per_second || 0,
|
||||
aliases: Array.isArray(tier.aliases) ? tier.aliases.join(',') : '',
|
||||
})),
|
||||
);
|
||||
setPricingModalVisible(true);
|
||||
// 表单初值通过 initValues 设置,这里延迟回填以确保 formApi 就绪
|
||||
setTimeout(() => {
|
||||
if (pricingFormRef.current) {
|
||||
pricingFormRef.current.setValues({
|
||||
model_name: record.model_name,
|
||||
tier_param: record.tier_param || 'resolution',
|
||||
default_tier: record.default_tier || '',
|
||||
default_duration: record.default_duration || undefined,
|
||||
enabled: record.enabled !== false,
|
||||
});
|
||||
}
|
||||
}, 0);
|
||||
};
|
||||
|
||||
const handleDeletePricing = (index) => {
|
||||
const next = [...pricingConfigs];
|
||||
next.splice(index, 1);
|
||||
setPricingConfigs(next);
|
||||
savePricingToServer(next);
|
||||
};
|
||||
|
||||
const handleTogglePricingEnabled = (index) => {
|
||||
const next = [...pricingConfigs];
|
||||
next[index] = { ...next[index], enabled: !next[index].enabled };
|
||||
setPricingConfigs(next);
|
||||
savePricingToServer(next);
|
||||
};
|
||||
|
||||
const updateTierRow = (idx, patch) => {
|
||||
setTierRows((rows) =>
|
||||
rows.map((r, i) => (i === idx ? { ...r, ...patch } : r)),
|
||||
);
|
||||
};
|
||||
|
||||
const addTierRow = () => {
|
||||
setTierRows((rows) => [
|
||||
...rows,
|
||||
{ key: '', label: '', price_per_second: 0, aliases: '' },
|
||||
]);
|
||||
};
|
||||
|
||||
const removeTierRow = (idx) => {
|
||||
setTierRows((rows) => rows.filter((_, i) => i !== idx));
|
||||
};
|
||||
|
||||
const handlePricingModalOk = () => {
|
||||
pricingFormRef.current.validate().then((values) => {
|
||||
const tiers = tierRows
|
||||
.filter((r) => r.key && r.key.trim() !== '')
|
||||
.map((r) => ({
|
||||
key: r.key.trim(),
|
||||
label: r.label?.trim() || r.key.trim(),
|
||||
price_per_second: Number(r.price_per_second) || 0,
|
||||
aliases: (r.aliases || '')
|
||||
.split(',')
|
||||
.map((a) => a.trim())
|
||||
.filter((a) => a !== ''),
|
||||
}));
|
||||
|
||||
if (tiers.length === 0) {
|
||||
showError(t('请至少配置一个档位'));
|
||||
return;
|
||||
}
|
||||
|
||||
// 单价必须为正,避免保存后该档位静默 0 扣费
|
||||
if (tiers.some((tier) => !(tier.price_per_second > 0))) {
|
||||
showError(t('每个档位的每秒单价必须大于 0'));
|
||||
return;
|
||||
}
|
||||
|
||||
// default_tier 留空时回退到首个档位,避免请求未带档位/档位不匹配时计费档位不确定
|
||||
const defaultTier = (values.default_tier || '').trim() || tiers[0].key;
|
||||
|
||||
const newConfig = {
|
||||
model_name: values.model_name,
|
||||
tier_param: values.tier_param || 'resolution',
|
||||
default_tier: defaultTier,
|
||||
default_duration: Math.max(0, parseInt(values.default_duration, 10) || 0),
|
||||
enabled: values.enabled !== false,
|
||||
tiers,
|
||||
};
|
||||
|
||||
const next = [...pricingConfigs];
|
||||
if (editingPricingIndex >= 0) {
|
||||
next[editingPricingIndex] = newConfig;
|
||||
} else {
|
||||
next.push(newConfig);
|
||||
}
|
||||
setPricingConfigs(next);
|
||||
savePricingToServer(next);
|
||||
setPricingModalVisible(false);
|
||||
});
|
||||
};
|
||||
|
||||
const pricingColumns = [
|
||||
{ title: t('模型名称'), dataIndex: 'model_name', key: 'model_name' },
|
||||
{
|
||||
title: t('选档字段'),
|
||||
dataIndex: 'tier_param',
|
||||
key: 'tier_param',
|
||||
render: (text) => {
|
||||
const opt = TIER_PARAM_OPTIONS.find((o) => o.value === text);
|
||||
return opt ? opt.label : text || 'resolution';
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('缺省时长'),
|
||||
dataIndex: 'default_duration',
|
||||
key: 'default_duration',
|
||||
render: (v) => (v > 0 ? `${v}s` : t('全局缺省')),
|
||||
},
|
||||
{
|
||||
title: t('档位'),
|
||||
dataIndex: 'tiers',
|
||||
key: 'tiers',
|
||||
render: (tiers) => (
|
||||
<Space wrap>
|
||||
{(tiers || []).map((tier) => (
|
||||
<Tag key={tier.key} color='blue'>
|
||||
{(tier.label || tier.key) + ':$' + tier.price_per_second + '/s'}
|
||||
</Tag>
|
||||
))}
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'enabled',
|
||||
key: 'enabled',
|
||||
render: (value, record, index) => (
|
||||
<Tag
|
||||
color={value ? 'green' : 'red'}
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={() => handleTogglePricingEnabled(index)}
|
||||
>
|
||||
{value ? t('启用') : t('禁用')}
|
||||
</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
key: 'action',
|
||||
render: (text, record, index) => (
|
||||
<Space>
|
||||
<Button
|
||||
icon={<IconEdit />}
|
||||
size='small'
|
||||
onClick={() => handleEditPricing(record, index)}
|
||||
/>
|
||||
<Popconfirm
|
||||
title={t('确定要删除此定价配置吗?')}
|
||||
onConfirm={() => handleDeletePricing(index)}
|
||||
>
|
||||
<Button icon={<IconDelete />} size='small' type='danger' />
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 表格列定义
|
||||
const columns = [
|
||||
{
|
||||
title: t('模型名称'),
|
||||
dataIndex: 'model_name',
|
||||
key: 'model_name',
|
||||
},
|
||||
{
|
||||
title: t('提供商'),
|
||||
dataIndex: 'provider',
|
||||
key: 'provider',
|
||||
render: (text) => {
|
||||
const provider = PROVIDER_OPTIONS.find((p) => p.value === text);
|
||||
return provider ? provider.label : text;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('渠道类型'),
|
||||
dataIndex: 'channel_type',
|
||||
key: 'channel_type',
|
||||
render: (text) => {
|
||||
const channel = VIDEO_CHANNEL_TYPES.find((c) => c.value === text);
|
||||
return channel ? channel.label : text;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: t('自定义端点'),
|
||||
dataIndex: 'endpoint',
|
||||
key: 'endpoint',
|
||||
render: (text) => text || <Tag color='grey'>{t('默认')}</Tag>,
|
||||
},
|
||||
{
|
||||
title: t('支持图生视频'),
|
||||
dataIndex: 'supports_i2v',
|
||||
key: 'supports_i2v',
|
||||
render: (value) =>
|
||||
value ? (
|
||||
<Tag color='green'>{t('是')}</Tag>
|
||||
) : (
|
||||
<Tag color='grey'>{t('否')}</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('状态'),
|
||||
dataIndex: 'enabled',
|
||||
key: 'enabled',
|
||||
render: (value, record, index) => (
|
||||
<Tag
|
||||
color={value ? 'green' : 'red'}
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={() => handleToggleEnabled(index)}
|
||||
>
|
||||
{value ? t('启用') : t('禁用')}
|
||||
</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: t('操作'),
|
||||
key: 'action',
|
||||
render: (text, record, index) => (
|
||||
<Space>
|
||||
<Button
|
||||
icon={<IconEdit />}
|
||||
size='small'
|
||||
onClick={() => handleEditModel(record, index)}
|
||||
/>
|
||||
<Popconfirm
|
||||
title={t('确定要删除此模型吗?')}
|
||||
onConfirm={() => handleDeleteModel(index)}
|
||||
>
|
||||
<Button icon={<IconDelete />} size='small' type='danger' />
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
useEffect(() => {
|
||||
const currentInputs = {};
|
||||
for (const key of Object.keys(defaultVideoModelInputs)) {
|
||||
if (props.options[key] !== undefined) {
|
||||
let value = props.options[key];
|
||||
currentInputs[key] = value;
|
||||
} else {
|
||||
currentInputs[key] = defaultVideoModelInputs[key];
|
||||
}
|
||||
}
|
||||
setInputs(currentInputs);
|
||||
setModels(parseModels(currentInputs['video.custom_video_models']));
|
||||
setPricingConfigs(parseModels(currentInputs['video.video_second_pricing']));
|
||||
setAssetLimit(
|
||||
parseInt(currentInputs['video.asset_library_user_limit'], 10) || 50,
|
||||
);
|
||||
setRealPortraitEnabled(
|
||||
String(currentInputs['video.real_portrait_enabled']).toLowerCase() ===
|
||||
'true',
|
||||
);
|
||||
}, [props.options]);
|
||||
|
||||
// ===== 素材库配额 =====
|
||||
const [assetLimit, setAssetLimit] = useState(50);
|
||||
const [realPortraitEnabled, setRealPortraitEnabled] = useState(false);
|
||||
|
||||
const saveAssetLimit = async () => {
|
||||
if (!assetLimit || assetLimit <= 0) {
|
||||
showError(t('配额必须为正整数'));
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.put('/api/option/', {
|
||||
key: 'video.asset_library_user_limit',
|
||||
value: String(assetLimit),
|
||||
});
|
||||
if (res.data.success) {
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
} else {
|
||||
showError(res.data.message || t('保存失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
showError(t('保存失败,请重试'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const saveRealPortraitEnabled = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.put('/api/option/', {
|
||||
key: 'video.real_portrait_enabled',
|
||||
value: String(realPortraitEnabled),
|
||||
});
|
||||
if (res.data.success) {
|
||||
showSuccess(t('保存成功'));
|
||||
props.refresh();
|
||||
} else {
|
||||
showError(res.data.message || t('保存失败'));
|
||||
}
|
||||
} catch (e) {
|
||||
showError(t('保存失败,请重试'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Spin spinning={loading}>
|
||||
<Form style={{ marginBottom: 15 }}>
|
||||
<Form.Section text={t('自定义视频模型')}>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col span={24}>
|
||||
<div
|
||||
style={{ color: 'var(--semi-color-text-2)', marginBottom: 8 }}
|
||||
>
|
||||
{t(
|
||||
'在此配置自定义视频生成模型,这些模型将与内置模型一起可用于视频生成功能。',
|
||||
)}
|
||||
</div>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col span={24}>
|
||||
<Button icon={<IconPlus />} onClick={handleAddModel}>
|
||||
{t('添加模型')}
|
||||
</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={models}
|
||||
rowKey={(record, index) => index}
|
||||
pagination={false}
|
||||
empty={t('暂无自定义视频模型')}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row style={{ marginBottom: 8 }}>
|
||||
<Col span={24}>
|
||||
<div
|
||||
style={{ color: 'var(--semi-color-text-2)', marginBottom: 8 }}
|
||||
>
|
||||
{t(
|
||||
'真人人像需要 TokenPony 素材权限、公网 HTTPS 服务地址、稳定的 CRYPTO_SECRET 和私有 COS。',
|
||||
)}
|
||||
</div>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col span={24}>
|
||||
<Space>
|
||||
<Switch
|
||||
checked={realPortraitEnabled}
|
||||
onChange={setRealPortraitEnabled}
|
||||
/>
|
||||
<Button onClick={saveRealPortraitEnabled}>
|
||||
{t('保存真人人像设置')}
|
||||
</Button>
|
||||
</Space>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
|
||||
<Form.Section text={t('视频按秒分档定价')}>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col span={24}>
|
||||
<div
|
||||
style={{ color: 'var(--semi-color-text-2)', marginBottom: 8 }}
|
||||
>
|
||||
{t(
|
||||
'为视频模型配置「按秒分档」计费:每个档位设置每秒单价(官方原价),实际计费 = 单价 × 视频时长 × 分组倍率(分组折扣无需在此重复配置)。配置后该模型计费类型变为「按秒计费」。',
|
||||
)}
|
||||
</div>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col span={24}>
|
||||
<Button icon={<IconPlus />} onClick={handleAddPricing}>
|
||||
{t('添加定价配置')}
|
||||
</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<Table
|
||||
columns={pricingColumns}
|
||||
dataSource={pricingConfigs}
|
||||
rowKey={(record, index) => index}
|
||||
pagination={false}
|
||||
empty={t('暂无按秒分档定价配置')}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
|
||||
<Form.Section text={t('视频素材库')}>
|
||||
<Row style={{ marginBottom: 8 }}>
|
||||
<Col span={24}>
|
||||
<div
|
||||
style={{ color: 'var(--semi-color-text-2)', marginBottom: 8 }}
|
||||
>
|
||||
{t(
|
||||
'每用户素材数量上限(审核中与已通过的素材计入,审核失败不计入)。保存后约 1 分钟内生效,无需重启。',
|
||||
)}
|
||||
</div>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row style={{ marginBottom: 16 }}>
|
||||
<Col xs={24} sm={12} md={8}>
|
||||
<InputNumber
|
||||
value={assetLimit}
|
||||
min={1}
|
||||
max={10000}
|
||||
onChange={(v) => setAssetLimit(v)}
|
||||
style={{ width: 200, marginRight: 12 }}
|
||||
/>
|
||||
<Button onClick={saveAssetLimit}>{t('保存素材库配额')}</Button>
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Section>
|
||||
</Form>
|
||||
</Spin>
|
||||
|
||||
<Modal
|
||||
title={editingIndex >= 0 ? t('编辑模型') : t('添加模型')}
|
||||
visible={modalVisible}
|
||||
onOk={handleModalOk}
|
||||
onCancel={() => setModalVisible(false)}
|
||||
okText={t('确定')}
|
||||
cancelText={t('取消')}
|
||||
>
|
||||
<Form
|
||||
getFormApi={(formAPI) => (modalFormRef.current = formAPI)}
|
||||
initValues={
|
||||
editingModel || {
|
||||
model_name: '',
|
||||
provider: 'ali',
|
||||
channel_type: 17,
|
||||
supports_i2v: false,
|
||||
enabled: true,
|
||||
endpoint: '',
|
||||
}
|
||||
}
|
||||
>
|
||||
<Form.Input
|
||||
field='model_name'
|
||||
label={t('模型名称')}
|
||||
placeholder={t('请输入模型名称,如 my-video-model')}
|
||||
rules={[{ required: true, message: t('请输入模型名称') }]}
|
||||
/>
|
||||
<Form.Select
|
||||
field='provider'
|
||||
label={t('提供商')}
|
||||
placeholder={t('请选择提供商')}
|
||||
rules={[{ required: true, message: t('请选择提供商') }]}
|
||||
onChange={handleProviderChange}
|
||||
>
|
||||
{PROVIDER_OPTIONS.map((option) => (
|
||||
<Form.Select.Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Form.Select.Option>
|
||||
))}
|
||||
</Form.Select>
|
||||
<Form.Select
|
||||
field='channel_type'
|
||||
label={t('渠道类型')}
|
||||
placeholder={t('请选择渠道类型')}
|
||||
rules={[{ required: true, message: t('请选择渠道类型') }]}
|
||||
extraText={t('渠道类型决定了使用哪种 API 协议与上游通信')}
|
||||
>
|
||||
{VIDEO_CHANNEL_TYPES.map((option) => (
|
||||
<Form.Select.Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Form.Select.Option>
|
||||
))}
|
||||
</Form.Select>
|
||||
<Form.Input
|
||||
field='endpoint'
|
||||
label={t('自定义API端点')}
|
||||
placeholder={t('如 /v1/chat/completions,留空则使用默认端点')}
|
||||
extraText={t('自定义请求的API路径,默认为 /v1/videos/generations')}
|
||||
/>
|
||||
<Form.Switch
|
||||
field='supports_i2v'
|
||||
label={t('支持图生视频')}
|
||||
extraText={t('是否支持以图片作为输入生成视频')}
|
||||
/>
|
||||
<Form.Switch
|
||||
field='enabled'
|
||||
label={t('启用')}
|
||||
extraText={t('是否启用此模型')}
|
||||
/>
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
title={
|
||||
editingPricingIndex >= 0 ? t('编辑定价配置') : t('添加定价配置')
|
||||
}
|
||||
visible={pricingModalVisible}
|
||||
onOk={handlePricingModalOk}
|
||||
onCancel={() => setPricingModalVisible(false)}
|
||||
okText={t('确定')}
|
||||
cancelText={t('取消')}
|
||||
width={640}
|
||||
>
|
||||
<Form
|
||||
getFormApi={(formAPI) => (pricingFormRef.current = formAPI)}
|
||||
initValues={{
|
||||
model_name: '',
|
||||
tier_param: 'resolution',
|
||||
default_tier: '',
|
||||
default_duration: undefined,
|
||||
enabled: true,
|
||||
}}
|
||||
>
|
||||
<Form.Input
|
||||
field='model_name'
|
||||
label={t('模型名称')}
|
||||
placeholder={t('请输入模型名称,如 kling-v2-master')}
|
||||
rules={[{ required: true, message: t('请输入模型名称') }]}
|
||||
extraText={t('支持前缀匹配,例如填 kling 可匹配所有 kling 开头模型')}
|
||||
/>
|
||||
<Form.Select
|
||||
field='tier_param'
|
||||
label={t('选档字段')}
|
||||
extraText={t('请求中用于选择档位的字段:分辨率/模式/尺寸')}
|
||||
>
|
||||
{TIER_PARAM_OPTIONS.map((option) => (
|
||||
<Form.Select.Option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</Form.Select.Option>
|
||||
))}
|
||||
</Form.Select>
|
||||
<Form.Input
|
||||
field='default_tier'
|
||||
label={t('默认档位')}
|
||||
placeholder={t('请求未带档位或不匹配时使用的档位 key')}
|
||||
extraText={t(
|
||||
'请填上游模型自身的默认档位,避免按错档计费(如 kling 默认 std、vidu 默认 1080p);留空则默认取第一个档位。',
|
||||
)}
|
||||
/>
|
||||
<Form.InputNumber
|
||||
field='default_duration'
|
||||
label={t('缺省时长(秒)')}
|
||||
min={0}
|
||||
precision={0}
|
||||
style={{ width: '100%' }}
|
||||
placeholder={t('请求未带时长时使用,留空则按全局缺省 5 秒')}
|
||||
extraText={t(
|
||||
'应填该上游模型自身的默认时长(如 hailuo 默认 6 秒),避免省略时长时按 5 秒少扣。',
|
||||
)}
|
||||
/>
|
||||
<Form.Switch field='enabled' label={t('启用')} />
|
||||
</Form>
|
||||
|
||||
<div
|
||||
style={{
|
||||
marginTop: 8,
|
||||
padding: '8px 12px',
|
||||
borderRadius: 6,
|
||||
background: 'var(--semi-color-fill-0)',
|
||||
color: 'var(--semi-color-text-2)',
|
||||
fontSize: 12,
|
||||
lineHeight: 1.6,
|
||||
}}
|
||||
>
|
||||
<Typography.Text strong style={{ fontSize: 12 }}>
|
||||
{t('选档值来源说明')}
|
||||
</Typography.Text>
|
||||
<div style={{ marginTop: 4 }}>
|
||||
{t(
|
||||
'视频生成接口(/v1/video/generations)与任务接口会自动读取请求中的 resolution / mode / size 字段来选档。',
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
{t(
|
||||
'通过对话接口(/v1/chat/completions)调用时,自然语言无法可靠表达计费档位,需在请求体的 metadata 中显式传入,例如:',
|
||||
)}
|
||||
</div>
|
||||
<pre
|
||||
style={{
|
||||
margin: '4px 0 0',
|
||||
padding: '6px 8px',
|
||||
borderRadius: 4,
|
||||
background: 'var(--semi-color-fill-1)',
|
||||
whiteSpace: 'pre-wrap',
|
||||
fontSize: 12,
|
||||
}}
|
||||
>
|
||||
{'"metadata": { "mode": "pro", "resolution": "1080P" }'}
|
||||
</pre>
|
||||
<div style={{ marginTop: 4 }}>
|
||||
{t('未传入选档值时,将按上面配置的「默认档位」计费。')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style={{ marginTop: 12 }}>
|
||||
<Typography.Text strong>{t('档位列表')}</Typography.Text>
|
||||
<div
|
||||
style={{
|
||||
color: 'var(--semi-color-text-2)',
|
||||
fontSize: 12,
|
||||
margin: '4px 0 8px',
|
||||
}}
|
||||
>
|
||||
{t(
|
||||
'key 为档位标识(需与请求字段值或别名匹配),label 用于展示(如 1080P/S),单价为每秒美元价。别名用英文逗号分隔。',
|
||||
)}
|
||||
</div>
|
||||
{tierRows.map((row, idx) => (
|
||||
<Row
|
||||
key={idx}
|
||||
gutter={8}
|
||||
type='flex'
|
||||
align='middle'
|
||||
style={{ marginBottom: 8 }}
|
||||
>
|
||||
<Col span={5}>
|
||||
<Input
|
||||
placeholder={t('key 如 1080P')}
|
||||
value={row.key}
|
||||
onChange={(v) => updateTierRow(idx, { key: v })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={5}>
|
||||
<Input
|
||||
placeholder={t('展示名 如 1080P/S')}
|
||||
value={row.label}
|
||||
onChange={(v) => updateTierRow(idx, { label: v })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={5}>
|
||||
<InputNumber
|
||||
placeholder={t('单价/秒')}
|
||||
prefix='$'
|
||||
min={0}
|
||||
step={0.01}
|
||||
style={{ width: '100%' }}
|
||||
value={row.price_per_second}
|
||||
onChange={(v) =>
|
||||
updateTierRow(idx, { price_per_second: v })
|
||||
}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={7}>
|
||||
<Input
|
||||
placeholder={t('别名(逗号分隔) 如 std,standard')}
|
||||
value={row.aliases}
|
||||
onChange={(v) => updateTierRow(idx, { aliases: v })}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={2}>
|
||||
<Button
|
||||
icon={<IconDelete />}
|
||||
size='small'
|
||||
type='danger'
|
||||
onClick={() => removeTierRow(idx)}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
))}
|
||||
<Button icon={<IconPlus />} size='small' onClick={addTierRow}>
|
||||
{t('添加档位')}
|
||||
</Button>
|
||||
</div>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user