use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::context::block::{TextPayload, ToolCallPayload};
use crate::context::ids::BlockId;
use crate::context::tool_data::{ArtifactKind, ArtifactRef, ToolResultPayload};
use crate::context::tools::ToolDefinition;
use crate::ports::control::CallControl;
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ToolSurface {
pub definitions: Vec<ToolDefinition>,
}
impl ToolSurface {
pub fn empty() -> Self {
Self {
definitions: Vec::new(),
}
}
pub fn from_definitions(definitions: Vec<ToolDefinition>) -> Self {
Self { definitions }
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ToolHints {
pub read_only: Option<bool>,
pub destructive: Option<bool>,
pub idempotent: Option<bool>,
pub open_world: Option<bool>,
}
impl ToolHints {
pub fn with_read_only(mut self, read_only: bool) -> Self {
self.read_only = Some(read_only);
self
}
pub fn with_destructive(mut self, destructive: bool) -> Self {
self.destructive = Some(destructive);
self
}
pub fn with_idempotent(mut self, idempotent: bool) -> Self {
self.idempotent = Some(idempotent);
self
}
pub fn with_open_world(mut self, open_world: bool) -> Self {
self.open_world = Some(open_world);
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ToolDescription {
pub definition: ToolDefinition,
pub hints: ToolHints,
}
impl ToolDescription {
pub fn new(definition: ToolDefinition) -> Self {
Self {
definition,
hints: ToolHints::default(),
}
}
pub fn with_hints(mut self, hints: ToolHints) -> Self {
self.hints = hints;
self
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ToolCallContext {
pub call_block_id: BlockId,
pub input: ToolCallPayload,
pub result_notes: Vec<TextPayload>,
}
impl ToolCallContext {
pub fn new(call_block_id: BlockId, input: ToolCallPayload) -> Self {
Self {
call_block_id,
input,
result_notes: Vec::new(),
}
}
}
impl ToolCallContext {
pub fn from_declaration(call_block_id: BlockId, declaration: &ToolCallPayload) -> Self {
Self {
call_block_id,
input: declaration.clone(),
result_notes: Vec::new(),
}
}
}
#[non_exhaustive]
pub struct ArtifactHint {
pub tool_name: String,
pub call_block_id: BlockId,
pub kind: ArtifactKind,
}
impl ArtifactHint {
pub fn new(tool_name: impl Into<String>, call_block_id: BlockId, kind: ArtifactKind) -> Self {
Self {
tool_name: tool_name.into(),
call_block_id,
kind,
}
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
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;
fn hints(&self) -> ToolHints {
ToolHints::default()
}
async fn execute(
&self,
ctx: &ToolCallContext,
control: &CallControl,
store: Option<&dyn ArtifactStore>,
) -> ToolResultPayload;
}