refactor: 全面转向 CLI 驱动架构与底层 SDK,裁撤繁杂 MCP 工具并参考 seal-home 规范
This commit is contained in:
1 parent
3d549e2e2f
commit
e7aa8a86cc
19 files changed
+1228
-589
No files matched your search
@@ -0,0 +1,292 @@
|
||||
import {
|
||||
fullPasswordLogin,
|
||||
fullSmsLogin,
|
||||
fullBrowserSyncLogin,
|
||||
sendSmsVerificationCode,
|
||||
importF12Credentials,
|
||||
parseHeadersOrCurl,
|
||||
refreshAccountCorporations,
|
||||
} from "../../core/auth/login.js";
|
||||
import {
|
||||
listAccounts,
|
||||
getActiveAccount,
|
||||
setActiveAccount,
|
||||
} from "../../core/auth/account-store.js";
|
||||
import { createEkbClient } from "../../core/auth/ekb.js";
|
||||
import { stringOption, booleanOption } from "../parser.js";
|
||||
import { printJson, printKeyValue, printTable } from "../printer.js";
|
||||
import { prompt } from "../helpers.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleAuthCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const action = parsed.action || "status";
|
||||
|
||||
switch (action) {
|
||||
case "login":
|
||||
return handleLogin(parsed, runtime);
|
||||
case "status":
|
||||
case "whoami":
|
||||
return handleStatus(parsed, runtime);
|
||||
case "refresh":
|
||||
return handleRefresh(parsed, runtime);
|
||||
case "import":
|
||||
return handleImport(parsed, runtime);
|
||||
case "accounts":
|
||||
return handleAccounts(parsed, runtime);
|
||||
case "switch-account":
|
||||
return handleSwitchAccount(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 auth 子命令: ${action}。支持: login, status, refresh, import, accounts, switch-account\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function handleLogin(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const phone = stringOption(parsed.options, ["phone", "p"]);
|
||||
const password = stringOption(parsed.options, ["password", "pwd"]);
|
||||
const isSms = booleanOption(parsed.options, ["sms"]);
|
||||
const isSync = booleanOption(parsed.options, ["sync"]);
|
||||
const isInteractive = booleanOption(parsed.options, ["interactive", "i"]) || (!phone && !password && !isSms && !isSync);
|
||||
|
||||
// 1. 浏览器同步
|
||||
if (isSync) {
|
||||
const res = await fullBrowserSyncLogin({ saveAll: true });
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(res), stderr: "", exitCode: 0 };
|
||||
}
|
||||
return {
|
||||
stdout: `[SUCCESS] 成功从浏览器会话同步企业凭证!\n默认企业: ${res.selectedCorp.name} (${res.selectedCorp.id})\n共保存企业: ${res.allCorps.length} 家\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 短信验证码登录
|
||||
if (isSms) {
|
||||
const inputPhone = phone || (await prompt("请输入手机号: "));
|
||||
if (!inputPhone) return { stdout: "", stderr: "手机号不能为空\n", exitCode: 1 };
|
||||
|
||||
let codeId: string | undefined;
|
||||
try {
|
||||
const sendRes = await sendSmsVerificationCode({ phone: inputPhone });
|
||||
codeId = sendRes.codeId;
|
||||
console.log("短信验证码已发送!");
|
||||
} catch (e) {
|
||||
console.log("若触发了人机验证,请在浏览器中获取验证码后直接在下方输入。");
|
||||
}
|
||||
|
||||
const code = await prompt("请输入收到的 6 位短信验证码: ");
|
||||
if (!code) return { stdout: "", stderr: "验证码不能为空\n", exitCode: 1 };
|
||||
|
||||
const res = await fullSmsLogin({
|
||||
phone: inputPhone,
|
||||
code,
|
||||
codeId,
|
||||
saveAll: true,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(res), stderr: "", exitCode: 0 };
|
||||
}
|
||||
return {
|
||||
stdout: `[SUCCESS] 短信验证码登录成功!\n账号: ${res.accountId}\n默认企业: ${res.selectedCorp.name} (${res.selectedCorp.id})\n共保存企业: ${res.allCorps.length} 家\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// 3. 账号密码登录
|
||||
const inputPhone = phone || (isInteractive ? await prompt("请输入手机号: ") : "");
|
||||
const inputPassword = password || (isInteractive ? await prompt("请输入密码: ") : "");
|
||||
|
||||
if (!inputPhone || !inputPassword) {
|
||||
return { stdout: "", stderr: "手机号和密码不能为空\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
const res = await fullPasswordLogin({
|
||||
phone: inputPhone,
|
||||
password: inputPassword,
|
||||
saveAll: true,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(res), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const rows = res.allCorps.map((c, i) => [
|
||||
i + 1,
|
||||
c.id,
|
||||
c.name,
|
||||
c.credentials.staffId,
|
||||
]);
|
||||
const table = printTable(["#", "企业 ID", "企业名称", "员工工号"], rows);
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 账密登录成功!\n账号: ${res.accountId}\n默认企业: ${res.selectedCorp.name} (${res.selectedCorp.id})\n共保存企业配置: ${res.allCorps.length} 家\n\n${table}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleStatus(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const currentCorp = runtime.selectedCorp;
|
||||
const activeAccount = getActiveAccount();
|
||||
|
||||
let sessionValid = false;
|
||||
let sessionData: unknown = null;
|
||||
|
||||
if (currentCorp) {
|
||||
try {
|
||||
const client = await createEkbClient(currentCorp);
|
||||
const res = await client.get("api/account/v2/session").json<{ value?: unknown }>();
|
||||
sessionValid = !!res.value;
|
||||
sessionData = res.value;
|
||||
} catch {
|
||||
sessionValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
const statusObj = {
|
||||
activeAccount: activeAccount?.accountId || null,
|
||||
currentCorp: currentCorp
|
||||
? {
|
||||
id: currentCorp.id,
|
||||
name: currentCorp.name,
|
||||
staffId: currentCorp.credentials.staffId,
|
||||
source: currentCorp.source,
|
||||
accountId: currentCorp.accountId,
|
||||
tokenValid: sessionValid,
|
||||
}
|
||||
: null,
|
||||
session: sessionData,
|
||||
};
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(statusObj), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const summary = printKeyValue({
|
||||
"当前活跃账号": activeAccount?.accountId || "未登录",
|
||||
"当前激活企业": currentCorp ? `${currentCorp.name} (${currentCorp.id})` : "未配置",
|
||||
"当前员工工号": currentCorp?.credentials.staffId || "-",
|
||||
"凭证来源类型": currentCorp?.source || "-",
|
||||
"Token 有效性": sessionValid ? "✅ 有效 (已通过 Session 验证)" : "❌ 无效或未连接",
|
||||
});
|
||||
|
||||
return {
|
||||
stdout: `====== 当前登录与企业状态 ======\n${summary}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleRefresh(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const accountId = stringOption(parsed.options, ["account", "a"]);
|
||||
const res = await refreshAccountCorporations(accountId, { saveAll: true });
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(res), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 成功批量刷新账号 ${res.accountId} 下的所有企业 AccessToken!\n共刷新企业: ${res.allCorps.length} 家\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleImport(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
let raw = stringOption(parsed.options, ["raw", "headers", "curl"]);
|
||||
let accessToken = stringOption(parsed.options, ["token", "accessToken"]);
|
||||
let corpId = stringOption(parsed.options, ["corp", "corpId"]);
|
||||
let staffId = stringOption(parsed.options, ["staff", "staffId"]);
|
||||
let name = stringOption(parsed.options, ["name"]);
|
||||
|
||||
if (!raw && !accessToken) {
|
||||
raw = await prompt("请粘贴 F12 抓包文本 / cURL 命令行 (直接回车则手动逐项输入): ");
|
||||
}
|
||||
|
||||
if (raw) {
|
||||
const parsedObj = parseHeadersOrCurl(raw);
|
||||
if (parsedObj.accessToken) accessToken = parsedObj.accessToken;
|
||||
if (parsedObj.corpId) corpId = parsedObj.corpId;
|
||||
if (parsedObj.staffId) staffId = parsedObj.staffId;
|
||||
}
|
||||
|
||||
if (!accessToken) accessToken = await prompt("请输入 accessToken: ");
|
||||
if (!corpId) corpId = await prompt("请输入 corpId: ");
|
||||
if (!staffId) staffId = await prompt("请输入 staffId: ");
|
||||
|
||||
if (!accessToken || !corpId || !staffId) {
|
||||
return { stdout: "", stderr: "accessToken, corpId, staffId 均为必填!\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
const imported = await importF12Credentials({
|
||||
accessToken,
|
||||
corpId,
|
||||
staffId,
|
||||
name,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(imported), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 成功导入并保存企业凭证!\n企业名称: ${imported.name}\n企业 ID: ${imported.id}\n员工工号: ${imported.credentials.staffId}\n保存路径: enterprises/${imported.id}.json\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleAccounts(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const accounts = listAccounts();
|
||||
const active = getActiveAccount();
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ activeAccountId: active?.accountId, accounts }),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (accounts.length === 0) {
|
||||
return { stdout: "暂无保存的账号,请使用 hose auth login 登录\n", stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const rows = accounts.map((acc, i) => [
|
||||
acc.accountId === active?.accountId ? `* ${i + 1}` : ` ${i + 1}`,
|
||||
acc.accountId,
|
||||
acc.userId || "-",
|
||||
acc.corporations.length,
|
||||
new Date(acc.updatedAt).toLocaleString(),
|
||||
]);
|
||||
|
||||
const table = printTable(["#", "账号标识", "用户 ID", "关联企业数", "最近更新时间"], rows);
|
||||
return { stdout: `====== 本地已保存账号列表 ======\n${table}\n`, stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
async function handleSwitchAccount(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["account", "a"]);
|
||||
if (!target) {
|
||||
return { stdout: "", stderr: "用法: hose auth switch-account <accountId>\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
const success = setActiveAccount(target);
|
||||
if (!success) {
|
||||
return { stdout: "", stderr: `未找到账号: ${target}\n`, exitCode: 1 };
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 当前活跃主账号已切换至: ${target}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { printJson, printKeyValue, printTable } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleCorpsCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const action = parsed.action || "list";
|
||||
|
||||
switch (action) {
|
||||
case "list":
|
||||
return handleList(parsed, runtime);
|
||||
case "switch":
|
||||
case "use":
|
||||
return handleSwitch(parsed, runtime);
|
||||
case "current":
|
||||
return handleCurrent(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 corps 子命令: ${action}。支持: list, switch, current\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const corps = runtime.corpContext.listCorps();
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({
|
||||
activeCorpId: runtime.selectedCorp?.id,
|
||||
corporations: corps,
|
||||
}),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (corps.length === 0) {
|
||||
return { stdout: "未找到任何企业配置,请先运行 hose auth login\n", stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const rows = corps.map((c, i) => [
|
||||
c.current ? `* ${i + 1}` : ` ${i + 1}`,
|
||||
c.id,
|
||||
c.name,
|
||||
c.staffId,
|
||||
c.source || "manual",
|
||||
c.accountId || "-",
|
||||
]);
|
||||
|
||||
const table = printTable(
|
||||
["#", "企业 ID", "企业名称", "员工工号", "来源", "所属账号"],
|
||||
rows
|
||||
);
|
||||
|
||||
return {
|
||||
stdout: `====== 已配置企业列表 (${corps.length} 家) ======\n${table}\n\n提示: 使用 hose corps switch <企业ID> 切换工作企业\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleSwitch(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const corpId = parsed.args[0];
|
||||
if (!corpId) {
|
||||
return { stdout: "", stderr: "用法: hose corps switch <企业ID>\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
const success = runtime.corpContext.switchCorp(corpId);
|
||||
if (!success) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `企业 ${corpId} 不存在或未配置。可运行 hose corps list 查看可用列表。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const current = runtime.corpContext.getCurrent();
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(current), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 已切换到企业: ${current?.name} (${current?.id})\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleCurrent(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const current = runtime.selectedCorp;
|
||||
if (!current) {
|
||||
return { stdout: "", stderr: "未选中任何企业\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(current), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const output = printKeyValue({
|
||||
"企业 ID": current.id,
|
||||
"企业名称": current.name,
|
||||
"员工工号": current.credentials.staffId,
|
||||
"来源类型": current.source || "manual",
|
||||
"所属账号": current.accountId || "-",
|
||||
"主站地址": current.baseUrl || "https://app.ekuaibao.com",
|
||||
});
|
||||
|
||||
return {
|
||||
stdout: `====== 当前激活企业 ======\n${output}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { spawn } from "child_process";
|
||||
import { join } from "path";
|
||||
import type { CliParsedArgs, CliResult, CliRunOptions } from "../types.js";
|
||||
|
||||
export async function handleMcpCommand(
|
||||
parsed: CliParsedArgs,
|
||||
options: CliRunOptions
|
||||
): Promise<CliResult> {
|
||||
const appRoot = options.appRoot || process.cwd();
|
||||
const serverPath = join(appRoot, "src", "server.ts");
|
||||
|
||||
console.error("启动 MCP Server on stdio...");
|
||||
const child = spawn("bun", ["run", serverPath], {
|
||||
stdio: "inherit",
|
||||
cwd: appRoot,
|
||||
env: process.env,
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
child.on("exit", () => resolve());
|
||||
});
|
||||
|
||||
return { stdout: "", stderr: "", exitCode: 0 };
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import * as api from "../../domains/platform/api.js";
|
||||
import { createClientsForCorp } from "../helpers.js";
|
||||
import { printJson, printTable } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handlePlatformCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const currentCorp = runtime.selectedCorp;
|
||||
if (!currentCorp) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "未选中企业,请先通过 hose corps switch 或 --corp 指定企业\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const action = parsed.action || "list";
|
||||
|
||||
switch (action) {
|
||||
case "list":
|
||||
case "ls":
|
||||
return handleList(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 platform 子命令: ${action}。支持: list\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
||||
const platforms = await api.listPlatforms(clients.ekbClient, runtime.selectedCorp!.credentials.corpId);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(platforms), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
if (platforms.length === 0) {
|
||||
return {
|
||||
stdout: `企业 [${runtime.selectedCorp!.name}] 下暂无自建应用\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const rows = platforms.map((p, i) => [
|
||||
i + 1,
|
||||
p.id,
|
||||
p.name,
|
||||
p.active ? "🟢 已启用" : "⚪ 未启用",
|
||||
]);
|
||||
|
||||
const table = printTable(["#", "自建应用 ID", "应用名称", "状态"], rows);
|
||||
|
||||
return {
|
||||
stdout: `====== [${runtime.selectedCorp!.name}] 自建应用列表 (共 ${platforms.length} 个) ======\n${table}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { createClientsForCorp } from "../helpers.js";
|
||||
import * as api from "../../domains/platform/api.js";
|
||||
import { printJson, printKeyValue } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleProbeCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const currentCorp = runtime.selectedCorp;
|
||||
if (!currentCorp) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "未配置任何企业,请先运行 hose auth login 或使用 --corp 指定\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const results: Record<string, unknown> = {
|
||||
enterprise: {
|
||||
id: currentCorp.id,
|
||||
name: currentCorp.name,
|
||||
staffId: currentCorp.credentials.staffId,
|
||||
baseUrl: currentCorp.baseUrl,
|
||||
source: currentCorp.source,
|
||||
accountId: currentCorp.accountId,
|
||||
},
|
||||
ekbSession: { status: "pending" },
|
||||
easoToken: { status: "pending" },
|
||||
platformAccess: { status: "pending" },
|
||||
};
|
||||
|
||||
try {
|
||||
const clients = await createClientsForCorp(currentCorp);
|
||||
|
||||
// 1. 测试 EKB 主站 Session
|
||||
try {
|
||||
const sessionRes = await clients.ekbClient
|
||||
.get("api/account/v2/session")
|
||||
.json<{ value?: { userId?: string; loginStaffId?: string; expTime?: number } }>();
|
||||
|
||||
results.ekbSession = {
|
||||
status: "ok",
|
||||
userId: sessionRes.value?.userId,
|
||||
loginStaffId: sessionRes.value?.loginStaffId,
|
||||
expTime: sessionRes.value?.expTime ? new Date(sessionRes.value.expTime).toLocaleString() : undefined,
|
||||
};
|
||||
} catch (e) {
|
||||
results.ekbSession = {
|
||||
status: "error",
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 测试 EASO Token 换取
|
||||
try {
|
||||
const platforms = await api.listPlatforms(clients.ekbClient, currentCorp.credentials.corpId);
|
||||
results.platformAccess = {
|
||||
status: "ok",
|
||||
totalPlatforms: platforms.length,
|
||||
};
|
||||
|
||||
const stats = await api.getWorkflowStats(clients.easoClient, platforms[0]?.id || "platform");
|
||||
results.easoToken = {
|
||||
status: "ok",
|
||||
message: "EASO 四步换票握手成功",
|
||||
};
|
||||
} catch (e) {
|
||||
results.easoToken = {
|
||||
status: "error",
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
results.error = error instanceof Error ? error.message : String(error);
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(results), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const summary = printKeyValue({
|
||||
"企业名称": currentCorp.name,
|
||||
"企业 ID": currentCorp.id,
|
||||
"员工工号": currentCorp.credentials.staffId,
|
||||
"主站 Session 连通": (results.ekbSession as any).status === "ok" ? "🟢 正常" : "🔴 异常",
|
||||
"EASO 引擎鉴权换票": (results.easoToken as any).status === "ok" ? "🟢 握手成功" : "🔴 失败",
|
||||
"自建应用扫描": (results.platformAccess as any).status === "ok" ? `🟢 可访问 (${(results.platformAccess as any).totalPlatforms} 个自建应用)` : "🔴 失败",
|
||||
});
|
||||
|
||||
return {
|
||||
stdout: `====== 诊断结果 (Enterprise Probe) ======\n${summary}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
import {
|
||||
listAllWorkflows,
|
||||
findWorkflowsByTaskType,
|
||||
} from "../../domains/platform/services.js";
|
||||
import * as api from "../../domains/platform/api.js";
|
||||
import { stringOption, numberOption, booleanOption } from "../parser.js";
|
||||
import { createClientsForCorp } from "../helpers.js";
|
||||
import { printJson, printTable, printKeyValue } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
export async function handleWorkflowCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const currentCorp = runtime.selectedCorp;
|
||||
if (!currentCorp) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "未选中企业,请先通过 hose corps switch 或 --corp 指定企业\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const action = parsed.action || "list";
|
||||
|
||||
switch (action) {
|
||||
case "list":
|
||||
case "list-all":
|
||||
case "ls":
|
||||
return handleList(parsed, runtime);
|
||||
case "get":
|
||||
case "detail":
|
||||
case "info":
|
||||
return handleGet(parsed, runtime);
|
||||
case "find":
|
||||
case "search":
|
||||
return handleFind(parsed, runtime);
|
||||
case "toggle":
|
||||
return handleToggle(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, toggle\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
||||
const page = numberOption(parsed.options, ["page"]) || 1;
|
||||
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
|
||||
|
||||
const result = await listAllWorkflows(clients, { page, pageSize });
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(result), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
if (result.workflows.length === 0) {
|
||||
return {
|
||||
stdout: `企业 [${runtime.selectedCorp!.name}] 下暂无工作流\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const rows = result.workflows.map((wf, i) => [
|
||||
i + 1,
|
||||
wf.id,
|
||||
wf.pipelineName,
|
||||
wf.platformName || wf.platformId || "-",
|
||||
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
|
||||
wf.tasks?.length ?? "-",
|
||||
]);
|
||||
|
||||
const table = printTable(
|
||||
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态", "节点数"],
|
||||
rows
|
||||
);
|
||||
|
||||
return {
|
||||
stdout: `====== [${runtime.selectedCorp!.name}] 工作流列表 (共 ${result.workflows.length} 条) ======\n${table}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleGet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
||||
if (!pipelineId) {
|
||||
return { stdout: "", stderr: "用法: hose workflow get <pipelineId>\n", exitCode: 1 };
|
||||
}
|
||||
|
||||
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
||||
const detail = await api.getWorkflowDetail(clients.easoClient, pipelineId);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(detail), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
const summary = printKeyValue({
|
||||
"工作流 ID": detail.id,
|
||||
"工作流名称": detail.label,
|
||||
"状态": detail.state === 1 ? "🟢 启用" : "⚪ 停用",
|
||||
"版本号": detail.version || "-",
|
||||
"节点总数": detail.tasks?.length ?? 0,
|
||||
});
|
||||
|
||||
let taskList = "";
|
||||
if (detail.tasks && detail.tasks.length > 0) {
|
||||
const taskRows = detail.tasks.map((t, i) => [
|
||||
i + 1,
|
||||
t.name || "-",
|
||||
t.label || "-",
|
||||
t.type,
|
||||
t.groupName || "-",
|
||||
]);
|
||||
taskList = `\n 节点结构:\n` + printTable(["#", "节点标识", "节点名称", "类型", "分组"], taskRows);
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `====== 工作流详情 ======\n${summary}\n${taskList}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleFind(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const taskType = stringOption(parsed.options, ["task", "taskType", "type"]) || parsed.args[0];
|
||||
if (!taskType) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "用法: hose workflow find --task=<节点类型> (如 code-js2, http, condition)\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
||||
const includeDetail = booleanOption(parsed.options, ["includeDetail", "detail"]);
|
||||
const page = numberOption(parsed.options, ["page"]) || 1;
|
||||
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
|
||||
|
||||
const result = await findWorkflowsByTaskType(clients, taskType, {
|
||||
page,
|
||||
pageSize,
|
||||
includeDetail,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(result), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
if (result.workflows.length === 0) {
|
||||
return {
|
||||
stdout: `未找到包含节点类型 [${taskType}] 的工作流\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const rows = result.workflows.map((wf, i) => [
|
||||
i + 1,
|
||||
wf.id,
|
||||
wf.pipelineName,
|
||||
wf.platformName || wf.platformId || "-",
|
||||
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
|
||||
]);
|
||||
|
||||
const table = printTable(
|
||||
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态"],
|
||||
rows
|
||||
);
|
||||
|
||||
return {
|
||||
stdout: `====== 匹配节点类型 [${taskType}] 的工作流 (共 ${result.workflows.length} 条) ======\n${table}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function handleToggle(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
||||
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
||||
const stateArg = stringOption(parsed.options, ["state", "s"]);
|
||||
|
||||
if (!pipelineId || stateArg === undefined) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "用法: hose workflow toggle <pipelineId> --state=<1|0> (1启用, 0停用)\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const state = (stateArg === "1" || stateArg === "true" ? 1 : 0) as 0 | 1;
|
||||
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
||||
const result = await api.toggleWorkflow(clients.easoClient, pipelineId, state);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return { stdout: printJson(result), stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `[SUCCESS] 工作流 ${pipelineId} 状态已更新为: ${state === 1 ? "🟢 启用" : "⚪ 停用"}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user