kowalski_core/
tools.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Display;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct ToolParameter {
6    pub name: String,
7    pub description: String,
8    pub required: bool,
9    pub default_value: Option<String>,
10    pub parameter_type: ParameterType,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub enum ParameterType {
15    String,
16    Number,
17    Boolean,
18    Array,
19    Object,
20}
21
22/// Trait for task types that can be executed by tools
23pub trait TaskType: Send + Sync + Display {
24    /// Get the name of the task type
25    fn name(&self) -> &str;
26
27    /// Get the description of the task type
28    fn description(&self) -> &str;
29}
30
31/// A tool that can be executed by the agent
32#[async_trait::async_trait]
33pub trait Tool: Send + Sync {
34    /// Execute the tool with the given input
35    async fn execute(
36        &mut self,
37        input: ToolInput,
38    ) -> Result<ToolOutput, crate::error::KowalskiError>;
39
40    fn name(&self) -> &str;
41    fn description(&self) -> &str;
42    fn parameters(&self) -> Vec<ToolParameter>;
43
44    fn validate_input(&self, input: &ToolInput) -> Result<(), crate::error::KowalskiError> {
45        let required_params = self
46            .parameters()
47            .iter()
48            .filter(|p| p.required)
49            .map(|p| p.name.clone())
50            .collect::<Vec<_>>();
51
52        if let Some(params) = input.parameters.as_object() {
53            for param in required_params {
54                if !params.contains_key(&param) {
55                    return Err(crate::error::KowalskiError::ToolInvalidInput(format!(
56                        "Missing required parameter: {}",
57                        param
58                    )));
59                }
60            }
61        }
62
63        Ok(())
64    }
65}
66
67/// Input for a tool execution
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct ToolInput {
70    /// The task type to execute
71    pub task_type: String,
72    /// The content to process
73    pub content: String,
74    /// The input parameters for the task
75    pub parameters: serde_json::Value,
76}
77
78impl ToolInput {
79    pub fn new(task_type: String, content: String, parameters: serde_json::Value) -> Self {
80        Self {
81            task_type,
82            content,
83            parameters,
84        }
85    }
86}
87
88/// Output from a tool execution
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct ToolOutput {
91    /// The result of the tool execution
92    pub result: serde_json::Value,
93    /// Any metadata about the execution
94    pub metadata: Option<serde_json::Value>,
95}
96
97impl ToolOutput {
98    pub fn new(result: serde_json::Value, metadata: Option<serde_json::Value>) -> Self {
99        Self { result, metadata }
100    }
101}