import { createPortal } from "react-dom"; import { useEffect, useRef, useState } from "react"; import type { PendingAssignment } from "../types"; import { usePanelDrag } from "../usePanelDrag"; import { usePanelResize } from "../usePanelResize"; import { PanelResizeHandle } from "./PanelResizeHandle"; import { centeredAnchorToDeskOffset, deskOffsetFromViewport, rowToViewport, useDeskAnchoredRowPosition, viewportToRow, type DeskOffset, } from "../deskPanelAnchor"; import { useTeamRowPanel } from "../TeamRowPanelContext"; export type PanelAnchor = { top: number; left: number }; const TEXT_EXTS = new Set([ "txt","md","py","js","ts","jsx","tsx","json","csv","yaml","yml", "html","css","xml","sh","bash","sql","r","toml","ini","cfg","log", "rst","java","c","cpp","h","hpp","go","rs","rb","php","swift","kt", ]); const IMAGE_EXTS = new Set(["jpg","jpeg","png","gif","webp","svg"]); type AttachedImage = { name: string; url: string }; async function processFiles(files: File[]): Promise<{ textAppend: string; images: AttachedImage[] }> { const parts: string[] = []; const images: AttachedImage[] = []; for (const file of files) { const ext = (file.name.split(".").pop() ?? "").toLowerCase(); if (IMAGE_EXTS.has(ext)) { const url = await new Promise((res) => { const fr = new FileReader(); fr.onload = (e) => res(e.target!.result as string); fr.readAsDataURL(file); }); images.push({ name: file.name, url }); // No text marker: the image is shown as a thumbnail and sent as an // attachment (the server tells the agent its saved path), so a // "[Attached image: …]" line would just be redundant UI noise. } else if (TEXT_EXTS.has(ext) || file.type.startsWith("text/")) { const content = await new Promise((res) => { const fr = new FileReader(); fr.onload = (e) => res(e.target!.result as string); fr.readAsText(file); }); parts.push(`\`\`\`${ext}\n# ${file.name}\n${content.slice(0, 12000)}\n\`\`\``); } else { parts.push(`[Attached file: ${file.name}]`); } } return { textAppend: parts.join("\n"), images }; } interface Props { deskIndex: number; scene?: string; isActive: boolean; dropHighlight?: boolean; initialMsg?: string; assignment?: PendingAssignment | null; onStart: (msg: string, agentId: string, images?: AttachedImage[], anchor?: PanelAnchor) => Promise; onSelect: () => void; onClose: () => void; onMsgChange?: (msg: string) => void; } const DESK_COLORS = ["#6b4c2a", "#5a3e22", "#7a5530", "#4e3018", "#635028", "#724830"]; function Dot({ delay, size = 5 }: { delay: number; size?: number }) { return (
); } const THINKING_PANEL_W = 380; const THINKING_PANEL_H = 420; const THINKING_PANEL_MIN = { width: 280, height: 240 }; export function PendingTaskDesk({ deskIndex, scene, isActive, dropHighlight, initialMsg, assignment, onStart, onSelect, onClose, onMsgChange }: Props) { const [msg, setMsg] = useState(initialMsg ?? ""); const [sending, setSending] = useState(false); const [error, setError] = useState(null); const agentId = assignment?.agentId ?? ""; const [panelDeskOffset, setPanelDeskOffset] = useState(null); const containerRef = useRef(null); const onDragCommitRef = useRef<(vp: { top: number; left: number }) => void>(() => {}); const { pos: panelDragPos, resetPos: resetPanelUserPos, dragging: panelDragging, bindHandle: bindPanelDrag } = usePanelDrag(12, (vp) => onDragCommitRef.current(vp)); const { size: panelUserSize, resetSize: resetPanelUserSize, resizing: panelResizing, bindResize: bindPanelResize } = usePanelResize(THINKING_PANEL_MIN); const [dragOver, setDragOver] = useState(false); const [attachedImages, setAttachedImages] = useState([]); const promptBoxRef = useRef(null); const textRef = useRef(null); const deskColor = DESK_COLORS[deskIndex % DESK_COLORS.length]; const { root: panelRoot } = useTeamRowPanel(); const rowRef = useRef(null); rowRef.current = panelRoot; onDragCommitRef.current = (vp) => { if (containerRef.current) setPanelDeskOffset(deskOffsetFromViewport(containerRef.current, vp)); resetPanelUserPos(); }; const panelRowPos = useDeskAnchoredRowPosition(containerRef, rowRef, panelDeskOffset, sending && !panelDragging && !!panelRoot); const panelDisplayPos = (() => { if (panelDragging && panelDragPos && panelRoot) return viewportToRow(panelRoot, panelDragPos); return panelRowPos; })(); useEffect(() => { if (isActive && !sending) textRef.current?.focus(); }, [isActive, sending]); async function handleDrop(e: React.DragEvent) { e.preventDefault(); setDragOver(false); const files = Array.from(e.dataTransfer.files); if (!files.length) return; const { textAppend, images } = await processFiles(files); if (textAppend) setMsg((prev) => prev ? `${prev}\n\n${textAppend}` : textAppend); if (images.length) setAttachedImages((prev) => [...prev, ...images]); } async function handleSend() { const text = msg.trim(); if (!text || sending) return; let anchor: PanelAnchor | undefined; if (promptBoxRef.current && containerRef.current) { const r = promptBoxRef.current.getBoundingClientRect(); anchor = { top: r.bottom + 10, left: r.left + r.width / 2 }; setPanelDeskOffset(centeredAnchorToDeskOffset(containerRef.current, anchor.left, anchor.top, THINKING_PANEL_W)); } resetPanelUserPos(); resetPanelUserSize(); setSending(true); setError(null); try { await onStart(text, agentId, attachedImages.length > 0 ? attachedImages : undefined, anchor); } catch (e) { // Surface the real server reason (e.g. 409 "agent already in use on another // desk") instead of always blaming Hermes. const detail = e instanceof Error ? e.message : ""; setError(detail || "Failed to start — is Hermes running?"); setSending(false); } } const panelW = panelUserSize?.width ?? THINKING_PANEL_W; const panelH = panelUserSize?.height ?? THINKING_PANEL_H; function getPanelTopLeft(): { top: number; left: number } { if (panelDragging && panelDragPos) return panelDragPos; if (panelDisplayPos && panelRoot) return rowToViewport(panelRoot, panelDisplayPos); return { top: 0, left: 0 }; } const panelDragHandle = bindPanelDrag(getPanelTopLeft); const panelResizeHandle = bindPanelResize(() => ({ width: panelW, height: panelH })); // Panel that appears immediately when sending, before session_id is known const thinkingPanel = sending && panelDeskOffset && panelDisplayPos && panelRoot ? createPortal(
{/* Tab bar — drag to move */}
⚡ Activity
{/* Thinking body */}
Starting agent…
First activity events will appear here shortly
, panelRoot, ) : null; return (
{sending ? ( /* Animated mock-desk while launching */
{[0.7, 0.85, 0.55, 0.85, 0.4].map((w, i) => (
))}
Launching agent…
) : ( /* Input form */
{ e.preventDefault(); setDragOver(true); }} onDragLeave={() => setDragOver(false)} onDrop={handleDrop} style={{ position: "relative", display: "flex", flexDirection: "column", gap: 8 }} > {/* Attached image thumbnails */} {attachedImages.length > 0 && (
{attachedImages.map((img) => (
{img.name}
))}
)} {/* Bench drag target only — profile/model/tools live in header */} {dropHighlight && (
e.stopPropagation()} style={{ padding: "8px 10px", borderRadius: 8, fontSize: 11, background: "#0f3048", border: "2px dashed var(--accent2)", color: "var(--accent2)", textAlign: "center", lineHeight: 1.45, fontWeight: 600, }} > Drop agent here to assign
)}