feat(form): Task 7 v0.11+ --ai 路径用 web 端 buildFormData (替代 _simplify)
新增:
- src/commands/form/_buildformdata.ts: 从 web/apps/.../formUtils.ts
动态 import buildFormData + buildFields (替换 #/ alias + @jnpf/utils
+ lodash-es, 单进程缓存)
改造:
- src/commands/form/create.ts --ai 路径:
- initBuildFormData() 加载 web buildFormData
- 把 webBuildFormData(aiModelList) 结果存到 opts.__aiBuildFormData
- formData 构造处: AI 路径用 buildFormData 输出, 其它用 _simplify
- 效果: AI 的 component 类型 (switch/radio/datePicker/select) 100% 保留
- 解决: BS-51 (formId)、BS-52 (options)、BS-54 (select options) 在 AI 路径全消失
验证 (formId=839790442198336261 enCode=customerInfo):
customer_name → JnpfInput
contact_phone → JnpfInput
status_flag → JnpfSwitch
customer_type → JnpfRadio + [{id:1, fullName:个人}, {id:2, fullName:企业}]
register_date → JnpfDatePicker
formId 全部是 formItemAiXXX 字符串 (UUID)
未删 _simplify (手动 --fields 路径仍依赖), 后续按用户决策再瘦身
This commit is contained in:
1 parent
5473fd4c6b
commit
d9505ae94b
2 files changed
+140
-44
No files matched your search
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* _buildformdata.ts — 复用 web 端 buildFormData (v0.11+)
|
||||
*
|
||||
* 解决: BS-51/BS-52/BS-54/BS-55 (dacli 复刻 buildFormData 漏 formId/renderKey/isSubTable/options)
|
||||
*
|
||||
* 设计:
|
||||
* - 不重写 buildFormData, 而是从 web 端源码 import 原版
|
||||
* - web 端用 Vite alias (#/...) + @jnpf/utils + lodash-es, Node 不直接认
|
||||
* - 解决: 读源码 → 替换 import → 写 tmp → dynamic import
|
||||
* - 缓存: 同一进程只做一次 (模块级 cache)
|
||||
*
|
||||
* 注意:
|
||||
* - 依赖 web/apps/jnpf-web-apps-main/src/components/FormGenerator/src/helper/ 存在
|
||||
* - 依赖 web/packages/jnpf/utils/src/helpers/uuid.ts 存在
|
||||
* - 不修改 web/ 源码 (CLAUDE.md §15 禁止)
|
||||
*/
|
||||
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
const WEB_SRC = '/home/ubuntu/ccWorkDir/web/apps/jnpf-web-apps-main/src';
|
||||
const FORM_UTILS_SRC = `${WEB_SRC}/components/FormGenerator/src/helper/formUtils.ts`;
|
||||
const JNPF_UTILS_UUID = '/home/ubuntu/ccWorkDir/web/packages/jnpf/utils/src/helpers/uuid.ts';
|
||||
|
||||
let cachedBuildFormData: ((aiModelList: any[]) => any) | null = null;
|
||||
let cachedBuildFields: ((aiModelList: any[]) => any[]) | null = null;
|
||||
|
||||
/** 加载 web 端的 buildFormData + buildFields (单次缓存) */
|
||||
async function loadWebBuildFormData(): Promise<{
|
||||
buildFormData: (aiModelList: any[]) => any;
|
||||
buildFields: (aiModelList: any[]) => any[];
|
||||
}> {
|
||||
if (cachedBuildFormData && cachedBuildFields) {
|
||||
return { buildFormData: cachedBuildFormData, buildFields: cachedBuildFields };
|
||||
}
|
||||
// 1. 读 formUtils.ts, 替换 3 个 import:
|
||||
// - #/... → 绝对路径
|
||||
// - @jnpf/utils → web 端 uuid.ts (避开 Vite env)
|
||||
// - lodash-es → Node 内置 structuredClone
|
||||
let src = readFileSync(FORM_UTILS_SRC, 'utf-8');
|
||||
src = src
|
||||
.replace(/from\s+['"]#\/(.+?)['"]/g, (_, p) => `from '${WEB_SRC}/${p}'`)
|
||||
.replace(/from\s+['"]@jnpf\/utils['"]/g, `from '${JNPF_UTILS_UUID}'`)
|
||||
.replace(/import\s*\{\s*cloneDeep\s*\}\s*from\s+['"]lodash-es['"];?/g,
|
||||
`const cloneDeep = (x) => structuredClone(x);`);
|
||||
|
||||
// 2. 写到 tmp, dynamic import
|
||||
const tmpDir = `${tmpdir()}/dacli-bfd-${process.pid}-${Date.now()}`;
|
||||
mkdirSync(tmpDir, { recursive: true });
|
||||
const tmpFile = `${tmpDir}/formUtils.ts`;
|
||||
writeFileSync(tmpFile, src);
|
||||
|
||||
const mod = await import(pathToFileURL(tmpFile).href);
|
||||
cachedBuildFormData = mod.buildFormData;
|
||||
cachedBuildFields = mod.buildFields;
|
||||
return { buildFormData: mod.buildFormData, buildFields: mod.buildFields };
|
||||
}
|
||||
|
||||
/** 同步版: 取已缓存的 buildFormData (loadWebBuildFormData 必须先 await) */
|
||||
export function buildFormData(aiModelList: any[]): any {
|
||||
if (!cachedBuildFormData) {
|
||||
throw new Error('buildFormData 未初始化, 请先 await initBuildFormData()');
|
||||
}
|
||||
return cachedBuildFormData(aiModelList);
|
||||
}
|
||||
|
||||
export function buildFields(aiModelList: any[]): any[] {
|
||||
if (!cachedBuildFields) {
|
||||
throw new Error('buildFields 未初始化, 请先 await initBuildFormData()');
|
||||
}
|
||||
return cachedBuildFields(aiModelList);
|
||||
}
|
||||
|
||||
/** 初始化 (在命令入口处调用一次) */
|
||||
export async function initBuildFormData(): Promise<void> {
|
||||
await loadWebBuildFormData();
|
||||
}
|
||||
|
||||
/** 用于 --ai-help 调试 */
|
||||
export function isBuildFormDataLoaded(): boolean {
|
||||
return cachedBuildFormData !== null;
|
||||
}
|
||||
+58
-44
@@ -12,6 +12,7 @@ import { JnpfClient } from '../../core/client.ts'
|
||||
import { installAiHelpHook } from '../../core/help.ts'
|
||||
import { log, printJson } from '../../core/log.ts'
|
||||
import { callAiForm, printAiFormResult } from './_ai.ts'
|
||||
import { initBuildFormData, buildFormData as webBuildFormData, isBuildFormDataLoaded } from './_buildformdata.ts'
|
||||
import { wrapSimplifiedFields, buildTablesFromFields, toSimpleField, type FullField, type SimpleField } from './_simplify.ts'
|
||||
import { browserVerify } from './_browser_verify.ts'
|
||||
import { createFormViaUi } from './_create_ui.ts'
|
||||
@@ -114,6 +115,16 @@ export function buildFormCreateCommand(): Command {
|
||||
log.info(`AI 生成 ${fields.length} 个字段, 已塞进 --fields`)
|
||||
}
|
||||
log.info(`AI 建议 enCode=${opts.enCode} fullName=${opts.fullName}`)
|
||||
// v0.11+ (Task 7 改造): --ai 路径用 web 端 buildFormData 而非 _simplify
|
||||
// 解决: AI 返回的 switch/datePicker/select 在 _simplify 里被简化成 input
|
||||
// buildFormData 100% 注入 formId/renderKey/isSubTable/options
|
||||
await initBuildFormData()
|
||||
const aiModelListForBfd = data.aiModelList || []
|
||||
const bfdResult = webBuildFormData(aiModelListForBfd)
|
||||
// bfdResult.formData 是 JSON 字符串, 里面有完整的 50+ formConf keys + fields
|
||||
// 后面 213 行的 _simplify formData 会被这个覆盖 (见 useBuildFormData 标志)
|
||||
;(opts as any).__aiBuildFormData = bfdResult
|
||||
log.info(`v0.11+ --ai 走 web buildFormData, formData.fields=${JSON.parse(bfdResult.formData).fields.length}`)
|
||||
// 强制 type=4 兜底 (AI 不会给完整 tables, 手动升级 type=1 需手工补 dm/tables)
|
||||
opts.type = '4'
|
||||
opts.webType = opts.webType || '1'
|
||||
@@ -210,50 +221,53 @@ export function buildFormCreateCommand(): Command {
|
||||
log.info(`D-3: 自动包 ${converted}/${total} 字段为 __config__ schema (解决盲点 42)`)
|
||||
}
|
||||
|
||||
const formData = {
|
||||
formRef: 'formRef',
|
||||
formModel: 'dataForm',
|
||||
size: 'middle',
|
||||
labelPosition: 'right',
|
||||
labelWidth: 100,
|
||||
labelSuffix: '',
|
||||
formRules: 'rules',
|
||||
popupType: 'general',
|
||||
generalWidth: '600px',
|
||||
fullScreenWidth: '100%',
|
||||
drawerWidth: '600px',
|
||||
gutter: 15,
|
||||
disabled: false,
|
||||
span: 24,
|
||||
colon: false,
|
||||
hasCancelBtn: true,
|
||||
cancelButtonText: '取消',
|
||||
cancelButtonTextI18nCode: 'common.cancelText',
|
||||
hasConfirmBtn: true,
|
||||
confirmButtonText: '确定',
|
||||
confirmButtonTextI18nCode: 'common.okText',
|
||||
hasConfirmAndAddBtn: true,
|
||||
hasPrintBtn: false,
|
||||
printButtonText: '打印',
|
||||
printButtonTextI18nCode: 'common.printText',
|
||||
customBtns: [],
|
||||
appCustomBtns: [],
|
||||
primaryKeyPolicy: 1,
|
||||
concurrencyLock: false,
|
||||
logicalDelete: false,
|
||||
dataLog: false,
|
||||
useBusinessKey: false,
|
||||
businessKeyList: [],
|
||||
businessKeyTip: '数据已存在,请勿重复提交!',
|
||||
printId: '',
|
||||
formStyle: '',
|
||||
classNames: [],
|
||||
className: [],
|
||||
classJson: '',
|
||||
detailExtraList: [],
|
||||
fields: fieldsArr,
|
||||
tableName: opts.enCode,
|
||||
}
|
||||
const formData = (opts as any).__aiBuildFormData
|
||||
? // v0.11+ --ai 路径: 用 web buildFormData (formId/renderKey/isSubTable/options 100% 完整)
|
||||
JSON.parse((opts as any).__aiBuildFormData.formData)
|
||||
: {
|
||||
formRef: 'formRef',
|
||||
formModel: 'dataForm',
|
||||
size: 'middle',
|
||||
labelPosition: 'right',
|
||||
labelWidth: 100,
|
||||
labelSuffix: '',
|
||||
formRules: 'rules',
|
||||
popupType: 'general',
|
||||
generalWidth: '600px',
|
||||
fullScreenWidth: '100%',
|
||||
drawerWidth: '600px',
|
||||
gutter: 15,
|
||||
disabled: false,
|
||||
span: 24,
|
||||
colon: false,
|
||||
hasCancelBtn: true,
|
||||
cancelButtonText: '取消',
|
||||
cancelButtonTextI18nCode: 'common.cancelText',
|
||||
hasConfirmBtn: true,
|
||||
confirmButtonText: '确定',
|
||||
confirmButtonTextI18nCode: 'common.okText',
|
||||
hasConfirmAndAddBtn: true,
|
||||
hasPrintBtn: false,
|
||||
printButtonText: '打印',
|
||||
printButtonTextI18nCode: 'common.printText',
|
||||
customBtns: [],
|
||||
appCustomBtns: [],
|
||||
primaryKeyPolicy: 1,
|
||||
concurrencyLock: false,
|
||||
logicalDelete: false,
|
||||
dataLog: false,
|
||||
useBusinessKey: false,
|
||||
businessKeyList: [],
|
||||
businessKeyTip: '数据已存在,请勿重复提交!',
|
||||
printId: '',
|
||||
formStyle: '',
|
||||
classNames: [],
|
||||
className: [],
|
||||
classJson: '',
|
||||
detailExtraList: [],
|
||||
fields: fieldsArr,
|
||||
tableName: opts.enCode,
|
||||
}
|
||||
// 2026-06-25 tdd9 BS-55: 同步去 appCode 前缀 (与 dm 物理表名一致, 避免后端 "请在数据库中添加对应的数据表")
|
||||
// formData 顶层 tableName 也用 cleanTable, 匹配物理表
|
||||
const appCode55 = (c as any).appCode as string
|
||||
|
||||
Reference in new issue
Block a user