默认不激活 AI PRO,需服务方离线下发 Ed25519 签名凭证后在本地执行 hose-aipro login 激活。凭证只含 sub / exp / maxVersion 等业务字段, 校验完全在本地完成,不联网。 约束仅两条: - 当前二进制版本不得高于 maxVersion(三段数字比较,忽略 -dev 后缀) - 当前时间不得晚于 exp(绝对到期时间;不做时长上限、不做时钟防篡改) 实现要点: - src/cli/aipro-license.ts 单文件承载验签、版本比较、0600 凭证落盘、 门禁与 login;公钥内嵌源码,私钥由签发方离线保管、绝不进仓库 - src/aipro-cli.ts 分发层 12 行统一门禁;login 在门禁前分流且不进命令 注册表。以后新增任何 AI PRO 命令只需照常注册,自动受门禁管辖, 授权代码零侵入 - scripts/license/issue.ts 离线签发,私钥只读本地文件或环境变量, exp 想签多久签多久,脚本不施加任何天数限制 - 放行 status / help / version 等自省命令,其余业务命令未激活一律拦截 - 测试用进程内依赖注入临时密钥对,不引入环境变量后门 - skills/hose-aipro/SKILL.md 补充激活说明 校验:bun test 1094 pass / 0 fail,bun run check 通过; 端到端冒烟(未激活拦截 → 签发 → login → 版本超限拒绝)全通过。
170 lines
5.8 KiB
TypeScript
170 lines
5.8 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 { 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,
|
|
});
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|