dsh-docx-format 1.1.1 → 1.2.0
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 +132 -10
- package/cordis.patch.yml +18 -7
- package/index.js +222 -52
- package/lib/docxfmt.py +460 -35
- package/locale/en.json +5 -4
- package/locale/zh.json +5 -4
- package/package.json +52 -52
package/index.js
CHANGED
|
@@ -6,15 +6,14 @@
|
|
|
6
6
|
* 2) systemPrompt 段:把格式硬规则写进提示词,模型一开始就知道该怎么排版。
|
|
7
7
|
* 3) tools/result 观察者:任何工具产出 .docx 路径后,自动排队规范化(默认只处理本次会话新写的文件)。
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* -
|
|
9
|
+
* 请求门控:
|
|
10
|
+
* - 当前会话最后一条用户消息必须命中 triggerKeywords(默认「公文」),否则整个插件不介入;
|
|
11
11
|
* 所以普通对话里生成的 .docx 不会被套上公文版式。
|
|
12
|
-
* - 排除:命中 skipRequestKeywords(默认「实验报告」)时同样不介入。
|
|
13
12
|
* 不介入 = 不排队、不改文件、工具调用直接返回 skipped(显式调用传 force=true 可绕过门控)。
|
|
14
13
|
*
|
|
15
14
|
* 真实排版逻辑在 lib/docxfmt.py(python-docx),本文件只做进程调用与调度。
|
|
16
15
|
*/
|
|
17
|
-
import { existsSync, statSync } from 'node:fs'
|
|
16
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
18
17
|
import { spawn } from 'node:child_process'
|
|
19
18
|
import { isAbsolute, resolve } from 'node:path'
|
|
20
19
|
import { fileURLToPath } from 'node:url'
|
|
@@ -27,6 +26,16 @@ const ENGINE = path.join(HERE, 'lib', 'docxfmt.py')
|
|
|
27
26
|
const READER = path.join(HERE, 'lib', 'docx_read.py')
|
|
28
27
|
const LOADED_AT = Date.now()
|
|
29
28
|
|
|
29
|
+
/* 反馈入口:出问题时不硬塞表单,只把预填链接和一行诊断交给用户,点不点由他决定。 */
|
|
30
|
+
const REPO_URL = 'https://github.com/Romebody/dsh-docx-format'
|
|
31
|
+
const PKG_VERSION = (() => {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(readFileSync(path.join(HERE, 'package.json'), 'utf8')).version ?? 'unknown'
|
|
34
|
+
} catch {
|
|
35
|
+
return 'unknown'
|
|
36
|
+
}
|
|
37
|
+
})()
|
|
38
|
+
|
|
30
39
|
const DEFAULTS = {
|
|
31
40
|
autoFormat: true,
|
|
32
41
|
formatExisting: false,
|
|
@@ -35,10 +44,18 @@ const DEFAULTS = {
|
|
|
35
44
|
python: '',
|
|
36
45
|
maxFilesPerTurn: 8,
|
|
37
46
|
skipPatterns: ['*格式要求*', '*模板*', '~$*', '*/node_modules/*'],
|
|
38
|
-
//
|
|
47
|
+
// 触发词:用户请求文字里必须出现其中之一,插件才套公文版式(空数组 = 任何请求都触发)
|
|
39
48
|
triggerKeywords: ['公文'],
|
|
40
|
-
//
|
|
41
|
-
|
|
49
|
+
// 没提触发词时是否仍做全局规范化(分页开关、中文引号、中英之间去空格)
|
|
50
|
+
globalNormalize: true,
|
|
51
|
+
// 字体档位:standard = 方正小标宋简体/仿宋/黑体/楷体;
|
|
52
|
+
// strict = 方正小标宋_GBK/仿宋_GB2312/方正黑体_GBK/楷体_GB2312(GB/T 9704-2012 的 GBK 系列)
|
|
53
|
+
strictFonts: false,
|
|
54
|
+
// 请求里出现这些词时改用 strict 档(strictFonts=true 则一律 strict)
|
|
55
|
+
strictKeywords: ['严格公文格式', '严格公文'],
|
|
56
|
+
// 字体自检:enabled=false 关掉检测;autoInstall 只认 'ask'(等用户授权)/'off',
|
|
57
|
+
// 'auto' 目前不自动执行——下载安装必须由用户在对话里点头。
|
|
58
|
+
fontCheck: { enabled: true, autoInstall: 'ask' },
|
|
42
59
|
prompt: { enabled: true, order: 3260 },
|
|
43
60
|
tool: { enabled: true },
|
|
44
61
|
defaults: {},
|
|
@@ -53,7 +70,11 @@ function normalizeConfig(config) {
|
|
|
53
70
|
? value.map(String).map((s) => s.trim()).filter(Boolean)
|
|
54
71
|
: fallback)
|
|
55
72
|
cfg.triggerKeywords = toKeywords(cfg.triggerKeywords, DEFAULTS.triggerKeywords)
|
|
56
|
-
cfg.
|
|
73
|
+
cfg.strictKeywords = toKeywords(cfg.strictKeywords, DEFAULTS.strictKeywords)
|
|
74
|
+
cfg.strictFonts = cfg.strictFonts === true
|
|
75
|
+
cfg.fontCheck = { ...DEFAULTS.fontCheck, ...(config?.fontCheck ?? {}) }
|
|
76
|
+
cfg.fontCheck.enabled = cfg.fontCheck.enabled !== false
|
|
77
|
+
cfg.globalNormalize = cfg.globalNormalize !== false
|
|
57
78
|
cfg.prompt = { ...DEFAULTS.prompt, ...(config?.prompt ?? {}) }
|
|
58
79
|
cfg.tool = { ...DEFAULTS.tool, ...(config?.tool ?? {}) }
|
|
59
80
|
cfg.defaults = config?.defaults && typeof config.defaults === 'object' ? config.defaults : {}
|
|
@@ -155,8 +176,11 @@ function parseReport(stdout) {
|
|
|
155
176
|
}
|
|
156
177
|
}
|
|
157
178
|
|
|
158
|
-
async function formatOne(cfg, python, target, mode, recursive) {
|
|
179
|
+
async function formatOne(cfg, python, target, mode, recursive, light, fontProfile) {
|
|
159
180
|
const args = [target, '--mode', mode, '--json']
|
|
181
|
+
if (light) args.push('--light')
|
|
182
|
+
if (fontProfile === 'strict') args.push('--font-profile', 'strict')
|
|
183
|
+
if (cfg.fontCheck?.enabled === false) args.push('--no-font-check')
|
|
160
184
|
if (recursive) args.push('--recursive')
|
|
161
185
|
if (cfg.defaults && Object.keys(cfg.defaults).length > 0) args.push('--config', JSON.stringify(cfg.defaults))
|
|
162
186
|
const run = await runEngine(python, args, cfg.timeoutMs)
|
|
@@ -174,6 +198,9 @@ async function formatOne(cfg, python, target, mode, recursive) {
|
|
|
174
198
|
ok: files.every((f) => f.ok !== false),
|
|
175
199
|
path: report.path ?? target,
|
|
176
200
|
mode,
|
|
201
|
+
light: light === true || files.some((f) => f.light === true) ? true : undefined,
|
|
202
|
+
fontProfile: fontProfile === 'strict' || files.some((f) => f.fontProfile === 'strict') ? 'strict' : undefined,
|
|
203
|
+
fonts: files.find((f) => f.fonts)?.fonts,
|
|
177
204
|
changed: mode === 'apply' ? fixes : 0,
|
|
178
205
|
violations: mode === 'check' ? fixes : 0,
|
|
179
206
|
fixes,
|
|
@@ -330,20 +357,21 @@ export function apply(ctx, config) {
|
|
|
330
357
|
}
|
|
331
358
|
|
|
332
359
|
/**
|
|
333
|
-
*
|
|
334
|
-
* -
|
|
335
|
-
* -
|
|
336
|
-
*
|
|
360
|
+
* 请求门控:决定这条请求用哪种力度处理。
|
|
361
|
+
* - 触发词(triggerKeywords,默认「公文」)非空且未命中 → 'light'(只做全局规范化);globalNormalize=false 时 → 'off';
|
|
362
|
+
* - 命中触发词(或触发词为空数组)→ 'full'(连版式一起套)。
|
|
363
|
+
* 全局规范化 = 分页开关全关、中文引号换“”、中英之间不留空格,与是否提公文无关。
|
|
337
364
|
*/
|
|
338
365
|
const requestGate = (sessionId) => {
|
|
339
366
|
const text = requestTextOf(sessionId)
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
return {
|
|
367
|
+
// 字体档:strictFonts 全局强制,或本条请求里提到「严格公文格式」
|
|
368
|
+
const strictHit = matchKeyword(text, cfg.strictKeywords)
|
|
369
|
+
const profile = cfg.strictFonts || strictHit ? 'strict' : 'standard'
|
|
370
|
+
if (cfg.triggerKeywords.length === 0) return { mode: 'full', text, profile, strictHit }
|
|
371
|
+
const trigger = matchKeyword(text, cfg.triggerKeywords)
|
|
372
|
+
if (trigger) return { mode: 'full', text, trigger, profile, strictHit }
|
|
373
|
+
if (cfg.globalNormalize) return { mode: 'light', text, profile, strictHit }
|
|
374
|
+
return { mode: 'off', text, profile, strictHit }
|
|
347
375
|
}
|
|
348
376
|
|
|
349
377
|
/* ---- 文件排队 ---- */
|
|
@@ -395,17 +423,18 @@ export function apply(ctx, config) {
|
|
|
395
423
|
const job = queue.shift()
|
|
396
424
|
// 出队时再判一次门控:用户可能在这期间改了要求
|
|
397
425
|
const gate = requestGate(job.sessionId)
|
|
398
|
-
if (
|
|
399
|
-
log('info', '[docx-format] skip ' + job.file + ' — '
|
|
400
|
-
|
|
401
|
-
: '请求文字未提触发词「' + cfg.triggerKeywords.join('|') + '」'))
|
|
426
|
+
if (gate.mode === 'off') {
|
|
427
|
+
log('info', '[docx-format] skip ' + job.file + ' — '
|
|
428
|
+
+ '请求文字未提触发词「' + cfg.triggerKeywords.join('|') + '」')
|
|
402
429
|
continue
|
|
403
430
|
}
|
|
404
431
|
formattedByUs.add(job.file)
|
|
405
|
-
const report = await formatOne(cfg, python, job.file, 'apply', false)
|
|
432
|
+
const report = await formatOne(cfg, python, job.file, 'apply', false, gate.mode === 'light', gate.profile)
|
|
406
433
|
handled += 1
|
|
407
434
|
if (report.ok) {
|
|
408
|
-
log('info', '[docx-format] ' + (report.skipped > 0
|
|
435
|
+
log('info', '[docx-format] ' + (report.skipped > 0
|
|
436
|
+
? 'skipped '
|
|
437
|
+
: (report.light ? 'normalized ' : 'formatted ')) + report.path
|
|
409
438
|
+ ' (' + report.changed + ' fixes, ' + report.violations + ' violations)')
|
|
410
439
|
} else {
|
|
411
440
|
log('warn', '[docx-format] failed ' + job.file + ': ' + (report.message ?? 'unknown'))
|
|
@@ -433,15 +462,70 @@ export function apply(ctx, config) {
|
|
|
433
462
|
warnings: { type: 'array', items: { type: 'string' } },
|
|
434
463
|
message: { type: 'string' },
|
|
435
464
|
files: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
465
|
+
fontProfile: { type: 'string' },
|
|
466
|
+
fonts: { type: 'object', additionalProperties: true },
|
|
467
|
+
consentRequired: { type: 'object', additionalProperties: true },
|
|
468
|
+
installed: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
436
469
|
},
|
|
437
470
|
required: ['ok', 'path', 'mode', 'changed', 'violations'],
|
|
438
471
|
additionalProperties: true,
|
|
439
472
|
}
|
|
440
473
|
|
|
474
|
+
/* 出问题时给一条反馈入口:只生成链接与一行诊断,不自动提交任何东西。 */
|
|
475
|
+
const issueUrl = (value) => {
|
|
476
|
+
const head = value?.ok === false
|
|
477
|
+
? ('执行失败 · ' + PKG_VERSION)
|
|
478
|
+
: ('格式偏差 ' + (value?.violations ?? 0) + ' 处 · ' + PKG_VERSION)
|
|
479
|
+
return REPO_URL + '/issues/new?template=bug_report.yml&title=' + encodeURIComponent('[bug] ' + head)
|
|
480
|
+
}
|
|
481
|
+
const diagnostics = (value) => {
|
|
482
|
+
const rules = {}
|
|
483
|
+
for (const f of Array.isArray(value?.files) ? value.files : []) {
|
|
484
|
+
for (const [rule, info] of Object.entries(f?.rules ?? {})) {
|
|
485
|
+
rules[rule] = (rules[rule] ?? 0) + (Number(info?.count) || 0)
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const top = Object.entries(rules)
|
|
489
|
+
.sort((a, b) => b[1] - a[1])
|
|
490
|
+
.slice(0, 6)
|
|
491
|
+
.map(([rule, n]) => rule + '×' + n)
|
|
492
|
+
.join(', ') || '无偏差规则'
|
|
493
|
+
return 'dsh-docx-format ' + PKG_VERSION + ' · ' + process.platform + ' · node ' + process.version
|
|
494
|
+
+ ' · ' + (value?.mode ?? '?') + ' · ' + (value?.fontProfile ?? 'standard') + ' · ' + top
|
|
495
|
+
}
|
|
496
|
+
|
|
441
497
|
const renderReport = (value) => {
|
|
442
498
|
const files = Array.isArray(value?.files) ? value.files : []
|
|
443
499
|
const lines = []
|
|
444
500
|
if (value?.message) lines.push(value.message)
|
|
501
|
+
|
|
502
|
+
/* ---- 字体 ---- */
|
|
503
|
+
if (value?.mode === 'fonts' || value?.mode === 'installFonts') {
|
|
504
|
+
const cr = value?.consentRequired
|
|
505
|
+
if (value.mode === 'installFonts') {
|
|
506
|
+
lines.push('[' + (value.ok ? 'OK' : 'FAIL') + '] 字体安装 — ' + (value.message ?? ''))
|
|
507
|
+
for (const it of value.installed ?? []) {
|
|
508
|
+
const verb = it.ok ? (it.action === 'exists' ? '已存在' : '已安装') : '失败'
|
|
509
|
+
lines.push(' - ' + verb + ' ' + (it.family ?? it.file ?? '') + (it.dest ? (' → ' + it.dest) : ''))
|
|
510
|
+
}
|
|
511
|
+
return [{ type: 'text', text: lines.join('\n') }]
|
|
512
|
+
}
|
|
513
|
+
const info = value.fonts ?? {}
|
|
514
|
+
const missing = Array.isArray(info.missing) ? info.missing : []
|
|
515
|
+
if (missing.length === 0) {
|
|
516
|
+
lines.push('[OK] 字体齐备(profile=' + (value.fontProfile ?? 'standard') + ')')
|
|
517
|
+
} else {
|
|
518
|
+
lines.push('[FAIL] 缺 ' + missing.length + ' 个字体(profile=' + (value.fontProfile ?? 'standard') + '):'
|
|
519
|
+
+ missing.join('、'))
|
|
520
|
+
if (cr) {
|
|
521
|
+
lines.push(' 来源:' + (cr.source ?? ''))
|
|
522
|
+
lines.push(' 体积:约 ' + (cr.mb ?? '?') + ' MB')
|
|
523
|
+
lines.push(' 要不要现在下载安装?同意的话我传 confirm=' + (cr.token ?? '') + ' 走 mode=installFonts。')
|
|
524
|
+
}
|
|
525
|
+
if (info.hint) lines.push(' ' + info.hint)
|
|
526
|
+
}
|
|
527
|
+
return [{ type: 'text', text: lines.join('\n') }]
|
|
528
|
+
}
|
|
445
529
|
if (value?.skipped > 0 && files.length <= 1) {
|
|
446
530
|
lines.push('[SKIP] ' + value.path + ' — 白名单命中,未处理(' + (value.skipReason ?? '') + ')')
|
|
447
531
|
return [{ type: 'text', text: lines.join('\n') }]
|
|
@@ -452,7 +536,9 @@ export function apply(ctx, config) {
|
|
|
452
536
|
: value.mode === 'check'
|
|
453
537
|
? (value.violations > 0 ? '格式偏差 ' + value.violations + ' 处' : '格式完全符合要求')
|
|
454
538
|
: (value.changed > 0 ? '已修正 ' + value.changed + ' 处' : '无需修改')
|
|
455
|
-
lines.push('[' + (value.ok ? 'OK' : 'FAIL') + '] ' + value.path + ' — ' + head
|
|
539
|
+
lines.push('[' + (value.ok ? 'OK' : 'FAIL') + '] ' + value.path + ' — ' + head
|
|
540
|
+
+ (value.fontProfile === 'strict' ? '(字体档:严格公文格式)' : '')
|
|
541
|
+
+ (value.light === true ? '(本条请求没提「公文」,只做全局规范化:分页开关/中文引号/中英之间不留空格)' : ''))
|
|
456
542
|
const only = files[0]
|
|
457
543
|
if (only?.rules) {
|
|
458
544
|
for (const [rule, info] of Object.entries(only.rules)) {
|
|
@@ -461,6 +547,7 @@ export function apply(ctx, config) {
|
|
|
461
547
|
+ (sample ? (': ' + sample.where + ' ' + (sample.current ?? '') + ' -> ' + (sample.expected ?? '')) : ''))
|
|
462
548
|
}
|
|
463
549
|
}
|
|
550
|
+
for (const w of Array.isArray(value.warnings) ? value.warnings : []) lines.push(' ! ' + w)
|
|
464
551
|
} else {
|
|
465
552
|
lines.push('[' + (value.ok ? 'OK' : 'FAIL') + '] ' + files.length + ' 个文件,修正 '
|
|
466
553
|
+ value.changed + ' 处,偏差 ' + value.violations + ' 处'
|
|
@@ -470,6 +557,11 @@ export function apply(ctx, config) {
|
|
|
470
557
|
+ (f.skipped ? ' (' + (f.skipReason ?? '') + ')' : ' (fixes ' + f.violations + ')'))
|
|
471
558
|
}
|
|
472
559
|
}
|
|
560
|
+
if (value?.ok === false || (value?.mode === 'check' && value?.violations > 0)) {
|
|
561
|
+
lines.push('')
|
|
562
|
+
lines.push(' · 反馈:' + issueUrl(value))
|
|
563
|
+
lines.push(' · 诊断:' + diagnostics(value))
|
|
564
|
+
}
|
|
473
565
|
return [{ type: 'text', text: lines.join('\n') }]
|
|
474
566
|
}
|
|
475
567
|
|
|
@@ -480,44 +572,107 @@ export function apply(ctx, config) {
|
|
|
480
572
|
'固定行距 29 磅、首行缩进 2 字符(w:firstLineChars)、段前段后 0 磅、页边距 3.6/3.0/2.7/2.7cm、',
|
|
481
573
|
'表格单元格水平垂直居中与“— 1 —”页码。',
|
|
482
574
|
'mode=apply 直接改文件(幂等,可重复调用);mode=check 只报告偏差不改文件。',
|
|
483
|
-
'
|
|
484
|
-
'
|
|
575
|
+
'用户请求提到「公文」(triggerKeywords)时套完整版式;没提到时仍会做全局规范化',
|
|
576
|
+
'(段落分页六项全关、中文引号换“”、中文里英文前后不加空格),并只改这些;',
|
|
577
|
+
'确需强制套完整版式时传 force=true。',
|
|
578
|
+
'mode=fonts 只报当前字体是否齐备(缺字体时返回 consentRequired,含一次性 token);',
|
|
579
|
+
'mode=installFonts 需要把 token 原样放进 confirm——只有用户明确同意后才调用。',
|
|
485
580
|
].join(''),
|
|
486
581
|
parameters: {
|
|
487
582
|
type: 'object',
|
|
488
583
|
properties: {
|
|
489
|
-
path: { type: 'string', description: '要处理的 .docx 文件或目录(绝对路径最稳)。' },
|
|
490
|
-
mode: { type: 'string', enum: ['apply', 'check'], description: 'apply=规范化并写回;check
|
|
584
|
+
path: { type: 'string', description: '要处理的 .docx 文件或目录(绝对路径最稳)。mode=fonts/installFonts 时可省略。' },
|
|
585
|
+
mode: { type: 'string', enum: ['apply', 'check', 'fonts', 'installFonts'], description: 'apply=规范化并写回;check=只体检;fonts=只报告字体;installFonts=按授权安装字体。默认 apply。' },
|
|
491
586
|
recursive: { type: 'boolean', description: 'path 是目录时是否递归子目录。默认 false。' },
|
|
492
|
-
force: { type: 'boolean', description: '
|
|
587
|
+
force: { type: 'boolean', description: '忽略请求触发词强制处理。默认 false。' },
|
|
588
|
+
confirm: { type: 'string', description: 'mode=installFonts 时必填:mode=fonts 返回的 consentRequired.token,原样传回。' },
|
|
589
|
+
preset: { type: 'string', description: 'mode=installFonts 可选:gongwen(普通档)/ gongwen-strict(严格档)。' },
|
|
590
|
+
fromZip: { type: 'string', description: 'mode=installFonts 可选:字体包 zip 的 URL 或本地路径。' },
|
|
493
591
|
},
|
|
494
|
-
required: [
|
|
592
|
+
required: [],
|
|
495
593
|
additionalProperties: false,
|
|
496
594
|
},
|
|
497
595
|
output: { schema: outputSchema, render: (_args, value) => renderReport(value) },
|
|
498
596
|
async execute(args, exec) {
|
|
499
|
-
const known = new Set(['path', 'mode', 'recursive', 'force'])
|
|
597
|
+
const known = new Set(['path', 'mode', 'recursive', 'force', 'confirm', 'preset', 'fromZip'])
|
|
500
598
|
for (const key of Object.keys(args ?? {})) {
|
|
501
599
|
if (!known.has(key)) throw new Error('docx_format: 未知参数 ' + key)
|
|
502
600
|
}
|
|
601
|
+
const requested = typeof args?.mode === 'string' ? args.mode : 'apply'
|
|
602
|
+
const mode = ['check', 'fonts', 'installFonts'].includes(requested) ? requested : 'apply'
|
|
603
|
+
const gate = args?.force === true ? undefined : requestGate(exec?.agent?.id)
|
|
604
|
+
const profile = gate?.profile === 'strict' ? 'strict' : undefined
|
|
605
|
+
|
|
606
|
+
/* ---- 字体:只报告 ---- */
|
|
607
|
+
if (mode === 'fonts') {
|
|
608
|
+
const argv = ['--fonts', '--json']
|
|
609
|
+
if (profile === 'strict') argv.push('--font-profile', 'strict')
|
|
610
|
+
const run = await runEngine(python, argv, cfg.timeoutMs)
|
|
611
|
+
const info = parseReport(run.stdout)
|
|
612
|
+
if (!info) {
|
|
613
|
+
const detail = (run.stderr || run.stdout || '').trim().split('\n').slice(-3).join(' | ')
|
|
614
|
+
throw new Error('docx_format: 字体检测失败 ' + detail)
|
|
615
|
+
}
|
|
616
|
+
const missing = Array.isArray(info.missing) ? info.missing : []
|
|
617
|
+
log('info', '[docx-format] fonts profile=' + (profile ?? 'standard')
|
|
618
|
+
+ ' missing=' + JSON.stringify(missing))
|
|
619
|
+
return stripUndefined({
|
|
620
|
+
ok: missing.length === 0,
|
|
621
|
+
path: '(字体)',
|
|
622
|
+
mode,
|
|
623
|
+
changed: 0,
|
|
624
|
+
violations: 0,
|
|
625
|
+
fontProfile: info.profile ?? profile ?? 'standard',
|
|
626
|
+
fonts: info,
|
|
627
|
+
consentRequired: info.consentRequired,
|
|
628
|
+
message: missing.length === 0 ? '字体齐备' : info.hint,
|
|
629
|
+
})
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/* ---- 字体:按授权安装 ---- */
|
|
633
|
+
if (mode === 'installFonts') {
|
|
634
|
+
const confirm = typeof args?.confirm === 'string' ? args.confirm.trim() : ''
|
|
635
|
+
if (!confirm) throw new Error('docx_format: mode=installFonts 需要 confirm 令牌(先用 mode=fonts 取 consentRequired.token)')
|
|
636
|
+
const argv = ['--install-fonts', '--json', '--confirm', confirm]
|
|
637
|
+
if (typeof args?.preset === 'string' && args.preset.trim()) argv.push('--preset', args.preset.trim())
|
|
638
|
+
if (typeof args?.fromZip === 'string' && args.fromZip.trim()) argv.push('--from-zip', args.fromZip.trim())
|
|
639
|
+
const run = await runEngine(python, argv, Math.max(cfg.timeoutMs, 900000))
|
|
640
|
+
const info = parseReport(run.stdout)
|
|
641
|
+
if (!info) {
|
|
642
|
+
const detail = (run.stderr || run.stdout || '').trim().split('\n').slice(-3).join(' | ')
|
|
643
|
+
throw new Error('docx_format: 字体安装失败 ' + detail)
|
|
644
|
+
}
|
|
645
|
+
const installed = Array.isArray(info.installed) ? info.installed : []
|
|
646
|
+
const okCount = installed.filter((i) => i?.ok).length
|
|
647
|
+
log('info', '[docx-format] fonts install confirm=ok installed=' + okCount
|
|
648
|
+
+ '/' + installed.length + ' dryRun=' + (info.dryRun === true))
|
|
649
|
+
return stripUndefined({
|
|
650
|
+
ok: okCount > 0 && info.error === undefined,
|
|
651
|
+
path: '(字体)',
|
|
652
|
+
mode,
|
|
653
|
+
changed: 0,
|
|
654
|
+
violations: 0,
|
|
655
|
+
installed,
|
|
656
|
+
message: info.error ?? (okCount + ' 个字体已安装,重启 Word 后生效'),
|
|
657
|
+
})
|
|
658
|
+
}
|
|
659
|
+
|
|
503
660
|
const target = typeof args?.path === 'string' ? args.path.trim() : ''
|
|
504
661
|
if (!target) throw new Error('docx_format: 缺少 path')
|
|
505
|
-
const mode = args?.mode === 'check' ? 'check' : 'apply'
|
|
506
662
|
const recursive = args?.recursive === true
|
|
507
663
|
const file = resolveCandidate(target)
|
|
508
664
|
if (!existsSync(file)) throw new Error('docx_format: 路径不存在 ' + file)
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (
|
|
512
|
-
const reason =
|
|
513
|
-
? '请求文字含排除词「' + gate.keyword + '」,本插件停用'
|
|
514
|
-
: '请求文字未提到「' + cfg.triggerKeywords.join('|') + '」,本插件停用(确需格式化请传 force=true)'
|
|
665
|
+
let light = false
|
|
666
|
+
if (gate) {
|
|
667
|
+
if (gate.mode === 'off') {
|
|
668
|
+
const reason = '请求文字未提到「' + cfg.triggerKeywords.join('|') + '」,本插件停用(确需格式化请传 force=true)'
|
|
515
669
|
log('info', '[docx-format] tool skip ' + file + ' — ' + reason)
|
|
516
670
|
return { ok: true, path: file, mode, changed: 0, violations: 0, fixes: 0,
|
|
517
671
|
changedFiles: 0, skipped: 1, skipReason: reason, warnings: [], files: [] }
|
|
518
672
|
}
|
|
673
|
+
light = gate.mode === 'light'
|
|
519
674
|
}
|
|
520
|
-
const report = await formatOne(cfg, python, file, mode, recursive)
|
|
675
|
+
const report = await formatOne(cfg, python, file, mode, recursive, light, profile)
|
|
521
676
|
if (report.ok && report.changed > 0 && mode === 'apply') formattedByUs.add(file.toLowerCase())
|
|
522
677
|
return report
|
|
523
678
|
},
|
|
@@ -649,6 +804,8 @@ export function apply(ctx, config) {
|
|
|
649
804
|
'',
|
|
650
805
|
'### 字体字号',
|
|
651
806
|
'- 大标题:方正小标宋简体 二号(22pt),居中。',
|
|
807
|
+
'- 副标题(标题下紧跟的那一行,常以「——」开头,如「——在XX会议上的讲话」):仿宋 三号(16pt),居中;',
|
|
808
|
+
' 主标题与副标题之间**不插空行**,标题后的空行从副标题之后算起。',
|
|
652
809
|
'- 正文:仿宋 三号(16pt),两端对齐。一级标题 黑体三号、二级 楷体三号、三级 仿宋三号,',
|
|
653
810
|
' 编号形如「一、」「(一)」「1.」(全角点,序号后不加空格)。',
|
|
654
811
|
'- 数字与字母一律 Times New Roman;破折号「——」用 TNR;时间冒号半角。',
|
|
@@ -680,14 +837,27 @@ export function apply(ctx, config) {
|
|
|
680
837
|
' 引擎(Word/WPS)不可用时退回“首行缩进 2/5 字符、不自动拆段”,折行留给使用者手工换行。',
|
|
681
838
|
' 文件末尾「附件1」黑体三号、无缩进、无冒号,其下与附件标题空一行。',
|
|
682
839
|
'',
|
|
683
|
-
'###
|
|
840
|
+
'### 字体',
|
|
841
|
+
'- 两档字体:普通档 方正小标宋简体 / 仿宋 / 黑体 / 楷体;严格档(请求里出现「严格公文格式」)',
|
|
842
|
+
' 方正小标宋_GBK / 仿宋_GB2312 / 方正黑体_GBK / 楷体_GB2312。数字与字母一律 Times New Roman。',
|
|
843
|
+
'- 不要凭印象说「你缺某字体」:要判断就调 docx_format(mode="fonts"),它返回缺哪几个、约几 MB、从哪下。',
|
|
844
|
+
'- 缺字体只做一件事:把返回的 consentRequired 讲清楚(缺几个、约几 MB、来源),',
|
|
845
|
+
' 问用户一句「要不要现在下载安装」;用户明确同意后,才调 docx_format(mode="installFonts", confirm=<token>)。',
|
|
846
|
+
'- 用户没点头就不要调 installFonts;也不要去别的站点找字体包。安装完提示「重启 Word 后生效」。',
|
|
847
|
+
'',
|
|
848
|
+
'### 触发',
|
|
684
849
|
'- 只有用户在这条要求里明确提到「公文」(如「按公文格式写一份通知」)时,才套用上面这套版式。',
|
|
685
|
-
'-
|
|
686
|
-
'
|
|
850
|
+
'- 用户没提公文时,不要主动套公文格式,按普通文档/报告排版——但下面的「全局规则」照旧全部适用。',
|
|
851
|
+
'',
|
|
852
|
+
'### 全局规则(与是否提公文无关,一律适用)',
|
|
853
|
+
'- 段落「换行和分页」:孤行控制、与下段同页、段中不分页、段前分页、取消行号、取消断字,**全部不启用**',
|
|
854
|
+
' (`w:widowControl="0"`、`w:keepNext="0"`、`w:keepLines="0"`、`w:pageBreakBefore="0"`、',
|
|
855
|
+
' `w:suppressLineNumbers="0"`、`w:suppressAutoHyphens="0"`)。',
|
|
856
|
+
'- 中文里不要用「」『』当引号,一律用“”‘’(「」只出现在讲格式规则的说明文字里)。',
|
|
857
|
+
'- 中英混排:见下一节。',
|
|
687
858
|
'',
|
|
688
859
|
'### 中英混排(与是否公文无关,一律适用)',
|
|
689
|
-
'- 中文内容里出现的英文单词或缩写(如 AI、PPT、OA、Word)前后**不加空格**:写「使用AI工具」「OA系统」「Word文档」。',
|
|
690
|
-
'- 正常的英文句子、英文段落仍按英文习惯加空格:「Please submit the form by Friday.」。',
|
|
860
|
+
'- 中文内容里出现的英文单词或缩写(如 AI、PPT、OA、Word)前后**不加空格**:写「使用AI工具」「OA系统」「Word文档」。', '- 正常的英文句子、英文段落仍按英文习惯加空格:「Please submit the form by Friday.」。',
|
|
691
861
|
'- 中英混排的英文用 Times New Roman;中文用仿宋(标题按各自规定)。',
|
|
692
862
|
'- 段落里不要开「自动调整中文与西文/数字的间距」(`w:autoSpaceDE` / `w:autoSpaceDN` 置 0),',
|
|
693
863
|
' 否则即使没有空格字符,渲染出来仍像「使用 AI 工具」。',
|
|
@@ -740,10 +910,8 @@ export function apply(ctx, config) {
|
|
|
740
910
|
if (result?.isError === true) return
|
|
741
911
|
const sessionId = exec?.agent?.id
|
|
742
912
|
const gate = requestGate(sessionId)
|
|
743
|
-
if (
|
|
744
|
-
log('info', '[docx-format]
|
|
745
|
-
? ('排除词命中「' + gate.keyword + '」')
|
|
746
|
-
: '本条请求未提「公文」') + ' — 不自动格式化')
|
|
913
|
+
if (gate.mode === 'off') {
|
|
914
|
+
log('info', '[docx-format] 本条请求未提「公文」 — 不自动格式化')
|
|
747
915
|
return
|
|
748
916
|
}
|
|
749
917
|
for (const candidate of extractDocxPaths(exec, result)) enqueue(candidate, sessionId)
|
|
@@ -757,5 +925,7 @@ export function apply(ctx, config) {
|
|
|
757
925
|
log('info', '[docx-format] ready python=' + python
|
|
758
926
|
+ ' autoFormat=' + cfg.autoFormat
|
|
759
927
|
+ ' trigger=' + JSON.stringify(cfg.triggerKeywords)
|
|
760
|
-
+ '
|
|
928
|
+
+ ' strict=' + JSON.stringify(cfg.strictKeywords)
|
|
929
|
+
+ ' strictFonts=' + cfg.strictFonts
|
|
930
|
+
+ ' fontCheck=' + cfg.fontCheck.enabled)
|
|
761
931
|
}
|