207 lines
7.6 KiB
TypeScript
207 lines
7.6 KiB
TypeScript
import type { ApprovalRunRecord } from "../../../domains/approval/index.js";
|
|
import { VERSION } from "../../../version.js";
|
|
import { booleanOption, numberOption, stringOption } from "../../parser.js";
|
|
import { formatExpiresAt, printJson, printTable } from "../../printer.js";
|
|
import type { AiProCommandResult, AiProExecutionContext } from "./types.js";
|
|
|
|
export async function handleAiProStatus(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
|
const { repo, corp, corpId, operator, runtime, parsed } = ctx;
|
|
const scopes = repo.listScopes("active");
|
|
const rules = repo.listRules();
|
|
const runs = corpId ? repo.listRuns({ corpId }) : repo.listRuns();
|
|
const pendingRuns = runs.filter((r) => r.decision === "PENDING");
|
|
const recentLogs = repo.listAiProLogs(undefined, 5);
|
|
const isJson = runtime.jsonMode || booleanOption(parsed.options, ["json", "j"]);
|
|
|
|
const corpStaffId = corp?.credentials?.staffId || corp?.staffId || "-";
|
|
const corpStaffName = corp?.staffName || corp?.session?.staffName || null;
|
|
const corpExpiresAt = corp?.auth?.expiresAt || corp?.session?.expTime;
|
|
|
|
if (isJson) {
|
|
return {
|
|
stdout:
|
|
JSON.stringify(
|
|
{
|
|
tool: "hose-aipro",
|
|
version: VERSION,
|
|
corp: corp
|
|
? {
|
|
id: corp.id,
|
|
name: corp.name,
|
|
staffId: corpStaffId,
|
|
userName: corpStaffName,
|
|
source: corp.source,
|
|
expiresAt: corpExpiresAt,
|
|
expired: corpExpiresAt ? corpExpiresAt <= Date.now() : false,
|
|
baseUrl: corp.baseUrl,
|
|
}
|
|
: null,
|
|
operator,
|
|
ruleEngine: {
|
|
scopesCount: scopes.length,
|
|
rulesCount: rules.length,
|
|
scopes: scopes.map((s) => ({
|
|
index: s.index,
|
|
scope: s.scope,
|
|
ruleCount: s.ruleCount,
|
|
})),
|
|
},
|
|
sessions: {
|
|
total: runs.length,
|
|
pending: pendingRuns.length,
|
|
},
|
|
recentLogsCount: recentLogs.length,
|
|
},
|
|
null,
|
|
2
|
|
) + "\n",
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const lines = [
|
|
`====== 合思 AI PRO 确定性审批网关运行上下文 (v${VERSION}) ======`,
|
|
` 当前连接企业 : ${corp ? `${corp.name} (${corp.id})` : "⚠️ 未指定企业"}`,
|
|
` 当前操作工号 : ${corpStaffId}`,
|
|
` 操作员标识 : ${operator}`,
|
|
` 凭证有效状态 : ${corp ? formatExpiresAt(corpExpiresAt) : "-"}`,
|
|
` 主站 BaseURL : ${corp?.baseUrl || "-"}`,
|
|
` 规约场景库 : 已加载 ${scopes.length} 个场景 (${scopes.map((s) => `#${s.index} ${s.scope}`).join(", ") || "无"}),共 ${rules.length} 条仲裁规则`,
|
|
` 未结审批会话 : ${pendingRuns.length} 个待决断会话 (当前企业历史总会话: ${runs.length} 个)`,
|
|
"",
|
|
"提示:",
|
|
" - 查看规约场景列表: hose-aipro scopes",
|
|
" - 切换全局工作企业: hose-use corps switch <企业ID>",
|
|
" - 临时指定执行企业: hose-aipro --corp=<企业ID> <子命令>",
|
|
" - 单据审批全流程 : hose-aipro bind <单号> <索引号> -> hose-aipro review -> hose-aipro decide",
|
|
"",
|
|
];
|
|
|
|
return { stdout: lines.join("\n"), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export async function handleAiProLogs(ctx: AiProExecutionContext): Promise<AiProCommandResult> {
|
|
const { parsed, repo, runtime, targetSn } = ctx;
|
|
const runHandle = stringOption(parsed.options, ["run", "r"]);
|
|
const limit = numberOption(parsed.options, ["limit", "n"], 50) ?? 50;
|
|
|
|
let runRecord: ApprovalRunRecord | null = null;
|
|
if (runHandle) {
|
|
runRecord = repo.getRunByHandle(runHandle);
|
|
} else if (targetSn) {
|
|
const maybeRun = repo.getRunByHandle(targetSn);
|
|
if (maybeRun?.shortId && maybeRun.shortId.toUpperCase() === targetSn.toUpperCase()) {
|
|
runRecord = maybeRun;
|
|
}
|
|
}
|
|
|
|
const filter: { runId?: string; documentSn?: string } = {};
|
|
if (runRecord) {
|
|
filter.runId = runRecord.shortId || runRecord.id;
|
|
} else if (runHandle) {
|
|
filter.runId = runHandle;
|
|
}
|
|
if (targetSn && targetSn !== runRecord?.shortId) {
|
|
filter.documentSn = targetSn;
|
|
}
|
|
|
|
const logs = repo.listAiProLogs(filter, limit);
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({ total: logs.length, logs }),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (logs.length === 0) {
|
|
const targetDesc = filter.runId
|
|
? ` (会话: ${filter.runId})`
|
|
: filter.documentSn
|
|
? ` (单号: ${filter.documentSn})`
|
|
: "";
|
|
return {
|
|
stdout: `[AI PRO 监控流水] 暂无任何登记日志记录${targetDesc}。\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const headers = [
|
|
"#",
|
|
"发生时间",
|
|
"会话 ID",
|
|
"动作 (Action)",
|
|
"单号",
|
|
"操作人",
|
|
"状态",
|
|
"网关决策",
|
|
"摘要说明",
|
|
];
|
|
const rows = logs.map((l, i) => [
|
|
i + 1,
|
|
new Date(l.createdAt).toLocaleString("zh-CN"),
|
|
l.runId || "-",
|
|
l.action,
|
|
l.documentSn || "-",
|
|
l.operator || "-",
|
|
l.status === "SUCCESS" ? "✅ SUCCESS" : l.status === "BLOCKED" ? "🚫 BLOCKED" : "❌ ERROR",
|
|
l.decision || "-",
|
|
l.summary || "-",
|
|
]);
|
|
|
|
const table = printTable(headers, rows);
|
|
const title = `====== [AI PRO 全流程监控流水账本] (共登记 ${logs.length} 条记录) ======\n`;
|
|
|
|
return { stdout: title + table, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
export function handleAiProHelp(ctx: AiProExecutionContext): AiProCommandResult {
|
|
const { corp, operator, jsonMode } = ctx;
|
|
const corpExpiresAt = corp?.auth?.expiresAt || corp?.session?.expTime;
|
|
const corpBanner = corp
|
|
? `[当前连接企业: ${corp.name} (${corp.id}) | 操作人: ${operator} | 凭证状态: ${formatExpiresAt(corpExpiresAt)}]\n[全局切换: hose-use corps switch <企业ID> | 临时指定: hose-aipro --corp=<企业ID>]`
|
|
: `[当前企业: 未指定 | 请先运行 hose-use auth login 或带上 --corp=<企业ID>]`;
|
|
|
|
const helpText = `
|
|
合思 AI PRO 确定性审批仲裁网关 CLI (hose-aipro) v${VERSION}
|
|
一切操作在本地 SQLite 强受控、全流程监控、不可篡改审计留痕。
|
|
|
|
${corpBanner}
|
|
|
|
核心工作流:
|
|
0. 查看 AI PRO 运行上下文与当前绑定企业:
|
|
hose-aipro status # 查看当前企业、操作人、规则库统计与待办会话
|
|
|
|
1. 查看规约场景索引库 (获取规则索引号):
|
|
hose-aipro scopes # 列出所有场景索引 (Index / Scope / 规则数)
|
|
|
|
2. 查看场景审核要点清单 (纯规则元数据,与单据无关):
|
|
hose-aipro points <索引号|场景名称> # 例如: hose-aipro points 1
|
|
|
|
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 "缺少发票"
|
|
|
|
6. 确定性网关仲裁与流转执行 (硬性条款物理切断同意分支):
|
|
hose-aipro decide <单号> # 预审仿真 (Dry Run)
|
|
hose-aipro decide <单号> --apply --yes # 真实驱动下游审批/驳回/评论
|
|
|
|
7. 全流程监控日志流水查看:
|
|
hose-aipro logs [单号] # 追溯所有 AI PRO 操作流水
|
|
`;
|
|
return {
|
|
stdout: jsonMode ? printJson({ help: helpText.trim() }) + "\n" : helpText.trim() + "\n",
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|