Skip to content

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(desktop): edit route algorithms and models in the terminal app (#1099)

Elyas Mehtabuddind7641f65d377
diff --git a/crates/switchyard-desktop/src/config.rs b/crates/switchyard-desktop/src/config.rs
index 5b558b1ed..ab3067b1d 100644
--- a/crates/switchyard-desktop/src/config.rs
+++ b/crates/switchyard-desktop/src/config.rs
@@ -36,8 +36,14 @@ pub struct Config {
pub server_url: String,
/// This field names the JSONL file where the server appends routing records.
pub routing_log: PathBuf,
- /// This field names the server TOML file opened by "Open server config".
+ /// This field names the server TOML file opened by "Open server config". Route edits save
+ /// only when it is the file that the installed server loads.
pub config_file: PathBuf,
+ /// This field names the server config that the installed service starts with: the
+ /// `composite.toml` in the directory from `install.toml`. It is `None` when no
+ /// `install.toml` sits next to the settings file. The settings file never sets it.
+ #[serde(skip)]
+ pub service_config: Option<PathBuf>,
/// This field names the LaunchAgent used to restart the server.
pub launchd_label: String,
/// This field sets the usage refresh interval in seconds.
@@ -59,6 +65,7 @@ impl Config {
refresh_seconds: 30,
baseline_model: "gpt-6-sol".into(),
prices_file: None,
+ service_config: None,
}
}
@@ -72,7 +79,10 @@ impl Config {
let metadata = directory.join("install.toml");
if metadata.try_exists().map_err(|e| e.to_string())? {
let settings = Settings::load(&metadata)?;
- Ok(Self::for_directory(&settings.sy_home, settings.port))
+ Ok(Self {
+ service_config: Some(settings.sy_home.join(SERVER_CONFIG)),
+ ..Self::for_directory(&settings.sy_home, settings.port)
+ })
} else {
Ok(Self::for_directory(directory, DEFAULT_PORT))
}
@@ -83,6 +93,7 @@ impl Config {
/// An omitted pricing_file selects bundled rates.
pub fn load(path: &Path) -> Result<Self, String> {
let defaults = Self::defaults_for(path)?;
+ let service_config = defaults.service_config.clone();
let mut values = toml::Table::try_from(defaults).map_err(|e| e.to_string())?;
match std::fs::read_to_string(path) {
Ok(text) => {
@@ -105,6 +116,7 @@ impl Config {
config.routing_log = expand_home(&config.routing_log)?;
config.config_file = expand_home(&config.config_file)?;
config.prices_file = config.prices_file.as_deref().map(expand_home).transpose()?;
+ config.service_config = service_config;
Ok(config)
}
diff --git a/crates/switchyard-desktop/src/controller.rs b/crates/switchyard-desktop/src/controller.rs
index 3d53b542c..a37aaee1e 100644
--- a/crates/switchyard-desktop/src/controller.rs
+++ b/crates/switchyard-desktop/src/controller.rs
@@ -7,7 +7,7 @@
use crate::{
config::Config,
history, models, pricing, rollup, server,
- server_config::{ALGORITHMS, Choice, ServerConfig},
+ server_config::{ALGORITHMS, Choice, Route, ServerConfig},
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
@@ -17,6 +17,18 @@ use std::{collections::BTreeMap, path::PathBuf};
#[strum_discriminants(name(ActionKind), derive(strum::EnumIter))]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Action {
+ Editor {
+ generation: u64,
+ route: String,
+ algorithm: String,
+ },
+ Apply {
+ generation: u64,
+ route: String,
+ algorithm: String,
+ choices: Vec<Choice>,
+ revision: Option<String>,
+ },
PreviewRoute {
generation: u64,
route: String,
@@ -208,6 +220,13 @@ pub struct Reply {
pub data: Value,
}
impl Reply {
+ fn saved_edit(result: server::SaveResult) -> Self {
+ Self {
+ message: result.message,
+ data: json!({"warning": result.warning, "details": result.details}),
+ }
+ }
+
fn message(message: String) -> Self {
Self {
message,
@@ -497,6 +516,7 @@ impl Controller {
refresh_seconds,
baseline_model,
prices_file,
+ service_config: _, // server::save_edit checks route edits against it
} = self.config.clone();
let update_status = install_metadata(&self.settings)
.and_then(|path| switchyard_desktop::install::update_status(&path))
@@ -598,6 +618,58 @@ impl Controller {
server::command("open", &[&logs_dir()?.display().to_string()])?;
Ok(Reply::message("Opened Switchyard logs.".into()))
}
+ Action::Editor {
+ generation,
+ route,
+ algorithm,
+ } => {
+ self.check_generation(generation)?;
+ let review = Review::capture(
+ json!({"route":route}),
+ vec![self.config.config_file.clone()],
+ )?;
+ let config = self.routes()?;
+ review.check(Some(&review.token), &review.inputs)?;
+ current_route(&config, &route, None)?;
+ config
+ .algorithm(&route)
+ .ok_or("This route must be edited in the server config file.")?;
+ let algorithm = algorithm_by_id(&algorithm)?;
+ let roles = config.roles(&route, algorithm);
+ let revision = self.remember_route(&route, review);
+ let data = json!({"revision":revision,"roles": roles.iter().map(role_info).collect::<Vec<_>>(),"choices":config.choices(&route)});
+ Ok(Reply {
+ message: algorithm.summary.into(),
+ data,
+ })
+ }
+ Action::Apply {
+ generation,
+ route,
+ algorithm,
+ choices,
+ revision,
+ } => {
+ self.check_generation(generation)?;
+ let config = self.routes()?;
+ current_route(&config, &route, None)?;
+ config
+ .algorithm(&route)
+ .ok_or("This route must be edited in the server config file.")?;
+ let algorithm = algorithm_by_id(&algorithm)?;
+ self.route_reviews
+ .get(&route)
+ .ok_or("Open the route editor before saving.")?
+ .check(revision.as_deref(), &json!({"route":route}))?;
+ server::apply(
+ &self.config,
+ &route,
+ algorithm,
+ &choices,
+ self.route_reviews[&route].original()?,
+ )
+ .map(Reply::saved_edit)
+ }
Action::Models {
client,
refresh,
@@ -811,6 +883,13 @@ fn algorithm_by_id(kind: &str) -> Result<&'static crate::server_config::Algorith
.find(|a| a.kind == kind)
.ok_or_else(|| "Unknown routing algorithm.".into())
}
+fn current_route(config: &ServerConfig, key: &str, id: Option<&str>) -> Result<Route, String> {
+ config
+ .routes()
+ .into_iter()
+ .find(|r| r.key == key && id.is_none_or(|id| id == r.id))
+ .ok_or_else(|| "The route changed or was removed. Refresh and choose it again.".into())
+}
fn install_metadata(settings: &std::path::Path) -> Result<PathBuf, String> {
let executable = std::env::current_exe().map_err(|e| e.to_string())?;
let bundle = switchyard_desktop::install::bundle_metadata(&executable);
diff --git a/crates/switchyard-desktop/src/server.rs b/crates/switchyard-desktop/src/server.rs
index e289dd952..7a9238cd4 100644
--- a/crates/switchyard-desktop/src/server.rs
+++ b/crates/switchyard-desktop/src/server.rs
@@ -1,21 +1,152 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
-//! This module checks server configs and saved key status and opens files.
+//! This module checks and saves server configs, checks saved key status, and opens files.
-use std::io::Write;
+use std::fs::OpenOptions;
+use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};
+use chrono::Local;
use switchyard_desktop::install::credentials::KeyStatus;
use crate::config::Config;
use crate::health::{Probe, ServerStatus};
+use crate::server_config::{Algorithm, Choice, ServerConfig};
/// RESTART_WAIT limits how long the app waits for the restarted server to answer `/health`.
const RESTART_WAIT: Duration = Duration::from_secs(10);
+/// BACKUPS_PER_SECOND limits the number of backups for one file within one second.
+const BACKUPS_PER_SECOND: u32 = 100;
+
+/// SaveResult distinguishes a saved config from a failed restart or health check.
+pub struct SaveResult {
+ pub message: String,
+ pub warning: bool,
+ pub details: Vec<String>,
+}
+
+/// This function switches a route to new models, checks the result with the server's
+/// `--dry-run`, saves it, and restarts the server.
+///
+/// The result names the changed file and reports whether the restart succeeded.
+/// The details contain the backup path and edit notes. An error means the file was not changed.
+pub fn apply(
+ settings: &Config,
+ route: &str,
+ algorithm: &Algorithm,
+ choices: &[Choice],
+ expected: &str,
+) -> Result<SaveResult, String> {
+ let path = &settings.config_file;
+ // Read the file again so an edit made while the window was open is kept.
+ let text = std::fs::read_to_string(path).map_err(|error| {
+ format!(
+ "Could not read {}: {error}. Check config_file in the app settings.",
+ path.display()
+ )
+ })?;
+ check_original(&text, expected)?;
+ let config = ServerConfig::parse(&text)?;
+ let edited = config.edit(route, algorithm, choices)?;
+ save_edit(settings, &text, edited, "Updated the route.")
+}
+
+fn check_original(text: &str, expected: &str) -> Result<(), String> {
+ if text != expected {
+ return Err("The server config changed since the editor loaded. \
+ Review it again; your draft is preserved."
+ .into());
+ }
+ Ok(())
+}
+
+fn save_edit(
+ settings: &Config,
+ text: &str,
+ edited: crate::server_config::Edited,
+ summary: &str,
+) -> Result<SaveResult, String> {
+ let path = &settings.config_file;
+ if edited.text == text {
+ return Ok(SaveResult {
+ message: "Nothing to save. The route already uses this algorithm and these models."
+ .into(),
+ warning: false,
+ details: Vec::new(),
+ });
+ }
+ service_config(settings).map_err(|error| format!("Nothing was saved. {error}"))?;
+ let binary = server_binary()?;
+ let backup = save_checked(&binary, path, text, &edited.text)?;
+ let saved = format!("{summary} Changed {}.", path.display());
+ let health = format!("{}/health", settings.server_url);
+ let (message, warning) = match restart(&settings.launchd_label) {
+ Err(error) => (
+ format!(
+ "{saved} Switchyard could not restart: {error}. \
+ Use Restart server in Settings to try again."
+ ),
+ true,
+ ),
+ Ok(()) if wait_for_health(&settings.server_url) => {
+ (format!("{saved} Restarted Switchyard."), false)
+ }
+ Ok(()) => (
+ format!(
+ "{saved} Switchyard restarted but did not answer {health} within {} seconds. \
+ Check the server log in ~/.switchyard/logs/.",
+ RESTART_WAIT.as_secs()
+ ),
+ true,
+ ),
+ };
+ let mut details = vec![format!("Backup: {}", backup.display())];
+ details.extend(edited.notes);
+ if warning {
+ details.push(format!(
+ "To undo, copy {} over {} and use Restart server in Settings.",
+ backup.display(),
+ path.display()
+ ));
+ }
+ Ok(SaveResult {
+ message,
+ warning,
+ details,
+ })
+}
+
+/// This function returns the file that the installed server loads, after checking that
+/// `config_file` names it. The service always starts with `composite.toml` in its installation
+/// directory, whatever `config_file` says. Saving, checking, or restarting with a different
+/// file would report success while the server kept its old routes, and `/health` would not
+/// notice.
+fn service_config(settings: &Config) -> Result<&Path, String> {
+ let Some(service) = &settings.service_config else {
+ return Err(
+ "The app found no install.toml next to its settings file, so it cannot \
+ tell which server config the installed server loads."
+ .into(),
+ );
+ };
+ // Comparing resolved paths accepts a symlink between the two names.
+ let resolved = |path: &Path| std::fs::canonicalize(path).ok();
+ if resolved(service).is_none() || resolved(service) != resolved(&settings.config_file) {
+ return Err(format!(
+ "The installed server loads {}, but the app settings select {}. \
+ Remove config_file from the app settings, or set it to {}.",
+ service.display(),
+ settings.config_file.display(),
+ service.display()
+ ));
+ }
+ Ok(service)
+}
+
/// This function finds the installed helper, which is the only program that reads or writes
/// server keys.
fn helper() -> Result<PathBuf, String> {
@@ -108,6 +239,120 @@ fn server_binary() -> Result<PathBuf, String> {
)
}
+/// This function checks `text` with `switchyard-server --dry-run`, then replaces `path`
+/// with it, keeping `original`, the text that `path` held, as a timestamped
+/// backup. Returns the backup's path.
+///
+/// The real file is `path`, or the file it points to when `path` is a
+/// symlink. The new text goes to a temporary file next to the real file. The
+/// temporary file is synced to disk and then renamed over the real file, so a
+/// reader never sees a half-written config. On Unix, the directory is synced
+/// after the rename, so a power loss keeps both the new file and the backup.
+/// When the check fails, or when the real file no longer holds `original`
+/// after the backup is written, the temporary file and the new backup are
+/// removed, `path` does not change, and the error is returned.
+fn save_checked(binary: &Path, path: &Path, original: &str, text: &str) -> Result<PathBuf, String> {
+ // Resolve a symlink and replace the file it points to, so the link stays.
+ let file = std::fs::canonicalize(path)
+ .map_err(|error| format!("Could not find {}: {error}", path.display()))?;
+ let dir = file.parent().unwrap_or(Path::new("/"));
+ let mut candidate = tempfile::Builder::new()
+ .prefix(".switchyard-picker-")
+ .suffix(".toml")
+ .tempfile_in(dir)
+ .map_err(|error| format!("Could not create a file in {}: {error}", dir.display()))?;
+ candidate
+ .write_all(text.as_bytes())
+ .and_then(|()| candidate.as_file().sync_all())
+ .map_err(|error| format!("Could not write {}: {error}", candidate.path().display()))?;
+ if let Ok(metadata) = std::fs::metadata(&file) {
+ candidate
+ .as_file()
+ .set_permissions(metadata.permissions())
+ .map_err(|error| {
+ format!(
+ "Could not copy the permissions of {}: {error}",
+ path.display()
+ )
+ })?;
+ }
+
+ // Name the real file in errors, not the temporary one.
+ check(binary, candidate.path()).map_err(|error| {
+ error.replace(
+ &candidate.path().display().to_string(),
+ &path.display().to_string(),
+ )
+ })?;
+
+ let backup = back_up(&file, original)
+ .map_err(|error| format!("Could not back up {}: {error}", path.display()))?;
+ // The check and the backup take a moment. Keep an edit that another
+ // program saved in the meantime. Compare right before the rename, because
+ // the rename replaces the file whatever it holds.
+ if std::fs::read_to_string(&file).ok().as_deref() != Some(original) {
+ let _ = std::fs::remove_file(&backup);
+ return Err(format!(
+ "Could not replace {}, because it changed on disk while Apply was checking it. \
+ Click Apply again to apply your choices to the new file.",
+ path.display()
+ ));
+ }
+ candidate
+ .persist(&file)
+ .map_err(|error| format!("Could not replace {}: {}", path.display(), error.error))?;
+ // The rename has already saved the file, so a failed directory sync does not
+ // return an error. Windows has no directory sync.
+ #[cfg(unix)]
+ if let Ok(directory) = std::fs::File::open(dir) {
+ let _ = directory.sync_all();
+ }
+ Ok(backup)
+}
+
+/// This function writes `text` to a new backup of `file` and returns its path. The backup
+/// is `<file>.switchyard-backup.<timestamp>`, the name the installer uses,
+/// with `-2`, `-3`, and so on added when that name is taken, so a backup
+/// never replaces another one. The backup gets the file's permissions before
+/// it gets the text.
+fn back_up(file: &Path, text: &str) -> Result<PathBuf, std::io::Error> {
+ let permissions = std::fs::metadata(file)?.permissions();
+ let stamp = Local::now().format("%Y%m%d%H%M%S");
+ for number in 1..=BACKUPS_PER_SECOND {
+ let suffix = if number == 1 {
+ String::new()
+ } else {
+ format!("-{number}")
+ };
+ let backup = PathBuf::from(format!(
+ "{}.switchyard-backup.{stamp}{suffix}",
+ file.display()
+ ));
+ let mut out = match OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(&backup)
+ {
+ Ok(out) => out,
+ Err(error) if error.kind() == ErrorKind::AlreadyExists => continue,
+ Err(error) => return Err(error),
+ };
+ let written = out
+ .set_permissions(permissions.clone())
+ .and_then(|()| out.write_all(text.as_bytes()))
+ .and_then(|()| out.sync_all());
+ if let Err(error) = written {
+ let _ = std::fs::remove_file(&backup);
+ return Err(error);
+ }
+ return Ok(backup);
+ }
+ Err(std::io::Error::new(
+ ErrorKind::AlreadyExists,
+ format!("{BACKUPS_PER_SECOND} backups from this second already exist"),
+ ))
+}
+
/// This function asks the installed helper to run the server's dry-run on `config` with the
/// saved server keys, the same keys the installed server starts with.
fn check(binary: &Path, config: &Path) -> Result<(), String> {
@@ -138,12 +383,20 @@ fn check(binary: &Path, config: &Path) -> Result<(), String> {
Err(if stderr.is_empty() { stdout } else { stderr })
}
+/// This function runs the server's config check on the file that the installed server loads.
+/// Without `install.toml` no installed server exists, so it checks the selected file.
pub fn check_config(settings: &Config) -> Result<(), String> {
- check(&server_binary()?, &settings.config_file)
+ let file = match settings.service_config {
+ Some(_) => service_config(settings)?,
+ None => &settings.config_file,
+ };
+ check(&server_binary()?, file)
}
+/// This function checks the file that the installed server loads, then restarts the server.
pub fn restart_checked(settings: &Config) -> Result<String, String> {
- check_config(settings)?;
+ let file = service_config(settings)?;
+ check(&server_binary()?, file)?;
restart(&settings.launchd_label)?;
if !wait_for_health(&settings.server_url) {
return Err(format!(
@@ -220,3 +473,88 @@ pub fn command(program: &str, args: &[&str]) -> Result<String, String> {
}
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// This function installs into `dir`: it writes `install.toml`, the service's
+ /// `composite.toml`, and app settings with `settings` as their text. It returns the path of
+ /// the app settings.
+ fn installed(dir: &Path, settings: &str) -> PathBuf {
+ let home = dir.join("home");
+ let install = switchyard_desktop::install::Settings {
+ sy_home: dir.to_path_buf(),
+ codex_home: home.join(".codex"),
+ service_dir: home.join("services"),
+ app_dir: None,
+ source: home.join("source"),
+ cargo: home.join("cargo"),
+ home,
+ port: 4123,
+ profile: "sy".into(),
+ model: "route".into(),
+ };
+ let install = toml::to_string(&install).expect("install metadata");
+ std::fs::write(dir.join("install.toml"), install).expect("install.toml");
+ std::fs::write(dir.join("composite.toml"), "# service config\n").expect("service config");
+ let path = dir.join("desktop.toml");
+ std::fs::write(&path, settings).expect("app settings");
+ path
+ }
+
+ // The installed service always loads its own composite.toml. A route edit to any other
+ // file must stop before it writes a backup, changes the file, or restarts the server, and
+ // Check server config and Restart server must not check the wrong file.
+ #[test]
+ fn server_config_actions_use_only_the_config_the_installed_server_loads() {
+ let dir = tempfile::tempdir().expect("directory");
+ let custom = dir.path().join("custom.toml");
+ std::fs::write(&custom, "# custom\n").expect("custom config");
+ let path = installed(dir.path(), &format!("config_file = {custom:?}\n"));
+ let settings = Config::load(&path).expect("settings");
+ let before = std::fs::read_dir(dir.path()).expect("files").count();
+ let edited = crate::server_config::Edited {
+ text: "# edited\n".into(),
+ notes: Vec::new(),
+ };
+ let error = save_edit(&settings, "# custom\n", edited, "Updated the route.")
+ .err()
+ .expect("a different file");
+ assert!(error.contains("The installed server loads"), "{error}");
+ assert_eq!(
+ std::fs::read_to_string(&custom).expect("custom"),
+ "# custom\n"
+ );
+ assert_eq!(
+ std::fs::read_dir(dir.path()).expect("files").count(),
+ before
+ );
+ for error in [
+ check_config(&settings).expect_err("check"),
+ restart_checked(&settings).expect_err("restart"),
+ ] {
+ assert!(error.contains("The installed server loads"), "{error}");
+ }
+
+ // Without install.toml the app cannot tell which file the server loads.
+ let bare = tempfile::tempdir().expect("directory");
+ let settings = Config::load(&bare.path().join("desktop.toml")).expect("defaults");
+ assert!(
+ service_config(&settings)
+ .expect_err("no install metadata")
+ .contains("no install.toml")
+ );
+
+ // The default config_file and a symlink to the service's file both pass.
+ let path = installed(dir.path(), "");
+ service_config(&Config::load(&path).expect("settings")).expect("default file");
+ #[cfg(unix)]
+ {
+ let link = dir.path().join("link.toml");
+ std::os::unix::fs::symlink(dir.path().join("composite.toml"), &link).expect("link");
+ std::fs::write(&path, format!("config_file = {link:?}\n")).expect("app settings");
+ service_config(&Config::load(&path).expect("settings")).expect("symlink");
+ }
+ }
+}
diff --git a/crates/switchyard-desktop/src/server_config.rs b/crates/switchyard-desktop/src/server_config.rs
index e6f6b09be..dd04d27a1 100644
--- a/crates/switchyard-desktop/src/server_config.rs
+++ b/crates/switchyard-desktop/src/server_config.rs
@@ -273,6 +273,16 @@ const TARGET_SETTINGS: [&str; 4] = [
"omit_body_fields",
];
+/// Route describes one entry under `[routes]`.
+#[derive(Clone, Debug, PartialEq)]
+pub struct Route {
+ /// This field names the route's table in this file. Callers never send it.
+ pub key: String,
+ /// This field sets the public model ID that callers send.
+ pub id: String,
+ pub kind: String,
+}
+
/// Client describes one entry under `[llm_clients]`.
#[derive(Clone, Debug, PartialEq)]
pub struct Client {
@@ -369,6 +379,8 @@ impl Algorithm {
/// Edited stores the result of [`ServerConfig::edit`].
#[derive(Debug)]
pub struct Edited {
+ /// This field stores the new config text.
+ pub text: String,
/// This field stores user-facing notes about the edit.
pub notes: Vec<String>,
}
@@ -396,6 +408,16 @@ impl ServerConfig {
})
}
+ pub fn routes(&self) -> Vec<Route> {
+ entries(self.table("routes"))
+ .map(|(key, route)| Route {
+ key: key.to_string(),
+ id: text(route, "id").unwrap_or(key).to_string(),
+ kind: text(route, "type").unwrap_or_default().to_string(),
+ })
+ .collect()
+ }
+
pub fn clients(&self) -> Vec<Client> {
entries(self.table("llm_clients"))
.map(|(name, client)| Client {
@@ -618,7 +640,10 @@ impl ServerConfig {
set_at(table, path, setting.value());
}
}
- Ok(Edited { notes })
+ Ok(Edited {
+ text: doc.to_string(),
+ notes,
+ })
}
fn table(&self, name: &str) -> Option<&dyn TableLike> {
diff --git a/crates/switchyard-desktop/src/tui.rs b/crates/switchyard-desktop/src/tui.rs
index fc49da5b5..1da19a0ec 100644
--- a/crates/switchyard-desktop/src/tui.rs
+++ b/crates/switchyard-desktop/src/tui.rs
@@ -12,6 +12,7 @@ use crate::controller::{
use crate::history::Entry;
use crate::pricing::Estimate;
use crate::rollup::{CLASSIFIER_TIER, ModelTokens, Totals, Usage};
+use crate::server_config::Choice;
use crate::summary::{money, tokens};
use chrono::{DateTime, Utc};
use ratatui::{
@@ -148,10 +149,14 @@ const ACTIONS: &[ActionRow] = &[
(
Tab::Routes,
'e',
- "preview",
- "Checks the selected route's current algorithm and models against the server \
- config and shows the notes. Nothing is saved.",
- &[ActionKind::PreviewRoute],
+ "edit",
+ "Changes the selected route's algorithm and models. Enter reviews the change, and \
+ typing SAVE saves it and restarts the server.",
+ &[
+ ActionKind::Editor,
+ ActionKind::PreviewRoute,
+ ActionKind::Apply,
+ ],
),
(
Tab::Routes,
@@ -413,6 +418,21 @@ enum FormKind {
RemoveKey,
/// The token comes from the update preview that the user read.
Update(Option<String>),
+ /// The route is the snapshot's copy from when the user opened the editor. The models form
+ /// keeps its revision and choices, so a server config that changed since then fails the
+ /// review instead of mixing old choices into the new file.
+ Algorithm {
+ generation: u64,
+ route: Box<RouteInfo>,
+ },
+ RouteModels {
+ generation: u64,
+ route: String,
+ algorithm: String,
+ revision: Option<String>,
+ },
+ /// The action is the one that the user reviewed. Typing SAVE submits it unchanged.
+ Save(Box<Action>),
Models,
/// This map holds each endpoint's base URL when the form opened. The controller refuses
/// the key if the chosen endpoint's URL no longer matches.
@@ -420,6 +440,7 @@ enum FormKind {
}
/// Choices names the list that ↑ and ↓ cycle through in a form field.
enum Choices {
+ Algorithms,
Endpoints,
}
/// Submit is what a form sends: a controller action.
@@ -511,9 +532,25 @@ impl Form {
fn value(&self, index: usize) -> String {
self.fields[index].1.trim().to_string()
}
+ /// This function reads the choices from fields that alternate endpoint and model.
+ fn choices(&self) -> Vec<Choice> {
+ self.fields
+ .as_chunks::<2>()
+ .0
+ .iter()
+ .map(|pair| Choice {
+ client: pair[0].1.trim().into(),
+ model: pair[1].1.trim().into(),
+ })
+ .collect()
+ }
/// This function names the list that the selected field chooses from, if any.
fn options(&self) -> Option<Choices> {
match self.kind {
+ FormKind::Algorithm { .. } => Some(Choices::Algorithms),
+ FormKind::RouteModels { .. } if self.index.is_multiple_of(2) => {
+ Some(Choices::Endpoints)
+ }
FormKind::Models | FormKind::ModelKey(_) if self.index == 0 => Some(Choices::Endpoints),
_ => None,
}
@@ -570,6 +607,27 @@ impl Form {
preview_token: preview_token.clone(),
}
}
+ FormKind::Algorithm { generation, route } => Action::Editor {
+ generation: *generation,
+ route: route.key.clone(),
+ algorithm: self.value(0),
+ },
+ FormKind::RouteModels {
+ generation,
+ route,
+ algorithm,
+ revision,
+ } => Action::PreviewRoute {
+ generation: *generation,
+ route: route.clone(),
+ algorithm: algorithm.clone(),
+ choices: self.choices(),
+ revision: revision.clone(),
+ },
+ FormKind::Save(action) => {
+ confirm("SAVE", "Type SAVE to apply the reviewed changes.")?;
+ (**action).clone()
+ }
FormKind::Models => Action::Models {
client: self.value(0),
refresh: true,
@@ -591,13 +649,84 @@ impl Form {
FormKind::ImportKey => "Importing the server key",
FormKind::RemoveKey => "Removing the server key",
FormKind::Update(_) => "Starting the update",
-
+ FormKind::Algorithm { .. } => "Opening the route editor",
+ FormKind::RouteModels { .. } => "Reviewing the route",
+ FormKind::Save(_) => "Saving the route",
FormKind::Models => "Loading models",
FormKind::ModelKey(_) => "Saving the key and loading models",
}
}
}
+/// This function returns an endpoint field and a model field for each role and its choice.
+fn role_fields<'a>(roles: impl Iterator<Item = (&'a str, Choice)>) -> Vec<(String, String)> {
+ roles
+ .flat_map(|(label, choice)| {
+ let model = if label == "Model" {
+ label.to_string()
+ } else {
+ format!("{label} model")
+ };
+ [
+ (format!("{label} endpoint"), choice.client),
+ (model, choice.model),
+ ]
+ })
+ .collect()
+}
+
+/// This function opens the models form after the controller returns the roles of `algorithm`.
+fn route_models_form(
+ generation: u64,
+ route: &RouteInfo,
+ algorithm: String,
+ editor: &Value,
+) -> Result<Form, String> {
+ let roles = editor["roles"].as_array().ok_or("No roles returned.")?;
+ let fields = role_fields(roles.iter().enumerate().map(|(index, role)| {
+ // A role keeps its model by tier when the new algorithm changes the role order.
+ let matching = route.tiers.as_ref().and_then(|tiers| {
+ let tier = role["tier"].as_str()?;
+ tiers.iter().position(|t| t.as_deref() == Some(tier))
+ });
+ let choice = matching
+ .and_then(|index| route.choices.get(index))
+ .or_else(|| (algorithm == route.kind).then(|| route.choices.get(index))?)
+ .or_else(|| route.choices.first())
+ .cloned()
+ .unwrap_or_default();
+ (role["label"].as_str().unwrap_or_default(), choice)
+ }));
+ let kind = FormKind::RouteModels {
+ generation,
+ route: route.key.clone(),
+ algorithm,
+ revision: Some(route.revision.clone()),
+ };
+ Ok(Form::new(kind, format!("Models for {}", route.id), fields))
+}
+
+/// This function returns the action that a preview's review window submits after the user
+/// types SAVE: the previewed inputs, plus the token for a route change.
+fn reviewed(previewed: Action) -> Option<Action> {
+ Some(match previewed {
+ Action::PreviewRoute {
+ generation,
+ route,
+ algorithm,
+ choices,
+ revision,
+ } => Action::Apply {
+ generation,
+ route,
+ algorithm,
+ choices,
+ revision,
+ },
+ _ => return None,
+ })
+}
+
enum Popup {
Help {
scroll: u16,
@@ -958,6 +1087,7 @@ impl App {
}
}
}
+ let action = busy.and_then(|busy| busy.action);
let reply = reply?;
match (kind, reply) {
(Some(ActionKind::ServerKeys), Err(error)) => {
@@ -991,28 +1121,47 @@ impl App {
"Type UPDATE to start",
)));
}
- (Some(ActionKind::PreviewRoute), Ok(reply)) => {
- if !popup_matches {
- self.report(
- "The route preview finished while another window was open. \
- Press e on Routes to preview it again."
- .into(),
- Tone::Done,
- false,
- );
- return None;
+ // The reply lists the roles of the chosen algorithm. The models form replaces the
+ // algorithm form only while that form is still open for the same route.
+ (Some(ActionKind::Editor), Ok(reply)) if popup_matches => {
+ if let Some(Action::Editor {
+ route: key,
+ algorithm,
+ ..
+ }) = action
+ && let Some(Popup::Form(form)) = &self.popup
+ && let FormKind::Algorithm { generation, route } = &form.kind
+ && route.key == key
+ {
+ match route_models_form(*generation, route, algorithm, &reply.data) {
+ Ok(next) => self.set_popup(Some(Popup::Form(next))),
+ Err(error) => self.notify(error, Tone::Error),
+ }
+ }
+ }
+ (Some(ActionKind::PreviewRoute), Ok(_)) if !popup_matches => {
+ self.report(
+ "The route preview finished while another window was open. \
+ Press e on Routes to edit the route again."
+ .into(),
+ Tone::Done,
+ false,
+ );
+ }
+ // The review window replaces the form that asked for the preview, so a form that
+ // the user closed meanwhile stays closed.
+ (Some(ActionKind::PreviewRoute), Ok(reply)) if popup_matches => {
+ if matches!(self.popup, Some(Popup::Form(_)))
+ && let Some(reviewed) = action.and_then(reviewed)
+ {
+ self.set_popup(Some(Popup::review(
+ change_lines(self.theme, &reply.data),
+ "Save",
+ FormKind::Save(Box::new(reviewed)),
+ "Review route changes",
+ "Type SAVE to apply",
+ )));
}
- let notes: Vec<&str> = reply.data["notes"]
- .as_array()
- .map(|items| items.iter().filter_map(Value::as_str).collect())
- .unwrap_or_default();
- let file = reply.data["file"].as_str().unwrap_or_default();
- self.set_popup(Some(Popup::Message {
- title: "Route preview",
- text: format!("Nothing was saved to {file}.\n\n{}", notes.join("\n")),
- tone: Tone::Done,
- scroll: 0,
- }));
}
(Some(ActionKind::Models), Ok(reply)) if popup_matches => {
let models: Vec<&str> = reply.data["models"]
@@ -1374,18 +1523,17 @@ impl App {
let request = |action, label| Ok(Some(Request::run(action, label)));
let generation = self.data.generation;
match kind {
- ActionKind::PreviewRoute => {
- let route = self.selected_route()?.clone();
- request(
- Action::PreviewRoute {
- generation,
- route: route.key,
- algorithm: route.kind,
- choices: route.choices,
- revision: Some(route.revision),
- },
- "Previewing the route",
- )
+ // The algorithm form submits Editor, the models form submits PreviewRoute, and the
+ // review window submits Apply.
+ ActionKind::Editor | ActionKind::PreviewRoute | ActionKind::Apply => {
+ let route = Box::new(self.selected_route()?.clone());
+ let title = format!("Routing algorithm for {}", route.id);
+ let fields = vec![("Algorithm", route.kind.clone())];
+ self.open(Form::new(
+ FormKind::Algorithm { generation, route },
+ title,
+ fields,
+ ))
}
ActionKind::Models => {
let client = self.data.clients.first();
@@ -1494,6 +1642,9 @@ impl App {
KeyCode::Up | KeyCode::Down | KeyCode::PageUp | KeyCode::PageDown => {
let forward = key.code == KeyCode::Down;
match (form.options(), key.code) {
+ (Some(Choices::Algorithms), KeyCode::Up | KeyCode::Down) => {
+ form.choose(self.data.algorithms.iter().map(|a| a.kind), forward);
+ }
(Some(Choices::Endpoints), KeyCode::Up | KeyCode::Down) => {
let names = self.data.clients.iter().map(|c| c.name.as_str());
form.choose(names, forward);
@@ -3137,6 +3288,22 @@ fn draw_popup(frame: &mut Frame, app: &mut App, area: Rect) {
fn form_notes(app: &App, form: &Form) -> Vec<Line<'static>> {
let theme = app.theme;
let data = &app.data;
+ let algorithms = |selected: &str| {
+ let names: Vec<String> = data
+ .algorithms
+ .iter()
+ .map(|known| format!("{} ({})", known.title, known.kind))
+ .collect();
+ let mut lines = vec![Line::styled(
+ format!("Algorithms: {}", names.join(" · ")),
+ theme.muted,
+ )];
+ if let Some(known) = data.algorithms.iter().find(|a| a.kind == selected.trim()) {
+ lines.push(Line::from(known.summary));
+ }
+ lines.push(Line::default());
+ lines
+ };
match &form.kind {
FormKind::SaveKey | FormKind::ImportKey | FormKind::RemoveKey => {
let mut lines = vec![Line::styled("Server keys", theme.muted)];
@@ -3144,7 +3311,15 @@ fn form_notes(app: &App, form: &Form) -> Vec<Line<'static>> {
lines.push(Line::default());
lines
}
-
+ FormKind::Algorithm { .. } => algorithms(&form.fields[0].1),
+ FormKind::RouteModels { .. } => {
+ let names: Vec<&str> = data.clients.iter().map(|c| c.name.as_str()).collect();
+ vec![
+ Line::styled(format!("Endpoints: {}", names.join(", ")), theme.muted),
+ Line::from("Use the endpoint name and a model ID. Unlisted model IDs are allowed."),
+ Line::default(),
+ ]
+ }
FormKind::Models | FormKind::ModelKey(_) => {
let key = matches!(form.kind, FormKind::ModelKey(_));
let mut lines: Vec<Line> = data
@@ -3166,8 +3341,24 @@ fn form_notes(app: &App, form: &Form) -> Vec<Line<'static>> {
lines.push(Line::default());
lines
}
- FormKind::Preferences | FormKind::Update(_) => Vec::new(),
+ FormKind::Preferences | FormKind::Update(_) | FormKind::Save(_) => Vec::new(),
+ }
+}
+
+/// This function shows the file and notes of a route preview before the user types SAVE.
+fn change_lines(theme: Theme, preview: &Value) -> Vec<Line<'static>> {
+ let file = preview["file"].as_str().unwrap_or_default().to_string();
+ let mut lines = vec![labeled(theme, "Changed file", 13, file)];
+ let notes = preview["notes"].as_array().map(Vec::as_slice);
+ for note in notes.unwrap_or_default().iter().filter_map(Value::as_str) {
+ lines.push(Line::default());
+ lines.push(Line::from(note.to_string()));
}
+ lines.push(Line::default());
+ lines.push(Line::from(
+ "Switchyard validates the config, saves a backup, and restarts the server.",
+ ));
+ lines
}
fn update_lines(theme: Theme, preview: &Value) -> Vec<Line<'static>> {