Skip to content
Closed
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
17 changes: 13 additions & 4 deletions apps/mobile/src/features/threads/ThreadFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
type OrchestrationV2ProjectedTurnItem,
type RunId,
} from "@t3tools/contracts";
import { renderCodexCitationsAsMarkdown } from "@t3tools/shared/codexCitations";
import { renderAssistantCitationsAsText } from "@t3tools/shared/assistantCitations";
import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard";
import { parseThreadLinkHref, resolveThreadLinkRef } from "@t3tools/shared/threadLinks";
Expand Down Expand Up @@ -59,6 +60,7 @@ import {
import { resolveViewedImageAsset } from "@t3tools/client-runtime/work-log/presentation";
import {
renderCodexInlineDirectivesAsMarkdown,
renderCodexDirectivesForCopy,
splitCodexArtifactTemplateMarkdown,
} from "@t3tools/shared/codexMarkdownDirectives";
import { CHAT_LIST_ANCHOR_OFFSET, resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";
Expand Down Expand Up @@ -798,14 +800,19 @@ function ArtifactTemplateCard(props: {

const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
readonly markdown: string;
readonly isStreaming: boolean;
readonly environmentId: EnvironmentId;
readonly markdownStyles: MarkdownStyleSet;
readonly linkHandlers: MarkdownLinkHandlers;
readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
readonly renderImage: MarkdownImageRenderer;
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
}) {
const liveMarkdown = useLiveThreadLinkLabels(props.markdown, props.environmentId);
const citationMarkdown = useMemo(
() => renderCodexCitationsAsMarkdown(props.markdown, { isStreaming: props.isStreaming }),
[props.markdown, props.isStreaming],
);
const liveMarkdown = useLiveThreadLinkLabels(citationMarkdown, props.environmentId);
const segments = useMemo(() => splitCodexArtifactTemplateMarkdown(liveMarkdown), [liveMarkdown]);

return segments.map((segment) => {
Expand Down Expand Up @@ -994,7 +1001,7 @@ function renderFeedEntry(
readonly markdownLinkHandlers: MarkdownLinkHandlers;
readonly renderMarkdownImage: MarkdownImageRenderer;
readonly renderViewedImage: MarkdownImageRenderer;
readonly renderReasoning: (text: string) => ReactNode;
readonly renderReasoning: (text: string, isStreaming: boolean) => ReactNode;
readonly iconSubtleColor: string | import("react-native").ColorValue;
readonly screenColor: string;
readonly userBubbleColor: string | import("react-native").ColorValue;
Expand Down Expand Up @@ -1380,6 +1387,7 @@ function renderFeedEntry(
<MarkdownImageAvailableWidthContext value={props.markdownContentWidth}>
<AssistantMarkdownContent
markdown={renderedText}
isStreaming={message.streaming}
environmentId={props.environmentId}
markdownStyles={styles}
linkHandlers={props.markdownLinkHandlers}
Expand Down Expand Up @@ -1416,7 +1424,7 @@ function renderFeedEntry(
<View className="mt-1 flex-row items-center gap-1">
<CopyTextButton
accessibilityLabel="Copy message"
text={renderedText}
text={renderCodexDirectivesForCopy(renderCodexCitationsAsMarkdown(renderedText))}
tintColor={iconSubtleColor}
buttonSize={28}
iconSize={13}
Expand Down Expand Up @@ -1964,9 +1972,10 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
) : null;
};
const markdownStyles = useMarkdownStyles();
const renderReasoning = (text: string) => (
const renderReasoning = (text: string, isStreaming: boolean) => (
<AssistantMarkdownContent
markdown={text}
isStreaming={isStreaming}
environmentId={props.environmentId}
markdownStyles={markdownStyles.assistant}
linkHandlers={markdownLinkHandlers}
Expand Down
26 changes: 22 additions & 4 deletions apps/mobile/src/features/threads/thread-work-log.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { renderCodexCitationsAsMarkdown } from "@t3tools/shared/codexCitations";
import { renderCodexDirectivesForCopy } from "@t3tools/shared/codexMarkdownDirectives";
import { Button } from "../../components/ui/Button";
import { SubagentStatusDot } from "./SubagentStatusDot";
import { ThreadSubagentGroup } from "./thread-subagent-group";
Expand Down Expand Up @@ -487,7 +489,7 @@ interface ThreadWorkLogProps {
/** `groupId` is the group a lone call stands in for; it opens and closes with the row. */
readonly onToggleRow: (rowId: string, anchorKey: string, groupId?: string) => void;
readonly renderImage: MarkdownImageRenderer;
readonly renderReasoning: (text: string) => ReactNode;
readonly renderReasoning: (text: string, isStreaming: boolean) => ReactNode;
readonly onPressPreview: (source: FilePreviewSource) => void;
}

Expand Down Expand Up @@ -525,7 +527,12 @@ export function ThreadWorkLog(props: ThreadWorkLogProps) {
return (
<ScrollView nestedScrollEnabled className="ml-7 max-h-96 py-1">
{props.activities.map((row) => (
<View key={row.id}>{props.renderReasoning(row.workEntry.detail ?? "")}</View>
<View key={row.id}>
{props.renderReasoning(
row.workEntry.detail ?? "",
row.projectedItem.item.type === "reasoning" && row.projectedItem.item.streaming,
)}
</View>
))}
</ScrollView>
);
Expand Down Expand Up @@ -1056,7 +1063,18 @@ const ThreadWorkLogRow = function ThreadWorkLogRow(
props.onToggleRow(row.id, props.anchorKey, row.groupStandIn?.groupId);
}
}}
onLongPress={() => props.onCopyRow(row.id, row.getCopyText())}
onLongPress={() =>
props.onCopyRow(
row.id,
reasoning
? renderCodexDirectivesForCopy(
renderCodexCitationsAsMarkdown(row.getCopyText(), {
isStreaming: reasoning.streaming,
}),
)
: row.getCopyText(),
)
}
>
{row.live && !expanded ? (
<ShimmeringWorkContent
Expand Down Expand Up @@ -1198,7 +1216,7 @@ const ThreadWorkLogRow = function ThreadWorkLogRow(
contentContainerStyle={{ paddingRight: 8 }}
>
{reasoning ? (
props.renderReasoning(reasoning.text)
props.renderReasoning(reasoning.text, reasoning.streaming)
) : call ? (
[
call.command,
Expand Down
2 changes: 2 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
"compression": "^1.8.2",
"oxc-transform-react": "~0.152.0",
"playwright": "1.60.0",
"remark-parse": "catalog:",
"remark-rehype": "catalog:",
"tailwindcss": "^4.0.0",
"unified": "catalog:",
"vite": "catalog:",
Expand Down
93 changes: 93 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,99 @@ describe("resolveAssistantMessageCopyState", () => {
visible: true,
});
});

it("copies readable citations without changing unrelated Markdown or literal examples", () => {
const example = "\uE200cite\uE202excluded\uE201";
const before = [
"# Report",
"",
"🦊 \\*literal\\* &amp; **bold** ",
`Example: \`${example}\` and [source ${example}](<https://example.com>).`,
`<a href="https://example.com">${example}</a>`,
"",
"```md",
example,
"```",
"",
"> Evidence ",
].join("\n");
const after = "\n\n- untouched spacing\n";
const text = `${before}\uE200cite\uE202__opaque_id__\uE201${after}`;
const input = Object.freeze({ text, showCopyButton: true, streaming: false });

expect(resolveAssistantMessageCopyState(input)).toEqual({
text: `${before}[Source 1: \\_\\_opaque\\_id\\_\\_\\]${after}`,
visible: true,
});
expect(input.text).toBe(text);
});

it("copies recovered nested list citations at their original positions", () => {
const text = [
"🦊 Before.",
"",
"- First **evidence** \uE200cite\uE202turn0view0\uE201.",
"",
" - Second \uE200cite\uE202turn1view0\uE201.",
"",
"After \uE200cite\uE202turn0view0\uE201.",
].join("\r\n");

expect(
resolveAssistantMessageCopyState({ text, showCopyButton: true, streaming: false }),
).toEqual({
text: [
"🦊 Before.",
"",
"- First **evidence** [Source 1: turn0view0\\].",
"",
" - Second [Source 2: turn1view0\\].",
"",
"After [Source 1: turn0view0\\].",
].join("\r\n"),
visible: true,
});
});

it("keeps source numbering across artifacts without numbering directive contents", () => {
const literal = "\uE200cite\uE202excluded\uE201";
const text = [
`Created :codex-file-citation{path="outputs/${literal}.txt" purpose="output"}.`,
"",
"Before \uE200cite\uE202turn0view0\uE201.",
"",
`::artifact-template{skill_name="artifact-template-report" skill_directory="/skills/report" display_name="${literal}" artifact_kind="document"}`,
"",
"After \uE200cite\uE202turn1search2\uE202turn0view0\uE202L8-L13\uE201.",
].join("\n");

expect(
resolveAssistantMessageCopyState({ text, showCopyButton: true, streaming: false }),
).toEqual({
text: [
`Created [${literal}.txt](<outputs/${literal}.txt>).`,
"",
"Before [Source 1: turn0view0\\].",
"",
`${literal} (Document template)`,
"",
"After [Source 2: turn1search2; Source 1: turn0view0; L8-L13\\].",
].join("\n"),
visible: true,
});
});

it("leaves streaming copy hidden and retains an unfinished marker when stopped", () => {
const tail = "\uE200cite\uE202next";
const text = `Evidence \uE200cite\uE202turn0view0\uE201 and ${tail}`;

expect(
resolveAssistantMessageCopyState({ text, showCopyButton: true, streaming: true }),
).toEqual({ text, visible: false });
expect(
resolveAssistantMessageCopyState({ text, showCopyButton: true, streaming: false }),
).toEqual({ text: `Evidence [Source 1: turn0view0\\] and ${tail}`, visible: true });
});
});

describe("deriveMessagesTimelineRows", () => {
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/components/chat/MessagesTimeline.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
collectAssistantCitations,
} from "@t3tools/shared/assistantCitations";
import { shallow } from "zustand/vanilla/shallow";
import { renderCodexCitationsAsMarkdown } from "@t3tools/shared/codexCitations";
import { renderCodexDirectivesForCopy } from "@t3tools/shared/codexMarkdownDirectives";
import {
commandDisplayText,
Expand Down Expand Up @@ -667,7 +668,11 @@ export function resolveAssistantMessageCopyState({
const hasText = text !== null && text.trim().length > 0;
const visible = showCopyButton && hasText && !streaming;
return {
text: hasText ? (visible ? renderCodexDirectivesForCopy(text) : text) : null,
text: hasText
? visible
? renderCodexDirectivesForCopy(renderCodexCitationsAsMarkdown(text))
: text
: null,
visible,
};
}
Expand Down
42 changes: 40 additions & 2 deletions apps/web/src/markdown-incremental.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { CHAT_MARKDOWN_REHYPE_PLUGINS } from "@t3tools/shared/markdownPipeline";
import {
CHAT_MARKDOWN_REHYPE_PLUGINS,
chatMarkdownRemarkPlugins,
} from "@t3tools/shared/markdownPipeline";
import type { Root } from "mdast";
import { renderToStaticMarkup } from "react-dom/server";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import type { Plugin } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified, type Plugin } from "unified";
import { describe, expect, it } from "vite-plus/test";

import { remarkCodexDirectives } from "@t3tools/shared/codexMarkdownDirectives";
Expand Down Expand Up @@ -47,6 +52,39 @@ function render(source: string, incremental?: Plugin<[], Root>, parsedSources?:
const prefix = "# Before\n\n```ts\nconst values = [1, 2];\n```\n\n";

describe("incremental Markdown parsing", () => {
it("preserves citation IDs and numbering across a cached closed fence", () => {
const incremental = createIncrementalMarkdownPlugin();
const parse = (source: string, plugin?: Plugin<[], Root>) => {
const processor = unified()
.use(remarkParse)
.use(chatMarkdownRemarkPlugins({ isStreaming: true }))
.use(plugin ? [plugin] : [])
.use(remarkRehype, { allowDangerousHtml: true })
.use(CHAT_MARKDOWN_REHYPE_PLUGINS);
return processor.runSync(processor.parse(source), { value: source });
};
type Node = { type: string; value?: string; children?: Node[] };
const text = (node: Node): string => node.value ?? node.children?.map(text).join("") ?? "";
const before = "Before \uE200cite\uE202turn0search1\uE201.\n\n```js\ncode\n```\n\n";
const after =
"After \uE200cite\uE202turn0search2\uE201, again \uE200cite\uE202turn0search1\uE201.";
for (const [tail, expectedTail] of [
["After", "After"],
[after, "After [Source 2: turn0search2], again [Source 1: turn0search1]."],
[
`${after} Also \uE200cite\uE202turn0search2\uE201.`,
"After [Source 2: turn0search2], again [Source 1: turn0search1]. Also [Source 2: turn0search2].",
],
]) {
const source = before + tail;
const full = parse(source);
const cached = parse(source, incremental);
expect(text(full)).toBe(`Before [Source 1: turn0search1].\ncode\n\n${expectedTail}`);
expect(text(cached)).toBe(text(full));
expect(cached).toEqual(full);
}
});

it("keeps the document prefix cached when list recovery parses contain fences", () => {
const source =
prefix +
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@
"types": "./src/codexArtifactTemplates.ts",
"import": "./src/codexArtifactTemplates.ts"
},
"./codexCitations": {
"types": "./src/codexCitations.ts",
"import": "./src/codexCitations.ts"
},
"./codexFileCitations": {
"types": "./src/codexFileCitations.ts",
"import": "./src/codexFileCitations.ts"
Expand Down
Loading
Loading