Skip to content
Open
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
2 changes: 2 additions & 0 deletions apps/web/src/components/chat/TimelineUserMessageRow.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}
/>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/components/chat/UserMessageContextChips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -564,6 +565,7 @@ function UserMessageReviewCommentCard({ comment }: { comment: ReviewCommentConte
skills={ctx.skills}
parseRawHtml={false}
lineBreaks
extraRemarkPlugins={CHAT_USER_MARKDOWN_REMARK_PLUGINS}
/>
</div>
)}
Expand Down
9 changes: 8 additions & 1 deletion apps/web/src/components/composerContextPresentation.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -291,7 +292,13 @@ function ComposerReviewCommentDetails({ comment }: { comment: ReviewCommentConte
{comment.sectionTitle} · {comment.rangeLabel}
</div>
</div>
{comment.text.trim() ? <ChatMarkdown text={comment.text.trim()} cwd={undefined} /> : null}
{comment.text.trim() ? (
<ChatMarkdown
text={comment.text.trim()}
cwd={undefined}
extraRemarkPlugins={CHAT_USER_MARKDOWN_REMARK_PLUGINS}
/>
) : null}
{comment.diff.trim() ? (
<div className="flex h-64 min-h-0 flex-col overflow-hidden rounded-md border border-border">
<ReadOnlySourcePreview name="review.diff" text={comment.diff} />
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/src/codexMarkdownDirectives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
]);
});
});
20 changes: 19 additions & 1 deletion packages/shared/src/codexMarkdownDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,33 @@ 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 = []);
micromarkExtensions.push(CODEX_DIRECTIVE_SYNTAX);
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;
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/markdownPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<p>` 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(
Expand Down
6 changes: 6 additions & 0 deletions packages/shared/src/threadFindText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
});
2 changes: 2 additions & 0 deletions packages/shared/src/threadFindText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Loading