{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "action-web",
  "title": "Action (Web Components)",
  "description": "An action run on selected text as a step, quoting the text above its streamed result.",
  "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",
    "@kind-ui/markdown-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/action.ts",
      "content": "import { actionPrimitive, type StepApi, type StepOpenChangeReason } from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton, wrappedChild } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport { defineMarkdown, type KindMarkdownElement } from \"./markdown\";\n\nconst DEFAULT_PARTS = `<kind-action-trigger><button><kind-action-icon></kind-action-icon><kind-action-title></kind-action-title></button></kind-action-trigger><kind-action-content><kind-action-quote><blockquote hidden></blockquote></kind-action-quote><kind-action-result hidden><kind-markdown></kind-markdown></kind-action-result></kind-action-content>`;\n\n// Titles and quotes this element filled in. One someone wrote text into keeps it, as React's children do. Kept\n// across instances, because UI SDK rebuilds one when the element 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\nconst fillEmpty = (part: Element | null, html: string) => {\n  if (part && !part.firstElementChild && !part.textContent?.trim()) part.innerHTML = html;\n};\n\n/**\n * `<kind-action label=\"…\" quote=\"…\" result=\"…\" status=\"…\">` fills in its trigger and content when it has no\n * children. It writes its title into an empty `<kind-action-title>`, its `quote` into an empty `<blockquote>` in\n * `<kind-action-quote>`, and its `result` into a `<kind-markdown>` inside `<kind-action-result>`.\n */\nexport function defineAction(): void {\n  if (customElements.get(\"kind-action\")) return;\n  defineMarkdown();\n  createParts(actionPrimitive, {\n    root: {\n      tag: \"kind-action\",\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\", \"expand\", \"collapse\", \"toggle\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const draw = () => {\n          const { title, quote, result, status } = instance.get();\n          // Parts a page wrote empty are filled in, as React's defaults are.\n          fillEmpty(el.querySelector(\"kind-action-quote\"), \"<blockquote hidden></blockquote>\");\n          fillEmpty(el.querySelector(\"kind-action-result\"), \"<kind-markdown></kind-markdown>\");\n          fill(el.querySelector(\"kind-action-title\"), title);\n          fill(el.querySelector(\"kind-action-quote > blockquote\"), quote);\n          const markdown = el.querySelector<KindMarkdownElement>(\n            \":scope > kind-action-content > kind-action-result > kind-markdown\",\n          );\n          if (markdown) {\n            if (markdown.source !== result) markdown.source = result;\n            const streaming = status === \"running\";\n            if (markdown.streaming !== streaming) markdown.streaming = streaming;\n          }\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        const arrivals = new MutationObserver((records) => {\n          // The blocks `kind-markdown` renders for each chunk aren't parts.\n          const arrived = records.some(\n            (record) =>\n              !(record.target instanceof Element && record.target.closest(\"kind-markdown\")) &&\n              [...record.addedNodes].some((node) => node instanceof Element),\n          );\n          if (arrived) draw();\n        });\n        arrivals.observe(el, { childList: true, subtree: true });\n        return () => {\n          unsubscribe();\n          arrivals.disconnect();\n        };\n      },\n    },\n    trigger: { tag: \"kind-action-trigger\", target: wrappedButton, display: \"contents\" },\n    icon: \"kind-action-icon\",\n    title: \"kind-action-title\",\n    content: \"kind-action-content\",\n    quote: { tag: \"kind-action-quote\", target: wrappedChild(\"blockquote\"), display: \"contents\" },\n    result: \"kind-action-result\",\n  });\n}\n\nexport type KindActionEvents = { openchange: UiChangeEvent<boolean, StepOpenChangeReason> };\n\nexport interface KindActionElement\n  extends UiElement<KindActionEvents>,\n    AttributeProperties<typeof actionPrimitive, \"api\" | \"open\"> {\n  open: boolean;\n  api: Pick<StepApi, \"isOpen\" | \"setOpen\" | \"expand\" | \"collapse\" | \"toggle\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-action\": KindActionElement;\n    \"kind-action-trigger\": HTMLElement;\n    \"kind-action-icon\": HTMLElement;\n    \"kind-action-title\": HTMLElement;\n    \"kind-action-content\": HTMLElement;\n    \"kind-action-quote\": HTMLElement;\n    \"kind-action-result\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/action.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
