Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
2188 lines (1919 loc) · 120 KB
/
Copy pathserver.js
File metadata and controls
2188 lines (1919 loc) · 120 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
// server.js
const express = require('express');
const dotenv = require('dotenv');
const schedule = require('node-schedule');
const lunarCalendar = require('chinese-lunar-calendar');
const fsSync = require('fs'); // Renamed to fsSync for clarity with fs.promises
const fs = require('fs').promises; // fs.promises for async operations
const path = require('path');
const { Writable } = require('stream');
const DEBUG_LOG_DIR = path.join(__dirname, 'DebugLog'); // Moved DEBUG_LOG_DIR definition higher
let currentServerLogPath = '';
let serverLogWriteStream = null;
// 确保 DebugLog 目录存在 (同步版本,因为在启动时需要)
function ensureDebugLogDirSync() {
if (!fsSync.existsSync(DEBUG_LOG_DIR)) {
try {
fsSync.mkdirSync(DEBUG_LOG_DIR, { recursive: true });
// Use originalConsoleLog if available, otherwise console.log (it might not be overridden yet)
(typeof originalConsoleLog === 'function' ? originalConsoleLog : console.log)(`[ServerSetup] DebugLog 目录已创建: ${DEBUG_LOG_DIR}`);
} catch (error) {
(typeof originalConsoleError === 'function' ? originalConsoleError : console.error)(`[ServerSetup] 创建 DebugLog 目录失败: ${DEBUG_LOG_DIR}`, error);
}
}
}
// 服务器启动时调用
function initializeServerLogger() {
ensureDebugLogDirSync(); // 确保目录存在
const now = new Date();
const timestamp = `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}_${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}${now.getSeconds().toString().padStart(2, '0')}_${now.getMilliseconds().toString().padStart(3, '0')}`;
currentServerLogPath = path.join(DEBUG_LOG_DIR, `ServerLog-${timestamp}.txt`);
try {
fsSync.writeFileSync(currentServerLogPath, `[${new Date().toLocaleString()}] Server log started.\n`, 'utf-8');
serverLogWriteStream = fsSync.createWriteStream(currentServerLogPath, { flags: 'a' });
(typeof originalConsoleLog === 'function' ? originalConsoleLog : console.log)(`[ServerSetup] 服务器日志将记录到: ${currentServerLogPath}`);
} catch (error) {
(typeof originalConsoleError === 'function' ? originalConsoleError : console.error)(`[ServerSetup] 初始化服务器日志文件失败: ${currentServerLogPath}`, error);
serverLogWriteStream = null;
}
}
// 保存原始 console 方法
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalConsoleInfo = console.info;
// 在服务器启动的最开始就初始化日志记录器
initializeServerLogger(); // Call this before console methods are overridden if they log during init
function formatLogMessage(level, args) {
const timestamp = new Date().toLocaleString();
// Handle potential circular structures in objects for JSON.stringify
const safeStringify = (obj) => {
const cache = new Set();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.has(value)) {
return '[Circular]';
}
cache.add(value);
}
return value;
}, 2);
};
const message = args.map(arg => (typeof arg === 'object' ? safeStringify(arg) : String(arg))).join(' ');
return `[${timestamp}] [${level.toUpperCase()}] ${message}\n`;
}
function writeToLogFile(formattedMessage) {
if (serverLogWriteStream) {
serverLogWriteStream.write(formattedMessage, (err) => {
if (err) {
originalConsoleError('[Logger] 写入日志文件失败:', err);
}
});
}
}
console.log = (...args) => {
originalConsoleLog.apply(console, args);
const formattedMessage = formatLogMessage('log', args);
writeToLogFile(formattedMessage);
};
console.error = (...args) => {
originalConsoleError.apply(console, args);
const formattedMessage = formatLogMessage('error', args);
writeToLogFile(formattedMessage);
};
console.warn = (...args) => {
originalConsoleWarn.apply(console, args);
const formattedMessage = formatLogMessage('warn', args);
writeToLogFile(formattedMessage);
};
console.info = (...args) => {
originalConsoleInfo.apply(console, args);
const formattedMessage = formatLogMessage('info', args);
writeToLogFile(formattedMessage);
};
const AGENT_DIR = path.join(__dirname, 'Agent'); // 定义 Agent 目录
const TVS_DIR = path.join(__dirname, 'TVStxt'); // 新增:定义 TVStxt 目录
const crypto = require('crypto');
const { VectorDBManager } = require('./VectorDBManager.js'); // 新增:引入向量数据库管理器
const pluginManager = require('./Plugin.js');
const taskScheduler = require('./routes/taskScheduler.js');
const webSocketServer = require('./WebSocketServer.js'); // 新增 WebSocketServer 引入
const FileFetcherServer = require('./FileFetcherServer.js'); // 引入新的 FileFetcherServer 模块
const vcpInfoHandler = require('./vcpInfoHandler.js'); // 引入新的 VCP 信息处理器
const basicAuth = require('basic-auth');
const cors = require('cors'); // 引入 cors 模块
const activeRequests = new Map(); // 新增:用于存储活动中的请求,以便中止
dotenv.config({ path: 'config.env' });
const ADMIN_USERNAME = process.env.AdminUsername;
const ADMIN_PASSWORD = process.env.AdminPassword;
const DEBUG_MODE = (process.env.DebugMode || "False").toLowerCase() === "true";
const SHOW_VCP_OUTPUT = (process.env.ShowVCP || "False").toLowerCase() === "true"; // 读取 ShowVCP 环境变量
// 新增:模型重定向功能
const ModelRedirectHandler = require('./modelRedirectHandler.js');
const modelRedirectHandler = new ModelRedirectHandler();
// ensureDebugLogDir is now ensureDebugLogDirSync and called by initializeServerLogger
// writeDebugLog remains for specific debug purposes, it uses fs.promises.
async function ensureDebugLogDirAsync() { // Renamed to avoid conflict, used by writeDebugLog
if (DEBUG_MODE) {
try {
await fs.mkdir(DEBUG_LOG_DIR, { recursive: true });
} catch (error) {
// console.error is now overridden, it will log to file too.
console.error(`创建 DebugLog 目录失败 (async): ${DEBUG_LOG_DIR}`, error);
}
}
}
async function writeDebugLog(filenamePrefix, data) {
if (DEBUG_MODE) {
await ensureDebugLogDirAsync(); // Use the async version here
const now = new Date();
const timestamp = `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}_${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}${now.getSeconds().toString().padStart(2, '0')}_${now.getMilliseconds().toString().padStart(3, '0')}`;
const filename = `${filenamePrefix}-${timestamp}.txt`;
const filePath = path.join(DEBUG_LOG_DIR, filename);
try {
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
console.log(`[DebugLog] 已记录日志: ${filename}`); // This console.log will also go to the main server log
} catch (error) {
console.error(`写入调试日志失败: ${filePath}`, error);
}
}
}
const detectors = [];
for (const key in process.env) {
if (/^Detector\d+$/.test(key)) {
const index = key.substring(8);
const outputKey = `Detector_Output${index}`;
if (process.env[outputKey]) {
detectors.push({ detector: process.env[key], output: process.env[outputKey] });
}
}
}
if (detectors.length > 0) console.log(`共加载了 ${detectors.length} 条系统提示词转换规则。`);
else console.log('未加载任何系统提示词转换规则。');
const superDetectors = [];
for (const key in process.env) {
if (/^SuperDetector\d+$/.test(key)) {
const index = key.substring(13);
const outputKey = `SuperDetector_Output${index}`;
if (process.env[outputKey]) {
superDetectors.push({ detector: process.env[key], output: process.env[outputKey] });
}
}
}
if (superDetectors.length > 0) console.log(`共加载了 ${superDetectors.length} 条全局上下文转换规则。`);
else console.log('未加载任何全局上下文转换规则。');
const vectorDBManager = new VectorDBManager(); // 新增:创建 VectorDBManager 实例
const app = express();
app.use(cors({ origin: '*' })); // 启用 CORS,允许所有来源的跨域请求,方便本地文件调试
// 在路由决策之前解析请求体,以便 req.body 可用
app.use(express.json({ limit: '300mb' }));
app.use(express.urlencoded({ limit: '300mb', extended: true }));
app.use(express.text({ limit: '300mb', type: 'text/plain' })); // 新增:用于处理纯文本请求体
// 新增:IP追踪中间件
app.use((req, res, next) => {
if (req.method === 'POST') {
let clientIp = req.ip;
// 标准化IPv6映射的IPv4地址 (e.g., from '::ffff:127.0.0.1' to '127.0.0.1')
if (clientIp && clientIp.substr(0, 7) === "::ffff:") {
clientIp = clientIp.substr(7);
}
// 始终记录收到的POST请求IP
console.log(`[IP Tracker] Received POST request from IP: ${clientIp}`);
const serverName = webSocketServer.findServerByIp(clientIp);
if (serverName) {
console.log(`[IP Tracker] SUCCESS: Post request is from known Distributed Server: '${serverName}' (IP: ${clientIp})`);
}
}
next();
});
// 引入并使用特殊模型路由
const specialModelRouter = require('./routes/specialModelRouter');
app.use(specialModelRouter); // 这个将处理所有白名单模型的请求
const port = process.env.PORT;
const apiKey = process.env.API_Key;
const apiUrl = process.env.API_URL;
const serverKey = process.env.Key;
const cachedEmojiLists = new Map();
// Authentication middleware for Admin Panel and Admin API
const adminAuth = (req, res, next) => {
const isAdminPath = req.path.startsWith('/AdminPanel') || req.path.startsWith('/admin_api');
if (isAdminPath) {
// Check if admin credentials are configured
if (!ADMIN_USERNAME || !ADMIN_PASSWORD) {
console.error('[AdminAuth] AdminUsername or AdminPassword not set in config.env. Admin panel is disabled.');
if (req.path.startsWith('/admin_api') || (req.headers.accept && req.headers.accept.includes('application/json'))) {
res.status(503).json({
error: 'Service Unavailable: Admin credentials not configured.',
message: 'Please set AdminUsername and AdminPassword in the config.env file to enable the admin panel.'
});
} else {
res.status(503).send('<h1>503 Service Unavailable</h1><p>Admin credentials (AdminUsername, AdminPassword) are not configured in config.env. Please configure them to enable the admin panel.</p>');
}
return; // Stop further processing
}
// Credentials are configured, proceed with Basic Auth
const credentials = basicAuth(req);
if (!credentials || credentials.name !== ADMIN_USERNAME || credentials.pass !== ADMIN_PASSWORD) {
res.setHeader('WWW-Authenticate', 'Basic realm="Admin Panel"');
if (req.path.startsWith('/admin_api') || (req.headers.accept && req.headers.accept.includes('application/json'))) {
return res.status(401).json({ error: 'Unauthorized' });
} else {
return res.status(401).send('<h1>401 Unauthorized</h1><p>Authentication required to access the Admin Panel.</p>');
}
}
// Authentication successful
return next();
}
// Not an admin path, proceed
return next();
};
app.use(adminAuth); // Apply admin authentication globally (it will only act on /AdminPanel and /admin_api paths)
// Serve Admin Panel static files (will only be reached if adminAuth passes for /AdminPanel paths)
app.use('/AdminPanel', express.static(path.join(__dirname, 'AdminPanel')));
// Image server logic is now handled by the ImageServer plugin.
// General API authentication (Bearer token) - This was the original one, now adminAuth handles its paths
app.use((req, res, next) => {
// Skip bearer token check for admin panel API and static files, as they use basic auth or no auth
if (req.path.startsWith('/admin_api') || req.path.startsWith('/AdminPanel')) {
return next();
}
const imageServicePathRegex = /^\/pw=[^/]+\/images\//;
if (imageServicePathRegex.test(req.path)) {
return next();
}
// Add a similar check for the FileServer plugin path
const fileServicePathRegex = /^\/pw=[^/]+\/files\//;
if (fileServicePathRegex.test(req.path)) {
return next();
}
// Skip bearer token check for plugin callbacks
if (req.path.startsWith('/plugin-callback')) {
return next();
}
const authHeader = req.headers.authorization;
if (!authHeader || authHeader !== `Bearer ${serverKey}`) {
return res.status(401).json({ error: 'Unauthorized (Bearer token required)' });
}
next();
});
// This function is no longer needed as the EmojiListGenerator plugin handles generation.
// async function updateAndLoadAgentEmojiList(agentName, dirPath, filePath) { ... }
async function replaceAgentVariables(text, model, role) {
if (text == null) return '';
let processedText = String(text);
// This function specifically handles agent placeholders, which should only be in system messages.
if (role !== 'system') {
return processedText;
}
const agentConfigs = {};
for (const envKey in process.env) {
if (envKey.startsWith('Agent')) {
const agentName = envKey.substring(5);
if (agentName) {
agentConfigs[agentName] = process.env[envKey];
}
}
}
// A single pass should be enough if we resolve them recursively, but a loop handles multiple different agents.
for (const agentName in agentConfigs) {
const placeholder = `{{${agentName}}}`;
if (processedText.includes(placeholder)) {
const agentFileName = agentConfigs[agentName];
const agentFilePath = path.join(AGENT_DIR, agentFileName);
try {
let agentFileContent = await fs.readFile(agentFilePath, 'utf-8');
// Recursively resolve agent placeholders within the loaded agent file.
// This handles nested agents like {{AgentA}} containing {{AgentB}}.
let resolvedAgentContent = await replaceAgentVariables(agentFileContent, model, role);
processedText = processedText.replaceAll(placeholder, resolvedAgentContent);
} catch (error) {
let errorMsg;
if (error.code === 'ENOENT') {
errorMsg = `[Agent ${agentName} (${agentFileName}) not found]`;
console.warn(`[Agent] Agent file not found: ${agentFilePath} for placeholder ${placeholder}`);
} else {
errorMsg = `[Error processing Agent ${agentName} (${agentFileName})]`;
console.error(`[Agent] Error reading or processing agent file ${agentFilePath} for placeholder ${placeholder}:`, error.message);
}
processedText = processedText.replaceAll(placeholder, errorMsg);
}
}
}
return processedText;
}
async function replaceOtherVariables(text, model, role) {
if (text == null) return '';
let processedText = String(text);
// 仅在 system role 中执行大多数占位符替换
if (role === 'system') {
// 新增 Tar/Var 变量处理逻辑 (支持 .txt 文件)
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')) {
// 值是 .txt 文件,从 TVStxt 目录读取
const txtFilePath = path.join(TVS_DIR, value);
try {
const fileContent = await fs.readFile(txtFilePath, 'utf-8');
// 递归解析文件内容中的变量
const resolvedContent = await replaceOtherVariables(fileContent, model, role);
processedText = processedText.replaceAll(placeholder, resolvedContent);
} catch (error) {
let errorMsg;
if (error.code === 'ENOENT') {
errorMsg = `[变量 ${envKey} 的文件 (${value}) 未找到]`;
console.warn(`[变量加载] 文件未找到: ${txtFilePath} (占位符: ${placeholder})`);
} else {
errorMsg = `[处理变量 ${envKey} 的文件 (${value}) 时出错]`;
console.error(`[变量加载] 读取文件失败 ${txtFilePath} (占位符: ${placeholder}):`, error.message);
}
processedText = processedText.replaceAll(placeholder, errorMsg);
}
} else {
// 值是直接的字符串
processedText = processedText.replaceAll(placeholder, value || `[未配置 ${envKey}]`);
}
}
}
}
// START: New SarModelX/SarPromptX logic
let sarPromptToInject = null;
// Create a map from a model name to its specific prompt.
const modelToPromptMap = new Map();
for (const envKey in process.env) {
if (/^SarModel\d+$/.test(envKey)) {
const index = envKey.substring(8);
const promptKey = `SarPrompt${index}`;
let promptValue = process.env[promptKey];
const models = process.env[envKey];
if (promptValue && models) {
if (typeof promptValue === 'string' && promptValue.toLowerCase().endsWith('.txt')) {
const txtFilePath = path.join(TVS_DIR, promptValue);
try {
const fileContent = await fs.readFile(txtFilePath, 'utf-8');
// 递归解析文件内容中的变量, 依赖用户配置来避免无限递归
promptValue = await replaceOtherVariables(fileContent, model, role);
} catch (error) {
let errorMsg;
if (error.code === 'ENOENT') {
errorMsg = `[SarPrompt 文件 (${promptValue}) 未找到]`;
console.warn(`[Sar加载] 文件未找到: ${txtFilePath}`);
} else {
errorMsg = `[处理 SarPrompt 文件 (${promptValue}) 时出错]`;
console.error(`[Sar加载] 读取文件失败 ${txtFilePath}:`, error.message);
}
promptValue = errorMsg;
}
}
const modelList = models.split(',').map(m => m.trim()).filter(m => m);
for (const m of modelList) {
modelToPromptMap.set(m, promptValue);
}
}
}
}
// Check if the current request's model has a specific prompt.
if (model && modelToPromptMap.has(model)) {
sarPromptToInject = modelToPromptMap.get(model);
}
// Replace all {{Sar...}} placeholders.
const sarPlaceholderRegex = /\{\{Sar[a-zA-Z0-9_]+\}\}/g;
if (sarPromptToInject !== null) {
// If a specific prompt is found, replace all Sar placeholders with it.
processedText = processedText.replaceAll(sarPlaceholderRegex, sarPromptToInject);
} else {
// If no specific prompt is found for the model, remove all Sar placeholders.
processedText = processedText.replaceAll(sarPlaceholderRegex, '');
}
// END: New SarModelX/SarPromptX logic
const now = new Date();
const date = now.toLocaleDateString('zh-CN', { timeZone: 'Asia/Shanghai' });
processedText = processedText.replace(/\{\{Date\}\}/g, date);
const time = now.toLocaleTimeString('zh-CN', { timeZone: 'Asia/Shanghai' });
processedText = processedText.replace(/\{\{Time\}\}/g, time);
const today = now.toLocaleDateString('zh-CN', { weekday: 'long', timeZone: 'Asia/Shanghai' });
processedText = processedText.replace(/\{\{Today\}\}/g, today);
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
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);
// START: 统一处理所有静态插件占位符 (VCP...)
const staticPlaceholderValues = pluginManager.staticPlaceholderValues;
if (staticPlaceholderValues && staticPlaceholderValues.size > 0) {
for (const [placeholder, value] of staticPlaceholderValues.entries()) {
// placeholder is already like "{{VCPPluginName}}"
const placeholderRegex = new RegExp(placeholder.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');
processedText = processedText.replace(placeholderRegex, value || `[${placeholder} 信息不可用]`);
}
}
// END: 统一处理所有静态插件占位符
// Replace individual VCP plugin descriptions
const individualPluginDescriptions = pluginManager.getIndividualPluginDescriptions();
if (individualPluginDescriptions && individualPluginDescriptions.size > 0) {
for (const [placeholderKey, description] of individualPluginDescriptions) {
// placeholderKey is already like "VCPPluginName"
processedText = processedText.replaceAll(`{{${placeholderKey}}}`, description || `[${placeholderKey} 信息不可用]`);
}
}
// 新增:处理 {{VCPAllTools}} 占位符
if (processedText.includes('{{VCPAllTools}}')) {
const vcpDescriptionsList = [];
// 从 individualPluginDescriptions 添加 (这些由 pluginManager.getIndividualPluginDescriptions() 提供)
if (individualPluginDescriptions && individualPluginDescriptions.size > 0) {
for (const description of individualPluginDescriptions.values()) {
vcpDescriptionsList.push(description);
}
}
// 注意: 如果未来有其他直接通过 pluginManager.getPlaceholderValue("{{VCPCustomXXX}}") 处理的占位符,
// 并且希望它们也列在 {{VCPAllTools}} 中,需要在此处手动添加逻辑来识别并包含它们。
// 使用双换行符和分隔线来分隔各个插件的描述,使其更易读
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.');
}
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}列表不可用`);
}
const diaryPlaceholderRegex = /\{\{(.+?)日记本\}\}/g;
let tempProcessedText = processedText; // Work on a temporary copy
// Attempt to get and parse the AllCharacterDiariesData placeholder from PluginManager
let allDiariesData = {};
const allDiariesDataString = pluginManager.getPlaceholderValue("{{AllCharacterDiariesData}}");
if (allDiariesDataString && !allDiariesDataString.startsWith("[Placeholder")) {
try {
allDiariesData = JSON.parse(allDiariesDataString);
} catch (e) {
console.error(`[replaceOtherVariables] Failed to parse AllCharacterDiariesData JSON: ${e.message}. Data: ${allDiariesDataString.substring(0,100)}...`); // Keep as error
// Keep allDiariesData as an empty object, so individual lookups will fail gracefully
}
} else if (allDiariesDataString && allDiariesDataString.startsWith("[Placeholder")) {
if (DEBUG_MODE) console.warn(`[replaceOtherVariables] Placeholder {{AllCharacterDiariesData}} not found or not yet populated by DailyNoteGet plugin. Value: ${allDiariesDataString}`);
}
// Use a loop that allows for async operations if needed, though simple string replacement is sync here
// We need to re-evaluate regex on each replacement as the string length changes
let match;
while ((match = diaryPlaceholderRegex.exec(tempProcessedText)) !== null) {
const placeholder = match[0]; // e.g., "{{小明同学日记本}}"
const characterName = match[1]; // e.g., "小明同学"
let diaryContent = `[${characterName}日记本内容为空或未从插件获取]`; // Default message
if (allDiariesData.hasOwnProperty(characterName)) {
diaryContent = allDiariesData[characterName];
} else {
// console.warn(`[replaceOtherVariables] Diary for character "${characterName}" not found in AllCharacterDiariesData.`);
// No need to log for every miss, default message is sufficient
}
// ReplaceAll is important if the same character's diary is mentioned multiple times
tempProcessedText = tempProcessedText.replaceAll(placeholder, diaryContent);
// Reset regex lastIndex because the string has changed, to re-scan from the beginning
diaryPlaceholderRegex.lastIndex = 0;
}
processedText = tempProcessedText; // Assign the fully processed text back
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);
}
}
}
// SuperDetectors 和 VCP_ASYNC_RESULT 应用于所有角色的消息
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);
}
}
// START: VCP_ASYNC_RESULT Placeholder Processing
const asyncResultPlaceholderRegex = /\{\{VCP_ASYNC_RESULT::([a-zA-Z0-9_.-]+)::([a-zA-Z0-9_-]+)\}\}/g;
let asyncMatch;
// We need to handle multiple occurrences, and since replacement changes string length,
// we collect all matches first, then replace. Or, more simply, loop until no more matches.
// For simplicity in this step, let's assume a loop that re-evaluates.
// A more robust way would be to collect all placeholders and their values first.
// Create a new string for replacements to avoid issues with changing string length during regex exec
let tempAsyncProcessedText = processedText;
const promises = [];
while ((asyncMatch = asyncResultPlaceholderRegex.exec(processedText)) !== null) {
const placeholder = asyncMatch[0]; // Full placeholder e.g., {{VCP_ASYNC_RESULT::Wan2.1VideoGen::xyz123}}
const pluginName = asyncMatch[1];
const requestId = asyncMatch[2];
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);
// You suggested replacing with the 'message' from the callback data.
// Or, we could use the whole callbackData string, or a formatted summary.
// Let's use callbackData.message if available, otherwise a generic success.
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)}`;
}
// Replace only the first occurrence in each iteration to handle multiple identical placeholders correctly if needed,
// though typically each requestId would be unique.
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, replacementText);
} catch (error) {
if (error.code === 'ENOENT') {
// File not found, means task is pending or callback hasn't happened/saved
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, `[任务 ${pluginName} (ID: ${requestId}) 结果待更新...]`);
} else {
// Other errors (e.g., JSON parse error from file)
console.error(`[replaceOtherVariables] Error processing async placeholder ${placeholder}:`, error);
tempAsyncProcessedText = tempAsyncProcessedText.replace(placeholder, `[获取任务 ${pluginName} (ID: ${requestId}) 结果时出错]`);
}
}
})()
);
}
await Promise.all(promises);
processedText = tempAsyncProcessedText;
// END: VCP_ASYNC_RESULT Placeholder Processing
return processedText;
}
app.get('/v1/models', async (req, res) => {
const { default: fetch } = await import('node-fetch');
try {
const modelsApiUrl = `${apiUrl}/v1/models`;
const apiResponse = await fetch(modelsApiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
...(req.headers['user-agent'] && { 'User-Agent': req.headers['user-agent'] }),
'Accept': req.headers['accept'] || 'application/json',
},
});
// 新增:如果启用了模型重定向,需要处理模型列表响应
if (modelRedirectHandler.isEnabled() && apiResponse.ok) {
const responseText = await apiResponse.text();
try {
const modelsData = JSON.parse(responseText);
// 替换模型列表中的内部模型名为公开模型名
if (modelsData.data && Array.isArray(modelsData.data)) {
modelsData.data = modelsData.data.map(model => {
if (model.id) {
const publicModelName = modelRedirectHandler.redirectModelForClient(model.id);
if (publicModelName !== model.id) {
if (DEBUG_MODE) {
console.log(`[ModelRedirect] 模型列表重定向: ${model.id} -> ${publicModelName}`);
}
return { ...model, id: publicModelName };
}
}
return model;
});
}
// 设置响应头
res.status(apiResponse.status);
apiResponse.headers.forEach((value, name) => {
if (!['content-encoding', 'transfer-encoding', 'connection', 'content-length', 'keep-alive'].includes(name.toLowerCase())) {
res.setHeader(name, value);
}
});
// 发送修改后的响应
res.json(modelsData);
return;
} catch (parseError) {
console.warn('[ModelRedirect] 解析模型列表响应失败,使用原始响应:', parseError.message);
// 如果解析失败,回退到原始流式转发
}
}
// 原始的流式转发逻辑(当模型重定向未启用或解析失败时使用)
res.status(apiResponse.status);
apiResponse.headers.forEach((value, name) => {
// Avoid forwarding hop-by-hop headers
if (!['content-encoding', 'transfer-encoding', 'connection', 'content-length', 'keep-alive'].includes(name.toLowerCase())) {
res.setHeader(name, value);
}
});
// Stream the response body back to the client
apiResponse.body.pipe(res);
} catch (error) {
console.error('转发 /v1/models 请求时出错:', error.message, error.stack);
if (!res.headersSent) {
res.status(500).json({ error: 'Internal Server Error', details: error.message });
} else if (!res.writableEnded) {
console.error('[STREAM ERROR] Headers already sent. Cannot send JSON error. Ending stream if not already ended.');
res.end();
}
}
});
// 新增:标准化任务创建API端点
const VCP_TIMED_CONTACTS_DIR = path.join(__dirname, 'VCPTimedContacts');
// 辅助函数:将 Date 对象格式化为包含时区偏移的本地时间字符串 (e.g., 2025-06-29T15:00:00+08:00)
function formatToLocalDateTimeWithOffset(date) {
const pad = (num) => num.toString().padStart(2, '0');
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const tzOffset = -date.getTimezoneOffset();
const offsetHours = pad(Math.floor(Math.abs(tzOffset) / 60));
const offsetMinutes = pad(Math.abs(tzOffset) % 60);
const offsetSign = tzOffset >= 0 ? '+' : '-';
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${offsetSign}${offsetHours}:${offsetMinutes}`;
}
app.post('/v1/schedule_task', async (req, res) => {
// 这是一个内部端点,由插件调用以创建定时任务。
// 它依赖于全局的 Bearer token 认证。
const { schedule_time, task_id, tool_call } = req.body;
if (!schedule_time || !task_id || !tool_call || !tool_call.tool_name || !tool_call.arguments) {
return res.status(400).json({ status: "error", error: "请求无效,缺少 'schedule_time', 'task_id', 或有效的 'tool_call' 对象。" });
}
const targetDate = new Date(schedule_time);
if (isNaN(targetDate.getTime())) {
return res.status(400).json({ status: "error", error: "无效的 'schedule_time' 时间格式。" });
}
if (targetDate.getTime() <= Date.now()) {
return res.status(400).json({ status: "error", error: "schedule_time 不能是过去的时间。" });
}
try {
// 确保目录存在
await fs.mkdir(VCP_TIMED_CONTACTS_DIR, { recursive: true });
const taskFilePath = path.join(VCP_TIMED_CONTACTS_DIR, `${task_id}.json`);
const scheduledTimeWithOffset = formatToLocalDateTimeWithOffset(targetDate);
const taskData = {
taskId: task_id,
scheduledLocalTime: scheduledTimeWithOffset, // 使用带时区偏移的本地时间格式
tool_call: tool_call, // 存储完整的 VCP Tool Call
requestor: `Plugin: ${tool_call.tool_name}`,
};
await fs.writeFile(taskFilePath, JSON.stringify(taskData, null, 2));
if (DEBUG_MODE) console.log(`[Server] 已通过API创建新的定时任务文件: ${taskFilePath}`);
// 返回成功的响应,插件可以基于此生成最终的用户回执
res.status(200).json({
status: "success",
message: "任务已成功调度。",
details: {
taskId: task_id,
scheduledTime: scheduledTimeWithOffset
}
});
} catch (error) {
console.error(`[Server] 通过API创建定时任务文件时出错:`, error);
res.status(500).json({ status: "error", error: "在服务器上保存定时任务时发生内部错误。" });
}
});
// 新增:紧急停止路由
app.post('/v1/interrupt', (req, res) => {
const id = req.body.requestId || req.body.messageId; // 兼容 requestId 和 messageId
if (!id) {
return res.status(400).json({ error: 'requestId or messageId is required.' });
}
const context = activeRequests.get(id);
if (context) {
console.log(`[Interrupt] Received stop signal for ID: ${id}`);
context.abortController.abort(); // 触发中止
// The actual response handling is done in the handleChatCompletion's error handler
res.status(200).json({ status: 'success', message: `Interrupt signal sent for request ${id}.` });
} else {
console.log(`[Interrupt] Received stop signal for non-existent or completed ID: ${id}`);
res.status(404).json({ status: 'error', message: `Request ${id} not found or already completed.` });
}
});
async function handleChatCompletion(req, res, forceShowVCP = false) {
const { default: fetch } = await import('node-fetch');
const shouldShowVCP = SHOW_VCP_OUTPUT || forceShowVCP; // Combine env var and route-specific flag
// 标准化客户端IP地址
let clientIp = req.ip;
if (clientIp && clientIp.substr(0, 7) === "::ffff:") {
clientIp = clientIp.substr(7);
}
const id = req.body.requestId || req.body.messageId; // 兼容 requestId 和 messageId
const abortController = new AbortController();
if (id) {
activeRequests.set(id, { req, res, abortController });
}
try {
let originalBody = req.body;
// 新增:执行模型重定向(客户端请求 -> 后端)
if (originalBody.model) {
const originalModel = originalBody.model;
const redirectedModel = modelRedirectHandler.redirectModelForBackend(originalModel);
if (redirectedModel !== originalModel) {
originalBody = { ...originalBody, model: redirectedModel };
console.log(`[ModelRedirect] 客户端请求模型 '${originalModel}' 已重定向为后端模型 '${redirectedModel}'`);
}
}
await writeDebugLog('LogInput', originalBody);
let shouldProcessMedia = true;
if (originalBody.messages && Array.isArray(originalBody.messages)) {
for (const msg of originalBody.messages) {
let foundPlaceholderInMsg = false;
if (msg.role === 'user' || msg.role === 'system') {
if (typeof msg.content === 'string' && msg.content.includes('{{ShowBase64}}')) {
foundPlaceholderInMsg = true;
msg.content = msg.content.replace(/\{\{ShowBase64\}\}/g, '');
} else if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (part.type === 'text' && typeof part.text === 'string' && part.text.includes('{{ShowBase64}}')) {
foundPlaceholderInMsg = true;
part.text = part.text.replace(/\{\{ShowBase64\}\}/g, '');
}
}
}
}
if (foundPlaceholderInMsg) {
shouldProcessMedia = false;
if (DEBUG_MODE) console.log('[Server] Media processing disabled by {{ShowBase64}} placeholder.');
break;
}
}
}
// --- New Staged Processing Logic ---
// Stage 1: Expand Agent placeholders first to reveal potential RAG markers.
let agentExpandedMessages = await Promise.all(originalBody.messages.map(async (msg) => {
const newMessage = JSON.parse(JSON.stringify(msg));
if (newMessage.content && typeof newMessage.content === 'string') {
newMessage.content = await replaceAgentVariables(newMessage.content, originalBody.model, msg.role);
} else if (Array.isArray(newMessage.content)) {
newMessage.content = await Promise.all(newMessage.content.map(async (part) => {
if (part.type === 'text' && typeof part.text === 'string') {
const newPart = JSON.parse(JSON.stringify(part));
newPart.text = await replaceAgentVariables(newPart.text, originalBody.model, msg.role);
return newPart;
}
return part;
}));
}
return newMessage;
}));
if (DEBUG_MODE) await writeDebugLog('LogAfterAgentExpansion', agentExpandedMessages);
// Stage 2: Run all message preprocessors (including RAG, Tavern, etc.)
let processedMessages = agentExpandedMessages;
// 2a. Handle MultiModalProcessor specifically due to the shouldProcessMedia flag
if (shouldProcessMedia) {
const processorName = pluginManager.messagePreprocessors.has("MultiModalProcessor") ? "MultiModalProcessor" : "ImageProcessor";
if (pluginManager.messagePreprocessors.has(processorName)) {
if (DEBUG_MODE) console.log(`[Server] Calling message preprocessor: ${processorName}`);
try {
processedMessages = await pluginManager.executeMessagePreprocessor(processorName, processedMessages);
} catch (pluginError) {
console.error(`[Server] Error in preprocessor ${processorName}:`, pluginError);
}
}
}
// 2b. Loop through all other message preprocessors
for (const name of pluginManager.messagePreprocessors.keys()) {
if (name === "ImageProcessor" || name === "MultiModalProcessor") continue; // Skip, handled above
if (DEBUG_MODE) console.log(`[Server] Calling message preprocessor: ${name}`);
try {
processedMessages = await pluginManager.executeMessagePreprocessor(name, processedMessages);
} catch (pluginError) {
console.error(`[Server] Error in preprocessor ${name}:`, pluginError);
}
}
if (DEBUG_MODE) await writeDebugLog('LogAfterPreprocessors', processedMessages);
// Stage 3: Replace all other placeholders on the pre-processed messages.
let finalMessages = await Promise.all(processedMessages.map(async (msg) => {
const newMessage = JSON.parse(JSON.stringify(msg));
if (newMessage.content && typeof newMessage.content === 'string') {
newMessage.content = await replaceOtherVariables(newMessage.content, originalBody.model, msg.role);
} else if (Array.isArray(newMessage.content)) {
newMessage.content = await Promise.all(newMessage.content.map(async (part) => {
if (part.type === 'text' && typeof part.text === 'string') {
const newPart = JSON.parse(JSON.stringify(part));
newPart.text = await replaceOtherVariables(newPart.text, originalBody.model, msg.role);
return newPart;
}
return part;
}));
}
return newMessage;
}));
originalBody.messages = finalMessages;
await writeDebugLog('LogOutputAfterProcessing', originalBody);
const isOriginalRequestStreaming = originalBody.stream === true;
// If VCP info needs to be shown, the response MUST be streamed to the client.
const willStreamResponse = isOriginalRequestStreaming; // Only stream if the original request was a stream.
let firstAiAPIResponse = await fetch(`${apiUrl}/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
...(req.headers['user-agent'] && { 'User-Agent': req.headers['user-agent'] }),
'Accept': willStreamResponse ? 'text/event-stream' : (req.headers['accept'] || 'application/json'),
},
// Force stream to be true if we are showing VCP info.
body: JSON.stringify({ ...originalBody, stream: willStreamResponse }),
signal: abortController.signal, // 传递中止信号
});
const isUpstreamStreaming = willStreamResponse && firstAiAPIResponse.headers.get('content-type')?.includes('text/event-stream');
if (!res.headersSent) {
res.status(firstAiAPIResponse.status);
firstAiAPIResponse.headers.forEach((value, name) => {
if (!['content-encoding', 'transfer-encoding', 'connection', 'content-length', 'keep-alive'].includes(name.toLowerCase())) {
res.setHeader(name, value);
}
});
if (isOriginalRequestStreaming && !res.getHeader('Content-Type')?.includes('text/event-stream')) {
res.setHeader('Content-Type', 'text/event-stream');
if (!res.getHeader('Cache-Control')) res.setHeader('Cache-Control', 'no-cache');
if (!res.getHeader('Connection')) res.setHeader('Connection', 'keep-alive');
}
}
let firstResponseRawDataForClientAndDiary = ""; // Used for non-streaming and initial diary
if (isUpstreamStreaming) {
let currentMessagesForLoop = originalBody.messages ? JSON.parse(JSON.stringify(originalBody.messages)) : [];
let recursionDepth = 0;
const maxRecursion = parseInt(process.env.MaxVCPLoopStream) || 5;
let currentAIContentForLoop = '';
let currentAIRawDataForDiary = '';
// Helper function to process an AI response stream
async function processAIResponseStreamHelper(aiResponse, isInitialCall) {
return new Promise((resolve, reject) => {
let sseBuffer = ""; // Buffer for incomplete SSE lines
let collectedContentThisTurn = ""; // Collects textual content from delta
let rawResponseDataThisTurn = ""; // Collects all raw chunks for diary
const isGpt5Mini = originalBody.model === 'GPT-5-mini';
const thinkingRegex = /^Thinking\.\.\.( \(\d+s elapsed\))?$/;
let sseLineBuffer = ""; // Buffer for incomplete SSE lines
aiResponse.body.on('data', (chunk) => {
const chunkString = chunk.toString('utf-8');
rawResponseDataThisTurn += chunkString;
sseLineBuffer += chunkString;
let lines = sseLineBuffer.split('\n');
// Keep the last part in buffer if it's not a complete line
sseLineBuffer = lines.pop();
const filteredLines = [];
for (const line of lines) {
if (line.startsWith('data: ')) {
const jsonData = line.substring(5).trim();
if (jsonData && jsonData !== '[DONE]') {
try {
const parsedData = JSON.parse(jsonData);
const content = parsedData.choices?.[0]?.delta?.content;
const reasoningContent = parsedData.choices?.[0]?.delta?.reasoning_content;
// Core filtering logic for thinking content (GPT-5-mini)