Files
eip4-web/src/pages/AiFeatureForm.tsx
zhengwr 768ffa8551 refactor(web): isAdmin 收口到 isAdminProfile(B4)
5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。
顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。
验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 11:20:16 +08:00

115 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// AI ①面加速器入口 —— amis form 渲染(与全站一致)。填需求 → 外部 agent 起草,
// 产物恒 READY_FOR_HUMAN_REVIEW,人点「审阅草稿」进 editor.html(edit-only)。
// 红线:永不自动发布;平台进程内不内置生成 LLM。
// 注意:/api/ai/features/auto-develop 直接返回 run 对象(status 为字符串,非 amis 的 {status:0}),
// 故 api 用 adaptor 包成 {status:0,data:payload},再以 ARRAYFILTER 取 PAGE_SCHEMA 草稿 targetId。
import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';
import { getProfile, isAdminProfile } from '../auth/token';
import { AmisRenderer } from '../components/AmisRenderer';
const TYPE_OPTIONS = [
{ label: '文本', value: 'string' },
{ label: '整数', value: 'integer' },
{ label: '小数', value: 'number' },
{ label: '日期', value: 'date' },
{ label: '日期时间', value: 'datetime' },
{ label: '布尔', value: 'boolean' },
];
function buildSchema(): Record<string, unknown> {
// 进设计器用的 templateId = development.drafts 里 artifactType==PAGE_SCHEMA 的 targetId
const reviewTpl = '${ARRAYFILTER(development.drafts, item.artifactType === "PAGE_SCHEMA")[0].targetId}';
return {
type: 'page',
body: {
type: 'form',
title: '',
mode: 'horizontal',
actions: [{ type: 'submit', label: '开始 AI 起草', level: 'primary' }],
// baseUrl=当前源(dev 经 vite 代理到后端);恒人审,故只产草稿
api: {
method: 'post',
url: '/api/ai/features/auto-develop',
data: {
feature: {
objective: '${objective}', featureName: '${featureName}', tableName: '${tableName}',
entityName: '${entityName}', pageRecipe: '${pageRecipe}', fields: '${fields}',
},
baseUrl: window.location.origin,
generateDeploymentPackage: true,
},
// auto-develop 返回裸 run 对象 → 包成 amis 信封,使 ${status}/${development}/${taskId} 进数据域
adaptor: 'return { status: 0, msg: "", data: payload };',
},
messages: { saveSuccess: 'AI 起草完成,请看下方状态' },
body: [
{ type: 'textarea', name: 'objective', label: '需求目标', required: true, minRows: 2, placeholder: '如:新增客户等级维护功能' },
{ type: 'input-text', name: 'featureName', label: '功能名', required: true, placeholder: '客户等级维护' },
{
type: 'input-text', name: 'tableName', label: '表名', required: true, placeholder: 'EIP4AI_CUSTOMER_LEVEL',
validations: { matchRegexp: '/^[A-Z][A-Z0-9_]{2,40}$/' },
validationErrors: { matchRegexp: '大写字母开头,3–41 位' },
},
{ type: 'input-text', name: 'entityName', label: '实体名', required: true, placeholder: '客户等级' },
{
type: 'radios', name: 'pageRecipe', label: '页面模板', value: 'maintenance-crud-v1',
options: [
{ label: '维护型 CRUD', value: 'maintenance-crud-v1' },
{ label: '树形 CRUD', value: 'tree-crud-v1' },
],
},
{
type: 'combo', name: 'fields', label: '字段', multiple: true, addable: true, removable: true,
addButtonText: '加字段', minLength: 1,
items: [
{ type: 'input-text', name: 'column', label: '列名', required: true, placeholder: 'NAME' },
{ type: 'input-text', name: 'label', label: '显示名', placeholder: '名称' },
{ type: 'select', name: 'type', label: '类型', value: 'string', options: TYPE_OPTIONS },
{ type: 'switch', name: 'required', label: '必填' },
],
},
{
type: 'alert', level: 'info', visibleOn: '${status}',
body: 'AI 起草状态:<b>${status}</b>(taskId=${taskId})。产物恒人审,平台不自动发布。',
},
{
type: 'button', label: '审阅草稿(进设计器)', level: 'primary', className: 'mt-2',
visibleOn: '${status === "READY_FOR_HUMAN_REVIEW"}',
actionType: 'url', url: '/editor.html?templateId=' + reviewTpl, blank: true,
},
{
type: 'alert', level: 'warning', visibleOn: '${status && status !== "READY_FOR_HUMAN_REVIEW"}',
body: '未达人审就绪状态(${status})。运行信息:${JOIN(messages, ";")}',
},
],
},
};
}
export function AiFeatureForm() {
const navigate = useNavigate();
const profile = getProfile();
const isAdmin = isAdminProfile(profile);
const schema = useMemo(buildSchema, []);
if (!isAdmin) {
return <div style={{ padding: 24, color: 'var(--text-3)' }}>AI 建功能仅管理员可用。</div>;
}
return (
<div style={{ height: '100%', display: 'flex', flexDirection: 'column' }} data-testid="ai-feature-form">
<div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: 'var(--pad-lg)', paddingBottom: 4 }}>
<button className="btn btn-sm" onClick={() => navigate('/design-center')}>← 返回设计中心</button>
<h2 style={{ margin: 0, fontSize: 18, fontWeight: 600 }}>⚡ AI 建功能</h2>
</div>
<div className="sub-text" style={{ padding: '0 var(--pad-lg)', marginBottom: 8 }}>
外部 AI agent 起草,产物恒人审;点「审阅草稿」进设计器,平台不自动发布。
</div>
<div style={{ flex: 1, minHeight: 0, overflow: 'auto', padding: '0 var(--pad-lg) var(--pad-lg)' }}>
<AmisRenderer schema={schema} />
</div>
</div>
);
}