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/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 { 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 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= 指定企业。"); 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; const isRunning = Boolean(status.running); const displayPairs: Record = { "服务状态": 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 { 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 \n`, exitCode: 1, }; } return handleServiceCommand(parsed, runtime, "hose-aipro"); } export async function handleApprovalRedirectCommand( parsed: CliParsedArgs, _runtime: CliRuntime ): Promise { 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 \n`, exitCode: 1, }; }