mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 17:25:51 +00:00
Update cwb_core.js
This commit is contained in:
@@ -7,8 +7,9 @@ import { extractBlocksByTags, applyExclusionRules } from '../../core/utils/rag-t
|
|||||||
import { getExtensionSettings } from '../../utils/settings.js';
|
import { getExtensionSettings } from '../../utils/settings.js';
|
||||||
import { getPresetPrompts, getMixedOrder } from '../../PresetSettings/index.js';
|
import { getPresetPrompts, getMixedOrder } from '../../PresetSettings/index.js';
|
||||||
import { generateRandomSeed } from '../../core/api.js';
|
import { generateRandomSeed } from '../../core/api.js';
|
||||||
|
import { getChatIdentifier } from '../../core/lore.js';
|
||||||
|
|
||||||
const { SillyTavern, TavernHelper, jQuery } = window;
|
const { SillyTavern, TavernHelper, jQuery, characters } = window;
|
||||||
|
|
||||||
let isUpdatingCard = false;
|
let isUpdatingCard = false;
|
||||||
let isBatchUpdating = false;
|
let isBatchUpdating = false;
|
||||||
@@ -77,31 +78,34 @@ export async function updateCardUpdateStatusDisplay($panel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadAllChatMessages($panel) {
|
async function loadAllChatMessages($panel) {
|
||||||
logDebug('尝试加载所有聊天消息...');
|
logDebug('尝试使用 getContext() 加载所有聊天消息...');
|
||||||
if (!TavernHelper || !SillyTavern) {
|
if (!SillyTavern) {
|
||||||
logError('用于加载消息的API不可用。');
|
logError('SillyTavern API 不可用。');
|
||||||
state.allChatMessages = [];
|
state.allChatMessages = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const context = SillyTavern.getContext();
|
const context = SillyTavern.getContext();
|
||||||
const chatLength = context?.chat?.length || 0;
|
const chat = context?.chat || [];
|
||||||
|
|
||||||
if (chatLength === 0) {
|
if (chat.length === 0) {
|
||||||
logDebug('聊天为空,无需加载消息。');
|
logDebug('聊天为空,无需加载消息。');
|
||||||
state.allChatMessages = [];
|
state.allChatMessages = [];
|
||||||
} else {
|
} else {
|
||||||
const lastMessageId = chatLength - 1;
|
state.allChatMessages = chat.map((msg, idx) => ({
|
||||||
const messagesFromApi = await TavernHelper.getChatMessages(`0-${lastMessageId}`, { include_swipes: false });
|
...msg,
|
||||||
state.allChatMessages = Array.isArray(messagesFromApi) ? messagesFromApi.map((msg, idx) => ({ ...msg, id: idx })) : [];
|
message: msg.mes,
|
||||||
|
id: idx
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
logDebug(`成功为 ${state.currentChatFileIdentifier} 加载了 ${state.allChatMessages.length} 条消息。`);
|
logDebug(`成功为 ${state.currentChatFileIdentifier} 加载了 ${state.allChatMessages.length} 条消息。`);
|
||||||
await updateCardUpdateStatusDisplay($panel);
|
await updateCardUpdateStatusDisplay($panel);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logError('获取聊天消息时发生严重错误:', error);
|
logError('使用 getContext() 获取聊天消息时发生严重错误:', error);
|
||||||
|
showToastr('error', '获取聊天记录时发生内部错误。');
|
||||||
state.allChatMessages = [];
|
state.allChatMessages = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -376,21 +380,21 @@ async function triggerAutomaticUpdate($panel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getLatestChatName() {
|
export async function getLatestChatName() {
|
||||||
let newChatFileIdentifier = 'unknown_chat_fallback';
|
let attempts = 0;
|
||||||
try {
|
const maxAttempts = 50;
|
||||||
let chatNameFromCommand = await TavernHelper.triggerSlash('/getchatname');
|
const interval = 100;
|
||||||
if (chatNameFromCommand && typeof chatNameFromCommand === 'string' && chatNameFromCommand.trim() && !['null', 'undefined'].includes(chatNameFromCommand.trim())) {
|
|
||||||
newChatFileIdentifier = cleanChatName(chatNameFromCommand.trim());
|
while (attempts < maxAttempts) {
|
||||||
} else {
|
const context = getContext();
|
||||||
const contextFallback = SillyTavern.getContext();
|
if (context && context.chatId) {
|
||||||
if (contextFallback && contextFallback.chat) {
|
return context.chatId;
|
||||||
newChatFileIdentifier = cleanChatName(contextFallback.chat);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
await new Promise((resolve) => setTimeout(resolve, interval));
|
||||||
logError('获取最新聊天名称时出错:', error);
|
attempts++;
|
||||||
}
|
}
|
||||||
return newChatFileIdentifier;
|
|
||||||
|
logError("[CWB] 长时间等待后,仍无法确定聊天ID。");
|
||||||
|
return "unknown_chat_timeout";
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function handleMessageReceived($panel) {
|
export async function handleMessageReceived($panel) {
|
||||||
|
|||||||
Reference in New Issue
Block a user