- 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。
184 lines
6.8 KiB
TypeScript
184 lines
6.8 KiB
TypeScript
import { VERSION_INFO, formatVersionLabel } from "../version.js";
|
|
import { parseArgs, stringOption, booleanOption, outputOptions, isJsonRequested } from "./parser.js";
|
|
import { printHelp, printJson, redactSensitiveText } from "./printer.js";
|
|
import { resolveCommandMetadata } from "./command-metadata.js";
|
|
import { classifyCliError } from "./errors.js";
|
|
import { initCorpRuntime } from "./helpers.js";
|
|
import { findCommandByArea, isStandingCommand } from "./registry.js";
|
|
import type { CliParsedArgs, CliResult, CliRunOptions } from "./types.js";
|
|
import { EkbError } from "../core/errors.js";
|
|
|
|
let runQueue = Promise.resolve<any>(undefined);
|
|
|
|
export function runCli(
|
|
rawArgs: string[],
|
|
options: CliRunOptions = {}
|
|
): Promise<CliResult> {
|
|
if (isStandingCommand(rawArgs)) {
|
|
return dispatchCli(rawArgs, options);
|
|
}
|
|
const run = runQueue.then(() => runCliOnce(rawArgs, options));
|
|
runQueue = run.then(
|
|
() => undefined,
|
|
() => undefined
|
|
);
|
|
return run;
|
|
}
|
|
|
|
async function runCliOnce(
|
|
rawArgs: string[],
|
|
options: CliRunOptions = {}
|
|
): Promise<CliResult> {
|
|
const stdoutChunks: string[] = [];
|
|
const stderrChunks: string[] = [];
|
|
const machineJson = isJsonRequested(rawArgs);
|
|
const originalLog = console.log;
|
|
const originalError = console.error;
|
|
|
|
console.log = (...values: unknown[]) => {
|
|
const line = values.map((v) => (typeof v === "string" ? v : String(v))).join(" ");
|
|
(machineJson ? stderrChunks : stdoutChunks).push(line);
|
|
};
|
|
console.error = (...values: unknown[]) => {
|
|
stderrChunks.push(values.map((v) => (typeof v === "string" ? v : String(v))).join(" "));
|
|
};
|
|
|
|
try {
|
|
const result = await dispatchCli(rawArgs, options);
|
|
const extraStdout = stdoutChunks.length ? `${stdoutChunks.join("\n")}\n` : "";
|
|
const extraStderr = stderrChunks.length ? `${stderrChunks.join("\n")}\n` : "";
|
|
const bodyStdout = result.preserveSensitiveStdout
|
|
? result.stdout || ""
|
|
: redactSensitiveText(result.stdout || "");
|
|
return {
|
|
stdout: redactSensitiveText(extraStdout) + bodyStdout,
|
|
stderr: redactSensitiveText(extraStderr + (result.stderr || "")),
|
|
exitCode: result.exitCode,
|
|
preserveSensitiveStdout: result.preserveSensitiveStdout,
|
|
};
|
|
} finally {
|
|
console.log = originalLog;
|
|
console.error = originalError;
|
|
}
|
|
}
|
|
|
|
function isHelpRequested(parsed: CliParsedArgs): boolean {
|
|
const normArea = (parsed.area || "").replace(/^-+/, "").toLowerCase();
|
|
if (normArea === "help" || normArea === "h" || normArea === "?" || parsed.action === "help") return true;
|
|
|
|
for (const [key, val] of Object.entries(parsed.options)) {
|
|
const normKey = key.replace(/^-+/, "").toLowerCase();
|
|
if (normKey === "help" || normKey === "h" || normKey === "?") {
|
|
if (val === true || val === "true" || val === "") return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isVersionRequested(parsed: CliParsedArgs): boolean {
|
|
const normArea = (parsed.area || "").replace(/^-+/, "").toLowerCase();
|
|
if (normArea === "version" || normArea === "v") return true;
|
|
|
|
for (const [key, val] of Object.entries(parsed.options)) {
|
|
const normKey = key.replace(/^-+/, "").toLowerCase();
|
|
const localFlags = resolveCommandMetadata(parsed.area)?.metadata.flags;
|
|
if (key === "v" && localFlags?.v) continue;
|
|
if (key === "version" && localFlags?.version === "value" && typeof val !== "boolean") continue;
|
|
if (normKey === "version" || normKey === "v") {
|
|
if (val === true || val === "true" || val === "") return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function resolveHelpTarget(parsed: CliParsedArgs): string[] {
|
|
if (["help", "h", "?"].includes((parsed.area || "").toLowerCase())) {
|
|
return [parsed.action, parsed.args[0], parsed.args[1]].filter((value): value is string => Boolean(value));
|
|
}
|
|
const primaryAction = parsed.action === "help" ? parsed.args[0] : parsed.action;
|
|
const subAction = parsed.action === "help" ? parsed.args[1] : parsed.args[0];
|
|
return [parsed.area, primaryAction, subAction].filter((value): value is string => Boolean(value));
|
|
}
|
|
|
|
async function dispatchCli(rawArgs: string[], options: CliRunOptions = {}): Promise<CliResult> {
|
|
try {
|
|
return await dispatchParsedCli(rawArgs, options);
|
|
} catch (error) {
|
|
return cliErrorResult(error, isJsonRequested(rawArgs));
|
|
}
|
|
}
|
|
|
|
export function cliErrorResult(error: unknown, jsonMode: boolean): CliResult {
|
|
const message = redactSensitiveText(error instanceof Error ? error.message : String(error));
|
|
const classification = classifyCliError(error);
|
|
return {
|
|
stdout: jsonMode ? redactSensitiveText(JSON.stringify({ error: true, message, exitCode: classification.exitCode, ...(classification.nextAction ? { nextAction: classification.nextAction } : {}), ...(error instanceof EkbError && error.details ? { details: error.details } : {}) }, null, 2)) : "",
|
|
stderr: jsonMode ? "" : `[ERROR] ${message}${classification.nextAction ? `\n下一步: ${classification.nextAction}` : ""}\n`,
|
|
exitCode: classification.exitCode,
|
|
};
|
|
}
|
|
|
|
async function dispatchParsedCli(
|
|
rawArgs: string[],
|
|
options: CliRunOptions = {}
|
|
): Promise<CliResult> {
|
|
const parsed = parseArgs(rawArgs);
|
|
const jsonMode = booleanOption(parsed.options, ["json", "j"]);
|
|
|
|
if (isHelpRequested(parsed)) {
|
|
const help = printHelp(resolveHelpTarget(parsed));
|
|
return {
|
|
stdout: jsonMode ? printJson({ help }) : `${help}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (isVersionRequested(parsed)) {
|
|
return {
|
|
stdout: jsonMode ? printJson(VERSION_INFO) : `${formatVersionLabel("hose-use")}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const corpIdOption = stringOption(parsed.options, ["corp"]);
|
|
const statelessOption = booleanOption(parsed.options, ["stateless"]);
|
|
const statefulOption = booleanOption(parsed.options, ["stateful"]);
|
|
const outputOpts = outputOptions(parsed.options);
|
|
const dryRun = booleanOption(parsed.options, ["dry-run", "dryRun", "dryrun"]);
|
|
const confirmed = booleanOption(parsed.options, ["yes", "y", "confirm", "force-write"]);
|
|
|
|
const runtime = initCorpRuntime({
|
|
corpId: corpIdOption,
|
|
stateless: statelessOption,
|
|
stateful: statefulOption,
|
|
jsonMode,
|
|
dryRun,
|
|
confirmed,
|
|
outputOptions: outputOpts,
|
|
appRoot: options.appRoot,
|
|
entryFile: options.entryFile,
|
|
launcher: options.launcher,
|
|
});
|
|
runtime.writeStdout = options.writeStdout;
|
|
runtime.app = "hose-use";
|
|
|
|
try {
|
|
if (!parsed.area) {
|
|
return { stdout: printHelp(resolveHelpTarget(parsed)) + "\n", stderr: "", exitCode: 0 };
|
|
}
|
|
const commandDef = findCommandByArea(parsed.area);
|
|
if (!commandDef) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知命令域: ${parsed.area}。运行 hose-use --help 查看帮助\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
return await commandDef.handler(parsed, runtime, options);
|
|
} catch (error) {
|
|
return cliErrorResult(error, jsonMode);
|
|
}
|
|
}
|