Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
+646
View File
@@ -0,0 +1,646 @@
/*
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, { useCallback, useEffect, useMemo, useState } from 'react';
import {
Button,
Input,
InputNumber,
Modal,
Select,
TabPane,
Tabs,
Tag,
TextArea,
Toast,
Typography,
} from '@douyinfe/semi-ui';
import { useTranslation } from 'react-i18next';
import { API } from '../../helpers';
import OrgPanel from './components/OrgPanel';
import {
usePageGuide,
PageGuideDrawer,
PageGuidePill,
PageGuideTrigger,
} from '../../components/common/PageGuide';
import './org-experts.css';
const { Title, Paragraph, Text } = Typography;
const emptyForm = {
definition_id: 0,
expert_key: '',
source_expert_id: 0,
name_zh: '',
name_en: '',
description_zh: '',
description_en: '',
scenario_zh: '',
scenario_en: '',
system_prompt_zh: '',
system_prompt_en: '',
default_model: '',
fallback_models: '',
input_schema:
'{\n "type": "object",\n "properties": {\n "task": { "type": "string" }\n },\n "required": ["task"]\n}',
required_sections_zh: '',
required_sections_en: '',
forbidden_placeholders: '',
contract_type: 'markdown',
artifact_mode: 'inline',
artifact_file_name: '',
artifact_content_type: '',
max_output_tokens: 2048,
max_total_output_tokens: 4096,
max_step_quota: 100000,
max_task_quota: 200000,
default_language: 'zh',
safety_policy_ref: 'org-default',
step_plan:
'[\n {\n "key": "primary",\n "name_zh": "生成交付物",\n "name_en": "Generate deliverable",\n "instruction_zh": "根据任务生成最终交付物。",\n "instruction_en": "Generate the final deliverable for the task.",\n "max_output_tokens": 2048\n }\n]',
};
const csv = (value) =>
String(value || '')
.split(',')
.map((item) => item.trim())
.filter(Boolean);
const OrgExperts = ({ orgId }) => {
const { t } = useTranslation();
const headers = useMemo(() => ({ 'X-Org-Id': String(orgId) }), [orgId]);
const [definitions, setDefinitions] = useState([]);
const [sources, setSources] = useState([]);
// 首次加载完成前进度胶囊不渲染,避免对老用户闪现「0/4」。
const [loaded, setLoaded] = useState(false);
const [open, setOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [form, setForm] = useState(emptyForm);
const load = useCallback(async () => {
// allSettled: a transient catalog failure must not discard the managed
// expert list and render a misleading "no experts yet" empty state.
const [managed, catalog] = await Promise.allSettled([
API.get('/api/org/experts', { headers, skipErrorHandler: true }),
// Mirrored under the experts router so an admin holding only
// expert_manage (without the cloud_brain admin permission) can still
// load the platform source catalog.
API.get('/api/org/experts/catalog?locale=zh', {
headers,
skipErrorHandler: true,
}),
]);
if (managed.status === 'fulfilled' && managed.value.data?.success) {
setDefinitions(managed.value.data.data || []);
} else {
const message =
managed.status === 'fulfilled'
? managed.value.data?.message
: managed.reason?.response?.data?.message;
Toast.error(message || t('企业专家列表加载失败'));
}
if (catalog.status === 'fulfilled' && catalog.value.data?.success) {
setSources(
(catalog.value.data.data || []).filter((item) => !item.organization_id),
);
} else {
const message =
catalog.status === 'fulfilled'
? catalog.value.data?.message
: catalog.reason?.response?.data?.message;
Toast.error(message || t('平台专家目录不可用,请检查管理权限'));
}
setLoaded(true);
}, [headers, t]);
useEffect(() => {
void load();
}, [load]);
const update = (key, value) =>
setForm((current) => ({ ...current, [key]: value }));
const beginVersion = (view) => {
const current = view?.current;
if (!current) {
// A definition whose first version is still draft/pending_review has no
// `current`; keep its identity so the new version attaches to it
// instead of colliding with the (organization_id, expert_key) unique
// index as a brand-new definition.
setForm({
...emptyForm,
definition_id: view?.definition?.id || 0,
expert_key: view?.definition?.expert_key || '',
});
} else {
let contract = {};
try {
contract = JSON.parse(current.output_contract || '{}');
} catch {
contract = {};
}
setForm({
...emptyForm,
definition_id: view.definition.id,
expert_key: current.expert_key,
source_expert_id: current.source_expert_id,
name_zh: current.name_zh,
name_en: current.name_en,
description_zh: current.description_zh,
description_en: current.description_en,
scenario_zh: current.scenario_zh,
scenario_en: current.scenario_en,
system_prompt_zh: '',
system_prompt_en: '',
default_model: current.default_model,
fallback_models: (() => {
try {
return JSON.parse(current.fallback_models || '[]').join(', ');
} catch {
return '';
}
})(),
input_schema: current.input_schema || emptyForm.input_schema,
required_sections_zh: (contract.required_sections_zh || []).join(', '),
required_sections_en: (contract.required_sections_en || []).join(', '),
forbidden_placeholders: (contract.forbidden_placeholders || []).join(
', ',
),
contract_type: contract.type || 'markdown',
artifact_mode: contract.artifact_mode || 'inline',
artifact_file_name: contract.file_name || '',
artifact_content_type: contract.content_type || '',
max_output_tokens: current.max_output_tokens,
max_total_output_tokens: current.max_total_output_tokens,
max_step_quota: current.max_step_quota,
max_task_quota: current.max_task_quota,
default_language: current.default_language,
safety_policy_ref: current.safety_policy_ref,
step_plan: current.step_plan || emptyForm.step_plan,
});
}
setOpen(true);
};
const save = async () => {
let inputSchema;
let stepPlan;
try {
inputSchema = JSON.parse(form.input_schema);
stepPlan = JSON.parse(form.step_plan);
} catch {
Toast.error(t('输入 Schema 与步骤计划必须是合法 JSON'));
return;
}
setSaving(true);
try {
const response = await API.post(
'/api/org/experts/versions',
{
...form,
fallback_models: csv(form.fallback_models),
input_schema: inputSchema,
output_contract: {
type: form.contract_type,
required_sections_zh: csv(form.required_sections_zh),
required_sections_en: csv(form.required_sections_en),
required_table_columns: [],
require_citations: false,
forbidden_placeholders: csv(form.forbidden_placeholders),
repair_once: true,
artifact_mode: form.artifact_mode,
file_name:
form.artifact_mode === 'file' ? form.artifact_file_name : '',
content_type:
form.artifact_mode === 'file' ? form.artifact_content_type : '',
},
step_plan: stepPlan,
skill_ids: [],
},
{ headers },
);
if (!response.data?.success) {
Toast.error(response.data?.message || t('保存失败'));
return;
}
Toast.success(t('专家草稿版本已创建'));
setOpen(false);
await load();
} finally {
setSaving(false);
}
};
const submit = async (id) => {
const response = await API.post(
`/api/org/experts/versions/${id}/submit`,
{},
{ headers },
);
if (!response.data?.success) {
Toast.error(response.data?.message || t('提交失败'));
return;
}
Toast.success(t('已提交平台审核'));
await load();
};
// 使用指引:企业专家从派生到上岗的流程,完成态由专家/版本数据驱动。
const guide = usePageGuide({
guideKey: 'org-experts',
ready: loaded,
title: t('企业专家工坊 · 使用指引'),
intro: t('派生 → 配置 → 平台审核 → 发布上岗,版本不可变、全程留审计'),
steps: [
{
title: t('从平台来源派生专家'),
detail: t('选择一个平台专家作为安全基线开始创建'),
done: definitions.length > 0,
cta: t('去创建专家'),
onAction: () => beginVersion(null),
points: [
t('专家标识创建后不可更改;「平台来源」决定继承哪套安全基线'),
t('填写中英文名称、说明与适用场景,成员在云端大脑会看到这些信息'),
],
},
{
title: t('配置模型与输出契约'),
detail: t('默认模型、降级序列、提示词与交付格式'),
done: definitions.length > 0,
points: [
t('「身份与模型」页配置默认模型与逗号分隔的降级模型序列'),
t(
'「提示词与契约」页定义系统提示词、输入 Schema 与产物形式(页面文本或安全文件)',
),
t('「步骤与预算」页规划多步骤交付路径和 token 预算上限'),
],
},
{
title: t('提交平台审核'),
detail: t('保存生成草稿版本,草稿需平台审核后才能发布'),
done: definitions.some((view) =>
(view.versions || []).some((version) => version.status !== 'draft'),
),
points: [
t('在专家卡片的版本列表里对 draft 版本点「提交审核」'),
t('每个版本不可变:审核中发现问题就「创建新版本」修正后重新提交'),
],
},
{
title: t('发布后到云端大脑分发'),
detail: t('审核通过的版本成为当前版本,再决定谁能用'),
done: definitions.some((view) => Boolean(view.current)),
points: [
t(
'到「云端大脑」右上角「管理企业专家」按全企业/部门/项目/成员启停分发',
),
t('停用后该范围内成员无法创建新任务,进行中任务按安全策略结算'),
],
},
],
faq: [
{
q: t('版本一直是 draft?'),
a: t('草稿不会自动送审,需要在版本列表手动点「提交审核」'),
},
{
q: t('专家显示「等待首个版本发布」?'),
a: t('尚无审核通过的版本;通过后自动成为当前版本'),
},
],
});
const closeGuide = guide.close;
// 抽屉进度是组织私有的:切组织时关闭并复位 loaded,
// 否则重取窗口内胶囊会按旧组织数据渲染完成态。
useEffect(() => {
setLoaded(false);
closeGuide();
}, [orgId, closeGuide]);
return (
<main className='org-expert-page'>
<header className='org-expert-hero'>
<div>
<Title heading={3}>{t('企业专家工坊')}</Title>
<Paragraph>
{t(
'从平台安全基线派生不可变版本,配置模型、契约与多步骤交付路径。',
)}
</Paragraph>
</div>
<div className='org-expert-hero-actions'>
<PageGuideTrigger guide={guide} />
<Button
theme='solid'
type='primary'
onClick={() => beginVersion(null)}
>
{t('创建企业专家')}
</Button>
</div>
</header>
<PageGuidePill guide={guide} />
<PageGuideDrawer guide={guide} />
<section className='org-expert-grid'>
{definitions.length === 0 ? (
<div className='org-expert-empty'>
<strong>{t('尚未创建企业专家')}</strong>
<Text type='tertiary'>
{t('选择一个平台专家作为安全来源开始派生。')}
</Text>
</div>
) : (
definitions.map((view) => (
<OrgPanel
key={view.definition.id}
title={view.current?.name_zh || view.definition.expert_key}
actions={<Tag color='green'>{view.definition.status}</Tag>}
>
<Paragraph>
{view.current?.description_zh || t('等待首个版本发布')}
</Paragraph>
<div className='org-expert-version-list'>
{(view.versions || []).map((version) => (
<div key={version.id}>
<span>v{version.version}</span>
<Tag>{version.status}</Tag>
{version.status === 'draft' && (
<Button size='small' onClick={() => submit(version.id)}>
{t('提交审核')}
</Button>
)}
</div>
))}
</div>
<Button onClick={() => beginVersion(view)}>
{t('创建新版本')}
</Button>
</OrgPanel>
))
)}
</section>
<Modal
title={form.definition_id ? t('创建专家新版本') : t('创建企业专家')}
visible={open}
onCancel={() => setOpen(false)}
onOk={save}
confirmLoading={saving}
width={880}
className='org-expert-modal'
>
<Tabs type='line'>
<TabPane tab={t('身份与模型')} itemKey='identity'>
<div className='org-expert-form-grid'>
<label>
{t('专家标识')}
<Input
value={form.expert_key}
disabled={!!form.definition_id}
onChange={(v) => update('expert_key', v)}
/>
</label>
<label>
{t('平台来源')}
<Select
value={form.source_expert_id || undefined}
onChange={(v) => update('source_expert_id', v)}
optionList={sources.map((item) => ({
value: item.id,
label: `${item.name} · v${item.version}`,
}))}
/>
</label>
<label>
{t('中文名称')}
<Input
value={form.name_zh}
onChange={(v) => update('name_zh', v)}
/>
</label>
<label>
{t('英文名称')}
<Input
value={form.name_en}
onChange={(v) => update('name_en', v)}
/>
</label>
<label className='wide'>
{t('中文描述')}
<TextArea
value={form.description_zh}
onChange={(v) => update('description_zh', v)}
/>
</label>
<label className='wide'>
{t('英文描述')}
<TextArea
value={form.description_en}
onChange={(v) => update('description_en', v)}
/>
</label>
<label>
{t('中文场景')}
<Input
value={form.scenario_zh}
onChange={(v) => update('scenario_zh', v)}
/>
</label>
<label>
{t('英文场景')}
<Input
value={form.scenario_en}
onChange={(v) => update('scenario_en', v)}
/>
</label>
<label>
{t('默认模型')}
<Input
value={form.default_model}
onChange={(v) => update('default_model', v)}
/>
</label>
<label>
{t('降级模型序列')}
<Input
value={form.fallback_models}
onChange={(v) => update('fallback_models', v)}
placeholder='model-a, model-b'
/>
</label>
</div>
</TabPane>
<TabPane tab={t('提示词与契约')} itemKey='contract'>
<div className='org-expert-form-grid'>
<label className='wide'>
{t('中文系统提示词')}
<TextArea
autosize={{ minRows: 5, maxRows: 12 }}
value={form.system_prompt_zh}
onChange={(v) => update('system_prompt_zh', v)}
/>
</label>
<label className='wide'>
{t('英文系统提示词')}
<TextArea
autosize={{ minRows: 5, maxRows: 12 }}
value={form.system_prompt_en}
onChange={(v) => update('system_prompt_en', v)}
/>
</label>
<label className='wide'>
{t('输入 JSON Schema')}
<TextArea
className='mono'
autosize={{ minRows: 6, maxRows: 14 }}
value={form.input_schema}
onChange={(v) => update('input_schema', v)}
/>
</label>
<label>
{t('输出类型')}
<Select
value={form.contract_type}
onChange={(v) => update('contract_type', v)}
optionList={['markdown', 'json', 'text'].map((v) => ({
value: v,
label: v,
}))}
/>
</label>
<label>
{t('默认语言')}
<Select
value={form.default_language}
onChange={(v) => update('default_language', v)}
optionList={[
{ value: 'zh', label: '中文' },
{ value: 'en', label: 'English' },
]}
/>
</label>
<label>
{t('产物交付方式')}
<Select
value={form.artifact_mode}
onChange={(v) => update('artifact_mode', v)}
optionList={[
{ value: 'inline', label: t('页面内文本') },
{ value: 'file', label: t('安全文件下载') },
]}
/>
</label>
{form.artifact_mode === 'file' && (
<>
<label>
{t('文件名')}
<Input
value={form.artifact_file_name}
onChange={(v) => update('artifact_file_name', v)}
placeholder='artifact.md'
/>
</label>
<label>
{t('文件 MIME 类型')}
<Select
value={form.artifact_content_type || undefined}
onChange={(v) => update('artifact_content_type', v)}
optionList={[
{ value: 'text/markdown', label: 'text/markdown' },
{
value: 'application/json',
label: 'application/json',
},
{ value: 'text/plain', label: 'text/plain' },
{ value: 'text/csv', label: 'text/csv' },
]}
/>
</label>
<p className='wide org-expert-help'>
{t(
'文件会先校验格式、拦截 CSV 公式注入并通过恶意软件扫描,再写入带版本号的私有 COS;下载链接每次重新鉴权且仅短期有效。',
)}
</p>
</>
)}
<label>
{t('中文必需章节')}
<Input
value={form.required_sections_zh}
onChange={(v) => update('required_sections_zh', v)}
/>
</label>
<label>
{t('英文必需章节')}
<Input
value={form.required_sections_en}
onChange={(v) => update('required_sections_en', v)}
/>
</label>
<label className='wide'>
{t('禁用占位符')}
<Input
value={form.forbidden_placeholders}
onChange={(v) => update('forbidden_placeholders', v)}
/>
</label>
</div>
</TabPane>
<TabPane tab={t('步骤与预算')} itemKey='steps'>
<div className='org-expert-form-grid'>
<label className='wide'>
{t('步骤计划 JSON')}
<TextArea
className='mono'
autosize={{ minRows: 10, maxRows: 20 }}
value={form.step_plan}
onChange={(v) => update('step_plan', v)}
/>
</label>
{[
'max_output_tokens',
'max_total_output_tokens',
'max_step_quota',
'max_task_quota',
].map((key) => (
<label key={key}>
{key}
<InputNumber
value={form[key]}
min={1}
onChange={(v) => update(key, Number(v))}
/>
</label>
))}
<label className='wide'>
{t('安全策略引用')}
<Input
value={form.safety_policy_ref}
onChange={(v) => update('safety_policy_ref', v)}
/>
</label>
</div>
</TabPane>
</Tabs>
</Modal>
</main>
);
};
export default OrgExperts;