Skip to main content

nexo_plugin_browser/
config.rs

1//! Phase 93.4.e — plugin-owned config types.
2//!
3//! Until 0.2.4 this plugin re-exported `nexo_config::BrowserConfig`.
4//! Phase 93 inverts: each plugin owns its config contract
5//! (manifest's `[plugin.config_schema]` + this module's Rust
6//! definitions); the daemon delivers the operator YAML opaquely
7//! via `plugin.configure` JSON-RPC.
8//!
9//! Field shapes mirror `nexo_config::types::plugins::BrowserConfig`
10//! verbatim — operator YAML keeps working unchanged.
11
12use serde::{Deserialize, Serialize};
13
14/// Operator YAML wire shape. The top-level `browser:` key accepts either
15/// a single map (legacy 0.2.x single-instance) or a sequence of maps
16/// (0.3.0+ multi-instance). The plugin normalises both to
17/// `Vec<BrowserConfig>` via [`BrowserPluginShape::into_vec`].
18#[derive(Debug, Deserialize, Serialize, Clone)]
19#[serde(deny_unknown_fields)]
20pub struct BrowserPluginConfigFile {
21    pub browser: BrowserPluginShape,
22}
23
24/// 0.2.x compatibility alias. Existing operator YAML keeps working;
25/// new code should prefer [`BrowserPluginConfigFile`].
26pub type BrowserConfigFile = BrowserPluginConfigFile;
27
28#[allow(clippy::large_enum_variant)]
29#[derive(Debug, Deserialize, Serialize, Clone)]
30#[serde(untagged)]
31pub enum BrowserPluginShape {
32    Single(BrowserConfig),
33    Many(Vec<BrowserConfig>),
34}
35
36impl BrowserPluginShape {
37    /// Flatten to the canonical `Vec<BrowserConfig>` the registry
38    /// consumes. Single-map shape produces a 1-element vec so the
39    /// downstream boot loop is shape-agnostic.
40    pub fn into_vec(self) -> Vec<BrowserConfig> {
41        match self {
42            Self::Single(c) => vec![c],
43            Self::Many(v) => v,
44        }
45    }
46}
47
48#[derive(Debug, Clone, Deserialize, Serialize)]
49#[serde(deny_unknown_fields)]
50pub struct BrowserConfig {
51    /// Instance label. `None` ⇒ legacy single-instance behaviour
52    /// (resolved to `"default"` at boot time).
53    #[serde(default)]
54    pub instance: Option<String>,
55    #[serde(default)]
56    pub headless: bool,
57    #[serde(default)]
58    pub executable: String,
59    /// Empty = launch new Chrome. Set to e.g. "http://127.0.0.1:9222" to attach.
60    #[serde(default)]
61    pub cdp_url: String,
62    /// Empty ⇒ daemon resolves to `${state_dir}/instances/<resolved_label>/`
63    /// at boot time. An explicit non-empty value overrides the auto-resolved
64    /// path so operators can pin a profile to a shared filesystem location.
65    #[serde(default = "default_user_data_dir")]
66    pub user_data_dir: String,
67    #[serde(default = "default_window_width")]
68    pub window_width: u32,
69    #[serde(default = "default_window_height")]
70    pub window_height: u32,
71    #[serde(default = "default_connect_timeout_ms")]
72    pub connect_timeout_ms: u64,
73    #[serde(default = "default_command_timeout_ms")]
74    pub command_timeout_ms: u64,
75    /// Extra CLI flags forwarded verbatim to the spawned Chrome/Chromium
76    /// process.
77    #[serde(default)]
78    pub args: Vec<String>,
79    /// Agents permitted to route browser tools to this instance. Empty
80    /// = accept any agent (back-compat with 0.2.x).
81    #[serde(default)]
82    pub allow_agents: Vec<String>,
83}
84
85fn default_user_data_dir() -> String {
86    "./data/browser/profile".to_string()
87}
88fn default_window_width() -> u32 {
89    1280
90}
91fn default_window_height() -> u32 {
92    800
93}
94fn default_connect_timeout_ms() -> u64 {
95    10_000
96}
97fn default_command_timeout_ms() -> u64 {
98    15_000
99}