- CliRuntime 新增 app?: "hose-use" | "hose-aipro",由 runCli 与 runAiProCli 各自标注;skill.ts 优先用它判断调用方,修复 hose-aipro skill dump 误输出 hose-use Skill 的既有 bug——根因是 multicall 二进制的 argv0 不会进入 parsed.raw。保留 raw 里的历史 aipro 标记作为兼容。 - 新增 skill.test.ts 覆盖两个入口与历史兼容三条路径。 - 补齐 4 份目录 README(scripts/build、cli/commands/aipro、web、 web/src):web/ 文件在 #177 合入后由 untracked 转为 tracked, 才被 check:readme 纳入扫描,暴露出 4 个目录缺文档。 校验:bun test 1111 pass / 0 fail,bun run check 通过, check:readme 36/36 通过;编译产物实测 hose-aipro skill dump 返回 AI PRO Skill、hose-use 返回平台 Skill。
178 lines
6.2 KiB
TypeScript
178 lines
6.2 KiB
TypeScript
#!/usr/bin/env bun
|
|
import { isDryRun, mutationPreview } from "./cli/mutation.js";
|
|
import { parseArgs, stringOption, booleanOption, isJsonRequested } from "./cli/parser.js";
|
|
import { cliErrorResult } from "./cli/runner.js";
|
|
import { printHelp, printJson, redactSensitiveText } from "./cli/printer.js";
|
|
import { getSessionClientForRuntime, initCorpRuntime, requireSelectedCorp } from "./cli/helpers.js";
|
|
import {
|
|
ApprovalService,
|
|
ApprovalRepository,
|
|
ApprovalGatewayBlockedError,
|
|
} from "./domains/approval/index.js";
|
|
import {
|
|
findAiProCommand,
|
|
isSaasPlatformCommand,
|
|
handleSaasPlatformRedirect,
|
|
} from "./cli/aipro-registry.js";
|
|
import { handleAiProHelp } from "./cli/commands/aipro/status.js";
|
|
import { handleAiProReport } from "./cli/commands/aipro/report.js";
|
|
import { assertAiProEnabled, handleAiProLogin } from "./cli/aipro-license.js";
|
|
import type { AiProExecutionContext } from "./cli/commands/aipro/types.js";
|
|
|
|
export async function runAiProCli(
|
|
rawArgs: string[]
|
|
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
|
|
try {
|
|
return await runAiProCliOnce(rawArgs);
|
|
} catch (error) {
|
|
return cliErrorResult(error, isJsonRequested(rawArgs));
|
|
}
|
|
}
|
|
|
|
async function runAiProCliOnce(rawArgs: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> {
|
|
const parsed = parseArgs(rawArgs, "aipro");
|
|
const jsonMode = booleanOption(parsed.options, ["json", "j"]);
|
|
if (booleanOption(parsed.options, ["help", "h", "?"])) {
|
|
const help = printHelp(["aipro", ...(parsed.area && parsed.area !== "help" ? [parsed.area] : [])]);
|
|
return { stdout: jsonMode ? printJson({ help }) : `${help}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
const runtime = initCorpRuntime({
|
|
corpId: stringOption(parsed.options, ["corp"]),
|
|
jsonMode,
|
|
});
|
|
// multicall 二进制的 argv0 不进 parsed.raw,显式标注入口身份供命令层判断
|
|
runtime.app = "hose-aipro";
|
|
|
|
const corp = runtime.selectedCorp;
|
|
const previewPositional = parsed.positional || [];
|
|
const previewCommand = (previewPositional[0] || "help").toLowerCase();
|
|
const previewDef = findAiProCommand(previewCommand);
|
|
const isMutating = typeof previewDef?.mutating === "function"
|
|
? previewDef.mutating(parsed, previewPositional)
|
|
: Boolean(previewDef?.mutating);
|
|
|
|
if (isMutating && isDryRun(parsed, runtime)) {
|
|
return mutationPreview(
|
|
{ ...parsed, raw: ["hose-aipro", ...rawArgs] },
|
|
runtime,
|
|
"预览 AI PRO 审批操作",
|
|
stringOption(parsed.options, ["run", "r", "code", "sn", "id", "target"]) || previewPositional[1] || "未指定",
|
|
{ action: previewCommand, corpId: corp?.corpId || corp?.id, apply: booleanOption(parsed.options, ["apply", "execute", "run", "do"]) }
|
|
);
|
|
}
|
|
|
|
const repo = new ApprovalRepository();
|
|
const service = new ApprovalService(repo);
|
|
let cachedSession: Awaited<ReturnType<typeof getSessionClientForRuntime>> | undefined;
|
|
|
|
const positional =
|
|
parsed.positional ??
|
|
[parsed.area, parsed.action, ...parsed.args].filter((v): v is string => Boolean(v));
|
|
const command = (positional[0] || "help").toLowerCase();
|
|
const targetSn = positional[1] || stringOption(parsed.options, ["code", "sn", "id", "target"]);
|
|
const corpId = corp?.corpId || corp?.id;
|
|
const operator = corp?.credentials?.staffId || corp?.staffId || corp?.name || "AI-Agent";
|
|
|
|
const requireCorpId = () => {
|
|
const selected = requireSelectedCorp(runtime);
|
|
return selected.corpId || selected.id;
|
|
};
|
|
|
|
const getClient = async () => {
|
|
cachedSession ??= await getSessionClientForRuntime(runtime);
|
|
return { client: cachedSession.client, corpId: cachedSession.corpId };
|
|
};
|
|
|
|
const ctx: AiProExecutionContext = {
|
|
rawArgs,
|
|
parsed,
|
|
runtime,
|
|
corp,
|
|
corpId,
|
|
operator,
|
|
positional,
|
|
command,
|
|
targetSn,
|
|
jsonMode,
|
|
repo,
|
|
service,
|
|
requireCorpId,
|
|
getClient,
|
|
};
|
|
|
|
try {
|
|
if (command === "login" || command === "activate") {
|
|
return handleAiProLogin(rawArgs, jsonMode);
|
|
}
|
|
if (booleanOption(parsed.options, ["version", "v"])) {
|
|
const versionDef = findAiProCommand("version");
|
|
if (versionDef) return await versionDef.handler(ctx);
|
|
}
|
|
|
|
// 统一门禁:除只读自省命令外,其余 AI PRO 命令一律要求有效凭证。
|
|
// 以后新增命令只需照常注册,自动被此处拦截,无需改动授权代码。
|
|
const readOnly = new Set(["status", "whoami", "context", "info", "help", "version", "v"]);
|
|
if (!readOnly.has(command)) {
|
|
const licenseGate = assertAiProEnabled(jsonMode);
|
|
if (licenseGate) return licenseGate;
|
|
}
|
|
|
|
// 本地 Web 控制台的聚合数据源(只读,不进命令注册表)
|
|
if (command === "report") {
|
|
return handleAiProReport(ctx);
|
|
}
|
|
|
|
const commandDef = findAiProCommand(command);
|
|
if (commandDef) {
|
|
return await commandDef.handler(ctx);
|
|
}
|
|
|
|
if (isSaasPlatformCommand(command)) {
|
|
return handleSaasPlatformRedirect(command, positional);
|
|
}
|
|
|
|
return await handleAiProHelp(ctx);
|
|
} catch (err: any) {
|
|
if (err instanceof ApprovalGatewayBlockedError) {
|
|
repo.logAiProAction({
|
|
action: command,
|
|
documentSn: targetSn,
|
|
operator,
|
|
args: JSON.stringify(rawArgs),
|
|
status: "BLOCKED",
|
|
summary: err.message.replace(/\n+/g, " "),
|
|
});
|
|
return {
|
|
stdout: jsonMode ? printJson({ error: true, message: err.message, exitCode: 1 }) + "\n" : "",
|
|
stderr: jsonMode ? "" : `\n${err.message}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
repo.logAiProAction({
|
|
action: command,
|
|
documentSn: targetSn,
|
|
operator,
|
|
args: JSON.stringify(rawArgs),
|
|
status: "ERROR",
|
|
summary: String(err?.message || err),
|
|
});
|
|
|
|
return {
|
|
stdout: jsonMode
|
|
? printJson({ error: true, message: String(err?.message || err), exitCode: 1 }) + "\n"
|
|
: "",
|
|
stderr: jsonMode ? "" : `[AI PRO 错误] ${err?.message || err}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
const rawArgs = process.argv.slice(2);
|
|
const result = await runAiProCli(rawArgs);
|
|
if (result.stdout) process.stdout.write(redactSensitiveText(result.stdout));
|
|
if (result.stderr) process.stderr.write(redactSensitiveText(result.stderr));
|
|
process.exit(result.exitCode);
|
|
}
|