The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 497 lines 17.1 KB
Raw
1 import { mediaFileReference, mediaUrlReference } from "@t3tools/client-runtime/media-reference";
2 import type { AssetResource, EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts";
3 import { msg, useGT, useMessages } from "gt-react";
4 import { TriangleAlertIcon } from "lucide-react";
5 import {
6 createContext,
7 memo,
8 use,
9 useState,
10 type ComponentProps,
11 type CSSProperties,
12 type KeyboardEvent as ReactKeyboardEvent,
13 type MouseEvent as ReactMouseEvent,
14 } from "react";
15
16 import { useAssetUrlRefresh, useAssetUrlState } from "../../assets/assetUrls";
17 import type { MessageResolver } from "../../i18n/messages";
18 import { cn } from "../../lib/utils";
19 import { useRightPanelStore } from "../../rightPanelStore";
20 import { MediaActions, type MediaActionSource } from "../media/MediaActions";
21 import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
22 import type { ExpandedImagePreview } from "./ExpandedImagePreview";
23 import { markdownImageGallery, markdownImageItems } from "./markdownImageGallery";
24
25 const CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME = "max-w-[min(100%,30rem)]";
26 const CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME = cn(
27 "max-h-[30rem]",
28 CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
29 );
30 const CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME = "inline-block!";
31 const CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME = "rounded-lg border border-border/40";
32 const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn(
33 "h-auto w-auto object-contain",
34 CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
35 );
36
37 export function markdownImageCopy(alt: string, src: string, title: string | undefined): string {
38 const escapedAlt = alt.replaceAll("\\", "\\\\").replaceAll("[", "\\[").replaceAll("]", "\\]");
39 const titleSuffix =
40 title === undefined ? "" : ` "${title.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
41 return `![${escapedAlt}](${markdownDestination(src)}${titleSuffix})`;
42 }
43
44 // Raw HTML sources keep spaces and parentheses that a bare Markdown destination cannot hold.
45 function markdownDestination(src: string): string {
46 if (!/[\s<>()]/.test(src)) return src;
47 const escaped = src
48 .replaceAll("\\", "\\\\")
49 .replaceAll("<", "\\<")
50 .replaceAll(">", "\\>")
51 .replaceAll("\n", "%0A")
52 .replaceAll("\r", "%0D");
53 return `<${escaped}>`;
54 }
55
56 function parseImageDimension(value: string | number | undefined): number {
57 return Number(typeof value === "string" ? value.trim().replace(/px$/i, "") : value);
58 }
59
60 /**
61 * `maxHeightRem` folds a height cap into the width bound: `max-height` alone
62 * would not feed back through `aspect-ratio` once `width` is definite, so a
63 * tall image would keep a box wider than the picture it draws.
64 */
65 export function authoredImageSizeStyle(
66 width: string | number | undefined,
67 height: string | number | undefined,
68 maxHeightRem = 30,
69 ): CSSProperties | undefined {
70 const parsedWidth = parseImageDimension(width);
71 const parsedHeight = parseImageDimension(height);
72 const hasWidth = Number.isFinite(parsedWidth) && parsedWidth > 0;
73 const hasHeight = Number.isFinite(parsedHeight) && parsedHeight > 0;
74 if (hasWidth && hasHeight) {
75 return {
76 width: parsedWidth,
77 height: "auto",
78 aspectRatio: `${parsedWidth} / ${parsedHeight}`,
79 maxWidth: `min(100%, 30rem, ${(maxHeightRem * parsedWidth) / parsedHeight}rem)`,
80 };
81 }
82 if (hasWidth) return { maxWidth: `min(100%, 30rem, ${parsedWidth}px)` };
83 if (hasHeight) return { maxHeight: `min(30rem, ${parsedHeight}px)` };
84 return undefined;
85 }
86
87 const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn(
88 CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
89 CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
90 );
91 export const MarkdownLinkContext = createContext(false);
92 /** The thread's workspace mutation id, so a host image the agent rewrites gets a fresh URL. */
93 export const WorkspaceMutationIdContext = createContext<string | null | undefined>(undefined);
94
95 function expandableMarkdownImageProps(
96 onImageExpand: ((preview: ExpandedImagePreview) => void) | undefined,
97 alt: string,
98 resolve: MessageResolver,
99 ) {
100 if (!onImageExpand) return {};
101 const previewName = alt.trim();
102 const expand = (event: ReactMouseEvent | ReactKeyboardEvent) => {
103 if (event.currentTarget.closest("a")) return;
104 event.preventDefault();
105 event.stopPropagation();
106 const item = markdownImageItems.get(event.currentTarget);
107 if (item) onImageExpand(markdownImageGallery(event.currentTarget, item));
108 };
109 return {
110 role: "button" as const,
111 tabIndex: 0,
112 "aria-label": previewName
113 ? resolve(msg("Preview {name}", { name: previewName }))
114 : resolve(msg("Preview image")),
115 onClick: expand,
116 onKeyDown: (event: ReactKeyboardEvent) => {
117 if (event.key === "Enter" || event.key === " ") expand(event);
118 },
119 };
120 }
121
122 function ChatMarkdownMediaUnavailableLabel(props: {
123 readonly alt: string;
124 readonly kind?: "image" | "video" | "audio" | undefined;
125 readonly detail?: string | undefined;
126 }) {
127 const gt = useGT();
128 const label =
129 props.kind === "audio"
130 ? gt("Audio unavailable")
131 : props.kind === "video"
132 ? gt("Video unavailable")
133 : gt("Image unavailable");
134 return (
135 <span data-thread-find-ignore className="inline-flex items-center gap-1.5">
136 <TriangleAlertIcon aria-hidden className="size-3.5 shrink-0" />
137 {[label, props.alt, props.detail].filter(Boolean).join(" · ")}
138 </span>
139 );
140 }
141
142 export function ChatMarkdownImageFallback(props: {
143 readonly alt: string;
144 readonly copyMarkdown?: string | undefined;
145 readonly kind?: "image" | "video" | "audio";
146 readonly actionsSource?: MediaActionSource | undefined;
147 readonly detail?: string | undefined;
148 }) {
149 const content = (
150 <span
151 data-markdown-copy={props.copyMarkdown}
152 className={cn(
153 CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
154 "rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
155 )}
156 >
157 <ChatMarkdownMediaUnavailableLabel alt={props.alt} kind={props.kind} detail={props.detail} />
158 </span>
159 );
160 return props.actionsSource ? (
161 <MediaActions source={props.actionsSource}>{content}</MediaActions>
162 ) : (
163 content
164 );
165 }
166
167 const CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME = cn(
168 "aspect-video w-full overflow-hidden bg-muted/60",
169 CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
170 CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
171 );
172
173 /**
174 * Callers key this on the file's identity, not its URL: a re-signed URL for
175 * the same file keeps the decoded image on screen while the new bytes arrive,
176 * and a different file starts from the slot again.
177 */
178 export function ChatMarkdownImage(props: {
179 readonly src: string | null;
180 readonly sourceFailed?: boolean | undefined;
181 readonly unavailableDetail?: string | undefined;
182 readonly alt: string;
183 readonly copyMarkdown: string | undefined;
184 readonly standalone: boolean;
185 readonly className?: string | undefined;
186 readonly style?: CSSProperties | undefined;
187 readonly imageProps?:
188 | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
189 | undefined;
190 readonly actionsSource: MediaActionSource;
191 readonly originalUrl?: string | undefined;
192 readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
193 }) {
194 const gt = useGT();
195 const m = useMessages();
196 const [loadedSrc, setLoadedSrc] = useState<string | null>(null);
197 const [failedSrc, setFailedSrc] = useState<string | null>(null);
198 const src = props.src ?? loadedSrc;
199 const failed = props.sourceFailed === true || (src !== null && failedSrc === src);
200 const settled = src !== null && !failed && (!props.standalone || loadedSrc !== null);
201 // Cached images are complete before `onLoad` can fire.
202 const markLoadedIfComplete = (image: HTMLImageElement | null) => {
203 if (!image) return;
204 if (image.complete && image.naturalWidth > 0) setLoadedSrc(image.currentSrc || image.src);
205 markdownImageItems.set(image, {
206 src,
207 name: props.alt.trim() || "image",
208 actionsSource: props.actionsSource,
209 ...(props.originalUrl ? { originalUrl: props.originalUrl } : {}),
210 });
211 };
212 const imageEvents = (loadingSrc: string) => ({
213 onLoad: () => {
214 setLoadedSrc(loadingSrc);
215 setFailedSrc(null);
216 },
217 onError: () => {
218 setFailedSrc(loadingSrc);
219 setLoadedSrc(null);
220 },
221 });
222
223 if (settled) {
224 return (
225 <MediaActions source={props.actionsSource}>
226 <img
227 {...props.imageProps}
228 ref={markLoadedIfComplete}
229 src={src}
230 alt={props.alt}
231 data-markdown-copy={props.copyMarkdown}
232 decoding="async"
233 loading="lazy"
234 draggable={false}
235 className={cn(
236 CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME,
237 props.className,
238 props.onImageExpand && "cursor-zoom-in",
239 )}
240 style={props.style}
241 {...expandableMarkdownImageProps(props.onImageExpand, props.alt, m)}
242 {...imageEvents(src)}
243 />
244 </MediaActions>
245 );
246 }
247 if (!props.standalone) {
248 return failed ? (
249 <ChatMarkdownImageFallback
250 alt={props.alt}
251 copyMarkdown={props.copyMarkdown}
252 actionsSource={props.actionsSource}
253 detail={props.unavailableDetail}
254 />
255 ) : (
256 <span
257 id={props.imageProps?.id}
258 data-markdown-copy={props.copyMarkdown}
259 role="status"
260 aria-label={gt("Loading image")}
261 className={CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME}
262 />
263 );
264 }
265 return (
266 <MediaActions source={props.actionsSource}>
267 <span
268 id={props.imageProps?.id}
269 data-markdown-copy={props.copyMarkdown}
270 className={cn(
271 CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
272 CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME,
273 "relative",
274 )}
275 style={props.style}
276 {...(failed
277 ? { role: "alert" as const }
278 : { role: "status" as const, "aria-label": gt("Loading image") })}
279 >
280 {failed ? (
281 <span className="flex size-full items-center justify-center p-2 text-center text-xs text-muted-foreground">
282 <ChatMarkdownMediaUnavailableLabel alt={props.alt} detail={props.unavailableDetail} />
283 </span>
284 ) : src !== null ? (
285 <img
286 ref={markLoadedIfComplete}
287 src={src}
288 alt={props.alt}
289 decoding="async"
290 loading="lazy"
291 draggable={false}
292 className="invisible absolute inset-0 size-full"
293 {...imageEvents(src)}
294 />
295 ) : null}
296 </span>
297 </MediaActions>
298 );
299 }
300
301 export function ChatMarkdownVideo(props: {
302 readonly src: string | null;
303 readonly alt: string;
304 readonly copyMarkdown: string | undefined;
305 readonly originalUrl?: string | undefined;
306 readonly sourceFailed?: boolean | undefined;
307 readonly unavailableDetail?: string | undefined;
308 readonly style?: CSSProperties | undefined;
309 readonly mediaIdentity?: string | undefined;
310 readonly actionsSource?: MediaActionSource | undefined;
311 readonly onRetry?: (() => Promise<unknown>) | undefined;
312 }) {
313 return (
314 <MediaVideoPlayer
315 key={props.mediaIdentity ?? props.copyMarkdown ?? props.src}
316 src={props.src}
317 sourceFailed={props.sourceFailed}
318 unavailableDetail={props.unavailableDetail}
319 label={props.alt}
320 originalUrl={props.originalUrl}
321 style={props.style}
322 copyMarkdown={props.copyMarkdown}
323 className={cn(
324 CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
325 CHAT_MARKDOWN_MEDIA_MAX_WIDTH_CLASS_NAME,
326 "w-full",
327 )}
328 videoClassName={cn(
329 CHAT_MARKDOWN_MEDIA_BOUNDS_CLASS_NAME,
330 CHAT_MARKDOWN_MEDIA_FRAME_CLASS_NAME,
331 )}
332 onRetry={props.onRetry}
333 actionsSource={props.actionsSource}
334 />
335 );
336 }
337
338 type ChatMarkdownAssetResource = Extract<
339 AssetResource,
340 {
341 readonly _tag:
342 | "attachment"
343 | "workspace-file"
344 | "media-file"
345 | typeof SOURCE_CONTROL_MEDIA_ASSET
346 | "tool-output-image";
347 }
348 >;
349
350 export function assetMediaActionsSource(input: {
351 readonly environmentId: EnvironmentId;
352 readonly resource: ChatMarkdownAssetResource;
353 readonly kind: "image" | "video";
354 readonly name: string;
355 readonly src: string | null;
356 readonly workspaceRoot?: string | undefined;
357 readonly originalUrl?: string | undefined;
358 readonly downloadable?: boolean | undefined;
359 }): MediaActionSource {
360 const { environmentId, resource } = input;
361 const path =
362 resource._tag === "media-file"
363 ? resource.path
364 : resource._tag === "workspace-file" && input.workspaceRoot
365 ? `${input.workspaceRoot.replace(/[\\/]+$/, "")}/${resource.path}`
366 : undefined;
367 const reference = path
368 ? mediaFileReference(path, input.workspaceRoot)
369 : input.originalUrl
370 ? mediaUrlReference(input.originalUrl)
371 : undefined;
372 const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
373 return {
374 kind: input.kind,
375 name: input.name,
376 src: input.src,
377 ...(input.downloadable === false ? {} : { asset: { environmentId, resource } }),
378 ...(reference ? { reference } : {}),
379 ...(relativePath && (resource._tag === "media-file" || resource._tag === "workspace-file")
380 ? {
381 onOpenFile: () =>
382 useRightPanelStore
383 .getState()
384 .openFile({ environmentId, threadId: resource.threadId }, relativePath),
385 }
386 : {}),
387 };
388 }
389
390 export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props: {
391 readonly environmentId: EnvironmentId;
392 readonly resource: ChatMarkdownAssetResource;
393 readonly kind?: "image" | "video";
394 readonly alt: string;
395 readonly copyMarkdown?: string;
396 readonly srcFragment?: string;
397 readonly standalone?: boolean | undefined;
398 readonly maxHeightRem?: number | undefined;
399 readonly style?: CSSProperties | undefined;
400 readonly className?: string | undefined;
401 readonly imageProps?:
402 | Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
403 | undefined;
404 readonly originalUrl?: string | undefined;
405 readonly framed?: boolean | undefined;
406 readonly fallbackSrc?: string | undefined;
407 readonly workspaceRoot?: string | undefined;
408 readonly workspaceMutationId?: string | null | undefined;
409 readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
410 }) {
411 const gt = useGT();
412 const resource = props.resource;
413 const threadWorkspaceMutationId = use(WorkspaceMutationIdContext);
414 const workspaceMutationId = props.workspaceMutationId ?? threadWorkspaceMutationId;
415 const assetUrl = useAssetUrlState(
416 props.environmentId,
417 resource,
418 workspaceMutationId !== undefined &&
419 props.kind !== "video" &&
420 (resource._tag === "media-file" || resource._tag === "workspace-file")
421 ? workspaceMutationId
422 : undefined,
423 );
424 const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);
425 const fallbackSrc = assetUrl._tag === "Failure" ? props.fallbackSrc : undefined;
426 const missingFileAccess =
427 assetUrl._tag === "Failure" &&
428 assetUrl.reason === "missing-file-access" &&
429 fallbackSrc === undefined;
430 const unavailableDetail = missingFileAccess
431 ? gt("This connection cannot read host files. Pair again with a new link to view it.")
432 : undefined;
433 const src =
434 assetUrl._tag === "Success"
435 ? assetUrl.url + (props.srcFragment ?? "")
436 : fallbackSrc === undefined
437 ? null
438 : fallbackSrc + (props.srcFragment ?? "");
439 const knownSize = assetUrl._tag === "Success" ? assetUrl.imageDimensions : undefined;
440 const maxHeightRem = props.maxHeightRem ?? 30;
441 const style =
442 props.style ??
443 (knownSize
444 ? authoredImageSizeStyle(knownSize.width, knownSize.height, maxHeightRem)
445 : maxHeightRem !== 30
446 ? { maxHeight: `${maxHeightRem}rem` }
447 : undefined);
448 const actionsSource = assetMediaActionsSource({
449 environmentId: props.environmentId,
450 resource,
451 kind: props.kind ?? "image",
452 name: props.alt || (props.kind ?? "image"),
453 src,
454 workspaceRoot: props.workspaceRoot,
455 originalUrl: props.originalUrl,
456 downloadable: fallbackSrc === undefined,
457 });
458
459 if (props.kind === "video") {
460 return (
461 <ChatMarkdownVideo
462 src={src}
463 sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
464 unavailableDetail={unavailableDetail}
465 alt={props.alt}
466 copyMarkdown={props.copyMarkdown}
467 originalUrl={props.originalUrl}
468 style={props.style}
469 mediaIdentity={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
470 // Re-signing cannot grant the file access this connection lacks.
471 onRetry={missingFileAccess ? undefined : refreshAssetUrl}
472 actionsSource={actionsSource}
473 />
474 );
475 }
476
477 return (
478 <ChatMarkdownImage
479 key={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
480 src={src}
481 sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
482 unavailableDetail={unavailableDetail}
483 alt={props.alt}
484 copyMarkdown={props.copyMarkdown}
485 standalone={props.standalone ?? true}
486 className={cn(
487 props.framed === false ? undefined : CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME,
488 props.className,
489 )}
490 style={style}
491 imageProps={props.imageProps}
492 actionsSource={actionsSource}
493 originalUrl={props.originalUrl}
494 onImageExpand={props.onImageExpand}
495 />
496 );
497 });