use magi_tool::Tool as MagiTool;
use crate::responses::request::{FunctionTool, Tool};
impl From<MagiTool> for Tool {
fn from(magi_tool: MagiTool) -> Self {
Tool::Function(FunctionTool {
name: magi_tool.name.to_string(),
description: magi_tool.description,
parameters: Some(magi_tool.input_schema),
strict: None,
})
}
}
impl From<Tool> for MagiTool {
fn from(tool: Tool) -> Self {
match tool {
Tool::Function(function) => MagiTool {
name: function.name.clone().into(),
description: function.description.clone(),
input_schema: function.parameters.unwrap_or_default(),
},
_ => MagiTool {
name: "unsupported_tool".into(),
description: None,
input_schema: serde_json::Value::Null,
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::responses::request::FunctionTool;
use serde_json::json;
use std::borrow::Cow;
#[test]
fn converts_magi_tool_to_openai_tool() {
let magi_tool = MagiTool {
name: "get_weather".into(),
description: Some(Cow::from("Get the current weather")),
input_schema: json!({
"type": "object",
"properties": {
"location": {
"type": "string"
}
},
"required": ["location"]
}),
};
let openai_tool: Tool = magi_tool.into();
match openai_tool {
Tool::Function(function) => {
assert_eq!(function.name, "get_weather");
}
_ => panic!("Expected Function variant"),
}
}
#[test]
fn converts_openai_tool_to_magi_tool() {
let openai_tool = Tool::Function(FunctionTool {
name: "get_weather".into(),
description: Some(Cow::from("Get the current weather")),
parameters: Some(json!({
"type": "object",
"properties": {
"location": {
"type": "string"
}
},
"required": ["location"]
})),
strict: None,
});
let magi_tool: MagiTool = openai_tool.into();
assert_eq!(magi_tool.name, "get_weather");
assert_eq!(
magi_tool.description,
Some(Cow::from("Get the current weather"))
);
}
}