From db08b9d898ef868dc7a57b4525f24d3170595823 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Tue, 9 Jun 2026 13:49:15 +0800 Subject: [PATCH] Add CLI command for Hose enterprise configs --- src/cli.ts | 69 ++++++++++++++++++++++++++++++++++++++- src/core/config/loader.ts | 6 ++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index c13f59b..b0419bc 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -5,7 +5,7 @@ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; -import { getEnterprisesDirCandidates, loadCorpConfigs } from "./core/config/loader.js"; +import { getEnterprisesDirCandidates, loadCorpConfigs, USER_ENTERPRISES_DIR } from "./core/config/loader.js"; import { createSealClient } from "./core/http/factory.js"; import { resolveLiveSealEnterpriseConfig } from "./domains/seal/source.js"; import { sealTools, type SealTool } from "./domains/seal/tools.js"; @@ -75,6 +75,11 @@ async function main() { return; } + if (area === "corps" && action === "add-hose") { + addHoseCorp(jsonObject(args.json)); + return; + } + const corp = selectCorp(String(args.options.corp ?? "")); if (area === "source" && action === "config") { @@ -325,6 +330,67 @@ function selectCorp(corpId: string): CorpConfig { return corp; } +function addHoseCorp(input: Record) { + const id = requiredString(input, "id"); + const name = requiredString(input, "name"); + const appKey = requiredString(input, "appKey"); + const appSecurity = requiredString(input, "appSecurity"); + const corpId = requiredString(input, "corpId"); + const staffId = requiredString(input, "staffId"); + const domain = optionalString(input, "domain") ?? "https://app.ekuaibao.com"; + const sealUrl = optionalString(input, "sealUrl") ?? `https://${id.toLowerCase()}.sealai.cc`; + + const config: CorpConfig = { + id, + name, + seal: { + url: sealUrl, + endpoints: { + approvalStylePreferences: "api/v1/agent/ai-approval/config" + } + }, + source: { + type: "hose", + domain, + appKey, + appSecurity, + corpId, + staffId + }, + auth: { + refreshTtl: 300 + } + }; + + mkdirSync(USER_ENTERPRISES_DIR, { recursive: true, mode: 0o700 }); + const file = join(USER_ENTERPRISES_DIR, `${safeFileName(id)}.json`); + if (existsSync(file) && input.overwrite !== true) { + throw new Error(`Enterprise config already exists: ${file}. Pass "overwrite": true to replace it.`); + } + + writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 }); + printJson({ + id, + name, + path: file + }); +} + +function requiredString(input: Record, key: string): string { + const value = optionalString(input, key); + if (!value) throw new Error(`Missing required JSON field: ${key}`); + return value; +} + +function optionalString(input: Record, key: string): string | undefined { + const value = input[key]; + return typeof value === "string" && value.trim() ? value.trim() : undefined; +} + +function safeFileName(value: string): string { + return value.replace(/[^a-zA-Z0-9._-]/g, "_"); +} + function parseArgs(argv: string[]): ParsedArgs { const command: string[] = []; const options: Record = {}; @@ -425,6 +491,7 @@ Usage: seal-home update seal-home tools list seal-home corps list [--corp ] + seal-home corps add-hose --json '{"id":"corp-id","name":"企业名称","appKey":"...","appSecurity":"...","corpId":"...","staffId":"..."}' seal-home source config [--corp ] seal-home tool [--corp ] [--json '{"key":"value"}'] seal-home approval-runs summary [--date YYYY-MM-DD] [--timezone Asia/Shanghai] diff --git a/src/core/config/loader.ts b/src/core/config/loader.ts index 85e659f..e98a4d3 100644 --- a/src/core/config/loader.ts +++ b/src/core/config/loader.ts @@ -5,6 +5,7 @@ import { CorpConfig, CorpConfigSchema } from "./types.js"; const ENTERPRISES_DIR = "enterprises"; const CONFIG_ENV = "SEAL_HOME_ENTERPRISES_DIR"; +export const USER_ENTERPRISES_DIR = join(homedir(), ".config", "seal-home", "enterprises"); export function loadCorpConfigs(): CorpConfig[] { const dir = resolveEnterprisesDir(); @@ -27,14 +28,13 @@ export function resolveEnterprisesDir(): string | undefined { if (existsSync(ENTERPRISES_DIR)) return ENTERPRISES_DIR; - const userConfigDir = join(homedir(), ".config", "seal-home", "enterprises"); - return existsSync(userConfigDir) ? userConfigDir : undefined; + return existsSync(USER_ENTERPRISES_DIR) ? USER_ENTERPRISES_DIR : undefined; } export function getEnterprisesDirCandidates(): string[] { return [ `${CONFIG_ENV}=`, ENTERPRISES_DIR, - join(homedir(), ".config", "seal-home", "enterprises") + USER_ENTERPRISES_DIR ]; }