| 1 | // The Connections page and its Add a connection page follow the person's |
| 2 | // event stream: an attempt's sign-in steps, its approval, every change of |
| 3 | // a connection's state and the preference arrive in place, rendered by |
| 4 | // the server. Nothing reloads the page. Add, Cancel, Prefer, Sign out and |
| 5 | // Forget post in the background; a row's Sign in again or Paste a new |
| 6 | // token goes to the Add page, where its card is. Once an attempt |
| 7 | // connects, the Add page shows the flourish and goes back to Connections |
| 8 | // with the new row marked. The living marks are the shared Stardust |
| 9 | // (stardust.js). |
| 10 | (function () { |
| 11 | "use strict"; |
| 12 | var root = document.querySelector("[data-connections]"); |
| 13 | if (!root || !window.fetch) return; |
| 14 | var cursor = Number(root.dataset.cursor) || 0; |
| 15 | var adding = root.hasAttribute("data-adding"), leaving = false; |
| 16 | var still = window.matchMedia ? matchMedia("(prefers-reduced-motion: reduce)") : null; |
| 17 | function moving() { return !(still && still.matches); } |
| 18 | var dust = window.EquipoStardust; |
| 19 | |
| 20 | // The server's still marks come alive as the shared Stardust. |
| 21 | function enliven(scope) { |
| 22 | if (!dust) return; |
| 23 | scope.querySelectorAll("[data-conn-star]").forEach(function (star) { |
| 24 | var live = dust.spark(dust.look); |
| 25 | star.replaceWith(live); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | // The loading line's words turn while the runner asks the provider; |
| 30 | // still under reduced motion. |
| 31 | function animate(scope) { |
| 32 | scope.querySelectorAll("[data-loading][data-phrases]").forEach(function (line) { |
| 33 | if (line.turning) return; |
| 34 | line.turning = true; |
| 35 | var phrases = line.dataset.phrases.split("|"), i = 0; |
| 36 | var span = line.querySelector("[data-phrase]"); |
| 37 | if (!span || phrases.length < 2 || !moving()) return; |
| 38 | var timer = setInterval(function () { |
| 39 | if (!line.isConnected) { clearInterval(timer); return; } |
| 40 | i = (i + 1) % phrases.length; |
| 41 | span.setAttribute("data-out", ""); |
| 42 | setTimeout(function () { span.textContent = phrases[i]; span.removeAttribute("data-out"); }, 300); |
| 43 | }, 2400); |
| 44 | }); |
| 45 | } |
| 46 | function live(scope) { enliven(scope); animate(scope); } |
| 47 | |
| 48 | // A connection that just finished connecting: the success card with a |
| 49 | // gentle flourish of the shared mark, then back to Connections, where |
| 50 | // the new row is marked. |
| 51 | function celebrate(id, provider, account) { |
| 52 | var area = root.querySelector("[data-attempts]"); |
| 53 | var tpl = document.querySelector("[data-success-template]"); |
| 54 | var back = "/conversation/settings/connections?added=" + encodeURIComponent(id); |
| 55 | leaving = true; |
| 56 | if (source) source.close(); |
| 57 | if (!area || !tpl) { location.assign(back); return; } |
| 58 | var name = provider === "codex" ? "Codex" : provider === "claude" ? "Claude" : provider; |
| 59 | var card = tpl.content.firstElementChild.cloneNode(true); |
| 60 | card.querySelector("[data-success-title]").textContent = (root.dataset.connected || "%s is connected").replace("%s", name); |
| 61 | card.querySelector("[data-success-account]").textContent = account ? account + ". " + (root.dataset.connectedNext || "") : (root.dataset.connectedNext || ""); |
| 62 | enliven(card); |
| 63 | var spark = card.querySelector("[equipo-sparkle]"); |
| 64 | if (spark && moving()) { |
| 65 | spark.setAttribute("data-flourish", ""); |
| 66 | spark.style.fontSize = "2.5rem"; |
| 67 | } |
| 68 | area.replaceChildren(card); |
| 69 | if (!moving()) { setTimeout(function () { location.assign(back); }, 900); return; } |
| 70 | setTimeout(function () { card.setAttribute("data-leaving", ""); }, 1700); |
| 71 | setTimeout(function () { location.assign(back); }, 2100); |
| 72 | } |
| 73 | |
| 74 | // The row just added keeps its mark once: a reload of the page does not |
| 75 | // play it again, and it fades. |
| 76 | if (!adding && /[?&]added=/.test(location.search)) { |
| 77 | history.replaceState(history.state, "", location.pathname); |
| 78 | var fresh = root.querySelector("[equipo-conn-row][data-new]"); |
| 79 | if (fresh) setTimeout(function () { fresh.removeAttribute("data-new"); }, 2800); |
| 80 | } |
| 81 | |
| 82 | // Refreshes run one at a time; a request during one runs once after it. |
| 83 | var inflight = null, again = false; |
| 84 | function refresh() { |
| 85 | if (leaving) return Promise.resolve(); |
| 86 | if (inflight) { again = true; return inflight; } |
| 87 | inflight = load().finally(function () { |
| 88 | inflight = null; |
| 89 | if (again) { again = false; refresh(); } |
| 90 | }); |
| 91 | return inflight; |
| 92 | } |
| 93 | // load reads the page again and swaps its live part in place. An Add |
| 94 | // whose attempt the server has not shown yet keeps its loading card. |
| 95 | function load() { |
| 96 | var box = root.querySelector("[data-connections-live]"); |
| 97 | var connecting = {}; |
| 98 | box.querySelectorAll("[data-connecting]").forEach(function (el) { connecting[el.dataset.connection] = el.dataset.provider; }); |
| 99 | var pending = box.querySelector('[data-connection="pending"]'); |
| 100 | return fetch(location.pathname, { credentials: "same-origin", headers: { "Accept": "text/html" } }) |
| 101 | .then(function (r) { if (!r.ok) throw new Error("refresh"); return r.text(); }) |
| 102 | .then(function (body) { |
| 103 | if (leaving) return; |
| 104 | var doc = new DOMParser().parseFromString(body, "text/html"); |
| 105 | var next = doc.querySelector("[data-connections-live]"); |
| 106 | if (!next) return; |
| 107 | var page = doc.querySelector("[data-connections]"); |
| 108 | if (page) cursor = Math.max(cursor, Number(page.dataset.cursor) || 0); |
| 109 | var off = doc.querySelector("[equipo-offline]"), here = root.querySelector("[equipo-offline]"); |
| 110 | if (here && !off) here.remove(); |
| 111 | if (pending && !next.querySelector('[data-connecting][data-provider="' + pending.dataset.provider + '"]')) { |
| 112 | var area = next.querySelector("[data-attempts]"); |
| 113 | if (area) area.replaceChildren(pending); |
| 114 | } |
| 115 | // What was on the page already stays still: only what is new |
| 116 | // arrives with its motion. |
| 117 | var seen = {}; |
| 118 | box.querySelectorAll("[data-connection]").forEach(function (el) { seen[el.dataset.connection] = true; }); |
| 119 | next.querySelectorAll("[data-connection]").forEach(function (el) { if (seen[el.dataset.connection]) el.setAttribute("data-settled", ""); }); |
| 120 | // A token or code card being typed into stays as it is: the same |
| 121 | // field, its value and its focus, while the runner asks for the |
| 122 | // same attempt. |
| 123 | var typing = null; |
| 124 | box.querySelectorAll("[data-token-form], [data-code-form]").forEach(function (form) { |
| 125 | var field = form.querySelector("[data-token-field], [data-code-field]"); |
| 126 | var card = form.closest("[data-connecting]"); |
| 127 | var same = card && next.querySelector('[data-connecting][data-connection="' + CSS.escape(card.dataset.connection) + '"] :is([data-token-form], [data-code-form])'); |
| 128 | var other = same && same.closest("[data-connecting]"); |
| 129 | var mail = form.querySelector("[data-email-field]"); |
| 130 | if (same && other.dataset.attempt === card.dataset.attempt && !form.closest("[data-token-steps], [data-code-steps]").hidden && (field.value || mail && mail.value || form.contains(document.activeElement))) { |
| 131 | card.setAttribute("data-settled", ""); |
| 132 | same.closest("[data-connecting]").replaceWith(card); |
| 133 | if (form.contains(document.activeElement)) typing = document.activeElement; |
| 134 | } |
| 135 | }); |
| 136 | // An open account email field stays open as it is, with what is |
| 137 | // typed in it, until it is saved or closed. |
| 138 | box.querySelectorAll("details[data-account-email][open]").forEach(function (el) { |
| 139 | var same = next.querySelector('details[data-account-email="' + CSS.escape(el.dataset.accountEmail) + '"]'); |
| 140 | if (!same) return; |
| 141 | if (el.contains(document.activeElement)) typing = document.activeElement; |
| 142 | same.replaceWith(el); |
| 143 | }); |
| 144 | box.replaceChildren.apply(box, Array.from(next.children)); |
| 145 | if (typing) typing.focus(); |
| 146 | live(box); |
| 147 | Object.keys(connecting).some(function (id) { |
| 148 | var done = box.querySelector('[data-held-connection="' + CSS.escape(id) + '"][data-state="healthy"]'); |
| 149 | if (done) celebrate(id, connecting[id], done.dataset.account); |
| 150 | return !!done; |
| 151 | }); |
| 152 | }); |
| 153 | } |
| 154 | // A failed refresh is tried again shortly: never a reload. |
| 155 | function refreshSoon() { |
| 156 | refresh().catch(function () { setTimeout(function () { refresh().catch(function () {}); }, 2000); }); |
| 157 | } |
| 158 | |
| 159 | // Copy: the clipboard where it can, the text selected otherwise. |
| 160 | root.addEventListener("click", function (event) { |
| 161 | var button = event.target.closest("[data-copy]"); |
| 162 | if (!button) return; |
| 163 | var text = button.dataset.copy, label = button.textContent; |
| 164 | function done() { |
| 165 | button.textContent = button.dataset.copied; |
| 166 | button.setAttribute("data-done", ""); |
| 167 | setTimeout(function () { button.textContent = label; button.removeAttribute("data-done"); }, 1800); |
| 168 | } |
| 169 | function select() { |
| 170 | var code = button.parentNode.querySelector("code"); |
| 171 | if (code) { |
| 172 | var range = document.createRange(); |
| 173 | range.selectNodeContents(code); |
| 174 | var sel = getSelection(); |
| 175 | sel.removeAllRanges(); |
| 176 | sel.addRange(range); |
| 177 | try { document.execCommand("copy"); } catch (e) { /* selected for the person to copy */ } |
| 178 | } else { |
| 179 | // A link has no code on the page to select: copy it from a |
| 180 | // field nobody sees. |
| 181 | var field = document.createElement("textarea"); |
| 182 | field.value = text; |
| 183 | field.setAttribute("readonly", ""); |
| 184 | field.setAttribute("equipo-visually-hidden", ""); |
| 185 | document.body.append(field); |
| 186 | field.select(); |
| 187 | try { document.execCommand("copy"); } catch (e) { /* nothing more to try */ } |
| 188 | field.remove(); |
| 189 | } |
| 190 | done(); |
| 191 | } |
| 192 | if (navigator.clipboard && window.isSecureContext) navigator.clipboard.writeText(text).then(done, select); |
| 193 | else select(); |
| 194 | }); |
| 195 | |
| 196 | // A Claude token, or the one-time code of Claude's sign-in: checked |
| 197 | // here, sealed here to the runner's seal key (seal.js) and posted as |
| 198 | // the box with its clear binding. A token is bound to Anthropic's API |
| 199 | // and attempt 1; a code to the code page and the attempt its card |
| 200 | // shows. The field is cleared at once; neither is ever shown, kept or |
| 201 | // sent in the clear. |
| 202 | // A provider API key (a metered connection) is sealed the same way, |
| 203 | // bound to its provider's origin (the card's endpoint) and attempt 1. |
| 204 | var TOKEN = /^sk-ant-oat[0-9A-Za-z_-]{8,500}$/; |
| 205 | var CODE = /^[!-~]{8,1024}$/; |
| 206 | function sealSecret(form) { |
| 207 | var card = form.closest("[data-connecting]"); |
| 208 | var code = form.matches("[data-code-form]"); |
| 209 | var key = form.matches("[data-key-form]"); |
| 210 | var field = form.querySelector(code ? "[data-code-field]" : key ? "[data-key-field]" : "[data-token-field]"); |
| 211 | var note = form.querySelector("[data-token-note]"); |
| 212 | var wrong = form.querySelector("[data-code-wrong]"); |
| 213 | var secret = field.value.trim(); |
| 214 | field.value = ""; |
| 215 | function say(key) { |
| 216 | if (wrong) wrong.hidden = true; |
| 217 | note.textContent = note.dataset[key]; |
| 218 | note.setAttribute("data-tone", "bad"); |
| 219 | note.hidden = false; |
| 220 | field.focus(); |
| 221 | } |
| 222 | var good = code || key ? CODE.test(secret) && secret.indexOf("sk-ant") < 0 : TOKEN.test(secret); |
| 223 | if (!good) { secret = ""; say("bad"); return; } |
| 224 | var d = card.dataset; |
| 225 | var attempt = code ? Number(d.attempt) : 1; |
| 226 | if (!d.sealKey || !window.EquipoSeal || !window.TextEncoder || !(attempt >= 1)) { secret = ""; say("noKey"); return; } |
| 227 | var clear = { runner_id: d.runner, connection_id: d.connection, provider: d.provider, endpoint: d.endpoint, attempt: attempt, expires_at: new Date(Date.now() + 10 * 60 * 1000).toISOString() }; |
| 228 | var message = new TextEncoder().encode(JSON.stringify(Object.assign({ secret: secret }, clear))); |
| 229 | secret = ""; |
| 230 | var box; |
| 231 | try { box = window.EquipoSeal.seal(d.sealKey, message); } catch (e) { box = ""; } |
| 232 | message.fill(0); |
| 233 | if (!box) { say("noKey"); return; } |
| 234 | note.hidden = true; |
| 235 | var steps = card.querySelector("[data-token-steps], [data-code-steps], [data-key-steps]"), waiting = card.querySelector("[data-token-waiting]"); |
| 236 | if (steps) steps.hidden = true; |
| 237 | if (waiting) waiting.hidden = false; |
| 238 | fetch("/conversation/connections/" + encodeURIComponent(d.connection) + "/secret", { method: "POST", credentials: "same-origin", headers: { "Content-Type": "application/json", "Accept": "application/json" }, body: JSON.stringify(Object.assign({ box: box }, clear)) }) |
| 239 | .then(function (r) { if (!r.ok) throw new Error("secret"); refreshSoon(); }) |
| 240 | .catch(function () { |
| 241 | if (steps) steps.hidden = false; |
| 242 | if (waiting) waiting.hidden = true; |
| 243 | say("failed"); |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | // Forms post in the background; an Add shows its loading card at once, |
| 248 | // in the chooser's place. On Connections, a row's Sign in again or |
| 249 | // Paste a new token posts as a form: it leads to the Add page. |
| 250 | root.addEventListener("submit", function (event) { |
| 251 | var form = event.target; |
| 252 | if (!form.matches || !form.matches("form")) return; |
| 253 | if (!adding && form.matches("[data-add-form]")) return; |
| 254 | // A metered connection's add form posts as a form: a refusal comes |
| 255 | // back on the page that says why. |
| 256 | if (form.matches("[data-metered-form]")) return; |
| 257 | event.preventDefault(); |
| 258 | if (form.matches("[data-code-form]")) { signInEmail(form); return; } |
| 259 | if (form.matches("[data-token-form], [data-key-form]")) { sealSecret(form); return; } |
| 260 | if (form.matches("[data-email-form]")) { saveEmail(form); return; } |
| 261 | // An ended attempt's note goes once dismissed, or once a new |
| 262 | // attempt starts. |
| 263 | var note = form.closest("[data-connection-note]"); |
| 264 | if (note) note.hidden = true; |
| 265 | if (form.matches("[data-add-form]")) { |
| 266 | root.querySelectorAll("[data-connection-note]").forEach(function (n) { n.hidden = true; }); |
| 267 | var provider = form.querySelector('[name="provider"]').value; |
| 268 | var area = root.querySelector("[data-attempts]"); |
| 269 | if (area && !area.querySelector('[data-connecting][data-provider="' + provider + '"]')) { |
| 270 | var tpl = document.querySelector(provider === "claude" ? "[data-loading-template-claude]" : "[data-loading-template]"); |
| 271 | var card = tpl.content.firstElementChild.cloneNode(true); |
| 272 | area.replaceChildren(card); |
| 273 | live(card); |
| 274 | } |
| 275 | } |
| 276 | if (form.matches('[action$="/prefer"]')) { |
| 277 | // The choice shows at once; the stream confirms it. |
| 278 | var seg = form.closest("[equipo-seg]"); |
| 279 | if (seg) seg.querySelectorAll("button").forEach(function (b) { b.setAttribute("aria-pressed", String(form.contains(b))); }); |
| 280 | } |
| 281 | fetch(form.action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json" }, body: new URLSearchParams(new FormData(form)) }) |
| 282 | .then(function (r) { |
| 283 | if (!r.ok) throw new Error("post"); |
| 284 | if (form.matches('[action$="/prefer"]')) saved(); |
| 285 | refreshSoon(); |
| 286 | }) |
| 287 | .catch(function () { form.submit(); }); |
| 288 | }); |
| 289 | |
| 290 | // The account email typed with the sign-in's code (Paul, 2026-11-23), |
| 291 | // if any: posted first, on its own (the code has no name, so it never |
| 292 | // goes with it), to be the connection's once it is made. One the |
| 293 | // server refuses says so beside the field and the code waits in its |
| 294 | // field; otherwise the code is sealed and sent as ever, the email |
| 295 | // being optional. |
| 296 | function signInEmail(form) { |
| 297 | var mail = form.querySelector("[data-email-field]"); |
| 298 | if (!mail || !mail.value.trim()) { sealSecret(form); return; } |
| 299 | if (form.hasAttribute("data-busy")) return; |
| 300 | var note = form.querySelector("[data-email-note]"); |
| 301 | note.hidden = true; |
| 302 | form.setAttribute("data-busy", ""); |
| 303 | fetch(form.action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json" }, body: new URLSearchParams(new FormData(form)) }) |
| 304 | .then(function (r) { return r.status; }, function () { return 0; }) |
| 305 | .then(function (status) { |
| 306 | form.removeAttribute("data-busy"); |
| 307 | if (status === 422) { |
| 308 | note.textContent = note.dataset.bad; |
| 309 | note.hidden = false; |
| 310 | mail.focus(); |
| 311 | return; |
| 312 | } |
| 313 | sealSecret(form); |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | // The account email a person types for a token row: posted in the |
| 318 | // background; one the server refuses says so beside the field, and a |
| 319 | // saved one closes the field and the row reads again. |
| 320 | function saveEmail(form) { |
| 321 | var note = form.querySelector("[data-email-note]"); |
| 322 | var field = form.querySelector("[data-email-field]"); |
| 323 | note.hidden = true; |
| 324 | fetch(form.action, { method: "POST", credentials: "same-origin", headers: { "Accept": "application/json" }, body: new URLSearchParams(new FormData(form)) }) |
| 325 | .then(function (r) { |
| 326 | if (r.status === 422) { |
| 327 | note.textContent = note.dataset.bad; |
| 328 | note.hidden = false; |
| 329 | field.focus(); |
| 330 | return; |
| 331 | } |
| 332 | if (!r.ok) throw new Error("post"); |
| 333 | var open = form.closest("details"); |
| 334 | if (open) open.open = false; |
| 335 | field.blur(); |
| 336 | saved(); |
| 337 | refreshSoon(); |
| 338 | }) |
| 339 | .catch(function () { form.submit(); }); |
| 340 | } |
| 341 | |
| 342 | // A preference saved: the quiet tick by the heading, as in Settings. |
| 343 | function saved() { |
| 344 | var tpl = document.querySelector("template[equipo-saved-template]"); |
| 345 | var head = root.querySelector("h1"); |
| 346 | if (!tpl || !head) return; |
| 347 | var t = head.querySelector("[equipo-saved-tick]"); |
| 348 | if (!t) { |
| 349 | t = tpl.content.firstElementChild.cloneNode(true); |
| 350 | head.append(t); |
| 351 | } |
| 352 | t.removeAttribute("data-gone"); |
| 353 | t.setAttribute("data-shown", ""); |
| 354 | clearTimeout(t.timer); |
| 355 | t.timer = setTimeout(function () { t.setAttribute("data-gone", ""); }, 2400); |
| 356 | } |
| 357 | var source, failures = 0; |
| 358 | live(root); |
| 359 | |
| 360 | // Resets count down ("resets in 22 hours 5 minutes"), so the |
| 361 | // Connections page reads itself again in place each minute while it is |
| 362 | // seen, and once on coming back to it. Not while a menu is open or a |
| 363 | // control in it has focus: a tick never takes them away. |
| 364 | if (!adding) { |
| 365 | var ticked = Date.now(); |
| 366 | var tick = function () { |
| 367 | var box = root.querySelector("[data-connections-live]"); |
| 368 | if (leaving || document.hidden || !box) return; |
| 369 | if (box.querySelector("details[open]") || box.contains(document.activeElement)) return; |
| 370 | ticked = Date.now(); |
| 371 | refresh().catch(function () {}); |
| 372 | }; |
| 373 | setInterval(tick, 60000); |
| 374 | document.addEventListener("visibilitychange", function () { |
| 375 | if (Date.now() - ticked >= 60000) tick(); |
| 376 | }); |
| 377 | } |
| 378 | if (!window.EventSource) return; |
| 379 | function open() { |
| 380 | if (leaving) return; |
| 381 | source = new EventSource("/api/v1/events?after=" + cursor); |
| 382 | ["connection", "preference", "turn", "step"].forEach(function (type) { |
| 383 | source.addEventListener(type, function (event) { |
| 384 | failures = 0; |
| 385 | try { cursor = Math.max(cursor, JSON.parse(event.data).cursor || 0); } catch (e) { /* keep */ } |
| 386 | refreshSoon(); |
| 387 | }); |
| 388 | }); |
| 389 | ["runner", "ping"].forEach(function (type) { source.addEventListener(type, function () { failures = 0; }); }); |
| 390 | source.onerror = function () { |
| 391 | source.close(); |
| 392 | // Past retention or a lost session: read the page again in place and |
| 393 | // follow from its cursor. |
| 394 | failures++; |
| 395 | setTimeout(function () { refresh().catch(function () {}).finally(open); }, 1000 * Math.min(failures, 10)); |
| 396 | }; |
| 397 | } |
| 398 | open(); |
| 399 | })(); |
| 400 | |