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