Skip to content

An AI-powered search engine with a generative UI

fix: update to transport-based useChat and upgrade zod

Yoshiki Miura6326939c69d9
diff --git a/bun.lock b/bun.lock
index bc07e88a..bd574a00 100644
--- a/bun.lock
+++ b/bun.lock
@@ -68,7 +68,7 @@
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "zod": "3.25.0",
+ "zod": "^4.0.5",
},
"devDependencies": {
"@testing-library/jest-dom": "^6.6.3",
@@ -1789,7 +1789,7 @@
"yocto-queue": ["[email protected]", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
- "zod": ["[email protected]", "", {}, "sha512-ficnZKUW0mlNivqeJkosTEkGbJ6NKCtSaOHGx5aXbtfeWMdRyzXLbAIn19my4C/KB7WPY/p9vlGPt+qpOp6c4Q=="],
+ "zod": ["[email protected]", "", {}, "sha512-/5UuuRPStvHXu7RS+gmvRf4NXrNxpSllGwDnCBcJZtQsKrviYXm54yDGV2KYNLT5kq0lHGcl7lqWJLgSaG+tgA=="],
"zod-to-json-schema": ["[email protected]", "", { "peerDependencies": { "zod": "^3.24.1" } }, "sha512-/AuWwMP+YqiPbsJx5D6TfgRTc4kTLjsh5SOcd4bLsfUg2RcEXrFMJl1DGgdHy2aCfsIA/cr/1JM0xcB2GZji8g=="],
diff --git a/components/answer-section.tsx b/components/answer-section.tsx
index d077649f..0bd4f2f2 100644
--- a/components/answer-section.tsx
+++ b/components/answer-section.tsx
@@ -1,9 +1,10 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { ChatRequestOptions } from 'ai'
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
+
import { CollapsibleMessage } from './collapsible-message'
import { DefaultSkeleton } from './default-skeleton'
import { BotMessage } from './message'
diff --git a/components/artifact/artifact-context.tsx b/components/artifact/artifact-context.tsx
index fabee036..09b9e8f1 100644
--- a/components/artifact/artifact-context.tsx
+++ b/components/artifact/artifact-context.tsx
@@ -9,9 +9,10 @@ import {
useReducer
} from 'react'
-import { useSidebar } from '../ui/sidebar'
import type { Part } from '@/lib/types/ai'
+import { useSidebar } from '../ui/sidebar'
+
interface ArtifactState {
part: Part | null
isOpen: boolean
diff --git a/components/artifact/retrieve-artifact-content.tsx b/components/artifact/retrieve-artifact-content.tsx
index ec05bbfe..961925af 100644
--- a/components/artifact/retrieve-artifact-content.tsx
+++ b/components/artifact/retrieve-artifact-content.tsx
@@ -13,7 +13,11 @@ import { MemoizedReactMarkdown } from '../ui/markdown'
const MAX_CONTENT_LENGTH = 1000
-export function RetrieveArtifactContent({ tool }: { tool: ToolPart<'retrieve'> }) {
+export function RetrieveArtifactContent({
+ tool
+}: {
+ tool: ToolPart<'retrieve'>
+}) {
const searchResults: TypeSearchResults | undefined =
tool.state === 'output-available' ? (tool.output ?? undefined) : undefined
const url = tool.input?.url
diff --git a/components/artifact/search-artifact-content.tsx b/components/artifact/search-artifact-content.tsx
index fb049f8d..bfdd46fa 100644
--- a/components/artifact/search-artifact-content.tsx
+++ b/components/artifact/search-artifact-content.tsx
@@ -1,7 +1,7 @@
'use client'
-import type { ToolPart } from '@/lib/types/ai'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
+import type { ToolPart } from '@/lib/types/ai'
import { SearchResults } from '@/components/search-results'
import { SearchResultsImageSection } from '@/components/search-results-image'
diff --git a/components/artifact/tool-invocation-content.tsx b/components/artifact/tool-invocation-content.tsx
index c2372d82..0fab2439 100644
--- a/components/artifact/tool-invocation-content.tsx
+++ b/components/artifact/tool-invocation-content.tsx
@@ -6,18 +6,16 @@ import { RetrieveArtifactContent } from '@/components/artifact/retrieve-artifact
import { SearchArtifactContent } from '@/components/artifact/search-artifact-content'
import { VideoSearchArtifactContent } from '@/components/artifact/video-search-artifact-content'
-export function ToolInvocationContent({
- part
-}: {
- part: ToolPart
-}) {
+export function ToolInvocationContent({ part }: { part: ToolPart }) {
switch (part.type) {
case 'tool-search':
return <SearchArtifactContent tool={part as ToolPart<'search'>} />
case 'tool-retrieve':
return <RetrieveArtifactContent tool={part as ToolPart<'retrieve'>} />
case 'tool-videoSearch':
- return <VideoSearchArtifactContent tool={part as ToolPart<'videoSearch'>} />
+ return (
+ <VideoSearchArtifactContent tool={part as ToolPart<'videoSearch'>} />
+ )
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.tsx
index 6ed3f74b..ba492133 100644
--- a/components/artifact/video-search-artifact-content.tsx
+++ b/components/artifact/video-search-artifact-content.tsx
@@ -1,15 +1,19 @@
'use client'
-import type { ToolPart } from '@/lib/types/ai'
import {
type SerperSearchResultItem,
type SerperSearchResults
} from '@/lib/types'
+import type { ToolPart } from '@/lib/types/ai'
import { ToolArgsSection } from '@/components/section'
import { VideoResultGrid } from '@/components/video-result-grid'
-export function VideoSearchArtifactContent({ tool }: { tool: ToolPart<'videoSearch'> }) {
+export function VideoSearchArtifactContent({
+ tool
+}: {
+ tool: ToolPart<'videoSearch'>
+}) {
const videoResults: SerperSearchResults | undefined =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index 4469726e..fcdb07bc 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -3,8 +3,8 @@
import { useState } from 'react'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { cn } from '@/lib/utils'
import { DefaultSkeleton } from './default-skeleton'
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 0763b762..ba5a259d 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -5,11 +5,11 @@ import Textarea from 'react-textarea-autosize'
import { useRouter } from 'next/navigation'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { ArrowUp, ChevronDown, MessageCirclePlus, Square } from 'lucide-react'
import { toast } from 'sonner'
import { UploadedFile } from '@/lib/types'
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { Model } from '@/lib/types/models'
import { cn } from '@/lib/utils'
@@ -95,10 +95,11 @@ export function ChatPanel({
return (
(lastPart?.type === 'tool-search' ||
- lastPart?.type === 'tool-retrieve' ||
- lastPart?.type === 'tool-videoSearch' ||
- lastPart?.type === 'tool-askQuestion') &&
- ((lastPart as any)?.state === 'input-streaming' || (lastPart as any)?.state === 'input-available')
+ lastPart?.type === 'tool-retrieve' ||
+ lastPart?.type === 'tool-videoSearch' ||
+ lastPart?.type === 'tool-askQuestion') &&
+ ((lastPart as any)?.state === 'input-streaming' ||
+ (lastPart as any)?.state === 'input-available')
)
}
diff --git a/components/chat.tsx b/components/chat.tsx
index b0ee6331..abcc92f6 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -3,12 +3,13 @@
import { useEffect, useMemo, useRef, useState } from 'react'
import { useChat } from '@ai-sdk/react'
+import { DefaultChatTransport } from 'ai'
import { toast } from 'sonner'
import { z } from 'zod'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { deleteTrailingMessages } from '@/lib/actions/chat-db'
import { UploadedFile } from '@/lib/types'
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { Model } from '@/lib/types/models'
import { cn, generateUUID } from '@/lib/utils'
@@ -50,11 +51,13 @@ export function Chat({
regenerate,
addToolResult
} = useChat({
- api: '/api/chat',
- body: {
- chatId: id
- },
- initialMessages: savedMessages,
+ transport: new DefaultChatTransport({
+ api: '/api/chat',
+ body: {
+ chatId: id
+ }
+ }),
+ messages: savedMessages,
onFinish: () => {
window.history.replaceState({}, '', `/search/${id}`)
window.dispatchEvent(new CustomEvent('chat-history-updated'))
@@ -73,7 +76,10 @@ export function Chat({
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (input.trim()) {
- sendMessage({ role: 'user' as const, parts: [{ type: 'text', text: input }] })
+ sendMessage({
+ role: 'user' as const,
+ parts: [{ type: 'text', text: input }]
+ })
setInput('')
}
}
@@ -283,11 +289,11 @@ export function Chat({
if (input.trim() || uploaded.length > 0) {
const parts: any[] = []
-
+
if (input.trim()) {
parts.push({ type: 'text', text: input })
}
-
+
uploaded.forEach(f => {
parts.push({
type: 'file',
@@ -327,7 +333,13 @@ export function Chat({
onQuerySelect={onQuerySelect}
status={status}
chatId={id}
- addToolResult={({ toolCallId, result }: { toolCallId: string; result: any }) => {
+ addToolResult={({
+ toolCallId,
+ result
+ }: {
+ toolCallId: string
+ result: any
+ }) => {
addToolResult({ toolCallId, output: result })
}}
scrollContainerRef={scrollContainerRef}
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
index 50e7f301..4f3a1050 100644
--- a/components/message-actions.tsx
+++ b/components/message-actions.tsx
@@ -1,10 +1,10 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { Copy } from 'lucide-react'
import { toast } from 'sonner'
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { cn } from '@/lib/utils'
import { Button } from './ui/button'
diff --git a/components/question-confirmation.tsx b/components/question-confirmation.tsx
index 800ec304..112cbe3a 100644
--- a/components/question-confirmation.tsx
+++ b/components/question-confirmation.tsx
@@ -3,6 +3,7 @@
import { useState } from 'react'
import { ArrowRight, Check, SkipForward } from 'lucide-react'
+
import type { ToolPart } from '@/lib/types/ai'
import { Button } from '@/components/ui/button'
@@ -41,7 +42,13 @@ export function QuestionConfirmation({
isCompleted = false
}: QuestionConfirmationProps) {
const input = (toolInvocation.input || {}) as QuestionInput
- const { question = '', options = [], allowsInput = false, inputLabel = '', inputPlaceholder = '' } = input
+ const {
+ question = '',
+ options = [],
+ allowsInput = false,
+ inputLabel = '',
+ inputPlaceholder = ''
+ } = input
// Get result data if available
const resultData =
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index 7f138d2f..264ee74f 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -4,9 +4,9 @@ import React from 'react'
import { UseChatHelpers } from '@ai-sdk/react'
import { ArrowRight } from 'lucide-react'
-import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { Related } from '@/lib/schema/related'
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { Button } from './ui/button'
import { Skeleton } from './ui/skeleton'
@@ -29,7 +29,10 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
status
}) => {
const isLoading =
- status === 'submitted' || status === 'streaming' || tool.state === 'input-streaming' || tool.state === 'input-available'
+ status === 'submitted' ||
+ status === 'streaming' ||
+ tool.state === 'input-streaming' ||
+ tool.state === 'input-available'
const data: Related | undefined =
tool.state === 'output-available' ? tool.output : undefined
diff --git a/components/render-message.tsx b/components/render-message.tsx
index d03b69db..1f34a1fb 100644
--- a/components/render-message.tsx
+++ b/components/render-message.tsx
@@ -1,5 +1,6 @@
import { UseChatHelpers } from '@ai-sdk/react'
-import type { UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
+
+import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { AnswerSection } from './answer-section'
import { ReasoningSection } from './reasoning-section'
@@ -88,14 +89,8 @@ export function RenderMessage({
<ToolSection
key={`${messageId}-tool-${index}`}
tool={part as any}
- isOpen={getIsOpen(
- part.toolCallId,
- part.type,
- hasNextPart
- )}
- onOpenChange={open =>
- onOpenChange(part.toolCallId, open)
- }
+ isOpen={getIsOpen(part.toolCallId, part.type, hasNextPart)}
+ onOpenChange={open => onOpenChange(part.toolCallId, open)}
addToolResult={addToolResult}
status={status}
onQuerySelect={onQuerySelect}
diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsx
index f601231f..8fb7bf17 100644
--- a/components/retrieve-section.tsx
+++ b/components/retrieve-section.tsx
@@ -1,9 +1,9 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import { SearchResults as SearchResultsType } from '@/lib/types'
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { useArtifact } from '@/components/artifact/artifact-context'
import { SearchResults } from '@/components/search-results'
@@ -25,12 +25,13 @@ export function RetrieveSection({
onOpenChange,
status
}: RetrieveSectionProps) {
- const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
+ const isToolLoading =
+ tool.state === 'input-streaming' || tool.state === 'input-available'
const isChatLoading = status === 'submitted' || status === 'streaming'
const isLoading = isToolLoading || isChatLoading
const data: SearchResultsType | undefined =
- tool.state === 'output-available' ? (tool.output || undefined) : undefined
+ tool.state === 'output-available' ? tool.output || undefined : undefined
const url = tool.input?.url
const { open } = useArtifact()
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 36a99da4..8b7e06da 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -1,9 +1,9 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { useArtifact } from '@/components/artifact/artifact-context'
@@ -28,7 +28,8 @@ export function SearchSection({
}: SearchSectionProps) {
const isLoading = status === 'submitted' || status === 'streaming'
- const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
+ const isToolLoading =
+ tool.state === 'input-streaming' || tool.state === 'input-available'
const searchResults: TypeSearchResults | undefined =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index de4530cf..e7ed53e3 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -1,7 +1,8 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
+
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { QuestionConfirmation } from './question-confirmation'
import { RelatedQuestions } from './related-questions'
@@ -29,7 +30,10 @@ export function ToolSection({
// Special handling for ask_question tool
if (tool.type === 'tool-askQuestion') {
// When waiting for user input
- if ((tool.state === 'input-streaming' || tool.state === 'input-available') && addToolResult) {
+ if (
+ (tool.state === 'input-streaming' || tool.state === 'input-available') &&
+ addToolResult
+ ) {
return (
<QuestionConfirmation
toolInvocation={tool as ToolPart<'askQuestion'>}
diff --git a/components/video-search-section.tsx b/components/video-search-section.tsx
index 74c4878d..4e7fc8b4 100644
--- a/components/video-search-section.tsx
+++ b/components/video-search-section.tsx
@@ -1,9 +1,9 @@
'use client'
import { UseChatHelpers } from '@ai-sdk/react'
-import type { ToolPart, UIMessage, UIDataTypes, UITools } from '@/lib/types/ai'
import type { SerperSearchResults } from '@/lib/types'
+import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'
import { useArtifact } from '@/components/artifact/artifact-context'
@@ -27,7 +27,8 @@ export function VideoSearchSection({
}: VideoSearchSectionProps) {
const isLoading = status === 'submitted' || status === 'streaming'
- const isToolLoading = tool.state === 'input-streaming' || tool.state === 'input-available'
+ const isToolLoading =
+ tool.state === 'input-streaming' || tool.state === 'input-available'
const videoResults: SerperSearchResults =
tool.state === 'output-available' ? tool.output : undefined
const query = tool.input?.query
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 5ffbe151..99339de7 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -53,7 +53,7 @@ export async function createChatStreamResponse(
}
const previousMessages = await getChatMessages(chatId)
- const messagesToModel = [...previousMessages as any[], message]
+ const messagesToModel = [...(previousMessages as any[]), message]
const result = researcher({
messages: convertToModelMessages(messagesToModel),
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 69be6e68..61603d75 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -110,17 +110,20 @@ export async function search(
includeDomains: string[] = [],
excludeDomains: string[] = []
): Promise<SearchResults> {
- return searchTool.execute?.(
- {
- query,
- max_results: maxResults,
- search_depth: searchDepth,
- include_domains: includeDomains as any,
- exclude_domains: excludeDomains as any
- },
- {
- toolCallId: 'search',
- messages: []
- }
- ) ?? Promise.resolve({ results: [], images: [], query, number_of_results: 0 })
+ return (
+ searchTool.execute?.(
+ {
+ query,
+ max_results: maxResults,
+ search_depth: searchDepth,
+ include_domains: includeDomains as any,
+ exclude_domains: excludeDomains as any
+ },
+ {
+ toolCallId: 'search',
+ messages: []
+ }
+ ) ??
+ Promise.resolve({ results: [], images: [], query, number_of_results: 0 })
+ )
}
diff --git a/lib/types/ai.ts b/lib/types/ai.ts
index d7cd0299..37f37fcc 100644
--- a/lib/types/ai.ts
+++ b/lib/types/ai.ts
@@ -1,10 +1,15 @@
-import type { UIMessage as AIMessage, InferUITool } from 'ai'
-import { searchTool } from '@/lib/tools/search'
+import type { InferUITool, UIMessage as AIMessage } from 'ai'
+
+import { askQuestionTool } from '@/lib/tools/question'
import { retrieveTool } from '@/lib/tools/retrieve'
+import { searchTool } from '@/lib/tools/search'
import { videoSearchTool } from '@/lib/tools/video-search'
-import { askQuestionTool } from '@/lib/tools/question'
-export type UIMessage<TMetadata = unknown, TDataTypes = UIDataTypes, TTools = UITools> = AIMessage
+export type UIMessage<
+ TMetadata = unknown,
+ TDataTypes = UIDataTypes,
+ TTools = UITools
+> = AIMessage
export type UIDataTypes = {
sources?: any[]
@@ -32,8 +37,12 @@ export type ToolPart<T extends keyof UITools = keyof UITools> = {
toolCallId: string
input: UITools[T]['input']
output?: UITools[T]['output']
- state: 'input-streaming' | 'input-available' | 'output-available' | 'output-error'
+ state:
+ | 'input-streaming'
+ | 'input-available'
+ | 'output-available'
+ | 'output-error'
errorText?: string
}
-export type Part = TextPart | ReasoningPart | ToolPart
\ No newline at end of file
+export type Part = TextPart | ReasoningPart | ToolPart
diff --git a/package.json b/package.json
index 002c422c..6dc7fa6c 100644
--- a/package.json
+++ b/package.json
@@ -80,7 +80,7 @@
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"vaul": "^1.1.2",
- "zod": "3.25.0"
+ "zod": "^4.0.5"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.6.3",