import { canRefreshCorpAccessToken } from "../../../core/auth/login.js"; import { getCorpEnv, isCorpTest } from "../../../core/config/types.js"; import { booleanOption, stringOption } from "../../parser.js"; import { printJson, printTable, formatExpiresAt, formatSourceLabel, formatCorpEnvLabel } from "../../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; const FIELD_LABEL_MAP: Record = { id: "配置ID", name: "企业名称", corpId: "企业ID", staffId: "员工工号", staffName: "员工姓名", accountId: "所属账号", env: "环境属性", }; export async function handleSearch(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const keyword = parsed.args[0] || stringOption(parsed.options, ["keyword", "query", "k", "q", "name", "id"]); if (!keyword || !keyword.trim()) { return { stdout: "", stderr: "用法: hose-use corps search <关键词>\n", exitCode: 1, }; } const kw = keyword.trim().toLowerCase(); // 从 CorpContext 中获取企业列表 const rawCorps = typeof runtime.corpContext.getAllCorps === "function" ? runtime.corpContext.getAllCorps() : null; const normalizedCorps = rawCorps ? rawCorps.map((c) => { const hasOpenApi = Boolean( (c.credentials?.appKey || c.auth?.appKey) && (c.credentials?.appSecurity || c.auth?.appSecurity) ); const env = getCorpEnv(c); const test = isCorpTest(c); return { id: c.id, name: c.name, corpId: c.corpId || c.credentials?.corpId || c.id, staffId: c.staffId || c.credentials?.staffId || "", staffName: c.staffName || c.session?.staffName || "", accountId: c.accountId || "", source: c.source || "manual", baseUrl: c.baseUrl, current: c.id === runtime.selectedCorp?.id, expiresAt: c.auth?.expiresAt || (c.session?.expTime ?? undefined), persistent: canRefreshCorpAccessToken(c), hasOpenApi, env, isTest: test, }; }) : runtime.corpContext.listCorps().map((c) => ({ id: c.id, name: c.name, corpId: c.corpId || c.id, staffId: c.staffId || "", staffName: c.staffName || "", accountId: c.accountId || "", source: c.source || "manual", baseUrl: c.baseUrl, current: c.current || c.id === runtime.selectedCorp?.id, expiresAt: c.expiresAt, persistent: c.persistent, hasOpenApi: c.hasOpenApi, env: c.env || (c.isTest ? "test" : "prod"), isTest: c.isTest ?? (c.env !== "prod"), })); const matches: Array = []; for (const corp of normalizedCorps) { const matchedFields: string[] = []; if (corp.id && corp.id.toLowerCase().includes(kw)) { matchedFields.push("id"); } if (corp.name && corp.name.toLowerCase().includes(kw)) { matchedFields.push("name"); } if (corp.corpId && corp.corpId.toLowerCase().includes(kw) && !matchedFields.includes("corpId") && corp.corpId !== corp.id) { matchedFields.push("corpId"); } if (corp.staffId && corp.staffId.toLowerCase().includes(kw)) { matchedFields.push("staffId"); } if (corp.staffName && corp.staffName.toLowerCase().includes(kw)) { matchedFields.push("staffName"); } if (corp.accountId && corp.accountId.toLowerCase().includes(kw)) { matchedFields.push("accountId"); } if (corp.env && (corp.env.toLowerCase() === kw || (kw === "正式" && corp.env === "prod") || (kw === "测试" && corp.env === "test") || (kw === "演示" && corp.env === "demo"))) { matchedFields.push("env"); } if (matchedFields.length > 0) { matches.push({ ...corp, matchedFields }); } } if (runtime.jsonMode) { return { stdout: printJson( { keyword: keyword.trim(), count: matches.length, activeCorpId: runtime.selectedCorp?.id, corporations: matches.map((m) => ({ id: m.id, name: m.name, corpId: m.corpId, env: m.env, isTest: m.isTest, staffId: m.staffId, staffName: m.staffName || undefined, accountId: m.accountId || undefined, source: m.source, baseUrl: m.baseUrl, current: m.current, expiresAt: m.expiresAt, persistent: m.persistent, matchedFields: m.matchedFields, })), }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } if (matches.length === 0) { return { stdout: `未找到匹配 "${keyword.trim()}" 的企业配置\n`, stderr: "", exitCode: 0, }; } const rows = matches.map((m, i) => { const isCurrent = m.current; const matchLabels = m.matchedFields.map((f) => FIELD_LABEL_MAP[f] || f); return [ isCurrent ? `* ${i + 1}` : ` ${i + 1}`, formatCorpEnvLabel(m.env), m.matchedFields.includes("id") || m.matchedFields.includes("corpId") ? `[${m.id}]` : m.id, m.matchedFields.includes("name") ? `[${m.name}]` : m.name, m.matchedFields.includes("staffId") ? `[${m.staffId}]` : m.staffId, m.staffName ? (m.matchedFields.includes("staffName") ? `[${m.staffName}]` : m.staffName) : "-", m.accountId ? (m.matchedFields.includes("accountId") ? `[${m.accountId}]` : m.accountId) : "-", matchLabels.map((l) => `命中${l}`).join(", "), formatExpiresAt(m.expiresAt, m.persistent, m.hasOpenApi ? "openapi" : m.source), ]; }); const table = printTable( ["#", "环境", "企业 ID", "企业名称", "员工工号", "员工姓名", "所属账号", "匹配项", "Token 过期时间"], rows ); return { stdout: `====== 企业搜索结果 (关键词: "${keyword.trim()}", 找到 ${matches.length} 家) ======\n${table}\n\n提示:\n - 标记 * 为当前激活企业\n - 标记 [...] 为命中关键词的字段\n - 使用 hose-use corps switch <企业ID> 切换工作企业\n`, stderr: "", exitCode: 0, }; } export async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const allCorps = runtime.corpContext.listCorps(); const filterTest = booleanOption(parsed.options, ["test", "t"]); const filterProd = booleanOption(parsed.options, ["prod", "production", "p"]); const filterDemo = booleanOption(parsed.options, ["demo"]); const filterEnv = stringOption(parsed.options, ["env", "e"])?.toLowerCase(); let filteredCorps = allCorps; if (filterTest) { filteredCorps = filteredCorps.filter((c) => c.isTest || c.env === "test" || c.env === "demo"); } else if (filterProd) { filteredCorps = filteredCorps.filter((c) => c.env === "prod"); } else if (filterDemo) { filteredCorps = filteredCorps.filter((c) => c.env === "demo"); } else if (filterEnv) { filteredCorps = filteredCorps.filter((c) => c.env === filterEnv); } const prodCount = allCorps.filter((c) => c.env === "prod").length; const testCount = allCorps.filter((c) => c.env === "test").length; const demoCount = allCorps.filter((c) => c.env === "demo").length; if (runtime.jsonMode) { return { stdout: printJson( { activeCorpId: runtime.selectedCorp?.id, total: allCorps.length, count: filteredCorps.length, summary: { prodCount, testCount, demoCount, }, corporations: filteredCorps, }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } if (allCorps.length === 0) { return { stdout: "未找到任何企业配置,请先运行 hose-use auth login\n", stderr: "", exitCode: 0 }; } if (filteredCorps.length === 0) { return { stdout: `没有找到符合筛选条件的企业 (总计 ${allCorps.length} 家: 🏭 正式 ${prodCount} 家, 🧪 测试 ${testCount} 家, 🎪 演示 ${demoCount} 家)\n`, stderr: "", exitCode: 0, }; } const rows = filteredCorps.map((c, i) => [ c.current ? `* ${i + 1}` : ` ${i + 1}`, formatCorpEnvLabel(c.env), c.id, c.name, c.staffId, formatSourceLabel(c.source, c.hasOpenApi), formatExpiresAt(c.expiresAt, c.persistent, c.hasOpenApi ? "openapi" : c.source), c.accountId || "-", ]); const table = printTable( ["#", "环境", "企业 ID", "企业名称", "员工工号", "来源", "Token 过期时间", "所属账号"], rows ); const filterHint = (filteredCorps.length !== allCorps.length) ? `已筛选显示 ${filteredCorps.length} 家 (总计 ${allCorps.length} 家: 🏭 正式 ${prodCount} 家, 🧪 测试 ${testCount} 家, 🎪 演示 ${demoCount} 家)` : `共 ${allCorps.length} 家企业 (🏭 正式 ${prodCount} 家, 🧪 测试 ${testCount} 家, 🎪 演示 ${demoCount} 家)`; return { stdout: `====== 已配置企业列表 (${filterHint}) ======\n${table}\n\n提示:\n - 标记 * 为当前激活企业(同时作为 hose-use 与 hose-aipro 的统一默认执行上下文)\n - 支持筛选: --test (仅测试/演示), --prod (仅正式生产), --env=\n - 使用 hose-use corps switch <企业ID> 切换工作企业\n`, stderr: "", exitCode: 0, }; }