import { z } from "zod"; import type { KyInstance } from "ky"; import { randomBytes } from "node:crypto"; import * as api from "../api.js"; import { probeHoseDomain } from "../../../core/auth/hose.js"; import { type HosecloudConfigInput } from "../types.js"; import type { ToolContext } from "./helpers.js"; import * as helpers from "./helpers.js"; const { redactIntegrationSecrets, mutationPreview } = helpers; export const deployTools = [ { name: "seal_deploy_hosecloud_requirements", description: "获取 Seal AI 审批发布到合思费控所需配置字段与说明", parameters: z.object({}), handler: async () => { return { input: "JSON via --json '{\"apiKey\":\"...\",\"apiSecret\":\"...\",\"mode\":\"assisted\"}'", questions: [ { field: "apiOrigin", prompt: "使用哪一种合思版本或 API 环境?(production/custom,未指定时自动探测)", required: false, default: "production" }, { field: "customApiUrl", prompt: "自定义环境的 API 地址是什么?(如专属私有云域名)", requiredWhen: "apiOrigin=custom" }, { field: "apiKey", prompt: "合思 API Key(appKey)是什么?", required: true, secret: true }, { field: "apiSecret", prompt: "合思 API Secret(appSecurity)是什么?", required: true, secret: true }, { field: "mode", prompt: "使用全自动、半自动还是辅助审核?(fully_automatic/semi_automatic/assisted)", required: true, default: "assisted" }, { field: "autoApprovalRate", prompt: "自动审批比例是多少?(0-100)", requiredWhen: "mode!=assisted" }, { field: "rejectResubmitMethod", prompt: "驳回重提后从哪个节点继续?(node_default/to_ebot/from_start)" }, { field: "autoApproveNodeNames", prompt: "哪些节点允许自动审批?(string[])" }, { field: "outboundName", prompt: "合思审批流节点消息名称是什么?", default: "SealAI 审批" } ] }; } }, { name: "seal_deploy_hosecloud_list", description: "列出当前企业【Seal AI 审批发布到合思费控】的集成配置列表", parameters: z.object({}), handler: async (client: KyInstance) => { const config = await api.getApprovalStylePreferences(client); const integrations = (config.integrations ?? []).filter((item: any) => item.type === "hosecloud"); return { count: integrations.length, integrations: integrations.map(redactIntegrationSecrets) }; } }, { name: "seal_deploy_hosecloud_get", description: "查询【Seal AI 审批发布到合思费控】的单项配置详情", parameters: z.object({ signKey: z.string().describe("合思集成 sign-key") }), handler: async (client: KyInstance, params: { signKey: string }) => { const config = await api.getApprovalStylePreferences(client); const integration = (config.integrations ?? []).find( (item: any) => item.type === "hosecloud" && item.signKey === params.signKey ); if (!integration) { throw new Error(`未找到合思配置:${params.signKey}`); } return redactIntegrationSecrets(integration as Record); } }, { name: "seal_deploy_hosecloud_status", description: "检查【Seal AI 审批发布到合思费控】与多平台集成状态", parameters: z.object({}), handler: async (client: KyInstance) => { const status = await api.getSimulationIntegrationStatus(client); const config = await api.getApprovalStylePreferences(client); const integrations = (config.integrations ?? []).filter((item: any) => item.type === "hosecloud"); return { status, integrations: integrations.map(redactIntegrationSecrets) }; } }, { name: "seal_deploy_hosecloud_configure", description: "自动探测平台并保存【Seal AI 审批发布到合思费控】集成配置", parameters: z.object({ apiKey: z.string().describe("合思 API Key (appKey)"), apiSecret: z.string().describe("合思 API Secret (appSecurity)"), signKey: z.string().optional().describe("集成 signKey,不提供则自动生成"), outboundName: z.string().optional().describe("出站消息名称(合思审批流节点中显示)"), specificationId: z.string().optional(), specificationName: z.string().optional(), mode: z.enum(["fully_automatic", "semi_automatic", "assisted"]).default("assisted"), autoApprovalRate: z.number().min(0).max(100).optional(), rejectResubmitMethod: z.enum(["node_default", "to_ebot", "from_start"]).optional(), autoApproveNodeNames: z.array(z.string()).optional(), apiOrigin: z.string().default("production"), customApiUrl: z.string().optional(), dryRun: z.boolean().optional().describe("只返回前后配置 Diff,不执行写入") }), handler: async (client: KyInstance, rawParams: HosecloudConfigInput, context: ToolContext) => { const { dryRun, ...inputParams } = rawParams; const apiKey = inputParams.apiKey ?? (inputParams as any).appKey; const apiSecret = inputParams.apiSecret ?? (inputParams as any).appSecurity; if (!apiKey || !apiSecret) { throw new Error("缺少合思凭证:apiKey (或 appKey) 与 apiSecret (或 appSecurity) 为必填项"); } const params = { ...inputParams, apiKey, apiSecret }; let effectiveApiOrigin = params.apiOrigin ?? "production"; let effectiveCustomApiUrl = params.customApiUrl; // 自动平台/域名探测:当未提供 customApiUrl 时,并发探测合思 6 大标准公共 SaaS 平台域名 if (!effectiveCustomApiUrl && effectiveApiOrigin !== "custom") { try { const probe = await probeHoseDomain(params.apiKey, params.apiSecret); if (probe?.domain) { if (probe.domain === "https://app.ekuaibao.com") { effectiveApiOrigin = "production"; effectiveCustomApiUrl = undefined; } else { effectiveApiOrigin = "custom"; effectiveCustomApiUrl = probe.domain; } } } catch { // 探测失败时平滑降级,由后续后端校验处理 } } if (effectiveApiOrigin === "custom" && !effectiveCustomApiUrl) { throw new Error("自定义合思环境必须提供 customApiUrl"); } const corpResult = await api.getHosecloudCorporation(client, { apiKey: params.apiKey, apiSecret: params.apiSecret, apiOrigin: effectiveApiOrigin, customApiUrl: effectiveCustomApiUrl }); const config = await api.getApprovalStylePreferences(client); const signKey = params.signKey ?? randomBytes(6).toString("hex"); const nextIntegration = { type: "hosecloud", ...params, apiOrigin: effectiveApiOrigin, ...(effectiveCustomApiUrl ? { customApiUrl: effectiveCustomApiUrl } : {}), signKey, corporationId: corpResult.corporationId }; const existing = (config.integrations ?? []).filter( (item: any) => item.type !== "hosecloud" || item.signKey !== signKey ); existing.push(nextIntegration); const nextConfig = { ...config, integrationSystem: "hosecloud", integrations: existing }; if (dryRun) { return mutationPreview( context, context.corp.seal.endpoints.approvalStylePreferences, config, nextConfig, 1 ); } await api.updateApprovalStylePreferences(client, nextConfig, context.corp.seal.endpoints.approvalStylePreferences); console.error(`✓ 配置已保存 (signKey: ${signKey})`); console.error(`👉 Next action: seal-use deploy hosecloud sync ${signKey} --corp ${context.corp.id}`); return { integration: redactIntegrationSecrets(nextIntegration) }; } }, { name: "seal_deploy_hosecloud_sync", description: "一键将 Seal AI 审核配置同步发布到合思费控审批流", parameters: z.object({ signKey: z.string().describe("合思集成 sign-key") }), handler: async (client: KyInstance, params: { signKey: string }, context: ToolContext) => { const config = await api.getApprovalStylePreferences(client); const integration = (config.integrations ?? []).find( (item: any) => item.type === "hosecloud" && item.signKey === params.signKey ); if (!integration) { throw new Error(`未找到合思配置:${params.signKey}`); } const sealUrl = (context.corp.seal.url ?? `https://${context.corp.id.toLowerCase()}.sealai.cc`).replace(/\/+$/, ""); const messageName = (integration as any).outboundName ?? "SealAI 审批"; const result = await api.syncHosecloudOutbound(client, { signKey: params.signKey, webhookUrl: `${sealUrl}/api/hosecloud/outbound?config_id=${encodeURIComponent(params.signKey)}`, completionWebhookUrl: `${sealUrl}/api/hosecloud/flow/completion?config_id=${encodeURIComponent(params.signKey)}`, messageName, mode: (integration as any).mode ?? "assisted" }); console.error(`✓ 同步成功!出站消息已在合思开放平台创建/更新: "${messageName}"`); console.error(`👉 发布完成:请登录合思后台在审批流中添加【EBot】节点并选择 "${messageName}"。`); return { signKey: params.signKey, synced: true, result }; } } ];