An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: implement comprehensive error handling for chat functionality
Yoshiki Miura72876b0cff1d
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex a2a9ef2b..7592582f 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -17,6 +17,8 @@ const DEFAULT_MODEL: Model = {}export async function POST(req: Request) {+ const abortSignal = req.signal+try {const body = await req.json()const { message, chatId, trigger, messageId } = body@@ -83,7 +85,8 @@ export async function POST(req: Request) {searchMode,userId: userId!,trigger,- messageId+ messageId,+ abortSignal})} catch (error) {console.error('API route error:', error)diff --git a/components/chat-error.tsx b/components/chat-error.tsxnew file mode 100644index 00000000..c5aeab9f--- /dev/null+++ b/components/chat-error.tsx@@ -0,0 +1,22 @@+import { AlertCircle } from 'lucide-react'++import { Card } from '@/components/ui/card'++interface ChatErrorProps {+ error: Error | string | null | undefined+}++export function ChatError({ error }: ChatErrorProps) {+ if (!error) return null++ const errorMessage = error instanceof Error ? error.message : error++ return (+ <Card className="border-destructive bg-destructive/10 p-4">+ <div className="flex items-center gap-3">+ <AlertCircle className="h-5 w-5 text-destructive" />+ <p className="text-sm text-destructive">{errorMessage}</p>+ </div>+ </Card>+ )+}diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex cfd69e69..d9e4e8a4 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -7,6 +7,7 @@ import { UseChatHelpers } from '@ai-sdk/react'import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'import { cn } from '@/lib/utils'+import { ChatError } from './chat-error'import { DefaultSkeleton } from './default-skeleton'import { RenderMessage } from './render-message'@@ -27,6 +28,7 @@ interface ChatMessagesProps {scrollContainerRef: React.RefObject<HTMLDivElement>onUpdateMessage?: (messageId: string, newContent: string) => Promise<void>reload?: (messageId: string) => Promise<void | string | null | undefined>+ error?: Error | string | null | undefined}export function ChatMessages({@@ -37,7 +39,8 @@ export function ChatMessages({addToolResult,scrollContainerRef,onUpdateMessage,- reload+ reload,+ error}: ChatMessagesProps) {// Track user-modified states (when user explicitly opens/closes)const [userModifiedStates, setUserModifiedStates] = useState<@@ -199,6 +202,7 @@ export function ChatMessages({/></div>))}+ <ChatError error={error} /></div>))}</div>diff --git a/components/chat.tsx b/components/chat.tsxindex bab37337..7a2e677a 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -5,11 +5,10 @@ import { useEffect, useMemo, useRef, useState } from 'react'import { useChat } from '@ai-sdk/react'import { DefaultChatTransport } from 'ai'import { toast } from 'sonner'-import { z } from 'zod'import { generateId } from '@/lib/db/schema'import { UploadedFile } from '@/lib/types'-import type { UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import type { UIMessage } from '@/lib/types/ai'import {isDynamicToolPart,isToolCallPart,@@ -54,7 +53,8 @@ export function Chat({stop,sendMessage,regenerate,- addToolResult+ addToolResult,+ error} = useChat({id, // use the provided chatIdtransport: new DefaultChatTransport({@@ -332,6 +332,7 @@ export function Chat({scrollContainerRef={scrollContainerRef}onUpdateMessage={handleUpdateAndReloadMessage}reload={handleReloadFrom}+ error={error}/><ChatPanelchatId={id}diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 945d50ed..ae2e7262 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -11,7 +11,7 @@ import type { UIMessage } from '@/lib/types/ai'import { getTextFromParts } from '@/lib/utils/message-utils'// Constants-const DEFAULT_CHAT_TITLE = 'New Chat'+const DEFAULT_CHAT_TITLE = 'Untitled'/*** Get all chats for the current userdiff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.tsindex 21dd9a6c..0a743606 100644--- a/lib/agents/generate-related-questions.ts+++ b/lib/agents/generate-related-questions.ts@@ -3,7 +3,10 @@ import { Experimental_Agent as Agent, tool } from 'ai'import { relatedSchema } from '../schema/related'import { getModel } from '../utils/registry'-export function generateRelatedQuestions(model: string) {+export function generateRelatedQuestions(+ model: string,+ abortSignal?: AbortSignal+) {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@@ -48,6 +51,7 @@ Bad follow-ups (avoid these):})},activeTools: ['relatedQuestions'],- toolChoice: 'required'+ toolChoice: 'required',+ abortSignal})}diff --git a/lib/agents/researcher.ts b/lib/agents/researcher.tsindex 6dd22f92..8b69954e 100644--- a/lib/agents/researcher.ts+++ b/lib/agents/researcher.ts@@ -51,10 +51,12 @@ Citation Format:export function researcher({model,- searchMode+ searchMode,+ abortSignal}: {model: stringsearchMode: boolean+ abortSignal?: AbortSignal}) {try {const currentDate = new Date().toLocaleString()@@ -73,7 +75,8 @@ export function researcher({askQuestion: askQuestionTool},activeTools: searchMode ? ['search', 'fetch'] : undefined,- stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1)+ stopWhen: searchMode ? stepCountIs(10) : stepCountIs(1),+ abortSignal})} catch (error) {console.error('Error in researcher:', error)diff --git a/lib/agents/title-generator.ts b/lib/agents/title-generator.tsindex fdec690d..f415ef35 100644--- a/lib/agents/title-generator.ts+++ b/lib/agents/title-generator.ts@@ -5,6 +5,7 @@ import { getModel } from '../utils/registry'interface GenerateChatTitleParams {userMessageContent: stringmodelId: string+ abortSignal?: AbortSignal}/**@@ -15,7 +16,8 @@ interface GenerateChatTitleParams {*/export async function generateChatTitle({userMessageContent,- modelId+ modelId,+ abortSignal}: GenerateChatTitleParams): Promise<string> {// Fallback title uses the first 75 characters of the message or a default string.const fallbackTitle = userMessageContent.substring(0, 75).trim() || 'New Chat'@@ -26,7 +28,8 @@ export async function generateChatTitle({const { text: generatedTitle } = await generateText({model: getModel(modelId),system: systemPrompt,- prompt: userMessageContent+ prompt: userMessageContent,+ abortSignal})const cleanedTitle = generatedTitle.trim()diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 9083a2e6..2c029c42 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -1,4 +1,5 @@import {+ consumeStream,convertToModelMessages,createUIMessageStream,createUIMessageStreamResponse,@@ -32,13 +33,21 @@ import { retryDatabaseOperation } from '../utils/retry'import { BaseStreamConfig } from './types'// Constants-const DEFAULT_CHAT_TITLE = 'New Chat'+const DEFAULT_CHAT_TITLE = 'Untitled'export async function createChatStreamResponse(config: BaseStreamConfig): Promise<Response> {- const { message, model, chatId, searchMode, userId, trigger, messageId } =- config+ const {+ message,+ model,+ chatId,+ searchMode,+ userId,+ trigger,+ messageId,+ abortSignal+ } = configconst modelId = `${model.providerId}:${model.id}`// Verify that chatId is provided@@ -60,6 +69,11 @@ export async function createChatStreamResponse(})}+ // Track variables for abort handling+ let researchMessage: UIMessage | null = null+ let hasStartedProcessing = false+ let wasAborted = false+// Create the streamconst stream = createUIMessageStream({execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {@@ -157,7 +171,8 @@ export async function createChatStreamResponse(// Get the researcher agentconst researchAgent = researcher({model: modelId,- searchMode+ searchMode,+ abortSignal})// Convert to model messages and apply context window management@@ -180,16 +195,19 @@ export async function createChatStreamResponse(messages: modelMessages})- // Store messages for merging- let researchMessage: UIMessage | null = null+ // Messages are tracked at the top level nowlet relatedQuestionsMessage: UIMessage | null = null+ hasStartedProcessing = true// Create a promise to track when research finishesconst researchPromise = new Promise<void>(resolve => {writer.merge(researchResult.toUIMessageStream({sendFinish: false,- onFinish: ({ responseMessage }) => {+ onFinish: ({ responseMessage, isAborted }) => {+ if (isAborted) {+ wasAborted = true+ }researchMessage = responseMessageresolve()}@@ -206,6 +224,12 @@ export async function createChatStreamResponse(return}+ // Check if aborted during research+ if (wasAborted) {+ writer.write({ type: 'finish' })+ return+ }+// At this point, TypeScript should know researchMessage is not nullconst validResearchMessage: UIMessage = researchMessage@@ -225,7 +249,8 @@ export async function createChatStreamResponse(saveOperations.push(() =>generateChatTitle({userMessageContent: userContent,- modelId+ modelId,+ abortSignal}).then(async title => {const { updateChatTitle } = await import('../db/actions')return updateChatTitle(chatId, title)@@ -234,6 +259,7 @@ export async function createChatStreamResponse(}// Execute saves in background with exponential backoff retry+ // Note: This is intentionally not awaited to avoid blocking the streamPromise.all(saveOperations.map(op => op())).catch(async error => {console.error('Error saving message or title:', error)@@ -275,7 +301,10 @@ export async function createChatStreamResponse(]// Get related questions agent- const relatedQuestionsAgent = generateRelatedQuestions(modelId)+ const relatedQuestionsAgent = generateRelatedQuestions(+ modelId,+ abortSignal+ )const relatedQuestionsResult = relatedQuestionsAgent.stream({messages: allMessages})@@ -285,7 +314,10 @@ export async function createChatStreamResponse(writer.merge(relatedQuestionsResult.toUIMessageStream({sendStart: false,- onFinish: ({ responseMessage }) => {+ onFinish: ({ responseMessage, isAborted }) => {+ if (isAborted) {+ wasAborted = true+ }relatedQuestionsMessage = responseMessageresolve()}@@ -296,6 +328,12 @@ export async function createChatStreamResponse(// Wait for related questions to completeawait relatedQuestionsPromise+ // Check if aborted during related questions+ if (wasAborted) {+ writer.write({ type: 'finish' })+ return+ }+// Save the complete message after both agents finishconst saveOperations: (() => Promise<any>)[] = []@@ -316,7 +354,8 @@ export async function createChatStreamResponse(saveOperations.push(() =>generateChatTitle({userMessageContent: userContent,- modelId+ modelId,+ abortSignal}).then(async title => {const { updateChatTitle } = await import('../db/actions')return updateChatTitle(chatId, title)@@ -325,6 +364,7 @@ export async function createChatStreamResponse(}// Execute saves in background with exponential backoff retry+ // Note: This is intentionally not awaited to avoid blocking the streamPromise.all(saveOperations.map(op => op())).catch(async error => {console.error('Error saving message or title:', error)@@ -358,5 +398,8 @@ export async function createChatStreamResponse(}})- return createUIMessageStreamResponse({ stream })+ return createUIMessageStreamResponse({+ stream,+ consumeSseStream: consumeStream+ })}diff --git a/lib/streaming/types.ts b/lib/streaming/types.tsindex ff6f6a55..87ce38e9 100644--- a/lib/streaming/types.ts+++ b/lib/streaming/types.ts@@ -10,4 +10,5 @@ export interface BaseStreamConfig {userId: stringtrigger?: 'submit-user-message' | 'regenerate-assistant-message'messageId?: string+ abortSignal?: AbortSignal}