Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
feat: noop algorithm for libsy (#78)
Graham King8a14f966a352
diff --git a/crates/libsy-protocol/src/envelope.rs b/crates/libsy-protocol/src/envelope.rsindex 33479cdeb..af2182519 100644--- a/crates/libsy-protocol/src/envelope.rs+++ b/crates/libsy-protocol/src/envelope.rs@@ -46,6 +46,12 @@ pub struct Request {pub metadata: Option<Metadata>,}+impl Request {+ pub fn requested_model(&self) -> Option<&str> {+ self.llm_request.model.as_deref()+ }+}+/// A response an algorithm returns: the [`LlmResponse`] (streamed or aggregate) plus/// optional correlation [`Metadata`].///@@ -56,3 +62,9 @@ pub struct Response {/// Correlation metadata carried through the response.pub metadata: Option<Metadata>,}++impl Response {+ pub fn selected_model(&self) -> Option<&str> {+ self.llm_response.selected_model()+ }+}diff --git a/crates/libsy-protocol/src/stream.rs b/crates/libsy-protocol/src/stream.rsindex 175fd1d19..102f9b7d9 100644--- a/crates/libsy-protocol/src/stream.rs+++ b/crates/libsy-protocol/src/stream.rs@@ -61,6 +61,14 @@ impl LlmResponse {}}}++ pub fn selected_model(&self) -> Option<&str> {+ match self {+ LlmResponse::Agg(agg) => agg.model.as_deref(),+ // TODO: How do we get the model name on a stream?+ LlmResponse::Stream(_) => None,+ }+ }}/// One provider-neutral streaming event — the normalized counterpart todiff --git a/crates/libsy/src/algorithms.rs b/crates/libsy/src/algorithms.rsnew file mode 100644index 000000000..e08c8fd2d--- /dev/null+++ b/crates/libsy/src/algorithms.rs@@ -0,0 +1,4 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++pub mod noop;diff --git a/crates/libsy/src/algorithms/noop.rs b/crates/libsy/src/algorithms/noop.rsnew file mode 100644index 000000000..4c509dc4b--- /dev/null+++ b/crates/libsy/src/algorithms/noop.rs@@ -0,0 +1,108 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! No-op router. Always returns a hard coded response. Does not route to a backend.+//! For testing.++use std::{error::Error, sync::Arc};++use switchyard_protocol::{+ AggLlmResponse, ContentBlock, LlmResponse, Request, Response, ResponseOutput, Role, StopReason,+};++use crate::{Algorithm, Context, Decision, Driver, Signals};++/// A routing algorithm that does not route. It returns a hard-coded response.+pub struct NoopAlgo {}++/// How [`NoopAlgo`] records which model it chose. This will be the model on the Request if any,+/// otherwise a hard coded placeholder. Neither is actually used.+pub struct NoopDecision {+ model: String,+}++impl Decision for NoopDecision {+ fn selected_model(&self) -> &str {+ &self.model+ }+ fn reasoning(&self) -> Option<&str> {+ None+ }+ fn as_any(&self) -> &dyn std::any::Any {+ self+ }+}++#[async_trait::async_trait]+impl Algorithm for NoopAlgo {+ async fn create_run_task(+ self: Arc<Self>,+ ctx: Context,+ driver: Driver,+ request: Request,+ ) -> Result<Response, Box<dyn Error + Send + Sync>> {+ let model = request+ .requested_model()+ .unwrap_or("switchyard/noop")+ .to_string();+ let decision: Arc<dyn Decision> = Arc::new(NoopDecision {+ model: model.clone(),+ });+ driver.info(ctx, decision.clone()).await?;++ let llm_response = LlmResponse::Agg(AggLlmResponse {+ id: Some("switchyard-noop".to_string()),+ model: Some(model),+ outputs: vec![ResponseOutput {+ role: Role::Assistant,+ content: vec![ContentBlock::Text {+ text: "OK".to_string(),+ }],+ stop_reason: Some(StopReason::EndTurn),+ }],+ ..Default::default()+ });+ let response = Response {+ llm_response,+ metadata: request.metadata.clone(),+ };+ Ok(response)+ }++ async fn process_signals(+ self: Arc<Self>,+ _signals: Signals,+ ) -> Result<(), Box<dyn Error + Send + Sync>> {+ Ok(())+ }+}++#[cfg(test)]+mod tests {+ use switchyard_protocol::{LlmRequest, Message, Role};++ use super::*;++ #[tokio::test]+ async fn test_noop_algo() -> Result<(), Box<dyn Error + Send + Sync>> {+ const TEST_MODEL: &str = "test_noop_algo";+ let request = Request {+ llm_request: LlmRequest {+ model: Some(TEST_MODEL.to_string()),+ messages: vec![Message::text(Role::User, "hi")],+ ..LlmRequest::default()+ },+ raw_request: None,+ metadata: None,+ };++ let a: Arc<dyn Algorithm> = Arc::new(NoopAlgo {});+ let (decisions, response) = a.run(Context::default(), request).await?;+ let Some(decision) = decisions.first() else {+ panic!("Expected exactly one Decision");+ };+ assert_eq!(decision.selected_model(), TEST_MODEL);+ assert_eq!(response.selected_model(), Some(TEST_MODEL));+ Ok(())+ }+}diff --git a/crates/libsy/src/lib.rs b/crates/libsy/src/lib.rsindex d30766dd8..b75961183 100644--- a/crates/libsy/src/lib.rs+++ b/crates/libsy/src/lib.rs@@ -57,6 +57,9 @@//! Worked implementations — a random router, an LLM classifier, and a stateful//! ensemble — plus runnable agents live in the `libsy-examples` crate.+mod algorithms;+pub use algorithms::noop::{NoopAlgo, NoopDecision};+mod driver;use std::{error::Error, pin::Pin, sync::Arc};