Skip to content
44 changes: 44 additions & 0 deletions apps/web/src/components/ChatView.browser.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createMemoryHistory } from "@tanstack/react-router";
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/text-paste";
import {
EnvironmentId,
ORCHESTRATION_V2_WS_METHODS,
Expand Down Expand Up @@ -662,3 +663,46 @@ it("selecting a side-chat tab focuses that side chat's composer", async () => {
await expect.element(sideEditor).toHaveFocus();
await expect.element(mainEditor).toHaveTextContent("main draft");
});

it("folds a large paste that mentions a thread link into a pasted-text attachment", async () => {
using _environment = installFakeEnvironment([]);
__setClientSettingsForTests({
...DEFAULT_CLIENT_SETTINGS,
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
});
const draftId = DraftId.make("draft-30577");
useComposerDraftStore
.getState()
.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, {
threadId: ThreadId.make("thread-30577"),
});
const log = [
...Array.from(
{ length: 400 },
(_, line) => `2026-11-03T12:00:00Z worker[${line}] retrying job`,
),
`see https://app.t3.codes/threads/${ENVIRONMENT_ID}/thread-abc for context`,
...Array.from({ length: 400 }, (_, line) => `2026-11-03T12:00:01Z worker[${line}] job failed`),
].join("\n");
expect(log.length).toBeGreaterThan(PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES);

const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] }));
await router.load();
await using _app = await render(<AppRoot router={router} />, {
// AppRoot brings its own router.
router: false,
});
await expect.element(page.getByTestId("composer-editor")).toBeVisible();
await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();
const editor = page.getByTestId("composer-editor").element() as HTMLElement;
editor.focus();

const clipboardData = new DataTransfer();
clipboardData.setData("text/plain", log);
editor.dispatchEvent(
new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }),
);

await expect.element(page.getByText("pasted-text.txt")).toBeVisible();
expect(editor.textContent).not.toContain("worker[399] retrying job");
});
8 changes: 6 additions & 2 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
import { isLocalEnvironmentDisabled } from "../../localEnvironment";
import { usePrimaryEnvironmentId } from "../../state/environments";
import { useRightPanelStore } from "~/rightPanelStore";
import { importPastedComposerText, readPastedComposerContext } from "../composerInlineTokenPaste";
import {
importPastedComposerText,
pastesCopiedComposerContext,
readPastedComposerContext,
} from "../composerInlineTokenPaste";
import {
questionAttachmentDraftId,
trackOpenQuestionAttachmentDraft,
Expand Down Expand Up @@ -2830,7 +2834,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
return;
}

if (pastedContext) return;
if (!isLiteralPendingAnswer && pastesCopiedComposerContext(event.clipboardData)) return;
if (!foldPastedText(plainText, bypassAutoAttachment)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/chat/usePasteToFocusComposer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pasteTextToFocusComposer, shouldRedirectInputToComposer } from "./composerFocusRedirect";
import { readPastedComposerContext } from "../composerInlineTokenPaste";
import { pastesCopiedComposerContext } from "../composerInlineTokenPaste";
import { isPasteAsTextShortcut } from "@t3tools/client-runtime/text-paste";
import { useEffect, useRef } from "react";
import { isMacPlatform } from "../../lib/utils";
Expand Down Expand Up @@ -38,7 +38,7 @@ export function usePasteToFocusComposer(
const bypassAutoAttachment = Date.now() <= pasteAsTextShortcutUntilRef.current;
pasteAsTextShortcutUntilRef.current = 0;
if (
((readPastedComposerContext(clipboardData)?.records.length ?? 0) === 0 &&
(!pastesCopiedComposerContext(clipboardData) &&
composerRef.current?.pasteTextAtEnd(text, { bypassAutoAttachment })) ||
composerRef.current?.insertTextAtEnd(text, { clipboardData })
) {
Expand Down
33 changes: 32 additions & 1 deletion apps/web/src/components/composerInlineTokenPaste.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
import { describe, expect, it } from "vite-plus/test";

import { importPastedComposerText } from "./composerInlineTokenPaste";
import { importPastedComposerText, pastesCopiedComposerContext } from "./composerInlineTokenPaste";

const thread = {
environmentId: EnvironmentId.make("env-1"),
Expand Down Expand Up @@ -63,3 +63,34 @@ describe("importPastedComposerText", () => {
).toBe("x\n\ny\nz");
});
});

describe("pastesCopiedComposerContext", () => {
it("counts a copied chip but not a bare thread link", () => {
const target = threadChipCopyTarget(thread, "Fix the login flow");
expect(
pastesCopiedComposerContext(
clipboard({
"text/plain": `See ${target.value}`,
[COMPOSER_CONTEXT_CLIPBOARD_MIME]: encodeComposerContextFragment(target.fragment!)!,
}),
),
).toBe(true);
expect(
pastesCopiedComposerContext(
clipboard({ "text/plain": "See https://app.t3.codes/threads/env-1/thread-1" }),
),
).toBe(false);
});

it("ignores a copied fragment the pasted text does not reference", () => {
const target = threadChipCopyTarget(thread, "Fix the login flow");
expect(
pastesCopiedComposerContext(
clipboard({
"text/plain": "See https://app.t3.codes/threads/env-1/thread-2",
[COMPOSER_CONTEXT_CLIPBOARD_MIME]: encodeComposerContextFragment(target.fragment!)!,
}),
),
).toBe(false);
});
});
20 changes: 17 additions & 3 deletions apps/web/src/components/composerInlineTokenPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import { readThreadShell } from "~/state/entities";

const threadTitle = (ref: ScopedThreadRef) => readThreadShell(ref)?.title;

const readCopiedComposerContext = (clipboardData: Pick<DataTransfer, "getData">) =>
decodeComposerContextFragment(clipboardData.getData(COMPOSER_CONTEXT_CLIPBOARD_MIME)) ??
decodeComposerContextClipboardHtml(clipboardData.getData("text/html"));

// Matches ProseMirror's default clipboard parser, which a custom handlePaste bypasses.
export function readPastedPlainText(clipboardData: Pick<DataTransfer, "getData">): string {
return clipboardData.getData("text/plain").replace(/\r\n?/g, "\n");
Expand All @@ -28,11 +32,9 @@ export function readPastedPlainText(clipboardData: Pick<DataTransfer, "getData">
/** Pasted thread links count as context too, so they become chips like a copied chip does. */
export function readPastedComposerContext(
clipboardData: Pick<DataTransfer, "getData">,
decodedFragment = readCopiedComposerContext(clipboardData),
): ComposerContextClipboardFragment | null {
const pastedText = clipboardData.getData("text/plain");
const decodedFragment =
decodeComposerContextFragment(clipboardData.getData(COMPOSER_CONTEXT_CLIPBOARD_MIME)) ??
decodeComposerContextClipboardHtml(clipboardData.getData("text/html"));
const threads = threadLinkContextReferences(
pastedText,
decodedFragment?.records ?? [],
Expand Down Expand Up @@ -64,6 +66,18 @@ export function readPastedComposerContext(
};
}

/** Whether the paste references context copied from T3, which folding it into a file would drop. */
export function pastesCopiedComposerContext(clipboardData: Pick<DataTransfer, "getData">): boolean {
const copied = readCopiedComposerContext(clipboardData);
if (copied === null) return false;
const copiedIds = new Set<string>(copied.records.map((record) => record.contextId));
return (
readPastedComposerContext(clipboardData, copied)?.records.some((record) =>
copiedIds.has(record.contextId),
) ?? false
);
}

export function importPastedComposerText(
clipboardData: Pick<DataTransfer, "getData">,
importContextFragment?: (
Expand Down
6 changes: 5 additions & 1 deletion apps/web/test/fakeEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
OrchestrationV2DispatchCommandError,
ORCHESTRATION_PROTOCOL_VERSION,
ORCHESTRATION_V2_WS_METHODS,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
ProjectId,
ProviderInstanceId,
RunId,
Expand Down Expand Up @@ -67,7 +68,10 @@ const descriptor = {
// A matching version, since a mismatch adds an update banner a typical user never sees.
serverVersion: APP_VERSION,
orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
capabilities: { repositoryIdentity: true },
capabilities: {
repositoryIdentity: true,
fileAttachments: { maxUploadBytes: PROVIDER_SEND_TURN_MAX_FILE_BYTES },
},
};
const auth = {
policy: "loopback-browser",
Expand Down
Loading