diff --git a/src/renderer/src/components/native-chat/NativeChatComposer.tsx b/src/renderer/src/components/native-chat/NativeChatComposer.tsx index f5a2f2c71..b255f787e 100644 --- a/src/renderer/src/components/native-chat/NativeChatComposer.tsx +++ b/src/renderer/src/components/native-chat/NativeChatComposer.tsx @@ -7,13 +7,16 @@ import { useRef, useState } from 'react' -import { translate } from '@/i18n/i18n' import { useAppStore } from '../../store' import type { AgentType } from '../../../../shared/agent-status-types' import { NATIVE_FILE_DROP_TARGET } from '../../../../shared/native-file-drop' import { sendRuntimePtyInput } from '@/runtime/runtime-terminal-inspection' import { getSettingsForAgentTabRuntimeOwner } from '@/lib/agent-paste-draft' -import { sendNativeChatMessage, submitNativeChatPrompt } from './native-chat-runtime-send' +import { + sendNativeChatMessage, + sendNativeChatMessageWithImageAttachments, + submitNativeChatPrompt +} from './native-chat-runtime-send' import { getAgentSlashCommands } from './native-chat-agent-commands' import { emitNativeChatMessageSent } from '@/lib/native-chat-telemetry' import { @@ -28,7 +31,8 @@ import { type HistoryState, type SlashCommandSuggestion } from './native-chat-composer-state' -import { resolveImagePaste } from './native-chat-image-paste' +import { readNativeChatDraftCache } from './native-chat-draft-cache' +import { useNativeChatDraft } from './use-native-chat-draft' import { NativeChatComposerField } from './NativeChatComposerField' import { nativeChatComposerTargetIsRemote, @@ -36,6 +40,7 @@ import { } from './native-chat-composer-target' import { useNativeChatSkills } from './use-native-chat-skills' import { useNativeChatComposerAttachments } from './use-native-chat-composer-attachments' +import { useNativeChatComposerPaste } from './use-native-chat-composer-paste' import { dispatchDictationControl } from '../dictation/dictation-control-events' import { useNativeChatComposerKeyDown } from './use-native-chat-composer-keydown' @@ -74,6 +79,17 @@ export type NativeChatComposerProps = { export type NativeChatComposerHandle = { focus: () => boolean insertTypedText: (text: string) => boolean + /** Handle a paste event captured at the pane root (the OS frequently + * retargets the paste off the focused textarea, so its own onPaste can't be + * relied on). An image is intercepted and attached; text falls through. */ + handlePasteEvent: (event: { + clipboardData: DataTransfer | null + preventDefault: () => void + defaultPrevented: boolean + }) => void + /** Paste the clipboard into the composer with no event in hand (menu paste): + * an image becomes an attachment, otherwise text is inserted at the caret. */ + pasteFromClipboard: () => void } /** @@ -98,8 +114,11 @@ export const NativeChatComposer = forwardRef(EMPTY_HISTORY) const [activeSuggestion, setActiveSuggestion] = useState(0) const [notice, setNotice] = useState(null) @@ -116,6 +135,15 @@ export const NativeChatComposer = forwardRef getAgentSlashCommands(agent), [agent]) const autocomplete = useMemo( () => @@ -173,7 +201,7 @@ export const NativeChatComposer = forwardRef { @@ -185,7 +213,21 @@ export const NativeChatComposer = forwardRef ({ focus, insertTypedText }), [focus, insertTypedText]) + const { handlePaste, pasteFromClipboard } = useNativeChatComposerPaste({ + agent, + disabled, + caret, + attachLocalPaths, + insertTypedText, + setCaret, + setNotice + }) + + useImperativeHandle( + ref, + () => ({ focus, insertTypedText, handlePasteEvent: handlePaste, pasteFromClipboard }), + [focus, insertTypedText, handlePaste, pasteFromClipboard] + ) useEffect(() => { return window.api.ui.onFileDrop((payload) => { @@ -236,17 +278,24 @@ export const NativeChatComposer = forwardRef 0) { + // Slash commands are TUI controls, not chat turns — never attach images to + // one (the chat turn is suppressed below, so the images would leak into the + // runtime with no visible message). Otherwise images are deferred to submit + // (like text) so the GUI chips and TUI input stay in sync and removing a + // chip needs no TUI un-paste: send images, then text, then Enter atomically. + const isSlashCommand = isSlashCommandDraft(text) + if (isSlashCommand) { + sendNativeChatMessage(target.settings, target.ptyId, text) + } else if (imagePaths.length > 0) { + sendNativeChatMessageWithImageAttachments(target.settings, target.ptyId, text, imagePaths) + } else if (text.trim().length > 0) { sendNativeChatMessage(target.settings, target.ptyId, text) } else { submitNativeChatPrompt(target.settings, target.ptyId) } - // Slash commands are TUI controls, not chat turns: don't echo a user bubble, - // but DO surface a small "Ran /clear" system line so the command leaves a - // visible trace instead of seeming to do nothing. - if (isSlashCommandDraft(text)) { + // Slash commands don't echo a user bubble, but DO surface a small + // "Ran /clear" system line so the command leaves a visible trace. + if (isSlashCommand) { onSlashCommand?.(text.trim()) } else { onOptimisticSend?.(text, imagePaths) @@ -270,7 +319,8 @@ export const NativeChatComposer = forwardRef { @@ -285,13 +335,16 @@ export const NativeChatComposer = forwardRef { - const next = applySlashSuggestion(command) - setDraft(next) - setCaret(next.length) - setActiveSuggestion(0) - textareaRef.current?.focus() - }, []) + const chooseSlash = useCallback( + (command: SlashCommandSuggestion) => { + const next = applySlashSuggestion(command) + setDraft(next) + setCaret(next.length) + setActiveSuggestion(0) + textareaRef.current?.focus() + }, + [setDraft] + ) const dispatchSlash = useCallback( (command: SlashCommandSuggestion) => { @@ -314,42 +367,7 @@ export const NativeChatComposer = forwardRef) => { - const hasImage = Array.from(event.clipboardData.items).some((item) => - item.type.startsWith('image/') - ) - if (!hasImage) { - return - } - event.preventDefault() - // Why: snapshot the caret before the async temp-file round-trip — `caret` - // state can move (further typing/selection) while the await is in flight. - const caretAtPaste = caret - void (async () => { - const tempPath = await window.api.ui.saveClipboardImageAsTempFile() - if (!tempPath) { - return - } - const result = resolveImagePaste(agent, tempPath) - if (result.kind === 'unsupported') { - setNotice( - translate( - 'components.native-chat.composer.imageUnsupported', - 'Image paste is not supported for this agent.' - ) - ) - return - } - attachLocalPaths([result.path]) - setCaret(caretAtPaste) - setNotice(null) - })() - }, - [agent, attachLocalPaths, caret] + [agent, disabled, resolveTarget, onSlashCommand, setDraft] ) const handleKeyDown = useNativeChatComposerKeyDown({ diff --git a/src/renderer/src/components/native-chat/NativeChatComposerField.tsx b/src/renderer/src/components/native-chat/NativeChatComposerField.tsx index 40cb7b6ad..d3149c8c4 100644 --- a/src/renderer/src/components/native-chat/NativeChatComposerField.tsx +++ b/src/renderer/src/components/native-chat/NativeChatComposerField.tsx @@ -4,6 +4,7 @@ import { translate } from '@/i18n/i18n' import { cn } from '@/lib/utils' import { NATIVE_FILE_DROP_TARGET } from '../../../../shared/native-file-drop' import { basename } from '@/lib/path' +import { isNativeChatPastedImagePath } from './native-chat-image-paste' import type { ComposerAutocomplete, SlashCommandSuggestion } from './native-chat-composer-state' import { NativeChatMentionHint, @@ -125,7 +126,14 @@ export function NativeChatComposerField({ title={attachment.path} > - {basename(attachment.path)} + + {isNativeChatPastedImagePath(attachment.path) + ? translate( + 'components.native-chat.composer.pastedImageLabel', + 'Pasted image' + ) + : basename(attachment.path)} +