Skip to main content

SmartRouter

Struct SmartRouter 

Source
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):

  1. Priority overrides — explicit (keyword, backend) pairs checked first. If a keyword matches the prompt and the backend is available, it wins immediately.

  2. Prompt complexity analysis — the prompt is scored for length, code presence, and technical depth. Each complexity level maps to a preferred backend.

  3. Capability fallback — an embedded CapabilityRouter scores 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

Source

pub fn new(default: BackendType) -> Self

Create a new smart router with a default backend.

Source

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.

Source

pub fn simple_backend(self, backend: BackendType) -> Self

Set the preferred backend for simple (short, non-technical) prompts.

Source

pub fn medium_backend(self, backend: BackendType) -> Self

Set the preferred backend for medium-complexity prompts.

Source

pub fn complex_backend(self, backend: BackendType) -> Self

Set the preferred backend for complex (long, code-heavy) prompts.

Source

pub fn complexity_threshold(self, simple: usize, complex: usize) -> Self

Set the character-length thresholds for complexity classification.

  • Prompts shorter than simple characters are Simple.
  • Prompts longer than complex characters are Complex.
  • Everything in between is Medium.

Defaults: simple = 200, complex = 800.

§Panics

Panics in debug mode if simple >= complex.

Source

pub fn cost_weight(self, weight: f32) -> Self

Set the cost vs latency weight for the capability fallback (0.0–1.0).

Source

pub fn require_multi_turn(self, require: bool) -> Self

Require multi-turn support in the capability fallback.

Source

pub fn with_capability( self, backend: BackendType, cap: BackendCapability, ) -> Self

Override capability data for a backend in the fallback scorer.

Source

pub fn analyze_complexity(&self, prompt: &str) -> PromptComplexity

Analyze a prompt and return its complexity level.

Trait Implementations§

Source§

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,

Choose the optimal backend for the given config. Read more
Source§

impl Debug for SmartRouter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more