268 lines
14 KiB
Java
268 lines
14 KiB
Java
package ssw.common.controller;
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import jnpf.base.ActionResult;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import ssw.common.service.AIRepairService;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* SSW AI 修复 Controller
|
|
*
|
|
* 用途: 当 jnpf 后端 /api/visualdev/ai/form 返回空 data 时,
|
|
* dacli fallback 调本端点, 自己调 LLM + 修复 JSON.
|
|
*
|
|
* v0.15+ (BS-新30 form + workflow 同步, 2026-07-02):
|
|
* - 新加 /api/ssw/ai/repair-workflow 端点, 镜像 jnpf /api/workflow/ai 行为
|
|
* - 同样的 maxTokens=3000 + JsonRepairUtil 修复层
|
|
* - 同样的 retry/fallback 模式 (dacli 端走 5s/10s/20s)
|
|
*
|
|
* 端点:
|
|
* - POST /api/ssw/ai/repair (form)
|
|
* 请求: {"keyword": "用户管理表:姓名、电话..."}
|
|
* 响应: jnpf VisualAiModel 兼容结构 (enCode/fullName/aiModelList)
|
|
* - POST /api/ssw/ai/repair-workflow (workflow, v0.15+)
|
|
* 请求: {"keyword": "请假流程: 员工提交 → 经理审批 → HR 备案"}
|
|
* 响应: jnpf WorkFlowAiModel 兼容结构 (enCode/fullName/flowXml/sortCode)
|
|
*
|
|
* @author claude (2026-07-02)
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api/ssw/ai")
|
|
@Tag(name = "SSW AI 修复", description = "绕过 jnpf SendAiMessage, 直接调 LLM 修复 (form + workflow)")
|
|
public class SswAIRepairController {
|
|
|
|
@Autowired
|
|
private AIRepairService aiRepairService;
|
|
|
|
@PostMapping("/repair")
|
|
@Operation(summary = "AI 修复 (form)", description = "绕开 jnpf SendAiMessage, SSW 直接调 LLM 修复 JSON")
|
|
public ActionResult<Map<String, Object>> repair(@RequestBody Map<String, Object> body) {
|
|
String keyword = (String) body.get("keyword");
|
|
if (keyword == null || keyword.isEmpty()) {
|
|
return ActionResult.fail("keyword 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.repair(keyword);
|
|
if (result.isEmpty() || !result.containsKey("enCode")) {
|
|
return ActionResult.fail("AI 修复失败, 返回空结果");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.repair 异常", e);
|
|
return ActionResult.fail("AI 修复异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.15+ (BS-新30 workflow): AI 修复 (workflow)
|
|
*
|
|
* 跟 form /repair 平行端点, 复用同样的 SSW 拦截层
|
|
* (SswLLMClient maxTokens=3000 + JsonRepairUtil 修复层)
|
|
*
|
|
* 适用场景: dacli workflow create --ai 走 JNPF 后端 /api/workflow/ai 失败时
|
|
* (LLM 输出超 1500 token 被截断, 后端 SYS181 错误), fallback 到本端点
|
|
*/
|
|
@PostMapping("/repair-workflow")
|
|
@Operation(summary = "AI 修复 (workflow)", description = "SSW 直接调 LLM 生成 BPMN XML, 防 1500 token 截断")
|
|
public ActionResult<Map<String, Object>> repairWorkflow(@RequestBody Map<String, Object> body) {
|
|
String keyword = (String) body.get("keyword");
|
|
if (keyword == null || keyword.isEmpty()) {
|
|
return ActionResult.fail("keyword 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.repairWorkflow(keyword);
|
|
if (result.isEmpty() || !result.containsKey("flowXml")) {
|
|
return ActionResult.fail("workflow AI 修复失败, 返回空结果");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.repairWorkflow 异常", e);
|
|
return ActionResult.fail("workflow AI 修复异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.15+ (2026-07-03): 字段类型推理 (dacli form --table 专用)
|
|
*
|
|
* 从已有物理表字段列表推理 JNPF 组件类型.
|
|
* 跟 /repair 区别: 不是从零设计表单, 而是基于已有表的列定义做字段→组件映射.
|
|
*
|
|
* 请求: {tableName: "mt842651242256662597", fieldDesc: "单行输入(input, varchar), 开关(...), ..."}
|
|
* 响应: jnpf VisualAiModel 兼容结构 (enCode/fullName/aiModelList)
|
|
*/
|
|
@PostMapping("/infer-fields")
|
|
@Operation(summary = "字段类型推理 (form --table)", description = "从已有表字段推理JNPF组件类型, 字段→组件映射")
|
|
public ActionResult<Map<String, Object>> inferFields(@RequestBody Map<String, Object> body) {
|
|
String tableName = (String) body.get("tableName");
|
|
String fieldDesc = (String) body.get("fieldDesc");
|
|
if (tableName == null || tableName.isEmpty() || fieldDesc == null || fieldDesc.isEmpty()) {
|
|
return ActionResult.fail("tableName 和 fieldDesc 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.inferFields(tableName, fieldDesc);
|
|
if (result.isEmpty() || !result.containsKey("aiModelList")) {
|
|
return ActionResult.fail("字段推理失败, 返回空结果");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.inferFields 异常", e);
|
|
return ActionResult.fail("字段推理异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.15+ (2026-07-03): 业务分类推理 (dacli form --ai / --table 通用)
|
|
*
|
|
* 根据表单名+应用名, 从可选分类列表中选择最合适的业务分类.
|
|
*
|
|
* 请求: {formName: "用户管理", appCode: "testspace", categories: "[{\"id\":\"x\",\"fullName\":\"车辆管理\"},...]"}
|
|
* 响应: {id: "xxx", fullName: "车辆管理"}
|
|
*/
|
|
@PostMapping("/infer-category")
|
|
@Operation(summary = "业务分类推理", description = "根据表单+应用名从可选分类中选最合适的")
|
|
public ActionResult<Map<String, Object>> inferCategory(@RequestBody Map<String, Object> body) {
|
|
String formName = (String) body.get("formName");
|
|
String appCode = (String) body.get("appCode");
|
|
String categories = (String) body.get("categories");
|
|
if (formName == null || categories == null) {
|
|
return ActionResult.fail("formName 和 categories 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.inferCategory(
|
|
formName, appCode != null ? appCode : "", categories);
|
|
if (result.isEmpty() || !result.containsKey("fullName")) {
|
|
return ActionResult.fail("分类推理失败, 返回空结果");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.inferCategory 异常", e);
|
|
return ActionResult.fail("分类推理异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.16+ (2026-07-07 dos 26 P0 闭环, BS-新65): 组件属性 AI 推理
|
|
*
|
|
* dacli form create --ai 路径的 4 类盲点 (BS-新61/62/63/64) 智能选配:
|
|
* - select 字段字典选配 (BS-新61)
|
|
* - input _id/_code → relationForm (BS-新62)
|
|
* - inputNumber 范围 (BS-新63)
|
|
* - input regList 格式校验 (BS-新64)
|
|
*
|
|
* 请求: {
|
|
* fields: '[{vModel:"port",jnpfKey:"inputNumber",label:"端口"},...]',
|
|
* dicts: '[{id:"x",enCode:"s52.status",fullName:"状态"},...]',
|
|
* forms: '[{id:"y",enCode:"dataset",fullName:"数据集"},...]'
|
|
* }
|
|
* 响应: {props: [{vModel, dictionaryType, min, max, modelId, regList, reason}]}
|
|
*
|
|
* vs dacli 关键词规则 (v0.15+ 已弃用, 用户反馈"无穷无尽"):
|
|
* 一次性 LLM 推理, 给全上下文 (fields + dicts + forms), 替代人工维护 SELECT_DICT_RULES 等
|
|
*/
|
|
@PostMapping("/infer-component-props")
|
|
@Operation(summary = "组件属性 AI 推理 (BS-新65, dos 26 P0 闭环)",
|
|
description = "dacli form create --ai 路径选配组件属性: 字典/min-max/relationForm/regList")
|
|
public ActionResult<Map<String, Object>> inferComponentProps(@RequestBody Map<String, Object> body) {
|
|
String fieldsJson = (String) body.get("fields");
|
|
String dictsJson = (String) body.get("dicts");
|
|
String formsJson = (String) body.get("forms");
|
|
if (fieldsJson == null || dictsJson == null || formsJson == null) {
|
|
return ActionResult.fail("fields / dicts / forms 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.inferComponentProps(
|
|
fieldsJson, dictsJson, formsJson);
|
|
if (result.isEmpty() || !result.containsKey("props")) {
|
|
return ActionResult.fail("组件属性推理失败, 返回空结果");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.inferComponentProps 异常", e);
|
|
return ActionResult.fail("组件属性推理异常: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.22+ (2026-07-25 BS-新131~133, tdd3 P1-5 闭环): 旁路 JNPF 后端 AI 直接构造 form schema.
|
|
*
|
|
* 替代 dacli 调 jnpf /api/visualdev/ai/form (JNPF 后端 不可控: 硬编码白名单 + 强制改名 + 输出长度限制).
|
|
* 直调 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 + 处理完全可控, 无 JNPF 后端 4+3 限制
|
|
*/
|
|
@PostMapping("/build-form")
|
|
@Operation(summary = "从描述构造完整 form schema (旁路 JNPF 后端, P1-5 闭环)",
|
|
description = "直调 SSW SswLLMClient, prompt 禁用 JNPF 后端 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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* v0.22+ (2026-07-25 BS-新43 闭环): 旁路 JNPF 后端 /api/workflow/ai 直接构造 workflow schema.
|
|
*
|
|
* 替代 dacli 调 jnpf /api/workflow/ai (JNPF 后端 不可控: 5/5 trivial 3-node + URL-encoded flowXml + 静默截断 + 输出长度限制).
|
|
* 直调 ssw-common SswLLMClient (同一 base_ai LLM, prompt 我们写, 强制 ≥4 节点).
|
|
*
|
|
* 入参:
|
|
* - workflowDesc: 业务描述 (中文自然语言, e.g. "请假流程: 员工提交 → 经理审批 → HR 备案")
|
|
* - appCode: 应用 code (用于字段命名规范, e.g. "tdd3")
|
|
*
|
|
* 响应: WorkFlowAiModel 兼容结构 {enCode, fullName, flowXml, sortCode}
|
|
* - 与 jnpf /api/workflow/ai 输出格式一致, dacli 端无感切换
|
|
* - flowXml 是真实 BPMN 2.0 XML (不可 URL-encoded)
|
|
* - 内部 prompt 强制 ≥4 节点 (startEvent + ≥1 userTask + ≥1 决策/审批 + endEvent)
|
|
*/
|
|
@PostMapping("/build-workflow")
|
|
@Operation(summary = "从描述构造完整 workflow schema (旁路 JNPF 后端, BS-新43 闭环)",
|
|
description = "直调 SSW SswLLMClient, prompt 禁用 JNPF 后端 已知问题 (trivial 3-node / URL-encoded flowXml / 静默截断 / 输出长度限制). 强制 ≥4 节点 BPMN XML. 输出 WorkFlowAiModel 兼容结构, dacli 端无感切换.")
|
|
public ActionResult<Map<String, Object>> buildWorkflow(@RequestBody Map<String, Object> body) {
|
|
String workflowDesc = (String) body.get("workflowDesc");
|
|
String appCode = (String) body.get("appCode");
|
|
if (workflowDesc == null || workflowDesc.trim().isEmpty()) {
|
|
return ActionResult.fail("workflowDesc 必填");
|
|
}
|
|
try {
|
|
Map<String, Object> result = aiRepairService.buildWorkflow(workflowDesc, appCode);
|
|
if (result.isEmpty() || !result.containsKey("flowXml")) {
|
|
return ActionResult.fail("buildWorkflow 返回空 (LLM 不可用或 prompt 解析失败)");
|
|
}
|
|
return ActionResult.success(result);
|
|
} catch (Exception e) {
|
|
log.error("SswAIRepairController.buildWorkflow 异常", e);
|
|
return ActionResult.fail("buildWorkflow 异常: " + e.getMessage());
|
|
}
|
|
}
|
|
} |