Skip to content

An AI-powered search engine with a generative UI

refactor: restructure streaming implementation

Yoshiki Miura669b66efd2d9
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 5d8946e7..54c1f577 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -1,15 +1,6 @@
-import { getChat, saveChat } from '@/lib/actions/chat'
-import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
-import { researcher } from '@/lib/agents/researcher'
-import { ExtendedCoreMessage } from '@/lib/types'
-import { convertToExtendedCoreMessages } from '@/lib/utils'
+import { createManualToolStreamResponse } from '@/lib/streaming/create-manual-tool-stream'
+import { createToolCallingStreamResponse } from '@/lib/streaming/create-tool-calling-stream'
import { isProviderEnabled } from '@/lib/utils/registry'
-import {
- convertToCoreMessages,
- createDataStreamResponse,
- JSONValue,
- streamText
-} from 'ai'
import { cookies } from 'next/headers'
export const maxDuration = 30
@@ -29,9 +20,6 @@ export async function POST(req: Request) {
})
}
- const coreMessages = convertToCoreMessages(messages)
- const extendedCoreMessages = convertToExtendedCoreMessages(messages)
-
const cookieStore = await cookies()
const modelFromCookie = cookieStore.get('selected-model')?.value
const model = modelFromCookie || DEFAULT_MODEL
@@ -44,97 +32,19 @@ export async function POST(req: Request) {
})
}
- return createDataStreamResponse({
- execute: async dataStream => {
- try {
- let researcherConfig
- try {
- researcherConfig = await researcher({
- messages: coreMessages,
- model
- })
- } catch (error) {
- console.error('Researcher configuration error:', error)
- throw new Error('Failed to initialize researcher configuration')
- }
-
- const result = streamText({
- ...researcherConfig,
- onFinish: async event => {
- try {
- const responseMessages = event.response.messages
-
- let annotation: JSONValue = {
- type: 'related-questions',
- data: {
- items: []
- }
- }
-
- // Notify related questions loading
- dataStream.writeMessageAnnotation(annotation)
-
- // Generate related questions
- const relatedQuestions = await generateRelatedQuestions(
- responseMessages,
- model
- )
-
- // Update the annotation with the related questions
- annotation = {
- ...annotation,
- data: relatedQuestions.object
- }
-
- // Send related questions to client
- dataStream.writeMessageAnnotation(annotation)
-
- // Create the message to save
- const generatedMessages = [
- ...extendedCoreMessages,
- ...responseMessages.slice(0, -1),
- {
- role: 'data',
- content: annotation
- },
- responseMessages[responseMessages.length - 1]
- ] as ExtendedCoreMessage[]
-
- // Get the chat from the database if it exists, otherwise create a new one
- const savedChat = (await getChat(chatId)) ?? {
- messages: [],
- createdAt: new Date(),
- userId: 'anonymous',
- path: `/search/${chatId}`,
- title: messages[0].content,
- id: chatId
- }
-
- // Save chat with complete response and related questions
- await saveChat({
- ...savedChat,
- messages: generatedMessages
- }).catch(error => {
- console.error('Failed to save chat:', error)
- throw new Error('Failed to save chat history')
- })
- } catch (error) {
- console.error('Error in onFinish:', error)
- throw error
- }
- }
- })
-
- result.mergeIntoDataStream(dataStream)
- } catch (error) {
- console.error('Stream execution error:', error)
- }
- },
- onError: error => {
- console.error('Stream error:', error)
- return error instanceof Error ? error.message : String(error)
- }
- })
+ const supportsToolCalling = provider === 'openai'
+
+ return supportsToolCalling
+ ? createToolCallingStreamResponse({
+ messages,
+ model,
+ chatId
+ })
+ : createManualToolStreamResponse({
+ messages,
+ model,
+ chatId
+ })
} catch (error) {
console.error('API route error:', error)
return new Response(
diff --git a/lib/streaming/create-manual-tool-stream.ts b/lib/streaming/create-manual-tool-stream.ts
new file mode 100644
index 00000000..eaff69cd
--- /dev/null
+++ b/lib/streaming/create-manual-tool-stream.ts
@@ -0,0 +1,18 @@
+import { createDataStreamResponse, DataStreamWriter } from 'ai'
+import { BaseStreamConfig } from './types'
+
+export function createManualToolStreamResponse(config: BaseStreamConfig) {
+ return createDataStreamResponse({
+ execute: async (dataStream: DataStreamWriter) => {
+ try {
+ // TODO: Implement manual tool calling
+ } catch (error) {
+ console.error('Stream execution error:', error)
+ }
+ },
+ onError: error => {
+ console.error('Stream error:', error)
+ return error instanceof Error ? error.message : String(error)
+ }
+ })
+}
diff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.ts
new file mode 100644
index 00000000..c6ec9f3d
--- /dev/null
+++ b/lib/streaming/create-tool-calling-stream.ts
@@ -0,0 +1,46 @@
+import { researcher } from '@/lib/agents/researcher'
+import {
+ convertToCoreMessages,
+ createDataStreamResponse,
+ DataStreamWriter,
+ streamText
+} from 'ai'
+import { handleStreamFinish } from './handle-stream-finish'
+import { BaseStreamConfig } from './types'
+
+export function createToolCallingStreamResponse(config: BaseStreamConfig) {
+ return createDataStreamResponse({
+ execute: async (dataStream: DataStreamWriter) => {
+ try {
+ const coreMessages = convertToCoreMessages(config.messages)
+
+ let researcherConfig = await researcher({
+ messages: coreMessages,
+ model: config.model
+ })
+
+ const result = streamText({
+ ...researcherConfig,
+ onFinish: async result => {
+ await handleStreamFinish({
+ responseMessages: result.response.messages,
+ originalMessages: config.messages,
+ model: config.model,
+ chatId: config.chatId,
+ dataStream
+ })
+ }
+ })
+
+ result.mergeIntoDataStream(dataStream)
+ } catch (error) {
+ console.error('Stream execution error:', error)
+ throw error
+ }
+ },
+ onError: error => {
+ console.error('Stream error:', error)
+ return error instanceof Error ? error.message : String(error)
+ }
+ })
+}
diff --git a/lib/streaming/handle-stream-finish.ts b/lib/streaming/handle-stream-finish.ts
new file mode 100644
index 00000000..4bdbed7a
--- /dev/null
+++ b/lib/streaming/handle-stream-finish.ts
@@ -0,0 +1,83 @@
+import { getChat, saveChat } from '@/lib/actions/chat'
+import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
+import { ExtendedCoreMessage } from '@/lib/types'
+import { convertToExtendedCoreMessages } from '@/lib/utils'
+import { CoreMessage, DataStreamWriter, JSONValue, Message } from 'ai'
+
+interface HandleStreamFinishParams {
+ responseMessages: CoreMessage[]
+ originalMessages: Message[]
+ model: string
+ chatId: string
+ dataStream: DataStreamWriter
+}
+
+export async function handleStreamFinish({
+ responseMessages,
+ originalMessages,
+ model,
+ chatId,
+ dataStream
+}: HandleStreamFinishParams) {
+ try {
+ const extendedCoreMessages = convertToExtendedCoreMessages(originalMessages)
+
+ let annotation: JSONValue = {
+ type: 'related-questions',
+ data: {
+ items: []
+ }
+ }
+
+ // Notify related questions loading
+ dataStream.writeMessageAnnotation(annotation)
+
+ // Generate related questions
+ const relatedQuestions = await generateRelatedQuestions(
+ responseMessages,
+ model
+ )
+
+ // Update the annotation with the related questions
+ annotation = {
+ ...annotation,
+ data: relatedQuestions.object
+ }
+
+ // Send related questions to client
+ dataStream.writeMessageAnnotation(annotation)
+
+ // Create the message to save
+ const generatedMessages = [
+ ...extendedCoreMessages,
+ ...responseMessages.slice(0, -1),
+ {
+ role: 'data',
+ content: annotation
+ } as ExtendedCoreMessage,
+ responseMessages[responseMessages.length - 1]
+ ] as ExtendedCoreMessage[]
+
+ // Get the chat from the database if it exists, otherwise create a new one
+ const savedChat = (await getChat(chatId)) ?? {
+ messages: [],
+ createdAt: new Date(),
+ userId: 'anonymous',
+ path: `/search/${chatId}`,
+ title: originalMessages[0].content,
+ id: chatId
+ }
+
+ // Save chat with complete response and related questions
+ await saveChat({
+ ...savedChat,
+ messages: generatedMessages
+ }).catch(error => {
+ console.error('Failed to save chat:', error)
+ throw new Error('Failed to save chat history')
+ })
+ } catch (error) {
+ console.error('Error in handleStreamFinish:', error)
+ throw error
+ }
+}
diff --git a/lib/streaming/types.ts b/lib/streaming/types.ts
new file mode 100644
index 00000000..dfd5f29f
--- /dev/null
+++ b/lib/streaming/types.ts
@@ -0,0 +1,7 @@
+import { Message } from 'ai'
+
+export interface BaseStreamConfig {
+ messages: Message[]
+ model: string
+ chatId: string
+}