{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sources-web",
  "title": "Sources (Web Components)",
  "description": "The sources a response used, as a compact count that opens a list of links.",
  "dependencies": [
    "@kind-ui/agent-ui@0.9.0",
    "@ui-sdk/core@0.1.0-beta.17",
    "@ui-sdk/web@0.1.0-beta.17"
  ],
  "registryDependencies": [
    "@kind-ui/agent-ui-setup"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/sources.ts",
      "content": "import \"@ui-sdk/web/avatar\";\nimport {\n  type SourcesApi,\n  type SourcesOpenChangeReason,\n  type SourcesOptions,\n  type SourcesState,\n  sourcesPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton, wrappedChild } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement, UiPartElement } from \"@ui-sdk/web/events\";\n\nconst DEFAULT_PARTS = `<kind-sources-trigger><button></button></kind-sources-trigger><kind-sources-content><kind-sources-list></kind-sources-list></kind-sources-content>`;\nconst ITEM_PARTS = `<kind-sources-link><a><kind-sources-icon></kind-sources-icon><kind-sources-title></kind-sources-title><kind-sources-host></kind-sources-host></a></kind-sources-link><kind-sources-snippet></kind-sources-snippet>`;\n\n// Parts this element wrote into, and items it added. A part someone wrote into keeps it, and an item someone wrote\n// stays, as React's children do. Kept across instances, because UI SDK rebuilds one when the element returns to\n// the page.\nconst owned = new WeakSet<Element>();\n\nconst claim = (part: Element | null): part is Element => {\n  if (!part || !(owned.has(part) || !part.firstChild)) return false;\n  owned.add(part);\n  return true;\n};\n\nconst fill = (part: Element | null, text: string) => {\n  if (claim(part) && part.textContent !== text) part.textContent = text;\n};\n\n/** Fills an empty icon part with a UI SDK Avatar, and keeps the one it wrote current. */\nconst drawIcon = (part: Element | null, { favicon }: SourcesState[\"entries\"][string]) => {\n  if (!claim(part)) return;\n  let avatar = part.querySelector(\"ui-avatar\");\n  if (!avatar) {\n    avatar = part.ownerDocument.createElement(\"ui-avatar\");\n    avatar.append(part.ownerDocument.createElement(\"ui-avatar-fallback\"));\n    part.replaceChildren(avatar);\n  }\n  let image = avatar.querySelector(\":scope > img\");\n  if (favicon) {\n    if (!image) {\n      image = part.ownerDocument.createElement(\"img\");\n      image.setAttribute(\"alt\", \"\");\n      avatar.prepend(image);\n    }\n    if (image.getAttribute(\"src\") !== favicon) image.setAttribute(\"src\", favicon);\n  } else image?.remove();\n};\n\ntype Movable = Element & { moveBefore?: (node: Node, child: Node | null) => void };\n\n// `moveBefore` keeps a focused link focused where the browser has it; `insertBefore` blurs it.\nfunction move(list: Movable, item: Element, before: Element | null): void {\n  if (list.moveBefore && item.isConnected) list.moveBefore(item, before);\n  else list.insertBefore(item, before);\n}\n\n/**\n * Adds an item for each source the list lacks, in order, removes the items it added for sources that left or that\n * a written item replaced, and fills in every item's parts.\n */\nconst drawItems = (list: Movable, { sources, entries }: SourcesState) => {\n  const children = new Map<string | null, Element>();\n  for (const child of [...list.children]) {\n    const value = child.getAttribute(\"value\");\n    const earlier = children.get(value);\n    if (earlier && owned.has(child)) {\n      child.remove();\n      continue;\n    }\n    if (earlier && owned.has(earlier)) earlier.remove();\n    if (!earlier || owned.has(earlier)) children.set(value, child);\n  }\n  const ids = new Set<string | null>(sources.map((source) => source.id));\n  for (const [id, child] of children) if (owned.has(child) && !ids.has(id)) child.remove();\n  let previous: Element | null = null;\n  for (const { id } of sources) {\n    const entry = entries[id];\n    if (!entry) continue;\n    let child = children.get(id);\n    if (!child) {\n      child = list.ownerDocument.createElement(\"kind-sources-item\");\n      child.setAttribute(\"value\", id);\n      owned.add(child);\n    }\n    const before: Element | null = previous ? previous.nextElementSibling : list.firstElementChild;\n    if (owned.has(child) && before !== child) move(list, child, before);\n    previous = child;\n    if (!child.firstElementChild) child.innerHTML = ITEM_PARTS;\n    drawIcon(child.querySelector(\"kind-sources-icon\"), entry);\n    fill(child.querySelector(\"kind-sources-title\"), entry.title);\n    fill(child.querySelector(\"kind-sources-host\"), entry.host);\n    fill(child.querySelector(\"kind-sources-snippet\"), entry.snippet);\n  }\n};\n\n/**\n * `<kind-sources>` fills in its trigger and content when it has no children. Set `sources` as a property: its list\n * gets a `kind-sources-item` for each source it lacks, in order, and every item's empty parts get a link to the\n * source with its icon, title and host name, then its snippet. Write a `<kind-sources-item value=\"…\">` to render a\n * source yourself.\n */\nexport function defineSources(): void {\n  if (customElements.get(\"kind-sources\")) return;\n  createParts(sourcesPrimitive, {\n    root: {\n      tag: \"kind-sources\",\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\", \"toggle\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const draw = () => {\n          const state = instance.get() as SourcesState;\n          fill(el.querySelector(\"kind-sources-trigger > button\"), state.label);\n          const list = el.querySelector(\"kind-sources-list\");\n          if (list) drawItems(list, state);\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        // Parts and items written or removed after the element connects redraw it.\n        const arrivals = new MutationObserver((records) => {\n          const changed = records.some((record) =>\n            [...record.addedNodes, ...record.removedNodes].some((node) => node instanceof Element),\n          );\n          if (changed) draw();\n        });\n        arrivals.observe(el, { childList: true, subtree: true });\n        return () => {\n          unsubscribe();\n          arrivals.disconnect();\n        };\n      },\n    },\n    trigger: { tag: \"kind-sources-trigger\", target: wrappedButton, display: \"contents\" },\n    content: \"kind-sources-content\",\n    list: \"kind-sources-list\",\n    item: \"kind-sources-item\",\n    link: { tag: \"kind-sources-link\", target: wrappedChild(\"a\"), display: \"contents\" },\n    icon: \"kind-sources-icon\",\n    title: \"kind-sources-title\",\n    host: \"kind-sources-host\",\n    snippet: \"kind-sources-snippet\",\n  });\n}\n\nexport type KindSourcesEvents = {\n  openchange: UiChangeEvent<boolean, SourcesOpenChangeReason>;\n};\n\nexport interface KindSourcesElement\n  extends UiElement<KindSourcesEvents>,\n    AttributeProperties<typeof sourcesPrimitive, \"api\" | \"open\" | \"sources\"> {\n  open: boolean;\n  sources?: SourcesOptions[\"sources\"];\n  api: Pick<SourcesApi, \"isOpen\" | \"setOpen\" | \"toggle\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-sources\": KindSourcesElement;\n    \"kind-sources-trigger\": HTMLElement;\n    \"kind-sources-content\": HTMLElement;\n    \"kind-sources-list\": HTMLElement;\n    \"kind-sources-item\": UiPartElement<typeof sourcesPrimitive, \"item\", \"value\">;\n    \"kind-sources-link\": HTMLElement;\n    \"kind-sources-icon\": HTMLElement;\n    \"kind-sources-title\": HTMLElement;\n    \"kind-sources-host\": HTMLElement;\n    \"kind-sources-snippet\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/sources.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
