use super::executor::FunctionCallingHandler;
use super::types::ToolCall;
use crate::core::models::openai::{ChatMessage, MessageContent, MessageRole};
use crate::utils::error::gateway_error::{GatewayError, Result};
use serde_json::Value;
use tracing::{error, warn};
impl FunctionCallingHandler {
pub async fn process_tool_calls(&self, tool_calls: &[ToolCall]) -> Result<Vec<ChatMessage>> {
let mut tool_responses = Vec::new();
for tool_call in tool_calls {
if tool_call.tool_type != "function" {
warn!("Unsupported tool type: {}", tool_call.tool_type);
continue;
}
let function_name = &tool_call.function.name;
if let Some(executor) = self.executors.get(function_name) {
let arguments: Value = match serde_json::from_str(&tool_call.function.arguments) {
Ok(args) => args,
Err(e) => {
error!("Failed to parse function arguments: {}", e);
return Err(GatewayError::Validation(format!(
"Invalid function arguments: {}",
e
)));
}
};
if let Err(e) = executor.validate_arguments(&arguments) {
error!("Function argument validation failed: {}", e);
return Err(e);
}
match executor.execute(arguments).await {
Ok(result) => {
let tool_message = ChatMessage {
role: MessageRole::Tool,
content: Some(MessageContent::Text(result.to_string())),
name: Some(function_name.clone()),
function_call: None,
tool_calls: None,
tool_call_id: None,
audio: None,
};
tool_responses.push(tool_message);
}
Err(e) => {
error!("Function execution failed: {}", e);
let error_message = ChatMessage {
role: MessageRole::Tool,
content: Some(MessageContent::Text(format!("Error: {}", e))),
name: Some(function_name.clone()),
function_call: None,
tool_calls: None,
tool_call_id: None,
audio: None,
};
tool_responses.push(error_message);
}
}
} else {
warn!("Unknown function: {}", function_name);
let error_message = ChatMessage {
role: MessageRole::Tool,
content: Some(MessageContent::Text(format!(
"Unknown function: {}",
function_name
))),
name: Some(function_name.clone()),
function_call: None,
tool_calls: None,
tool_call_id: None,
audio: None,
};
tool_responses.push(error_message);
}
}
Ok(tool_responses)
}
}