feat(aipro): add multi-index rules command outputting matched scopes and rules

This commit is contained in:
gitea committed 2026-09-06 22:44:00 +08:00
1 parent 8d203fc230
commit 950dd6d8fa
1 file changed
+84 -3
+84 -3
View File
@@ -68,9 +68,6 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
switch (command) {
// ── 1. 规约场景索引列表查看 ─────────────────────────────────────────
case "scopes":
case "scope":
case "rules":
case "rule":
case "index":
case "indexes": {
const scopes = repo.listScopes().map((s) => ({
@@ -80,6 +77,90 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
return { stdout: JSON.stringify(scopes, null, 2) + "\n", stderr: "", exitCode: 0 };
}
// ── 2. 查询命中索引关联的所有规则(支持传入一个或多个索引)──────────────
case "rules":
case "rule":
case "scope": {
const rawTargets = [
...positional.slice(1),
stringOption(parsed.options, ["scope", "s", "index", "i"]),
].filter(Boolean) as string[];
const targets: string[] = [];
for (const raw of rawTargets) {
const trimmed = raw.trim();
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
try {
const parsedJson = JSON.parse(trimmed);
if (Array.isArray(parsedJson)) {
targets.push(...parsedJson.map(String));
continue;
}
} catch {}
}
if (trimmed.includes(",")) {
targets.push(...trimmed.split(",").map((s) => s.trim()).filter(Boolean));
} else if (trimmed) {
targets.push(trimmed);
}
}
const uniqueTargets = [...new Set(targets)];
if (uniqueTargets.length === 0) {
// 未传入索引时,默认输出所有场景及其规则
const allScopes = repo.listScopes("active");
const result = allScopes.map((s) => {
const detail = repo.getScopeDetail(s.index)!;
return {
index: detail.index,
scope: detail.scope,
rules: detail.rules.map((r) => ({
code: r.code,
text: r.body,
})),
};
});
return { stdout: JSON.stringify(result, null, 2) + "\n", stderr: "", exitCode: 0 };
}
const results: Array<{
index: number;
scope: string;
rules: Array<{ code: number; text: string }>;
}> = [];
const notFound: string[] = [];
for (const target of uniqueTargets) {
const detail = repo.getScopeDetail(target);
if (!detail) {
notFound.push(target);
continue;
}
results.push({
index: detail.index,
scope: detail.scope,
rules: detail.rules.map((r) => ({
code: r.code,
text: r.body,
})),
});
}
if (notFound.length > 0 && results.length === 0) {
return {
stdout: "",
stderr: `[NOT FOUND] 未找到指定索引对应的规约场景: ${notFound.join(", ")}\n提示: 请先运行 "hose-aipro scopes" 查看所有可用场景索引。\n`,
exitCode: 1,
};
}
return {
stdout: JSON.stringify(results, null, 2) + "\n",
stderr: notFound.length > 0 ? `[WARNING] 部分索引未找到: ${notFound.join(", ")}\n` : "",
exitCode: 0,
};
}
// ── 2. 查看审核要点清单(纯规则元数据,与具体单据无关)────────────────
case "points":
case "clauses": {