#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use crate::pricing::{default_pricing_table, merge_into, ModelPricing, PricingTable};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(rename_all = "snake_case")
)]
pub enum TaskClass {
Fast,
Balanced,
Power,
Embedding,
}
impl TaskClass {
pub fn for_phase_name(phase: &str) -> Self {
match phase.to_lowercase().as_str() {
"think" | "test" | "verify" | "summarize" | "summarise" => Self::Fast,
"embed" | "embedding" | "embeddings" => Self::Embedding,
"build" | "implement" | "code" => Self::Power,
_ => Self::Balanced,
}
}
}
impl std::fmt::Display for TaskClass {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Fast => write!(f, "fast"),
Self::Balanced => write!(f, "balanced"),
Self::Power => write!(f, "power"),
Self::Embedding => write!(f, "embedding"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TaskComplexity {
Simple,
Medium,
Complex,
Critical,
}
impl TaskComplexity {
pub fn min_quality_tier(self) -> u8 {
match self {
Self::Simple => 2,
Self::Medium => 3,
Self::Complex => 4,
Self::Critical => 5,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RoutingRule {
pub task: TaskClass,
pub model: String,
pub fallback: Option<Box<RoutingRule>>,
}
impl RoutingRule {
pub fn new(task: TaskClass, model: impl Into<String>) -> Self {
Self {
task,
model: model.into(),
fallback: None,
}
}
pub fn with_fallback(mut self, fallback: RoutingRule) -> Self {
self.fallback = Some(Box::new(fallback));
self
}
pub fn chain(&self) -> impl Iterator<Item = &RoutingRule> {
let mut current = Some(self);
std::iter::from_fn(move || {
let rule = current?;
current = rule.fallback.as_deref();
Some(rule)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouteDecision<'a> {
pub task: TaskClass,
pub primary: &'a str,
pub fallbacks: Vec<&'a str>,
}
impl<'a> RouteDecision<'a> {
pub fn candidates(&self) -> impl Iterator<Item = &'a str> + '_ {
std::iter::once(self.primary).chain(self.fallbacks.iter().copied().skip(1))
}
}
#[derive(Debug, Clone)]
pub struct Router {
rules: HashMap<TaskClass, RoutingRule>,
default_model: String,
pricing: BTreeMap<String, ModelPricing>,
}
impl Router {
pub fn new(default_model: impl Into<String>) -> Self {
Self {
rules: HashMap::new(),
default_model: default_model.into(),
pricing: default_pricing_table(),
}
}
pub fn set_default_model(&mut self, model: impl Into<String>) {
self.default_model = model.into();
}
pub fn add_rule(&mut self, rule: RoutingRule) {
self.rules.insert(rule.task, rule);
}
pub fn with_rule(mut self, rule: RoutingRule) -> Self {
self.add_rule(rule);
self
}
pub fn add_pricing(&mut self, model: impl Into<String>, pricing: ModelPricing) {
self.pricing.insert(model.into(), pricing);
}
pub fn with_pricing(mut self, model: impl Into<String>, pricing: ModelPricing) -> Self {
self.add_pricing(model, pricing);
self
}
pub fn merge_pricing(&mut self, table: &PricingTable) {
merge_into(&mut self.pricing, table);
}
pub fn default_model(&self) -> &str {
&self.default_model
}
pub fn pricing_for(&self, model: &str) -> Option<&ModelPricing> {
self.pricing.get(model)
}
pub fn pricing_table(&self) -> &BTreeMap<String, ModelPricing> {
&self.pricing
}
pub fn rule_for(&self, task: TaskClass) -> Option<&RoutingRule> {
self.rules.get(&task)
}
pub fn route(&self, task: TaskClass) -> RouteDecision<'_> {
match self.rules.get(&task) {
Some(rule) => {
let fallbacks = rule.chain().map(|r| r.model.as_str()).collect();
RouteDecision {
task,
primary: rule.model.as_str(),
fallbacks,
}
}
None => RouteDecision {
task,
primary: &self.default_model,
fallbacks: Vec::new(),
},
}
}
pub fn select_by_complexity(&self, complexity: TaskComplexity) -> Option<&str> {
let min_quality = complexity.min_quality_tier();
let mut candidates: Vec<(&String, &ModelPricing)> = self
.pricing
.iter()
.filter(|(_, p)| p.quality_tier >= min_quality)
.collect();
candidates.sort_by(|a, b| {
b.1.efficiency()
.partial_cmp(&a.1.efficiency())
.unwrap_or(std::cmp::Ordering::Equal)
});
candidates.first().map(|(name, _)| name.as_str())
}
pub fn estimate_cost(&self, model: &str, input_tokens: usize, output_tokens: usize) -> f64 {
self.pricing
.get(model)
.unwrap_or(&ModelPricing::default())
.cost(input_tokens, output_tokens)
}
pub fn default_rules(provider: &str, primary_model: &str) -> Vec<RoutingRule> {
let cheap_model = match provider {
"anthropic" => "claude-3-5-haiku-20241022",
"openai" => "gpt-4o-mini",
"google" => "gemini-2.0-flash",
"zai" => "glm-4.6",
"openrouter" => "google/gemma-3-4b-it:free",
_ => primary_model,
};
vec![
RoutingRule::new(TaskClass::Fast, cheap_model),
RoutingRule::new(TaskClass::Balanced, primary_model),
RoutingRule::new(TaskClass::Power, primary_model),
]
}
pub fn with_default_rules(provider: &str, primary_model: impl Into<String>) -> Self {
let primary_model = primary_model.into();
let mut router = Router::new(primary_model.clone());
for rule in Router::default_rules(provider, &primary_model) {
router.add_rule(rule);
}
router
}
}