import { existsSync, readFileSync } from "node:fs"; import type { CorpConfig } from "../../core/config/types.js"; import type { createSealClient } from "../../core/http/factory.js"; import { runTool } from "../helpers.js"; import { booleanOption, jsonObject, numberOption, splitOption, stringOption } from "../parser.js"; import { outputOptions } from "../printer.js"; import type { ParsedArgs } from "../types.js"; export async function handleSimulationCommand( action: string | undefined, maybeName: string | undefined, client: Awaited>, corp: CorpConfig, args: ParsedArgs ) { if (action === "batch-records" || action === "records") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); if (!batchId) throw new Error("Usage: seal-use simulation batch-records "); await runTool( "seal_simulation_batch_records_get", client, corp, { batchId, query: stringOption(args.options.query) }, outputOptions(args.options) ); return; } if (action === "create" || action === "run") { const modeArg = stringOption(args.options.mode) ?? stringOption(args.options.sourceSystem) ?? stringOption(args.options["source-system"]) ?? "hosecloud"; const mode = modeArg === "material-pack" ? "material_pack" : modeArg; const schemaConfigPath = stringOption(args.options.schemaConfig) ?? stringOption(args.options["schema-config"]); const schemaConfig = schemaConfigPath ? JSON.parse(readFileSync(schemaConfigPath, "utf8")) : undefined; const rawInput = stringOption(args.options.input); const inputPayload = rawInput ? JSON.parse(readFileSync(rawInput, "utf8")) : jsonObject(args.json); const packageId = stringOption(args.options.packageId) ?? stringOption(args.options["package-id"]); const packageIds = splitOption(args.options.packageIds) ?? splitOption(args.options["package-ids"]) ?? (packageId ? [packageId] : undefined); const flowIds = splitOption(args.options.flowIds) ?? splitOption(args.options["flow-ids"]) ?? splitOption(args.options.documentSn) ?? splitOption(args.options["document-sn"]); const signKey = stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]) ?? stringOption(args.options.integration); const noWait = booleanOption(args.options.noWait) ?? booleanOption(args.options["no-wait"]); await runTool( "seal_simulation_batch_create", client, corp, { mode, sourceSystem: mode, flowIds, documentSn: flowIds, rejectNodeName: stringOption(args.options.rejectNode) ?? stringOption(args.options["reject-node"]) ?? null, thinkingDepthOverride: stringOption(args.options.thinkingDepth) ?? stringOption(args.options["thinking-depth"]), skipInvoiceGeneration: booleanOption(args.options.skipInvoiceGeneration) ?? booleanOption(args.options["skip-invoice-generation"]), skipExpenseExtraction: booleanOption(args.options.skipExpenseExtraction) ?? booleanOption(args.options["skip-expense-extraction"]), disableAttachmentCache: booleanOption(args.options.disableAttachmentCache) ?? booleanOption(args.options["disable-attachment-cache"]), signKey, packageIds, packageId, templateId: stringOption(args.options.templateId) ?? stringOption(args.options["template-id"]), schemaConfig, requestIds: splitOption(args.options.requestIds) ?? splitOption(args.options["request-ids"]), workflowTypeId: stringOption(args.options.workflowTypeId) ?? stringOption(args.options["workflow-type-id"]), flowStatusList: splitOption(args.options.flowStatusList)?.map(Number) ?? splitOption(args.options["flow-status-list"])?.map(Number), businessCodes: splitOption(args.options.businessCodes) ?? splitOption(args.options["business-codes"]), documentType: stringOption(args.options.documentType) ?? stringOption(args.options["document-type"]), startTime: numberOption(args.options.startTime) ?? numberOption(args.options["start-time"]), endTime: numberOption(args.options.endTime) ?? numberOption(args.options["end-time"]), status: splitOption(args.options.status), limit: numberOption(args.options.limit), noWait, rawParams: inputPayload }, outputOptions(args.options) ); return; } if (action === "wait") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); if (!batchId) throw new Error("Usage: seal-use simulation wait [--timeout 10m] [--poll-interval 2s]"); await runTool( "seal_simulation_batch_wait", client, corp, { batchId, timeout: stringOption(args.options.timeout), pollInterval: stringOption(args.options.pollInterval) ?? stringOption(args.options["poll-interval"]) }, outputOptions(args.options) ); return; } if (action === "status") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); if (!batchId) throw new Error("Usage: seal-use simulation status "); await runTool( "seal_simulation_batch_status", client, corp, { batchId }, outputOptions(args.options) ); return; } if (action === "clean" || action === "delete") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); if (!batchId) throw new Error("Usage: seal-use simulation clean "); await runTool("seal_simulation_batch_clean", client, corp, { batchId }, outputOptions(args.options)); return; } if (action === "specifications") { const signKey = maybeName ?? stringOption(args.options.integration) ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); await runTool("seal_simulation_specifications_list", client, corp, { signKey }, outputOptions(args.options)); return; } if (action === "approval-definitions" || action === "definitions") { await runTool( "seal_simulation_feishu_approval_definitions_list", client, corp, {}, outputOptions(args.options) ); return; } if (action === "list") { await runTool( "seal_simulation_batches_list", client, corp, { limit: numberOption(args.options.limit) }, outputOptions(args.options) ); return; } if (action === "get") { const batchId = maybeName ?? stringOption(args.options.batchId) ?? stringOption(args.options["batch-id"]); if (!batchId) throw new Error("Usage: seal-use simulation get "); await runTool("seal_simulation_batch_get", client, corp, { batchId }, outputOptions(args.options)); return; } if (action === "templates") { await runTool("seal_simulation_templates_get", client, corp, {}, outputOptions(args.options)); return; } if (action === "integrations") { await runTool("seal_simulation_integrations_get", client, corp, {}, outputOptions(args.options)); return; } if (action === "package-upload") { const filePath = maybeName ?? stringOption(args.options.file); if (!filePath) throw new Error("Usage: seal-use simulation package-upload <材料包.zip>"); if (!existsSync(filePath)) throw new Error(`File not found: ${filePath}`); await runTool("seal_simulation_material_package_upload", client, corp, { filePath }, outputOptions(args.options)); return; } throw new Error(`Unknown simulation command: ${action ?? ""}. Run 'seal-use help' for available commands.`); }