Skip to main content

bitrouter_core/routers/
admin.rs

1//! Admin routing types and trait for runtime route management.
2//!
3//! Provides the [`AdminRoutingTable`] trait which extends [`RoutingTable`] with
4//! methods for creating, removing, and listing dynamically-added routes at
5//! runtime — without requiring config file rewrites or daemon restarts.
6
7use serde::{Deserialize, Serialize};
8
9use crate::errors::Result;
10
11use super::routing_table::RoutingTable;
12
13/// A single endpoint in a dynamic route.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct RouteEndpoint {
16    /// Provider name (must exist in the providers section or built-ins).
17    pub provider: String,
18    /// The upstream model ID to send to this provider.
19    pub model_id: String,
20}
21
22/// Strategy for distributing requests across multiple endpoints.
23#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum RouteStrategy {
26    /// Try endpoints in declared order.
27    #[default]
28    Priority,
29    /// Distribute requests evenly via round-robin.
30    LoadBalance,
31}
32
33/// A dynamically-configured route definition.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct DynamicRoute {
36    /// The virtual model name (e.g. "research", "fast").
37    pub model: String,
38    /// Routing strategy across endpoints.
39    #[serde(default)]
40    pub strategy: RouteStrategy,
41    /// One or more upstream endpoints to route to.
42    pub endpoints: Vec<RouteEndpoint>,
43}
44
45/// Extension trait for routing tables that support runtime route management.
46///
47/// Implementations store dynamic routes separately from config-defined routes.
48/// Dynamic routes take precedence during resolution.
49pub trait AdminRoutingTable: RoutingTable {
50    /// Create or update a dynamic route.
51    fn add_route(&self, route: DynamicRoute) -> Result<()>;
52
53    /// Remove a dynamically-added route. Returns `true` if the route existed.
54    ///
55    /// Config-defined routes cannot be removed.
56    fn remove_route(&self, model: &str) -> Result<bool>;
57
58    /// List all dynamically-added routes.
59    fn list_dynamic_routes(&self) -> Vec<DynamicRoute>;
60}