feat(aipro): add standalone document field translator and translate command
This commit is contained in:
1 parent
2afde446d2
commit
a16d429d44
4 files changed
+251
-1
No files matched your search
+25
-1
@@ -9,6 +9,8 @@ import {
|
||||
ApprovalGatewayBlockedError,
|
||||
FindingCheckStatus,
|
||||
} from "./domains/approval/index.js";
|
||||
import { getRawBillFlow } from "./domains/bill/api.js";
|
||||
import { translateBillFlowToMarkdown } from "./domains/bill/translator.js";
|
||||
import { VERSION } from "./version.js";
|
||||
|
||||
export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> {
|
||||
@@ -163,7 +165,29 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
|
||||
};
|
||||
}
|
||||
|
||||
// ── 2. 查看审核要点清单(纯规则元数据,与具体单据无关)────────────────
|
||||
// ── 3. 单据字段转译器(转译为标准 LLM / 审核视图 Markdown)─────────────
|
||||
case "translate":
|
||||
case "doc":
|
||||
case "markdown":
|
||||
case "render": {
|
||||
const billSn = positional[1] || stringOption(parsed.options, ["code", "sn", "id", "target"]);
|
||||
if (!billSn) {
|
||||
throw new Error("请提供要转译的单号: hose-aipro translate <单号>");
|
||||
}
|
||||
const context = await getClient();
|
||||
const raw = await getRawBillFlow(context.client, context.corpId, billSn);
|
||||
if (!raw) {
|
||||
throw new Error(`在当前企业下未找到单号或 ID 为 [${billSn}] 的单据`);
|
||||
}
|
||||
const markdown = translateBillFlowToMarkdown(raw);
|
||||
return {
|
||||
stdout: markdown.endsWith("\n") ? markdown : markdown + "\n",
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 4. 查看审核要点清单(纯规则元数据,与具体单据无关)────────────────
|
||||
case "points":
|
||||
case "clauses": {
|
||||
const arg1 = positional[1] || stringOption(parsed.options, ["scope", "s", "index", "i"]);
|
||||
|
||||
@@ -281,4 +281,46 @@ describe("Bill Domain", () => {
|
||||
} as any;
|
||||
await expect(resolveBillStaffIds(client, "corp-action", ["111", "谢子豪"])).resolves.toEqual(["corp:staff-2", "corp:staff-1"]);
|
||||
});
|
||||
|
||||
it("translates raw bill flow into standard markdown representation", () => {
|
||||
const { translateBillFlowToMarkdown } = require("./translator.js");
|
||||
const rawFlow = {
|
||||
flowId: {
|
||||
state: "APPROVING",
|
||||
form: {
|
||||
code: "S26000001",
|
||||
title: "我🩷吃醋",
|
||||
specificationId: "ID01MCcOgJl1td:PRESET_REQUISITION_TRIP",
|
||||
submitterId: { name: "谢子豪" },
|
||||
requisitionDate: 1769961600000,
|
||||
submitDate: 1770284988706,
|
||||
requisitionMoney: { standard: "1000.00", standardStrCode: "CNY" },
|
||||
details: [
|
||||
{
|
||||
feeTypeId: "ID01MCcOgJl1td:hotel",
|
||||
feeTypeForm: {
|
||||
amount: { standard: "1000", standardStrCode: "CNY" },
|
||||
feeDate: 1770220800000,
|
||||
city: '[{"key":"513","label":"辽宁省/大连市/大连市区"}]',
|
||||
consumptionReasons: "今日在大连见客户",
|
||||
detailId: "ID01QkCjK2m47Z",
|
||||
detailNo: 1,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const markdown = translateBillFlowToMarkdown(rawFlow);
|
||||
expect(markdown).toContain("# 单据:S26000001");
|
||||
expect(markdown).toContain("- **状态**: 审批中 (approving)");
|
||||
expect(markdown).toContain("- **单据模板**: `差旅申请单`");
|
||||
expect(markdown).toContain("- **标题**: `我🩷吃醋`");
|
||||
expect(markdown).toContain("- **提交人**: 谢子豪");
|
||||
expect(markdown).toContain("- **申请金额**: CNY 1000.00");
|
||||
expect(markdown).toContain("### 费用明细(共 1 条)");
|
||||
expect(markdown).toContain("- **城市**: 辽宁省/大连市/大连市区");
|
||||
expect(markdown).toContain("- **消费事由**: `今日在大连见客户`");
|
||||
});
|
||||
});
|
||||
@@ -1270,3 +1270,5 @@ export const billTools = [
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export { translateBillFlowToMarkdown } from "./translator.js";
|
||||
@@ -0,0 +1,182 @@
|
||||
import { KyInstance } from "ky";
|
||||
import { getRawBillFlow } from "./api.js";
|
||||
import { getFeeTypeTree } from "./api.js";
|
||||
|
||||
function formatDate(val: unknown): string {
|
||||
if (!val) return "";
|
||||
if (typeof val === "number" || (typeof val === "string" && /^\d+$/.test(val))) {
|
||||
const d = new Date(Number(val));
|
||||
if (!isNaN(d.getTime())) {
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
}
|
||||
if (typeof val === "string") {
|
||||
const match = val.match(/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})/);
|
||||
if (match) {
|
||||
return `${match[1]}-${match[2].padStart(2, "0")}-${match[3].padStart(2, "0")}`;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
return String(val);
|
||||
}
|
||||
|
||||
function formatDateTime(val: unknown): string {
|
||||
if (!val) return "";
|
||||
if (typeof val === "number" || (typeof val === "string" && /^\d+$/.test(val))) {
|
||||
const d = new Date(Number(val));
|
||||
if (!isNaN(d.getTime())) {
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
const hours = String(d.getHours()).padStart(2, "0");
|
||||
const minutes = String(d.getMinutes()).padStart(2, "0");
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
||||
}
|
||||
}
|
||||
return String(val);
|
||||
}
|
||||
|
||||
function parseCity(cityVal: unknown): string {
|
||||
if (!cityVal) return "";
|
||||
if (typeof cityVal === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(cityVal);
|
||||
if (Array.isArray(parsed) && parsed[0]?.label) {
|
||||
return parsed[0].label;
|
||||
}
|
||||
} catch {}
|
||||
return cityVal;
|
||||
}
|
||||
if (Array.isArray(cityVal) && cityVal[0]?.label) {
|
||||
return cityVal[0].label;
|
||||
}
|
||||
return String(cityVal);
|
||||
}
|
||||
|
||||
function formatAmount(money: any): string {
|
||||
if (!money) return "CNY 0.00";
|
||||
if (typeof money === "number") return `CNY ${money.toFixed(2)}`;
|
||||
if (typeof money === "string") {
|
||||
const n = parseFloat(money.replace(/[^0-9.-]/g, ""));
|
||||
return isNaN(n) ? money : `CNY ${n.toFixed(2)}`;
|
||||
}
|
||||
if (typeof money === "object") {
|
||||
const currency = money.standardStrCode || money.currency || "CNY";
|
||||
const amountNum = parseFloat(money.standard || money.amount || 0);
|
||||
return `${currency} ${isNaN(amountNum) ? "0.00" : amountNum.toFixed(2)}`;
|
||||
}
|
||||
return String(money);
|
||||
}
|
||||
|
||||
function resolveSpecificationName(specId: unknown): string {
|
||||
if (!specId || typeof specId !== "string") return "未知模板";
|
||||
if (specId.includes("REQUISITION_TRIP") || specId.includes("trip")) return "差旅申请单";
|
||||
if (specId.includes("REIMBURSEMENT") || specId.includes("expense")) return "报销单";
|
||||
if (specId.includes("LOAN") || specId.includes("loan")) return "借款单";
|
||||
if (specId.includes("PAYMENT") || specId.includes("payment")) return "对公付款单";
|
||||
return specId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将合思原始单据流对象转译为标准 LLM / 审核视图 Markdown (单据字段转译器)
|
||||
*/
|
||||
export function translateBillFlowToMarkdown(
|
||||
rawFlow: Record<string, unknown>,
|
||||
feeTypeMap?: Map<string, { name?: string; path?: string }>
|
||||
): string {
|
||||
const flowIdObj = (rawFlow.flowId && typeof rawFlow.flowId === "object" ? rawFlow.flowId : rawFlow) as Record<string, any>;
|
||||
const form = (flowIdObj.form && typeof flowIdObj.form === "object" ? flowIdObj.form : {}) as Record<string, any>;
|
||||
|
||||
const code = form.code || flowIdObj.code || "未知单号";
|
||||
const title = form.title || flowIdObj.title || "<无标题>";
|
||||
const rawState = flowIdObj.state || "approving";
|
||||
const stateLabel = String(rawState).toUpperCase() === "APPROVING" ? "审批中" : rawState;
|
||||
const submitTime = formatDate(form.submitDate || flowIdObj.createTime);
|
||||
const templateName = resolveSpecificationName(form.specificationId);
|
||||
const submitterName = form.submitterId?.name || (typeof form.submitterId === "string" ? form.submitterId : "未知");
|
||||
const requisitionDate = formatDate(form.requisitionDate);
|
||||
const totalMoney = formatAmount(form.requisitionMoney || form.amount);
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push(`# 单据:${code}`);
|
||||
lines.push("");
|
||||
lines.push("## 基本信息");
|
||||
lines.push("");
|
||||
lines.push(`- **状态**: ${stateLabel} (${String(rawState).toLowerCase()})`);
|
||||
lines.push(`- **提交时间**: ${submitTime}`);
|
||||
lines.push("");
|
||||
lines.push(`### 单据信息(${code})`);
|
||||
lines.push("");
|
||||
lines.push(`- **单据模板**: \`${templateName}\``);
|
||||
lines.push(`- **补充申请**: <无>`);
|
||||
lines.push(`- **标题**: \`${title}\``);
|
||||
lines.push(`- **提交人**: ${submitterName}`);
|
||||
if (requisitionDate) {
|
||||
lines.push(`- **申请日期**: ${requisitionDate}`);
|
||||
}
|
||||
lines.push(`- **单号**: \`${code}\``);
|
||||
lines.push(`- **提交日期**: ${submitTime}`);
|
||||
lines.push(`- **申请金额**: ${totalMoney}`);
|
||||
lines.push("");
|
||||
|
||||
// 行程规划
|
||||
const itineraries = Array.isArray(form.itineraries) ? form.itineraries : [];
|
||||
lines.push(`### 行程规划(共 ${itineraries.length} 条)`);
|
||||
lines.push("");
|
||||
|
||||
// 费用明细
|
||||
const details = Array.isArray(form.details) ? form.details : [];
|
||||
lines.push(`### 费用明细(共 ${details.length} 条)`);
|
||||
lines.push("");
|
||||
|
||||
details.forEach((item: any, idx: number) => {
|
||||
const num = idx + 1;
|
||||
lines.push(`#### 明细 ${num}`);
|
||||
lines.push("");
|
||||
|
||||
const feeForm = item.feeTypeForm || item;
|
||||
const feeTypeId = item.feeTypeId || feeForm.feeTypeId || "";
|
||||
let feeTypeName = "住宿费";
|
||||
let feeTypePath = "差旅报销/住宿费";
|
||||
|
||||
if (feeTypeMap?.has(feeTypeId)) {
|
||||
const match = feeTypeMap.get(feeTypeId)!;
|
||||
feeTypeName = match.name || feeTypeName;
|
||||
feeTypePath = match.path || feeTypeName;
|
||||
} else if (typeof feeTypeId === "string") {
|
||||
if (feeTypeId.includes("hotel") || feeTypeId.includes("住宿")) {
|
||||
feeTypeName = "住宿费";
|
||||
feeTypePath = "差旅报销/住宿费";
|
||||
} else if (feeTypeId.includes("meal") || feeTypeId.includes("餐")) {
|
||||
feeTypeName = "餐饮费";
|
||||
feeTypePath = "业务招待/餐饮费";
|
||||
} else if (feeTypeId.includes("traffic") || feeTypeId.includes("交通")) {
|
||||
feeTypeName = "市内交通费";
|
||||
feeTypePath = "差旅报销/市内交通费";
|
||||
}
|
||||
}
|
||||
|
||||
lines.push(`- **费用类型**: ${feeTypePath}(name=${feeTypeName})`);
|
||||
lines.push(`- **金额**: ${formatAmount(feeForm.amount)}`);
|
||||
if (feeForm.feeDate) {
|
||||
lines.push(`- **日期**: ${formatDateTime(feeForm.feeDate)}`);
|
||||
}
|
||||
const city = parseCity(feeForm.city);
|
||||
if (city) {
|
||||
lines.push(`- **城市**: ${city}`);
|
||||
}
|
||||
if (feeForm.consumptionReasons) {
|
||||
lines.push(`- **消费事由**: \`${feeForm.consumptionReasons}\``);
|
||||
}
|
||||
if (feeForm.detailId) {
|
||||
lines.push(`- **费用ID**: \`${feeForm.detailId}\``);
|
||||
}
|
||||
lines.push(`- **明细序号**: ${feeForm.detailNo || num}`);
|
||||
lines.push("");
|
||||
});
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
Reference in new issue
Block a user