#!/usr/bin/env bun import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { createSealClient } from "../core/http/factory.js"; import type { CorpConfig } from "../core/config/types.js"; import { VERSION, VERSION_INFO } from "../version.js"; import { handleApprovalRunsCommand } from "./commands/approval-runs.js"; import { handleAnalyticsCommand } from "./commands/analytics.js"; import { handleAuthCommand } from "./commands/auth.js"; import { handleBillingCommand } from "./commands/billing.js"; import { handleConfigCommand } from "./commands/config.js"; import { handleContactsCommand } from "./commands/contacts.js"; import { handleDataCommand } from "./commands/data.js"; import { handleContextCommand } from "./commands/context.js"; import { handleCorpsCommand } from "./commands/corps.js"; import { handleDelegationCommand, handleDelegationFromHoseLink } from "./commands/delegation.js"; import { handleDeployCommand } from "./commands/deploy.js"; import { handleDoctorCommand } from "./commands/doctor.js"; import { handleHttpCommand } from "./commands/http.js"; import { handleLlmCommand } from "./commands/llm.js"; import { handleMcpCommand, printMcpConfig } from "./commands/mcp.js"; import { handleOpenApiCommand } from "./commands/openapi.js"; import { handlePermissionsCommand } from "./commands/permissions.js"; import { handlePreferenceCommand } from "./commands/preference.js"; import { handleProbeCommand } from "./commands/probe.js"; import { handleRulesCommand } from "./commands/rules.js"; import { handleServiceCommand } from "./commands/service.js"; import { handleSimulationCommand } from "./commands/simulation.js"; import { handleSkillCommand } from "./commands/skill.js"; import { handleSourceCommand } from "./commands/source.js"; import { handleToolCommand } from "./commands/tool.js"; import { handleToolsListCommand } from "./commands/tools.js"; import { handleUpdateCommand } from "./commands/update.js"; import { handleUsersCommand } from "./commands/users.js"; import { handleWebhookCommand, handleWebhookMaterialPackPreview } from "./commands/webhook.js"; import { classifyCliError } from "./errors.js"; import { selectCorp } from "./helpers.js"; import { booleanOption, materialPackFilePolicyOptions, numberOption, parseArgs, stringOption } from "./parser.js"; import { extractHelpRequest } from "./help.js"; import { formatConsoleOutput, joinOutput, printHelp, printJson, redactSensitiveText } from "./printer.js"; import type { CliResult, CliRunOptions, CliRuntime } from "./types.js"; const DEFAULT_APP_ROOT = dirname(dirname(dirname(fileURLToPath(import.meta.url)))); export const KNOWN_AREAS = new Set([ "config", "service", "stop", "http", "serve", "update", "tools", "corps", "corp", "tool", "source", "auth", "delegation", "users", "contacts", "data", "llm", "permissions", "billing", "webhook", "webhooks", "context", "rules", "approval-runs", "analytics", "simulation", "deploy", "preference", "preferences", "mcp", "server", "mcp-config", "skill", "openapi", "probe", "doctor", "changelog" ]); export type { CliResult, CliRunOptions } from "./types.js"; const STANDING_COMMANDS = new Set(["http", "server", "web", "mcp"]); function isStandingCommand(argv: string[]): boolean { if (!argv.length) return false; const first = argv[0]?.toLowerCase(); const second = argv[1]?.toLowerCase(); if (first && STANDING_COMMANDS.has(first)) return true; if ((first === "service" || first === "daemon") && second === "run") return true; return false; } let runQueue = Promise.resolve(); export function runCli(argv: string[], options: CliRunOptions = {}): Promise { if (isStandingCommand(argv)) { return runCliOnce(argv, options); } const run = runQueue.then(() => runCliOnce(argv, options)); runQueue = run.then( () => undefined, () => undefined ); return run; } async function runCliOnce(argv: string[], options: CliRunOptions): Promise { const stdout: string[] = []; const stderr: string[] = []; const originalLog = console.log; const originalError = console.error; const runtime: CliRuntime = { appRoot: options.appRoot ?? DEFAULT_APP_ROOT, entryFile: options.entryFile ?? join(options.appRoot ?? DEFAULT_APP_ROOT, "src", "cli.ts"), launcher: options.launcher }; console.log = (...values: unknown[]) => { stdout.push(formatConsoleOutput(values)); }; console.error = (...values: unknown[]) => { stderr.push(formatConsoleOutput(values)); }; try { await dispatch(argv, runtime); return { stdout: joinOutput(stdout), stderr: joinOutput(stderr), isError: false, exitCode: 0 }; } catch (error) { const message = redactSensitiveText(error instanceof Error ? error.message : String(error)); const classification = classifyCliError(error); stderr.push(message); if (classification.nextAction && !message.includes("Next action:")) { stderr.push(`👉 Next action: ${classification.nextAction}`); } return { stdout: joinOutput(stdout), stderr: joinOutput(stderr), isError: true, exitCode: classification.exitCode }; } finally { console.log = originalLog; console.error = originalError; } } async function dispatch(argv: string[], runtime: CliRuntime) { const args = parseArgs(argv); const [area, action, maybeName] = args.command; if ( area === "version" || area === "v" || Boolean(args.options.version) || Boolean(args.options.v) || Boolean(args.options.V) ) { printJson(VERSION_INFO); return; } const helpRequest = extractHelpRequest(args); if (helpRequest.isHelp) { printHelp(helpRequest.target); return; } if (!area || !KNOWN_AREAS.has(area)) { throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-use help' for available commands.`); } if (area === "webhook" && action === "material-pack" && maybeName === "preview") { const filePath = args.command[3] ?? stringOption(args.options.file) ?? stringOption(args.options.input); if (!filePath) throw new Error("Usage: seal-use webhook material-pack preview <材料包.zip>"); await handleWebhookMaterialPackPreview(filePath, materialPackFilePolicyOptions(args.options)); return; } if (area === "openapi") { handleOpenApiCommand(args); return; } if (area === "mcp-config") { printMcpConfig(); return; } if (area === "skill") { handleSkillCommand(action, maybeName, args); return; } if (area === "mcp" || area === "server") { await handleMcpCommand(action, args); return; } if (area === "probe") { await handleProbeCommand(action, maybeName, args); return; } if (area === "doctor") { await handleDoctorCommand(action, maybeName, args); return; } if (area === "config") { handleConfigCommand(action, maybeName, args); return; } if (area === "service") { await handleServiceCommand(action, runtime, args); return; } if (area === "stop") { await handleServiceCommand("stop", runtime, args); return; } if (area === "http" || area === "serve") { await handleHttpCommand(args); return; } if (area === "update" || area === "changelog") { const isChangelog = area === "changelog" || action === "changelog" || action === "log" || action === "history" || booleanOption(args.options.changelog) === true; const isCheck = action === "check" || booleanOption(args.options.check) === true || booleanOption(args.options["check-only"]) === true; const targetVersion = stringOption(args.options.version) ?? stringOption(args.options.tag) ?? (!isChangelog && !isCheck && action && !action.startsWith("-") ? action : undefined); await handleUpdateCommand(runtime, { checkOnly: isCheck, changelogOnly: isChangelog, limit: numberOption(args.options.limit), force: booleanOption(args.options.force), targetVersion, repo: stringOption(args.options.repo) }); return; } if (area === "tools" && action === "list") { handleToolsListCommand(); return; } if (area === "corps" || area === "corp") { await handleCorpsCommand(action, maybeName, args); return; } if (area === "delegation" && action === "create" && ("from-link" in args.options || "link" in args.options)) { const link = stringOption(args.options["from-link"]) ?? stringOption(args.options.link); if (!link) throw new Error("Usage: seal-use delegation create --from-link [--note ]"); await handleDelegationFromHoseLink(link, stringOption(args.options.note)); return; } if (area === "rules" && action === "lint" && ("file" in args.options || "f" in args.options)) { let corp: CorpConfig; try { corp = selectCorp(stringOption(args.options.corp)); } catch { corp = { id: "local", name: "Local Workspace", source: { type: "native" } } as any; } await handleRulesCommand(action, maybeName, null as any, corp, args); return; } const corp = selectCorp(stringOption(args.options.corp)); if (area === "source") { await handleSourceCommand(action, corp); return; } if (area === "auth") { await handleAuthCommand(action, corp, args); return; } const client = await createSealClient(corp); if (area === "delegation") { await handleDelegationCommand(action, maybeName, client, corp, args); return; } if (area === "users") { await handleUsersCommand(action, maybeName, client, corp, args); return; } if (area === "contacts") { await handleContactsCommand(action, maybeName, client, corp, args); return; } if (area === "data") { await handleDataCommand(action, client, corp, args); return; } if (area === "llm") { await handleLlmCommand(action, client, corp, args); return; } if (area === "permissions") { await handlePermissionsCommand(action, client, corp, args); return; } if (area === "billing") { await handleBillingCommand(action, client, corp, args); return; } if (area === "webhook" || area === "webhooks") { await handleWebhookCommand(action, maybeName, client, corp, args); return; } if (area === "context") { await handleContextCommand(client, corp, args); return; } if (area === "rules") { await handleRulesCommand(action, maybeName, client, corp, args); return; } if (area === "approval-runs") { await handleApprovalRunsCommand(action, maybeName, client, corp, args); return; } if (area === "analytics") { await handleAnalyticsCommand(action, client, corp, args); return; } if (area === "simulation") { await handleSimulationCommand(action, maybeName, client, corp, args); return; } if (area === "deploy") { await handleDeployCommand(action, maybeName, args.command[3], client, corp, args); return; } if (area === "preference" || area === "preferences") { await handlePreferenceCommand(action, maybeName, args.command[3], args.command[4], client, corp, args); return; } if (area === "tool") { await handleToolCommand(action, client, corp, args); return; } throw new Error(`Unknown command: ${args.command.join(" ")}. Run 'seal-use help' for available commands.`); }