Skip to main content

switchyard_libsy/algorithms/
passthrough.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Single-target routing for direct model calls and integration diagnostics.
5
6use std::sync::Arc;
7
8use switchyard_protocol::{Request, Response};
9
10use crate::Result;
11use crate::core::algorithm::{Algorithm, Driver, LlmTarget};
12use switchyard_protocol::{Context, Decision, RoutedLlmClient};
13
14/// Routing algorithm that always calls one configured target.
15pub struct Passthrough {
16    target: LlmTarget,
17}
18
19impl Passthrough {
20    /// Creates an algorithm that always calls `target`.
21    pub fn new(target: LlmTarget) -> Self {
22        Passthrough { target }
23    }
24}
25
26/// Decision emitted before [`Passthrough`] calls its configured target.
27pub struct PassthroughDecision {
28    model_id: String,
29}
30
31impl Decision for PassthroughDecision {
32    fn selected_model(&self) -> &str {
33        &self.model_id
34    }
35    fn reasoning(&self) -> Option<&str> {
36        // There is no decision to take, so no reasoning
37        None
38    }
39    fn as_any(&self) -> &dyn std::any::Any {
40        self
41    }
42}
43
44#[async_trait::async_trait]
45impl Algorithm for Passthrough {
46    fn name(&self) -> &str {
47        "passthrough"
48    }
49
50    fn count_tokens_client(&self) -> Option<Arc<dyn RoutedLlmClient>> {
51        self.target
52            .llm_client
53            .as_ref()
54            .filter(|client| client.supports_count_tokens())
55            .cloned()
56    }
57
58    async fn create_run_task(
59        self: Arc<Self>,
60        ctx: Context,
61        driver: Driver,
62        request: Request,
63    ) -> Result<Response> {
64        let decision: Arc<dyn Decision> = Arc::new(PassthroughDecision {
65            model_id: self.target.semantic_name.clone(),
66        });
67        driver.info(ctx.clone(), decision.clone()).await?;
68        driver
69            .call_llm_target(ctx, &self.target, request, decision)
70            .await
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use std::sync::Arc;
77
78    use super::Passthrough;
79    use crate::core::algorithm::{Algorithm, LlmTarget};
80    use switchyard_protocol::{
81        Context, Decision, LlmResponse, Request, Response, RoutedLlmClient, completion_text,
82        text_request, text_response,
83    };
84
85    /// Echoes the selected target so tests can inspect which target was called.
86    /// TODO: Duplicated from rand.rs
87    struct EchoClient;
88
89    #[async_trait::async_trait]
90    impl RoutedLlmClient for EchoClient {
91        async fn call(
92            &self,
93            _ctx: Context,
94            _request: Request,
95            decision: Arc<dyn Decision>,
96        ) -> std::result::Result<Response, switchyard_protocol::LlmClientError> {
97            Ok(Response {
98                llm_response: LlmResponse::Agg(text_response(None, decision.selected_model())),
99                metadata: None,
100            })
101        }
102    }
103
104    #[tokio::test]
105    async fn test_passthrough() -> crate::Result<()> {
106        const MODEL_ID: &str = "testing/passthrough";
107        let request = Request {
108            llm_request: text_request(Some("auto".to_string()), "hi"),
109            raw_request: None,
110            metadata: None,
111        };
112        let algorithm: Arc<dyn Algorithm> = Arc::new(Passthrough::new(LlmTarget {
113            semantic_name: MODEL_ID.to_string(),
114            llm_client: Some(Arc::new(EchoClient)),
115        }));
116        let (trace, response) = algorithm.run(Context::default(), request).await?;
117
118        assert_eq!(
119            response
120                .llm_response
121                .as_agg()
122                .map(completion_text)
123                .unwrap_or_default(),
124            MODEL_ID
125        );
126        assert_eq!(trace.len(), 1);
127        assert_eq!(trace[0].selected_model(), MODEL_ID);
128        Ok(())
129    }
130}