An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: update artifact components for AI SDK v5 beta
Yoshiki Miura7ce9d6a0860b
diff --git a/components/artifact/artifact-content.tsx b/components/artifact/artifact-content.tsxindex 4adb0738..311280f0 100644--- a/components/artifact/artifact-content.tsx+++ b/components/artifact/artifact-content.tsx@@ -1,6 +1,6 @@'use client'-import { Part } from '@/components/artifact/artifact-context'+import { Part } from '@/lib/types/ai'import { ReasoningContent } from './reasoning-content'import { ToolInvocationContent } from './tool-invocation-content'@@ -9,10 +9,13 @@ export function ArtifactContent({ part }: { part: Part | null }) {if (!part) return nullswitch (part.type) {- case 'tool-invocation':- return <ToolInvocationContent toolInvocation={part.toolInvocation} />+ case 'tool-search':+ case 'tool-retrieve':+ case 'tool-videoSearch':+ case 'tool-askQuestion':+ return <ToolInvocationContent part={part} />case 'reasoning':- return <ReasoningContent reasoning={part.reasoning} />+ return <ReasoningContent reasoning={part.text} />default: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.tsxindex 1d2b7ff2..fabee036 100644--- a/components/artifact/artifact-context.tsx+++ b/components/artifact/artifact-context.tsx@@ -9,27 +9,8 @@ import {useReducer} from 'react'-import type { ToolInvocation } from 'ai'-import { useSidebar } from '../ui/sidebar'--// 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+import type { Part } from '@/lib/types/ai'interface ArtifactState {part: Part | nulldiff --git a/components/artifact/retrieve-artifact-content.tsx b/components/artifact/retrieve-artifact-content.tsxindex 3fe2dfdb..474012ac 100644--- a/components/artifact/retrieve-artifact-content.tsx+++ b/components/artifact/retrieve-artifact-content.tsx@@ -1,7 +1,6 @@'use client'-import type { ToolInvocation } from 'ai'-+import type { ToolPart } from '@/lib/types/ai'import type {SearchResultItem,SearchResults as TypeSearchResults@@ -14,10 +13,10 @@ import { MemoizedReactMarkdown } from '../ui/markdown'const MAX_CONTENT_LENGTH = 1000-export function RetrieveArtifactContent({ tool }: { tool: ToolInvocation }) {+export function RetrieveArtifactContent({ tool }: { tool: ToolPart<'retrieve'> }) {const searchResults: TypeSearchResults | undefined =- tool.state === 'result' ? tool.result : undefined- const url = tool.args?.url as string | undefined+ tool.state === 'output-available' ? tool.output : undefined+ const url = tool.input?.urlif (!searchResults?.results) {return <div className="p-4">No retrieved content</div>diff --git a/components/artifact/search-artifact-content.tsx b/components/artifact/search-artifact-content.tsxindex d1ee1e70..8ce14cb5 100644--- a/components/artifact/search-artifact-content.tsx+++ b/components/artifact/search-artifact-content.tsx@@ -1,17 +1,16 @@'use client'-import type { ToolInvocation } from 'ai'-+import type { ToolPart } from '@/lib/types/ai'import type { SearchResults as TypeSearchResults } from '@/lib/types'import { SearchResults } from '@/components/search-results'import { SearchResultsImageSection } from '@/components/search-results-image'import { Section, ToolArgsSection } from '@/components/section'-export function SearchArtifactContent({ tool }: { tool: ToolInvocation }) {+export function SearchArtifactContent({ tool }: { tool: ToolPart<'search'> }) {const searchResults: TypeSearchResults =- tool.state === 'result' ? tool.result : undefined- const query = tool.args?.query as string | undefined+ tool.state === 'output-available' ? tool.output : undefined+ const query = tool.input?.queryif (!searchResults?.results) {return <div className="p-4">No search results</div>diff --git a/components/artifact/tool-invocation-content.tsx b/components/artifact/tool-invocation-content.tsxindex f4f52b36..4790e1db 100644--- a/components/artifact/tool-invocation-content.tsx+++ b/components/artifact/tool-invocation-content.tsx@@ -1,23 +1,23 @@'use client'-import type { ToolInvocation } from 'ai'+import type { ToolPart } from '@/lib/types/ai'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'export function ToolInvocationContent({- toolInvocation+ part}: {- toolInvocation: ToolInvocation+ part: ToolPart}) {- switch (toolInvocation.toolName) {- case 'search':- return <SearchArtifactContent tool={toolInvocation} />- case 'retrieve':- return <RetrieveArtifactContent tool={toolInvocation} />- case 'videoSearch':- return <VideoSearchArtifactContent tool={toolInvocation} />+ switch (part.type) {+ case 'tool-search':+ return <SearchArtifactContent tool={part} />+ case 'tool-retrieve':+ return <RetrieveArtifactContent tool={part} />+ case 'tool-videoSearch':+ return <VideoSearchArtifactContent tool={part} />default:return <div className="p-4">Details for this tool are not available</div>}diff --git a/components/artifact/video-search-artifact-content.tsx b/components/artifact/video-search-artifact-content.tsxindex a82beab4..6ed3f74b 100644--- a/components/artifact/video-search-artifact-content.tsx+++ b/components/artifact/video-search-artifact-content.tsx@@ -1,7 +1,6 @@'use client'-import type { ToolInvocation } from 'ai'-+import type { ToolPart } from '@/lib/types/ai'import {type SerperSearchResultItem,type SerperSearchResults@@ -10,10 +9,10 @@ import {import { ToolArgsSection } from '@/components/section'import { VideoResultGrid } from '@/components/video-result-grid'-export function VideoSearchArtifactContent({ tool }: { tool: ToolInvocation }) {+export function VideoSearchArtifactContent({ tool }: { tool: ToolPart<'videoSearch'> }) {const videoResults: SerperSearchResults | undefined =- tool.state === 'result' ? tool.result : undefined- const query = tool.args?.query as string | undefined+ tool.state === 'output-available' ? tool.output : undefined+ const query = tool.input?.queryconst videos = (videoResults?.videos || []).filter((video: SerperSearchResultItem) => {diff --git a/lib/types/ai.ts b/lib/types/ai.tsnew file mode 100644index 00000000..caa87cd3--- /dev/null+++ b/lib/types/ai.ts@@ -0,0 +1,61 @@+import type { Message } from 'ai'++export type UIMessage = Message++export type UIDataTypes = {+ sources?: any[]+}++export type UITools = {+ search: {+ input: {+ query: string+ max_results?: number+ search_depth?: 'basic' | 'advanced'+ include_domains?: string[]+ exclude_domains?: string[]+ }+ output: any+ }+ retrieve: {+ input: {+ url: string+ }+ output: any+ }+ videoSearch: {+ input: {+ query: string+ }+ output: any+ }+ askQuestion: {+ input: {+ question: string+ options: string[]+ allowMultiple?: boolean+ }+ output: any+ }+}++export type TextPart = {+ type: 'text'+ text: string+}++export type ReasoningPart = {+ type: 'reasoning'+ text: string+}++export type ToolPart<T extends keyof UITools = keyof UITools> = {+ type: `tool-${T}`+ toolCallId: string+ input: UITools[T]['input']+ output?: UITools[T]['output']+ state: 'input-streaming' | 'input-available' | 'output-available' | 'output-error'+ errorText?: string+}++export type Part = TextPart | ReasoningPart | ToolPart\ No newline at end of file