use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::context::tool_data::{ArtifactKind, ArtifactRef, ToolCallId, ToolResultPayload};
use crate::ports::control::CallControl;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolDefinition {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
#[derive(Debug, Clone)]
pub struct ToolCallContext {
pub call_id: ToolCallId,
pub tool_name: String,
pub arguments: serde_json::Value,
}
pub struct ArtifactHint {
pub tool_name: String,
pub call_id: ToolCallId,
pub kind: ArtifactKind,
}
#[derive(Debug, thiserror::Error)]
pub enum StoreError {
#[error("persist failed: {0}")]
Persist(String),
#[error("read failed: {0}")]
Read(String),
}
#[async_trait]
pub trait ArtifactStore: Send + Sync {
async fn persist(&self, data: &[u8], hint: ArtifactHint) -> Result<ArtifactRef, StoreError>;
async fn read(
&self,
id: &str,
range: Option<std::ops::Range<u64>>,
) -> Result<Vec<u8>, StoreError>;
}
#[async_trait]
pub trait Tool: Send + Sync {
fn definition(&self) -> ToolDefinition;
async fn execute(&self, ctx: &ToolCallContext, control: &CallControl) -> ToolResultPayload;
async fn execute_with_store(
&self,
ctx: &ToolCallContext,
control: &CallControl,
store: Option<&dyn ArtifactStore>,
) -> ToolResultPayload {
let _ = store;
self.execute(ctx, control).await
}
}