An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: message regeneration with AI SDK ID mismatch
Yoshiki Miura5c754f370691
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 950c0cf4..02c212c6 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -68,11 +68,31 @@ export async function createChatStreamResponse(throw new Error('No messages found')}- const messageIndex = currentChat.messages.findIndex(+ let messageIndex = currentChat.messages.findIndex(m => m.id === messageId)++ // Fallback: If message not found by ID, try to find by position+ // This handles cases where AI SDK generates different IDsif (messageIndex === -1) {- throw new Error(`Message ${messageId} not found`)+ // For assistant message regeneration: find the last assistant message+ const lastAssistantIndex = currentChat.messages.findLastIndex(+ m => m.role === 'assistant'+ )++ // For user message edit: find the last user message+ const lastUserIndex = currentChat.messages.findLastIndex(+ m => m.role === 'user'+ )++ // Use the most recent message (either user or assistant)+ if (lastAssistantIndex >= 0 || lastUserIndex >= 0) {+ messageIndex = Math.max(lastAssistantIndex, lastUserIndex)+ } else {+ throw new Error(+ `Message ${messageId} not found and no fallback available`+ )+ }}// Check if it's an assistant message that needs regeneration