agentmux_srv/server/
browser_chooser.rs1use axum::http::StatusCode;
19use axum::response::Response;
20use serde_json::{json, Value};
21
22use super::browser_actions::{note, Kind};
23use super::browser_powers::Power;
24use super::ui_handlers::{banner_for, proxy_value};
25use super::AppState;
26
27pub(crate) const HELD_SECONDS: u64 = 30;
29
30pub(crate) fn holds(state: &AppState, target: &str, agent: &str, page: &str, ask: &str) -> bool {
33 ask != "choose a folder"
34 && !agent.is_empty()
35 && super::browser_actions::not_paused(target).is_ok()
36 && super::browser_press::grant(state, &banner_for(target).0, agent, page, Power::Uploads).is_some()
37}
38
39pub(crate) fn held_reason() -> String {
41 format!(
42 "AgentMux is keeping the page's file chooser for {HELD_SECONDS} seconds: call Browser set_files with chooser: true and paths (files in your workspace) to fill it. A page shows one chooser at a time, so a control you click that opens another one now shows it only after this is filled or has run out, with its own 30 seconds"
43 )
44}
45
46const NOTICE_SECONDS: u64 = 120;
48
49fn notice_payload(ask: &str) -> Value {
51 json!({ "chooser": ask })
52}
53
54pub(crate) fn tell_person(state: &AppState, block_id: &str, ask: &str) {
57 use super::browser_attention::{self, Kind};
58 match browser_attention::raise(state, block_id, "", Kind::Notice, notice_payload(ask)) {
59 Ok(raised) => {
60 tokio::spawn(async move {
61 let (_, waiting) = raised.answer(Some(std::time::Duration::from_secs(NOTICE_SECONDS))).await;
62 drop(waiting);
63 });
64 }
65 Err(e) => tracing::info!(pane = %block_id, error = %e, "[browser-chooser] couldn't tell the person a file chooser was cancelled"),
66 }
67}
68
69pub(crate) async fn fill(state: &AppState, block_id: &str, own: &str, agent: &str, paths: Vec<String>) -> Response {
72 let log = |names: &str| note(block_id, agent, Kind::Upload).target("the page's file chooser").detail(names);
73 let page = match super::browser_bypass_routes::page_url(state, block_id).await {
74 Ok(p) => p,
75 Err(e) => return log("").fail(state, StatusCode::BAD_GATEWAY, e),
76 };
77 let Some(g) = super::browser_press::grant(state, &banner_for(block_id).0, agent, &page, Power::Uploads) else {
78 return log("").fail(
79 state,
80 StatusCode::FORBIDDEN,
81 "no file chooser was filled: that needs the uploads power on this site, and no grant gives it to you here. Ask for it with request_auto_approve (powers [\"uploads\"]), or use set_files with the ref of the control that opens the chooser".to_string(),
82 );
83 };
84 if let Err((code, e)) = super::browser_actions::not_paused(block_id) {
85 return log("").fail(state, code, e);
86 }
87 let files = match super::browser_uploads::files_for_agent(own, paths).await {
88 Ok(f) => f,
89 Err((code, e)) => return log("").fail(state, code, e),
90 };
91 let names: Vec<&str> = files.iter().filter_map(|f| f["name"].as_str()).collect();
92 tracing::info!(agent_id = %agent, block_id = %block_id, files = ?names, "[ui-automation] browser upload into the page's file chooser");
93 let sent = json!({ "block_id": block_id, "files": files, "allowed_origins": g.origins });
94 let done: Result<Value, _> = proxy_value(state, "chooser_files", sent, std::time::Duration::from_secs(60)).await;
95 log(&names.join(", ")).outcome(state, done)
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn the_person_s_notice_carries_the_kind_of_chooser_for_the_frontend_to_word() {
104 assert_eq!(notice_payload("choose a file"), json!({ "chooser": "choose a file" }));
105 assert_eq!(notice_payload("choose a folder")["chooser"], "choose a folder");
106 }
107
108 #[test]
109 fn the_agent_is_told_how_long_the_chooser_is_kept_and_how_to_fill_it() {
110 let r = held_reason();
111 assert!(r.contains("30 seconds") && r.contains("chooser: true") && r.contains("your workspace"), "{r}");
112 }
113}