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::{ModelId, Request};
9
10use crate::core::algorithm::{Algorithm, Driver};
11use crate::{Result, RoutingOutcome};
12
13/// Routing algorithm that always selects one configured target.
14pub struct Passthrough {
15    target: ModelId,
16}
17
18impl Passthrough {
19    /// Creates an algorithm that always selects `target`.
20    pub fn new(target: impl Into<ModelId>) -> Self {
21        Passthrough {
22            target: target.into(),
23        }
24    }
25}
26
27#[async_trait::async_trait]
28impl Algorithm for Passthrough {
29    fn name(&self) -> &str {
30        "passthrough"
31    }
32
33    async fn route(self: Arc<Self>, _driver: Driver, request: Request) -> Result<RoutingOutcome> {
34        tracing::info!(target = %self.target, "passthrough selected target");
35        Ok(RoutingOutcome::route_to(
36            self.target.clone(),
37            Vec::new(),
38            request,
39        ))
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use std::sync::Arc;
46
47    use super::Passthrough;
48    use crate::core::algorithm::Algorithm;
49    use crate::core::testing::{echo, test_drive};
50    use switchyard_protocol::{Request, completion_text, text_request};
51
52    #[tokio::test]
53    async fn test_passthrough() -> crate::Result<()> {
54        const MODEL_ID: &str = "testing/passthrough";
55        let request = Request {
56            llm_request: text_request(Some("auto".to_string()), "hi"),
57            raw_request: None,
58            metadata: None,
59        };
60        let algorithm: Arc<dyn Algorithm> = Arc::new(Passthrough::new(MODEL_ID));
61        let (selected_model, response) = test_drive(algorithm, request, echo()).await?;
62
63        assert_eq!(
64            response
65                .llm_response
66                .as_agg()
67                .map(completion_text)
68                .unwrap_or_default(),
69            MODEL_ID
70        );
71        assert_eq!(selected_model, MODEL_ID);
72        Ok(())
73    }
74}