Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion apps/mobile/src/lib/composerContextClipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion apps/mobile/src/lib/composerContextClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -54,12 +64,16 @@ function Composer({
skills,
onChange,
onCommandKeyDown = () => false,
importContextFragment,
literalText = false,
}: {
value: string;
richTextEnabled: boolean;
skills: ReadonlyArray<ServerProviderSkill>;
onChange?: ComposerPromptEditorProps["onChange"] | undefined;
onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"] | undefined;
importContextFragment?: ComposerPromptEditorProps["importContextFragment"];
literalText?: boolean;
}) {
const [prompt, setPrompt] = useState({ value, cursor: value.length });
return (
Expand All @@ -78,6 +92,8 @@ function Composer({
}}
onPaste={() => {}}
onCommandKeyDown={onCommandKeyDown}
importContextFragment={importContextFragment}
literalText={literalText}
/>
);
}
Expand Down Expand Up @@ -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(
<Composer
value=""
richTextEnabled
skills={[]}
literalText={literalText}
importContextFragment={importContextFragment}
/>,
);
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) => {
Expand Down
13 changes: 11 additions & 2 deletions apps/web/src/components/ComposerPromptEditorTiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 33 additions & 4 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { copySnapshot } from "./copySnapshot";
import { VoiceInputSetup } from "./VoiceInputSetup";
import { useComposerTypingGuard } from "./useComposerTypingGuard";
import { insertAttachmentReferencesForDraft } from "./composerAttachmentReferences";
Expand Down Expand Up @@ -148,6 +149,7 @@ import {
type TerminalContextSelection,
} from "../../lib/terminalContext";
import {
formatComposerContextReference,
threadContextRecord,
toKindScopedComposerContextId,
} from "@t3tools/shared/composerContextReferences";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await copySnapshot(
image.previewUrl,
formatComposerContextReference({ kind: "image", contextId, label: image.name }),
fragment,
);
};
const copyComposerSnapshotStable = useStableCallback(copyComposerSnapshot);
const buildContextClipboardFragmentStable = useStableCallback(buildContextClipboardFragment);
const {
loadThreadTranscript,
Expand Down Expand Up @@ -2416,6 +2439,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
allImages={composerImages}
resolvedTheme={resolvedTheme}
onExpandImage={onExpandImage}
onCopySnapshot={copyComposerSnapshotStable}
onShowMore={expandMobileComposer}
/>
) : null;
Expand Down Expand Up @@ -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 &&
Comment thread
Yash-Singh1 marked this conversation as resolved.
Resolved
activeThreadId &&
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
files.length > 0 &&
shouldHandleComposerAttachmentPaste({ files, plainText }))
) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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}
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/components/chat/ComposerAttachmentStrip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export function ComposerAttachmentMediaRow({
attachmentDraftTarget,
canOperateThread,
onExpandImage,
onCopySnapshot,
onFileOpen,
onRemoveImage,
onRemoveFile,
Expand All @@ -167,6 +168,7 @@ export function ComposerAttachmentMediaRow({
readonly environmentId: EnvironmentId;
readonly attachmentDraftTarget: ScopedThreadRef | DraftId;
readonly canOperateThread: boolean;
readonly onCopySnapshot: (imageId: string) => Promise<void>;
readonly onExpandImage: (preview: ExpandedImagePreview) => void;
readonly onFileOpen: (attachment: ChatFileAttachment) => void;
readonly onRemoveImage: (imageId: string) => void;
Expand Down Expand Up @@ -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);
}}
Expand Down Expand Up @@ -254,6 +256,7 @@ export function ComposerAttachmentMediaRow({
{source?.kind === "snap-shot" ? (
<SnapShotAttachmentDetails
source={source}
onCopySnapshot={ready ? () => onCopySnapshot(ready.id) : undefined}
className={cn(
upload?.status === "uploading" && "bottom-4",
upload?.status === "failed" && "bottom-8",
Expand Down Expand Up @@ -586,11 +589,13 @@ export function ComposerRestingImagePreviews({
allImages,
resolvedTheme,
onExpandImage,
onCopySnapshot,
onShowMore,
}: {
readonly images: ReadonlyArray<ComposerImageAttachment>;
readonly allImages: ReadonlyArray<ComposerImageAttachment>;
readonly resolvedTheme: "light" | "dark";
readonly onCopySnapshot: (imageId: string) => Promise<void>;
readonly onExpandImage: (preview: ExpandedImagePreview) => void;
readonly onShowMore: () => void;
}) {
Expand All @@ -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);
}}
>
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/chat/ExpandedImageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
import { isContextMenuOpen } from "../../contextMenuFallback";
import {
SnapShotAccessibilityData,
SnapShotCopyButton,
SnapShotContentsButton,
snapShotAccessibilityDetails,
} from "./SnapShotAttachmentDetails";
Expand Down Expand Up @@ -277,6 +278,11 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
) : item.source ? (
<SnapShotContentsButton source={item.source} side="top" />
) : null}
{item.onCopySnapshot && (
<div className="-my-1.5">
<SnapShotCopyButton onCopy={item.onCopySnapshot} />
</div>
)}
</div>
</div>
</MediaActions>
Expand Down
9 changes: 8 additions & 1 deletion apps/web/src/components/chat/ExpandedImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) | undefined;
autoPlay?: boolean;
originalUrl?: string;
srcFragment?: string;
Expand Down Expand Up @@ -162,6 +163,7 @@ export function attachVideoThumbnail(video: HTMLVideoElement, file: File): () =>
export function buildExpandedImagePreview(
images: ReadonlyArray<ChatImageAttachment | ComposerFileAttachment>,
selectedImageId: string,
onCopySnapshot?: (id: string) => Promise<void>,
): ExpandedImagePreview | null {
const selected = images.find((image) => image.id === selectedImageId);
if (selected?.type === "file" && selected.file && isVideoAttachment(selected)) {
Expand Down Expand Up @@ -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,
};
Expand Down
Loading
Loading