| 1 | use codewhale_command_contract::handler::CommandHandler; |
| 2 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 3 | use codewhale_command_contract::outcome::CommandResult; |
| 4 | |
| 5 | /// A Router setup preset. |
| 6 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 7 | pub enum RouterPreset<R> { |
| 8 | /// Jev; `None` picks whichever route has a key (OpenRouter first). |
| 9 | Jev(Option<R>), |
| 10 | Fast, |
| 11 | Off, |
| 12 | Custom, |
| 13 | } |
| 14 | |
| 15 | impl<R> RouterPreset<R> { |
| 16 | /// Parse `jev [openrouter|typesafe]`, `fast`, `off` or `custom`. |
| 17 | #[must_use] |
| 18 | pub fn parse(args: &str, parse_route: impl FnOnce(&str) -> Option<R>) -> Option<Self> { |
| 19 | let mut words = args.split_whitespace(); |
| 20 | let preset = match words.next()?.to_ascii_lowercase().as_str() { |
| 21 | "jev" => Self::Jev(match words.next() { |
| 22 | None => None, |
| 23 | Some(route) => Some(parse_route(route)?), |
| 24 | }), |
| 25 | "fast" => Self::Fast, |
| 26 | "off" | "none" => Self::Off, |
| 27 | "custom" => Self::Custom, |
| 28 | _ => return None, |
| 29 | }; |
| 30 | words.next().is_none().then_some(preset) |
| 31 | } |
| 32 | |
| 33 | /// The `/router` arguments that select this preset. |
| 34 | #[must_use] |
| 35 | pub fn command_args(self, route_name: impl FnOnce(R) -> &'static str) -> String { |
| 36 | match self { |
| 37 | Self::Jev(None) => "jev".to_string(), |
| 38 | Self::Jev(Some(route)) => format!("jev {}", route_name(route)), |
| 39 | Self::Fast => "fast".to_string(), |
| 40 | Self::Off => "off".to_string(), |
| 41 | Self::Custom => "custom".to_string(), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// What `/router` asked the app to do. |
| 47 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 48 | pub enum RouterRequest<R> { |
| 49 | /// Open the Router setup view. |
| 50 | Open, |
| 51 | /// Make one test call with this preset and show the result. |
| 52 | Test(RouterPreset<R>), |
| 53 | /// Persist this preset to `config.toml` and the live config. |
| 54 | Save(RouterPreset<R>), |
| 55 | } |
| 56 | |
| 57 | /// Parse `/router` (or `/model router`) arguments. |
| 58 | pub fn parse_router_args<R>( |
| 59 | args: Option<&str>, |
| 60 | parse_route: impl FnOnce(&str) -> Option<R>, |
| 61 | ) -> Result<RouterRequest<R>, String> { |
| 62 | let args = args.map(str::trim).unwrap_or_default(); |
| 63 | if args.is_empty() { |
| 64 | return Ok(RouterRequest::Open); |
| 65 | } |
| 66 | let (save, rest) = match args.split_once(char::is_whitespace) { |
| 67 | Some((head, rest)) if head.eq_ignore_ascii_case("save") => (true, rest.trim()), |
| 68 | _ => (false, args), |
| 69 | }; |
| 70 | let preset = RouterPreset::parse(rest, parse_route).ok_or_else(|| { |
| 71 | "Usage: /router [jev [openrouter|typesafe]|fast|off|custom] · /router save <preset>" |
| 72 | .to_string() |
| 73 | })?; |
| 74 | Ok(if save { |
| 75 | RouterRequest::Save(preset) |
| 76 | } else { |
| 77 | RouterRequest::Test(preset) |
| 78 | }) |
| 79 | } |
| 80 | |
| 81 | pub trait RouterHost { |
| 82 | type Route; |
| 83 | fn parse_route(name: &str) -> Option<Self::Route>; |
| 84 | fn router_action(request: RouterRequest<Self::Route>) -> Self; |
| 85 | } |
| 86 | |
| 87 | pub struct RouterCmd; |
| 88 | |
| 89 | impl<A: RouterHost> RegisterCommand<CommandResult<A>> for RouterCmd { |
| 90 | fn info() -> &'static CommandInfo { |
| 91 | &CommandInfo { |
| 92 | name: "router", |
| 93 | aliases: &[], |
| 94 | usage: "/router [jev|fast|off|custom|save]", |
| 95 | description_key: "cmd_router_description", |
| 96 | } |
| 97 | } |
| 98 | fn handler() -> CommandHandler<CommandResult<A>> { |
| 99 | CommandHandler::Pure(|args| match parse_router_args(args, A::parse_route) { |
| 100 | Ok(request) => CommandResult::action(A::router_action(request)), |
| 101 | Err(reason) => CommandResult::error(reason), |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[cfg(test)] |
| 107 | mod tests { |
| 108 | use super::*; |
| 109 | #[test] |
| 110 | fn saving_a_preset_requires_an_explicit_verb_and_valid_route() { |
| 111 | let parse = |args| parse_router_args(args, |name| (name == "vendor").then_some(7)); |
| 112 | assert_eq!(parse(None), Ok(RouterRequest::Open)); |
| 113 | assert_eq!( |
| 114 | parse(Some("jev vendor")), |
| 115 | Ok(RouterRequest::Test(RouterPreset::Jev(Some(7)))) |
| 116 | ); |
| 117 | assert_eq!( |
| 118 | parse(Some("SAVE Jev vendor")), |
| 119 | Ok(RouterRequest::Save(RouterPreset::Jev(Some(7)))) |
| 120 | ); |
| 121 | assert!(parse(Some("save jev unavailable")).is_err()); |
| 122 | assert!(parse(Some("fast vendor")).is_err()); |
| 123 | } |
| 124 | } |
| 125 |