Skip to content

An AI-powered search engine with a generative UI

feat: Add render message

Yoshiki Miura669c1dae1e4f
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index 832705bb..ed681a85 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -1,37 +1,19 @@
'use client'
import { Section } from './section'
-import { StreamableValue, useStreamableValue } from 'ai/rsc'
import { BotMessage } from './message'
-import { useEffect, useState } from 'react'
import { DefaultSkeleton } from './default-skeleton'
export type AnswerSectionProps = {
- result?: StreamableValue<string>
- hasHeader?: boolean
+ content: string
}
-export function AnswerSection({
- result,
- hasHeader = true
-}: AnswerSectionProps) {
- const [data, error, pending] = useStreamableValue(result)
- const [content, setContent] = useState<string>('')
-
- useEffect(() => {
- if (!data) return
- setContent(data)
- }, [data])
-
+export function AnswerSection({ content }: AnswerSectionProps) {
return (
<div>
- {content.length > 0 ? (
- <Section title={hasHeader ? 'Answer' : undefined}>
- <BotMessage content={content} />
- </Section>
- ) : (
- <DefaultSkeleton />
- )}
+ <Section title="Answer">
+ {content ? <BotMessage message={content} /> : <DefaultSkeleton />}
+ </Section>
</div>
)
}
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 8b4c420d..9e2cdc30 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -1,7 +1,6 @@
import { Message } from 'ai'
-import { UserMessage } from './user-message'
-import { BotMessage } from './message'
import RelatedQuestions from './related-questions'
+import { RenderMessage } from './render-message'
interface ChatMessagesProps {
messages: Message[]
@@ -17,11 +16,7 @@ export function ChatMessages({ messages, onQuerySelect }: ChatMessagesProps) {
<div className="relative mx-auto px-4 w-full">
{messages.map(message => (
<div key={message.id} className="mb-4">
- {message.role === 'user' ? (
- <UserMessage message={message.content} />
- ) : (
- <BotMessage message={message.content} />
- )}
+ <RenderMessage message={message} />
{!message.toolInvocations && message.annotations && (
<RelatedQuestions
annotations={message.annotations}
diff --git a/components/default-skeleton.tsx b/components/default-skeleton.tsx
index 6c086e2b..0cc587bf 100644
--- a/components/default-skeleton.tsx
+++ b/components/default-skeleton.tsx
@@ -5,7 +5,7 @@ import { Skeleton } from './ui/skeleton'
export const DefaultSkeleton = () => {
return (
- <div className="flex flex-col gap-2 pb-4">
+ <div className="flex flex-col gap-2 pb-4 pt-2">
<Skeleton className="h-6 w-48" />
<Skeleton className="w-full h-6" />
</div>
diff --git a/components/render-message.tsx b/components/render-message.tsx
new file mode 100644
index 00000000..dc6399ab
--- /dev/null
+++ b/components/render-message.tsx
@@ -0,0 +1,26 @@
+import { Message } from 'ai'
+import { UserMessage } from './user-message'
+import { ToolSection } from './tool-section'
+import { AnswerSection } from './answer-section'
+
+interface RenderMessageProps {
+ message: Message
+}
+
+export function RenderMessage({ message }: RenderMessageProps) {
+ if (message.role === 'user') {
+ return <UserMessage message={message.content} />
+ }
+
+ if (message.toolInvocations?.length) {
+ return (
+ <>
+ {message.toolInvocations.map(tool => (
+ <ToolSection key={tool.toolCallId} tool={tool} />
+ ))}
+ </>
+ )
+ }
+
+ return <AnswerSection content={message.content} />
+}
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index d9c4c404..908eff65 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -1,17 +1,33 @@
-import React from 'react'
-import { Section } from '@/components/section'
+'use client'
+
+import { Section, ToolArgsSection } from '@/components/section'
import { SearchResults } from '@/components/search-results'
import { SearchResults as SearchResultsType } from '@/lib/types'
+import { ToolInvocation } from 'ai'
+import { DefaultSkeleton } from './default-skeleton'
+import { ToolBadge } from './tool-badge'
interface RetrieveSectionProps {
- data: SearchResultsType
+ tool: ToolInvocation
}
-const RetrieveSection: React.FC<RetrieveSectionProps> = ({ data }) => {
+export function RetrieveSection({ tool }: RetrieveSectionProps) {
+ const isLoading = tool.state === 'call'
+ const data: SearchResultsType =
+ tool.state === 'result' ? tool.result : undefined
+ const url = tool.args.url as string | undefined
+
return (
- <Section title="Sources">
- <SearchResults results={data.results} />
- </Section>
+ <div>
+ <ToolArgsSection tool="retrieve">{url}</ToolArgsSection>
+ {!isLoading && data ? (
+ <Section title="Sources">
+ <SearchResults results={data.results} />
+ </Section>
+ ) : (
+ <DefaultSkeleton />
+ )}
+ </div>
)
}
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 8c6455ad..c111db58 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -3,41 +3,41 @@
import { SearchResults } from './search-results'
import { DefaultSkeleton } from './default-skeleton'
import { SearchResultsImageSection } from './search-results-image'
-import { Section } from './section'
-import { ToolBadge } from './tool-badge'
+import { Section, ToolArgsSection } from './section'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
-import { StreamableValue, useStreamableValue } from 'ai/rsc'
+import { ToolInvocation } from 'ai'
-export type SearchSectionProps = {
- result?: StreamableValue<string>
- includeDomains?: string[]
+interface SearchSectionProps {
+ tool: ToolInvocation
}
-export function SearchSection({ result, includeDomains }: SearchSectionProps) {
- const [data, error, pending] = useStreamableValue(result)
- const searchResults: TypeSearchResults = data ? JSON.parse(data) : undefined
+export function SearchSection({ tool }: SearchSectionProps) {
+ const isLoading = tool.state === 'call'
+ const searchResults: TypeSearchResults =
+ tool.state === 'result' ? tool.result : undefined
+ const query = tool.args.query as string | undefined
+ const includeDomains = tool.args.includeDomains as string[] | undefined
const includeDomainsString = includeDomains
? ` [${includeDomains.join(', ')}]`
: ''
+
return (
<div>
- {!pending && data ? (
- <>
- <Section size="sm" className="pt-2 pb-0">
- <ToolBadge tool="search">{`${searchResults.query}${includeDomainsString}`}</ToolBadge>
- </Section>
- {searchResults.images && searchResults.images.length > 0 && (
- <Section title="Images">
- <SearchResultsImageSection
- images={searchResults.images}
- query={searchResults.query}
- />
- </Section>
- )}
- <Section title="Sources">
- <SearchResults results={searchResults.results} />
+ <ToolArgsSection tool="search">{`${query}${includeDomainsString}`}</ToolArgsSection>
+ {searchResults &&
+ searchResults.images &&
+ searchResults.images.length > 0 && (
+ <Section title="Images">
+ <SearchResultsImageSection
+ images={searchResults.images}
+ query={query}
+ />
</Section>
- </>
+ )}
+ {!isLoading && searchResults.results ? (
+ <Section title="Sources">
+ <SearchResults results={searchResults.results} />
+ </Section>
) : (
<DefaultSkeleton />
)}
diff --git a/components/section.tsx b/components/section.tsx
index a5c20a10..ec7ea155 100644
--- a/components/section.tsx
+++ b/components/section.tsx
@@ -12,6 +12,7 @@ import {
} from 'lucide-react'
import React from 'react'
import { Separator } from './ui/separator'
+import { ToolBadge } from './tool-badge'
type SectionProps = {
children: React.ReactNode
@@ -75,3 +76,17 @@ export const Section: React.FC<SectionProps> = ({
</>
)
}
+
+export function ToolArgsSection({
+ children,
+ tool
+}: {
+ children: React.ReactNode
+ tool: string
+}) {
+ return (
+ <Section size="sm" className="pt-2 pb-0">
+ <ToolBadge tool={tool}>{children}</ToolBadge>
+ </Section>
+ )
+}
diff --git a/components/tool-badge.tsx b/components/tool-badge.tsx
index f2a22c10..59684026 100644
--- a/components/tool-badge.tsx
+++ b/components/tool-badge.tsx
@@ -1,5 +1,5 @@
import React from 'react'
-import { Search } from 'lucide-react'
+import { Link, Search, Video } from 'lucide-react'
import { Badge } from './ui/badge'
type ToolBadgeProps = {
@@ -14,7 +14,9 @@ export const ToolBadge: React.FC<ToolBadgeProps> = ({
className
}) => {
const icon: Record<string, React.ReactNode> = {
- search: <Search size={14} />
+ search: <Search size={14} />,
+ retrieve: <Link size={14} />,
+ video_search: <Video size={14} />
}
return (
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
new file mode 100644
index 00000000..48942289
--- /dev/null
+++ b/components/tool-section.tsx
@@ -0,0 +1,23 @@
+'use client'
+
+import { ToolInvocation } from 'ai'
+import { SearchSection } from './search-section'
+import { VideoSearchSection } from './video-search-section'
+import RetrieveSection from './retrieve-section'
+
+interface ToolSectionProps {
+ tool: ToolInvocation
+}
+
+export function ToolSection({ tool }: ToolSectionProps) {
+ switch (tool.toolName) {
+ case 'search':
+ return <SearchSection tool={tool} />
+ case 'video_search':
+ return <VideoSearchSection tool={tool} />
+ case 'retrieve':
+ return <RetrieveSection tool={tool} />
+ default:
+ return null
+ }
+}
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
index fad388fc..2b132f2c 100644
--- a/components/video-search-section.tsx
+++ b/components/video-search-section.tsx
@@ -1,34 +1,30 @@
'use client'
import { DefaultSkeleton } from './default-skeleton'
-import { Section } from './section'
+import { Section, ToolArgsSection } from './section'
import type { SerperSearchResults } from '@/lib/types'
-import { StreamableValue, useStreamableValue } from 'ai/rsc'
+import { ToolInvocation } from 'ai'
import { VideoSearchResults } from './video-search-results'
-import { ToolBadge } from './tool-badge'
-export type VideoSearchSectionProps = {
- result?: StreamableValue<string>
+interface VideoSearchSectionProps {
+ tool: ToolInvocation
}
-export function VideoSearchSection({ result }: VideoSearchSectionProps) {
- const [data, error, pending] = useStreamableValue(result)
- const searchResults: SerperSearchResults = data ? JSON.parse(data) : undefined
+export function VideoSearchSection({ tool }: VideoSearchSectionProps) {
+ const isLoading = tool.state === 'call'
+ const searchResults: SerperSearchResults =
+ tool.state === 'result' ? tool.result : undefined
+ const query = tool.args.q as string | undefined
+
return (
<div>
- {!pending && data ? (
- <>
- <Section size="sm" className="pt-2 pb-0">
- <ToolBadge tool="search">{`${searchResults.searchParameters.q}`}</ToolBadge>
- </Section>
- <Section title="Videos">
- <VideoSearchResults results={searchResults} />
- </Section>
- </>
- ) : (
- <Section className="pt-2 pb-0">
- <DefaultSkeleton />
+ <ToolArgsSection tool="video_search">{query}</ToolArgsSection>
+ {!isLoading && searchResults ? (
+ <Section title="Videos">
+ <VideoSearchResults results={searchResults} />
</Section>
+ ) : (
+ <DefaultSkeleton />
)}
</div>
)