use anyhow::Result;
use regex::Regex;
use crate::models::action::ActionModel;
use crate::models::action::ActionRun;
use crate::models::action::ActionValue;
use crate::validate::ValidateTrait;
impl ValidateTrait for ActionModel {
fn validate(&self) -> Result<()> {
match &self.action {
ActionValue::Simple(s) => {
if s.trim().is_empty() {
anyhow::bail!("Action '{}' has empty command/prompt.", self.tag);
}
}
ActionValue::Switch(cases) => {
for case in cases {
let trimmed_when = case.when.trim();
if trimmed_when != "true" {
let mut iter = crate::engine::parser::TagIterator::new(trimmed_when);
match (iter.next(), iter.next()) {
(Some(mat), None) => {
if mat.full_match.len() != trimmed_when.len() {
anyhow::bail!(
"When condition in '{}' must be a single {{tag|modifier}} or 'true', got: '{}'",
self.tag,
case.when
);
}
}
_ => {
anyhow::bail!(
"When condition in '{}' must be a single {{tag|modifier}} or 'true', got: '{}'",
self.tag,
case.when
);
}
}
}
}
}
}
let is_llm = matches!(
self.run,
ActionRun::Tiny
| ActionRun::Small
| ActionRun::Medium
| ActionRun::Large
| ActionRun::Vision
);
if is_llm && self.expect.is_none() {
anyhow::bail!(
"LLM action '{}' must have expect set. Specify what to expect.",
self.tag
);
}
if let Some(pattern) = &self.check {
Regex::new(pattern).map_err(|e| {
anyhow::anyhow!("Action '{}' has invalid check regex: {}", self.tag, e)
})?;
}
Ok(())
}
}