From 6c40f20718af4b110fb2245bcb021bed3e2a197b Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:51:52 +0100 Subject: [PATCH 1/4] feat(bridge-core): let a browser attach a file in chunks A browser may now send upload-file in its chunked form: the file travels on the signed stream itself, every chunk under the browser's signature, and lands in the workspace's uploads folder as a phone's does. The Blossom form stays refused: 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 it would let a page make the bridge fetch any URL it names. The engine test covers both forms; the web page end-to-end test attaches a two-chunk file and reads it back from where the agent was told it is. Co-Authored-By: Claude Code --- crates/bridge-core/src/engine/web.rs | 18 +++++++++++------ crates/bridge-core/tests/web_client.rs | 18 +++++++++++++++++ crates/bridge-runtime/tests/web_page.rs | 27 ++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 7 deletions(-) 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/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(()) }) From a5e412feac8056eb7f060b9ec5d51b38323a955b Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:54:35 +0100 Subject: [PATCH 2/4] feat(bridge-runtime): give the web page a favicon The page's tab showed the browser's blank icon. It now links /favicon.png: the app's launcher logo itself, scaled down from the launcher's foreground image onto its dark background with rounded corners (64 px, 5 KB), served like the page's other files (img-src 'self' already allows it). A test reads the page's HTML and checks that every file it links is served before sign-in. Co-Authored-By: Claude Code --- crates/bridge-runtime/src/web.rs | 17 ++++++++++++++++- crates/bridge-runtime/web/favicon.png | Bin 0 -> 5458 bytes crates/bridge-runtime/web/index.html | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 crates/bridge-runtime/web/favicon.png 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/web/favicon.png b/crates/bridge-runtime/web/favicon.png new file mode 100644 index 0000000000000000000000000000000000000000..251404a7806113adae4b12a4858a7bb3ff0ef80a GIT binary patch literal 5458 zcmV-Y6|L%tP)Ub;f`DoO|!P_l?z6Jr9aPGsvLK2m%5T5Tnp25>eDBB%)$a6vYt5;DD}xipe60 z7(s=Iibgwdw=`e-#$l}{G$NdzEm!kJHcA}W^3)Sz#bwp$p68QwRQ)vPDJhzk=tsu+FgJF zq`m8zv*>?^yIwB=kv!KW9|U5?JqcUXSk3UOTBJDV2@=k zhwm3~_&z@SzAVG?`3{AgN>|)kstv=Y?J-Y27xLhrnmq7CgS8t&NFWFljw2+=jPs;p zOI;~-TD4leyJNF-0IyUk?Gc9IA4H_o4tG5lhO4%Vt2INARlM=2Jnwo#k>g*TWpKz% zU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=9HWPZ@D4hZ%pphO?sEXnl4Yo3 z2|O<~Hi@AXVk%?A&u=7J^C+#~|CaFKhe$?tAg+V>LHAh69IgCR$L8kd;H0RufsZ0= zvzfxI>!L~nsA4JY0KH5aF|`_|Jc=ogVWTLW<@r9i?pzx_ CodeDeck+ bridge + From 701f63d437b56155a018a445cbaf207cf75a0b80 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:58:00 +0100 Subject: [PATCH 3/4] feat(bridge-runtime): attach a file from the web page The composer gets an Attach button; a file pasted into the message box or dropped on the session works too. The file goes up in chunks on the signed stream, each a signed upload-file command of about 180,000 base64 characters (inside the stream's 256 KiB frame cap), the next sent only once the bridge answered the last, with its progress shown on the file. The words typed with it become the message, as on the phone. The page refuses a file over the bridge's 25 MB cap before reading it, drops a chosen file when another session is opened so it never reaches the wrong one, and keeps a file dropped elsewhere from replacing the page. Verified in headless Chrome against a held bridge: a 1.1 MB file in nine chunks arrived byte for byte where the fake agent was told it is. Co-Authored-By: Claude Code --- crates/bridge-runtime/web/app.css | 9 +- crates/bridge-runtime/web/app.js | 185 ++++++++++++++++++++++++++- crates/bridge-runtime/web/index.html | 13 +- 3 files changed, 203 insertions(+), 4 deletions(-) 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/index.html b/crates/bridge-runtime/web/index.html index 33142b15..cceb010f 100644 --- a/crates/bridge-runtime/web/index.html +++ b/crates/bridge-runtime/web/index.html @@ -85,8 +85,17 @@

New session

- - + +
+ + + + +
From be09f2507ddd73127b277acacb641a3e5d6f5447 Mon Sep 17 00:00:00 2001 From: deymosh <232244450+deymosh@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:58:01 +0100 Subject: [PATCH 4/4] docs: a browser attaches files, in chunks only WEB-CLIENT.md moves upload-file to what a browser may send, in its chunked form, says why the Blossom form stays refused, and describes how the page sends a file. BRIDGE.md lists attaching a file among what the page does. Co-Authored-By: Claude Code --- docs/BRIDGE.md | 3 ++- docs/WEB-CLIENT.md | 33 ++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) 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