{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "agent-composer-attachments",
  "type": "registry:component",
  "title": "Agent Composer with Attachments",
  "description": "A message composer with animated attachment chips, a slash-command menu that opens on \"/\" and a send button with idle, ready, sending and sent states.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "agent-kit",
    "agent",
    "composer",
    "attachments",
    "slash commands",
    "chat",
    "input"
  ],
  "docs": "https://hyperblur.dev/effects/agent-composer-attachments",
  "files": [
    {
      "path": "registry/hyperblur/agent-composer-attachments.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/agent-composer-attachments.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ComposerAttachment {\n  id: string;\n  name: string;\n  size?: number;\n}\n\nexport interface ComposerCommand {\n  name: string;\n  description: string;\n}\n\nexport interface AgentComposerAttachmentsProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  attachments?: ComposerAttachment[];\n  defaultAttachments?: ComposerAttachment[];\n  onAttachmentsChange?: (attachments: ComposerAttachment[]) => void;\n  commands?: ComposerCommand[];\n  onSend?: (text: string, attachments: ComposerAttachment[]) => void | Promise<void>;\n  sending?: boolean;\n  placeholder?: string;\n  accept?: string;\n  accent?: string;\n}\n\nconst defaultCommands: ComposerCommand[] = [\n  { name: \"summarize\", description: \"Condense the thread or a file\" },\n  { name: \"search\", description: \"Look something up in the workspace\" },\n  { name: \"explain\", description: \"Walk through selected code\" },\n  { name: \"review\", description: \"Check a change before merging\" },\n];\n\nconst css = `\n.hb-yf6 {\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  font-family: inherit;\n  font-size: 12px;\n  line-height: 1.4;\n  color: #ededed;\n  text-align: left;\n}\n.hb-yf6 *, .hb-yf6 *::before, .hb-yf6 *::after { box-sizing: border-box; font-family: inherit; }\n.hb-yf6-box {\n  display: flex;\n  flex-direction: column;\n  gap: 6px;\n  padding: 8px 8px 8px 10px;\n  background: #111111;\n  border: 1px solid #2a2a2a;\n  border-radius: 14px;\n  transition: border-color 0.15s ease, box-shadow 0.15s ease;\n}\n.hb-yf6-box:focus-within { border-color: var(--hb-yf6-a, #2f7bff); box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-yf6-a, #2f7bff) 18%, transparent); }\n.hb-yf6-chips { display: flex; flex-wrap: wrap; gap: 6px; }\n.hb-yf6-chip {\n  display: inline-flex;\n  align-items: center;\n  gap: 6px;\n  max-width: 100%;\n  height: 26px;\n  padding: 0 4px 0 7px;\n  background: #1a1a1a;\n  border: 1px solid #2a2a2a;\n  border-radius: 8px;\n  animation: hb-yf6-chip-in 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;\n}\n.hb-yf6-chip[data-leaving=\"true\"] { animation: hb-yf6-chip-out 0.18s ease forwards; pointer-events: none; }\n.hb-yf6-ico { flex: none; width: 13px; height: 13px; color: var(--hb-yf6-a, #7fb3ff); }\n.hb-yf6-fname { min-width: 0; max-width: 118px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11.5px; }\n.hb-yf6-fsize { flex: none; font-size: 10.5px; color: #6b6b6b; font-variant-numeric: tabular-nums; }\n.hb-yf6-x { display: grid; place-items: center; flex: none; width: 18px; height: 18px; margin: 0; padding: 0; color: #8f8f8f; background: transparent; border: 0; border-radius: 6px; cursor: pointer; transition: background-color 0.15s ease, color 0.15s ease; }\n.hb-yf6-x:hover { color: #ededed; background: rgba(255, 255, 255, 0.1); }\n.hb-yf6-x:focus-visible, .hb-yf6-attach:focus-visible, .hb-yf6-send:focus-visible { outline: 2px solid var(--hb-yf6-a, #2f7bff); outline-offset: 1px; }\n.hb-yf6-x svg { width: 9px; height: 9px; }\n.hb-yf6-field {\n  display: block;\n  width: 100%;\n  min-height: 22px;\n  max-height: 96px;\n  margin: 0;\n  padding: 0;\n  font: inherit;\n  font-size: 13px;\n  line-height: 22px;\n  color: #ededed;\n  background: transparent;\n  border: 0;\n  outline: none;\n  resize: none;\n  field-sizing: content;\n}\n.hb-yf6-field::placeholder { color: #6b6b6b; }\n.hb-yf6-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }\n.hb-yf6-tools { display: flex; align-items: center; gap: 8px; min-width: 0; }\n.hb-yf6-attach { display: grid; place-items: center; width: 28px; height: 28px; margin: 0; padding: 0; color: #a1a1a1; background: transparent; border: 1px solid #2a2a2a; border-radius: 9px; cursor: pointer; transition: color 0.15s ease, background-color 0.15s ease, border-color 0.15s ease; }\n.hb-yf6-attach:hover { color: #ededed; background: rgba(255, 255, 255, 0.06); border-color: #3a3a3a; }\n.hb-yf6-attach svg { width: 14px; height: 14px; }\n.hb-yf6-hint { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; color: #6b6b6b; }\n.hb-yf6-hint kbd { padding: 0 5px; font: inherit; font-family: var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, monospace); font-size: 10.5px; color: #a1a1a1; background: #1a1a1a; border: 1px solid #2a2a2a; border-radius: 5px; }\n.hb-yf6-send { position: relative; display: grid; place-items: center; flex: none; width: 30px; height: 30px; margin: 0; padding: 0; color: #6b6b6b; background: #1c1c1c; border: 0; border-radius: 10px; cursor: not-allowed; transition: background-color 0.2s ease, color 0.2s ease, transform 0.15s ease; }\n.hb-yf6-send svg { width: 15px; height: 15px; }\n.hb-yf6-send[data-state=\"ready\"] { color: #fff; background: var(--hb-yf6-a, #2f7bff); cursor: pointer; }\n.hb-yf6-send[data-state=\"ready\"]:hover { transform: translateY(-1px); }\n.hb-yf6-send[data-state=\"ready\"]:active { transform: scale(0.94); }\n.hb-yf6-send[data-state=\"sending\"] { color: #fff; background: color-mix(in srgb, var(--hb-yf6-a, #2f7bff) 70%, #111111); cursor: progress; }\n.hb-yf6-send[data-state=\"sent\"] { color: #0a0a0a; background: #3ecf8e; cursor: default; }\n.hb-yf6-spin { animation: hb-yf6-spin 0.8s linear infinite; }\n.hb-yf6-menu {\n  position: absolute;\n  z-index: 20;\n  left: 0;\n  right: 0;\n  bottom: calc(100% + 6px);\n  max-height: 168px;\n  margin: 0;\n  padding: 4px;\n  overflow-y: auto;\n  list-style: none;\n  background: #111111;\n  border: 1px solid #2a2a2a;\n  border-radius: 12px;\n  box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.5);\n  transform-origin: bottom center;\n  animation: hb-yf6-menu-in 0.16s cubic-bezier(0.2, 0.8, 0.2, 1) both;\n  scrollbar-width: thin;\n  scrollbar-color: #333333 transparent;\n}\n.hb-yf6-cmd { display: flex; align-items: baseline; gap: 10px; height: 32px; padding: 0 10px; border-radius: 8px; cursor: pointer; align-items: center; }\n.hb-yf6-cmd[aria-selected=\"true\"] { background: #1c1c1c; }\n.hb-yf6-name { flex: none; font-family: var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, monospace); font-size: 12px; color: var(--hb-yf6-a, #7fb3ff); }\n.hb-yf6-desc { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11.5px; color: #8f8f8f; }\n@keyframes hb-yf6-chip-in { from { opacity: 0; transform: scale(0.7); } to { opacity: 1; transform: none; } }\n@keyframes hb-yf6-chip-out { to { opacity: 0; transform: scale(0.7); } }\n@keyframes hb-yf6-menu-in { from { opacity: 0; transform: translateY(4px) scale(0.98); } to { opacity: 1; transform: none; } }\n@keyframes hb-yf6-spin { to { transform: rotate(360deg); } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-yf6-chip, .hb-yf6-chip[data-leaving=\"true\"], .hb-yf6-menu, .hb-yf6-spin { animation: none; }\n  .hb-yf6-box, .hb-yf6-send, .hb-yf6-attach, .hb-yf6-x { transition: none; }\n}\n`;\n\nfunction formatSize(bytes?: number) {\n  if (bytes === undefined) return \"\";\n  if (bytes < 1024) return `${bytes} B`;\n  if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;\n  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;\n}\n\nfunction isImage(name: string) {\n  return /\\.(png|jpe?g|gif|webp|svg|avif)$/i.test(name);\n}\n\nexport function AgentComposerAttachments({\n  value,\n  defaultValue = \"\",\n  onValueChange,\n  attachments,\n  defaultAttachments = [],\n  onAttachmentsChange,\n  commands = defaultCommands,\n  onSend,\n  sending,\n  placeholder = \"Message the agent\",\n  accept,\n  accent = \"#2f7bff\",\n  className = \"\",\n  style,\n  ...rest\n}: AgentComposerAttachmentsProps) {\n  const uid = React.useId();\n  const fileRef = React.useRef<HTMLInputElement>(null);\n  const fieldRef = React.useRef<HTMLTextAreaElement>(null);\n  const counter = React.useRef(0);\n  const timers = React.useRef<number[]>([]);\n  const [innerText, setInnerText] = React.useState(defaultValue);\n  const [innerAtts, setInnerAtts] = React.useState(defaultAttachments);\n  const [leaving, setLeaving] = React.useState<string[]>([]);\n  const [dismissed, setDismissed] = React.useState(false);\n  const [active, setActive] = React.useState(0);\n  const [phase, setPhase] = React.useState<\"idle\" | \"sending\" | \"sent\">(\"idle\");\n\n  const textControlled = value !== undefined;\n  const attsControlled = attachments !== undefined;\n  const text = textControlled ? value : innerText;\n  const atts = attsControlled ? attachments : innerAtts;\n  const attsRef = React.useRef(atts);\n\n  React.useEffect(() => {\n    attsRef.current = atts;\n  }, [atts]);\n\n  React.useEffect(() => {\n    const pending = timers.current;\n    return () => pending.forEach((t) => window.clearTimeout(t));\n  }, []);\n\n  const slash = /(^|\\s)\\/([\\w-]*)$/.exec(text);\n  const query = slash ? slash[2].toLowerCase() : \"\";\n  const matches = slash\n    ? commands.filter((c) => c.name.toLowerCase().startsWith(query) || (query.length > 1 && c.name.toLowerCase().includes(query)))\n    : [];\n  const menuOpen = !!slash && matches.length > 0 && !dismissed;\n  const safeActive = Math.min(active, Math.max(0, matches.length - 1));\n\n  const busy = sending ?? phase === \"sending\";\n  const canSend = (text.trim().length > 0 || atts.length > 0) && !busy && phase !== \"sent\";\n  const state = busy ? \"sending\" : phase === \"sent\" ? \"sent\" : canSend ? \"ready\" : \"idle\";\n\n  function setText(next: string) {\n    if (!textControlled) setInnerText(next);\n    onValueChange?.(next);\n  }\n\n  function setAtts(next: ComposerAttachment[]) {\n    if (!attsControlled) setInnerAtts(next);\n    onAttachmentsChange?.(next);\n  }\n\n  function addFiles(list: FileList | null) {\n    if (!list || list.length === 0) return;\n    const added = Array.from(list).map((f) => {\n      counter.current += 1;\n      return { id: `${uid}-f${counter.current}`, name: f.name, size: f.size };\n    });\n    setAtts([...attsRef.current, ...added]);\n  }\n\n  function removeAttachment(id: string) {\n    setLeaving((l) => [...l, id]);\n    const t = window.setTimeout(() => {\n      setLeaving((l) => l.filter((x) => x !== id));\n      setAtts(attsRef.current.filter((a) => a.id !== id));\n    }, 190);\n    timers.current.push(t);\n  }\n\n  function pick(cmd: ComposerCommand) {\n    if (!slash) return;\n    const start = text.length - slash[0].length + slash[1].length;\n    setText(`${text.slice(0, start)}/${cmd.name} `);\n    setDismissed(true);\n    fieldRef.current?.focus({ preventScroll: true });\n  }\n\n  function finish() {\n    setText(\"\");\n    setAtts([]);\n    setPhase(\"sent\");\n    const t = window.setTimeout(() => setPhase(\"idle\"), 1100);\n    timers.current.push(t);\n  }\n\n  function send() {\n    if (!canSend) return;\n    const result = onSend?.(text, atts);\n    if (result && typeof (result as Promise<void>).then === \"function\") {\n      setPhase(\"sending\");\n      (result as Promise<void>).then(finish, () => setPhase(\"idle\"));\n    } else {\n      finish();\n    }\n  }\n\n  function onKeyDown(e: React.KeyboardEvent<HTMLTextAreaElement>) {\n    if (menuOpen) {\n      if (e.key === \"ArrowDown\") {\n        e.preventDefault();\n        setActive((safeActive + 1) % matches.length);\n        return;\n      }\n      if (e.key === \"ArrowUp\") {\n        e.preventDefault();\n        setActive((safeActive - 1 + matches.length) % matches.length);\n        return;\n      }\n      if (e.key === \"Enter\" || e.key === \"Tab\") {\n        e.preventDefault();\n        pick(matches[safeActive]);\n        return;\n      }\n      if (e.key === \"Escape\") {\n        e.preventDefault();\n        e.stopPropagation();\n        setDismissed(true);\n        return;\n      }\n    }\n    if (e.key === \"Enter\" && !e.shiftKey && !e.nativeEvent.isComposing) {\n      e.preventDefault();\n      send();\n    }\n  }\n\n  const listId = `${uid}-cmds`;\n\n  return (\n    <>\n      <style href=\"hb-agent-composer-attachments\" precedence=\"default\">{css}</style>\n      <div\n        className={`hb-yf6 ${className}`}\n        style={{ \"--hb-yf6-a\": accent, ...style } as React.CSSProperties}\n        {...rest}\n      >\n        {menuOpen ? (\n          <ul id={listId} role=\"listbox\" aria-label=\"Commands\" className=\"hb-yf6-menu\" onMouseDown={(e) => e.preventDefault()}>\n            {matches.map((c, i) => (\n              <li\n                key={c.name}\n                id={`${uid}-cmd-${i}`}\n                role=\"option\"\n                aria-selected={i === safeActive}\n                className=\"hb-yf6-cmd\"\n                onPointerEnter={() => setActive(i)}\n                onClick={() => pick(c)}\n              >\n                <span className=\"hb-yf6-name\">/{c.name}</span>\n                <span className=\"hb-yf6-desc\">{c.description}</span>\n              </li>\n            ))}\n          </ul>\n        ) : null}\n        <div className=\"hb-yf6-box\">\n          {atts.length > 0 ? (\n            <ul className=\"hb-yf6-chips\" style={{ listStyle: \"none\", margin: 0, padding: 0 }} aria-label=\"Attachments\">\n              {atts.map((a) => (\n                <li key={a.id} className=\"hb-yf6-chip\" data-leaving={leaving.includes(a.id)}>\n                  <svg className=\"hb-yf6-ico\" viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n                    {isImage(a.name) ? (\n                      <>\n                        <rect x=\"1.8\" y=\"2.4\" width=\"10.4\" height=\"9.2\" rx=\"1.8\" stroke=\"currentColor\" strokeWidth=\"1.2\" />\n                        <circle cx=\"5\" cy=\"5.5\" r=\"1\" fill=\"currentColor\" />\n                        <path d=\"M2.4 10.2l3-2.8 2.2 2 1.6-1.4 2.4 2.2\" stroke=\"currentColor\" strokeWidth=\"1.2\" strokeLinejoin=\"round\" />\n                      </>\n                    ) : (\n                      <path d=\"M3.2 1.8h5l2.6 2.6v7.8H3.2zM8.2 1.8v2.6h2.6M5 7h4M5 9.2h4\" stroke=\"currentColor\" strokeWidth=\"1.2\" strokeLinejoin=\"round\" strokeLinecap=\"round\" />\n                    )}\n                  </svg>\n                  <span className=\"hb-yf6-fname\" title={a.name}>{a.name}</span>\n                  {a.size !== undefined ? <span className=\"hb-yf6-fsize\">{formatSize(a.size)}</span> : null}\n                  <button type=\"button\" className=\"hb-yf6-x\" aria-label={`Remove ${a.name}`} onClick={() => removeAttachment(a.id)}>\n                    <svg viewBox=\"0 0 10 10\" fill=\"none\" aria-hidden=\"true\"><path d=\"M2 2l6 6M8 2l-6 6\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" /></svg>\n                  </button>\n                </li>\n              ))}\n            </ul>\n          ) : null}\n          <textarea\n            ref={fieldRef}\n            rows={1}\n            className=\"hb-yf6-field\"\n            placeholder={placeholder}\n            aria-label=\"Message\"\n            role=\"combobox\"\n            aria-autocomplete=\"list\"\n            aria-expanded={menuOpen}\n            aria-controls={menuOpen ? listId : undefined}\n            aria-activedescendant={menuOpen ? `${uid}-cmd-${safeActive}` : undefined}\n            value={text}\n            onChange={(e) => {\n              setText(e.target.value);\n              setDismissed(false);\n              setActive(0);\n            }}\n            onBlur={() => setDismissed(true)}\n            onFocus={() => setDismissed(false)}\n            onKeyDown={onKeyDown}\n          />\n          <div className=\"hb-yf6-bar\">\n            <span className=\"hb-yf6-tools\">\n              <button type=\"button\" className=\"hb-yf6-attach\" aria-label=\"Attach files\" onClick={() => fileRef.current?.click()}>\n                <svg viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n                  <path d=\"M11.4 6.6l-4.5 4.5a2.8 2.8 0 0 1-4-4L7.4 2.6a1.9 1.9 0 0 1 2.7 2.7L5.7 9.7a1 1 0 0 1-1.4-1.4l4-4\" stroke=\"currentColor\" strokeWidth=\"1.3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n                </svg>\n              </button>\n              <input\n                ref={fileRef}\n                type=\"file\"\n                multiple\n                accept={accept}\n                hidden\n                onChange={(e) => {\n                  addFiles(e.target.files);\n                  e.target.value = \"\";\n                }}\n              />\n              <span className=\"hb-yf6-hint\">\n                Type <kbd>/</kbd> for commands\n              </span>\n            </span>\n            <button\n              type=\"button\"\n              className=\"hb-yf6-send\"\n              data-state={state}\n              aria-label={state === \"sending\" ? \"Sending\" : state === \"sent\" ? \"Sent\" : \"Send message\"}\n              aria-disabled={state === \"idle\"}\n              onClick={send}\n            >\n              {state === \"sending\" ? (\n                <svg className=\"hb-yf6-spin\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n                  <circle cx=\"8\" cy=\"8\" r=\"5.6\" stroke=\"currentColor\" strokeOpacity=\"0.3\" strokeWidth=\"1.6\" />\n                  <path d=\"M8 2.4a5.6 5.6 0 0 1 5.6 5.6\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n                </svg>\n              ) : state === \"sent\" ? (\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\"><path d=\"M3.6 8.4l3 3 5.8-6.2\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" /></svg>\n              ) : (\n                <svg viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\"><path d=\"M8 13V3.4M3.8 7.4L8 3.2l4.2 4.2\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" /></svg>\n              )}\n            </button>\n          </div>\n        </div>\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}