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
22pub trait TaskType: Send + Sync + Display {
24 fn name(&self) -> &str;
26
27 fn description(&self) -> &str;
29}
30
31#[async_trait::async_trait]
33pub trait Tool: Send + Sync {
34 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(¶m) {
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#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct ToolInput {
70 pub task_type: String,
72 pub content: String,
74 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#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct ToolOutput {
91 pub result: serde_json::Value,
93 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}