fix: pass message index when processing regex script

This commit is contained in:
Bo26fhmC5M
2025-05-18 13:01:32 +09:00
parent 0a792db7c2
commit a41ac074db
2 changed files with 46 additions and 55 deletions

View File

@@ -413,11 +413,17 @@
} }
function isOrphan(summaryIndex: number): boolean { function isOrphan(summaryIndex: number): boolean {
const char = DBState.db.characters[$selectedCharID];
const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage];
const summary = hypaV3DataState.summaries[summaryIndex]; const summary = hypaV3DataState.summaries[summaryIndex];
for (const chatMemo of summary.chatMemos) { for (const chatMemo of summary.chatMemos) {
if (!getMessageFromChatMemo(chatMemo)) { if (chatMemo == null) {
return true; // Check first message exists
if (!getFirstMessage()) return true;
} else {
if (chat.message.findIndex((m) => m.chatId === chatMemo) === -1)
return true;
} }
} }
@@ -437,8 +443,7 @@
const summary = hypaV3DataState.summaries[summaryIndex]; const summary = hypaV3DataState.summaries[summaryIndex];
const toSummarize: OpenAIChat[] = await Promise.all( const toSummarize: OpenAIChat[] = await Promise.all(
summary.chatMemos.map(async (chatMemo) => { summary.chatMemos.map(async (chatMemo) => {
// Processed message const message = await getMessageFromChatMemo(chatMemo);
const message = await getProcessedMessageFromChatMemo(chatMemo);
return { return {
role: (message.role === "char" role: (message.role === "char"
@@ -495,31 +500,28 @@
summaryUIState.isRerolledTranslating = false; summaryUIState.isRerolledTranslating = false;
} }
async function getProcessedMessageFromChatMemo( async function getMessageFromChatMemo(
chatMemo: string | null chatMemo: string | null
): Promise<Message | null> { ): Promise<Message | null> {
const unprocessed = getMessageFromChatMemo(chatMemo);
if (!unprocessed) {
return null;
}
return getCurrentHypaV3Preset().settings.processRegexScript
? await processRegexScript(unprocessed)
: unprocessed;
}
function getMessageFromChatMemo(chatMemo: string | null): Message | null {
const char = DBState.db.characters[$selectedCharID]; const char = DBState.db.characters[$selectedCharID];
const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage]; const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage];
const shouldProcess = getCurrentHypaV3Preset().settings.processRegexScript;
let msg = null;
let msgIndex = -1;
if (chatMemo == null) { if (chatMemo == null) {
const firstMessage = getFirstMessage(); const firstMessage = getFirstMessage();
return firstMessage ? { role: "char", data: firstMessage } : null; if (!firstMessage) return null;
msg = { role: "char", data: firstMessage };
} else {
msgIndex = chat.message.findIndex((m) => m.chatId === chatMemo);
if (msgIndex === -1) return null;
msg = chat.message[msgIndex];
} }
return chat.message.find((m) => m.chatId === chatMemo) || null; return shouldProcess ? await processRegexScript(msg, msgIndex) : msg;
} }
function getFirstMessage(): string | null { function getFirstMessage(): string | null {
@@ -533,15 +535,17 @@
: null; : null;
} }
async function processRegexScript(msg: Message): Promise<Message> { async function processRegexScript(
msg: Message,
msgIndex: number = -1
): Promise<Message> {
const char = DBState.db.characters[$selectedCharID]; const char = DBState.db.characters[$selectedCharID];
const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage];
const newData: string = ( const newData: string = (
await processScriptFull( await processScriptFull(
char, char,
risuChatParser(msg.data, { chara: char, role: msg.role }), risuChatParser(msg.data, { chara: char, role: msg.role }),
"editprocess", "editprocess",
-1, msgIndex,
{ {
chatRole: msg.role, chatRole: msg.role,
} }
@@ -593,8 +597,7 @@
return; return;
} }
// Processed message const message = await getMessageFromChatMemo(
const message = await getProcessedMessageFromChatMemo(
expandedMessageUIState.selectedChatMemo expandedMessageUIState.selectedChatMemo
); );
@@ -632,52 +635,41 @@
} }
} }
async function getProcessedNextSummarizationTarget(): Promise<Message | null> { async function getNextSummarizationTarget(): Promise<Message | null> {
const unprocessed = getNextSummarizationTarget();
if (!unprocessed) {
return null;
}
return getCurrentHypaV3Preset().settings.processRegexScript
? await processRegexScript(unprocessed)
: unprocessed;
}
function getNextSummarizationTarget(): Message | null {
const char = DBState.db.characters[$selectedCharID]; const char = DBState.db.characters[$selectedCharID];
const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage]; const chat = char.chats[DBState.db.characters[$selectedCharID].chatPage];
const shouldProcess = getCurrentHypaV3Preset().settings.processRegexScript;
// Summaries exist // Summaries exist
if (hypaV3DataState.summaries.length > 0) { if (hypaV3DataState.summaries.length > 0) {
const lastSummary = hypaV3DataState.summaries.at(-1); const lastSummary = hypaV3DataState.summaries.at(-1);
const lastMessageIndex = chat.message.findIndex( const lastMessageIndex = chat.message.findIndex(
(msg) => msg.chatId === lastSummary.chatMemos.at(-1) (m) => m.chatId === lastSummary.chatMemos.at(-1)
); );
if (lastMessageIndex !== -1) { if (lastMessageIndex !== -1) {
const nextMessage = chat.message[lastMessageIndex + 1]; const next = chat.message[lastMessageIndex + 1] ?? null;
if (nextMessage) { return next && shouldProcess
return nextMessage; ? await processRegexScript(next, lastMessageIndex + 1)
} : next;
} }
} }
// When no summaries exist OR couldn't find last connected message,
// check if first message is available
const firstMessage = getFirstMessage(); const firstMessage = getFirstMessage();
// When no summaries exist OR couldn't find last connected message if (!firstMessage) {
// Check if first message is available const next = chat.message[0] ?? null;
if (!firstMessage || firstMessage.trim() === "") {
if (chat.message.length > 0) {
return chat.message[0];
}
return null; return next && shouldProcess ? await processRegexScript(next, 0) : next;
} }
// will summarize first message // Will summarize first message
return { role: "char", chatId: "first", data: firstMessage }; const next: Message = { role: "char", chatId: "first", data: firstMessage };
return shouldProcess ? await processRegexScript(next) : next;
} }
function isHypaV2ConversionPossible(): boolean { function isHypaV2ConversionPossible(): boolean {
@@ -1278,8 +1270,7 @@
{#if expandedMessageUIState?.summaryIndex === i} {#if expandedMessageUIState?.summaryIndex === i}
<!-- Expanded Message --> <!-- Expanded Message -->
<div class="mt-2 sm:mt-4"> <div class="mt-2 sm:mt-4">
<!-- Processed Message --> {#await getMessageFromChatMemo(expandedMessageUIState.selectedChatMemo) then expandedMessage}
{#await getProcessedMessageFromChatMemo(expandedMessageUIState.selectedChatMemo) then expandedMessage}
{#if expandedMessage} {#if expandedMessage}
<!-- Role --> <!-- Role -->
<div class="mb-2 sm:mb-4 text-sm text-zinc-400"> <div class="mb-2 sm:mb-4 text-sm text-zinc-400">
@@ -1336,7 +1327,7 @@
<!-- Next Summarization Target --> <!-- Next Summarization Target -->
<div class="mt-2 sm:mt-4"> <div class="mt-2 sm:mt-4">
{#await getProcessedNextSummarizationTarget() then nextMessage} {#await getNextSummarizationTarget() then nextMessage}
{#if nextMessage} {#if nextMessage}
{@const chatId = {@const chatId =
nextMessage.chatId === "first" nextMessage.chatId === "first"

View File

@@ -128,7 +128,7 @@ export async function hypaMemoryV3(
} finally { } finally {
if (settings.summarizationModel !== "subModel") { if (settings.summarizationModel !== "subModel") {
try { try {
unloadEngine(); await unloadEngine();
} catch {} } catch {}
} }
} }