diff --git a/apps/web/src/widgets/session/backend.ts b/apps/web/src/widgets/session/backend.ts index 393ced63..e77bde56 100644 --- a/apps/web/src/widgets/session/backend.ts +++ b/apps/web/src/widgets/session/backend.ts @@ -181,10 +181,11 @@ export const session = defineBackend({ const notice = Option.getOrUndefined(decodeChanged(event.payload)); if (notice === undefined) return; const detail = notice.threadId === followed.threadId; - // Messages first, so a promoted prompt is in the conversation before it leaves the queue. + // Messages first, so a promoted prompt is in the conversation before it leaves the queue. A + // draft is a checkpoint the next one replaces, so only settled messages are stored. if (detail && notice.entry.length > 0) for (const message of yield* sync.entries(notice.entry)) - yield* publish(key, { _tag: "Message", message }); + yield* publish(key, { _tag: "Message", message }, message.state === "draft"); if (detail || notice.thread.length > 0 || notice.removed.length > 0) yield* refresh(key); }); diff --git a/apps/web/src/widgets/session/highlight.tsx b/apps/web/src/widgets/session/highlight.tsx index 52654487..37cccd68 100644 --- a/apps/web/src/widgets/session/highlight.tsx +++ b/apps/web/src/widgets/session/highlight.tsx @@ -1,7 +1,7 @@ import { toJsxRuntime } from "hast-util-to-jsx-runtime"; -import { useEffect, useReducer } from "react"; +import { memo, useEffect, useReducer, useState } from "react"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; -import type { BundledLanguage, BundledTheme, Highlighter } from "shiki"; +import type { BundledLanguage, BundledTheme, CodeToHastOptions, Highlighter } from "shiki"; import type { ThemeId } from "../../theme"; @@ -12,6 +12,8 @@ const SHIKI_THEMES: Record = { }; type Root = ReturnType; +/** One `span.line` of Shiki's hast tree. */ +type Line = Extract; /** * A fence's language, lower-cased; whether Shiki has a grammar for it is known once Shiki loads. @@ -62,13 +64,76 @@ function ready(lang: BundledLanguage, theme: BundledTheme) { return hasLang && hasTheme; } +/** The `span.line` elements of Shiki's `pre > code`. */ +function linesOf(root: Root): Line[] { + const pre = root.children[0]; + const code = pre?.type === "element" ? pre.children[0] : undefined; + return code?.type === "element" ? code.children.filter((node) => node.type === "element") : []; +} + +// Finished code, highlighted once: lists remount their rows on scroll and thread switches remount them all. +const CACHE_SIZE = 500; +const cache = new Map(); + +function highlight(highlighter: Highlighter, code: string, lang: BundledLanguage, theme: BundledTheme) { + const key = `${theme}|${lang}|${code}`; + const hit = cache.get(key); + if (hit !== undefined) { + cache.delete(key); + cache.set(key, hit); + return hit; + } + const lines = linesOf(highlighter.codeToHast(code, { lang, theme })); + cache.set(key, lines); + if (cache.size > CACHE_SIZE) cache.delete(cache.keys().next().value!); + return lines; +} + +/** + * A block's highlighter for code that grows by appending: completed lines keep their nodes, and + * only new lines and the open last line are tokenized, resuming from the grammar state where the + * completed lines end. Shiki tokenizes line by line carrying that state, so the lines match a full + * highlight of the code. It starts over when the language or theme changes or the code is rewritten. + */ +function incremental() { + let key = ""; + let done = ""; + let lines: Line[] = []; + let options: CodeToHastOptions | undefined; + return (highlighter: Highlighter, code: string, lang: BundledLanguage, theme: BundledTheme) => { + if (options === undefined || key !== `${theme}|${lang}` || !code.startsWith(done)) { + key = `${theme}|${lang}`; + done = ""; + lines = []; + options = { lang, theme }; + } + const end = code.lastIndexOf("\n") + 1; + if (end > done.length) { + const root = highlighter.codeToHast(code.slice(done.length, end - 1), options); + const grammarState = highlighter.getLastGrammarState(root); + lines = [...lines, ...linesOf(root)]; + if (grammarState !== undefined) options = { lang, theme, grammarState }; + done = code.slice(0, end); + } + return [...lines, ...linesOf(highlighter.codeToHast(code.slice(end), options))]; + }; +} + /** - * `code` highlighted for the palette, as Shiki's hast tree, or undefined until its grammar and + * `code` highlighted for the palette, as Shiki's line elements, or undefined until its grammar and * theme have loaded (the caller shows the plain text meanwhile). Synchronous once loaded, so a * streaming block re-highlights in the same render as its text changes, with nothing to flash. + * Finished code is `cached`; code still streaming grows incrementally instead, as caching would + * only fill the cache with its prefixes. */ -export function useHighlight(code: string, lang: BundledLanguage | undefined, theme: ThemeId): Root | undefined { +export function useHighlight( + code: string, + lang: BundledLanguage | undefined, + theme: ThemeId, + cached: boolean, +): readonly Line[] | undefined { const [, rerender] = useReducer((n: number) => n + 1, 0); + const [grow] = useState(incremental); useEffect(() => { listeners.add(rerender); return () => { @@ -76,15 +141,33 @@ export function useHighlight(code: string, lang: BundledLanguage | undefined, th }; }, []); const shikiTheme = SHIKI_THEMES[theme]; - const available = lang !== undefined && ready(lang, shikiTheme); - return available && lang !== undefined ? highlighter?.codeToHast(code, { lang, theme: shikiTheme }) : undefined; + if (lang === undefined || !ready(lang, shikiTheme) || highlighter === undefined) return undefined; + // A CR may be half of a CRLF Shiki would split on. + if (cached || code.includes("\r")) { + return cached + ? highlight(highlighter, code, lang, shikiTheme) + : linesOf(highlighter.codeToHast(code, { lang, theme: shikiTheme })); + } + return grow(highlighter, code, lang, shikiTheme); } const runtime = { Fragment, jsx, jsxs }; -/** The highlighted lines alone: Shiki's own `pre` (and its background) is left out. */ -export function HighlightedCode({ root }: { readonly root: Root }) { - const pre = root.children[0]; - const code = pre?.type === "element" ? pre.children[0] : undefined; - return code?.type === "element" ? toJsxRuntime(code, runtime) : toJsxRuntime(root, runtime); -} +/** A completed line keeps its node while the code grows, so it renders once. */ +const CodeLine = memo(function CodeLine({ line }: { readonly line: Line }) { + return toJsxRuntime(line, runtime); +}); + +/** The highlighted lines alone, in Shiki's `code` shape: its `pre` (and background) is left out. */ +export const HighlightedCode = memo(function HighlightedCode({ lines }: { readonly lines: readonly Line[] }) { + return ( + + {lines.map((line, index) => ( + + {index > 0 && "\n"} + + + ))} + + ); +}); diff --git a/apps/web/src/widgets/session/markdown.tsx b/apps/web/src/widgets/session/markdown.tsx index 552d0633..30aa53f1 100644 --- a/apps/web/src/widgets/session/markdown.tsx +++ b/apps/web/src/widgets/session/markdown.tsx @@ -3,11 +3,13 @@ import { useAtomValue } from "@effect/atom-react"; import { type ComponentProps, + createContext, createElement, isValidElement, type JSX, memo, type ReactNode, + use, useEffect, useState, } from "react"; @@ -75,7 +77,7 @@ function CodeBlock(props: ComponentProps<"pre"> & ExtraProps) { const block = fence(props.children); const theme = useAtomValue(themeAtom); const language = languageOf(block?.language); - const highlighted = useHighlight(block?.code ?? "", language, theme); + const highlighted = useHighlight(block?.code ?? "", language, theme, !use(Growing)); if (block === undefined) return
{props.children}
; return (
@@ -84,7 +86,7 @@ function CodeBlock(props: ComponentProps<"pre"> & ExtraProps) {
-				{highlighted === undefined ? {block.code} : }
+				{highlighted === undefined ? {block.code} : }
 			
); @@ -129,25 +131,33 @@ const components: Components = { const plugins = [remarkGfm]; -// One finished block of a streaming reply: it keeps its render while later blocks grow. +/** Whether a block is the one a streaming reply is still writing, so its code changes every delta. */ +const Growing = createContext(false); + +// One block of a reply: it keeps its render while later blocks grow. const Block = memo(function Block({ source, + growing = false, remarkPlugins = plugins, }: { readonly source: string; + readonly growing?: boolean; readonly remarkPlugins?: Options["remarkPlugins"]; }) { return ( - - {source} - + + + {source} + + ); }); /** * A message's markdown, rendered as text: raw HTML in it stays escaped. It re-renders only when its - * text changes. While a reply streams, it renders block by block, so only the growing block is - * parsed again on each delta; text that can't be split but has code fences parses incrementally. + * text changes. It renders block by block, so a streaming reply parses only its growing block again + * on each delta and keeps its blocks when it finishes; streamed text that can't be split but has + * code fences parses incrementally. */ export const Markdown = memo(function Markdown({ text, @@ -159,17 +169,18 @@ export const Markdown = memo(function Markdown({ readonly className?: string; }) { const [incremental] = useState(incrementalMarkdown); - const blocks = streaming ? splitMarkdownBlocks(text) : null; + const blocks = splitMarkdownBlocks(text); const fenced = streaming && /(?:^|\n) {0,3}(?:`{3}|~{3})/.test(text); const remarkPlugins = fenced ? [remarkGfm, incremental] : plugins; return (
- {/* react-markdown renders a Fragment, so the blocks' elements sit in this div as one document's would. - Whole text stays block 0, so a finished reply reuses its first block rather than remounting. */} + {/* react-markdown renders a Fragment, so the blocks' elements sit in this div as one document's would. */} {blocks === null ? ( - + ) : ( - blocks.map((source, index) => ) + blocks.map((source, index) => ( + + )) )}
); diff --git a/apps/web/src/widgets/session/view.tsx b/apps/web/src/widgets/session/view.tsx index 3ac6c842..0df608e6 100644 --- a/apps/web/src/widgets/session/view.tsx +++ b/apps/web/src/widgets/session/view.tsx @@ -4,8 +4,18 @@ import { type HistoryEntry, type LiveDelta, Part, type QueuedPrompt, type ToolCa import { useBus, useEvents, useFrame, useShell, type WidgetProps } from "@codeworksh/widget/view"; import { Option, Schema } from "effect"; import { Atom } from "effect/reactivity"; -import { LegendList, type LegendListRef, type LegendListRenderItemProps } from "@legendapp/list/react"; -import { createContext, memo, type ReactNode, use, useEffect, useLayoutEffect, useRef, useState } from "react"; +import { LegendList, type LegendListRenderItemProps } from "@legendapp/list/react"; +import { + createContext, + type CSSProperties, + memo, + type ReactNode, + use, + useEffect, + useLayoutEffect, + useRef, + useState, +} from "react"; import { Logo } from "@/components/logo"; import { NoticeLine, noticeSurface, type Notice } from "@/components/banner"; @@ -569,19 +579,16 @@ function Conversation({ } : undefined); - const surface = useRef(null); const composer = useRef(null); // The last composer's height, so a conversation opens at its end with the composer already // counted and the list does not scroll again once it is measured. const [inset, setInset] = useState(lastInset); // Before paint, so the first frame is laid out with the composer's height. useLayoutEffect(() => { - const root = surface.current; const element = composer.current; - if (root === null || element === null) return; + if (element === null) return; const sync = () => { const height = element.getBoundingClientRect().height; - root.querySelector(".session-transcript")?.style.setProperty("--composer-height", `${height}px`); lastInset = Math.ceil(height); setInset(lastInset); }; @@ -599,7 +606,7 @@ function Conversation({ if (view.status === "failed") return

{view.message}

; const running = isRunning(view); return ( -
+
); - const transcriptRef = (instance: LegendListRef | null) => { - instance?.getScrollableNode().style.setProperty("--composer-height", `${inset}px`); - return ref(instance); - }; return ( - ref={transcriptRef} + ref={ref} + style={{ "--composer-height": `${inset}px` } as CSSProperties} data={turns} extraData={extraData} keyExtractor={turnKey} @@ -1205,15 +1209,30 @@ function WorkLog({ ); } +/** + * The first paragraph with text, code fences blanked: fences are blanked one at a time and only + * until that paragraph ends, so a long streaming block isn't scanned whole on every delta. + */ +function firstParagraph(text: string) { + const separator = /\n\s*\n/g; + let body = ""; + for (let at = 0; ;) { + const open = text.indexOf("```", at); + body += open === -1 ? text.slice(at) : `${text.slice(at, open)} `; + const start = body.search(/\S/); + separator.lastIndex = Math.max(start, 0); + const end = start === -1 ? undefined : separator.exec(body)?.index; + if (end !== undefined) return body.slice(start, end).trim(); + // An unclosed fence runs to the end. + const close = open === -1 ? -1 : text.indexOf("```", open + 3); + if (close === -1) return body.trim(); + at = close + 3; + } +} + /** First paragraph of a folded block, stripped to one plain line. */ function proseSummary(text: string) { - const body = text.replace(/```[\s\S]*?(?:```|$)/g, " "); - const paragraph = - body - .split(/\n\s*\n/) - .map((part) => part.trim()) - .find(Boolean) ?? ""; - return paragraph + return firstParagraph(text) .replace(/^\s{0,3}(?:#{1,6}|>|[-*+]|\d+\.)\s+/gm, "") .replace(/`([^`]*)`/g, "$1") .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")