swiftide_core/chat_completion/
tools.rsuse derive_builder::Builder;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ToolOutput {
Text(String),
Fail(String),
Stop,
}
impl ToolOutput {
pub fn content(&self) -> Option<&str> {
match self {
ToolOutput::Fail(s) | ToolOutput::Text(s) => Some(s),
_ => None,
}
}
}
impl<T: AsRef<str>> From<T> for ToolOutput {
fn from(s: T) -> Self {
ToolOutput::Text(s.as_ref().to_string())
}
}
impl std::fmt::Display for ToolOutput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ToolOutput::Text(value) => write!(f, "{value}"),
ToolOutput::Fail(value) => write!(f, "Tool call failed: {value}"),
ToolOutput::Stop => write!(f, "Stop"),
}
}
}
#[derive(Clone, Debug, Builder, PartialEq)]
#[builder(setter(into, strip_option))]
pub struct ToolCall {
id: String,
name: String,
#[builder(default)]
args: Option<String>,
}
impl std::fmt::Display for ToolCall {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{id}#{name} {args}",
id = self.id,
name = self.name,
args = self.args.as_deref().unwrap_or("")
)
}
}
impl ToolCall {
pub fn builder() -> ToolCallBuilder {
ToolCallBuilder::default()
}
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn args(&self) -> Option<&str> {
self.args.as_deref()
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Default, Builder)]
pub struct ToolSpec {
pub name: &'static str,
pub description: &'static str,
#[builder(default)]
pub parameters: Vec<ParamSpec>,
}
impl ToolSpec {
pub fn builder() -> ToolSpecBuilder {
ToolSpecBuilder::default()
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Builder)]
pub struct ParamSpec {
pub name: &'static str,
pub description: &'static str,
#[builder(default = true)]
pub required: bool,
}
impl ParamSpec {
pub fn builder() -> ParamSpecBuilder {
ParamSpecBuilder::default()
}
}