weaver-work-cli 0.1.54 → 0.1.57

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.
Files changed (189) hide show
  1. package/dist/chunks/{archive-ZQ4A4TV6.js → archive-4SQAVTIB.js} +3 -2
  2. package/dist/chunks/{archive-oa-57R2FYHV.js → archive-oa-W2UEJ67L.js} +137 -8
  3. package/dist/chunks/{asset-ZOJVBWZH.js → asset-EGZEABBI.js} +3 -2
  4. package/dist/chunks/{asset-idle-XGB5IPGT.js → asset-idle-EBUZBB6M.js} +3 -2
  5. package/dist/chunks/{attachment-content-2EPEI4HV.js → attachment-content-QGM5MLQE.js} +4 -3
  6. package/dist/chunks/{blog-QSHLZSFW.js → blog-2YKMWOWI.js} +3 -2
  7. package/dist/chunks/{caizhilian-KCQYFH72.js → caizhilian-QJ4FLSQJ.js} +5 -4
  8. package/dist/chunks/{calendar-DLGMRJQA.js → calendar-H4QTJCUY.js} +3 -2
  9. package/dist/chunks/{chunk-XPQN4E5O.js → chunk-44KJ7WFA.js} +300 -55
  10. package/dist/chunks/{chunk-CZNZKOHR.js → chunk-4GG72JAT.js} +2 -2
  11. package/dist/chunks/{chunk-YIKSL36S.js → chunk-5N222HK6.js} +2 -2
  12. package/dist/chunks/{chunk-XHMJBRLZ.js → chunk-6DVL5IN5.js} +4 -1
  13. package/dist/chunks/{chunk-JJRIT2ZN.js → chunk-6MS3RLPO.js} +5 -334
  14. package/dist/chunks/{chunk-QUDHSMG3.js → chunk-7EBV7Q3R.js} +112 -27
  15. package/dist/chunks/{chunk-CNVZETM5.js → chunk-AHJZIMCX.js} +1 -1
  16. package/dist/chunks/{chunk-TGAGQV4I.js → chunk-CQ4OVCKG.js} +2 -2
  17. package/dist/chunks/{chunk-2BQESR2U.js → chunk-DVR2EDUU.js} +99 -34
  18. package/dist/chunks/{chunk-7KKI5KDY.js → chunk-FDAJC7R4.js} +4 -4
  19. package/dist/chunks/{chunk-GR76ZDDX.js → chunk-FMNCSYIG.js} +2 -2
  20. package/dist/chunks/{chunk-6LGLI46Q.js → chunk-L2LMBFG5.js} +1 -1
  21. package/dist/chunks/{chunk-DLY5GUXX.js → chunk-PH7N4UFT.js} +1 -1
  22. package/dist/chunks/{chunk-U6R6FZHD.js → chunk-SFM3M2L3.js} +286 -93
  23. package/dist/chunks/chunk-SHYQUBY3.js +48 -0
  24. package/dist/chunks/{chunk-HFXK54M4.js → chunk-TGM7BWEH.js} +2 -2
  25. package/dist/chunks/{chunk-Z7B7QSR2.js → chunk-TJUAGZU7.js} +11 -11
  26. package/dist/chunks/{chunk-LYOFRYC4.js → chunk-UI66EMOR.js} +4 -4
  27. package/dist/chunks/{chunk-762M5XJS.js → chunk-VMCCRWFO.js} +2 -2
  28. package/dist/chunks/{chunk-FE3CQAAM.js → chunk-WHJIUKJW.js} +681 -48
  29. package/dist/chunks/{chunk-PGO4ILMQ.js → chunk-WMB3SMXI.js} +1 -1
  30. package/dist/chunks/chunk-WMRNO6IU.js +337 -0
  31. package/dist/chunks/{chunk-6NXT4ICB.js → chunk-WVH5ESDA.js} +1 -1
  32. package/dist/chunks/{chunk-O6RIBG5E.js → chunk-XU3FL3AW.js} +4 -4
  33. package/dist/chunks/{chunk-Z6UUBY37.js → chunk-Z4IPY5GV.js} +2 -2
  34. package/dist/chunks/{conflict-K2IAKKFR.js → conflict-VAFFPUY3.js} +2 -2
  35. package/dist/chunks/{contract-LJRAXVZU.js → contract-LLRKMLQQ.js} +2655 -1319
  36. package/dist/chunks/{cowork-3ACICHDQ.js → cowork-2RDFBZWB.js} +3 -2
  37. package/dist/chunks/{create-flow-UL37MZGP.js → create-flow-TVYE3IOH.js} +6 -6
  38. package/dist/chunks/{data-WK5CQXNF.js → data-WVICO3DT.js} +57 -11
  39. package/dist/chunks/{ebuilder-form-CBYI6CQ6.js → ebuilder-form-TWSXS5NA.js} +7 -6
  40. package/dist/chunks/{ehr-XI4BIVR2.js → ehr-52GBKNJR.js} +3 -2
  41. package/dist/chunks/{env-BZDB5OVZ.js → env-BVVFIN4P.js} +2 -2
  42. package/dist/chunks/{esb-4BQHNP5C.js → esb-HJSXCEOM.js} +5 -4
  43. package/dist/chunks/{factory-VD3YI6N7.js → factory-RCQ36LLE.js} +1 -1
  44. package/dist/chunks/{fna-ECOYBQYL.js → fna-OBACRT3J.js} +72 -3
  45. package/dist/chunks/{group-chat-DHNVO76L.js → group-chat-YFZDUUY3.js} +3 -3
  46. package/dist/chunks/{host-TUOIOEMC.js → host-2BKMPWDH.js} +4 -3
  47. package/dist/chunks/{host-3TNBMDTR.js → host-KJT3G5D2.js} +4 -3
  48. package/dist/chunks/{host-7OWWPLM4.js → host-KOWUMNAY.js} +4 -3
  49. package/dist/chunks/{host-M4XP5URR.js → host-MFXRA2MY.js} +4 -3
  50. package/dist/chunks/{host-7NTIBSPK.js → host-TTSGY5NZ.js} +4 -3
  51. package/dist/chunks/{host-G3VU7SDW.js → host-W4JCNRFS.js} +4 -3
  52. package/dist/chunks/{hrm-YGIR3HGF.js → hrm-AK3WGBMN.js} +4 -3
  53. package/dist/chunks/{im-TN2JYVGJ.js → im-JZSBPO4Z.js} +4 -3
  54. package/dist/chunks/{invoice-V7ZTITUO.js → invoice-7JGSEWT5.js} +22 -9
  55. package/dist/chunks/{jiuchuanhui-ASFZBMPU.js → jiuchuanhui-K4EQU6GK.js} +3 -2
  56. package/dist/chunks/{lazy-dispatch-XSAHDK6Z.js → lazy-dispatch-JHGFCKJN.js} +5 -4
  57. package/dist/chunks/{mail-M3HJF5ER.js → mail-IKVCQCVZ.js} +3 -2
  58. package/dist/chunks/{meeting-OWG4O2XH.js → meeting-JOHV4RH3.js} +14 -12
  59. package/dist/chunks/minute-doc-JLS4KMFZ.js +156 -0
  60. package/dist/chunks/{okr-EVX7FB5G.js → okr-KV43FPPX.js} +3 -2
  61. package/dist/chunks/{operations-J7PUIVWZ.js → operations-ZPWBMYKE.js} +5 -4
  62. package/dist/chunks/{plan-2QJQEPBO.js → plan-WOYR4WEG.js} +3 -2
  63. package/dist/chunks/{project-XELELW2T.js → project-CQJKBUIL.js} +3 -2
  64. package/dist/chunks/{reception-QGPNTGHG.js → reception-MPBU3JXC.js} +3 -2
  65. package/dist/chunks/{recruit-stage-XCMVEQA2.js → recruit-stage-USTTRHOX.js} +3 -2
  66. package/dist/chunks/{registry-CJOVECZD.js → registry-GURQG4ZV.js} +3 -2
  67. package/dist/chunks/{registry-ASYCWF2U.js → registry-LITYOH2W.js} +12 -10
  68. package/dist/chunks/{registry-QJCZ2GVG.js → registry-SS5J2FZQ.js} +5 -4
  69. package/dist/chunks/{room-recommend-LSVAMGKV.js → room-recommend-62MECG4J.js} +4 -4
  70. package/dist/chunks/{schedule-7QOPRUU4.js → schedule-6QRXIBGU.js} +4 -4
  71. package/dist/chunks/{smart-extract-FOUYNIG3.js → smart-extract-APX2PNIN.js} +3 -2
  72. package/dist/chunks/{workflow-YHXFIU63.js → workflow-WMPW3WSB.js} +5 -4
  73. package/dist/index.js +33 -32
  74. package/dist/internal/e10/auth/agent-host-key.js +68 -0
  75. package/dist/internal/e10/auth/agent-hosts.js +250 -0
  76. package/dist/internal/e10/auth/commands.js +99 -30
  77. package/dist/internal/e10/auth/paths.js +9 -0
  78. package/dist/internal/e10/auth/session.js +17 -0
  79. package/dist/internal/e10/auth/xiaoe.js +37 -3
  80. package/dist/shortcuts/archive-oa/operations/contract-modules.js +106 -0
  81. package/dist/shortcuts/archive-oa/operations/contract.js +38 -4
  82. package/dist/shortcuts/archive-oa/operations/registry.js +2 -1
  83. package/dist/shortcuts/contract/index.js +6 -7
  84. package/dist/shortcuts/contract/manifest.js +101 -24
  85. package/dist/shortcuts/contract/ooxml/docx-table.js +75 -3
  86. package/dist/shortcuts/contract/ooxml/xlsx.js +49 -13
  87. package/dist/shortcuts/contract/operations/card-action.js +113 -12
  88. package/dist/shortcuts/contract/operations/card-ebdf-list.js +29 -7
  89. package/dist/shortcuts/contract/operations/card-plan.js +75 -9
  90. package/dist/shortcuts/contract/operations/card-risk-report.js +2 -2
  91. package/dist/shortcuts/contract/operations/card.js +1 -1
  92. package/dist/shortcuts/contract/operations/clause-library.js +88 -16
  93. package/dist/shortcuts/contract/operations/compare-screen.js +378 -0
  94. package/dist/shortcuts/contract/operations/compare.js +1 -1
  95. package/dist/shortcuts/contract/operations/credit-page.js +1 -1
  96. package/dist/shortcuts/contract/operations/docx-slots.js +1000 -0
  97. package/dist/shortcuts/contract/operations/flow.js +2 -2
  98. package/dist/shortcuts/contract/operations/quote-to-contract.js +495 -0
  99. package/dist/shortcuts/contract/operations/quote.js +2 -229
  100. package/dist/shortcuts/contract/operations/recommend.js +1 -1
  101. package/dist/shortcuts/contract/operations/registry.js +6 -2
  102. package/dist/shortcuts/contract/operations/review.js +230 -4
  103. package/dist/shortcuts/contract/operations/save-recognize.js +122 -3
  104. package/dist/shortcuts/contract/operations/template.js +11 -10
  105. package/dist/shortcuts/data/operations/ds.js +5 -5
  106. package/dist/shortcuts/data/operations/shared.js +76 -4
  107. package/dist/shortcuts/fna/manifest.js +7 -2
  108. package/dist/shortcuts/fna/operations/budget.js +68 -0
  109. package/dist/shortcuts/invoice/manifest.js +8 -3
  110. package/dist/shortcuts/invoice/operations/reim.js +10 -3
  111. package/dist/shortcuts/meeting/minute-doc.js +153 -0
  112. package/dist/shortcuts/meeting/operations/compose-outcome.js +176 -0
  113. package/dist/shortcuts/meeting/operations/eb/catalog.js +1 -1
  114. package/dist/shortcuts/meeting/operations/eb/create-flow.js +26 -0
  115. package/dist/shortcuts/meeting/operations/eb/decision-task-batch.js +144 -0
  116. package/dist/shortcuts/meeting/operations/eb/decision.js +92 -4
  117. package/dist/shortcuts/meeting/operations/eb/index.js +133 -4
  118. package/dist/shortcuts/meeting/operations/eb/meeting-link.js +90 -0
  119. package/dist/shortcuts/meeting/operations/from-group-chat.js +46 -0
  120. package/dist/shortcuts/meeting/operations/minute-content.js +61 -0
  121. package/dist/shortcuts/meeting/operations/registry.js +14 -1
  122. package/dist/shortcuts/meeting/operations/self-check.js +49 -0
  123. package/dist/shortcuts/workflow/endpoints.js +2 -0
  124. package/dist/shortcuts/workflow/operations/read.js +114 -8
  125. package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.collect.js +72 -46
  126. package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.core.js +41 -0
  127. package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.js +46 -11
  128. package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.smartGroup.js +18 -1
  129. package/dist/shortcuts/workflow/operations/todoSmartConvert/todoSmartConvert.smartGroupAdd.js +51 -0
  130. package/docs/e10-auth.md +78 -0
  131. package/docs/fna.md +8 -3
  132. package/docs/invoice.md +1 -1
  133. package/package.json +4 -2
  134. package/scripts/pack-offline.mjs +228 -0
  135. package/scripts/package-skill.mjs +14 -5
  136. package/skill-template/business-info.json +3 -0
  137. package/skill-template/domains/qiyecheng.md +4 -3
  138. package/skills/weaver-e10-archive-oa/SKILL.md +6 -1
  139. package/skills/weaver-e10-archive-oa/references/contract-search.md +91 -4
  140. package/skills/weaver-e10-jinchengda/SKILL.md +100 -81
  141. package/skills/weaver-e10-jinchengda/references/contract-action-card-template.html +11 -6
  142. package/skills/weaver-e10-jinchengda/references/contract-action-card.md +5 -4
  143. package/skills/weaver-e10-jinchengda/references/contract-action-engine.md +38 -12
  144. package/skills/weaver-e10-jinchengda/references/contract-agent-entry.md +1 -1
  145. package/skills/weaver-e10-jinchengda/references/contract-ai-assistant.md +1 -1
  146. package/skills/weaver-e10-jinchengda/references/contract-ai-review.md +20 -6
  147. package/skills/weaver-e10-jinchengda/references/contract-card-render-internals.md +8 -3
  148. package/skills/weaver-e10-jinchengda/references/contract-clause-library-crud.md +56 -13
  149. package/skills/weaver-e10-jinchengda/references/contract-clause-recommender.md +7 -0
  150. package/skills/weaver-e10-jinchengda/references/contract-command-index.md +4 -4
  151. package/skills/weaver-e10-jinchengda/references/contract-compare-report.md +11 -5
  152. package/skills/weaver-e10-jinchengda/references/contract-credit-page.md +1 -1
  153. package/skills/weaver-e10-jinchengda/references/contract-disabled-capabilities.md +1 -1
  154. package/skills/weaver-e10-jinchengda/references/contract-doc-compare.md +38 -7
  155. package/skills/weaver-e10-jinchengda/references/contract-doc-triage.md +13 -25
  156. package/skills/weaver-e10-jinchengda/references/contract-element-save-record.md +9 -8
  157. package/skills/weaver-e10-jinchengda/references/contract-execution-rules.md +8 -6
  158. package/skills/weaver-e10-jinchengda/references/contract-field-extract-new-customer.md +10 -7
  159. package/skills/weaver-e10-jinchengda/references/contract-flow-launch.md +2 -2
  160. package/skills/weaver-e10-jinchengda/references/contract-ledger-catalog.md +3 -1
  161. package/skills/weaver-e10-jinchengda/references/contract-ledger-query-playbook.md +26 -18
  162. package/skills/weaver-e10-jinchengda/references/contract-modeltext-library.md +4 -4
  163. package/skills/weaver-e10-jinchengda/references/contract-multi-file.md +18 -13
  164. package/skills/weaver-e10-jinchengda/references/contract-performance-query.md +5 -3
  165. package/skills/weaver-e10-jinchengda/references/contract-quote-to-contract.md +105 -49
  166. package/skills/weaver-e10-jinchengda/references/contract-template-recommend.md +1 -1
  167. package/skills/weaver-e10-jinchengda/references/contract-template-selector.md +3 -6
  168. package/skills/weaver-e10-meeting/SKILL.md +47 -9
  169. package/skills/weaver-e10-qiyecheng/SKILL.md +16 -7
  170. package/skills/weaver-e10-qiyecheng/references/budget.md +77 -3
  171. package/skills/weaver-e10-qiyecheng/references/catalog.md +6 -4
  172. package/skills/weaver-e10-qiyecheng/references/getEnabledTemplate-api.md +255 -0
  173. package/skills/weaver-e10-qiyecheng/references/source-manifest.json +70 -25
  174. package/skills/weaver-e10-workflow/SKILL.md +54 -40
  175. package/skills/weaver-e10-workflow/references/todo-dialogue.md +134 -0
  176. package/skills/weaver-e10-workflow/references/todo-smart-convert.md +102 -80
  177. package/skills/weaver-e10-workflow/references/workflow-agent-entry.md +23 -13
  178. package/skills/weaver-e10-workflow/references/workflow-query.md +68 -46
  179. package/skills/weaver-e10-workflow/references/workflow-view.md +21 -13
  180. package/skills/weaver-e10-workflow/tools/todo-summary.cjs +317 -332
  181. package/skills/weaver-e10-yepiaotong/references/reim-api-reference.md +1 -0
  182. package/skills/weaver-e10-yepiaotong/references/reim-examples.md +23 -13
  183. package/skills/weaver-e10-yepiaotong.zip +0 -0
  184. package/dist/shortcuts/contract/ooxml/docx.js +0 -295
  185. package/dist/shortcuts/contract/operations/docx-fillfromquote.js +0 -532
  186. package/skills/weaver-e10-jinchengda/references/contract-risk-report-template.html +0 -178
  187. package/skills/weaver-e10-jinchengda/references/source-manifest.json +0 -1270
  188. package/skills/weaver-e10-jinchengda/scripts/fill_plan.py +0 -998
  189. package/skills/weaver-e10-workflow/tools/todo-serve.cjs +0 -327
@@ -329,6 +329,7 @@ weaver-work-cli --json invoice run invoice.reim.requests.list --input-json '{"pa
329
329
  | 传了 `scene="phone"`(本技能不再使用) | 结束日前 90 天,不再放宽 | 同上 |
330
330
  | 用户指定了开始日 | 用户指定的日期,不再放宽 | 同上 |
331
331
 
332
+ - 事前申请列表只在 `data.result.data`(不再另给一层与 `result.data` 重复的 `data.data`)。
332
333
  - 返回的 `data.query` 写明本次实际用的窗口:`cusCreateDateStart`、`cusCreateDateEnd`(业务结束日,未加 1 天)、`widened`(是否放宽到 1 个月)。
333
334
  - 翻页(`pageNo` ≥ 2)时把第 1 页 `data.query.cusCreateDateStart` 原样传回,保证各页窗口一致。
334
335
  - 放宽后仍为空:告诉用户「最近一个月没有查到事前申请」,请用户给出发起时间范围后按指定日期再查。
@@ -193,22 +193,32 @@ macOS/Linux(bash/zsh):
193
193
  weaver-work-cli --json invoice run invoice.reim.requests.list --input-json '{"pageNo":1,"pageSize":100}'
194
194
  ```
195
195
 
196
- **返回:**
196
+ **返回:** 读 `data.result.data`(列表只在这一层)。
197
197
 
198
198
  ```json
199
199
  {
200
- "code": 200,
201
- "data": [
202
- {
203
- "requestid": "1298294241967177729",
204
- "workflowid": "1181057526276915214",
205
- "requestname": "交通费报销-后端开发111-2026-07-25",
206
- "flowStatus": 6,
207
- "creatorId": "1201729946891231232",
208
- "creatorName": "后端开发111",
209
- "createTime": "2026-07-25 10:30:00"
210
- }
211
- ]
200
+ "result": {
201
+ "code": 200,
202
+ "msg": "接口返回成功",
203
+ "status": true,
204
+ "data": [
205
+ {
206
+ "requestid": "1298294241967177729",
207
+ "workflowid": "1181057526276915214",
208
+ "requestname": "交通费报销-后端开发111-2026-07-25",
209
+ "flowStatus": 6,
210
+ "creatorId": "1201729946891231232",
211
+ "creatorName": "后端开发111",
212
+ "createTime": "2026-07-25 10:30:00"
213
+ }
214
+ ],
215
+ "fail": false
216
+ },
217
+ "query": {
218
+ "cusCreateDateStart": "2026-07-18",
219
+ "cusCreateDateEnd": "2026-07-25",
220
+ "widened": false
221
+ }
212
222
  }
213
223
  ```
214
224
 
Binary file
@@ -1,295 +0,0 @@
1
- /**
2
- * 最小 docx 填充引擎(零依赖)。
3
- *
4
- * 只处理 word/document.xml,语义与源脚本 quote_to_contract.py 的 fill_docx 对齐:
5
- * - 正文段落(不含表格内)先做具名占位符 + 匿名空白填充(低阈值)。
6
- * - 表格单元格里「空值格」只认高阈值(≥95)的精确命中,避免误填签名栏。
7
- * - 改写策略与 python-docx 的 set_para_text 一致:首个 w:t 写入新文本,其余清空。
8
- * - 明细追加表自带 tblBorders,不依赖模板存在 Table Grid 样式。
9
- *
10
- * XML 处理为分段正则扫描(机器生成的 OOXML 结构稳定),不做通用 XML 解析。
11
- */
12
- import { readFileSync } from 'node:fs';
13
- import { readZipEntry, rewriteZip } from './zip.js';
14
- import { encodeXml, decodeXml } from './xlsx.js';
15
- /** 注意:带 /g 的正则对象 .test() 有状态,统一用 SOURCE 现场构造。 */
16
- export const PLACEHOLDER_SOURCE = '\\{\\{[^}]{1,60}\\}\\}|\\$\\{[^}]{1,60}\\}|【[^】]{1,60}】';
17
- export const BLANK_SOURCE = '_{2,}|_{2,}|【\\s*】|\\{\\s*\\}';
18
- export function hasPlaceholder(text) {
19
- return new RegExp(PLACEHOLDER_SOURCE, 'u').test(text);
20
- }
21
- export function hasBlank(text) {
22
- return new RegExp(BLANK_SOURCE, 'u').test(text);
23
- }
24
- /** 取占位符之前的标签(以 :/: 结尾),退化取段内第一个词组。 */
25
- export function labelBefore(text, pos) {
26
- const seg = text.slice(0, pos);
27
- const matches = [...seg.matchAll(/([^\s,,;;。]{1,20})[::]\s*$/gu)];
28
- if (matches.length)
29
- return matches[matches.length - 1][1];
30
- const m2 = /([^\s,,;;。]{2,20})[::]/u.exec(seg);
31
- return m2 ? m2[1] : '';
32
- }
33
- function fillText(text, floor, filler, log) {
34
- const orig = text;
35
- // 1) 具名占位符
36
- text = text.replace(new RegExp(PLACEHOLDER_SOURCE, 'gu'), (token) => {
37
- const label = token.replace(/^[\{$【]+/u, '').replace(/[}\}】]+$/u, '').trim();
38
- const { key, score } = filler.bestKey(label, floor);
39
- const value = key ? filler.valOf(key) : '';
40
- if (value) {
41
- log.push({ label, key, score, type: 'named' });
42
- return value;
43
- }
44
- log.push({ label, key, score, type: 'named_unfilled' });
45
- return token;
46
- });
47
- // 2) 匿名空白
48
- let out = '';
49
- let idx = 0;
50
- let changed = false;
51
- for (const m of text.matchAll(new RegExp(BLANK_SOURCE, 'gu'))) {
52
- const start = m.index ?? 0;
53
- const label = labelBefore(text, start);
54
- const { key, score } = filler.bestKey(label, floor);
55
- const value = key ? filler.valOf(key) : '';
56
- if (value) {
57
- out += text.slice(idx, start) + value;
58
- log.push({ label, key, score, type: 'blank' });
59
- changed = true;
60
- }
61
- else {
62
- out += text.slice(idx, start + m[0].length);
63
- log.push({ label, key, score, type: 'blank_unfilled' });
64
- }
65
- idx = start + m[0].length;
66
- }
67
- out += text.slice(idx);
68
- return { text: out, changed: changed || out !== orig };
69
- }
70
- function collectRanges(xml, tag) {
71
- const open = new RegExp(`<${tag}(\\s[^>]*)?>`, 'gu');
72
- const close = new RegExp(`</${tag}>`, 'gu');
73
- const ranges = [];
74
- const events = [];
75
- for (const m of xml.matchAll(open))
76
- events.push({ pos: m.index ?? 0, open: true });
77
- for (const m of xml.matchAll(close))
78
- events.push({ pos: m.index ?? 0, open: false });
79
- events.sort((a, b) => a.pos - b.pos || (a.open ? -1 : 1));
80
- let depth = 0;
81
- let currentStart = -1;
82
- for (const e of events) {
83
- if (e.open) {
84
- if (depth === 0)
85
- currentStart = e.pos;
86
- depth++;
87
- }
88
- else {
89
- depth--;
90
- if (depth === 0 && currentStart >= 0) {
91
- const end = e.pos + tag.length + 3;
92
- ranges.push({ start: currentStart, end });
93
- currentStart = -1;
94
- }
95
- }
96
- }
97
- return ranges;
98
- }
99
- function paragraphFullText(pXml) {
100
- let text = '';
101
- for (const m of pXml.matchAll(/<w:t(?:\s[^>]*)?>([\s\S]*?)<\/w:t>/gu))
102
- text += decodeXml(m[1]);
103
- return text;
104
- }
105
- function setParagraphText(pXml, newText) {
106
- let first = true;
107
- return pXml.replace(/<w:t((?:\s[^>]*)?)>([\s\S]*?)<\/w:t>/gu, (_all, attrs) => {
108
- if (first) {
109
- first = false;
110
- const spaceAttr = /xml:space/.test(attrs) ? attrs : attrs + ' xml:space="preserve"';
111
- return `<w:t${spaceAttr}>${encodeXml(newText)}</w:t>`;
112
- }
113
- return '<w:t xml:space="preserve"></w:t>';
114
- });
115
- }
116
- function topRanges(xml, tag) {
117
- // 只取深度 0 的顶层范围
118
- return collectRanges(xml, tag);
119
- }
120
- function isInside(pos, ranges) {
121
- return ranges.some((r) => pos >= r.start && pos < r.end);
122
- }
123
- /** 填充 docx 模板:body 段落低阈值、表格空格高阈值、可选追加明细表。 */
124
- export function fillDocx(templatePath, filler, options = {}) {
125
- // 硬门禁与源脚本一致:没有真实模板 docx 不得生成合同
126
- const entriesXml = readDocumentXml(templatePath);
127
- const log = [];
128
- let xml = entriesXml;
129
- // ---- 表格处理(先做,避免被段落低阈值抢填)
130
- const tableRanges = topRanges(xml, 'w:tbl');
131
- for (let ti = tableRanges.length - 1; ti >= 0; ti--) {
132
- const range = tableRanges[ti];
133
- const tableXml = xml.slice(range.start, range.end);
134
- const patched = fillTable(tableXml, filler, log);
135
- xml = xml.slice(0, range.start) + patched + xml.slice(range.end);
136
- }
137
- // ---- 正文段落(表格之外的顶层段落)
138
- const paraRanges = collectRanges(xml, 'w:p').filter((r) => !isInside(r.start, topRanges(xml, 'w:tbl')));
139
- for (let i = paraRanges.length - 1; i >= 0; i--) {
140
- const r = paraRanges[i];
141
- const pXml = xml.slice(r.start, r.end);
142
- const full = paragraphFullText(pXml);
143
- if (!full)
144
- continue;
145
- const { text, changed } = fillText(full, 60, filler, log);
146
- if (changed) {
147
- xml = xml.slice(0, r.start) + setParagraphText(pXml, text) + xml.slice(r.end);
148
- }
149
- }
150
- // ---- 追加费用明细
151
- if (options.detail !== 'none' && options.sections?.length) {
152
- xml = appendDetailTable(xml, options.sections, options.amount ?? null);
153
- }
154
- const filled = log.filter((m) => !m.type.endsWith('_unfilled'));
155
- const unfilled = log.filter((m) => m.type.endsWith('_unfilled'));
156
- return { buffer: rewriteDocument(templatePath, xml), result: { filled, unfilled } };
157
- }
158
- function fillTable(tableXml, filler, log) {
159
- // 逐行处理单元格,收集右侧标签
160
- const rowRanges = collectRanges(tableXml, 'w:tr');
161
- let patched = tableXml;
162
- // 从后往前替换,避免位移
163
- const cellLabels = [];
164
- for (const row of rowRanges) {
165
- const rowXml = tableXml.slice(row.start, row.end);
166
- const cellRanges = collectRanges(rowXml, 'w:tc');
167
- const cells = cellRanges.map((cr) => ({
168
- start: cr.start,
169
- xml: rowXml.slice(cr.start, cr.end),
170
- }));
171
- for (let ci = cells.length - 1; ci >= 0; ci--) {
172
- const cell = cells[ci];
173
- const cellText = paragraphFullText(cell.xml);
174
- if (cellText && !hasBlank(cellText) && !hasPlaceholder(cellText))
175
- continue;
176
- let label = '';
177
- for (let j = ci - 1; j >= 0; j--) {
178
- const cand = cells[j];
179
- const candText = paragraphFullText(cand.xml).trim();
180
- if (candText && !hasBlank(candText) && !hasPlaceholder(candText)) {
181
- label = candText.replace(/[::]$/u, '');
182
- break;
183
- }
184
- }
185
- if (!label)
186
- continue;
187
- cellLabels.push({ cellStart: row.start + cell.start, cellXml: cell.xml, label });
188
- }
189
- }
190
- // cellStart 是相对 tableXml 的绝对位置;从后往前替换
191
- cellLabels.sort((a, b) => b.cellStart - a.cellStart);
192
- for (const { cellStart, cellXml, label } of cellLabels) {
193
- const patchedCell = fillTableCell(cellXml, label, filler, log);
194
- patched = patched.slice(0, cellStart) + patchedCell + patched.slice(cellStart + cellXml.length);
195
- }
196
- return patched;
197
- }
198
- function fillTableCell(cellXml, label, filler, log) {
199
- const paraRanges = collectRanges(cellXml, 'w:p');
200
- let patched = cellXml;
201
- let hit = false;
202
- for (let i = paraRanges.length - 1; i >= 0; i--) {
203
- const r = paraRanges[i];
204
- const pXml = cellXml.slice(r.start, r.end);
205
- const full = paragraphFullText(pXml);
206
- const { key, score } = filler.bestKey(label, 95);
207
- const value = key ? filler.valOf(key) : '';
208
- if (!value)
209
- continue;
210
- if (full.trim() === '') {
211
- log.push({ label, key, score, type: 'cell' });
212
- patched = patched.slice(0, r.start) + addRunToParagraph(pXml, value) + patched.slice(r.end);
213
- hit = true;
214
- break;
215
- }
216
- const { text, changed } = fillText(full, 95, filler, log);
217
- if (changed) {
218
- log.push({ label, key, score, type: 'cell' });
219
- patched = patched.slice(0, r.start) + setParagraphText(pXml, text) + patched.slice(r.end);
220
- hit = true;
221
- }
222
- break;
223
- }
224
- void hit;
225
- return patched;
226
- }
227
- function addRunToParagraph(pXml, value) {
228
- const run = `<w:r><w:t xml:space="preserve">${encodeXml(value)}</w:t></w:r>`;
229
- const closeIdx = pXml.lastIndexOf('</w:p>');
230
- if (closeIdx < 0)
231
- return pXml;
232
- return pXml.slice(0, closeIdx) + run + pXml.slice(closeIdx);
233
- }
234
- function appendDetailTable(xml, sections, amount) {
235
- const border = '<w:tblBorders>' +
236
- ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']
237
- .map((s) => `<w:${s} w:val="single" w:sz="4" w:space="0" w:color="000000"/>`)
238
- .join('') +
239
- '</w:tblBorders>';
240
- const cell = (text, bold = false) => '<w:tc><w:tcPr/><w:p><w:r>' +
241
- (bold ? '<w:rPr><w:b/></w:rPr>' : '') +
242
- `<w:t xml:space="preserve">${encodeXml(text)}</w:t></w:r></w:p></w:tc>`;
243
- const row = (...cells) => `<w:tr>${cells.join('')}</w:tr>`;
244
- const rows = [row(cell('费用类别', true), cell('项目', true), cell('数量', true), cell('金额(元)', true))];
245
- for (const sec of sections) {
246
- for (const item of sec.items) {
247
- rows.push(row(cell(`${sec.code}、${sec.title}`), cell(String(item.name ?? item.desc ?? '')), cell(item.qty === null || item.qty === undefined || item.qty === '' ? '' : String(item.qty)), cell(formatAmount(item.amount))));
248
- }
249
- if (sec.total !== null && sec.total !== undefined) {
250
- rows.push(row(cell('小计'), cell(`${sec.code}、${sec.title}`), cell(''), cell(formatAmount(sec.total))));
251
- }
252
- }
253
- if (amount !== null && amount !== '') {
254
- rows.push(row(cell('合计'), cell('合同总金额'), cell(''), cell(formatAmount(amount))));
255
- }
256
- const pageBreak = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>';
257
- const heading = '<w:p><w:pPr><w:outlineLvl w:val="0"/></w:pPr><w:r><w:rPr><w:b/><w:sz w:val="32"/></w:rPr>' +
258
- '<w:t xml:space="preserve">附件:产品及服务费用明细</w:t></w:r></w:p>';
259
- const table = `<w:tbl><w:tblPr><w:tblW w:w="0" w:type="auto"/>${border}</w:tblPr>${rows.join('')}</w:tbl>`;
260
- const sectPr = xml.lastIndexOf('<w:sectPr');
261
- const insertAt = sectPr >= 0 ? sectPr : xml.lastIndexOf('</w:body>');
262
- if (insertAt < 0)
263
- return xml + pageBreak + heading + table;
264
- return xml.slice(0, insertAt) + pageBreak + heading + table + xml.slice(insertAt);
265
- }
266
- function formatAmount(v) {
267
- if (v === null || v === undefined || v === '')
268
- return '';
269
- const s = String(v).replace(/[,¥¥]/gu, '').trim();
270
- const f = Number(s);
271
- if (!Number.isFinite(f))
272
- return String(v);
273
- return f === Math.trunc(f) ? f.toLocaleString('en-US') : f.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
274
- }
275
- function readDocumentXml(templatePath) {
276
- let buffer;
277
- try {
278
- buffer = readFileSync(templatePath);
279
- }
280
- catch {
281
- throw new Error('合同模板缺失:必须先取到真实的 .docx 模板才能生成合同。取不到就停止并请用户提供模板文件;禁止把报价单内容直接生成成一份合同。');
282
- }
283
- try {
284
- return readDocumentXmlFrom(buffer);
285
- }
286
- catch {
287
- throw new Error('模板不是有效的 .docx(可能拿到的是 HTML 或压缩包):请重新取模板;禁止用非 docx 文件当模板,也禁止绕过模板直接生成合同。');
288
- }
289
- }
290
- function readDocumentXmlFrom(buffer) {
291
- return readZipEntry(buffer, 'word/document.xml');
292
- }
293
- function rewriteDocument(templatePath, documentXml) {
294
- return rewriteZip(templatePath, { 'word/document.xml': documentXml });
295
- }