feat(composer): fold large pastes into attachments #10823
Closedchrisdeeming(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/10823/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/mobile/src/features/threads/ThreadComposer.tsx line 696OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -655,0 +676,21 @@676+ advertisedMax === undefined677+ ? null678+ : clampFileAttachmentUploadBytes(advertisedMax);679+ const wouldExceedInputLimit =680+ props.draftMessage.length -681+ Math.max(0, paste.selection.end - paste.selection.start) +682+ paste.text.length >683+ PROVIDER_SEND_TURN_MAX_INPUT_CHARS;684+ const canAttach =685+ maxBytes !== null &&686+ props.draftAttachments.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&687+ new TextEncoder().encode(paste.text).byteLength <= maxBytes;688+ if (689+ pastedTextDisposition({690+ text: paste.text,691+ wouldExceedInputLimit,692+ canAttach: true,693+ }) === "attachment"694+ ) {695+ if (canAttach) {696+ void props.onNativePasteText(paste.text);apps/mobile/src/features/threads/NewTaskDraftScreen.tsx line 1018Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -941,0 +975,21 @@975+ paste.text.length >976+ PROVIDER_SEND_TURN_MAX_INPUT_CHARS;977+ const canAttach =978+ maxBytes !== null &&979+ flow.attachments.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&980+ new TextEncoder().encode(paste.text).byteLength <= maxBytes;981+ if (982+ pastedTextDisposition({983+ text: paste.text,984+ wouldExceedInputLimit,985+ canAttach: true,986+ }) === "attachment"987+ ) {988+ if (canAttach && maxBytes !== null) {989+ try {990+ const attachment = await createPastedTextComposerAttachment({991+ text: paste.text,992+ name: nextPastedTextFileName(flow.attachments.map((item) => item.name)),993+ maxBytes,994+ });995+ if (flow.appendAttachments([attachment]) > 0) {apps/mobile/src/state/use-thread-composer-state.ts line 543Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -516,4 +521,5 @@516521 });517522 const rejectedPasteCount = appendComposerDraftAttachments(threadKey, result.images);518523 if (result.text) {519- appendComposerDraftText(threadKey, result.text);524+ const currentAttachments = composerDrafts[threadKey]?.attachments ?? [];525+ const advertisedMax =apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift lines 392 to 395OutdatedResolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -364,3 +388,8 @@364388 textView.onPasteImages = { [weak self] urls in365389 self?.onComposerPasteImages(["uris": urls])366390 }391+ textView.onPasteText = { [weak self] text, selection in392+ self?.onComposerPasteText([393+ "text": text,394+ "selection": ["start": selection.location, "end": selection.location + selection.length],395+ ])apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt line 259Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -244,3 +254,5 @@244254 updateInputFlags()245255 }246256257+ fun setInterceptTextPastes(intercept: Boolean) {258+ editor.interceptTextPastes = interceptapps/web/src/components/chat/ChatComposer.tsx line 4618OutdatedResolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -4515,0 +4598,21 @@4598+ const canStageAttachment =4599+ Boolean(activeThreadId) &&4600+ questionCanAttach &&4601+ hasAttachmentSlot &&4602+ fileStagingLimit !== null;4603+ if (!canStageAttachment) {4604+ if (!wouldExceedInputLimit) {4605+ return false;4606+ }4607+ toastManager.add({4608+ type: "error",4609+ title: "Pasted text is too large for this message",4610+ description: "Remove some text or an attachment, then paste again.",4611+ data: { hideCopyButton: true },4612+ });4613+ return true;4614+ }4615+4616+ const foldedFile = new File(4617+ [plainText],4618+ nextPastedTextFileName(composerFilesRef.current.map((file) => file.name)),apps/web/src/components/chat/ChatComposer.tsx line 1990OutdatedResolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -1970,3 +1979,12 @@19701979 */19711980 const pendingImageCompressionsRef = useRef<Map<string, number>>(new Map());197219811982+ useEffect(() => {1983+ const armPasteAsTextShortcut = () => {1984+ // Electron can deliver its native menu action just before the paste1985+ // event, while browsers normally deliver keydown first. A short deadline1986+ // bridges both event paths without leaving later pastes in bypass mode.1987+ pasteAsTextShortcutUntilRef.current = Date.now() + 1_000;1988+ };1989+ const onKeyDown = (event: KeyboardEvent) => {1990+ if (isPasteAsTextShortcut(event, isMacPlatform(navigator.platform))) {
- apps/mobile/src/features/threads/NewTaskDraftScreen.tsx lines 1018 to 1024Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -941,0 +977,21 @@977+ const canAttach =978+ maxBytes !== null &&979+ flow.attachments.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&980+ new TextEncoder().encode(paste.text).byteLength <= maxBytes;981+ if (982+ pastedTextDisposition({983+ text: paste.text,984+ wouldExceedInputLimit,985+ canAttach: true,986+ }) === "attachment"987+ ) {988+ if (canAttach && maxBytes !== null) {989+ try {990+ const attachment = await createPastedTextComposerAttachment({991+ text: paste.text,992+ name: nextPastedTextFileName(flow.attachments.map((item) => item.name)),993+ maxBytes,994+ });995+ if (flow.appendAttachments([attachment]) > 0) {996+ await removePersistedComposerAttachmentFile(attachment.fileUri);997+ }apps/mobile/src/state/use-thread-composer-state.ts lines 572 to 577Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -519,0 +532,21 @@532+ PROVIDER_SEND_TURN_MAX_INPUT_CHARS;533+ const shouldFold =534+ pastedTextDisposition({535+ text: result.text,536+ wouldExceedInputLimit,537+ canAttach: true,538+ }) === "attachment";539+ const canAttach =540+ maxBytes !== null &&541+ currentAttachments.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&542+ new TextEncoder().encode(result.text).byteLength <= maxBytes;543+ if (shouldFold && canAttach && maxBytes !== null) {544+ try {545+ const attachment = await createPastedTextComposerAttachment({546+ text: result.text,547+ name: nextPastedTextFileName(currentAttachments.map((item) => item.name)),548+ maxBytes,549+ });550+ if (appendComposerDraftAttachments(threadKey, [attachment]) > 0) {551+ await removePersistedComposerAttachmentFile(attachment.fileUri);552+ }apps/server/src/provider/Layers/ProviderService.ts line 1548Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1537,4 +1538,7 @@15371538 appendAttachmentContext(15381539 attachmentPath === null15391540 ? undefined1540- : `[Attached ${attachment.type} "${attachment.name}" is saved at: ${attachmentPath}]`,1541+ : attachment.type === "file" &&1542+ "source" in attachment &&1543+ attachment.source?._tag === "pasted-text"1544+ ? `[Pasted text "${attachment.name}" is saved at: ${attachmentPath}. Inspect it as needed.]`
- apps/mobile/src/native/T3ComposerEditor.tsx line 15Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -12,3 +12,4 @@1212 skills: _skills,1313 selection,1414 onPasteImages,15+ onPasteText: _onPasteText,apps/mobile/src/features/threads/NewTaskDraftScreen.tsx line 1018Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -941,0 +993,21 @@993+ wouldExceedInputLimit,994+ canAttach: maxBytes !== null,995+ }) === "attachment"996+ ) {997+ if (canAttach && maxBytes !== null) {998+ pendingPastedTextAttachmentCountRef.current += 1;999+ setPendingPastedTextAttachmentCount(pendingPastedTextAttachmentCountRef.current);1000+ try {1001+ if (pastedTextFileNamesRef.current.draftKey !== flow.draftKey) {1002+ pastedTextFileNamesRef.current = { draftKey: flow.draftKey, names: new Set() };1003+ }1004+ const reservedNames = pastedTextFileNamesRef.current.names;1005+ for (const attachment of flow.attachments) reservedNames.add(attachment.name);1006+ const name = nextPastedTextFileName([...reservedNames]);1007+ reservedNames.add(name);1008+ const attachment = await createPastedTextComposerAttachment({1009+ text: paste.text,1010+ name,1011+ maxBytes,1012+ });1013+ if (flow.appendAttachments([attachment]) > 0) {
- apps/web/src/components/chat/ChatComposer.tsx line 4598OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4513,3 +4584,15 @@45134584 // ------------------------------------------------------------------45144585 // Callbacks: paste / drag45154586 // ------------------------------------------------------------------4587+ const foldPastedText = (plainText: string, bypassAutoAttachment: boolean): boolean => {4588+ const questionCanAttach =4589+ !activePendingProgress ||4590+ (supportsQuestionAttachments &&4591+ activePendingProgress.activeQuestion?.allowCustomAnswer !== false &&4592+ !activePendingIsResponding);4593+ const hasAttachmentSlot =4594+ composerImagesRef.current.length +4595+ composerFilesRef.current.length +4596+ (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) <4597+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS;4598+ const wouldExceedInputLimit =
- apps/server/src/provider/acp/AntigravityAcpSupport.ts line 273OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -262,3 +262,12 @@262262 let totalBytes = 0;263263264264 for (const attachment of input.attachments ?? []) {265+ // ProviderService has already put the file path in the text block. Keep a266+ // folded clipboard paste lazy so the agent can search or sample it rather267+ // than paying to embed the entire resource in context immediately.268+ if (269+ attachment.type === "file" &&270+ "source" in attachment &&271+ attachment.source?._tag === "pasted-text"272+ ) {273+ continue;apps/web/src/components/chat/ChatComposer.tsx line 4886OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4731,3 +4882,5 @@47314882 focusComposer();47324883 },47334884 insertTextAtEnd: insertComposerTextAtEnd,4885+ pasteTextAtEnd: (text: string) => {4886+ const bypassAutoAttachment = Date.now() <= pasteAsTextShortcutUntilRef.current;
- apps/mobile/src/state/use-thread-composer-state.ts line 583OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -519,0 +563,21 @@563+ try {564+ const attachment = await createPastedTextComposerAttachment({565+ text: result.text,566+ name: reservePastedTextFileName(567+ threadKey,568+ currentAttachments.map((item) => item.name),569+ ),570+ maxBytes,571+ });572+ if (appendComposerDraftAttachments(threadKey, [attachment]) > 0) {573+ await removePersistedComposerAttachmentFile(attachment.fileUri);574+ setPendingConnectionError(575+ `You can attach up to ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} files per message.`,576+ );577+ }578+ } catch (error) {579+ setPendingConnectionError(580+ error instanceof Error ? error.message : "Could not attach pasted text.",581+ );582+ }583+ } else if (shouldFold && wouldExceedInputLimit) {apps/web/src/components/chat/ChatComposer.tsx line 4612Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4513,3 +4591,18 @@45134591 // ------------------------------------------------------------------45144592 // Callbacks: paste / drag45154593 // ------------------------------------------------------------------4594+ const foldPastedText = (plainText: string, bypassAutoAttachment: boolean): boolean => {4595+ const questionCanAttach =4596+ !activePendingProgress ||4597+ (supportsQuestionAttachments &&4598+ activePendingProgress.activeQuestion?.allowCustomAnswer !== false &&4599+ !activePendingIsResponding);4600+ const hasAttachmentSlot =4601+ composerImagesRef.current.length +4602+ composerFilesRef.current.length +4603+ (pendingImageCompressionsRef.current.get(attachmentTargetKey) ?? 0) <4604+ PROVIDER_SEND_TURN_MAX_ATTACHMENTS;4605+ const selection = composerEditorRef.current?.readSelectionRange();4606+ const wouldExceedInputLimit = wouldTextPasteExceedLimit({4607+ valueLength: promptRef.current.length,4608+ selection: selection ?? { start: 0, end: 0 },
- apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift lines 109 to 116Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -89,7 +93,21 @@93+ input: "v",94+ modifierFlags: [.command, .shift],95+ action: (pasteInline(_:))96+ )97+ pasteAsText.discoverabilityTitle = "Paste as Text"98+ pasteAsText.wantsPriorityOverSystemBehavior = true99+ commands.append(pasteAsText)100+ }89101 return commands90102 }9110392104 @objc private func submitMessage(_ sender: UIKeyCommand) {93105 onSubmit?()94106 }95107108+ @objc private func pasteInline(_ sender: UIKeyCommand) {109+ guard !isReadOnly else {110+ return111+ }112+ super.paste(sender)113+ }apps/mobile/src/features/threads/NewTaskDraftScreen.tsx line 994OutdatedResolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -941,0 +974,21 @@974+ const capabilities = selectedEnvironmentServerConfig?.environment.capabilities;975+ const advertisedMax =976+ capabilities?.attachmentUploads === true977+ ? capabilities.fileAttachments?.maxUploadBytes978+ : undefined;979+ const maxBytes =980+ advertisedMax === undefined ? null : clampFileAttachmentUploadBytes(advertisedMax);981+ const wouldExceedInputLimit =982+ flow.prompt.length -983+ Math.max(0, paste.selection.end - paste.selection.start) +984+ paste.text.length >985+ PROVIDER_SEND_TURN_MAX_INPUT_CHARS;986+ const canAttach =987+ maxBytes !== null &&988+ flow.attachments.length < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&989+ new TextEncoder().encode(paste.text).byteLength <= maxBytes;990+ if (991+ pastedTextDisposition({992+ text: paste.text,993+ wouldExceedInputLimit,994+ canAttach: maxBytes !== null,
- apps/mobile/src/features/threads/NewTaskDraftScreen.tsx lines 1039 to 1048Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -941,0 +1026,21 @@1026+ Alert.alert(1027+ "Could not attach pasted text",1028+ error instanceof Error ? error.message : "Try again.",1029+ );1030+ } finally {1031+ pendingPastedTextAttachmentCountRef.current = Math.max(1032+ 0,1033+ pendingPastedTextAttachmentCountRef.current - 1,1034+ );1035+ setPendingPastedTextAttachmentCount(pendingPastedTextAttachmentCountRef.current);1036+ }1037+ } else {1038+ Alert.alert(1039+ wouldExceedInputLimit1040+ ? "Pasted text is too large for this message"1041+ : "Could not attach pasted text",1042+ wouldExceedInputLimit1043+ ? "Remove some text or an attachment, then paste again."1044+ : "Remove an attachment or use a smaller paste, then try again.",1045+ );1046+ }
- apps/mobile/src/state/use-thread-composer-state.ts line 649Resolvedby chrisdeeming(GitHub profile, opens in a new tab)@@ -554,3 +633,17 @@554633 [composerDrafts, selectedThreadShell],555634 );556635636+ const onNativePasteText = useCallback(637+ async (text: string) => {638+ if (!selectedThreadShell) return;639+ const capabilities = selectedEnvironmentRuntime?.serverConfig?.environment.capabilities;640+ const advertisedMax =641+ capabilities?.attachmentUploads === true642+ ? capabilities.fileAttachments?.maxUploadBytes643+ : undefined;644+ if (advertisedMax === undefined) return;645+646+ const threadKey = scopedThreadKey(selectedThreadShell.environmentId, selectedThreadShell.id);647+ const currentAttachments = composerDrafts[threadKey]?.attachments ?? [];648+ try {649+ const attachment = await createPastedTextComposerAttachment({
chrisdeeming(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed