337 lines
12 KiB
TypeScript
337 lines
12 KiB
TypeScript
import {
|
||
ApprovalDecision,
|
||
ApprovalFindingRecord,
|
||
ApprovalGatewayBlockedError,
|
||
StrictnessTier,
|
||
isValidFindingCheckStatus,
|
||
} from "./types.js";
|
||
|
||
/**
|
||
* 审批仲裁网关策略配置接口
|
||
*/
|
||
export interface GatewayPolicyConfig {
|
||
/**
|
||
* 弹性违规项允许的最大数量。超过此数量自动转为建议人工复核 (默认: 2)
|
||
*/
|
||
maxElasticViolationsForPassWithNote: number;
|
||
|
||
/**
|
||
* 是否严格禁止在无硬性违规且无弹性违规时执行驳回 (默认: true)
|
||
* 业务铁律:没有硬性违反或弹性违反不能驳回,物理切断恶意/无故驳回分支
|
||
*/
|
||
blockRejectWithoutHardOrElasticFailure: boolean;
|
||
|
||
/**
|
||
* 是否在存在硬性违规时一票否决,物理切断同意分支 (默认: true)
|
||
* 业务铁律:有硬性违反必须驳回(或复核),绝对禁止放行同意
|
||
*/
|
||
blockApproveWithHardFailure: boolean;
|
||
|
||
/**
|
||
* 审批意见的最大字符数限制 (默认: 150)
|
||
*/
|
||
maxOpinionLength: number;
|
||
|
||
/**
|
||
* 可配置的自定义逻辑扩展钩子。
|
||
* 返回具体结果时直接作为最终裁决;返回 void 或 undefined 时继续执行标准裁决矩阵。
|
||
*/
|
||
customHook?: (
|
||
context: GatewayContext,
|
||
analysis: FindingsAnalysis,
|
||
config: GatewayPolicyConfig
|
||
) => GatewayDecisionResult | void;
|
||
}
|
||
|
||
/**
|
||
* 网关默认策略配置
|
||
*/
|
||
export const DEFAULT_GATEWAY_CONFIG: GatewayPolicyConfig = {
|
||
maxElasticViolationsForPassWithNote: 2,
|
||
blockRejectWithoutHardOrElasticFailure: true,
|
||
blockApproveWithHardFailure: true,
|
||
maxOpinionLength: 150,
|
||
};
|
||
|
||
/**
|
||
* 网关裁决输入上下文
|
||
*/
|
||
export interface GatewayContext {
|
||
documentSn: string;
|
||
findings: ApprovalFindingRecord[];
|
||
requestedAction?: "approve" | "reject" | "comment";
|
||
opinion?: string;
|
||
}
|
||
|
||
/**
|
||
* 要点事实核验分析结果
|
||
*/
|
||
export interface FindingsAnalysis {
|
||
allFindings: ApprovalFindingRecord[];
|
||
pendingFindings: ApprovalFindingRecord[];
|
||
passedFindings: ApprovalFindingRecord[];
|
||
failedFindings: ApprovalFindingRecord[];
|
||
skippedFindings: ApprovalFindingRecord[];
|
||
hardFailed: ApprovalFindingRecord[];
|
||
elasticFailed: ApprovalFindingRecord[];
|
||
suggestFailed: ApprovalFindingRecord[];
|
||
specialFailed: ApprovalFindingRecord[];
|
||
}
|
||
|
||
/**
|
||
* 网关综合判定结果
|
||
*/
|
||
export interface GatewayDecisionResult {
|
||
decision: ApprovalDecision;
|
||
decisionSummary: string;
|
||
analysis: FindingsAnalysis;
|
||
hardFailedCount: number;
|
||
elasticFailedCount: number;
|
||
suggestFailedCount: number;
|
||
specialFailedCount: number;
|
||
}
|
||
|
||
/**
|
||
* 分析审核要点状态与严格度分布
|
||
*/
|
||
export function analyzeFindings(findings: ApprovalFindingRecord[]): FindingsAnalysis {
|
||
const pendingFindings: ApprovalFindingRecord[] = [];
|
||
const passedFindings: ApprovalFindingRecord[] = [];
|
||
const failedFindings: ApprovalFindingRecord[] = [];
|
||
const skippedFindings: ApprovalFindingRecord[] = [];
|
||
const hardFailed: ApprovalFindingRecord[] = [];
|
||
const elasticFailed: ApprovalFindingRecord[] = [];
|
||
const suggestFailed: ApprovalFindingRecord[] = [];
|
||
const specialFailed: ApprovalFindingRecord[] = [];
|
||
|
||
for (const f of findings) {
|
||
if (!isValidFindingCheckStatus(f.checkStatus)) throw new ApprovalGatewayBlockedError("审核要点包含非法核验状态,禁止审批决策");
|
||
if (f.checkStatus === "PENDING") {
|
||
pendingFindings.push(f);
|
||
} else if (f.checkStatus === "PASS") {
|
||
passedFindings.push(f);
|
||
} else if (f.checkStatus === "SKIP") {
|
||
skippedFindings.push(f);
|
||
} else if (f.checkStatus === "FAIL") {
|
||
failedFindings.push(f);
|
||
if (f.strictness === "硬性") {
|
||
hardFailed.push(f);
|
||
} else if (f.strictness === "弹性") {
|
||
elasticFailed.push(f);
|
||
} else if (f.strictness === "建议") {
|
||
suggestFailed.push(f);
|
||
} else if (f.strictness === "特殊") {
|
||
specialFailed.push(f);
|
||
}
|
||
}
|
||
}
|
||
|
||
return {
|
||
allFindings: findings,
|
||
pendingFindings,
|
||
passedFindings,
|
||
failedFindings,
|
||
skippedFindings,
|
||
hardFailed,
|
||
elasticFailed,
|
||
suggestFailed,
|
||
specialFailed,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 校验网关拟执行的动作是否合法(前置门禁防御)
|
||
*
|
||
* 包含核心卡点:
|
||
* 1. 空要点清单拦截:未注入要点前物理切断审批链路
|
||
* 2. 审批意见长度校验:超长直接拦截
|
||
* 3. 待核验拦截:存在 PENDING 要点时禁止提前决断
|
||
* 4. 硬性一票否决:有硬性违规绝对禁止同意放行
|
||
* 5. 无因驳回拦截:无硬性违规且无弹性违规时绝对禁止驳回
|
||
*
|
||
* @throws {ApprovalGatewayBlockedError} 当任一门禁被触发时抛出
|
||
*/
|
||
export function validateGatewayAction(
|
||
context: GatewayContext,
|
||
userConfig?: Partial<GatewayPolicyConfig>
|
||
): FindingsAnalysis {
|
||
const config = { ...DEFAULT_GATEWAY_CONFIG, ...userConfig };
|
||
|
||
// ── 门禁 1: 空要点清单拦截 ────────────────────────────────────────────────
|
||
if (!context.findings || context.findings.length === 0) {
|
||
throw new ApprovalGatewayBlockedError(
|
||
`[审批链路阻断] 单据 [${context.documentSn}] 尚未注入任何明细级审核要点(要点列表为空)!\n` +
|
||
`网关卡点生效:审核要点未注入前,审批链路物理切断,绝对禁止进入审批决策!\n` +
|
||
`请先执行: hose-aipro bind 挂载审核要点清单。`
|
||
);
|
||
}
|
||
|
||
// ── 门禁 2: 审批意见长度边角检查 ──────────────────────────────────────────
|
||
if (context.opinion && context.opinion.length > config.maxOpinionLength) {
|
||
throw new ApprovalGatewayBlockedError(
|
||
`[参数校验阻断] 审批意见不可超过 ${config.maxOpinionLength} 字符,当前长度为 ${context.opinion.length} 字符,请精简。`
|
||
);
|
||
}
|
||
|
||
// ── 门禁 3: 漏检拦截(存在待核验项)───────────────────────────────────────
|
||
const analysis = analyzeFindings(context.findings);
|
||
if (analysis.pendingFindings.length > 0) {
|
||
const pendingIndices = analysis.pendingFindings
|
||
.map((f) => `#${context.findings.indexOf(f) + 1} [${f.strictness}] ${f.clauseText}`)
|
||
.join(";");
|
||
throw new ApprovalGatewayBlockedError(
|
||
`[审批链路阻断] 单据 [${context.documentSn}] 尚有 ${analysis.pendingFindings.length} 处审核要点处于 PENDING 待核验状态!\n` +
|
||
`网关卡点生效:禁止在核验未全部闭环前提前决断。\n` +
|
||
`待核验项: ${pendingIndices}`
|
||
);
|
||
}
|
||
|
||
// ── 门禁 4: 硬性条款刚性底线(有硬性违规时绝对禁止同意)─────────────────
|
||
if (context.requestedAction === "approve" && config.blockApproveWithHardFailure && analysis.hardFailed.length > 0) {
|
||
const violatedNames = analysis.hardFailed
|
||
.map((f) => `【#${context.findings.indexOf(f) + 1} ${f.clauseText}】`)
|
||
.join("、");
|
||
throw new ApprovalGatewayBlockedError(
|
||
`[网关强力阻断] 单据 [${context.documentSn}] 存在硬性违规条款未通过(${violatedNames})!\n` +
|
||
`网关物理切断“同意”流转,绝对禁止放行通过!当前只允许执行驳回 (reject) 或建议复核。`
|
||
);
|
||
}
|
||
|
||
// ── 门禁 5: 无因驳回拦截(没有硬性或弹性违规不能驳回)────────────────────
|
||
if (
|
||
context.requestedAction === "reject" &&
|
||
config.blockRejectWithoutHardOrElasticFailure &&
|
||
analysis.hardFailed.length === 0 &&
|
||
analysis.elasticFailed.length === 0
|
||
) {
|
||
let detail = "单据所有核验要点均已通过";
|
||
if (analysis.suggestFailed.length > 0) {
|
||
const suggestNames = analysis.suggestFailed
|
||
.map((f) => `【#${context.findings.indexOf(f) + 1} ${f.clauseText}】`)
|
||
.join("、");
|
||
detail = `单据仅存在倡导性的【建议】项未满足(${suggestNames}),建议项不构成驳回理由`;
|
||
} else if (analysis.specialFailed.length > 0) {
|
||
const specialNames = analysis.specialFailed
|
||
.map((f) => `【#${context.findings.indexOf(f) + 1} ${f.clauseText}】`)
|
||
.join("、");
|
||
detail = `单据命中【特殊】条款(${specialNames}),按规则须提交人工复核,不能直接驳回`;
|
||
}
|
||
|
||
throw new ApprovalGatewayBlockedError(
|
||
`[网关安全阻断] 单据 [${context.documentSn}] 不存在任何【硬性】或【弹性】违规条款(${detail})!\n` +
|
||
`网关物理切断驳回分支,严禁无故或恶意驳回!当前只允许执行同意 (approve) 或转人工复核。`
|
||
);
|
||
}
|
||
|
||
return analysis;
|
||
}
|
||
|
||
/**
|
||
* 确定性网关仲裁:执行所有校验并计算最终决策结果与审查报告
|
||
*/
|
||
export function evaluateGatewayPolicy(
|
||
context: GatewayContext,
|
||
userConfig?: Partial<GatewayPolicyConfig>
|
||
): GatewayDecisionResult {
|
||
const config = { ...DEFAULT_GATEWAY_CONFIG, ...userConfig };
|
||
|
||
// 1. 前置动作合法性校验(拦截非法同意、非法驳回、未闭环核验等)
|
||
const analysis = validateGatewayAction(context, config);
|
||
|
||
// 2. 优先执行用户自定义逻辑钩子
|
||
if (config.customHook) {
|
||
const customResult = config.customHook(context, analysis, config);
|
||
if (customResult) {
|
||
return customResult;
|
||
}
|
||
}
|
||
|
||
// 3. 执行标准确定性裁决矩阵
|
||
let decision: ApprovalDecision;
|
||
if (analysis.hardFailed.length > 0) {
|
||
decision = "REJECT";
|
||
} else if (analysis.specialFailed.length > 0) {
|
||
decision = "REVIEW";
|
||
} else if (analysis.elasticFailed.length > 0) {
|
||
decision =
|
||
analysis.elasticFailed.length > config.maxElasticViolationsForPassWithNote
|
||
? "REVIEW"
|
||
: "PASS_WITH_NOTE";
|
||
} else if (analysis.suggestFailed.length > 0) {
|
||
decision = "PASS_WITH_NOTE";
|
||
} else {
|
||
decision = "APPROVE";
|
||
}
|
||
|
||
// 4. 组装格式化合规审查报告
|
||
const summaryLines: string[] = [];
|
||
summaryLines.push(`【合思 AI 确定性网关审查报告】`);
|
||
summaryLines.push(`单号: ${context.documentSn} | 审核要点总数: ${context.findings.length} 项`);
|
||
summaryLines.push(``);
|
||
summaryLines.push(`[逐项核验事实]`);
|
||
context.findings.forEach((f, idx) => {
|
||
const icon = f.checkStatus === "PASS" ? "✅" : f.checkStatus === "FAIL" ? "❌" : "➖";
|
||
const reason = f.evidence || f.reasoning ? ` (${[f.evidence, f.reasoning].filter(Boolean).join(" - ")})` : "";
|
||
summaryLines.push(` ${icon} #${idx + 1} [${f.strictness}] ${f.clauseText}${reason}`);
|
||
});
|
||
summaryLines.push(``);
|
||
|
||
if (decision === "REJECT") {
|
||
summaryLines.push(`[网关结论] ❌ 触犯硬性刚性条款,决策锁定为【驳回】`);
|
||
} else if (decision === "REVIEW") {
|
||
summaryLines.push(`[网关结论] ⚠️ 触发特殊或弹性超标条款,决策锁定为【建议人工复核】`);
|
||
} else if (decision === "PASS_WITH_NOTE") {
|
||
summaryLines.push(`[网关结论] 💡 满足核心要求,包含弹性/建议提示,决策锁定为【备注通过】`);
|
||
} else {
|
||
summaryLines.push(`[网关结论] ✅ 所有规则判定全部通过,决策锁定为【同意通过】`);
|
||
}
|
||
|
||
if (context.opinion) {
|
||
summaryLines.push(`[审批附加说明] ${context.opinion}`);
|
||
}
|
||
|
||
const decisionSummary = summaryLines.join("\n");
|
||
|
||
return {
|
||
decision,
|
||
decisionSummary,
|
||
analysis,
|
||
hardFailedCount: analysis.hardFailed.length,
|
||
elasticFailedCount: analysis.elasticFailed.length,
|
||
suggestFailedCount: analysis.suggestFailed.length,
|
||
specialFailedCount: analysis.specialFailed.length,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 可配置的审批仲裁网关策略类
|
||
*/
|
||
export class ApprovalGatewayPolicy {
|
||
private config: GatewayPolicyConfig;
|
||
|
||
constructor(config?: Partial<GatewayPolicyConfig>) {
|
||
this.config = { ...DEFAULT_GATEWAY_CONFIG, ...config };
|
||
}
|
||
|
||
public getConfig(): GatewayPolicyConfig {
|
||
return { ...this.config };
|
||
}
|
||
|
||
public updateConfig(updates: Partial<GatewayPolicyConfig>): void {
|
||
this.config = { ...this.config, ...updates };
|
||
}
|
||
|
||
public validate(context: GatewayContext): FindingsAnalysis {
|
||
return validateGatewayAction(context, this.config);
|
||
}
|
||
|
||
public evaluate(context: GatewayContext): GatewayDecisionResult {
|
||
return evaluateGatewayPolicy(context, this.config);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 全局默认网关单例
|
||
*/
|
||
export const defaultGatewayPolicy = new ApprovalGatewayPolicy();
|