-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
1175 lines (1062 loc) · 56.7 KB
/
script.js
File metadata and controls
1175 lines (1062 loc) · 56.7 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
// --- Configuration Files ---
const availableConfigs = [
{ name: "默认配置", file: "config_default.js" },
{ name: "闲聊模式", file: "config1.js" },
{ name: "夜伽模式", file: "config2.js" }
];
const CONFIG_STORAGE_KEY = 'selectedConfigFile';
// --- Global config variable ---
// This will be populated when the selected config file is loaded.
let config = null;
document.addEventListener('DOMContentLoaded', () => {
// --- DOM Elements ---
const chatMessages = document.getElementById('chat-messages');
const messageInput = document.getElementById('message-input');
const sendButton = document.getElementById('send-button');
const newChatButton = document.getElementById('new-chat-button');
const aiStatusDiv = document.getElementById('ai-status');
const aiButtonsDiv = document.getElementById('ai-buttons');
const sessionSelect = document.getElementById('chat-session-select');
const configSelect = document.getElementById('config-select'); // Get config selector
// Image related elements
const attachImageButton = document.getElementById('attach-image-button');
const imageInput = document.getElementById('image-input');
const imagePreviewArea = document.getElementById('image-preview-area');
const imagePreview = document.getElementById('image-preview');
const removeImageButton = document.getElementById('remove-image-button');
// Floating AI Status Window elements
const floatingAiStatusWindow = document.getElementById('floating-ai-status-window');
const currentRoundAisContainer = document.getElementById('current-round-ais');
// Dark Mode elements
const darkModeToggle = document.getElementById('dark-mode-toggle');
const DARK_MODE_STORAGE_KEY = 'darkModeEnabled';
// --- Configuration (These will be initialized AFTER config is loaded) ---
let activeModels = [];
// let currentAiIndex = 0; // Not used in NatureRandom or ButtonSend as much
// let aiTurnOrder = []; // Not used in NatureRandom
// --- State ---
let allChatData = { sessions: {}, activeSessionId: null }; // Holds all sessions and the active one
let chatHistory = []; // Represents the history of the *active* session
let activeSessionId = null; // ID of the currently active session
let isAiResponding = false;
let selectedImageBase64 = null; // To store the selected image data
// --- State for NatureRandom and exclusions ---
let excludedAiForNextRound = new Set(); // Stores names of AIs user wants to exclude for the *immediate next* round (cleared after use)
let persistentlyMutedAiNames = new Set(); // Stores names of AIs persistently muted by user
let aiCurrentlyOptedOut = new Set(); // Stores names of AIs that included [[QuitGroup]] in their *most recent* response. Used to exclude them from the next turn.
let isAtAllTriggered = false; // Flag for "@所有人" command
// --- Constants ---
const CHAT_DATA_KEY = 'aiGroupChatData';
const MUTED_AI_KEY = 'persistentlyMutedAiNames'; // Key for localStorage
// --- Marked.js Configuration ---
if (typeof marked !== 'undefined') {
marked.setOptions({
breaks: true,
gfm: true
});
console.log("marked.js configured.");
} else {
console.error("marked.js library not loaded. Markdown rendering will be disabled.");
}
// --- Initialization ---
populateConfigSelect();
const selectedConfigFile = localStorage.getItem(CONFIG_STORAGE_KEY) || availableConfigs[0].file;
configSelect.value = selectedConfigFile;
configSelect.addEventListener('change', handleConfigChange);
loadConfigAndInitialize(selectedConfigFile);
// --- Dark Mode Logic ---
function applyDarkMode(isEnabled) {
if (isEnabled) {
document.body.classList.add('night-mode');
} else {
document.body.classList.remove('night-mode');
}
localStorage.setItem(DARK_MODE_STORAGE_KEY, isEnabled);
}
const savedDarkModePreference = localStorage.getItem(DARK_MODE_STORAGE_KEY);
if (savedDarkModePreference !== null) {
applyDarkMode(savedDarkModePreference === 'true');
} else {
applyDarkMode(false);
}
if (darkModeToggle) {
darkModeToggle.addEventListener('click', () => {
const isEnabled = document.body.classList.contains('night-mode');
applyDarkMode(!isEnabled);
});
} else {
console.error("Dark mode toggle button not found!");
}
// --- Event Listeners ---
sendButton.addEventListener('click', handleSendMessage);
newChatButton.addEventListener('click', createNewSession);
sessionSelect.addEventListener('change', (e) => switchSession(e.target.value));
imageInput.addEventListener('change', handleImageSelection);
removeImageButton.addEventListener('click', removeSelectedImage);
messageInput.addEventListener('keydown', (e) => {
const isMobileWidth = window.innerWidth <= 768;
if (e.key === 'Enter' && !e.shiftKey && !isMobileWidth) {
e.preventDefault();
handleSendMessage();
}
});
messageInput.addEventListener('input', adjustTextareaHeight);
// --- Config Loading and Initialization Functions ---
function populateConfigSelect() {
availableConfigs.forEach(cfg => {
const option = document.createElement('option');
option.value = cfg.file;
option.textContent = cfg.name;
configSelect.appendChild(option);
});
}
function handleConfigChange(event) {
const newConfigFile = event.target.value;
localStorage.setItem(CONFIG_STORAGE_KEY, newConfigFile);
alert(`配置已更改为 ${availableConfigs.find(c => c.file === newConfigFile)?.name || newConfigFile}. 页面将重新加载以应用更改。`);
location.reload();
}
function loadConfigAndInitialize(configFileName) {
console.log(`Loading config: ${configFileName}`);
const existingScript = document.querySelector('script[src^="config"]');
if (existingScript) {
existingScript.remove();
}
const script = document.createElement('script');
script.src = configFileName;
script.onload = () => {
console.log(`Config ${configFileName} loaded successfully.`);
if (typeof window.loadedConfig === 'object' && window.loadedConfig !== null) {
config = window.loadedConfig;
delete window.loadedConfig;
console.log("Config object assigned successfully.");
initializeApplication();
} else {
console.error(`Config object (window.loadedConfig) not found after loading ${configFileName}.`);
alert(`错误:配置文件 ${configFileName} 加载成功,但未正确定义配置对象。`);
if (configFileName !== availableConfigs[0].file) {
console.log("Attempting to load default config as fallback.");
localStorage.setItem(CONFIG_STORAGE_KEY, availableConfigs[0].file);
loadConfigAndInitialize(availableConfigs[0].file);
} else {
alert("错误:无法加载默认配置文件。应用程序无法启动。");
}
}
};
script.onerror = () => {
console.error(`Failed to load config file: ${configFileName}`);
alert(`错误:无法加载配置文件 ${configFileName}。将尝试加载默认配置。`);
if (configFileName !== availableConfigs[0].file) {
localStorage.setItem(CONFIG_STORAGE_KEY, availableConfigs[0].file);
loadConfigAndInitialize(availableConfigs[0].file);
} else {
alert("错误:无法加载默认配置文件。应用程序无法启动。");
}
};
document.body.appendChild(script);
}
function initializeApplication() {
console.log("Initializing application with loaded config...");
activeModels = config.getActiveModels();
// currentAiIndex = 0; // Reset for modes that use it
loadAllChatData();
initializeSessions();
populateSessionSelect();
if (activeSessionId) {
switchSession(activeSessionId);
} else {
console.warn("No active session ID found after loading data.");
if (Object.keys(allChatData.sessions).length === 0) {
createNewSession();
}
}
updateAiStatus();
setupAiButtons();
adjustTextareaHeight();
loadMutedAiNames(); // Load persistent mute state *before* first window update
updateFloatingAiWindow(activeModels); // Initialize floating window
setRandomBackground();
setBodyBackground();
console.log("Application initialized.");
}
// --- Textarea Height Adjustment ---
function adjustTextareaHeight() {
messageInput.style.height = 'auto';
const maxHeight = parseInt(window.getComputedStyle(messageInput).maxHeight, 10);
const newHeight = Math.min(messageInput.scrollHeight, maxHeight);
messageInput.style.height = `${newHeight}px`;
}
// --- Chat Data Management (Sessions, History, Mutes) ---
function loadAllChatData() {
const savedData = localStorage.getItem(CHAT_DATA_KEY);
if (savedData) {
try {
const parsedData = JSON.parse(savedData);
if (parsedData && typeof parsedData.sessions === 'object' && parsedData.sessions !== null) {
allChatData = parsedData;
if (!allChatData.sessions[allChatData.activeSessionId]) {
allChatData.activeSessionId = Object.keys(allChatData.sessions)[0] || null;
}
} else {
resetChatData();
}
} catch (error) {
console.error("Error parsing chat data:", error);
resetChatData();
}
} else {
resetChatData();
}
activeSessionId = allChatData.activeSessionId;
}
function saveAllChatData() {
try {
if (activeSessionId && allChatData.sessions[activeSessionId]) {
allChatData.sessions[activeSessionId].history = chatHistory;
}
allChatData.activeSessionId = activeSessionId;
localStorage.setItem(CHAT_DATA_KEY, JSON.stringify(allChatData));
} catch (error) {
console.error("Error saving chat data:", error);
}
}
function loadMutedAiNames() {
const savedMutedNames = localStorage.getItem(MUTED_AI_KEY);
if (savedMutedNames) {
try {
const namesArray = JSON.parse(savedMutedNames);
if (Array.isArray(namesArray)) {
persistentlyMutedAiNames = new Set(namesArray);
} else {
persistentlyMutedAiNames = new Set();
}
} catch (error) {
console.error("Error parsing muted AI names:", error);
persistentlyMutedAiNames = new Set();
}
} else {
persistentlyMutedAiNames = new Set();
}
}
function saveMutedAiNames() {
try {
localStorage.setItem(MUTED_AI_KEY, JSON.stringify(Array.from(persistentlyMutedAiNames)));
} catch (error) {
console.error("Error saving muted AI names:", error);
}
}
function resetChatData() {
allChatData = { sessions: {}, activeSessionId: null };
activeSessionId = null;
chatHistory = [];
}
function initializeSessions() {
if (Object.keys(allChatData.sessions).length === 0) {
createNewSession(false);
} else if (!activeSessionId || !allChatData.sessions[activeSessionId]) {
activeSessionId = Object.keys(allChatData.sessions)[0];
allChatData.activeSessionId = activeSessionId;
saveAllChatData();
}
activeSessionId = allChatData.activeSessionId;
}
function generateSessionId() {
return `session_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`;
}
function populateSessionSelect() {
sessionSelect.innerHTML = '';
const sessionIds = Object.keys(allChatData.sessions);
if (sessionIds.length === 0) {
const option = document.createElement('option');
option.textContent = "无聊天记录";
option.disabled = true;
sessionSelect.appendChild(option);
return;
}
sessionIds.forEach(sessionId => {
const session = allChatData.sessions[sessionId];
const option = document.createElement('option');
option.value = sessionId;
option.textContent = session.name || `聊天 ${sessionId.substring(8, 12)}`;
option.selected = sessionId === activeSessionId;
sessionSelect.appendChild(option);
});
}
function switchSession(sessionId) {
if (!sessionId || !allChatData.sessions[sessionId]) {
const firstSessionId = Object.keys(allChatData.sessions)[0];
if (firstSessionId) sessionId = firstSessionId;
else { createNewSession(); return; }
}
activeSessionId = sessionId;
chatHistory = [...allChatData.sessions[activeSessionId].history];
allChatData.activeSessionId = activeSessionId;
displayChatHistory();
saveAllChatData();
sessionSelect.value = activeSessionId;
}
function createNewSession(switchToNew = true) {
const newSessionId = generateSessionId();
const now = new Date();
const dateTimeString = now.toLocaleString('zh-CN', {
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false
}).replace(/\//g, '-');
const newSessionName = `聊天 ${dateTimeString}`;
allChatData.sessions[newSessionId] = { name: newSessionName, history: [] };
if (switchToNew) {
activeSessionId = newSessionId;
chatHistory = [];
allChatData.activeSessionId = activeSessionId;
saveAllChatData();
populateSessionSelect();
switchSession(newSessionId);
} else {
saveAllChatData();
}
}
function saveChatHistory() {
if (activeSessionId && allChatData.sessions[activeSessionId]) {
allChatData.sessions[activeSessionId].history = chatHistory;
saveAllChatData();
}
}
// --- Message Display Functions ---
function displayChatHistory() {
chatMessages.innerHTML = '';
if (Array.isArray(chatHistory)) {
chatHistory.forEach(msg => {
const messageContent = msg.content || {};
appendMessage(msg.name, messageContent, msg.role === 'user');
});
} else {
chatHistory = [];
}
}
function appendMessage(sender, contentData, isUser, isLoading = false) {
const messageElement = document.createElement('div');
messageElement.classList.add('message');
messageElement.classList.add(isUser ? 'user-message' : 'ai-message');
const avatarElement = document.createElement('img');
avatarElement.classList.add('avatar');
let avatarSrc = '';
const imageDir = 'image/';
const defaultUserAvatar = imageDir + 'default-user.png';
const defaultAiAvatar = imageDir + 'default-ai.png';
if (isUser) {
avatarSrc = config?.UserAvatar ? imageDir + config.UserAvatar : defaultUserAvatar;
} else {
const aiModel = config?.models?.find(model => model.Name === sender);
avatarSrc = aiModel?.Avatar ? imageDir + aiModel.Avatar : defaultAiAvatar;
}
avatarElement.src = avatarSrc;
avatarElement.alt = `${sender} 头像`;
avatarElement.onerror = () => { avatarElement.src = isUser ? defaultUserAvatar : defaultAiAvatar; };
const messageContentElement = document.createElement('div');
messageContentElement.classList.add('message-content');
const senderElement = document.createElement('div');
senderElement.classList.add('sender');
senderElement.textContent = sender;
messageContentElement.appendChild(senderElement);
const contentWrapper = document.createElement('div');
contentWrapper.classList.add('content-wrapper');
const textContent = (typeof contentData === 'string') ? contentData : contentData.text;
const imageBase64 = (typeof contentData === 'object' && contentData.image) ? contentData.image : null;
if (imageBase64) {
const imgElement = document.createElement('img');
imgElement.src = imageBase64;
imgElement.alt = "用户图片";
imgElement.style.maxWidth = '200px';
imgElement.style.maxHeight = '200px';
imgElement.style.display = 'block';
imgElement.style.marginTop = '5px';
imgElement.style.borderRadius = '4px';
contentWrapper.appendChild(imgElement);
}
if (textContent || isLoading) {
const contentElement = document.createElement('div');
contentElement.classList.add('content');
let highlightedText = highlightMentions(textContent || '');
const preprocessedText = highlightedText.replace(/~~/g, '\\~\\~');
const dailyNoteRegexSource = "<<<DailyNoteStart>>>[\\s\\S]*?<<<DailyNoteEnd>>>";
const toolUseRegexSource = "<<<\\[TOOL_REQUEST\\]>>>[\\s\\S]*?<<<\\[END_TOOL_REQUEST\\]>>>";
const toolNameInnerRegex = /tool_name:「始」([^「」]+)「末」/;
let finalHtml = '';
let lastIndex = 0;
const combinedRegex = new RegExp(`(${dailyNoteRegexSource})|(${toolUseRegexSource})`, 'g');
preprocessedText.replace(combinedRegex, (match, dailyNoteFullMatch, toolUseFullMatch, index) => {
const beforeText = preprocessedText.slice(lastIndex, index);
if (beforeText) finalHtml += (typeof marked !== 'undefined') ? marked.parse(beforeText) : beforeText.replace(/\n/g, '<br>');
if (dailyNoteFullMatch) {
const noteContent = dailyNoteFullMatch.replace('<<<DailyNoteStart>>>', '').replace('<<<DailyNoteEnd>>>', '');
const maidMatch = noteContent.match(/Maid:\s*([^\n]*)/);
const dateMatch = noteContent.match(/Date:\s*([^\n]*)/);
const contentMatch = noteContent.match(/Content:\s*([\s\S]*)/);
let formattedContent = (maidMatch && dateMatch && contentMatch) ? `DailyNote: ${maidMatch[1].trim()} - ${dateMatch[1].trim()}<br>${contentMatch[1].trim().replace(/\n/g, '<br>')}` : noteContent.replace(/\n/g, '<br>');
finalHtml += `<div class="daily-note-bubble">${formattedContent}</div>`;
} else if (toolUseFullMatch) {
const toolContent = toolUseFullMatch.replace('<<<[TOOL_REQUEST]>>>', '').replace('<<<[END_TOOL_REQUEST]>>>', '');
const toolNameMatchResult = toolContent.match(toolNameInnerRegex);
let formattedContent = `ToolUse: ${toolNameMatchResult && toolNameMatchResult[1] ? toolNameMatchResult[1].trim() : 'Unknown'}`;
finalHtml += `<div class="tool-use-bubble">${formattedContent}</div>`;
}
lastIndex = index + match.length;
return match;
});
const afterText = preprocessedText.slice(lastIndex);
if (afterText) finalHtml += (typeof marked !== 'undefined') ? marked.parse(afterText) : afterText.replace(/\n/g, '<br>');
if (preprocessedText && !finalHtml.trim() && !combinedRegex.test(preprocessedText)) {
finalHtml = (typeof marked !== 'undefined') ? marked.parse(preprocessedText) : preprocessedText.replace(/\n/g, '<br>');
}
contentElement.innerHTML = finalHtml;
if (isLoading) {
const loadingIndicator = document.createElement('span');
loadingIndicator.classList.add('loading-indicator');
contentElement.appendChild(loadingIndicator);
messageElement.dataset.loadingId = sender;
}
contentWrapper.appendChild(contentElement);
}
messageContentElement.appendChild(contentWrapper);
messageElement.appendChild(avatarElement);
messageElement.appendChild(messageContentElement);
chatMessages.appendChild(messageElement);
scrollToBottom();
}
function updateLoadingMessage(sender, textContent, isFinalUpdate = false) {
const loadingMessage = chatMessages.querySelector(`.message[data-loading-id="${sender}"]`);
if (loadingMessage) {
let contentElement = loadingMessage.querySelector('.content-wrapper .content');
if (!contentElement) {
const contentWrapper = loadingMessage.querySelector('.content-wrapper');
if (contentWrapper) {
contentElement = document.createElement('div');
contentElement.classList.add('content');
contentWrapper.appendChild(contentElement);
} else { return; }
}
let highlightedText = highlightMentions(textContent || '');
const preprocessedText = highlightedText.replace(/~~/g, '\\~\\~');
const dailyNoteRegexSource = "<<<DailyNoteStart>>>[\\s\\S]*?<<<DailyNoteEnd>>>";
const toolUseRegexSource = "<<<\\[TOOL_REQUEST\\]>>>[\\s\\S]*?<<<\\[END_TOOL_REQUEST\\]>>>";
const toolNameInnerRegex = /tool_name:「始」([^「」]+)「末」/;
let finalHtml = '';
let lastIndex = 0;
const combinedRegex = new RegExp(`(${dailyNoteRegexSource})|(${toolUseRegexSource})`, 'g');
preprocessedText.replace(combinedRegex, (match, dailyNoteFullMatch, toolUseFullMatch, index) => {
const beforeText = preprocessedText.slice(lastIndex, index);
if (beforeText) finalHtml += (typeof marked !== 'undefined') ? marked.parse(beforeText) : beforeText.replace(/\n/g, '<br>');
if (dailyNoteFullMatch) {
const noteContent = dailyNoteFullMatch.replace('<<<DailyNoteStart>>>', '').replace('<<<DailyNoteEnd>>>', '');
const maidMatch = noteContent.match(/Maid:\s*([^\n]*)/);
const dateMatch = noteContent.match(/Date:\s*([^\n]*)/);
const contentMatch = noteContent.match(/Content:\s*([\s\S]*)/);
let formattedContent = (maidMatch && dateMatch && contentMatch) ? `DailyNote: ${maidMatch[1].trim()} - ${dateMatch[1].trim()}<br>${contentMatch[1].trim().replace(/\n/g, '<br>')}` : noteContent.replace(/\n/g, '<br>');
finalHtml += `<div class="daily-note-bubble">${formattedContent}</div>`;
} else if (toolUseFullMatch) {
const toolContent = toolUseFullMatch.replace('<<<[TOOL_REQUEST]>>>', '').replace('<<<[END_TOOL_REQUEST]>>>', '');
const toolNameMatchResult = toolContent.match(toolNameInnerRegex);
let formattedContent = `ToolUse: ${toolNameMatchResult && toolNameMatchResult[1] ? toolNameMatchResult[1].trim() : 'Unknown'}`;
finalHtml += `<div class="tool-use-bubble">${formattedContent}</div>`;
}
lastIndex = index + match.length;
return match;
});
const afterText = preprocessedText.slice(lastIndex);
if (afterText) finalHtml += (typeof marked !== 'undefined') ? marked.parse(afterText) : afterText.replace(/\n/g, '<br>');
if (preprocessedText && !finalHtml.trim() && !combinedRegex.test(preprocessedText)) {
finalHtml = (typeof marked !== 'undefined') ? marked.parse(preprocessedText) : preprocessedText.replace(/\n/g, '<br>');
}
contentElement.innerHTML = finalHtml;
let loadingIndicator = contentElement.querySelector('.loading-indicator');
if (!isFinalUpdate) {
if (!loadingIndicator) {
loadingIndicator = document.createElement('span');
loadingIndicator.classList.add('loading-indicator');
contentElement.appendChild(loadingIndicator);
}
} else {
if (loadingIndicator) loadingIndicator.remove();
delete loadingMessage.dataset.loadingId;
}
} else if (isFinalUpdate) {
appendMessage(sender, { text: textContent }, false);
}
scrollToBottom();
}
function scrollToBottom() {
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function updateAiStatus() {
aiStatusDiv.innerHTML = '';
activeModels.forEach((model, index) => {
const statusSpan = document.createElement('span');
statusSpan.textContent = `${index + 1}. ${model.Name}`;
aiStatusDiv.appendChild(statusSpan);
});
}
function setupAiButtons() {
aiButtonsDiv.innerHTML = '';
if (config.AI_CHAT_MODE === 'ButtonSend') {
activeModels.forEach((model, index) => {
const button = document.createElement('button');
button.textContent = `邀请 ${model.Name} 发言`;
button.dataset.modelIndex = index;
button.addEventListener('click', () => handleAiButtonSend(index));
aiButtonsDiv.appendChild(button);
});
aiButtonsDiv.style.display = 'flex';
} else {
aiButtonsDiv.style.display = 'none';
}
}
// --- Image Handling ---
function handleImageSelection(event) {
const file = event.target.files[0];
if (file && file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = (e) => {
selectedImageBase64 = e.target.result;
displayImagePreview(selectedImageBase64);
}
reader.readAsDataURL(file);
} else {
removeSelectedImage();
if (file) alert("请选择一个图片文件。");
}
imageInput.value = null;
}
function displayImagePreview(base64Data) {
imagePreview.src = base64Data;
imagePreviewArea.style.display = 'block';
}
function removeSelectedImage() {
selectedImageBase64 = null;
imagePreview.src = '#';
imagePreviewArea.style.display = 'none';
imageInput.value = null;
}
// --- Message Sending and AI Response Trigger ---
function handleSendMessage() {
const messageText = messageInput.value.trim();
if ((!messageText && !selectedImageBase64) || isAiResponding) return;
const messageContent = {};
if (messageText) messageContent.text = messageText;
if (selectedImageBase64) messageContent.image = selectedImageBase64;
const userName = config?.User_Name || "User";
appendMessage(userName, messageContent, true);
chatHistory.push({ role: 'user', name: userName, content: messageContent });
saveChatHistory();
messageInput.value = '';
removeSelectedImage();
adjustTextareaHeight();
if (messageText.includes('@所有人')) {
console.log("Detected '@所有人' command.");
isAtAllTriggered = true;
}
triggerAiResponse();
}
function handleAiButtonSend(modelIndex) {
if (isAiResponding) return;
const model = activeModels[modelIndex];
if (model) {
isAiResponding = true;
setUiResponding(true);
// For ButtonSend, AI opt-out status might need specific handling if it's to be respected.
// Current NatureRandom's aiCurrentlyOptedOut is separate.
// Let's assume ButtonSend bypasses the [[QuitGroup]] opt-out for a direct invitation.
const dummyAiNewlyOptedOutThisTurn = new Set(); // For callAiApi signature
callAiApi(model, 0, dummyAiNewlyOptedOutThisTurn);
}
}
function setUiResponding(isResponding) {
if (!config) return;
messageInput.disabled = isResponding;
sendButton.disabled = isResponding;
if (config.AI_CHAT_MODE === 'ButtonSend') {
aiButtonsDiv.querySelectorAll('button').forEach(button => button.disabled = isResponding);
}
sendButton.textContent = isResponding ? "思考中..." : "发送";
}
/**
* Main function to trigger AI responses based on the configured mode.
* For NatureRandom, it orchestrates speaker selection and calls.
*/
async function triggerAiResponse() {
if (!config) {
console.error("Cannot trigger AI response: Config not loaded yet.");
return;
}
if (isAiResponding && config.AI_CHAT_MODE !== 'ButtonSend') { // Allow ButtonSend to interrupt/overlap if needed, or manage its own isAiResponding
console.log("AI response cycle already in progress for non-ButtonSend mode.");
return;
}
isAiResponding = true;
setUiResponding(true);
let actualSpeakers = [];
const mode = config.AI_CHAT_MODE;
// `aiCurrentlyOptedOut` (from previous turn) and `excludedAiForNextRound` (user command for this turn)
// are used by determineNatureRandomSpeakers or the @所有人 filter.
if (mode === 'NatureRandom') {
if (isAtAllTriggered) { // Highest priority: @所有人
console.log("@所有人 is active: Forcing all non-muted/non-excluded AIs to respond.");
actualSpeakers = activeModels.filter(model =>
!persistentlyMutedAiNames.has(model.Name) &&
!excludedAiForNextRound.has(model.Name) &&
!aiCurrentlyOptedOut.has(model.Name)
);
console.log(`@所有人 speakers: ${actualSpeakers.map(m => m.Name).join(', ')}`);
} else {
// Determine speakers based on NatureRandom logic, passing all relevant exclusion sets
actualSpeakers = determineNatureRandomSpeakers(
activeModels,
chatHistory,
config,
persistentlyMutedAiNames,
excludedAiForNextRound,
aiCurrentlyOptedOut
);
}
} else if (mode === 'ButtonSend') {
// ButtonSend is handled by its own event handler, no automatic speaker selection here.
isAiResponding = false; // Reset flags as ButtonSend manages its own flow
setUiResponding(false);
updateFloatingAiWindow(activeModels); // Update UI
return; // Exit early
}
// ... (other modes like sequentialQueue, shuffledQueue, randomSubsetQueue would go here) ...
else {
console.warn(`AI_CHAT_MODE "${mode}" is not fully implemented for automatic triggering beyond NatureRandom/ButtonSend in this version. Defaulting to no speakers.`);
actualSpeakers = []; // Fallback for other modes not detailed here
}
console.log(`Actual speakers for this round (${mode}): ${actualSpeakers.map(m => m.Name).join(', ')}`);
// User-defined exclusions were for the current turn's decision. Clear for the next user input.
excludedAiForNextRound.clear();
// This set will be populated by AIs that include [[QuitGroup]] in *this* turn's responses.
const aiNewlyOptedOutThisTurn = new Set();
updateFloatingAiWindow(activeModels, actualSpeakers); // Update UI based on who will speak
if (actualSpeakers.length === 0) {
console.log("No AI speakers selected for this round.");
} else {
for (const model of actualSpeakers) { // Iterate in the order determined
if (model) {
console.log(`--- Calling AI: ${model.Name} ---`);
await callAiApi(model, 0, aiNewlyOptedOutThisTurn); // Pass the set to be populated
} else {
console.error(`Found invalid model entry in actualSpeakers.`);
}
}
}
// After all AIs in this round have spoken (or tried to),
// update `aiCurrentlyOptedOut` for the *next* AI response cycle.
aiCurrentlyOptedOut = new Set([...aiNewlyOptedOutThisTurn]);
isAiResponding = false;
setUiResponding(false);
isAtAllTriggered = false; // Reset @所有人 flag for the next user message
updateFloatingAiWindow(activeModels); // Update floating window (e.g., to remove 'speaking' highlights)
}
/**
* NatureRandom mode: Determines speakers based on message content and priorities.
* This function implements the core logic as per user's specification.
* @param {Array<Object>} currentActiveModels - All currently active AI models.
* @param {Array<Object>} currentChatHistory - The current chat history.
* @param {Object} currentConfig - The global configuration object.
* @param {Set<string>} currentPersistentlyMutedAiNames - Set of persistently muted AI names.
* @param {Set<string>} currentExcludedAiForNextRound - Set of AI names excluded by user for this specific turn.
* @param {Set<string>} currentAiOptedOutLastTurn - Set of AI names that opted out in their previous turn.
* @returns {Array<Object>} An array of AI model objects that should speak, in order.
*/
function determineNatureRandomSpeakers(
currentActiveModels,
currentChatHistory,
currentConfig,
currentPersistentlyMutedAiNames,
currentExcludedAiForNextRound,
currentAiOptedOutLastTurn
) {
// 1. Determine truly eligible models for this round's consideration
const eligibleModels = currentActiveModels.filter(model =>
!currentPersistentlyMutedAiNames.has(model.Name) &&
!currentExcludedAiForNextRound.has(model.Name) &&
!currentAiOptedOutLastTurn.has(model.Name)
);
if (eligibleModels.length === 0) {
console.log("NatureRandom: No eligible models available for consideration.");
return [];
}
let speakers = []; // Final ordered list of speakers
const spokenThisTurnTracker = new Set(); // Tracks AI names added to 'speakers' to avoid duplicates and respect priorities
// Get text from the last message (usually user's input, or AI's if it's a continuation that triggers others)
// Get text from the last round of messages (user input + preceding AI responses in the same turn)
let roundText = "";
// Iterate backwards from the last message
for (let i = currentChatHistory.length - 1; i >= 0; i--) {
const msg = currentChatHistory[i];
const msgContentText = (msg.content && msg.content.text) ? msg.content.text : "";
if (msgContentText) {
// Prepend message text to roundText. Add a space for separation.
roundText = msgContentText + (roundText ? " " + roundText : "");
}
// Stop if we hit a user message that is NOT the very last message.
// The very last message is the one that triggered this response cycle, so we include it.
// Any user message before the last one marks the start of the previous round.
if (msg.role === 'user' && i < currentChatHistory.length - 1) {
break;
}
}
// --- Priority: @角色标签 (e.g., @小克) ---
// "被@的角色...必须发言。并且优先发言!"
if (roundText) {
eligibleModels.forEach(model => {
if (roundText.includes(`@${model.Name}`)) { // Assumes model.Name is the "角色标签"
if (!spokenThisTurnTracker.has(model.Name)) {
speakers.push(model);
spokenThisTurnTracker.add(model.Name);
}
}
});
}
// --- Priority: Pure Text Keyword Mention (from model.Tag) ---
// "拥有该关键词标签的角色...则必须发言。"
if (roundText) {
eligibleModels.forEach(model => {
if (spokenThisTurnTracker.has(model.Name)) return; // Already added by @mention
const tags = (model.Tag || "").split(',').map(t => t.trim()).filter(t => t);
if (tags.some(tag => roundText.includes(tag))) {
speakers.push(model);
spokenThisTurnTracker.add(model.Name);
}
});
}
// --- Priority: Random Speaking for un-triggered, eligible AIs ---
// "对于以上都未被触发...它们将以 1/AI_LIST 的概率随机选择发言。"
const baseProbabilityCount = currentConfig.AI_LIST > 0 ? currentConfig.AI_LIST : eligibleModels.length;
const probability = baseProbabilityCount > 0 ? (1 / baseProbabilityCount) : 0;
if (probability > 0) {
eligibleModels.forEach(model => {
if (spokenThisTurnTracker.has(model.Name)) return; // Already chosen by mention or keyword
if (Math.random() < probability) {
speakers.push(model);
spokenThisTurnTracker.add(model.Name);
}
});
}
// --- Priority: Final Fallback (Guaranteed Speaker) ---
// "如果以上所有逻辑都没有选出任何AI发言,则从所有符合条件...的AI中随机选择一个发言。"
if (speakers.length === 0 && eligibleModels.length > 0) {
console.log("NatureRandom: Fallback - No speakers selected by priority logic, selecting one random eligible model.");
// Select from `eligibleModels` which are already filtered for all conditions.
const randomIndex = Math.floor(Math.random() * eligibleModels.length);
const fallbackSpeaker = eligibleModels[randomIndex];
speakers.push(fallbackSpeaker);
// spokenThisTurnTracker.add(fallbackSpeaker.Name); // Not strictly necessary as it's the final addition
}
// The `speakers` array is built in priority order. Duplicates are prevented by `spokenThisTurnTracker`.
console.log(`NatureRandom determined speakers: ${speakers.map(m => m.Name).join(', ')}`);
return speakers;
}
/** Highlight @mentions in text content */
function highlightMentions(text) {
if (!text || typeof text !== 'string' || !config || !config.models) {
return text;
}
const allIndividualTags = new Set();
activeModels.forEach(model => { // Use activeModels for highlighting all potential mentions
if (model.Tag && typeof model.Tag === 'string') {
model.Tag.split(',')
.map(tag => tag.trim())
.filter(tag => tag)
.forEach(tag => allIndividualTags.add(tag));
}
// Add model names themselves as potential @mention targets for highlighting
allIndividualTags.add(model.Name);
});
const escapedIndividualTags = Array.from(allIndividualTags).map(tag => tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
// Regex to match @所有人 or @<tag/name> ensuring it's a whole word mention
const mentionRegex = new RegExp(`(@所有人|@(?:${escapedIndividualTags.join('|')}))(?![\\w-])`, 'g');
return text.replace(mentionRegex, (match) => {
return `<span class="mention-highlight">${match}</span>`;
});
}
/** Construct prompt and call the AI API (handles multimodal) */
async function callAiApi(model, retryCount = 0, aiNewlyOptedOutThisTurnRef) { // Added aiNewlyOptedOutThisTurnRef
if (!config) {
console.error("Cannot call API: Config not loaded yet.");
if (config.AI_CHAT_MODE !== 'ButtonSend') { /*isAiResponding = false; setUiResponding(false);*/ } // Managed by triggerAiResponse
else { isAiResponding = false; setUiResponding(false); }
return;
}
if (!model) {
console.error("Attempted to call API with an invalid model.");
if (config.AI_CHAT_MODE !== 'ButtonSend') { /*isAiResponding = false; setUiResponding(false);*/ }
else { isAiResponding = false; setUiResponding(false); }
return;
}
const retryText = retryCount > 0 ? `(重试 ${retryCount}/2)` : '';
if (retryCount > 0) {
updateLoadingMessage(model.Name, `重新连接中${retryText}...`, false);
} else {
appendMessage(model.Name, { text: '...' }, false, true);
}
const messages = [];
const currentTime = new Date().toLocaleString('zh-CN');
const groupPrompt = (config.GroupPrompt || "").replace("{{Date::time}}", currentTime);
let combinedSystemPrompt = `${config.USER_Prompt || ""}\n${groupPrompt}`;
if (model.SystemPrompts && model.SystemPrompts.trim()) {
combinedSystemPrompt += `\n${model.SystemPrompts}`;
}
if (combinedSystemPrompt.trim()) {
messages.push({ role: 'system', content: combinedSystemPrompt });
}
chatHistory.forEach(msg => {
const apiMessage = { role: msg.role === 'user' ? 'user' : 'assistant', name: msg.name };
const content = msg.content || {};
const senderName = msg.name || (msg.role === 'user' ? config.User_Name || 'User' : 'AI');
const senderPrefix = `${senderName}: `;
if (content.image && model.Image) {
apiMessage.content = [];
const textPart = content.text ? senderPrefix + content.text : senderPrefix + "[图片]";
apiMessage.content.push({ type: "text", text: textPart });
apiMessage.content.push({ type: "image_url", image_url: { url: content.image } });
} else if (content.text) {
apiMessage.content = senderPrefix + content.text;
} else if (content.image && !model.Image) {
apiMessage.content = senderPrefix + "[图片]";
} else { return; } // Skip if no content
messages.push(apiMessage);
});
if (model.InvitePrompts && model.InvitePrompts.trim()) {
messages.push({ role: 'user', content: model.InvitePrompts });
}
if (messages.length === 0 || (messages.length === 1 && messages[0].role === 'system')) {
updateLoadingMessage(model.Name, "错误:没有有效内容发送给 AI。", true);
if (config.AI_CHAT_MODE === 'ButtonSend') { isAiResponding = false; setUiResponding(false); }
return;
}
try {
const requestBody = {
model: model.Model,
messages: messages,
max_tokens: model.Outputtoken || 1500,
temperature: model.Temperature || 0.7,
stream: config.StreamingOutput
};
if (model.Websearch === true) {
requestBody.tools = [{ type: "function", function: { name: "google_search", description: "Perform a Google search.", parameters: { type: "object", properties: { query: { type: "string", description: "The search query." } }, required: ["query"] } } }];
requestBody.tool_choice = "auto";
}
const apiUrl = config.API_URl || "";
if (!apiUrl) {
updateLoadingMessage(model.Name, "错误:API URL 未在配置中定义。", true);
return;
}
const response = await fetch(`${apiUrl}/v1/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${config.API_Key}` },
body: JSON.stringify(requestBody),
signal: AbortSignal.timeout(config.API_Timeout * 1000)
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ message: response.statusText }));
if (retryCount < 2) {
await new Promise(resolve => setTimeout(resolve, 1000));
return await callAiApi(model, retryCount + 1, aiNewlyOptedOutThisTurnRef); // Pass ref
}
updateLoadingMessage(model.Name, `错误: ${errorData.message || response.statusText} (已重试${retryCount}次)`, true);
chatHistory.push({ role: 'assistant', name: model.Name, content: { text: `错误: ${errorData.message || response.statusText} (已重试${retryCount}次)` } });
saveChatHistory();
return;
}
if (config.StreamingOutput) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulatedResponse = "";
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
let breakOuterLoop = false;
for (const line of lines) {
if (line.startsWith('data: ')) {
const dataString = line.substring(6).trim();
if (dataString === '[DONE]') { buffer = '[DONE]'; breakOuterLoop = true; break; }
try {
const chunk = JSON.parse(dataString);
let choice = chunk.choices && chunk.choices[0];
if (choice && choice.delta && choice.delta.content) {
accumulatedResponse += choice.delta.content;
updateLoadingMessage(model.Name, accumulatedResponse + "...", false);
}
// Handle finish_reason if necessary (e.g., tool_calls)
} catch (error) { console.error(`[STREAM ERROR - ${model.Name}] Parsing chunk. Data: "${dataString}". Error:`, error); }
}
}
if (breakOuterLoop || buffer === '[DONE]') break;