From ee3b82926b37843f2771c6a8d432781a557ea96a Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 9 May 2026 06:25:38 +0900 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 6aa1542449..d469c58729 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -1353,6 +1353,30 @@ ? chatContent.history : convertMessagesToHistory(chatContent.messages); + // Sanitize history: repair orphaned references from failed regenerations (#24424) + for (const message of Object.values(history.messages)) { + if (message.childrenIds) { + message.childrenIds = message.childrenIds.filter( + (childId) => history.messages[childId] + ); + } + } + if (history.currentId && !history.messages[history.currentId]) { + const messageIds = Object.keys(history.messages); + let lastMessageId = null; + for (const messageId of messageIds) { + const message = history.messages[messageId]; + if ( + (message.childrenIds ?? []).length === 0 && + (!lastMessageId || + (message.timestamp ?? 0) > (history.messages[lastMessageId].timestamp ?? 0)) + ) { + lastMessageId = messageId; + } + } + history.currentId = lastMessageId ?? messageIds[0] ?? null; + } + chatTitle.set(chatContent.title); params = chatContent?.params ?? {};