Skip to content

An AI-powered search engine with a generative UI

Refactor chat messages and components

Yoshiki Miuraf91fb916a283
diff --git a/app/actions.tsx b/app/actions.tsx
index c7516ea8..2046145a 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -34,10 +34,9 @@ async function submit(formData?: FormData, skip?: boolean) {
].filter(
message =>
message.role !== 'tool' &&
- message.type !== undefined &&
message.type !== 'followup' &&
message.type !== 'related' &&
- message.type !== 'inquiry'
+ message.content !== 'end'
)
// goupeiId is used to group the messages for collapse
@@ -187,7 +186,7 @@ async function submit(formData?: FormData, skip?: boolean) {
// Add the answer, related queries, and follow-up panel to the state
// Wait for 1 second before adding the answer to the state
- await new Promise(resolve => setTimeout(resolve, 1000))
+ await new Promise(resolve => setTimeout(resolve, 500))
aiState.done({
...aiState.get(),
@@ -274,8 +273,10 @@ export const AI = createAI<AIState, UIState>({
const userId = 'anonymous'
const path = `/search/${chatId}`
const title =
- JSON.parse(messages[0].content)?.input?.substring(0, 100) || 'Untitled'
-
+ messages.length > 0
+ ? JSON.parse(messages[0].content)?.input?.substring(0, 100) ||
+ 'Untitled'
+ : 'Untitled'
// Add an 'end' message at the end to determine if the history needs to be reloaded
const updatedMessages: AIMessage[] = [
...messages,
@@ -314,20 +315,12 @@ export const getUIStateFromAIState = (aiState: Chat) => {
const value = type === 'input' ? json.input : json.related_query
return {
id,
- component: (
- <Section>
- <UserMessage message={value} />
- </Section>
- )
+ component: <UserMessage message={value} />
}
case 'inquiry':
return {
id,
- component: (
- <Section>
- <CopilotDisplay content={content} />
- </Section>
- )
+ component: <CopilotDisplay content={content} />
}
}
case 'assistant':
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 3d7e5568..88b797c8 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -52,7 +52,9 @@ export function ChatMessages({ messages }: ChatMessagesProps) {
key={`${groupedMessage.id}`}
message={{
id: groupedMessage.id,
- component: <div key={index}>{groupedMessage.components}</div>,
+ component: groupedMessage.components.map((component, i) => (
+ <div key={`${groupedMessage.id}-${i}`}>{component}</div>
+ )),
isCollapsed: groupedMessage.isCollapsed
}}
isLastMessage={
diff --git a/components/chat.tsx b/components/chat.tsx
index 4b4fc9d7..7fd51524 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -25,10 +25,8 @@ export function Chat({ id }: ChatProps) {
useEffect(() => {
console.log('aiState', aiState.messages[aiState.messages.length - 1]?.type)
if (aiState.messages[aiState.messages.length - 1]?.type === 'followup') {
- // Refresh the page after 1 second to chat history updates
- setTimeout(() => {
- router.refresh()
- }, 1000)
+ // Refresh the page to chat history updates
+ router.refresh()
}
}, [aiState, router])
diff --git a/components/followup-panel.tsx b/components/followup-panel.tsx
index 68b85e83..43034d84 100644
--- a/components/followup-panel.tsx
+++ b/components/followup-panel.tsx
@@ -20,7 +20,7 @@ export function FollowupPanel() {
const userMessage = {
id: Date.now(),
isGenerating: false,
- component: <UserMessage message={input} isFirstMessage={false} />
+ component: <UserMessage message={input} />
}
const responseMessage = await submit(formData)
diff --git a/components/search-related.tsx b/components/search-related.tsx
index 5a3f04ee..af7b2b88 100644
--- a/components/search-related.tsx
+++ b/components/search-related.tsx
@@ -40,7 +40,7 @@ export const SearchRelated: React.FC<SearchRelatedProps> = ({
const userMessage = {
id: Date.now(),
- component: <UserMessage message={query} isFirstMessage={false} />
+ component: <UserMessage message={query} />
}
const responseMessage = await submit(formData)
diff --git a/components/user-message.tsx b/components/user-message.tsx
index cb8ca25f..678070d4 100644
--- a/components/user-message.tsx
+++ b/components/user-message.tsx
@@ -1,17 +1,12 @@
-import { cn } from '@/lib/utils'
import React from 'react'
type UserMessageProps = {
message: string
- isFirstMessage?: boolean
}
-export const UserMessage: React.FC<UserMessageProps> = ({
- message,
- isFirstMessage
-}) => {
+export const UserMessage: React.FC<UserMessageProps> = ({ message }) => {
return (
- <div className={cn({ 'mt-4': !isFirstMessage })}>
+ <div className="mt-6">
<div className="text-xl">{message}</div>
</div>
)