1. 架构对称性与声明式注册表抽取:
- 新增 src/cli/aipro-registry.ts,声明式定义 AIPRO_COMMAND_REGISTRY 及其别名映射与 mutating 突变检查
- 重构 src/aipro-cli.ts,移除过程式 switch-case 分发,改为查表式声明分发,与 hose-use 的 CLI_COMMAND_REGISTRY 保持 100% 对称
- 在 src/cli/registry.ts 统一重导出 aipro-registry,巩固其作为顶层声明注册表的单一事实源地位
- 增加 aipro-cli 测试用例,强制校验 COMMAND_METADATA["hose-aipro"] 与声明式注册表双向一致性
2. 双 Skill 独立打包与内嵌部署:
- 注册两个独立技能:skills/hose-use 与 skills/hose-aipro,互相关联引用
- src/packaging/embedded-skill.ts 增加双 Skill 编译期嵌入与 installSkillFiles 全量同步
- hose-use / hose-aipro 下的 skill status/dump/install 完全对齐
3. 守护服务交叉治理:
- src/cli/service.ts 支持 ServiceTargetName ("hose-use" | "hose-aipro"),默认端口 8888 与 8889
- 支持 hose-use aipro <start|stop|...> 与 hose-aipro hose-use <start|stop|...> 双向服务纳管
- 误触非本领域业务命令时提供友好互导拦截
4. 审批领域数据层防御加固:
- 迁移 004:approval_runs 增加 short_id 唯一约束
- 增加 allocateUniqueShortId 冲突重试,多步写操作纳入事务
5. 版本升级至 v0.9.0:
- package.json、README.md、specs、.zcode-plugin 及两套 Skill 同步更新至 0.9.0
- bump-version 脚本适配多 Skill 自动递增
170 lines
6.3 KiB
TypeScript
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/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,
|
|
};
|
|
}
|