fix(web,mobile): render Codex citations and artifact templates #8584
main fromt3code/codex-citation-handlingmerged by Yash-Singh1(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change
- packages/client-runtime/src/codexFileCitations.ts lines 27 to 30OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +10,21 @@10+function positiveInteger(value: string | null | undefined): number | undefined {11+ if (value === null || value === undefined || value.trim().length === 0) return undefined;12+ const parsed = Number(value);13+ return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;14+}15+16+function citationLabel(path: string): string {17+ const normalized = path.replaceAll("\\", "/").replace(/\/+$/, "");18+ return normalized.slice(normalized.lastIndexOf("/") + 1) || normalized || "File";19+}20+21+export function resolveCodexFileCitationLink(22+ attributes: CodexFileCitationAttributes | null | undefined,23+): CodexFileCitationLink | null {24+ const path = attributes?.path?.trim();25+ if (!path) return null;26+27+ const lineRangeStart = positiveInteger(attributes?.line_range_start);28+ return {29+ path,30+ href: lineRangeStart === undefined ? path : `${path}#L${lineRangeStart}`,packages/client-runtime/src/codexFileCitations.ts lines 37 to 37OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +17,21 @@17+ const normalized = path.replaceAll("\\", "/").replace(/\/+$/, "");18+ return normalized.slice(normalized.lastIndexOf("/") + 1) || normalized || "File";19+}20+21+export function resolveCodexFileCitationLink(22+ attributes: CodexFileCitationAttributes | null | undefined,23+): CodexFileCitationLink | null {24+ const path = attributes?.path?.trim();25+ if (!path) return null;26+27+ const lineRangeStart = positiveInteger(attributes?.line_range_start);28+ return {29+ path,30+ href: lineRangeStart === undefined ? path : `${path}#L${lineRangeStart}`,31+ label: citationLabel(path),32+ ...(lineRangeStart === undefined ? {} : { lineRangeStart }),33+ };34+}35+36+function markdownLabel(value: string): string {37+ return value.replace(/[\\[\]]/g, "\\$&");apps/web/src/components/ChatMarkdown.tsx lines 429 to 432OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -391,0 +412,21 @@412+ if (!citation) {413+ const start = node.position?.start.offset;414+ const end = node.position?.end.offset;415+ node.type = "text";416+ node.value =417+ start === undefined || end === undefined418+ ? ":codex-file-citation"419+ : source.slice(start, end);420+ delete node.children;421+ return;422+ }423+ node.type = "link";424+ node.url = citation.href;425+ node.children = [{ type: "text", value: citation.label }];426+ return;427+ }428+429+ if (node.type === "link") return;430+ for (const child of node.children ?? []) {431+ visit(child);432+ }
- apps/web/src/components/ChatMarkdown.tsx line 437OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -391,0 +417,21 @@417+ start === undefined || end === undefined418+ ? ":codex-file-citation"419+ : source.slice(start, end);420+ delete node.children;421+ return;422+ }423+ node.type = "link";424+ node.url = citation.href;425+ node.children = [{ type: "text", value: citation.label }];426+ return;427+ }428+429+ if (node.type === "link") return;430+ for (const child of node.children ?? []) {431+ visit(child);432+ }433+ };434+435+ visit(tree);436+ };437+}apps/mobile/src/features/threads/ThreadFeed.tsx line 1036OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -1026,3 +1030,7 @@10261030 if (entry.type === "message") {10271031 const { message } = entry;10281032 const isUser = message.role === "user";1033+ const renderedText = message.text;1034+ const copiedText = isUser1035+ ? renderedText1036+ : replaceCodexFileCitationsWithMarkdownLinks(renderedText);
- apps/web/src/components/ChatMarkdown.tsx lines 410 to 427OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -391,0 +407,21 @@407+ return (tree: MarkdownAstNode, file: { value: unknown }) => {408+ const source = String(file.value);409+ const visit = (node: MarkdownAstNode) => {410+ if (node.type === "textDirective" && node.name === "codex-file-citation") {411+ const citation = resolveCodexFileCitationLink(node.attributes);412+ if (!citation) {413+ const start = node.position?.start.offset;414+ const end = node.position?.end.offset;415+ node.type = "text";416+ node.value =417+ start === undefined || end === undefined418+ ? ":codex-file-citation"419+ : source.slice(start, end);420+ delete node.children;421+ return;422+ }423+ node.type = "link";424+ node.url = citation.href;425+ node.children = [{ type: "text", value: citation.label }];426+ return;427+ }
- apps/web/src/components/ChatMarkdown.tsx lines 420 to 446OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -391,0 +426,21 @@426+ if (node.type === "textDirective" && node.name === "codex-file-citation") {427+ const citation = resolveCodexFileCitationLink(node.attributes);428+ if (citation) {429+ node.type = "link";430+ node.url = citation.href;431+ node.children = [{ type: "text", value: citation.label }];432+ return;433+ }434+ }435+436+ restoreDirectiveSource(node);437+ return;438+ }439+440+ if (node.type === "link") return;441+ for (const child of node.children ?? []) {442+ visit(child);443+ }444+ };445+446+ visit(tree);apps/web/src/components/ChatMarkdown.tsx lines 409 to 418OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -386,6 +398,21 @@398+ };386399 data?: {387400 hProperties?: Record<string, unknown>;388401 };389402 children?: MarkdownAstNode[];390403 };391404405+/** Turns Codex's private file directive into the same links agents write explicitly. */406+function remarkCodexFileCitations() {407+ return (tree: MarkdownAstNode, file: { value: unknown }) => {408+ const source = String(file.value);409+ const restoreDirectiveSource = (node: MarkdownAstNode) => {410+ const start = node.position?.start.offset;411+ const end = node.position?.end.offset;412+ node.type = "text";413+ node.value =414+ start === undefined || end === undefined ? `:${node.name ?? ""}` : source.slice(start, end);415+ delete node.name;416+ delete node.attributes;417+ delete node.children;418+ };
- apps/web/src/components/ChatMarkdown.tsx lines 291 to 292OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -286,3 +288,5 @@286288 remarkGfm,287289 remarkGithubAlerts,288290 remarkNormalizeListItemIndentation,291+ remarkDirective,292+ remarkCodexFileCitations,
- apps/web/src/components/ChatMarkdown.tsx lines 1642 to 1643OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1637,3 +1639,5 @@16371639 lineBreaks = false,16381640 parseRawHtml = true,16391641 }: ChatMarkdownProps) {1642+ // Keep directive grammar isolated so ordinary chat Markdown retains its existing parse rules.1643+ const markdownText = useMemo(() => replaceCodexFileCitationsWithMarkdownLinks(text), [text]);
- apps/web/src/components/ChatMarkdown.tsx lines 387 to 387OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -383,3 +384,4 @@383384 type?: string;384385 meta?: unknown;385386 url?: string;387+ value?: string;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
Yash-Singh1(GitHub profile, opens in a new tab) changed the title
- apps/mobile/src/features/threads/ThreadFeed.tsx line 1388Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1126,15 +1255,6 @@1126- skills={props.skills}1127- textStyle={styles.nativeTextStyle}1128- onLinkPress={props.onMarkdownLinkPress}1129- renderImage={props.renderMarkdownImage}1130- />1131- ) : (1132- <Markdown1133- options={{ gfm: true }}1134- renderers={styles.renderers}1135- styles={styles.styles}1136- theme={styles.theme}1137- >1138- {message.text}1139- </Markdown>1140- )1255+ {renderedText.trim().length > 0 ? (1256+ <AssistantMarkdownContent1257+ markdown={renderedText}1258+ markdownStyles={styles}1259+ onLinkPress={props.onMarkdownLinkPress}1260+ onUseArtifactTemplate={props.onUseArtifactTemplate}apps/web/src/components/ChatView.tsx line 2986OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2981,3 +2981,6 @@29812981 focusComposer();29822982 });29832983 }, [focusComposer]);2984+ const useArtifactTemplate = useCallback(2985+ (prompt: string) => {2986+ if (composerRef.current?.insertTextAtEnd(prompt, { ensureLeadingBoundary: true })) {
- apps/web/src/components/ChatMarkdown.tsx lines 2040 to 2045OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1944,3 +2037,9 @@1944203719452038 return {19462039 p({ node: _node, children, ...props }) {2040+ const artifactTemplate = artifactTemplateFromMarkdownChildren(children);2041+ if (artifactTemplate) {2042+ return (2043+ <CodexArtifactTemplateCard template={artifactTemplate} onUse={onUseArtifactTemplate} />2044+ );2045+ }
- apps/web/src/components/ChatMarkdown.tsx lines 2040 to 2045OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1944,3 +2037,9 @@1944203719452038 return {19462039 p({ node: _node, children, ...props }) {2040+ const artifactTemplate = artifactTemplateFromMarkdownChildren(children);2041+ if (artifactTemplate) {2042+ return (2043+ <CodexArtifactTemplateCard template={artifactTemplate} onUse={onUseArtifactTemplate} />2044+ );2045+ }
- apps/web/src/components/ChatMarkdown.tsx lines 236 to 242Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -184,0 +209,21 @@209+ image: { label: "Image template", Icon: ImageIcon },210+ email: { label: "Email template", Icon: MailIcon },211+ slack: { label: "Slack template", Icon: MessageSquareIcon },212+} satisfies Record<213+ CodexArtifactTemplateKind,214+ { readonly label: string; readonly Icon: LucideIcon }215+>;216+217+function CodexArtifactTemplateCard(props: {218+ readonly template: CodexArtifactTemplate;219+ readonly onUse?: ((template: CodexArtifactTemplate) => void) | undefined;220+}) {221+ const presentation = ARTIFACT_TEMPLATE_PRESENTATIONS[props.template.artifactKind];222+223+ return (224+ <div225+ role="group"226+ aria-label={`${props.template.displayName} template`}227+ className="chat-markdown-artifact-template my-[0.65rem] flex w-full min-w-0 items-center gap-3 rounded-xl border border-border/70 bg-card/60 px-3 py-2.5 text-foreground shadow-xs"228+ data-artifact-kind={props.template.artifactKind}229+ data-skill-name={props.template.skillName}
- apps/web/src/components/ChatMarkdown.tsx line 229OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -184,0 +209,21 @@209+ image: { label: "Image template", Icon: ImageIcon },210+ email: { label: "Email template", Icon: MailIcon },211+ slack: { label: "Slack template", Icon: MessageSquareIcon },212+} satisfies Record<213+ CodexArtifactTemplateKind,214+ { readonly label: string; readonly Icon: LucideIcon }215+>;216+217+function CodexArtifactTemplateCard(props: {218+ readonly template: CodexArtifactTemplate;219+ readonly onUse?: ((template: CodexArtifactTemplate) => void) | undefined;220+}) {221+ const presentation = ARTIFACT_TEMPLATE_PRESENTATIONS[props.template.artifactKind];222+223+ return (224+ <div225+ role="group"226+ aria-label={`${props.template.displayName} template`}227+ className="chat-markdown-artifact-template my-[0.65rem] flex w-full min-w-0 items-center gap-3 rounded-xl border border-border/70 bg-card/60 px-3 py-2.5 text-foreground shadow-xs"228+ data-artifact-kind={props.template.artifactKind}229+ data-markdown-copy={`${props.template.displayName} (${presentation.label})`}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/web/src/components/ChatMarkdown.tsx lines 367 to 367OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -284,3 +364,4 @@284364285365 const CHAT_MARKDOWN_REMARK_PLUGINS = [286366 remarkGfm,367+ remarkCodexDirectives,
- apps/web/src/components/ChatMarkdown.tsx lines 376 to 376Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -286,3 +366,4 @@286366 remarkGfm,287367 remarkGithubAlerts,288368 remarkNormalizeListItemIndentation,369+ remarkCodexDirectives,
- packages/client-runtime/src/codexFileCitationMarkdown.ts line 50OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +30,21 @@30+ readonly start: number;31+ readonly end: number;32+ readonly markdown?: string;33+ readonly artifactTemplate?: CodexArtifactTemplate;34+}35+36+export type CodexArtifactTemplateMarkdownSegment =37+ | { readonly kind: "markdown"; readonly markdown: string; readonly sourceOffset: number }38+ | {39+ readonly kind: "artifact-template";40+ readonly sourceOffset: number;41+ readonly template: CodexArtifactTemplate;42+ };43+44+interface CodexDirectiveKinds {45+ readonly artifactTemplates: boolean;46+ readonly fileCitations: boolean;47+}48+49+const directiveParser = unified().use(remarkParse).use(remarkDirective).freeze();50+
- apps/web/src/components/chat/MessagesTimeline.logic.ts lines 444 to 444OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -440,4 +441,4 @@440441 }) {441442 const hasText = text !== null && text.trim().length > 0;442443 return {443- text: hasText ? text : null,444+ text: hasText ? replaceCodexFileCitationsWithMarkdownLinks(text) : null,
- apps/web/src/components/chat/MessagesTimeline.logic.ts lines 444 to 444OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -440,4 +441,4 @@440441 }) {441442 const hasText = text !== null && text.trim().length > 0;442443 return {443- text: hasText ? text : null,444+ text: hasText ? codexMarkdownForCopy(text) : null,
- apps/mobile/src/features/threads/ThreadFeed.tsx line 1149OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1026,3 +1143,7 @@10261143 if (entry.type === "message") {10271144 const { message } = entry;10281145 const isUser = message.role === "user";1146+ const renderedText = message.text;1147+ const copiedText = isUser1148+ ? renderedText1149+ : replaceCodexFileCitationsWithMarkdownLinks(renderedText);
- apps/web/src/components/chat/MessagesTimeline.logic.ts lines 442 to 447OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -440,6 +441,6 @@440441 }) {441442 const hasText = text !== null && text.trim().length > 0;442443 return {443- text: hasText ? text : null,444+ text: hasText ? codexMarkdownForCopy(text) : null,444445 visible: showCopyButton && hasText && !streaming,445446 };
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) removed labels📱 Native Change
- apps/web/src/components/ChatMarkdown.tsx line 1813OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1728,6 +1810,4 @@17281810 ];17291811 return buildFileLinkParentSuffixByPath(filePaths);17301812 }, [inlineCodeFileLinkMetaByText, markdownFileLinkMetaByHref]);1731- const markdownUrlTransform = useCallback((href: string) => {1732- return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);1733- }, []);1813+ const markdownUrlTransform = useCallback(apps/web/src/components/ChatMarkdown.tsx line 1816OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1728,6 +1810,7 @@17281810 ];17291811 return buildFileLinkParentSuffixByPath(filePaths);17301812 }, [inlineCodeFileLinkMetaByText, markdownFileLinkMetaByHref]);1731- const markdownUrlTransform = useCallback((href: string) => {1732- return rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href);1733- }, []);1813+ const markdownUrlTransform = useCallback(1814+ (href: string) => rewriteMarkdownFileUriHref(href) ?? defaultUrlTransform(href),1815+ [],1816+ );packages/client-runtime/src/codexMarkdownDirectives.ts line 266Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +234,21 @@234+function attachCodexDirectives(this: Processor) {235+ const data = this.data();236+ const micromarkExtensions = data.micromarkExtensions ?? (data.micromarkExtensions = []);237+ const fromMarkdownExtensions = data.fromMarkdownExtensions ?? (data.fromMarkdownExtensions = []);238+ micromarkExtensions.push(CODEX_DIRECTIVE_SYNTAX);239+ fromMarkdownExtensions.push(CODEX_DIRECTIVE_FROM_MARKDOWN);240+241+ return (tree: unknown, file: MarkdownFile) => {242+ transformCodexDirectives(tree as MarkdownAstNode, String(file.value));243+ };244+}245+246+export const remarkCodexDirectives = attachCodexDirectives;247+248+const directiveParser = unified().use(remarkParse).use(remarkCodexDirectives).freeze();249+250+function parseCodexMarkdown(markdown: string): MarkdownAstNode {251+ return directiveParser.runSync(directiveParser.parse(markdown), {252+ value: markdown,253+ }) as MarkdownAstNode;254+}packages/client-runtime/src/codexMarkdownDirectives.ts line 171Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +139,21 @@139+ };140+}141+142+const CODEX_DIRECTIVE_SYNTAX = codexDirectiveSyntax();143+const CODEX_DIRECTIVE_FROM_MARKDOWN = directiveFromMarkdown();144+145+function sourceForNode(node: MarkdownAstNode, source: string): string {146+ const start = node.position?.start.offset;147+ const end = node.position?.end.offset;148+ return start === undefined || end === undefined ? "" : source.slice(start, end);149+}150+151+function restoreTextDirective(node: MarkdownAstNode, source: string): void {152+ node.type = "text";153+ node.value = sourceForNode(node, source) || `:${node.name ?? ""}`;154+ delete node.name;155+ delete node.attributes;156+ delete node.url;157+ delete node.data;158+ delete node.children;159+}apps/web/src/components/ChatView.tsx line 3149Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -3055,3 +3057,19 @@30553057 focusComposer();30563058 });30573059 }, [focusComposer]);3060+ const useArtifactTemplate = useCallback(3061+ (template: CodexArtifactTemplate) => {3062+ const composer = composerRef.current;3063+ if (!composer) return;3064+3065+ const currentDraft = composer.getSendContext().prompt;3066+ const prompt = codexArtifactTemplatePromptToAppend(currentDraft, template);3067+ if (prompt !== null) {3068+ composer.insertTextAtEnd(prompt, {3069+ ensureLeadingBoundary: true,3070+ });3071+ }3072+ scheduleComposerFocus();3073+ },3074+ [composerRef, scheduleComposerFocus],3075+ );
- apps/web/src/components/chat/MessagesTimeline.tsx lines 1263 to 1263Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -1212,3 +1219,4 @@12121219 isStreaming={Boolean(row.message.streaming)}12131220 lineBreaks={shouldPreserveAssistantLineBreaks(messageText)}12141221 skills={ctx.skills}1222+ onUseArtifactTemplate={ctx.onUseArtifactTemplate}
- apps/web/src/components/ChatMarkdown.tsx lines 376 to 376Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -286,3 +365,4 @@286365 remarkGfm,287366 remarkGithubAlerts,288367 remarkNormalizeListItemIndentation,368+ remarkCodexDirectives,
Sign in to join this conversation.


Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
f51bc45c-4572-425f-aacf-b003d4db402eYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Warning
Your free Security trial is over. An organization admin can activate Security or dismiss this notice.
Comment `@coderabbitai help` to get the list of available commands.