import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { AnimatePresence, motion } from "framer-motion" import { ArrowBendUpLeft, PaperPlaneTilt, X, ChatCircle, UserCircle, Paperclip, FileText } from "@phosphor-icons/react" import type { AgentMessageRecord, AgentRecord, DocumentRecord, MessageChannel } from "@shared/types" import { unwrap } from "../../lib/desktop" import { useAppStore } from "../../state/store" import { InlineNotice } from "../ui" import { useT } from "../../i18n" const HUMAN_SENDER_ID = "__board__"; function SendButton({ sending, disabled, onClick, label }: { sending: boolean; disabled: boolean; onClick: () => void; label: string }) { return ( ) } // ─── Attachment Picker ────────────────────────────────────────────────────── interface AttachmentItem { type: string; id: string; title: string } function AttachmentPicker({ documents, attachments, onAdd, onRemove }: { documents: DocumentRecord[]; attachments: AttachmentItem[]; onAdd: (doc: DocumentRecord) => void; onRemove: (id: string) => void; }) { const [open, setOpen] = useState(false) const [filter, setFilter] = useState("") const containerRef = useRef(null) const attachedIds = useMemo(() => new Set(attachments.map((a) => a.id)), [attachments]) const filtered = useMemo(() => { const q = filter.toLowerCase() return documents.filter((d) => !attachedIds.has(d.id) && (d.title.toLowerCase().includes(q) || d.type.toLowerCase().includes(q))) }, [documents, attachedIds, filter]) useEffect(() => { if (!open) return const handler = (e: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(e.target as Node)) setOpen(false) } document.addEventListener("mousedown", handler) return () => document.removeEventListener("mousedown", handler) }, [open]) return (
{open && (
setFilter(e.target.value)} placeholder="Search documents..." className="w-full rounded-lg border border-border bg-muted px-2.5 py-1.5 text-[12px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-1" autoFocus />
{filtered.length === 0 ? (
{documents.length === 0 ? "No documents in this company" : "No matching documents"}
) : ( filtered.slice(0, 20).map((doc) => ( )) )}
)} {attachments.length > 0 && (
{attachments.map((att) => ( {att.title} ))}
)}
) } // ─── @Mention Autocomplete ────────────────────────────────────────────────── function MentionDropdown({ agents, query, onSelect, position }: { agents: AgentRecord[]; query: string; onSelect: (agent: AgentRecord | null) => void; position: { top: number; left: number }; }) { const [selectedIndex, setSelectedIndex] = useState(0) const filtered = useMemo(() => { const q = query.toLowerCase() return agents.filter((a) => a.status !== "terminated" && ( a.name.toLowerCase().includes(q) || (a.title?.toLowerCase().includes(q)) || (a.department?.toLowerCase().includes(q)) )).slice(0, 8) }, [agents, query]) useEffect(() => { setSelectedIndex(0) }, [query]) useEffect(() => { const handler = (e: KeyboardEvent) => { if (e.key === "ArrowDown") { e.preventDefault(); setSelectedIndex((i) => Math.min(i + 1, filtered.length - 1)) } else if (e.key === "ArrowUp") { e.preventDefault(); setSelectedIndex((i) => Math.max(i - 1, 0)) } else if (e.key === "Enter" || e.key === "Tab") { if (filtered[selectedIndex]) { e.preventDefault(); onSelect(filtered[selectedIndex]) } } else if (e.key === "Escape") { onSelect(null) } } document.addEventListener("keydown", handler, true) return () => document.removeEventListener("keydown", handler, true) }, [filtered, selectedIndex, onSelect]) if (filtered.length === 0) return null return (
{filtered.map((agent, i) => ( ))}
) } // ─── Main Composer ────────────────────────────────────────────────────────── interface MessageComposerProps { agents: AgentRecord[]; companyId: string; channel: MessageChannel; channelTargetId: string | null directParticipantIds?: string[]; replyTo: AgentMessageRecord | null; onSent: () => void; onCancelReply: () => void documents?: DocumentRecord[] /** Thread ID for precise checkpoint resolution. */ threadId?: string | null /** Wait state ID for precise checkpoint resolution. */ waitStateId?: string | null } export function MessageComposer({ agents, companyId, channel, channelTargetId, directParticipantIds = [], replyTo, onSent, onCancelReply, documents = [], threadId = null, waitStateId = null }: MessageComposerProps) { const [body, setBody] = useState(""); const [subject, setSubject] = useState("") const [sending, setSending] = useState(false); const [sendError, setSendError] = useState(null) const [attachments, setAttachments] = useState([]) const [mentionState, setMentionState] = useState<{ active: boolean; query: string; startIndex: number }>({ active: false, query: "", startIndex: -1 }) const textareaRef = useRef(null) const addToast = useAppStore((s) => s.addToast) useEffect(() => { if (replyTo) textareaRef.current?.focus() }, [replyTo]) const directRecipientId = useMemo(() => { if (channel !== "direct") return null if (replyTo?.fromAgentId) return replyTo.fromAgentId if (directParticipantIds.length === 0) return null const nonBoardParticipant = directParticipantIds.find((id) => id !== HUMAN_SENDER_ID) return nonBoardParticipant ?? directParticipantIds[0] ?? null }, [channel, replyTo, directParticipantIds]) const handleAddAttachment = useCallback((doc: DocumentRecord) => { setAttachments((prev) => [...prev, { type: "document", id: doc.id, title: doc.title }]) }, []) const handleRemoveAttachment = useCallback((id: string) => { setAttachments((prev) => prev.filter((a) => a.id !== id)) }, []) const handleMentionSelect = useCallback((agent: AgentRecord | null) => { if (!agent) { setMentionState({ active: false, query: "", startIndex: -1 }); return } setBody((prev) => { const before = prev.slice(0, mentionState.startIndex) const after = prev.slice(mentionState.startIndex + mentionState.query.length + 1) return `${before}@${agent.name} ${after}` }) setMentionState({ active: false, query: "", startIndex: -1 }) textareaRef.current?.focus() }, [mentionState]) const handleBodyChange = useCallback((e: React.ChangeEvent) => { const value = e.target.value setBody(value) const cursorPos = e.target.selectionStart const textBeforeCursor = value.slice(0, cursorPos) const atIndex = textBeforeCursor.lastIndexOf("@") if (atIndex >= 0) { const charBefore = atIndex > 0 ? textBeforeCursor[atIndex - 1] : " " if (charBefore === " " || charBefore === "\n" || atIndex === 0) { const query = textBeforeCursor.slice(atIndex + 1) if (!query.includes(" ") && !query.includes("\n") && query.length <= 30) { setMentionState({ active: true, query, startIndex: atIndex }) return } } } setMentionState({ active: false, query: "", startIndex: -1 }) }, []) const handleSend = useCallback(async () => { if (!body.trim()) return; if (channel === "direct" && !directRecipientId) return setSending(true); setSendError(null) const attachmentsJson = attachments.length > 0 ? JSON.stringify(attachments) : "[]" try { const result = unwrap(await window.agentCompany.sendAgentMessage({ companyId, fromAgentId: HUMAN_SENDER_ID, toAgentId: channel === "direct" ? directRecipientId : null, channel, channelTargetId, subject: subject.trim() || (replyTo ? `Re: ${replyTo.subject}` : ""), body: body.trim(), priority: "normal", parentMessageId: replyTo?.id ?? null, attachmentsJson, threadId: threadId ?? undefined, waitStateId: waitStateId ?? undefined, })) if (result.mentionResults && result.mentionResults.length > 0) { const woken = result.mentionResults.filter((m) => m.outcome === "woken") const queued = result.mentionResults.filter((m) => m.outcome === "queued") const busy = result.mentionResults.filter((m) => m.outcome === "busy") if (woken.length > 0) addToast(`Notified: ${woken.map((m) => m.agentName).join(", ")}`, "success") if (queued.length > 0) addToast(`Queued: ${queued.map((m) => m.agentName).join(", ")}`, "warn") if (busy.length > 0) addToast(`Busy: ${busy.map((m) => m.agentName).join(", ")}`, "warn") } setBody(""); setSubject(""); setAttachments([]); onSent() } catch (e) { setSendError(e instanceof Error ? e.message : "Failed to send message.") } finally { setSending(false) } }, [body, channel, channelTargetId, companyId, directRecipientId, onSent, replyTo, subject, attachments, threadId, waitStateId, addToast]) const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (mentionState.active) return // let MentionDropdown handle keys if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); void handleSend() } }, [handleSend, mentionState.active]) return (
{sendError ?
: null} {channel === "direct" && !directRecipientId ?
: null} {replyTo ? ( Replying to {agents.find((a) => a.id === replyTo.fromAgentId)?.name ?? "agent"}{replyTo.subject ? `: ${replyTo.subject}` : ""} ) : null}
You (Board) {!replyTo ? setSubject(e.target.value)} placeholder="Subject (optional)" aria-label="Message subject" className="min-w-0 flex-1 rounded-lg border border-border bg-card px-2.5 py-1 text-[11px] text-foreground placeholder:text-muted-foreground" /> : null}
{attachments.length > 0 && (
{attachments.map((att) => ( {att.title} ))}
)}