Skip to content

An AI-powered search engine with a generative UI

feat: Implement AI SDK v5 migration (initial working version)

Yoshiki Miura40223f13dbd4
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 92fa8c79..b67a3ba4 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -17,11 +17,14 @@ const DEFAULT_MODEL: Model = {
export async function POST(req: Request) {
try {
- const { message, messages, id: chatId } = await req.json()
+ const { message, chatId } = await req.json()
const referer = req.headers.get('referer')
const isSharePage = referer?.includes('/share/')
const userId = await getCurrentUserId()
+ console.log('message', message)
+ console.log('chatId', chatId)
+
if (isSharePage) {
return new Response('Chat API is not available on share pages', {
status: 403,
diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsx
index 68ef0481..12052e08 100644
--- a/app/search/[id]/page.tsx
+++ b/app/search/[id]/page.tsx
@@ -2,7 +2,7 @@ import { Chat } from '@/components/chat'
import { getChat } from '@/lib/actions/chat-db'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
import { getModels } from '@/lib/config/models'
-import { Message } from 'ai'
+import { UIMessage } from 'ai'
import { notFound, redirect } from 'next/navigation'
export const maxDuration = 60
@@ -40,12 +40,13 @@ export default async function SearchPage(props: {
redirect('/auth/login')
}
- const messages: Message[] = chat.messages.map(message => ({
+ const messages: UIMessage[] = chat.messages.map(message => ({
id: message.id,
- parts: message.parts as Message['parts'],
- role: message.role as Message['role'],
- content: '',
- createdAt: new Date(message.createdAt)
+ parts: message.parts as UIMessage['parts'],
+ role: message.role as UIMessage['role'],
+ metadata: {
+ createdAt: new Date(message.createdAt)
+ }
}))
const models = await getModels()
diff --git a/components/chat-messages.tsx b/components/chat-messages.tsx
index e00ae5f1..2a80e0dd 100644
--- a/components/chat-messages.tsx
+++ b/components/chat-messages.tsx
@@ -1,22 +1,20 @@
'use client'
import { cn } from '@/lib/utils'
-import { JSONValue, Message } from 'ai'
-import { useEffect, useMemo, useState } from 'react'
+import { UIMessage } from 'ai'
+import { useEffect, useState } from 'react'
import { RenderMessage } from './render-message'
-import { ToolSection } from './tool-section'
import { Spinner } from './ui/spinner'
// Import section structure interface
interface ChatSection {
id: string
- userMessage: Message
- assistantMessages: Message[]
+ userMessage: UIMessage
+ assistantMessages: UIMessage[]
}
interface ChatMessagesProps {
sections: ChatSection[] // Changed from messages to sections
- data: JSONValue[] | undefined
onQuerySelect: (query: string) => void
isLoading: boolean
chatId?: string
@@ -29,7 +27,6 @@ interface ChatMessagesProps {
export function ChatMessages({
sections,
- data,
onQuerySelect,
isLoading,
chatId,
@@ -51,31 +48,6 @@ export function ChatMessages({
}
}, [sections])
- // get last tool data for manual tool call
- const lastToolData = useMemo(() => {
- if (!data || !Array.isArray(data) || data.length === 0) return null
-
- const lastItem = data[data.length - 1] as {
- type: 'tool_call'
- data: {
- toolCallId: string
- state: 'call' | 'result'
- toolName: string
- args: string
- }
- }
-
- if (lastItem.type !== 'tool_call') return null
-
- const toolData = lastItem.data
- return {
- state: 'call' as const,
- toolCallId: toolData.toolCallId,
- toolName: toolData.toolName,
- args: toolData.args ? JSON.parse(toolData.args) : undefined
- }
- }, [data])
-
if (!sections.length) return null
// Get all messages as a flattened array
@@ -168,16 +140,6 @@ export function ChatMessages({
))}
</div>
))}
-
- {showLoading && lastToolData && (
- <ToolSection
- key={manualToolCallId}
- tool={lastToolData}
- isOpen={getIsOpen(manualToolCallId)}
- onOpenChange={open => handleOpenChange(manualToolCallId, open)}
- addToolResult={addToolResult}
- />
- )}
</div>
</div>
)
diff --git a/components/chat.tsx b/components/chat.tsx
index 3bf3c588..e42246f7 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -1,21 +1,21 @@
'use client'
import { deleteTrailingMessages } from '@/lib/actions/chat-db'
-import { CHAT_ID } from '@/lib/constants'
import { Model } from '@/lib/types/models'
import { cn, generateUUID } from '@/lib/utils'
import { useChat } from '@ai-sdk/react'
-import { Message } from 'ai/react'
+import { UIMessage, defaultChatStore } from 'ai'
import { useEffect, useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
+import { z } from 'zod'
import { ChatMessages } from './chat-messages'
import { ChatPanel } from './chat-panel'
// Define section structure
interface ChatSection {
id: string // User message ID
- userMessage: Message
- assistantMessages: Message[]
+ userMessage: UIMessage
+ assistantMessages: UIMessage[]
}
export function Chat({
@@ -25,13 +25,31 @@ export function Chat({
models
}: {
id: string
- savedMessages?: Message[]
+ savedMessages?: UIMessage[]
query?: string
models?: Model[]
}) {
const scrollContainerRef = useRef<HTMLDivElement>(null)
const [isAtBottom, setIsAtBottom] = useState(true)
+ const chatStore: any = defaultChatStore({
+ api: '/api/chat',
+ messageMetadataSchema: z.object({
+ createdAt: z.date()
+ }),
+ chats: {
+ [id]: {
+ messages: savedMessages
+ }
+ },
+ prepareRequestBody: (body: any) => {
+ return {
+ chatId: body.chatId,
+ message: body.messages.at(-1)
+ }
+ }
+ })
+
const {
messages,
input,
@@ -41,13 +59,10 @@ export function Chat({
setMessages,
stop,
append,
- data,
- setData,
addToolResult,
reload
} = useChat({
- initialMessages: savedMessages,
- id: CHAT_ID,
+ chatId: id,
onFinish: () => {
window.history.replaceState({}, '', `/search/${id}`)
window.dispatchEvent(new CustomEvent('chat-history-updated'))
@@ -55,13 +70,9 @@ export function Chat({
onError: error => {
toast.error(`Error in chat: ${error.message}`)
},
- sendExtraMessageFields: false,
experimental_throttle: 100,
- generateId: generateUUID,
- experimental_prepareRequestBody: body => ({
- id,
- message: body.messages.at(-1)
- })
+ chatStore,
+ generateId: generateUUID
})
const isLoading = status === 'submitted' || status === 'streaming'
@@ -133,14 +144,14 @@ export function Chat({
}
}, [sections, messages])
- useEffect(() => {
- setMessages(savedMessages)
- }, [id])
+ // useEffect(() => {
+ // setMessages(savedMessages)
+ // }, [id])
const onQuerySelect = (query: string) => {
append({
role: 'user',
- content: query
+ parts: [{ type: 'text', text: query }]
})
}
@@ -165,7 +176,9 @@ export function Chat({
return
}
const pivotTimestamp =
- pivotMessage.createdAt?.toISOString() ?? new Date(0).toISOString()
+ (
+ pivotMessage.metadata as { createdAt?: Date }
+ )?.createdAt?.toISOString() ?? new Date(0).toISOString()
try {
setMessages(prevMessages => {
@@ -177,12 +190,10 @@ export function Chat({
? prevMessages.slice(0, messageIndex)
: prevMessages
- const newUIMessage: Message = {
+ const newUIMessage: UIMessage = {
id: generateUUID(),
role: 'user',
- content: newContentText,
- parts: [{ type: 'text', text: newContentText }],
- createdAt: new Date()
+ parts: [{ type: 'text', text: newContentText }]
}
return [...messagesBeforeEdited, newUIMessage]
@@ -231,17 +242,16 @@ export function Chat({
return
}
- const followerMessage = messages[followerMessageIndex]
const deletionTimestamp =
- followerMessage.createdAt?.toISOString() ?? new Date(0).toISOString()
+ (
+ targetUserMessage.metadata as { createdAt?: Date }
+ )?.createdAt?.toISOString() ?? new Date(0).toISOString()
const contentToResend =
targetUserMessage.parts
?.filter(p => p.type === 'text')
.map(p => p.text)
- .join('') ||
- targetUserMessage.content ||
- ''
+ .join('') || ''
try {
setMessages(prevMessages => {
@@ -250,12 +260,10 @@ export function Chat({
targetUserMessageIndex
)
- const newResentUserMessage: Message = {
+ const newResentUserMessage: UIMessage = {
id: generateUUID(),
role: 'user',
- content: contentToResend,
- parts: [{ type: 'text', text: contentToResend }],
- createdAt: new Date()
+ parts: [{ type: 'text', text: contentToResend }]
}
return [...messagesBeforeTarget, newResentUserMessage]
})
@@ -273,10 +281,11 @@ export function Chat({
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
- setData(undefined)
handleSubmit(e)
}
+ console.log('id', id)
+
return (
<div
className={cn(
@@ -287,7 +296,6 @@ export function Chat({
>
<ChatMessages
sections={sections}
- data={data}
onQuerySelect={onQuerySelect}
isLoading={isLoading}
chatId={id}
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 2212dfbb..4b70f75d 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -1,9 +1,9 @@
-import { Message, smoothStream, streamText } from 'ai'
+import { openai } from '@ai-sdk/openai'
+import { ModelMessage, smoothStream, streamText } from 'ai'
import { createQuestionTool } from '../tools/question'
import { retrieveTool } from '../tools/retrieve'
import { createSearchTool } from '../tools/search'
import { createVideoSearchTool } from '../tools/video-search'
-import { getModel } from '../utils/registry'
const SYSTEM_PROMPT = `
Instructions:
@@ -40,7 +40,7 @@ export function researcher({
model,
searchMode
}: {
- messages: Message[]
+ messages: ModelMessage[]
model: string
searchMode: boolean
}): ResearcherReturn {
@@ -53,7 +53,7 @@ export function researcher({
const askQuestionTool = createQuestionTool(model)
return {
- model: getModel(model),
+ model: openai('gpt-4o-mini'), // TODO: Make this configurable
system: `${SYSTEM_PROMPT}\nCurrent date and time: ${currentDate}`,
messages,
tools: {
diff --git a/lib/agents/title-generator.ts b/lib/agents/title-generator.ts
index de538c86..78d9d75b 100644
--- a/lib/agents/title-generator.ts
+++ b/lib/agents/title-generator.ts
@@ -1,3 +1,4 @@
+import { openai } from '@ai-sdk/openai'
import { generateText, LanguageModel } from 'ai'
interface GenerateChatTitleParams {
@@ -22,10 +23,9 @@ export async function generateChatTitle({
const systemPrompt = `System: You are an AI assistant specialized in creating very short, concise, and informative titles for chat conversations based on the user's first message. The title should ideally be 3-5 words long, and no more than 10 words. Only output the title itself, with no prefixes, labels, or quotation marks.`
const { text: generatedTitle } = await generateText({
- model: model,
+ model: openai('gpt-4o-mini'), // TODO: Make this configurable
system: systemPrompt,
- prompt: userMessageContent,
- maxTokens: 25 // A bit more tokens for safety, but prompt guides length.
+ prompt: userMessageContent
})
const cleanedTitle = generatedTitle.trim()
diff --git a/lib/db/chat.ts b/lib/db/chat.ts
index 96af400f..70195b6c 100644
--- a/lib/db/chat.ts
+++ b/lib/db/chat.ts
@@ -1,6 +1,5 @@
import { and, desc, eq, gte, inArray } from 'drizzle-orm'
import { revalidatePath } from 'next/cache'
-import { redirect } from 'next/navigation'
import { db } from '.'
import { chats, messages, type Chat, type Message } from './schema'
@@ -226,7 +225,8 @@ export async function clearChats(userId: string): Promise<{ error?: string }> {
await db.delete(chats).where(eq(chats.userId, userId))
revalidatePath('/')
- redirect('/')
+
+ return { error: undefined }
} catch (error) {
console.error('Error clearing chats:', error)
return { error: 'Failed to clear chats' }
diff --git a/lib/schema/search.tsx b/lib/schema/search.tsx
index 19283dd1..614f6594 100644
--- a/lib/schema/search.tsx
+++ b/lib/schema/search.tsx
@@ -30,9 +30,7 @@ export const searchSchema = z.object({
// Strict schema with all fields required
export const strictSearchSchema = z.object({
query: z.string().describe('The query to search for'),
- max_results: z
- .number()
- .describe('The maximum number of results to return. default is 20'),
+ max_results: z.number().describe('The maximum number of results to return.'),
search_depth: z
.enum(['basic', 'advanced'])
.describe('The depth of the search'),
@@ -53,6 +51,8 @@ export const strictSearchSchema = z.object({
* Uses the strict schema for OpenAI models starting with 'o'.
*/
export function getSearchSchemaForModel(fullModel: string) {
+ return strictSearchSchema
+
const [provider, modelName] = fullModel?.split(':') ?? []
const useStrictSchema =
(provider === 'openai' || provider === 'azure') &&
diff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.ts
index 1327356a..7784ce09 100644
--- a/lib/streaming/create-tool-calling-stream.ts
+++ b/lib/streaming/create-tool-calling-stream.ts
@@ -1,22 +1,22 @@
import { researcher } from '@/lib/agents/researcher'
+import { openai } from '@ai-sdk/openai'
import {
appendClientMessage,
- appendResponseMessages,
- createDataStreamResponse,
- DataStreamWriter,
- streamText
+ convertToModelMessages,
+ createUIMessageStream,
+ createUIMessageStreamResponse,
+ streamText,
+ UIMessageStreamWriter
} from 'ai'
import { saveChatMessage, saveSingleMessage } from '../actions/chat-db'
import { generateChatTitle } from '../agents/title-generator'
import { getChat, getChatMessages } from '../db/chat'
-import { generateUUID } from '../utils'
import { getTextFromParts } from '../utils/message-utils'
-import { getModel } from '../utils/registry'
import { BaseStreamConfig } from './types'
export function createToolCallingStreamResponse(config: BaseStreamConfig) {
- return createDataStreamResponse({
- execute: async (dataStream: DataStreamWriter) => {
+ const stream = createUIMessageStream({
+ execute: async (writer: UIMessageStreamWriter) => {
const { message, model, chatId, searchMode, userId } = config
const modelId = `${model.providerId}:${model.id}`
@@ -25,7 +25,7 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {
if (!chat) {
const title = await generateChatTitle({
userMessageContent: getTextFromParts(message.parts),
- model: getModel(modelId)
+ model: openai('gpt-4o-mini')
})
// Save the chat and user message to the database using server action
@@ -63,48 +63,39 @@ export function createToolCallingStreamResponse(config: BaseStreamConfig) {
})
let researcherConfig = await researcher({
- messages,
+ messages: convertToModelMessages(messages),
model: modelId,
searchMode
})
const result = streamText({
- ...researcherConfig,
- experimental_generateMessageId: generateUUID
+ ...researcherConfig
})
- result.mergeIntoDataStream(dataStream)
+ writer.merge(result.toUIMessageStream())
const responseMessages = (await result.response).messages
- const assistantId = responseMessages
- .filter(message => message.role === 'assistant')
- .at(-1)?.id
- if (!assistantId) {
- throw new Error('No assistant id found')
- }
-
- const [, assistantMessage] = appendResponseMessages({
- messages: [message],
- responseMessages: responseMessages
- })
+ console.log('responseMessages', responseMessages)
// Save the assistant message to the database using server action
- if (assistantMessage) {
- await saveSingleMessage({
- id: assistantId,
- chatId,
- role: assistantMessage.role,
- parts: assistantMessage.parts
- })
- }
+ // if (assistantMessage) {
+ // await saveSingleMessage({
+ // id: assistantId,
+ // chatId,
+ // role: assistantMessage.role,
+ // parts: assistantMessage.parts
+ // })
+ // }
} catch (error) {
console.error('Stream execution error:', error)
throw error
}
},
- onError: error => {
+ onError: (error: any) => {
// console.error('Stream error:', error)
return error instanceof Error ? error.message : String(error)
}
})
+
+ return createUIMessageStreamResponse({ stream })
}