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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! The desktop and terminal apps manage a local Switchyard server.
//!
//! The printed summary compares list-price estimates with the configured baseline model.
#![cfg_attr(windows, windows_subsystem = "windows")]
use switchyard_desktop::{config, health, history, rollup};
mod accounts;
mod controller;
#[cfg(any(target_os = "macos", windows))]
mod gui;
mod harness;
mod models;
mod pricing;
mod server;
mod server_config;
mod sessions;
mod summary;
mod tui;
#[cfg(windows)]
mod windows;
use std::path::PathBuf;
use std::process::ExitCode;
use config::Config;
const USAGE: &str = "\
Usage: switchyard-desktop [--tui | --print | --validate-toml] [FILE]
This app opens the desktop window on macOS and Windows.
Use --tui for the terminal app or --print for a usage summary.
FILE The app reads settings from FILE.
--tui Run the terminal app.
--print Print the current summary and exit.
--validate-toml Parse FILE as TOML and exit.
Without FILE, the app uses bundled installation metadata to find desktop.toml.
Without that metadata, it uses $SY_HOME/desktop.toml, or ~/.switchyard/desktop.toml
when SY_HOME is unset.
Missing settings use the directory and port from adjacent install.toml, or the
settings file's directory and port 4123 when that metadata is absent.
The default server URL includes the port, such as http://127.0.0.1:4123.
A configured HTTP URL without a port uses port 80.
Savings compare spending on all calls with the cost of the answers at
baseline_model's bundled rates or the table selected by pricing_file.
Recorded spending remains visible when savings are unknown.";
/// The command selects the graphical app, terminal app, printed summary, or TOML validation.
fn main() -> ExitCode {
#[cfg(windows)]
if let Some(result) = windows::terminal_job() {
return match result {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("switchyard-desktop: {error}");
ExitCode::FAILURE
}
};
}
#[cfg(windows)]
windows::attach_console();
let mut settings = None;
let mut print_only = false;
let mut validate_toml = false;
let mut terminal = false;
for arg in std::env::args().skip(1) {
match arg.as_str() {
"-h" | "--help" => {
println!("{USAGE}");
return ExitCode::SUCCESS;
}
"--print" => print_only = true,
"--tui" => terminal = true,
"--validate-toml" => validate_toml = true,
_ if arg.starts_with('-') => {
eprintln!("switchyard-desktop: unknown option {arg}\n\n{USAGE}");
return ExitCode::FAILURE;
}
_ if settings.is_some() => {
eprintln!("switchyard-desktop: expected at most one settings file\n\n{USAGE}");
return ExitCode::FAILURE;
}
_ => settings = Some(PathBuf::from(arg)),
}
}
if terminal && (print_only || validate_toml) {
eprintln!("{USAGE}");
return ExitCode::FAILURE;
}
if validate_toml {
let Some(path) = settings else {
eprintln!("switchyard-desktop: --validate-toml requires a file path");
return ExitCode::FAILURE;
};
if let Err(error) = config::validate_toml_file(&path) {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
return ExitCode::SUCCESS;
}
let settings = match settings.map(Ok).unwrap_or_else(Config::default_path) {
Ok(path) => path,
Err(error) => {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
};
if !terminal && !print_only && !cfg!(any(target_os = "macos", windows)) {
eprintln!("The desktop window supports macOS and Windows. Use --print or --tui.");
return ExitCode::FAILURE;
}
let config = match Config::load(&settings) {
Ok(config) => config,
Err(error) => {
eprintln!("switchyard-desktop: {error}");
if print_only || (!terminal && !cfg!(any(target_os = "macos", windows))) {
return ExitCode::FAILURE;
}
match Config::defaults_for(&settings) {
Ok(config) => config,
Err(error) => {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
}
}
};
if print_only {
let usage = match rollup::Reader::default()
.read(&config.routing_log, chrono::Local::now().date_naive())
{
Ok(usage) => usage,
Err(error) => {
eprintln!(
"Usage unavailable. Could not read {}: {error}",
config.routing_log.display()
);
return ExitCode::FAILURE;
}
};
let rates = match pricing::load(config.prices_file.as_deref()) {
Ok(rates) => rates,
Err(error) => {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
};
let status = health::Probe::default().probe(&config.server_url);
for row in summary::build(status, &usage, &config, &rates) {
println!("{row}");
}
return ExitCode::SUCCESS;
}
if !settings.try_exists().unwrap_or(true)
&& matches!(config::needs_setup(&settings), Ok(false))
&& let Err(error) = config::save_configuration(&settings, &config, false)
{
eprintln!("switchyard-desktop: {error}");
}
let controller = controller::Controller::new(config, settings.clone());
if terminal {
#[cfg(windows)]
windows::allocate_console();
if let Err(error) = tui::run(controller) {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
} else {
#[cfg(any(target_os = "macos", windows))]
if let Err(error) = gui::run(controller, settings) {
eprintln!("switchyard-desktop: {error}");
return ExitCode::FAILURE;
}
}
ExitCode::SUCCESS
}