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
39 .call_model(request, vec![self.target.clone()], true)
40 .await
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use std::sync::Arc;
47
48 use super::Passthrough;
49 use crate::core::algorithm::Algorithm;
50 use crate::core::testing::{echo, test_drive};
51 use switchyard_protocol::{Request, completion_text, text_request};
52
53 #[tokio::test]
54 async fn test_passthrough() -> crate::Result<()> {
55 const MODEL_ID: &str = "testing/passthrough";
56 let request = Request {
57 llm_request: text_request(Some("auto".to_string()), "hi"),
58 raw_request: None,
59 metadata: None,
60 };
61 let algorithm: Arc<dyn Algorithm> = Arc::new(Passthrough::new(MODEL_ID));
62 let (trace, response) = test_drive(algorithm, request, echo()).await?;
63
64 assert_eq!(
65 response
66 .llm_response
67 .as_agg()
68 .map(completion_text)
69 .unwrap_or_default(),
70 MODEL_ID
71 );
72 assert_eq!(trace.len(), 1);
73 assert_eq!(trace[0].selected_model_id(), MODEL_ID);
74 assert!(trace[0].is_answer_call());
75 Ok(())
76 }
77}