300 lines
11 KiB
TypeScript
300 lines
11 KiB
TypeScript
import type { ApprovalRunRecord } from "../../../domains/approval/index.js";
|
||
import { stringOption } from "../../parser.js";
|
||
import { printJson, printTable } from "../../printer.js";
|
||
import { parseIntegerList, type AiProCommandResult, type AiProExecutionContext } from "./types.js";
|
||
|
||
export async function handleAiProScopes(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
||
const { parsed, positional, repo, operator, rawArgs } = ctx;
|
||
const runHandle = stringOption(parsed.options, ["run", "r"]) || positional[1];
|
||
let runRecord: ApprovalRunRecord | null = null;
|
||
if (runHandle) {
|
||
runRecord = repo.getRunByHandle(runHandle);
|
||
if (!runRecord) {
|
||
throw new Error(`未找到会话 ID 为 [${runHandle}] 的审批实例`);
|
||
}
|
||
}
|
||
|
||
repo.logAiProAction({
|
||
action: "scopes",
|
||
runId: runRecord?.shortId || runRecord?.id,
|
||
documentSn: runRecord?.documentSn,
|
||
operator,
|
||
args: JSON.stringify(rawArgs),
|
||
status: "SUCCESS",
|
||
summary: runRecord ? `在会话 [${runRecord.shortId}] 查阅规约场景索引` : "查阅规约场景索引",
|
||
});
|
||
|
||
const scopes = repo.listScopes().map((s) => ({
|
||
索引号: s.index,
|
||
适用场景: s.scope,
|
||
}));
|
||
return { stdout: JSON.stringify(scopes, null, 2) + "\n", stderr: "", exitCode: 0 };
|
||
}
|
||
|
||
export async function handleAiProRules(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
||
const { parsed, positional, repo, operator, rawArgs } = ctx;
|
||
const runHandle = stringOption(parsed.options, ["run", "r"]);
|
||
const rawIndexes =
|
||
stringOption(parsed.options, ["indexes", "index", "i", "scope", "s"]) ||
|
||
positional[1] ||
|
||
positional[2];
|
||
|
||
let runRecord: ApprovalRunRecord | null = null;
|
||
if (runHandle) {
|
||
runRecord = repo.getRunByHandle(runHandle);
|
||
if (!runRecord) {
|
||
throw new Error(`未找到会话 ID 为 [${runHandle}] 的审批实例`);
|
||
}
|
||
}
|
||
|
||
const targetIndexes = parseIntegerList(rawIndexes);
|
||
const uniqueIndexes = [...new Set(targetIndexes)].sort((a, b) => a - b);
|
||
if (uniqueIndexes.length === 0) {
|
||
return {
|
||
stdout: "",
|
||
stderr: `请提供具名参数,例如:\n hose-aipro rules --run <runId> --indexes "[1]"\n hose-aipro rules --run <runId> --indexes "[1, 2]"\n`,
|
||
exitCode: 1,
|
||
};
|
||
}
|
||
|
||
const rulesMap = new Map<number, { 编号: number; 适用场景: string; 规则明细: 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, {
|
||
编号: r.code,
|
||
适用场景: detail.scope,
|
||
规则明细: r.body,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
if (notFound.length > 0 && rulesMap.size === 0) {
|
||
return {
|
||
stdout: "",
|
||
stderr: `[NOT FOUND] 未找到指定索引对应的规约场景: [${notFound.join(", ")}]\n提示: 请先运行 "hose-aipro scopes" 查看所有可用场景索引。\n`,
|
||
exitCode: 1,
|
||
};
|
||
}
|
||
|
||
const flattenedRules = Array.from(rulesMap.values()).sort((a, b) => a.编号 - b.编号);
|
||
|
||
repo.logAiProAction({
|
||
action: "rules",
|
||
runId: runRecord?.shortId || runRecord?.id,
|
||
documentSn: runRecord?.documentSn,
|
||
operator,
|
||
args: JSON.stringify(rawArgs),
|
||
status: "SUCCESS",
|
||
summary: runRecord
|
||
? `在会话 [${runRecord.shortId}] 查询索引 ${JSON.stringify(uniqueIndexes)} 的规约明细 (${flattenedRules.length} 条)`
|
||
: `查询索引 ${JSON.stringify(uniqueIndexes)} 的规约明细 (${flattenedRules.length} 条)`,
|
||
});
|
||
|
||
return {
|
||
stdout: JSON.stringify(flattenedRules, null, 2) + "\n",
|
||
stderr: notFound.length > 0 ? `[WARNING] 部分索引未找到: [${notFound.join(", ")}]\n` : "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
export async function handleAiProPoints(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
||
const { parsed, positional, repo, service, runtime, operator, rawArgs, getClient } = ctx;
|
||
const arg1 = positional[1] || stringOption(parsed.options, ["scope", "s", "index", "i"]);
|
||
const arg2 = positional[2];
|
||
|
||
// 兼容双参数调用: 如果同时提供了单号和索引号,走单据绑定流
|
||
if (arg1 && arg2) {
|
||
const billSn = arg1;
|
||
const scopeTarget = arg2;
|
||
const context = await getClient();
|
||
const result = await service.injectAuditPoints(context.client, context.corpId, billSn, {
|
||
scope: scopeTarget ? (isNaN(Number(scopeTarget)) ? scopeTarget : Number(scopeTarget)) : undefined,
|
||
});
|
||
|
||
repo.logAiProAction({
|
||
action: "bind",
|
||
documentSn: result.bill.billCode,
|
||
operator,
|
||
args: JSON.stringify(rawArgs),
|
||
status: "SUCCESS",
|
||
decision: "PENDING",
|
||
summary: `绑定单据并挂载审核要点成功,共 ${result.findings.length} 条要点(场景: ${result.scope})`,
|
||
});
|
||
|
||
if (runtime.jsonMode) {
|
||
return {
|
||
stdout: printJson({
|
||
sn: result.bill.billCode,
|
||
scope: result.scope,
|
||
runId: result.run.id,
|
||
count: result.findings.length,
|
||
findings: result.findings,
|
||
}),
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
const headers = ["#", "严格程度", "归属规则", "审核要点内容", "核验状态"];
|
||
const rows = result.findings.map((f, i) => [
|
||
i + 1,
|
||
`[${f.strictness}]`,
|
||
f.ruleCode ? `#${f.ruleCode}` : "-",
|
||
f.clauseText,
|
||
f.checkStatus,
|
||
]);
|
||
|
||
const table = printTable(headers, rows);
|
||
const title = `====== [AI PRO] 单据 ${result.bill.billCode} 已挂载场景 [${result.scope}] 审核要点 (共 ${result.findings.length} 条) ======\n`;
|
||
const tip =
|
||
`\n✅ [已安全登记] 审核要点已固化至数据库审批实例,全流程监控已激活。\n` +
|
||
`下一步: 运行 "hose-aipro review ${result.bill.billCode} --eval '...' " 进行事实核验打标。\n` +
|
||
`最终决断: 运行 "hose-aipro decide ${result.bill.billCode} --apply --yes" 由确定性网关仲裁审批。\n`;
|
||
|
||
return { stdout: title + table + tip, stderr: "", exitCode: 0 };
|
||
}
|
||
|
||
const targetScope = arg1;
|
||
if (!targetScope) {
|
||
const allScopes = repo.listScopes("active");
|
||
if (allScopes.length === 0) {
|
||
return {
|
||
stdout: "规约库中暂无生效规则。可使用 hose-use rule add <场景> <正文> 新增规则。\n",
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
const lines: string[] = [
|
||
`====== [AI PRO] 规约审核要点总览 (共 ${allScopes.length} 个适用场景) ======\n`,
|
||
];
|
||
for (const s of allScopes) {
|
||
const detail = repo.getScopeDetail(s.index);
|
||
if (!detail) continue;
|
||
lines.push(`▶ 场景 [${detail.index}] ${detail.scope} (包含 ${detail.ruleCount} 条规则):`);
|
||
let count = 0;
|
||
detail.rules.forEach((r) => {
|
||
r.parsed.clauses.forEach((c) => {
|
||
count++;
|
||
lines.push(` ${count}. [${c.strictness}] (规则 #${r.code}) ${c.action}`);
|
||
});
|
||
});
|
||
lines.push("");
|
||
}
|
||
lines.push(`提示: 输入 "hose-aipro points <索引号|场景名称>" 查看指定场景要点明细。\n`);
|
||
return { stdout: lines.join("\n"), stderr: "", exitCode: 0 };
|
||
}
|
||
|
||
const detail = repo.getScopeDetail(targetScope);
|
||
if (!detail) {
|
||
return {
|
||
stdout: "",
|
||
stderr: `[NOT FOUND] 未找到索引号或场景为 "${targetScope}" 的规约场景。\n提示: 请先运行 "hose-aipro scopes" 查看所有可用场景索引。\n`,
|
||
exitCode: 1,
|
||
};
|
||
}
|
||
|
||
const points: Array<{ index: number; strictness: string; ruleCode: number; clauseText: string }> = [];
|
||
let count = 0;
|
||
for (const rule of detail.rules) {
|
||
for (const clause of rule.parsed.clauses) {
|
||
count++;
|
||
points.push({
|
||
index: count,
|
||
strictness: clause.strictness,
|
||
ruleCode: rule.code,
|
||
clauseText: clause.action,
|
||
});
|
||
}
|
||
}
|
||
|
||
if (runtime.jsonMode) {
|
||
return {
|
||
stdout: printJson({
|
||
index: detail.index,
|
||
scope: detail.scope,
|
||
ruleCount: detail.ruleCount,
|
||
pointsCount: points.length,
|
||
points,
|
||
}),
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
const headers = ["#", "严格程度", "归属规则", "审核要点内容 (Clause)"];
|
||
const rows = points.map((p) => [p.index, `[${p.strictness}]`, `#${p.ruleCode}`, p.clauseText]);
|
||
|
||
const table = printTable(headers, rows);
|
||
const title = `====== [AI PRO] 适用场景 [${detail.index}] ${detail.scope} - 审核要点清单 (共 ${points.length} 条要点) ======\n`;
|
||
const tip =
|
||
`\n说明: 审核要点为规约条款提取出的判定标准,纯规则元数据,与具体单据无关。\n` +
|
||
`绑定单据: 运行 "hose-aipro bind <单号> ${detail.index}" 将此要点清单挂载到具体单据。\n`;
|
||
|
||
return { stdout: title + table + tip, stderr: "", exitCode: 0 };
|
||
}
|
||
|
||
export async function handleAiProChecklist(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
||
const { targetSn, requireCorpId, service, repo, operator, rawArgs, runtime } = ctx;
|
||
if (!targetSn) {
|
||
throw new Error("请提供单号: hose-aipro checklist <单号>");
|
||
}
|
||
|
||
const activeCorpId = requireCorpId();
|
||
const { run, findings } = service.getAuditChecklist(targetSn, activeCorpId);
|
||
if (!run || findings.length === 0) {
|
||
repo.logAiProAction({
|
||
action: "checklist",
|
||
documentSn: targetSn,
|
||
operator,
|
||
args: JSON.stringify(rawArgs),
|
||
status: "BLOCKED",
|
||
summary: "查询审核清单阻断:单据尚未绑定任何审核要点",
|
||
});
|
||
|
||
const errorMsg =
|
||
`[审批链路阻断] 单据 [${targetSn}] 尚未绑定任何明细级审核要点(要点列表为空)!\n` +
|
||
`网关安全卡点生效:在要点未绑定前,审批链路物理不通!\n` +
|
||
`请先执行: hose-aipro bind ${targetSn} <索引号> 挂载明细级审核要点清单。\n`;
|
||
return { stdout: "", stderr: errorMsg, exitCode: 1 };
|
||
}
|
||
|
||
if (runtime.jsonMode) {
|
||
return {
|
||
stdout: printJson({ run, findings }),
|
||
stderr: "",
|
||
exitCode: 0,
|
||
};
|
||
}
|
||
|
||
const headers = ["#", "严格程度", "规则", "审核要点内容", "核验状态", "事实证据 / 说明"];
|
||
const rows = findings.map((f, i) => [
|
||
i + 1,
|
||
`[${f.strictness}]`,
|
||
f.ruleCode ? `#${f.ruleCode}` : "-",
|
||
f.clauseText,
|
||
f.checkStatus === "PASS"
|
||
? "✅ PASS"
|
||
: f.checkStatus === "FAIL"
|
||
? "❌ FAIL"
|
||
: f.checkStatus === "SKIP"
|
||
? "➖ SKIP"
|
||
: "⏳ PENDING",
|
||
[f.evidence, f.reasoning].filter(Boolean).join(" | ") || "-",
|
||
]);
|
||
|
||
const table = printTable(headers, rows);
|
||
const pendingCount = findings.filter((f) => f.checkStatus === "PENDING").length;
|
||
const title = `====== [AI PRO] 单据 ${targetSn} 审核清单核验进度 (适用场景: [${run.scope || "未关联"}] | 总数: ${findings.length} | 待核验: ${pendingCount} | 决策: ${run.decision}) ======\n`;
|
||
|
||
return { stdout: title + table, stderr: "", exitCode: 0 };
|
||
}
|