Skip to content

An AI-powered search engine with a generative UI

feat(artifact): generalize artifact panel to display message parts

Yoshiki Miurac1f97d3f9a0b
diff --git a/components/artifact/artifact-content.tsx b/components/artifact/artifact-content.tsx
index 14a4396a..ad912e20 100644
--- a/components/artifact/artifact-content.tsx
+++ b/components/artifact/artifact-content.tsx
@@ -1,25 +1,20 @@
'use client'
-import { RetrieveArtifactContent } from '@/components/artifact/retrieve-artifact-content'
-import { SearchArtifactContent } from '@/components/artifact/search-artifact-content'
-import { VideoSearchArtifactContent } from '@/components/artifact/video-search-artifact-content'
-import type { ToolInvocation } from 'ai'
+import { Part } from '@/components/artifact/artifact-context'
+import { ReasoningContent } from './reasoning-content'
+import { ToolInvocationContent } from './tool-invocation-content'
-export function ArtifactContent({
- artifact
-}: {
- artifact: ToolInvocation | null
-}) {
- if (!artifact) return null
+export function ArtifactContent({ part }: { part: Part | null }) {
+ if (!part) return null
- switch (artifact.toolName) {
- case 'search':
- return <SearchArtifactContent tool={artifact} />
- case 'retrieve':
- return <RetrieveArtifactContent tool={artifact} />
- case 'videoSearch':
- return <VideoSearchArtifactContent tool={artifact} />
+ switch (part.type) {
+ case 'tool-invocation':
+ return <ToolInvocationContent toolInvocation={part.toolInvocation} />
+ case 'reasoning':
+ return <ReasoningContent reasoning={part.reasoning} />
default:
- return <div className="p-4">Details for this tool are not available</div>
+ return (
+ <div className="p-4">Details for this part type are not available</div>
+ )
}
}
diff --git a/components/artifact/artifact-context.tsx b/components/artifact/artifact-context.tsx
index a9edf693..49aa2525 100644
--- a/components/artifact/artifact-context.tsx
+++ b/components/artifact/artifact-context.tsx
@@ -3,17 +3,33 @@
import type { ToolInvocation } from 'ai'
import { createContext, ReactNode, useContext, useReducer } from 'react'
+// Part types as seen in render-message.tsx
+export type TextPart = {
+ type: 'text'
+ text: string
+}
+
+export type ReasoningPart = {
+ type: 'reasoning'
+ reasoning: string
+}
+
+export type ToolInvocationPart = {
+ type: 'tool-invocation'
+ toolInvocation: ToolInvocation
+}
+
+export type Part = TextPart | ReasoningPart | ToolInvocationPart
+
interface ArtifactState {
- artifact: ToolInvocation | null
+ part: Part | null
isOpen: boolean
}
-type ArtifactAction =
- | { type: 'OPEN'; payload: ToolInvocation }
- | { type: 'CLOSE' }
+type ArtifactAction = { type: 'OPEN'; payload: Part } | { type: 'CLOSE' }
const initialState: ArtifactState = {
- artifact: null,
+ part: null,
isOpen: false
}
@@ -23,7 +39,7 @@ function artifactReducer(
): ArtifactState {
switch (action.type) {
case 'OPEN':
- return { artifact: action.payload, isOpen: true }
+ return { part: action.payload, isOpen: true }
case 'CLOSE':
return { ...state, isOpen: false }
default:
@@ -33,7 +49,7 @@ function artifactReducer(
interface ArtifactContextValue {
state: ArtifactState
- open: (artifact: ToolInvocation) => void
+ open: (part: Part) => void
close: () => void
}
@@ -44,8 +60,8 @@ const ArtifactContext = createContext<ArtifactContextValue | undefined>(
export function ArtifactProvider({ children }: { children: ReactNode }) {
const [state, dispatch] = useReducer(artifactReducer, initialState)
- const open = (artifact: ToolInvocation) => {
- dispatch({ type: 'OPEN', payload: artifact })
+ const open = (part: Part) => {
+ dispatch({ type: 'OPEN', payload: part })
}
const close = () => {
diff --git a/components/artifact/artifact-drawer.tsx b/components/artifact/artifact-drawer.tsx
index 88fbd329..3c09574b 100644
--- a/components/artifact/artifact-drawer.tsx
+++ b/components/artifact/artifact-drawer.tsx
@@ -1,23 +1,10 @@
'use client'
import { Drawer, DrawerContent } from '@/components/ui/drawer'
-import { useEffect, useState } from 'react'
+import { useMediaQuery } from '@/lib/hooks/use-media-query'
import { useArtifact } from './artifact-context'
import { ArtifactPanel } from './artifact-panel'
-// Hook to detect mobile screen width
-function useMediaQuery(query: string): boolean {
- const [matches, setMatches] = useState(false)
- useEffect(() => {
- const mql = window.matchMedia(query)
- const handler = (e: MediaQueryListEvent) => setMatches(e.matches)
- setMatches(mql.matches)
- mql.addEventListener('change', handler)
- return () => mql.removeEventListener('change', handler)
- }, [query])
- return matches
-}
-
export function ArtifactDrawer() {
const { state, close } = useArtifact()
const isMobile = useMediaQuery('(max-width: 767px)')
diff --git a/components/artifact/artifact-panel.tsx b/components/artifact/artifact-panel.tsx
index 4fd4bdde..3f63eead 100644
--- a/components/artifact/artifact-panel.tsx
+++ b/components/artifact/artifact-panel.tsx
@@ -8,14 +8,42 @@ import {
TooltipProvider,
TooltipTrigger
} from '@/components/ui/tooltip'
-import { Minimize2, Wrench } from 'lucide-react'
+import { LightbulbIcon, MessageSquare, Minimize2, Wrench } from 'lucide-react'
import { ArtifactContent } from './artifact-content'
import { useArtifact } from './artifact-context'
export function ArtifactPanel() {
const { state, close } = useArtifact()
- const artifact = state.artifact
- if (!artifact) return null
+ const part = state.part
+ if (!part) return null
+
+ // Get the icon and title based on part type
+ const getIconAndTitle = () => {
+ switch (part.type) {
+ case 'tool-invocation':
+ return {
+ icon: <Wrench size={18} />,
+ title: part.toolInvocation.toolName
+ }
+ case 'reasoning':
+ return {
+ icon: <LightbulbIcon size={18} />,
+ title: 'Reasoning'
+ }
+ case 'text':
+ return {
+ icon: <MessageSquare size={18} />,
+ title: 'Text'
+ }
+ default:
+ return {
+ icon: <MessageSquare size={18} />,
+ title: 'Content'
+ }
+ }
+ }
+
+ const { icon, title } = getIconAndTitle()
return (
<TooltipProvider>
@@ -24,11 +52,9 @@ export function ArtifactPanel() {
<div className="flex items-center justify-between px-4 py-2">
<h3 className="flex items-center gap-2">
<div className="bg-muted p-2 rounded-md flex items-center gap-2">
- <Wrench size={18} />
+ {icon}
</div>
- <span className="text-sm font-medium capitalize">
- {artifact.toolName}
- </span>
+ <span className="text-sm font-medium capitalize">{title}</span>
</h3>
<Tooltip>
<TooltipTrigger asChild>
@@ -48,7 +74,7 @@ export function ArtifactPanel() {
</div>
<Separator className="my-1 bg-border/50" />
<div data-vaul-no-drag className="flex-1 overflow-y-auto p-4">
- <ArtifactContent artifact={artifact} />
+ <ArtifactContent part={part} />
</div>
</div>
</div>
diff --git a/components/artifact/reasoning-content.tsx b/components/artifact/reasoning-content.tsx
new file mode 100644
index 00000000..f2f4fe56
--- /dev/null
+++ b/components/artifact/reasoning-content.tsx
@@ -0,0 +1,18 @@
+'use client'
+
+import { cn } from '@/lib/utils'
+import remarkGfm from 'remark-gfm'
+import { MemoizedReactMarkdown } from '../ui/markdown'
+
+export function ReasoningContent({ reasoning }: { reasoning: string }) {
+ return (
+ <div className="p-4 overflow-auto">
+ <h3 className="text-lg font-semibold mb-2">Reasoning</h3>
+ <div className={cn('prose prose-sm dark:prose-invert max-w-none')}>
+ <MemoizedReactMarkdown remarkPlugins={[remarkGfm]}>
+ {reasoning}
+ </MemoizedReactMarkdown>
+ </div>
+ </div>
+ )
+}
diff --git a/components/artifact/tool-invocation-content.tsx b/components/artifact/tool-invocation-content.tsx
new file mode 100644
index 00000000..25d1550c
--- /dev/null
+++ b/components/artifact/tool-invocation-content.tsx
@@ -0,0 +1,23 @@
+'use client'
+
+import { RetrieveArtifactContent } from '@/components/artifact/retrieve-artifact-content'
+import { SearchArtifactContent } from '@/components/artifact/search-artifact-content'
+import { VideoSearchArtifactContent } from '@/components/artifact/video-search-artifact-content'
+import type { ToolInvocation } from 'ai'
+
+export function ToolInvocationContent({
+ toolInvocation
+}: {
+ toolInvocation: ToolInvocation
+}) {
+ switch (toolInvocation.toolName) {
+ case 'search':
+ return <SearchArtifactContent tool={toolInvocation} />
+ case 'retrieve':
+ return <RetrieveArtifactContent tool={toolInvocation} />
+ case 'videoSearch':
+ return <VideoSearchArtifactContent tool={toolInvocation} />
+ default:
+ return <div className="p-4">Details for this tool are not available</div>
+ }
+}
diff --git a/components/reasoning-section.tsx b/components/reasoning-section.tsx
index 1a894bb8..3471a3d8 100644
--- a/components/reasoning-section.tsx
+++ b/components/reasoning-section.tsx
@@ -61,6 +61,7 @@ export function ReasoningSection({
isOpen={isOpen}
onOpenChange={onOpenChange}
showBorder={true}
+ showIcon={false}
>
<BotMessage
message={content.reasoning}
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index b7415278..c3c54488 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -28,7 +28,7 @@ export function RetrieveSection({
const header = (
<button
type="button"
- onClick={() => open(tool)}
+ onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
title="Open details"
>
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 4c125ab1..b4aac733 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -40,7 +40,7 @@ export function SearchSection({
const header = (
<button
type="button"
- onClick={() => open(tool)}
+ onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
title="Open details"
>
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
index e19481d0..c016fb1d 100644
--- a/components/video-search-section.tsx
+++ b/components/video-search-section.tsx
@@ -35,7 +35,7 @@ export function VideoSearchSection({
const header = (
<button
type="button"
- onClick={() => open(tool)}
+ onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
title="Open details"
>