diff --git a/crates/bridge-core/src/engine/web.rs b/crates/bridge-core/src/engine/web.rs index 0f3742f8..80214657 100644 --- a/crates/bridge-core/src/engine/web.rs +++ b/crates/bridge-core/src/engine/web.rs @@ -9,21 +9,28 @@ use std::collections::{BTreeMap, HashMap}; -use protocol::commands::PhoneToBridge; +use protocol::commands::{PhoneToBridge, UploadFileMsg}; use protocol::common::TerminalVia; use protocol::events::{BridgeToPhone, WebSignInResultMsg}; use super::Engine; use crate::io::{Addressee, Effect, WatchState}; -/// Whether a browser may send `msg`. A browser drives sessions and reads; -/// what belongs to an identity (its keys, letting in another browser, -/// pairing), what moves a secret or a sign-in, the terminal (whose allowlist -/// names devices, and a browser is none), uploads and the agents' +/// Whether a browser may send `msg`. A browser drives sessions, reads, and +/// attaches files; what belongs to an identity (its keys, letting in another +/// browser, pairing), what moves a secret or a sign-in, the terminal (whose +/// allowlist names devices, and a browser is none) and the agents' /// configuration stay with the phone. Every command is named, so a new one /// is decided here rather than let through by default. +/// +/// A file comes only in chunks, on the signed stream itself. The Blossom +/// form names a URL for the bridge to download, through the Tor proxy when +/// one is set; a browser has no Blossom server of its own, and taking one +/// would let a page make the bridge fetch whatever URL it names. pub fn browser_may_send(msg: &PhoneToBridge) -> bool { match msg { + PhoneToBridge::UploadFile(UploadFileMsg::Chunk(_)) => true, + PhoneToBridge::UploadFile(UploadFileMsg::Blossom(_)) => false, PhoneToBridge::Input(_) | PhoneToBridge::PermissionResponse(_) | PhoneToBridge::QuestionResponse(_) @@ -63,7 +70,6 @@ pub fn browser_may_send(msg: &PhoneToBridge) -> bool { | PhoneToBridge::TerminalReplay(_) | PhoneToBridge::TerminalTakeover(_) | PhoneToBridge::TerminalClose(_) - | PhoneToBridge::UploadFile(_) | PhoneToBridge::PluginAction(_) | PhoneToBridge::McpAction(_) | PhoneToBridge::AgentAction(_) diff --git a/crates/bridge-core/tests/web_client.rs b/crates/bridge-core/tests/web_client.rs index cf6f40a1..e59d3c86 100644 --- a/crates/bridge-core/tests/web_client.rs +++ b/crates/bridge-core/tests/web_client.rs @@ -106,6 +106,9 @@ fn a_command_a_browser_may_not_send_does_nothing() { json!({"type":"set-credentials","agent":"alpha","values":{"anthropic_api_key":"sk-ant-x"}}), json!({"type":"mcp-action","agent":"alpha","action":"remove","names":["github"]}), json!({"type":"terminal-open","openId":"o1","cols":80,"rows":24}), + // A file from a URL: the bridge would download whatever a page names. + json!({"type":"upload-file","sessionId":"s1","hash":"00","url":"https://blossom.example/00","key":"k","iv":"i", + "filename":"a.png","mimeType":"image/png","text":"","sizeBytes":1}), ] { let Ok(parsed) = serde_json::from_value::({ let mut m = msg.clone(); @@ -120,6 +123,21 @@ fn a_command_a_browser_may_not_send_does_nothing() { } } +#[test] +fn a_browser_attaches_a_file_in_chunks() { + let mut rig = Rig::new(); + rig.host_up(); + let session = rig.ready_session("alpha"); + online(&mut rig, BROWSER); + let chunk = json!({"type":"upload-file","sessionId":session,"uploadId":"u1","filename":"notes.md","mimeType":"text/markdown", + "base64Data":"aGk=","text":"read this","chunkIndex":0,"totalChunks":1}); + let effects = from_browser(&mut rig, BROWSER, chunk); + assert!( + effects.iter().any(|e| matches!(e, Effect::HandleFileUpload(protocol::commands::UploadFileMsg::Chunk(c)) if c.upload_id == "u1")), + "the chunk went to the runtime: {effects:#?}" + ); +} + #[test] fn live_text_reaches_only_a_browser_watching_that_session() { let mut rig = Rig::new(); diff --git a/crates/bridge-runtime/src/web.rs b/crates/bridge-runtime/src/web.rs index 483da172..6bbb93bd 100644 --- a/crates/bridge-runtime/src/web.rs +++ b/crates/bridge-runtime/src/web.rs @@ -101,6 +101,7 @@ const AGENT_MAX_CHARS: usize = 120; const INDEX_HTML: &str = include_str!("../web/index.html"); const APP_JS: &str = include_str!("../web/app.js"); const APP_CSS: &str = include_str!("../web/app.css"); +const FAVICON_PNG: &[u8] = include_bytes!("../web/favicon.png"); fn now_ms() -> u64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) @@ -1219,6 +1220,7 @@ fn route(request: &Request, peer: IpAddr, web: &Web) -> Response { ("GET", "/") => Response::new(200, "text/html; charset=utf-8", INDEX_HTML), ("GET", "/app.js") => Response::new(200, "text/javascript; charset=utf-8", APP_JS), ("GET", "/app.css") => Response::new(200, "text/css; charset=utf-8", APP_CSS), + ("GET", "/favicon.png") => Response::new(200, "image/png", FAVICON_PNG), ("GET", "/api/me") => Response::json(200, me_json(browser.as_ref().map(|(_, b)| b), &web.machine_npub, now)), ("POST", "/api/sign-in") => { let Some(key) = sign_in_key(&request.body) else { return Response::error(400) }; @@ -1250,7 +1252,7 @@ fn route(request: &Request, peer: IpAddr, web: &Web) -> Response { response.set_cookie = Some(session_cookie("", 0)); response } - (_, "/" | "/app.js" | "/app.css" | "/api/me" | "/api/sign-in" | "/api/sign-in/qr.svg" | "/api/sign-out") => Response::error(405), + (_, "/" | "/app.js" | "/app.css" | "/favicon.png" | "/api/me" | "/api/sign-in" | "/api/sign-in/qr.svg" | "/api/sign-out") => Response::error(405), _ => Response::error(404), } } @@ -1431,6 +1433,19 @@ pub(crate) mod tests { assert!(cookie.contains("Secure") && cookie.contains("HttpOnly") && cookie.contains("SameSite=Strict")); } + #[test] + fn every_file_the_page_links_is_served_before_sign_in() { + let (web, _, _dir) = web(); + let linked: Vec<&str> = INDEX_HTML.split(['"', '\'']).filter(|s| s.starts_with('/') && s.contains('.')).collect(); + assert!(linked.contains(&"/favicon.png"), "{linked:?}"); + for path in linked { + let served = route(&request("GET", path, HOST), PEER, &web); + assert_eq!(served.status, 200, "{path}"); + } + let icon = route(&request("GET", "/favicon.png", HOST), PEER, &web); + assert_eq!(icon.content_type, "image/png"); + } + #[test] fn a_cookie_alone_reaches_nothing_but_its_own_sign_in() { let (web, mut inputs, _dir) = web(); diff --git a/crates/bridge-runtime/tests/web_page.rs b/crates/bridge-runtime/tests/web_page.rs index 95d56851..72062be9 100644 --- a/crates/bridge-runtime/tests/web_page.rs +++ b/crates/bridge-runtime/tests/web_page.rs @@ -280,8 +280,33 @@ async fn a_browser_signed_in_by_the_phone_drives_a_session_over_its_signed_strea } } - // The phone sees the session the browser made: one engine, two clients. + // A file attached in two chunks lands in the workspace, and the + // agent is told where. let session = session.unwrap(); + for (i, part) in ["bm90ZXMg", "ZnJvbSBhIGJyb3dzZXI="].into_iter().enumerate() { + let chunk = json!({"v":12,"type":"upload-file","sessionId":session,"uploadId":"w-up","filename":"notes.md", + "mimeType":"text/markdown","base64Data":part,"text":"read this","chunkIndex":i,"totalChunks":2}); + command(&mut ws, chunk).await; + } + let attached = loop { + assert!(tokio::time::Instant::now() < deadline + TIMEOUT, "the file's echo, in time"); + match next(&mut ws).await { + WebFrame::Ok { accepted, message, .. } => assert!(accepted, "{message}"), + WebFrame::Msg(text) => { + let msg: Value = serde_json::from_str(&text).unwrap(); + let text = msg["entries"].as_array().into_iter().flatten().filter_map(|e| e["text"].as_str()).collect::(); + if msg["type"] == "output" && text.contains("echo: read this") { + break text; + } + } + other => panic!("unexpected {other:?}"), + } + }; + let path = attached.split("[Attached file: ").nth(1).and_then(|rest| rest.split(']').next()).expect("the file's path"); + assert!(Path::new(path).parent().is_some_and(|dir| dir.ends_with(Path::new(".codedeck").join("uploads"))), "{path}"); + assert_eq!(std::fs::read_to_string(path).unwrap(), "notes from a browser"); + + // The phone sees the session the browser made: one engine, two clients. until("the phone to list the browser's session", || async { core.machines_view().await.machines.get(&machine)?.sessions.contains_key(&session).then_some(()) }) diff --git a/crates/bridge-runtime/web/app.css b/crates/bridge-runtime/web/app.css index 92fa8f2f..54aaead0 100644 --- a/crates/bridge-runtime/web/app.css +++ b/crates/bridge-runtime/web/app.css @@ -176,9 +176,16 @@ button.small-btn { padding: 6px 14px; margin-top: 0; font-size: 14px; } .choices { display: flex; flex-direction: column; gap: 6px; } .choice { display: flex; gap: 8px; align-items: baseline; } -.composer { display: flex; gap: 10px; align-items: flex-end; padding: 12px 14px; border-top: 1px solid var(--line); } +.composer { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--line); } +.composer-row { display: flex; gap: 10px; align-items: flex-end; } .composer textarea { min-height: 44px; max-height: 40vh; } .composer .primary { margin-top: 0; } +.composer button:disabled { opacity: 0.5; cursor: default; } +.attachment { display: flex; align-items: center; gap: 10px; min-width: 0; align-self: flex-start; max-width: 100%; background: var(--raised); border: 1px solid var(--line); border-radius: 10px; padding: 4px 4px 4px 12px; } +.attachment-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; } +.attachment .small { white-space: nowrap; } +.attachment button { padding: 4px 12px; font-size: 13px; } +.session-view.dropping { border-color: var(--dim); } .toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--raised); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; max-width: min(560px, calc(100vw - 32px)); } diff --git a/crates/bridge-runtime/web/app.js b/crates/bridge-runtime/web/app.js index 0de84f32..c314e6aa 100644 --- a/crates/bridge-runtime/web/app.js +++ b/crates/bridge-runtime/web/app.js @@ -349,6 +349,7 @@ function onMessage(msg) { render(); return; case "output": { + if (expected.size) fileDelivered(msg.sessionId, msg.entries); if (!transcripts.has(msg.sessionId)) return; const t = transcript(msg.sessionId); msg.entries.forEach((entry, i) => t.entries.set(msg.seq + i, entry)); @@ -529,6 +530,11 @@ function render() { const watching = tab === "sessions" ? id : null; if (watching !== selected) { selected = watching; + // A file chosen for one session is never sent to the next one opened. + if (!sending && attached) { + attached = null; + drawAttachment(); + } if (link.ready) sendPresence(); if (selected) { transcript(selected); @@ -627,8 +633,10 @@ $("close-session").addEventListener("click", async () => { $("composer").addEventListener("submit", async (event) => { event.preventDefault(); + if (sending || !selected) return; const text = $("input").value; - if (!text.trim() || !selected) return; + if (attached) return sendFile(selected, attached, text); + if (!text.trim()) return; const inputId = crypto.randomUUID(); $("input").value = ""; const answer = await link.command({ type: "input", sessionId: selected, text, inputId }); @@ -637,6 +645,181 @@ $("composer").addEventListener("submit", async (event) => { notify(answer.message || "It did not reach the bridge."); } }); + +// --- a file for the agent --------------------------------------------------- +// +// A file goes up in chunks on the signed stream, each one its own signed +// command, the next sent only once the bridge took the last — so a large file +// never queues ahead of the page's other frames for long. The bridge saves it +// in the workspace and tells the agent where; the words typed with it go along +// as the message. Its caps are the bridge's: 25 MB, and a partial upload idle +// for a minute is dropped. + +const MAX_FILE_BYTES = 25 * 1024 * 1024; +// Base64 per chunk: with the command around it and its signature, a frame +// stays well inside the 256 KiB the stream accepts. +const FILE_CHUNK_CHARS = 180000; +let attached = null; +let sending = false; + +function attach(file) { + if (!file) return; + if (sending) return notify("A file is still being sent."); + if (file.size > MAX_FILE_BYTES) return notify(`${file.name} is over the 25 MB a file may be.`); + attached = file; + drawAttachment(); +} + +function drawAttachment(progress) { + $("attachment").hidden = !attached; + $("attach").disabled = sending; + $("send").disabled = sending; + $("attachment-remove").hidden = sending; + if (!attached) return; + $("attachment-name").textContent = attached.name || "file"; + $("attachment-state").textContent = progress != null ? `sending ${progress}%` : size(attached.size); +} + +function size(bytes) { + if (bytes < 1024) return `${bytes} B`; + if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`; + return `${(bytes / 1024 / 1024).toFixed(1)} MB`; +} + +/** The file's bytes as base64, without the `data:` prefix. */ +function base64Of(file) { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => resolve(String(reader.result).replace(/^data:[^,]*,/, "")); + reader.onerror = () => reject(reader.error); + reader.readAsDataURL(file); + }); +} + +async function sendFile(sessionId, file, text) { + const session = (heartbeat?.sessions || []).find((s) => s.id === sessionId); + if (session?.state === "offline") return notify("The session is not running."); + sending = true; + drawAttachment(0); + try { + let data; + try { + data = await base64Of(file); + } catch { + return notify(`${file.name} could not be read.`); + } + // The words ride in the first chunk, so it carries that much less of the + // file: the frame cap is in bytes, after the body is escaped once more as + // a string in the frame. + const textBytes = new TextEncoder().encode(JSON.stringify(JSON.stringify(text))).length; + if (textBytes > FILE_CHUNK_CHARS / 2) return notify("The message is too long to go with a file; send it on its own."); + const ends = []; + for (let end = FILE_CHUNK_CHARS - textBytes; ; end += FILE_CHUNK_CHARS) { + ends.push(Math.min(end, data.length)); + if (end >= data.length) break; + } + const total = ends.length; + const uploadId = crypto.randomUUID(); + for (let i = 0; i < total; i++) { + const answer = await link.command({ + type: "upload-file", + sessionId, + uploadId, + filename: file.name || "file", + mimeType: file.type || "application/octet-stream", + base64Data: data.slice(i === 0 ? 0 : ends[i - 1], ends[i]), + text: i === 0 ? text : "", + chunkIndex: i, + totalChunks: total, + }); + if (!answer.accepted) return notify(`${file.name} was not sent: ${answer.message || "the bridge did not take it"}.`); + drawAttachment(Math.round(((i + 1) / total) * 100)); + } + attached = null; + if ($("input").value === text) $("input").value = ""; + expectDelivery(sessionId, file.name || "file"); + } finally { + sending = false; + // A send that failed after another session was opened leaves nothing + // attached there: the file was chosen for the session it was sent to. + if (attached === file && selected !== sessionId) attached = null; + drawAttachment(); + } +} + +// The bridge answers each chunk once it has it queued, not once the file is +// saved and handed to the agent, and the wire has no answer for that: a +// session that stopped meanwhile, or a file the bridge could not write, drops +// it without a word. The message the agent gets names the file, so the page +// waits for it to show up in the transcript and says so when it does not. +const DELIVERY_MS = 60000; +const expected = new Map(); + +/** The file's name as the bridge saves it (`uploads.rs`). */ +function savedName(name) { + const safe = name.replace(/[^A-Za-z0-9._-]/g, "_"); + return safe.replace(/^[._]+|[._]+$/g, "") ? safe : "file"; +} + +function expectDelivery(sessionId, name) { + const key = `${sessionId}\n${savedName(name)}`; + clearTimeout(expected.get(key)); + expected.set(key, setTimeout(() => { + expected.delete(key); + notify(`No sign that ${name} reached the session. If it does not show up, the session may have stopped: attach it again.`); + }, DELIVERY_MS)); +} + +function fileDelivered(sessionId, entries) { + for (const [key, timer] of expected) { + const [session, name] = key.split("\n"); + if (session !== sessionId) continue; + if (entries.some((e) => e.entryType === "text" && e.role === "user" && String(e.text).includes(name))) { + clearTimeout(timer); + expected.delete(key); + } + } +} + +$("attach").addEventListener("click", () => $("file").click()); +$("file").addEventListener("change", () => { + attach($("file").files[0]); + $("file").value = ""; +}); +$("attachment-remove").addEventListener("click", () => { + attached = null; + drawAttachment(); +}); +$("input").addEventListener("paste", (event) => { + const file = [...(event.clipboardData?.files || [])][0]; + if (!file) return; + event.preventDefault(); + attach(file); +}); +for (const name of ["dragenter", "dragover"]) { + $("session-view").addEventListener(name, (event) => { + if (!event.dataTransfer?.types?.includes("Files")) return; + event.preventDefault(); + $("session-view").classList.add("dropping"); + }); +} +$("session-view").addEventListener("dragleave", (event) => { + if (!$("session-view").contains(event.relatedTarget)) $("session-view").classList.remove("dropping"); +}); +// A file dropped anywhere else would make the browser open it in place of the +// page. +for (const name of ["dragover", "drop"]) { + document.addEventListener(name, (event) => { + if (event.dataTransfer?.types?.includes("Files")) event.preventDefault(); + }); +} +$("session-view").addEventListener("drop", (event) => { + $("session-view").classList.remove("dropping"); + const file = [...(event.dataTransfer?.files || [])][0]; + if (!file) return; + event.preventDefault(); + attach(file); +}); $("input").addEventListener("keydown", (event) => { if (event.key === "Enter" && !event.shiftKey && !event.isComposing) { event.preventDefault(); diff --git a/crates/bridge-runtime/web/favicon.png b/crates/bridge-runtime/web/favicon.png new file mode 100644 index 00000000..251404a7 Binary files /dev/null and b/crates/bridge-runtime/web/favicon.png differ diff --git a/crates/bridge-runtime/web/index.html b/crates/bridge-runtime/web/index.html index a8ab5f6a..cceb010f 100644 --- a/crates/bridge-runtime/web/index.html +++ b/crates/bridge-runtime/web/index.html @@ -5,6 +5,7 @@ CodeDeck+ bridge + @@ -84,8 +85,17 @@

New session

- - + +
+ + + + +
diff --git a/docs/BRIDGE.md b/docs/BRIDGE.md index d41f6dd7..afcf17f6 100644 --- a/docs/BRIDGE.md +++ b/docs/BRIDGE.md @@ -270,7 +270,8 @@ With the direct link listening on `wss://`, the same port serves a web page for a browser: open `https://
:7447/` (the bridge logs it at start, `[Web] The bridge's web page: …`). From it a browser drives the bridge's sessions as the phone does — the list, a transcript with live text, input, -permission, question and plan cards, Stop, the mode, a new session — and +a file attached to a message, permission, question and plan cards, Stop, +the mode, a new session — and sees the bridge, its agents, usage, the signed-in browsers, the paired phones and the settings. The browser gets the same self-signed certificate as the phone, so it shows a certificate warning: the phone shows the fingerprint to diff --git a/docs/WEB-CLIENT.md b/docs/WEB-CLIENT.md index 91493bdd..997c65eb 100644 --- a/docs/WEB-CLIENT.md +++ b/docs/WEB-CLIENT.md @@ -154,20 +154,43 @@ three card responses, `set-option`, `sync-request`/`sync-ack`, `create-session`, `refresh-sessions`, `close-session`, `interrupt`, `stop-task`, `create-folder`, `presence`, the read-only requests (usage, models, agent style, commands, personas, plugins, MCP, session MCP, -permission rules, accounts, provider profiles), `web-browsers-request` and -`web-browser-sign-out`. Refused, with `OK false`: +permission rules, accounts, provider profiles), `web-browsers-request`, +`web-browser-sign-out`, and `upload-file` in its chunked form. Refused, with +`OK false`: - `session-key`, `pair-request` and `web-sign-in` belong to an identity: a browser never lets in another browser. - `set-credentials`, `set-provider-profile` and `account-action` move secrets and sign-ins. - `terminal-*`: the terminal allowlist names devices, and a browser is none. -- `upload-file`, and the agent-configuration writes: `plugin-action`, - `mcp-action`, `agent-action`, `session-mcp-toggle`, - `permission-rules-remove`. +- `upload-file` in its Blossom form: it names a URL for the bridge to + download (through the Tor proxy when one is set). A browser has no Blossom + server of its own, and taking the form would let a page make the bridge + fetch any URL it names. +- The agent-configuration writes: `plugin-action`, `mcp-action`, + `agent-action`, `session-mcp-toggle`, `permission-rules-remove`. These can be opened one at a time later, each with its own reasoning. +**Files.** A file attached on the page (the composer's Attach, a paste or a +drop) goes up as the phone's chunked upload does, on the stream itself: each +chunk is a signed `CMD` of about 180,000 base64 characters, which keeps the +frame inside the stream's 256 KiB, and the next is sent only once the last +one's `OK` came back. The bridge's caps apply: 25 MB, and 16 uploads in +progress at once; a partial upload idle for a minute is dropped when the +next chunk of any upload arrives. The words typed with it go in the first +chunk, which carries that much less of the file (so words longer than about +90 KB go on their own, not with a file), and become the message; the bridge +saves the file in the workspace's `.codedeck/uploads` and tells the agent +where. A file chosen for one session is dropped when another is opened, so +it never reaches the wrong one. + +An `OK` says the bridge has the chunk, not that the file reached the agent: +a session that stopped meanwhile, or a file the bridge could not write, +drops it with no answer on the wire. The message the agent gets names the +file, so the page waits a minute for it in the transcript and says so when +it does not show up. + ## Lifetimes - **30 days** from sign-in at most (the cookie's `Max-Age` is set at the