{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "inline-citation-web",
  "title": "Inline Citation (Web Components)",
  "description": "A marker in running text whose card shows the sources it points to, paging through several.",
  "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/inline-citation.ts",
      "content": "import {\n  type InlineCitationApi,\n  type InlineCitationOpenChangeReason,\n  type InlineCitationOptions,\n  type InlineCitationState,\n  inlineCitationPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton, wrappedChild } from \"@ui-sdk/web\";\nimport type { CarouselController } from \"@ui-sdk/web/carousel\";\nimport type { AttributeProperties, UiChangeEvent, UiElement, UiPartElement } from \"@ui-sdk/web/events\";\nimport \"@ui-sdk/web/avatar\";\nimport \"@ui-sdk/web/carousel\";\n\n// Lucide \"chevron-left\" and \"chevron-right\", see THIRD_PARTY_NOTICES.md.\nconst chevron = (path: string) =>\n  `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" aria-hidden=\"true\"><path d=\"${path}\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>`;\n\nconst DEFAULT_PARTS = `<kind-inline-citation-trigger><button></button></kind-inline-citation-trigger><kind-inline-citation-content hidden></kind-inline-citation-content>`;\n// Previous and Next are hidden from agents, which takes the Carousel out of their snapshot: an agent pages with the\n// citation's own `next` and `previous`.\nconst CAROUSEL_PARTS = `<ui-carousel aria-label=\"Sources\" drag=\"false\"><ui-carousel-viewport><ui-carousel-content></ui-carousel-content></ui-carousel-viewport><span data-inline-citation-controls><ui-carousel-previous><button aria-label=\"Previous source\" data-agent-hidden data-kind-icon-button>${chevron(\"m15 18-6-6 6-6\")}</button></ui-carousel-previous><kind-inline-citation-index></kind-inline-citation-index><ui-carousel-next><button aria-label=\"Next source\" data-agent-hidden data-kind-icon-button>${chevron(\"m9 18 6-6-6-6\")}</button></ui-carousel-next></span></ui-carousel>`;\nconst ITEM_PARTS = `<kind-inline-citation-link><a><kind-inline-citation-icon></kind-inline-citation-icon><kind-inline-citation-source-title></kind-inline-citation-source-title><kind-inline-citation-host></kind-inline-citation-host></a></kind-inline-citation-link><kind-inline-citation-snippet></kind-inline-citation-snippet>`;\n\n// Parts this element wrote into. A part someone wrote into keeps what they wrote, as React's children do. Kept across\n// instances, because UI SDK rebuilds one when the element returns to 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: string | undefined) => {\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(row: Movable, slide: Element, before: Element | null): void {\n  if (row.moveBefore && slide.isConnected) row.moveBefore(slide, before);\n  else row.insertBefore(slide, before);\n}\n\nconst createItem = (doc: Document, id: string) => {\n  const item = doc.createElement(\"kind-inline-citation-item\");\n  item.setAttribute(\"value\", id);\n  item.innerHTML = ITEM_PARTS;\n  return item;\n};\n\n/** Fills each item's empty parts with its source's icon, title, host name and snippet. */\nconst fillItems = (content: Element, { entries }: InlineCitationState) => {\n  for (const item of content.querySelectorAll(\"kind-inline-citation-item\")) {\n    const entry = entries[item.getAttribute(\"value\") ?? \"\"];\n    if (!entry) continue;\n    drawIcon(item.querySelector(\"kind-inline-citation-icon\"), entry.favicon);\n    fill(item.querySelector(\"kind-inline-citation-source-title\"), entry.title);\n    fill(item.querySelector(\"kind-inline-citation-host\"), entry.host);\n    fill(item.querySelector(\"kind-inline-citation-snippet\"), entry.snippet);\n  }\n};\n\n/**\n * Draws one item for a single source, else the Carousel with a slide per source, reusing the slide of a source with\n * the same id so the shown one stays put.\n */\nconst drawContent = (content: Element, ids: string[], controller: CarouselController) => {\n  const doc = content.ownerDocument;\n  if (ids.length > 1) {\n    let carousel = content.querySelector(\"kind-inline-citation-carousel\");\n    if (!carousel) {\n      carousel = doc.createElement(\"kind-inline-citation-carousel\");\n      carousel.innerHTML = CAROUSEL_PARTS;\n      content.replaceChildren(carousel);\n    }\n    const element = carousel.querySelector<HTMLElement & { controller?: CarouselController }>(\"ui-carousel\");\n    if (element && element.controller !== controller) element.controller = controller;\n    const row = carousel.querySelector<Movable>(\"ui-carousel-content\");\n    if (!row) return;\n    const slides = new Map<string, Element>();\n    for (const slide of row.children) slides.set(slide.firstElementChild?.getAttribute(\"value\") ?? \"\", slide);\n    let previous: Element | null = null;\n    for (const id of ids) {\n      let slide = slides.get(id);\n      slides.delete(id);\n      if (!slide) {\n        slide = doc.createElement(\"ui-carousel-item\");\n        slide.append(createItem(doc, id));\n      }\n      const before: Element | null = previous ? previous.nextElementSibling : row.firstElementChild;\n      if (before !== slide) move(row, slide, before);\n      previous = slide;\n    }\n    for (const slide of slides.values()) slide.remove();\n    return;\n  }\n  const [id] = ids;\n  const item = content.querySelector(\":scope > kind-inline-citation-item\");\n  if (id === undefined) content.replaceChildren();\n  else if (item?.getAttribute(\"value\") !== id) content.replaceChildren(createItem(doc, id));\n};\n\n/**\n * `<kind-inline-citation>` fills in its marker and card when it has no children. Set `sources` as a property: the\n * card shows one source, or a Carousel that pages through several.\n */\nexport function defineInlineCitation(): void {\n  if (customElements.get(\"kind-inline-citation\")) return;\n  createParts(inlineCitationPrimitive, {\n    root: {\n      tag: \"kind-inline-citation\",\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) {\n          el.innerHTML = DEFAULT_PARTS;\n          const content = el.querySelector(\"kind-inline-citation-content\");\n          if (content) owned.add(content);\n        }\n        const api = instance.api as InlineCitationApi;\n        const draw = () => {\n          const state = instance.get() as InlineCitationState;\n          fill(el.querySelector(\"kind-inline-citation-trigger > button\"), state.label);\n          const content = el.querySelector(\"kind-inline-citation-content\");\n          if (!content) return;\n          // Opening the card leaves focus on the marker, as React's default does.\n          if (!content.hasAttribute(\"initial-focus\")) content.setAttribute(\"initial-focus\", \"none\");\n          if (owned.has(content)) {\n            const { controller, ids } = api.carouselState(state);\n            drawContent(content, ids, controller);\n          }\n          fillItems(content, state);\n          fill(content.querySelector(\"kind-inline-citation-index\"), api.pageText(state));\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        // Items and parts written after the element connects are filled in too.\n        const arrivals = new MutationObserver((records) => {\n          const changed = records.some((record) =>\n            [...record.addedNodes].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-inline-citation-trigger\", target: wrappedButton, display: \"contents\" },\n    content: \"kind-inline-citation-content\",\n    carousel: \"kind-inline-citation-carousel\",\n    item: \"kind-inline-citation-item\",\n    link: { tag: \"kind-inline-citation-link\", target: wrappedChild(\"a\"), display: \"contents\" },\n    icon: \"kind-inline-citation-icon\",\n    host: \"kind-inline-citation-host\",\n    \"source-title\": \"kind-inline-citation-source-title\",\n    snippet: \"kind-inline-citation-snippet\",\n    index: \"kind-inline-citation-index\",\n  });\n}\n\nexport type KindInlineCitationEvents = {\n  openchange: UiChangeEvent<boolean, InlineCitationOpenChangeReason>;\n};\n\nexport interface KindInlineCitationElement\n  extends UiElement<KindInlineCitationEvents>,\n    AttributeProperties<typeof inlineCitationPrimitive, \"api\" | \"open\" | \"sources\"> {\n  open: boolean;\n  sources?: InlineCitationOptions[\"sources\"];\n  api: Pick<InlineCitationApi, \"isOpen\" | \"setOpen\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-inline-citation\": KindInlineCitationElement;\n    \"kind-inline-citation-trigger\": HTMLElement;\n    \"kind-inline-citation-content\": HTMLElement;\n    \"kind-inline-citation-carousel\": HTMLElement;\n    \"kind-inline-citation-item\": UiPartElement<typeof inlineCitationPrimitive, \"item\", \"value\">;\n    \"kind-inline-citation-link\": HTMLElement;\n    \"kind-inline-citation-icon\": HTMLElement;\n    \"kind-inline-citation-host\": HTMLElement;\n    \"kind-inline-citation-source-title\": HTMLElement;\n    \"kind-inline-citation-snippet\": HTMLElement;\n    \"kind-inline-citation-index\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/inline-citation.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
