Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/web/src/widgets/session/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
107 changes: 95 additions & 12 deletions apps/web/src/widgets/session/highlight.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,6 +12,8 @@ const SHIKI_THEMES: Record<ThemeId, BundledTheme> = {
};

type Root = ReturnType<Highlighter["codeToHast"]>;
/** One `span.line` of Shiki's hast tree. */
type Line = Extract<Root["children"][number], { type: "element" }>;

/**
* A fence's language, lower-cased; whether Shiki has a grammar for it is known once Shiki loads.
Expand Down Expand Up @@ -62,29 +64,110 @@ 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<string, Line[]>();

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<BundledLanguage, BundledTheme> | 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 () => {
listeners.delete(rerender);
};
}, []);
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 (
<code>
{lines.map((line, index) => (
<Fragment key={index}>
{index > 0 && "\n"}
<CodeLine line={line} />
</Fragment>
))}
</code>
);
});
37 changes: 24 additions & 13 deletions apps/web/src/widgets/session/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 <pre className={preClass}>{props.children}</pre>;
return (
<div className="my-4 overflow-hidden rounded-lg border border-ink/10 bg-muted first:mt-0 last:mb-0">
Expand All @@ -84,7 +86,7 @@ function CodeBlock(props: ComponentProps<"pre"> & ExtraProps) {
<CopyButton text={block.code} />
</div>
<pre className={preClass}>
{highlighted === undefined ? <code>{block.code}</code> : <HighlightedCode root={highlighted} />}
{highlighted === undefined ? <code>{block.code}</code> : <HighlightedCode lines={highlighted} />}
</pre>
</div>
);
Expand Down Expand Up @@ -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 (
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
{source}
</ReactMarkdown>
<Growing value={growing}>
<ReactMarkdown remarkPlugins={remarkPlugins} components={components}>
{source}
</ReactMarkdown>
</Growing>
);
});

/**
* 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,
Expand All @@ -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 (
<div className={`agent-markdown min-w-0 ${className ?? ""}`}>
{/* 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 ? (
<Block key={0} source={text} remarkPlugins={remarkPlugins} />
<Block key={0} source={text} growing={streaming} remarkPlugins={remarkPlugins} />
) : (
blocks.map((source, index) => <Block key={index} source={source} />)
blocks.map((source, index) => (
<Block key={index} source={source} growing={streaming && index === blocks.length - 1} />
))
)}
</div>
);
Expand Down
57 changes: 38 additions & 19 deletions apps/web/src/widgets/session/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -569,19 +579,16 @@ function Conversation({
}
: undefined);

const surface = useRef<HTMLDivElement>(null);
const composer = useRef<HTMLDivElement>(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<HTMLElement>(".session-transcript")?.style.setProperty("--composer-height", `${height}px`);
lastInset = Math.ceil(height);
setInset(lastInset);
};
Expand All @@ -599,7 +606,7 @@ function Conversation({
if (view.status === "failed") return <p className="p-3 text-ink-muted">{view.message}</p>;
const running = isRunning(view);
return (
<div ref={surface} inert={stand !== undefined} className="relative flex h-full flex-col">
<div inert={stand !== undefined} className="relative flex h-full flex-col">
<StreamedContext value={message}>
<ForkContext value={forkAt}>
<Transcript
Expand Down Expand Up @@ -740,13 +747,10 @@ function Transcript({
modelName={modelName}
/>
);
const transcriptRef = (instance: LegendListRef | null) => {
instance?.getScrollableNode().style.setProperty("--composer-height", `${inset}px`);
return ref(instance);
};
return (
<LegendList<Turn>
ref={transcriptRef}
ref={ref}
style={{ "--composer-height": `${inset}px` } as CSSProperties}
data={turns}
extraData={extraData}
keyExtractor={turnKey}
Expand Down Expand Up @@ -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")
Expand Down