feat(web): add lightweight workspace matching existing UI
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
.assistant-message-footer {
|
||||
position: relative;
|
||||
color: var(--message-footer-color, hsl(var(--muted-foreground)));
|
||||
}
|
||||
|
||||
.assistant-message-footer__metadata,
|
||||
.assistant-message-footer__status {
|
||||
font-size: .75rem;
|
||||
line-height: 1.25rem;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.assistant-message-footer__metadata time {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.assistant-message-footer__actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: .25rem;
|
||||
margin-left: -.5rem;
|
||||
}
|
||||
|
||||
.assistant-message-footer__action {
|
||||
display: inline-flex;
|
||||
height: 2.5rem;
|
||||
min-height: 2.5rem;
|
||||
align-items: center;
|
||||
gap: .375rem;
|
||||
border: 0;
|
||||
border-radius: var(--radius, 0);
|
||||
padding: 0 .5rem;
|
||||
font: inherit;
|
||||
font-size: .75rem;
|
||||
line-height: 1rem;
|
||||
color: inherit;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
transition: var(--message-footer-transition, color 150ms cubic-bezier(.4, 0, .2, 1), background-color 150ms cubic-bezier(.4, 0, .2, 1));
|
||||
}
|
||||
|
||||
.assistant-message-footer__action svg {
|
||||
width: .875rem;
|
||||
height: .875rem;
|
||||
}
|
||||
|
||||
.assistant-message-footer__action:hover {
|
||||
background: var(--message-footer-hover-background, rgb(167 139 250 / .12));
|
||||
color: var(--message-footer-hover-color, #ede9fe);
|
||||
}
|
||||
|
||||
.assistant-message-footer__action:focus-visible {
|
||||
outline: 2px solid var(--message-footer-focus-color, hsl(var(--ring)));
|
||||
}
|
||||
|
||||
.assistant-message-footer__action:disabled {
|
||||
cursor: wait;
|
||||
opacity: .5;
|
||||
}
|
||||
|
||||
.assistant-message-footer__status {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.assistant-message-footer__status--hidden {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border-width: 0;
|
||||
}
|
||||
@@ -2,11 +2,12 @@ import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { Check, Copy, GitFork } from "lucide-preact";
|
||||
import type { Message } from "@/lib/api";
|
||||
import { getMessageGenerationLabel } from "@/lib/message-generation";
|
||||
import "./assistant-message-footer.css";
|
||||
|
||||
const receivedFormat = new Intl.DateTimeFormat(undefined, {
|
||||
year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "2-digit",
|
||||
});
|
||||
const actionClass = "inline-flex h-10 items-center gap-1.5 rounded-md px-2 text-xs transition-colors hover:bg-violet-400/12 hover:text-violet-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring disabled:cursor-wait disabled:opacity-50";
|
||||
const actionClass = "assistant-message-footer__action";
|
||||
|
||||
export function AssistantMessageFooter({ message, onFork, isForking = false }: {
|
||||
message: Message;
|
||||
@@ -32,24 +33,24 @@ export function AssistantMessageFooter({ message, onFork, isForking = false }: {
|
||||
const generation = getMessageGenerationLabel(message);
|
||||
|
||||
return (
|
||||
<div className="text-muted-foreground" data-assistant-footer={message.id} data-mobile-swipe-ignore>
|
||||
<div className="text-xs leading-5 [overflow-wrap:anywhere]">
|
||||
{hasDate ? <><time className="whitespace-nowrap" dateTime={message.createdAt} title={`Received ${received.toLocaleString()}`}>
|
||||
<div className="assistant-message-footer" data-assistant-footer={message.id} data-mobile-swipe-ignore>
|
||||
<div className="assistant-message-footer__metadata">
|
||||
{hasDate ? <><time dateTime={message.createdAt} title={`Received ${received.toLocaleString()}`}>
|
||||
{receivedFormat.format(received)}
|
||||
</time><span aria-hidden="true"> · </span></> : null}
|
||||
<span data-message-generation>{generation ?? "Model not recorded"}</span>
|
||||
</div>
|
||||
<div className="-ml-2 flex flex-wrap items-center gap-1" role="group" aria-label="Message actions">
|
||||
<div className="assistant-message-footer__actions" role="group" aria-label="Message actions">
|
||||
{message.content.trim() ? <button type="button" className={actionClass} onClick={() => void copy()} title="Copy message" aria-label="Copy message">
|
||||
{copyState === "copied" ? <Check className="h-3.5 w-3.5" aria-hidden="true" /> : <Copy className="h-3.5 w-3.5" aria-hidden="true" />}
|
||||
{copyState === "copied" ? <Check aria-hidden="true" /> : <Copy aria-hidden="true" />}
|
||||
<span>{copyState === "copied" ? "Copied" : "Copy"}</span>
|
||||
</button> : null}
|
||||
{onFork ? <button type="button" className={actionClass} onClick={() => onFork(message.id)} disabled={isForking} title="Fork chat from this message" aria-label="Fork chat from this message">
|
||||
<GitFork className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
<GitFork aria-hidden="true" />
|
||||
<span>Fork</span>
|
||||
</button> : null}
|
||||
</div>
|
||||
<span className={copyState === "failed" ? "block text-xs leading-5" : "sr-only"} role="status" aria-live="polite">
|
||||
<span className={`assistant-message-footer__status${copyState === "failed" ? "" : " assistant-message-footer__status--hidden"}`} role="status" aria-live="polite">
|
||||
{copyState === "copied" ? "Message copied." : copyState === "failed" ? "Could not copy. Select the message text and use your browser’s Copy command." : ""}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/* Geometry is shared across regular chat, Agent, and the lightweight view. */
|
||||
.message-bubble {
|
||||
min-width: 0;
|
||||
padding: 13px 17px;
|
||||
border: 1px solid var(--message-bubble-border, hsl(256 37% 57% / .18));
|
||||
border-radius: 18px 18px 18px 5px;
|
||||
background: var(--message-bubble-background, linear-gradient(150deg, hsl(251 36% 15% / .93), hsl(244 35% 11% / .93)));
|
||||
box-shadow: var(--message-bubble-shadow, 0 3px 12px hsl(239 55% 2% / .13));
|
||||
font-size: 14px;
|
||||
line-height: 1.68;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.message-bubble--user {
|
||||
border-color: var(--message-bubble-user-border, hsl(266 80% 72% / .3));
|
||||
border-radius: 18px 18px 5px 18px;
|
||||
background: var(--message-bubble-user-background, linear-gradient(135deg, hsl(264 59% 33% / .86), hsl(259 58% 26% / .9)));
|
||||
color: var(--message-bubble-user-color, hsl(265 55% 97%));
|
||||
}
|
||||
|
||||
.message-bubble .md-content {
|
||||
font-size: inherit;
|
||||
line-height: inherit;
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.message-bubble {
|
||||
padding: 11px 14px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
|
||||
import { SendHorizontal } from "lucide-preact";
|
||||
import { useAgentRoom, type AgentRoomHookResult } from "@/hooks/use-agent-room";
|
||||
import { MarkdownContent } from "@/components/markdown/markdown-content";
|
||||
import { agentTranscriptMessages } from "@/lib/agent-room-presentation";
|
||||
import { agentAttachments, attachmentSize, type AgentAttachment } from "@/lib/agent-attachments";
|
||||
|
||||
const DRAFT_KEY = "sybil.pi-root.draft";
|
||||
const timeFormat = new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit" });
|
||||
|
||||
function readDraft() {
|
||||
try { return sessionStorage.getItem(DRAFT_KEY) || ""; } catch { return ""; }
|
||||
}
|
||||
|
||||
function saveDraft(value: string) {
|
||||
try {
|
||||
if (value) sessionStorage.setItem(DRAFT_KEY, value);
|
||||
else sessionStorage.removeItem(DRAFT_KEY);
|
||||
} catch { /* Draft storage is optional; the shared outbox handles durable delivery. */ }
|
||||
}
|
||||
|
||||
function LiteAgentAttachment({ file, download }: {
|
||||
file: AgentAttachment;
|
||||
download: AgentRoomHookResult["downloadAttachment"];
|
||||
}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const request = useRef<AbortController | null>(null);
|
||||
useEffect(() => () => request.current?.abort(), []);
|
||||
|
||||
const retrieve = async () => {
|
||||
if (request.current) return;
|
||||
const controller = new AbortController();
|
||||
request.current = controller;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const blob = await download(file, controller.signal);
|
||||
if (controller.signal.aborted) return;
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = file.filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
||||
} catch (failure) {
|
||||
if (!controller.signal.aborted) setError(failure instanceof Error ? failure.message : "Download failed.");
|
||||
} finally {
|
||||
request.current = null;
|
||||
if (!controller.signal.aborted) setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
return <li>
|
||||
<span>{file.filename} ({attachmentSize(file.sizeBytes)})</span>{" "}
|
||||
<button type="button" disabled={busy} onClick={() => void retrieve()} aria-label={`Download ${file.filename}`}>
|
||||
{busy ? "Downloading…" : "Download"}
|
||||
</button>
|
||||
{error ? <p className="lite-error" role="alert">{error}</p> : null}
|
||||
</li>;
|
||||
}
|
||||
|
||||
/** Mounted only while Agent is selected; protocol, outbox and read state stay shared. */
|
||||
export function LiteAgent() {
|
||||
const room = useAgentRoom(true);
|
||||
const [draft, setDraft] = useState(readDraft);
|
||||
const [sending, setSending] = useState(false);
|
||||
const [sendError, setSendError] = useState<string | null>(null);
|
||||
const sendingRef = useRef(false);
|
||||
const mounted = useRef(true);
|
||||
const draftRef = useRef(draft);
|
||||
const end = useRef<HTMLDivElement>(null);
|
||||
const scroller = useRef<HTMLDivElement>(null);
|
||||
const viewport = useRef({ pinned: true, top: 0, height: 0, firstId: null as string | null });
|
||||
const messages = useMemo(() => agentTranscriptMessages(room.messages), [room.messages]);
|
||||
const uploads = room.uploadQueue.snapshot();
|
||||
const pendingUploads = uploads.some(file => file.status !== "ready");
|
||||
|
||||
useEffect(() => () => { mounted.current = false; }, []);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const element = scroller.current;
|
||||
if (!element || room.isLoading) return;
|
||||
const previous = viewport.current;
|
||||
const firstId = messages[0]?.id ?? null;
|
||||
if (previous.pinned) element.scrollTop = element.scrollHeight;
|
||||
else if (previous.firstId && firstId !== previous.firstId && messages.some(message => message.id === previous.firstId)) {
|
||||
// Keep the existing messages in place when an older page is prepended.
|
||||
element.scrollTop = previous.top + element.scrollHeight - previous.height;
|
||||
}
|
||||
previous.top = element.scrollTop;
|
||||
previous.height = element.scrollHeight;
|
||||
previous.firstId = firstId;
|
||||
}, [messages, room.isLoading, room.isTyping]);
|
||||
|
||||
useEffect(() => {
|
||||
const markVisibleRead = () => {
|
||||
if (room.isLoading || document.visibilityState !== "visible" || !document.hasFocus()) return;
|
||||
const position = end.current?.getBoundingClientRect();
|
||||
const viewport = scroller.current?.getBoundingClientRect();
|
||||
if (position && viewport && viewport.width > 0 && viewport.height > 0 && position.top >= Math.max(0, viewport.top) && position.top <= Math.min(window.innerHeight, viewport.bottom)) room.markRead();
|
||||
};
|
||||
const scrollElement = scroller.current;
|
||||
markVisibleRead();
|
||||
scrollElement?.addEventListener("scroll", markVisibleRead, { passive: true });
|
||||
window.addEventListener("focus", markVisibleRead);
|
||||
window.addEventListener("resize", markVisibleRead);
|
||||
document.addEventListener("visibilitychange", markVisibleRead);
|
||||
return () => {
|
||||
scrollElement?.removeEventListener("scroll", markVisibleRead);
|
||||
window.removeEventListener("focus", markVisibleRead);
|
||||
window.removeEventListener("resize", markVisibleRead);
|
||||
document.removeEventListener("visibilitychange", markVisibleRead);
|
||||
};
|
||||
}, [room.markRead, room.isLoading, messages]);
|
||||
|
||||
const updateDraft = (value: string) => {
|
||||
draftRef.current = value;
|
||||
setDraft(value);
|
||||
saveDraft(value);
|
||||
};
|
||||
|
||||
const submit = async () => {
|
||||
const content = draftRef.current.trim();
|
||||
if (sendingRef.current || (!content && !uploads.length) || pendingUploads || room.isLoading) return;
|
||||
sendingRef.current = true;
|
||||
setSending(true);
|
||||
setSendError(null);
|
||||
try {
|
||||
const attachments = room.uploadQueue.ready();
|
||||
await room.sendMessage(content, attachments, () => {
|
||||
// A saved outbox row owns retries from here, even if the HTTP response is lost.
|
||||
saveDraft("");
|
||||
draftRef.current = "";
|
||||
if (mounted.current) setDraft("");
|
||||
});
|
||||
} catch (failure) {
|
||||
if (mounted.current) setSendError(failure instanceof Error ? failure.message : "Message could not be sent.");
|
||||
} finally {
|
||||
sendingRef.current = false;
|
||||
if (mounted.current) setSending(false);
|
||||
}
|
||||
};
|
||||
|
||||
return <section className="lite-agent" aria-label="Sybil Agent">
|
||||
<div className="lite-agent-connection" aria-label="Agent connection">
|
||||
<span className="lite-muted" role="status">
|
||||
{room.isLoading ? "Loading conversation…" : room.connection === "connected" ? "Connected" : room.connection === "connecting" ? "Connecting…" : room.connection === "reconnecting" ? "Reconnecting…" : "Offline"}
|
||||
</span>
|
||||
<a href="#lite-agent-end">Latest messages</a>
|
||||
<button type="button" onClick={room.retry} disabled={room.isLoading}>Reconnect</button>
|
||||
</div>
|
||||
<div className="lite-agent-scroll" ref={scroller} onScroll={event => {
|
||||
const element = event.currentTarget;
|
||||
viewport.current.pinned = element.scrollHeight - element.scrollTop - element.clientHeight <= 24;
|
||||
viewport.current.top = element.scrollTop;
|
||||
viewport.current.height = element.scrollHeight;
|
||||
}}>
|
||||
<div className="lite-transcript" aria-label="Agent messages" aria-busy={room.isLoading}>
|
||||
{room.error ? <p className="lite-error" role="alert">{room.error}</p> : null}
|
||||
{room.hasOlder ? <button type="button" onClick={room.loadOlder} disabled={room.isLoadingOlder}>
|
||||
{room.isLoadingOlder ? "Loading older messages…" : "Load older messages"}
|
||||
</button> : null}
|
||||
{room.olderError ? <p className="lite-error" role="alert">{room.olderError}</p> : null}
|
||||
{messages.map(message => {
|
||||
const info = message.metadata && typeof message.metadata === "object" && !Array.isArray(message.metadata)
|
||||
? message.metadata as Record<string, unknown> : {};
|
||||
const status = typeof info.status === "string" ? info.status : "complete";
|
||||
const own = message.role === "user";
|
||||
const failed = own && (status === "failed" || status === "unconfirmed");
|
||||
const files = agentAttachments(message.metadata, message.role);
|
||||
const date = new Date(message.createdAt);
|
||||
return <article key={message.id} className={`lite-message${own ? " lite-message-user" : ""}`} data-agent-message-id={message.id} aria-label={own ? "Your message" : "Sybil's message"}>
|
||||
<div className="lite-message-content">
|
||||
<div className={`message-bubble${own ? " message-bubble--user" : ""}`}>
|
||||
{own ? <p className="lite-plain-text">{message.content}</p>
|
||||
: <MarkdownContent markdown={message.content} allowImages={false} />}
|
||||
{files.length ? <ul className="lite-attachments">{files.map(file => <LiteAgentAttachment key={file.id} file={file} download={room.downloadAttachment} />)}</ul> : null}
|
||||
</div>
|
||||
<div className="lite-message-meta">
|
||||
<span>{own ? "You" : "Sybil"}</span>
|
||||
{Number.isFinite(date.getTime()) && date.getFullYear() >= 2000 ? <time dateTime={message.createdAt} title={date.toLocaleString()}>{timeFormat.format(date)}</time> : null}
|
||||
{own && status === "sending" ? <span role="status">Sending…</span> : null}
|
||||
{own && status === "queued" ? <span role="status">Queued</span> : null}
|
||||
{failed ? <span className="lite-delivery-error" role="status">
|
||||
{status === "unconfirmed" ? "Unconfirmed" : "Not sent"}{" "}
|
||||
<button type="button" aria-label="Retry message" onClick={() => void room.retryMessage(message.id).catch(() => undefined)}>Retry</button>
|
||||
</span> : null}
|
||||
</div>
|
||||
{failed && typeof info.error === "string" ? <p className="lite-error" role="alert">{info.error}</p> : null}
|
||||
</div>
|
||||
</article>;
|
||||
})}
|
||||
{room.isTyping ? <p className="lite-status" role="status">Sybil is thinking…</p> : null}
|
||||
<div id="lite-agent-end" ref={end} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="lite-composer-shell">
|
||||
<form className="lite-composer" onSubmit={event => { event.preventDefault(); void submit(); }}>
|
||||
<label className="lite-visually-hidden" htmlFor="lite-agent-message">Message Sybil Agent</label>
|
||||
<textarea id="lite-agent-message" rows={1} maxLength={40000} value={draft} disabled={sending} placeholder="Message Sybil Agent…"
|
||||
onInput={event => updateDraft(event.currentTarget.value)}
|
||||
onKeyDown={event => { if (event.key === "Enter" && !event.shiftKey && !event.isComposing) { event.preventDefault(); void submit(); } }} />
|
||||
{uploads.length ? <div className="lite-attachments">
|
||||
<p>Attachments saved with your draft</p>
|
||||
<ul>{uploads.map(file => <li key={file.uploadId}>
|
||||
{file.filename} — {file.status === "ready" ? "Ready to send" : "Open the full interface to resume this upload"}{" "}
|
||||
<button type="button" disabled={sending} onClick={() => {
|
||||
try { room.uploadQueue.remove(file.uploadId); setSendError(null); }
|
||||
catch (error) { setSendError(error instanceof Error ? error.message : String(error)); }
|
||||
}}>Remove</button>
|
||||
</li>)}</ul>
|
||||
</div> : null}
|
||||
<div className="lite-composer-footer">
|
||||
<button className="lite-primary lite-send" type="submit" aria-label="Send message" disabled={sending || room.isLoading || pendingUploads || (!draft.trim() && !uploads.length)}>
|
||||
<SendHorizontal size={19} aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
{sendError ? <p className="lite-error" role="alert">{sendError}</p> : null}
|
||||
</form>
|
||||
</div>
|
||||
</section>;
|
||||
}
|
||||
Reference in New Issue
Block a user