{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "agent-memory-scopes",
  "type": "registry:component",
  "title": "Agent Memory Scopes",
  "description": "Agent memory split into You, Project and This chat, with a token budget bar that packs pinned facts first and marks the ones that no longer fit. Facts edit in place, pause with a switch, glow when they were used in the last reply and can be added to the open scope.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "agent-kit",
    "memory",
    "context",
    "tokens",
    "agents",
    "editable",
    "budget"
  ],
  "docs": "https://hyperblur.dev/effects/agent-memory-scopes",
  "files": [
    {
      "path": "registry/hyperblur/agent-memory-scopes.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/agent-memory-scopes.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type MemoryScope = \"user\" | \"project\" | \"chat\";\n\nexport interface ScopedFact {\n  id: string;\n  text: string;\n  scope: MemoryScope;\n  pinned?: boolean;\n  enabled?: boolean;\n}\n\nexport interface AgentMemoryScopesProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"title\"> {\n  facts?: ScopedFact[];\n  defaultFacts?: ScopedFact[];\n  onFactsChange?: (facts: ScopedFact[]) => void;\n  recalledIds?: string[];\n  budget?: number;\n  defaultScope?: MemoryScope;\n  title?: string;\n  accent?: string;\n}\n\nconst sampleFacts: ScopedFact[] = [\n  { id: \"f1\", scope: \"user\", text: \"Goes by Sam, writes in British English.\", pinned: true },\n  { id: \"f2\", scope: \"user\", text: \"Prefers diffs over full files when reviewing code.\" },\n  { id: \"f3\", scope: \"user\", text: \"Works 9:00 to 17:30 Lisbon time, no meetings on Fridays.\" },\n  { id: \"f4\", scope: \"project\", text: \"Monorepo uses pnpm workspaces and Turborepo.\", pinned: true },\n  { id: \"f5\", scope: \"project\", text: \"API errors follow RFC 9457 problem+json.\" },\n  { id: \"f6\", scope: \"project\", text: \"Money is stored as integer cents, never floats.\" },\n  { id: \"f7\", scope: \"project\", text: \"Feature flags live in config/flags.yaml and ship dark by default.\" },\n  { id: \"f8\", scope: \"chat\", text: \"We are fixing proration for February invoices.\" },\n  { id: \"f9\", scope: \"chat\", text: \"Tests must pass on Node 22 and 24.\" },\n];\n\nconst scopeMeta: Record<MemoryScope, { label: string; color: string; hint: string }> = {\n  user: { label: \"You\", color: \"#9a7bff\", hint: \"Follows you across projects\" },\n  project: { label: \"Project\", color: \"#3cc6e0\", hint: \"Shared with everyone in harbor-api\" },\n  chat: { label: \"This chat\", color: \"#f0b24a\", hint: \"Forgotten when the chat ends\" },\n};\nconst order: MemoryScope[] = [\"user\", \"project\", \"chat\"];\nconst tokens = (t: string) => Math.ceil(t.length / 4) + 3;\n\nconst css = `\n.hb-wj7 {\n  --hb-wj7-a: #2f7bff;\n  box-sizing: border-box;\n  width: 100%;\n  max-width: 420px;\n  font-family: inherit;\n  font-size: 12px;\n  color: #ededed;\n  background: linear-gradient(180deg, #141414, #0f0f0f);\n  border: 1px solid #262626;\n  border-radius: 14px;\n  box-shadow: 0 1px 0 rgba(255,255,255,0.05) inset, 0 30px 60px -36px rgba(0,0,0,0.9);\n  overflow: hidden;\n}\n.hb-wj7 *, .hb-wj7 *::before, .hb-wj7 *::after { box-sizing: border-box; font-family: inherit; }\n.hb-wj7-top { padding: 11px 12px 9px; }\n.hb-wj7-hr { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }\n.hb-wj7-h { margin: 0; font-size: 12.5px; font-weight: 600; letter-spacing: -0.01em; }\n.hb-wj7-use { font-size: 10.5px; color: #8f8f8f; font-variant-numeric: tabular-nums; }\n.hb-wj7-use b { color: #ededed; font-weight: 600; }\n.hb-wj7-use[data-over=\"true\"] b { color: #ff6b5f; }\n.hb-wj7-meter { position: relative; display: flex; gap: 2px; height: 6px; margin-top: 7px; padding: 0; overflow: hidden; background: #1c1c1c; border-radius: 4px; }\n.hb-wj7-seg { height: 100%; background: var(--c); border-radius: 2px; transition: width .45s cubic-bezier(.2,.8,.2,1); }\n.hb-wj7-tabs { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); margin: 0 12px; border-bottom: 1px solid #222; }\n.hb-wj7-tab { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 4px 8px; font: inherit; font-size: 11.5px; font-weight: 500; color: #8f8f8f; background: none; border: 0; cursor: pointer; transition: color .15s ease; }\n.hb-wj7-tab i { width: 6px; height: 6px; border-radius: 50%; background: var(--c); font-style: normal; opacity: .55; transition: opacity .15s ease; }\n.hb-wj7-tab small { font-size: 10px; color: #5f5f5f; font-variant-numeric: tabular-nums; }\n.hb-wj7-tab[aria-selected=\"true\"] { color: #fff; }\n.hb-wj7-tab[aria-selected=\"true\"] i { opacity: 1; }\n.hb-wj7-ind { position: absolute; bottom: -1px; left: 0; width: calc(100% / 3); height: 2px; background: var(--c); border-radius: 2px; transition: transform .35s cubic-bezier(.3,1.2,.4,1), background-color .3s ease; }\n.hb-wj7-hint { margin: 7px 12px 2px; font-size: 10.5px; color: #6f6f6f; }\n.hb-wj7-list { margin: 0; padding: 2px 6px; list-style: none; }\n.hb-wj7-f { position: relative; display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 4px; padding: 4px 4px 4px 2px; border-radius: 9px; animation: hb-wj7-in .32s cubic-bezier(.2,.8,.2,1) both; transition: background-color .15s ease, opacity .2s ease; }\n.hb-wj7-f:hover, .hb-wj7-f:focus-within { background: #191919; }\n.hb-wj7-f[data-off=\"true\"] .hb-wj7-tx, .hb-wj7-f[data-cut=\"true\"] .hb-wj7-tx { color: #6a6a6a; }\n.hb-wj7-f[data-recalled=\"true\"]::before { content: \"\"; position: absolute; inset: 0; border-radius: 9px; border: 1px solid color-mix(in srgb, var(--c) 55%, transparent); background: color-mix(in srgb, var(--c) 7%, transparent); animation: hb-wj7-glow 2.4s ease-in-out infinite; pointer-events: none; }\n.hb-wj7-sw { position: relative; justify-self: center; width: 20px; height: 12px; padding: 0; background: #2a2a2a; border: 0; border-radius: 999px; cursor: pointer; transition: background-color .2s ease; }\n.hb-wj7-sw::after { content: \"\"; position: absolute; top: 2px; left: 2px; width: 8px; height: 8px; border-radius: 50%; background: #8f8f8f; transition: transform .25s cubic-bezier(.3,1.5,.5,1), background-color .2s ease; }\n.hb-wj7-sw[aria-checked=\"true\"] { background: color-mix(in srgb, var(--c) 45%, #1a1a1a); }\n.hb-wj7-sw[aria-checked=\"true\"]::after { transform: translateX(8px); background: #fff; }\n.hb-wj7-body { min-width: 0; }\n.hb-wj7-tx { display: block; width: 100%; padding: 2px 4px; font: inherit; font-size: 12px; line-height: 1.35; text-align: left; color: #e5e5e5; background: none; border: 0; border-radius: 5px; cursor: text; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transition: color .2s ease; }\n.hb-wj7-ed { display: block; width: 100%; padding: 2px 6px; font: inherit; font-size: 12px; line-height: 1.35; color: #fff; background: #0a0a0a; border: 1px solid var(--hb-wj7-a); border-radius: 6px; outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-wj7-a) 22%, transparent); }\n.hb-wj7-tags { display: flex; gap: 5px; padding: 0 4px; min-height: 0; }\n.hb-wj7-tag { margin-top: 1px; font-size: 9.5px; font-weight: 500; color: #6f6f6f; white-space: nowrap; font-variant-numeric: tabular-nums; }\n.hb-wj7-tag[data-k=\"recall\"] { color: var(--c); }\n.hb-wj7-tag[data-k=\"cut\"] { color: #ff8a80; }\n.hb-wj7-acts { display: flex; gap: 1px; }\n.hb-wj7-ib { display: grid; place-items: center; width: 22px; height: 22px; padding: 0; color: #6f6f6f; background: none; border: 0; border-radius: 6px; cursor: pointer; transition: color .15s ease, background-color .15s ease, opacity .15s ease; }\n.hb-wj7-ib svg { width: 12px; height: 12px; }\n.hb-wj7-ib:hover { color: #fff; background: #242424; }\n.hb-wj7-ib[data-k=\"del\"] { opacity: 0; }\n.hb-wj7-f:hover .hb-wj7-ib[data-k=\"del\"], .hb-wj7-ib[data-k=\"del\"]:focus-visible { opacity: 1; }\n.hb-wj7-ib[data-k=\"del\"]:hover { color: #ff7a7a; background: rgba(255,90,79,0.12); }\n.hb-wj7-ib[aria-pressed=\"true\"] { color: var(--c); }\n.hb-wj7-add { display: flex; align-items: center; gap: 8px; margin: 4px 12px 12px; padding: 0 4px 0 10px; height: 32px; background: #0b0b0b; border: 1px solid #262626; border-radius: 9px; transition: border-color .15s ease, box-shadow .2s ease; }\n.hb-wj7-add:focus-within { border-color: color-mix(in srgb, var(--hb-wj7-a) 70%, transparent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--hb-wj7-a) 18%, transparent); }\n.hb-wj7-add svg { flex: none; width: 13px; height: 13px; color: #6f6f6f; }\n.hb-wj7-add input { flex: 1; min-width: 0; height: 100%; padding: 0; font: inherit; font-size: 12px; color: #ededed; background: none; border: 0; outline: none; }\n.hb-wj7-add input::placeholder { color: #5f5f5f; }\n.hb-wj7-add button { height: 24px; padding: 0 9px; font: inherit; font-size: 11px; font-weight: 500; color: #fff; background: var(--hb-wj7-a); border: 0; border-radius: 6px; cursor: pointer; transition: opacity .15s ease; }\n.hb-wj7-add button:disabled { opacity: .3; cursor: default; }\n.hb-wj7-tab:focus-visible, .hb-wj7-sw:focus-visible, .hb-wj7-tx:focus-visible, .hb-wj7-ib:focus-visible, .hb-wj7-add button:focus-visible { outline: 2px solid var(--hb-wj7-a); outline-offset: 1px; }\n@keyframes hb-wj7-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }\n@keyframes hb-wj7-glow { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-wj7 *, .hb-wj7 *::before, .hb-wj7 *::after { transition: none !important; animation: none !important; }\n}\n`;\n\nexport function AgentMemoryScopes({\n  facts,\n  defaultFacts = sampleFacts,\n  onFactsChange,\n  recalledIds = [\"f6\", \"f8\"],\n  budget = 120,\n  defaultScope = \"project\",\n  title = \"Memory\",\n  accent = \"#2f7bff\",\n  className,\n  style,\n  ...rest\n}: AgentMemoryScopesProps) {\n  const [inner, setInner] = React.useState(defaultFacts);\n  const list = facts ?? inner;\n  const [scope, setScope] = React.useState<MemoryScope>(defaultScope);\n  const [editing, setEditing] = React.useState<string | null>(null);\n  const [draft, setDraft] = React.useState(\"\");\n  const [adding, setAdding] = React.useState(\"\");\n  const counter = React.useRef(0);\n  const tabRefs = React.useRef<(HTMLButtonElement | null)[]>([]);\n\n  const commit = (next: ScopedFact[]) => {\n    if (facts === undefined) setInner(next);\n    onFactsChange?.(next);\n  };\n\n  const packed = React.useMemo(() => {\n    const included = new Set<string>();\n    let used = 0;\n    const live = list.filter((f) => f.enabled !== false);\n    const ranked = [...live.filter((f) => f.pinned), ...live.filter((f) => !f.pinned)];\n    for (const f of ranked) {\n      const t = tokens(f.text);\n      if (f.pinned || used + t <= budget) {\n        included.add(f.id);\n        used += t;\n      }\n    }\n    const byScope = order.map((s) => list.filter((f) => f.scope === s && included.has(f.id)).reduce((n, f) => n + tokens(f.text), 0));\n    return { included, used, byScope };\n  }, [list, budget]);\n\n  const patch = (id: string, p: Partial<ScopedFact>) => commit(list.map((f) => (f.id === id ? { ...f, ...p } : f)));\n  const shown = list.filter((f) => f.scope === scope);\n  const idx = order.indexOf(scope);\n  const c = scopeMeta[scope].color;\n  const recalled = new Set(recalledIds);\n\n  const saveEdit = () => {\n    if (editing && draft.trim()) patch(editing, { text: draft.trim() });\n    setEditing(null);\n  };\n\n  const add = () => {\n    const text = adding.trim();\n    if (!text) return;\n    counter.current += 1;\n    commit([...list, { id: `new-${counter.current}`, text, scope, enabled: true }]);\n    setAdding(\"\");\n  };\n\n  return (\n    <div className={[\"hb-wj7\", className].filter(Boolean).join(\" \")} style={{ \"--hb-wj7-a\": accent, ...style } as React.CSSProperties} {...rest}>\n      <style href=\"hb-agent-memory-scopes\" precedence=\"default\">{css}</style>\n      <div className=\"hb-wj7-top\">\n        <div className=\"hb-wj7-hr\">\n          <h3 className=\"hb-wj7-h\">{title}</h3>\n          <span className=\"hb-wj7-use\" data-over={packed.used > budget}>\n            <b>{packed.used}</b> / {budget} tokens in context\n          </span>\n        </div>\n        <div className=\"hb-wj7-meter\" role=\"meter\" aria-label=\"Context budget\" aria-valuemin={0} aria-valuemax={budget} aria-valuenow={packed.used}>\n          {order.map((s, i) => (\n            <span key={s} className=\"hb-wj7-seg\" title={`${scopeMeta[s].label}: ${packed.byScope[i]} tokens`} style={{ \"--c\": scopeMeta[s].color, width: `${Math.min(100, (packed.byScope[i] / Math.max(budget, packed.used)) * 100).toFixed(2)}%` } as React.CSSProperties} />\n          ))}\n        </div>\n      </div>\n      <div className=\"hb-wj7-tabs\" role=\"tablist\" aria-label=\"Memory scope\">\n        {order.map((s, i) => (\n          <button\n            key={s}\n            ref={(el) => { tabRefs.current[i] = el; }}\n            type=\"button\"\n            role=\"tab\"\n            aria-selected={scope === s}\n            tabIndex={scope === s ? 0 : -1}\n            className=\"hb-wj7-tab\"\n            style={{ \"--c\": scopeMeta[s].color } as React.CSSProperties}\n            onClick={() => setScope(s)}\n            onKeyDown={(e) => {\n              if (e.key !== \"ArrowRight\" && e.key !== \"ArrowLeft\") return;\n              e.preventDefault();\n              const n = (i + (e.key === \"ArrowRight\" ? 1 : -1) + 3) % 3;\n              setScope(order[n]);\n              tabRefs.current[n]?.focus();\n            }}\n          >\n            <i />\n            {scopeMeta[s].label}\n            <small>{list.filter((f) => f.scope === s).length}</small>\n          </button>\n        ))}\n        <span className=\"hb-wj7-ind\" style={{ \"--c\": c, transform: `translateX(${idx * 100}%)` } as React.CSSProperties} />\n      </div>\n      <p className=\"hb-wj7-hint\">{scopeMeta[scope].hint}</p>\n      <ul className=\"hb-wj7-list\" role=\"tabpanel\" aria-label={`${scopeMeta[scope].label} memories`}>\n        {shown.map((f) => {\n          const on = f.enabled !== false;\n          const cut = on && !packed.included.has(f.id);\n          const isRecalled = recalled.has(f.id) && on && !cut;\n          return (\n            <li key={f.id} className=\"hb-wj7-f\" data-off={!on} data-cut={cut} data-recalled={isRecalled} style={{ \"--c\": c } as React.CSSProperties}>\n              <button type=\"button\" role=\"switch\" aria-checked={on} aria-label={on ? \"Exclude from context\" : \"Include in context\"} className=\"hb-wj7-sw\" onClick={() => patch(f.id, { enabled: !on })} />\n              <div className=\"hb-wj7-body\">\n                {editing === f.id ? (\n                  <input\n                    className=\"hb-wj7-ed\"\n                    autoFocus\n                    value={draft}\n                    aria-label=\"Edit memory\"\n                    onChange={(e) => setDraft(e.target.value)}\n                    onBlur={saveEdit}\n                    onKeyDown={(e) => {\n                      if (e.key === \"Enter\") saveEdit();\n                      if (e.key === \"Escape\") setEditing(null);\n                    }}\n                  />\n                ) : (\n                  <button type=\"button\" className=\"hb-wj7-tx\" title={`${f.text} (click to edit)`} onClick={() => { setEditing(f.id); setDraft(f.text); }}>\n                    {f.text}\n                  </button>\n                )}\n                <div className=\"hb-wj7-tags\">\n                  <span className=\"hb-wj7-tag\">{tokens(f.text)} tok</span>\n                  {isRecalled && <span className=\"hb-wj7-tag\" data-k=\"recall\">Used in last reply</span>}\n                  {cut && <span className=\"hb-wj7-tag\" data-k=\"cut\">Over budget, not sent</span>}\n                  {!on && <span className=\"hb-wj7-tag\">Paused</span>}\n                </div>\n              </div>\n              <div className=\"hb-wj7-acts\">\n                <button type=\"button\" className=\"hb-wj7-ib\" aria-pressed={!!f.pinned} aria-label={f.pinned ? \"Unpin\" : \"Pin, always send\"} onClick={() => patch(f.id, { pinned: !f.pinned })}>\n                  <svg viewBox=\"0 0 16 16\" fill={f.pinned ? \"currentColor\" : \"none\"} stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinejoin=\"round\" strokeLinecap=\"round\" aria-hidden=\"true\"><path d=\"M9.5 2.5 13.5 6.5 11 7.5 8.5 10l.5 3-6.5-6.5 3 .5L8 4.5Z\" /><path d=\"m5 11-2.5 2.5\" /></svg>\n                </button>\n                <button type=\"button\" className=\"hb-wj7-ib\" data-k=\"del\" aria-label=\"Forget\" onClick={() => commit(list.filter((x) => x.id !== f.id))}>\n                  <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\"><path d=\"M3 4.5h10M6.5 4.5V3h3v1.5M4.5 4.5l.6 8.5h5.8l.6-8.5\" /></svg>\n                </button>\n              </div>\n            </li>\n          );\n        })}\n      </ul>\n      <form className=\"hb-wj7-add\" onSubmit={(e) => { e.preventDefault(); add(); }}>\n        <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" aria-hidden=\"true\"><path d=\"M8 3v10M3 8h10\" /></svg>\n        <input value={adding} onChange={(e) => setAdding(e.target.value)} placeholder={`Remember for ${scopeMeta[scope].label.toLowerCase()}…`} aria-label=\"New memory\" />\n        <button type=\"submit\" disabled={!adding.trim()}>Save</button>\n      </form>\n    </div>\n  );\n}\n"
    }
  ]
}