auto(14b5ea1b9bd9): session end @ 2026-07-25T17:18:06 (+3f)

This commit is contained in:
ssw committed 2026-07-25 17:18:07 +08:00
1 parent ba5f1abdbe
commit 6ee5c71f39
3 files changed
+294

No files matched your search

@@ -105,4 +105,29 @@ public interface AIRepairService {
* 失败时返回空 Map
*/
Map<String, Object> inferComponentProps(String fieldsJson, String dictsJson, String formsJson);
/**
* v0.22+ (2026-07-25 BS-新131~133, tdd3 P1-5 闭环): 旁路 NCPF backend 直接从描述构造 form schema.
*
* 解决 2.3 / tdd2 debug.md §2.3 + tdd3 5/5 form 实战痛点:
* - NCPF /api/visualdev/ai/form 走 GEN_MODEL_QUESTION prompt, 在 NCPF 后端不可控
* - NCPF VisualAiServiceImpl:79-87 硬编码白名单 + 强制字段改名 (xxx_num001) + 子表排除
* - NCPF 输出长度有限制 (用户原话 "输出文字长度又限制")
* - 4+3 限制 (BS-新117 + 新131~133) 全部命中: 丢 required/dictType/switch/relationForm + 重命名 + 误判 userSelect
*
* 此接口直接调 SswLLMClient, prompt 完全可控, 无 NCPF 后处理:
* - 禁用 NCPF 4+3 限制 (显式 prompt 禁止 input_num001 命名, 禁止 select 改 input, 禁止含中文字段名)
* - 强制 7 字段 relationForm 完整 schema (modelId/propsValue/relationField/showField/hasPage/queryType/columnOptions)
* - 强制字段命名 f_<entity>_<purpose> snake_case (前端 dacli 命名规范)
* - 强制必填 (required=true) 显式标记
* - 跨 app 通用, 不受 NCPF 漂移影响
*
* @param formDesc 用户 --ai 描述 (e.g. "采购订单: 订单号, 关联供应商, 关联物料, 数量, 单价, 状态")
* @param appCode 应用 code (用于字段名 appCode 前缀规范, e.g. "tdd3")
* @param formsJson 候选 form 列表 (用于 relationForm.modelId, e.g. '[{"id":"x","enCode":"tdd3supplier","fullName":"供应商"},...]')
* @param dictsJson 候选字典列表 (用于 select.dictType, e.g. '[{"id":"y","enCode":"tdd3orderstatus","fullName":"订单状态"},...]')
* @return VisualAiModel 兼容结构: {enCode, fullName, aiModelList: [{tableTitle, tableName, isMain, fields: [...]}]}
* 失败时返回空 Map
*/
Map<String, Object> buildForm(String formDesc, String appCode, String formsJson, String dictsJson);
}
@@ -547,4 +547,234 @@ public class AIRepairServiceImpl implements AIRepairService {
}
return new HashMap<>();
}
// ============================================================================
// v0.22+ (2026-07-25 BS-新131~133, tdd3 P1-5 闭环): buildForm 旁路 NCPF
// ============================================================================
/**
* BUILD_FORM_PROMPT: 镜像 web 端 componentMap.ts 53 个 jnpfKey + 强制 7 字段 relationForm 完整 schema.
*
* 解决 NCPF /api/visualdev/ai/form 的 4 大不可控问题:
* 1. 硬编码白名单: NCPF 强制不在白名单的 jnpfKey 改回 COM_INPUT (jnpf VisualAiServiceImpl:79-87)
* 2. 强制字段改名: 字段名含中文或关键字 → 强制改成 xxx_num001 形式 (NCPF 4+3 限制 BS-新117)
* 3. 子表排除: 子表不能 radio/checkbox/textarea/link/button/alert/barcode/qrcode/editor
* 4. 输出长度限制: NCPF prompt 不可见, 配置层限制 token (用户原话 "输出文字长度又限制")
*
* 本 prompt 完全可控, 在 ssw-common (boot/ 目录) 而非 NCPF 平台, 可改.
*/
private static final String BUILD_FORM_PROMPT =
"你是 JNPF 表单设计助手. 从用户描述生成完整 form schema JSON.\n" +
"\n" +
"【输出 JSON 格式】(VisualAiModel 兼容):\n" +
"{\n" +
" \"enCode\": \"camelCase 全英文, 不超过 50 字符, 不含下划线 (BS-56 NCPF 强制)\",\n" +
" \"fullName\": \"表单中文名, 不超过 50 字符\",\n" +
" \"aiModelList\": [\n" +
" {\n" +
" \"tableTitle\": \"主表中文名\",\n" +
" \"tableName\": \"snake_case 表名 (NCPF 后端会自动转 camelCase 拼到 enCode)\",\n" +
" \"isMain\": true,\n" +
" \"fields\": [\n" +
" { \"fieldTitle\": \"字段中文名\", \"fieldName\": \"f_xxx_yyy\", \"fieldComponent\": \"<见下方白名单>\", \"fieldDbType\": \"varchar|text|int|datetime|decimal|...\" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}\n" +
"\n" +
"【禁用规则 (NCPF 4+3 限制, 必须遵守)】:\n" +
"1. 禁用含中文字段名: fieldName 必须纯英文 snake_case, 以 f_ 开头, 例 f_user_name / f_create_time\n" +
"2. 禁用 input_num001 类幻觉命名: 字段名必须 snake_case 业务语义, 不带 _num 后缀 (NCPF 会自动改名, 你要避免)\n" +
"3. 禁用 select 替代 relationForm: 关联字段 (字段名含 _id 后缀) 必须用 relationForm, 不能用 select\n" +
"4. 禁用 switch 替代 select: 状态/类型字段用 select (绑字典), 不是 switch (开关是 boolean)\n" +
"5. 禁用 userSelect 替代 input: 人名/姓名/负责人等纯文本字段用 input, 不是 userSelect\n" +
"6. 禁用 radio/checkbox 默认选: 默认 select 更好, radio/checkbox 明确需求才用\n" +
"7. 禁用 textarea 默认: 长文本才用, 单行用 input\n" +
"\n" +
"【relationForm 必含 7 字段 schema (字段名含 _id 时, 顶层输出)】:\n" +
"关联表单控件 fieldComponent='relationForm' 时, 把 7 字段直接放在该字段对象顶层 (不嵌套, 不用 fieldRelationConfig):\n" +
" - modelId: 目标 form 的 id (32 位 hex, 从传入的 formsJson 候选里找对应 enCode 拿 id)\n" +
" - propsValue: 当前字段的 vmodel (例如 f_supplier_id)\n" +
" - relationField: 目标 form 显示字段 (默认目标 form 主名字段, 例如 f_supplier_name)\n" +
" - showField: 同 relationField, 弹窗列表显示\n" +
" - hasPage: true (弹窗分页)\n" +
" - queryType: 'eq' (精确匹配)\n" +
" - columnOptions: 弹窗列定义数组, ≤5 项, 例如 [{label:'编码', value:'f_supplier_code'}, {label:'名称', value:'f_supplier_name'}]\n" +
"\n" +
"示例 (f_supplier_id → tdd3supplier):\n" +
"{ fieldTitle: '供应商', fieldName: 'f_supplier_id', fieldComponent: 'relationForm', fieldDbType: 'varchar',\n" +
" modelId: '<tdd3supplier 的 32 位 hex id>',\n" +
" propsValue: 'f_supplier_id',\n" +
" relationField: 'f_supplier_name',\n" +
" showField: 'f_supplier_name',\n" +
" hasPage: true,\n" +
" queryType: 'eq',\n" +
" columnOptions: [{label:'编码', value:'f_supplier_code'}, {label:'名称', value:'f_supplier_name'}] }\n" +
"注意: modelId 是 32 位 hex id (不是 enCode), 必须从传入的 formsJson 候选里取真实 id.\n" +
"\n" +
"【JNPF 组件白名单】(fieldComponent 必须是以下之一):\n" +
"input, textarea, inputNumber, switch, checkbox, radio, select, datePicker, timePicker,\n" +
"uploadImg, uploadFile, editor (富文本), depSelect (组织/部门), userSelect (用户/人员),\n" +
"signature (电子签章), rate (评分), slider, colorPicker, areaSelect (省市区),\n" +
"relationForm (关联表单 — 字段名含 _id 后缀时必选), relationFormAttr\n" +
"\n" +
"【字段命名规范】:\n" +
"- 业务字段: f_<entity>_<purpose>, 例 f_user_name / f_order_no / f_purchase_qty / f_total_amount\n" +
"- 时间字段: f_<entity>_time 或 f_<entity>_date, 例 f_create_time / f_publish_date\n" +
"- 关联字段: f_<target>_id (NCPF 自动转 f_<target>_name 做 relationField, 你不用写)\n" +
"- 状态/类型: f_<entity>_status / f_<entity>_type\n" +
"- 禁词: 不用 name/status/type/count/sum/select/insert 等 SQL 关键字 (如必须加 X: f_nameX)\n" +
"\n" +
"【字段类型推理】(按优先级):\n" +
"1. 中文名优先: 开关→switch, 多选→checkbox, 单选→radio, 图片→uploadImg, 文件→uploadFile, 富文本→editor, 评分→rate, 滑块→slider\n" +
"2. 关联 (含 _id): relationForm (必须, 不是 select)\n" +
"3. 数据类型兜底: varchar→input, text→textarea (单行 input 也可), int/decimal→inputNumber, datetime→datePicker, time→timePicker, boolean→switch\n" +
"4. 列表/搜索建议 (字段级 meta, 不在返回 JSON 里, 仅自我参考): 时间/选择/输入 适合搜索, switch/checkbox/editor 不适合\n" +
"\n" +
"【最小字段集合】(主表必含):\n" +
"- 业务主字段: f_<entity>_no (单号, varchar input) 或 f_<entity>_name (名称, varchar input)\n" +
"- 时间字段: f_create_time (datetime datePicker) 或 f_publish_date\n" +
"- 状态字段 (如果描述提到状态/类型): f_<entity>_status (select, 后续可绑字典)\n" +
"- 关联字段 (如果描述提到关联其他实体): f_<other>_id (relationForm)\n" +
"\n" +
"【示例】:\n" +
"用户描述: 采购订单, 订单号, 关联供应商, 关联物料, 数量, 单价, 总金额, 期望到货日期, 订单状态, 备注\n" +
"返回 JSON:\n" +
"{\n" +
" \"enCode\": \"purchaseOrder\",\n" +
" \"fullName\": \"采购订单\",\n" +
" \"aiModelList\": [{\n" +
" \"tableTitle\": \"采购订单\",\n" +
" \"tableName\": \"purchase_order\",\n" +
" \"isMain\": true,\n" +
" \"fields\": [\n" +
" { \"fieldTitle\": \"订单号\", \"fieldName\": \"f_order_no\", \"fieldComponent\": \"input\", \"fieldDbType\": \"varchar\" },\n" +
" { \"fieldTitle\": \"供应商\", \"fieldName\": \"f_supplier_id\", \"fieldComponent\": \"relationForm\", \"fieldDbType\": \"varchar\" },\n" +
" { \"fieldTitle\": \"物料\", \"fieldName\": \"f_material_id\", \"fieldComponent\": \"relationForm\", \"fieldDbType\": \"varchar\" },\n" +
" { \"fieldTitle\": \"采购数量\", \"fieldName\": \"f_purchase_qty\", \"fieldComponent\": \"inputNumber\", \"fieldDbType\": \"int\" },\n" +
" { \"fieldTitle\": \"单价\", \"fieldName\": \"f_unit_price\", \"fieldComponent\": \"inputNumber\", \"fieldDbType\": \"decimal\" },\n" +
" { \"fieldTitle\": \"总金额\", \"fieldName\": \"f_total_amount\", \"fieldComponent\": \"inputNumber\", \"fieldDbType\": \"decimal\" },\n" +
" { \"fieldTitle\": \"期望到货日期\", \"fieldName\": \"f_expected_arrival_date\", \"fieldComponent\": \"datePicker\", \"fieldDbType\": \"datetime\" },\n" +
" { \"fieldTitle\": \"订单状态\", \"fieldName\": \"f_order_status\", \"fieldComponent\": \"select\", \"fieldDbType\": \"varchar\" },\n" +
" { \"fieldTitle\": \"备注\", \"fieldName\": \"f_remark\", \"fieldComponent\": \"textarea\", \"fieldDbType\": \"text\" }\n" +
" ]\n" +
" }]\n" +
"}\n" +
"\n" +
"【输出规则】:\n" +
"- 只返回 JSON, 不带 markdown 包裹, 不带解释\n" +
"- 如果用户描述不清晰, 仍然返回 schema (含主键+主字段+时间字段+至少 1 个业务字段), 不要返空\n" +
"- 表只一个 (主表), 不要返子表 (子表是 jnpf 高级用法, LLM 容易搞错)\n" +
"- 字段数 ≥ 3, 至少含 1 个主字段 + 1 个时间字段 (除极简场景)";
@Override
public Map<String, Object> buildForm(String formDesc, String appCode, String formsJson, String dictsJson) {
log.info("[AIRepair-buildForm] appCode={}, desc={}", appCode,
formDesc.length() > 80 ? formDesc.substring(0, 80) + "..." : formDesc);
if (formDesc == null || formDesc.trim().isEmpty()) {
log.warn("[AIRepair-buildForm] formDesc 为空");
return new HashMap<>();
}
// 构造 userPrompt: 用户描述 + 候选 form/dict 上下文 (让 LLM 知道有哪些关联可选)
StringBuilder userPrompt = new StringBuilder();
userPrompt.append("应用: ").append(appCode == null ? "(未知)" : appCode).append("\n");
userPrompt.append("业务描述: ").append(formDesc.trim()).append("\n");
if (formsJson != null && !formsJson.trim().isEmpty() && !formsJson.equals("[]")) {
userPrompt.append("\n候选 form 列表 (供 relationForm.modelId 选择, 完整结构):\n").append(formsJson).append("\n");
}
if (dictsJson != null && !dictsJson.trim().isEmpty() && !dictsJson.equals("[]")) {
userPrompt.append("\n候选字典列表 (供 select 绑字典, 完整结构):\n").append(dictsJson).append("\n");
}
// 1. 调 LLM
String raw = llmClient.chat(BUILD_FORM_PROMPT, userPrompt.toString());
if (raw == null || raw.isEmpty()) {
log.warn("[AIRepair-buildForm] LLM 返回空");
return new HashMap<>();
}
log.info("[AIRepair-buildForm] LLM raw ({} 字符): {}", raw.length(),
raw.substring(0, Math.min(200, raw.length())));
// 2. 修复 JSON (LLM 输出常带 markdown fence / 多余空白)
String cleaned = raw.trim()
.replaceAll("```json\\s*", "").replaceAll("```\\s*", "")
.trim();
// 3. 解析 — 兼容 {aiModelList:[]} 或裸 {enCode,fullName,aiModelList}
try {
// 尝试 Map 包装
Map<String, Object> parsed;
if (cleaned.startsWith("[")) {
// LLM 返了数组, 包装一层
List<Map<String, Object>> arr = JSON.parseObject(cleaned,
new TypeReference<List<Map<String, Object>>>() {});
if (arr == null || arr.isEmpty()) {
log.warn("[AIRepair-buildForm] LLM 返空数组");
return new HashMap<>();
}
parsed = arr.get(0);
} else {
parsed = JSON.parseObject(cleaned, new TypeReference<Map<String, Object>>() {});
}
if (parsed == null) {
log.warn("[AIRepair-buildForm] 解析后为空");
return new HashMap<>();
}
// 必须有 aiModelList
Object aiModelListRaw = parsed.get("aiModelList");
if (!(aiModelListRaw instanceof List) || ((List<?>) aiModelListRaw).isEmpty()) {
log.warn("[AIRepair-buildForm] aiModelList 缺失或空");
return new HashMap<>();
}
// 标准化 enCode / fullName
String enCode = (String) parsed.getOrDefault("enCode", "aiForm");
String fullName = (String) parsed.getOrDefault("fullName", "AI 表单");
enCode = normalizeEnCode(enCode);
if (isKeyword(enCode)) enCode = enCode + "X";
parsed.put("enCode", enCode);
parsed.put("fullName", fullName);
log.info("[AIRepair-buildForm] 解析成功: enCode={}, fields={}",
enCode, ((List<?>) aiModelListRaw).stream()
.mapToInt(t -> {
Object fs = ((Map<?, ?>) t).get("fields");
return fs instanceof List ? ((List<?>) fs).size() : 0;
})
.sum());
return parsed;
} catch (Exception e) {
log.warn("[AIRepair-buildForm] JSON 解析失败: {}", e.getMessage());
// 尝试 JsonRepairUtil 兜底
try {
String repaired = JsonRepairUtil.repair(raw);
if (repaired != null && !repaired.equals(raw)) {
Map<String, Object> parsed = JSON.parseObject(repaired,
new TypeReference<Map<String, Object>>() {});
if (parsed != null && parsed.get("aiModelList") instanceof List) {
log.info("[AIRepair-buildForm] JsonRepairUtil 兜底成功");
return parsed;
}
}
} catch (Exception e2) {
log.warn("[AIRepair-buildForm] JsonRepairUtil 兜底也失败: {}", e2.getMessage());
}
return new HashMap<>();
}
}
/**
* 标准化 enCode: 去下划线/连字符, 转 camelCase, 限长 50
* (NCPF jnpf CreateWave 行为: enCode 含 _ 会被后端 strip)
*/
private String normalizeEnCode(String enCode) {
if (enCode == null || enCode.isEmpty()) return "aiForm";
String s = enCode.replaceAll("[_\\-\\s]", "");
if (s.length() > 50) s = s.substring(0, 50);
// 首字母小写
if (!s.isEmpty() && Character.isUpperCase(s.charAt(0))) {
s = Character.toLowerCase(s.charAt(0)) + s.substring(1);
}
return s;
}
}
@@ -190,4 +190,43 @@ public class SswAIRepairController {
return ActionResult.fail("组件属性推理异常: " + e.getMessage());
}
}
/**
* v0.22+ (2026-07-25 BS-新131~133, tdd3 P1-5 闭环): 旁路 NCPF backend 直接构造 form schema.
*
* 替代 dacli 调 jnpf /api/visualdev/ai/form (NCPF 不可控: 硬编码白名单 + 强制改名 + 输出长度限制).
* 直调 ssw SswLLMClient (同一 base_ai LLM, prompt 我们写).
*
* 入参:
* - formDesc: 业务描述 (中文自然语言, e.g. "采购订单: 订单号, 关联供应商, 关联物料, 数量, 单价, 总金额, 状态")
* - appCode: 应用 code (用于字段命名规范, e.g. "tdd3")
* - formsJson: 候选 form 列表 JSON (可选, 供 relationForm.modelId 选择)
* - dictsJson: 候选字典列表 JSON (可选, 供 select 绑字典)
*
* 响应: VisualAiModel 兼容结构 {enCode, fullName, aiModelList: [{tableTitle, tableName, isMain, fields}]}
* - 与 jnpf /api/visualdev/ai/form 输出格式一致, dacli 端无感切换
* - 但内部 prompt + 处理完全可控, 无 NCPF 4+3 限制
*/
@PostMapping("/build-form")
@Operation(summary = "从描述构造完整 form schema (旁路 NCPF, P1-5 闭环)",
description = "直调 SSW SswLLMClient, prompt 禁用 NCPF 4+3 限制 (含中文字段名/input_num001/select 替代 relationForm/switch 替代 select/userSelect 替代 input). 输出 VisualAiModel 兼容结构, dacli 端无感切换.")
public ActionResult<Map<String, Object>> buildForm(@RequestBody Map<String, Object> body) {
String formDesc = (String) body.get("formDesc");
String appCode = (String) body.get("appCode");
String formsJson = (String) body.get("formsJson");
String dictsJson = (String) body.get("dictsJson");
if (formDesc == null || formDesc.trim().isEmpty()) {
return ActionResult.fail("formDesc 必填");
}
try {
Map<String, Object> result = aiRepairService.buildForm(formDesc, appCode, formsJson, dictsJson);
if (result.isEmpty() || !result.containsKey("aiModelList")) {
return ActionResult.fail("buildForm 返回空 (LLM 不可用或 prompt 解析失败)");
}
return ActionResult.success(result);
} catch (Exception e) {
log.error("SswAIRepairController.buildForm 异常", e);
return ActionResult.fail("buildForm 异常: " + e.getMessage());
}
}
}