{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chain-of-thought-web",
  "title": "Chain of Thought (Web Components)",
  "description": "A summary of the steps an agent took, which opens into each step's details.",
  "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/chain-of-thought.ts",
      "content": "import {\n  type ChainOfThoughtApi,\n  type ChainOfThoughtOptions,\n  type ChainOfThoughtState,\n  chainOfThoughtPrimitive,\n  type StepOpenChangeReason,\n  stepDuration,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement, UiPartElement } from \"@ui-sdk/web/events\";\n\nconst DEFAULT_PARTS = `<kind-chain-of-thought-trigger><button><kind-chain-of-thought-icon></kind-chain-of-thought-icon><kind-chain-of-thought-title></kind-chain-of-thought-title><kind-chain-of-thought-meta></kind-chain-of-thought-meta></button></kind-chain-of-thought-trigger><kind-chain-of-thought-content><kind-chain-of-thought-list></kind-chain-of-thought-list></kind-chain-of-thought-content>`;\nconst ITEM_TRIGGER_PARTS = `<kind-chain-of-thought-item-trigger><button><kind-chain-of-thought-item-icon></kind-chain-of-thought-item-icon><kind-chain-of-thought-item-title></kind-chain-of-thought-item-title><kind-chain-of-thought-item-meta></kind-chain-of-thought-item-meta></button></kind-chain-of-thought-item-trigger>`;\n\n// Parts this element wrote text into, and items it added. A part someone wrote text into keeps it, and an item\n// someone wrote stays, as React's children do. Kept across instances, because UI SDK rebuilds one when the element\n// returns to the page.\nconst owned = new WeakSet<Element>();\n\nconst fill = (part: Element | null, text: string) => {\n  if (!part || !(owned.has(part) || !part.textContent)) return;\n  owned.add(part);\n  if (part.textContent !== text) part.textContent = text;\n};\n\n/** This element's own part of `selector` under `scope`, not one of a chain nested in a step's content. */\nconst own = (scope: Element, selector: string, root: Element) =>\n  [...scope.querySelectorAll(selector)].find((part) => part.closest(\"kind-chain-of-thought\") === root) ??\n  null;\n\n/**\n * Adds an item for each step the list lacks, in order, removes the items it added for steps that left or that a\n * written item replaced, and gives every item without a trigger the default one.\n */\nconst drawItems = (root: Element, list: Element, { steps }: ChainOfThoughtState) => {\n  const items = new Map<string | null, Element>();\n  for (const item of list.querySelectorAll(\":scope > kind-chain-of-thought-item\")) {\n    const value = item.getAttribute(\"value\");\n    const earlier = items.get(value);\n    if (earlier && owned.has(item)) {\n      item.remove();\n      continue;\n    }\n    if (earlier && owned.has(earlier)) earlier.remove();\n    if (!earlier || owned.has(earlier)) items.set(value, item);\n  }\n  const ids = new Set<string | null>(steps.map((step) => step.id));\n  for (const [id, item] of items) if (owned.has(item) && !ids.has(id)) item.remove();\n  let previous: Element | undefined;\n  for (const step of steps) {\n    let item = items.get(step.id);\n    if (!item) {\n      item = document.createElement(\"kind-chain-of-thought-item\");\n      item.setAttribute(\"value\", step.id);\n      owned.add(item);\n      if (previous) previous.after(item);\n      else list.prepend(item);\n    }\n    previous = item;\n    if (!item.querySelector(\":scope > kind-chain-of-thought-item-trigger\"))\n      item.insertAdjacentHTML(\"afterbegin\", ITEM_TRIGGER_PARTS);\n    fill(own(item, \"kind-chain-of-thought-item-title\", root), step.title);\n    fill(own(item, \"kind-chain-of-thought-item-meta\", root), stepDuration(step.durationSeconds));\n  }\n};\n\n/**\n * `<kind-chain-of-thought>` fills in its trigger and content when it has no children. Set `steps` as a property: its\n * list gets an item for each step it lacks, and every item without a trigger gets one with the step's icon, title and\n * duration. Write a `<kind-chain-of-thought-item value=\"…\">` with a `<kind-chain-of-thought-item-content>` to give a\n * step content.\n */\nexport function defineChainOfThought(): void {\n  if (customElements.get(\"kind-chain-of-thought\")) return;\n  createParts(chainOfThoughtPrimitive, {\n    root: {\n      tag: \"kind-chain-of-thought\",\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n        openSteps: {\n          get: (api) => api.getOpenSteps(),\n          set: (api, openSteps) => api.setOpenSteps(openSteps as string[]),\n        },\n      },\n      api: [\n        \"isOpen\",\n        \"setOpen\",\n        \"expand\",\n        \"collapse\",\n        \"toggle\",\n        \"getOpenSteps\",\n        \"setOpenSteps\",\n        \"isStepOpen\",\n        \"setStepOpen\",\n        \"expandStep\",\n      ],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        let drawn: ChainOfThoughtState[\"steps\"] | undefined;\n        const draw = (force = false) => {\n          const state = instance.get() as ChainOfThoughtState;\n          fill(own(el, \"kind-chain-of-thought-title\", el), state.title);\n          fill(own(el, \"kind-chain-of-thought-meta\", el), stepDuration(state.durationSeconds));\n          if (state.steps === drawn && !force) return;\n          drawn = state.steps;\n          const list = own(el, \"kind-chain-of-thought-list\", el);\n          if (list) drawItems(el, list, state);\n        };\n        draw();\n        const unsubscribe = instance.subscribe(() => draw());\n        const arrivals = new MutationObserver((records) => {\n          // What a step's content holds isn't parts.\n          const arrived = records.some(\n            (record) =>\n              !(\n                record.target instanceof Element &&\n                record.target.closest(\"kind-chain-of-thought-item-content\")\n              ) && [...record.addedNodes].some((node) => node instanceof Element),\n          );\n          if (arrived) draw(true);\n        });\n        arrivals.observe(el, { childList: true, subtree: true });\n        return () => {\n          unsubscribe();\n          arrivals.disconnect();\n        };\n      },\n    },\n    trigger: { tag: \"kind-chain-of-thought-trigger\", target: wrappedButton, display: \"contents\" },\n    icon: \"kind-chain-of-thought-icon\",\n    title: \"kind-chain-of-thought-title\",\n    meta: \"kind-chain-of-thought-meta\",\n    content: \"kind-chain-of-thought-content\",\n    list: \"kind-chain-of-thought-list\",\n    item: \"kind-chain-of-thought-item\",\n    \"item-trigger\": { tag: \"kind-chain-of-thought-item-trigger\", target: wrappedButton, display: \"contents\" },\n    \"item-icon\": \"kind-chain-of-thought-item-icon\",\n    \"item-title\": \"kind-chain-of-thought-item-title\",\n    \"item-meta\": \"kind-chain-of-thought-item-meta\",\n    \"item-content\": \"kind-chain-of-thought-item-content\",\n  });\n}\n\nexport type KindChainOfThoughtEvents = {\n  openchange: UiChangeEvent<boolean, StepOpenChangeReason>;\n  openstepschange: UiChangeEvent<string[], StepOpenChangeReason>;\n};\n\nexport interface KindChainOfThoughtElement\n  extends UiElement<KindChainOfThoughtEvents>,\n    AttributeProperties<typeof chainOfThoughtPrimitive, \"api\" | \"open\" | \"openSteps\" | \"steps\"> {\n  open: boolean;\n  openSteps: string[];\n  steps?: ChainOfThoughtOptions[\"steps\"];\n  api: Pick<\n    ChainOfThoughtApi,\n    | \"isOpen\"\n    | \"setOpen\"\n    | \"expand\"\n    | \"collapse\"\n    | \"toggle\"\n    | \"getOpenSteps\"\n    | \"setOpenSteps\"\n    | \"isStepOpen\"\n    | \"setStepOpen\"\n    | \"expandStep\"\n  >;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-chain-of-thought\": KindChainOfThoughtElement;\n    \"kind-chain-of-thought-trigger\": HTMLElement;\n    \"kind-chain-of-thought-icon\": HTMLElement;\n    \"kind-chain-of-thought-title\": HTMLElement;\n    \"kind-chain-of-thought-meta\": HTMLElement;\n    \"kind-chain-of-thought-content\": HTMLElement;\n    \"kind-chain-of-thought-list\": HTMLElement;\n    \"kind-chain-of-thought-item\": UiPartElement<typeof chainOfThoughtPrimitive, \"item\", \"value\">;\n    \"kind-chain-of-thought-item-trigger\": HTMLElement;\n    \"kind-chain-of-thought-item-icon\": HTMLElement;\n    \"kind-chain-of-thought-item-title\": HTMLElement;\n    \"kind-chain-of-thought-item-meta\": HTMLElement;\n    \"kind-chain-of-thought-item-content\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/chain-of-thought.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
