/** 新建深度测评表单:字段与 internal/handler/deep_assessment.go 的创建结构体一致。 */
import { useCallback, useEffect, useId, useRef, useState } from 'react';
import { Banner, Button, Form, Spin, Typography } from '@douyinfe/semi-ui';
import { IconPlus } from '@douyinfe/semi-icons';
import { useAuth } from '../../lib/auth';
import { api } from '../../lib/http';
import { useT } from '../../lib/i18n';
import { modelChannelOptions, selectedModelChannelName } from '../../lib/model-channel';
import { useResource } from '../../components/hooks';
import { Drawer, ErrorBanner, toastError, toastSuccess } from '../../components/ui';
import ProjectFormModal from '../projects/project-form';
const HITL_OPTIONS = [
{ value: '', label: '系统默认' },
{ value: 'off', label: '关闭审批' },
{ value: 'human', label: '人工审批' },
{ value: 'audit_agent', label: '审计代理审批' },
{ value: 'review_edit', label: '复核并允许编辑' }
];
const NOISE_PROFILE_OPTIONS = [
{ value: '', label: '继承项目设置 / 默认不限速' },
{ value: 'off', label: '关闭限速提示(off)' },
{ value: 'quiet', label: '低噪串行(quiet · 2 RPS)' },
{ value: 'balanced', label: '均衡速率(balanced · 5 RPS)' },
{ value: 'aggressive', label: '维护窗口高吞吐(aggressive · 20 RPS)' }
];
const DEFAULT_ROLE = '默认';
function assessmentRoleRecord(item) {
if (typeof item === 'string') return { name: item.trim(), icon: '' };
if (!item || typeof item !== 'object') return null;
const name = String(item.name || '').trim();
if (!name) return null;
return { name, icon: String(item.icon || '').trim() };
}
function assessmentRoleOption(item) {
const isDefault = item.name === DEFAULT_ROLE;
return {
value: isDefault ? '' : item.name,
name: item.name,
icon: item.icon,
label: (
{item.icon ? {item.icon} : null}
{item.name}
)
};
}
function assessmentRoleOptions(raw) {
const records = (Array.isArray(raw) ? raw : []).map(assessmentRoleRecord).filter(Boolean);
const named = records.filter(item => item.name !== DEFAULT_ROLE);
const fallback = records.find(item => item.name === DEFAULT_ROLE);
return [
...(fallback ? [assessmentRoleOption(fallback)] : []),
...named.map(assessmentRoleOption)
];
}
function ProjectAssetScope({ projectId }) {
const t = useT();
const assets = useResource(
() => api.get('/assets', { project_id: projectId, page: 1, page_size: 1 }),
[projectId]
);
const total = Number.isFinite(Number(assets.data?.total)) ? Number(assets.data.total) : null;
let message = t('common.loading', null, '加载中…');
if (!assets.loading && assets.error) {
message = '资产库暂时读不到。保存后仍使用该项目在资产库中的资产。';
} else if (!assets.loading && total === 0) {
message = '这个项目的资产库还是空的。测评中新发现的目标会写进资产库,对话和批量任务也能看到。';
} else if (!assets.loading && total != null) {
message = `资产范围是资产库里这个项目的 ${total} 个资产。对话、批量任务和这次测评用的是同一批,会自动交给模型。`;
}
return (
{message}
);
}
export default function CreateAssessmentDrawer({ visible, onCancel, onCreated }) {
const t = useT();
const title = t('deepAssessments.create', null, '新建测评');
const { hasPermission } = useAuth();
const canCreateProject = hasPermission('project:read') && hasPermission('project:write');
const formId = useId();
const formApi = useRef(null);
const [options, setOptions] = useState(null);
const [error, setError] = useState(null);
const [saving, setSaving] = useState(false);
const [projectFormOpen, setProjectFormOpen] = useState(false);
const [projectId, setProjectId] = useState('');
const busy = saving || projectFormOpen;
const loadOptions = useCallback(() => {
setOptions(null);
setError(null);
setProjectId('');
api.get('/deep-assessments/options')
.then(data => setOptions(data || {}))
.catch(err => setError(err));
}, []);
useEffect(() => {
if (!visible) return;
loadOptions();
}, [visible, loadOptions]);
const changeProject = value => {
setProjectId(value || '');
};
const submit = async values => {
if (busy) return;
setSaving(true);
try {
const body = {
name: String(values.name || '').trim(),
objective: String(values.objective || '').trim(),
scope: String(values.scope || '').trim(),
criteria: String(values.criteria || '').trim(),
project_id: values.project_id || '',
role: values.role || '',
model: values.model || '',
hitl_policy: values.hitl_policy || '',
noise_profile: values.noise_profile || '',
concurrency: Number(values.concurrency) || 2,
auto_resume: values.auto_resume !== false
};
const data = await api.post('/deep-assessments', body);
toastSuccess(t('deepAssessments.saved', null, '已创建测评'));
onCreated?.(data?.task);
} catch (err) {
toastError(err, t('deepAssessments.saveFailed', null, '创建失败'));
} finally {
setSaving(false);
}
};
const handleProjectCreated = project => {
setProjectFormOpen(false);
if (!project?.id) return;
setOptions(current => ({ ...current, projects: [project, ...(current?.projects || [])] }));
formApi.current?.setValue('project_id', project.id);
changeProject(project.id);
};
const models = modelChannelOptions((options?.models || []).map(item => [item.id, item]));
const roles = assessmentRoleOptions(options?.roles);
const projects = (options?.projects || []).map(project => ({ value: project.id, label: project.name || project.id }));
return (
)}
>
{error ? (
) : null}
{!options && !error ? (
) : (
(
{option.icon ? {option.icon} : null}
{option.name}
)}
style={{ width: '100%' }}
/>
{canCreateProject && (
}
theme="light"
type="primary"
disabled={saving}
onClick={() => setProjectFormOpen(true)}
>
{t('projects.newProject', null, '新建项目')}
)}
{projectId && }
)}
{projectFormOpen && (
setProjectFormOpen(false)}
onSaved={handleProjectCreated}
/>
)}
);
}