agent: config.rs — CLI args (server, session, fps, quality, display, audio, heartbeat, reconnect)

This commit is contained in:
Butterfly Dev 2026-04-07 04:34:53 +00:00
parent 56f6e88389
commit 5a26c7c60e

90
agent/src/config.rs Normal file
View File

@ -0,0 +1,90 @@
//! Agent configuration parsed from CLI arguments and environment variables.
use clap::Parser;
use std::time::Duration;
/// Butterfly Desktop Agent — captures display from this machine and streams
/// it to a Butterfly server. Also receives and executes HUD commands (mouse,
/// keyboard) from remote viewers for full remote control.
#[derive(Parser, Debug)]
#[command(name = "butterfly-agent", version, about)]
pub struct AgentConfig {
/// WebSocket URL of the Butterfly server.
///
/// Example: ws://192.168.1.100:8080
/// The agent will connect to ws://<server>/ws/<session_id>?client_type=agent
#[arg(long, default_value = "ws://localhost:8080", env = "BUTTERFLY_SERVER")]
pub server: String,
/// Session ID to connect to. If not provided, the agent will request the
/// server to create a new session via REST API first.
#[arg(long, env = "BUTTERFLY_SESSION_ID")]
pub session_id: Option<String>,
/// Target capture frame rate (frames per second).
#[arg(long, default_value_t = 30, env = "BUTTERFLY_FPS")]
pub fps: u32,
/// JPEG encoding quality (1100). Lower = smaller frames, less quality.
#[arg(long, default_value_t = 60, env = "BUTTERFLY_QUALITY")]
pub quality: u8,
/// Index of the display to capture (0 = primary, 1 = second, etc.).
#[arg(long, default_value_t = 0, env = "BUTTERFLY_DISPLAY")]
pub display: usize,
/// Enable audio capture and streaming.
#[arg(long, default_value_t = false, env = "BUTTERFLY_AUDIO")]
pub audio: bool,
/// Heartbeat interval in seconds.
#[arg(long, default_value_t = 15, env = "BUTTERFLY_HEARTBEAT")]
pub heartbeat_secs: u64,
/// Reconnection delay in seconds (0 = no reconnect).
#[arg(long, default_value_t = 3, env = "BUTTERFLY_RECONNECT_DELAY")]
pub reconnect_delay_secs: u64,
/// Maximum reconnection attempts (0 = infinite).
#[arg(long, default_value_t = 0, env = "BUTTERFLY_MAX_RECONNECT")]
pub max_reconnect: u32,
}
impl AgentConfig {
/// Parse configuration from CLI arguments.
pub fn parse_args() -> Self {
Self::parse()
}
/// Duration between captured frames.
pub fn frame_interval(&self) -> Duration {
Duration::from_secs_f64(1.0 / self.fps as f64)
}
/// Duration between heartbeat messages.
pub fn heartbeat_interval(&self) -> Duration {
Duration::from_secs(self.heartbeat_secs)
}
/// Duration to wait before reconnecting.
pub fn reconnect_delay(&self) -> Duration {
Duration::from_secs(self.reconnect_delay_secs)
}
/// Build the full WebSocket URL for a given session ID.
pub fn ws_url(&self, session_id: &str) -> String {
format!(
"{}/ws/{}?client_type=agent",
self.server.trim_end_matches('/'),
session_id
)
}
/// Build the REST API base URL.
pub fn api_base(&self) -> String {
format!(
"{}/api",
self.server.replace("ws://", "http://").replace("wss://", "https://")
)
}
}