An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Merge branch 'main' of https://github.com/miurla/morphic into v0.5
Yoshiki Miura9dd2d3145309
diff --cc components/chat-messages.tsxindex 38fd2838,40e47abd..aa550f07--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@@ -17,13 -24,10 +24,7 @@@ interface ChatMessagesProps/** Ref for the scroll container */scrollContainerRef: React.RefObject<HTMLDivElement>onUpdateMessage?: (messageId: string, newContent: string) => Promise<void>- reload?: (- messageId: string,- options?: ChatRequestOptions- ) => Promise<string | null | undefined>+ reload?: (messageId: string) => Promise<void | string | null | undefined>- /** Whether auto-scroll is enabled (used by the button, now removed) */- // isAutoScroll?: boolean // No longer needed- /** Function to enable auto-scroll (used by the button, now removed) */- // enableAutoScroll?: () => void // No longer needed}export function ChatMessages({diff --cc components/chat.tsxindex c8e92747,4ab2f821..3bf3c588--- a/components/chat.tsx+++ b/components/chat.tsx@@@ -1,14 -1,12 +1,12 @@@'use client'+import { deleteTrailingMessages } from '@/lib/actions/chat-db'import { CHAT_ID } from '@/lib/constants'- import { useAutoScroll } from '@/lib/hooks/use-auto-scroll'import { Model } from '@/lib/types/models'-import { cn } from '@/lib/utils'+import { cn, generateUUID } from '@/lib/utils'import { useChat } from '@ai-sdk/react'- import { Message as UIMessage } from 'ai'- import { useRouter } from 'next/navigation'- import { useEffect, useRef, useTransition } from 'react'-import { ChatRequestOptions } from 'ai'+ import { Message } from 'ai/react'+ import { useEffect, useMemo, useRef, useState } from 'react'import { toast } from 'sonner'import { ChatMessages } from './chat-messages'import { ChatPanel } from './chat-panel'@@@ -86,128 -143,33 +145,128 @@@ export function Chat(}const handleUpdateAndReloadMessage = async (- messageId: string,- newContent: string+ editedMessageId: string,+ newContentText: string) => {- setMessages(currentMessages =>- currentMessages.map(msg =>- msg.id === messageId ? { ...msg, content: newContent } : msg+ if (!id) {+ toast.error('Chat ID is missing.')+ console.error(+ 'handleUpdateAndReloadMessage: chatId (id prop) is undefined.')- )+ return+ }++ const pivotMessage = messages.find(m => m.id === editedMessageId)+ if (!pivotMessage) {+ toast.error('Original message not found to edit locally.')+ console.error(+ 'handleUpdateAndReloadMessage: Pivot message not found for timestamp.'+ )+ return+ }+ const pivotTimestamp =+ pivotMessage.createdAt?.toISOString() ?? new Date(0).toISOString()try {- const messageIndex = messages.findIndex(msg => msg.id === messageId)- if (messageIndex === -1) return+ setMessages(prevMessages => {+ const messageIndex = prevMessages.findIndex(+ m => m.id === editedMessageId+ )+ const messagesBeforeEdited =+ messageIndex !== -1+ ? prevMessages.slice(0, messageIndex)+ : prevMessages- const newUIMessage: UIMessage = {- const messagesUpToEdited = messages.slice(0, messageIndex + 1)++ const newUIMessage: Message = {+ id: generateUUID(),+ role: 'user',+ content: newContentText,+ parts: [{ type: 'text', text: newContentText }],+ createdAt: new Date()+ }- setMessages(messagesUpToEdited)+ return [...messagesBeforeEdited, newUIMessage]+ })- setData(undefined)+ await deleteTrailingMessages(id, pivotTimestamp)+ await reload()+ } catch (error) {+ console.error('Error during message edit and reload process:', error)+ toast.error(+ `Error processing edited message: ${(error as Error).message}`+ )+ }+ }++ const handleReloadFrom = async (reloadFromFollowerMessageId: string) => {+ if (!id) {+ toast.error('Chat ID is missing for reload.')+ return+ }- await reload({- body: {- chatId: id,- regenerate: true+ const followerMessageIndex = messages.findIndex(+ m => m.id === reloadFromFollowerMessageId+ )++ if (followerMessageIndex < 1) {+ toast.error(+ 'Cannot reload: No preceding message found or message is the first.'+ )+ console.error(+ `handleReloadFrom: No message found before id ${reloadFromFollowerMessageId} or it is the first message.`+ )+ return+ }++ const targetUserMessageIndex = followerMessageIndex - 1+ const targetUserMessage = messages[targetUserMessageIndex]++ if (targetUserMessage.role !== 'user') {+ toast.error(+ 'Cannot reload: The message to resend must be a user message.'+ )+ console.error(+ `handleReloadFrom: Preceding message (id: ${targetUserMessage.id}) is not a user message.`+ )+ return+ }++ const followerMessage = messages[followerMessageIndex]+ const deletionTimestamp =+ followerMessage.createdAt?.toISOString() ?? new Date(0).toISOString()++ const contentToResend =+ targetUserMessage.parts+ ?.filter(p => p.type === 'text')+ .map(p => p.text)+ .join('') ||+ targetUserMessage.content ||+ ''++ try {+ setMessages(prevMessages => {+ const messagesBeforeTarget = prevMessages.slice(+ 0,+ targetUserMessageIndex+ )+- const newResentUserMessage: UIMessage = {++ const newResentUserMessage: Message = {+ id: generateUUID(),+ role: 'user',+ content: contentToResend,+ parts: [{ type: 'text', text: contentToResend }],+ createdAt: new Date()}+ return [...messagesBeforeTarget, newResentUserMessage]})++ await deleteTrailingMessages(id, deletionTimestamp)+ await reload()} catch (error) {- console.error('Failed to reload after message update:', error)+ console.error(+ `Error during reload from message preceding ${reloadFromFollowerMessageId}:`,+ error+ )toast.error(`Failed to reload conversation: ${(error as Error).message}`)}}