An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix error handling in submit and writer functions
Yoshiki Miura91e36572c6cd
diff --git a/app/actions.tsx b/app/actions.tsxindex 091ea457..0e9bca88 100644--- a/app/actions.tsx+++ b/app/actions.tsx@@ -142,6 +142,8 @@ async function submit(formData?: FormData, skip?: boolean) {toolOutputs = toolResponseserrorOccurred = hasError+ console.log('response: ' + fullResponse, 'error: ' + hasError)+if (toolOutputs.length > 0) {toolOutputs.map(output => {aiState.update({@@ -175,7 +177,13 @@ async function submit(formData?: FormData, skip?: boolean) {: msg) as CoreMessage[]const latestMessages = modifiedMessages.slice(maxMessages * -1)- answer = await writer(uiStream, streamText, latestMessages)+ const { response, hasError } = await writer(+ uiStream,+ streamText,+ latestMessages+ )+ answer = response+ errorOccurred = hasError} else {streamText.done()}diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsxindex b655510f..7ca6a921 100644--- a/lib/agents/researcher.tsx+++ b/lib/agents/researcher.tsx@@ -41,8 +41,17 @@ export async function researcher(uiStream,fullResponse})+ }).catch(err => {+ hasError = true+ fullResponse = 'Error: ' + err.message+ streamText.update(fullResponse)})+ // If the result is not available, return an error response+ if (!result) {+ return { result, fullResponse, hasError, toolResponses: [] }+ }+// Remove the spinneruiStream.update(null)@@ -77,6 +86,7 @@ export async function researcher(toolResponses.push(delta)breakcase 'error':+ console.log('Error: ' + delta.error)hasError = truefullResponse += `\nError occurred while executing the tool`breakdiff --git a/lib/agents/writer.tsx b/lib/agents/writer.tsxindex db3968cd..9c99aef5 100644--- a/lib/agents/writer.tsx+++ b/lib/agents/writer.tsx@@ -10,6 +10,7 @@ export async function writer(messages: CoreMessage[]) {let fullResponse = ''+ let hasError = falseconst answerSection = (<Section title="Answer"><BotMessage content={streamText.value} />@@ -42,9 +43,14 @@ export async function writer(}}})+ .catch(err => {+ hasError = true+ fullResponse = 'Error: ' + err.message+ streamText.update(fullResponse)+ }).finally(() => {streamText.done()})- return fullResponse+ return { response: fullResponse, hasError }}