switchyard_libsy/algorithms/
passthrough.rs1use std::sync::Arc;
7
8use switchyard_protocol::{ModelId, Request, Response};
9
10use crate::Result;
11use crate::core::algorithm::{Algorithm, Driver};
12use switchyard_protocol::Decision;
13
14pub struct Passthrough {
16 target: ModelId,
17}
18
19impl Passthrough {
20 pub fn new(target: impl Into<ModelId>) -> Self {
22 Passthrough {
23 target: target.into(),
24 }
25 }
26}
27
28#[async_trait::async_trait]
29impl Algorithm for Passthrough {
30 fn name(&self) -> &str {
31 "passthrough"
32 }
33
34 async fn route(self: Arc<Self>, driver: Driver, request: Request) -> Result<Response> {
35 tracing::info!(target = %self.target, "passthrough selected target");
36 let decision: Decision = Decision::new(self.target.clone(), true);
37 driver.decide(decision.clone()).await?;
38 driver.call_model(request, decision).await
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use std::sync::Arc;
45
46 use super::Passthrough;
47 use crate::core::algorithm::Algorithm;
48 use crate::core::testing::{echo, test_drive};
49 use switchyard_protocol::{Request, completion_text, text_request};
50
51 #[tokio::test]
52 async fn test_passthrough() -> crate::Result<()> {
53 const MODEL_ID: &str = "testing/passthrough";
54 let request = Request {
55 llm_request: text_request(Some("auto".to_string()), "hi"),
56 raw_request: None,
57 metadata: None,
58 };
59 let algorithm: Arc<dyn Algorithm> = Arc::new(Passthrough::new(MODEL_ID));
60 let (trace, response) = test_drive(algorithm, request, echo()).await?;
61
62 assert_eq!(
63 response
64 .llm_response
65 .as_agg()
66 .map(completion_text)
67 .unwrap_or_default(),
68 MODEL_ID
69 );
70 assert_eq!(trace.len(), 1);
71 assert_eq!(trace[0].selected_model_id(), MODEL_ID);
72 assert!(trace[0].is_answer_call());
73 Ok(())
74 }
75}