weaver-work-cli 0.1.50 → 0.1.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/chunks/{attachment-content-EJYIOJPH.js → attachment-content-W3TSJC3Y.js} +3 -3
- package/dist/chunks/{chunk-BFKLQLBK.js → chunk-2J3XY4ED.js} +7 -7
- package/dist/chunks/{chunk-K67FODVC.js → chunk-6GCXWKG6.js} +1 -1
- package/dist/chunks/{chunk-WNJUGI2F.js → chunk-76PRVYH4.js} +1 -1
- package/dist/chunks/{chunk-RYFRGTUV.js → chunk-DEHJ7PMA.js} +4 -4
- package/dist/chunks/{chunk-3NCFFHB2.js → chunk-IF33CG4V.js} +1 -1
- package/dist/chunks/{chunk-YB3AJYPY.js → chunk-PE334WL3.js} +4 -4
- package/dist/chunks/{chunk-MCZG6HPM.js → chunk-QQXMHM3J.js} +2 -2
- package/dist/chunks/{chunk-7ECB22G3.js → chunk-U7ROML5E.js} +23 -23
- package/dist/chunks/{chunk-7AYQJSFY.js → chunk-WTLQGS6D.js} +2 -2
- package/dist/chunks/{chunk-6T53BSWZ.js → chunk-XPQN4E5O.js} +50 -19
- package/dist/chunks/{chunk-HARQF5IP.js → chunk-Z4G7LLDD.js} +25 -25
- package/dist/chunks/{conflict-C4UDHCWV.js → conflict-57WDJWL7.js} +2 -2
- package/dist/chunks/{contract-VUC6JOPG.js → contract-LJRAXVZU.js} +8587 -1421
- package/dist/chunks/{create-flow-6OG2LYBD.js → create-flow-3FMMZBCA.js} +6 -6
- package/dist/chunks/{data-QO7QWOIV.js → data-WK5CQXNF.js} +17 -32
- package/dist/chunks/{env-WP57TF2S.js → env-JYZE3BZB.js} +2 -2
- package/dist/chunks/{factory-VUXI7N5C.js → factory-66SCIKW3.js} +1 -1
- package/dist/chunks/{group-chat-UQRGXILK.js → group-chat-MNRYMLNG.js} +3 -3
- package/dist/chunks/{invoice-E7GOITEU.js → invoice-V7ZTITUO.js} +725 -205
- package/dist/chunks/{meeting-4BBLVAWM.js → meeting-IIY4UAJL.js} +10 -10
- package/dist/chunks/{recruit-stage-FKWS5UQF.js → recruit-stage-XCMVEQA2.js} +718 -131
- package/dist/chunks/{registry-V4CHRQDI.js → registry-CJOVECZD.js} +1 -1
- package/dist/chunks/{registry-5CRCN2TV.js → registry-METZ2LWE.js} +10 -10
- package/dist/chunks/{room-recommend-AYR3UG7R.js → room-recommend-YFY7HTOF.js} +4 -4
- package/dist/chunks/{schedule-JZEFIXUS.js → schedule-6I63TERA.js} +4 -4
- package/dist/chunks/smart-extract-FOUYNIG3.js +2882 -0
- package/dist/chunks/{workflow-3ZAF2J5E.js → workflow-YHXFIU63.js} +1 -1
- package/dist/index.js +11 -6
- package/dist/shortcuts/contract/manifest.js +251 -0
- package/dist/shortcuts/contract/ooxml/docx-table.js +233 -0
- package/dist/shortcuts/contract/operations/card-action.js +1165 -0
- package/dist/shortcuts/contract/operations/card-ebdf-list.js +571 -0
- package/dist/shortcuts/contract/operations/card-plan.js +327 -0
- package/dist/shortcuts/contract/operations/card-risk-report.js +565 -0
- package/dist/shortcuts/contract/operations/card.js +6 -5
- package/dist/shortcuts/contract/operations/docx-fillfromquote.js +532 -0
- package/dist/shortcuts/contract/operations/docx-verify.js +258 -0
- package/dist/shortcuts/contract/operations/elements-build.js +761 -0
- package/dist/shortcuts/contract/operations/elements-spec.js +216 -0
- package/dist/shortcuts/contract/operations/first-screen.js +588 -0
- package/dist/shortcuts/contract/operations/flow.js +81 -25
- package/dist/shortcuts/contract/operations/ledger.js +438 -0
- package/dist/shortcuts/contract/operations/modeltext.js +132 -0
- package/dist/shortcuts/contract/operations/op-resolver.js +22 -0
- package/dist/shortcuts/contract/operations/performance.js +104 -3
- package/dist/shortcuts/contract/operations/quote.js +131 -25
- package/dist/shortcuts/contract/operations/quotescreen.js +529 -0
- package/dist/shortcuts/contract/operations/registry.js +30 -0
- package/dist/shortcuts/contract/operations/review.js +73 -58
- package/dist/shortcuts/contract/operations/summary-validate.js +293 -0
- package/dist/shortcuts/contract/operations/summary.js +4 -2
- package/dist/shortcuts/contract/operations/triage.js +1100 -0
- package/dist/shortcuts/contract/operations/upload.js +48 -7
- package/dist/shortcuts/contract/prewarm-cache.js +90 -0
- package/dist/shortcuts/contract/prewarm.js +88 -0
- package/dist/shortcuts/contract/upload-cache.js +49 -4
- package/dist/shortcuts/data/index.js +1 -6
- package/dist/shortcuts/data/manifest.js +2 -2
- package/dist/shortcuts/data/operations/index.js +1 -1
- package/dist/shortcuts/data/operations/query.js +23 -46
- package/dist/shortcuts/index.js +5 -0
- package/dist/shortcuts/invoice/index.js +0 -12
- package/dist/shortcuts/invoice/manifest.js +200 -49
- package/dist/shortcuts/invoice/operations/registry.js +4 -2
- package/dist/shortcuts/invoice/operations/reim-relation.js +438 -0
- package/dist/shortcuts/invoice/operations/shared.js +3 -19
- package/dist/shortcuts/invoice/operations/upload-and-validate.js +109 -73
- package/dist/shortcuts/recruit-stage/operations/interview-arrange.js +4 -2
- package/dist/shortcuts/recruit-stage/operations/jd-create.js +6 -2
- package/dist/shortcuts/recruit-stage/operations/list.js +217 -1
- package/dist/shortcuts/recruit-stage/operations/resume-import.js +144 -158
- package/dist/shortcuts/recruit-stage/operations/search.js +348 -0
- package/dist/shortcuts/recruit-stage/shared.js +5 -0
- package/dist/shortcuts/recruit-stage/tag-lookup.js +136 -0
- package/dist/shortcuts/smart-extract/constants.js +79 -0
- package/dist/shortcuts/smart-extract/continuation.js +71 -0
- package/dist/shortcuts/smart-extract/errors.js +68 -0
- package/dist/shortcuts/smart-extract/host.js +191 -0
- package/dist/shortcuts/smart-extract/index.js +105 -0
- package/dist/shortcuts/smart-extract/ledger.js +70 -0
- package/dist/shortcuts/smart-extract/manifest.js +35 -0
- package/dist/shortcuts/smart-extract/operations/attachment.js +212 -0
- package/dist/shortcuts/smart-extract/operations/batch.js +67 -0
- package/dist/shortcuts/smart-extract/operations/config.js +118 -0
- package/dist/shortcuts/smart-extract/operations/fill.js +492 -0
- package/dist/shortcuts/smart-extract/operations/preview.js +174 -0
- package/dist/shortcuts/smart-extract/operations/registry.js +34 -0
- package/dist/shortcuts/smart-extract/operations/save.js +355 -0
- package/dist/shortcuts/smart-extract/operations/shared.js +191 -0
- package/dist/shortcuts/smart-extract/operations/structure.js +401 -0
- package/dist/shortcuts/smart-extract/operations/target.js +167 -0
- package/dist/shortcuts/smart-extract/operations/types.js +22 -0
- package/dist/shortcuts/smart-extract/operations/validate.js +454 -0
- package/dist/shortcuts/workflow/operations/form.js +68 -33
- package/dist/shortcuts/workflow/operations/read.js +19 -1
- package/docs/SKILL.md +1 -1
- package/docs/_catalog.md +3 -0
- package/docs/agent-invoice.md +5 -7
- package/docs/data.md +5 -1
- package/docs/fna.md +14 -1
- package/docs/invoice.md +7 -5
- package/docs/smart-extract.md +156 -0
- package/package.json +1 -1
- package/skill-template/business-info.json +16 -1
- package/skill-template/domains/qiyecheng.md +2 -0
- package/skill-template/domains/smart-extract.md +16 -0
- package/skill-template/domains/yepiaotong.md +2 -2
- package/skills/weaver-e10-data/references/scene-and-misc.md +4 -3
- package/skills/weaver-e10-jinchengda/SKILL.md +223 -35
- package/skills/weaver-e10-jinchengda/references/contract-action-card.md +47 -481
- package/skills/weaver-e10-jinchengda/references/contract-action-engine.md +11 -11
- package/skills/weaver-e10-jinchengda/references/contract-agent-entry.md +3 -3
- package/skills/weaver-e10-jinchengda/references/contract-ai-assistant.md +1 -1
- package/skills/weaver-e10-jinchengda/references/contract-ai-review.md +32 -16
- package/skills/weaver-e10-jinchengda/references/contract-card-render-internals.md +478 -0
- package/skills/weaver-e10-jinchengda/references/contract-command-index.md +4 -4
- package/skills/weaver-e10-jinchengda/references/contract-disabled-capabilities.md +2 -2
- package/skills/weaver-e10-jinchengda/references/contract-doc-triage.md +39 -61
- package/skills/weaver-e10-jinchengda/references/contract-element-save-record.md +18 -13
- package/skills/weaver-e10-jinchengda/references/contract-execution-rules.md +2 -2
- package/skills/weaver-e10-jinchengda/references/contract-field-extract-new-customer.md +21 -6
- package/skills/weaver-e10-jinchengda/references/contract-field-extract-other-flows.md +6 -4
- package/skills/weaver-e10-jinchengda/references/contract-flow-launch.md +3 -3
- package/skills/weaver-e10-jinchengda/references/contract-ledger-catalog.md +263 -0
- package/skills/weaver-e10-jinchengda/references/contract-ledger-query-playbook.md +525 -0
- package/skills/weaver-e10-jinchengda/references/contract-ledger-rules.md +36 -0
- package/skills/weaver-e10-jinchengda/references/contract-modeltext-library.md +128 -0
- package/skills/weaver-e10-jinchengda/references/contract-multi-file.md +87 -0
- package/skills/weaver-e10-jinchengda/references/contract-performance-query.md +80 -54
- package/skills/weaver-e10-jinchengda/references/contract-quote-to-contract.md +110 -14
- package/skills/weaver-e10-jinchengda/references/contract-template-selector.md +19 -21
- package/skills/weaver-e10-jinchengda/references/orchestration.md +12 -12
- package/skills/weaver-e10-jinchengda/references/source-manifest.json +1 -6
- package/skills/weaver-e10-jinchengda/scripts/fill_plan.py +2 -1
- package/skills/weaver-e10-qiyecheng/SKILL.md +37 -0
- package/skills/weaver-e10-qiyecheng/references/ai-check-audit.md +14 -0
- package/skills/weaver-e10-qiyecheng/references/budget.md +14 -0
- package/skills/weaver-e10-qiyecheng/references/catalog.md +25 -0
- package/skills/weaver-e10-qiyecheng/references/expense-center.md +8 -0
- package/skills/weaver-e10-qiyecheng/references/expense-chart.md +9 -0
- package/skills/weaver-e10-qiyecheng/references/invoice-writeoff.md +13 -0
- package/skills/weaver-e10-qiyecheng/references/loan-management.md +14 -0
- package/skills/weaver-e10-qiyecheng/references/self-hosted-mode.md +239 -0
- package/skills/weaver-e10-qiyecheng/references/source-manifest.json +34 -27
- package/skills/weaver-e10-recruit-stage/SKILL.md +62 -4
- package/skills/weaver-e10-recruit-stage/agents/openai.yaml +42 -8
- package/skills/weaver-e10-recruit-stage/references/env-ziyong.md +79 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-arrange-interview.md +11 -3
- package/skills/weaver-e10-recruit-stage/references/recruit-edit-interview-feedback.md +3 -2
- package/skills/weaver-e10-recruit-stage/references/recruit-interview-not-attended.md +153 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-jd-create.md +7 -4
- package/skills/weaver-e10-recruit-stage/references/recruit-match-candidates.md +1 -1
- package/skills/weaver-e10-recruit-stage/references/recruit-my-positions.md +66 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-output-links.md +156 -42
- package/skills/weaver-e10-recruit-stage/references/recruit-resume-import-batch.md +122 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-resume-import.md +36 -8
- package/skills/weaver-e10-recruit-stage/references/recruit-search-by-tag.md +157 -0
- package/skills/weaver-e10-recruit-stage/references/recruit-utterance-glossary.md +35 -0
- package/skills/weaver-e10-smart-extract/SKILL.md +180 -0
- package/skills/weaver-e10-smart-extract/references/api-contract.md +71 -0
- package/skills/weaver-e10-smart-extract/references/result-format.md +102 -0
- package/skills/weaver-e10-smart-extract/references/troubleshooting.md +106 -0
- package/skills/weaver-e10-workflow/references/workflow-create.md +6 -5
- package/skills/weaver-e10-workflow/references/workflow-edit.md +1 -1
- package/skills/weaver-e10-workflow/references/workflow-query.md +1 -1
- package/skills/weaver-e10-workflow/references/workflow-view.md +1 -1
- package/skills/weaver-e10-yepiaotong/SKILL.md +46 -33
- package/skills/weaver-e10-yepiaotong/references/invoice-add.md +2 -0
- package/skills/weaver-e10-yepiaotong/references/invoice-file-upload.md +1 -1
- package/skills/weaver-e10-yepiaotong/references/invoice-import.md +19 -2
- package/skills/weaver-e10-yepiaotong/references/invoice-ocr-preview.md +8 -2
- package/skills/weaver-e10-yepiaotong/references/invoice-reim-relation.md +407 -0
- package/skills/weaver-e10-yepiaotong/references/invoice-reim.md +12 -8
- package/skills/weaver-e10-yepiaotong/references/invoice-upload-and-validate.md +97 -39
- package/skills/weaver-e10-yepiaotong/references/reim-api-reference.md +91 -2
- package/skills/weaver-e10-yepiaotong/references/reim-examples.md +1 -1
- package/skills/weaver-e10-yepiaotong/references/reim-form-fill-rules.md +12 -12
- package/skills/weaver-e10-yepiaotong/references/reim-matching-rules.md +8 -4
- package/skills/weaver-e10-yepiaotong/references/reim-phone-subsidy-rules.md +136 -91
- package/skills/weaver-e10-yepiaotong/references/reim-workflow.md +109 -23
- package/skills/weaver-e10-yepiaotong/references/source-manifest.json +110 -43
- package/dist/shortcuts/invoice/operations/validate-preview.js +0 -40
- package/skills/weaver-e10-jinchengda/scripts/plan_actions.py +0 -279
- package/skills/weaver-e10-jinchengda/scripts/render_action_card.py +0 -450
- package/skills/weaver-e10-jinchengda/scripts/render_ebdf_list_card.py +0 -265
- package/skills/weaver-e10-jinchengda/scripts/render_risk_report.py +0 -358
- package/skills/weaver-e10-jinchengda/scripts/validate_action_card.py +0 -588
- package/skills/weaver-e10-jinchengda/scripts/verify_docx_placeholders.py +0 -118
- package/skills/weaver-e10-yepiaotong/references/invoice-validation-preview.md +0 -40
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { contractValidationError } from '../errors.js';
|
|
4
|
+
import { readZip, rewriteZip } from '../ooxml/zip.js';
|
|
5
|
+
import { collectRanges, deleteTableRows, parseTables, paragraphText, setCellText, setParagraphText, } from '../ooxml/docx-table.js';
|
|
6
|
+
import { resolveOperation } from './op-resolver.js';
|
|
7
|
+
/**
|
|
8
|
+
* 报价单数据 → 合同 docx 一体化填充(扫描 → 替换 → 填表 → 删行 → 保存 → 回读校验)。
|
|
9
|
+
*
|
|
10
|
+
* 迁移自 `scripts/fill_contract_from_quote.py`(2026-11-13 第九批下沉,P3)。
|
|
11
|
+
* 前置能力:`ooxml/docx-table.ts` 的 `deleteTableRows()`(按 `<w:tr>` 边界整体切除,
|
|
12
|
+
* 不重建 XML,实测 22 行表压到 9 行后其余表格行数与 0 格行均不变)。
|
|
13
|
+
*
|
|
14
|
+
* 行为与原脚本逐段对齐:
|
|
15
|
+
* - 占位符集合、`¥` 千分位金额格式、标题整段替换与重复折叠;
|
|
16
|
+
* - 五类表格角色按**文本特征**探测(cover / parties / total / detail / payment),不硬编码索引;
|
|
17
|
+
* - 明细表只复用现有行,多余行从后往前删;行数不足时告警 `not_enough_rows`,**不新增行**
|
|
18
|
+
* (新增需重建 `<w:tr>`,会破坏合并单元格);
|
|
19
|
+
* - 保存后复用 `contract.docx.verify` 做回读校验(占位符残留 / 标题次数 / 0 格行)。
|
|
20
|
+
*
|
|
21
|
+
* 已知差异(与脚本一致,非本次引入):含文本框的段落整段替换时,文本框内容会被清空,
|
|
22
|
+
* 因为脚本的 `set_para_text()` 同样是「首个 w:t 写值、其余清空」。
|
|
23
|
+
*/
|
|
24
|
+
const PLACEHOLDER_PATTERNS = [
|
|
25
|
+
'xxx公司',
|
|
26
|
+
'XXX公司',
|
|
27
|
+
'***有限公司',
|
|
28
|
+
'***',
|
|
29
|
+
'年 月 日',
|
|
30
|
+
'年 月 日',
|
|
31
|
+
'【】',
|
|
32
|
+
'____',
|
|
33
|
+
'{{',
|
|
34
|
+
'}}',
|
|
35
|
+
];
|
|
36
|
+
function isRecord(value) {
|
|
37
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
function text(value) {
|
|
40
|
+
return typeof value === 'string' ? value : '';
|
|
41
|
+
}
|
|
42
|
+
/** `'¥{:,.0f}'`:千分位、四舍五入到整数。 */
|
|
43
|
+
function money(value) {
|
|
44
|
+
if (value === null || value === undefined || value === '')
|
|
45
|
+
return '';
|
|
46
|
+
const num = Number(String(value).replace(/,/gu, ''));
|
|
47
|
+
if (!Number.isFinite(num))
|
|
48
|
+
return String(value);
|
|
49
|
+
return `¥${Math.round(num).toLocaleString('en-US')}`;
|
|
50
|
+
}
|
|
51
|
+
/** 连续重复折叠(封面标题被写重时用)。 */
|
|
52
|
+
function foldDuplicate(input, unit) {
|
|
53
|
+
if (!unit)
|
|
54
|
+
return input;
|
|
55
|
+
let out = input;
|
|
56
|
+
while (out.includes(unit + unit))
|
|
57
|
+
out = out.split(unit + unit).join(unit);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/** 所有 `<w:p>` 区间里**最外层**的(文本框内段落被外层覆盖,按脚本口径整段处理)。 */
|
|
61
|
+
function topParagraphRanges(xml) {
|
|
62
|
+
const all = collectRanges(xml, 'w:p');
|
|
63
|
+
return all.filter((range) => !all.some((other) => other.start < range.start && other.end >= range.end));
|
|
64
|
+
}
|
|
65
|
+
function tableInfos(xml) {
|
|
66
|
+
return parseTables(xml).map((table) => ({
|
|
67
|
+
index: table.index,
|
|
68
|
+
cols: table.cols,
|
|
69
|
+
text: table.text,
|
|
70
|
+
rows: table.rows.map((row) => ({
|
|
71
|
+
index: row.index,
|
|
72
|
+
text: row.text,
|
|
73
|
+
uniqueCellCount: row.uniqueCellCount,
|
|
74
|
+
cells: row.cells.map((cell) => ({ text: cell.text })),
|
|
75
|
+
logicalCols: row.cells.reduce((sum, cell) => {
|
|
76
|
+
const spanHit = /<w:gridSpan[^>]*w:val="(\d+)"/u.exec(cell.xml);
|
|
77
|
+
return sum + (spanHit ? Number(spanHit[1]) : 1);
|
|
78
|
+
}, 0),
|
|
79
|
+
})),
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
/** 按表头特征探测表格(对应脚本 `find_table()`)。 */
|
|
83
|
+
function findTable(infos, opts) {
|
|
84
|
+
const minRows = opts.minRows ?? 1;
|
|
85
|
+
for (const info of infos) {
|
|
86
|
+
if (info.rows.length < minRows)
|
|
87
|
+
continue;
|
|
88
|
+
if (opts.mustAll && !opts.mustAll.every((key) => info.text.includes(key)))
|
|
89
|
+
continue;
|
|
90
|
+
if (opts.mustAny && !opts.mustAny.some((key) => info.text.includes(key)))
|
|
91
|
+
continue;
|
|
92
|
+
return info;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 逻辑列 → 物理 `<w:tc>` 下标。
|
|
98
|
+
*
|
|
99
|
+
* 合并单元格行(如「e-cology系统软件产品费用」横跨 5 列 + 金额 1 列)在 XML 里
|
|
100
|
+
* 只有 2 个 `<w:tc>`,但 python-docx 的 `row.cells[5]` 会按 `w:gridSpan` 展开成
|
|
101
|
+
* 6 个逻辑列并指向最后一个 tc。若按物理下标取 cell 5 会越界,写入被静默跳过、
|
|
102
|
+
* 保留模板原值——2026-11-13 实测正是这个原因导致两处合计写错。
|
|
103
|
+
*/
|
|
104
|
+
function logicalToPhysical(cellXmls, logicalIndex) {
|
|
105
|
+
let acc = 0;
|
|
106
|
+
for (let i = 0; i < cellXmls.length; i += 1) {
|
|
107
|
+
const spanHit = /<w:gridSpan[^>]*w:val="(\d+)"/u.exec(cellXmls[i] ?? '');
|
|
108
|
+
const span = spanHit ? Number(spanHit[1]) : 1;
|
|
109
|
+
if (logicalIndex < acc + span)
|
|
110
|
+
return i;
|
|
111
|
+
acc += span;
|
|
112
|
+
}
|
|
113
|
+
return -1;
|
|
114
|
+
}
|
|
115
|
+
/** 批量写单元格:一次解析、按绝对偏移从后往前替换,避免偏移漂移。 */
|
|
116
|
+
function applyCellWrites(xml, tableIndex, writes) {
|
|
117
|
+
const table = parseTables(xml)[tableIndex];
|
|
118
|
+
if (!table)
|
|
119
|
+
return xml;
|
|
120
|
+
const items = writes
|
|
121
|
+
.map((write) => {
|
|
122
|
+
const row = table.rows[write.row];
|
|
123
|
+
if (!row)
|
|
124
|
+
return null;
|
|
125
|
+
const physical = logicalToPhysical(row.cells.map((cell) => cell.xml), write.cell);
|
|
126
|
+
const cell = physical >= 0 ? row.cells[physical] : undefined;
|
|
127
|
+
if (!cell)
|
|
128
|
+
return null;
|
|
129
|
+
return {
|
|
130
|
+
absStart: table.start + cell.start,
|
|
131
|
+
absEnd: table.start + cell.end,
|
|
132
|
+
cellXml: cell.xml,
|
|
133
|
+
value: write.value,
|
|
134
|
+
};
|
|
135
|
+
})
|
|
136
|
+
.filter((item) => item !== null)
|
|
137
|
+
.sort((a, b) => b.absStart - a.absStart);
|
|
138
|
+
let out = xml;
|
|
139
|
+
for (const item of items) {
|
|
140
|
+
out = out.slice(0, item.absStart) + setCellText(item.cellXml, item.value) + out.slice(item.absEnd);
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
function scanPlaceholders(xml, patterns) {
|
|
145
|
+
const hits = new Map();
|
|
146
|
+
const record = (content, loc) => {
|
|
147
|
+
for (const pattern of patterns) {
|
|
148
|
+
if (!pattern || !content.includes(pattern))
|
|
149
|
+
continue;
|
|
150
|
+
const list = hits.get(pattern) ?? [];
|
|
151
|
+
let count = 0;
|
|
152
|
+
let at = content.indexOf(pattern);
|
|
153
|
+
while (at !== -1) {
|
|
154
|
+
count += 1;
|
|
155
|
+
at = content.indexOf(pattern, at + pattern.length);
|
|
156
|
+
}
|
|
157
|
+
list.push({ loc, count });
|
|
158
|
+
hits.set(pattern, list);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
topParagraphRanges(xml).forEach((range, index) => {
|
|
162
|
+
record(paragraphText(xml.slice(range.start, range.end)), `P#${index}`);
|
|
163
|
+
});
|
|
164
|
+
for (const table of parseTables(xml)) {
|
|
165
|
+
table.rows.forEach((row) => {
|
|
166
|
+
row.cells.forEach((cell) => {
|
|
167
|
+
record(cell.text, `T${table.index}R${row.index}C${cell.index}`);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return [...hits.entries()]
|
|
172
|
+
.map(([placeholder, locations]) => ({
|
|
173
|
+
placeholder,
|
|
174
|
+
total: locations.reduce((sum, item) => sum + item.count, 0),
|
|
175
|
+
locations,
|
|
176
|
+
}))
|
|
177
|
+
.sort((a, b) => b.total - a.total);
|
|
178
|
+
}
|
|
179
|
+
async function callOperation(name, input, context) {
|
|
180
|
+
const operation = resolveOperation(name);
|
|
181
|
+
const result = await operation.execute(input, context);
|
|
182
|
+
return (result?.data ?? {});
|
|
183
|
+
}
|
|
184
|
+
export const contractDocxFillFromQuoteOperations = [
|
|
185
|
+
{
|
|
186
|
+
name: 'contract.docx.fillfromquote',
|
|
187
|
+
async execute(input, context) {
|
|
188
|
+
const templateInput = text(input.template).trim();
|
|
189
|
+
if (!templateInput)
|
|
190
|
+
throw contractValidationError('template_required', 'template 必须是模板 docx 绝对路径');
|
|
191
|
+
const templatePath = resolve(templateInput);
|
|
192
|
+
if (!existsSync(templatePath)) {
|
|
193
|
+
throw contractValidationError('template_not_found', `模板不存在:${templatePath}`);
|
|
194
|
+
}
|
|
195
|
+
const patterns = [...PLACEHOLDER_PATTERNS];
|
|
196
|
+
if (Array.isArray(input.expect)) {
|
|
197
|
+
for (const item of input.expect) {
|
|
198
|
+
const value = text(item).trim();
|
|
199
|
+
if (value)
|
|
200
|
+
patterns.push(value);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// ── 步骤 1:扫描占位符清单(不做任何替换)
|
|
204
|
+
const documentXml = readZip(readFileSync(templatePath))
|
|
205
|
+
.find((entry) => entry.name === 'word/document.xml')?.data.toString('utf8');
|
|
206
|
+
if (!documentXml) {
|
|
207
|
+
throw contractValidationError('unreadable_docx', 'docx 内未找到 word/document.xml,不是有效的 Word 文档');
|
|
208
|
+
}
|
|
209
|
+
// 显式声明为 string:闭包(locate)内对 let 变量会退回声明类型,否则是 string | undefined
|
|
210
|
+
let xml = documentXml;
|
|
211
|
+
const placeholders = scanPlaceholders(xml, patterns);
|
|
212
|
+
const scanOnly = input.scanOnly === true;
|
|
213
|
+
if (scanOnly) {
|
|
214
|
+
return { status: 'COMPLETE', data: { ok: true, mode: 'scan_only', placeholders } };
|
|
215
|
+
}
|
|
216
|
+
const dataInput = input.data;
|
|
217
|
+
const outInput = text(input.out).trim();
|
|
218
|
+
// data 同时支持「对象」与「JSON 文件路径」,两者其一即合规
|
|
219
|
+
const dataIsObject = isRecord(dataInput);
|
|
220
|
+
const dataIsPath = typeof dataInput === 'string' && dataInput.trim().length > 0;
|
|
221
|
+
if ((!dataIsObject && !dataIsPath) || !outInput) {
|
|
222
|
+
throw contractValidationError('data_and_out_required', 'data(对象或其 JSON 文件)与 out 均为必填');
|
|
223
|
+
}
|
|
224
|
+
let data = dataIsObject ? dataInput : {};
|
|
225
|
+
const dataText = dataIsPath ? text(dataInput).trim() : '';
|
|
226
|
+
if (dataText) {
|
|
227
|
+
if (!existsSync(resolve(dataText))) {
|
|
228
|
+
throw contractValidationError('data_not_found', `数据文件不存在:${dataText}`);
|
|
229
|
+
}
|
|
230
|
+
const parsed = JSON.parse(readFileSync(resolve(dataText), 'utf8').replace(/^\uFEFF/u, ''));
|
|
231
|
+
if (!isRecord(parsed))
|
|
232
|
+
throw contractValidationError('data_invalid', '数据文件顶层必须是对象');
|
|
233
|
+
data = parsed;
|
|
234
|
+
}
|
|
235
|
+
const customer = text(data.customer);
|
|
236
|
+
const ourCompany = text(data.our_company);
|
|
237
|
+
const signDate = text(data.sign_date);
|
|
238
|
+
const signPlace = text(data.sign_place);
|
|
239
|
+
const title = text(data.title);
|
|
240
|
+
const warnings = [];
|
|
241
|
+
// ── 步骤 2:文本级替换(先标题整段,再通用占位)
|
|
242
|
+
const replaced = [];
|
|
243
|
+
const paragraphs = topParagraphRanges(xml);
|
|
244
|
+
const paraEdits = [];
|
|
245
|
+
paragraphs.forEach((range) => {
|
|
246
|
+
const pXml = xml.slice(range.start, range.end);
|
|
247
|
+
const original = paragraphText(pXml);
|
|
248
|
+
if (!original)
|
|
249
|
+
return;
|
|
250
|
+
let next = original;
|
|
251
|
+
if (title && (original.includes('xxx公司') || original.includes('XXX公司')) && original.includes('购买合同')) {
|
|
252
|
+
next = foldDuplicate(title, title);
|
|
253
|
+
}
|
|
254
|
+
if (next === original) {
|
|
255
|
+
next = original
|
|
256
|
+
.split('xxx公司').join(customer)
|
|
257
|
+
.split('XXX公司').join(customer)
|
|
258
|
+
.split('***有限公司').join(customer)
|
|
259
|
+
.split('***').join(customer);
|
|
260
|
+
if (title)
|
|
261
|
+
next = foldDuplicate(next, title);
|
|
262
|
+
}
|
|
263
|
+
if (next !== original) {
|
|
264
|
+
paraEdits.push({ start: range.start, end: range.end, next: setParagraphText(pXml, next) });
|
|
265
|
+
replaced.push({ from: original.trim().slice(0, 60), to: next.trim().slice(0, 60) });
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
for (let i = paraEdits.length - 1; i >= 0; i -= 1) {
|
|
269
|
+
const edit = paraEdits[i];
|
|
270
|
+
xml = xml.slice(0, edit.start) + edit.next + xml.slice(edit.end);
|
|
271
|
+
}
|
|
272
|
+
// ── 步骤 3:表格填充(按特征探测,找不到就跳过并告警)
|
|
273
|
+
const tablesFilled = [];
|
|
274
|
+
// 3.1 封面签署块(两列,含「甲方:」「签订时间:」)
|
|
275
|
+
let infos = tableInfos(xml);
|
|
276
|
+
const cover = findTable(infos, { mustAll: ['甲方:'], mustAny: ['签订时间:'], minRows: 2 });
|
|
277
|
+
if (cover && cover.cols === 2) {
|
|
278
|
+
const writes = [{ row: 0, cell: 1, value: customer }];
|
|
279
|
+
for (const row of cover.rows) {
|
|
280
|
+
const key = row.cells[0].text.trim();
|
|
281
|
+
if (key.startsWith('乙方'))
|
|
282
|
+
writes.push({ row: row.index, cell: 1, value: ourCompany });
|
|
283
|
+
else if (key.startsWith('签订时间'))
|
|
284
|
+
writes.push({ row: row.index, cell: 1, value: signDate });
|
|
285
|
+
else if (key.startsWith('签订地点') && signPlace)
|
|
286
|
+
writes.push({ row: row.index, cell: 1, value: signPlace });
|
|
287
|
+
}
|
|
288
|
+
xml = applyCellWrites(xml, cover.index, writes);
|
|
289
|
+
tablesFilled.push({ table: cover.index, role: 'cover' });
|
|
290
|
+
}
|
|
291
|
+
// 3.2 双方信息表 / 签署页(四列,含「甲方:」「乙方:」)
|
|
292
|
+
infos = tableInfos(xml);
|
|
293
|
+
for (const info of infos) {
|
|
294
|
+
if (info.cols !== 4)
|
|
295
|
+
continue;
|
|
296
|
+
if (!info.text.includes('甲方:') || !info.text.includes('乙方:'))
|
|
297
|
+
continue;
|
|
298
|
+
const writes = [
|
|
299
|
+
{ row: 0, cell: 1, value: customer },
|
|
300
|
+
{ row: 0, cell: 3, value: ourCompany },
|
|
301
|
+
];
|
|
302
|
+
if (info.text.includes('生效日期')) {
|
|
303
|
+
for (const row of info.rows) {
|
|
304
|
+
if (row.cells[0].text.trim().startsWith('生效日期')) {
|
|
305
|
+
writes.push({ row: row.index, cell: 1, value: signDate });
|
|
306
|
+
writes.push({ row: row.index, cell: 3, value: signDate });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
xml = applyCellWrites(xml, info.index, writes);
|
|
311
|
+
tablesFilled.push({ table: info.index, role: 'parties' });
|
|
312
|
+
}
|
|
313
|
+
// 3.3 合同标的总额表
|
|
314
|
+
infos = tableInfos(xml);
|
|
315
|
+
const totalTable = findTable(infos, {
|
|
316
|
+
mustAll: ['编号', '内容', '价格', '折扣', '金额'],
|
|
317
|
+
minRows: 3,
|
|
318
|
+
});
|
|
319
|
+
if (totalTable) {
|
|
320
|
+
const writes = [];
|
|
321
|
+
for (const row of totalTable.rows) {
|
|
322
|
+
const first = row.cells[0].text.trim();
|
|
323
|
+
// 合并行的「金额列」是逻辑最后一列(含 gridSpan 展开),不能用物理下标
|
|
324
|
+
const lastLogical = row.logicalCols - 1;
|
|
325
|
+
if (first === '总额' || row.cells[0].text.includes('总额')) {
|
|
326
|
+
writes.push({ row: row.index, cell: lastLogical, value: money(data.grand_total) });
|
|
327
|
+
}
|
|
328
|
+
else if (row.text.includes('产品费用')) {
|
|
329
|
+
writes.push({ row: row.index, cell: lastLogical, value: money(data.product_total) });
|
|
330
|
+
}
|
|
331
|
+
else if (row.text.includes('服务费用') && !row.text.includes('产品费用')) {
|
|
332
|
+
writes.push({ row: row.index, cell: lastLogical, value: money(data.service_total) });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
xml = applyCellWrites(xml, totalTable.index, writes);
|
|
336
|
+
tablesFilled.push({ table: totalTable.index, role: 'total' });
|
|
337
|
+
}
|
|
338
|
+
// 3.4 明细表(编号/名称/单价/数量/折扣/金额)
|
|
339
|
+
const detailTableInput = input.detailTable;
|
|
340
|
+
const detailIndex = typeof detailTableInput === 'number' && Number.isFinite(detailTableInput)
|
|
341
|
+
? Math.floor(detailTableInput)
|
|
342
|
+
: findTable(tableInfos(xml), {
|
|
343
|
+
mustAll: ['名称', '单价', '数量', '折扣', '金额'],
|
|
344
|
+
minRows: 4,
|
|
345
|
+
})?.index ?? null;
|
|
346
|
+
let detailInfo = null;
|
|
347
|
+
if (detailIndex !== null) {
|
|
348
|
+
// 闭包内 TS 会丢失 detailIndex 的非空收窄,先固定为具体数字
|
|
349
|
+
const detailIdx = detailIndex;
|
|
350
|
+
const locate = () => {
|
|
351
|
+
const table = parseTables(xml)[detailIdx];
|
|
352
|
+
let headerRow = -1;
|
|
353
|
+
let productTotalRow = -1;
|
|
354
|
+
let serviceSectionRow = -1;
|
|
355
|
+
let serviceTotalRow = -1;
|
|
356
|
+
if (!table)
|
|
357
|
+
return { headerRow, productTotalRow, serviceSectionRow, serviceTotalRow };
|
|
358
|
+
const HEADER_KEYS = ['编号', '名称', '单价', '数量', '折扣', '金额'];
|
|
359
|
+
for (const row of table.rows) {
|
|
360
|
+
const cells = row.cells.map((cell) => cell.text.trim());
|
|
361
|
+
const uniq = new Set(cells);
|
|
362
|
+
if (headerRow < 0 && HEADER_KEYS.every((key) => uniq.has(key))) {
|
|
363
|
+
headerRow = row.index;
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (headerRow < 0)
|
|
367
|
+
continue;
|
|
368
|
+
if (row.text.includes('产品费用') && row.uniqueCellCount <= 2) {
|
|
369
|
+
productTotalRow = row.index;
|
|
370
|
+
}
|
|
371
|
+
else if (/^\d+\s*服务费用/u.test(cells[0] ?? '') && row.uniqueCellCount <= 2) {
|
|
372
|
+
// 服务区分隔行必须先判,否则会被当成服务合计行
|
|
373
|
+
serviceSectionRow = row.index;
|
|
374
|
+
}
|
|
375
|
+
else if (row.text.includes('服务费用') && row.uniqueCellCount <= 2) {
|
|
376
|
+
serviceTotalRow = row.index;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return { headerRow, productTotalRow, serviceSectionRow, serviceTotalRow };
|
|
380
|
+
};
|
|
381
|
+
let pos = locate();
|
|
382
|
+
if (pos.headerRow < 0) {
|
|
383
|
+
warnings.push('明细表未识别到表头行,跳过行操作以外的处理');
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
const productItems = Array.isArray(data.product_items) ? data.product_items : [];
|
|
387
|
+
const serviceItems = Array.isArray(data.service_items) ? data.service_items : [];
|
|
388
|
+
// 产品明细区:header+1 ~ productTotal-1
|
|
389
|
+
if (pos.productTotalRow >= 0) {
|
|
390
|
+
const have = pos.productTotalRow - pos.headerRow - 1;
|
|
391
|
+
if (have !== productItems.length) {
|
|
392
|
+
const res = deleteTableRows(xml, detailIndex, pos.headerRow + 1, productItems.length, have);
|
|
393
|
+
if (!res.ok) {
|
|
394
|
+
warnings.push(`产品明细行不足:模板 ${have} 行、需要 ${productItems.length} 行,未自动新增`);
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
xml = res.xml;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
pos = locate();
|
|
401
|
+
}
|
|
402
|
+
// 服务明细区:serviceSection+1 ~ serviceTotal-1
|
|
403
|
+
if (pos.serviceSectionRow >= 0 && pos.serviceTotalRow >= 0) {
|
|
404
|
+
const haveS = pos.serviceTotalRow - pos.serviceSectionRow - 1;
|
|
405
|
+
if (haveS !== serviceItems.length) {
|
|
406
|
+
const res = deleteTableRows(xml, detailIndex, pos.serviceSectionRow + 1, serviceItems.length, haveS);
|
|
407
|
+
if (!res.ok) {
|
|
408
|
+
warnings.push(`服务明细行不足:模板 ${haveS} 行、需要 ${serviceItems.length} 行,未自动新增`);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
xml = res.xml;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
pos = locate();
|
|
415
|
+
}
|
|
416
|
+
// 填明细(对应脚本 fill_detail_table)
|
|
417
|
+
const writes = [];
|
|
418
|
+
const rowWrites = (rowIndex, item) => {
|
|
419
|
+
const amount = item.amount;
|
|
420
|
+
const amountText = amount === null || amount === undefined ? '' : money(amount);
|
|
421
|
+
writes.push({ row: rowIndex, cell: 0, value: text(item.no) });
|
|
422
|
+
writes.push({ row: rowIndex, cell: 1, value: text(item.name) });
|
|
423
|
+
writes.push({ row: rowIndex, cell: 2, value: amountText });
|
|
424
|
+
writes.push({
|
|
425
|
+
row: rowIndex,
|
|
426
|
+
cell: 3,
|
|
427
|
+
value: item.qty === null || item.qty === undefined ? '1' : String(item.qty),
|
|
428
|
+
});
|
|
429
|
+
writes.push({ row: rowIndex, cell: 4, value: text(item.discount) || '100%' });
|
|
430
|
+
writes.push({ row: rowIndex, cell: 5, value: amountText });
|
|
431
|
+
};
|
|
432
|
+
productItems.forEach((item, i) => {
|
|
433
|
+
if (isRecord(item))
|
|
434
|
+
rowWrites(pos.headerRow + 1 + i, item);
|
|
435
|
+
});
|
|
436
|
+
if (pos.productTotalRow >= 0) {
|
|
437
|
+
writes.push({ row: pos.productTotalRow, cell: 5, value: money(data.product_total) });
|
|
438
|
+
}
|
|
439
|
+
serviceItems.forEach((item, i) => {
|
|
440
|
+
if (isRecord(item) && pos.serviceSectionRow >= 0) {
|
|
441
|
+
rowWrites(pos.serviceSectionRow + 1 + i, item);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
if (pos.serviceTotalRow >= 0) {
|
|
445
|
+
writes.push({ row: pos.serviceTotalRow, cell: 5, value: money(data.service_total) });
|
|
446
|
+
}
|
|
447
|
+
xml = applyCellWrites(xml, detailIndex, writes);
|
|
448
|
+
detailInfo = {
|
|
449
|
+
table: detailIndex,
|
|
450
|
+
header_row: pos.headerRow,
|
|
451
|
+
product_total_row: pos.productTotalRow,
|
|
452
|
+
service_section_row: pos.serviceSectionRow,
|
|
453
|
+
service_total_row: pos.serviceTotalRow,
|
|
454
|
+
};
|
|
455
|
+
tablesFilled.push({ table: detailIndex, role: 'detail' });
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
warnings.push('未识别到明细表,明细与合计未填');
|
|
460
|
+
}
|
|
461
|
+
// 3.5 付款计划表
|
|
462
|
+
infos = tableInfos(xml);
|
|
463
|
+
const paymentTable = findTable(infos, { mustAll: ['付款比例', '付款金额'], minRows: 2 });
|
|
464
|
+
if (paymentTable) {
|
|
465
|
+
const ratios = Array.isArray(data.payment_ratios) ? data.payment_ratios : [];
|
|
466
|
+
const grand = Number(data.grand_total ?? 0) || 0;
|
|
467
|
+
const writes = [];
|
|
468
|
+
ratios.forEach((ratio, i) => {
|
|
469
|
+
if (1 + i >= paymentTable.rows.length) {
|
|
470
|
+
warnings.push('付款表行数少于比例数量,未追加行');
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const value = Number(ratio);
|
|
474
|
+
if (!Number.isFinite(value))
|
|
475
|
+
return;
|
|
476
|
+
writes.push({ row: 1 + i, cell: 1, value: `${Math.round(value * 100)}%` });
|
|
477
|
+
writes.push({ row: 1 + i, cell: 2, value: money(Math.round(grand * value)) });
|
|
478
|
+
});
|
|
479
|
+
xml = applyCellWrites(xml, paymentTable.index, writes);
|
|
480
|
+
tablesFilled.push({ table: paymentTable.index, role: 'payment' });
|
|
481
|
+
}
|
|
482
|
+
// ── 步骤 4:保存
|
|
483
|
+
const outPath = resolve(outInput);
|
|
484
|
+
const outDir = dirname(outPath);
|
|
485
|
+
if (outDir)
|
|
486
|
+
mkdirSync(outDir, { recursive: true });
|
|
487
|
+
if (input.dryRun === true) {
|
|
488
|
+
return {
|
|
489
|
+
status: 'COMPLETE',
|
|
490
|
+
data: {
|
|
491
|
+
ok: true,
|
|
492
|
+
mode: 'dry_run',
|
|
493
|
+
placeholders,
|
|
494
|
+
replaced: replaced.length,
|
|
495
|
+
tables: tablesFilled,
|
|
496
|
+
detail: detailInfo,
|
|
497
|
+
warnings,
|
|
498
|
+
},
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
writeFileSync(outPath, rewriteZip(templatePath, { 'word/document.xml': xml }));
|
|
502
|
+
// ── 步骤 5:回读校验(复用已下沉的 docx.verify,保证判据只有一份)
|
|
503
|
+
const titleCountInput = input.titleCount;
|
|
504
|
+
const verify = await callOperation('contract.docx.verify', {
|
|
505
|
+
path: outPath,
|
|
506
|
+
patterns,
|
|
507
|
+
...(title ? { title } : {}),
|
|
508
|
+
...(typeof titleCountInput === 'number' && Number.isFinite(titleCountInput)
|
|
509
|
+
? { titleCount: Math.floor(titleCountInput) }
|
|
510
|
+
: {}),
|
|
511
|
+
}, context);
|
|
512
|
+
const passed = verify.ok === true;
|
|
513
|
+
return {
|
|
514
|
+
status: 'COMPLETE',
|
|
515
|
+
data: {
|
|
516
|
+
ok: passed,
|
|
517
|
+
out: outPath,
|
|
518
|
+
template: templatePath,
|
|
519
|
+
placeholders_found: placeholders,
|
|
520
|
+
text_replaced: replaced.length,
|
|
521
|
+
tables_filled: tablesFilled,
|
|
522
|
+
detail_rows: detailInfo,
|
|
523
|
+
warnings,
|
|
524
|
+
validate: verify,
|
|
525
|
+
note: passed
|
|
526
|
+
? '通过:占位符 0 残留、标题唯一、表格结构完好'
|
|
527
|
+
: '不通过:见 validate.residue / titleDoubled / zeroCellRows',
|
|
528
|
+
},
|
|
529
|
+
};
|
|
530
|
+
},
|
|
531
|
+
},
|
|
532
|
+
];
|