Skip to content

Commit 92b91bc

Browse files
committed
fix(web,mobile): render Codex artifact templates in chat
- parse `::artifact-template` directives into interactive cards - let web and mobile insert the template prompt into the composer
1 parent c5ce1fd commit 92b91bc

13 files changed

Lines changed: 855 additions & 38 deletions

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
import {
3+
appendCodexArtifactTemplateUsePrompt,
4+
type CodexArtifactTemplate,
5+
} from "@t3tools/client-runtime/codex-artifact-templates";
26
import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
37
import { useKeyboardChatComposerInset, useKeyboardScrollToEnd } from "@legendapp/list/keyboard";
48
import type { LegendListRef } from "@legendapp/list/react-native";
@@ -555,6 +559,20 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
555559
composerEditorRef.current?.blur();
556560
}, []);
557561

562+
const handleUseArtifactTemplate = useCallback(
563+
(template: CodexArtifactTemplate) => {
564+
const nextDraft = appendCodexArtifactTemplateUsePrompt(props.draftMessage, template);
565+
if (nextDraft !== props.draftMessage) {
566+
props.onChangeDraftMessage(nextDraft);
567+
}
568+
requestAnimationFrame(() => {
569+
composerEditorRef.current?.focus();
570+
composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length });
571+
});
572+
},
573+
[props.draftMessage, props.onChangeDraftMessage],
574+
);
575+
558576
const handleScrollToEnd = useCallback(() => {
559577
void Haptics.selectionAsync();
560578
void scrollMessageToEnd({ animated: true, closeKeyboard: false }).catch(() => {
@@ -629,6 +647,7 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
629647
onHeaderMaterialVisibilityChange={props.onHeaderMaterialVisibilityChange}
630648
onEndFollowEnabledChange={setEndFollowEnabled}
631649
skills={selectedProviderSkills}
650+
onUseArtifactTemplate={handleUseArtifactTemplate}
632651
loadEarlier={props.loadEarlier ?? null}
633652
/>
634653
</View>

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 137 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as Haptics from "expo-haptics";
22
import { KeyboardAwareLegendList } from "@legendapp/list/keyboard";
33
import { type LegendListRef } from "@legendapp/list/react-native";
44
import type { EnvironmentId, MessageId, ThreadId, TurnId } from "@t3tools/contracts";
5+
import type { CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
56
import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";
7+
import { splitCodexArtifactTemplateMarkdown } from "@t3tools/client-runtime/codex-markdown-directives";
68
import { CHAT_LIST_ANCHOR_OFFSET, resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList";
79
import { formatElapsed } from "@t3tools/shared/orchestrationTiming";
8-
import { SymbolView } from "../../components/AppSymbol";
10+
import { SymbolView, type AppSymbolName } from "../../components/AppSymbol";
911
import { HeaderHeightContext } from "@react-navigation/elements";
1012
import { useNavigation } from "@react-navigation/native";
1113
import {
@@ -173,6 +175,7 @@ export interface ThreadFeedProps {
173175
readonly onHeaderMaterialVisibilityChange?: (visible: boolean) => void;
174176
readonly onEndFollowEnabledChange?: (enabled: boolean) => void;
175177
readonly skills?: ReadonlyArray<SelectableMarkdownSkill>;
178+
readonly onUseArtifactTemplate?: (template: CodexArtifactTemplate) => void;
176179
/** Non-null when older turns exist beyond the loaded window. */
177180
readonly loadEarlier?: {
178181
readonly loading: boolean;
@@ -435,6 +438,128 @@ const MarkdownExternalLink = memo(function MarkdownExternalLink(props: {
435438
);
436439
});
437440

441+
const ARTIFACT_TEMPLATE_LABEL_BY_KIND: Record<CodexArtifactTemplate["artifactKind"], string> = {
442+
document: "Document template",
443+
presentation: "Presentation template",
444+
spreadsheet: "Spreadsheet template",
445+
site: "Site template",
446+
"google-docs": "Google Doc template",
447+
"google-slides": "Google Slides template",
448+
"google-sheets": "Google Sheet template",
449+
image: "Image template",
450+
email: "Email template",
451+
slack: "Slack template",
452+
};
453+
454+
const ARTIFACT_TEMPLATE_SYMBOL_BY_KIND: Record<
455+
CodexArtifactTemplate["artifactKind"],
456+
AppSymbolName
457+
> = {
458+
document: "doc.text",
459+
presentation: "chart.bar.xaxis",
460+
spreadsheet: "chart.bar.xaxis",
461+
site: "safari",
462+
"google-docs": "doc.text",
463+
"google-slides": "chart.bar.xaxis",
464+
"google-sheets": "chart.bar.xaxis",
465+
image: "camera",
466+
email: "text.bubble",
467+
slack: "text.bubble",
468+
};
469+
470+
function ArtifactTemplateCard(props: {
471+
readonly template: CodexArtifactTemplate;
472+
readonly onUse?: ((template: CodexArtifactTemplate) => void) | undefined;
473+
}) {
474+
return (
475+
<View
476+
className="my-2 min-w-0 flex-row items-center gap-3 rounded-2xl border border-border bg-card px-3 py-3"
477+
>
478+
<View className="relative h-10 w-10 shrink-0 items-center justify-center rounded-xl border border-border bg-subtle">
479+
<SymbolView
480+
name={ARTIFACT_TEMPLATE_SYMBOL_BY_KIND[props.template.artifactKind]}
481+
size={20}
482+
tintColorClassName="accent-foreground-muted"
483+
type="monochrome"
484+
/>
485+
<View className="absolute -right-1 -bottom-1 h-4 w-4 items-center justify-center rounded-full bg-fuchsia-500">
486+
<SymbolView
487+
name={{ ios: "sparkles", android: "auto_awesome" }}
488+
size={9}
489+
tintColor="white"
490+
type="monochrome"
491+
/>
492+
</View>
493+
</View>
494+
<View className="min-w-0 flex-1">
495+
<Text className="font-t3-bold text-sm text-foreground" numberOfLines={1}>
496+
{props.template.displayName}
497+
</Text>
498+
<Text className="text-xs text-foreground-muted">
499+
{ARTIFACT_TEMPLATE_LABEL_BY_KIND[props.template.artifactKind]}
500+
</Text>
501+
</View>
502+
{props.onUse ? (
503+
<Pressable
504+
accessibilityRole="button"
505+
accessibilityLabel={`Use ${props.template.displayName} template`}
506+
className="min-h-9 justify-center rounded-lg border border-border bg-subtle px-3 active:opacity-65"
507+
onPress={() => props.onUse?.(props.template)}
508+
>
509+
<Text className="font-t3-bold text-xs text-foreground">Use template</Text>
510+
</Pressable>
511+
) : null}
512+
</View>
513+
);
514+
}
515+
516+
const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
517+
readonly markdown: string;
518+
readonly markdownStyles: MarkdownStyleSet;
519+
readonly onLinkPress: (href: string) => void;
520+
readonly onUseArtifactTemplate?: ((template: CodexArtifactTemplate) => void) | undefined;
521+
readonly renderImage: MarkdownImageRenderer;
522+
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
523+
}) {
524+
const segments = useMemo(
525+
() => splitCodexArtifactTemplateMarkdown(props.markdown),
526+
[props.markdown],
527+
);
528+
529+
return segments.map((segment) =>
530+
segment.kind === "artifact-template" ? (
531+
<ArtifactTemplateCard
532+
key={`artifact-template:${segment.sourceOffset}`}
533+
template={segment.template}
534+
onUse={props.onUseArtifactTemplate}
535+
/>
536+
) : segment.markdown.trim().length > 0 ? (
537+
hasNativeSelectableMarkdownText() ? (
538+
<SelectableMarkdownText
539+
key={`markdown:${segment.sourceOffset}`}
540+
markdown={segment.markdown}
541+
plugins={CODEX_FILE_CITATION_MARKDOWN_PLUGINS}
542+
skills={props.skills}
543+
textStyle={props.markdownStyles.nativeTextStyle}
544+
onLinkPress={props.onLinkPress}
545+
renderImage={props.renderImage}
546+
/>
547+
) : (
548+
<Markdown
549+
key={`markdown:${segment.sourceOffset}`}
550+
options={{ gfm: true }}
551+
plugins={CODEX_FILE_CITATION_MARKDOWN_PLUGINS}
552+
renderers={props.markdownStyles.renderers}
553+
styles={props.markdownStyles.styles}
554+
theme={props.markdownStyles.theme}
555+
>
556+
{segment.markdown}
557+
</Markdown>
558+
)
559+
) : null,
560+
);
561+
});
562+
438563
function MarkdownCodeBlock(props: {
439564
readonly backgroundColor: string;
440565
readonly borderColor: string;
@@ -966,7 +1091,7 @@ function useMarkdownStyles(
9661091

9671092
function renderFeedEntry(
9681093
info: { item: ThreadFeedEntry; index: number },
969-
props: Pick<ThreadFeedProps, "environmentId" | "skills"> & {
1094+
props: Pick<ThreadFeedProps, "environmentId" | "onUseArtifactTemplate" | "skills"> & {
9701095
readonly copiedRowId: string | null;
9711096
readonly expandedWorkRows: Record<string, boolean>;
9721097
readonly terminalAssistantMessageIds: ReadonlySet<string>;
@@ -1128,26 +1253,14 @@ function renderFeedEntry(
11281253
{...(enterAnimated ? { entering: FadeIn.duration(220) } : {})}
11291254
>
11301255
{renderedText.trim().length > 0 ? (
1131-
hasNativeSelectableMarkdownText() ? (
1132-
<SelectableMarkdownText
1133-
markdown={renderedText}
1134-
plugins={CODEX_FILE_CITATION_MARKDOWN_PLUGINS}
1135-
skills={props.skills}
1136-
textStyle={styles.nativeTextStyle}
1137-
onLinkPress={props.onMarkdownLinkPress}
1138-
renderImage={props.renderMarkdownImage}
1139-
/>
1140-
) : (
1141-
<Markdown
1142-
options={{ gfm: true }}
1143-
plugins={CODEX_FILE_CITATION_MARKDOWN_PLUGINS}
1144-
renderers={styles.renderers}
1145-
styles={styles.styles}
1146-
theme={styles.theme}
1147-
>
1148-
{renderedText}
1149-
</Markdown>
1150-
)
1256+
<AssistantMarkdownContent
1257+
markdown={renderedText}
1258+
markdownStyles={styles}
1259+
onLinkPress={props.onMarkdownLinkPress}
1260+
onUseArtifactTemplate={props.onUseArtifactTemplate}
1261+
renderImage={props.renderMarkdownImage}
1262+
skills={props.skills}
1263+
/>
11511264
) : null}
11521265
{attachments.map((attachment) => {
11531266
return (
@@ -2034,6 +2147,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20342147
reviewCommentBubbleWidth,
20352148
userBubbleMaxWidth,
20362149
skills: props.skills,
2150+
onUseArtifactTemplate: props.onUseArtifactTemplate,
20372151
}),
20382152
[
20392153
copiedRowId,
@@ -2053,6 +2167,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
20532167
onToggleWorkGroup,
20542168
onToggleWorkRow,
20552169
props.environmentId,
2170+
props.onUseArtifactTemplate,
20562171
props.skills,
20572172
renderMarkdownImage,
20582173
],

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,64 @@ describe("ChatMarkdown file option chips", () => {
265265
});
266266
});
267267

268+
const ARTIFACT_TEMPLATE_DIRECTIVE =
269+
'::artifact-template{skill_name="artifact-template-hello-world" skill_directory="/Users/test/.codex/skills/artifact-template-hello-world" display_name="Hello World" artifact_kind="document"}';
270+
271+
describe("ChatMarkdown artifact-template cards", () => {
272+
it.each([true, false])("renders the Codex result card with parseRawHtml=%s", (parseRawHtml) => {
273+
const html = renderToStaticMarkup(
274+
<ChatMarkdown
275+
cwd="/tmp/project"
276+
text={ARTIFACT_TEMPLATE_DIRECTIVE}
277+
parseRawHtml={parseRawHtml}
278+
onUseArtifactTemplate={() => undefined}
279+
/>,
280+
);
281+
282+
expect(html).not.toContain("::artifact-template");
283+
expect(html).toContain("chat-markdown-artifact-template");
284+
expect(html).toContain('data-artifact-kind="document"');
285+
expect(html).toContain('data-skill-name="artifact-template-hello-world"');
286+
expect(html).toContain("Hello World");
287+
expect(html).toContain("Document template");
288+
expect(html).toContain("Use template");
289+
expect(html).not.toContain("<p><div");
290+
});
291+
292+
it("renders a passive card outside a composer-backed timeline", () => {
293+
const html = renderToStaticMarkup(
294+
<ChatMarkdown cwd="/tmp/project" text={ARTIFACT_TEMPLATE_DIRECTIVE} />,
295+
);
296+
297+
expect(html).toContain("chat-markdown-artifact-template");
298+
expect(html).not.toContain("Use template");
299+
});
300+
301+
it("leaves malformed and unfinished artifact-template directives literal", () => {
302+
const malformed =
303+
'::artifact-template{skill_name="artifact-template-hello-world" display_name="Hello World" artifact_kind="document"}';
304+
const unfinished = ARTIFACT_TEMPLATE_DIRECTIVE.slice(0, -1);
305+
306+
for (const text of [malformed, unfinished]) {
307+
const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={text} />);
308+
expect(html).toContain("::artifact-template");
309+
expect(html).not.toContain("chat-markdown-artifact-template");
310+
}
311+
});
312+
313+
it("preserves artifact-template examples inside code", () => {
314+
const html = renderToStaticMarkup(
315+
<ChatMarkdown
316+
cwd="/tmp/project"
317+
text={`\`${ARTIFACT_TEMPLATE_DIRECTIVE}\`\n\n\`\`\`text\n${ARTIFACT_TEMPLATE_DIRECTIVE}\n\`\`\``}
318+
/>,
319+
);
320+
321+
expect(html.match(/::artifact-template/g)).toHaveLength(2);
322+
expect(html).not.toContain("chat-markdown-artifact-template");
323+
});
324+
});
325+
268326
describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {
269327
it("uses the browser when it is the only available primary action", () => {
270328
expect(

0 commit comments

Comments
 (0)