Files
hose-use/src/cli/commands/service.ts
T
gitea 42bf0bfb0b refactor(core/http): 7大通信底座客户端语义化全量重构与合思开放文档客户端纳管
- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名:
  * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端)
  * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端)
  * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端)
  * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端)
- 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信
- 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转
- 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验
- 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
2026-11-16 17:15:29 +08:00

170 lines
6.3 KiB
TypeScript

import { isDryRun, mutationPreview } from "../mutation.js";
import {
runServiceCommand,
getHoseUseServiceConfig,
getAiProServiceConfig,
getPidFile,
getLogFile,
SERVICE_STATE_DIR,
type ServiceConfig,
type ServiceTargetName,
} from "../service.js";
import { printJson, printKeyValue } from "../printer.js";
import { numberOption, stringOption } from "../parser.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { createSessionClient } from "../../core/http/hose-web-session-client.js";
import { EventStore } from "../../domains/events/store.js";
import { pollEvents } from "../../domains/events/poller.js";
export { SERVICE_STATE_DIR };
export async function handleServiceCommand(
parsed: CliParsedArgs,
runtime: CliRuntime,
forcedTarget?: ServiceTargetName
): Promise<CliResult> {
const firstPos = (parsed.action || parsed.args[0] || "").toLowerCase();
const secondPos = (parsed.args[0] || parsed.args[1] || "").toLowerCase();
let target: ServiceTargetName = forcedTarget || "hose-use";
let action = parsed.action || parsed.args[0] || "status";
// 支持 hose-use service aipro <start|stop|status>
if (!forcedTarget) {
if (firstPos === "aipro" || firstPos === "hose-aipro") {
target = "hose-aipro";
action = secondPos || "status";
} else if (firstPos === "hose-use" || firstPos === "hose") {
target = "hose-use";
action = secondPos || "status";
}
}
const port = numberOption(parsed.options, ["port", "p"]);
const host = stringOption(parsed.options, ["host", "h"]);
if (action === "poll") {
const corp = runtime.selectedCorp;
if (!corp) throw new Error("事件轮询需要企业上下文,请通过 --corp=<id> 指定企业。");
const corpId = corp.corpId || corp.id;
const client = await createSessionClient(corp);
const limit = numberOption(parsed.options, ["limit", "count", "n"], 50) || 50;
const stateFile = stringOption(parsed.options, ["state-file", "stateFile"]);
const store = new EventStore(stateFile || `corp:${corpId}`);
const result = await pollEvents(client, corpId, store, limit);
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
return {
stdout: `====== 事件轮询 (企业: ${corp.name}) ======\n${printKeyValue({
"待办扫描": result.scanned.todo,
"未读消息扫描": result.scanned.messages,
"新增事件": result.newEvents.length,
"重复事件": result.duplicateCount,
"存储后端": stateFile || "SQLite (observed_events)",
})}\n`,
stderr: "",
exitCode: 0,
};
}
const config: ServiceConfig =
target === "hose-aipro"
? getAiProServiceConfig(runtime, { port, host })
: getHoseUseServiceConfig(runtime, { port, host });
if (["start", "stop", "restart", "run"].includes(action) && isDryRun(parsed, runtime)) {
return mutationPreview(
parsed,
runtime,
`管理 ${config.serviceName} 服务`,
action,
{ action, port, host, stateDir: config.stateDir }
);
}
const result = await runServiceCommand(action, config);
if (result === undefined) {
// 'run' keeps running, resolves on shutdown
return { stdout: "", stderr: "", exitCode: 0 };
}
if (runtime.jsonMode) {
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
}
if (action === "logs" || action === "log") {
const logsResult = result as { logFile: string; lines: string[]; content: string };
return {
stdout: `====== ${config.serviceName} 守护进程日志 (${logsResult.logFile}) ======\n${logsResult.content}\n`,
stderr: "",
exitCode: 0,
};
}
const status = result as Record<string, unknown>;
const isRunning = Boolean(status.running);
const displayPairs: Record<string, unknown> = {
"服务状态": isRunning ? "🟢 正在运行 (Running)" : "⚪ 已停止 (Stopped)",
...(status.pid ? { "进程 PID": status.pid } : {}),
...(status.url ? { "服务地址": status.url } : {}),
...(target === "hose-use" && status.url ? { "交互式文档": `${status.url}/docs` } : {}),
...(status.url ? { "CLI 网关": `POST ${status.url}/api/cli` } : {}),
"运行版本": status.version || config.version,
"状态目录": status.stateDir || config.stateDir,
"PID 文件": status.pidFile || getPidFile(config),
"日志文件": status.logFile || getLogFile(config),
};
const title = (status.serviceTitle as string) || config.serviceTitle || "常驻服务管理 (Service)";
const formatted = printKeyValue(displayPairs);
return {
stdout: `====== ${title} ======\n${formatted}\n`,
stderr: "",
exitCode: 0,
};
}
export async function handleAiProServiceCommand(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const action = (parsed.action || parsed.args[0] || "status").toLowerCase();
const serviceActions = ["start", "stop", "restart", "status", "logs", "log", "run"];
if (!serviceActions.includes(action)) {
return {
stdout: "",
stderr:
`[提示] AI PRO 确定性审批仲裁命令已完全独立为 hose-aipro 命令行。\n` +
`请直接运行: hose-aipro ${action} ${parsed.args.join(" ")}\n\n` +
`如需管理本机的 AI PRO 网关常驻服务,请运行:\n` +
` hose-use aipro <start|stop|restart|status|logs>\n`,
exitCode: 1,
};
}
return handleServiceCommand(parsed, runtime, "hose-aipro");
}
export async function handleApprovalRedirectCommand(
parsed: CliParsedArgs,
_runtime: CliRuntime
): Promise<CliResult> {
const action = parsed.action || parsed.args[0] || "";
const subArgs = parsed.args.slice(1).join(" ");
return {
stdout: "",
stderr:
`[提示] 确定性审批仲裁命令已独立为 hose-aipro 命令行。\n` +
`请直接运行: hose-aipro ${action}${subArgs ? ` ${subArgs}` : ""}\n\n` +
`常用命令推荐:\n` +
` • 查看要点: hose-aipro points <单号>\n` +
` • 事实证据核验: hose-aipro review <单号> --findings='[...]'\n` +
` • 执行审批仲裁: hose-aipro decide <单号> --apply --yes\n` +
` • 审批日志溯源: hose-aipro logs <单号>\n` +
` • 网关服务启停: hose-use aipro <start|stop|status>\n`,
exitCode: 1,
};
}