Skip to content
Open
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
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 } 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";
Expand Down Expand Up @@ -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 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");
});
4 changes: 2 additions & 2 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
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 @@ -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);
});
});
22 changes: 18 additions & 4 deletions apps/web/src/components/composerInlineTokenPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ 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"));

/** Pasted thread links count as context too, so they become chips like a copied chip does. */
export function readPastedComposerContext(
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 @@ -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<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 @@ -9,6 +9,7 @@ import {
MessageId,
ORCHESTRATION_PROTOCOL_VERSION,
ORCHESTRATION_V2_WS_METHODS,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
ProjectId,
ProviderInstanceId,
RunId,
Expand Down Expand Up @@ -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",
Expand Down
Loading