feat(aipro): require wrapped array for rules input and flatten output by code
This commit is contained in:
1 parent
950dd6d8fa
commit
2afde446d2
1 file changed
+58
-56
+58
-56
@@ -77,86 +77,88 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
|
||||
return { stdout: JSON.stringify(scopes, null, 2) + "\n", stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
// ── 2. 查询命中索引关联的所有规则(支持传入一个或多个索引)──────────────
|
||||
// ── 2. 查询命中索引关联的所有规则(按 code 升序平铺输出纯规则)──────────
|
||||
case "rules":
|
||||
case "rule":
|
||||
case "scope": {
|
||||
const rawTargets = [
|
||||
const rawInputs = [
|
||||
...positional.slice(1),
|
||||
stringOption(parsed.options, ["scope", "s", "index", "i"]),
|
||||
stringOption(parsed.options, ["scope", "s", "index", "i", "indexes"]),
|
||||
].filter(Boolean) as string[];
|
||||
|
||||
const targets: string[] = [];
|
||||
for (const raw of rawTargets) {
|
||||
const targetIndexes: number[] = [];
|
||||
for (const raw of rawInputs) {
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
|
||||
try {
|
||||
const parsedJson = JSON.parse(trimmed);
|
||||
if (Array.isArray(parsedJson)) {
|
||||
targets.push(...parsedJson.map(String));
|
||||
for (const item of parsedJson) {
|
||||
const n = Number(item);
|
||||
if (!isNaN(n)) targetIndexes.push(n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} catch {}
|
||||
} catch {
|
||||
// 兼容非严格 JSON 数组,如 [1, 2]
|
||||
const inner = trimmed.slice(1, -1);
|
||||
const parts = inner.split(/[,,\s]+/).filter(Boolean);
|
||||
for (const p of parts) {
|
||||
const n = Number(p.replace(/['"]/g, ""));
|
||||
if (!isNaN(n)) targetIndexes.push(n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (trimmed.includes(",")) {
|
||||
targets.push(...trimmed.split(",").map((s) => s.trim()).filter(Boolean));
|
||||
} else if (trimmed) {
|
||||
targets.push(trimmed);
|
||||
const parts = trimmed.split(/[,,\s]+/).filter(Boolean);
|
||||
for (const p of parts) {
|
||||
const n = Number(p.replace(/[\[\]'"]/g, ""));
|
||||
if (!isNaN(n)) targetIndexes.push(n);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
const uniqueIndexes = [...new Set(targetIndexes)].sort((a, b) => a - b);
|
||||
if (uniqueIndexes.length === 0) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 未找到指定索引对应的规约场景: ${notFound.join(", ")}\n提示: 请先运行 "hose-aipro scopes" 查看所有可用场景索引。\n`,
|
||||
stderr: `请提供包裹好的索引数组,例如:\n hose-aipro rules "[2]"\n hose-aipro rules "[1, 2]"\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const rulesMap = new Map<number, { code: number; text: string }>();
|
||||
const notFound: number[] = [];
|
||||
|
||||
for (const idx of uniqueIndexes) {
|
||||
const detail = repo.getScopeDetail(idx);
|
||||
if (!detail) {
|
||||
notFound.push(idx);
|
||||
continue;
|
||||
}
|
||||
for (const r of detail.rules) {
|
||||
if (!rulesMap.has(r.code)) {
|
||||
rulesMap.set(r.code, {
|
||||
code: r.code,
|
||||
text: r.body,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (notFound.length > 0 && rulesMap.size === 0) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 未找到指定索引对应的规约场景: [${notFound.join(", ")}]\n提示: 请先运行 "hose-aipro scopes" 查看所有可用场景索引。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
// 按照 code 顺序平铺规则
|
||||
const flattenedRules = Array.from(rulesMap.values()).sort((a, b) => a.code - b.code);
|
||||
|
||||
return {
|
||||
stdout: JSON.stringify(results, null, 2) + "\n",
|
||||
stderr: notFound.length > 0 ? `[WARNING] 部分索引未找到: ${notFound.join(", ")}\n` : "",
|
||||
stdout: JSON.stringify(flattenedRules, null, 2) + "\n",
|
||||
stderr: notFound.length > 0 ? `[WARNING] 部分索引未找到: [${notFound.join(", ")}]\n` : "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user