The VS Code of video editing, built for agents.
TypeScript
Implement file attachment handling in agent chat
konstantin-paulus371841b2536a
diff --git a/apps/web/src/agent-chat/attachments.tsx b/apps/web/src/agent-chat/attachments.tsxindex d54f1d29..e0832379 100644--- a/apps/web/src/agent-chat/attachments.tsx+++ b/apps/web/src/agent-chat/attachments.tsx@@ -2,13 +2,11 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-// Dropping files and folders onto a composer. Nothing is read, copied or-// uploaded: an attachment is a path the agent reads where it is, so drops-// only work where the shell can say where a file lives (the desktop app).-// Shared by the home view and the chat composer.import { Show, createSignal } from "solid-js";+import { pickFiles } from "@diffusionstudio/assets";+import { Icon } from "@/components/ui/icon";import { RemoveButton } from "@/components/ui/remove-button";import { cx } from "@/lib/cva";@@ -52,19 +50,24 @@ export function droppedAttachments(event: DragEvent): Attachment[] {const file = item.getAsFile();if (!file) continue;- const path = window.desktop?.getPathForFile(file) || null;- const name = entry?.name || file.name;- result.push({- key: path ?? `${name}:${file.size}:${file.lastModified}`,- name,- kind: entry?.isDirectory ? "folder" : "file",- path,- });+ result.push(fileAttachment(file, entry?.isDirectory ? "folder" : "file", entry?.name || file.name));}return result;}+/** A file picked or dropped without the entry API, which only a drop has. */+function fileAttachment(file: File, kind: Attachment["kind"] = "file", name = file.name): Attachment {+ const path = window.desktop?.getPathForFile(file) || null;+ return { key: path ?? `${name}:${file.size}:${file.lastModified}`, name, kind, path };+}++/** Files chosen from the system picker, as attachments; folders can only be dropped. */+export async function pickAttachments(): Promise<Attachment[]> {+ const files = await pickFiles({ accept: "" });+ return files.map((file) => fileAttachment(file));+}+/** The same file dropped twice is one attachment, not two tiles. */export function mergeAttachments(current: Attachment[], dropped: Attachment[]): Attachment[] {const known = new Set(current.map((entry) => entry.key));diff --git a/apps/web/src/agent-chat/composer.tsx b/apps/web/src/agent-chat/composer.tsxindex 8058e54d..322cfda3 100644--- a/apps/web/src/agent-chat/composer.tsx+++ b/apps/web/src/agent-chat/composer.tsx@@ -8,11 +8,12 @@import { For, Show, createEffect } from "solid-js";+import { Button } from "@/components/ui/button";import { Icon } from "@/components/ui/icon";import type { ModelRef } from "@diffusionstudio/agent-chat";-import { AttachmentTile, DropOverlay, createDropZone, mergeAttachments, type Attachment } from "./attachments";+import { AttachmentTile, DropOverlay, createDropZone, mergeAttachments, pickAttachments, type Attachment } from "./attachments";import { ModelPicker } from "./model-picker";const MAX_HEIGHT_PX = 160;@@ -37,7 +38,8 @@ type ComposerProps = {export function Composer(props: ComposerProps) {let textarea: HTMLTextAreaElement | undefined;- const drop = createDropZone((dropped) => props.onAttachments(mergeAttachments(props.attachments, dropped)));+ const attach = (added: Attachment[]) => props.onAttachments(mergeAttachments(props.attachments, added));+ const drop = createDropZone(attach);const canSend = () => !props.running && !props.blocked && props.model !== null && (props.text.trim().length > 0 || props.attachments.length > 0);@@ -103,32 +105,44 @@ export function Composer(props: ComposerProps) {/><div class="flex min-h-4 items-center justify-between gap-1">- <ModelPicker value={props.model} onSelect={props.onModel} class="min-w-0" />- <Show- when={props.running}- fallback={+ <Button+ variant="ghost"+ size="icon-square"+ onClick={async () => attach(await pickAttachments())}+ aria-label="Add files"+ title="Add files"+ class="text-muted-foreground hover:bg-muted"+ >+ <Icon name="plus-add" />+ </Button>+ <div class="flex min-w-0 items-center gap-1.5">+ <ModelPicker value={props.model} onSelect={props.onModel} class="min-w-0" />+ <Show+ when={props.running}+ fallback={+ <button+ type="button"+ onClick={() => canSend() && props.onSend()}+ disabled={!canSend()}+ aria-label="Send"+ title={sendTitle()}+ class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground transition-opacity hover:bg-primary-hover disabled:pointer-events-none disabled:opacity-40 focus-ring"+ >+ <Icon name="arrow-top" />+ </button>+ }+ ><buttontype="button"- onClick={() => canSend() && props.onSend()}- disabled={!canSend()}- aria-label="Send"- title={sendTitle()}- class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground transition-opacity hover:bg-primary-hover disabled:pointer-events-none disabled:opacity-40 focus-ring"+ onClick={props.onStop}+ aria-label="Stop"+ title="Stop"+ class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground hover:bg-primary-hover focus-ring">- <Icon name="arrow-top" />+ <Icon name="stop" /></button>- }- >- <button- type="button"- onClick={props.onStop}- aria-label="Stop"- title="Stop"- class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground hover:bg-primary-hover focus-ring"- >- <Icon name="stop" />- </button>- </Show>+ </Show>+ </div></div><Show when={drop.dragging()}>diff --git a/apps/web/src/agent-chat/index.ts b/apps/web/src/agent-chat/index.tsindex fa0573ae..c2126d3a 100644--- a/apps/web/src/agent-chat/index.ts+++ b/apps/web/src/agent-chat/index.ts@@ -28,5 +28,6 @@ export {createDropZone,droppedAttachments,mergeAttachments,+ pickAttachments,type Attachment,} from "./attachments";diff --git a/apps/web/src/agent-chat/model-picker.tsx b/apps/web/src/agent-chat/model-picker.tsxindex 3ca4eb18..457db717 100644--- a/apps/web/src/agent-chat/model-picker.tsx+++ b/apps/web/src/agent-chat/model-picker.tsx@@ -2,9 +2,10 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-// One dropdown for harness and model: Claude Code's models, then Codex's,-// fed by the host's probes. A harness that is not ready is one disabled row-// that says why, never hidden, so the picker also says what this works with.+// One dropdown for harness and model, two levels deep: Claude Code and+// Codex first, each opening onto its models, fed by the host's probes. A+// harness that is not ready is one disabled row that says why, never hidden,+// so the picker also says what this works with.import { For, Show } from "solid-js";@@ -12,13 +13,17 @@ import {DropdownMenu,DropdownMenuContent,DropdownMenuGroup,- DropdownMenuGroupLabel,DropdownMenuItem,+ DropdownMenuItemDetail,DropdownMenuPortal,- DropdownMenuSeparator,+ DropdownMenuSub,+ DropdownMenuSubContent,+ DropdownMenuSubTrigger,DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";+import { Button } from "@/components/ui/button";import { Icon } from "@/components/ui/icon";+import { cx } from "@/lib/cva";import type { HarnessId, HarnessInfo, ModelRef } from "@diffusionstudio/agent-chat";@@ -52,66 +57,69 @@ export function ModelPicker(props: ModelPickerProps) {const harnesses = () => chatState.harnesses;return (- <DropdownMenu placement="bottom-start" onOpenChange={(open) => open && refreshHarnesses()}>+ <DropdownMenu placement="bottom-end" onOpenChange={(open) => open && refreshHarnesses()}><DropdownMenuTrigger- as="button"- type="button"+ as={Button}+ variant="ghost"aria-label="Choose the agent and model"- class={`flex h-7 shrink-0 items-center rounded-md pl-0.5 pr-2 text-xs font-450 text-muted-foreground hover:bg-muted focus-ring ${props.class ?? ""}`}+ class={cx("text-muted-foreground hover:bg-muted data-[expanded]:bg-muted", props.class)}>- <span class="grid size-6 shrink-0 place-items-center overflow-clip">- <Icon name={harnessIcon(props.value?.harness)} />- </span><span class="max-w-40 truncate">{modelLabel(props.value)}</span></DropdownMenuTrigger><DropdownMenuPortal>- <DropdownMenuContent class="w-56">- <Show when={harnesses().length === 0}>- <DropdownMenuGroup>+ <DropdownMenuContent class="w-48">+ <DropdownMenuGroup>+ <Show when={harnesses().length === 0}><DropdownMenuItem disabled><span class="min-w-0 flex-1 truncate text-muted-foreground">{chatState.connection === "unavailable" ? "Chat runs in the desktop app" : "Connecting…"}</span></DropdownMenuItem>- </DropdownMenuGroup>- </Show>- <For each={harnesses()}>- {(harness, index) => (- <>- <Show when={index() > 0}>- <DropdownMenuSeparator />- </Show>- <DropdownMenuGroup>- <DropdownMenuGroupLabel>{harness.label}</DropdownMenuGroupLabel>- <Show when={harness.status === "ready" && harness.detail}>- {/* "Update Claude Code": the harness works, but is older than we test against. */}- <p class="px-2 pb-1 text-[10px] leading-3.5 text-muted-foreground">{harness.detail}</p>- </Show>- <Show- when={harness.status === "ready"}- fallback={- <DropdownMenuItem disabled>+ </Show>+ <For each={harnesses()}>+ {(harness) => (+ <Show+ when={harness.status === "ready"}+ fallback={+ <DropdownMenuItem disabled>+ <Icon name={harnessIcon(harness.id)} />+ <span class="min-w-0 flex-1 truncate">{harness.label}</span>+ <DropdownMenuItemDetail class="truncate">{unavailableLabel(harness)}</DropdownMenuItemDetail>+ </DropdownMenuItem>+ }+ >+ <DropdownMenuSub>+ <DropdownMenuSubTrigger>+ <span class="flex items-center gap-1"><Icon name={harnessIcon(harness.id)} />- <span class="min-w-0 flex-1 truncate text-muted-foreground">{unavailableLabel(harness)}</span>- </DropdownMenuItem>- }- >- <For each={harness.models}>- {(model) => (- <DropdownMenuItem onSelect={() => props.onSelect({ harness: harness.id, model: model.id })}>- <Icon name={harnessIcon(harness.id)} />- <span class="min-w-0 flex-1 truncate">{model.label}</span>- <Show when={props.value?.harness === harness.id && props.value?.model === model.id}>- <Icon name="confirm-check" class="size-6" />- </Show>- </DropdownMenuItem>- )}- </For>- </Show>- </DropdownMenuGroup>- </>- )}- </For>+ <span class="min-w-0 truncate">{harness.label}</span>+ </span>+ </DropdownMenuSubTrigger>+ <DropdownMenuPortal>+ <DropdownMenuSubContent class="w-48">+ <Show when={harness.detail}>+ {/* "Update Claude Code": the harness works, but is older than we test against. */}+ <p class="px-2 text-[10px] leading-3.5 text-muted-foreground">{harness.detail}</p>+ </Show>+ <DropdownMenuGroup>+ <For each={harness.models}>+ {(model) => (+ <DropdownMenuItem onSelect={() => props.onSelect({ harness: harness.id, model: model.id })}>+ <span class="min-w-0 flex-1 truncate">{model.label}</span>+ <Show when={props.value?.harness === harness.id && props.value?.model === model.id}>+ <Icon name="confirm-check" class="size-6" />+ </Show>+ </DropdownMenuItem>+ )}+ </For>+ </DropdownMenuGroup>+ </DropdownMenuSubContent>+ </DropdownMenuPortal>+ </DropdownMenuSub>+ </Show>+ )}+ </For>+ </DropdownMenuGroup></DropdownMenuContent></DropdownMenuPortal></DropdownMenu>diff --git a/apps/web/src/components/dashboard/home-view.tsx b/apps/web/src/components/dashboard/home-view.tsxindex 7cef19af..aa1a824f 100644--- a/apps/web/src/components/dashboard/home-view.tsx+++ b/apps/web/src/components/dashboard/home-view.tsx@@ -25,6 +25,7 @@ import {DropdownMenuSeparator,DropdownMenuTrigger,} from "@/components/ui/dropdown-menu";+import { Button } from "@/components/ui/button";import { Icon } from "@/components/ui/icon";import { Kbd } from "@/components/ui/kbd";import {@@ -35,6 +36,7 @@ import {createDropZone,currentModel,mergeAttachments,+ pickAttachments,setStoredModel,startChat,type Attachment,@@ -108,7 +110,8 @@ export function DashboardHomeView() {const [busy, setBusy] = createSignal(false);const [attachments, setAttachments] = createSignal<Attachment[]>([]);- const drop = createDropZone((dropped) => setAttachments((current) => mergeAttachments(current, dropped)));+ const attach = (added: Attachment[]) => setAttachments((current) => mergeAttachments(current, added));+ const drop = createDropZone(attach);let textarea: HTMLTextAreaElement | undefined;@@ -403,19 +406,32 @@ export function DashboardHomeView() {</div><div class="flex min-h-4 items-center justify-between">- <ModelPicker value={model()} onSelect={setStoredModel} />-- <button- type="button"- onClick={handleSubmit}- disabled={!canSubmit()}- aria-label="Send to the coding agent"- class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground transition-opacity hover:bg-primary-hover disabled:pointer-events-none disabled:opacity-40 focus-ring"+ <Button+ variant="ghost"+ size="icon-square"+ onClick={async () => attach(await pickAttachments())}+ aria-label="Add files"+ title="Add files"+ class="text-muted-foreground hover:bg-muted">- <Show when={busy()} fallback={<Icon name="arrow-top" />}>- <Icon name="spinner-loader" class="animate-spin" />- </Show>- </button>+ <Icon name="plus-add" />+ </Button>++ <div class="flex min-w-0 items-center gap-1.5">+ <ModelPicker value={model()} onSelect={setStoredModel} class="min-w-0" />++ <button+ type="button"+ onClick={handleSubmit}+ disabled={!canSubmit()}+ aria-label="Send to the coding agent"+ class="grid size-7 shrink-0 place-items-center rounded-full bg-primary text-primary-foreground transition-opacity hover:bg-primary-hover disabled:pointer-events-none disabled:opacity-40 focus-ring"+ >+ <Show when={busy()} fallback={<Icon name="arrow-top" />}>+ <Icon name="spinner-loader" class="animate-spin" />+ </Show>+ </button>+ </div></div><Show when={drop.dragging()}>