diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 79f0c1fc9a41d..7cef73090fee8 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -1,5 +1,6 @@ import { InlineAudioPreview } from "../files/InlineAudioPreview"; import { filePreviewKind } from "@t3tools/shared/filePreview"; +import { CHAT_USER_MARKDOWN_REMARK_PLUGINS } from "@t3tools/shared/markdownPipeline"; import { findRevealRef } from "./markdownFindContext"; import { type EnvironmentId, @@ -1203,6 +1204,7 @@ const UserMessageBody = memo(function UserMessageBody(props: { className="text-foreground" lineBreaks parseRawHtml={false} + extraRemarkPlugins={CHAT_USER_MARKDOWN_REMARK_PLUGINS} renderContextReference={props.renderContextReference} headingLevelOffset={MESSAGE_HEADING_LEVEL} /> diff --git a/apps/web/src/components/chat/UserMessageContextChips.tsx b/apps/web/src/components/chat/UserMessageContextChips.tsx index 2638692caab26..ab9cc1db7bf3c 100644 --- a/apps/web/src/components/chat/UserMessageContextChips.tsx +++ b/apps/web/src/components/chat/UserMessageContextChips.tsx @@ -45,6 +45,7 @@ import { reviewCommentContextLabel, } from "~/lib/composerContextRecords"; import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; +import { CHAT_USER_MARKDOWN_REMARK_PLUGINS } from "@t3tools/shared/markdownPipeline"; import { ContextChip, ContextChipLabel, type ContextChipKind } from "../ContextChip"; import { createContextPresentationRegistry } from "../contextPresentationRegistry"; import type { ChatMarkdownContextReference } from "../ChatMarkdown"; @@ -564,6 +565,7 @@ function UserMessageReviewCommentCard({ comment }: { comment: ReviewCommentConte skills={ctx.skills} parseRawHtml={false} lineBreaks + extraRemarkPlugins={CHAT_USER_MARKDOWN_REMARK_PLUGINS} /> )} diff --git a/apps/web/src/components/composerContextPresentation.tsx b/apps/web/src/components/composerContextPresentation.tsx index 845e921725b55..9a26913da8a48 100644 --- a/apps/web/src/components/composerContextPresentation.tsx +++ b/apps/web/src/components/composerContextPresentation.tsx @@ -1,5 +1,6 @@ import ChatMarkdown from "./ChatMarkdown"; import { isSvgAttachment } from "@t3tools/shared/image"; +import { CHAT_USER_MARKDOWN_REMARK_PLUGINS } from "@t3tools/shared/markdownPipeline"; import { ReadOnlySourcePreview } from "./files/AttachmentFilePreview"; import type { EnvironmentContextRecord, @@ -291,7 +292,13 @@ function ComposerReviewCommentDetails({ comment }: { comment: ReviewCommentConte {comment.sectionTitle} · {comment.rangeLabel} - {comment.text.trim() ? : null} + {comment.text.trim() ? ( + + ) : null} {comment.diff.trim() ? (
diff --git a/packages/shared/src/codexMarkdownDirectives.test.ts b/packages/shared/src/codexMarkdownDirectives.test.ts index c09914a5f05f6..d12344dcb4082 100644 --- a/packages/shared/src/codexMarkdownDirectives.test.ts +++ b/packages/shared/src/codexMarkdownDirectives.test.ts @@ -222,3 +222,23 @@ describe("directive copy adapter", () => { ); }); }); + +describe("remarkCodexDirectives keepWebCitations", () => { + it("keeps web citation markers as typed in text, labels, and link destinations", () => { + const marker = "citeturn0search0"; + const markdown = `See https://a.dev${marker} and [s](https://b.dev${marker}), :codex-followup[Read ${marker}]{prompt="Read"}`; + const processor = unified() + .use(remarkParse) + .use(remarkGfm) + .use(remarkCodexDirectives) + .use(remarkCodexDirectives, { keepWebCitations: true }); + const tree = processor.runSync(processor.parse(markdown), { value: markdown }) as TestNode; + const links = tree.children?.[0]?.children?.filter((node) => node.type === "link") ?? []; + + expect(textContent(tree)).toBe(`See https://a.dev${marker} and s, Read ${marker}`); + expect(links.map((link) => link.url)).toEqual([ + `https://a.dev${marker}`, + `https://b.dev${marker}`, + ]); + }); +}); diff --git a/packages/shared/src/codexMarkdownDirectives.ts b/packages/shared/src/codexMarkdownDirectives.ts index 06f3130cd222a..0ea007940642e 100644 --- a/packages/shared/src/codexMarkdownDirectives.ts +++ b/packages/shared/src/codexMarkdownDirectives.ts @@ -318,7 +318,23 @@ function transformCodexDirectives(node: MarkdownAstNode, source: string, insideL } } -function attachCodexDirectives(this: Processor) { +type WebCitationField = readonly [node: MarkdownAstNode, key: "value" | "url", text: string]; + +function webCitationFields(node: MarkdownAstNode, fields: WebCitationField[]): WebCitationField[] { + for (const key of ["value", "url"] as const) { + const text = node[key]; + if (text?.includes(WEB_CITATION_OPEN)) fields.push([node, key, text]); + } + for (const child of node.children ?? []) webCitationFields(child, fields); + return fields; +} + +interface CodexDirectiveOptions { + /** User-authored text shows web citation markers as typed; only assistant output hides them. */ + readonly keepWebCitations?: boolean; +} + +function attachCodexDirectives(this: Processor, options: CodexDirectiveOptions = {}) { const data = this.data(); const micromarkExtensions = data.micromarkExtensions ?? (data.micromarkExtensions = []); const fromMarkdownExtensions = data.fromMarkdownExtensions ?? (data.fromMarkdownExtensions = []); @@ -326,7 +342,9 @@ function attachCodexDirectives(this: Processor) { fromMarkdownExtensions.push(CODEX_DIRECTIVE_FROM_MARKDOWN); return (tree: unknown, file: MarkdownFile) => { + const kept = options.keepWebCitations ? webCitationFields(tree as MarkdownAstNode, []) : []; transformCodexDirectives(tree as MarkdownAstNode, String(file.value)); + for (const [node, key, text] of kept) node[key] = text; }; } diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.ts index e4527a8a017c9..3bc19cb35f8c3 100644 --- a/packages/shared/src/markdownPipeline.ts +++ b/packages/shared/src/markdownPipeline.ts @@ -250,6 +250,11 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [ remarkNormalizeLinksAndTagInlineCode, ]; +// Appended after a base list, this reconfigures its remarkCodexDirectives entry. +export const CHAT_USER_MARKDOWN_REMARK_PLUGINS: PluggableList = [ + [remarkCodexDirectives, { keepWebCitations: true }], +]; + // Start tags an HTML parser closes an open `

` for. const CLOSES_PARAGRAPH = new Set( "address article aside blockquote center details dialog dir div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr li listing main menu nav ol p plaintext pre search section summary table ul xmp dd dt".split( diff --git a/packages/shared/src/threadFindText.test.ts b/packages/shared/src/threadFindText.test.ts index 77a0874c21336..88c51a4ddf744 100644 --- a/packages/shared/src/threadFindText.test.ts +++ b/packages/shared/src/threadFindText.test.ts @@ -117,3 +117,9 @@ it("indexes the citation chip label instead of its link text", () => { "my note", ]); }); + +it("indexes web citation markers as typed in user messages only", () => { + const text = "Explain citeturn0search0 please"; + expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([text]); + expect(assistantSegments(text)).toEqual(["Explain please"]); +}); diff --git a/packages/shared/src/threadFindText.ts b/packages/shared/src/threadFindText.ts index 3afec6e04729e..f51b9370d963b 100644 --- a/packages/shared/src/threadFindText.ts +++ b/packages/shared/src/threadFindText.ts @@ -36,6 +36,7 @@ import { CHAT_MARKDOWN_REHYPE_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, + CHAT_USER_MARKDOWN_REMARK_PLUGINS, } from "./markdownPipeline.ts"; // Inline wrappers (including Shiki token spans) must not split a search phrase. @@ -91,6 +92,7 @@ const assistantBreaksProcessor = unified() const userProcessor = unified() .use(remarkParse) .use(CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS) + .use(CHAT_USER_MARKDOWN_REMARK_PLUGINS) .use(remarkRehype, { allowDangerousHtml: true }); interface TextTree {