more refactoring

This commit is contained in:
pavel 2026-02-11 01:41:07 +01:00
commit 7268d49b4a
12 changed files with 319 additions and 189 deletions

View file

@ -1,5 +1,15 @@
use super::api::{self, FunctionDefinition, Message, Tool, ToolCall};
use std::collections::HashMap;
use serde::Deserialize;
#[derive(Deserialize)]
struct GoogleSearchArgs {
query: String,
}
#[derive(Deserialize)]
struct FinishArgs {
result: String,
}
pub fn get_tools() -> Vec<Tool> {
vec![
@ -24,13 +34,13 @@ pub fn get_tools() -> Vec<Tool> {
tool_type: "function".to_string(),
function: FunctionDefinition {
name: "finish".to_string(),
description: "Finish the task".to_string(),
description: "Finish the task and provide a final answer".to_string(),
parameters: serde_json::json!({
"type": "object",
"properties": {
"result": {
"type": "string",
"description": "The result of the task"
"description": "The final detailed answer to the task"
}
},
"required": ["result"]
@ -48,8 +58,8 @@ pub async fn handle_tool_call(
let name = &tool_call.function.name;
let (content, written) = if name == "google_search" {
let args: HashMap<String, String> = serde_json::from_str(&tool_call.function.arguments)?;
let query = args.get("query").ok_or("Missing query argument")?;
let args: GoogleSearchArgs = serde_json::from_str(&tool_call.function.arguments)?;
let query = &args.query;
let search_result = if let Some(key) = tavily_api_key {
match api::perform_search(query, key).await {
@ -61,8 +71,8 @@ pub async fn handle_tool_call(
};
(search_result, false)
} else if name == "finish" {
let args: HashMap<String, String> = serde_json::from_str(&tool_call.function.arguments)?;
let result = args.get("result").ok_or("Missing result argument")?;
let args: FinishArgs = serde_json::from_str(&tool_call.function.arguments)?;
let result = &args.result;
answer = Some(result.clone());
(result.clone(), true)