use serde_json::{json, Value};
use crate::scripting::event_handler::EventPattern;
use crate::scripting::{EventHandlerConfig, EventHandlerType};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Driver {
Manual,
Llm,
Silent,
Rules,
}
impl Driver {
pub fn label(&self) -> &'static str {
match self {
Driver::Manual => "MANUAL",
Driver::Llm => "LLM",
Driver::Silent => "SILENT",
Driver::Rules => "RULES",
}
}
pub fn describe(&self) -> &'static str {
match self {
Driver::Manual => "you answer each event here",
Driver::Llm => "the model answers, from the instruction",
Driver::Silent => "events are acknowledged and never answered",
Driver::Rules => "a wildcard rule answers (script or fixed actions)",
}
}
pub fn next(&self) -> Driver {
match self {
Driver::Manual => Driver::Llm,
Driver::Llm => Driver::Silent,
Driver::Silent | Driver::Rules => Driver::Manual,
}
}
}
pub fn driver_of(routing: Option<&EventHandlerConfig>) -> Driver {
let Some(config) = routing else {
return Driver::Llm;
};
let wildcard = config
.handlers
.iter()
.find(|h| matches!(h.event_pattern, EventPattern::Wildcard));
match wildcard.map(|h| &h.handler) {
None => Driver::Llm,
Some(EventHandlerType::Manual { .. }) => Driver::Manual,
Some(EventHandlerType::Llm { .. }) => Driver::Llm,
Some(EventHandlerType::Static { actions }) if actions.is_empty() => Driver::Silent,
Some(EventHandlerType::Static { .. }) | Some(EventHandlerType::Script { .. }) => {
Driver::Rules
}
}
}
pub fn specific_rule_count(routing: Option<&EventHandlerConfig>) -> usize {
routing
.map(|c| {
c.handlers
.iter()
.filter(|h| !matches!(h.event_pattern, EventPattern::Wildcard))
.count()
})
.unwrap_or(0)
}
pub fn handlers_with_driver(routing: Option<&EventHandlerConfig>, driver: Driver) -> Vec<Value> {
let mut out: Vec<Value> = routing
.map(|c| {
c.handlers
.iter()
.filter(|h| !matches!(h.event_pattern, EventPattern::Wildcard))
.filter_map(|h| serde_json::to_value(h).ok())
.collect()
})
.unwrap_or_default();
match driver {
Driver::Llm => {}
Driver::Silent => out.push(json!({
"event_pattern": "*",
"handler": {"type": "static", "actions": []}
})),
Driver::Manual | Driver::Rules => out.push(json!({
"event_pattern": "*",
"handler": {"type": "manual"}
})),
}
out
}