import { 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, stringOption } from "../parser.js"; import { outputOptions } from "../printer.js"; import type { ParsedArgs } from "../types.js"; import * as api from "../../domains/seal/api.js"; export async function handleDeployCommand( systemOrAction: string | undefined, actionOrTarget: string | undefined, maybeTarget: string | undefined, client: Awaited>, corp: CorpConfig, args: ParsedArgs ) { if (!systemOrAction || systemOrAction === "help") { throw new Error( "Usage: seal-use deploy [options]\n\n" + "支持的审批系统与发布目标:\n" + " • hosecloud (合思费控): seal-use deploy hosecloud --corp \n" + " • webhook (通用审批流回调): seal-use webhook --corp \n" + " • feishu (飞书审批): 建议使用 Webhook 模式对接\n" + " • weaver (泛微 OA): 建议使用 Webhook 模式对接\n" + " • helios (汇联易): 建议使用 Webhook 模式对接\n" + " • maycur (每刻报销): 建议使用 Webhook 模式对接" ); } if (systemOrAction === "webhook" || systemOrAction === "webhooks") { throw new Error("Webhook 审批流发布请直接使用专有命令:seal-use webhook --corp "); } if (systemOrAction === "feishu" || systemOrAction === "lark") { throw new Error("飞书审批发布:建议使用 Webhook 通用发布模式对接。命令:seal-use webhook add --name '飞书审批' --result-url --corp "); } if (systemOrAction === "weaver") { throw new Error("泛微 OA 发布:建议使用 Webhook 通用发布模式对接。命令:seal-use webhook add --name '泛微OA' --result-url --corp "); } if (systemOrAction === "maycur" || systemOrAction === "helios") { const name = systemOrAction === "maycur" ? "每刻报销" : "汇联易"; throw new Error(`${name}发布:目前可通过 Webhook 模式接入。命令:seal-use webhook add --name '${name}' --result-url --corp `); } const isHosecloud = systemOrAction === "hosecloud"; const action = systemOrAction === "hosecloud" ? actionOrTarget : systemOrAction; const target = systemOrAction === "hosecloud" ? maybeTarget : actionOrTarget; if (!isHosecloud) { throw new Error(`未知的发布目标系统: ${systemOrAction}。当前支持: hosecloud (合思费控) 与 webhook (通用回调)。`); } if (action === "requirements") { await runTool("seal_deploy_hosecloud_requirements", client, corp, {}, outputOptions(args.options)); return; } if (action === "list") { await runTool("seal_deploy_hosecloud_list", client, corp, {}, outputOptions(args.options)); return; } if (action === "get") { const signKey = target ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); if (!signKey) { const config = await api.getApprovalStylePreferences(client); const hoseConfigs = (config.integrations ?? []).filter((i: any) => i.type === "hosecloud"); if (hoseConfigs.length === 0) { throw new Error( `当前企业 (${corp.name}) 尚未添加合思发布配置。\n` + `👉 请先运行配置命令:seal-use deploy hosecloud configure --json '{...}' --corp ${corp.id}` ); } console.error(`Usage: seal-use deploy hosecloud get [--corp ${corp.id}]\n`); console.error(`ℹ️ 当前企业发现以下可用的合思配置卡片:`); for (const item of hoseConfigs) { const name = (item as any).outboundName ?? "SealAI 审批"; const key = item.signKey; const origin = (item as any).apiOrigin ?? "production"; console.error(` • "${name}" (signKey: ${key}, 环境: ${origin})`); console.error(` 👉 查看命令: seal-use deploy hosecloud get ${key} --corp ${corp.id}\n`); } throw new Error("缺少必需参数 。请从上述候选列表中选择并指定 signKey。"); } await runTool("seal_deploy_hosecloud_get", client, corp, { signKey }, outputOptions(args.options)); return; } if (action === "status") { await runTool("seal_deploy_hosecloud_status", client, corp, {}, outputOptions(args.options)); return; } if (action === "configure") { const rawInput = stringOption(args.options.input); const payload = rawInput ? JSON.parse(readFileSync(rawInput, "utf8")) : jsonObject(args.json); if (!payload || Object.keys(payload).length === 0) { throw new Error("Usage: seal-use deploy hosecloud configure --json '{...}' or --input "); } await runTool("seal_deploy_hosecloud_configure", client, corp, payload, outputOptions(args.options)); return; } if (action === "sync") { const signKey = target ?? stringOption(args.options.signKey) ?? stringOption(args.options["sign-key"]); const autoOpt = booleanOption(args.options.auto) ?? booleanOption(args.options.yes); if (!signKey) { const config = await api.getApprovalStylePreferences(client); const hoseConfigs = (config.integrations ?? []).filter((i: any) => i.type === "hosecloud"); if (hoseConfigs.length === 0) { throw new Error( `当前企业 (${corp.name}) 尚未添加合思发布配置。\n` + `👉 请先运行配置命令:seal-use deploy hosecloud configure --json '{...}' --corp ${corp.id}` ); } const firstHose = hoseConfigs[0]; if (firstHose && hoseConfigs.length === 1 && autoOpt) { const singleKey = firstHose.signKey; console.error(`ℹ️ [auto] 检测到企业仅有 1 个合思配置,已自动选择 (signKey: ${singleKey})`); await runTool("seal_deploy_hosecloud_sync", client, corp, { signKey: singleKey }, outputOptions(args.options)); return; } console.error(`Usage: seal-use deploy hosecloud sync [--corp ${corp.id}]\n`); console.error(`ℹ️ 当前企业发现以下可用的合思配置卡片:`); for (const item of hoseConfigs) { const name = (item as any).outboundName ?? "SealAI 审批"; const key = item.signKey; const origin = (item as any).apiOrigin ?? "production"; console.error(` • "${name}" (signKey: ${key}, 环境: ${origin})`); console.error(` 👉 同步命令: seal-use deploy hosecloud sync ${key} --corp ${corp.id}\n`); } if (hoseConfigs.length === 1) { console.error(`💡 提示:若希望单配置自动同步,可附加 --auto 参数:seal-use deploy hosecloud sync --auto --corp ${corp.id}\n`); } throw new Error("缺少必需参数 。请从上述候选列表中选择并指定 signKey 执行同步。"); } await runTool("seal_deploy_hosecloud_sync", client, corp, { signKey }, outputOptions(args.options)); return; } throw new Error(`Unknown deploy hosecloud action: ${action ?? ""}. Available: requirements, list, get, status, configure, sync.`); }