use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use aither_core::llm::tool::ToolDefinition;
use aither_core::llm::{CallIdentity, ToolResult};
use serde_json::Value;
pub trait CommandSet: Send + Sync + 'static {
fn definitions(&self) -> Vec<ToolDefinition>;
fn call(
&self,
name: &str,
call: Option<CallIdentity>,
arguments: Value,
) -> impl Future<Output = Result<ToolResult, String>> + Send;
}
trait CommandSetImpl: Send + Sync {
fn definitions(&self) -> Vec<ToolDefinition>;
fn call<'a>(
&'a self,
name: &'a str,
call: Option<CallIdentity>,
arguments: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, String>> + Send + 'a>>;
}
impl<T: CommandSet> CommandSetImpl for T {
fn definitions(&self) -> Vec<ToolDefinition> {
CommandSet::definitions(self)
}
fn call<'a>(
&'a self,
name: &'a str,
call: Option<CallIdentity>,
arguments: Value,
) -> Pin<Box<dyn Future<Output = Result<ToolResult, String>> + Send + 'a>> {
Box::pin(CommandSet::call(self, name, call, arguments))
}
}
#[derive(Clone)]
pub struct AnyCommandSet(Arc<dyn CommandSetImpl>);
impl AnyCommandSet {
#[must_use]
pub fn new(set: impl CommandSet) -> Self {
Self(Arc::new(set))
}
#[must_use]
pub fn definitions(&self) -> Vec<ToolDefinition> {
self.0.definitions()
}
pub fn call<'a>(
&'a self,
name: &'a str,
call: Option<CallIdentity>,
arguments: Value,
) -> impl Future<Output = Result<ToolResult, String>> + Send + 'a {
self.0.call(name, call, arguments)
}
}
impl std::fmt::Debug for AnyCommandSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("AnyCommandSet")
}
}