Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(browser): extract argument parsing into a dedicated module

Steven Enamakelefe85598f499
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser.rs b/crates/openhuman-core/src/tools/impl/browser/browser.rs
index 287d12cece..795f11b491 100644
--- a/crates/openhuman-core/src/tools/impl/browser/browser.rs
+++ b/crates/openhuman-core/src/tools/impl/browser/browser.rs
@@ -1,6 +1,10 @@
//! Agent-facing browser backed by the TinyComputer module's browser and task members.
#[path = "browser_session_pool.rs"]
mod session_pool;
+#[path = "browser_args.rs"]
+mod args;
+
+use args::{parse_action, task_inputs};
use crate::modules::browser::BrowserClient;
use crate::security::approval::{ApprovalGate, GateOutcome};
@@ -524,25 +528,6 @@ impl BrowserTool {
}
}
-fn task_inputs(args: &Value) -> anyhow::Result<BTreeMap<String, String>> {
- args["inputs"].as_object().map_or_else(
- || Ok(BTreeMap::new()),
- |inputs| {
- inputs
- .iter()
- .map(|(k, v)| {
- Ok((
- k.clone(),
- v.as_str()
- .ok_or_else(|| anyhow::anyhow!("Task input '{k}' must be text"))?
- .to_owned(),
- ))
- })
- .collect()
- },
- )
-}
-
/// Ask the host approval gate about a paused task's exact action. A missing
/// gate denies: a task never takes an irreversible step unapproved.
async fn approve_task_action(pending: &Pending) -> anyhow::Result<bool> {
@@ -579,81 +564,6 @@ async fn approve_task_action(pending: &Pending) -> anyhow::Result<bool> {
)
}
-fn required<'a>(args: &'a Value, key: &str) -> anyhow::Result<&'a str> {
- args.get(key)
- .and_then(Value::as_str)
- .filter(|s| !s.trim().is_empty())
- .ok_or_else(|| anyhow::anyhow!("Missing '{key}' parameter"))
-}
-
-fn parse_action(args: &Value) -> anyhow::Result<Action> {
- let target = || required(args, "selector").map(Target::parse);
- Ok(match required(args, "action")? {
- "click" => Action::Click {
- target: target()?,
- new_tab: false,
- },
- "fill" => Action::Fill {
- target: target()?,
- value: required(args, "value")?.into(),
- },
- "type" => Action::Type {
- target: args["selector"].as_str().map(Target::parse),
- text: required(args, "text")?.into(),
- delay_ms: None,
- },
- "get_text" => Action::GetText { target: target()? },
- "is_visible" => Action::IsVisible { target: target()? },
- "hover" => Action::Hover { target: target()? },
- "press" => Action::Press {
- key: required(args, "key")?.into(),
- },
- "scroll" => Action::Scroll {
- direction: match required(args, "direction")? {
- "up" => ScrollDirection::Up,
- "down" => ScrollDirection::Down,
- "left" => ScrollDirection::Left,
- "right" => ScrollDirection::Right,
- x => anyhow::bail!("Invalid direction: {x}"),
- },
- pixels: args["pixels"].as_u64().and_then(|v| u32::try_from(v).ok()),
- target: None,
- },
- "wait" => Action::WaitFor {
- target: args["selector"].as_str().map(Target::parse),
- text: args["text"].as_str().map(str::to_owned),
- state: WaitState::Visible,
- ms: args["ms"].as_u64(),
- timeout_ms: args["timeout_ms"].as_u64(),
- },
- "find" => {
- let by = match required(args, "by")? {
- "role" => LocateBy::Role,
- "text" => LocateBy::Text,
- "label" => LocateBy::Label,
- "placeholder" => LocateBy::Placeholder,
- "testid" => LocateBy::TestId,
- x => anyhow::bail!("Invalid locator: {x}"),
- };
- let target = Target::locator(Locator::new(by, required(args, "value")?));
- match required(args, "find_action")? {
- "click" => Action::Click {
- target,
- new_tab: false,
- },
- "fill" => Action::Fill {
- target,
- value: required(args, "fill_value")?.into(),
- },
- "text" => Action::GetText { target },
- "hover" => Action::Hover { target },
- x => anyhow::bail!("Invalid find action: {x}"),
- }
- }
- x => anyhow::bail!("Unsupported browser action: {x}"),
- })
-}
-
#[async_trait]
impl Tool for BrowserTool {
fn exposure(&self) -> tinytools::ToolExposure {
diff --git a/crates/openhuman-core/src/tools/impl/browser/browser_args.rs b/crates/openhuman-core/src/tools/impl/browser/browser_args.rs
new file mode 100644
index 0000000000..6819112743
--- /dev/null
+++ b/crates/openhuman-core/src/tools/impl/browser/browser_args.rs
@@ -0,0 +1,96 @@
+use super::*;
+
+pub(super) fn task_inputs(args: &Value) -> anyhow::Result<BTreeMap<String, String>> {
+ args["inputs"].as_object().map_or_else(
+ || Ok(BTreeMap::new()),
+ |inputs| {
+ inputs
+ .iter()
+ .map(|(k, v)| {
+ Ok((
+ k.clone(),
+ v.as_str()
+ .ok_or_else(|| anyhow::anyhow!("Task input '{k}' must be text"))?
+ .to_owned(),
+ ))
+ })
+ .collect()
+ },
+ )
+}
+
+fn required<'a>(args: &'a Value, key: &str) -> anyhow::Result<&'a str> {
+ args.get(key)
+ .and_then(Value::as_str)
+ .filter(|s| !s.trim().is_empty())
+ .ok_or_else(|| anyhow::anyhow!("Missing '{key}' parameter"))
+}
+
+pub(super) fn parse_action(args: &Value) -> anyhow::Result<Action> {
+ let target = || required(args, "selector").map(Target::parse);
+ Ok(match required(args, "action")? {
+ "click" => Action::Click {
+ target: target()?,
+ new_tab: false,
+ },
+ "fill" => Action::Fill {
+ target: target()?,
+ value: required(args, "value")?.into(),
+ },
+ "type" => Action::Type {
+ target: args["selector"].as_str().map(Target::parse),
+ text: required(args, "text")?.into(),
+ delay_ms: None,
+ },
+ "get_text" => Action::GetText { target: target()? },
+ "is_visible" => Action::IsVisible { target: target()? },
+ "hover" => Action::Hover { target: target()? },
+ "press" => Action::Press {
+ key: required(args, "key")?.into(),
+ },
+ "scroll" => Action::Scroll {
+ direction: match required(args, "direction")? {
+ "up" => ScrollDirection::Up,
+ "down" => ScrollDirection::Down,
+ "left" => ScrollDirection::Left,
+ "right" => ScrollDirection::Right,
+ x => anyhow::bail!("Invalid direction: {x}"),
+ },
+ pixels: args["pixels"].as_u64().and_then(|v| u32::try_from(v).ok()),
+ target: None,
+ },
+ "wait" => Action::WaitFor {
+ target: args["selector"].as_str().map(Target::parse),
+ text: args["text"].as_str().map(str::to_owned),
+ state: WaitState::Visible,
+ ms: args["ms"].as_u64(),
+ timeout_ms: args["timeout_ms"].as_u64(),
+ },
+ "find" => {
+ let by = match required(args, "by")? {
+ "role" => LocateBy::Role,
+ "text" => LocateBy::Text,
+ "label" => LocateBy::Label,
+ "placeholder" => LocateBy::Placeholder,
+ "testid" => LocateBy::TestId,
+ x => anyhow::bail!("Invalid locator: {x}"),
+ };
+ let target = Target::locator(Locator::new(by, required(args, "value")?));
+ match required(args, "find_action")? {
+ "click" => Action::Click {
+ target,
+ new_tab: false,
+ },
+ "fill" => Action::Fill {
+ target,
+ value: required(args, "fill_value")?.into(),
+ },
+ "text" => Action::GetText { target },
+ "hover" => Action::Hover { target },
+ x => anyhow::bail!("Invalid find action: {x}"),
+ }
+ }
+ x => anyhow::bail!("Unsupported browser action: {x}"),
+ })
+}
+