forked from lioensky/VCPToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageProcessor.js
More file actions
911 lines (787 loc) · 43.5 KB
/
Copy pathmessageProcessor.js
File metadata and controls
911 lines (787 loc) · 43.5 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
// modules/messageProcessor.js
const fs = require('fs').promises;
const path = require('path');
const lunarCalendar = require('chinese-lunar-calendar');
const agentManager = require('./agentManager.js'); // 引入新的Agent管理器
const tvsManager = require('./tvsManager.js'); // 引入新的TVS管理器
const toolboxManager = require('./toolboxManager.js');
const dynamicToolRegistry = require('./dynamicToolRegistry.js');
const sarPromptManager = require('./sarPromptManager.js');
const DEFAULT_TIMEZONE = process.env.DEFAULT_TIMEZONE || 'Asia/Shanghai';
const REPORT_TIMEZONE = process.env.REPORT_TIMEZONE || DEFAULT_TIMEZONE; // 用于控制 AI 报告的时间,默认回退到根目录 config.env 的 DEFAULT_TIMEZONE
function resolveAgentDir() {
const configPath = process.env.AGENT_DIR_PATH;
if (!configPath || typeof configPath !== 'string' || configPath.trim() === '') {
return path.join(__dirname, '..', 'Agent');
}
const normalizedPath = path.normalize(configPath.trim());
return path.isAbsolute(normalizedPath)
? normalizedPath
: path.resolve(__dirname, '..', normalizedPath);
}
const AGENT_DIR = resolveAgentDir();
function resolveTvsDir() {
const configPath = process.env.TVSTXT_DIR_PATH;
if (!configPath || typeof configPath !== 'string' || configPath.trim() === '') {
return path.join(__dirname, '..', 'TVStxt');
}
const normalizedPath = path.normalize(configPath.trim());
return path.isAbsolute(normalizedPath)
? normalizedPath
: path.resolve(__dirname, '..', normalizedPath);
}
const TVS_DIR = resolveTvsDir();
const VCP_ASYNC_RESULTS_DIR = path.join(__dirname, '..', 'VCPAsyncResults');
function escapeRegExp(value) {
return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function replaceFirstAliasPlaceholder(text, alias, replacementText, prefix = '') {
const escapedAlias = escapeRegExp(alias);
const escapedPrefix = prefix ? `${escapeRegExp(prefix)}:` : '';
const aliasPlaceholderRegex = new RegExp(`\\{\\{(?:${escapedPrefix})?${escapedAlias}\\}\\}`, 'g');
let hasReplacedFirst = false;
return String(text).replace(aliasPlaceholderRegex, () => {
if (hasReplacedFirst) {
return '';
}
hasReplacedFirst = true;
return replacementText;
});
}
const SYSTEM_USER_PREFIX_REGEX = /^\s*\[系统[^\]]*\]/;
const SYSTEM_NOTIFICATION_PREFIX_REGEX = /^\s*\[系统通知[::]?\]/;
const SYSTEM_EMPTY_PROMPT_PREFIX_REGEX = /^\s*\[系统提示:\]无内容/;
const SYSTEM_INVITATION_PREFIX_REGEX = /^\s*\[系统邀请指令[::]?\]/;
const VCP_TOOL_PAYLOAD_PREFIX_REGEX = /^\s*<!-- VCP_TOOL_PAYLOAD -->/;
function extractTextFromMessageContent(content) {
if (typeof content === 'string') return content;
if (Array.isArray(content)) {
return content
.filter(part => part && part.type === 'text' && typeof part.text === 'string')
.map(part => part.text)
.join('\n')
.trim();
}
if (content && typeof content === 'object' && typeof content.text === 'string') {
return content.text;
}
return '';
}
function isSystemNotificationText(text) {
return SYSTEM_NOTIFICATION_PREFIX_REGEX.test(String(text || ''));
}
function isBetaSystemUserText(text) {
const normalizedText = String(text || '');
if (!normalizedText) return false;
if (isSystemNotificationText(normalizedText)) return false;
return SYSTEM_USER_PREFIX_REGEX.test(normalizedText);
}
function stripSystemNotificationBlocks(text) {
if (!text || typeof text !== 'string') return text || '';
return text.replace(/\[系统通知[::]?\][\s\S]*?\[系统通知结束\]/g, '').trim();
}
function findLastRealUserMessage(messages, options = {}) {
if (!Array.isArray(messages)) {
return { index: -1, rawContent: '', sanitizedContent: '' };
}
const sanitizer = typeof options.sanitize === 'function' ? options.sanitize : null;
const skipBetaSystemUser = options.skipBetaSystemUser !== false;
const skipEmptySystemPrompt = options.skipEmptySystemPrompt !== false;
const skipSystemInvitation = options.skipSystemInvitation !== false;
const skipToolPayload = options.skipToolPayload !== false;
for (let index = messages.length - 1; index >= 0; index--) {
const message = messages[index];
if (!message || message.role !== 'user') continue;
const rawContent = extractTextFromMessageContent(message.content);
if (!rawContent || !rawContent.trim()) continue;
if (skipEmptySystemPrompt && SYSTEM_EMPTY_PROMPT_PREFIX_REGEX.test(rawContent.trim())) {
continue;
}
if (skipSystemInvitation && SYSTEM_INVITATION_PREFIX_REGEX.test(rawContent.trim())) {
continue;
}
if (skipToolPayload && VCP_TOOL_PAYLOAD_PREFIX_REGEX.test(rawContent.trim())) {
continue;
}
if (skipBetaSystemUser && isBetaSystemUserText(rawContent)) {
continue;
}
const sanitizedContent = sanitizer
? sanitizer(rawContent, 'user')
: stripSystemNotificationBlocks(rawContent);
if (!sanitizedContent || !sanitizedContent.trim()) {
continue;
}
return {
index,
message,
rawContent,
sanitizedContent: sanitizedContent.trim()
};
}
return { index: -1, rawContent: '', sanitizedContent: '' };
}
async function resolveAllVariables(text, model, role, context, processingStack = new Set()) {
if (text == null) return '';
let processedText = String(text);
// 🔒 安全防护:Agent 和 Toolbox 占位符仅在特权角色中展开
// 特权角色包括:1) 标准 system 消息 2) VCPTavern 注入的以 [系统提示:] / [系统邀请指令:] 开头的 user 消息
// 防止用户在普通 user/assistant 消息中通过 {{agent:XXX}} 注入来读取 Agent prompt 或触发意外展开
const isPrivilegedRole = (role === 'system') || (role === 'user' && (processedText.startsWith('[系统提示:]') || processedText.startsWith('[系统邀请指令:]')));
// 通用正则表达式,匹配所有 {{...}} 格式的占位符
// CJK Radicals Supplement - Ideographic Description Characters 0x2E80 - 0x2FFF
// Hiragana - CJK Unified Ideographs 0x3040 - 0x9FFF
// 跳过标点符号 CJK Symbols and Punctuation 0x3000 - 0x303F
// 扩展支持 @ 和 #%&^+-_ 符号
const placeholderRegex = /\{\{([a-zA-Z0-9_:@#%&^+_\-\u2e80-\u2fff\u3040-\u9fff]+)\}\}/g;
const matches = [...processedText.matchAll(placeholderRegex)];
// 提取所有潜在的别名(去除 "agent:" / "toolbox:" 前缀)
const allAliases = new Set(matches.map(match => match[1].replace(/^(agent:|toolbox:)/, '')));
if (isPrivilegedRole) {
for (const alias of allAliases) {
// 关键:使用 agentManager 来判断这是否是一个真正的Agent
if (agentManager.isAgent(alias)) {
// 🔒 灵魂级安全:Agent 占位符在整个上下文中只允许展开一个
// 如果已有其他 Agent 被展开,当前 Agent 占位符静默移除(替换为空串)
if (context.expandedAgentName !== undefined && context.expandedAgentName !== null) {
if (context.expandedAgentName !== alias) {
// 已有不同的 Agent 被展开,静默移除当前占位符
if (context.DEBUG_MODE) {
console.log(`[AgentGuard] Agent '${alias}' 被拒绝展开:上下文中已展开 '${context.expandedAgentName}',仅允许一个 Agent`);
}
processedText = processedText.replaceAll(`{{${alias}}}`, '').replaceAll(`{{agent:${alias}}}`, '');
continue;
}
// 同名 Agent 在后续消息中重复出现,也静默移除(首次已展开)
processedText = processedText.replaceAll(`{{${alias}}}`, '').replaceAll(`{{agent:${alias}}}`, '');
continue;
}
if (processingStack.has(alias)) {
console.error(`[AgentManager] Circular dependency detected! Stack: [${[...processingStack].join(' -> ')} -> ${alias}]`);
const errorMessage = `[Error: Circular agent reference detected for '${alias}']`;
processedText = processedText.replaceAll(`{{${alias}}}`, errorMessage).replaceAll(`{{agent:${alias}}}`, errorMessage);
continue;
}
const agentContent = await agentManager.getAgentPrompt(alias);
processingStack.add(alias);
const resolvedAgentContent = await resolveAllVariables(agentContent, model, role, context, processingStack);
processingStack.delete(alias);
// 替换两种可能的Agent占位符格式
processedText = processedText.replaceAll(`{{${alias}}}`, resolvedAgentContent);
processedText = processedText.replaceAll(`{{agent:${alias}}}`, resolvedAgentContent);
// 标记此 Agent 已被展开,后续消息中的任何 Agent 占位符都将被忽略
context.expandedAgentName = alias;
}
}
// 在所有Agent都被递归展开后,处理 toolbox 占位符
for (const alias of allAliases) {
if (toolboxManager.isToolbox(alias)) {
// 🔒 Toolbox 去重:每种 toolbox 在整个上下文中只展开一次
// 同名 toolbox 在后续消息中重复出现时静默移除
if (context.expandedToolboxes && context.expandedToolboxes.has(alias)) {
if (context.DEBUG_MODE) {
console.log(`[ToolboxGuard] Toolbox '${alias}' 已在之前的消息中展开,跳过重复展开`);
}
processedText = processedText
.replaceAll(`{{${alias}}}`, '')
.replaceAll(`{{toolbox:${alias}}}`, '');
continue;
}
const stackKey = `toolbox:${alias}`;
if (processingStack.has(stackKey)) {
const errorMessage = `[Error: Circular toolbox reference detected for '${alias}']`;
processedText = processedText
.replaceAll(`{{${alias}}}`, errorMessage)
.replaceAll(`{{toolbox:${alias}}}`, errorMessage);
continue;
}
processingStack.add(stackKey);
const foldObj = await toolboxManager.getFoldObject(alias);
const expandedText = await resolveDynamicFoldProtocol(
foldObj,
context,
`{{${alias}}}`
);
processingStack.delete(stackKey);
processedText = replaceFirstAliasPlaceholder(processedText, alias, expandedText, 'toolbox');
// 标记此 Toolbox 已展开
if (context.expandedToolboxes) {
context.expandedToolboxes.add(alias);
}
}
}
}
// 处理剩余的非Agent占位符
processedText = await replacePriorityVariables(processedText, context, role);
processedText = await replaceOtherVariables(processedText, model, role, context);
return processedText;
}
const STATIC_FOLD_MODE_REGEX = /\[\[VCPStaticFold::(Auto|Lite|Full)\]\]/gi;
function extractStaticFoldMode(text) {
const rawText = String(text || '');
let mode = 'auto';
let match;
while ((match = STATIC_FOLD_MODE_REGEX.exec(rawText)) !== null) {
mode = String(match[1] || 'Auto').toLowerCase();
}
STATIC_FOLD_MODE_REGEX.lastIndex = 0;
return mode;
}
function removeStaticFoldModePlaceholders(text) {
STATIC_FOLD_MODE_REGEX.lastIndex = 0;
return String(text || '').replace(STATIC_FOLD_MODE_REGEX, '').trim();
}
function getNormalizedFoldBlocks(foldObj) {
if (!foldObj || !Array.isArray(foldObj.fold_blocks)) return [];
return foldObj.fold_blocks
.map((block, index) => ({
...block,
_originalIndex: index,
threshold: Number.isFinite(Number(block?.threshold)) ? Number(block.threshold) : 0
}))
.filter(block => block && typeof block.content === 'string');
}
function resolveStaticFoldLite(foldObj) {
const blocks = getNormalizedFoldBlocks(foldObj);
if (blocks.length === 0) return '';
const sortedBlocks = [...blocks].sort((a, b) => {
if (a.threshold !== b.threshold) return a.threshold - b.threshold;
return a._originalIndex - b._originalIndex;
});
return sortedBlocks[0]?.content || '';
}
function resolveStaticFoldFull(foldObj) {
const blocks = getNormalizedFoldBlocks(foldObj);
if (blocks.length === 0) return '';
return blocks
.sort((a, b) => a._originalIndex - b._originalIndex)
.map(block => block.content)
.filter(Boolean)
.join('\n\n---\n\n');
}
// 🌟 新增:动态折叠协议处理器
async function resolveDynamicFoldProtocol(foldObj, context, placeholderKey) {
if (!foldObj || !foldObj.vcp_dynamic_fold || !Array.isArray(foldObj.fold_blocks) || foldObj.fold_blocks.length === 0) {
return `[无效的动态折叠数据结构: ${placeholderKey}]`;
}
const blocks = foldObj.fold_blocks
.filter(block => block && typeof block.content === 'string');
const blocksByThreshold = [...blocks].sort((a, b) => (b.threshold || 0) - (a.threshold || 0));
const fallbackBlock = [...blocksByThreshold].reverse().find(block => block.content)
|| { threshold: 0.0, content: '' };
try {
const ragPlugin = context.pluginManager.messagePreprocessors?.get('RAGDiaryPlugin');
if (!ragPlugin || typeof ragPlugin.getSingleEmbeddingCached !== 'function') {
if (context.DEBUG_MODE) console.log(`[DynamicFold] RAGDiaryPlugin 不可用,返回基础内容 (${placeholderKey})`);
return fallbackBlock.content;
}
const contextMessages = context.messages || [];
const lastUserMessage = findLastRealUserMessage(contextMessages, {
sanitize: typeof ragPlugin.sanitizeForEmbedding === 'function'
? ragPlugin.sanitizeForEmbedding.bind(ragPlugin)
: null
});
const lastAiMessageIndex = contextMessages.findLastIndex(m => m.role === 'assistant');
let userContent = lastUserMessage.sanitizedContent || '';
let aiContent = null;
if (lastAiMessageIndex > -1) {
const m = contextMessages[lastAiMessageIndex];
aiContent = extractTextFromMessageContent(m.content);
}
if (!userContent) {
if (context.DEBUG_MODE) console.log(`[DynamicFold] 未找到 User 文本消息,返回基础内容 (${placeholderKey})`);
return fallbackBlock.content;
}
if (typeof ragPlugin.sanitizeForEmbedding === 'function') {
if (aiContent) {
const originalAiContent = aiContent;
aiContent = ragPlugin.sanitizeForEmbedding(aiContent, 'assistant');
if (context.DEBUG_MODE && originalAiContent.length !== aiContent.length) {
console.log('[DynamicFold] AI content was sanitized via unified sanitizer.');
}
}
} else {
if (typeof ragPlugin._stripSystemNotification === 'function') {
if (userContent) {
userContent = ragPlugin._stripSystemNotification(userContent);
userContent = ragPlugin._stripHtml(userContent);
userContent = ragPlugin._stripEmoji(userContent);
userContent = ragPlugin._stripToolMarkers(userContent);
}
}
if (aiContent && typeof ragPlugin._stripHtml === 'function') {
aiContent = ragPlugin._stripHtml(aiContent);
aiContent = ragPlugin._stripEmoji(aiContent);
aiContent = ragPlugin._stripToolMarkers(aiContent);
}
}
const config = ragPlugin.ragParams?.RAGDiaryPlugin || {};
const mainWeights = config.mainSearchWeights || [0.7, 0.3];
const fuzzyConfig = ragPlugin.ragParams?.ContextFoldingV2?.fuzzyEmbedding || {};
const fuzzyOptions = {
threshold: Number.isFinite(Number(fuzzyConfig.threshold)) ? Number(fuzzyConfig.threshold) : 0.985,
minLength: Number.isFinite(Number(fuzzyConfig.minLength)) ? Number(fuzzyConfig.minLength) : 80,
maxScan: Number.isFinite(Number(fuzzyConfig.maxScan)) ? Number(fuzzyConfig.maxScan) : 200,
maxLengthDiffRatio: Number.isFinite(Number(fuzzyConfig.maxLengthDiffRatio)) ? Number(fuzzyConfig.maxLengthDiffRatio) : 0.02,
maxLengthDiffAbs: Number.isFinite(Number(fuzzyConfig.maxLengthDiffAbs)) ? Number(fuzzyConfig.maxLengthDiffAbs) : 80
};
// DynamicFold 专用向量获取:精确缓存 → 高阈值 fuzzy 缓存 → Embedding API。
// 这里常在 RAGDiaryPlugin 主链路之后运行,AI 文本可能只有极小差异;
// 先 fuzzy 复用可避免动态折叠再次向量化同一段 AI 输出。
const getDynamicFoldEmbedding = async (text, label = 'unknown') => {
if (!text || typeof text !== 'string' || !text.trim()) return null;
if (typeof ragPlugin._getEmbeddingFromCacheOnly === 'function') {
const exact = ragPlugin._getEmbeddingFromCacheOnly(text);
if (exact) return exact;
}
if (typeof ragPlugin._findFuzzyEmbeddingFromCache === 'function') {
const fuzzy = ragPlugin._findFuzzyEmbeddingFromCache(text, fuzzyOptions);
if (fuzzy && fuzzy.vector) {
if (context.DEBUG_MODE) {
console.log(
`[DynamicFold] Fuzzy embedding cache hit (${label}): ` +
`sim=${fuzzy.similarity.toFixed(4)}, len=${text.length}/${fuzzy.length}`
);
}
return fuzzy.vector;
}
}
return await ragPlugin.getSingleEmbeddingCached(text);
};
const [uVec, aVec] = await Promise.all([
userContent ? getDynamicFoldEmbedding(userContent, 'user_context') : Promise.resolve(null),
aiContent ? getDynamicFoldEmbedding(aiContent, 'assistant_context') : Promise.resolve(null)
]);
const userVector = ragPlugin._getWeightedAverageVector([uVec, aVec], mainWeights);
if (!userVector) {
if (context.DEBUG_MODE) console.log(`[DynamicFold] 获取用户上下文向量失败,返回基础内容 (${placeholderKey})`);
return fallbackBlock.content;
}
const vectorCache = new Map();
const getDescriptionVector = async (descriptionText) => {
const key = String(descriptionText || '').trim();
if (!key) return null;
if (vectorCache.has(key)) return vectorCache.get(key);
let descVector = null;
if (ragPlugin.vectorDBManager && typeof ragPlugin.vectorDBManager.getPluginDescriptionVector === 'function') {
descVector = await ragPlugin.vectorDBManager.getPluginDescriptionVector(
key,
ragPlugin.getSingleEmbeddingCached.bind(ragPlugin)
);
} else {
descVector = await ragPlugin.getSingleEmbeddingCached(key);
}
vectorCache.set(key, descVector);
return descVector;
};
const cosineSimilarity = (vectorA, vectorB) => {
if (!vectorA || !vectorB) return 0;
let dotProduct = 0;
let normA = 0;
let normB = 0;
const len = Math.min(vectorA.length, vectorB.length);
for (let i = 0; i < len; i++) {
dotProduct += vectorA[i] * vectorB[i];
normA += vectorA[i] * vectorA[i];
normB += vectorB[i] * vectorB[i];
}
return (normA === 0 || normB === 0)
? 0
: dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
};
const toolboxBlockStrategy = foldObj.dynamic_fold_strategy === 'toolbox_block_similarity';
let pluginSimilarity = null;
const getPluginSimilarity = async () => {
if (pluginSimilarity != null) return pluginSimilarity;
const descText = foldObj.plugin_description || placeholderKey;
const descVector = await getDescriptionVector(descText);
if (!descVector) {
if (context.DEBUG_MODE) console.log(`[DynamicFold] 获取插件描述向量失败,返回基础内容 (${placeholderKey})`);
pluginSimilarity = 0;
return pluginSimilarity;
}
pluginSimilarity = cosineSimilarity(descVector, userVector);
if (context.DEBUG_MODE) {
console.log(`[DynamicFold] ${placeholderKey} 上下文相似度: ${pluginSimilarity.toFixed(3)} (目标区块数: ${blocks.length})`);
}
return pluginSimilarity;
};
if (!toolboxBlockStrategy) {
const sim = await getPluginSimilarity();
for (const block of blocksByThreshold) {
const threshold = Number.isFinite(Number(block.threshold)) ? Number(block.threshold) : 0;
if (sim >= threshold) {
if (context.DEBUG_MODE) console.log(`[DynamicFold] ${placeholderKey} 命中阈值 >= ${threshold},展开相关内容。`);
return block.content;
}
}
return fallbackBlock.content;
}
const getThreshold = (block) => Number.isFinite(Number(block.threshold)) ? Number(block.threshold) : 0;
const includedContents = [];
let hiddenBlocksCount = 0;
const legacyBlocks = blocks.filter(block => !(typeof block.description === 'string' && block.description.trim()));
let activeLegacyBlocks = new Set();
if (legacyBlocks.length > 0) {
const legacySim = await getPluginSimilarity();
const matchedLegacyBlocks = legacyBlocks.filter(block => legacySim >= getThreshold(block));
if (matchedLegacyBlocks.length > 0) {
activeLegacyBlocks = new Set(matchedLegacyBlocks);
} else {
const minLegacyThreshold = legacyBlocks.reduce((min, block) => Math.min(min, getThreshold(block)), Infinity);
activeLegacyBlocks = new Set(legacyBlocks.filter(block => getThreshold(block) <= minLegacyThreshold));
}
}
for (const block of blocks) {
const threshold = getThreshold(block);
const description = typeof block.description === 'string' ? block.description.trim() : '';
const content = block.content;
if (!description) {
if (activeLegacyBlocks.has(block)) {
includedContents.push(content);
} else {
hiddenBlocksCount += 1;
}
continue;
}
const descVector = await getDescriptionVector(description);
const sim = cosineSimilarity(descVector, userVector);
if (context.DEBUG_MODE) {
console.log(`[DynamicFold] ${placeholderKey} 区块描述相似度: ${sim.toFixed(3)} / 阈值 ${threshold.toFixed(3)} / 描述 ${description}`);
}
if (sim >= threshold) {
includedContents.push(content);
} else {
hiddenBlocksCount += 1;
}
}
let combinedContent = includedContents.filter(Boolean).join('\n\n---\n\n');
if (!combinedContent) {
combinedContent = fallbackBlock.content;
}
if (hiddenBlocksCount > 0) {
combinedContent += `\n\n*(提示:当前上下文中还隐藏收纳了另外 ${hiddenBlocksCount} 个工具模块分组,您可以通过明确提问或强调相关语境来获得展开。)*`;
}
return combinedContent;
} catch (e) {
console.error(`[DynamicFold] 处理动态折叠时发生异常 (${placeholderKey}):`, e.message);
return fallbackBlock.content;
}
}
function applyDetectorRules(text, role, context = {}) {
const { detectors = [], superDetectors = [] } = context;
if (text == null) return '';
let processedText = String(text);
if (role === 'system') {
for (const rule of detectors) {
if (typeof rule.detector === 'string' && rule.detector.length > 0 && typeof rule.output === 'string') {
processedText = processedText.replaceAll(rule.detector, rule.output);
}
}
}
for (const rule of superDetectors) {
if (typeof rule.detector === 'string' && rule.detector.length > 0 && typeof rule.output === 'string') {
processedText = processedText.replaceAll(rule.detector, rule.output);
}
}
return processedText;
}
function applyDetectorsToMessages(messages, context = {}) {
if (!Array.isArray(messages)) {
return messages;
}
return messages.map((message) => {
const newMessage = JSON.parse(JSON.stringify(message));
if (typeof newMessage.content === 'string') {
newMessage.content = applyDetectorRules(newMessage.content, newMessage.role, context);
} else if (Array.isArray(newMessage.content)) {
newMessage.content = newMessage.content.map((part) => {
if (part && part.type === 'text' && typeof part.text === 'string') {
return {
...part,
text: applyDetectorRules(part.text, newMessage.role, context)
};
}
return part;
});
}
return newMessage;
});
}
async function replaceOtherVariables(text, model, role, context) {
const { pluginManager, cachedEmojiLists, DEBUG_MODE } = context;
if (text == null) return '';
let processedText = String(text);
// SarModel 高级预设注入,对 system 角色或 VCPTavern 注入的 user 角色生效
const systemMarkers = ['[系统提示:]', '[系统邀请指令:]', '[系统通知:]', '[系统通知]'];
const isSystemLike = role === 'system' || (role === 'user' && systemMarkers.some(marker => processedText.startsWith(marker)));
if (isSystemLike) {
// 查找所有独特的 SarPrompt 占位符,例如 {{SarPrompt1}}, {{SarPrompt2}}
const sarPlaceholderRegex = /\{\{(SarPrompt\d+)\}\}/g;
const matches = [...processedText.matchAll(sarPlaceholderRegex)];
const uniquePlaceholders = [...new Set(matches.map(match => match[0]))];
for (const placeholder of uniquePlaceholders) {
// 从 {{SarPrompt4}} 中提取 SarPrompt4
const promptKey = placeholder.substring(2, placeholder.length - 2);
// 从 sarPromptManager 中查找匹配的 promptKey
const prompts = sarPromptManager.getAllPrompts();
const group = prompts.find(g => g.promptKey === promptKey);
let replacementText = ''; // 默认替换为空字符串
if (group && group.models && group.content) {
const modelList = group.models.map(m => m.trim().toLowerCase());
const matchMode = group.matchMode || 'exact';
// 检查当前模型是否匹配(支持exact/includes两种模式)
if (model && sarPromptManager.isModelMatch(modelList, model.toLowerCase(), matchMode)) {
let promptValue = group.content;
// 模型匹配,准备注入的文本
if (typeof promptValue === 'string' && promptValue.toLowerCase().endsWith('.txt')) {
const fileContent = await tvsManager.getContent(promptValue);
if (fileContent.startsWith('[变量文件') || fileContent.startsWith('[处理变量文件')) {
promptValue = fileContent;
} else {
// 递归解析文件内容中的变量
promptValue = await replaceOtherVariables(fileContent, model, role, context);
}
}
replacementText = promptValue;
}
}
// 对当前文本中所有匹配的占位符进行替换
const placeholderRegExp = new RegExp(placeholder.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');
processedText = processedText.replace(placeholderRegExp, replacementText);
}
// === {{SarPromptAll}} 批量模型匹配注入 ===
if (processedText.includes('{{SarPromptAll}}')) {
const allPrompts = sarPromptManager.getAllPrompts();
const matchedContents = [];
for (const group of allPrompts) {
if (!group.models || !group.content) continue;
const modelList = group.models.map(m => m.trim().toLowerCase());
const matchMode = group.matchMode || 'exact';
if (model && sarPromptManager.isModelMatch(modelList, model.toLowerCase(), matchMode)) {
let promptValue = group.content;
// .txt 文件引用支持(复用现有模式)
if (typeof promptValue === 'string' && promptValue.toLowerCase().endsWith('.txt')) {
const fileContent = await tvsManager.getContent(promptValue);
if (fileContent.startsWith('[变量文件') || fileContent.startsWith('[处理变量文件')) {
promptValue = fileContent;
} else {
promptValue = await replaceOtherVariables(fileContent, model, role, context);
}
}
matchedContents.push(promptValue);
}
}
const sarAllText = matchedContents.join('\n');
processedText = processedText.replace(/\{\{SarPromptAll\}\}/g, sarAllText);
}
}
if (role === 'system') {
for (const envKey in process.env) {
if (envKey.startsWith('Tar') || envKey.startsWith('Var')) {
const placeholder = `{{${envKey}}}`;
if (processedText.includes(placeholder)) {
const value = process.env[envKey];
if (value && typeof value === 'string' && value.toLowerCase().endsWith('.txt')) {
const fileContent = await tvsManager.getContent(value);
// 检查内容是否表示错误
if (fileContent.startsWith('[变量文件') || fileContent.startsWith('[处理变量文件')) {
processedText = processedText.replaceAll(placeholder, fileContent);
} else {
const resolvedContent = await replaceOtherVariables(fileContent, model, role, context);
processedText = processedText.replaceAll(placeholder, resolvedContent);
}
} else {
processedText = processedText.replaceAll(placeholder, value || `[未配置 ${envKey}]`);
}
}
}
}
if (processedText.includes('{{VCPDistributedServerList}}')) {
let distributedServerListText = '[VCPDistributedServerList information unavailable]';
try {
const formatter = context.webSocketServer?.formatDistributedServerListForPrompt;
if (typeof formatter === 'function') {
distributedServerListText = formatter();
}
} catch (error) {
console.error('[replaceOtherVariables] Error processing {{VCPDistributedServerList}}:', error);
}
processedText = processedText.replaceAll('{{VCPDistributedServerList}}', distributedServerListText);
}
const now = new Date();
if (DEBUG_MODE) {
console.log(`[TimeVar] Raw Date: ${now.toISOString()}`);
console.log(`[TimeVar] Default Timezone (for internal use): ${DEFAULT_TIMEZONE}`);
console.log(`[TimeVar] Report Timezone (for AI prompt): ${REPORT_TIMEZONE}`);
}
// 使用 REPORT_TIMEZONE 替换时间占位符;REPORT_TIMEZONE 未配置时回退 DEFAULT_TIMEZONE
const date = now.toLocaleDateString('zh-CN', { timeZone: REPORT_TIMEZONE });
processedText = processedText.replace(/\{\{Date\}\}/g, date);
const time = now.toLocaleTimeString('zh-CN', { timeZone: REPORT_TIMEZONE });
processedText = processedText.replace(/\{\{Time\}\}/g, time);
const today = now.toLocaleDateString('zh-CN', { weekday: 'long', timeZone: REPORT_TIMEZONE });
processedText = processedText.replace(/\{\{Today\}\}/g, today);
// 农历/节气也必须按同一报告时区取年月日,不能使用服务器宿主机本地时区
const dateParts = new Intl.DateTimeFormat('zh-CN', {
timeZone: REPORT_TIMEZONE,
year: 'numeric',
month: 'numeric',
day: 'numeric'
}).formatToParts(now);
const year = Number(dateParts.find(part => part.type === 'year')?.value);
const month = Number(dateParts.find(part => part.type === 'month')?.value);
const day = Number(dateParts.find(part => part.type === 'day')?.value);
const lunarDate = lunarCalendar.getLunar(year, month, day);
let yearName = lunarDate.lunarYear.replace('年', '');
let festivalInfo = `${yearName}${lunarDate.zodiac}年${lunarDate.dateStr}`;
if (lunarDate.solarTerm) festivalInfo += ` ${lunarDate.solarTerm}`;
processedText = processedText.replace(/\{\{Festival\}\}/g, festivalInfo);
const staticFoldMode = extractStaticFoldMode(processedText);
processedText = removeStaticFoldModePlaceholders(processedText);
const staticPlaceholderValues = pluginManager.getAllPlaceholderValues(); // Use the getter
if (staticPlaceholderValues && staticPlaceholderValues.size > 0) {
for (const [placeholder, entry] of staticPlaceholderValues.entries()) {
// 修复上下文折叠漏洞:如果当前文本压根没有这个占位符,直接跳过,避免触发不必要的向量化和计算
// 修复占位符前缀包含冲突:使用 {{}} 边界精确匹配,防止短名称吞噬长名称(如 VCPClawMailInbox ⊂ VCPClawMailInboxMail1)
const fullPlaceholder = `{{${placeholder}}}`;
if (!processedText.includes(fullPlaceholder)) {
continue;
}
const escapedPlaceholder = placeholder.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const placeholderRegex = new RegExp('\\{\\{' + escapedPlaceholder + '\\}\\}', 'g');
let valueToInject = entry;
if (typeof entry === 'object' && entry !== null && entry.hasOwnProperty('value')) {
valueToInject = entry.value;
}
// 支持 vcp_dynamic_fold 协议
if (typeof valueToInject === 'object' && valueToInject !== null && valueToInject.vcp_dynamic_fold) {
if (staticFoldMode === 'lite') {
valueToInject = resolveStaticFoldLite(valueToInject);
if (DEBUG_MODE) console.log(`[StaticFold] ${placeholder} 使用 Lite 模式,跳过语义向量判定。`);
} else if (staticFoldMode === 'full') {
valueToInject = resolveStaticFoldFull(valueToInject);
if (DEBUG_MODE) console.log(`[StaticFold] ${placeholder} 使用 Full 模式,跳过语义向量判定。`);
} else {
valueToInject = await resolveDynamicFoldProtocol(valueToInject, context, placeholder);
}
}
processedText = processedText.replace(placeholderRegex, valueToInject || `[${placeholder} 信息不可用]`);
}
}
const individualPluginDescriptions = pluginManager.getIndividualPluginDescriptions();
if (processedText.includes('{{VCPDynamicTools}}')) {
let dynamicToolsText = '[VCPDynamicTools information unavailable]';
try {
dynamicToolsText = await dynamicToolRegistry.buildInjection({
messages: context.messages || context.originalMessages || [],
pluginManager,
debugMode: DEBUG_MODE
});
} catch (error) {
console.error('[replaceOtherVariables] Error processing {{VCPDynamicTools}}:', error);
}
processedText = processedText.replaceAll('{{VCPDynamicTools}}', dynamicToolsText);
}
if (individualPluginDescriptions && individualPluginDescriptions.size > 0) {
for (const [placeholderKey, description] of individualPluginDescriptions) {
processedText = processedText.replaceAll(`{{${placeholderKey}}}`, description || `[${placeholderKey} 信息不可用]`);
}
}
if (processedText.includes('{{VCPAllTools}}')) {
const vcpDescriptionsList = [];
if (individualPluginDescriptions && individualPluginDescriptions.size > 0) {
for (const description of individualPluginDescriptions.values()) {
vcpDescriptionsList.push(description);
}
}
const allVcpToolsString = vcpDescriptionsList.length > 0 ? vcpDescriptionsList.join('\n\n---\n\n') : '没有可用的VCP工具描述信息';
processedText = processedText.replaceAll('{{VCPAllTools}}', allVcpToolsString);
}
if (process.env.PORT) {
processedText = processedText.replaceAll('{{Port}}', process.env.PORT);
}
const effectiveImageKey = pluginManager.getResolvedPluginConfigValue('ImageServer', 'Image_Key');
if (processedText && typeof processedText === 'string' && effectiveImageKey) {
processedText = processedText.replaceAll('{{Image_Key}}', effectiveImageKey);
} else if (processedText && typeof processedText === 'string' && processedText.includes('{{Image_Key}}')) {
if (DEBUG_MODE) console.warn('[replaceOtherVariables] {{Image_Key}} placeholder found in text, but ImageServer plugin or its Image_Key is not resolved. Placeholder will not be replaced.');
}
}
// 同时兼容标准双花括号、异常三花括号、以及被字符串转义后常见的四花括号格式
// 例如:
// {{VCP_ASYNC_RESULT::Plugin::id}}
// {{{VCP_ASYNC_RESULT::Plugin::id}}}
// {{{{VCP_ASYNC_RESULT::Plugin::id}}}}
const asyncResultPlaceholderRegex = /\{\{\{\{VCP_ASYNC_RESULT::([a-zA-Z0-9_.-]+)::([a-zA-Z0-9_-]+)\}\}\}\}|\{\{\{VCP_ASYNC_RESULT::([a-zA-Z0-9_.-]+)::([a-zA-Z0-9_-]+)\}\}\}|\{\{VCP_ASYNC_RESULT::([a-zA-Z0-9_.-]+)::([a-zA-Z0-9_-]+)\}\}/g;
let asyncMatch;
let tempAsyncProcessedText = processedText;
const promises = [];
while ((asyncMatch = asyncResultPlaceholderRegex.exec(processedText)) !== null) {
const placeholder = asyncMatch[0];
const pluginName = asyncMatch[1] || asyncMatch[3] || asyncMatch[5];
const requestId = asyncMatch[2] || asyncMatch[4] || asyncMatch[6];
promises.push(
(async () => {
const resultFilePath = path.join(VCP_ASYNC_RESULTS_DIR, `${pluginName}-${requestId}.json`);
try {
const fileContent = await fs.readFile(resultFilePath, 'utf-8');
const callbackData = JSON.parse(fileContent);
let replacementText = `[任务 ${pluginName} (ID: ${requestId}) 已完成]`;
if (callbackData && callbackData.message) {
replacementText = callbackData.message;
} else if (callbackData && callbackData.status === 'Succeed') {
replacementText = `任务 ${pluginName} (ID: ${requestId}) 已成功完成。详情: ${JSON.stringify(callbackData.data || callbackData.result || callbackData)}`;
} else if (callbackData && callbackData.status === 'Failed') {
replacementText = `任务 ${pluginName} (ID: ${requestId}) 处理失败。原因: ${callbackData.reason || JSON.stringify(callbackData.data || callbackData.error || callbackData)}`;
}
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, replacementText);
} catch (error) {
if (error.code === 'ENOENT') {
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, `[任务 ${pluginName} (ID: ${requestId}) 结果待更新...]`);
} else {
console.error(`[replaceOtherVariables] Error processing async placeholder ${placeholder}:`, error);
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, `[获取任务 ${pluginName} (ID: ${requestId}) 结果时出错]`);
}
}
})()
);
}
await Promise.all(promises);
processedText = tempAsyncProcessedText;
return processedText;
}
async function replacePriorityVariables(text, context, role) {
const { pluginManager, cachedEmojiLists, DEBUG_MODE } = context;
if (text == null) return '';
let processedText = String(text);
// 只在 system role 中处理
if (role !== 'system') {
return processedText;
}
// --- 表情包处理 ---
const emojiPlaceholderRegex = /\{\{([^{}]+?表情包)\}\}/g;
let emojiMatch;
while ((emojiMatch = emojiPlaceholderRegex.exec(processedText)) !== null) {
const placeholder = emojiMatch[0];
const emojiName = emojiMatch[1];
const emojiList = cachedEmojiLists.get(emojiName);
processedText = processedText.replaceAll(placeholder, emojiList || `[${emojiName}列表不可用]`);
}
// --- 日记本处理 (迁移到 RAGDiaryPlugin) ---
// (逻辑已移除)
return processedText;
}
module.exports = {
// 导出主函数,并重命名旧函数以供内部调用
replaceAgentVariables: resolveAllVariables,
replaceOtherVariables,
replacePriorityVariables,
applyDetectorRules,
applyDetectorsToMessages,
extractTextFromMessageContent,
isSystemNotificationText,
isBetaSystemUserText,
stripSystemNotificationBlocks,
findLastRealUserMessage
};