use std::convert::Infallible;
use clex_gen as clex;
use rig_core::{completion::ToolDefinition, tool::Tool};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[derive(Debug, Deserialize)]
pub struct ValidateClexArgs {
pub expression: String,
}
#[derive(Debug, Serialize)]
pub struct ValidateClexResult {
pub valid: bool,
pub error: Option<String>,
}
#[derive(Default)]
pub struct ValidateClex;
impl Tool for ValidateClex {
const NAME: &'static str = "validate_clex";
type Error = Infallible;
type Args = ValidateClexArgs;
type Output = ValidateClexResult;
async fn definition(&self, _prompt: String) -> ToolDefinition {
serde_json::from_value(json!({
"name": Self::NAME,
"description": "Validate a Clex expression against the Clex grammar and return whether it is valid with an optional error message.",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "The Clex expression to validate."
}
},
"required": ["expression"]
}
}))
.expect("valid tool definition")
}
async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
let result = match clex::generator(args.expression) {
Ok(_) => ValidateClexResult {
valid: true,
error: None,
},
Err(e) => ValidateClexResult {
valid: false,
error: Some(format!("{e:?}")),
},
};
Ok(result)
}
}