pub struct SmartRouter { /* private fields */ }Expand description
A smart router that combines keyword matching, capability scoring, and prompt complexity analysis to select the optimal backend.
Routing strategy (evaluated in order):
-
Priority overrides — explicit
(keyword, backend)pairs checked first. If a keyword matches the prompt and the backend is available, it wins immediately. -
Prompt complexity analysis — the prompt is scored for length, code presence, and technical depth. Each complexity level maps to a preferred backend.
-
Capability fallback — an embedded
CapabilityRouterscores remaining candidates on cost/latency when neither keywords nor complexity produce a match.
§Example
use agent_teams::backend::router::SmartRouter;
use agent_teams::BackendType;
let router = SmartRouter::new(BackendType::ClaudeCode)
.priority("security audit", BackendType::ClaudeCode)
.priority("quick fix", BackendType::Codex)
.simple_backend(BackendType::GeminiCli)
.complex_backend(BackendType::ClaudeCode)
.complexity_threshold(200, 800)
.cost_weight(0.6);Implementations§
Source§impl SmartRouter
impl SmartRouter
Sourcepub fn new(default: BackendType) -> Self
pub fn new(default: BackendType) -> Self
Create a new smart router with a default backend.
Sourcepub fn priority(self, keyword: impl Into<String>, backend: BackendType) -> Self
pub fn priority(self, keyword: impl Into<String>, backend: BackendType) -> Self
Add a priority keyword override.
Priority keywords use word-boundary matching (same semantics as
[KeywordRouter::word_boundary(true)]). The first matching priority wins.
Sourcepub fn simple_backend(self, backend: BackendType) -> Self
pub fn simple_backend(self, backend: BackendType) -> Self
Set the preferred backend for simple (short, non-technical) prompts.
Sourcepub fn medium_backend(self, backend: BackendType) -> Self
pub fn medium_backend(self, backend: BackendType) -> Self
Set the preferred backend for medium-complexity prompts.
Sourcepub fn complex_backend(self, backend: BackendType) -> Self
pub fn complex_backend(self, backend: BackendType) -> Self
Set the preferred backend for complex (long, code-heavy) prompts.
Sourcepub fn complexity_threshold(self, simple: usize, complex: usize) -> Self
pub fn complexity_threshold(self, simple: usize, complex: usize) -> Self
Set the character-length thresholds for complexity classification.
- Prompts shorter than
simplecharacters areSimple. - Prompts longer than
complexcharacters areComplex. - Everything in between is
Medium.
Defaults: simple = 200, complex = 800.
§Panics
Panics in debug mode if simple >= complex.
Sourcepub fn cost_weight(self, weight: f32) -> Self
pub fn cost_weight(self, weight: f32) -> Self
Set the cost vs latency weight for the capability fallback (0.0–1.0).
Sourcepub fn require_multi_turn(self, require: bool) -> Self
pub fn require_multi_turn(self, require: bool) -> Self
Require multi-turn support in the capability fallback.
Sourcepub fn with_capability(
self,
backend: BackendType,
cap: BackendCapability,
) -> Self
pub fn with_capability( self, backend: BackendType, cap: BackendCapability, ) -> Self
Override capability data for a backend in the fallback scorer.
Sourcepub fn analyze_complexity(&self, prompt: &str) -> PromptComplexity
pub fn analyze_complexity(&self, prompt: &str) -> PromptComplexity
Analyze a prompt and return its complexity level.
Trait Implementations§
Source§impl BackendRouter for SmartRouter
impl BackendRouter for SmartRouter
Source§fn route<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
config: &'life1 SpawnConfig,
available: &'life2 [BackendType],
) -> Pin<Box<dyn Future<Output = Option<BackendType>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn route<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
config: &'life1 SpawnConfig,
available: &'life2 [BackendType],
) -> Pin<Box<dyn Future<Output = Option<BackendType>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Auto Trait Implementations§
impl Freeze for SmartRouter
impl RefUnwindSafe for SmartRouter
impl Send for SmartRouter
impl Sync for SmartRouter
impl Unpin for SmartRouter
impl UnwindSafe for SmartRouter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more