@@ -10,12 +10,20 @@ import type {
1010 ThreadId ,
1111 TurnId ,
1212} from "@t3tools/contracts" ;
13+ import {
14+ codexArtifactTemplatePresentationLabel ,
15+ type CodexArtifactTemplate ,
16+ } from "@t3tools/client-runtime/codex-artifact-templates" ;
1317import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets" ;
1418import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments" ;
1519import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
1620import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images" ;
21+ import {
22+ renderCodexFileCitationsAsMarkdown ,
23+ splitCodexArtifactTemplateMarkdown ,
24+ } from "@t3tools/client-runtime/codex-markdown-directives" ;
1725import { CHAT_LIST_ANCHOR_OFFSET , resolveChatListAnchoredEndSpace } from "@t3tools/shared/chatList" ;
18- import { SymbolView } from "../../components/AppSymbol" ;
26+ import { SymbolView , type AppSymbolName } from "../../components/AppSymbol" ;
1927import { HeaderHeightContext } from "@react-navigation/elements" ;
2028import { useFocusEffect , useNavigation } from "@react-navigation/native" ;
2129import {
@@ -190,6 +198,7 @@ export interface ThreadFeedProps {
190198 readonly onHeaderMaterialVisibilityChange ?: ( visible : boolean ) => void ;
191199 readonly onEndFollowEnabledChange ?: ( enabled : boolean ) => void ;
192200 readonly skills ?: ReadonlyArray < SelectableMarkdownSkill > ;
201+ readonly onUseArtifactTemplate ?: ( template : CodexArtifactTemplate ) => void ;
193202 /** Non-null when older turns exist beyond the loaded window. */
194203 readonly loadEarlier ?: {
195204 readonly loading : boolean ;
@@ -568,6 +577,115 @@ const MarkdownExternalLink = memo(function MarkdownExternalLink(props: {
568577 ) ;
569578} ) ;
570579
580+ const ARTIFACT_TEMPLATE_SYMBOL_BY_KIND : Record <
581+ CodexArtifactTemplate [ "artifactKind" ] ,
582+ AppSymbolName
583+ > = {
584+ document : "doc.text" ,
585+ presentation : "chart.bar.xaxis" ,
586+ spreadsheet : "chart.bar.xaxis" ,
587+ site : "safari" ,
588+ "google-docs" : "doc.text" ,
589+ "google-slides" : "chart.bar.xaxis" ,
590+ "google-sheets" : "chart.bar.xaxis" ,
591+ image : "camera" ,
592+ email : "text.bubble" ,
593+ slack : "text.bubble" ,
594+ } ;
595+
596+ function ArtifactTemplateCard ( props : {
597+ readonly template : CodexArtifactTemplate ;
598+ readonly onUse ?: ( ( template : CodexArtifactTemplate ) => void ) | undefined ;
599+ } ) {
600+ return (
601+ < View className = "my-2 min-w-0 flex-row items-center gap-3 rounded-2xl border border-border bg-card px-3 py-3" >
602+ < View className = "relative h-10 w-10 shrink-0 items-center justify-center rounded-xl border border-border bg-subtle" >
603+ < SymbolView
604+ name = { ARTIFACT_TEMPLATE_SYMBOL_BY_KIND [ props . template . artifactKind ] }
605+ size = { 20 }
606+ tintColorClassName = "accent-foreground-muted"
607+ type = "monochrome"
608+ />
609+ < View className = "absolute -right-1 -bottom-1 h-4 w-4 items-center justify-center rounded-full bg-fuchsia-500" >
610+ < SymbolView
611+ name = { { ios : "sparkles" , android : "auto_awesome" } }
612+ size = { 9 }
613+ tintColor = "white"
614+ type = "monochrome"
615+ />
616+ </ View >
617+ </ View >
618+ < View className = "min-w-0 flex-1" >
619+ < Text className = "font-t3-bold text-sm text-foreground" numberOfLines = { 1 } >
620+ { props . template . displayName }
621+ </ Text >
622+ < Text className = "text-xs text-foreground-muted" >
623+ { codexArtifactTemplatePresentationLabel ( props . template . artifactKind ) }
624+ </ Text >
625+ </ View >
626+ { props . onUse ? (
627+ < Pressable
628+ accessibilityRole = "button"
629+ accessibilityLabel = { `Use ${ props . template . displayName } template` }
630+ className = "min-h-9 justify-center rounded-lg border border-border bg-subtle px-3 active:opacity-65"
631+ onPress = { ( ) => props . onUse ?.( props . template ) }
632+ >
633+ < Text className = "font-t3-bold text-xs text-foreground" > Use template</ Text >
634+ </ Pressable >
635+ ) : null }
636+ </ View >
637+ ) ;
638+ }
639+
640+ const AssistantMarkdownContent = memo ( function AssistantMarkdownContent ( props : {
641+ readonly markdown : string ;
642+ readonly markdownStyles : MarkdownStyleSet ;
643+ readonly onLinkPress : ( href : string ) => void ;
644+ readonly onUseArtifactTemplate ?: ( ( template : CodexArtifactTemplate ) => void ) | undefined ;
645+ readonly renderImage : MarkdownImageRenderer ;
646+ readonly skills ?: ReadonlyArray < SelectableMarkdownSkill > | undefined ;
647+ } ) {
648+ const segments = useMemo (
649+ ( ) => splitCodexArtifactTemplateMarkdown ( props . markdown ) ,
650+ [ props . markdown ] ,
651+ ) ;
652+
653+ return segments . map ( ( segment ) => {
654+ if ( segment . kind === "artifact-template" ) {
655+ return (
656+ < ArtifactTemplateCard
657+ key = { `artifact-template:${ segment . sourceOffset } ` }
658+ template = { segment . template }
659+ onUse = { props . onUseArtifactTemplate }
660+ />
661+ ) ;
662+ }
663+ if ( segment . markdown . trim ( ) . length === 0 ) return null ;
664+
665+ const markdown = renderCodexFileCitationsAsMarkdown ( segment . markdown ) ;
666+ return hasNativeSelectableMarkdownText ( ) ? (
667+ < SelectableMarkdownText
668+ key = { `markdown:${ segment . sourceOffset } ` }
669+ markdown = { markdown }
670+ skills = { props . skills }
671+ textStyle = { props . markdownStyles . nativeTextStyle }
672+ onLinkPress = { props . onLinkPress }
673+ renderImage = { props . renderImage }
674+ />
675+ ) : (
676+ < Markdown
677+ key = { `markdown:${ segment . sourceOffset } ` }
678+ options = { { gfm : true } }
679+ renderers = { props . markdownStyles . renderers }
680+ styles = { props . markdownStyles . styles }
681+ theme = { props . markdownStyles . theme }
682+ >
683+ { markdown }
684+ </ Markdown >
685+ ) ;
686+ } ) ;
687+ } ) ;
688+
571689function MarkdownCodeBlock ( props : {
572690 readonly backgroundColor : string ;
573691 readonly borderColor : string ;
@@ -1099,7 +1217,7 @@ function useMarkdownStyles(
10991217
11001218function renderFeedEntry (
11011219 info : { item : ThreadFeedEntry ; index : number } ,
1102- props : Pick < ThreadFeedProps , "environmentId" | "skills" > & {
1220+ props : Pick < ThreadFeedProps , "environmentId" | "onUseArtifactTemplate" | " skills"> & {
11031221 readonly copiedRowId : string | null ;
11041222 readonly expandedWorkRows : Record < string , boolean > ;
11051223 readonly terminalAssistantMessageIds : ReadonlySet < string > ;
@@ -1162,6 +1280,7 @@ function renderFeedEntry(
11621280 if ( entry . type === "message" ) {
11631281 const { message } = entry ;
11641282 const isUser = message . role === "user" ;
1283+ const renderedText = message . text ;
11651284 const styles = isUser ? markdownStyles . user : markdownStyles . assistant ;
11661285 const timestampLabel = formatMessageTime ( isUser ? message . createdAt : message . updatedAt ) ;
11671286 const attachments = message . attachments ?? [ ] ;
@@ -1171,7 +1290,7 @@ function renderFeedEntry(
11711290 // children during the unclamped pass and never moves them once the width
11721291 // is clamped, so the paragraphs around the block end up drawn on top of
11731292 // each other. Pinning the width removes that pass.
1174- const hasWideBlock = hasWideMarkdownBlock ( message . text , WIDE_MARKDOWN_BLOCK_OPTIONS ) ;
1293+ const hasWideBlock = hasWideMarkdownBlock ( renderedText , WIDE_MARKDOWN_BLOCK_OPTIONS ) ;
11751294 const assistantTurnStillInProgress =
11761295 message . role === "assistant" &&
11771296 props . unsettledTurnId !== null &&
@@ -1251,7 +1370,7 @@ function renderFeedEntry(
12511370
12521371 // Skip empty assistant messages (no text, no attachments) — they would
12531372 // render as an orphaned timestamp and break adjacent activity-group merging.
1254- if ( message . text . trim ( ) . length === 0 && attachments . length === 0 ) {
1373+ if ( renderedText . trim ( ) . length === 0 && attachments . length === 0 ) {
12551374 return null ;
12561375 }
12571376
@@ -1261,25 +1380,15 @@ function renderFeedEntry(
12611380 className = { cn ( showAssistantMeta ? "mb-5 px-1" : "mb-2 px-1" ) }
12621381 { ...( enterAnimated ? { entering : FadeIn . duration ( 220 ) } : { } ) }
12631382 >
1264- { message . text . trim ( ) . length > 0 ? (
1265- hasNativeSelectableMarkdownText ( ) ? (
1266- < SelectableMarkdownText
1267- markdown = { message . text }
1268- skills = { props . skills }
1269- textStyle = { styles . nativeTextStyle }
1270- onLinkPress = { props . onMarkdownLinkPress }
1271- renderImage = { props . renderMarkdownImage }
1272- />
1273- ) : (
1274- < Markdown
1275- options = { { gfm : true } }
1276- renderers = { styles . renderers }
1277- styles = { styles . styles }
1278- theme = { styles . theme }
1279- >
1280- { message . text }
1281- </ Markdown >
1282- )
1383+ { renderedText . trim ( ) . length > 0 ? (
1384+ < AssistantMarkdownContent
1385+ markdown = { renderedText }
1386+ markdownStyles = { styles }
1387+ onLinkPress = { props . onMarkdownLinkPress }
1388+ onUseArtifactTemplate = { props . onUseArtifactTemplate }
1389+ renderImage = { props . renderMarkdownImage }
1390+ skills = { props . skills }
1391+ />
12831392 ) : null }
12841393 { attachments . map ( ( attachment ) => {
12851394 return isImageAttachment ( attachment ) ? (
@@ -1304,7 +1413,7 @@ function renderFeedEntry(
13041413 < View className = "mt-1 flex-row items-center gap-1" >
13051414 < CopyTextButton
13061415 accessibilityLabel = "Copy message"
1307- text = { message . text }
1416+ text = { renderedText }
13081417 tintColor = { iconSubtleColor }
13091418 buttonSize = { 28 }
13101419 iconSize = { 13 }
@@ -2192,6 +2301,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
21922301 reviewCommentBubbleWidth,
21932302 userBubbleMaxWidth,
21942303 skills : props . skills ,
2304+ onUseArtifactTemplate : props . onUseArtifactTemplate ,
21952305 } ) }
21962306 </ Animated . View >
21972307 ) ,
@@ -2214,6 +2324,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22142324 onToggleWorkGroup ,
22152325 onToggleWorkRow ,
22162326 props . environmentId ,
2327+ props . onUseArtifactTemplate ,
22172328 props . skills ,
22182329 renderMarkdownImage ,
22192330 ] ,
0 commit comments