diff --git a/apps/web/src/components/ChatView.browser.test.tsx b/apps/web/src/components/ChatView.browser.test.tsx index dc4908867a3e..f4d04196ef73 100644 --- a/apps/web/src/components/ChatView.browser.test.tsx +++ b/apps/web/src/components/ChatView.browser.test.tsx @@ -1,5 +1,6 @@ import { createMemoryHistory } from "@tanstack/react-router"; import { scopeProjectRef } from "@t3tools/client-runtime/environment"; +import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/text-paste"; import { ThreadId } from "@t3tools/contracts"; import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; import type * as Context from "effect/Context"; @@ -91,3 +92,46 @@ it("opens the thread actions menu for an older settled thread the shell leaves o .element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 }) .toBeVisible(); }); + +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 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"); +}); diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx index cba7c434b1ba..8f801d63a634 100644 --- a/apps/web/src/components/chat/ChatComposer.tsx +++ b/apps/web/src/components/chat/ChatComposer.tsx @@ -11,7 +11,7 @@ import { import { isLocalEnvironmentDisabled } from "../../localEnvironment"; import { usePrimaryEnvironmentId } from "../../state/environments"; import { useRightPanelStore } from "~/rightPanelStore"; -import { importPastedComposerText, readPastedComposerContext } from "../composerInlineTokenPaste"; +import { importPastedComposerText, pastesCopiedComposerContext } from "../composerInlineTokenPaste"; import { questionAttachmentDraftId, trackOpenQuestionAttachmentDraft, @@ -2654,7 +2654,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps) return; } - if ((readPastedComposerContext(event.clipboardData)?.records.length ?? 0) > 0) return; + if (pastesCopiedComposerContext(event.clipboardData)) return; if (!foldPastedText(plainText, bypassAutoAttachment)) { return; } diff --git a/apps/web/src/components/chat/usePasteToFocusComposer.ts b/apps/web/src/components/chat/usePasteToFocusComposer.ts index 84672cc570c5..c616aa085a89 100644 --- a/apps/web/src/components/chat/usePasteToFocusComposer.ts +++ b/apps/web/src/components/chat/usePasteToFocusComposer.ts @@ -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"; @@ -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 }) ) { diff --git a/apps/web/src/components/composerInlineTokenPaste.test.ts b/apps/web/src/components/composerInlineTokenPaste.test.ts index 1ab28a5b7ff5..db5b4460341d 100644 --- a/apps/web/src/components/composerInlineTokenPaste.test.ts +++ b/apps/web/src/components/composerInlineTokenPaste.test.ts @@ -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"), @@ -57,3 +57,34 @@ describe("importPastedComposerText", () => { ); }); }); + +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); + }); +}); diff --git a/apps/web/src/components/composerInlineTokenPaste.ts b/apps/web/src/components/composerInlineTokenPaste.ts index 0862f81ee835..aa4b52381865 100644 --- a/apps/web/src/components/composerInlineTokenPaste.ts +++ b/apps/web/src/components/composerInlineTokenPaste.ts @@ -20,14 +20,16 @@ import { readThreadShell } from "~/state/entities"; const threadTitle = (ref: ScopedThreadRef) => readThreadShell(ref)?.title; +const readCopiedComposerContext = (clipboardData: Pick) => + decodeComposerContextFragment(clipboardData.getData(COMPOSER_CONTEXT_CLIPBOARD_MIME)) ?? + decodeComposerContextClipboardHtml(clipboardData.getData("text/html")); + /** Pasted thread links count as context too, so they become chips like a copied chip does. */ -export function readPastedComposerContext( +function readPastedComposerContext( clipboardData: Pick, + 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 ?? [], @@ -59,6 +61,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): boolean { + const copied = readCopiedComposerContext(clipboardData); + if (copied === null) return false; + const copiedIds = new Set(copied.records.map((record) => record.contextId)); + return ( + readPastedComposerContext(clipboardData, copied)?.records.some((record) => + copiedIds.has(record.contextId), + ) ?? false + ); +} + export function importPastedComposerText( clipboardData: Pick, importContextFragment?: ( diff --git a/apps/web/test/fakeEnvironment.ts b/apps/web/test/fakeEnvironment.ts index e878a3965343..514a0c5be4fd 100644 --- a/apps/web/test/fakeEnvironment.ts +++ b/apps/web/test/fakeEnvironment.ts @@ -9,6 +9,7 @@ import { MessageId, ORCHESTRATION_PROTOCOL_VERSION, ORCHESTRATION_V2_WS_METHODS, + PROVIDER_SEND_TURN_MAX_FILE_BYTES, ProjectId, ProviderInstanceId, RunId, @@ -59,7 +60,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",