An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: Update chat route to save complete response and related questions
Yoshiki Miura7cb3ef6fafd4
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 49c397e1..e267fc9b 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -1,9 +1,7 @@import {streamText,createDataStreamResponse,- Message,convertToCoreMessages,- generateId,JSONValue,ToolInvocation} from 'ai'@@ -11,6 +9,7 @@ import { researcher } from '@/lib/agents/researcher'import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'import { cookies } from 'next/headers'import { getChat, saveChat } from '@/lib/actions/chat'+import { ExtendedCoreMessage } from '@/lib/types'export const maxDuration = 30@@ -71,13 +70,15 @@ export async function POST(req: Request) {dataStream.writeMessageAnnotation(annotation)// Create the message to save- const generatedMessage: Message = {- role: 'assistant',- content: event.text,- toolInvocations: toolResults,- annotations: [annotation],- id: generateId()- }+ const generatedMessages = [+ ...coreMessages,+ ...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 oneconst savedChat = (await getChat(chatId)) ?? {@@ -89,10 +90,12 @@ export async function POST(req: Request) {id: chatId}+ console.log('generatedMessages', generatedMessages)+// Save chat with complete response and related questionsawait saveChat({...savedChat,- messages: [...savedChat.messages, generatedMessage]+ messages: generatedMessages})}})diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsxindex 0a7bd4c7..0c96bea0 100644--- a/app/search/[id]/page.tsx+++ b/app/search/[id]/page.tsx@@ -1,6 +1,8 @@import { notFound, redirect } from 'next/navigation'import { Chat } from '@/components/chat'import { getChat } from '@/lib/actions/chat'+import { convertToCoreMessages } from 'ai'+import { convertToUIMessages } from '@/lib/utils'export const maxDuration = 60@@ -20,6 +22,8 @@ export default async function SearchPage(props: {const userId = 'anonymous'const { id } = await props.paramsconst chat = await getChat(id, userId)+ // convertToUIMessages for useChat hook+ const messages = convertToUIMessages(chat?.messages || [])if (!chat) {redirect('/')@@ -29,5 +33,7 @@ export default async function SearchPage(props: {notFound()}- return <Chat id={id} />+ console.log(chat)++ return <Chat id={id} savedMessages={messages} />}diff --git a/components/chat.tsx b/components/chat.tsxindex 10369a1f..3df67f1b 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -1,10 +1,16 @@'use client'-import { useChat } from 'ai/react'+import { Message, useChat } from 'ai/react'import { ChatMessages } from './chat-messages'import { ChatPanel } from './chat-panel'-export function Chat({ id }: { id: string }) {+export function Chat({+ id,+ savedMessages = []+}: {+ id: string+ savedMessages?: Message[]+}) {const {messages,input,@@ -15,6 +21,7 @@ export function Chat({ id }: { id: string }) {stop,append} = useChat({+ initialMessages: savedMessages,body: {id},@@ -23,6 +30,8 @@ export function Chat({ id }: { id: string }) {}})+ console.log(JSON.stringify(messages, null, 2))+return (<div className="flex flex-col w-full max-w-3xl pt-10 pb-16 mx-auto stretch"><ChatMessagesdiff --git a/components/search-section.tsx b/components/search-section.tsxindex 90630256..4cadf1a3 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -50,7 +50,7 @@ export function SearchSection({/></Section>)}- {!isLoading && searchResults.results ? (+ {!isLoading && searchResults?.results ? (<Section title="Sources"><SearchResults results={searchResults.results} /></Section>diff --git a/lib/types/index.ts b/lib/types/index.tsindex 9ab2ef21..6ba9f7a7 100644--- a/lib/types/index.ts+++ b/lib/types/index.ts@@ -1,4 +1,4 @@-import { Message } from 'ai'+import { CoreMessage, JSONValue, Message } from 'ai'export type SearchResults = {images: SearchResultImage[]@@ -63,10 +63,16 @@ export interface Chat extends Record<string, any> {createdAt: DateuserId: stringpath: string- messages: Message[] // Note: Changed from AIMessage to Message+ messages: ExtendedCoreMessage[] // Note: Changed from AIMessage to ExtendedCoreMessagesharePath?: string}+// ExtendedCoreMessage for saveing annotations+export type ExtendedCoreMessage = Omit<CoreMessage, 'role' | 'content'> & {+ role: CoreMessage['role'] | 'data'+ content: CoreMessage['content'] | JSONValue+}+export type AIMessage = {role: 'user' | 'assistant' | 'system' | 'function' | 'data' | 'tool'content: stringdiff --git a/lib/utils/index.ts b/lib/utils/index.tsindex 4adfa3c9..115b5d32 100644--- a/lib/utils/index.ts+++ b/lib/utils/index.ts@@ -1,7 +1,15 @@import { type ClassValue, clsx } from 'clsx'import { twMerge } from 'tailwind-merge'-import { CoreMessage } from 'ai'+import {+ CoreMessage,+ CoreToolMessage,+ generateId,+ JSONValue,+ Message,+ ToolInvocation+} from 'ai'import { type Model } from '@/lib/types/models'+import { ExtendedCoreMessage } from '../types'export function cn(...inputs: ClassValue[]) {return twMerge(clsx(inputs))}@@ -46,3 +54,117 @@ export function getDefaultModelId(models: Model[]): string {}return createModelId(models[0])}++function addToolMessageToChat({+ toolMessage,+ messages+}: {+ toolMessage: CoreToolMessage+ messages: Array<Message>+}): Array<Message> {+ return messages.map(message => {+ if (message.toolInvocations) {+ return {+ ...message,+ toolInvocations: message.toolInvocations.map(toolInvocation => {+ const toolResult = toolMessage.content.find(+ tool => tool.toolCallId === toolInvocation.toolCallId+ )++ if (toolResult) {+ return {+ ...toolInvocation,+ state: 'result',+ result: toolResult.result+ }+ }++ return toolInvocation+ })+ }+ }++ return message+ })+}++export function convertToUIMessages(+ messages: Array<ExtendedCoreMessage>+): Array<Message> {+ let pendingAnnotations: JSONValue[] = []++ return messages.reduce((chatMessages: Array<Message>, message) => {+ // Handle tool messages+ if (message.role === 'tool') {+ return addToolMessageToChat({+ toolMessage: message as CoreToolMessage,+ messages: chatMessages+ })+ }++ // Store data message content for next assistant message+ if (message.role === 'data') {+ if (+ message.content !== null &&+ message.content !== undefined &&+ typeof message.content !== 'string' &&+ typeof message.content !== 'number' &&+ typeof message.content !== 'boolean'+ ) {+ pendingAnnotations.push(message.content as JSONValue)+ }+ return chatMessages+ }++ let textContent = ''+ let toolInvocations: Array<ToolInvocation> = []++ // Handle message content+ if (message.content) {+ if (typeof message.content === 'string') {+ textContent = message.content+ } else if (Array.isArray(message.content)) {+ for (const content of message.content) {+ if (content && typeof content === 'object' && 'type' in content) {+ if (content.type === 'text' && 'text' in content) {+ textContent += content.text+ } else if (+ content.type === 'tool-call' &&+ 'toolCallId' in content &&+ 'toolName' in content &&+ 'args' in content+ ) {+ toolInvocations.push({+ state: 'call',+ toolCallId: content.toolCallId,+ toolName: content.toolName,+ args: content.args+ } as ToolInvocation)+ }+ }+ }+ }+ }++ // Create new message+ const newMessage: Message = {+ id: generateId(),+ role: message.role,+ content: textContent,+ toolInvocations: toolInvocations.length > 0 ? toolInvocations : undefined,+ // Add pending annotations if this is an assistant message+ ...(message.role === 'assistant' && pendingAnnotations.length > 0+ ? { annotations: pendingAnnotations }+ : {})+ }++ chatMessages.push(newMessage)++ // Clear pending annotations after adding them+ if (message.role === 'assistant') {+ pendingAnnotations = []+ }++ return chatMessages+ }, [])+}