Skip to content

An AI-powered search engine with a generative UI

refactor: update section components for AI SDK v5 beta

Yoshiki Miura78c8b640de3a
diff --git a/components/question-confirmation.tsx b/components/question-confirmation.tsx
index 0a327996..a678796b 100644
--- a/components/question-confirmation.tsx
+++ b/components/question-confirmation.tsx
@@ -2,8 +2,8 @@
import { useState } from 'react'
-import { ToolInvocation } from 'ai'
import { ArrowRight, Check, SkipForward } from 'lucide-react'
+import type { ToolPart } from '@/lib/types/ai'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
@@ -11,7 +11,7 @@ import { Checkbox } from '@/components/ui/checkbox'
import { Input } from '@/components/ui/input'
interface QuestionConfirmationProps {
- toolInvocation: ToolInvocation
+ toolInvocation: ToolPart<'askQuestion'>
onConfirm: (toolCallId: string, approved: boolean, response?: any) => void
isCompleted?: boolean
}
@@ -27,12 +27,12 @@ export function QuestionConfirmation({
isCompleted = false
}: QuestionConfirmationProps) {
const { question, options, allowsInput, inputLabel, inputPlaceholder } =
- toolInvocation.args
+ toolInvocation.input || {}
// Get result data if available
const resultData =
- toolInvocation.state === 'result' && toolInvocation.result
- ? toolInvocation.result
+ toolInvocation.state === 'output-available' && toolInvocation.output
+ ? toolInvocation.output
: null
const [selectedOptions, setSelectedOptions] = useState<string[]>([])
@@ -119,7 +119,7 @@ export function QuestionConfirmation({
}
// Show result view if completed or if tool has result state
- if (completed || toolInvocation.state === 'result') {
+ if (completed || toolInvocation.state === 'output-available') {
const isSkipped = wasSkipped()
return (
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index 43a17a87..7f138d2f 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -3,8 +3,8 @@
import React from 'react'
import { UseChatHelpers } from '@ai-sdk/react'
-import { ToolInvocation } from 'ai'
import { ArrowRight } from 'lucide-react'
+import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { Related } from '@/lib/schema/related'
@@ -14,11 +14,11 @@ import { CollapsibleMessage } from './collapsible-message'
import { Section } from './section'
export interface RelatedQuestionsProps {
- tool: ToolInvocation
+ tool: ToolPart
onQuerySelect: (query: string) => void
isOpen: boolean
onOpenChange: (open: boolean) => void
- status?: UseChatHelpers['status']
+ status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
}
export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
@@ -29,10 +29,10 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
status
}) => {
const isLoading =
- status === 'submitted' || status === 'streaming' || tool.state === 'call'
+ status === 'submitted' || status === 'streaming' || tool.state === 'input-streaming' || tool.state === 'input-available'
const data: Related | undefined =
- tool.state === 'result' ? tool.result : undefined
+ tool.state === 'output-available' ? tool.output : undefined
if (!data && isLoading) {
return (
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index ed467fa8..2d8331b8 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -1,7 +1,7 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import { ToolInvocation } from 'ai'
+import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { SearchResults as SearchResultsType } from '@/lib/types'
@@ -13,10 +13,10 @@ import { CollapsibleMessage } from './collapsible-message'
import { DefaultSkeleton } from './default-skeleton'
interface RetrieveSectionProps {
- tool: ToolInvocation
+ tool: ToolPart<'retrieve'>
isOpen: boolean
onOpenChange: (open: boolean) => void
- status?: UseChatHelpers['status']
+ status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
}
export function RetrieveSection({
@@ -25,19 +25,19 @@ export function RetrieveSection({
onOpenChange,
status
}: RetrieveSectionProps) {
- const isToolLoading = tool.state === 'call'
+ const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
const isChatLoading = status === 'submitted' || status === 'streaming'
const isLoading = isToolLoading || isChatLoading
const data: SearchResultsType =
- 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?.url
const { open } = useArtifact()
const header = (
<button
type="button"
- onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
+ onClick={() => open(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 fc43e653..7f35fb0d 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -1,7 +1,7 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import { ToolInvocation } from 'ai'
+import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
@@ -14,10 +14,10 @@ import { SearchResultsImageSection } from './search-results-image'
import { Section, ToolArgsSection } from './section'
interface SearchSectionProps {
- tool: ToolInvocation
+ tool: ToolPart<'search'>
isOpen: boolean
onOpenChange: (open: boolean) => void
- status?: UseChatHelpers['status']
+ status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
}
export function SearchSection({
@@ -28,11 +28,11 @@ export function SearchSection({
}: SearchSectionProps) {
const isLoading = status === 'submitted' || status === 'streaming'
- const isToolLoading = tool.state === 'call'
+ const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
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
+ tool.state === 'output-available' ? tool.output : undefined
+ const query = tool.input?.query
+ const includeDomains = tool.input?.include_domains
const includeDomainsString = includeDomains
? ` [${includeDomains.join(', ')}]`
: ''
@@ -41,7 +41,7 @@ export function SearchSection({
const header = (
<button
type="button"
- onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
+ onClick={() => open(tool)}
className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
title="Open details"
>
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index a829de46..27bb72b9 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -1,7 +1,7 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import { ToolInvocation } from 'ai'
+import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { QuestionConfirmation } from './question-confirmation'
import { RelatedQuestions } from './related-questions'
@@ -10,10 +10,10 @@ import { SearchSection } from './search-section'
import { VideoSearchSection } from './video-search-section'
interface ToolSectionProps {
- tool: ToolInvocation
+ tool: ToolPart
isOpen: boolean
onOpenChange: (open: boolean) => void
- status?: UseChatHelpers['status']
+ status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
addToolResult?: (params: { toolCallId: string; result: any }) => void
onQuerySelect: (query: string) => void
}
@@ -27,12 +27,12 @@ export function ToolSection({
onQuerySelect
}: ToolSectionProps) {
// Special handling for ask_question tool
- if (tool.toolName === 'ask_question') {
+ if (tool.type === 'tool-askQuestion') {
// When waiting for user input
- if (tool.state === 'call' && addToolResult) {
+ if ((tool.state === 'input-streaming' || tool.state === 'input-available') && addToolResult) {
return (
<QuestionConfirmation
- toolInvocation={tool}
+ toolInvocation={tool as ToolPart<'askQuestion'>}
onConfirm={(toolCallId, approved, response) => {
addToolResult({
toolCallId,
@@ -50,10 +50,10 @@ export function ToolSection({
}
// When result is available, display the result
- if (tool.state === 'result') {
+ if (tool.state === 'output-available') {
return (
<QuestionConfirmation
- toolInvocation={tool}
+ toolInvocation={tool as ToolPart<'askQuestion'>}
isCompleted={true}
onConfirm={() => {}} // Not used in result display mode
/>
@@ -61,35 +61,35 @@ export function ToolSection({
}
}
- switch (tool.toolName) {
- case 'search':
+ switch (tool.type) {
+ case 'tool-search':
return (
<SearchSection
- tool={tool}
+ tool={tool as ToolPart<'search'>}
isOpen={isOpen}
onOpenChange={onOpenChange}
status={status}
/>
)
- case 'videoSearch':
+ case 'tool-videoSearch':
return (
<VideoSearchSection
- tool={tool}
+ tool={tool as ToolPart<'videoSearch'>}
isOpen={isOpen}
onOpenChange={onOpenChange}
status={status}
/>
)
- case 'retrieve':
+ case 'tool-retrieve':
return (
<RetrieveSection
- tool={tool}
+ tool={tool as ToolPart<'retrieve'>}
isOpen={isOpen}
onOpenChange={onOpenChange}
status={status}
/>
)
- case 'related_questions':
+ case 'tool-relatedQuestions':
return (
<RelatedQuestions
tool={tool}
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
index 424438ff..74c4878d 100644
--- a/components/video-search-section.tsx
+++ b/components/video-search-section.tsx
@@ -1,7 +1,7 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import { ToolInvocation } from 'ai'
+import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import type { SerperSearchResults } from '@/lib/types'
@@ -13,10 +13,10 @@ import { Section, ToolArgsSection } from './section'
import { VideoSearchResults } from './video-search-results'
interface VideoSearchSectionProps {
- tool: ToolInvocation
+ tool: ToolPart<'videoSearch'>
isOpen: boolean
onOpenChange: (open: boolean) => void
- status?: UseChatHelpers['status']
+ status?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']
}
export function VideoSearchSection({
@@ -27,16 +27,16 @@ export function VideoSearchSection({
}: VideoSearchSectionProps) {
const isLoading = status === 'submitted' || status === 'streaming'
- const isToolLoading = tool.state === 'call'
+ const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
const videoResults: SerperSearchResults =
- 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?.query
const { open } = useArtifact()
const header = (
<button
type="button"
- onClick={() => open({ type: 'tool-invocation', toolInvocation: tool })}
+ onClick={() => open(tool)}
className="flex items-center justify-between w-full text-left rounded-md p-1 -ml-1"
title="Open details"
>