Compare commits

...
Author SHA1 Message Date
0d73e66a1c Defer streaming chat markdown rendering
Co-authored-by: Julius Marminge <[email protected]>

Co-authored-by: codex <[email protected]>
2026-06-18 23:34:50 -07:00
Mika CohenandJulius Marminge 20f37f367a Avoid repeated theme DOM sync during startup (#2779)
Co-authored-by: Julius Marminge <[email protected]>
2026-06-19 05:46:12 +00:00
Soorria SaruvaandCursor 804d44cfb1 fix(ssh): fix support for remotes that use fnm (#2641)
Co-authored-by: Cursor <[email protected]>
2026-06-18 22:45:04 -07:00
5 changed files with 72 additions and 12 deletions

No files matched your search

@@ -0,0 +1,40 @@
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
const reactMock = vi.hoisted(() => ({
useDeferredValue: vi.fn<(text: string) => string>(),
}));
vi.mock("react", async (importOriginal) => ({
...(await importOriginal<typeof import("react")>()),
useDeferredValue: reactMock.useDeferredValue,
}));
import ChatMarkdown from "./ChatMarkdown";
describe("ChatMarkdown streaming rendering", () => {
beforeEach(() => {
reactMock.useDeferredValue.mockReset();
});
it("renders the scheduled markdown text instead of the latest streaming source", () => {
reactMock.useDeferredValue.mockReturnValue("**deferred markdown**");
const markup = renderToStaticMarkup(
<ChatMarkdown text="latest markdown" cwd={undefined} isStreaming />,
);
expect(reactMock.useDeferredValue).toHaveBeenCalledWith("latest markdown");
expect(markup).toContain("<strong>deferred markdown</strong>");
expect(markup).not.toContain("latest markdown");
});
it("renders the latest markdown immediately after streaming finishes", () => {
reactMock.useDeferredValue.mockReturnValue("stale markdown");
const markup = renderToStaticMarkup(<ChatMarkdown text="final markdown" cwd={undefined} />);
expect(markup).toContain("final markdown");
expect(markup).not.toContain("stale markdown");
});
});
+10 -5
View File
@@ -25,6 +25,7 @@ import React, {
isValidElement,
use,
useCallback,
useDeferredValue,
memo,
useEffect,
useMemo,
@@ -1172,12 +1173,14 @@ function ChatMarkdown({
serverConfig?.availableEditors ?? [],
);
const diffThemeName = resolveDiffThemeName(resolvedTheme);
const deferredText = useDeferredValue(text);
const markdownText = isStreaming ? deferredText : text;
const markdownFileLinkMetaByHref = useMemo(() => {
const metaByHref = new Map<
string,
NonNullable<ReturnType<typeof resolveMarkdownFileLinkMeta>>
>();
for (const href of extractMarkdownLinkHrefs(text)) {
for (const href of extractMarkdownLinkHrefs(markdownText)) {
const normalizedHref = normalizeMarkdownLinkHrefKey(href);
if (metaByHref.has(normalizedHref)) continue;
const meta = resolveMarkdownFileLinkMeta(normalizedHref, cwd);
@@ -1186,7 +1189,7 @@ function ChatMarkdown({
}
}
return metaByHref;
}, [cwd, text]);
}, [cwd, markdownText]);
const fileLinkParentSuffixByPath = useMemo(() => {
const filePaths = [...markdownFileLinkMetaByHref.values()].map((meta) => meta.filePath);
return buildFileLinkParentSuffixByPath(filePaths);
@@ -1253,7 +1256,9 @@ function ChatMarkdown({
li({ node, children, ...props }) {
const listItemStart = node?.position?.start.offset;
const markerOffset =
typeof listItemStart === "number" ? findTaskListMarkerOffset(text, listItemStart) : null;
typeof listItemStart === "number"
? findTaskListMarkerOffset(markdownText, listItemStart)
: null;
return (
<li {...props} data-task-marker-offset={markerOffset ?? undefined}>
{renderSkillInlineMarkdownChildren(children, skills)}
@@ -1439,7 +1444,7 @@ function ChatMarkdown({
openMarkdownFileInPreview,
resolvedTheme,
skills,
text,
markdownText,
threadRef,
],
);
@@ -1462,7 +1467,7 @@ function ChatMarkdown({
components={markdownComponents}
urlTransform={markdownUrlTransform}
>
{text}
{markdownText}
</ReactMarkdown>
</div>
);
+17 -5
View File
@@ -18,6 +18,7 @@ const DYNAMIC_THEME_COLOR_SELECTOR = `meta[name="${THEME_COLOR_META_NAME}"][data
let listeners: Array<() => void> = [];
let lastSnapshot: ThemeSnapshot | null = null;
let lastDesktopTheme: Theme | null = null;
let lastAppliedTheme: ThemeSnapshot | null = null;
function emitChange() {
for (const listener of listeners) listener();
@@ -28,7 +29,11 @@ function hasThemeStorage() {
}
function getSystemDark() {
return typeof window !== "undefined" && window.matchMedia(MEDIA_QUERY).matches;
return (
typeof window !== "undefined" &&
typeof window.matchMedia === "function" &&
window.matchMedia(MEDIA_QUERY).matches
);
}
function getStored(): Theme {
@@ -89,11 +94,18 @@ export function syncBrowserChromeTheme() {
function applyTheme(theme: Theme, suppressTransitions = false) {
if (typeof document === "undefined" || typeof window === "undefined") return;
const systemDark = theme === "system" ? getSystemDark() : false;
if (lastAppliedTheme?.theme === theme && lastAppliedTheme.systemDark === systemDark) {
syncDesktopTheme(theme);
return;
}
if (suppressTransitions) {
document.documentElement.classList.add("no-transitions");
}
const isDark = theme === "dark" || (theme === "system" && getSystemDark());
const isDark = theme === "dark" || (theme === "system" && systemDark);
document.documentElement.classList.toggle("dark", isDark);
lastAppliedTheme = { theme, systemDark };
syncBrowserChromeTheme();
syncDesktopTheme(theme);
if (suppressTransitions) {
@@ -148,12 +160,12 @@ function subscribe(listener: () => void): () => void {
listeners.push(listener);
// Listen for system preference changes
const mq = window.matchMedia(MEDIA_QUERY);
const mq = typeof window.matchMedia === "function" ? window.matchMedia(MEDIA_QUERY) : null;
const handleChange = () => {
if (getStored() === "system") applyTheme("system", true);
emitChange();
};
mq.addEventListener("change", handleChange);
mq?.addEventListener("change", handleChange);
// Listen for storage changes from other tabs
const handleStorage = (e: StorageEvent) => {
@@ -166,7 +178,7 @@ function subscribe(listener: () => void): () => void {
return () => {
listeners = listeners.filter((l) => l !== listener);
mq.removeEventListener("change", handleChange);
mq?.removeEventListener("change", handleChange);
window.removeEventListener("storage", handleStorage);
};
}
+3 -1
View File
@@ -105,7 +105,9 @@ describe("ssh tunnel scripts", () => {
assert.include(script, 'prepend_path_if_dir "$VOLTA_HOME/bin"');
assert.include(script, 'prepend_path_if_dir "$HOME/.asdf/shims"');
assert.include(script, 'prepend_path_if_dir "$HOME/.local/share/mise/shims"');
assert.include(script, 'eval "$(fnm env --use-on-cd --shell sh)"');
assert.include(script, 'eval "$(fnm env --shell bash)"');
assert.include(script, "fnm use --silent-if-unchanged");
assert.include(script, "fnm use default");
assert.include(script, 'prepend_path_if_dir "$HOME/.nodenv/shims"');
assert.include(script, 'NVM_DIR="$HOME/.nvm"');
assert.include(script, "nvm use --silent default");
+2 -1
View File
@@ -398,7 +398,8 @@ ensure_remote_node_path() {
prepend_path_if_dir "$FNM_DIR"
prepend_path_if_dir "$HOME/.fnm"
if ! command -v node >/dev/null 2>&1 && command -v fnm >/dev/null 2>&1; then
eval "$(fnm env --use-on-cd --shell sh)" >/dev/null 2>&1 || eval "$(fnm env --shell sh)" >/dev/null 2>&1 || true
eval "$(fnm env --shell bash)" >/dev/null 2>&1 || true
fnm use --silent-if-unchanged >/dev/null 2>&1 || fnm use default >/dev/null 2>&1 || true
fi
prepend_path_if_dir "$HOME/.nodenv/bin"