better error handling on agent processing

This commit is contained in:
dal 2025-04-20 08:35:55 -06:00
parent 4d071843df
commit 77a21b37ce
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
2 changed files with 63 additions and 17 deletions

View File

@ -695,6 +695,9 @@ impl Agent {
{
Ok(rx) => rx,
Err(e) => {
// --- Added Error Handling ---
let error_message = format!("Error starting LLM stream: {:?}", e);
tracing::error!(agent_name = %agent.name, chat_id = %agent.session_id, user_id = %agent.user_id, "{}", error_message);
// Log error in span
if let Some(parent_span) = parent_span.clone() {
if let Some(client) = &*BRAINTRUST_CLIENT {
@ -708,8 +711,8 @@ impl Agent {
}
}
}
let error_message = format!("Error starting stream: {:?}", e);
return Err(anyhow::anyhow!(error_message));
// --- End Added Error Handling ---
return Err(anyhow::anyhow!(error_message)); // Return immediately
}
};
@ -767,6 +770,9 @@ impl Agent {
}
}
Err(e) => {
// --- Added Error Handling ---
let error_message = format!("Error receiving chunk from LLM stream: {:?}", e);
tracing::error!(agent_name = %agent.name, chat_id = %agent.session_id, user_id = %agent.user_id, "{}", error_message);
// Log error in parent span
if let Some(parent) = &parent_for_tool_spans {
if let Some(client) = &*BRAINTRUST_CLIENT {
@ -784,8 +790,8 @@ impl Agent {
}
}
}
let error_message = format!("Error in stream: {:?}", e);
return Err(anyhow::anyhow!(error_message));
// --- End Added Error Handling ---
return Err(anyhow::anyhow!(error_message)); // Return immediately
}
}
}
@ -926,6 +932,12 @@ impl Agent {
{
Ok(r) => r,
Err(e) => {
// --- Added Error Handling ---
let error_message = format!(
"Tool execution error for {}: {:?}",
tool_call.function.name, e
);
tracing::error!(agent_name = %agent.name, chat_id = %agent.session_id, user_id = %agent.user_id, tool_name = %tool_call.function.name, "{}", error_message);
// Log error in tool span
if let Some(tool_span) = &tool_span {
if let Some(client) = &*BRAINTRUST_CLIENT {
@ -945,12 +957,13 @@ impl Agent {
}
}
}
// --- End Added Error Handling ---
let error_message = format!(
"Tool execution error for {}: {:?}",
tool_call.function.name, e
);
error!("{}", error_message); // Log locally
return Err(anyhow::anyhow!(error_message));
return Err(anyhow::anyhow!(error_message)); // Return immediately
}
};
@ -1044,6 +1057,9 @@ impl Agent {
// Update thread and push the error result for the next LLM call
agent.update_current_thread(error_result.clone()).await?;
// Continue processing other tool calls if any
// --- Added: Consider returning error here if hallucinated tool is fatal ---
// return Err(anyhow::anyhow!(err_msg)); // Uncomment if hallucinated tools should stop processing
// --- End Added ---
}
}

View File

@ -729,19 +729,49 @@ pub async fn post_chat_handler(
}
}
Err(e) => {
// If we have a tx channel, send the error
if let Some(tx) = &tx {
let _ = tx
.send(Err(anyhow!("Error receiving message from agent: {}", e)))
.await;
}
// --- Modified Error Handling ---
// Log the original error for debugging
tracing::error!(
chat_id = %chat_id,
message_id = %message_id,
"Error received from agent stream: {}",
e
);
tracing::error!("Error receiving message from agent: {}", e);
// --- Update timestamp before breaking ---
// No update needed here
// --- End Update timestamp ---
// Don't return early, continue processing remaining messages
break;
// If we have a tx channel, send a user-friendly apology message
if let Some(tx) = &tx {
let apology_text = "I apologize, but I encountered an issue while processing your request. Please try again or rephrase your message.".to_string();
let apology_message = BusterChatMessage::Text {
id: Uuid::new_v4().to_string(), // Unique ID for this apology message
message: Some(apology_text),
message_chunk: None,
is_final_message: Some(true),
originating_tool_name: None,
};
let apology_container = BusterContainer::ChatMessage(BusterChatMessageContainer {
response_message: apology_message,
chat_id,
message_id,
});
// Send the apology, log if sending fails
if let Err(send_err) = tx
.send(Ok((
apology_container,
ThreadEvent::GeneratingResponseMessage,
)))
.await
{
tracing::warn!(
"Failed to send apology message to client (channel closed?): {}",
send_err
);
}
}
// Do not break the loop here. Let the agent send Done or the channel close naturally.
// The error in agent.rs should have already stopped its processing.
// --- End Modified Error Handling ---
}
}
}