oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1(function () {
2 "use strict";
3 var chat = document.querySelector("[equipo-conversation]");
4 if (!chat || !window.fetch || !window.crypto || !crypto.randomUUID) return;
5 var pending = new Map(), confirmed = new Map();
6 function reconcile() {
7 var feed = chat.querySelector("[equipo-feed]");
8 if (!feed) return;
9 var requests = new Map(Array.from(feed.querySelectorAll("[data-request-id]")).map(function (n) { return [n.dataset.requestId, n]; }));
10 // A live fetch started before the POST can arrive after its response.
11 confirmed.forEach(function (node, id) {
12 if (requests.has(id)) confirmed.set(id, requests.get(id));
13 else feed.append(node);
14 });
15 pending.forEach(function (item, id) {
16 if (requests.has(id)) {
17 clearTimeout(item.timer);
18 confirmed.set(id, requests.get(id));
19 if (confirmed.size > 50) confirmed.delete(confirmed.keys().next().value);
20 item.node.remove();
21 if (item.thinking) item.thinking.remove();
22 pending.delete(id);
23 } else {
24 if (!feed.contains(item.node)) feed.append(item.node);
25 if (item.thinking && item.waiting) {
26 if (feed.querySelector("[equipo-thinking]:not([data-optimistic-thinking])")) item.thinking.remove();
27 else if (!feed.contains(item.thinking)) feed.append(item.thinking);
28 }
29 }
30 });
31 }
32 new MutationObserver(reconcile).observe(chat, { childList: true, subtree: true });
33 function send(item) {
34 if (item.busy) return;
35 item.busy = true;
36 item.waiting = true;
37 reconcile();
38 item.status.replaceChildren();
39 item.timer = setTimeout(function () { if (pending.has(item.id)) item.status.textContent = item.sending; }, 500);
40 var form = document.getElementById("composer");
41 var input = form.querySelector("textarea"), button = form.querySelector("button[type=submit]");
42 form.setAttribute("aria-busy", "true");
43 input.readOnly = true;
44 button.disabled = true;
45 var abort = new AbortController();
46 var timeout = setTimeout(function () { abort.abort(); }, 30000);
47 fetch(item.url, {
48 method: "POST", body: new URLSearchParams(item.body), signal: abort.signal,
49 headers: { "Rastrillo-Page": location.pathname, "Rastrillo-Fragment": "conversation-feed notice" }
50 }).then(function (response) {
51 if (!response.ok || !response.headers.get("Rastrillo-Regions")) throw new Error("send");
52 return response.text();
53 }).then(function (body) {
54 var doc = new DOMParser().parseFromString(body, "text/html");
55 var feed = doc.getElementById("conversation-feed");
56 if (!feed || !Array.from(feed.querySelectorAll("[data-request-id]")).some(function (n) { return n.dataset.requestId === item.id; })) throw new Error("receipt");
57 chat.querySelector("#conversation-feed").replaceWith(feed);
58 reconcile();
59 }).catch(function () {
60 if (!pending.has(item.id)) return;
61 item.waiting = false;
62 if (item.thinking) item.thinking.remove();
63 item.status.replaceChildren();
64 var retry = document.createElement("button");
65 retry.type = "button";
66 retry.textContent = item.retry;
67 retry.addEventListener("click", function () { send(item); });
68 item.status.append(retry);
69 }).finally(function () {
70 clearTimeout(timeout);
71 clearTimeout(item.timer);
72 item.busy = false;
73 form.removeAttribute("aria-busy");
74 input.readOnly = false;
75 button.disabled = false;
76 input.focus({ preventScroll: true });
77 });
78 }
79 chat.addEventListener("submit", function (event) {
80 var form = event.target;
81 if (form.id !== "composer") return;
82 event.preventDefault();
83 event.stopPropagation();
84 if (form.getAttribute("aria-busy") === "true") return;
85 var data = new FormData(form);
86 var id = data.get("request_id");
87 var node = document.createElement("li"), bubble = document.createElement("div"), text = document.createElement("p"), status = document.createElement("small");
88 node.dataset.pending = id;
89 node.setAttribute("equipo-message", "own");
90 bubble.setAttribute("equipo-bubble", "");
91 text.setAttribute("equipo-message-body", "");
92 text.textContent = data.get("body");
93 status.setAttribute("equipo-send-status", "");
94 status.setAttribute("role", "status");
95 bubble.append(text, status);
96 node.append(bubble);
97 var item = { id: id, node: node, status: status, body: new URLSearchParams(data), url: form.action, sending: form.dataset.sending, retry: form.dataset.retry };
98 var template = chat.querySelector("[equipo-thinking-template]");
99 if (template && new URL(form.action).pathname === "/conversation") item.thinking = template.content.firstElementChild.cloneNode(true);
100 item.waiting = true;
101 pending.set(id, item);
102 reconcile();
103 form.querySelector("textarea").value = "";
104 form.querySelector('[name="request_id"]').value = crypto.randomUUID();
105 var scroll = chat.querySelector("[equipo-scroll]");
106 scroll.scrollTop = scroll.scrollHeight;
107 send(item);
108 }, true);
109})();
110