From 62025cb6f1dee3a0e87d948a771619d0741c92f1 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Sun, 6 Sep 2026 22:36:58 +0800 Subject: [PATCH] feat(aipro): add scopes index command and decouple points from bills --- src/aipro-cli.ts | 228 +++++++++++++++++++++++++---- src/domains/approval/db.ts | 6 + src/domains/approval/repository.ts | 10 +- src/domains/approval/service.ts | 1 + src/domains/approval/types.ts | 1 + 5 files changed, 217 insertions(+), 29 deletions(-) diff --git a/src/aipro-cli.ts b/src/aipro-cli.ts index 9b924fd..5709458 100644 --- a/src/aipro-cli.ts +++ b/src/aipro-cli.ts @@ -66,27 +66,200 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; try { switch (command) { - // ── 1. 明细级规则注入(出审核要点清单)──────────────────────────────── + // ── 1. 规约场景索引列表查看 ───────────────────────────────────────── + case "scopes": + case "scope": + case "rules": + case "rule": + case "index": + case "indexes": { + const scopes = repo.listScopes(); + if (runtime.jsonMode) { + return { + stdout: printJson({ count: scopes.length, scopes }), + stderr: "", + exitCode: 0, + }; + } + const headers = ["索引号", "适用场景 (Scope)", "有效规则数", "包含规则编号"]; + const rows = scopes.map((s) => [ + s.index, + s.scope, + s.ruleCount, + s.ruleCodes.map((c) => `#${c}`).join(", "), + ]); + const table = printTable(headers, rows); + const title = `====== [AI PRO] 规约场景索引列表 (共 ${scopes.length} 个适用场景) ======\n`; + const tip = + `\n提示:\n` + + ` • 查看场景审核要点 (纯规则元数据,与单据无关): hose-aipro points <索引号|场景名称> (例如: hose-aipro points 1)\n` + + ` • 绑定单据发起审核: hose-aipro bind <单号> <索引号> (例如: hose-aipro bind S26000001 1)\n`; + return { stdout: title + table + tip, stderr: "", exitCode: 0 }; + } + + // ── 2. 查看审核要点清单(纯规则元数据,与具体单据无关)──────────────── case "points": - case "inject": { - if (!targetSn) { - throw new Error("请提供单号: hose-aipro points <单号> [--scope <1|场景>]"); + case "clauses": { + 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 }; + } + + // ── 3. 绑定单据与规则场景(激活审批运行实例)───────────────────────── + case "bind": + case "mount": + case "init": { + const billSn = positional[1] || stringOption(parsed.options, ["code", "sn", "id", "target"]); + const scopeTarget = positional[2] || stringOption(parsed.options, ["scope", "s", "index", "i"]); + + if (!billSn) { + throw new Error("请提供单号: hose-aipro bind <单号> [索引号|场景名称]"); } - const scopeOption = stringOption(parsed.options, ["scope", "s"]); const context = await getClient(); - const result = await service.injectAuditPoints(context.client, context.corpId, targetSn, { - scope: scopeOption ? (isNaN(Number(scopeOption)) ? scopeOption : Number(scopeOption)) : undefined, + const result = await service.injectAuditPoints(context.client, context.corpId, billSn, { + scope: scopeTarget ? (isNaN(Number(scopeTarget)) ? scopeTarget : Number(scopeTarget)) : undefined, }); repo.logAiProAction({ - action: "points", + action: "bind", documentSn: result.bill.billCode, operator, args: JSON.stringify(rawArgs), status: "SUCCESS", decision: "PENDING", - summary: `注入审核要点成功,共 ${result.findings.length} 条要点(场景: ${result.scope})`, + summary: `绑定单据并挂载审核要点成功,共 ${result.findings.length} 条要点(场景: ${result.scope})`, }); if (runtime.jsonMode) { @@ -113,7 +286,7 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; ]); const table = printTable(headers, rows); - const title = `====== [AI PRO] 单据 ${result.bill.billCode} 审核要点清单 (场景: ${result.scope} | 共 ${result.findings.length} 条要点) ======\n`; + 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` + @@ -122,7 +295,7 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; return { stdout: title + table + tip, stderr: "", exitCode: 0 }; } - // ── 2. 查看审核要点清单与进度(未注入则硬阻断)──────────────────────── + // ── 4. 查看审核要点清单与进度(未绑定则硬阻断)──────────────────────── case "checklist": case "list": case "status": { @@ -139,13 +312,13 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; operator, args: JSON.stringify(rawArgs), status: "BLOCKED", - summary: "查询审核清单阻断:单据尚未注入任何审核要点", + summary: "查询审核清单阻断:单据尚未绑定任何审核要点", }); const errorMsg = - `[审批链路阻断] 单据 [${targetSn}] 尚未注入任何明细级审核要点(要点列表为空)!\n` + - `网关安全卡点生效:在要点未注入前,审批链路物理不通!\n` + - `请先执行: hose-aipro points ${targetSn} 注入明细级审核要点清单。\n`; + `[审批链路阻断] 单据 [${targetSn}] 尚未绑定任何明细级审核要点(要点列表为空)!\n` + + `网关安全卡点生效:在要点未绑定前,审批链路物理不通!\n` + + `请先执行: hose-aipro bind ${targetSn} <索引号> 挂载明细级审核要点清单。\n`; return { stdout: "", stderr: errorMsg, exitCode: 1 }; } @@ -169,7 +342,7 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; const table = printTable(headers, rows); const pendingCount = findings.filter((f) => f.checkStatus === "PENDING").length; - const title = `====== [AI PRO] 单据 ${targetSn} 审核清单核验进度 (总数: ${findings.length} | 待核验: ${pendingCount} | 决策: ${run.decision}) ======\n`; + const title = `====== [AI PRO] 单据 ${targetSn} 审核清单核验进度 (适用场景: [${run.scope || "未关联"}] | 总数: ${findings.length} | 待核验: ${pendingCount} | 决策: ${run.decision}) ======\n`; return { stdout: title + table, stderr: "", exitCode: 0 }; } @@ -382,25 +555,28 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; 一切操作在本地 SQLite 强受控、全流程监控、不可篡改审计留痕。 核心工作流: - 1. 规则注入 (出明细级审核要点清单): - hose-aipro points <单号> [--scope <索引号|场景>] + 1. 查看规约场景索引库 (获取规则索引号): + hose-aipro scopes # 列出所有场景索引 (Index / Scope / 规则数) - 2. 查看审核要点清单与核验进度 (未注入则硬阻断): - hose-aipro checklist <单号> + 2. 查看场景审核要点清单 (纯规则元数据,与单据无关): + hose-aipro points <索引号|场景名称> # 例如: hose-aipro points 1 - 3. 逐项录入事实核验打标: + 3. 显式绑定单据并挂载审核要点: + hose-aipro bind <单号> <索引号> # 例如: hose-aipro bind S26000001 1 + + 4. 查看单据审核清单与核验进度: + hose-aipro checklist <单号> # 查看当前单据要点核验进度与挂载场景 + + 5. 逐项录入事实核验打标: hose-aipro review <单号> --eval "1=PASS; 2=FAIL:证据说明; 3=SKIP" hose-aipro review <单号> --point 1 --status FAIL --reason "缺少发票" - 4. 确定性网关仲裁与流转执行 (硬性条款物理切断同意分支): + 6. 确定性网关仲裁与流转执行 (硬性条款物理切断同意分支): hose-aipro decide <单号> # 预审仿真 (Dry Run) hose-aipro decide <单号> --apply --yes # 真实驱动下游审批/驳回/评论 - 5. 全流程监控日志流水查看: + 7. 全流程监控日志流水查看: hose-aipro logs [单号] # 追溯所有 AI PRO 操作流水 - - 6. 查看审批规约场景: - hose-aipro rules # 查看所有场景与规则分布 `; return { stdout: helpText.trim() + "\n", stderr: "", exitCode: 0 }; } diff --git a/src/domains/approval/db.ts b/src/domains/approval/db.ts index d005982..16a1822 100644 --- a/src/domains/approval/db.ts +++ b/src/domains/approval/db.ts @@ -108,6 +108,7 @@ export function initDatabaseSchema(db: Database): void { flow_id TEXT, document_sn TEXT NOT NULL, corp_id TEXT, + scope TEXT, decision TEXT NOT NULL, decision_summary TEXT, matched_rules_count INTEGER NOT NULL DEFAULT 0, @@ -117,6 +118,11 @@ export function initDatabaseSchema(db: Database): void { updated_at INTEGER NOT NULL ); `); + try { + db.run("ALTER TABLE approval_runs ADD COLUMN scope TEXT;"); + } catch { + // column already exists + } db.run("CREATE INDEX IF NOT EXISTS idx_runs_document_sn ON approval_runs(document_sn);"); db.run("CREATE INDEX IF NOT EXISTS idx_runs_created_at ON approval_runs(created_at);"); diff --git a/src/domains/approval/repository.ts b/src/domains/approval/repository.ts index 7cfd844..ce51295 100644 --- a/src/domains/approval/repository.ts +++ b/src/domains/approval/repository.ts @@ -296,12 +296,13 @@ export class ApprovalRepository { this.db.run( ` UPDATE approval_runs - SET flow_id = ?, corp_id = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ? + SET flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ? WHERE id = ? `, [ (input.flowId !== undefined ? input.flowId : existing.flowId) ?? null, (input.corpId !== undefined ? input.corpId : existing.corpId) ?? null, + (input.scope !== undefined ? input.scope : existing.scope) ?? null, input.decision ?? "PENDING", (input.decisionSummary !== undefined ? input.decisionSummary : null) ?? null, input.matchedRulesCount ?? 0, @@ -318,14 +319,15 @@ export class ApprovalRepository { this.db.run( ` INSERT INTO approval_runs ( - id, flow_id, document_sn, corp_id, decision, decision_summary, matched_rules_count, failed_rules_count, document_snapshot, created_at, updated_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + id, flow_id, document_sn, corp_id, scope, decision, decision_summary, matched_rules_count, failed_rules_count, document_snapshot, created_at, updated_at + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, input.flowId ?? null, input.documentSn.trim(), input.corpId ?? null, + input.scope ?? null, input.decision ?? "PENDING", input.decisionSummary ?? null, input.matchedRulesCount ?? 0, @@ -515,6 +517,7 @@ export class ApprovalRepository { flowId: row.flow_id, documentSn: row.document_sn, corpId: row.corp_id, + scope: row.scope ?? null, decision: row.decision as ApprovalDecision, decisionSummary: row.decision_summary, matchedRulesCount: Number(row.matched_rules_count || 0), @@ -611,6 +614,7 @@ export interface CreateApprovalRunInput { documentSn: string; flowId?: string | null; corpId?: string | null; + scope?: string | null; decision?: ApprovalDecision; decisionSummary?: string | null; matchedRulesCount?: number; diff --git a/src/domains/approval/service.ts b/src/domains/approval/service.ts index e9348b1..60ce21c 100644 --- a/src/domains/approval/service.ts +++ b/src/domains/approval/service.ts @@ -118,6 +118,7 @@ export class ApprovalService { documentSn: actualSn, flowId: bill.billId, corpId, + scope: scopeDetail.scope, decision: "PENDING", matchedRulesCount: clauseInputs.length, failedRulesCount: 0, diff --git a/src/domains/approval/types.ts b/src/domains/approval/types.ts index e406592..3976aea 100644 --- a/src/domains/approval/types.ts +++ b/src/domains/approval/types.ts @@ -80,6 +80,7 @@ export interface ApprovalRunRecord { flowId?: string | null; documentSn: string; corpId?: string | null; + scope?: string | null; decision: ApprovalDecision; decisionSummary?: string | null; matchedRulesCount: number;