@devries-cameron20 / t3code Mirror · read-only / commits / 750e492ed6
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

feat(web): copy snapshots into another composer (#37771)

Co-authored-by: Cursor <cursoragent@cursor.com>

Yash Singh committed Nov 16, 2026 at 15:38 UTC 750e492ed68c70ac9aadc4c0dc85af8cda7aef37
15 files changed +374 -20
apps/mobile/src/lib/composerContextClipboard.test.ts
+12 -1
@@ -73,6 +73,13 @@ const image = {
73 mimeType: "image/png",
74 sizeBytes: 7,
75 attachmentId: "source-file",
76 + source: {
77 + kind: "snap-shot" as const,
78 + capturedAt: "2026-01-01T00:00:00.000Z",
79 + appName: "Finder",
80 + windowTitle: "Downloads",
81 + accessibleText: "Downloads",
82 + },
83 };
84 const terminal = {
85 version: 1 as const,
@@ -146,7 +153,11 @@ describe("mobile context clipboard imports", () => {
153 const signal = { aborted: false } as AbortSignal;
154 const result = await importComposerContextClipboard(clipboard, 0, signal);
155 expect(result?.failures).toEqual([]);
149 - expect(result?.attachments[0]).toMatchObject({ fileUri: "file:///owned.png", sizeBytes: 42 });
156 + expect(result?.attachments[0]).toMatchObject({
157 + fileUri: "file:///owned.png",
158 + sizeBytes: 42,
159 + source: image.source,
160 + });
161 expect(result?.context.records[0]).toMatchObject({
162 contextId: "import-1",
163 attachmentId: result?.attachments[0]?.id,
apps/mobile/src/lib/composerContextClipboard.ts
+6 -1
@@ -253,6 +253,11 @@ async function persistImportedAttachment(
253 sizeBytes: new File(fileUri).size,
254 };
255 return record.kind === "image"
256 - ? { ...common, type: "image", previewUri: fileUri }
256 + ? {
257 + ...common,
258 + type: "image",
259 + previewUri: fileUri,
260 + ...(record.source ? { source: record.source } : {}),
261 + }
262 : { ...common, type: "file" };
263 }
apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx
+66 -1
@@ -7,7 +7,17 @@ import {
7 useRetainedContextPayloads,
8 } from "./chat/composerContextUndo";
9
10 -import type { ServerProviderSkill } from "@t3tools/contracts";
10 +import {
11 + ComposerContextId,
12 + type ComposerContextRecord,
13 + EnvironmentId,
14 + type ServerProviderSkill,
15 +} from "@t3tools/contracts";
16 +import {
17 + COMPOSER_CONTEXT_CLIPBOARD_MIME,
18 + encodeComposerContextFragment,
19 +} from "@t3tools/shared/composerContextClipboard";
20 +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences";
21 import type { Editor } from "@tiptap/core";
22 import { redoDepth, undoDepth } from "@tiptap/pm/history";
23 import { NodeSelection } from "@tiptap/pm/state";
@@ -54,12 +64,16 @@ function Composer({
64 skills,
65 onChange,
66 onCommandKeyDown = () => false,
67 + importContextFragment,
68 + literalText = false,
69 }: {
70 value: string;
71 richTextEnabled: boolean;
72 skills: ReadonlyArray<ServerProviderSkill>;
73 onChange?: ComposerPromptEditorProps["onChange"] | undefined;
74 onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"] | undefined;
75 + importContextFragment?: ComposerPromptEditorProps["importContextFragment"];
76 + literalText?: boolean;
77 }) {
78 const [prompt, setPrompt] = useState({ value, cursor: value.length });
79 return (
@@ -78,6 +92,8 @@ function Composer({
92 }}
93 onPaste={() => {}}
94 onCommandKeyDown={onCommandKeyDown}
95 + importContextFragment={importContextFragment}
96 + literalText={literalText}
97 />
98 );
99 }
@@ -482,6 +498,55 @@ it.each([
498 expect(serializeEditorDoc(editor.state.doc).value).toBe(pasted);
499 });
500
501 +it.each([
502 + { literalText: false, imports: 1 },
503 + { literalText: true, imports: 0 },
504 +])(
505 + "imports a copied snapshot's context over its image file: %o",
506 + async ({ literalText, imports }) => {
507 + const record = {
508 + version: 1,
509 + kind: "image",
510 + contextId: ComposerContextId.make("image_snapshot"),
511 + label: "shot.png",
512 + attachmentId: "snapshot",
513 + name: "shot.png",
514 + mimeType: "image/png",
515 + sizeBytes: 3,
516 + } satisfies ComposerContextRecord;
517 + const importContextFragment = vi.fn(() => new Map([[record.contextId, record.contextId]]));
518 + await using screen = await render(
519 + <Composer
520 + value=""
521 + richTextEnabled
522 + skills={[]}
523 + literalText={literalText}
524 + importContextFragment={importContextFragment}
525 + />,
526 + );
527 + const editor = editorIn(screen.container);
528 + await act(async () => {
529 + const clipboardData = new DataTransfer();
530 + clipboardData.items.add(
531 + new File([new Uint8Array([1, 2, 3])], "shot.png", { type: "image/png" }),
532 + );
533 + clipboardData.setData("text/plain", formatComposerContextReference(record));
534 + clipboardData.setData(
535 + COMPOSER_CONTEXT_CLIPBOARD_MIME,
536 + encodeComposerContextFragment({
537 + version: 1,
538 + source: { environmentId: EnvironmentId.make("snapshot-source") },
539 + records: [record],
540 + })!,
541 + );
542 + editor.view.dom.dispatchEvent(
543 + new ClipboardEvent("paste", { clipboardData, cancelable: true }),
544 + );
545 + });
546 + expect(importContextFragment).toHaveBeenCalledTimes(imports);
547 + },
548 +);
549 +
550 it.each(["x\r\n\r\ny", "x\r\ry"])(
551 "pastes %j with the same blank line as x\\n\\ny",
552 async (text) => {
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+11 -2
@@ -138,7 +138,11 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
138 import { ComposerCodeBlockNodeView } from "./chat/ComposerCodeBlockNodeView";
139 import { resolveAssistantCitationCommentDismissal } from "./chat/assistantCitationCommentDismissal";
140 import { composerCodeBlockHighlight } from "./composerCodeBlockHighlight";
141 -import { importPastedComposerText, readPastedPlainText } from "./composerInlineTokenPaste";
141 +import {
142 + importPastedComposerText,
143 + readPastedComposerContext,
144 + readPastedPlainText,
145 +} from "./composerInlineTokenPaste";
146 import { didComposerSelectionChangeVisibly } from "./composerSelection";
147 import {
148 captureComposerAttachmentInsertion,
@@ -1404,7 +1408,12 @@ function ComposerPromptEditorTiptapInner(
1408 },
1409 handlePaste: (view, event) => {
1410 const clipboardData = event.clipboardData;
1407 - if (!clipboardData || clipboardData.files.length > 0) return false;
1411 + if (!clipboardData) return false;
1412 + if (
1413 + clipboardData.files.length > 0 &&
1414 + (literalText || !readPastedComposerContext(clipboardData)?.records.length)
1415 + )
1416 + return false;
1417 const pastedText = readPastedPlainText(clipboardData);
1418 if (!pastedText) return false;
1419 event.preventDefault();
apps/web/src/components/chat/ChatComposer.tsx
+33 -4
@@ -1,3 +1,4 @@
1 +import { copySnapshot } from "./copySnapshot";
2 import { VoiceInputSetup } from "./VoiceInputSetup";
3 import { useComposerTypingGuard } from "./useComposerTypingGuard";
4 import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
@@ -148,6 +149,7 @@ import {
149 type TerminalContextSelection,
150 } from "../../lib/terminalContext";
151 import {
152 + formatComposerContextReference,
153 threadContextRecord,
154 toKindScopedComposerContextId,
155 } from "@t3tools/shared/composerContextReferences";
@@ -717,7 +719,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
719 cwd: gitCwd ?? undefined,
720 projectId: activeProjectId ?? undefined,
721 expandImage: (imageId: string) => {
720 - const preview = buildExpandedImagePreview(composerImages, imageId);
722 + const preview = buildExpandedImagePreview(
723 + composerImages,
724 + imageId,
725 + copyComposerSnapshotStable,
726 + );
727 if (preview) onExpandImage(preview);
728 },
729 openFile: setPreviewFileId,
@@ -1334,7 +1340,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1340 : null);
1341 if (!record) return [];
1342 if (attachment.file) copiedFiles.set(record.attachmentId, attachment.file);
1337 - return [record];
1343 + return [
1344 + record.kind === "image" && attachment.type === "image" && attachment.source
1345 + ? { ...record, source: attachment.source }
1346 + : record,
1347 + ];
1348 }),
1349 ];
1350 if (records.length === 0) return null;
@@ -1346,6 +1356,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
1356 if (fragment) rememberCopiedComposerAttachments(environmentId, copiedFiles);
1357 return fragment;
1358 };
1359 + const copyComposerSnapshot = async (imageId: string) => {
1360 + const image = composerImages.find((candidate) => candidate.id === imageId);
1361 + if (!image) throw new Error("Snapshot is no longer available.");
1362 + const contextId = toKindScopedComposerContextId("image", image.id);
1363 + const fragment = buildContextClipboardFragment([contextId]);
1364 + if (!fragment || !image.previewUrl) throw new Error("The snapshot is not ready to copy.");
1365 + await copySnapshot(
1366 + image.previewUrl,
1367 + formatComposerContextReference({ kind: "image", contextId, label: image.name }),
1368 + fragment,
1369 + );
1370 + };
1371 + const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot);
1372 const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment);
1373 const {
1374 loadThreadTranscript,
@@ -2416,6 +2439,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2439 allImages={composerImages}
2440 resolvedTheme={resolvedTheme}
2441 onExpandImage={onExpandImage}
2442 + onCopySnapshot={copyComposerSnapshotStable}
2443 onShowMore={expandMobileComposer}
2444 />
2445 ) : null;
@@ -2760,9 +2784,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2784 const { files, folders } = splitTransferredFiles(event.clipboardData);
2785 const plainText = event.clipboardData.getData("text/plain");
2786 const bypassAutoAttachment = consumePasteAsTextShortcut(pasteAsTextShortcutUntilRef);
2787 + const pastedContext =
2788 + !isLiteralPendingAnswer &&
2789 + (readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0;
2790 if (
2791 folders.length > 0 ||
2765 - (activeThreadId &&
2792 + (!pastedContext &&
2793 + activeThreadId &&
2794 files.length > 0 &&
2795 shouldHandleComposerAttachmentPaste({ files, plainText }))
2796 ) {
@@ -2775,7 +2803,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
2803 return;
2804 }
2805
2778 - if ((readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0) return;
2806 + if (pastedContext) return;
2807 if (!foldPastedText(plainText, bypassAutoAttachment)) {
2808 return;
2809 }
@@ -3315,6 +3343,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
3343 environmentId={environmentId}
3344 attachmentDraftTarget={attachmentDraftTarget}
3345 canOperateThread={canOperateThread}
3346 + onCopySnapshot={copyComposerSnapshotStable}
3347 onExpandImage={onExpandImage}
3348 onFileOpen={onFileOpen}
3349 onRemoveImage={removeComposerImage}
apps/web/src/components/chat/ComposerAttachmentStrip.tsx
+7 -2
@@ -149,6 +149,7 @@ export function ComposerAttachmentMediaRow({
149 attachmentDraftTarget,
150 canOperateThread,
151 onExpandImage,
152 + onCopySnapshot,
153 onFileOpen,
154 onRemoveImage,
155 onRemoveFile,
@@ -167,6 +168,7 @@ export function ComposerAttachmentMediaRow({
168 readonly environmentId: EnvironmentId;
169 readonly attachmentDraftTarget: ScopedThreadRef | DraftId;
170 readonly canOperateThread: boolean;
171 + readonly onCopySnapshot: (imageId: string) => Promise<void>;
172 readonly onExpandImage: (preview: ExpandedImagePreview) => void;
173 readonly onFileOpen: (attachment: ChatFileAttachment) => void;
174 readonly onRemoveImage: (imageId: string) => void;
@@ -224,7 +226,7 @@ export function ComposerAttachmentMediaRow({
226 aria-label={`Preview ${image.name}`}
227 disabled={!ready}
228 onClick={() => {
227 - const preview = buildExpandedImagePreview(images, image.id);
229 + const preview = buildExpandedImagePreview(images, image.id, onCopySnapshot);
230 if (!preview) return;
231 onExpandImage(preview);
232 }}
@@ -254,6 +256,7 @@ export function ComposerAttachmentMediaRow({
256 {source?.kind === "snap-shot" ? (
257 <SnapShotAttachmentDetails
258 source={source}
259 + onCopySnapshot={ready ? () => onCopySnapshot(ready.id) : undefined}
260 className={cn(
261 upload?.status === "uploading" && "bottom-4",
262 upload?.status === "failed" && "bottom-8",
@@ -586,11 +589,13 @@ export function ComposerRestingImagePreviews({
589 allImages,
590 resolvedTheme,
591 onExpandImage,
592 + onCopySnapshot,
593 onShowMore,
594 }: {
595 readonly images: ReadonlyArray<ComposerImageAttachment>;
596 readonly allImages: ReadonlyArray<ComposerImageAttachment>;
597 readonly resolvedTheme: "light" | "dark";
598 + readonly onCopySnapshot: (imageId: string) => Promise<void>;
599 readonly onExpandImage: (preview: ExpandedImagePreview) => void;
600 readonly onShowMore: () => void;
601 }) {
@@ -606,7 +611,7 @@ export function ComposerRestingImagePreviews({
611 aria-label={`Preview ${image.name}`}
612 onPointerDown={(event) => event.preventDefault()}
613 onClick={() => {
609 - const preview = buildExpandedImagePreview(allImages, image.id);
614 + const preview = buildExpandedImagePreview(allImages, image.id, onCopySnapshot);
615 if (preview) onExpandImage(preview);
616 }}
617 >
apps/web/src/components/chat/ExpandedImageDialog.tsx
+6
@@ -18,6 +18,7 @@ import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
18 import { isContextMenuOpen } from "../../contextMenuFallback";
19 import {
20 SnapShotAccessibilityData,
21 + SnapShotCopyButton,
22 SnapShotContentsButton,
23 snapShotAccessibilityDetails,
24 } from "./SnapShotAttachmentDetails";
@@ -277,6 +278,11 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
278 ) : item.source ? (
279 <SnapShotContentsButton source={item.source} side="top" />
280 ) : null}
281 + {item.onCopySnapshot && (
282 + <div className="-my-1.5">
283 + <SnapShotCopyButton onCopy={item.onCopySnapshot} />
284 + </div>
285 + )}
286 </div>
287 </div>
288 </MediaActions>
apps/web/src/components/chat/ExpandedImagePreview.tsx
+8 -1
@@ -27,6 +27,7 @@ export interface ExpandedImageItem {
27 /** Revoke this owned object URL; other blob URLs belong to the timeline. */
28 ownsSrc?: boolean;
29 source?: SnapShotSource;
30 + onCopySnapshot?: (() => Promise<void>) | undefined;
31 autoPlay?: boolean;
32 originalUrl?: string;
33 srcFragment?: string;
@@ -162,6 +163,7 @@ export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () =>
163 export function buildExpandedImagePreview(
164 images: ReadonlyArray<ChatImageAttachment | ComposerFileAttachment>,
165 selectedImageId: string,
166 + onCopySnapshot?: (id: string) => Promise<void>,
167 ): ExpandedImagePreview | null {
168 const selected = images.find((image) => image.id === selectedImageId);
169 if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) {
@@ -202,7 +204,12 @@ export function buildExpandedImagePreview(
204 src: image.src,
205 name: image.name,
206 mimeType: image.mimeType,
205 - ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}),
207 + ...(image.source?.kind === "snap-shot"
208 + ? {
209 + source: image.source,
210 + onCopySnapshot: onCopySnapshot ? () => onCopySnapshot(image.id) : undefined,
211 + }
212 + : {}),
213 })),
214 index: selectedIndex,
215 };
apps/web/src/components/chat/SnapShotAttachmentDetails.tsx
+71 -4
@@ -1,6 +1,6 @@
1 import type { SnapShotSource } from "@t3tools/contracts";
2 -import { ImageIcon, TextIcon } from "lucide-react";
3 -import { Suspense } from "react";
2 +import { CheckIcon, CopyIcon, ImageIcon, TextIcon } from "lucide-react";
3 +import { Suspense, useRef, useState } from "react";
4
5 import { useRenderedAppearance } from "../../hooks/useTheme";
6 import { cn } from "../../lib/utils";
@@ -8,6 +8,10 @@ import { RenderErrorBoundary } from "../RenderErrorBoundary";
8 import { Button } from "../ui/button";
9 import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
10 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
11 +import {
12 + ANCHORED_COPY_TOAST_TIMEOUT_MS,
13 + showAnchoredCopyErrorToast,
14 +} from "../ui/anchoredCopyToast";
15 import { HighlightedTokens } from "./HighlightedTokens";
16
17 export const SNAP_SHOT_ATTACHMENT_FRAME_CLASS =
@@ -71,14 +75,64 @@ function snapShotIncludesAccessibility(source: SnapShotSource): boolean {
75 return Boolean(source.accessibility || source.accessibleText?.trim());
76 }
77
78 +export function SnapShotCopyButton({
79 + onCopy,
80 + side = "top",
81 +}: {
82 + onCopy: () => Promise<void>;
83 + side?: "top" | "right" | "bottom" | "left";
84 +}) {
85 + const ref = useRef<HTMLButtonElement>(null);
86 + const [copied, setCopied] = useState(false);
87 +
88 + return (
89 + <Tooltip>
90 + <TooltipTrigger
91 + render={
92 + <Button
93 + ref={ref}
94 + aria-label="Copy snapshot"
95 + disabled={copied}
96 + onClick={(event) => {
97 + event.stopPropagation();
98 + void onCopy().then(
99 + () => {
100 + setCopied(true);
101 + setTimeout(() => setCopied(false), ANCHORED_COPY_TOAST_TIMEOUT_MS);
102 + },
103 + (error: unknown) =>
104 + showAnchoredCopyErrorToast(
105 + ref,
106 + error instanceof Error ? error : new Error(String(error)),
107 + ),
108 + );
109 + }}
110 + size="xs"
111 + variant="ghost-muted"
112 + />
113 + }
114 + >
115 + {copied ? (
116 + <CheckIcon className="size-3 text-primary" aria-hidden="true" />
117 + ) : (
118 + <CopyIcon className="size-3" aria-hidden="true" />
119 + )}
120 + </TooltipTrigger>
121 + <TooltipPopup side={side}>Copy snapshot</TooltipPopup>
122 + </Tooltip>
123 + );
124 +}
125 +
126 export function SnapShotContentsButton({
127 source,
128 className,
129 side = "top",
130 + onCopySnapshot,
131 }: {
132 source: SnapShotSource;
133 className?: string;
134 side?: "top" | "right" | "bottom" | "left";
135 + onCopySnapshot?: (() => Promise<void>) | undefined;
136 }) {
137 const includesAccessibility = snapShotIncludesAccessibility(source);
138 const ContentsIcon = includesAccessibility ? TextIcon : ImageIcon;
@@ -111,7 +165,14 @@ export function SnapShotContentsButton({
165 </Tooltip>
166 <PopoverPopup side={side} align="center" width="md">
167 <div className="max-h-[min(28rem,70vh)] space-y-2 overflow-y-auto">
114 - <PopoverTitle>Accessibility data</PopoverTitle>
168 + <div className="flex items-center justify-between gap-2">
169 + <PopoverTitle>Accessibility data</PopoverTitle>
170 + {onCopySnapshot && (
171 + <div className="-my-1.5">
172 + <SnapShotCopyButton onCopy={onCopySnapshot} side={side} />
173 + </div>
174 + )}
175 + </div>
176 {accessibilityDetails ? (
177 <SnapShotAccessibilityData
178 details={accessibilityDetails}
@@ -135,9 +196,11 @@ export function SnapShotContentsButton({
196
197 export function SnapShotAttachmentDetails({
198 source,
199 + onCopySnapshot,
200 className,
201 }: {
202 source: SnapShotSource;
203 + onCopySnapshot?: (() => Promise<void>) | undefined;
204 className?: string;
205 }) {
206 return (
@@ -157,7 +220,11 @@ export function SnapShotAttachmentDetails({
220 <div className="min-w-0 flex-1">
221 <div className="flex min-w-0 items-center gap-1.5 text-2xs font-medium leading-3.5 text-white">
222 <span className="truncate">{source.appName}</span>
160 - <SnapShotContentsButton source={source} className="pointer-events-auto" />
223 + <SnapShotContentsButton
224 + source={source}
225 + className="pointer-events-auto"
226 + onCopySnapshot={onCopySnapshot}
227 + />
228 </div>
229 <div className="truncate text-3xs leading-3.5 text-white/70">
230 {source.windowTitle || "Captured window"}
apps/web/src/components/chat/TimelineUserMessageRow.tsx
+26 -3
@@ -1,3 +1,4 @@
1 +import { copySnapshot as copySnapshotToClipboard } from "./copySnapshot";
2 import { InlineAudioPreview } from "../files/InlineAudioPreview";
3 import { filePreviewKind } from "@t3tools/shared/filePreview";
4 import {
@@ -597,6 +598,25 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
598 });
599 const copyText =
600 resolvedContext.text || mediaCopyRecords.map(formatComposerContextReference).join("\n");
601 + const copySnapshot = async (id: string) => {
602 + const image = userImages.find((candidate) => candidate.id === id);
603 + const record = resolvedContext.records
604 + .map(asKnownContextRecord)
605 + .find((candidate) => candidate?.kind === "image" && candidate.attachmentId === id);
606 + if (!image?.previewUrl || record?.kind !== "image")
607 + throw new Error("Snapshot is no longer available.");
608 + const fragment = encodeComposerContextFragment({
609 + version: 1,
610 + source: { environmentId: ctx.activeThreadEnvironmentId, messageId: row.message.id },
611 + records: [{ ...record, ...(image.source ? { source: image.source } : {}) }],
612 + });
613 + if (!fragment) throw new Error("The snapshot is too large to copy.");
614 + await copySnapshotToClipboard(
615 + image.previewUrl,
616 + formatComposerContextReference(record),
617 + fragment,
618 + );
619 + };
620 const onBodyCopyCapture = (event: React.ClipboardEvent<HTMLDivElement>) => {
621 if (resolvedContext.records.length === 0 || !event.clipboardData) return;
622 const selection = window.getSelection();
@@ -661,7 +681,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
681 annotationImage={annotationImage}
682 attachment={attachment}
683 onExpandImage={(image) => {
664 - const preview = buildExpandedImagePreview(userImages, image.id);
684 + const preview = buildExpandedImagePreview(userImages, image.id, copySnapshot);
685 if (preview) onImageExpand(preview);
686 }}
687 onOpenFile={onFileOpen}
@@ -737,7 +757,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
757 className="block h-full w-full cursor-zoom-in"
758 aria-label={`Preview ${image.name}`}
759 onClick={() => {
740 - const preview = buildExpandedImagePreview(regularImages, image.id);
760 + const preview = buildExpandedImagePreview(regularImages, image.id, copySnapshot);
761 if (!preview) return;
762 ctx.onImageExpand(preview);
763 }}
@@ -772,7 +792,10 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
792 </div>
793 )}
794 {image.previewUrl && image.source?.kind === "snap-shot" ? (
775 - <SnapShotAttachmentDetails source={image.source} />
795 + <SnapShotAttachmentDetails
796 + source={image.source}
797 + onCopySnapshot={() => copySnapshot(image.id)}
798 + />
799 ) : null}
800 </div>
801 ))}
apps/web/src/components/chat/copySnapshot.test.ts new
+75
@@ -0,0 +1,75 @@
1 +import { ComposerContextId, EnvironmentId } from "@t3tools/contracts";
2 +import {
3 + COMPOSER_CONTEXT_CLIPBOARD_MIME,
4 + decodeComposerContextClipboardHtml,
5 + encodeComposerContextFragment,
6 +} from "@t3tools/shared/composerContextClipboard";
7 +import { expect, it } from "vite-plus/test";
8 +
9 +import { copySnapshot } from "./copySnapshot";
10 +
11 +it.each([false, true])(
12 + "preserves image bytes and snapshot metadata, custom format rejected: %s",
13 + async (rejectCustom) => {
14 + class Item {
15 + constructor(readonly data: Record<string, Blob | Promise<Blob>>) {}
16 + }
17 + let clipboard: Item | undefined;
18 + const write = async ([item]: Item[]) => {
19 + if (rejectCustom && item?.data[COMPOSER_CONTEXT_CLIPBOARD_MIME])
20 + throw new Error("Unsupported format");
21 + clipboard = item;
22 + await Promise.all(Object.values(item!.data));
23 + };
24 + const originals = {
25 + navigator: Object.getOwnPropertyDescriptor(globalThis, "navigator"),
26 + ClipboardItem: Object.getOwnPropertyDescriptor(globalThis, "ClipboardItem"),
27 + };
28 + using _globals = {
29 + [Symbol.dispose]() {
30 + for (const key of ["navigator", "ClipboardItem"] as const) {
31 + const descriptor = originals[key];
32 + if (descriptor) Object.defineProperty(globalThis, key, descriptor);
33 + else Reflect.deleteProperty(globalThis, key);
34 + }
35 + },
36 + };
37 + Object.defineProperty(globalThis, "navigator", {
38 + configurable: true,
39 + value: { clipboard: { write } },
40 + });
41 + Object.defineProperty(globalThis, "ClipboardItem", { configurable: true, value: Item });
42 + const fragment = encodeComposerContextFragment({
43 + version: 1,
44 + source: { environmentId: EnvironmentId.make("snapshot-source") },
45 + records: [
46 + {
47 + version: 1,
48 + kind: "image",
49 + contextId: ComposerContextId.make("image_snapshot"),
50 + label: "Snapshot",
51 + attachmentId: "snapshot",
52 + name: "snapshot.png",
53 + mimeType: "image/png",
54 + sizeBytes: 3,
55 + source: {
56 + kind: "snap-shot",
57 + capturedAt: "2026-01-01T00:00:00.000Z",
58 + appName: "Finder",
59 + windowTitle: "Downloads",
60 + accessibleText: "Downloads",
61 + },
62 + },
63 + ],
64 + })!;
65 + const image = "data:image/png;base64,AQID";
66 + await copySnapshot(image, "Snapshot", fragment);
67 + const png = await clipboard!.data["image/png"]!;
68 + expect(png.type).toBe("image/png");
69 + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual([1, 2, 3]);
70 + expect(
71 + decodeComposerContextClipboardHtml(await (await clipboard!.data["text/html"]!).text()),
72 + ).toEqual(JSON.parse(fragment));
73 + expect(await (await clipboard!.data["text/plain"]!).text()).toBe("Snapshot");
74 + },
75 +);
apps/web/src/components/chat/copySnapshot.ts new
+33
@@ -0,0 +1,33 @@
1 +import {
2 + COMPOSER_CONTEXT_CLIPBOARD_MIME,
3 + encodeComposerContextClipboardHtml,
4 +} from "@t3tools/shared/composerContextClipboard";
5 +
6 +import { readMediaPng } from "../media/mediaContent";
7 +
8 +export async function copySnapshot(src: string, text: string, fragment: string): Promise<void> {
9 + if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") {
10 + throw new Error("Snapshot copying requires a secure browser connection.");
11 + }
12 + const formats = {
13 + "image/png": readMediaPng(src),
14 + "text/plain": new Blob([text], { type: "text/plain" }),
15 + "text/html": new Blob([encodeComposerContextClipboardHtml(text, fragment)], {
16 + type: "text/html",
17 + }),
18 + };
19 + // Start writing within the click gesture, before the image fetch or decode completes.
20 + try {
21 + await navigator.clipboard.write([
22 + new ClipboardItem({
23 + ...formats,
24 + [COMPOSER_CONTEXT_CLIPBOARD_MIME]: new Blob([fragment], {
25 + type: COMPOSER_CONTEXT_CLIPBOARD_MIME,
26 + }),
27 + }),
28 + ]);
29 + } catch {
30 + // HTML carries the same snapshot metadata when custom clipboard formats are unsupported.
31 + await navigator.clipboard.write([new ClipboardItem(formats)]);
32 + }
33 +}
apps/web/src/components/chat/useComposerContextImport.browser.test.tsx
+16 -1
@@ -48,6 +48,7 @@ function deferred<T>() {
48 it.each([
49 { kind: "file", edit: "delete" },
50 { kind: "image", edit: "delete" },
51 + { kind: "image", edit: "retain" },
52 { kind: "file", edit: "undo" },
53 { kind: "file", edit: "retain" },
54 { kind: "file", edit: "switch" },
@@ -124,7 +125,17 @@ it.each([
125 const attachment = (
126 kind === "file"
127 ? { ...attachmentFields, kind: "file" as const }
127 - : { ...attachmentFields, kind: "image" as const }
128 + : {
129 + ...attachmentFields,
130 + kind: "image" as const,
131 + source: {
132 + kind: "snap-shot" as const,
133 + appName: "Finder",
134 + windowTitle: "Downloads",
135 + capturedAt: "2026-01-01T00:00:00.000Z",
136 + accessibleText: "Downloads",
137 + },
138 + }
139 ) satisfies ComposerContextRecord;
140 const annotation = {
141 version: 1,
@@ -182,6 +193,10 @@ it.each([
193 const draft = store.getComposerDraft(target)!;
194 expect(draft.prompt).toBe(promptRef.current);
195 const attachments = kind === "file" ? draft.files : draft.images;
196 + if (kind === "image" && edit === "retain")
197 + expect(draft.images[0]?.source).toEqual(
198 + attachment.kind === "image" ? attachment.source : undefined,
199 + );
200 expect(attachments.map((file) => file.name)).toEqual(
201 edit === "undo" ||
202 edit === "retain" ||
apps/web/src/components/chat/useComposerContextImport.ts
+1
@@ -246,6 +246,7 @@ export function useComposerContextImport(input: {
246 sizeBytes: file.size,
247 previewUrl: URL.createObjectURL(file),
248 file,
249 + ...(record.source ? { source: record.source } : {}),
250 },
251 ],
252 {
packages/contracts/src/composerContext.ts
+3
@@ -1,5 +1,7 @@
1 import * as Schema from "effect/Schema";
2
3 +import { SnapShotSource } from "./chatAttachment.ts";
4 +
5 import { AssistantCitation } from "./assistantCitations.ts";
6 import {
7 EnvironmentId,
@@ -99,6 +101,7 @@ export const ImageContextRecord = Schema.Struct({
101 ...recordBase,
102 kind: Schema.Literal("image"),
103 ...attachmentBinding,
104 + source: Schema.optional(SnapShotSource),
105 });
106 export type ImageContextRecord = typeof ImageContextRecord.Type;
107