Files
hose-use/src/cli/aipro-registry.ts
T
gitea 362acfba58 feat(cli): 分离 hose-aipro 独立命令体系并升级至 v0.9.0
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 自动递增
2026-11-03 19:16:12 +08:00

235 lines
6.4 KiB
TypeScript

import { handleAiProHelp, handleAiProLogs, handleAiProStatus } from "./commands/aipro/status.js";
import {
handleAiProChecklist,
handleAiProPoints,
handleAiProRules,
handleAiProScopes,
} from "./commands/aipro/rules.js";
import {
handleAiProBind,
handleAiProDecide,
handleAiProInit,
handleAiProReview,
handleAiProTranslate,
} from "./commands/aipro/session.js";
import { handleUpdateCommand } from "./commands/update/index.js";
import { handleSkillCommand } from "./commands/skill.js";
import { handleServiceCommand } from "./commands/service.js";
import { formatVersionLabel, getVersionInfo } from "../version.js";
import { printJson } from "./printer.js";
import type { AiProExecutionContext } from "./commands/aipro/types.js";
import type { CliParsedArgs, CliResult } from "./types.js";
export type AiProMutatingCheck =
| boolean
| ((parsed: CliParsedArgs, positional: string[]) => boolean);
export interface AiProCommandDefinition {
name: string;
summary: string;
aliases?: readonly string[];
mutating?: AiProMutatingCheck;
handler: (ctx: AiProExecutionContext) => Promise<CliResult> | CliResult;
}
export const AIPRO_COMMAND_REGISTRY: readonly AiProCommandDefinition[] = [
{
name: "status",
summary: "查看网关状态",
aliases: ["whoami", "context", "info"],
handler: (ctx) => handleAiProStatus(ctx),
},
{
name: "init",
summary: "初始化审批上下文",
aliases: ["start"],
mutating: true,
handler: (ctx) => handleAiProInit(ctx),
},
{
name: "scopes",
summary: "列出审批规则场景",
aliases: ["index", "indexes"],
handler: (ctx) => handleAiProScopes(ctx),
},
{
name: "translate",
summary: "转译单据",
aliases: ["doc", "markdown", "render"],
handler: (ctx) => handleAiProTranslate(ctx),
},
{
name: "rules",
summary: "查看审批规则",
aliases: ["rule"],
handler: (ctx) => handleAiProRules(ctx),
},
{
name: "bind",
summary: "绑定单据",
aliases: ["mount"],
mutating: true,
handler: (ctx) => handleAiProBind(ctx),
},
{
name: "points",
summary: "查看审核要点",
aliases: ["clauses"],
mutating: (_parsed, positional) => positional.length >= 3,
handler: (ctx) => handleAiProPoints(ctx),
},
{
name: "checklist",
summary: "查看核验清单",
aliases: ["list"],
handler: (ctx) => handleAiProChecklist(ctx),
},
{
name: "review",
summary: "核验要点",
aliases: ["eval", "evaluate"],
mutating: true,
handler: (ctx) => handleAiProReview(ctx),
},
{
name: "decide",
summary: "执行仲裁",
aliases: ["approve", "reject"],
mutating: true,
handler: (ctx) => handleAiProDecide(ctx),
},
{
name: "logs",
summary: "查看审批日志",
aliases: ["log", "history", "trail"],
handler: (ctx) => handleAiProLogs(ctx),
},
{
name: "service",
summary: "管理本机 hose-aipro 常驻服务",
aliases: ["daemon"],
handler: async (ctx) => {
const subTarget = ctx.positional[1]?.toLowerCase();
if (subTarget === "hose-use" || subTarget === "hose") {
return handleServiceCommand(
{ ...ctx.parsed, action: ctx.positional[2] || "status", args: ctx.positional.slice(3) },
ctx.runtime,
"hose-use"
);
}
return handleServiceCommand(
{ ...ctx.parsed, action: ctx.positional[1] || "status", args: ctx.positional.slice(2) },
ctx.runtime,
"hose-aipro"
);
},
},
{
name: "hose-use",
summary: "管理本机 hose-use 常驻服务",
aliases: ["hose"],
handler: async (ctx) => {
const subAction = (ctx.positional[1] || "status").toLowerCase();
const serviceActions = ["start", "stop", "restart", "status", "logs", "log", "run"];
if (serviceActions.includes(subAction)) {
return handleServiceCommand(
{ ...ctx.parsed, action: subAction, args: ctx.positional.slice(2) },
ctx.runtime,
"hose-use"
);
}
return {
stdout: "",
stderr:
`[提示] 运行 hose-use ${subAction} 可直接调用合思企业平台命令。\n` +
`如需管理本机的 hose-use 常驻服务,请运行:\n` +
` hose-aipro hose-use <start|stop|restart|status|logs>\n`,
exitCode: 1,
};
},
},
{
name: "update",
summary: "检查并升级版本",
aliases: ["self-update"],
handler: async (ctx) => handleUpdateCommand(ctx.parsed, ctx.runtime),
},
{
name: "skill",
summary: "管理 Agent 技能",
aliases: ["skills"],
handler: async (ctx) => handleSkillCommand(ctx.parsed, ctx.runtime),
},
{
name: "version",
summary: "查看版本信息",
aliases: ["v"],
handler: async (ctx) => ({
stdout: ctx.jsonMode
? printJson(getVersionInfo("hose-aipro")) + "\n"
: `${formatVersionLabel("hose-aipro")}\n`,
stderr: "",
exitCode: 0,
}),
},
{
name: "help",
summary: "查看帮助说明",
handler: (ctx) => handleAiProHelp(ctx),
},
];
const aiproCommandMap = new Map<string, AiProCommandDefinition>();
for (const def of AIPRO_COMMAND_REGISTRY) {
aiproCommandMap.set(def.name.toLowerCase(), def);
for (const alias of def.aliases || []) {
aiproCommandMap.set(alias.toLowerCase(), def);
}
}
export function findAiProCommand(name: string): AiProCommandDefinition | undefined {
return aiproCommandMap.get(name.toLowerCase());
}
export const SAAS_PLATFORM_COMMANDS = [
"bill",
"flow",
"workflow",
"platform",
"datalink",
"auth",
"corps",
"contact",
"feetype",
"dimension",
"role",
"permission",
"keys",
"store",
"message",
"task",
"config",
"mcp",
"assist-platform",
] as const;
export function isSaasPlatformCommand(command: string): boolean {
return (SAAS_PLATFORM_COMMANDS as readonly string[]).includes(command.toLowerCase());
}
export function handleSaasPlatformRedirect(
command: string,
positional: string[] = []
): CliResult {
const subArgs = positional.slice(1).join(" ");
return {
stdout: "",
stderr:
`[提示] ${command} 属于合思企业平台能力,由 hose-use 命令行提供。\n` +
`请直接运行: hose-use ${command}${subArgs ? ` ${subArgs}` : ""}\n\n` +
`如需管理本机的 hose-use 常驻服务,请运行:\n` +
` hose-aipro hose-use <start|stop|restart|status|logs>\n`,
exitCode: 1,
};
}