From d7873241e0955268d287497223aabc641c39fd3a Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Sun, 28 Dec 2025 22:52:16 -0500 Subject: [PATCH] Add image and PDF attachment support with drag-drop and paste --- frontend/src/api/types.ts | 8 + .../src/components/message/PromptInput.tsx | 201 +++++++++++++++--- .../session/SessionDetailHeader.tsx | 9 +- frontend/src/hooks/useOpenCode.ts | 33 ++- frontend/src/lib/promptParser.ts | 17 +- frontend/src/pages/SessionDetail.tsx | 5 + 6 files changed, 227 insertions(+), 46 deletions(-) diff --git a/frontend/src/api/types.ts b/frontend/src/api/types.ts index 074b08fa..54545f52 100644 --- a/frontend/src/api/types.ts +++ b/frontend/src/api/types.ts @@ -159,9 +159,17 @@ export type SSEEvent = export type ContentPart = | { type: 'text', content: string } | { type: 'file', path: string, name: string } + | { type: 'image', id: string, filename: string, mime: string, dataUrl: string } export interface FileInfo { path: string name: string mime?: string } + +export interface ImageAttachment { + id: string + filename: string + mime: string + dataUrl: string +} diff --git a/frontend/src/components/message/PromptInput.tsx b/frontend/src/components/message/PromptInput.tsx index 7d27bc66..1c2f8326 100644 --- a/frontend/src/components/message/PromptInput.tsx +++ b/frontend/src/components/message/PromptInput.tsx @@ -10,7 +10,7 @@ import { useUserBash } from '@/stores/userBashStore' import { useMobile } from '@/hooks/useMobile' import { useSessionStatusForSession } from '@/stores/sessionStatusStore' import { usePermissionContext } from '@/contexts/PermissionContext' -import { ChevronDown, Square } from 'lucide-react' +import { ChevronDown, Square, Upload, X } from 'lucide-react' import { CommandSuggestions } from '@/components/command/CommandSuggestions' import { MentionSuggestions, type MentionItem } from './MentionSuggestions' @@ -19,7 +19,10 @@ import { detectMentionTrigger, parsePromptToParts, getFilename, filterAgentsByQu import type { components } from '@/api/opencode-types' -import type { MessageWithParts, FileInfo } from '@/api/types' +import type { MessageWithParts, FileInfo, ImageAttachment } from '@/api/types' + +const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"] +const ACCEPTED_FILE_TYPES = [...ACCEPTED_IMAGE_TYPES, "application/pdf"] type CommandType = components['schemas']['Command'] @@ -27,6 +30,7 @@ type CommandType = components['schemas']['Command'] export interface PromptInputHandle { setPromptValue: (value: string) => void clearPrompt: () => void + triggerFileUpload: () => void } interface PromptInputProps { @@ -72,11 +76,15 @@ export const PromptInput = forwardRef(funct clearPrompt: () => { setPrompt('') setAttachedFiles(new Map()) + setImageAttachments([]) setSelectedAgent(null) if (textareaRef.current) { textareaRef.current.style.height = 'auto' textareaRef.current.focus() } + }, + triggerFileUpload: () => { + fileInputRef.current?.click() } }), []) @@ -84,6 +92,8 @@ export const PromptInput = forwardRef(funct const [showSuggestions, setShowSuggestions] = useState(false) const [suggestionQuery, setSuggestionQuery] = useState('') const [attachedFiles, setAttachedFiles] = useState(new Map()) + const [imageAttachments, setImageAttachments] = useState([]) + const [isDragging, setIsDragging] = useState(false) const [showMentionSuggestions, setShowMentionSuggestions] = useState(false) const [mentionQuery, setMentionQuery] = useState('') const [mentionRange, setMentionRange] = useState<{ start: number, end: number } | null>(null) @@ -92,6 +102,7 @@ export const PromptInput = forwardRef(funct const [selectedAgent, setSelectedAgent] = useState(null) const textareaRef = useRef(null) + const fileInputRef = useRef(null) const sendPrompt = useSendPrompt(opcodeUrl, directory) const sendShell = useSendShell(opcodeUrl, directory) const abortSession = useAbortSession(opcodeUrl, directory, sessionID) @@ -145,10 +156,11 @@ export const PromptInput = forwardRef(funct const { addUserBashCommand } = useUserBash() const handleSubmit = () => { - if (!prompt.trim() || disabled) return - + if (disabled) return + if (!prompt.trim() && imageAttachments.length === 0) return + if (hasActiveStream) { - const parts = parsePromptToParts(prompt, attachedFiles) + const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments) sendPrompt.mutate({ sessionID, parts, @@ -157,6 +169,7 @@ export const PromptInput = forwardRef(funct }) setPrompt('') setAttachedFiles(new Map()) + setImageAttachments([]) setSelectedAgent(null) if (textareaRef.current) { textareaRef.current.style.height = 'auto' @@ -197,8 +210,8 @@ export const PromptInput = forwardRef(funct } } - const parts = parsePromptToParts(prompt, attachedFiles) - + const parts = parsePromptToParts(prompt, attachedFiles, imageAttachments) + sendPrompt.mutate({ sessionID, parts, @@ -208,6 +221,7 @@ export const PromptInput = forwardRef(funct setPrompt('') setAttachedFiles(new Map()) + setImageAttachments([]) setSelectedAgent(null) if (textareaRef.current) { textareaRef.current.style.height = 'auto' @@ -319,6 +333,83 @@ export const PromptInput = forwardRef(funct updateSettings({ mode: newMode }) } + const addImageAttachment = (file: File) => { + if (!ACCEPTED_FILE_TYPES.includes(file.type)) return + + const reader = new FileReader() + reader.onload = () => { + const dataUrl = reader.result as string + const attachment: ImageAttachment = { + id: crypto.randomUUID(), + filename: file.name, + mime: file.type, + dataUrl, + } + setImageAttachments((prev) => [...prev, attachment]) + } + reader.onerror = () => { + console.error('Failed to read file:', file.name) + } + reader.readAsDataURL(file) + } + + const removeImageAttachment = (id: string) => { + setImageAttachments((prev) => prev.filter((a) => a.id !== id)) + } + + const handlePaste = async (event: React.ClipboardEvent) => { + const clipboardData = event.clipboardData + if (!clipboardData) return + + const items = Array.from(clipboardData.items) + const imageItems = items.filter((item) => ACCEPTED_FILE_TYPES.includes(item.type)) + + if (imageItems.length > 0) { + event.preventDefault() + for (const item of imageItems) { + const file = item.getAsFile() + if (file) addImageAttachment(file) + } + } + } + + const handleDragOver = (event: React.DragEvent) => { + event.preventDefault() + event.stopPropagation() + if (event.dataTransfer?.types.includes('Files')) { + setIsDragging(true) + } + } + + const handleDragLeave = (event: React.DragEvent) => { + event.preventDefault() + event.stopPropagation() + setIsDragging(false) + } + + const handleDrop = async (event: React.DragEvent) => { + event.preventDefault() + event.stopPropagation() + setIsDragging(false) + + const files = event.dataTransfer?.files + if (files) { + for (const file of Array.from(files)) { + if (ACCEPTED_FILE_TYPES.includes(file.type)) { + addImageAttachment(file) + } + } + } + } + + const handleFileInputChange = (event: React.ChangeEvent) => { + const file = event.currentTarget.files?.[0] + if (file) { + addImageAttachment(file) + } + event.currentTarget.value = '' + } + const handleKeyDown = (e: KeyboardEvent) => { if (isBashMode && e.key === 'Escape') { e.preventDefault() @@ -405,6 +496,7 @@ export const PromptInput = forwardRef(funct setMentionQuery('') setMentionRange(null) setPrompt('') + setImageAttachments([]) if (textareaRef.current) { textareaRef.current.style.height = 'auto' } @@ -514,26 +606,50 @@ return ( )} - +