use std::sync::Arc;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio_util::sync::CancellationToken;
use crate::ai::types::{ToolResultContent, ToolSchema};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ExecutionMode {
Sequential,
#[default]
Parallel,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolResult {
pub content: Vec<ToolResultContent>,
#[serde(default)]
pub details: Value,
#[serde(default)]
pub terminate: bool,
}
impl ToolResult {
pub fn text(text: impl Into<String>) -> Self {
Self {
content: vec![ToolResultContent::text(text)],
details: Value::Null,
terminate: false,
}
}
pub fn error(message: impl Into<String>) -> Self {
Self::text(message)
}
pub fn with_details(mut self, details: Value) -> Self {
self.details = details;
self
}
}
#[async_trait]
pub trait AgentTool: Send + Sync {
fn name(&self) -> &str;
fn label(&self) -> &str {
self.name()
}
fn description(&self) -> &str;
fn input_schema(&self) -> Value;
fn execution_mode(&self) -> ExecutionMode {
ExecutionMode::Parallel
}
fn prepare_arguments(&self, args: Value) -> Value {
args
}
async fn execute(
&self,
args: Value,
cancel: CancellationToken,
) -> anyhow::Result<ToolResult>;
fn schema(&self) -> ToolSchema {
ToolSchema {
name: self.name().to_string(),
description: self.description().to_string(),
input_schema: self.input_schema(),
}
}
}
pub fn validate_required(schema: &Value, args: &Value) -> Result<(), String> {
let Some(required) = schema.get("required").and_then(|v| v.as_array()) else {
return Ok(());
};
let obj = args.as_object().ok_or_else(|| "tool arguments must be an object".to_string())?;
for r in required {
if let Some(key) = r.as_str() {
let present = obj
.get(key)
.map(|v| !v.is_null())
.unwrap_or(false);
if !present {
return Err(format!("missing required parameter: {key}"));
}
}
}
Ok(())
}
pub struct ToolCallContext<'a> {
pub tool_call_id: &'a str,
pub tool_name: &'a str,
pub args: &'a Value,
}
pub struct BeforeToolCallResult {
pub block: bool,
pub reason: Option<String>,
}
impl BeforeToolCallResult {
pub fn allow() -> Self {
Self {
block: false,
reason: None,
}
}
pub fn block(reason: impl Into<String>) -> Self {
Self {
block: true,
reason: Some(reason.into()),
}
}
}
#[async_trait]
pub trait ToolGate: Send + Sync {
async fn before(&self, ctx: ToolCallContext<'_>) -> BeforeToolCallResult;
}
pub struct AllowAllGate;
#[async_trait]
impl ToolGate for AllowAllGate {
async fn before(&self, _ctx: ToolCallContext<'_>) -> BeforeToolCallResult {
BeforeToolCallResult::allow()
}
}
pub fn allow_all_gate() -> Arc<dyn ToolGate> {
Arc::new(AllowAllGate)
}