rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `[browser]`: which browser the daemon starts, with what sandbox, and when
//! it closes. A build without the `browser` feature still reads the table,
//! so one config file serves both.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::Duration;

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub(crate) struct BrowserConfig {
    /// A browser to run instead of the pinned chrome-headless-shell, such as
    /// a system Chrome. It must be absolute: the daemon and
    /// `rho browser install` run in different directories.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "absolute_path"
    )]
    pub(crate) executable: Option<PathBuf>,
    /// `false` runs the browser without Chrome's sandbox. Unset means on.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) sandbox: Option<bool>,
    /// How long the browser may go unused before it closes, such as `"5m"`.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "duration_text"
    )]
    pub(crate) unload_idle_after: Option<Duration>,
    /// `true` offers `javascript_exec`, which runs the model's JavaScript
    /// in the page with the page's own privileges. Off by default.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub(crate) javascript_exec: bool,
    /// A directory of files the model may upload with `file_upload`, which
    /// is offered only when this is set. It must be absolute, and only files
    /// inside it, after following links, are accepted.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "absolute_path"
    )]
    pub(crate) upload_dir: Option<PathBuf>,
}

impl BrowserConfig {
    pub(crate) fn is_empty(&self) -> bool {
        self == &Self::default()
    }

    /// The part of the config the running browser was started with: which
    /// tools sessions get does not need a new browser.
    #[cfg(feature = "browser")]
    pub(crate) fn launch(&self) -> Self {
        Self {
            javascript_exec: false,
            upload_dir: None,
            ..self.clone()
        }
    }
}

fn absolute_path<'de, D: serde::Deserializer<'de>>(
    deserializer: D,
) -> Result<Option<PathBuf>, D::Error> {
    let path = PathBuf::deserialize(deserializer)?;
    match path.is_absolute() {
        true => Ok(Some(path)),
        false => Err(serde::de::Error::custom(format!(
            "must be an absolute path; got {}",
            path.display()
        ))),
    }
}

/// An optional duration written as text, such as `"5m"` or `"90s"`.
mod duration_text {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::time::Duration;

    pub(super) fn serialize<S: Serializer>(
        duration: &Option<Duration>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match duration {
            Some(duration) => {
                serializer.serialize_str(&humantime::format_duration(*duration).to_string())
            }
            None => serializer.serialize_none(),
        }
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<Duration>, D::Error> {
        let text = String::deserialize(deserializer)?;
        let duration = crate::app::automation_protocol::parse_duration(text.trim())
            .map_err(serde::de::Error::custom)?;
        if duration.is_zero() {
            return Err(serde::de::Error::custom("must be greater than zero"));
        }
        Ok(Some(duration))
    }
}

#[cfg(all(test, feature = "browser"))]
#[path = "config_tests.rs"]
mod tests;