Rust API Reference¶
Complete Rust API documentation for StrataRouter Core.
Installation¶
Quick Start¶
use stratarouter_core::{Router, RouterConfig, Route};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut router = Router::new(RouterConfig::default())?;
router.add_route(Route::new("billing")
.with_description("Billing questions")
.with_keywords(vec!["invoice".into(), "payment".into()]))?;
let embeddings = vec![vec![0.1; 384]];
router.build_index(&embeddings)?;
let result = router.route("My invoice?", &vec![0.15; 384])?;
println!("Route: {} ({}%)", result.route_id, result.confidence * 100.0);
Ok(())
}
Core Types¶
Router¶
pub struct Router {
config: RouterConfig,
routes: Vec<Route>,
index: Option<HNSWIndex>,
}
impl Router {
pub fn new(config: RouterConfig) -> Result<Self>;
pub fn add_route(&mut self, route: Route) -> Result<()>;
pub fn remove_route(&mut self, id: &str) -> Result<()>;
pub fn build_index(&mut self, embeddings: &[Vec<f32>]) -> Result<()>;
pub fn route(&self, query: &str, embedding: &[f32]) -> Result<RouteResult>;
pub fn save_index(&self, path: &Path) -> Result<()>;
pub fn load_index(&mut self, path: &Path) -> Result<()>;
}
RouterConfig¶
#[derive(Debug, Clone)]
pub struct RouterConfig {
pub dimension: usize,
pub threshold: f32,
pub max_candidates: usize,
pub hnsw_m: usize,
pub hnsw_ef_construction: usize,
pub hnsw_ef_search: usize,
pub semantic_weight: f32,
pub keyword_weight: f32,
pub rule_weight: f32,
}
impl Default for RouterConfig {
fn default() -> Self {
Self {
dimension: 384,
threshold: 0.5,
max_candidates: 10,
hnsw_m: 16,
hnsw_ef_construction: 200,
hnsw_ef_search: 50,
semantic_weight: 0.6,
keyword_weight: 0.3,
rule_weight: 0.1,
}
}
}
Route¶
#[derive(Debug, Clone)]
pub struct Route {
pub id: String,
pub description: String,
pub keywords: Vec<String>,
pub examples: Vec<String>,
pub priority: i32,
}
impl Route {
pub fn new(id: impl Into<String>) -> Self;
pub fn with_description(self, desc: impl Into<String>) -> Self;
pub fn with_keywords(self, keywords: Vec<String>) -> Self;
pub fn with_priority(self, priority: i32) -> Self;
}
RouteResult¶
#[derive(Debug, Clone)]
pub struct RouteResult {
pub route_id: String,
pub confidence: f32,
pub latency_ms: f64,
pub alternatives: Vec<(String, f32)>,
}
Error Handling¶
use stratarouter_core::StratarouterError;
match router.route(&query, &embedding) {
Ok(result) => println!("Success: {}", result.route_id),
Err(StratarouterError::InvalidEmbedding(msg)) => eprintln!("Error: {}", msg),
Err(e) => eprintln!("Error: {}", e),
}
Advanced Usage¶
Concurrent Routing¶
use std::sync::Arc;
use tokio::task;
let router = Arc::new(router);
let handles: Vec<_> = queries.into_iter().map(|(query, emb)| {
let router = Arc::clone(&router);
task::spawn_blocking(move || router.route(&query, &emb))
}).collect();
for handle in handles {
let result = handle.await??;
println!("Route: {}", result.route_id);
}
Batch Operations¶
let routes = vec![
Route::new("route1"),
Route::new("route2"),
Route::new("route3"),
];
for route in routes {
router.add_route(route)?;
}
let embeddings = vec![vec![0.1; 384], vec![0.2; 384], vec![0.3; 384]];
router.build_index(&embeddings)?;