import { getWorkflowDetail } from "../../../domains/platform/services.js"; import { analyzeWorkflow, extractWorkflowSpecificationIds, type WorkflowFormSpecLike, } from "../../../domains/platform/analysis.js"; import { getFormSpecification } from "../../../domains/bill/tools.js"; import { stringOption } from "../../parser.js"; import { printJson, printTable, printKeyValue } from "../../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; import { createWorkflowDetailClient } from "./shared.js"; export async function handleAnalyze(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]); if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow analyze \n", exitCode: 1 }; const { client, clients } = await createWorkflowDetailClient(parsed, runtime, pipelineId); const detail = await getWorkflowDetail(client, pipelineId); const specIds = extractWorkflowSpecificationIds(detail); const specifications: Record = {}; if (specIds.length > 0) { const corpId = runtime.selectedCorp!.corpId || runtime.selectedCorp!.id; await Promise.all( specIds.map(async (specId) => { try { const spec = await getFormSpecification(clients.ekbClient, corpId, specId); if (spec) specifications[specId] = spec; } catch { // Ignore specification fetch failure in offline or restricted environments } }) ); } const result = analyzeWorkflow(detail, { specifications }); if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 }; const diagnosticRows = result.diagnostics.map((item) => [item.severity, item.code, item.path, item.message]); const dependencyRows = result.dependencies.map((item) => [item.path, item.taskType, item.references.join(", ")]); return { stdout: [ `====== 工作流静态分析 (${detail.label || detail.id}) ======`, printKeyValue({ "工作流 ID": detail.id, "状态": detail.state === 1 ? "🟢 启用 (state=1)" : "⚪ 停用/草稿 (state=0)", "根节点数": result.ast.length, "依赖节点数": result.dependencies.length, "诊断数": result.diagnostics.length, }), `\n依赖引用:\n${dependencyRows.length ? printTable(["节点路径", "类型", "引用"], dependencyRows) : "暂无代码引用"}`, `\n诊断:\n${diagnosticRows.length ? printTable(["级别", "代码", "节点路径", "说明"], diagnosticRows) : "未发现明显配置问题"}`, "", ].join("\n"), stderr: "", exitCode: 0 }; }