From 767eba619991c8f34af7c78569a5c4b42a0acbf5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 19 Oct 2026 23:38:12 +0000 Subject: [PATCH 1/4] fix(web): show Codex citation details and copy source references --- .../components/ChatMarkdown.browser.test.tsx | 158 ++++++++++++++++++ apps/web/src/components/ChatMarkdown.tsx | 15 +- .../src/components/chat/CodexCitationChip.tsx | 58 +++++++ apps/web/src/markdown-clipboard.ts | 14 +- docs/user/providers-codex.md | 8 + 5 files changed, 249 insertions(+), 4 deletions(-) create mode 100644 apps/web/src/components/chat/CodexCitationChip.tsx diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index f29ee18bd929e..828d05fd51036 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -849,6 +849,164 @@ describe("ChatMarkdown artifact-template cards", () => { }); }); +describe("ChatMarkdown source citations", () => { + const marker = (...parts: string[]) => `\uE200cite\uE202${parts.join("\uE202")}\uE201`; + + it("opens numbered source details by keyboard and keeps repeated opaque IDs and ranges honest", async () => { + const container = await renderText( + `First ${marker("L42")}.\n\nTogether ${marker("alpha__id__", "L42", "alpha__id__", "L8-L13")}. Again ${marker("alpha__id__")}.`, + ); + const chips = [...container.querySelectorAll("button")]; + expect(chips.map((chip) => chip.textContent)).toEqual(["1", "2, 1", "2"]); + expect(container.textContent).not.toContain("\uE200"); + expect(container.querySelectorAll("a")).toHaveLength(0); + + await userEvent.tab(); + const single = page.getByRole("button", { name: "View source 1", exact: true }); + await expect.element(single).toHaveFocus(); + await userEvent.keyboard("{Enter}"); + const singleDetails = page.getByRole("dialog", { name: "Source 1", exact: true }); + await expect.element(singleDetails).toBeVisible(); + expect(singleDetails.element().textContent).toContain("L42"); + expect(singleDetails.element().textContent).toMatch(/source URL unavailable/i); + expect(singleDetails.element().textContent).not.toContain("Lines"); + expect(singleDetails.element().querySelectorAll("a")).toHaveLength(0); + await userEvent.keyboard("{Escape}"); + await expect.element(singleDetails).not.toBeInTheDocument(); + await expect.element(single).toHaveFocus(); + + const grouped = page.getByRole("button", { name: "View sources 2, 1", exact: true }); + await userEvent.click(grouped); + const details = page.getByRole("dialog", { name: "Sources 2, 1", exact: true }); + await expect.element(details).toBeVisible(); + expect(details.element().textContent).toContain("alpha__id__"); + expect(details.element().textContent).toContain("L42"); + expect(details.element().textContent).toContain("L8-L13"); + expect(details.element().textContent).toMatch(/source URL unavailable/i); + expect(details.element().querySelectorAll("a")).toHaveLength(0); + await userEvent.keyboard("{Escape}"); + await expect.element(details).not.toBeInTheDocument(); + await expect.element(grouped).toHaveFocus(); + expect(chips.map((chip) => chip.textContent)).toEqual(["1", "2, 1", "2"]); + }); + + it("copies full references from number-only selections while clipping prose and unrelated card titles", async () => { + const container = await renderText( + `Outside before ${marker("turn0view0")} middle ${marker("turn0view0", "opaque-id", "L8-L13")} after outside.\n\n${ARTIFACT_TEMPLATE_DIRECTIVE.replace("Hello World", "Quarterly Report")}`, + ); + const textNode = (within: Node, text: string) => { + const walker = document.createTreeWalker(within, NodeFilter.SHOW_TEXT); + for (let node = walker.nextNode(); node; node = walker.nextNode()) { + if (node.textContent === text) return node as Text; + } + throw new Error(`Rendered text not found: ${text}`); + }; + const copy = (start: Text, from: number, end: Text, to: number) => { + const range = document.createRange(); + range.setStart(start, from); + range.setEnd(end, to); + const selection = window.getSelection()!; + selection.removeAllRanges(); + selection.addRange(range); + const selected = selection.toString(); + const clipboardData = new DataTransfer(); + start.parentElement!.dispatchEvent( + new ClipboardEvent("copy", { bubbles: true, cancelable: true, clipboardData }), + ); + expect(selection.toString()).toBe(selected); + const html = new DOMParser().parseFromString(clipboardData.getData("text/html"), "text/html"); + expect(html.body.querySelectorAll("button")).toHaveLength(0); + return { text: clipboardData.getData("text/plain"), html: html.body.textContent }; + }; + + const title = textNode(container, "Quarterly Report"); + expect(copy(title, 10, title, title.length)).toEqual({ text: "Report", html: "Report" }); + + const single = buttonsLabelled(container, "View source 1"); + const grouped = buttonsLabelled(container, "View sources 1, 2"); + expect(single).toHaveLength(1); + expect(grouped).toHaveLength(1); + const singleLabel = textNode(single[0]!, "1"); + const groupedLabel = textNode(grouped[0]!, "1, 2"); + expect(copy(singleLabel, 0, singleLabel, singleLabel.length)).toEqual({ + text: "\\[Source 1: turn0view0\\]", + html: "[Source 1: turn0view0]", + }); + expect(copy(groupedLabel, 0, groupedLabel, groupedLabel.length)).toEqual({ + text: "\\[Source 1: turn0view0; Source 2: opaque-id; L8-L13\\]", + html: "[Source 1: turn0view0; Source 2: opaque-id; L8-L13]", + }); + + const before = textNode(container, "Outside before "); + const after = textNode(container, " after outside."); + expect(copy(before, 8, before, 14)).toEqual({ text: "before", html: "before" }); + expect(copy(before, 8, after, 6)).toEqual({ + text: "before \\[Source 1: turn0view0\\] middle \\[Source 1: turn0view0; Source 2: opaque-id; L8-L13\\] after", + html: "before [Source 1: turn0view0] middle [Source 1: turn0view0; Source 2: opaque-id; L8-L13] after", + }); + expect(single[0]!.textContent).toBe("1"); + expect(grouped[0]!.textContent).toBe("1, 2"); + window.getSelection()!.removeAllRanges(); + }); + + it("hides only unfinished streaming tails, restores them when stopped, and preserves literal examples", async () => { + const complete = marker("turn0view0"); + const partial = "\uE200cite\uE202next"; + const example = marker("excluded"); + const nonTail = "\uE200cite\uE202earlier"; + const unknown = "\uE200chart"; + const malformed = "\uE200cite\uE202two words\uE201"; + for (const literalTail of [ + unknown, + `\`\`\`text\n${partial}`, + `HTML ${partial}`, + `https://example.com/${partial}`, + ]) { + const container = await renderText(literalTail, { isStreaming: true }); + expect(container.textContent).toContain(literalTail === unknown ? unknown : partial); + expect(buttonsLabelled(container, "View source 1")).toHaveLength(0); + } + + const examples = [ + `Inline \`${example}\` and [link ${example}](https://example.com).`, + `\`\`\`text\n${example}\n\`\`\``, + `HTML ${example}.`, + `${nonTail}\n\nStill here. ${unknown} ${malformed}`, + ].join("\n\n"); + + for (const prefix of [ + `Answer ${complete}. Next `, + `${examples}\n\n- Answer ${complete}. Next `, + ]) { + const text = `${prefix}${partial}`; + const { container, rerender } = await render( + , + ); + expect.soft(container.textContent).not.toContain(partial); + expect.soft(buttonsLabelled(container, "View source 1")).toHaveLength(1); + if (prefix.includes(examples)) { + expect(container.querySelector("li")?.textContent).toContain("Answer "); + expect(container.querySelectorAll("pre")).toHaveLength(1); + expect(container.textContent?.split(example)).toHaveLength(5); + expect(container.textContent).toContain(nonTail); + expect(container.textContent).toContain(unknown); + expect(container.textContent).toContain(malformed); + } + + await rerender(); + expect(container.textContent).toContain(partial); + expect.soft(buttonsLabelled(container, "View source 1")).toHaveLength(1); + + await rerender( + , + ); + expect.soft(buttonsLabelled(container, "View source 1")).toHaveLength(2); + expect.soft(buttonsLabelled(container, "View source 2")).toHaveLength(1); + expect(container.textContent).not.toContain(partial); + } + }); +}); + describe("ChatMarkdown headings", () => { it("exposes headings below the host heading without changing their tags", async () => { const container = await renderText("# Top\n\n## Section\n\n###### Fine print", { diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index e8461925b3637..b284eee416918 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -16,8 +16,7 @@ import { isAbsolutePath, resolveWorkspaceFilePath } from "@t3tools/shared/path"; import { usePullRequestLinking } from "~/hooks/usePullRequestLinking"; import { AuthFilesystemReadScope, AuthOrchestrationOperateScope } from "@t3tools/contracts"; import { - CHAT_MARKDOWN_REMARK_PLUGINS, - CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, + chatMarkdownRemarkPlugins, CHAT_MARKDOWN_REHYPE_PLUGINS, } from "@t3tools/shared/markdownPipeline"; import { useAtomValue } from "@effect/atom-react"; @@ -109,6 +108,8 @@ import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations"; import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences"; import { parseThreadLinkHref } from "@t3tools/shared/threadLinks"; import { AssistantCitationChip } from "./chat/AssistantCitationChip"; +import { codexCitationFromHastProperties } from "@t3tools/shared/codexCitations"; +import { CodexCitationChip } from "./chat/CodexCitationChip"; import { MarkdownThreadLink } from "./chat/MarkdownThreadLink"; import { mathKind } from "@t3tools/shared/markdownMath"; import remarkMath from "remark-math"; @@ -2849,6 +2850,14 @@ const CHAT_MARKDOWN_COMPONENTS = { h4: markdownHeadingRenderer(4), h5: markdownHeadingRenderer(5), h6: markdownHeadingRenderer(6), + span: function MarkdownSpan({ node, children, ...props }) { + const citation = codexCitationFromHastProperties(node?.properties); + return citation ? ( + + ) : ( + {children} + ); + }, div: function MarkdownDiv({ node, children, ...props }) { const { onUseArtifactTemplate } = use(ChatMarkdownRendererContext); const artifactTemplate = artifactTemplateFromHastProperties(node?.properties); @@ -3475,7 +3484,7 @@ function ChatMarkdown({ markdownRef.current = node; }; const remarkPlugins = [ - ...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS), + ...chatMarkdownRemarkPlugins({ isStreaming: props.isStreaming === true, lineBreaks }), ...extraRemarkPlugins, ...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []), ]; diff --git a/apps/web/src/components/chat/CodexCitationChip.tsx b/apps/web/src/components/chat/CodexCitationChip.tsx new file mode 100644 index 0000000000000..ced47f05169c6 --- /dev/null +++ b/apps/web/src/components/chat/CodexCitationChip.tsx @@ -0,0 +1,58 @@ +import { useId } from "react"; +import { + codexCitationMarkdown, + codexCitationText, + type CodexCitation, +} from "@t3tools/shared/codexCitations"; + +import { ContextChip, ContextChipLabel } from "../ContextChip"; +import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover"; + +export function CodexCitationChip({ citation }: { citation: CodexCitation }) { + const descriptionId = useId(); + const numbers = citation.sources.map((source) => source.number).join(", "); + const label = `${citation.sources.length === 1 ? "Source" : "Sources"} ${numbers}`; + + return ( + + + } />} + aria-label={`View ${label.toLowerCase()}`} + title={`${label}. Source URL unavailable.`} + > + {numbers} + + + {label} +
+ {citation.sources.map((source) => ( +
+
Source {source.number}
+
{source.id}
+
+ ))} + {citation.locator ? ( +
+
Lines
+
{citation.locator}
+
+ ) : null} +
+

+ Source URL unavailable. Codex supplied source IDs without destination links. +

+
+
+
+ ); +} diff --git a/apps/web/src/markdown-clipboard.ts b/apps/web/src/markdown-clipboard.ts index 698aaf2952c73..c65efceb10462 100644 --- a/apps/web/src/markdown-clipboard.ts +++ b/apps/web/src/markdown-clipboard.ts @@ -373,6 +373,9 @@ function sanitizedHtmlFrom(container: Element): string { for (const wrapper of container.querySelectorAll(MATH_SELECTOR)) { wrapper.replaceWith(mathSourceElement(wrapper)); } + for (const node of container.querySelectorAll("[data-markdown-copy-text]")) { + node.textContent = node.getAttribute("data-markdown-copy-text"); + } for (const node of container.querySelectorAll(SANITIZED_HTML_SELECTOR)) { if ( node.classList.contains("chat-markdown-file-link") || @@ -397,7 +400,6 @@ export function chatMarkdownClipboardPayload( const range = selection.getRangeAt(index); if (range.collapsed) continue; const container = document.createElement("div"); - container.appendChild(range.cloneContents()); const ancestor = range.commonAncestorContainer; const ancestorElement = ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement; @@ -408,6 +410,16 @@ export function chatMarkdownClipboardPayload( htmls.push(`${mathSourceElement(math).outerHTML}`); continue; } + const copySource = ancestorElement?.closest("[data-markdown-copy][data-markdown-copy-text]"); + const fragment = range.cloneContents(); + // A range inside a citation's number label omits its copy metadata wrapper. + if (copySource) { + const wrapper = copySource.cloneNode(false); + wrapper.appendChild(fragment); + container.appendChild(wrapper); + } else { + container.appendChild(fragment); + } if (ancestorElement?.closest("pre")) { const text = range.toString(); if (text) { diff --git a/docs/user/providers-codex.md b/docs/user/providers-codex.md index bdc87902a63b2..63b69bacd441e 100644 --- a/docs/user/providers-codex.md +++ b/docs/user/providers-codex.md @@ -85,6 +85,14 @@ If you do not want to answer, dismiss the question from its panel. Dismissing closes it without sending anything to Codex. This requires a Codex version that supports async questions. +## Source citations + +Codex research responses can include numbered source citations. On web and desktop, select a +citation to inspect its source IDs and any cited line ranges. Source IDs identify references in +Codex's context; when no destination link is supplied, the details say the source URL is unavailable. + +Mobile and copied responses show readable source references with the same numbering. + ## Approve app access Codex tools can request access to another app. Respond to the named app's request From 986907fba26836d61311158d71021c6b47ec5c1e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 20 Oct 2026 00:02:53 +0000 Subject: [PATCH 2/4] fix(web): align citation search and CSV copy with rendered sources --- .../components/ChatMarkdown.browser.test.tsx | 66 ++++++++++++++++++- apps/web/src/markdown-clipboard.ts | 9 ++- packages/shared/src/markdownPipeline.test.ts | 2 +- packages/shared/src/threadFindText.ts | 44 +++++++++---- 4 files changed, 103 insertions(+), 18 deletions(-) diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index 828d05fd51036..ba450fd3e639b 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -10,8 +10,9 @@ import { page, userEvent } from "vite-plus/test/browser"; import { filesystemEnvironment } from "../state/filesystem"; import { useRightPanelStore } from "../rightPanelStore"; import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; +import { serializeTableElementToCsv } from "../markdown-clipboard"; import { MarkdownFindContext } from "./chat/markdownFindContext"; -import { useThreadFindHighlights } from "./chat/threadFindHighlights"; +import { collectThreadFindRanges, useThreadFindHighlights } from "./chat/threadFindHighlights"; import { setMarkdownTaskChecked } from "./files/filePreviewMode"; const { workspaceSearch, filesystemMetadata } = vi.hoisted(() => ({ @@ -947,6 +948,13 @@ describe("ChatMarkdown source citations", () => { expect(single[0]!.textContent).toBe("1"); expect(grouped[0]!.textContent).toBe("1, 2"); window.getSelection()!.removeAllRanges(); + + const tableContainer = await renderText( + `| City | Evidence |\n| --- | --- |\n| Paris | Population ${marker("turn0view0", "opaque-id", "L8-L13")} |`, + ); + expect(serializeTableElementToCsv(tableContainer.querySelector("table")!)).toBe( + "City,Evidence\nParis,Population [Source 1: turn0view0; Source 2: opaque-id; L8-L13]", + ); }); it("hides only unfinished streaming tails, restores them when stopped, and preserves literal examples", async () => { @@ -1133,6 +1141,62 @@ describe("ChatMarkdown in-page links", () => { }); describe("ChatMarkdown thread find", () => { + it("indexes the visible citation labels and literal examples as streaming messages stop", async () => { + const marker = (...parts: string[]) => `\uE200cite\uE202${parts.join("\uE202")}\uE201`; + const codeExample = marker("literal_code"); + const linkExample = marker("literal_link"); + const text = [ + `First ${marker("opaque_alpha")}. Group ${marker("opaque_beta", "opaque_alpha", "opaque_beta", "L8-L13")}. Repeat ${marker("opaque_alpha")}.`, + `Literal \`${codeExample}\` and [${linkExample}](https://example.com).`, + "Tail \uE200cite\uE202partial_tail", + ].join("\n\n"); + const { container, rerender } = await render(
); + for (const state of [ + { role: "assistant", streaming: true }, + { role: "assistant", streaming: false }, + { role: "user", streaming: false }, + ] as const) { + await rerender( +
+
+ +
+
, + ); + const segments = searchableMessageSegments({ ...state, text }) ?? []; + for (const [query, count] of [ + ["1", 3], + ["2, 1", 1], + ["opaque_alpha", 0], + ["L8-L13", 0], + [codeExample, 1], + [linkExample, 1], + ["partial_tail", state.streaming ? 0 : 1], + ] as const) { + const context = `${state.role}, streaming=${state.streaming}, query=${query}`; + expect( + collectThreadFindRanges(container, query).map(({ range }) => range.toString()), + context, + ).toEqual(Array.from({ length: count }, () => query)); + expect + .soft( + segments.reduce( + (sum, segment) => sum + countThreadSearchOccurrences(segment, query), + 0, + ), + context, + ) + .toBe(count); + } + } + }); + function FindProbe(props: { container: HTMLElement; query: string; diff --git a/apps/web/src/markdown-clipboard.ts b/apps/web/src/markdown-clipboard.ts index c65efceb10462..a1e543e6bec40 100644 --- a/apps/web/src/markdown-clipboard.ts +++ b/apps/web/src/markdown-clipboard.ts @@ -332,13 +332,18 @@ export function mathWrapperOf(element: Element | null): Element | null { return element?.closest(MATH_SELECTOR) ?? null; } -/** A cell's text, with each formula as its TeX. */ +/** Copy formulas as TeX and citation labels as complete references. */ function csvCellText(cell: Element): string { - if (!cell.querySelector(MATH_SELECTOR)) return cell.textContent ?? ""; + if (!cell.querySelector(`${MATH_SELECTOR}, [data-markdown-copy-text]`)) { + return cell.textContent ?? ""; + } const copy = cell.cloneNode(true) as Element; for (const wrapper of copy.querySelectorAll(MATH_SELECTOR)) { wrapper.replaceWith(wrapper.getAttribute("data-markdown-copy")?.trim() ?? ""); } + for (const node of copy.querySelectorAll("[data-markdown-copy-text]")) { + node.textContent = node.getAttribute("data-markdown-copy-text"); + } return copy.textContent ?? ""; } diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.ts index 2edd4668611fd..4de166a4b3b0c 100644 --- a/packages/shared/src/markdownPipeline.test.ts +++ b/packages/shared/src/markdownPipeline.test.ts @@ -107,7 +107,7 @@ it("renders stable source numbers, grouped locators and opaque IDs through both ).toBe("[Source 1: L42]"); expect( searchableMessageSegments({ role: "assistant", text: markdown, streaming: false }), - ).toEqual([expected]); + ).toEqual(["First 1. Grouped 2, 1. Sole 33."]); }); it("keeps citation examples literal without numbering them or suppressing later prose", () => { diff --git a/packages/shared/src/threadFindText.ts b/packages/shared/src/threadFindText.ts index 865a82708960d..3f2c0536b1048 100644 --- a/packages/shared/src/threadFindText.ts +++ b/packages/shared/src/threadFindText.ts @@ -8,6 +8,7 @@ import { renderCodexInlineDirectivesAsMarkdown, } from "./codexMarkdownDirectives.ts"; import { codexArtifactTemplatePresentationLabel } from "./codexArtifactTemplates.ts"; +import { codexCitationFromHastProperties } from "./codexCitations.ts"; import { buildFileLinkParentSuffixByPath, splitFilePathPosition, @@ -30,8 +31,8 @@ import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { shouldPreserveAssistantLineBreaks, + chatMarkdownRemarkPlugins, CHAT_MARKDOWN_REHYPE_PLUGINS, - CHAT_MARKDOWN_REMARK_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, } from "./markdownPipeline.ts"; @@ -75,16 +76,18 @@ export const THREAD_FIND_BLOCK_TAGS = new Set([ "ul", ]); -const assistantProcessor = unified() - .use(remarkParse) - .use(CHAT_MARKDOWN_REMARK_PLUGINS) - .use(remarkRehype, { allowDangerousHtml: true }) - .use(CHAT_MARKDOWN_REHYPE_PLUGINS); -const assistantBreaksProcessor = unified() - .use(remarkParse) - .use(CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS) - .use(remarkRehype, { allowDangerousHtml: true }) - .use(CHAT_MARKDOWN_REHYPE_PLUGINS); +function createAssistantProcessor(isStreaming = false, lineBreaks = false) { + return unified() + .use(remarkParse) + .use(chatMarkdownRemarkPlugins({ isStreaming, lineBreaks })) + .use(remarkRehype, { allowDangerousHtml: true }) + .use(CHAT_MARKDOWN_REHYPE_PLUGINS); +} + +const assistantProcessor = createAssistantProcessor(); +const assistantBreaksProcessor = createAssistantProcessor(false, true); +const streamingAssistantProcessor = createAssistantProcessor(true); +const streamingAssistantBreaksProcessor = createAssistantProcessor(true, true); const userProcessor = unified() .use(remarkParse) .use(CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS) @@ -98,6 +101,7 @@ interface TextTree { readonly href?: unknown; readonly src?: unknown; readonly dataInlineCode?: unknown; + readonly dataCodexCitation?: unknown; }; readonly children?: ReadonlyArray; } @@ -108,12 +112,17 @@ function markdownThreadFindText( cwd?: string, skills: readonly InlineSkill[] = [], lineBreaks = false, + isStreaming = false, ): string[] { const processor = userMessage ? userProcessor - : lineBreaks - ? assistantBreaksProcessor - : assistantProcessor; + : isStreaming + ? lineBreaks + ? streamingAssistantBreaksProcessor + : streamingAssistantProcessor + : lineBreaks + ? assistantBreaksProcessor + : assistantProcessor; const tree = processor.runSync(processor.parse(markdown), { value: markdown }); const paths = [ ...extractMarkdownLinkHrefs(renderCodexInlineDirectivesAsMarkdown(markdown)), @@ -130,6 +139,12 @@ function markdownThreadFindText( text = ""; }; const visit = (node: TextTree, inPre = false, inlineSkills = false) => { + const codexCitation = + node.tagName === "span" ? codexCitationFromHastProperties(node.properties) : null; + if (codexCitation) { + text += codexCitation.sources.map((source) => source.number).join(", "); + return; + } const href = node.properties?.href ?? node.properties?.src; if (userMessage && typeof href === "string" && parseComposerContextHref(href)) { flush(); @@ -226,5 +241,6 @@ export function searchableMessageSegments( cwd, skills, shouldPreserveAssistantLineBreaks(message.text), + message.streaming, ); } From 865b0d3ef66988a6ed156bf6fa974dabcc3c1f79 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 20 Oct 2026 00:29:47 +0000 Subject: [PATCH 3/4] fix(web): publish citation helpers alongside chip consumers --- .../components/ChatMarkdown.browser.test.tsx | 1 - packages/shared/src/codexCitations.ts | 48 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index ba450fd3e639b..a4efa437e17fe 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -10,7 +10,6 @@ import { page, userEvent } from "vite-plus/test/browser"; import { filesystemEnvironment } from "../state/filesystem"; import { useRightPanelStore } from "../rightPanelStore"; import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting"; -import { serializeTableElementToCsv } from "../markdown-clipboard"; import { MarkdownFindContext } from "./chat/markdownFindContext"; import { collectThreadFindRanges, useThreadFindHighlights } from "./chat/threadFindHighlights"; import { setMarkdownTaskChecked } from "./files/filePreviewMode"; diff --git a/packages/shared/src/codexCitations.ts b/packages/shared/src/codexCitations.ts index b7caef64e9901..5f3b953cb35e1 100644 --- a/packages/shared/src/codexCitations.ts +++ b/packages/shared/src/codexCitations.ts @@ -117,14 +117,14 @@ const CITATION_SYNTAX: Extension = { text: { [START.charCodeAt(0)]: { tokenize: tokenizeCitation } }, }; -function codexCitationText(citation: CodexCitation): string { +export function codexCitationText(citation: CodexCitation): string { const sources = citation.sources.map(({ id, number }) => `Source ${number}: ${id}`); if (citation.locator !== undefined) sources.push(citation.locator); return `[${sources.join("; ")}]`; } /** Keeps the opaque source IDs readable without inventing link destinations. */ -function codexCitationMarkdown(citation: CodexCitation): string { +export function codexCitationMarkdown(citation: CodexCitation): string { return codexCitationText(citation).replace(/[\\\]*_`<&]/g, "\\$&"); } @@ -304,3 +304,47 @@ export function renderCodexCitationsAsMarkdown( } return rendered + markdown.slice(cursor); } + +export function codexCitationFromHastProperties( + properties: Readonly> | null | undefined, +): CodexCitation | null { + const serialized = properties?.dataCodexCitation; + if (typeof serialized !== "string") return null; + let value: unknown; + try { + value = JSON.parse(serialized); + } catch { + return null; + } + if (typeof value !== "object" || value === null || !("raw" in value)) return null; + if (typeof value.raw !== "string" || !("sources" in value) || !Array.isArray(value.sources)) { + return null; + } + const parsed = parseCitation(value.raw); + if (!parsed || parsed.ids.length !== value.sources.length) return null; + const locator = "locator" in value ? value.locator : undefined; + if (locator !== parsed.locator) return null; + const sources: { readonly id: string; readonly number: number }[] = []; + const rawSources: readonly unknown[] = value.sources; + for (const [index, source] of rawSources.entries()) { + if ( + typeof source !== "object" || + source === null || + !("id" in source) || + typeof source.id !== "string" || + source.id !== parsed.ids[index] || + !("number" in source) || + typeof source.number !== "number" || + !Number.isSafeInteger(source.number) || + source.number <= 0 + ) { + return null; + } + sources.push({ id: source.id, number: source.number }); + } + return { + raw: value.raw, + sources, + ...(parsed.locator === undefined ? {} : { locator: parsed.locator }), + }; +} From a8bfa4f1910735fd3257e20b7ec19c5b7a2d9771 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 20 Oct 2026 00:43:06 +0000 Subject: [PATCH 4/4] test(web): preserve copied citation text with math enabled --- apps/web/src/components/ChatMarkdown.browser.test.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index a4efa437e17fe..3f51c1b655bf2 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -929,11 +929,11 @@ describe("ChatMarkdown source citations", () => { const singleLabel = textNode(single[0]!, "1"); const groupedLabel = textNode(grouped[0]!, "1, 2"); expect(copy(singleLabel, 0, singleLabel, singleLabel.length)).toEqual({ - text: "\\[Source 1: turn0view0\\]", + text: "[Source 1: turn0view0\\]", html: "[Source 1: turn0view0]", }); expect(copy(groupedLabel, 0, groupedLabel, groupedLabel.length)).toEqual({ - text: "\\[Source 1: turn0view0; Source 2: opaque-id; L8-L13\\]", + text: "[Source 1: turn0view0; Source 2: opaque-id; L8-L13\\]", html: "[Source 1: turn0view0; Source 2: opaque-id; L8-L13]", }); @@ -941,7 +941,7 @@ describe("ChatMarkdown source citations", () => { const after = textNode(container, " after outside."); expect(copy(before, 8, before, 14)).toEqual({ text: "before", html: "before" }); expect(copy(before, 8, after, 6)).toEqual({ - text: "before \\[Source 1: turn0view0\\] middle \\[Source 1: turn0view0; Source 2: opaque-id; L8-L13\\] after", + text: "before [Source 1: turn0view0\\] middle [Source 1: turn0view0; Source 2: opaque-id; L8-L13\\] after", html: "before [Source 1: turn0view0] middle [Source 1: turn0view0; Source 2: opaque-id; L8-L13] after", }); expect(single[0]!.textContent).toBe("1");