Skip to content

An AI-powered search engine with a generative UI

fix(chat): resolve chat isolation and component prop issues

Dhaval52fdaaab436f
diff --git a/app/page.tsx b/app/page.tsx
index 1bbbce48..1966d0e4 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -7,5 +7,5 @@ import { Chat } from '@/components/chat'
export default async function Page() {
const id = generateId()
const models = await getModels()
- return <Chat id={id} models={models} />
+ return <Chat key={id} id={id} models={models} />
}
diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsx
index b693348a..d1df6a6c 100644
--- a/app/search/[id]/page.tsx
+++ b/app/search/[id]/page.tsx
@@ -75,5 +75,5 @@ export default async function SearchPage(props: {
}
const models = await getModels()
- return <Chat id={id} savedMessages={messages} models={models} />
+ return <Chat key={id} id={id} savedMessages={messages} models={models} />
}
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index 81e3c1a7..deb570cc 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -45,7 +45,7 @@ export function AnswerSection({
<MessageActions
message={content} // Keep original message content for copy
messageId={messageId}
- chatId={chatId}
+ chatId={chatId || ''}
enableShare={enableShare}
reload={handleReload}
/>
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 24830e1e..af176829 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -22,7 +22,7 @@ interface ChatMessagesProps {
data: JSONValue[] | undefined
onQuerySelect: (query: string) => void
isLoading: boolean
- chatId?: string
+ chatId: string
addToolResult?: (params: { toolCallId: string; result: any }) => void
/** Ref for the scroll container */
scrollContainerRef: React.RefObject<HTMLDivElement>
@@ -182,6 +182,7 @@ export function ChatMessages({
isOpen={getIsOpen(manualToolCallId)}
onOpenChange={open => handleOpenChange(manualToolCallId, open)}
addToolResult={addToolResult}
+ chatId={chatId}
/>
)}
</div>
diff --git a/components/chat.tsx b/components/chat.tsx
index 82197d8e..08f90460 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -7,7 +7,6 @@ import { ChatRequestOptions } from 'ai'
import { Message } from 'ai/react'
import { toast } from 'sonner'
-import { CHAT_ID } from '@/lib/constants'
import { Model } from '@/lib/types/models'
import { cn } from '@/lib/utils'
@@ -50,12 +49,16 @@ export function Chat({
reload
} = useChat({
initialMessages: savedMessages,
- id: CHAT_ID,
+ id: id, // Use unique chat ID for isolated streaming
body: {
id
},
onFinish: () => {
- window.history.replaceState({}, '', `/search/${id}`)
+ // Only update URL if we're on the home page (new chat)
+ // Don't update if we're already on a search page to avoid hijacking navigation
+ if (window.location.pathname === '/') {
+ window.history.replaceState({}, '', `/search/${id}`)
+ }
window.dispatchEvent(new CustomEvent('chat-history-updated'))
},
onError: error => {
@@ -121,7 +124,11 @@ export function Chat({
// Scroll to the section when a new user message is sent
useEffect(() => {
- if (sections.length > 0) {
+ // Only scroll if this chat is currently visible in the URL
+ const isCurrentChat = window.location.pathname === `/search/${id}` ||
+ (window.location.pathname === '/' && sections.length > 0)
+
+ if (isCurrentChat && sections.length > 0) {
const lastMessage = messages[messages.length - 1]
if (lastMessage && lastMessage.role === 'user') {
// If the last message is from user, find the corresponding section
@@ -132,7 +139,7 @@ export function Chat({
})
}
}
- }, [sections, messages])
+ }, [sections, messages, id])
useEffect(() => {
setMessages(savedMessages)
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
index 6b32a2fe..e4ffb1e2 100644
--- a/components/message-actions.tsx
+++ b/components/message-actions.tsx
@@ -4,7 +4,6 @@ import { useChat } from '@ai-sdk/react'
import { Copy } from 'lucide-react'
import { toast } from 'sonner'
-import { CHAT_ID } from '@/lib/constants'
import { cn } from '@/lib/utils'
import { Button } from './ui/button'
@@ -15,7 +14,7 @@ interface MessageActionsProps {
message: string
messageId: string
reload?: () => Promise<string | null | undefined>
- chatId?: string
+ chatId: string
enableShare?: boolean
className?: string
}
@@ -29,7 +28,7 @@ export function MessageActions({
className
}: MessageActionsProps) {
const { status } = useChat({
- id: CHAT_ID
+ id: chatId
})
const isLoading = status === 'submitted' || status === 'streaming'
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index b3b6c38e..d81aadec 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -6,8 +6,6 @@ import { useChat } from '@ai-sdk/react'
import { JSONValue } from 'ai'
import { ArrowRight } from 'lucide-react'
-import { CHAT_ID } from '@/lib/constants'
-
import { Button } from './ui/button'
import { Skeleton } from './ui/skeleton'
import { CollapsibleMessage } from './collapsible-message'
@@ -18,6 +16,7 @@ export interface RelatedQuestionsProps {
onQuerySelect: (query: string) => void
isOpen: boolean
onOpenChange: (open: boolean) => void
+ chatId: string
}
interface RelatedQuestionsAnnotation extends Record<string, JSONValue> {
@@ -31,10 +30,11 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
annotations,
onQuerySelect,
isOpen,
- onOpenChange
+ onOpenChange,
+ chatId
}) => {
const { status } = useChat({
- id: CHAT_ID
+ id: chatId
})
const isLoading = status === 'submitted' || status === 'streaming'
diff --git a/components/render-message.tsx b/components/render-message.tsx
index 01c332b4..cc15dac0 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -119,6 +119,7 @@ export function RenderMessage({
isOpen={getIsOpen(tool.toolCallId)}
onOpenChange={open => onOpenChange(tool.toolCallId, open)}
addToolResult={addToolResult}
+ chatId={chatId}
/>
))}
{message.parts?.map((part, index) => {
@@ -136,6 +137,7 @@ export function RenderMessage({
onOpenChange(part.toolInvocation.toolCallId, open)
}
addToolResult={addToolResult}
+ chatId={chatId}
/>
)
case 'text':
@@ -175,6 +177,7 @@ export function RenderMessage({
onQuerySelect={onQuerySelect}
isOpen={getIsOpen(`${messageId}-related`)}
onOpenChange={open => onOpenChange(`${messageId}-related`, open)}
+ chatId={chatId || ''}
/>
)}
</>
diff --git a/components/search-section.tsx b/components/search-section.tsx
index c17ad2a0..baaa6f07 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -3,7 +3,6 @@
import { useChat } from '@ai-sdk/react'
import { ToolInvocation } from 'ai'
-import { CHAT_ID } from '@/lib/constants'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
import { useArtifact } from '@/components/artifact/artifact-context'
@@ -18,15 +17,17 @@ interface SearchSectionProps {
tool: ToolInvocation
isOpen: boolean
onOpenChange: (open: boolean) => void
+ chatId: string
}
export function SearchSection({
tool,
isOpen,
- onOpenChange
+ onOpenChange,
+ chatId
}: SearchSectionProps) {
const { status } = useChat({
- id: CHAT_ID
+ id: chatId
})
const isLoading = status === 'submitted' || status === 'streaming'
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index 98ad2c16..f5425f71 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -12,13 +12,15 @@ interface ToolSectionProps {
isOpen: boolean
onOpenChange: (open: boolean) => void
addToolResult?: (params: { toolCallId: string; result: any }) => void
+ chatId?: string
}
export function ToolSection({
tool,
isOpen,
onOpenChange,
- addToolResult
+ addToolResult,
+ chatId
}: ToolSectionProps) {
// Special handling for ask_question tool
if (tool.toolName === 'ask_question') {
@@ -62,6 +64,7 @@ export function ToolSection({
tool={tool}
isOpen={isOpen}
onOpenChange={onOpenChange}
+ chatId={chatId || ''}
/>
)
case 'videoSearch':
@@ -70,6 +73,7 @@ export function ToolSection({
tool={tool}
isOpen={isOpen}
onOpenChange={onOpenChange}
+ chatId={chatId || ''}
/>
)
case 'retrieve':
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
index f39cc28f..a7fd239d 100644
--- a/components/video-search-section.tsx
+++ b/components/video-search-section.tsx
@@ -3,7 +3,6 @@
import { useChat } from '@ai-sdk/react'
import { ToolInvocation } from 'ai'
-import { CHAT_ID } from '@/lib/constants'
import type { SerperSearchResults } from '@/lib/types'
import { useArtifact } from '@/components/artifact/artifact-context'
@@ -17,15 +16,17 @@ interface VideoSearchSectionProps {
tool: ToolInvocation
isOpen: boolean
onOpenChange: (open: boolean) => void
+ chatId: string
}
export function VideoSearchSection({
tool,
isOpen,
- onOpenChange
+ onOpenChange,
+ chatId
}: VideoSearchSectionProps) {
const { status } = useChat({
- id: CHAT_ID
+ id: chatId
})
const isLoading = status === 'submitted' || status === 'streaming'