Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions crates/bridge-core/src/engine/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_)
Expand Down Expand Up @@ -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(_)
Expand Down
18 changes: 18 additions & 0 deletions crates/bridge-core/tests/web_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<PhoneToBridge>({
let mut m = msg.clone();
Expand All @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion crates/bridge-runtime/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) };
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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();
Expand Down
27 changes: 26 additions & 1 deletion crates/bridge-runtime/tests/web_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
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(())
})
Expand Down
9 changes: 8 additions & 1 deletion crates/bridge-runtime/web/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -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)); }

Expand Down
185 changes: 184 additions & 1 deletion crates/bridge-runtime/web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand All @@ -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();
Expand Down
Binary file added crates/bridge-runtime/web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading