Skip to content

An AI-powered search engine with a generative UI

feat: migrate to AI SDK v5.0.0-beta Agent abstraction

Yoshiki Miura0493397ebe37
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 15b13c27..46c3384f 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -18,7 +18,19 @@ const DEFAULT_MODEL: Model = {
export async function POST(req: Request) {
try {
- const { message, chatId } = await req.json()
+ const body = await req.json()
+ const { messages, chatId } = body
+
+ // Get the last message from the messages array (the new user message)
+ const message = messages?.[messages.length - 1]
+
+ if (!message) {
+ return new Response('No message provided', {
+ status: 400,
+ statusText: 'Bad Request'
+ })
+ }
+
const referer = req.headers.get('referer')
const isSharePage = referer?.includes('/share/')
const userId = await getCurrentUserId()
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index 264ee74f..b784ec28 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -34,8 +34,16 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
tool.state === 'input-streaming' ||
tool.state === 'input-available'
- const data: Related | undefined =
- tool.state === 'output-available' ? tool.output : undefined
+ let data: Related | undefined = undefined
+
+ if (tool.state === 'output-available' && tool.output) {
+ // Handle both array and object formats
+ if (Array.isArray(tool.output)) {
+ data = { questions: tool.output }
+ } else if (tool.output.questions) {
+ data = tool.output as Related
+ }
+ }
if (!data && isLoading) {
return (
@@ -71,7 +79,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
>
<Section title="Related" className="pt-0 pb-4">
<div className="flex flex-col">
- {Array.isArray(data?.questions) ? (
+ {data && data.questions && Array.isArray(data.questions) ? (
data.questions.map((item, index) => (
<div className="flex items-start w-full" key={index}>
<ArrowRight className="h-4 w-4 mr-2 mt-1.5 flex-shrink-0 text-accent-foreground/50" />
@@ -88,7 +96,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
</div>
))
) : (
- <div>Not an array</div>
+ <div>No data available</div>
)}
</div>
</Section>
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
index e5a6fce5..21dd9a6c 100644
--- a/lib/agents/generate-related-questions.ts
+++ b/lib/agents/generate-related-questions.ts
@@ -1,55 +1,53 @@
-import { generateObject, ModelMessage, streamText, tool } from 'ai'
-import { z } from 'zod'
+import { Experimental_Agent as Agent, tool } from 'ai'
import { relatedSchema } from '../schema/related'
import { getModel } from '../utils/registry'
-export function generateRelatedQuestions(
- messages: ModelMessage[],
- model: string
-) {
- const lastMessages = messages.slice(-1).map(message => ({
- ...message,
- role: 'user'
- })) as ModelMessage[]
-
- const systemPrompt = `As a professional web researcher, generate 3 related queries that explore the topic more deeply, based on the initial query and search results.
-
- The queries should:
- - Progress from general to specific aspects
- - Cover implications and connected topics
- - Match the user's language style and level
- - Help build comprehensive understanding
- - Be relevant and naturally follow from the context
-
- Example format for "Starship's third test flight key milestones":
- - "What technical improvements were made for Starship's third test flight?"
- - "How does the third flight's performance compare to previous launches?"
- - "What are the implications of this test flight for future Starship missions?"`
-
- const result = streamText({
+export function generateRelatedQuestions(model: string) {
+ const systemPrompt = `You are a professional web researcher tasked with generating follow-up questions. Based on the conversation history and search results, create 3 DIFFERENT related questions that:
+
+1. Explore NEW aspects not covered in the original query
+2. Dig deeper into specific details mentioned in the search results
+3. Connect to broader implications or related topics
+
+Guidelines:
+- NEVER repeat or rephrase the original question
+- Each question should explore a UNIQUE angle or aspect
+- Questions should build upon information found in the search results
+- Use natural, conversational language
+- Be specific and actionable
+
+Example:
+Original: "Why is Nvidia growing rapidly?"
+Good follow-ups:
+- "What specific AI technologies is Nvidia developing that competitors lack?"
+- "How does Nvidia's data center revenue compare to its gaming division?"
+- "Which companies are Nvidia's biggest customers for AI chips?"
+
+Bad follow-ups (avoid these):
+- "Why is Nvidia growing so fast?" (rephrases original)
+- "Is Nvidia growing?" (less specific than original)
+- "Tell me about Nvidia" (too general)`
+
+ // Return an agent instance for related questions
+ return new Agent({
model: getModel(model),
system:
- 'Generate related questions. Use the generateRelatedQuestions tool exactly once to provide the related questions.',
- messages: lastMessages,
+ systemPrompt +
+ '\n\nGenerate 3 unique follow-up questions that explore different aspects of the topic.',
tools: {
- related_questions: tool({
- description: 'Generate related questions',
- inputSchema: z.object({}),
- execute: async () => {
- const questions = await generateObject({
- model: getModel(model),
- system: systemPrompt,
- messages: lastMessages,
- schema: relatedSchema
- })
-
- return questions.object
+ relatedQuestions: tool({
+ name: 'relatedQuestions',
+ description:
+ 'Generate 3 unique follow-up questions that explore different aspects of the topic, avoiding repetition of the original query',
+ inputSchema: relatedSchema,
+ execute: async ({ questions }) => {
+ // Return the questions in the expected format
+ return { questions }
}
})
},
+ activeTools: ['relatedQuestions'],
toolChoice: 'required'
})
-
- return result
}
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index 89e18655..58657155 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -1,4 +1,4 @@
-import { ModelMessage, smoothStream, streamText } from 'ai'
+import { Experimental_Agent as Agent, stepCountIs } from 'ai'
import { createQuestionTool } from '../tools/question'
import { retrieveTool } from '../tools/retrieve'
@@ -35,11 +35,9 @@ Citation Format:
`
export function researcher({
- messages,
model,
searchMode
}: {
- messages: ModelMessage[]
model: string
searchMode: boolean
}) {
@@ -51,26 +49,23 @@ export function researcher({
const videoSearchTool = createVideoSearchTool(model)
const askQuestionTool = createQuestionTool(model)
- const config = {
+ // Return an agent instance
+ return new Agent({
model: getModel(model),
system: `${SYSTEM_PROMPT}\nCurrent date and time: ${currentDate}`,
- messages,
tools: {
search: searchTool,
retrieve: retrieveTool,
videoSearch: videoSearchTool,
- ask_question: askQuestionTool
+ askQuestion: askQuestionTool
},
- experimental_activeTools: searchMode
+ activeTools: searchMode
? ['search', 'retrieve', 'videoSearch']
- : [],
- maxSteps: searchMode ? 5 : 1,
- experimental_transform: smoothStream()
- }
-
- return streamText(config)
+ : undefined,
+ stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1)
+ })
} catch (error) {
- console.error('Error in chatResearcher:', error)
+ console.error('Error in researcher:', error)
throw error
}
}
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 99339de7..1a4f10d6 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -12,7 +12,6 @@ import { saveChatMessage, saveSingleMessage } from '../actions/chat-db'
import { generateRelatedQuestions } from '../agents/generate-related-questions'
import { generateChatTitle } from '../agents/title-generator'
import { getChat, getChatMessages } from '../db/chat'
-import { generateUUID } from '../utils'
import { getTextFromParts, mergeUIMessages } from '../utils/message-utils'
import { BaseStreamConfig } from './types'
@@ -53,44 +52,110 @@ export async function createChatStreamResponse(
}
const previousMessages = await getChatMessages(chatId)
- const messagesToModel = [...(previousMessages as any[]), message]
- const result = researcher({
- messages: convertToModelMessages(messagesToModel),
+ // Convert database messages to UIMessage format
+ const previousUIMessages: UIMessage[] = previousMessages.map(msg => ({
+ id: msg.id,
+ role: msg.role as 'user' | 'assistant',
+ parts: msg.parts as UIMessage['parts']
+ }))
+
+ const messagesToModel = [...previousUIMessages, message]
+
+ // Get the researcher agent
+ const researchAgent = researcher({
model: modelId,
searchMode
})
- // Create a variable to store the messages from the first callback
- let firstResponseMessage: UIMessage | null = null
+ // Stream with the research agent
+ const researchResult = researchAgent.stream({
+ messages: convertToModelMessages(messagesToModel)
+ })
+ // Merge research stream without finishing
writer.merge(
- result.toUIMessageStream({
- onFinish: ({ responseMessage }) => {
- // Store the messages for later use
- firstResponseMessage = responseMessage
- }
+ researchResult.toUIMessageStream({
+ sendFinish: false
})
)
- const relatedQuestions = generateRelatedQuestions(
- (await result.response).messages,
- modelId
- )
- writer.merge(
- relatedQuestions.toUIMessageStream({
- onFinish: ({ responseMessage }) => {
- // If there is a first response message, merge it with the new message
- if (firstResponseMessage) {
- const mergedMessage = mergeUIMessages(
- firstResponseMessage,
- responseMessage
- )
- saveSingleMessage(chatId, mergedMessage)
- }
+ // Store messages for merging
+ let researchMessage: UIMessage | null = null
+ let relatedQuestionsMessage: UIMessage | null = null
+
+ // After research completes, generate related questions
+ researchResult.response
+ .then(async researchData => {
+ // Check if request was aborted
+ if (researchData.messages.length === 0) {
+ return
+ }
+
+ try {
+ // Prepare messages for related questions agent
+ const allMessages = [
+ ...convertToModelMessages(messagesToModel),
+ ...researchData.messages
+ ]
+
+ // Get related questions agent
+ const relatedQuestionsAgent = generateRelatedQuestions(modelId)
+ const relatedQuestionsResult = relatedQuestionsAgent.stream({
+ messages: allMessages
+ })
+
+ // Capture research message from the stream
+ researchResult.toUIMessageStream({
+ onFinish: ({ responseMessage }) => {
+ researchMessage = responseMessage
+ }
+ })
+
+ // Merge related questions stream
+ writer.merge(
+ relatedQuestionsResult.toUIMessageStream({
+ sendStart: false,
+ onFinish: ({ responseMessage }) => {
+ relatedQuestionsMessage = responseMessage
+ }
+ })
+ )
+
+ // Save the complete message after both agents finish
+ relatedQuestionsResult.response
+ .then(async () => {
+ // Merge and save both messages
+ if (researchMessage && relatedQuestionsMessage) {
+ const mergedMessage = mergeUIMessages(
+ researchMessage,
+ relatedQuestionsMessage
+ )
+ await saveSingleMessage(chatId, mergedMessage)
+ } else if (researchMessage) {
+ // Save research message only if related questions failed
+ await saveSingleMessage(chatId, researchMessage)
+ }
+ })
+ .catch(error => {
+ console.error('Related questions error:', error)
+ // Save research message even if related questions fail
+ if (researchMessage) {
+ saveSingleMessage(chatId, researchMessage)
+ }
+ })
+ } catch (error) {
+ console.error('Error generating related questions:', error)
}
})
- )
+ .catch(error => {
+ // Handle abort errors gracefully
+ if (error.name === 'AbortError') {
+ console.log('Stream aborted by client')
+ return
+ }
+ console.error('Research agent error:', error)
+ })
} catch (error) {
console.error('Stream execution error:', error)
throw error // This error will be handled by the onError callback