Skip to content

An AI-powered search engine with a generative UI

feat: Implement retrieve artifact display

Yoshiki Miuraaa7b75ceab6a
diff --git a/components/artifact/artifact-content.tsx b/components/artifact/artifact-content.tsx
index 073e87d0..3871afce 100644
--- a/components/artifact/artifact-content.tsx
+++ b/components/artifact/artifact-content.tsx
@@ -1,5 +1,6 @@
'use client'
+import { RetrieveArtifactContent } from '@/components/artifact/retrieve-artifact-content'
import { SearchArtifactContent } from '@/components/artifact/search-artifact-content'
import type { ToolInvocation } from 'ai'
@@ -13,6 +14,8 @@ export function ArtifactContent({
switch (artifact.toolName) {
case 'search':
return <SearchArtifactContent tool={artifact} />
+ case 'retrieve':
+ return <RetrieveArtifactContent tool={artifact} />
default:
return <div className="p-4">Details for this tool are not available</div>
}
diff --git a/components/artifact/retrieve-artifact-content.tsx b/components/artifact/retrieve-artifact-content.tsx
new file mode 100644
index 00000000..856efdc3
--- /dev/null
+++ b/components/artifact/retrieve-artifact-content.tsx
@@ -0,0 +1,49 @@
+'use client'
+
+import { SearchResults } from '@/components/search-results'
+import { Section, ToolArgsSection } from '@/components/section'
+import type {
+ SearchResultItem,
+ SearchResults as TypeSearchResults
+} from '@/lib/types/index'
+import type { ToolInvocation } from 'ai'
+import { MemoizedReactMarkdown } from '../ui/markdown'
+
+const MAX_CONTENT_LENGTH = 1000
+
+export function RetrieveArtifactContent({ tool }: { tool: ToolInvocation }) {
+ const searchResults: TypeSearchResults | undefined =
+ tool.state === 'result' ? tool.result : undefined
+ const url = tool.args?.url as string | undefined
+
+ if (!searchResults?.results) {
+ return <div className="p-4">No retrieved content</div>
+ }
+
+ const truncatedResults: SearchResultItem[] = searchResults.results.map(
+ result => ({
+ ...result,
+ content:
+ result.content.length > MAX_CONTENT_LENGTH
+ ? `${result.content.substring(0, MAX_CONTENT_LENGTH)}...`
+ : result.content
+ })
+ )
+
+ return (
+ <div className="space-y-2">
+ <ToolArgsSection tool="retrieve">{url}</ToolArgsSection>
+
+ <Section title="Sources">
+ <SearchResults results={truncatedResults} displayMode="list" />
+ </Section>
+ {truncatedResults[0].content && (
+ <Section title="Content">
+ <MemoizedReactMarkdown className="prose-sm prose-neutral prose-p:text-sm text-muted-foreground">
+ {truncatedResults[0].content}
+ </MemoizedReactMarkdown>
+ </Section>
+ )}
+ </div>
+ )
+}
diff --git a/components/collapsible-message.tsx b/components/collapsible-message.tsx
index bb7cad99..3894533e 100644
--- a/components/collapsible-message.tsx
+++ b/components/collapsible-message.tsx
@@ -29,13 +29,13 @@ export function CollapsibleMessage({
showBorder = true,
showIcon = true
}: CollapsibleMessageProps) {
- const content = <div className="py-2 flex-1">{children}</div>
+ const content = <div className="flex-1">{children}</div>
return (
<div className="flex">
{showIcon && (
<div className="relative flex flex-col items-center">
- <div className={cn('mt-[10px] w-5', role === 'assistant' && 'mt-4')}>
+ <div className={cn('mt-[2px] w-5', role === 'assistant' && 'mt-4')}>
{role === 'user' ? (
<UserCircle2 size={20} className="text-muted-foreground" />
) : (
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index 0d0faeb3..b7415278 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -1,5 +1,6 @@
'use client'
+import { useArtifact } from '@/components/artifact/artifact-context'
import { SearchResults } from '@/components/search-results'
import { Section, ToolArgsSection } from '@/components/section'
import { SearchResults as SearchResultsType } from '@/lib/types'
@@ -23,7 +24,19 @@ export function RetrieveSection({
tool.state === 'result' ? tool.result : undefined
const url = tool.args.url as string | undefined
- const header = <ToolArgsSection tool="retrieve">{url}</ToolArgsSection>
+ const { open } = useArtifact()
+ const header = (
+ <button
+ type="button"
+ onClick={() => open(tool)}
+ className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
+ title="Open details"
+ >
+ <ToolArgsSection tool="retrieve" number={data?.results?.length}>
+ {url}
+ </ToolArgsSection>
+ </button>
+ )
return (
<CollapsibleMessage
diff --git a/components/section.tsx b/components/section.tsx
index ed1cc5aa..736a375c 100644
--- a/components/section.tsx
+++ b/components/section.tsx
@@ -4,6 +4,7 @@ import { cn } from '@/lib/utils'
import {
BookCheck,
Check,
+ File,
Film,
Image,
MessageCircleMore,
@@ -57,6 +58,10 @@ export const Section: React.FC<SectionProps> = ({
case 'Follow-up':
icon = <MessageCircleMore size={iconSize} className={iconClassName} />
break
+ case 'Content':
+ icon = <File size={iconSize} className={iconClassName} />
+ type = 'badge'
+ break
default:
icon = <Search size={iconSize} className={iconClassName} />
}