forked from lioensky/VCPChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptoriumCollaboratorService.js
More file actions
1155 lines (1071 loc) · 38 KB
/
Copy pathScriptoriumCollaboratorService.js
File metadata and controls
1155 lines (1071 loc) · 38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const crypto = require('crypto');
let runtime = {
control: null,
logger: console,
};
function initialize(options = {}) {
runtime = {
control: options.services?.scriptoriumAgentControl
|| options.scriptoriumAgentControl
|| null,
logger: options.logger || console,
};
}
function requireControl() {
if (!runtime.control) {
throw new Error('[ScriptoriumCollaborator] Scriptorium Agent 控制服务当前不可用。');
}
return runtime.control;
}
function commandOf(args = {}) {
return String(args.command || args.action || '').trim().toLowerCase();
}
function getSerialCommandEntries(args = {}) {
return Object.entries(args)
.map(([key, value]) => {
const match = key.match(/^command(\d+)$/i);
return match
? { index: Number(match[1]), command: String(value || '').trim() }
: null;
})
.filter((entry) => entry && entry.index > 0 && entry.command)
.sort((a, b) => a.index - b.index);
}
function extractSerialStepArgs(rawArgs, index) {
const step = {};
for (const [key, value] of Object.entries(rawArgs)) {
const match = key.match(/^(.+?)(\d+)$/);
if (!match || Number(match[2]) !== index || match[1].toLowerCase() === 'command') {
continue;
}
step[match[1]] = value;
}
// 未编号字段是串行请求的公共参数,例如 endpoint、maid 与截图延迟。
// command/action 和所有编号字段不能泄漏到单步参数中。
for (const [key, value] of Object.entries(rawArgs)) {
if (
key.toLowerCase() === 'command'
|| key.toLowerCase() === 'action'
|| /\d+$/.test(key)
|| Object.prototype.hasOwnProperty.call(step, key)
) {
continue;
}
step[key] = value;
}
step.command = String(rawArgs[`command${index}`] || '').trim();
return step;
}
function parseWaitMs(args = {}, fallback = 1000) {
let value = args.waitMs ?? args.durationMs ?? args.delayMs;
if (value === undefined && args.seconds !== undefined) {
value = Number(args.seconds) * 1000;
}
const parsed = value === undefined || value === ''
? fallback
: Number(value);
if (!Number.isFinite(parsed) || parsed < 0) {
throw new Error('[ScriptoriumCollaborator] 等待时长必须是非负数。');
}
return Math.min(Math.round(parsed), 30000);
}
function parseVisualDelayMs(args = {}) {
const value = args.visualDelayMs
?? args.captureDelayMs
?? args.screenshotDelayMs;
return parseWaitMs({ waitMs: value }, 750);
}
function delay(waitMs) {
return new Promise((resolve) => setTimeout(resolve, waitMs));
}
function isWaitCommand(command) {
return ['wait', 'sleep', 'delay'].includes(String(command || '').trim().toLowerCase());
}
function isVisualCommand(command) {
return [
'getvisualcontext',
'getviewportimage',
'getslidevisual',
].includes(String(command || '').trim().toLowerCase());
}
function parseObject(value, fieldName, fallback = {}) {
if (value === undefined || value === null || value === '') return fallback;
if (typeof value === 'object' && !Array.isArray(value)) return value;
try {
const parsed = JSON.parse(String(value));
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('根值不是对象');
}
return parsed;
} catch (error) {
throw new Error(`[ScriptoriumCollaborator] ${fieldName} 必须是 JSON 对象:${error.message}`);
}
}
function parseArray(value, fieldName, fallback = []) {
if (value === undefined || value === null || value === '') return fallback;
if (Array.isArray(value)) return value;
try {
const parsed = JSON.parse(String(value));
if (!Array.isArray(parsed)) throw new Error('根值不是数组');
return parsed;
} catch (error) {
throw new Error(`[ScriptoriumCollaborator] ${fieldName} 必须是 JSON 数组:${error.message}`);
}
}
function booleanOf(value, fallback = false) {
if (value === undefined || value === null || value === '') return fallback;
if (typeof value === 'boolean') return value;
const normalized = String(value).trim().toLowerCase();
if (normalized === 'true') return true;
if (normalized === 'false') return false;
throw new Error('[ScriptoriumCollaborator] 布尔参数必须为 true 或 false。');
}
function authorFromMaid(args = {}, executionContext = {}) {
const supplied = executionContext?.vcpContext
&& typeof executionContext.vcpContext === 'object'
? executionContext.vcpContext
: executionContext;
const rawMaid = args.maid ?? args.author ?? args.signature;
const maid = typeof rawMaid === 'string'
? { name: rawMaid.trim() }
: parseObject(rawMaid, 'maid', null);
const name = String(
maid?.name || maid?.signature || maid?.id || ''
).trim();
if (!name) {
throw new Error('[ScriptoriumCollaborator] 写操作缺少 maid 署名字段。');
}
return {
id: String(
maid?.id || supplied?.agentId || supplied?.ownerId || name
).trim(),
name,
type: 'agent',
};
}
function requestIdOf(args = {}, executionContext = {}) {
const trustedRequestId = String(executionContext?.requestId || '').trim();
if (trustedRequestId) return trustedRequestId;
return String(args.requestId || crypto.randomUUID());
}
function endpointFor(args = {}) {
const explicit = String(args.endpoint || args.documentType || '').toLowerCase();
if (['docx', 'vdocx'].includes(explicit)) return 'docx';
if (['pptx', 'vpptx'].includes(explicit)) return 'pptx';
return 'common';
}
const MARKDOWN_FIELD_LABELS = Object.freeze({
success: '成功',
code: '状态码',
message: '消息',
documentId: '文档 ID',
documentKind: '文档类型',
revision: '修订号',
title: '标题',
name: '名称',
dirty: '存在未保存修改',
activeSlideIndex: '当前幻灯片页码',
slideCount: '幻灯片总数',
scene: '场景配置',
programmableContent: '可编程内容',
status: '状态',
dependencies: '依赖',
diagnostics: '诊断信息',
text: '渲染文本',
renderedText: '渲染文本',
pages: '页面',
items: '目录项',
count: '数量',
totalCharacters: '文档总字符数',
index: '目录索引',
level: '标题级别',
characterCount: '章节字符数',
contentCharacterCount: '章节正文字数',
sourceRange: '章节源码范围',
headingRange: '标题源码范围',
records: '历史记录',
results: '检索结果',
query: '检索词',
sourceKind: '源码类型',
slideIndex: '幻灯片页码',
line: '插入行号',
startLine: '起始行',
endLine: '结束行',
totalLines: '总行数',
source: '源码',
html: 'HTML',
documentCss: '文档 CSS',
deckCss: '演示共享 CSS',
context: '上下文源码',
target: '目标源码',
insert: '插入源码',
append: '末尾追加源码',
replace: '替换源码',
replacement: '替换源码',
heading: '章节标题',
visibleBlockIds: '可见文本块 ID',
media: '媒体',
notes: '备注',
note: '备注',
summary: '摘要',
packs: '样式主题包',
pack: '样式主题包',
packId: '样式主题包 ID',
styleCount: '样式数量',
deletedStyleCount: '已删除样式数量',
assets: 'SVG 资产',
asset: 'SVG 资产',
assetId: 'SVG 资产 ID',
assetCount: 'SVG 资产数量',
animatedCount: '动画资产数量',
deletedAssetCount: '已删除 SVG 资产数量',
kind: '类型',
category: '分类',
tags: '标签',
description: '描述',
defaultSize: '默认尺寸',
builtin: '内置只读',
editable: '允许编辑',
builtinPackId: '内置包 ID',
format: '格式',
version: '版本',
maid: 'Maid 署名',
author: '作者',
reviewer: '审阅者',
receipt: '审批回执',
decision: '审批决定',
automatic: '自动审批',
createdAt: '创建时间',
reviewedAt: '审阅时间',
baseRevision: '基础修订号',
operation: '操作',
proposal: '提案',
changeSet: '变更集',
pr: 'PR',
result: '执行结果',
root: '根目录',
docxDirectory: 'VDOCX 目录',
pptxDirectory: 'VPPTX 目录',
defaultConflictPolicy: '默认重名策略',
overwriteRequiresExpectedFileHash: '覆盖需要预期文件哈希',
});
const MARKDOWN_CODE_FIELDS = new Set([
'source',
'html',
'documentCss',
'deckCss',
'context',
'target',
'insert',
'append',
'replace',
'replacement',
]);
function compactDetails(value) {
if (!value || typeof value !== 'object') return {};
const { serialized, snapshot, ...rest } = value;
return rest;
}
function markdownLabel(key) {
return MARKDOWN_FIELD_LABELS[key] || String(key)
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/^./, (character) => character.toUpperCase());
}
function escapeMarkdownInline(value) {
return String(value)
.replace(/\\/g, '\\\\')
.replace(/([`*_[\]<>])/g, '\\$1')
.replace(/\r?\n/g, ' ');
}
function markdownFence(content, language = 'text') {
const text = String(content ?? '').replace(/\r\n?/g, '\n');
const longestFence = Math.max(
0,
...([...text.matchAll(/`+/g)].map((match) => match[0].length))
);
const fence = '`'.repeat(Math.max(3, longestFence + 1));
return `${fence}${language}\n${text}\n${fence}`;
}
function codeLanguage(key, parent = {}) {
if (key === 'deckCss' || key === 'documentCss') return 'css';
if (key === 'html') return 'html';
const sourceKind = String(parent.sourceKind || '').toLowerCase();
if (['deck-css', 'document-css'].includes(sourceKind)) return 'css';
if (sourceKind === 'markdown-hybrid') return 'markdown';
if ([
'source',
'context',
'target',
'insert',
'append',
'replace',
'replacement',
].includes(key)) {
return sourceKind === 'html' || /<\/?[a-z][\s\S]*>/i.test(String(parent[key] || ''))
? 'html'
: 'text';
}
return 'text';
}
function markdownScalar(value) {
if (value === null) return '无';
if (value === undefined) return '未提供';
if (typeof value === 'boolean') return value ? '是' : '否';
return escapeMarkdownInline(value);
}
function markdownValue(key, value, parent, depth = 2) {
const label = markdownLabel(key);
const heading = '#'.repeat(Math.min(6, depth));
if (MARKDOWN_CODE_FIELDS.has(key) && typeof value === 'string') {
return [`${heading} ${label}`, '', markdownFence(value, codeLanguage(key, parent))];
}
if (typeof value === 'string' && value.includes('\n')) {
return [`${heading} ${label}`, '', markdownFence(value, 'text')];
}
if (Array.isArray(value)) {
if (!value.length) return [`- **${label}**:无`];
if (value.every((item) =>
item === null || ['string', 'number', 'boolean'].includes(typeof item)
)) {
return [
`${heading} ${label}`,
'',
...value.map((item) => `- ${markdownScalar(item)}`),
];
}
const lines = [`${heading} ${label}`, ''];
value.forEach((item, index) => {
const itemHeading = '#'.repeat(Math.min(6, depth + 1));
lines.push(`${itemHeading} ${label} ${index + 1}`, '');
if (item && typeof item === 'object') {
lines.push(...markdownObject(item, depth + 2), '');
} else {
lines.push(markdownScalar(item), '');
}
});
return lines.slice(0, -1);
}
if (value && typeof value === 'object') {
return [
`${heading} ${label}`,
'',
...markdownObject(value, depth + 1),
];
}
return [`- **${label}**:${markdownScalar(value)}`];
}
function markdownObject(value, depth = 2) {
const lines = [];
for (const [key, fieldValue] of Object.entries(value || {})) {
if (key === 'serialized' || key === 'snapshot') continue;
const block = markdownValue(key, fieldValue, value, depth);
const blockLike = block[0]?.startsWith('#');
if (blockLike && lines.length && lines.at(-1) !== '') lines.push('');
lines.push(...block);
if (blockLike) lines.push('');
}
while (lines.at(-1) === '') lines.pop();
return lines;
}
function resultText(title, result) {
const markdown = [
`# ${title}`,
'',
...markdownObject(compactDetails(result)),
].join('\n');
return {
content: [{
type: 'text',
text: markdown,
}],
details: result,
};
}
function outlineResultText(result = {}) {
const items = Array.isArray(result.items) ? result.items : [];
const docx = result.sourceKind === 'markdown-hybrid';
if (!docx) return resultText('Scriptorium · getOutline', result);
const lines = [
'# Scriptorium · 分层章节目录',
'',
`- **章节数**:${Number(result.count ?? items.length)}`,
`- **文档总字符数**:${Number(result.totalCharacters || 0)}`,
`- **修订号**:${Number(result.revision || 0)}`,
'',
'> 建议先按标题层级和章节字符数选择少量关键章节,再用 GetSection 按 ID 读取;需要人物、地点或情节细节时使用 SearchSource 全文检索。',
'',
'## 章节',
'',
];
if (!items.length) {
lines.push('(未识别到章节标题)');
} else {
items.forEach((item) => {
const level = Math.max(1, Number(item.level) || 1);
const startLine = Number(item.startLine) || 1;
const endLine = Number(item.endLine) || startLine;
const characters = Number(item.characterCount) || 0;
lines.push(
`${' '.repeat(level - 1)}- [${Number(item.index)}] ${
escapeMarkdownInline(item.text || '未命名章节')
} · L${startLine}-${endLine} · ${characters} 字 · ID: \`${
String(item.id || '').replace(/`/g, '')
}\``
);
});
}
return {
content: [{
type: 'text',
text: lines.join('\n'),
}],
details: result,
};
}
function internalSlideIndex(value, fieldName = 'slideIndex') {
if (value === undefined || value === null || value === '') return undefined;
const pageNumber = Number(value);
if (!Number.isInteger(pageNumber) || pageNumber < 1) {
throw new Error(
`[ScriptoriumCollaborator] ${fieldName} 必须是从 1 开始的整数页码。`
);
}
return pageNumber - 1;
}
function internalizePagePayload(payload = {}, endpoint = 'common') {
if (endpoint === 'docx'
|| !Object.prototype.hasOwnProperty.call(payload, 'slideIndex')) {
return payload;
}
return {
...payload,
slideIndex: internalSlideIndex(payload.slideIndex),
};
}
function externalizePageNumbers(value, context = {}) {
if (Array.isArray(value)) {
return value.map((item) => externalizePageNumbers(item, context));
}
if (!value || typeof value !== 'object') return value;
const deck = context.deck || value.documentKind === 'pptx';
const result = {};
for (const [key, fieldValue] of Object.entries(value)) {
const numberedIndex = deck
&& Number.isInteger(fieldValue)
&& (
key === 'slideIndex'
|| key === 'activeSlideIndex'
|| (
key === 'index'
&& (
context.collection === 'pages'
|| context.collection === 'items'
|| Object.prototype.hasOwnProperty.call(value, 'slideId')
)
)
);
result[key] = numberedIndex
? fieldValue + 1
: externalizePageNumbers(fieldValue, {
deck,
collection: Array.isArray(fieldValue) ? key : context.collection,
});
}
return result;
}
async function call(
args,
method,
payload = {},
endpoint = endpointFor(args),
executionContext = {}
) {
const result = await requireControl().call({
requestId: requestIdOf(args, executionContext),
endpoint,
method,
payload: internalizePagePayload(payload, endpoint),
});
return resultText(
`Scriptorium · ${method}`,
externalizePageNumbers(result, {
deck: endpoint === 'pptx' || result?.documentKind === 'pptx',
})
);
}
async function listFonts(args = {}) {
const result = await requireControl().listFonts({
language: args.language || args.locale || 'all',
forceRefresh: booleanOf(args.forceRefresh, false),
});
const labels = {
'zh-CN': '中文字体',
en: '英文字体 / 拉丁字体',
all: '全部系统字体',
};
const fonts = Array.isArray(result.fonts) ? result.fonts : [];
return {
content: [{
type: 'text',
text: [
`# Scriptorium 可用字体 · ${labels[result.language] || result.language}`,
'',
`共 ${fonts.length} 种已安装字体。以下名称可直接用于 CSS font-family:`,
'',
...fonts.map((font) => `- ${font}`),
].join('\n'),
}],
};
}
async function getDocumentInfo(args) {
return call(args, 'getDocumentInfo');
}
async function getRenderedText(args) {
return call(args, 'getRenderedText', {
slideIndex: args.slideIndex,
});
}
async function getOutline(args, executionContext = {}) {
const endpoint = endpointFor(args);
const result = await requireControl().call({
requestId: requestIdOf(args, executionContext),
endpoint,
method: 'getOutline',
payload: {},
});
const externalized = externalizePageNumbers(result, {
deck: endpoint === 'pptx' || result?.documentKind === 'pptx',
});
return outlineResultText(externalized);
}
async function getSection(args) {
return call(args, 'getSection', {
id: args.id,
index: args.index,
}, 'docx');
}
function defaultSourceKind(args = {}) {
const endpoint = endpointFor(args);
if (endpoint === 'docx') return 'markdown-hybrid';
if (endpoint === 'pptx') return 'html';
// 未指定端点时不强加旧范式,让窗口端口按当前文档类型选择真源。
return undefined;
}
async function getSource(args) {
return call(args, 'getSource', {
sourceKind: args.sourceKind || defaultSourceKind(args),
slideIndex: args.slideIndex,
startLine: args.startLine,
endLine: args.endLine,
});
}
async function searchSource(args) {
if (!String(args.query || '').trim()) {
throw new Error('[ScriptoriumCollaborator] SearchSource 缺少 query。');
}
return call(args, 'searchSource', {
query: String(args.query),
sourceKind: args.sourceKind || 'all',
slideIndex: args.slideIndex,
regex: booleanOf(args.regex, false),
caseSensitive: booleanOf(args.caseSensitive, false),
});
}
async function getViewportSource(args) {
return call(args, 'getViewportSource', {
sourceKind: args.sourceKind || defaultSourceKind(args),
radius: args.radius,
});
}
async function getVisualContext(args, executionContext = {}) {
const endpoint = endpointFor(args);
const result = await requireControl().captureVisualContext({
requestId: requestIdOf(args, executionContext),
endpoint,
scope: args.scope || 'viewport',
slideIndex: endpoint === 'docx'
? args.slideIndex
: internalSlideIndex(args.slideIndex),
format: args.format || args.imageFormat,
quality: args.quality,
stabilizationMs: args.stabilizationMs
?? args.renderStabilizationMs
?? args.visualDelayMs
?? args.captureDelayMs
?? args.screenshotDelayMs,
});
return externalizePageNumbers(result, {
deck: endpoint === 'pptx'
|| result?.details?.documentKind === 'pptx',
});
}
async function getPrHistory(args) {
return call(args, 'getPrHistory', {
limit: args.limit,
status: args.status,
});
}
async function listStylePacks(args) {
return call(args, 'listStylePacks', {
query: args.query,
editableOnly: booleanOf(args.editableOnly, false),
}, 'common');
}
async function getStylePack(args) {
const packId = String(args.packId || args.id || '').trim();
if (!packId) {
throw new Error('[ScriptoriumCollaborator] GetStylePack 缺少 packId。');
}
return call(args, 'getStylePack', { packId }, 'common');
}
async function upsertStylePack(args, executionContext = {}) {
const supplied = args.pack ?? args.source;
if (supplied === undefined || supplied === null || supplied === '') {
throw new Error(
'[ScriptoriumCollaborator] UpsertStylePack 缺少 pack 或 source。'
);
}
const pack = typeof supplied === 'object'
? parseObject(supplied, 'pack')
: parseObject(String(supplied), 'source');
const maid = authorFromMaid(args, executionContext);
return call(args, 'upsertStylePack', {
requestId: requestIdOf(args, executionContext),
pack,
maid,
author: maid,
}, 'common', executionContext);
}
async function deleteStylePack(args, executionContext = {}) {
const packId = String(args.packId || args.id || '').trim();
if (!packId) {
throw new Error(
'[ScriptoriumCollaborator] DeleteStylePack 缺少 packId。'
);
}
const maid = authorFromMaid(args, executionContext);
return call(args, 'deleteStylePack', {
requestId: requestIdOf(args, executionContext),
packId,
maid,
author: maid,
}, 'common', executionContext);
}
async function listSvgAssetPacks(args) {
return call(args, 'listSvgAssetPacks', {
query: args.query,
editableOnly: booleanOf(args.editableOnly, false),
}, 'common');
}
async function listSvgAssets(args) {
return call(args, 'listSvgAssets', {
query: args.query,
packId: args.packId,
category: args.category,
kind: args.kind,
}, 'common');
}
async function getSvgAsset(args) {
const assetId = String(args.assetId || args.id || '').trim();
if (!assetId) {
throw new Error(
'[ScriptoriumCollaborator] GetSvgAsset 缺少 assetId。'
);
}
return call(args, 'getSvgAsset', { assetId }, 'common');
}
async function getSvgAssetPack(args) {
const packId = String(args.packId || args.id || '').trim();
if (!packId) {
throw new Error(
'[ScriptoriumCollaborator] GetSvgAssetPack 缺少 packId。'
);
}
return call(args, 'getSvgAssetPack', { packId }, 'common');
}
async function upsertSvgAssetPack(args, executionContext = {}) {
const supplied = args.pack ?? args.source;
if (supplied === undefined || supplied === null || supplied === '') {
throw new Error(
'[ScriptoriumCollaborator] UpsertSvgAssetPack 缺少 pack 或 source。'
);
}
const pack = typeof supplied === 'object'
? parseObject(supplied, 'pack')
: parseObject(String(supplied), 'source');
const maid = authorFromMaid(args, executionContext);
return call(args, 'upsertSvgAssetPack', {
requestId: requestIdOf(args, executionContext),
pack,
maid,
author: maid,
}, 'common', executionContext);
}
async function deleteSvgAssetPack(args, executionContext = {}) {
const packId = String(args.packId || args.id || '').trim();
if (!packId) {
throw new Error(
'[ScriptoriumCollaborator] DeleteSvgAssetPack 缺少 packId。'
);
}
const maid = authorFromMaid(args, executionContext);
return call(args, 'deleteSvgAssetPack', {
requestId: requestIdOf(args, executionContext),
packId,
maid,
author: maid,
}, 'common', executionContext);
}
async function submitSourcePr(args, executionContext = {}) {
const hasAppend = Object.prototype.hasOwnProperty.call(args, 'append');
const hasInsert = Object.prototype.hasOwnProperty.call(args, 'insert');
const replacements = args.replacements === undefined
? [
hasAppend
? {
append: args.append,
}
: hasInsert
? {
insert: args.insert,
line: args.line,
}
: {
target: args.target,
replace: args.replace ?? args.replacement ?? '',
startLine: args.startLine,
},
]
: parseArray(args.replacements, 'replacements');
const maid = authorFromMaid(args, executionContext);
return call(args, 'submitSourcePr', {
requestId: requestIdOf(args, executionContext),
sourceKind: args.sourceKind || defaultSourceKind(args),
slideIndex: args.slideIndex,
replacements,
expectedRevision: args.expectedRevision,
maid,
author: maid,
summary: String(args.summary || '').trim(),
name: args.name,
note: args.note,
}, endpointFor(args), executionContext);
}
async function mutateSlide(args, method, executionContext = {}) {
const maid = authorFromMaid(args, executionContext);
const deleting = method === 'deleteSlide';
const source = String(args.source || '');
if (!deleting && !source.trim()) {
throw new Error(
'[ScriptoriumCollaborator] AddSlide/InsertSlide 必须通过 source 一次提交包含 <style>、页面 HTML、依赖声明和内联 <script> 的完整单页源码。'
);
}
const payload = {
requestId: requestIdOf(args, executionContext),
slideIndex: args.slideIndex,
name: args.name,
source: deleting ? undefined : source,
notes: args.notes,
transition: args.transition,
resources: parseArray(args.resources, 'resources'),
expectedRevision: args.expectedRevision,
maid,
author: maid,
summary: String(args.summary || '').trim(),
note: args.note,
};
return call(args, method, payload, 'pptx', executionContext);
}
function presentationConfigFromArgs(args = {}) {
const page = {
...parseObject(args.page, 'page'),
};
const presentation = {
...parseObject(args.presentation, 'presentation'),
};
if (args.width !== undefined) page.width = args.width;
if (args.height !== undefined) page.height = args.height;
if (args.gap !== undefined) page.gap = args.gap;
if (args.aspectRatio !== undefined) presentation.aspectRatio = args.aspectRatio;
if (args.theme !== undefined) presentation.theme = args.theme;
if (args.navigation !== undefined) presentation.navigation = args.navigation;
if (args.loop !== undefined) presentation.loop = booleanOf(args.loop, false);
if (args.defaultTransition !== undefined) {
presentation.defaultTransition = parseObject(
args.defaultTransition,
'defaultTransition'
);
} else if (args.transition !== undefined) {
presentation.defaultTransition = typeof args.transition === 'object'
? args.transition
: { type: args.transition, duration: args.transitionDuration };
}
return { page, presentation };
}
async function updatePresentationConfig(args, executionContext = {}) {
const maid = authorFromMaid(args, executionContext);
const config = presentationConfigFromArgs(args);
return call(args, 'updatePresentationConfig', {
requestId: requestIdOf(args, executionContext),
...config,
expectedRevision: args.expectedRevision,
maid,
author: maid,
summary: String(args.summary || '').trim(),
name: args.name,
note: args.note,
}, 'pptx', executionContext);
}
async function createProject(args, executionContext = {}) {
const config = presentationConfigFromArgs(args);
const projectType = String(args.projectType || args.type || '').trim().toLowerCase();
const deck = ['pptx', 'vpptx', 'presentation', 'slide-deck'].includes(projectType);
const source = String(args.source || '');
const slides = parseArray(args.slides, 'slides');
if (!deck && !source.trim()) {
throw new Error(
'[ScriptoriumCollaborator] 创建 VDOCX 必须通过 source 提交非空 Markdown-first 混合源码;文档级样式请使用 documentCss。'
);
}
if (deck && (!slides.length || slides.some((slide) =>
!slide || typeof slide !== 'object' || !String(slide.source || '').trim()
))) {
throw new Error(
'[ScriptoriumCollaborator] 创建 PPTX 必须提供 slides,且每页都必须包含唯一完整 source。'
);
}
return requireControl().createProjectArtifact({
requestId: requestIdOf(args, executionContext),
projectType,
fileName: args.fileName,
title: args.title,
source: deck ? undefined : source,
documentCss: deck ? undefined : String(args.documentCss || ''),
deckCss: deck ? String(args.deckCss || '') : undefined,
slides: deck ? slides : undefined,
page: config.page,
presentation: config.presentation,
maid: authorFromMaid(args, executionContext),
summary: args.summary,
conflictPolicy: args.conflictPolicy || 'rename',
expectedFileHash: args.expectedFileHash,
openAfterCreate: booleanOf(args.openAfterCreate, false),
});
}
async function processSingleToolCall(args, executionContext = {}) {
switch (commandOf(args)) {
case 'listfonts':
case 'getfonts':
return listFonts(args);
case 'getdocumentinfo':
return getDocumentInfo(args);
case 'getrenderedtext':
case 'getfulltext':
return getRenderedText(args);
case 'getoutline':
return getOutline(args, executionContext);
case 'getsection':
return getSection(args);
case 'getsource':
return getSource(args);
case 'searchsource':
return searchSource(args);
case 'getviewportsource':
return getViewportSource(args);
case 'getvisualcontext':
case 'getviewportimage':
case 'getslidevisual':
return getVisualContext(args, executionContext);
case 'getprhistory':
return getPrHistory(args);
case 'liststylepacks':
case 'liststyles':
return listStylePacks(args);
case 'getstylepack':
case 'getstylesource':
return getStylePack(args);
case 'upsertstylepack':
case 'savestylepack':
return upsertStylePack(args, executionContext);
case 'deletestylepack':
return deleteStylePack(args, executionContext);
case 'listsvgassetpacks':
return listSvgAssetPacks(args);
case 'listsvgassets':
return listSvgAssets(args);
case 'getsvgasset':
return getSvgAsset(args);
case 'getsvgassetpack':
return getSvgAssetPack(args);
case 'upsertsvgassetpack':
case 'savesvgassetpack':
return upsertSvgAssetPack(args, executionContext);
case 'deletesvgassetpack':
return deleteSvgAssetPack(args, executionContext);
case 'submitsourcepr':
return submitSourcePr(args, executionContext);
case 'addslide':
return mutateSlide(args, 'addSlide', executionContext);
case 'insertslide':
return mutateSlide(args, 'insertSlide', executionContext);
case 'deleteslide':
return mutateSlide(args, 'deleteSlide', executionContext);
case 'updatepresentationconfig':
case 'updatesceneconfig':
case 'setpresentationconfig':
return updatePresentationConfig(args, executionContext);
case 'createscriptoriumproject':
case 'createproject':
return createProject(args, executionContext);
case 'getstorageinfo':
return resultText(
'Scriptorium 工程存储约定',
requireControl().getStorageInfo()
);
default:
throw new Error(