From dec1f45d1c8f0eca1be1ebc283c40fbf663d4e19 Mon Sep 17 00:00:00 2001 From: "A.J. Shulman" Date: Mon, 21 Apr 2025 13:04:28 -0400 Subject: improved error handling in agent --- .../views/nodes/chatbot/agentsystem/Agent.ts | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts index b166254bb..43faf5bf4 100644 --- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts +++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts @@ -103,7 +103,14 @@ export class Agent { // Check if the question exceeds the maximum length if (question.length > MAX_QUERY_LENGTH) { - return { role: ASSISTANT_ROLE.ASSISTANT, content: [{ text: 'User query too long. Please shorten your question and try again.', index: 0, type: TEXT_TYPE.NORMAL, citation_ids: null }], processing_info: [] }; + const errorText = `Your query is too long (${question.length} characters). Please shorten it to ${MAX_QUERY_LENGTH} characters or less and try again.`; + console.warn(errorText); // Log the specific reason + return { + role: ASSISTANT_ROLE.ASSISTANT, + // Use ERROR type for clarity in the UI if handled differently + content: [{ text: errorText, index: 0, type: TEXT_TYPE.ERROR, citation_ids: null }], + processing_info: [], + }; } const sanitizedQuestion = escape(question); // Sanitized user input @@ -210,9 +217,25 @@ export class Agent { console.log(observation); this.interMessages.push({ role: 'user', content: nextPrompt }); this.processingNumber++; + console.log(`Tool ${currentAction} executed successfully. Observations:`, observation); + break; } catch (error) { - throw new Error(`Error processing action: ${error}`); + console.error(`Error during execution of tool '${currentAction}':`, error); + const errorMessage = error instanceof Error ? error.message : String(error); + // Return an error observation formatted for the LLM loop + return { + role: ASSISTANT_ROLE.USER, + content: [ + { + type: TEXT_TYPE.ERROR, + text: `Execution failed: ${escape(errorMessage)}`, + index: 0, + citation_ids: null, + }, + ], + processing_info: [], + }; } } else { throw new Error('Error: Action input without a valid action'); -- cgit v1.2.3-70-g09d2