Skip to content

An AI-powered search engine with a generative UI

Implement chat and message database storage in streaming response using server actions

Yoshiki Miura0bdbdde6590d
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 906379e3..b3d8ce1a 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -18,7 +18,7 @@ const DEFAULT_MODEL: Model = {
export async function POST(req: Request) {
try {
- const { messages, id: chatId } = await req.json()
+ const { message, messages, id: chatId } = await req.json()
const referer = req.headers.get('referer')
const isSharePage = referer?.includes('/share/')
const userId = await getCurrentUserId()
@@ -61,18 +61,18 @@ export async function POST(req: Request) {
return supportsToolCalling
? createToolCallingStreamResponse({
- messages,
+ message,
model: selectedModel,
chatId,
searchMode,
- userId
+ userId: userId!
})
: createManualToolStreamResponse({
- messages,
+ message,
model: selectedModel,
chatId,
searchMode,
- userId
+ userId: userId!
})
} catch (error) {
console.error('API route error:', error)
diff --git a/app/page.tsx b/app/page.tsx
index bd5844a8..eba2e815 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -1,9 +1,9 @@
import { Chat } from '@/components/chat'
import { getModels } from '@/lib/config/models'
-import { generateId } from 'ai'
+import { generateUUID } from '@/lib/utils'
export default async function Page() {
- const id = generateId()
+ const id = generateUUID()
const models = await getModels()
return <Chat id={id} models={models} />
}
diff --git a/app/search/page.tsx b/app/search/page.tsx
index b1533fe4..bba3b14b 100644
--- a/app/search/page.tsx
+++ b/app/search/page.tsx
@@ -1,6 +1,6 @@
import { Chat } from '@/components/chat'
import { getModels } from '@/lib/config/models'
-import { generateId } from 'ai'
+import { generateUUID } from '@/lib/utils'
import { redirect } from 'next/navigation'
export const maxDuration = 60
@@ -13,7 +13,7 @@ export default async function SearchPage(props: {
redirect('/')
}
- const id = generateId()
+ const id = generateUUID()
const models = await getModels()
return <Chat id={id} query={q} models={models} />
}
diff --git a/components/chat.tsx b/components/chat.tsx
index c95173d3..98e11911 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -3,7 +3,7 @@
import { CHAT_ID } from '@/lib/constants'
import { useAutoScroll } from '@/lib/hooks/use-auto-scroll'
import { Model } from '@/lib/types/models'
-import { cn } from '@/lib/utils'
+import { cn, generateUUID } from '@/lib/utils'
import { useChat } from '@ai-sdk/react'
import { ChatRequestOptions } from 'ai'
import { Message } from 'ai/react'
@@ -44,9 +44,6 @@ export function Chat({
} = useChat({
initialMessages: savedMessages,
id: CHAT_ID,
- body: {
- id
- },
onFinish: () => {
window.history.replaceState({}, '', `/search/${id}`)
window.dispatchEvent(new CustomEvent('chat-history-updated'))
@@ -55,7 +52,12 @@ export function Chat({
toast.error(`Error in chat: ${error.message}`)
},
sendExtraMessageFields: false, // Disable extra message fields,
- experimental_throttle: 100
+ experimental_throttle: 100,
+ generateId: generateUUID,
+ experimental_prepareRequestBody: body => ({
+ id,
+ message: body.messages.at(-1)
+ })
})
const isLoading = status === 'submitted' || status === 'streaming'
diff --git a/lib/actions/chat-db.ts b/lib/actions/chat-db.ts
index 82c312ff..3de13c15 100644
--- a/lib/actions/chat-db.ts
+++ b/lib/actions/chat-db.ts
@@ -41,6 +41,74 @@ interface ClientNewMessageInput {
parts: DBMessage['parts']
}
+// Interface for single message save
+interface SaveMessageInput {
+ id?: string
+ chatId: string
+ role: string
+ parts: any
+}
+
+// Save a single message
+export async function saveSingleMessage(
+ message: SaveMessageInput
+): Promise<DBMessage> {
+ try {
+ return await chatDb.addMessage(message)
+ } catch (error) {
+ console.error(`Error saving message for chat ID ${message.chatId}:`, error)
+ throw error
+ }
+}
+
+// Save or create a chat and add a user message
+export async function saveChatMessage(
+ chatId: string,
+ messageId: string,
+ messageContent: any,
+ messageRole: string,
+ userId: string,
+ title?: string
+): Promise<{ chat: DBChat; message: DBMessage }> {
+ try {
+ // 1. Check if chat exists
+ const existingChat = await chatDb.getChat(chatId, userId)
+
+ // 2. Save or update chat if needed
+ let chat: DBChat
+ if (!existingChat) {
+ // Create new chat
+ const chatTitle =
+ title ||
+ (typeof messageContent === 'string' ? messageContent : 'New Chat')
+ const chatDataForDb: Partial<DBChat> = {
+ id: chatId,
+ title: chatTitle.substring(0, 255), // Limit title length
+ userId: userId,
+ visibility: 'private'
+ }
+
+ const savedChats = await chatDb.saveChat(chatDataForDb as DBChat, userId)
+ chat = savedChats[0]
+ } else {
+ chat = existingChat
+ }
+
+ // 3. Save message
+ const message = await chatDb.addMessage({
+ id: messageId,
+ chatId,
+ role: messageRole,
+ parts: messageContent
+ })
+
+ return { chat, message }
+ } catch (error) {
+ console.error(`Error in saveChatMessage for chat ID ${chatId}:`, error)
+ throw error
+ }
+}
+
export async function saveChat(
clientChatInput: ClientChatInput,
clientNewMessages: ClientNewMessageInput[] | undefined, // New messages are optional (e.g., when only updating metadata)
diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.ts
index a2806d51..0902bc63 100644
--- a/lib/agents/researcher.ts
+++ b/lib/agents/researcher.ts
@@ -1,4 +1,4 @@
-import { CoreMessage, smoothStream, streamText } from 'ai'
+import { Message, smoothStream, streamText } from 'ai'
import { createQuestionTool } from '../tools/question'
import { retrieveTool } from '../tools/retrieve'
import { createSearchTool } from '../tools/search'
@@ -40,7 +40,7 @@ export function researcher({
model,
searchMode
}: {
- messages: CoreMessage[]
+ messages: Message[]
model: string
searchMode: boolean
}): ResearcherReturn {
diff --git a/lib/auth/get-current-user.ts b/lib/auth/get-current-user.ts
index 2663346a..8070d76c 100644
--- a/lib/auth/get-current-user.ts
+++ b/lib/auth/get-current-user.ts
@@ -15,5 +15,5 @@ export async function getCurrentUser() {
export async function getCurrentUserId() {
const user = await getCurrentUser()
- return user?.id ?? 'anonymous'
+ return user?.id
}
diff --git a/lib/streaming/create-tool-calling-stream.ts b/lib/streaming/create-tool-calling-stream.ts
index 39cf361f..cee6bec8 100644
--- a/lib/streaming/create-tool-calling-stream.ts
+++ b/lib/streaming/create-tool-calling-stream.ts
@@ -1,105 +1,97 @@
import { researcher } from '@/lib/agents/researcher'
import {
- convertMessagesForDB,
- extractTitleFromMessage
-} from '@/lib/utils/message-utils'
-import {
- convertToCoreMessages,
- CoreMessage,
+ appendClientMessage,
+ appendResponseMessages,
createDataStreamResponse,
DataStreamWriter,
streamText
} from 'ai'
-import { saveChat } from '../actions/chat-db'
-import { getCurrentUserId } from '../auth/get-current-user'
-import { getChat } from '../db/chat'
-import { getMaxAllowedTokens, truncateMessages } from '../utils/context-window'
+import { saveChatMessage, saveSingleMessage } from '../actions/chat-db'
+import { getChat, getChatMessages } from '../db/chat'
+import { generateUUID } from '../utils'
import { BaseStreamConfig } from './types'
-// Function to check if a message contains ask_question tool invocation
-function containsAskQuestionTool(message: CoreMessage) {
- // For CoreMessage format, we check the content array
- if (message.role !== 'assistant' || !Array.isArray(message.content)) {
- return false
- }
-
- // Check if any content item is a tool-call with ask_question tool
- return message.content.some(
- item => item.type === 'tool-call' && item.toolName === 'ask_question'
- )
-}
-
export function createToolCallingStreamResponse(config: BaseStreamConfig) {
return createDataStreamResponse({
execute: async (dataStream: DataStreamWriter) => {
- const { messages, model, chatId, searchMode, userId } = config
+ const { message, model, chatId, searchMode, userId } = config
const modelId = `${model.providerId}:${model.id}`
try {
- const coreMessages = convertToCoreMessages(messages)
- const truncatedMessages = truncateMessages(
- coreMessages,
- getMaxAllowedTokens(model)
- )
+ const chat = await getChat(chatId, userId)
+ if (!chat) {
+ // Extract title from message content
+ const title = message.content
+
+ // Save the chat and user message to the database using server action
+ await saveChatMessage(
+ chatId,
+ message.id,
+ message.parts,
+ message.role,
+ userId,
+ title
+ )
+ } else {
+ if (chat.userId !== userId) {
+ // TODO: Handle this
+ // return new Response('You are not allowed to access this chat', {
+ // status: 403,
+ // statusText: 'Forbidden'
+ // })
+ }
+
+ // Save just the user message to an existing chat
+ await saveSingleMessage({
+ id: message.id,
+ chatId,
+ role: message.role,
+ parts: message.parts
+ })
+ }
+
+ const previousMessages = await getChatMessages(chatId)
+ const messages = appendClientMessage({
+ // @ts-ignore
+ messages: previousMessages,
+ message
+ })
let researcherConfig = await researcher({
- messages: truncatedMessages,
+ messages,
model: modelId,
searchMode
})
const result = streamText({
- ...researcherConfig
- // onFinish: async result => {
- // // Check if the last message contains an ask_question tool invocation
- // const shouldSkipRelatedQuestions =
- // isReasoningModel(modelId) ||
- // (result.response.messages.length > 0 &&
- // containsAskQuestionTool(
- // result.response.messages[
- // result.response.messages.length - 1
- // ] as CoreMessage
- // ))
-
- // await handleStreamFinish({
- // responseMessages: result.response.messages,
- // originalMessages: messages,
- // model: modelId,
- // chatId,
- // dataStream,
- // userId,
- // skipRelatedQuestions: shouldSkipRelatedQuestions
- // })
- // }
+ ...researcherConfig,
+ experimental_generateMessageId: generateUUID
})
result.mergeIntoDataStream(dataStream)
- const userId = await getCurrentUserId()
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')
+ }
- // Convert messages to database format
- const saveMessages = convertMessagesForDB(responseMessages)
-
- // Check if chat exists to determine title and update strategy
- const existingChat = await getChat(chatId, userId)
-
- // Get appropriate title - use existing title if available, otherwise extract from message
- const title = existingChat
- ? existingChat.title
- : extractTitleFromMessage(coreMessages[0]) || 'New Chat'
+ const [, assistantMessage] = appendResponseMessages({
+ messages: [message],
+ responseMessages: responseMessages
+ })
- // Save the chat to the database
- // Only update metadata if this is a new chat (existingChat is null)
- await saveChat(
- {
- id: chatId,
- title
- },
- saveMessages,
- userId,
- !existingChat // Only update metadata for new chats
- )
+ // Save the assistant message to the database using server action
+ if (assistantMessage) {
+ await saveSingleMessage({
+ id: assistantId,
+ chatId,
+ role: assistantMessage.role,
+ parts: assistantMessage.parts
+ })
+ }
} catch (error) {
console.error('Stream execution error:', error)
throw error
diff --git a/lib/streaming/types.ts b/lib/streaming/types.ts
index 4501235d..d207ff18 100644
--- a/lib/streaming/types.ts
+++ b/lib/streaming/types.ts
@@ -2,7 +2,7 @@ import { Message } from 'ai'
import { Model } from '../types/models'
export interface BaseStreamConfig {
- messages: Message[]
+ message: Message
model: Model
chatId: string
searchMode: boolean
diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index 7ea663c9..da992d56 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -11,6 +11,17 @@ import {
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { ExtendedCoreMessage } from '../types'
+
+// Function to generate a UUID
+export function generateUUID(): string {
+ // Generate UUIDv4
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+ const r = (Math.random() * 16) | 0,
+ v = c === 'x' ? r : (r & 0x3) | 0x8
+ return v.toString(16)
+ })
+}
+
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}