Files
eip4-web/src/pages/AiFeatureForm.tsx
zhengwr 71535929f7 refactor(design-center): 编排壳/AI入口改用 amis 渲染(替手搓原生表单)
用户反馈手搓 input/select/嵌套弹窗又丑又不一致。改:
- NewFeatureShell → amis wizard(建实体 combo字段含引用列 → 选recipe → 发布),三步 api 直打后端、数据自动透传
- AiFeatureForm → amis form(api adaptor 包裹非标响应 + ARRAYFILTER 取草稿 targetId)
- 组件测试改为守 admin/容器(amis 内部交 Playwright);five-ring.spec 改驱动 amis wizard
与全站业务页同一套 AmisRenderer,外观一致。wizard e2e 1 passed / 72 vitest / build 绿。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 14:39:00 +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 } 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 = profile?.account === 'admin' || profile?.userId === 'ADMIN';
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>
);
}