diff --git a/typescript/src/agents/amazonBedrockAgent.ts b/typescript/src/agents/amazonBedrockAgent.ts index 21283aa7..58367229 100644 --- a/typescript/src/agents/amazonBedrockAgent.ts +++ b/typescript/src/agents/amazonBedrockAgent.ts @@ -90,16 +90,7 @@ export class AmazonBedrockAgent extends Agent { } catch (err) { // Handle errors encountered while invoking the Amazon Bedrock agent Logger.logger.error(err); - - // Return a default error message as a fallback response - return { - role: ParticipantRole.ASSISTANT, - content: [ - { - text: "Sorry, I encountered an error while processing your request.", - }, - ], - }; + throw err; } } } diff --git a/typescript/src/agents/chainAgent.ts b/typescript/src/agents/chainAgent.ts index 49e0533b..0a319f28 100644 --- a/typescript/src/agents/chainAgent.ts +++ b/typescript/src/agents/chainAgent.ts @@ -41,13 +41,13 @@ export class ChainAgent extends Agent { let currentInput = inputText; let finalResponse: ConversationMessage | AsyncIterable; - + console.log(`Processing chain with ${this.agents.length} agents`); - + for (let i = 0; i < this.agents.length; i++) { const isLastAgent = i === this.agents.length - 1; const agent = this.agents[i]; - + try { console.log(`Input for agent ${i}: ${currentInput}`); const response = await agent.processRequest( @@ -57,7 +57,7 @@ export class ChainAgent extends Agent { chatHistory, additionalParams ); - + if (this.isConversationMessage(response)) { if (response.content.length > 0 && 'text' in response.content[0]) { currentInput = response.content[0].text; @@ -78,7 +78,7 @@ export class ChainAgent extends Agent { Logger.logger.warn(`Agent ${agent.name} returned an invalid response type.`); return this.createDefaultResponse(); } - + // If it's not the last agent, ensure we have a non-streaming response to pass to the next agent if (!isLastAgent && !this.isConversationMessage(finalResponse)) { Logger.logger.error(`Expected non-streaming response from intermediate agent ${agent.name}`); @@ -86,17 +86,17 @@ export class ChainAgent extends Agent { } } catch (error) { Logger.logger.error(`Error processing request with agent ${agent.name}:`, error); - return this.createDefaultResponse(); + throw `Error processing request with agent ${agent.name}:${String(error)}`; } } - + return finalResponse; } private isAsyncIterable(obj: any): obj is AsyncIterable { return obj && typeof obj[Symbol.asyncIterator] === 'function'; } - + private isConversationMessage(response: any): response is ConversationMessage { return response && 'role' in response && 'content' in response && Array.isArray(response.content); diff --git a/typescript/src/agents/openAIAgent.ts b/typescript/src/agents/openAIAgent.ts index 51785d3c..9465bfe4 100644 --- a/typescript/src/agents/openAIAgent.ts +++ b/typescript/src/agents/openAIAgent.ts @@ -36,7 +36,7 @@ export class OpenAIAgent extends Agent { this.inferenceConfig = { maxTokens: options.inferenceConfig?.maxTokens ?? DEFAULT_MAX_TOKENS, temperature: options.inferenceConfig?.temperature, - topP: options.inferenceConfig?.topP, + topP: options.inferenceConfig?.topP, stopSequences: options.inferenceConfig?.stopSequences, }; } @@ -49,8 +49,8 @@ export class OpenAIAgent extends Agent { chatHistory: ConversationMessage[], additionalParams?: Record ): Promise> { - - + + const messages = [ ...chatHistory.map(msg => ({ role: msg.role.toLowerCase() as OpenAI.Chat.ChatCompletionMessageParam['role'], @@ -71,7 +71,7 @@ export class OpenAIAgent extends Agent { stop: stopSequences, }; - + if (this.streaming) { return this.handleStreamingResponse(requestOptions); @@ -90,7 +90,7 @@ export class OpenAIAgent extends Agent { } const assistantMessage = chatCompletion.choices[0]?.message?.content; - + if (typeof assistantMessage !== 'string') { throw new Error('Unexpected response format from OpenAI API'); } @@ -101,10 +101,7 @@ export class OpenAIAgent extends Agent { }; } catch (error) { Logger.logger.error('Error in OpenAI API call:', error); - return { - role: ParticipantRole.ASSISTANT, - content: [{ text: 'I encountered an error while processing your request.' }], - }; + throw error; } } @@ -117,8 +114,8 @@ export class OpenAIAgent extends Agent { } } } - - + + } \ No newline at end of file diff --git a/typescript/src/orchestrator.ts b/typescript/src/orchestrator.ts index b1948884..9939fe71 100644 --- a/typescript/src/orchestrator.ts +++ b/typescript/src/orchestrator.ts @@ -58,7 +58,7 @@ export interface OrchestratorConfig { /** * The message to display when no agent is selected to handle the user's request. - * + * * This message is shown when the classifier couldn't determine an appropriate agent * and USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED is set to false. */ @@ -66,7 +66,7 @@ export interface OrchestratorConfig { /** * The general error message to display when an error occurs during request routing. - * + * * This message is shown when an unexpected error occurs during the processing of a user's request, * such as errors in agent dispatch or processing. */ @@ -217,15 +217,11 @@ export class MultiAgentOrchestrator { USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED: options.config?.USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED ?? DEFAULT_CONFIG.USE_DEFAULT_AGENT_IF_NONE_IDENTIFIED, - CLASSIFICATION_ERROR_MESSAGE: - options.config?.CLASSIFICATION_ERROR_MESSAGE ?? - DEFAULT_CONFIG.CLASSIFICATION_ERROR_MESSAGE, + CLASSIFICATION_ERROR_MESSAGE: options.config?.CLASSIFICATION_ERROR_MESSAGE, NO_SELECTED_AGENT_MESSAGE: options.config?.NO_SELECTED_AGENT_MESSAGE ?? DEFAULT_CONFIG.NO_SELECTED_AGENT_MESSAGE, - GENERAL_ROUTING_ERROR_MSG_MESSAGE: - options.config?.GENERAL_ROUTING_ERROR_MSG_MESSAGE ?? - DEFAULT_CONFIG.GENERAL_ROUTING_ERROR_MSG_MESSAGE, + GENERAL_ROUTING_ERROR_MSG_MESSAGE: options.config?.GENERAL_ROUTING_ERROR_MSG_MESSAGE }; this.executionTimes = new Map(); @@ -349,7 +345,7 @@ export class MultiAgentOrchestrator { this.executionTimes = new Map(); let classifierResult: ClassifierResult; const chatHistory = (await this.storage.fetchAllChats(userId, sessionId)) || []; - + try { classifierResult = await this.measureExecutionTime( "Classifying user intent", @@ -361,7 +357,7 @@ export class MultiAgentOrchestrator { this.logger.error("Error during intent classification:", error); return { metadata: this.createMetadata(null, userInput, userId, sessionId, additionalParams), - output: this.config.CLASSIFICATION_ERROR_MESSAGE, + output: this.config.CLASSIFICATION_ERROR_MESSAGE ? this.config.CLASSIFICATION_ERROR_MESSAGE:error, streaming: false, }; } @@ -379,7 +375,7 @@ export class MultiAgentOrchestrator { }; } } - + try { const agentResponse = await this.dispatchToAgent({ userInput, @@ -388,9 +384,9 @@ export class MultiAgentOrchestrator { classifierResult, additionalParams, }); - + const metadata = this.createMetadata(classifierResult, userInput, userId, sessionId, additionalParams); - + if (this.isAsyncIterable(agentResponse)) { const accumulatorTransform = new AccumulatorTransform(); this.processStreamInBackground( @@ -407,7 +403,7 @@ export class MultiAgentOrchestrator { streaming: true, }; } - + // Check if we should save the conversation if (classifierResult?.selectedAgent.saveChat) { await saveConversationExchange( @@ -431,7 +427,7 @@ export class MultiAgentOrchestrator { this.logger.error("Error during agent dispatch or processing:", error); return { metadata: this.createMetadata(classifierResult, userInput, userId, sessionId, additionalParams), - output: this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE, + output: this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE ? this.config.GENERAL_ROUTING_ERROR_MSG_MESSAGE: error, streaming: false, }; } finally { @@ -469,7 +465,7 @@ export class MultiAgentOrchestrator { if (fullResponse) { - + if (agent.saveChat) { await saveConversationExchange( userInput, @@ -480,7 +476,7 @@ export class MultiAgentOrchestrator { agent.id ); } - + } else { this.logger.warn("No data accumulated, messages not saved"); }