Skip to content

Commit ce71c04

Browse files
authored
feat(client): render viewed images in work logs (pingdotgg#8936)
Co-authored-by: maria-rcks <[email protected]>
1 parent ff93aba commit ce71c04

7 files changed

Lines changed: 239 additions & 183 deletions

File tree

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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 {
5+
AssetResource,
56
ChatAttachment,
67
ChatFileAttachment,
78
ChatImageAttachment,
@@ -17,7 +18,11 @@ import {
1718
import { resolveAssetUrl } from "@t3tools/client-runtime/state/assets";
1819
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
1920
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
20-
import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images";
21+
import {
22+
classifyMarkdownImageSource,
23+
markdownImageSourceFragment,
24+
} from "@t3tools/client-runtime/markdown-images";
25+
import { resolveViewedImageAsset } from "@t3tools/client-runtime/work-log/presentation";
2126
import {
2227
renderCodexFileCitationsAsMarkdown,
2328
splitCodexArtifactTemplateMarkdown,
@@ -565,24 +570,24 @@ function ThreadMarkdownImageRequest(props: {
565570
);
566571
}
567572

568-
/** Markdown image whose src is a workspace file — loads through a signed asset URL. */
573+
/** Environment-hosted image that loads through a signed asset URL. */
569574
function ThreadMarkdownImage(props: {
570575
readonly environmentId: EnvironmentId;
571-
readonly threadId: ThreadId;
572-
readonly path: string;
576+
readonly resource: Extract<AssetResource, { readonly _tag: "attachment" | "workspace-file" }>;
573577
readonly alt: string | null;
578+
readonly srcFragment?: string;
574579
readonly onPressPreview: (source: FilePreviewSource) => void;
575580
}) {
576-
const assetUrl = useAssetUrlState(props.environmentId, {
577-
_tag: "workspace-file",
578-
threadId: props.threadId,
579-
path: props.path,
580-
});
581+
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
581582

582583
return (
583584
<ThreadMarkdownImageView
584-
uri={assetUrl._tag === "Success" ? assetUrl.url : null}
585-
sourceKey={props.path}
585+
uri={assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null}
586+
sourceKey={
587+
props.resource._tag === "attachment"
588+
? `attachment:${props.resource.attachmentId}`
589+
: `workspace:${props.resource.path}`
590+
}
586591
unavailable={assetUrl._tag === "Failure"}
587592
alt={props.alt}
588593
onPressPreview={props.onPressPreview}
@@ -1333,6 +1338,7 @@ function renderFeedEntry(
13331338
readonly onPressVideo: (attachment: ChatFileAttachment, sourceIdentifier: string) => void;
13341339
readonly onMarkdownLinkPress: (href: string) => void;
13351340
readonly renderMarkdownImage: MarkdownImageRenderer;
1341+
readonly renderViewedImage: MarkdownImageRenderer;
13361342
readonly iconSubtleColor: string | import("react-native").ColorValue;
13371343
readonly userBubbleColor: string | import("react-native").ColorValue;
13381344
readonly markdownStyles: MarkdownStyleSets;
@@ -1545,6 +1551,7 @@ function renderFeedEntry(
15451551
iconSubtleColor={iconSubtleColor}
15461552
onCopyRow={props.onCopyWorkRow}
15471553
onToggleRow={props.onToggleWorkRow}
1554+
renderImage={props.renderViewedImage}
15481555
/>
15491556
);
15501557
}
@@ -1983,15 +1990,37 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
19831990
return (
19841991
<ThreadMarkdownImage
19851992
environmentId={props.environmentId}
1986-
threadId={props.threadId}
1987-
path={imageSource.path}
1993+
resource={{
1994+
_tag: "workspace-file",
1995+
threadId: props.threadId,
1996+
path: imageSource.path,
1997+
}}
19881998
alt={image.alt}
1999+
srcFragment={markdownImageSourceFragment(image.href)}
19892000
onPressPreview={(source) => setExpandedFile((current) => current ?? source)}
19902001
/>
19912002
);
19922003
},
19932004
[props.environmentId, props.threadId, props.workspaceRoot],
19942005
);
2006+
const renderViewedImage = useCallback<MarkdownImageRenderer>(
2007+
(image) => {
2008+
const viewedImage = resolveViewedImageAsset(image.href, {
2009+
threadId: props.threadId,
2010+
workspaceRoot: props.workspaceRoot,
2011+
});
2012+
return viewedImage ? (
2013+
<ThreadMarkdownImage
2014+
environmentId={props.environmentId}
2015+
resource={viewedImage.resource}
2016+
alt={viewedImage.alt}
2017+
srcFragment={viewedImage.srcFragment}
2018+
onPressPreview={(source) => setExpandedFile((current) => current ?? source)}
2019+
/>
2020+
) : null;
2021+
},
2022+
[props.environmentId, props.threadId, props.workspaceRoot],
2023+
);
19952024
const markdownStyles = useMarkdownStyles(onMarkdownLinkPress, renderMarkdownImage);
19962025
const reviewCommentColors = useReviewCommentColors();
19972026
// LegendList does not invalidate visible rows when only the renderItem closure changes.
@@ -2443,6 +2472,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24432472
onPressVideo,
24442473
onMarkdownLinkPress,
24452474
renderMarkdownImage,
2475+
renderViewedImage,
24462476
iconSubtleColor,
24472477
userBubbleColor,
24482478
markdownStyles,
@@ -2477,6 +2507,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24772507
props.onUseArtifactTemplate,
24782508
props.skills,
24792509
renderMarkdownImage,
2510+
renderViewedImage,
24802511
],
24812512
);
24822513

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
1717
import { AppText as Text } from "../../components/AppText";
1818
import { cn } from "../../lib/cn";
1919
import type { ThreadFeedActivity } from "../../lib/threadActivity";
20-
import type { ToolGroupSummaryKind } from "@t3tools/client-runtime/work-log/presentation";
20+
import {
21+
type ToolGroupSummaryKind,
22+
workEntryViewedImagePath,
23+
} from "@t3tools/client-runtime/work-log/presentation";
24+
import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText";
2125
import Animated, {
2226
cancelAnimation,
2327
Easing,
@@ -277,6 +281,7 @@ export function ThreadWorkLog(props: {
277281
readonly iconSubtleColor: import("react-native").ColorValue;
278282
readonly onCopyRow: (rowId: string, value: string) => void;
279283
readonly onToggleRow: (rowId: string) => void;
284+
readonly renderImage: MarkdownImageRenderer;
280285
}) {
281286
const rows = props.activities.map((activity) => ({
282287
...activity,
@@ -294,6 +299,7 @@ export function ThreadWorkLog(props: {
294299
const expanded = props.expandedRows[row.id] ?? false;
295300
const canExpand = row.canExpand;
296301
const fullDetail = expanded ? row.getFullDetail() : null;
302+
const viewedImagePath = workEntryViewedImagePath(row.workEntry);
297303
const displayText = row.detail ?? row.summary;
298304
const iconIsDestructive = row.icon === "alert" || row.icon === "warning";
299305
const failed = row.status === "failure";
@@ -393,6 +399,11 @@ export function ThreadWorkLog(props: {
393399
layout={WORK_LOG_LAYOUT_TRANSITION}
394400
className="ml-7 border-l border-adaptive-neutral-300-a60-white-a12 pb-1 pl-3 pt-0.5"
395401
>
402+
{viewedImagePath ? (
403+
<View className="pb-1.5">
404+
{props.renderImage({ href: viewedImagePath, alt: null, title: null })}
405+
</View>
406+
) : null}
396407
<ScrollView
397408
nestedScrollEnabled
398409
directionalLockEnabled

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type LucideIcon,
2323
} from "lucide-react";
2424
import type {
25+
AssetResource,
2526
EnvironmentId,
2627
ScopedThreadRef,
2728
ServerProviderSkill,
@@ -1218,21 +1219,17 @@ function ChatMarkdownImageFallback(props: {
12181219
);
12191220
}
12201221

1221-
/** Markdown images whose src is a workspace file path load through a signed asset URL. */
1222-
const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(props: {
1223-
readonly threadRef: ScopedThreadRef;
1224-
readonly path: string;
1222+
/** Environment-hosted images load through a signed asset URL. */
1223+
export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
1224+
readonly environmentId: EnvironmentId;
1225+
readonly resource: Extract<AssetResource, { readonly _tag: "attachment" | "workspace-file" }>;
12251226
readonly alt: string;
1226-
readonly copyMarkdown: string;
1227-
readonly srcFragment: string;
1227+
readonly copyMarkdown?: string;
1228+
readonly srcFragment?: string;
12281229
readonly style?: CSSProperties | undefined;
12291230
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
12301231
}) {
1231-
const assetUrl = useAssetUrlState(props.threadRef.environmentId, {
1232-
_tag: "workspace-file",
1233-
threadId: props.threadRef.threadId,
1234-
path: props.path,
1235-
});
1232+
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
12361233
const [failedUrl, setFailedUrl] = useState<string | null>(null);
12371234

12381235
if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {
@@ -1253,7 +1250,7 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
12531250
/>
12541251
);
12551252
}
1256-
const src = assetUrl.url + props.srcFragment;
1253+
const src = assetUrl.url + (props.srcFragment ?? "");
12571254
return (
12581255
<img
12591256
src={src}
@@ -2377,9 +2374,13 @@ function ChatMarkdown({
23772374
}
23782375
if (imageSource._tag === "WorkspaceFile" && threadRef) {
23792376
return (
2380-
<ChatMarkdownWorkspaceImage
2381-
threadRef={threadRef}
2382-
path={imageSource.path}
2377+
<ChatMarkdownAssetImage
2378+
environmentId={threadRef.environmentId}
2379+
resource={{
2380+
_tag: "workspace-file",
2381+
threadId: threadRef.threadId,
2382+
path: imageSource.path,
2383+
}}
23832384
alt={altText}
23842385
copyMarkdown={copyMarkdown}
23852386
srcFragment={markdownImageSourceFragment(classifiedSrc)}

0 commit comments

Comments
 (0)