Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
feat(cli): configure screencast intervals in milliseconds
ldm0e688652948de
diff --git a/README.md b/README.mdindex 87e3774f4..d5b09d97b 100644--- a/README.md+++ b/README.md@@ -241,7 +241,7 @@ enabled by default:| --- | --- || Default | `LayoutPolicy::Mock` — deterministic geometry in a compatible format, with no real layout or paint || `--layout` | `LayoutPolicy::OnDemand` — real layout, geometry, hit-testing, coordinate input, screenshots, screencast |-| `--cdp-screencast-fps <FPS>` | Set the CDP screencast rate from `1` to `60`; requires `--layout` (default: `1`) |+| `--screencast-interval <MILLISECONDS>` | Set a positive integer interval between screencast captures, in milliseconds; requires `--layout` (default: `1000`). For example, `33` targets about 30 FPS. || `--resource` | Fetch all optional visual/media resource families || `--image`, `--font`, `--audio`, `--video`, `--media`, `--text-track` | Enable one specific optional resource family || `--profile-dir`, `--http-cache-dir`, `--cookie-file` | Selectively enable the persistence required by the workload |diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.mdindex 6e1cab09c..d2e94227f 100644--- a/docs/README.zh-CN.md+++ b/docs/README.zh-CN.md@@ -190,7 +190,7 @@ Moli 是 Lexmount 旗下的开源无头浏览器;Lexmount Browser 则是围绕| --- | --- || 默认 | `LayoutPolicy::Mock`——返回确定性的、格式兼容的几何信息,不执行真实的布局或绘制 || `--layout` | `LayoutPolicy::OnDemand`——提供真实的布局、几何信息、命中测试、坐标输入、截图和屏幕串流 |-| `--cdp-screencast-fps <FPS>` | 设置 `1` 到 `60` 的 CDP 屏幕串流帧率;需要同时启用 `--layout`(默认:`1`) |+| `--screencast-interval <MILLISECONDS>` | 设置屏幕串流截帧间隔,单位毫秒,须为正整数;需要同时启用 `--layout`(默认:`1000`)。例如 `33` 对应约 30 FPS。 || `--resource` | 拉取所有可选的视觉/媒体资源类别 || `--image`、`--font`、`--audio`、`--video`、`--media`、`--text-track` | 单独启用某一类可选资源 || `--profile-dir`、`--http-cache-dir`、`--cookie-file` | 按工作负载需要,选择性开启持久化能力 |diff --git a/moli-protocol-server/src/cdp_scheduler.rs b/moli-protocol-server/src/cdp_scheduler.rsindex 3215172a1..d190ee6b5 100644--- a/moli-protocol-server/src/cdp_scheduler.rs+++ b/moli-protocol-server/src/cdp_scheduler.rs@@ -30,6 +30,8 @@ use serde_json::Value;use tokio::sync::mpsc;use tokio::time::Instant as TokioInstant;+use crate::config::DEFAULT_SCREENCAST_INTERVAL_MS;+const PAGE_SCREENCAST_RETRY_INTERVAL: Duration = Duration::from_secs(1);mod actor;@@ -114,7 +116,7 @@ pub(crate) struct CdpScheduler {runtime_command_output_barriers: RuntimeCommandOutputBarriers,queues: SchedulerQueues,page_screencasts: HashMap<Option<String>, PageScreencastSchedule>,- page_screencast_fps: u8,+ page_screencast_interval_ms: u32,}#[derive(Clone, Copy)]@@ -154,10 +156,10 @@ pub(crate) struct ScheduledPageScreencastFrame {visual_state: RendererVisualStateToken,}-fn page_screencast_interval(fps: u8, every_nth_frame: u32) -> Duration {- debug_assert!((1..=60).contains(&fps));+fn page_screencast_interval(interval_ms: u32, every_nth_frame: u32) -> Duration {+ debug_assert!(interval_ms > 0);debug_assert!(every_nth_frame > 0);- Duration::from_secs_f64(f64::from(every_nth_frame) / f64::from(fps))+ Duration::from_millis(u64::from(interval_ms)) * every_nth_frame}fn next_page_screencast_deadline(now: TokioInstant, interval: Duration) -> TokioInstant {@@ -503,8 +505,10 @@ impl CdpScheduler {now: TokioInstant,) {let session_id = registration.session_id().map(str::to_owned);- let interval =- page_screencast_interval(self.page_screencast_fps, registration.every_nth_frame());+ let interval = page_screencast_interval(+ self.page_screencast_interval_ms,+ registration.every_nth_frame(),+ );self.page_screencasts.insert(session_id,PageScreencastSchedule {@@ -696,20 +700,20 @@ impl CdpScheduler {output}- fn new_with_screencast_fps(conn: CdpConnection, page_screencast_fps: u8) -> Self {+ fn new_with_screencast_interval(conn: CdpConnection, page_screencast_interval_ms: u32) -> Self {Self {conn,pending_navigation_background_events: VecDeque::new(),runtime_command_output_barriers: RuntimeCommandOutputBarriers::default(),queues: SchedulerQueues::default(),page_screencasts: HashMap::new(),- page_screencast_fps,+ page_screencast_interval_ms,}}#[cfg(test)]fn new(conn: CdpConnection) -> Self {- Self::new_with_screencast_fps(conn, 1)+ Self::new_with_screencast_interval(conn, DEFAULT_SCREENCAST_INTERVAL_MS)}pub(crate) fn new_with_initial_state_runtime_config(@@ -733,22 +737,22 @@ impl CdpScheduler {navigation_runtime_config,target_host_integration,DefaultTargetRuntimeInitialization::Materialized,- 1,+ DEFAULT_SCREENCAST_INTERVAL_MS,)}- pub(crate) fn new_with_deferred_default_target_runtime_and_screencast_fps(+ pub(crate) fn new_with_deferred_default_target_runtime_and_screencast_interval(initial_storage_partition: CdpInitialStoragePartition,navigation_runtime_config: NavigationRuntimeConfig,target_host_integration: Option<CdpTargetHostIntegration>,- page_screencast_fps: u8,+ page_screencast_interval_ms: u32,) -> (Self, CdpSchedulerEventReceivers) {Self::new_with_default_target_runtime_initialization(initial_storage_partition,navigation_runtime_config,target_host_integration,DefaultTargetRuntimeInitialization::Deferred,- page_screencast_fps,+ page_screencast_interval_ms,)}@@ -757,7 +761,7 @@ impl CdpScheduler {navigation_runtime_config: NavigationRuntimeConfig,target_host_integration: Option<CdpTargetHostIntegration>,initialization: DefaultTargetRuntimeInitialization,- page_screencast_fps: u8,+ page_screencast_interval_ms: u32,) -> (Self, CdpSchedulerEventReceivers) {let conn = match initialization {DefaultTargetRuntimeInitialization::Materialized => {@@ -773,7 +777,7 @@ impl CdpScheduler {)}};- let mut scheduler = Self::new_with_screencast_fps(conn, page_screencast_fps);+ let mut scheduler = Self::new_with_screencast_interval(conn, page_screencast_interval_ms);if let Some(target_host_integration) = target_host_integration {target_host_integration.install(&mut scheduler.conn);}diff --git a/moli-protocol-server/src/cdp_scheduler/tests.rs b/moli-protocol-server/src/cdp_scheduler/tests.rsindex f3261459c..b2e83206c 100644--- a/moli-protocol-server/src/cdp_scheduler/tests.rs+++ b/moli-protocol-server/src/cdp_scheduler/tests.rs@@ -40,30 +40,39 @@ use super::{};#[test]-fn screencast_deadlines_respect_configured_fps_without_catch_up() {+fn screencast_deadlines_respect_configured_milliseconds_without_catch_up() {let started = tokio::time::Instant::now();assert_eq!(- page_screencast_interval(1, 1),+ page_screencast_interval(1000, 1),std::time::Duration::from_secs(1));assert_eq!(- page_screencast_interval(1, 3),+ page_screencast_interval(1000, 3),std::time::Duration::from_secs(3));let completion = started + std::time::Duration::from_secs(10);assert_eq!(- next_page_screencast_deadline(completion, page_screencast_interval(1, 1)),+ next_page_screencast_deadline(completion, page_screencast_interval(1000, 1)),started + std::time::Duration::from_secs(11),"an overdue capture schedules from completion instead of catching up");assert_eq!(- page_screencast_interval(20, 1),- std::time::Duration::from_millis(50)+ page_screencast_interval(17, 1),+ std::time::Duration::from_millis(17));assert_eq!(- page_screencast_interval(20, 3),- std::time::Duration::from_millis(150)+ page_screencast_interval(17, 3),+ std::time::Duration::from_millis(51)+ );+ assert_eq!(+ next_page_screencast_deadline(completion, page_screencast_interval(17, 3)),+ completion + std::time::Duration::from_millis(51)+ );+ assert_eq!(+ page_screencast_interval(u32::MAX, 2),+ std::time::Duration::from_millis(8_589_934_590),+ "multiplying the millisecond interval must not overflow u32");}diff --git a/moli-protocol-server/src/config.rs b/moli-protocol-server/src/config.rsindex ec46aaf60..2dfc9c415 100644--- a/moli-protocol-server/src/config.rs+++ b/moli-protocol-server/src/config.rs@@ -1,10 +1,13 @@+pub(crate) const DEFAULT_SCREENCAST_INTERVAL_MS: u32 = 1000;+/// Network and connection settings for the automation protocol server.#[derive(Debug, Clone)]pub struct ServerConfig {pub host: String,pub port: u16,pub timeout_secs: u32,- pub cdp_screencast_fps: u8,+ /// Base screencast interval in milliseconds; must be positive.+ pub screencast_interval_ms: u32,}impl ServerConfig {@@ -19,7 +22,7 @@ impl Default for ServerConfig {host: "127.0.0.1".to_owned(),port: 9222,timeout_secs: 10,- cdp_screencast_fps: 1,+ screencast_interval_ms: DEFAULT_SCREENCAST_INTERVAL_MS,}}}diff --git a/moli-protocol-server/src/protocol_server/cdp_owner.rs b/moli-protocol-server/src/protocol_server/cdp_owner.rsindex d5e71fdfd..e9be8602d 100644--- a/moli-protocol-server/src/protocol_server/cdp_owner.rs+++ b/moli-protocol-server/src/protocol_server/cdp_owner.rs@@ -57,7 +57,7 @@ struct CdpOwnerRuntimeConfig {cookie_profile: SharedCookieProfile,storage_partition: Arc<StoragePartitionState>,navigation_runtime_config: NavigationRuntimeConfig,- cdp_screencast_fps: u8,+ screencast_interval_ms: u32,}impl SharedCdpOwnerRegistry {@@ -68,7 +68,7 @@ impl SharedCdpOwnerRegistry {cookie_profile: SharedCookieProfile,storage_partition: Arc<StoragePartitionState>,navigation_runtime_config: NavigationRuntimeConfig,- cdp_screencast_fps: u8,+ screencast_interval_ms: u32,) -> Self {Self {inner: Arc::new(CdpOwnerRegistryInner {@@ -79,7 +79,7 @@ impl SharedCdpOwnerRegistry {cookie_profile,storage_partition,navigation_runtime_config,- cdp_screencast_fps,+ screencast_interval_ms,},state: Mutex::new(CdpOwnerRegistryState::default()),shared_owner: Mutex::new(None),@@ -136,7 +136,7 @@ impl SharedCdpOwnerRegistry {receivers,initial_storage_partition,self.inner.config.navigation_runtime_config.clone(),- self.inner.config.cdp_screencast_fps,+ self.inner.config.screencast_interval_ms,Some(CdpTargetHostIntegration::new(self.inner.config.target_id_allocator.clone(),self.inner.config.tab_target_id_allocator.clone(),@@ -204,18 +204,18 @@ fn spawn_owner_task(receivers: CdpFrontendReceivers,initial_storage_partition: CdpInitialStoragePartition,navigation_runtime_config: NavigationRuntimeConfig,- cdp_screencast_fps: u8,+ screencast_interval_ms: u32,target_host_integration: Option<CdpTargetHostIntegration>,owner_lifecycle: Option<CdpOwnerActorLifecycle>,checkpoint_tx: mpsc::UnboundedSender<CdpCookieSnapshot>,) -> oneshot::Receiver<()> {spawn_protocol_local_task("cdp-owner", move || async move {let (scheduler, scheduler_receivers) =- CdpScheduler::new_with_deferred_default_target_runtime_and_screencast_fps(+ CdpScheduler::new_with_deferred_default_target_runtime_and_screencast_interval(initial_storage_partition,navigation_runtime_config,target_host_integration,- cdp_screencast_fps,+ screencast_interval_ms,);let actor = spawn_cdp_scheduler_actor(scheduler,diff --git a/moli-protocol-server/src/protocol_server/mod.rs b/moli-protocol-server/src/protocol_server/mod.rsindex cf460ecf9..200e763d3 100644--- a/moli-protocol-server/src/protocol_server/mod.rs+++ b/moli-protocol-server/src/protocol_server/mod.rs@@ -155,7 +155,7 @@ impl ProtocolServer {addr,self.storage_partition.clone(),self.navigation_runtime_config.clone(),- self.config.cdp_screencast_fps,+ self.config.screencast_interval_ms,)?;let cdp_owner_registry = app_state.cdp_owner_registry.clone();@@ -771,7 +771,7 @@ impl AppState {subframe_loading_enabled,LayoutPolicy::default(),),- 1,+ crate::config::DEFAULT_SCREENCAST_INTERVAL_MS,)}@@ -779,14 +779,14 @@ impl AppState {addr: SocketAddr,storage_partition: Arc<StoragePartitionState>,navigation_runtime_config: NavigationRuntimeConfig,- cdp_screencast_fps: u8,+ screencast_interval_ms: u32,) -> anyhow::Result<Self> {Ok(Self::from_parts(addr,SharedCookieProfile::from_storage_partition(storage_partition.clone()),storage_partition,navigation_runtime_config,- cdp_screencast_fps,+ screencast_interval_ms,))}@@ -795,7 +795,7 @@ impl AppState {cookie_profile: SharedCookieProfile,storage_partition: Arc<StoragePartitionState>,navigation_runtime_config: NavigationRuntimeConfig,- cdp_screencast_fps: u8,+ screencast_interval_ms: u32,) -> Self {let cdp_agent_host_directory = SharedCdpAgentHostDirectory::default();let cdp_target_id_allocator = Arc::new(AtomicU64::new(0));@@ -807,7 +807,7 @@ impl AppState {cookie_profile.clone(),storage_partition.clone(),navigation_runtime_config.clone(),- cdp_screencast_fps,+ screencast_interval_ms,);Self {browser_ws_url: format!("ws://{addr}/devtools/browser/{DEFAULT_BROWSER_ID}"),diff --git a/moli-protocol-server/src/protocol_server/tests/mod.rs b/moli-protocol-server/src/protocol_server/tests/mod.rsindex 77b6b17dd..f8dbcc304 100644--- a/moli-protocol-server/src/protocol_server/tests/mod.rs+++ b/moli-protocol-server/src/protocol_server/tests/mod.rs@@ -275,7 +275,7 @@ fn protocol_server_test_state(protocol_server_test_fetch_config(fetch_config),optional_resource_fetch_mask,),- 1,+ crate::config::DEFAULT_SCREENCAST_INTERVAL_MS,).expect("test app state should initialize")}@@ -436,7 +436,7 @@ async fn spawn_test_protocol_server_with_runtime_config(addr,storage_partition,navigation_runtime_config,- 1,+ crate::config::DEFAULT_SCREENCAST_INTERVAL_MS,).expect("test app state should initialize");let server = tokio::spawn(async move {diff --git a/moli/src/cli.rs b/moli/src/cli.rsindex 8ef3667d9..b0b0c1990 100644--- a/moli/src/cli.rs+++ b/moli/src/cli.rs@@ -443,14 +443,14 @@ pub struct ServeArgs {#[arg(short, long, default_value_t = 10)]pub timeout: u32,- /// Set the DevTools screencast frame rate. Only valid with --layout.+ /// Set the screencast interval in milliseconds (default: 1000). Requires --layout.#[arg(long,- value_name = "FPS",- value_parser = clap::value_parser!(u8).range(1..=60),+ value_name = "MILLISECONDS",+ value_parser = clap::value_parser!(u32).range(1..),requires = "layout")]- pub cdp_screencast_fps: Option<u8>,+ pub screencast_interval: Option<u32>,#[command(flatten)]pub common: CommonArgs,diff --git a/moli/src/config.rs b/moli/src/config.rsindex ec459dd90..b23450933 100644--- a/moli/src/config.rs+++ b/moli/src/config.rs@@ -63,8 +63,8 @@ impl AppConfig {config.server.host = args.host.clone();config.server.port = args.port;config.server.timeout_secs = args.timeout;- if let Some(fps) = args.cdp_screencast_fps {- config.server.cdp_screencast_fps = fps;+ if let Some(interval_ms) = args.screencast_interval {+ config.server.screencast_interval_ms = interval_ms;}}Commands::Import(_) => {}diff --git a/moli/tests/cli.rs b/moli/tests/cli.rsindex f04595aae..345dedf7f 100644--- a/moli/tests/cli.rs+++ b/moli/tests/cli.rs@@ -614,7 +614,7 @@ fn infers_serve_mode_when_called_without_args() {host: "127.0.0.1".to_owned(),port: 9222,timeout: 10,- cdp_screencast_fps: None,+ screencast_interval: None,common: CommonArgs::default(),})));@@ -640,7 +640,7 @@ fn parses_serve_flags_with_explicit_command() {host: "0.0.0.0".to_owned(),port: 9333,timeout: 42,- cdp_screencast_fps: None,+ screencast_interval: None,common: CommonArgs::default(),})));@@ -1571,29 +1571,50 @@ fn layout_selects_on_demand_policy_for_fetch_and_serve() {}#[test]-fn cdp_screencast_fps_requires_layout_and_accepts_one_through_sixty() {- for fps in ["1", "30", "60"] {+fn screencast_interval_requires_layout_and_accepts_positive_milliseconds() {+ for interval_ms in ["1", "16", "17", "33", "250", "1000", "1500", "4294967295"] {let cli = Cli::try_parse_from(normalize_args_for_compat(["moli","serve","--layout",- "--cdp-screencast-fps",- fps,+ "--screencast-interval",+ interval_ms,])).unwrap();let config = AppConfig::from_cli(&cli).unwrap();- assert_eq!(config.server.cdp_screencast_fps, fps.parse::<u8>().unwrap());+ assert_eq!(+ config.server.screencast_interval_ms,+ interval_ms.parse::<u32>().unwrap()+ );}for args in [- vec!["moli", "serve", "--cdp-screencast-fps", "30"],- vec!["moli", "serve", "--layout", "--cdp-screencast-fps", "0"],- vec!["moli", "serve", "--layout", "--cdp-screencast-fps", "61"],+ vec!["moli", "serve", "--screencast-interval", "33"],+ vec!["moli", "serve", "--layout", "--screencast-interval", "0"],+ vec!["moli", "serve", "--layout", "--screencast-interval", "-1"],+ vec!["moli", "serve", "--layout", "--screencast-interval", "1.5"],+ vec![+ "moli",+ "serve",+ "--layout",+ "--screencast-interval",+ "4294967296",+ ],+ vec!["moli", "serve", "--layout", "--cdp-screencast-fps", "30"],] {assert!(Cli::try_parse_from(normalize_args_for_compat(args)).is_err());}}+#[test]+fn screencast_interval_defaults_to_one_second() {+ for args in [vec!["moli", "serve"], vec!["moli", "serve", "--layout"]] {+ let cli = Cli::try_parse_from(normalize_args_for_compat(args)).unwrap();+ let config = AppConfig::from_cli(&cli).unwrap();+ assert_eq!(config.server.screencast_interval_ms, 1000);+ }+}+#[test]fn env_flags_are_fallbacks_and_cli_flags_take_priority() {for (case, env_value, expected) in [