The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(browser_task): add browser task module
Steven Enamakelf041d0cdf445
diff --git a/crates/openhuman-core/src/modules/browser_task.rs b/crates/openhuman-core/src/modules/browser_task.rsindex fe31f1c64c..1516dc1246 100644--- a/crates/openhuman-core/src/modules/browser_task.rs+++ b/crates/openhuman-core/src/modules/browser_task.rs@@ -9,8 +9,6 @@//! surfaces as `needs_approval` and only `ContinueTask.approve` releases.use std::collections::BTreeMap;-use std::future::Future;-use std::pin::Pin;use std::time::{Duration, Instant};use serde::{de::DeserializeOwned, Serialize};@@ -26,9 +24,6 @@ use crate::config::Config;#[path = "browser_task_tests.rs"]mod tests;-/// A pending `StartTask` call.-pub(crate) type Started<'a> = Pin<Box<dyn Future<Output = Result<TaskView, String>> + Send + 'a>>;-/// The longest one `AwaitTask` long-poll asks for. The module caps it at 60s.const AWAIT_SLICE_MS: u64 = 30_000;/// The bus deadline for one call; beyond the longest long-poll.@@ -50,16 +45,24 @@ pub struct BrowserTask {/// as a plan saved from an earlier run. `goal` then explains it.pub flow: Option<tinycomputer_bus::Flow>,/// The site the task starts on, from its starting address- /// ([`super::browser_sites::site_of`]): what it learns is kept under it,- /// and what earlier tasks there learned is handed to it.+ /// ([`super::site_memory_host::site_of`]): the module keeps what it learns+ /// under it, through the host's `SiteMemoryHost`, and hands the task what+ /// earlier tasks there learned.pub site: Option<String>,}/// Build the `StartTask` request for a browser-only task under host policy.#[must_use]pub fn start_request(config: &Config, task: &BrowserTask) -> StartTaskRequest {+ // With learning off the module is not told the site, so it neither reads+ // nor writes what earlier tasks there left.+ let site = task+ .site+ .clone()+ .filter(|_| super::site_memory_host::learning(config));StartTaskRequest {task: Some(task.goal.clone()),+ site,flow: task.flow.clone(),facts: task.facts.clone(),constraints: TaskConstraints {@@ -88,59 +91,27 @@ pub fn start_request(config: &Config, task: &BrowserTask) -> StartTaskRequest {}/// Start a browser task and follow it until it pauses, finishes, or the host-/// deadline passes (then the view still says `running`).+/// deadline passes (then the view still says `running`). The module hands the+/// task what earlier tasks on its site left, and keeps what it learns, through+/// the host's `SiteMemoryHost` (see [`super::site_memory_host`]).////// # Errors////// Returns a module, transport, or task error without page content.pub async fn start(config: &Config, task: &BrowserTask) -> Result<TaskView, String> {- let view = begin(config, task, |request| {- Box::pin(call(config, methods::START_TASK, request, true))- })- .await?;- settle(config, view).await-}--/// Hands `task` to the module through `send`, its `StartTask` call, with what-/// earlier tasks on its site left (see [`super::browser_sites`]), and follows-/// the task for what it may teach. A saved plan the module refuses is-/// forgotten, and the task is sent again to be planned afresh.-///-/// # Errors-///-/// Returns a module, transport, or task error without page content.-pub(crate) async fn begin<'a>(- config: &'a Config,- task: &BrowserTask,- send: impl Fn(StartTaskRequest) -> Started<'a>,-) -> Result<TaskView, String> {if task.origins.is_empty() {return Err("a browser task needs at least one allowed origin".to_owned());}let mut request = start_request(config, task);let claimed = claim_profile(config, &mut request, super::profile_claim::TASK_WAIT_MS).await;- let mut following = super::browser_sites::apply(config, task, &mut request).await;tracing::debug!(origins = task.origins.len(),max_actions = task.max_actions,saved_flow = task.flow.is_some(),- learning = following.is_some(),+ site_memory = request.site.is_some(),"[browser-task] starting");- let sent = match send(request.clone()).await {- Err(error) if refused_flow(&error) => match following.as_mut() {- Some(reusing) if reusing.reuses_plan() => {- // The error's text is the module's, and can quote the plan.- tracing::warn!("[browser-task] the module refused a saved plan; planning afresh");- super::browser_sites::refused(config, reusing).await;- request.flow = None;- send(request).await- }- _ => Err(error),- },- sent => sent,- };- let view = match sent {+ let view: TaskView = match call(config, methods::START_TASK, request, true).await {Ok(view) => view,Err(error) => {// A task that did not start starts no browser on the profile.@@ -150,8 +121,7 @@ pub(crate) async fn begin<'a>(return Err(error);}};- super::browser_sites::follow(config, &view.id, following);- Ok(view)+ settle(config, view).await}/// Answer a paused task and follow it again.@@ -161,7 +131,6 @@ pub(crate) async fn begin<'a>(/// Returns a module, transport, or task error.pub async fn resume(config: &Config, request: ContinueTaskRequest) -> Result<TaskView, String> {tracing::debug!(task = %request.id, approve = ?request.approve, "[browser-task] continuing");- super::browser_sites::note_inputs(config, &request.id, &request.inputs);let view: TaskView = call(config, methods::CONTINUE_TASK, request, true).await?;settle(config, view).await}@@ -192,10 +161,7 @@ pub async fn wait(config: &Config, id: TaskId) -> Result<TaskView, String> {/// Returns a module or transport error.pub async fn cancel(config: &Config, id: TaskId) -> Result<TaskView, String> {tracing::debug!(task = %id, "[browser-task] cancelling");- let following = super::browser_sites::token_of(config, &id);- let view: TaskView = call(config, methods::CANCEL_TASK, TaskRef { id }, false).await?;- super::browser_sites::learn(config, &view, following).await;- Ok(view)+ call(config, methods::CANCEL_TASK, TaskRef { id }, false).await}/// The task's record: its steps, what it collected, and every rescue. Page@@ -231,15 +197,10 @@ pub(crate) async fn report_with(}/// Follow `view` until it stops or the host deadline passes, then record-/// how it stopped (see [`super::browser_task_report`]) and learn from it-/// once it has ended (see [`super::browser_sites`]).+/// how it stopped (see [`super::browser_task_report`]).async fn settle(config: &Config, view: TaskView) -> Result<TaskView, String> {- // Taken before the wait: the module may be set up again meanwhile, and- // number another task as this one.- let following = super::browser_sites::token_of(config, &view.id);let view = follow(config, view).await?;super::browser_task_report::record(config, &view).await;- super::browser_sites::learn(config, &view, following).await;Ok(view)}@@ -287,15 +248,6 @@ async fn call<Request: Serialize + Send, Reply: DeserializeOwned>(unwrap_response(member, response)}-/// Whether `error`, as [`unwrap_response`] spells it, is the module refusing-/// the flow a task was handed: its code, not text its message may quote.-fn refused_flow(error: &str) -> bool {- error.starts_with(&format!(- "browser task {} failed [INVALID_FLOW]",- methods::START_TASK- ))-}-/// Claims `request`'s persistent profile for the task's browser, or leaves it/// a fresh profile when another browser still holds the folder after `wait`;/// the folder it claimed. Chrome lets one browser at a time use a profile