Skip to content

Commit 22b6107

Browse files
committed
fix(client): resolve viewed image paths consistently
1 parent 8ae3ea0 commit 22b6107

5 files changed

Lines changed: 160 additions & 35 deletions

File tree

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

Lines changed: 44 additions & 14 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,7 +1551,7 @@ function renderFeedEntry(
15451551
iconSubtleColor={iconSubtleColor}
15461552
onCopyRow={props.onCopyWorkRow}
15471553
onToggleRow={props.onToggleWorkRow}
1548-
renderImage={props.renderMarkdownImage}
1554+
renderImage={props.renderViewedImage}
15491555
/>
15501556
);
15511557
}
@@ -1984,15 +1990,37 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
19841990
return (
19851991
<ThreadMarkdownImage
19861992
environmentId={props.environmentId}
1987-
threadId={props.threadId}
1988-
path={imageSource.path}
1993+
resource={{
1994+
_tag: "workspace-file",
1995+
threadId: props.threadId,
1996+
path: imageSource.path,
1997+
}}
19891998
alt={image.alt}
1999+
srcFragment={markdownImageSourceFragment(image.href)}
19902000
onPressPreview={(source) => setExpandedFile((current) => current ?? source)}
19912001
/>
19922002
);
19932003
},
19942004
[props.environmentId, props.threadId, props.workspaceRoot],
19952005
);
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+
);
19962024
const markdownStyles = useMarkdownStyles(onMarkdownLinkPress, renderMarkdownImage);
19972025
const reviewCommentColors = useReviewCommentColors();
19982026
// LegendList does not invalidate visible rows when only the renderItem closure changes.
@@ -2444,6 +2472,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24442472
onPressVideo,
24452473
onMarkdownLinkPress,
24462474
renderMarkdownImage,
2475+
renderViewedImage,
24472476
iconSubtleColor,
24482477
userBubbleColor,
24492478
markdownStyles,
@@ -2478,6 +2507,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
24782507
props.onUseArtifactTemplate,
24792508
props.skills,
24802509
renderMarkdownImage,
2510+
renderViewedImage,
24812511
],
24822512
);
24832513

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

Lines changed: 13 additions & 12 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,
@@ -1215,21 +1216,17 @@ function ChatMarkdownImageFallback(props: {
12151216
);
12161217
}
12171218

1218-
/** Markdown images whose src is a workspace file path load through a signed asset URL. */
1219-
export const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(props: {
1220-
readonly threadRef: ScopedThreadRef;
1221-
readonly path: string;
1219+
/** Environment-hosted images load through a signed asset URL. */
1220+
export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
1221+
readonly environmentId: EnvironmentId;
1222+
readonly resource: Extract<AssetResource, { readonly _tag: "attachment" | "workspace-file" }>;
12221223
readonly alt: string;
12231224
readonly copyMarkdown?: string;
12241225
readonly srcFragment?: string;
12251226
readonly style?: CSSProperties | undefined;
12261227
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
12271228
}) {
1228-
const assetUrl = useAssetUrlState(props.threadRef.environmentId, {
1229-
_tag: "workspace-file",
1230-
threadId: props.threadRef.threadId,
1231-
path: props.path,
1232-
});
1229+
const assetUrl = useAssetUrlState(props.environmentId, props.resource);
12331230
const [failedUrl, setFailedUrl] = useState<string | null>(null);
12341231

12351232
if (assetUrl._tag === "Failure" || (assetUrl._tag === "Success" && failedUrl === assetUrl.url)) {
@@ -2361,9 +2358,13 @@ function ChatMarkdown({
23612358
}
23622359
if (imageSource._tag === "WorkspaceFile" && threadRef) {
23632360
return (
2364-
<ChatMarkdownWorkspaceImage
2365-
threadRef={threadRef}
2366-
path={imageSource.path}
2361+
<ChatMarkdownAssetImage
2362+
environmentId={threadRef.environmentId}
2363+
resource={{
2364+
_tag: "workspace-file",
2365+
threadId: threadRef.threadId,
2366+
path: imageSource.path,
2367+
}}
23672368
alt={altText}
23682369
copyMarkdown={copyMarkdown}
23692370
srcFragment={markdownImageSourceFragment(classifiedSrc)}

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
import { parseScopedThreadKey } from "@t3tools/client-runtime/environment";
1010
import type { CodexArtifactTemplate } from "@t3tools/client-runtime/codex-artifact-templates";
1111
import { commandProgramName } from "@t3tools/client-runtime/work-log/command-label";
12-
import { workEntryViewedImagePath } from "@t3tools/client-runtime/work-log/presentation";
12+
import {
13+
resolveViewedImageAsset,
14+
workEntryViewedImagePath,
15+
} from "@t3tools/client-runtime/work-log/presentation";
1316
import type { AgentPanelModel } from "@t3tools/client-runtime/state/subagentRuntime";
1417
import {
1518
emptyAgentPanelModel,
@@ -56,7 +59,7 @@ import {
5659
resolveDiffThemeName,
5760
resolveFileDiffPath,
5861
} from "../../lib/diffRendering";
59-
import ChatMarkdown, { ChatMarkdownWorkspaceImage } from "../ChatMarkdown";
62+
import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";
6063
import {
6164
BotIcon,
6265
CheckIcon,
@@ -2423,6 +2426,13 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {
24232426
const displayText = workEntryPreview(workEntry, workspaceRoot) ?? toolWorkEntryHeading(workEntry);
24242427
const expandedBody = buildToolCallExpandedBody(workEntry, workspaceRoot);
24252428
const viewedImagePath = workEntryViewedImagePath(workEntry);
2429+
const viewedImage =
2430+
viewedImagePath && threadRef
2431+
? resolveViewedImageAsset(viewedImagePath, {
2432+
threadId: threadRef.threadId,
2433+
workspaceRoot,
2434+
})
2435+
: null;
24262436
const canExpand = expandedBody !== null;
24272437
const showDestructiveRowStyle =
24282438
showFailedIndicator &&
@@ -2516,12 +2526,13 @@ const PlainWorkEntryRow = memo(function PlainWorkEntryRow(props: {
25162526
onClick={stopRowToggle}
25172527
onPointerDown={stopRowToggle}
25182528
>
2519-
{viewedImagePath && threadRef ? (
2529+
{viewedImage && threadRef ? (
25202530
<div className="mb-1.5">
2521-
<ChatMarkdownWorkspaceImage
2522-
threadRef={threadRef}
2523-
path={viewedImagePath}
2524-
alt={viewedImagePath.split(/[\\/]/).at(-1) ?? "image"}
2531+
<ChatMarkdownAssetImage
2532+
environmentId={threadRef.environmentId}
2533+
resource={viewedImage.resource}
2534+
alt={viewedImage.alt}
2535+
srcFragment={viewedImage.srcFragment}
25252536
style={{ maxHeight: "16rem" }}
25262537
onImageExpand={onImageExpand}
25272538
/>

‎packages/client-runtime/src/work-log/presentation.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { workEntryViewedImagePath } from "./presentation.js";
3+
import { ThreadId } from "@t3tools/contracts";
4+
5+
import { resolveViewedImageAsset, workEntryViewedImagePath } from "./presentation.js";
46

57
describe("workEntryViewedImagePath", () => {
68
const entry = { label: "Read", tone: "tool" } as const;
@@ -29,3 +31,40 @@ describe("workEntryViewedImagePath", () => {
2931
expect(workEntryViewedImagePath({ ...entry, detail: "a.png" })).toBeNull();
3032
});
3133
});
34+
35+
describe("resolveViewedImageAsset", () => {
36+
const threadId = ThreadId.make("thread-1");
37+
38+
it("loads t3 attachment paths as attachments", () => {
39+
const attachmentId =
40+
"11111111-1111-4111-8111-111111111111-22222222-2222-4222-8222-222222222222";
41+
expect(
42+
resolveViewedImageAsset(`/Users/demo/.t3/dev/attachments/${attachmentId}.png`, {
43+
threadId,
44+
workspaceRoot: "/workspace",
45+
}),
46+
).toEqual({
47+
resource: { _tag: "attachment", attachmentId },
48+
alt: `${attachmentId}.png`,
49+
srcFragment: "",
50+
});
51+
});
52+
53+
it("normalizes workspace image sources", () => {
54+
expect(
55+
resolveViewedImageAsset("screens/logo.svg?v=2#mark", {
56+
threadId,
57+
workspaceRoot: "/workspace",
58+
}),
59+
).toEqual({
60+
resource: {
61+
_tag: "workspace-file",
62+
threadId,
63+
path: "/workspace/screens/logo.svg",
64+
},
65+
alt: "logo.svg",
66+
srcFragment: "#mark",
67+
});
68+
expect(resolveViewedImageAsset("https://example.com/logo.png", { threadId })).toBeNull();
69+
});
70+
});

‎packages/client-runtime/src/work-log/presentation.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { isToolLifecycleItemType, type ToolLifecycleItemType } from "@t3tools/contracts";
1+
import {
2+
isToolLifecycleItemType,
3+
type AssetResource,
4+
type ThreadId,
5+
type ToolLifecycleItemType,
6+
} from "@t3tools/contracts";
27
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
38

9+
import { classifyMarkdownImageSource, markdownImageSourceFragment } from "../markdownImages.js";
10+
411
export function isWorktreeSetupActivity(kind: string): boolean {
512
return kind === "setup-script.requested" || kind === "setup-script.started";
613
}
@@ -89,6 +96,43 @@ export function workEntryViewedImagePath(entry: WorkLogPresentationEntry): strin
8996
: null;
9097
}
9198

99+
export interface ViewedImageAsset {
100+
readonly resource: Extract<AssetResource, { readonly _tag: "attachment" | "workspace-file" }>;
101+
readonly alt: string;
102+
readonly srcFragment: string;
103+
}
104+
105+
const ABSOLUTE_IMAGE_SOURCE_PATTERN = /^(?:file:|[\\/]|[a-z]:[\\/])/i;
106+
const T3_ATTACHMENT_IMAGE_PATH_PATTERN =
107+
/(?:^|[\\/])(?:dev|userdata)[\\/]attachments[\\/]([a-z0-9_-]{1,128})\.[a-z0-9]{1,10}$/i;
108+
109+
export function resolveViewedImageAsset(
110+
source: string,
111+
input: {
112+
readonly threadId: ThreadId;
113+
readonly workspaceRoot?: string | null | undefined;
114+
},
115+
): ViewedImageAsset | null {
116+
const imageSource = classifyMarkdownImageSource(source, input.workspaceRoot ?? ".");
117+
if (imageSource._tag !== "WorkspaceFile") return null;
118+
119+
const path =
120+
input.workspaceRoot == null && imageSource.path.startsWith("./")
121+
? imageSource.path.slice(2)
122+
: imageSource.path;
123+
const attachmentId = ABSOLUTE_IMAGE_SOURCE_PATTERN.test(source)
124+
? (T3_ATTACHMENT_IMAGE_PATH_PATTERN.exec(path)?.[1] ?? null)
125+
: null;
126+
127+
return {
128+
resource: attachmentId
129+
? { _tag: "attachment", attachmentId }
130+
: { _tag: "workspace-file", threadId: input.threadId, path },
131+
alt: path.split(/[\\/]/).at(-1) ?? "image",
132+
srcFragment: markdownImageSourceFragment(source),
133+
};
134+
}
135+
92136
function toolGroupActionCount(
93137
action: ToolGroupAction,
94138
entries: ReadonlyArray<WorkLogPresentationEntry>,

0 commit comments

Comments
 (0)