fix: resolve chatMemos serialization issue in HypaV2Data

This commit is contained in:
Bo26fhmC5M
2025-01-02 18:47:16 +09:00
parent 821ba1812f
commit 2b92582df4
2 changed files with 52 additions and 17 deletions

View File

@@ -26,6 +26,16 @@ export interface HypaV2Data {
}[]; }[];
} }
// Reuse HypaV2Data and override only chatMemos in mainChunks
export interface SerializableHypaV2Data extends Omit<HypaV2Data, 'mainChunks'> {
mainChunks: {
id: number;
text: string;
chatMemos: string[]; // Override Set<string> with string[]
lastChatMemo: string;
}[];
}
async function summary( async function summary(
stringlizedChat: string stringlizedChat: string
): Promise<{ success: boolean; data: string }> { ): Promise<{ success: boolean; data: string }> {
@@ -294,6 +304,34 @@ export async function regenerateSummary(
const targetMainChunk = data.mainChunks[mainChunkIndex]; const targetMainChunk = data.mainChunks[mainChunkIndex];
} }
function toSerializableHypaV2Data(data: HypaV2Data): SerializableHypaV2Data {
return {
...data,
mainChunks: data.mainChunks.map(mainChunk => ({
...mainChunk,
chatMemos: Array.from(mainChunk.chatMemos),
})),
};
}
function toHypaV2Data(data: SerializableHypaV2Data): HypaV2Data {
// Handle corrupted data due to invalid json serialization
data.mainChunks.forEach((mainChunk) => {
if (!Array.isArray(mainChunk.chatMemos)) {
mainChunk.chatMemos = [];
}
});
return {
...data,
mainChunks: data.mainChunks.map(mainChunk => ({
...mainChunk,
chatMemos: new Set(mainChunk.chatMemos),
})),
};
}
export async function hypaMemoryV2( export async function hypaMemoryV2(
chats: OpenAIChat[], chats: OpenAIChat[],
currentTokens: number, currentTokens: number,
@@ -305,26 +343,23 @@ export async function hypaMemoryV2(
currentTokens: number; currentTokens: number;
chats: OpenAIChat[]; chats: OpenAIChat[];
error?: string; error?: string;
memory?: HypaV2Data; memory?: SerializableHypaV2Data;
}> { }> {
const db = getDatabase(); const db = getDatabase();
let data: HypaV2Data = {
if(room.hypaV2Data && isOldHypaV2Data(room.hypaV2Data)){
console.log("Old HypaV2 data detected. Converting to new format...");
room.hypaV2Data = convertOldToNewHypaV2Data(room.hypaV2Data, chats);
}
const data: HypaV2Data = room.hypaV2Data ?? {
lastMainChunkID: 0, lastMainChunkID: 0,
chunks: [], chunks: [],
mainChunks: [], mainChunks: [],
}; };
// JSON s
data.mainChunks.forEach(mainChunk => { if (room.hypaV2Data) {
if (mainChunk.chatMemos && Array.isArray(mainChunk.chatMemos)) { if (isOldHypaV2Data(room.hypaV2Data)) {
mainChunk.chatMemos = new Set(mainChunk.chatMemos); console.log("Old HypaV2 data detected. Converting to new format...");
} data = convertOldToNewHypaV2Data(room.hypaV2Data, chats);
}); } else {
data = toHypaV2Data(room.hypaV2Data);
}
}
// Clean invalid HypaV2 data // Clean invalid HypaV2 data
cleanInvalidChunks(chats, data); cleanInvalidChunks(chats, data);
@@ -590,6 +625,6 @@ export async function hypaMemoryV2(
return { return {
currentTokens: currentTokens, currentTokens: currentTokens,
chats: unsummarizedChats, chats: unsummarizedChats,
memory: data, memory: toSerializableHypaV2Data(data),
}; };
} }

View File

@@ -1255,7 +1255,7 @@ export interface Chat{
localLore: loreBook[] localLore: loreBook[]
sdData?:string sdData?:string
supaMemoryData?:string supaMemoryData?:string
hypaV2Data?:HypaV2Data hypaV2Data?:SerializableHypaV2Data
lastMemory?:string lastMemory?:string
suggestMessages?:string[] suggestMessages?:string[]
isStreaming?:boolean isStreaming?:boolean
@@ -1611,7 +1611,7 @@ import { encode as encodeMsgpack, decode as decodeMsgpack } from "msgpackr";
import * as fflate from "fflate"; import * as fflate from "fflate";
import type { OnnxModelFiles } from '../process/transformers'; import type { OnnxModelFiles } from '../process/transformers';
import type { RisuModule } from '../process/modules'; import type { RisuModule } from '../process/modules';
import type { HypaV2Data } from '../process/memory/hypav2'; import type { SerializableHypaV2Data } from '../process/memory/hypav2';
import { decodeRPack, encodeRPack } from '../rpack/rpack_bg'; import { decodeRPack, encodeRPack } from '../rpack/rpack_bg';
import { DBState, selectedCharID } from '../stores.svelte'; import { DBState, selectedCharID } from '../stores.svelte';
import { LLMFlags, LLMFormat } from '../model/modellist'; import { LLMFlags, LLMFormat } from '../model/modellist';