agentmux_mcp/tools/
browser_account.rs1use super::*;
9use crate::identity::sign_ui_automation_auth;
10use crate::srv_http::require_agent_env;
11
12pub(super) async fn run(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
14 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
15 require_agent_env(local_url, auth_key, block_id)?;
16 let auth = sign_ui_automation_auth()?;
17 let text = |k: &str| arguments.get(k).and_then(Value::as_str);
18 let ref_ = text("ref").map(str::trim).filter(|r| !r.is_empty());
19 let Some(ref_) = ref_ else {
20 anyhow::bail!("generate_password needs ref: the first password field of the sign-up form, from a snapshot or a find");
21 };
22 let body = json!({ "auth": auth, "pane": pane_arg(arguments), "ref": ref_, "username": text("username"), "reason": text("reason") });
23 let url = format!("{}/api/v1/ui/browser/generate_password", cx.local_url.trim_end_matches('/'));
24 let resp = cx
26 .client
27 .post(&url)
28 .header(AUTH_KEY_HEADER, cx.auth_key)
29 .json(&body)
30 .timeout(std::time::Duration::from_secs(11 * 60 + 30))
31 .send()
32 .await
33 .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
34 let status = resp.status();
35 let text = resp.text().await.unwrap_or_default();
36 let body: Value = serde_json::from_str(&text).unwrap_or(Value::Null);
37 if !status.is_success() {
38 let why = body.get("error").and_then(Value::as_str).map(str::to_string).unwrap_or(text);
39 anyhow::bail!("Browser generate_password: {why}");
40 }
41 Ok(generate_password_text(&body))
42}
43
44fn generate_password_text(body: &Value) -> String {
46 let d = |k: &str| body.pointer(&format!("/data/{k}")).and_then(Value::as_str).unwrap_or("");
47 format!(
48 "Browser generate_password: the Vault made a password for a new account on {} and typed it into the new-password field and its confirmation field (hidden: not shown to you, in snapshots or in read-backs). It is kept as the Vault entry {} until the form is sent. Fill the other fields yourself and click the form's button, then take a snapshot to see whether the site accepted it; if it rejected the password, call generate_password again.{}",
49 d("site"),
50 d("entry"),
51 super::browser::approval_text(body.pointer("/data/approval"))
52 )
53}