use rig::tool::{Tool, ToolContext};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::llm::package_resolver::PackageResolver;
#[derive(Deserialize, Serialize)]
pub struct FindPackageArgs {
pattern: String,
}
pub struct FindPackage {
dependencies: Arc<Mutex<PackageResolver>>,
}
impl FindPackage {
pub fn new(dependencies: Arc<Mutex<PackageResolver>>) -> Self {
Self { dependencies }
}
}
impl Tool for FindPackage {
const NAME: &'static str = "find_package";
type Error = super::ToolError;
type Args = FindPackageArgs;
type Output = Vec<String>;
async fn call(
&self,
_context: &mut ToolContext,
args: Self::Args,
) -> Result<Self::Output, Self::Error> {
let dependencies = self.dependencies.lock().await;
Ok(dependencies.search_dependency(&args.pattern))
}
fn description(&self) -> String {
"Find all packages/dependencies whose name matches the provided pattern. \
Returns a list of identifier for the packages that match the pattern."
.to_string()
}
fn parameters(&self) -> serde_json::Value {
json!({
"type": "object",
"properties": {
"pattern": {
"type": "string",
"description": "Pattern to match agains the names of packages. \
Matches all packages that contain the pattern text as part of their name."
},
},
"required": ["pattern"]
})
}
fn map_error(&self, error: Self::Error) -> rig::tool::ToolExecutionError {
rig::tool::ToolExecutionError::from_error(error)
}
}