Skip to content

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

feat(assistant-ui): move file drop handling from composer to thread root

Steven Enamakel1a64a38dc4c1
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index 5ef59ee281..db8f1204b6 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -253,6 +253,58 @@ function filesFromDrop(dataTransfer: DataTransfer | null): File[] {
.filter((file): file is File => file !== null);
}
+/**
+ * Host-driven file drop for the whole open thread, not just the composer box:
+ * a file dropped anywhere over the transcript lands as a composer attachment.
+ * Mirrors the legacy composer's handlers (`ChatComposer.tsx`) and feeds the
+ * same host path as the picker and paste, whose validator decides what the
+ * active model can take (images only with vision, documents text-extracted).
+ *
+ * `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. Outside a thread, `installFileDropGuard` refuses the drop instead.
+ */
+function useThreadFileDrop() {
+ const { onComposerFiles, canAcceptComposerFiles } = useContext(ThreadComponentsContext);
+ const [isDraggingFiles, setIsDraggingFiles] = useState(false);
+
+ const isFileDrag = (event: React.DragEvent) =>
+ Array.from(event.dataTransfer?.types ?? []).includes('Files');
+ const onDragOver = (event: React.DragEvent) => {
+ if (!onComposerFiles || !isFileDrag(event)) return;
+ event.preventDefault();
+ if (!canAcceptComposerFiles) {
+ event.dataTransfer.dropEffect = 'none';
+ return;
+ }
+ event.dataTransfer.dropEffect = 'copy';
+ setIsDraggingFiles(true);
+ };
+ const onDragLeave = (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 onDrop = (event: React.DragEvent) => {
+ if (!onComposerFiles || !isFileDrag(event)) return;
+ event.preventDefault();
+ setIsDraggingFiles(false);
+ if (!canAcceptComposerFiles) {
+ debug('[assistant-composer] drop: refused, ingest not accepting');
+ return;
+ }
+ const files = filesFromDrop(event.dataTransfer);
+ if (files.length === 0) {
+ debug('[assistant-composer] drop: file drag carried no readable files');
+ return;
+ }
+ debug('[assistant-composer] drop: ingesting %d file(s)', files.length);
+ onComposerFiles(files);
+ };
+
+ return { isDraggingFiles, dropHandlers: { onDragOver, onDragLeave, onDrop } };
+}
+
const EMPTY_COMPONENTS: ThreadComponents = {};
const ThreadComponentsContext = createContext<ThreadComponents>(EMPTY_COMPONENTS);
@@ -335,10 +387,12 @@ const ThreadRoot: FC<{
const { claimScroll } = useFollowBottom(viewportRef, scrollContentRef);
useOpenThreadAtBottom(viewportRef, claimScroll);
+ const { isDraggingFiles, dropHandlers } = useThreadFileDrop();
return (
<ThreadPrimitive.Root
className="aui-root aui-thread-root bg-background @container flex h-full flex-col"
+ {...dropHandlers}
style={{
['--thread-max-width' as string]: '44rem',
['--composer-bg' as string]: 'var(--color-card)',
@@ -396,7 +450,12 @@ const ThreadRoot: FC<{
)}>
<ThreadScrollToBottom />
<ThreadFollowupSuggestions />
- <Composer model={model} onModelChange={onModelChange} onEscape={onEscape} />
+ <Composer
+ model={model}
+ onModelChange={onModelChange}
+ onEscape={onEscape}
+ isDraggingFiles={isDraggingFiles}
+ />
<AuiIf condition={s => isNewChatView(s) && s.composer.isEmpty}>
<ThreadSuggestions />
</AuiIf>
@@ -774,7 +833,9 @@ const Composer: FC<{
model: string | null;
onModelChange?: (value: string | null, contextWindow?: number | null) => void;
onEscape?: () => void;
-}> = ({ model, onModelChange, onEscape }) => {
+ /** A file drag is over the thread and will land here; see `useThreadFileDrop`. */
+ isDraggingFiles: boolean;
+}> = ({ model, onModelChange, onEscape, isDraggingFiles }) => {
const aui = useAui();
const commands = useContext(SlashCommandsContext);
const slash = unstable_useSlashCommandAdapter({ commands, fallbackIcon: SlashIcon });
@@ -785,7 +846,6 @@ const Composer: FC<{
onComposerFiles,
canAcceptComposerFiles,
} = useContext(ThreadComponentsContext);
- const [isDraggingFiles, setIsDraggingFiles] = useState(false);
useEffect(() => {
const textbox = inputWrapperRef.current?.querySelector<HTMLElement>('[contenteditable="true"]');
textbox?.setAttribute('aria-label', 'Message input');
@@ -818,46 +878,6 @@ 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) {
- debug('[assistant-composer] drop: refused, ingest not accepting');
- return;
- }
- const files = filesFromDrop(event.dataTransfer);
- if (files.length === 0) {
- debug('[assistant-composer] drop: file drag carried no readable files');
- return;
- }
- debug('[assistant-composer] drop: ingesting %d file(s)', files.length);
- 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) => {
@@ -896,18 +916,15 @@ const Composer: FC<{
{ComposerHeader ? <ComposerHeader /> : null}
{/*
* 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
+ * primitive short-circuits on `disabled`, so the thread-wide handlers in
+ * `useThreadFileDrop` are the only ones left and the `data-dragging`
+ * styling runs off their 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