Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(chat): accept dropped and pasted files on the assistant composer

Shanucf87603cf4b6
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index bab8615f45..079e58eecf 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -75,6 +75,7 @@ import {
useEffect,
useLayoutEffect,
useRef,
+ useState,
} from 'react';
export type ThreadGroupPart = MessagePrimitive.GroupedParts.GroupPart;
@@ -142,6 +143,23 @@ export type ThreadComponents = {
ComposerIdleAction?: ComponentType | undefined;
/** Switches the host chat surface into its microphone-first composer. */
onSwitchToMicCloud?: (() => void) | undefined;
+ /**
+ * Host sink for files dropped on the composer or pasted into it.
+ *
+ * Supplying it also replaces `ComposerPrimitive.AttachmentDropzone` with the
+ * equivalent host-driven handlers, because that primitive routes files to the
+ * runtime's attachment adapter and refuses the drag outright
+ * (`dataTransfer.dropEffect = 'none'`) when the runtime declares no
+ * attachment capability — which is every runtime that keeps attachments on
+ * the host side, as this app does.
+ */
+ onComposerFiles?: ((files: FileList | File[] | null) => void) | undefined;
+ /**
+ * Whether the host can take files right now (feature enabled, composer
+ * unlocked, budget left). Drives the drag affordance only; the host still
+ * validates whatever arrives.
+ */
+ canAcceptComposerFiles?: boolean | undefined;
};
export type ThreadProps = {
@@ -438,8 +456,13 @@ const Composer: FC<{
const commands = useContext(SlashCommandsContext);
const slash = unstable_useSlashCommandAdapter({ commands, fallbackIcon: SlashIcon });
const inputWrapperRef = useRef<HTMLDivElement>(null);
- const { ComposerHeader, ComposerAttachments: HostComposerAttachments } =
- useContext(ThreadComponentsContext);
+ const {
+ ComposerHeader,
+ ComposerAttachments: HostComposerAttachments,
+ onComposerFiles,
+ canAcceptComposerFiles,
+ } = useContext(ThreadComponentsContext);
+ const [isDraggingFiles, setIsDraggingFiles] = useState(false);
useEffect(() => {
const textbox = inputWrapperRef.current?.querySelector<HTMLElement>('[contenteditable="true"]');
textbox?.setAttribute('aria-label', 'Message input');
@@ -472,6 +495,52 @@ const Composer: FC<{
// composition that started in between makes this write stale, and dropping it
// loses nothing, because the DOM is the source of truth and that
// composition's own commit reads the whole of it.
+ // Host-driven file ingest. Mirrors the legacy composer's handlers
+ // (`ChatComposer.tsx`) so both surfaces accept a drop and a pasted
+ // screenshot through the same host path.
+ //
+ // `preventDefault` on a *file* drag happens whether or not ingest is allowed:
+ // without it the webview navigates away to the dropped file and the whole
+ // chat is gone.
+ const isFileDrag = (event: React.DragEvent) =>
+ Array.from(event.dataTransfer?.types ?? []).includes('Files');
+ const handleDragOver = (event: React.DragEvent) => {
+ if (!onComposerFiles || !isFileDrag(event)) return;
+ event.preventDefault();
+ if (!canAcceptComposerFiles) {
+ event.dataTransfer.dropEffect = 'none';
+ return;
+ }
+ event.dataTransfer.dropEffect = 'copy';
+ setIsDraggingFiles(true);
+ };
+ const handleDragLeave = (event: React.DragEvent) => {
+ // Ignore leave events that bubble while the cursor is still over a child.
+ if (event.currentTarget.contains(event.relatedTarget as Node | null)) return;
+ setIsDraggingFiles(false);
+ };
+ const handleDrop = (event: React.DragEvent) => {
+ if (!onComposerFiles || !isFileDrag(event)) return;
+ event.preventDefault();
+ setIsDraggingFiles(false);
+ if (!canAcceptComposerFiles) return;
+ const files = event.dataTransfer?.files;
+ if (!files || files.length === 0) return;
+ onComposerFiles(files);
+ };
+ // Capture phase, so the media is pulled out and the default cancelled before
+ // Lexical's own paste handling turns it into editor content.
+ const handlePasteCapture = (event: React.ClipboardEvent) => {
+ if (!onComposerFiles || !canAcceptComposerFiles) return;
+ const files = Array.from(event.clipboardData?.items ?? [])
+ .filter(item => item.kind === 'file' && /^(image|video)\//.test(item.type))
+ .map(item => item.getAsFile())
+ .filter((file): file is File => file !== null);
+ if (files.length === 0) return;
+ event.preventDefault();
+ onComposerFiles(files);
+ };
+
const syncComposerFromDom = (target: EventTarget | null) => {
if (!(target instanceof HTMLElement)) return;
const text = target.textContent ?? '';
@@ -487,9 +556,20 @@ const Composer: FC<{
className="aui-composer-root relative flex w-full flex-col"
data-walkthrough="chat-agent-panel">
{ComposerHeader ? <ComposerHeader /> : null}
- <ComposerPrimitive.AttachmentDropzone asChild>
+ {/*
+ * Neutered whenever the host owns file ingest: every handler in the
+ * primitive short-circuits on `disabled`, so the drag handlers below
+ * are the only ones left and the `data-dragging` styling runs off this
+ * component's own state. Left enabled otherwise, so a host that does
+ * use a runtime attachment adapter keeps the primitive's behaviour.
+ */}
+ <ComposerPrimitive.AttachmentDropzone asChild disabled={!!onComposerFiles}>
<div
data-slot="aui_composer-shell"
+ data-dragging={onComposerFiles && isDraggingFiles ? 'true' : undefined}
+ onDragOver={handleDragOver}
+ onDragLeave={handleDragLeave}
+ onDrop={handleDrop}
// Keyed to `content-faint` rather than `line`/`line-strong`, which
// sat too close to the composer's own surface to read as an edge at
// all; `content-faint` is a real step along the grey ramp in both
@@ -560,6 +640,7 @@ const Composer: FC<{
<LexicalComposerInput
ref={inputWrapperRef}
placeholder="Send a message..."
+ onPasteCapture={handlePasteCapture}
onCompositionStartCapture={() => {
isComposingTextRef.current = true;
}}
diff --git a/app/src/features/conversations/components/AssistantUiChat.tsx b/app/src/features/conversations/components/AssistantUiChat.tsx
index 53d6a5d488..f10e4ee6dc 100644
--- a/app/src/features/conversations/components/AssistantUiChat.tsx
+++ b/app/src/features/conversations/components/AssistantUiChat.tsx
@@ -291,6 +291,13 @@ export function AssistantUiChat({
) : null;
}, [t]);
+ // Files arriving from a drop or a paste, routed to the same host validator
+ // the picker uses. Stable like the slots above, and for the same reason: it
+ // is handed to `thread.tsx` through the components object.
+ const handleComposerFiles = useCallback((files: FileList | File[] | null) => {
+ void slotPropsRef.current.onAttachFiles(files);
+ }, []);
+
const components: ThreadComponents = useMemo(
() => ({
ToolFallback: ChatToolFallback,
@@ -311,6 +318,13 @@ export function AssistantUiChat({
ComposerAddAttachment,
hasComposerAttachments: attachments.length > 0,
onComposerAttachmentSend: onAttachmentOnlySend,
+ // Drop and paste reach the same validator as the picker. Without
+ // this the assistant surface had no host file path at all: its only
+ // dropzone was the assistant-ui primitive, which hands files to a
+ // runtime attachment adapter this app does not use.
+ onComposerFiles: handleComposerFiles,
+ canAcceptComposerFiles:
+ !attachmentInteractionBlocked && attachments.length < maxAttachments,
}
: {}),
}),
@@ -320,6 +334,9 @@ export function AssistantUiChat({
ComposerExtras,
ComposerHeader,
ComposerIdleAction,
+ attachmentInteractionBlocked,
+ handleComposerFiles,
+ maxAttachments,
// The array itself, not just its length: the slot components above hold a
// constant identity now, so this memo is what makes `thread.tsx`'s
// context change and re-render them against the latest attachments.