{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "agent-file-diff",
  "type": "registry:component",
  "title": "Agent File Diff",
  "description": "A compact unified diff viewer with line numbers, hunk headers, added and removed lines, collapsible unchanged context and a button that copies the patch.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "agent-kit",
    "agent",
    "diff",
    "patch",
    "code",
    "review",
    "git"
  ],
  "docs": "https://hyperblur.dev/effects/agent-file-diff",
  "files": [
    {
      "path": "registry/hyperblur/agent-file-diff.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/agent-file-diff.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface AgentFileDiffProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onCopy\"> {\n  patch?: string;\n  fileName?: string;\n  context?: number;\n  accent?: string;\n  onCopy?: (patch: string) => void;\n}\n\ninterface DiffLine {\n  kind: \"add\" | \"del\" | \"ctx\";\n  text: string;\n  oldNo?: number;\n  newNo?: number;\n}\n\ninterface Hunk {\n  header: string;\n  lines: DiffLine[];\n}\n\ntype Item =\n  | { type: \"line\"; line: DiffLine }\n  | { type: \"fold\"; id: string; lines: DiffLine[] };\n\nconst samplePatch = [\n  \"--- a/src/session.ts\",\n  \"+++ b/src/session.ts\",\n  \"@@ -18,7 +18,8 @@ export async function loadSession(req)\",\n  \"   const token = readToken(req);\",\n  \"   const store = await openStore();\",\n  \"-  const user = store.get(token);\",\n  \"+  const user = token ? store.get(token) : null;\",\n  \"+  if (!user) return redirect(\\\"/login\\\");\",\n  \"   const prefs = await loadPrefs(user.id);\",\n  \"   const flags = await loadFlags(user.id);\",\n  \"   const theme = prefs.theme ?? \\\"dark\\\";\",\n  \"   return { user, prefs, flags, theme };\",\n].join(\"\\n\");\n\nconst css = `\n.hb-yf5 {\n  box-sizing: border-box;\n  width: 100%;\n  overflow: hidden;\n  font-family: inherit;\n  font-size: 12px;\n  color: #ededed;\n  background: #0d0d0d;\n  border: 1px solid #262626;\n  border-radius: 12px;\n  text-align: left;\n}\n.hb-yf5 *, .hb-yf5 *::before, .hb-yf5 *::after { box-sizing: border-box; font-family: inherit; }\n.hb-yf5-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; height: 34px; padding: 0 6px 0 12px; background: #131313; border-bottom: 1px solid #222222; }\n.hb-yf5-file { display: flex; align-items: center; gap: 7px; min-width: 0; font-family: var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, monospace); font-size: 11.5px; }\n.hb-yf5-file svg { flex: none; width: 13px; height: 13px; color: #8f8f8f; }\n.hb-yf5-file span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n.hb-yf5-stat { display: flex; align-items: center; gap: 8px; flex: none; }\n.hb-yf5-n { font-family: var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, monospace); font-size: 11px; font-variant-numeric: tabular-nums; }\n.hb-yf5-n[data-k=\"add\"] { color: #3ecf8e; }\n.hb-yf5-n[data-k=\"del\"] { color: #ff6b6b; }\n.hb-yf5-copy {\n  display: inline-flex;\n  align-items: center;\n  gap: 5px;\n  height: 24px;\n  margin: 0;\n  padding: 0 9px;\n  font: inherit;\n  font-size: 11px;\n  font-weight: 500;\n  color: #a1a1a1;\n  background: transparent;\n  border: 1px solid #2a2a2a;\n  border-radius: 999px;\n  cursor: pointer;\n  transition: color 0.15s ease, border-color 0.15s ease, background-color 0.15s ease;\n}\n.hb-yf5-copy svg { width: 11px; height: 11px; }\n.hb-yf5-copy:hover { color: #ededed; border-color: #3c3c3c; background: rgba(255, 255, 255, 0.05); }\n.hb-yf5-copy[data-done=\"true\"] { color: #3ecf8e; border-color: rgba(62, 207, 142, 0.45); }\n.hb-yf5-copy:focus-visible, .hb-yf5-fold:focus-visible { outline: 2px solid var(--hb-yf5-a, #2f7bff); outline-offset: -2px; }\n.hb-yf5-body { overflow-x: auto; scrollbar-width: thin; scrollbar-color: #333333 transparent; }\n.hb-yf5-grid { min-width: max-content; font-family: var(--font-geist-mono, ui-monospace, SFMono-Regular, Menlo, monospace); font-size: 11px; line-height: 16px; }\n.hb-yf5-hunk { display: block; padding: 3px 12px 3px 88px; color: color-mix(in srgb, var(--hb-yf5-a, #2f7bff) 60%, #ededed); background: color-mix(in srgb, var(--hb-yf5-a, #2f7bff) 9%, #0d0d0d); white-space: pre; }\n.hb-yf5-row { display: flex; min-width: 100%; width: max-content; }\n.hb-yf5-no { flex: none; width: 36px; padding-right: 8px; text-align: right; color: #565656; user-select: none; font-variant-numeric: tabular-nums; }\n.hb-yf5-sign { flex: none; width: 16px; text-align: center; user-select: none; color: #6b6b6b; }\n.hb-yf5-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }\n.hb-yf5-code { position: relative; flex: 1; padding-right: 14px; white-space: pre; color: #d4d4d4; }\n.hb-yf5-row[data-k=\"add\"] { background: rgba(62, 207, 142, 0.1); }\n.hb-yf5-row[data-k=\"add\"] .hb-yf5-no { background: rgba(62, 207, 142, 0.08); color: #4f9a77; }\n.hb-yf5-row[data-k=\"add\"] .hb-yf5-sign { color: #3ecf8e; }\n.hb-yf5-row[data-k=\"add\"] .hb-yf5-code { color: #c8f2dc; }\n.hb-yf5-row[data-k=\"del\"] { background: rgba(255, 93, 93, 0.1); }\n.hb-yf5-row[data-k=\"del\"] .hb-yf5-no { background: rgba(255, 93, 93, 0.08); color: #a36060; }\n.hb-yf5-row[data-k=\"del\"] .hb-yf5-sign { color: #ff6b6b; }\n.hb-yf5-row[data-k=\"del\"] .hb-yf5-code { color: #f6c9c9; }\n.hb-yf5-row[data-new=\"true\"] { animation: hb-yf5-in 0.25s ease both; }\n.hb-yf5-fold {\n  display: flex;\n  align-items: center;\n  gap: 8px;\n  width: 100%;\n  min-width: 100%;\n  height: 22px;\n  margin: 0;\n  padding: 0 12px 0 14px;\n  font: inherit;\n  font-family: inherit;\n  font-size: 11px;\n  color: #6f8fbf;\n  text-align: left;\n  background: #101114;\n  border: 0;\n  border-top: 1px solid #1a1a1a;\n  border-bottom: 1px solid #1a1a1a;\n  cursor: pointer;\n}\n.hb-yf5-fold:hover { color: #9dbdf0; background: #14161b; }\n.hb-yf5-fold svg { width: 11px; height: 11px; }\n@keyframes hb-yf5-in { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-yf5-row[data-new=\"true\"] { animation: none; }\n  .hb-yf5-copy { transition: none; }\n}\n`;\n\nfunction parsePatch(patch: string) {\n  const hunks: Hunk[] = [];\n  let file: string | undefined;\n  let hunk: Hunk | null = null;\n  let oldNo = 0;\n  let newNo = 0;\n  const rows = patch.replace(/\\r\\n/g, \"\\n\").split(\"\\n\");\n  if (rows[rows.length - 1] === \"\") rows.pop();\n  for (const raw of rows) {\n    if (raw.startsWith(\"@@\")) {\n      const m = /^@@ -(\\d+)(?:,\\d+)? \\+(\\d+)(?:,\\d+)? @@/.exec(raw);\n      oldNo = m ? Number(m[1]) : 1;\n      newNo = m ? Number(m[2]) : 1;\n      hunk = { header: raw, lines: [] };\n      hunks.push(hunk);\n      continue;\n    }\n    if (!hunk) {\n      if (raw.startsWith(\"+++ \")) {\n        const path = raw.slice(4).trim();\n        file = path.startsWith(\"b/\") ? path.slice(2) : path;\n      }\n      continue;\n    }\n    if (raw.startsWith(\"\\\\\")) continue;\n    const mark = raw[0];\n    const text = raw.slice(1);\n    if (mark === \"+\") hunk.lines.push({ kind: \"add\", text, newNo: newNo++ });\n    else if (mark === \"-\") hunk.lines.push({ kind: \"del\", text, oldNo: oldNo++ });\n    else hunk.lines.push({ kind: \"ctx\", text, oldNo: oldNo++, newNo: newNo++ });\n  }\n  return { file, hunks };\n}\n\nfunction fold(hunk: Hunk, index: number, context: number): Item[] {\n  const lines = hunk.lines;\n  const changed = lines.some((l) => l.kind !== \"ctx\");\n  const items: Item[] = [];\n  let i = 0;\n  while (i < lines.length) {\n    if (lines[i].kind !== \"ctx\") {\n      items.push({ type: \"line\", line: lines[i] });\n      i += 1;\n      continue;\n    }\n    let j = i;\n    while (j < lines.length && lines[j].kind === \"ctx\") j += 1;\n    const run = lines.slice(i, j);\n    const head = i === 0 ? 0 : context;\n    const tail = j === lines.length ? 0 : context;\n    const hidden = run.length - head - tail;\n    if (!changed || hidden < 2) {\n      run.forEach((line) => items.push({ type: \"line\", line }));\n    } else {\n      run.slice(0, head).forEach((line) => items.push({ type: \"line\", line }));\n      items.push({ type: \"fold\", id: `${index}-${i}`, lines: run.slice(head, run.length - tail) });\n      run.slice(run.length - tail).forEach((line) => items.push({ type: \"line\", line }));\n    }\n    i = j;\n  }\n  return items;\n}\n\nexport function AgentFileDiff({\n  patch = samplePatch,\n  fileName,\n  context = 2,\n  accent = \"#2f7bff\",\n  onCopy,\n  className = \"\",\n  style,\n  ...rest\n}: AgentFileDiffProps) {\n  const [open, setOpen] = React.useState<string[]>([]);\n  const [copied, setCopied] = React.useState(false);\n  const timer = React.useRef(0);\n  const parsed = React.useMemo(() => parsePatch(patch), [patch]);\n\n  React.useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  const adds = parsed.hunks.reduce((n, h) => n + h.lines.filter((l) => l.kind === \"add\").length, 0);\n  const dels = parsed.hunks.reduce((n, h) => n + h.lines.filter((l) => l.kind === \"del\").length, 0);\n\n  async function copy() {\n    try {\n      await navigator.clipboard.writeText(patch);\n    } catch {\n      const area = document.createElement(\"textarea\");\n      area.value = patch;\n      area.style.position = \"fixed\";\n      area.style.opacity = \"0\";\n      document.body.appendChild(area);\n      area.select();\n      document.execCommand(\"copy\");\n      area.remove();\n    }\n    onCopy?.(patch);\n    setCopied(true);\n    window.clearTimeout(timer.current);\n    timer.current = window.setTimeout(() => setCopied(false), 1600);\n  }\n\n  function renderLine(line: DiffLine, key: string, isNew: boolean) {\n    return (\n      <div className=\"hb-yf5-row\" data-k={line.kind} data-new={isNew} key={key}>\n        <span className=\"hb-yf5-no\" aria-hidden=\"true\">{line.oldNo ?? \"\"}</span>\n        <span className=\"hb-yf5-no\" aria-hidden=\"true\">{line.newNo ?? \"\"}</span>\n        <span className=\"hb-yf5-sign\" aria-hidden=\"true\">{line.kind === \"add\" ? \"+\" : line.kind === \"del\" ? \"-\" : \"\"}</span>\n        <span className=\"hb-yf5-code\">\n          <span className=\"hb-yf5-sr\">\n            {line.kind === \"add\" ? \"Added: \" : line.kind === \"del\" ? \"Removed: \" : \"\"}\n          </span>\n          {line.text || \" \"}\n        </span>\n      </div>\n    );\n  }\n\n  return (\n    <>\n      <style href=\"hb-agent-file-diff\" precedence=\"default\">{css}</style>\n      <div\n        className={`hb-yf5 ${className}`}\n        style={{ \"--hb-yf5-a\": accent, ...style } as React.CSSProperties}\n        {...rest}\n      >\n        <div className=\"hb-yf5-head\">\n          <span className=\"hb-yf5-file\">\n            <svg viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n              <path d=\"M3 1.8h5l3 3v7.4H3z M8 1.8v3h3\" stroke=\"currentColor\" strokeWidth=\"1.2\" strokeLinejoin=\"round\" />\n            </svg>\n            <span>{fileName ?? parsed.file ?? \"changes.patch\"}</span>\n          </span>\n          <span className=\"hb-yf5-stat\">\n            <span className=\"hb-yf5-n\" data-k=\"add\">+{adds}</span>\n            <span className=\"hb-yf5-n\" data-k=\"del\">-{dels}</span>\n            <button type=\"button\" className=\"hb-yf5-copy\" data-done={copied} onClick={copy} aria-live=\"polite\">\n              {copied ? (\n                <svg viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\"><path d=\"M2.4 6.4l2.4 2.4 4.8-5\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\" /></svg>\n              ) : (\n                <svg viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\"><rect x=\"4\" y=\"4\" width=\"6.2\" height=\"6.2\" rx=\"1.4\" stroke=\"currentColor\" strokeWidth=\"1.2\" /><path d=\"M8 4V3a1.2 1.2 0 0 0-1.2-1.2H3A1.2 1.2 0 0 0 1.8 3v3.8A1.2 1.2 0 0 0 3 8h1\" stroke=\"currentColor\" strokeWidth=\"1.2\" /></svg>\n              )}\n              {copied ? \"Copied\" : \"Copy patch\"}\n            </button>\n          </span>\n        </div>\n        <div className=\"hb-yf5-body\" role=\"region\" aria-label=\"Diff\" tabIndex={0}>\n          <div className=\"hb-yf5-grid\">\n            {parsed.hunks.map((h, hi) => (\n              <div key={hi}>\n                <span className=\"hb-yf5-hunk\">{h.header}</span>\n                {fold(h, hi, context).map((item, ii) => {\n                  if (item.type === \"line\") return renderLine(item.line, `${hi}-l-${ii}`, false);\n                  const shown = open.includes(item.id);\n                  if (shown) return <React.Fragment key={item.id}>{item.lines.map((l, k) => renderLine(l, `${item.id}-${k}`, true))}</React.Fragment>;\n                  return (\n                    <button\n                      type=\"button\"\n                      key={item.id}\n                      className=\"hb-yf5-fold\"\n                      aria-label={`Show ${item.lines.length} unchanged lines`}\n                      onClick={() => setOpen((o) => [...o, item.id])}\n                    >\n                      <svg viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n                        <path d=\"M3 4.5L6 1.8l3 2.7M3 7.5L6 10.2l3-2.7\" stroke=\"currentColor\" strokeWidth=\"1.3\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n                      </svg>\n                      {item.lines.length} unchanged lines\n                    </button>\n                  );\n                })}\n              </div>\n            ))}\n          </div>\n        </div>\n      </div>\n    </>\n  );\n}\n"
    }
  ]
}