Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543<!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>pi-collab</title><script src="vendor/marked.min.js"></script><style> :root { --bg: #16181d; --fg: #d4d4d4; --dim: #8a8a8a; --magenta: #c678dd; --cyan: #56b6c2; --green: #98c379; --blue: #61afef; --red: #e06c75; --amber: #e6c07b; --border: #2a2d35; --panel: #1d2026; } * { box-sizing: border-box; } .hidden { display: none; } .dim { color: var(--dim); } html, body { height: 100%; } body { margin: 0; background: var(--bg); color: var(--fg); font: 14px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; } #app { height: 100%; display: flex; flex-direction: column; }
/* ---- login ---- */ #login { padding: 48px 16px; } #login .panel { width: min(420px, 100%); margin: 0 auto; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 24px; } #login h1 { font-size: 18px; margin: 0 0 4px; letter-spacing: -0.01em; } #login h1 .dot { color: var(--magenta); } #login .lede { margin: 0 0 20px; color: var(--dim); font-size: 13px; } #login label { display: block; margin: 14px 0 4px; color: var(--dim); font-size: 12px; } #login input { width: 100%; padding: 10px 12px; background: #0d0f12; color: var(--fg); border: 1px solid var(--border); border-radius: 6px; font: inherit; font-size: 16px; } #login input:focus { outline: none; border-color: var(--magenta); } #login button { margin-top: 20px; padding: 10px 18px; background: var(--magenta); color: #0d0f12; border: 0; border-radius: 6px; font: inherit; font-weight: bold; cursor: pointer; } #login button:hover { filter: brightness(1.1); } #login button:disabled { opacity: 0.6; cursor: default; } #login .small { color: var(--dim); font-size: 12px; margin: 14px 0 0; } #login a { color: var(--blue); cursor: pointer; text-decoration: underline; } #login .msg { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 1em; } #login .msg.ok { color: var(--green); } #token-row { margin-top: 4px; }
/* ---- header ---- */ #header { padding: 6px 12px; border-bottom: 1px solid var(--border); display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; } #title { font-weight: bold; } #room { color: var(--dim); } #status { color: var(--dim); } #users { color: var(--dim); flex: 1; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } #users .me { color: var(--green); font-weight: bold; } #users .them { color: var(--cyan); } #signout { color: var(--dim); font-size: 12px; cursor: pointer; text-decoration: underline; }
/* ---- thinking ---- */ #thinking { color: var(--magenta); padding: 2px 12px; border-bottom: 1px solid var(--border); min-height: 0; } #thinking.hidden { display: none; } #thinking .spin { display: inline-block; animation: spin 1.2s steps(1) infinite; } @keyframes spin { 0%,100% { transform: rotate(0); } 50% { transform: rotate(360deg); } }
/* ---- messages ---- */ #msgs { flex: 1; overflow-y: auto; padding: 8px 12px; } .msg { margin: 6px 0; } .msg-head { color: var(--dim); } .msg-head .name.agent { color: var(--magenta); font-weight: bold; } .msg-head .name.user { color: var(--cyan); font-weight: bold; } .msg-body { white-space: pre-wrap; word-wrap: break-word; }
/* ---- markdown theme (mirrors client.mjs mdTheme + pi-tui Markdown) ---- */ .md { white-space: normal; } .md .md-h1 { font-weight: bold; text-decoration: underline; } .md .md-h2 { font-weight: bold; } .md .md-h3, .md .md-h4, .md .md-h5, .md .md-h6 { font-weight: bold; } .md p { margin: 4px 0; } .md code { background: none; color: var(--amber); font-family: inherit; } .md .md-code { margin: 6px 0; } .md .md-code-border { color: var(--fg); } .md .md-code pre { margin: 0; padding: 0 0 0 2ch; color: var(--amber); white-space: pre-wrap; word-wrap: break-word; font-family: inherit; } .md blockquote { margin: 4px 0; padding-left: 2ch; position: relative; color: var(--dim); font-style: italic; } .md blockquote::before { content: "\2502 "; position: absolute; left: 0; color: var(--dim); } .md li::marker { color: var(--dim); } .md a { color: var(--blue); text-decoration: underline; } .md hr { border: 0; border-top: 1px solid var(--dim); margin: 6px 0; } .md table { border-collapse: collapse; margin: 6px 0; } .md th, .md td { border: 1px solid var(--border); padding: 2px 8px; }
/* ---- tool lines ---- */ .tool-line { padding-left: 2ch; color: var(--dim); } .tool-line .tool-name { color: var(--blue); } .tool-line .ok { color: var(--green); } .tool-line .err { color: var(--red); }
/* ---- input ---- */ #input-row { display: flex; border-top: 1px solid var(--border); } #input { flex: 1; padding: 10px 12px; background: #0d0f12; color: var(--fg); border: 0; outline: none; font: inherit; }
/* ---- commit bar (visible on both login and chat) ---- */ #ver { padding: 3px 12px; border-top: 1px solid var(--border); color: var(--dim); font-size: 11px; display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; } #ver a { color: var(--dim); text-decoration: underline; } #ver .right { margin-left: auto; }</style></head><body><div id="app"> <div id="login"> <div class="panel"> <h1>pi-collab<span class="dot">.</span></h1> <p class="lede">A shared room with an AI coding agent. Sign in with the atproto account you already own — you are sent to your own server to do it, and nothing here ever sees your password.</p>
<form id="login-form"> <label for="handle">handle</label> <input id="handle" placeholder="you.bsky.social" autocapitalize="none" autocorrect="off" autocomplete="username" spellcheck="false" /> <label for="nick">nick (optional, shown in the room)</label> <input id="nick" placeholder="e.g. johan" autocomplete="off" maxlength="24" /> <button id="login-btn" type="submit">Continue</button> </form> <div class="msg" id="login-msg"></div> <p class="small"><a id="token-toggle">have a chat token instead?</a></p> <div id="token-row" class="hidden"> <label for="token">chat token</label> <input id="token" type="password" autocomplete="off" placeholder="paste chat token" /> <button id="token-btn">join</button> </div> </div> </div>
<div id="header" class="hidden"> <span id="title">pi-collab</span> <span id="room">· connecting…</span> <span id="status"></span> <span id="users"></span> <a id="signout" class="hidden">sign out</a> </div>
<div id="thinking" class="hidden"><span class="spin">⟳</span> thinking</div>
<div id="msgs"></div>
<div id="input-row" class="hidden"> <input id="input" placeholder="message…" autocomplete="off" /> </div>
<div id="ver"> <span>pi-collab <span class="dim">·</span> commit <span id="ver-commit">…</span></span> <span class="right dim" id="ver-room"></span> </div></div>
<script>(function () { "use strict";
const $ = (id) => document.getElementById(id); const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
// The bridge reverse-proxies /me, /chat/token and /oauth/* to the gate, so the // web client is always same-origin with its session cookie — no CORS, no // cross-origin cookies (which SameSite=Lax blocks). Behind the tunnel both live // on one hostname; on loopback we normalize to 127.0.0.1 because the OAuth // callback (sidecar PROXY_URL) is http://127.0.0.1:8000/oauth/callback and the // cookie therefore lives on 127.0.0.1 (localhost vs 127.0.0.1 are different // sites for cookies). // next must be a relative path: the gate's NEXT_SAFE allowlist rejects // absolute public-host URLs (only localhost/127.0.0.1 pass), and the callback // and the web client share an origin (via the tunnel in production, or the // bridge on loopback), so "/" resolves correctly either way. const PUBLIC = location.protocol === "https:"; const GATE = ""; const NEXT = "/"; const NICK_KEY = "pi-collab-nick";
if (!PUBLIC && location.hostname !== "127.0.0.1") { location.replace("http://127.0.0.1:" + location.port + location.pathname + location.search + location.hash); }
// ---- markdown rendering (mirror of client.mjs mdTheme + pi-tui Markdown) ---- // marked v18 renderer methods are token-based: { tokens, depth } for headings, // { text, lang } for code, { href, tokens } for links, { text } for html. const renderer = new marked.Renderer(); renderer.heading = function (token) { const level = token.depth; const prefix = level >= 3 ? "#".repeat(level) + " " : ""; const cls = level === 1 ? "md-h1" : "md-h" + level; return '<div class="md-heading ' + cls + '">' + esc(prefix) + this.parser.parseInline(token.tokens) + "</div>"; }; renderer.code = function (token) { return ( '<div class="md-code">' + '<div class="md-code-border">```' + esc(token.lang || "") + "</div>" + "<pre><code>" + esc(token.text) + "</code></pre>" + '<div class="md-code-border">```</div>' + "</div>" ); }; renderer.html = function (token) { return esc(token.text); // TUI renders raw HTML as plain text }; renderer.link = function (token) { return '<a href="' + esc(token.href) + '" target="_blank" rel="noopener">' + this.parser.parseInline(token.tokens) + "</a>"; }; renderer.hr = function () { return '<div class="md-hr"></div>'; }; marked.setOptions({ renderer, breaks: false, gfm: true });
function renderMd(text) { return '<div class="md">' + marked.parse(text) + "</div>"; }
// ---- state ---- let ws = null; let connected = false; let self = null; let room = "…"; const msgEls = new Map(); // message id -> { head, body } const toolEls = new Map(); // tool id -> { line }
// ---- auth (gate cookie session -> chat token) ---- async function checkSession() { try { const res = await fetch(GATE + "/me", { credentials: "include", redirect: "manual" }); if (res.type === "opaqueredirect" || res.status === 302 || res.status === 0) { return { ok: false, reason: "no session cookie — please sign in below" }; } if (!res.ok) return { ok: false, reason: "session check failed (HTTP " + res.status + ")" }; return { ok: true, session: await res.json() }; } catch (err) { return { ok: false, reason: "gate unreachable: " + err.message }; } }
async function mintToken() { const res = await fetch(GATE + "/chat/token", { method: "POST", credentials: "include" }); if (!res.ok) throw new Error("the gate could not mint a chat token"); return (await res.json()).token; }
function setLoginMsg(text, ok) { const el = $("login-msg"); el.textContent = text || ""; el.classList.toggle("ok", !!ok); }
// ---- ui helpers ---- function div(cls) { const d = document.createElement("div"); if (cls) d.className = cls; return d; } function timeStr(ts) { const d = new Date(ts); return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0"); } function setStatus(text) { $("status").textContent = text || ""; } function setUsers(list) { const el = $("users"); if (!list || !list.length) { el.textContent = ""; return; } el.textContent = "users: "; list.forEach((u, i) => { const s = document.createElement("span"); s.className = u === self ? "me" : "them"; s.textContent = (i ? ", " : "") + u; el.appendChild(s); }); } function setThinking(active) { const el = $("thinking"); if (active) el.classList.remove("hidden"); else el.classList.add("hidden"); } function scrollBottom() { const m = $("msgs"); m.scrollTop = m.scrollHeight; }
function senderHead(user, kind, ts) { const head = div("msg-head"); head.innerHTML = '<span class="dim">[</span>' + '<span class="name ' + (kind === "agent" ? "agent" : "user") + '">' + esc(user) + "</span>" + '<span class="dim">]</span> <span class="dim">' + timeStr(ts) + "</span>"; return head; }
function appendMessage(m) { const msg = div("msg"); msg.appendChild(senderHead(m.user, m.kind, m.ts)); const body = div("msg-body"); body.innerHTML = renderMd(m.text); msg.appendChild(body); $("msgs").appendChild(msg); return { msg, body }; }
function handleMessage(m) { const existing = msgEls.get(m.id); if (existing) { existing.body.innerHTML = renderMd(m.text); scrollBottom(); return; } const block = appendMessage(m); msgEls.set(m.id, block); scrollBottom(); }
function toolLine(m) { const mark = m.status === "error" ? '<span class="err"> ✗</span>' : m.status === "completed" ? '<span class="ok"> ✓</span>' : ""; return '<span class="tool-name">' + esc(m.tool) + "</span> " + esc(m.detail || "") + mark; }
function addTool(m) { const existing = toolEls.get(m.id); if (existing) { existing.line.innerHTML = toolLine(m); scrollBottom(); return; } const msg = div("msg"); msg.appendChild(senderHead("agent", "agent", m.ts)); const line = div("tool-line"); line.innerHTML = toolLine(m); msg.appendChild(line); $("msgs").appendChild(msg); toolEls.set(m.id, { line }); scrollBottom(); }
function clearChat() { msgEls.clear(); toolEls.clear(); $("msgs").textContent = ""; }
// ---- connection ---- function showLogin(err) { setLoginMsg(err || ""); $("login").classList.remove("hidden"); $("header").classList.add("hidden"); $("input-row").classList.add("hidden"); connected = false; }
function showChat() { $("login").classList.add("hidden"); $("header").classList.remove("hidden"); $("signout").classList.remove("hidden"); $("input-row").classList.remove("hidden"); }
function connectWS(token, nick) { clearChat(); setUsers([]); setStatus("connecting…");
const proto = location.protocol === "https:" ? "wss:" : "ws:"; ws = new WebSocket(proto + "//" + location.host);
ws.onopen = () => { connected = true; setStatus("connected"); const join = { type: "join", token }; if (nick) join.nick = nick; ws.send(JSON.stringify(join)); };
ws.onmessage = (ev) => { let msg; try { msg = JSON.parse(ev.data); } catch { return; } switch (msg.type) { case "joined": room = msg.room; self = msg.user; $("room").textContent = "· " + room; setStatus("connected"); setUsers(msg.users); clearChat(); for (const m of msg.messages) handleMessage(m); break; case "message": handleMessage(msg); break; case "presence": setUsers(msg.users); break; case "thinking": setThinking(msg.active); break; case "tool": addTool(msg); break; case "error": setStatus("auth error: " + msg.text); break; } };
ws.onclose = (e) => { connected = false; if (e && e.code === 4001) { showLogin("invalid token — sign in again"); } else { setStatus("disconnected"); } }; ws.onerror = () => setStatus("connection error"); }
// ---- entry paths ---- const TOKEN_KEY = "pi-collab-token";
function tokenFromHash() { const h = location.hash; if (!h || !h.includes("tok=")) return null; const t = decodeURIComponent(h.split("tok=")[1]?.split("&")[0] || ""); history.replaceState(null, "", location.pathname + location.search); return t; }
// connect with an already-minted chat token; returns true if it could start function connectWithToken(token) { if (!token) return false; showChat(); setStatus("connected"); connectWS(token, localStorage.getItem(NICK_KEY) || ""); return true; }
async function boot() { // 1. token freshly handed back by the OAuth callback (fragment) const fragToken = tokenFromHash(); if (fragToken) { localStorage.setItem(TOKEN_KEY, fragToken); connectWithToken(fragToken); return; } // 2. a token we've seen before (survives cookie-less logins) const saved = localStorage.getItem(TOKEN_KEY); if (saved) { connectWithToken(saved); return; } // 3. a browser session cookie (works where cookies are kept) const { ok, session, reason } = await checkSession(); if (ok) { try { const token = await mintToken(); localStorage.setItem(TOKEN_KEY, token); self = session.handle || session.did; showChat(); setStatus("signed in as " + (session.handle || session.did)); connectWS(token, localStorage.getItem(NICK_KEY) || ""); } catch (err) { showLogin("could not join: " + err.message); } return; } showLogin(reason || ""); }
function joinWithToken() { const token = $("token").value.trim(); if (!token) { setLoginMsg("paste a chat token"); return; } localStorage.setItem(TOKEN_KEY, token); connectWithToken(token); }
// ---- wire up ---- $("login-form").addEventListener("submit", (e) => { e.preventDefault(); const handle = $("handle").value.trim(); if (!handle) { setLoginMsg("enter your handle"); return; } localStorage.setItem(NICK_KEY, $("nick").value.trim()); location.href = GATE + "/oauth/login?handle=" + encodeURIComponent(handle) + "&next=" + encodeURIComponent(NEXT); }); $("token-toggle").addEventListener("click", (e) => { e.preventDefault(); $("token-row").classList.toggle("hidden"); }); $("token-btn").addEventListener("click", joinWithToken); $("token").addEventListener("keydown", (e) => { if (e.key === "Enter") joinWithToken(); }); $("signout").addEventListener("click", () => { localStorage.removeItem(TOKEN_KEY); location.href = GATE + "/oauth/logout?next=" + encodeURIComponent(NEXT); }); $("input").addEventListener("keydown", (e) => { if (e.key === "Enter") { const text = e.target.value.trim(); if (text && connected) { ws.send(JSON.stringify({ type: "chat", text })); e.target.value = ""; } } });
// restore nick, then try the cookie session $("nick").value = localStorage.getItem(NICK_KEY) || "";
// show which commit is running (same-origin /version from the bridge) fetch("/version", { cache: "no-store" }) .then((r) => r.json()) .then((v) => { $("ver-commit").textContent = v.commit || "unknown"; }) .catch(() => { $("ver-commit").textContent = "unknown"; });
boot();})();</script></body></html>