feat(webui): chat from the dashboard
Brings the chat surface to the Web UI: /api/chat with its own handlers and tests, a self-contained ui/src/chat feature folder, and the shared plumbing to reach the same router Telegram uses. Includes a dangerous-command approval path (internal/gateway/dangerous.go, internal/webui/dangerous.go, and the Telegram counterpart) so /rollback and /stop are gated the same way on both surfaces, persona handling for web sessions, and release-update formatting split out of the Telegram service so both can render it. web_parity_test.go pins that the two surfaces expose the same commands. Authored in a parallel session; committed here so it lands with the rest rather than sitting uncommitted while other work builds on top of it. task check passes with it in place. Screenshots used while building are gitignored (*.png) rather than committed, matching the call made on the earlier design reference.
This commit is contained in:
1 parent
8ae21f25db
commit
68ba3456ee
29 files changed
+2616
-112
No files matched your search
@@ -71,6 +71,67 @@ export const api = {
|
||||
config: () => req("/api/config"),
|
||||
logs: (params) => req("/api/logs" + qs(params)),
|
||||
memory: () => req("/api/memory"),
|
||||
chatSessions: () => req("/api/chat/sessions"),
|
||||
chatMessages: (id) => req(`/api/chat/sessions/${encodeURIComponent(id)}/messages`),
|
||||
chatCancel: async (sessionID) => {
|
||||
const res = await fetch("/api/chat/cancel", {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ session_id: sessionID }), signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatMessage: async (channelID, text) => {
|
||||
const res = await fetch("/api/chat/message", {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ channel_id: channelID, source_id: crypto.randomUUID(), text }),
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatPersona: async (sessionID, name) => {
|
||||
const res = await fetch("/api/chat/persona", {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ session_id: sessionID, name }), signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatUpdate: () => req("/api/chat/update"),
|
||||
chatUpdateDefer: async (snapshot) => {
|
||||
const res = await fetch("/api/chat/update/defer", {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ snapshot }), signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatUpdateInstall: async (snapshot) => {
|
||||
const res = await fetch("/api/chat/update/install", {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ snapshot }), signal: AbortSignal.timeout(120000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatDangerous: () => req("/api/chat/dangerous"),
|
||||
chatDangerousRequest: async (kind, spec) => {
|
||||
const res = await fetch(`/api/chat/dangerous/${encodeURIComponent(kind)}`, {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ spec }), signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
chatDangerousDecision: async (id, decision) => {
|
||||
const res = await fetch(`/api/chat/dangerous/${encodeURIComponent(id)}/decision`, {
|
||||
method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ decision }), signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
||||
return res.json();
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
const paths = {
|
||||
dashboard: '<rect x="3" y="3" width="7" height="9" rx="1.5"/><rect x="14" y="3" width="7" height="5" rx="1.5"/><rect x="14" y="12" width="7" height="9" rx="1.5"/><rect x="3" y="16" width="7" height="5" rx="1.5"/>',
|
||||
chat: '<path d="M4 5.5A2.5 2.5 0 0 1 6.5 3h11A2.5 2.5 0 0 1 20 5.5v7a2.5 2.5 0 0 1-2.5 2.5H11l-4.5 4v-4.1A2.5 2.5 0 0 1 4 12.5z"/><path d="M8 8h8M8 11h5"/>',
|
||||
tasks: '<path d="M9 6h11M9 12h11M9 18h11"/><path d="M4 6l1 1 2-2M4 12l1 1 2-2M4 18l1 1 2-2"/>',
|
||||
logs: '<path d="M4 5h16M4 10h16M4 15h10M4 20h7"/>',
|
||||
skills: '<path d="M12 3l2.6 5.6 6.1.8-4.5 4.2 1.2 6-5.4-3-5.4 3 1.2-6L3.3 9.4l6.1-.8z"/>',
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
.chat-page { min-height: calc(100vh - 5rem); }
|
||||
.chat-topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
|
||||
.chat-kicker, .chat-workspace-label { color: var(--fg-muted); font-size: .68rem; font-weight: 700; letter-spacing: .12em; }
|
||||
.chat-topbar-actions { display: flex; align-items: center; gap: .55rem; }
|
||||
.chat-topbar-actions .chat-select { max-width: 14rem; min-width: 0; }
|
||||
.chat-update-panel { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: 0 0 .75rem; padding: .7rem .9rem; border: 1px solid var(--border); border-radius: .75rem; color: var(--fg-muted); font-size: .8rem; }
|
||||
.chat-update-panel > div:first-child { display: grid; gap: .2rem; }
|
||||
.chat-update-panel strong { color: var(--fg); }
|
||||
.chat-update-actions { display: flex; gap: .45rem; flex-shrink: 0; }
|
||||
.chat-dangerous-panel { margin: 0 0 .75rem; padding: .8rem .9rem; border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--border)); border-radius: .75rem; }
|
||||
.chat-dangerous-head, .chat-dangerous-controls, .chat-dangerous-pending { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
|
||||
.chat-dangerous-head { margin-bottom: .7rem; }
|
||||
.chat-dangerous-controls { align-items: stretch; flex-wrap: wrap; }
|
||||
.chat-dangerous-controls > div { display: flex; gap: .45rem; flex: 1 1 18rem; }
|
||||
.chat-dangerous-input { min-width: 0; flex: 1; padding: .5rem .65rem; background: var(--surface-hover); color: var(--fg); border: 1px solid var(--border-strong); border-radius: .5rem; font: inherit; }
|
||||
.chat-dangerous-pending-list { display: grid; gap: .45rem; margin-top: .75rem; }
|
||||
.chat-dangerous-pending { padding-top: .55rem; border-top: 1px solid var(--border); color: var(--fg-muted); font-size: .8rem; }
|
||||
.chat-layout { display: grid; grid-template-columns: 242px minmax(0, 1fr); gap: .75rem; min-height: 72vh; }
|
||||
.chat-sidebar, .chat-workspace { background: var(--surface); border: 1px solid var(--border); border-radius: 1rem; }
|
||||
.chat-sidebar { padding: .8rem; }
|
||||
.chat-sidebar-head, .chat-toolbar, .chat-compose-actions { display: flex; align-items: center; justify-content: space-between; gap: .6rem; }
|
||||
.chat-session-list { display: grid; gap: .35rem; margin-top: 1rem; }
|
||||
.chat-session { display: grid; gap: .25rem; text-align: left; background: transparent; color: var(--fg-muted); border: 1px solid transparent; border-radius: .65rem; padding: .7rem; cursor: pointer; }
|
||||
.chat-session:hover, .chat-session.active { background: var(--surface-hover); color: var(--fg); border-color: var(--border-strong); }
|
||||
.chat-session span { font-size: .72rem; }
|
||||
.chat-workspace { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; overflow: hidden; min-width: 0; }
|
||||
.chat-workspace-head { display: flex; align-items: center; justify-content: space-between; padding: .85rem 1.1rem; border-bottom: 1px solid var(--border); }
|
||||
.chat-session-tools { position: relative; margin-left: .6rem; color: var(--fg-muted); font-size: .78rem; }
|
||||
.chat-session-tools summary { cursor: pointer; }
|
||||
.chat-session-tool-list { position: absolute; z-index: 2; right: 0; top: calc(100% + .45rem); display: flex; flex-wrap: wrap; gap: .35rem; width: min(22rem, 72vw); padding: .55rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: .65rem; box-shadow: 0 .7rem 2rem color-mix(in srgb, #000 30%, transparent); }
|
||||
.chat-select { background: var(--surface-hover); color: var(--fg); border: 1px solid var(--border-strong); border-radius: .5rem; padding: .5rem .65rem; }
|
||||
.chat-transcript { overflow: auto; padding: 1.25rem; display: grid; align-content: start; gap: 1rem; }
|
||||
.chat-empty-state { align-self: center; justify-self: center; width: min(100%, 40rem); padding: 3rem 1rem; text-align: center; }
|
||||
.chat-empty-mark { display: grid; place-items: center; width: 2.6rem; height: 2.6rem; margin: 0 auto 1rem; border: 1px solid var(--border-strong); border-radius: .8rem; color: var(--accent); font-weight: 700; }
|
||||
.chat-empty-state h2 { margin: 0; font-size: clamp(1.5rem, 3vw, 2.25rem); letter-spacing: -.04em; }
|
||||
.chat-empty-state p { margin: .6rem 0 1.4rem; color: var(--fg-muted); }
|
||||
.chat-prompt-grid { display: flex; justify-content: center; flex-wrap: wrap; gap: .5rem; }
|
||||
.chat-prompt { padding: .55rem .75rem; border: 1px solid var(--border); border-radius: 999px; background: var(--surface-hover); color: var(--fg-muted); cursor: pointer; }
|
||||
.chat-prompt:hover { color: var(--fg); border-color: var(--border-strong); }
|
||||
.chat-bubble-row { display: flex; }
|
||||
.chat-bubble-row.user { justify-content: flex-end; }
|
||||
.chat-bubble { max-width: min(78ch, 86%); padding: .8rem 1rem; border-radius: .85rem; background: var(--surface-hover); line-height: 1.5; }
|
||||
.chat-bubble-row.user .chat-bubble { background: color-mix(in srgb, var(--accent) 18%, var(--surface)); }
|
||||
.chat-bubble-meta { font-size: .7rem; color: var(--fg-muted); margin-bottom: .3rem; }
|
||||
.chat-bubble-text { white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.chat-bubble-text p { margin: 0 0 .7rem; }
|
||||
.chat-bubble-text p:last-child { margin-bottom: 0; }
|
||||
.chat-bubble-text h1, .chat-bubble-text h2, .chat-bubble-text h3 { margin: .2rem 0 .6rem; line-height: 1.2; letter-spacing: -.02em; }
|
||||
.chat-bubble-text h1 { font-size: 1.25rem; }
|
||||
.chat-bubble-text h2 { font-size: 1.1rem; }
|
||||
.chat-bubble-text h3 { font-size: 1rem; }
|
||||
.chat-bubble-text ul, .chat-bubble-text ol { margin: .25rem 0 .75rem; padding-left: 1.35rem; }
|
||||
.chat-bubble-text blockquote { margin: .5rem 0; padding: .35rem .75rem; border-left: 3px solid var(--accent); color: var(--fg-muted); }
|
||||
.chat-bubble-text code { padding: .1rem .3rem; border-radius: .3rem; background: color-mix(in srgb, var(--fg) 10%, transparent); font-size: .9em; }
|
||||
.chat-bubble-text pre { margin: .6rem 0; padding: .75rem; overflow: auto; border: 1px solid var(--border); border-radius: .5rem; background: color-mix(in srgb, var(--surface) 75%, #000); }
|
||||
.chat-bubble-text pre code { padding: 0; background: transparent; }
|
||||
.chat-bubble-text a { color: var(--accent); }
|
||||
.chat-bubble-text table { width: 100%; margin: .7rem 0; border-collapse: collapse; font-size: .92em; }
|
||||
.chat-bubble-text th, .chat-bubble-text td { padding: .45rem .6rem; text-align: left; border: 1px solid var(--border); }
|
||||
.chat-bubble-text th { color: var(--fg); background: color-mix(in srgb, var(--surface) 70%, transparent); font-weight: 700; }
|
||||
.chat-compose { padding: .85rem 1rem 1rem; border-top: 1px solid var(--border); }
|
||||
.chat-composer-shell { position: relative; padding: .7rem .8rem .55rem; background: var(--surface-hover); border: 1px solid var(--border-strong); border-radius: .9rem; }
|
||||
.chat-composer { width: 100%; min-height: 3.3rem; resize: vertical; box-sizing: border-box; background: transparent; color: var(--fg); border: 0; padding: .25rem 0; font: inherit; }
|
||||
.chat-composer:focus { outline: 0; }
|
||||
.chat-command-menu { position: absolute; z-index: 3; right: .8rem; bottom: calc(100% - .25rem); left: .8rem; display: grid; gap: .15rem; max-height: 18rem; overflow: auto; padding: .35rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: .7rem; box-shadow: 0 .7rem 2rem color-mix(in srgb, #000 30%, transparent); }
|
||||
.chat-command-option { display: grid; grid-template-columns: minmax(8rem, 14rem) 1fr; gap: .65rem; align-items: center; padding: .5rem .6rem; text-align: left; color: var(--fg-muted); background: transparent; border: 1px solid transparent; border-radius: .45rem; cursor: pointer; }
|
||||
.chat-command-option:hover, .chat-command-option[aria-selected="true"] { color: var(--fg); background: var(--surface-hover); border-color: var(--border); }
|
||||
.chat-command-option code { color: var(--fg); }
|
||||
.chat-compose-actions { margin-top: .55rem; }
|
||||
.chat-hint, .chat-empty { color: var(--fg-muted); font-size: .78rem; }
|
||||
.chat-command-help { color: var(--fg-muted); font-size: .78rem; margin-right: auto; }
|
||||
.chat-command-help summary { cursor: pointer; }
|
||||
.chat-command-list { min-width: min(34rem, 70vw); max-width: 40rem; padding-top: .4rem; line-height: 1.45; }
|
||||
.chat-command-item { display: grid; grid-template-columns: minmax(10rem, 16rem) 1fr; gap: .7rem; padding: .3rem 0; }
|
||||
.chat-command-item code { color: var(--fg); }
|
||||
@media (max-width: 760px) { .chat-topbar, .chat-topbar-actions { align-items: flex-start; flex-direction: column; } .chat-layout { grid-template-columns: 1fr; } .chat-sidebar { max-height: 14rem; overflow: auto; } }
|
||||
@media (max-width: 520px) { .chat-update-panel { align-items: flex-start; flex-direction: column; } }
|
||||
@@ -0,0 +1,609 @@
|
||||
import { el, mount } from "../base/dom.js";
|
||||
import { api } from "../base/api.js";
|
||||
|
||||
import "./chat.css";
|
||||
|
||||
const CHANNEL_KEY = "archie.web.chat.channel";
|
||||
|
||||
function channelID() {
|
||||
let id = localStorage.getItem(CHANNEL_KEY);
|
||||
if (!id) {
|
||||
id = crypto.randomUUID();
|
||||
localStorage.setItem(CHANNEL_KEY, id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
function sessionTitle(session) {
|
||||
return session.title || session.branch_name || `Conversation ${session.session_id.slice(0, 8)}`;
|
||||
}
|
||||
|
||||
function inlineMarkdown(text) {
|
||||
const fragment = document.createDocumentFragment();
|
||||
const pattern = /(~~[^~]+~~|\*\*[^*]+\*\*|__[^_]+__|`[^`]+`|\[[^\]]+\]\(https?:\/\/[^\s)]+\)|\*[^*]+\*|_[^_]+_)/g;
|
||||
let cursor = 0;
|
||||
for (const match of text.matchAll(pattern)) {
|
||||
if (match.index > cursor) fragment.append(document.createTextNode(text.slice(cursor, match.index)));
|
||||
const value = match[0];
|
||||
if (value.startsWith("~~")) fragment.append(el("del", value.slice(2, -2)));
|
||||
else if (value.startsWith("**") || value.startsWith("__")) fragment.append(el("strong", value.slice(2, -2)));
|
||||
else if (value.startsWith("`")) fragment.append(el("code", value.slice(1, -1)));
|
||||
else if (value.startsWith("[")) {
|
||||
const link = value.match(/^\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)$/);
|
||||
fragment.append(el("a", { href: link[2], target: "_blank", rel: "noreferrer" }, link[1]));
|
||||
} else fragment.append(el("em", value.slice(1, -1)));
|
||||
cursor = match.index + value.length;
|
||||
}
|
||||
if (cursor < text.length) fragment.append(document.createTextNode(text.slice(cursor)));
|
||||
return fragment;
|
||||
}
|
||||
|
||||
function renderMarkdown(text) {
|
||||
const root = el("div.chat-bubble-text");
|
||||
const lines = String(text || "").split("\n");
|
||||
let paragraph = [];
|
||||
let list = null;
|
||||
let code = null;
|
||||
const flushParagraph = () => {
|
||||
if (paragraph.length) {
|
||||
root.append(el("p", inlineMarkdown(paragraph.join(" "))));
|
||||
paragraph = [];
|
||||
}
|
||||
};
|
||||
const flushList = () => {
|
||||
if (list) root.append(list);
|
||||
list = null;
|
||||
};
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
const line = lines[index];
|
||||
if (line.startsWith("```")) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) {
|
||||
root.append(el("pre", el("code", code.join("\n"))));
|
||||
code = null;
|
||||
} else code = [];
|
||||
continue;
|
||||
}
|
||||
if (code) {
|
||||
code.push(line);
|
||||
continue;
|
||||
}
|
||||
const headerCells = tableCells(line);
|
||||
const separatorCells = index + 1 < lines.length ? tableCells(lines[index + 1]) : null;
|
||||
if (headerCells && separatorCells && isTableSeparator(separatorCells) && headerCells.length === separatorCells.length) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
const rows = [];
|
||||
for (index += 2; index < lines.length; index++) {
|
||||
const cells = tableCells(lines[index]);
|
||||
if (!cells || cells.length !== headerCells.length) {
|
||||
index--;
|
||||
break;
|
||||
}
|
||||
rows.push(cells);
|
||||
}
|
||||
const body = el("tbody");
|
||||
for (const row of rows) {
|
||||
body.append(el("tr", ...row.map((cell) => el("td", inlineMarkdown(cell)))));
|
||||
}
|
||||
root.append(el(
|
||||
"table",
|
||||
el("thead", el("tr", ...headerCells.map((cell) => el("th", inlineMarkdown(cell))))),
|
||||
body,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
if (!line.trim()) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
continue;
|
||||
}
|
||||
const heading = line.match(/^(#{1,3})\s+(.+)$/);
|
||||
if (heading) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
root.append(el(`h${heading[1].length}`, inlineMarkdown(heading[2])));
|
||||
continue;
|
||||
}
|
||||
const bullet = line.match(/^\s*[-*]\s+(.+)$/);
|
||||
const ordered = line.match(/^\s*\d+[.)]\s+(.+)$/);
|
||||
if (bullet || ordered) {
|
||||
flushParagraph();
|
||||
const tag = ordered ? "ol" : "ul";
|
||||
if (!list || list.tagName.toLowerCase() !== tag) {
|
||||
flushList();
|
||||
list = el(tag);
|
||||
}
|
||||
list.append(el("li", inlineMarkdown((bullet || ordered)[1])));
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith(">")) {
|
||||
flushParagraph();
|
||||
flushList();
|
||||
root.append(el("blockquote", inlineMarkdown(line.replace(/^>\s?/, ""))));
|
||||
continue;
|
||||
}
|
||||
paragraph.push(line);
|
||||
}
|
||||
flushParagraph();
|
||||
flushList();
|
||||
if (code) root.append(el("pre", el("code", code.join("\n"))));
|
||||
return root;
|
||||
}
|
||||
|
||||
function tableCells(line) {
|
||||
const value = String(line || "").trim();
|
||||
if (!value.includes("|")) return null;
|
||||
const unwrapped = value.replace(/^\|/, "").replace(/\|$/, "");
|
||||
const cells = unwrapped.split("|").map((cell) => cell.trim());
|
||||
return cells.length > 1 && cells.every((cell) => cell !== "") ? cells : null;
|
||||
}
|
||||
|
||||
function isTableSeparator(cells) {
|
||||
return cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
||||
}
|
||||
|
||||
function chatBubble(message) {
|
||||
const from = message.from ?? message.From ?? "";
|
||||
const text = message.text ?? message.Text ?? "";
|
||||
const isAssistant = from !== "web";
|
||||
return el(
|
||||
`div.chat-bubble-row.${isAssistant ? "assistant" : "user"}`,
|
||||
el(
|
||||
"div.chat-bubble",
|
||||
el("div.chat-bubble-meta", isAssistant ? "Archie" : "You"),
|
||||
isAssistant ? renderMarkdown(text) : el("div.chat-bubble-text", text),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function chatPage() {
|
||||
const root = el("div.chat-page");
|
||||
const sessionList = el("div.chat-session-list");
|
||||
const transcript = el("div.chat-transcript");
|
||||
const composer = el("textarea.chat-composer", {
|
||||
rows: 3,
|
||||
placeholder: "Message Archie… (commands like /status and /new work here)",
|
||||
"aria-label": "Message Archie",
|
||||
});
|
||||
const send = el("button.btn.btn-primary", "Send");
|
||||
const stop = el("button.btn", { disabled: true }, "Stop");
|
||||
const status = el("span.chat-status", "Ready");
|
||||
const persona = el("select.chat-select", { "aria-label": "Personality" });
|
||||
const provider = el("select.chat-select", { "aria-label": "Provider", hidden: true });
|
||||
const model = el("select.chat-select", { "aria-label": "Model" });
|
||||
const restart = el("button.btn", { hidden: true, onclick: () => {
|
||||
if (!window.confirm("Reload the Telegram chat adapter?")) return;
|
||||
composer.value = "/restart";
|
||||
sendMessage();
|
||||
} }, "Reload Telegram");
|
||||
let currentSession = "";
|
||||
let sessions = [];
|
||||
let sending = false;
|
||||
let activeController = null;
|
||||
let updateSnapshot = null;
|
||||
let selectorData = {};
|
||||
let commandSpecs = [];
|
||||
let commandMatches = [];
|
||||
let commandSelection = 0;
|
||||
|
||||
const emptyState = el(
|
||||
"div.chat-empty-state",
|
||||
el("span.chat-empty-mark", "A"),
|
||||
el("h2", "What are we working on?"),
|
||||
el("p", "Ask Archie anything, or start with one of these prompts."),
|
||||
el("div.chat-prompt-grid",
|
||||
...["Summarise active tasks", "Show running agents", "Check system status"].map((prompt) =>
|
||||
el("button.chat-prompt", { onclick: () => { composer.value = prompt; composer.focus(); } }, prompt)),
|
||||
),
|
||||
);
|
||||
|
||||
function appendMessage(message) {
|
||||
emptyState.remove();
|
||||
transcript.append(chatBubble(message));
|
||||
transcript.scrollTop = transcript.scrollHeight;
|
||||
}
|
||||
|
||||
function renderSessions() {
|
||||
sessionList.replaceChildren();
|
||||
sessionTools.hidden = !currentSession;
|
||||
if (!sessions.length) {
|
||||
sessionList.append(el("div.chat-empty", "Your conversations will appear here."));
|
||||
return;
|
||||
}
|
||||
for (const session of sessions) {
|
||||
const item = el(
|
||||
"button.chat-session",
|
||||
{ class: currentSession === session.session_id ? "active" : "", onclick: () => selectSession(session.session_id) },
|
||||
el("strong", sessionTitle(session)),
|
||||
el("span", new Date(session.last_active_at).toLocaleString()),
|
||||
);
|
||||
sessionList.append(item);
|
||||
}
|
||||
}
|
||||
|
||||
function renderSelectors(data) {
|
||||
selectorData = data;
|
||||
persona.replaceChildren(el("option", { value: "" }, "Personality"));
|
||||
for (const name of data.personas || []) persona.append(el("option", { value: name }, name));
|
||||
persona.value = data.active_persona || "";
|
||||
const providers = data.providers || [];
|
||||
provider.hidden = providers.length === 0;
|
||||
provider.replaceChildren(el("option", { value: "" }, "Provider"));
|
||||
for (const name of providers) provider.append(el("option", { value: name }, name));
|
||||
provider.value = data.active_provider || providers[0] || "";
|
||||
renderModelsForProvider(provider.value);
|
||||
restart.hidden = !data.restart_available;
|
||||
dangerousPanel.hidden = !data.dangerous_available;
|
||||
if (data.dangerous_available) refreshDangerous();
|
||||
}
|
||||
|
||||
function renderModelsForProvider(providerName) {
|
||||
const grouped = selectorData.models_by_provider || {};
|
||||
const names = providerName && grouped[providerName] ? grouped[providerName] : selectorData.models || [];
|
||||
model.replaceChildren(el("option", { value: "" }, "Model"));
|
||||
for (const name of names) model.append(el("option", { value: name }, name));
|
||||
model.value = selectorData.active_model || "";
|
||||
}
|
||||
|
||||
function renderCommands(data) {
|
||||
const commands = data.commands || [];
|
||||
commandSpecs = commands.map((item) => typeof item === "string" ? { command: item, usage: item, description: "" } : item);
|
||||
const list = el("div.chat-command-list");
|
||||
if (!commands.length) list.append(el("span", "Send a message to begin."));
|
||||
for (const item of commands) {
|
||||
const spec = typeof item === "string" ? { command: item, usage: item, description: "" } : item;
|
||||
list.append(el("div.chat-command-item", el("code", spec.usage || spec.command), el("span", spec.description || "")));
|
||||
}
|
||||
commandHelp.replaceChildren(el("summary", `Available commands (${commands.length})`), list);
|
||||
}
|
||||
|
||||
const updatePanel = el("div.chat-update-panel");
|
||||
const dangerousPanel = el("div.chat-dangerous-panel", { hidden: true });
|
||||
const commandMenu = el("div.chat-command-menu", { hidden: true, role: "listbox", "aria-label": "Commands" });
|
||||
const sessionTools = el("details.chat-session-tools", { hidden: true });
|
||||
|
||||
function commandQuery() {
|
||||
const before = composer.value.slice(0, composer.selectionStart);
|
||||
const match = before.match(/(?:^|\s)\/([^\s]*)$/);
|
||||
return match ? match[1].toLowerCase() : null;
|
||||
}
|
||||
|
||||
function hideCommandMenu() {
|
||||
commandMenu.hidden = true;
|
||||
commandMatches = [];
|
||||
commandSelection = 0;
|
||||
}
|
||||
|
||||
function renderCommandMenu() {
|
||||
const query = commandQuery();
|
||||
if (query === null) {
|
||||
hideCommandMenu();
|
||||
return;
|
||||
}
|
||||
commandMatches = commandSpecs.filter((spec) => {
|
||||
const haystack = `${spec.command} ${spec.description}`.toLowerCase();
|
||||
return haystack.includes(query);
|
||||
}).slice(0, 8);
|
||||
if (!commandMatches.length) {
|
||||
hideCommandMenu();
|
||||
return;
|
||||
}
|
||||
commandSelection = Math.min(commandSelection, commandMatches.length - 1);
|
||||
commandMenu.replaceChildren(...commandMatches.map((spec, index) => el(
|
||||
"button.chat-command-option",
|
||||
{
|
||||
type: "button",
|
||||
role: "option",
|
||||
"aria-selected": index === commandSelection,
|
||||
onmousedown: (event) => event.preventDefault(),
|
||||
onclick: () => chooseCommand(index),
|
||||
},
|
||||
el("code", spec.command),
|
||||
el("span", spec.description || spec.usage || ""),
|
||||
)));
|
||||
commandMenu.hidden = false;
|
||||
}
|
||||
|
||||
function chooseCommand(index) {
|
||||
const spec = commandMatches[index];
|
||||
if (!spec) return;
|
||||
const before = composer.value.slice(0, composer.selectionStart);
|
||||
const after = composer.value.slice(composer.selectionStart);
|
||||
const start = before.search(/(?:^|\s)\/[^\s]*$/);
|
||||
const tokenStart = start < 0 ? before.length : start + (before[start] === " " ? 1 : 0);
|
||||
composer.value = `${before.slice(0, tokenStart)}${spec.command} ${after}`;
|
||||
const cursor = tokenStart + spec.command.length + 1;
|
||||
composer.setSelectionRange(cursor, cursor);
|
||||
hideCommandMenu();
|
||||
composer.focus();
|
||||
}
|
||||
|
||||
function renderUpdate(data) {
|
||||
updatePanel.replaceChildren();
|
||||
updateSnapshot = data?.snapshot || null;
|
||||
const available = data?.available || [];
|
||||
if (!available.length) {
|
||||
updatePanel.append(el("span", data?.snapshot?.deferred ? "Update deferred." : "Archie is up to date."));
|
||||
return;
|
||||
}
|
||||
updatePanel.append(
|
||||
el("div", el("strong", "Update available"), el("span", available.map((component) => `${component.Label || component.label}: ${component.Available || component.available}`).join(" · "))),
|
||||
el("div.chat-update-actions",
|
||||
el("button.btn", { onclick: async () => { await api.chatUpdateDefer(updateSnapshot); renderUpdate({ snapshot: { deferred: true }, available: [] }); status.textContent = "Update deferred"; } }, "Defer"),
|
||||
data.can_install ? el("button.btn.btn-primary", { onclick: async () => { status.textContent = "Installing…"; await api.chatUpdateInstall(updateSnapshot); status.textContent = "Update started"; refreshUpdate(); } }, "Install update") : null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function refreshUpdate() {
|
||||
try {
|
||||
renderUpdate(await api.chatUpdate());
|
||||
} catch (err) {
|
||||
updatePanel.replaceChildren(el("span", err.message || "Updates unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
function dangerousActionButton(id, decision, label) {
|
||||
return el("button.btn", { onclick: async () => {
|
||||
try {
|
||||
await api.chatDangerousDecision(id, decision);
|
||||
status.textContent = decision === "deny" ? "Action denied" : "Action approved";
|
||||
await refreshDangerous();
|
||||
} catch (err) {
|
||||
status.textContent = err.message || "Approval failed";
|
||||
}
|
||||
} }, label);
|
||||
}
|
||||
|
||||
function renderDangerous(data) {
|
||||
dangerousPanel.replaceChildren();
|
||||
const checkpoints = data?.checkpoints || [];
|
||||
const pending = data?.pending || [];
|
||||
const checkpoint = el("select.chat-select", { "aria-label": "Checkpoint" },
|
||||
el("option", { value: "" }, "Select checkpoint"),
|
||||
...checkpoints.map((item) => el("option", { value: item.Number ?? item.number }, `${item.Number ?? item.number} — ${item.Label ?? item.label ?? "checkpoint"}`)),
|
||||
);
|
||||
const stopSpec = el("input.chat-dangerous-input", { placeholder: "Process name or id", "aria-label": "Process name or id" });
|
||||
const request = (kind, spec) => async () => {
|
||||
if (!String(spec.value || spec).trim()) return;
|
||||
try {
|
||||
await api.chatDangerousRequest(kind, String(spec.value || spec).trim());
|
||||
status.textContent = "Approval requested";
|
||||
await refreshDangerous();
|
||||
} catch (err) {
|
||||
status.textContent = err.message || "Could not request action";
|
||||
}
|
||||
};
|
||||
const pendingRows = pending.length ? pending.map((action) => el("div.chat-dangerous-pending",
|
||||
el("span", action.description),
|
||||
el("div.chat-update-actions", dangerousActionButton(action.id, "approve", "Approve"), dangerousActionButton(action.id, "permanent", "Approve for 24h"), dangerousActionButton(action.id, "deny", "Deny")),
|
||||
)) : [el("span.chat-hint", "No pending dangerous actions.")];
|
||||
dangerousPanel.append(
|
||||
el("div.chat-dangerous-head", el("strong", "Dangerous actions"), el("span.chat-hint", "Every action requires approval.")),
|
||||
el("div.chat-dangerous-controls",
|
||||
el("div", checkpoint, el("button.btn", { onclick: request("rollback", checkpoint) }, "Request rollback")),
|
||||
el("div", stopSpec, el("button.btn", { onclick: request("stop", stopSpec) }, "Request stop")),
|
||||
),
|
||||
el("div.chat-dangerous-pending-list", ...pendingRows),
|
||||
);
|
||||
}
|
||||
|
||||
async function refreshDangerous() {
|
||||
try {
|
||||
renderDangerous(await api.chatDangerous());
|
||||
} catch (err) {
|
||||
dangerousPanel.replaceChildren(el("span.chat-hint", err.message || "Dangerous actions unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshSessions() {
|
||||
const data = await api.chatSessions();
|
||||
sessions = data.sessions || [];
|
||||
renderSelectors(data);
|
||||
renderCommands(data);
|
||||
renderSessions();
|
||||
if (!currentSession && sessions[0]) await selectSession(sessions[0].session_id);
|
||||
}
|
||||
|
||||
async function runSessionCommand(command) {
|
||||
if (!currentSession) return;
|
||||
try {
|
||||
status.textContent = "Working…";
|
||||
const result = await api.chatMessage(channelID(), command);
|
||||
if (result.session_id) currentSession = result.session_id;
|
||||
status.textContent = result.reply || "Session updated";
|
||||
await refreshSessions();
|
||||
if (currentSession) await selectSession(currentSession);
|
||||
sessionTools.open = false;
|
||||
} catch (err) {
|
||||
status.textContent = err.message || "Session action failed";
|
||||
}
|
||||
}
|
||||
|
||||
function promptSessionCommand(command, message) {
|
||||
const value = window.prompt(message);
|
||||
if (value?.trim()) runSessionCommand(`${command} ${value.trim()}`);
|
||||
}
|
||||
|
||||
async function selectSession(id) {
|
||||
currentSession = id;
|
||||
renderSessions();
|
||||
transcript.replaceChildren();
|
||||
await api.chatMessage(channelID(), `/resume ${id}`);
|
||||
persona.value = selectorData.active_personas?.[id] || selectorData.active_persona || "";
|
||||
const messages = await api.chatMessages(id);
|
||||
for (const message of messages) appendMessage(message);
|
||||
if (!transcript.children.length) transcript.append(emptyState);
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const text = composer.value.trim();
|
||||
if (!text || sending) return;
|
||||
hideCommandMenu();
|
||||
sending = true;
|
||||
send.disabled = true;
|
||||
stop.disabled = false;
|
||||
composer.disabled = true;
|
||||
status.textContent = "Thinking…";
|
||||
appendMessage({ from: "web", text });
|
||||
composer.value = "";
|
||||
const replyBubble = el("div.chat-bubble-row.assistant", el("div.chat-bubble", el("div.chat-bubble-meta", "Archie"), el("div.chat-bubble-text")));
|
||||
transcript.append(replyBubble);
|
||||
const replyText = replyBubble.querySelector(".chat-bubble-text");
|
||||
let timeout;
|
||||
try {
|
||||
activeController = new AbortController();
|
||||
timeout = setTimeout(() => activeController.abort(), 120000);
|
||||
const response = await fetch("/api/chat/stream", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Accept: "text/event-stream" },
|
||||
body: JSON.stringify({ channel_id: channelID(), source_id: crypto.randomUUID(), text }),
|
||||
signal: activeController.signal,
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.text()) || `${response.status} ${response.statusText}`);
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
|
||||
const frames = buffer.split("\n\n");
|
||||
buffer = frames.pop() || "";
|
||||
for (const frame of frames) {
|
||||
const line = frame.split("\n").find((part) => part.startsWith("data: "));
|
||||
if (!line) continue;
|
||||
const event = JSON.parse(line.slice(6));
|
||||
if (event.session_id) currentSession = event.session_id;
|
||||
if (event.type === "delta") replyText.textContent += event.text;
|
||||
if (event.type === "done" && !replyText.textContent) replyText.textContent = event.text;
|
||||
if (event.type === "error") throw new Error(event.text);
|
||||
}
|
||||
if (done) break;
|
||||
}
|
||||
if (replyText.isConnected) replyText.replaceWith(renderMarkdown(replyText.textContent));
|
||||
status.textContent = "Ready";
|
||||
await refreshSessions();
|
||||
if (currentSession) await selectSession(currentSession);
|
||||
} catch (err) {
|
||||
if (err.name === "AbortError") {
|
||||
replyText.textContent = "Turn stopped.";
|
||||
status.textContent = "Stopped";
|
||||
} else {
|
||||
replyText.textContent = `Unable to complete that turn: ${err.message || err}`;
|
||||
status.textContent = "Error";
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
activeController = null;
|
||||
sending = false;
|
||||
send.disabled = false;
|
||||
stop.disabled = true;
|
||||
composer.disabled = false;
|
||||
composer.focus();
|
||||
}
|
||||
}
|
||||
|
||||
send.onclick = sendMessage;
|
||||
stop.onclick = () => {
|
||||
const sessionID = currentSession;
|
||||
status.textContent = "Stopping…";
|
||||
if (sessionID) void api.chatCancel(sessionID).catch(() => {});
|
||||
activeController?.abort();
|
||||
};
|
||||
composer.oninput = renderCommandMenu;
|
||||
composer.onkeydown = (event) => {
|
||||
if (!commandMenu.hidden && commandMatches.length) {
|
||||
if (event.key === "ArrowDown") {
|
||||
event.preventDefault();
|
||||
commandSelection = (commandSelection + 1) % commandMatches.length;
|
||||
renderCommandMenu();
|
||||
return;
|
||||
}
|
||||
if (event.key === "ArrowUp") {
|
||||
event.preventDefault();
|
||||
commandSelection = (commandSelection + commandMatches.length - 1) % commandMatches.length;
|
||||
renderCommandMenu();
|
||||
return;
|
||||
}
|
||||
if (event.key === "Enter" && !event.ctrlKey && !event.metaKey) {
|
||||
event.preventDefault();
|
||||
chooseCommand(commandSelection);
|
||||
return;
|
||||
}
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
hideCommandMenu();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
||||
event.preventDefault();
|
||||
sendMessage();
|
||||
}
|
||||
};
|
||||
persona.onchange = async () => {
|
||||
if (!currentSession || !persona.value) return;
|
||||
await api.chatPersona(currentSession, persona.value);
|
||||
selectorData.active_personas = { ...(selectorData.active_personas || {}), [currentSession]: persona.value };
|
||||
status.textContent = `Personality: ${persona.value}`;
|
||||
};
|
||||
provider.onchange = async () => {
|
||||
renderModelsForProvider(provider.value);
|
||||
if (!provider.value) return;
|
||||
await applyModelCommand(`/model --provider ${provider.value}`);
|
||||
};
|
||||
model.onchange = async () => {
|
||||
if (model.value) await applyModelCommand(`/model ${model.value}`);
|
||||
};
|
||||
async function applyModelCommand(command) {
|
||||
try {
|
||||
const result = await api.chatMessage(channelID(), command);
|
||||
status.textContent = result.reply || "Model updated";
|
||||
await refreshSessions();
|
||||
} catch (err) {
|
||||
status.textContent = `Model update failed: ${err.message || err}`;
|
||||
}
|
||||
};
|
||||
|
||||
const commandHelp = el("details.chat-command-help");
|
||||
const checkUpdates = el("button.btn", { onclick: refreshUpdate }, "Check updates");
|
||||
const sessionActions = el(
|
||||
"div.chat-session-tool-list",
|
||||
el("button.btn", { type: "button", onclick: () => runSessionCommand("/retry") }, "Retry"),
|
||||
el("button.btn", { type: "button", onclick: () => runSessionCommand("/undo") }, "Undo"),
|
||||
el("button.btn", { type: "button", onclick: () => promptSessionCommand("/title", "Session title") }, "Rename"),
|
||||
el("button.btn", { type: "button", onclick: () => promptSessionCommand("/branch", "Branch name (optional)") }, "Branch"),
|
||||
el("button.btn", { type: "button", onclick: () => runSessionCommand("/compress --preview") }, "Preview compression"),
|
||||
);
|
||||
sessionTools.append(el("summary", "Session actions"), sessionActions);
|
||||
const chatTopbar = el(
|
||||
"div.chat-topbar",
|
||||
el("div", el("span.chat-kicker", "ARCHIE WORKSPACE"), el("h1.page-title", "Chat")),
|
||||
el("div.chat-topbar-actions", el("span.chat-status", status), restart, checkUpdates, persona, provider, model),
|
||||
);
|
||||
const chatLayout = el(
|
||||
"div.chat-layout",
|
||||
el("aside.chat-sidebar", el("div.chat-sidebar-head", el("strong", "Conversations"), el("button.btn.btn-primary", { onclick: () => { composer.value = "/new"; sendMessage(); } }, "+ New chat")), sessionList),
|
||||
el(
|
||||
"section.chat-workspace",
|
||||
el("div.chat-workspace-head", el("span.chat-workspace-label", "PERSONAL ASSISTANT"), commandHelp, sessionTools),
|
||||
transcript,
|
||||
el("div.chat-compose", el("div.chat-composer-shell", composer, commandMenu, el("div.chat-compose-actions", el("span.chat-hint", "⌘/Ctrl + Enter to send"), stop, send))),
|
||||
),
|
||||
);
|
||||
mount(root, el(
|
||||
"div.chat-page-content",
|
||||
chatTopbar,
|
||||
updatePanel,
|
||||
dangerousPanel,
|
||||
chatLayout,
|
||||
));
|
||||
|
||||
transcript.append(emptyState);
|
||||
|
||||
refreshSessions().catch((err) => { status.textContent = err.message || "Chat unavailable"; });
|
||||
refreshUpdate();
|
||||
return root;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { channelsPage } from "./channels/channels.js";
|
||||
import { settingsPage } from "./settings/settings.js";
|
||||
import { logsPage } from "./logs/logs.js";
|
||||
import { memoryPage } from "./memory/memory.js";
|
||||
import { chatPage } from "./chat/chat.js";
|
||||
|
||||
/**
|
||||
* Routes are declared once here. Adding a section means adding a feature
|
||||
@@ -20,6 +21,7 @@ import { memoryPage } from "./memory/memory.js";
|
||||
*/
|
||||
const routes = [
|
||||
{ path: "/", label: "Dashboard", icon: "dashboard", view: dashboardPage },
|
||||
{ path: "/chat", label: "Chat", icon: "chat", view: chatPage },
|
||||
{ path: "/tasks", label: "Tasks", icon: "tasks", view: tasksPage },
|
||||
{ path: "/logs", label: "Logs", icon: "logs", view: logsPage },
|
||||
{ path: "/skills", label: "Skills", icon: "skills", view: skillsPage },
|
||||
|
||||
Reference in new issue
Block a user