@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
html 958 lines 29.4 KB
Raw
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8" />
5 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6 <title>Room Chat</title>
7 <style>
8 *,
9 *::before,
10 *::after {
11 box-sizing: border-box;
12 margin: 0;
13 padding: 0;
14 }
15 body {
16 font-family:
17 system-ui,
18 -apple-system,
19 sans-serif;
20 background: #0f172a;
21 color: #e2e8f0;
22 height: 100vh;
23 display: flex;
24 flex-direction: column;
25 align-items: center;
26 justify-content: center;
27 padding: 1rem;
28 }
29 .container {
30 width: 100%;
31 max-width: 560px;
32 display: flex;
33 flex-direction: column;
34 gap: 0.75rem;
35 height: 90vh;
36 }
37 h1 {
38 font-size: 1.25rem;
39 font-weight: 600;
40 color: #94a3b8;
41 }
42 #status {
43 font-size: 0.8rem;
44 padding: 0.35rem 0.75rem;
45 border-radius: 999px;
46 display: inline-block;
47 align-self: flex-start;
48 }
49 #status.connected {
50 background: #065f46;
51 color: #6ee7b7;
52 }
53 #status.disconnected {
54 background: #7f1d1d;
55 color: #fca5a5;
56 }
57 #status.connecting {
58 background: #78350f;
59 color: #fcd34d;
60 }
61 #messages {
62 flex: 1;
63 overflow-y: auto;
64 background: #1e293b;
65 border-radius: 0.5rem;
66 padding: 0.75rem;
67 display: flex;
68 flex-direction: column;
69 gap: 0.35rem;
70 font-size: 0.9rem;
71 font-family: "SF Mono", "Fira Code", monospace;
72 }
73 .msg {
74 padding: 0.25rem 0;
75 border-bottom: 1px solid #334155;
76 word-break: break-word;
77 }
78 .msg.system {
79 color: #64748b;
80 font-style: italic;
81 }
82 #form {
83 display: flex;
84 gap: 0.5rem;
85 }
86 #input {
87 flex: 1;
88 padding: 0.6rem 0.75rem;
89 border-radius: 0.375rem;
90 border: 1px solid #334155;
91 background: #1e293b;
92 color: #e2e8f0;
93 font-size: 0.9rem;
94 outline: none;
95 }
96 #input:focus {
97 border-color: #3b82f6;
98 }
99 button {
100 padding: 0.6rem 1.25rem;
101 border-radius: 0.375rem;
102 border: none;
103 background: #3b82f6;
104 color: #fff;
105 font-weight: 600;
106 cursor: pointer;
107 font-size: 0.9rem;
108 }
109 button:hover {
110 background: #2563eb;
111 }
112 button:disabled {
113 opacity: 0.4;
114 cursor: not-allowed;
115 }
116 .header {
117 display: flex;
118 align-items: center;
119 justify-content: space-between;
120 }
121 .header-right {
122 display: flex;
123 align-items: center;
124 gap: 0.5rem;
125 }
126 #avatar-btn {
127 width: 2.25rem;
128 height: 2.25rem;
129 border-radius: 50%;
130 background: #334155;
131 border: 2px solid #475569;
132 color: #94a3b8;
133 font-size: 1rem;
134 cursor: pointer;
135 display: flex;
136 align-items: center;
137 justify-content: center;
138 padding: 0;
139 transition:
140 border-color 0.15s,
141 background 0.15s;
142 position: relative;
143 }
144 #avatar-btn:hover {
145 border-color: #3b82f6;
146 background: #1e293b;
147 }
148 #avatar-btn.active {
149 border-color: #3b82f6;
150 background: #1e293b;
151 }
152 #avatar-btn svg {
153 width: 1.1rem;
154 height: 1.1rem;
155 fill: currentColor;
156 }
157 #toggle {
158 background: #dc2626;
159 font-size: 0.8rem;
160 padding: 0.35rem 0.75rem;
161 }
162 #toggle:hover {
163 background: #b91c1c;
164 }
165 #toggle.off {
166 background: #16a34a;
167 }
168 #toggle.off:hover {
169 background: #15803d;
170 }
171 #actions {
172 display: flex;
173 gap: 0.5rem;
174 }
175 #increment {
176 background: #7c3aed;
177 }
178 #increment:hover {
179 background: #6d28d9;
180 }
181 #increment:disabled {
182 opacity: 0.4;
183 cursor: not-allowed;
184 }
185 #workflow-run {
186 background: #0ea5e9;
187 }
188 #workflow-run:hover {
189 background: #0284c7;
190 }
191 #workflow-run:disabled {
192 opacity: 0.4;
193 cursor: not-allowed;
194 }
195 #workflow-panel {
196 display: flex;
197 flex-direction: column;
198 gap: 0.5rem;
199 padding: 0.75rem;
200 background: #1e293b;
201 border-radius: 0.5rem;
202 border: 1px solid #334155;
203 }
204 #workflow-panel h2 {
205 font-size: 0.75rem;
206 font-weight: 600;
207 color: #94a3b8;
208 text-transform: uppercase;
209 letter-spacing: 0.04em;
210 }
211 #workflow-status {
212 font-size: 0.75rem;
213 font-family: "SF Mono", "Fira Code", monospace;
214 color: #cbd5e1;
215 white-space: pre-wrap;
216 word-break: break-word;
217 min-height: 2.5rem;
218 line-height: 1.4;
219 }
220 #artifacts-panel {
221 display: flex;
222 flex-direction: column;
223 gap: 0.5rem;
224 padding: 0.75rem;
225 background: #1e293b;
226 border-radius: 0.5rem;
227 border: 1px solid #334155;
228 }
229 #artifacts-panel h2 {
230 font-size: 0.75rem;
231 font-weight: 600;
232 color: #94a3b8;
233 text-transform: uppercase;
234 letter-spacing: 0.04em;
235 }
236 #artifacts-status {
237 font-size: 0.75rem;
238 font-family: "SF Mono", "Fira Code", monospace;
239 color: #cbd5e1;
240 white-space: pre-wrap;
241 word-break: break-word;
242 min-height: 2.5rem;
243 line-height: 1.4;
244 }
245 #ai-panel {
246 display: flex;
247 flex-direction: column;
248 gap: 0.5rem;
249 padding: 0.75rem;
250 background: #1e293b;
251 border-radius: 0.5rem;
252 border: 1px solid #334155;
253 }
254 #ai-panel h2 {
255 font-size: 0.75rem;
256 font-weight: 600;
257 color: #94a3b8;
258 text-transform: uppercase;
259 letter-spacing: 0.04em;
260 }
261 #ai-status {
262 font-size: 0.75rem;
263 font-family: "SF Mono", "Fira Code", monospace;
264 color: #cbd5e1;
265 white-space: pre-wrap;
266 word-break: break-word;
267 min-height: 2.5rem;
268 line-height: 1.4;
269 }
270 #auth-panel {
271 display: none;
272 flex-direction: column;
273 gap: 0.75rem;
274 padding: 0.75rem;
275 background: #1e293b;
276 border-radius: 0.5rem;
277 border: 1px solid #334155;
278 }
279 #auth-panel.visible {
280 display: flex;
281 }
282 #auth-panel h2 {
283 font-size: 0.75rem;
284 font-weight: 600;
285 color: #94a3b8;
286 text-transform: uppercase;
287 letter-spacing: 0.04em;
288 }
289 #auth-fields {
290 display: grid;
291 grid-template-columns: repeat(2, minmax(0, 1fr));
292 gap: 0.5rem;
293 }
294 #auth-fields input {
295 width: 100%;
296 padding: 0.6rem 0.75rem;
297 border-radius: 0.375rem;
298 border: 1px solid #334155;
299 background: #0f172a;
300 color: #e2e8f0;
301 font-size: 0.9rem;
302 outline: none;
303 }
304 #auth-fields input:focus {
305 border-color: #3b82f6;
306 }
307 #auth-email {
308 grid-column: 1 / -1;
309 }
310 #auth-name {
311 grid-column: 1 / -1;
312 }
313 #auth-actions {
314 display: flex;
315 gap: 0.5rem;
316 }
317 #auth-signup {
318 background: #2563eb;
319 }
320 #auth-signin {
321 background: #059669;
322 }
323 #auth-signin:hover {
324 background: #047857;
325 }
326 #auth-status {
327 font-size: 0.75rem;
328 font-family: "SF Mono", "Fira Code", monospace;
329 color: #cbd5e1;
330 white-space: pre-wrap;
331 word-break: break-word;
332 min-height: 2.5rem;
333 line-height: 1.4;
334 }
335 @media (max-width: 640px) {
336 #auth-fields {
337 grid-template-columns: 1fr;
338 }
339 }
340 </style>
341 </head>
342 <body>
343 <div class="container">
344 <div class="header">
345 <h1>Room Chat</h1>
346 <div class="header-right">
347 <button
348 id="clear-chat"
349 title="Clear chat"
350 style="background: #475569; font-size: 0.8rem; padding: 0.35rem 0.75rem"
351 >
352 Clear
353 </button>
354 <button id="toggle">Disconnect</button>
355 <button id="avatar-btn" title="Account">
356 <svg viewBox="0 0 24 24">
357 <path
358 d="M12 12c2.7 0 4.8-2.1 4.8-4.8S14.7 2.4 12 2.4 7.2 4.5 7.2 7.2 9.3 12 12 12zm0 2.4c-3.2 0-9.6 1.6-9.6 4.8v2.4h19.2v-2.4c0-3.2-6.4-4.8-9.6-4.8z"
359 />
360 </svg>
361 </button>
362 </div>
363 </div>
364 <span id="status" class="disconnected">disconnected</span>
365 <div id="auth-panel">
366 <h2>Auth</h2>
367 <div id="auth-fields">
368 <input id="auth-email" type="email" placeholder="Email" autocomplete="email" />
369 <input
370 id="auth-password"
371 type="password"
372 placeholder="Password"
373 autocomplete="current-password"
374 />
375 <input id="auth-name" type="text" placeholder="Display name" autocomplete="name" />
376 </div>
377 <div id="auth-actions">
378 <button id="auth-signup" type="button">Sign Up</button>
379 <button id="auth-signin" type="button">Sign In</button>
380 </div>
381 <div id="auth-status">Idle.</div>
382 </div>
383 <div id="messages"></div>
384 <div id="actions">
385 <button id="increment">Increment Counter</button>
386 </div>
387 <div id="reminder-panel">
388 <h2>Scheduled Reminders</h2>
389 <div style="display: flex; gap: 0.5rem">
390 <input
391 id="reminder-delay"
392 type="number"
393 min="1"
394 value="5"
395 style="
396 width: 4rem;
397 padding: 0.6rem 0.75rem;
398 border-radius: 0.375rem;
399 border: 1px solid #334155;
400 background: #0f172a;
401 color: #e2e8f0;
402 font-size: 0.9rem;
403 outline: none;
404 "
405 />
406 <span style="align-self: center; color: #94a3b8; font-size: 0.8rem">seconds</span>
407 <input
408 id="reminder-msg"
409 type="text"
410 placeholder="Reminder message…"
411 style="
412 flex: 1;
413 padding: 0.6rem 0.75rem;
414 border-radius: 0.375rem;
415 border: 1px solid #334155;
416 background: #0f172a;
417 color: #e2e8f0;
418 font-size: 0.9rem;
419 outline: none;
420 "
421 />
422 <button
423 id="reminder-send"
424 type="button"
425 style="background: #f59e0b; padding: 0.6rem 1rem"
426 >
427 Schedule
428 </button>
429 </div>
430 <div
431 id="reminder-status"
432 style="
433 font-size: 0.75rem;
434 font-family: &quot;SF Mono&quot;, &quot;Fira Code&quot;, monospace;
435 color: #cbd5e1;
436 min-height: 1.2rem;
437 line-height: 1.4;
438 "
439 >
440 Type /remind &lt;seconds&gt; &lt;message&gt; in chat, or use the form above.
441 </div>
442 </div>
443 <div id="artifacts-panel">
444 <h2>Artifacts (Git for Agents)</h2>
445 <div style="display: flex; gap: 0.5rem; flex-wrap: wrap">
446 <input
447 id="artifacts-name"
448 type="text"
449 placeholder="repo name (e.g. starter-1234)"
450 style="
451 flex: 1;
452 min-width: 12rem;
453 padding: 0.6rem 0.75rem;
454 border-radius: 0.375rem;
455 border: 1px solid #334155;
456 background: #0f172a;
457 color: #e2e8f0;
458 font-size: 0.9rem;
459 outline: none;
460 "
461 />
462 <button id="artifacts-create" type="button" style="background: #10b981">Create</button>
463 <button id="artifacts-info" type="button" style="background: #6366f1">Info</button>
464 <button id="artifacts-token" type="button" style="background: #eab308; color: #1f2937">
465 New token
466 </button>
467 <button id="artifacts-list" type="button" style="background: #475569">List</button>
468 </div>
469 <div id="artifacts-status">Create a repo, then mint scoped tokens for git clone/push.</div>
470 </div>
471 <div id="ai-panel">
472 <h2>AI Gateway</h2>
473 <div style="display: flex; gap: 0.5rem; flex-wrap: wrap">
474 <input
475 id="ai-prompt"
476 type="text"
477 placeholder="Ask the model anything…"
478 style="
479 flex: 1;
480 min-width: 12rem;
481 padding: 0.6rem 0.75rem;
482 border-radius: 0.375rem;
483 border: 1px solid #334155;
484 background: #0f172a;
485 color: #e2e8f0;
486 font-size: 0.9rem;
487 outline: none;
488 "
489 />
490 <button id="ai-send" type="button" style="background: #f43f5e">Ask</button>
491 </div>
492 <div id="ai-status">
493 Routes Workers AI calls through Cloudflare AI Gateway for caching, rate limiting and
494 observability.
495 </div>
496 </div>
497 <div id="workflow-panel">
498 <h2>NotifyWorkflow</h2>
499 <button id="workflow-run" type="button">Run workflow (broadcasts to this room)</button>
500 <div id="workflow-status">Idle — messages will appear in the chat above.</div>
501 </div>
502 <form id="form">
503 <input id="input" type="text" placeholder="Type a message…" autocomplete="off" />
504 <button type="submit">Send</button>
505 </form>
506 </div>
507
508 <script>
509 const proto = location.protocol === "https:" ? "wss:" : "ws:";
510 const BASE = `${proto}//${location.host}`;
511 const roomId = new URLSearchParams(location.search).get("room") || "default";
512
513 const statusEl = document.getElementById("status");
514 const messagesEl = document.getElementById("messages");
515 const form = document.getElementById("form");
516 const input = document.getElementById("input");
517 const authEmailEl = document.getElementById("auth-email");
518 const authPasswordEl = document.getElementById("auth-password");
519 const authNameEl = document.getElementById("auth-name");
520 const authSignUpBtn = document.getElementById("auth-signup");
521 const authSignInBtn = document.getElementById("auth-signin");
522 const authStatusEl = document.getElementById("auth-status");
523
524 function setStatus(s) {
525 statusEl.textContent = s;
526 statusEl.className = s;
527 }
528
529 function addMessage(text, system = false) {
530 const div = document.createElement("div");
531 div.className = "msg" + (system ? " system" : "");
532 div.textContent = text;
533 messagesEl.appendChild(div);
534 messagesEl.scrollTop = messagesEl.scrollHeight;
535 }
536
537 function setAuthStatus(text) {
538 authStatusEl.textContent = text;
539 }
540
541 async function readResponseBody(response) {
542 const text = await response.text();
543 try {
544 return JSON.stringify(JSON.parse(text), null, 2);
545 } catch {
546 return text || "(empty)";
547 }
548 }
549
550 function getAuthPayload() {
551 return {
552 email: authEmailEl.value.trim(),
553 password: authPasswordEl.value,
554 name: authNameEl.value.trim() || "Room Chat User",
555 };
556 }
557
558 async function submitAuth(path, body, buttonLabel) {
559 authSignUpBtn.disabled = true;
560 authSignInBtn.disabled = true;
561 setAuthStatus(`${buttonLabel}…`);
562 try {
563 const response = await fetch(path, {
564 method: "POST",
565 headers: {
566 "content-type": "application/json",
567 },
568 credentials: "include",
569 body: JSON.stringify(body),
570 });
571 const payload = await readResponseBody(response);
572 setAuthStatus(`status: ${response.status}\n${payload}`);
573 addMessage(`${buttonLabel} -> HTTP ${response.status}`, true);
574 } catch (error) {
575 const message = error instanceof Error ? error.message : String(error);
576 setAuthStatus(`request failed: ${message}`);
577 addMessage(`${buttonLabel} failed: ${message}`, true);
578 } finally {
579 authSignUpBtn.disabled = false;
580 authSignInBtn.disabled = false;
581 }
582 }
583
584 authSignUpBtn.addEventListener("click", async () => {
585 const { email, password, name } = getAuthPayload();
586 if (!email || !password) {
587 setAuthStatus("Email and password are required.");
588 return;
589 }
590 await submitAuth("/auth/sign-up/email", { email, password, name }, "Sign Up");
591 });
592
593 authSignInBtn.addEventListener("click", async () => {
594 const { email, password } = getAuthPayload();
595 if (!email || !password) {
596 setAuthStatus("Email and password are required.");
597 return;
598 }
599 await submitAuth("/auth/sign-in/email", { email, password }, "Sign In");
600 });
601
602 document.getElementById("clear-chat").addEventListener("click", () => {
603 messagesEl.innerHTML = "";
604 });
605
606 const avatarBtn = document.getElementById("avatar-btn");
607 const authPanel = document.getElementById("auth-panel");
608 avatarBtn.addEventListener("click", () => {
609 authPanel.classList.toggle("visible");
610 avatarBtn.classList.toggle("active");
611 });
612
613 const toggleBtn = document.getElementById("toggle");
614 let ws;
615 let autoReconnect = true;
616 let reconnectTimer;
617
618 function connect() {
619 setStatus("connecting");
620 addMessage(`Connecting to room "${roomId}"…`, true);
621
622 ws = new WebSocket(`${BASE}/room/${roomId}`);
623
624 ws.addEventListener("open", () => {
625 setStatus("connected");
626 addMessage("Connected.", true);
627 });
628
629 ws.addEventListener("message", (e) => {
630 addMessage(e.data);
631 });
632
633 ws.addEventListener("close", (e) => {
634 setStatus("disconnected");
635 addMessage(`Disconnected (code ${e.code}).`, true);
636 if (autoReconnect) {
637 reconnectTimer = setTimeout(connect, 2000);
638 }
639 });
640
641 ws.addEventListener("error", () => {
642 setStatus("disconnected");
643 addMessage("WebSocket error.", true);
644 });
645 }
646
647 toggleBtn.addEventListener("click", () => {
648 if (autoReconnect) {
649 autoReconnect = false;
650 toggleBtn.textContent = "Connect";
651 toggleBtn.className = "off";
652 clearTimeout(reconnectTimer);
653 if (ws && ws.readyState === WebSocket.OPEN) {
654 ws.close();
655 }
656 } else {
657 autoReconnect = true;
658 toggleBtn.textContent = "Disconnect";
659 toggleBtn.className = "";
660 if (!ws || ws.readyState !== WebSocket.OPEN) {
661 connect();
662 }
663 }
664 });
665
666 const incrementBtn = document.getElementById("increment");
667 incrementBtn.addEventListener("click", async () => {
668 incrementBtn.disabled = true;
669 incrementBtn.textContent = "Incrementing…";
670 try {
671 const resp = await fetch("/sandbox/increment", { method: "POST" });
672 const data = await resp.json();
673 addMessage(`Counter incremented to ${data.counter}`, true);
674 } catch (err) {
675 addMessage(`Increment failed: ${err.message}`, true);
676 } finally {
677 incrementBtn.disabled = false;
678 incrementBtn.textContent = "Increment Counter";
679 }
680 });
681
682 const workflowRunBtn = document.getElementById("workflow-run");
683 const workflowStatusEl = document.getElementById("workflow-status");
684
685 function isWorkflowTerminal(status) {
686 if (!status || typeof status.status !== "string") return false;
687 const s = status.status.toLowerCase();
688 return (
689 s === "complete" ||
690 s === "completed" ||
691 s === "errored" ||
692 s === "failed" ||
693 s === "terminated" ||
694 s === "canceled" ||
695 s === "cancelled" ||
696 (status.error != null && status.error !== undefined)
697 );
698 }
699
700 function formatWorkflowStatus(status) {
701 const lines = [
702 `status: ${status.status ?? "(none)"}`,
703 status.output !== undefined ? `output: ${JSON.stringify(status.output)}` : null,
704 status.error ? `error: ${JSON.stringify(status.error)}` : null,
705 ].filter(Boolean);
706 return lines.join("\n");
707 }
708
709 let workflowPollTimer = null;
710
711 workflowRunBtn.addEventListener("click", async () => {
712 if (workflowPollTimer) {
713 clearTimeout(workflowPollTimer);
714 workflowPollTimer = null;
715 }
716 workflowRunBtn.disabled = true;
717 workflowStatusEl.textContent = "Starting workflow…";
718 addMessage(
719 `Starting NotifyWorkflow for room "${roomId}" (watch for [workflow] lines)…`,
720 true,
721 );
722 try {
723 const startResp = await fetch(`/workflow/start/${encodeURIComponent(roomId)}`, {
724 method: "POST",
725 });
726 if (!startResp.ok) {
727 const errText = await startResp.text();
728 throw new Error(errText || `HTTP ${startResp.status}`);
729 }
730 const { instanceId } = await startResp.json();
731 if (!instanceId) {
732 throw new Error("No instanceId in response");
733 }
734 workflowStatusEl.textContent = `instance: ${instanceId}\npolling…`;
735
736 let pollCount = 0;
737 const maxPolls = 120;
738
739 const poll = async () => {
740 try {
741 pollCount += 1;
742 if (pollCount > maxPolls) {
743 workflowStatusEl.textContent = `instance: ${instanceId}\n(stopped polling after ${maxPolls} attempts — check logs)`;
744 workflowRunBtn.disabled = false;
745 addMessage("Workflow status polling timed out.", true);
746 return;
747 }
748 const stResp = await fetch(`/workflow/status/${encodeURIComponent(instanceId)}`);
749 if (!stResp.ok) {
750 workflowStatusEl.textContent = `status fetch failed: HTTP ${stResp.status}`;
751 workflowRunBtn.disabled = false;
752 return;
753 }
754 const st = await stResp.json();
755 workflowStatusEl.textContent = `instance: ${instanceId}\n` + formatWorkflowStatus(st);
756 if (isWorkflowTerminal(st)) {
757 workflowRunBtn.disabled = false;
758 addMessage(
759 `Workflow finished: ${st.status}` +
760 (st.output !== undefined ? ` — ${JSON.stringify(st.output)}` : ""),
761 true,
762 );
763 return;
764 }
765 workflowPollTimer = setTimeout(poll, 600);
766 } catch (e) {
767 workflowStatusEl.textContent = `poll error: ${e.message}`;
768 workflowRunBtn.disabled = false;
769 }
770 };
771 await poll();
772 } catch (err) {
773 workflowStatusEl.textContent = `Error: ${err.message}`;
774 addMessage(`Workflow start failed: ${err.message}`, true);
775 workflowRunBtn.disabled = false;
776 }
777 });
778
779 const artifactsNameEl = document.getElementById("artifacts-name");
780 const artifactsStatusEl = document.getElementById("artifacts-status");
781 const artifactsCreateBtn = document.getElementById("artifacts-create");
782 const artifactsInfoBtn = document.getElementById("artifacts-info");
783 const artifactsTokenBtn = document.getElementById("artifacts-token");
784 const artifactsListBtn = document.getElementById("artifacts-list");
785
786 function setArtifactsStatus(label, payload) {
787 const body = typeof payload === "string" ? payload : JSON.stringify(payload, null, 2);
788 artifactsStatusEl.textContent = `${label}\n${body}`;
789 }
790
791 async function callArtifacts(method, path, body) {
792 const init = { method };
793 if (body !== undefined) {
794 init.headers = { "content-type": "application/json" };
795 init.body = JSON.stringify(body);
796 }
797 const resp = await fetch(path, init);
798 const text = await resp.text();
799 let parsed;
800 try {
801 parsed = JSON.parse(text);
802 } catch {
803 parsed = text;
804 }
805 return { status: resp.status, body: parsed };
806 }
807
808 function getRepoName() {
809 const name = artifactsNameEl.value.trim();
810 if (!name) {
811 setArtifactsStatus("error", "repo name is required");
812 return null;
813 }
814 return name;
815 }
816
817 artifactsCreateBtn.addEventListener("click", async () => {
818 const name = getRepoName();
819 if (!name) return;
820 artifactsCreateBtn.disabled = true;
821 setArtifactsStatus(`POST /repos/create ${name}`, "...");
822 try {
823 const { status, body } = await callArtifacts("POST", "/repos/create", {
824 name,
825 description: "Created from Room Chat",
826 });
827 setArtifactsStatus(`HTTP ${status}`, body);
828 addMessage(`Artifacts: created repo "${name}" -> HTTP ${status}`, true);
829 } catch (e) {
830 setArtifactsStatus("request failed", e.message);
831 } finally {
832 artifactsCreateBtn.disabled = false;
833 }
834 });
835
836 artifactsInfoBtn.addEventListener("click", async () => {
837 const name = getRepoName();
838 if (!name) return;
839 artifactsInfoBtn.disabled = true;
840 setArtifactsStatus(`GET /repos/info?name=${name}`, "...");
841 try {
842 const { status, body } = await callArtifacts(
843 "GET",
844 `/repos/info?name=${encodeURIComponent(name)}`,
845 );
846 setArtifactsStatus(`HTTP ${status}`, body);
847 } catch (e) {
848 setArtifactsStatus("request failed", e.message);
849 } finally {
850 artifactsInfoBtn.disabled = false;
851 }
852 });
853
854 artifactsTokenBtn.addEventListener("click", async () => {
855 const name = getRepoName();
856 if (!name) return;
857 artifactsTokenBtn.disabled = true;
858 setArtifactsStatus(`POST /repos/token ${name}`, "...");
859 try {
860 const { status, body } = await callArtifacts("POST", "/repos/token", {
861 name,
862 scope: "write",
863 ttl: 3600,
864 });
865 setArtifactsStatus(`HTTP ${status}`, body);
866 } catch (e) {
867 setArtifactsStatus("request failed", e.message);
868 } finally {
869 artifactsTokenBtn.disabled = false;
870 }
871 });
872
873 artifactsListBtn.addEventListener("click", async () => {
874 artifactsListBtn.disabled = true;
875 setArtifactsStatus("GET /repos/list", "...");
876 try {
877 const { status, body } = await callArtifacts("GET", "/repos/list");
878 setArtifactsStatus(`HTTP ${status}`, body);
879 } catch (e) {
880 setArtifactsStatus("request failed", e.message);
881 } finally {
882 artifactsListBtn.disabled = false;
883 }
884 });
885
886 const aiPromptEl = document.getElementById("ai-prompt");
887 const aiSendBtn = document.getElementById("ai-send");
888 const aiStatusEl = document.getElementById("ai-status");
889
890 aiSendBtn.addEventListener("click", async () => {
891 const prompt = aiPromptEl.value.trim();
892 if (!prompt) {
893 aiStatusEl.textContent = "Enter a prompt first.";
894 return;
895 }
896 aiSendBtn.disabled = true;
897 aiStatusEl.textContent = `> ${prompt}\n…`;
898 try {
899 const resp = await fetch("/ai", {
900 method: "POST",
901 headers: { "content-type": "application/json" },
902 body: JSON.stringify({ prompt }),
903 });
904 const text = await resp.text();
905 let parsed;
906 try {
907 parsed = JSON.parse(text);
908 } catch {
909 parsed = text;
910 }
911 const out =
912 typeof parsed === "object" && parsed && "response" in parsed
913 ? parsed.response
914 : typeof parsed === "string"
915 ? parsed
916 : JSON.stringify(parsed, null, 2);
917 aiStatusEl.textContent = `> ${prompt}\n${out}`;
918 addMessage(`AI Gateway -> HTTP ${resp.status}`, true);
919 } catch (e) {
920 aiStatusEl.textContent = `Error: ${e.message}`;
921 } finally {
922 aiSendBtn.disabled = false;
923 }
924 });
925
926 const reminderSendBtn = document.getElementById("reminder-send");
927 const reminderDelayEl = document.getElementById("reminder-delay");
928 const reminderMsgEl = document.getElementById("reminder-msg");
929 const reminderStatusEl = document.getElementById("reminder-status");
930
931 reminderSendBtn.addEventListener("click", () => {
932 const delay = parseInt(reminderDelayEl.value, 10);
933 const msg = reminderMsgEl.value.trim() || "ping!";
934 if (!delay || delay < 1) {
935 reminderStatusEl.textContent = "Delay must be at least 1 second.";
936 return;
937 }
938 if (!ws || ws.readyState !== WebSocket.OPEN) {
939 reminderStatusEl.textContent = "Not connected.";
940 return;
941 }
942 ws.send(`/remind ${delay} ${msg}`);
943 reminderStatusEl.textContent = `Sent: /remind ${delay} ${msg}`;
944 reminderMsgEl.value = "";
945 });
946
947 form.addEventListener("submit", (e) => {
948 e.preventDefault();
949 const text = input.value.trim();
950 if (!text || !ws || ws.readyState !== WebSocket.OPEN) return;
951 ws.send(text);
952 input.value = "";
953 });
954
955 connect();
956 </script>
957 </body>
958 </html>