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 Miura7b50ec1b2087
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex e267fc9b..82ea1e22 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -10,6 +10,7 @@ import { generateRelatedQuestions } from '@/lib/agents/generate-related-questionimport { cookies } from 'next/headers'import { getChat, saveChat } from '@/lib/actions/chat'import { ExtendedCoreMessage } from '@/lib/types'+import { convertToExtendedCoreMessages } from '@/lib/utils'export const maxDuration = 30@@ -18,7 +19,11 @@ const DEFAULT_MODEL = 'openai:gpt-4o-mini'export async function POST(req: Request) {const { messages, id: chatId } = await req.json()+ // streamText requires core messagesconst coreMessages = convertToCoreMessages(messages)+ // convertToExtendedCoreMessages for saving annotations+ const extendedCoreMessages = convertToExtendedCoreMessages(messages)+const cookieStore = await cookies()const modelFromCookie = cookieStore.get('selected-model')?.valueconst model = modelFromCookie || DEFAULT_MODEL@@ -30,15 +35,8 @@ export async function POST(req: Request) {model})- let toolResults: ToolInvocation[] = []const result = streamText({...researcherConfig,- onStepFinish(event) {- // onFinish's event.toolResults is empty. Use onStepFinish to get the tool results.- if (event.stepType === 'initial') {- toolResults = event.toolResults- }- },onFinish: async event => {const responseMessages = event.response.messages@@ -71,7 +69,7 @@ export async function POST(req: Request) {// Create the message to saveconst generatedMessages = [- ...coreMessages,+ ...extendedCoreMessages,...responseMessages.slice(0, -1),{role: 'data',diff --git a/components/chat.tsx b/components/chat.tsxindex 3df67f1b..67a84665 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -26,12 +26,10 @@ export function Chat({id},onFinish: () => {- // window.history.replaceState({}, '', `/search/${id}`)+ window.history.replaceState({}, '', `/search/${id}`)}})- 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/lib/utils/index.ts b/lib/utils/index.tsindex 115b5d32..b7870042 100644--- a/lib/utils/index.ts+++ b/lib/utils/index.ts@@ -1,6 +1,7 @@import { type ClassValue, clsx } from 'clsx'import { twMerge } from 'tailwind-merge'import {+ convertToCoreMessages,CoreMessage,CoreToolMessage,generateId,@@ -168,3 +169,27 @@ export function convertToUIMessages(return chatMessages}, [])}++export function convertToExtendedCoreMessages(+ messages: Message[]+): ExtendedCoreMessage[] {+ const result: ExtendedCoreMessage[] = []++ for (const message of messages) {+ // Convert annotations to data messages+ if (message.annotations && message.annotations.length > 0) {+ message.annotations.forEach(annotation => {+ result.push({+ role: 'data',+ content: annotation+ })+ })+ }++ // Convert current message+ const converted = convertToCoreMessages([message])+ result.push(...converted)+ }++ return result+}