diff --git a/apps/mobile/src/lib/composerContextClipboard.test.ts b/apps/mobile/src/lib/composerContextClipboard.test.ts index 77305cce84439..41cdcf3078bce 100644 --- a/apps/mobile/src/lib/composerContextClipboard.test.ts +++ b/apps/mobile/src/lib/composerContextClipboard.test.ts @@ -73,6 +73,13 @@ const image = { mimeType: "image/png", sizeBytes: 7, attachmentId: "source-file", + source: { + kind: "snap-shot" as const, + capturedAt: "2026-01-01T00:00:00.000Z", + appName: "Finder", + windowTitle: "Downloads", + accessibleText: "Downloads", + }, }; const terminal = { version: 1 as const, @@ -146,7 +153,11 @@ describe("mobile context clipboard imports", () => { const signal = { aborted: false } as AbortSignal; const result = await importComposerContextClipboard(clipboard, 0, signal); expect(result?.failures).toEqual([]); - expect(result?.attachments[0]).toMatchObject({ fileUri: "file:///owned.png", sizeBytes: 42 }); + expect(result?.attachments[0]).toMatchObject({ + fileUri: "file:///owned.png", + sizeBytes: 42, + source: image.source, + }); expect(result?.context.records[0]).toMatchObject({ contextId: "import-1", attachmentId: result?.attachments[0]?.id, diff --git a/apps/mobile/src/lib/composerContextClipboard.ts b/apps/mobile/src/lib/composerContextClipboard.ts index 8c5a31cdb7e44..e744a2cbbf96a 100644 --- a/apps/mobile/src/lib/composerContextClipboard.ts +++ b/apps/mobile/src/lib/composerContextClipboard.ts @@ -253,6 +253,11 @@ async function persistImportedAttachment( sizeBytes: new File(fileUri).size, }; return record.kind === "image" - ? { ...common, type: "image", previewUri: fileUri } + ? { + ...common, + type: "image", + previewUri: fileUri, + ...(record.source ? { source: record.source } : {}), + } : { ...common, type: "file" }; } diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx index 692a135ad883b..58e9a29b57c1d 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx @@ -7,7 +7,17 @@ import { useRetainedContextPayloads, } from "./chat/composerContextUndo"; -import type { ServerProviderSkill } from "@t3tools/contracts"; +import { + ComposerContextId, + type ComposerContextRecord, + EnvironmentId, + type ServerProviderSkill, +} from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextFragment, +} from "@t3tools/shared/composerContextClipboard"; +import { formatComposerContextReference } from "@t3tools/shared/composerContextReferences"; import type { Editor } from "@tiptap/core"; import { redoDepth, undoDepth } from "@tiptap/pm/history"; import { NodeSelection } from "@tiptap/pm/state"; @@ -54,12 +64,16 @@ function Composer({ skills, onChange, onCommandKeyDown = () => false, + importContextFragment, + literalText = false, }: { value: string; richTextEnabled: boolean; skills: ReadonlyArray; onChange?: ComposerPromptEditorProps["onChange"] | undefined; onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"] | undefined; + importContextFragment?: ComposerPromptEditorProps["importContextFragment"]; + literalText?: boolean; }) { const [prompt, setPrompt] = useState({ value, cursor: value.length }); return ( @@ -78,6 +92,8 @@ function Composer({ }} onPaste={() => {}} onCommandKeyDown={onCommandKeyDown} + importContextFragment={importContextFragment} + literalText={literalText} /> ); } @@ -482,6 +498,55 @@ it.each([ expect(serializeEditorDoc(editor.state.doc).value).toBe(pasted); }); +it.each([ + { literalText: false, imports: 1 }, + { literalText: true, imports: 0 }, +])( + "imports a copied snapshot's context over its image file: %o", + async ({ literalText, imports }) => { + const record = { + version: 1, + kind: "image", + contextId: ComposerContextId.make("image_snapshot"), + label: "shot.png", + attachmentId: "snapshot", + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, + } satisfies ComposerContextRecord; + const importContextFragment = vi.fn(() => new Map([[record.contextId, record.contextId]])); + await using screen = await render( + , + ); + const editor = editorIn(screen.container); + await act(async () => { + const clipboardData = new DataTransfer(); + clipboardData.items.add( + new File([new Uint8Array([1, 2, 3])], "shot.png", { type: "image/png" }), + ); + clipboardData.setData("text/plain", formatComposerContextReference(record)); + clipboardData.setData( + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextFragment({ + version: 1, + source: { environmentId: EnvironmentId.make("snapshot-source") }, + records: [record], + })!, + ); + editor.view.dom.dispatchEvent( + new ClipboardEvent("paste", { clipboardData, cancelable: true }), + ); + }); + expect(importContextFragment).toHaveBeenCalledTimes(imports); + }, +); + it.each(["x\r\n\r\ny", "x\r\ry"])( "pastes %j with the same blank line as x\\n\\ny", async (text) => { diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx index d89f57068e9cf..a2d2c54333c28 100644 --- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx +++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx @@ -136,7 +136,11 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { ComposerCodeBlockNodeView } from "./chat/ComposerCodeBlockNodeView"; import { resolveAssistantCitationCommentDismissal } from "./chat/assistantCitationCommentDismissal"; import { composerCodeBlockHighlight } from "./composerCodeBlockHighlight"; -import { importPastedComposerText, readPastedPlainText } from "./composerInlineTokenPaste"; +import { + importPastedComposerText, + readPastedComposerContext, + readPastedPlainText, +} from "./composerInlineTokenPaste"; import { didComposerSelectionChangeVisibly } from "./composerSelection"; import { captureComposerAttachmentInsertion, @@ -1398,7 +1402,12 @@ function ComposerPromptEditorTiptapInner( }, handlePaste: (view, event) => { const clipboardData = event.clipboardData; - if (!clipboardData || clipboardData.files.length > 0) return false; + if (!clipboardData) return false; + if ( + clipboardData.files.length > 0 && + (literalText || !readPastedComposerContext(clipboardData)?.records.length) + ) + return false; const pastedText = readPastedPlainText(clipboardData); if (!pastedText) return false; event.preventDefault(); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index be3144efb40b9..8fb98edc14466 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -1,3 +1,4 @@ +import { copySnapshot } from "./copySnapshot"; import { VoiceInputSetup } from "./VoiceInputSetup"; import { useComposerTypingGuard } from "./useComposerTypingGuard"; import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences"; @@ -148,6 +149,7 @@ import { type TerminalContextSelection, } from "../../lib/terminalContext"; import { + formatComposerContextReference, threadContextRecord, toKindScopedComposerContextId, } from "@t3tools/shared/composerContextReferences"; @@ -717,7 +719,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) cwd: gitCwd ?? undefined, projectId: activeProjectId ?? undefined, expandImage: (imageId: string) => { - const preview = buildExpandedImagePreview(composerImages, imageId); + const preview = buildExpandedImagePreview( + composerImages, + imageId, + copyComposerSnapshotStable, + ); if (preview) onExpandImage(preview); }, openFile: setPreviewFileId, @@ -1334,7 +1340,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) : null); if (!record) return []; if (attachment.file) copiedFiles.set(record.attachmentId, attachment.file); - return [record]; + return [ + record.kind === "image" && attachment.type === "image" && attachment.source + ? { ...record, source: attachment.source } + : record, + ]; }), ]; if (records.length === 0) return null; @@ -1346,6 +1356,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) if (fragment) rememberCopiedComposerAttachments(environmentId, copiedFiles); return fragment; }; + const copyComposerSnapshot = async (imageId: string) => { + const image = composerImages.find((candidate) => candidate.id === imageId); + if (!image) throw new Error("Snapshot is no longer available."); + const contextId = toKindScopedComposerContextId("image", image.id); + const fragment = buildContextClipboardFragment([contextId]); + if (!fragment || !image.previewUrl) throw new Error("The snapshot is not ready to copy."); + await copySnapshot( + image.previewUrl, + formatComposerContextReference({ kind: "image", contextId, label: image.name }), + fragment, + ); + }; + const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot); const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment); const { loadThreadTranscript, @@ -2416,6 +2439,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) allImages={composerImages} resolvedTheme={resolvedTheme} onExpandImage={onExpandImage} + onCopySnapshot={copyComposerSnapshotStable} onShowMore={expandMobileComposer} /> ) : null; @@ -2760,9 +2784,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) const { files, folders } = splitTransferredFiles(event.clipboardData); const plainText = event.clipboardData.getData("text/plain"); const bypassAutoAttachment = consumePasteAsTextShortcut(pasteAsTextShortcutUntilRef); + const pastedContext = + !isLiteralPendingAnswer && + (readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0; if ( folders.length > 0 || - (activeThreadId && + (!pastedContext && + activeThreadId && files.length > 0 && shouldHandleComposerAttachmentPaste({ files, plainText })) ) { @@ -2775,7 +2803,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) return; } - if ((readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0) return; + if (pastedContext) return; if (!foldPastedText(plainText, bypassAutoAttachment)) { return; } @@ -3315,6 +3343,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) environmentId={environmentId} attachmentDraftTarget={attachmentDraftTarget} canOperateThread={canOperateThread} + onCopySnapshot={copyComposerSnapshotStable} onExpandImage={onExpandImage} onFileOpen={onFileOpen} onRemoveImage={removeComposerImage} diff --git a/apps/web/src/components/chat/ComposerAttachmentStrip.tsx b/apps/web/src/components/chat/ComposerAttachmentStrip.tsx index ca98b4d2f6c79..59115804e4972 100644 --- a/apps/web/src/components/chat/ComposerAttachmentStrip.tsx +++ b/apps/web/src/components/chat/ComposerAttachmentStrip.tsx @@ -149,6 +149,7 @@ export function ComposerAttachmentMediaRow({ attachmentDraftTarget, canOperateThread, onExpandImage, + onCopySnapshot, onFileOpen, onRemoveImage, onRemoveFile, @@ -167,6 +168,7 @@ export function ComposerAttachmentMediaRow({ readonly environmentId: EnvironmentId; readonly attachmentDraftTarget: ScopedThreadRef | DraftId; readonly canOperateThread: boolean; + readonly onCopySnapshot: (imageId: string) => Promise; readonly onExpandImage: (preview: ExpandedImagePreview) => void; readonly onFileOpen: (attachment: ChatFileAttachment) => void; readonly onRemoveImage: (imageId: string) => void; @@ -224,7 +226,7 @@ export function ComposerAttachmentMediaRow({ aria-label={`Preview ${image.name}`} disabled={!ready} onClick={() => { - const preview = buildExpandedImagePreview(images, image.id); + const preview = buildExpandedImagePreview(images, image.id, onCopySnapshot); if (!preview) return; onExpandImage(preview); }} @@ -254,6 +256,7 @@ export function ComposerAttachmentMediaRow({ {source?.kind === "snap-shot" ? ( onCopySnapshot(ready.id) : undefined} className={cn( upload?.status === "uploading" && "bottom-4", upload?.status === "failed" && "bottom-8", @@ -586,11 +589,13 @@ export function ComposerRestingImagePreviews({ allImages, resolvedTheme, onExpandImage, + onCopySnapshot, onShowMore, }: { readonly images: ReadonlyArray; readonly allImages: ReadonlyArray; readonly resolvedTheme: "light" | "dark"; + readonly onCopySnapshot: (imageId: string) => Promise; readonly onExpandImage: (preview: ExpandedImagePreview) => void; readonly onShowMore: () => void; }) { @@ -606,7 +611,7 @@ export function ComposerRestingImagePreviews({ aria-label={`Preview ${image.name}`} onPointerDown={(event) => event.preventDefault()} onClick={() => { - const preview = buildExpandedImagePreview(allImages, image.id); + const preview = buildExpandedImagePreview(allImages, image.id, onCopySnapshot); if (preview) onExpandImage(preview); }} > diff --git a/apps/web/src/components/chat/ExpandedImageDialog.tsx b/apps/web/src/components/chat/ExpandedImageDialog.tsx index 2d70c5f2ef1dc..a539994d01862 100644 --- a/apps/web/src/components/chat/ExpandedImageDialog.tsx +++ b/apps/web/src/components/chat/ExpandedImageDialog.tsx @@ -18,6 +18,7 @@ import { MediaVideoPlayer } from "../media/MediaVideoPlayer"; import { isContextMenuOpen } from "../../contextMenuFallback"; import { SnapShotAccessibilityData, + SnapShotCopyButton, SnapShotContentsButton, snapShotAccessibilityDetails, } from "./SnapShotAttachmentDetails"; @@ -277,6 +278,11 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({ ) : item.source ? ( ) : null} + {item.onCopySnapshot && ( +
+ +
+ )} diff --git a/apps/web/src/components/chat/ExpandedImagePreview.tsx b/apps/web/src/components/chat/ExpandedImagePreview.tsx index 0eec6753f8e96..ddd2990000119 100644 --- a/apps/web/src/components/chat/ExpandedImagePreview.tsx +++ b/apps/web/src/components/chat/ExpandedImagePreview.tsx @@ -27,6 +27,7 @@ export interface ExpandedImageItem { /** Revoke this owned object URL; other blob URLs belong to the timeline. */ ownsSrc?: boolean; source?: SnapShotSource; + onCopySnapshot?: (() => Promise) | undefined; autoPlay?: boolean; originalUrl?: string; srcFragment?: string; @@ -162,6 +163,7 @@ export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () => export function buildExpandedImagePreview( images: ReadonlyArray, selectedImageId: string, + onCopySnapshot?: (id: string) => Promise, ): ExpandedImagePreview | null { const selected = images.find((image) => image.id === selectedImageId); if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) { @@ -202,7 +204,12 @@ export function buildExpandedImagePreview( src: image.src, name: image.name, mimeType: image.mimeType, - ...(image.source?.kind === "snap-shot" ? { source: image.source } : {}), + ...(image.source?.kind === "snap-shot" + ? { + source: image.source, + onCopySnapshot: onCopySnapshot ? () => onCopySnapshot(image.id) : undefined, + } + : {}), })), index: selectedIndex, }; diff --git a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx index 07f16c8794e98..826b335469d09 100644 --- a/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx +++ b/apps/web/src/components/chat/SnapShotAttachmentDetails.tsx @@ -1,6 +1,6 @@ import type { SnapShotSource } from "@t3tools/contracts"; -import { ImageIcon, TextIcon } from "lucide-react"; -import { Suspense } from "react"; +import { CheckIcon, CopyIcon, ImageIcon, TextIcon } from "lucide-react"; +import { Suspense, useRef, useState } from "react"; import { useRenderedAppearance } from "../../hooks/useTheme"; import { cn } from "../../lib/utils"; @@ -8,6 +8,10 @@ import { RenderErrorBoundary } from "../RenderErrorBoundary"; import { Button } from "../ui/button"; import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; +import { + ANCHORED_COPY_TOAST_TIMEOUT_MS, + showAnchoredCopyErrorToast, +} from "../ui/anchoredCopyToast"; import { HighlightedTokens } from "./HighlightedTokens"; export const SNAP_SHOT_ATTACHMENT_FRAME_CLASS = @@ -71,14 +75,64 @@ function snapShotIncludesAccessibility(source: SnapShotSource): boolean { return Boolean(source.accessibility || source.accessibleText?.trim()); } +export function SnapShotCopyButton({ + onCopy, + side = "top", +}: { + onCopy: () => Promise; + side?: "top" | "right" | "bottom" | "left"; +}) { + const ref = useRef(null); + const [copied, setCopied] = useState(false); + + return ( + + { + event.stopPropagation(); + void onCopy().then( + () => { + setCopied(true); + setTimeout(() => setCopied(false), ANCHORED_COPY_TOAST_TIMEOUT_MS); + }, + (error: unknown) => + showAnchoredCopyErrorToast( + ref, + error instanceof Error ? error : new Error(String(error)), + ), + ); + }} + size="xs" + variant="ghost-muted" + /> + } + > + {copied ? ( + + Copy snapshot + + ); +} + export function SnapShotContentsButton({ source, className, side = "top", + onCopySnapshot, }: { source: SnapShotSource; className?: string; side?: "top" | "right" | "bottom" | "left"; + onCopySnapshot?: (() => Promise) | undefined; }) { const includesAccessibility = snapShotIncludesAccessibility(source); const ContentsIcon = includesAccessibility ? TextIcon : ImageIcon; @@ -111,7 +165,14 @@ export function SnapShotContentsButton({
- Accessibility data +
+ Accessibility data + {onCopySnapshot && ( +
+ +
+ )} +
{accessibilityDetails ? ( Promise) | undefined; className?: string; }) { return ( @@ -157,7 +220,11 @@ export function SnapShotAttachmentDetails({
{source.appName} - +
{source.windowTitle || "Captured window"} diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx index 163635a9b6748..7cd57ed18c0c1 100644 --- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx +++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx @@ -1,3 +1,4 @@ +import { copySnapshot as copySnapshotToClipboard } from "./copySnapshot"; import { InlineAudioPreview } from "../files/InlineAudioPreview"; import { filePreviewKind } from "@t3tools/shared/filePreview"; import { @@ -597,6 +598,25 @@ function UserTimelineRow({ row }: { row: Extract { + const image = userImages.find((candidate) => candidate.id === id); + const record = resolvedContext.records + .map(asKnownContextRecord) + .find((candidate) => candidate?.kind === "image" && candidate.attachmentId === id); + if (!image?.previewUrl || record?.kind !== "image") + throw new Error("Snapshot is no longer available."); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: ctx.activeThreadEnvironmentId, messageId: row.message.id }, + records: [{ ...record, ...(image.source ? { source: image.source } : {}) }], + }); + if (!fragment) throw new Error("The snapshot is too large to copy."); + await copySnapshotToClipboard( + image.previewUrl, + formatComposerContextReference(record), + fragment, + ); + }; const onBodyCopyCapture = (event: React.ClipboardEvent) => { if (resolvedContext.records.length === 0 || !event.clipboardData) return; const selection = window.getSelection(); @@ -661,7 +681,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(userImages, image.id); + const preview = buildExpandedImagePreview(userImages, image.id, copySnapshot); if (preview) onImageExpand(preview); }} onOpenFile={onFileOpen} @@ -737,7 +757,7 @@ function UserTimelineRow({ row }: { row: Extract { - const preview = buildExpandedImagePreview(regularImages, image.id); + const preview = buildExpandedImagePreview(regularImages, image.id, copySnapshot); if (!preview) return; ctx.onImageExpand(preview); }} @@ -772,7 +792,10 @@ function UserTimelineRow({ row }: { row: Extract )} {image.previewUrl && image.source?.kind === "snap-shot" ? ( - + copySnapshot(image.id)} + /> ) : null}
))} diff --git a/apps/web/src/components/chat/copySnapshot.test.ts b/apps/web/src/components/chat/copySnapshot.test.ts new file mode 100644 index 0000000000000..9ac7080f7b0f7 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.test.ts @@ -0,0 +1,75 @@ +import { ComposerContextId, EnvironmentId } from "@t3tools/contracts"; +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + decodeComposerContextClipboardHtml, + encodeComposerContextFragment, +} from "@t3tools/shared/composerContextClipboard"; +import { expect, it } from "vite-plus/test"; + +import { copySnapshot } from "./copySnapshot"; + +it.each([false, true])( + "preserves image bytes and snapshot metadata, custom format rejected: %s", + async (rejectCustom) => { + class Item { + constructor(readonly data: Record>) {} + } + let clipboard: Item | undefined; + const write = async ([item]: Item[]) => { + if (rejectCustom && item?.data[COMPOSER_CONTEXT_CLIPBOARD_MIME]) + throw new Error("Unsupported format"); + clipboard = item; + await Promise.all(Object.values(item!.data)); + }; + const originals = { + navigator: Object.getOwnPropertyDescriptor(globalThis, "navigator"), + ClipboardItem: Object.getOwnPropertyDescriptor(globalThis, "ClipboardItem"), + }; + using _globals = { + [Symbol.dispose]() { + for (const key of ["navigator", "ClipboardItem"] as const) { + const descriptor = originals[key]; + if (descriptor) Object.defineProperty(globalThis, key, descriptor); + else Reflect.deleteProperty(globalThis, key); + } + }, + }; + Object.defineProperty(globalThis, "navigator", { + configurable: true, + value: { clipboard: { write } }, + }); + Object.defineProperty(globalThis, "ClipboardItem", { configurable: true, value: Item }); + const fragment = encodeComposerContextFragment({ + version: 1, + source: { environmentId: EnvironmentId.make("snapshot-source") }, + records: [ + { + version: 1, + kind: "image", + contextId: ComposerContextId.make("image_snapshot"), + label: "Snapshot", + attachmentId: "snapshot", + name: "snapshot.png", + mimeType: "image/png", + sizeBytes: 3, + source: { + kind: "snap-shot", + capturedAt: "2026-01-01T00:00:00.000Z", + appName: "Finder", + windowTitle: "Downloads", + accessibleText: "Downloads", + }, + }, + ], + })!; + const image = "data:image/png;base64,AQID"; + await copySnapshot(image, "Snapshot", fragment); + const png = await clipboard!.data["image/png"]!; + expect(png.type).toBe("image/png"); + expect([...new Uint8Array(await png.arrayBuffer())]).toEqual([1, 2, 3]); + expect( + decodeComposerContextClipboardHtml(await (await clipboard!.data["text/html"]!).text()), + ).toEqual(JSON.parse(fragment)); + expect(await (await clipboard!.data["text/plain"]!).text()).toBe("Snapshot"); + }, +); diff --git a/apps/web/src/components/chat/copySnapshot.ts b/apps/web/src/components/chat/copySnapshot.ts new file mode 100644 index 0000000000000..118121e753647 --- /dev/null +++ b/apps/web/src/components/chat/copySnapshot.ts @@ -0,0 +1,33 @@ +import { + COMPOSER_CONTEXT_CLIPBOARD_MIME, + encodeComposerContextClipboardHtml, +} from "@t3tools/shared/composerContextClipboard"; + +import { readMediaPng } from "../media/mediaContent"; + +export async function copySnapshot(src: string, text: string, fragment: string): Promise { + if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") { + throw new Error("Snapshot copying requires a secure browser connection."); + } + const formats = { + "image/png": readMediaPng(src), + "text/plain": new Blob([text], { type: "text/plain" }), + "text/html": new Blob([encodeComposerContextClipboardHtml(text, fragment)], { + type: "text/html", + }), + }; + // Start writing within the click gesture, before the image fetch or decode completes. + try { + await navigator.clipboard.write([ + new ClipboardItem({ + ...formats, + [COMPOSER_CONTEXT_CLIPBOARD_MIME]: new Blob([fragment], { + type: COMPOSER_CONTEXT_CLIPBOARD_MIME, + }), + }), + ]); + } catch { + // HTML carries the same snapshot metadata when custom clipboard formats are unsupported. + await navigator.clipboard.write([new ClipboardItem(formats)]); + } +} diff --git a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx index e2bf9676b8b6d..d48e965895ddd 100644 --- a/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx +++ b/apps/web/src/components/chat/useComposerContextImport.browser.test.tsx @@ -48,6 +48,7 @@ function deferred() { it.each([ { kind: "file", edit: "delete" }, { kind: "image", edit: "delete" }, + { kind: "image", edit: "retain" }, { kind: "file", edit: "undo" }, { kind: "file", edit: "retain" }, { kind: "file", edit: "switch" }, @@ -124,7 +125,17 @@ it.each([ const attachment = ( kind === "file" ? { ...attachmentFields, kind: "file" as const } - : { ...attachmentFields, kind: "image" as const } + : { + ...attachmentFields, + kind: "image" as const, + source: { + kind: "snap-shot" as const, + appName: "Finder", + windowTitle: "Downloads", + capturedAt: "2026-01-01T00:00:00.000Z", + accessibleText: "Downloads", + }, + } ) satisfies ComposerContextRecord; const annotation = { version: 1, @@ -182,6 +193,10 @@ it.each([ const draft = store.getComposerDraft(target)!; expect(draft.prompt).toBe(promptRef.current); const attachments = kind === "file" ? draft.files : draft.images; + if (kind === "image" && edit === "retain") + expect(draft.images[0]?.source).toEqual( + attachment.kind === "image" ? attachment.source : undefined, + ); expect(attachments.map((file) => file.name)).toEqual( edit === "undo" || edit === "retain" || diff --git a/apps/web/src/components/chat/useComposerContextImport.ts b/apps/web/src/components/chat/useComposerContextImport.ts index 0cc66d6a71d19..265278faec747 100644 --- a/apps/web/src/components/chat/useComposerContextImport.ts +++ b/apps/web/src/components/chat/useComposerContextImport.ts @@ -246,6 +246,7 @@ export function useComposerContextImport(input: { sizeBytes: file.size, previewUrl: URL.createObjectURL(file), file, + ...(record.source ? { source: record.source } : {}), }, ], { diff --git a/packages/contracts/src/composerContext.ts b/packages/contracts/src/composerContext.ts index 982202b05bc30..9884284b3f0db 100644 --- a/packages/contracts/src/composerContext.ts +++ b/packages/contracts/src/composerContext.ts @@ -1,5 +1,7 @@ import * as Schema from "effect/Schema"; +import { SnapShotSource } from "./chatAttachment.ts"; + import { AssistantCitation } from "./assistantCitations.ts"; import { EnvironmentId, @@ -99,6 +101,7 @@ export const ImageContextRecord = Schema.Struct({ ...recordBase, kind: Schema.Literal("image"), ...attachmentBinding, + source: Schema.optional(SnapShotSource), }); export type ImageContextRecord = typeof ImageContextRecord.Type;