diff --git a/src/commands/form/_ai.ts b/src/commands/form/_ai.ts index 6ac2d24..1eade0e 100644 --- a/src/commands/form/_ai.ts +++ b/src/commands/form/_ai.ts @@ -296,16 +296,74 @@ async function callSswAiRepair(keyword: string, reason: string): Promise { const c = await JnpfClient.fromToken() log.info(`[NCPF backend] POST /workflow/ai keyword="${keyword.slice(0, 50)}${keyword.length > 50 ? '...' : ''}"`) const r = await c.postJson('/workflow/ai', { keyword }) - if (r.code !== 200) { - throw new Error(`AI 创建流程失败 (code=${r.code}): ${r.msg}`) + if (r.code === 200) { + // 静默截断检测 (BS-新30): NCPF 返 200 但 flowXml 为空 + if (!r.data?.flowXml) { + log.warn(`! NCPF 后端静默截断 (返 200 但 flowXml 空) → fallback SSW 修复`) + return await callSswAiWorkflowRepair(keyword, 'NCPF 静默截断') + } + return r.data } - return r.data || {} + // NCPF 错误 → fallback SSW + log.warn(`! NCPF workflow AI 失败 (code=${r.code}): ${r.msg} → fallback SSW 修复`) + return await callSswAiWorkflowRepair(keyword, 'NCPF 错误') +} + +/** + * v0.15+ (BS-新30 workflow): SSW AI 修复 fallback + * + * 端点: POST /api/ssw/ai/repair-workflow + * 来源: ssw-common-controller/SswAIRepairController.repairWorkflow + * 逻辑: SSW 端点直接调 LLM (maxTokens=3000) + 修复 BPMN XML 截断/格式 + * + * 跟 callSswAiRepair (form) 平行, 复用同样的 retry 模式 (5s/10s/20s) + */ +async function callSswAiWorkflowRepair(keyword: string, reason: string): Promise { + const c = await JnpfClient.fromToken() + const maxSswRetries = 2 + for (let attempt = 1; attempt <= maxSswRetries; attempt++) { + log.info(`[SSW workflow fallback] POST /ssw/ai/repair-workflow reason="${reason}" attempt=${attempt}/${maxSswRetries}`) + try { + const r = await c.postJson('/ssw/ai/repair-workflow', { keyword }) + if (r.code === 200 && r.data && r.data.flowXml) { + log.ok(`✓ SSW workflow 修复成功: enCode=${r.data.enCode}, fullName=${r.data.fullName}, flowXml ${r.data.flowXml.length} 字符`) + return r.data + } + // SSW 429: retry + if (isTokenPlanExhausted(r)) { + log.warn(`! SSW workflow 429 token plan 用尽 (attempt=${attempt}/${maxSswRetries}): ${r.msg}`) + if (attempt < maxSswRetries) { + await backoffSleep(attempt, 5000) + continue + } + } + log.warn(`! SSW workflow 修复失败 (code=${r.code}): ${r.msg}`) + break + } catch (e) { + log.warn(`! SSW workflow 修复异常 (attempt=${attempt}/${maxSswRetries}): ${(e as Error).message?.slice(0, 100)}`) + if (attempt < maxSswRetries) { + await backoffSleep(attempt, 5000) + continue + } + } + } + // SSW 也失败, 抛错让调用方走原错误流 + throw new Error(`workflow AI 修复失败 (NCPF + SSW 都未成功), 建议拆分 --ai 描述 (≤5 节点)`) } // ============ 调试输出 (--ai-field 用) ============