import {localeTag} from '../lib/i18n'; import { tr } from '../lib/i18n'; import { UsageDashboard } from '../components/UsageDashboard'; import { FloatingMenu } from '../components/FloatingMenu'; import { lazyScreen } from '../lib/lazy-screen'; import { measureFrame } from '../lib/overlay-place'; import { timeSkipPrompt, timeSkipTabId, type TimeSkipRun } from '../lib/time-skip'; import { ChatToolbar } from '../components/ChatToolbar'; import { ChatTelemetry } from '../components/TelemetryPopover'; import { ArtifactCard } from '../components/ArtifactCard'; import { ChatMedia } from '../components/ChatMedia'; import { closePreviewTab } from '../lib/preview-tabs'; import { endPreviewDeferral, previewsDeferred } from '../lib/recovery'; import { messageArtifacts, openItem, syncArtifacts } from '../lib/preview-merge'; import { createImeGuard } from '../lib/ime-enter'; import { mainCodePreview, setSelectedPreviewId } from '../lib/code-preview'; import { ArtifactPanel } from '../components/ArtifactPanel'; import { PreviewResizeHandle } from '../components/PreviewResizeHandle'; import { DEFAULT_PREVIEW_FRACTION, PREVIEW_SPLIT_KEY, previewWidth } from '../lib/preview-layout'; import { ComposerControls } from '../components/ComposerControls'; import { ChatPhotos } from '../components/ChatPhotos'; import { useChatPhotos, removeChatPhoto } from '../lib/chat-photos'; import { useNative } from '../lib/native'; import { COMPACT_FILES_FROM, mainResult, previewOf } from '../lib/deliverables'; import { noModelNotice } from '../lib/no-model'; import { ReasoningBlock } from '../components/ReasoningBlock'; import { StatusLine } from '../components/StatusLine'; import { GoalBar } from '../components/GoalBar'; import { PlanCard, PlanPanel, QueuedMessages } from '../components/PlanCard'; import { FolderChip } from '../components/FolderChip'; import { DropZone, DROP_EVENT, pastedFiles } from '../components/DropZone'; import { ActivityLine, DraftLines, EditedFiles, FileCard, FilesSummary, SkillsUsed } from '../components/Deliverables'; import { queueMessage, usePlan, usePlanQuiet } from '../lib/plan'; import { plainFailureText } from '../lib/plain-errors'; import { contextRecovery } from '../lib/context-recovery'; import { useLiveStatus } from '../lib/live-status'; import { ApprovalDock, SCOPE } from '../components/ApprovalCard'; import { artifactFromUrl, type PreviewArtifact } from '../lib/artifacts'; import { openExternal } from '../lib/open-external'; import { busyPlan, parkedReady } from '../lib/while-busy'; import { composeQuoted, parseQuoted, type Quote } from '../lib/quotes'; /** Chat — talk to the one loaded model. */ import { memo, Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Markdown } from '../components/Markdown'; import { Btn, Icon, IconBtn, ErrorNotice, type IconName } from '../components/ui'; import { composerCap, scrollBehavior } from '../lib/responsive'; import { relTime, cx, duration, tokens, tps } from '../lib/format'; import { addQuote, clearQuotes, removeQuote, createSubagentExample, parkMessage, takeParked, pushToast, setPreviewTabClosed, branchFrom, continueLast, markRead, retryLast, selectSession, rewindTo, sendMessage, stopStreaming, useStore, } from '../lib/store'; import { CopyBtn, CopyMenuItem, MenuCloseContext } from '../components/CopyBtn'; import { KEYS_UP, PILL_DISTANCE, distanceFromBottom, followAfterScroll, wheelLeavesBottom } from '../lib/follow-scroll'; import { getSlashCommands } from '../lib/commands'; import { useLocation } from '../lib/router'; import { composerSends } from '../lib/shortcut-bindings'; import type { ChatMessage, Session, ToolCall } from '../lib/types'; // The Time-skip panel and its progress card are only drawn in a chat that has a Time-skip run: their code loads then (and a few seconds after start, see main.tsx). const TimeSkipPanel = lazyScreen(() => import('../components/TimeSkipPanel'), (m) => m.TimeSkipPanel); const TimeSkipWorking = lazyScreen(() => import('../components/TimeSkipWorking'), (m) => m.TimeSkipWorking); export function ChatScreen() { const location = useLocation(); const sessions = useStore((s) => s.sessions); const activeId = useStore((s) => s.activeSession); const library = useStore((s) => s.library); const streaming = useStore((s) => s.streaming); const whileBusy = useStore(s => s.whileBusy); const setup = useStore(s => s.setup); const setupFlag = useStore(s => s.setupOpen); const setupVisible = location.route === 'setup' || setupFlag || !!setup && !setup.complete && !setup.dismissed; const session: Session | null = useMemo(() => { const wanted = location.param ?? activeId; return sessions.find((s) => s.id === wanted) ?? sessions[0] ?? null; }, [sessions, activeId, location.param]); /* A conversation you are watching is read by definition — including while a reply streams into it. Without this, switching away from a session that answered you would mark it unread. */ const messageCount = session?.messages.length ?? 0; useEffect(() => { if (session && session.id === activeId && !streaming) markRead(session.id); // eslint-disable-next-line react-hooks/exhaustive-deps }, [session?.id, messageCount, streaming]); const parked = useStore(s => (session ? s.parked[session.id] : undefined)); const quotes = useStore(s => (session ? s.quotes[session.id] : undefined)) ?? NO_QUOTES; // Selecting words in a message offers to quote just them (#4785). const [selection, setSelection] = useState<{ text: string; messageId: string; role: 'user' | 'assistant'; x: number; y: number } | null>(null); useEffect(() => { const read = () => { const sel = window.getSelection(); const el = scroller.current; if (!sel || sel.isCollapsed || !sel.rangeCount || !el) { setSelection(null); return; } const range = sel.getRangeAt(0); const node = range.commonAncestorContainer; const host = (node instanceof Element ? node : node.parentElement)?.closest('.st-msg'); const text = sel.toString().trim(); if (!host || !el.contains(host) || text.length < 2) { setSelection(null); return; } const rect = range.getBoundingClientRect(); // screen units of this engine, written back in the page's own pixels (lib/overlay-place.ts: Interface size zooms the page) const { scale, viewport } = measureFrame(); setSelection({ text, messageId: host.getAttribute('data-message-id') ?? '', role: host.classList.contains('st-msg--user') ? 'user' : 'assistant', x: Math.min(viewport.width - 96 * scale, Math.max(8, rect.left)) / scale, y: Math.max(8, rect.top - 40 * scale) / scale }); }; document.addEventListener('selectionchange', read); return () => document.removeEventListener('selectionchange', read); }, [session?.id]); // Messages parked while an answer was written (Send after, Interrupt) go one by one, each when the chat is quiet again, // and only from the chat that is also the active one: sendMessage sends to that one (#5806, parkedReady). useEffect(() => { if (!session || !parkedReady(session.id, activeId, streaming, parked?.length ?? 0)) return; const next = takeParked(session.id); if (next !== undefined) void sendMessage(next); }, [streaming, parked?.length, session?.id, activeId]); const [draft, setDraft] = useState(''); const draftKey = `strata.chat-draft.${session?.id ?? 'new'}`; useEffect(() => { setDraft(sessionStorage.getItem(draftKey) ?? ''); if (session && session.id !== activeId) selectSession(session.id); }, [draftKey]); const updateDraft = (value: string) => { setDraft(value); sessionStorage.setItem(draftKey, value); }; const splitRef = useRef(null); const [splitWidth, setSplitWidth] = useState(0); const [previewFraction, setPreviewFraction] = useState(() => { try { const saved = localStorage.getItem(PREVIEW_SPLIT_KEY); return saved && Number.isFinite(Number(saved)) ? Number(saved) : DEFAULT_PREVIEW_FRACTION; } catch { return DEFAULT_PREVIEW_FRACTION; } }); useEffect(() => { const timer = setTimeout(() => { try { localStorage.setItem(PREVIEW_SPLIT_KEY, String(previewFraction)); } catch { } }, 180); return () => clearTimeout(timer); }, [previewFraction]); useEffect(() => { if (!splitRef.current) return; const ro = new ResizeObserver(entries => setSplitWidth(entries[0].contentRect.width)); ro.observe(splitRef.current); return () => ro.disconnect(); }, [session?.id]); const [expanded, setExpanded] = useState(false); useEffect(() => { setExpanded(false); }, [session?.id]); useEffect(() => { if (!expanded) return; const exit = (e: KeyboardEvent) => { if (e.key === 'Escape') setExpanded(false); }; document.addEventListener('keydown', exit); return () => document.removeEventListener('keydown', exit); }, [expanded]); const [previews, setPreviews] = useState([]); const [selectedPreview, setSelectedPreview] = useState(null); const timeSkipRuns = (session?.timeSkips ?? []).filter(run => session?.messages.some(m => m.timeSkipId === run.id && m.role === 'assistant') && (!run.job || (run.job.files?.length ?? 0) > 0)); const runTabs = timeSkipRuns.map((run, i) => ({ id: timeSkipTabId(run.id), title: timeSkipRuns.length > 1 ? `Time-skip ${i + 1}` : 'Time-skip', icon: 'branch' as const, controls: 'time-skip-content' })).filter(tab => !session?.closedPreviews?.includes(tab.id)); const plan = usePlan(session?.id); const planTabId = session ? `plan:${session.id}` : ''; const planTab = plan && plan.state !== 'rejected' && !session?.closedPreviews?.includes(planTabId) ? [{ id: planTabId, title: 'Plan', icon: 'check' as const, controls: 'plan-content' }] : []; const planShown = planTab.length > 0 && selectedPreview === planTabId; const planRunId = plan?.state === 'running' ? plan.id : ''; useEffect(() => { if (planRunId && planTabId && !session?.closedPreviews?.includes(planTabId)) setSelectedPreview(planTabId); }, [planRunId]); // The chat shows every run of this chat (a running one has no files yet, so no tab); the sidebar tabs only those with something to compare. const allTimeSkipRuns = session?.timeSkips ?? []; const extraTabs = [...runTabs, ...planTab]; const selectedRun = timeSkipRuns.find(run => timeSkipTabId(run.id) === selectedPreview); const inlinePreview = splitWidth >= (selectedRun || planShown ? 760 : 1120); const paneWidth = previewWidth(splitWidth, previewFraction); const splitStyle = inlinePreview && selectedPreview && !expanded ? { gridTemplateColumns: `minmax(0,1fr) ${paneWidth}px` } : undefined; const resizeHandle = !setupVisible && inlinePreview && selectedPreview && !expanded ? : null; const runCount = timeSkipRuns.length; const previewCount = session?.messages.reduce((n, m) => n + (m.artifacts?.length ?? 0), 0) ?? 0; useEffect(() => { const items = [...new Map((session?.messages.flatMap(m => m.artifacts ?? []) ?? []).map(a => [a.id, a])).values()]; setPreviews(items.filter(a => !session?.closedPreviews?.includes(a.id))); setSelectedPreview(session && previewsDeferred(session.id) || session?.closedPreviews?.includes(session.previewOpen ?? '') ? null : session?.previewOpen?.startsWith('time-skip:') && !timeSkipRuns.some(r => timeSkipTabId(r.id) === session.previewOpen) ? null : session?.previewOpen ?? null); }, [session?.id]); useEffect(() => { // A message's copy of a file never replaces one that was read later (lib/preview-merge.ts); nothing changed = the same array = no update. const items = messageArtifacts(session?.messages); if (items.length) setPreviews(old => syncArtifacts(old, items, session?.closedPreviews)); }, [previewCount]); const localUrls = useRef([]); useEffect(() => { const id = session?.previewOpen; if (id?.startsWith('time-skip:') && timeSkipRuns.some(r => timeSkipTabId(r.id) === id) && !session?.closedPreviews?.includes(id) && !(session && previewsDeferred(session.id))) setSelectedPreview(id); }, [session?.id, runCount]); useEffect(() => () => localUrls.current.forEach(url => URL.revokeObjectURL(url)), []); function openPreview(item: PreviewArtifact) { endPreviewDeferral(); if (item.url?.startsWith('blob:')) localUrls.current.push(item.url); setPreviewTabClosed(session?.id, item.id, false, item.id); // The file was read just now: it replaces the tab of the same id when its content differs (it used to be dropped, keeping the first copy). setPreviews(old => openItem(old, item)); setSelectedPreview(item.id); } /* A run that finishes with a file the sidebar can show opens its main result there by itself (one file, never several). Only a run seen finishing here does it: opening an old chat shows nothing new. A file that cannot be read just stays a card with Open. */ const replyWasStreaming = useRef>(new Map()); const lastReply = [...(session?.messages ?? [])].reverse().find(m => m.role === 'assistant'); useEffect(() => { if (!session || !lastReply) return; const before = replyWasStreaming.current.get(lastReply.id); replyWasStreaming.current.set(lastReply.id, !!lastReply.streaming); if (before !== true || lastReply.streaming || lastReply.error) return; const main = mainResult(lastReply.deliverables, lastReply.content); // No file to open: one whole HTML page in the answer goes to the sidebar too (a snippet keeps its preview button). if (!main) { const page = lastReply.blocks?.some(b => b.kind === 'group') ? null : mainCodePreview(lastReply.id, lastReply.content); if (page) openPreview(page); return; } const sessionId = session.id; void previewOf(sessionId, main).then(item => { if (session.id === sessionId) openPreview(item); }, () => { }); }, [lastReply?.id, lastReply?.streaming]); useEffect(() => { setSelectedPreviewId(setupVisible ? null : selectedPreview); return () => setSelectedPreviewId(null); }, [selectedPreview, setupVisible]); function closeTab(id: string) { const next = closePreviewTab([...previews.map(p => ({ id: p.id })), ...extraTabs], selectedPreview, id); setPreviews(previews.filter(p => p.id !== id)); setSelectedPreview(next.selected); setPreviewTabClosed(session?.id, id, true, next.selected); if (!next.items.length) { setExpanded(false); requestAnimationFrame(() => textarea.current?.focus()); } } function selectPreview(id: string) { endPreviewDeferral(); setSelectedPreview(id); setPreviewTabClosed(session?.id, id, false, id); } function openTimeSkip(id: string) { selectPreview(timeSkipTabId(id)); } function openLink(url: string) { const item = artifactFromUrl(url); if (!item) return; // A web address opens in the person's own browser; only a file (a PDF, picture, table, code or Markdown) is shown in the side panel (#4721). if (item.kind === 'website') { void openExternal(url); return; } openPreview(item); } /* The message list is memoised (#4640 family: Out of Memory in a long chat): a streamed token changes one message, so the others must not re-render. These handlers keep one identity for the life of the screen and always call the newest functions above. */ const latestHandlers = useRef({ editPrompt, openPreview, closeTab, openTimeSkip, openLink }); latestHandlers.current = { editPrompt, openPreview, closeTab, openTimeSkip, openLink }; const messageHandlers = useMemo(() => ({ onEditPrompt: (text: string) => latestHandlers.current.editPrompt(text), onPreview: (a: PreviewArtifact) => latestHandlers.current.openPreview(a), onClosePreview: (id: string) => latestHandlers.current.closeTab(id), onOpenTimeSkip: (id: string) => latestHandlers.current.openTimeSkip(id), onOpenLink: (url: string) => latestHandlers.current.openLink(url), }), []); const scroller = useRef(null); const focusMode = useStore((s) => s.focus); /** Per-conversation scroll offsets, so returning to one restores the view. */ const scrollPositions = useRef>(new Map()); /* Scrolling back to reread something is normal; being stranded there while a reply arrives is not. The pill only appears once you are far enough up that the newest message is genuinely off screen. */ const [awayFromBottom, setAwayFromBottom] = useState(false); /* A reply that finishes while you are reading further up has no way to say so — the text simply stops arriving off screen. The pill pulses once, on opacity alone, so the cue is noticed without anything moving. */ const [justFinished, setJustFinished] = useState(false); const wasStreaming = useRef(false); useEffect(() => { if (wasStreaming.current && !streaming) { setJustFinished(true); const t = setTimeout(() => setJustFinished(false), 700); wasStreaming.current = streaming; return () => clearTimeout(t); } wasStreaming.current = streaming; return undefined; }, [streaming]); /** Whether the reader is following the newest message rather than reading back. */ const pinned = useRef(true); /** When the reader last used the wheel, a finger, a key or the scrollbar on the transcript. */ const readerInput = useRef(0); const seenMessages = useRef(0); /** * Scrolls to the newest message and stays there. * * `content-visibility: auto` means the transcript's height is an estimate * until each turn renders, so a single `scrollTo(scrollHeight)` lands short * and the reader ends up above the reply they asked to see. Re-aiming until * the height stops growing settles it; the loop is bounded because a * transcript that never settles must not spin. */ const scrollToLatest = useCallback((smooth = true) => { const el = scroller.current; if (!el) return; pinned.current = true; el.scrollTo({ top: el.scrollHeight, behavior: smooth ? scrollBehavior() : 'auto' }); setAwayFromBottom(false); }, []); /** * Keeps a pinned transcript pinned. * * With `content-visibility: auto`, an off-screen turn has an *estimated* * height until it renders, so the scroll height grows as you approach the * bottom and any single scroll lands short. Watching the content box and * re-aiming while the reader is pinned is what makes the two compatible — * and it fixes the same drift while a long reply streams in. */ useEffect(() => { const el = scroller.current; // The content, not the scroller's first child — which is the reading // progress hairline, and never changes size. const inner = el?.querySelector('.chat__inner'); if (!el || !inner) return undefined; const ro = new ResizeObserver(() => { if (pinned.current && readerHasSelection(el)) { // Words are selected: the view stays where they are (#3818); the way back to the newest message is offered. pinned.current = false; setAwayFromBottom(distanceFromBottom(el) > PILL_DISTANCE); } if (pinned.current) el.scrollTop = el.scrollHeight; }); ro.observe(inner); // The scroller itself shrinks when a question appears above the message box (the approval dock): keep the end in view then too. ro.observe(el); return () => ro.disconnect(); }, [session?.id, session?.messages.length === 0]); const textarea = useRef(null); const remote = useStore(s => s.remoteModel), providers = useStore(s => s.providers); const apiModel = providers?.connections.find(c => c.id === remote?.connectionId)?.models.find(m => m.id === remote?.modelId); const loaded = apiModel ?? library?.variants.find((v) => v.id === library.loaded) ?? null; /** * Follow the stream only when the reader is already at the bottom. Scrolling * back through a long conversation should not be yanked forward by the next * token — which is the single most irritating thing a chat UI can do over a * long session. */ useEffect(() => { const el = scroller.current; if (!el || (session?.showcase && !streaming)) return; // The reader's own scrolling decides (#4014): a wheel notch is about 100 px and used to land inside a 120 px band that pulled // the view back on every token. `pinned` is set by the scroller's wheel, touch, key and scroll handlers below. const count = session?.messages.length ?? 0; const sent = count > seenMessages.current && session?.messages.at(-1)?.role === 'user'; seenMessages.current = count; // Sending a message always brings the reader to it, as it did before; only the answer's growth waits for the reader. if (pinned.current && !sent && readerHasSelection(el)) { pinned.current = false; setAwayFromBottom(distanceFromBottom(el) > PILL_DISTANCE); } if (pinned.current || sent) scrollToLatest(); }, [session?.messages.length, session?.messages.at(-1)?.content, scrollToLatest]); // Scroll position is remembered per conversation, so returning to one puts you // back where you were reading rather than at the top or the bottom. useEffect(() => { const el = scroller.current; if (!el || !session) return; const saved = scrollPositions.current.get(session.id); el.scrollTop = saved ?? (session.showcase ? 0 : el.scrollHeight); pinned.current = !session.showcase || saved !== undefined; // Deliberately keyed on the conversation only: re-running on every message // would fight the follow-the-stream effect above. // eslint-disable-next-line react-hooks/exhaustive-deps }, [session?.id]); // The box grows with the draft but never past 30% of the window, and scrolls inside after that, so Send and the toolbar stay on screen (QA F08). useEffect(() => { const fit = () => { const el = textarea.current; if (!el) return; el.style.height = 'auto'; el.style.height = `${Math.min(composerCap(window.innerHeight), el.scrollHeight)}px`; }; fit(); window.addEventListener('resize', fit); return () => window.removeEventListener('resize', fit); }, [draft]); /** Hands a rewound prompt back to the composer so it can be changed. */ function editPrompt(text: string) { setDraft(text); requestAnimationFrame(() => textarea.current?.focus()); } /** * Turn navigation, for reading rather than writing. * * Focus mode is for reading a long conversation, and reading needs a next. * `Alt+↑`/`Alt+↓` step between your own turns — the boundaries a reader * actually navigates by, rather than by message — and `Cmd+↓` returns to the * latest. Only bound while focus mode is on, so the keys stay free elsewhere. */ useEffect(() => { if (!focusMode) return undefined; const onKey = (e: KeyboardEvent) => { const el = scroller.current; if (!el) return; const atLatest = () => { pinned.current = true; el.scrollTo({ top: el.scrollHeight, behavior: scrollBehavior() }); }; if ((e.metaKey || e.ctrlKey) && e.key === 'ArrowDown') { e.preventDefault(); atLatest(); return; } if (!e.altKey || (e.key !== 'ArrowUp' && e.key !== 'ArrowDown')) return; e.preventDefault(); const turns = [...el.querySelectorAll('.st-msg--user')]; if (!turns.length) return; const top = el.scrollTop; /* The tolerance has to exceed TURN_OFFSET, or the key sticks on the turn it just moved to: positioning a turn at `scrollTop + 16` leaves it satisfying `offsetTop > scrollTop + 8` on the next press. */ const TURN_OFFSET = 16; const seen = TURN_OFFSET + 24; const next = e.key === 'ArrowDown' ? turns.find((t) => t.offsetTop > top + seen) : [...turns].reverse().find((t) => t.offsetTop < top - seen); if (next) { pinned.current = false; el.scrollTo({ top: Math.max(0, next.offsetTop - TURN_OFFSET), behavior: scrollBehavior() }); setAwayFromBottom(true); } else if (e.key === 'ArrowDown') { atLatest(); } }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, [focusMode]); const chatNative = useNative(), queuedPhotos = useChatPhotos(session?.id); const readyForChat = !!loaded || !!chatNative?.instances.some(i => i.status === 'ready'); useEffect(() => { if (setupVisible || !readyForChat || messageCount) return; if (scroller.current?.querySelector('.first-chat-start')) textarea.current?.focus({ preventScroll: true }); }, [setupVisible, readyForChat, session?.id, messageCount]); function submit() { const text = draft.trim(); if (!text && !queuedPhotos.length) return; if (streaming) { // Typed while a goal runs: it waits in the queue, shown under the conversation. With no goal running there is nothing to queue behind. if (!text || !session) return; const plan = busyPlan(whileBusy); const out = quotedMessage(quotes, text); // Interrupt: the answer stops where it is (what is written stays) and the message goes as soon as it has stopped (the effect below sends it). if (plan === 'interrupt') { updateDraft(''); clearQuotes(session.id); parkMessage(session.id, out); stopStreaming(); return; } updateDraft(''); void queueMessage(session.id, out).then(ok => { if (ok) { clearQuotes(session.id); return; } if (!ok) { if (plan === 'goal') { clearQuotes(session.id); parkMessage(session.id, out); return; } updateDraft(text); pushToast('info', tr('The assistant is still answering'), tr('Send your message when it has finished.')); } }, e => { updateDraft(text); pushToast('error', tr('The message was not queued'), plainFailureText(e instanceof Error ? e.message : String(e)).message); }); return; } const timeSkip = timeSkipPrompt(text); if (timeSkip === '') { pushToast('info', 'Add a prompt after /time-skip'); return; } if (timeSkip === null) { // No model to answer: say so plainly (or show the download), and do not start a run. const why = noModelNotice(chatNative ?? null, { libraryLoaded: !!loaded }); if (why) { pushToast('info', tr(why.title), tr(why.message)); return; } } updateDraft(''); if (session) clearQuotes(session.id); void sendMessage(session ? quotedMessage(quotes, text) : text); } function onComposerKey(_e: React.KeyboardEvent) { } if (!session) { return (<> { setSelectedPreview(null); setExpanded(false); }} inline={inlinePreview} expanded={expanded} onExpand={() => setExpanded(v => !v)}/>
{tr(awayFromBottom ? () : null)}
setSelectedPreview(null)} inline={inlinePreview}/> {tr(resizeHandle)}
); } return (<> {tr(selection && session ? : null)} { setSelectedPreview(null); setExpanded(false); }} inline={inlinePreview} expanded={expanded} onExpand={() => setExpanded(v => !v)}/> {/* The composer belongs to the conversation column, not the window: with the context panel open, a full-width composer sat underneath the panel and drifted out of alignment with the transcript it belongs to. */}
{ const el = e.currentTarget; if (session) scrollPositions.current.set(session.id, el.scrollTop); // How far you are from the newest message, which decides whether // the way back is worth offering. // The threshold has to tolerate the height estimate moving under // us, or the pill flickers as turns render during a scroll. const distance = distanceFromBottom(el); pinned.current = followAfterScroll(distance, pinned.current, Date.now() - readerInput.current); setAwayFromBottom(distance > PILL_DISTANCE); }} onWheel={(e) => { readerInput.current = Date.now(); if (wheelLeavesBottom(e.deltaY) && e.currentTarget.scrollTop > 0) pinned.current = false; }} onTouchMove={() => { readerInput.current = Date.now(); }} onPointerDown={() => { readerInput.current = Date.now(); }} onKeyDown={(e) => { readerInput.current = Date.now(); if (KEYS_UP.has(e.key) && e.target === e.currentTarget && e.currentTarget.scrollTop > 0) pinned.current = false; }}>
{tr(session.messages.length === 0 ? (session.statsAvailable === false ?

{tr("Editing from an earlier message. Update the prompt below.")}

: ) : (session.messages.map((m, i) => ( r.id === m.timeSkipId)?.completedAt} timeSkipReal={!!allTimeSkipRuns.find(r => r.id === m.timeSkipId)?.job} timeSkipRun={allTimeSkipRuns.find(r => r.id === m.timeSkipId)} onOpenLink={messageHandlers.onOpenLink}/>))))}
{tr(awayFromBottom ? () : null)}
{tr(!setupVisible && selectedRun && session ? { setSelectedPreview(null); setExpanded(false); }}/> : null)} {tr(!setupVisible && planShown ? : null)} setSelectedPreview(null)} inline={inlinePreview}/> {tr(resizeHandle)}
); } interface SlashCommand { id: string; label: string; hint: string; icon: IconName; run: () => void; } function useSlashCommands(clear: () => void, setDraft: (value: string) => void): SlashCommand[] { const demo = useStore(s => s.library?.mode === 'simulated'); return [ { id: 'time-skip', label: '/time-skip', hint: 'Try a change on a copy, check it, and keep it only if it is better', icon: 'branch', run: () => setDraft('/time-skip ') }, { id: 'goal', label: '/goal', hint: 'Set a goal and let the assistant keep working until it is done', icon: 'bolt', run: () => setDraft('/goal ') }, { id: 'plan', label: '/plan', hint: 'Have the assistant write a plan first; nothing runs until you approve it', icon: 'check', run: () => setDraft('/plan ') }, { id: 'research', label: '/research', hint: 'Search the web, read the pages and answer with sources', icon: 'search', run: () => setDraft('/research ') }, { id: 'loop', label: '/loop', hint: 'Repeat a message every few minutes while this chat is open', icon: 'refresh', run: () => setDraft('/loop 10m ') }, { id: 'website', label: '/website', hint: 'Open a website in the preview sidebar', icon: 'link', run: () => { clear(); window.dispatchEvent(new Event('strata:open-website')); } }, { id: 'preview', label: '/preview', hint: 'Open a local file in the preview sidebar', icon: 'file', run: () => { clear(); window.dispatchEvent(new Event('strata:open-preview')); } }, ...(demo ? [{ id: 'subagents', label: '/subagents', hint: 'Preview subagent statistics', icon: 'experts' as const, run: () => { clear(); createSubagentExample(); } }] : []), ...getSlashCommands().map(command => ({ ...command, run: () => { clear(); command.run(); } })) ]; } function SlashMenu({ query, commands, cursor, onPick, onHover, }: { query: string; commands: SlashCommand[]; cursor: number; onPick: (c: SlashCommand) => void; onHover: (i: number) => void; }) { const matches = commands.filter((c) => c.label.slice(1).startsWith(query.toLowerCase())); if (!matches.length) { return (
{tr("No command matches \u201C/")}{query}”
); } return (
{tr("Commands")}
{tr(matches.map((c, i) => ()))}
); } const NO_PARKED: string[] = []; /** Whether the reader has words selected inside the transcript: a view that moves under the pointer loses the selection (#3818). */ function readerHasSelection(scroller: HTMLElement): boolean { const sel = window.getSelection(); return !!sel && !sel.isCollapsed && !!sel.anchorNode && scroller.contains(sel.anchorNode) && sel.toString().trim().length > 0; } const NO_QUOTES: Quote[] = []; /** The message that goes out: the quoted passages in their fixed form, then the person's words. A command such as /plan goes out as typed. */ function quotedMessage(quotes: readonly Quote[], text: string): string { return quotes.length && !text.startsWith('/') ? composeQuoted(quotes.map(q => q.text), text) : text; } function Composer({ draft, setDraft, loaded, streaming, onSubmit, onKey, session, onPreview, textareaRef, }: { draft: string; setDraft: (v: string) => void; loaded: boolean; streaming: boolean; onSubmit: () => void; onKey: (e: React.KeyboardEvent) => void; session: Session | null; onPreview: (a: PreviewArtifact) => void; textareaRef: React.RefObject; }) { const clearDraft = useCallback(() => setDraft(''), [setDraft]); const photos = useChatPhotos(session?.id), native = useNative(); const canChat = loaded || native?.instances.some(i => i.status === 'ready'); const whileBusy = useStore(s => s.whileBusy), parkedList = useStore(s => (session ? s.parked[session.id] : undefined)) ?? NO_PARKED; const quoteList = useStore(s => (session ? s.quotes[session.id] : undefined)) ?? NO_QUOTES; const commands = useSlashCommands(clearDraft, setDraft); const [cursor, setCursor] = useState(0); const [fileError, setFileError] = useState(''); const [slashOpen, setSlashOpen] = useState(false); const [slashDismissed, setSlashDismissed] = useState(false); const slashHostRef = useRef(null); const ime = useRef(createImeGuard()); const slashQuery = slashDismissed ? null : slashOpen ? '' : draft.startsWith('/') && !/\s/.test(draft) ? draft.slice(1) : null; const matches = slashQuery === null ? [] : commands.filter((c) => c.label.slice(1).startsWith(slashQuery.toLowerCase())); useEffect(() => { setSlashDismissed(false); }, [draft, session?.id]); useEffect(() => { if (slashQuery === null) return; const outside = (event: PointerEvent) => { if (slashHostRef.current?.contains(event.target as Node) || event.target === textareaRef.current) return; setSlashOpen(false); setSlashDismissed(true); }; document.addEventListener('pointerdown', outside); return () => document.removeEventListener('pointerdown', outside); }, [slashQuery, textareaRef]); useEffect(() => { setCursor(0); }, [slashQuery]); return (
{tr(fileError ? setFileError('')}/> : null)} {tr(quoteList.length ?
{tr(quoteList.map(q =>
removeQuote(session!.id, q.id)}/>
))}
: null)} {tr(parkedList.length ?
{tr(parkedList.map((text, i) =>
{tr("Waiting to send")}{text} { const back = takeParked(session!.id, i); if (back !== undefined && !draft.trim()) setDraft(back); }}/>
))}
: null)}
{tr(photos.length ? removeChatPhoto(id, session?.id)}/> : null)}