Repository navigation
feat(desktop): open a basic window for server status and usage #1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /gen |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /// Supported desktop platforms need Tauri resources and command metadata at build time. | ||
| fn main() { | ||
| #[cfg(any(target_os = "macos", windows))] | ||
| tauri_build::build(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! The desktop window shows server health and recorded request and token counts. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::sync::{Arc, Mutex}; | ||
| use switchyard_desktop::{config::Config, health::Probe, rollup}; | ||
| use tauri::State; | ||
|
|
||
| /// The window retains the probe and reader so refreshes reuse the health worker and log offset. | ||
| struct Desktop { | ||
| settings: PathBuf, | ||
| health: Probe, | ||
| reader: rollup::Reader, | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| struct Counts { | ||
| requests: u64, | ||
| tokens: u64, | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| struct Snapshot { | ||
| server_url: String, | ||
| health: serde_json::Value, | ||
| today: Option<Counts>, | ||
| week: Option<Counts>, | ||
| errors: Vec<String>, | ||
| refresh_seconds: u64, | ||
| } | ||
|
|
||
| type Shared = Arc<Mutex<Desktop>>; | ||
|
|
||
| /// The worker reads health and usage while the mutex prevents overlapping refreshes. | ||
| #[tauri::command] | ||
| async fn snapshot(state: State<'_, Shared>) -> Result<Snapshot, String> { | ||
| let shared = state.inner().clone(); | ||
| tauri::async_runtime::spawn_blocking(move || { | ||
| let mut desktop = shared | ||
| .try_lock() | ||
| .map_err(|_| "A refresh is running. Try again when it finishes.".to_string())?; | ||
| let config = Config::load(&desktop.settings)?; | ||
| let health = desktop.health.details(&config.server_url); | ||
| let mut errors = Vec::new(); | ||
| // A failed read leaves counts absent so the window cannot show incomplete usage as zero. | ||
| let usage = desktop | ||
| .reader | ||
| .read(&config.routing_log, chrono::Local::now().date_naive()) | ||
| .map_err(|error| errors.push(format!("Could not read usage: {error}"))) | ||
| .ok(); | ||
| let counts = |totals: &rollup::Totals| Counts { | ||
| requests: totals.requests(), | ||
| tokens: totals.tokens(), | ||
| }; | ||
| Ok(Snapshot { | ||
| server_url: config.server_url, | ||
| health, | ||
| today: usage.as_ref().map(|usage| counts(&usage.today)), | ||
| week: usage.as_ref().map(|usage| counts(&usage.week)), | ||
| errors, | ||
| refresh_seconds: config.refresh_seconds.max(1), | ||
| }) | ||
| }) | ||
| .await | ||
| .map_err(|error| error.to_string())? | ||
| } | ||
|
|
||
| #[derive(serde::Deserialize)] | ||
| #[serde(rename_all = "snake_case")] | ||
| enum FileKind { | ||
| Config, | ||
| Logs, | ||
| } | ||
|
|
||
| /// The worker waits for any refresh, then opens the configured file after releasing the mutex. | ||
| #[tauri::command] | ||
| async fn open_file(state: State<'_, Shared>, kind: FileKind) -> Result<(), String> { | ||
| let shared = state.inner().clone(); | ||
| tauri::async_runtime::spawn_blocking(move || { | ||
| let config = { | ||
| let desktop = shared | ||
| .lock() | ||
| .map_err(|_| "Could not access desktop state after a worker failed.".to_string())?; | ||
| Config::load(&desktop.settings)? | ||
| }; | ||
| let path = match kind { | ||
| FileKind::Config => config.config_file, | ||
| FileKind::Logs => config.routing_log, | ||
| }; | ||
| #[cfg(target_os = "macos")] | ||
| { | ||
| let output = std::process::Command::new("open") | ||
| .arg("--") | ||
| .arg(&path) | ||
| .output() | ||
| .map_err(|error| format!("Could not open {}: {error}", path.display()))?; | ||
| if !output.status.success() { | ||
| return Err(format!( | ||
| "Could not open {}: {}", | ||
| path.display(), | ||
| String::from_utf8_lossy(&output.stderr).trim() | ||
| )); | ||
| } | ||
| Ok(()) | ||
| } | ||
| #[cfg(windows)] | ||
| { | ||
| use std::os::windows::ffi::OsStrExt; | ||
| use windows_sys::Win32::UI::{ | ||
| Shell::ShellExecuteW, WindowsAndMessaging::SW_SHOWNORMAL, | ||
| }; | ||
| let path: Vec<u16> = path.as_os_str().encode_wide().chain(Some(0)).collect(); | ||
| let status = unsafe { | ||
| ShellExecuteW( | ||
| std::ptr::null_mut(), | ||
| std::ptr::null(), | ||
| path.as_ptr(), | ||
| std::ptr::null(), | ||
| std::ptr::null(), | ||
| SW_SHOWNORMAL, | ||
| ) | ||
| } as isize; | ||
| if status > 32 { | ||
| Ok(()) | ||
| } else { | ||
| Err(format!("Windows could not open the file (error {status}).")) | ||
| } | ||
| } | ||
| }) | ||
| .await | ||
| .map_err(|error| error.to_string())? | ||
| } | ||
|
|
||
| /// Quitting closes the window and stops the application. | ||
| #[tauri::command] | ||
| fn quit_app(app: tauri::AppHandle) { | ||
| app.exit(0); | ||
| } | ||
|
|
||
| /// The application owns one health probe and log reader for the lifetime of the window. | ||
| pub fn run(settings: PathBuf) -> Result<(), String> { | ||
| tauri::Builder::default() | ||
| .manage(Arc::new(Mutex::new(Desktop { | ||
| settings, | ||
| health: Probe::default(), | ||
| reader: rollup::Reader::default(), | ||
| }))) | ||
| .invoke_handler(tauri::generate_handler![snapshot, open_file, quit_app]) | ||
| .run(tauri::generate_context!()) | ||
| .map_err(|error| error.to_string()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! The desktop window shows local server health and recorded usage. | ||
|
|
||
| #![cfg_attr(windows, windows_subsystem = "windows")] | ||
|
|
||
| use switchyard_desktop::config; | ||
| #[cfg(any(target_os = "macos", windows))] | ||
| mod gui; | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::ExitCode; | ||
|
|
||
| use config::Config; | ||
|
|
||
| const USAGE: &str = "\ | ||
| Usage: switchyard-desktop [--validate-toml] [FILE] | ||
|
|
||
| This app opens a window with server status and recorded usage. | ||
| The desktop window supports macOS and Windows. | ||
|
|
||
| FILE The app reads settings from FILE. | ||
| --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."; | ||
|
|
||
| /// The command checks TOML syntax or opens the desktop window. | ||
| fn main() -> ExitCode { | ||
| // A desktop launch may have no parent console, so attachment failure does not stop the app. | ||
| #[cfg(windows)] | ||
| let _ = unsafe { | ||
| windows_sys::Win32::System::Console::AttachConsole( | ||
| windows_sys::Win32::System::Console::ATTACH_PARENT_PROCESS, | ||
| ) | ||
| }; | ||
| let mut settings = None; | ||
| let mut validate_toml = false; | ||
| for arg in std::env::args().skip(1) { | ||
| match arg.as_str() { | ||
| "-h" | "--help" => { | ||
| println!("{USAGE}"); | ||
| return ExitCode::SUCCESS; | ||
| } | ||
| "--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)), | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| }; | ||
| #[cfg(any(target_os = "macos", windows))] | ||
| match gui::run(settings) { | ||
| Ok(()) => ExitCode::SUCCESS, | ||
| Err(error) => { | ||
| eprintln!("switchyard-desktop: {error}"); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
| #[cfg(not(any(target_os = "macos", windows)))] | ||
| { | ||
| let _ = settings; | ||
| eprintln!("The desktop window supports macOS and Windows."); | ||
| ExitCode::FAILURE | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "productName": "Switchyard", | ||
| "version": "0.3.0", | ||
| "identifier": "com.nvidia.switchyard", | ||
| "build": { | ||
| "frontendDist": "ui" | ||
| }, | ||
| "app": { | ||
| "withGlobalTauri": true, | ||
| "windows": [ | ||
| { | ||
| "label": "main", | ||
| "title": "Switchyard", | ||
| "width": 680, | ||
| "height": 500, | ||
| "minWidth": 440, | ||
| "minHeight": 400 | ||
| } | ||
| ], | ||
| "security": { | ||
| "csp": "default-src 'self'; script-src 'self'; style-src 'self'; connect-src ipc: http://ipc.localhost; img-src 'self' data:; object-src 'none'; frame-src 'none'; base-uri 'none'" | ||
| } | ||
| }, | ||
| "bundle": { | ||
| "active": false, | ||
| "icon": [ | ||
| "icons/icon.icns", | ||
| "icons/icon.png", | ||
| "icons/icon.ico" | ||
| ] | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.