{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tool-web",
  "title": "Tool (Web Components)",
  "description": "A tool call as a step, from its streamed input to its output or error.",
  "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",
    "@kind-ui/code-block-web",
    "@kind-ui/confirmation-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/tool.ts",
      "content": "import {\n  codeSpans,\n  type StepOpenChangeReason,\n  stepDuration,\n  type ToolApi,\n  type ToolOptions,\n  type ToolState,\n  toolPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport { defineCodeBlock, type KindCodeBlockElement } from \"./code-block\";\nimport {\n  APPROVAL_KINDS,\n  type ApprovalAttributes,\n  approvalOptions,\n  defineConfirmation,\n  type KindConfirmationElement,\n  type KindConfirmationEvents,\n  writeApproval,\n} from \"./confirmation\";\nimport { defineMarkdown, type KindMarkdownElement } from \"./markdown\";\n\nconst DEFAULT_PARTS = `<kind-tool-trigger><button><kind-tool-icon></kind-tool-icon><kind-tool-title></kind-tool-title><kind-tool-meta></kind-tool-meta></button></kind-tool-trigger><kind-tool-content><kind-tool-input></kind-tool-input><kind-tool-output></kind-tool-output><kind-tool-error></kind-tool-error></kind-tool-content><kind-tool-approval></kind-tool-approval>`;\nconst INPUT_PARTS = `<div data-tool-heading>Input<span data-tool-badge data-kind-badge>Partial</span><kind-tool-input-copy><button></button></kind-tool-input-copy></div><kind-code-block headerless language=\"json\"></kind-code-block>`;\nconst OUTPUT_PARTS = `<div data-tool-heading>Output<span data-tool-badge data-kind-badge>Preliminary</span><kind-tool-output-copy><button></button></kind-tool-output-copy></div><kind-code-block headerless language=\"json\"></kind-code-block><kind-markdown></kind-markdown>`;\n\n// Parts this element wrote text into, and code blocks it wrote. A part someone wrote into keeps what they wrote, as\n// React's children do. Kept 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 titles = new WeakMap<Element, string>();\n\n// The title, with its backticks as `code` elements.\nconst fillTitle = (part: Element | null, text: string) => {\n  if (!part || !(owned.has(part) || !part.textContent)) return;\n  owned.add(part);\n  if (titles.get(part) === text) return;\n  titles.set(part, text);\n  part.replaceChildren(\n    ...codeSpans(text).map((piece, index) => {\n      if (!(index % 2)) return piece;\n      const code = part.ownerDocument.createElement(\"code\");\n      code.textContent = piece;\n      return code;\n    }),\n  );\n};\n\nconst fillParts = (part: Element | null, html: string) => {\n  if (part && !part.firstElementChild) {\n    part.innerHTML = html;\n    const code = part.querySelector(\":scope > kind-code-block\");\n    if (code) owned.add(code);\n  }\n  return part;\n};\n\n// Names a code block this element wrote, after the tool, as React's default parts do.\nconst label = (code: KindCodeBlockElement | null | undefined, text: string) => {\n  if (code && owned.has(code)) write(code, \"ariaLabel\", text);\n};\n\nconst write = <Target extends object, Key extends keyof Target>(\n  target: Target,\n  key: Key,\n  value: Target[Key],\n) => {\n  if (target[key] !== value) target[key] = value;\n};\n\n/**\n * `<kind-tool>` fills in its trigger and content when it has no children, and an empty `<kind-tool-input>` or\n * `<kind-tool-output>` with its heading, copy button and code block. Set `call` and `heading` as properties. It writes its title\n * and error into those parts while they're empty, and its values into the code blocks and Markdown view. While `call`\n * has an approval, an empty `<kind-tool-approval>` gets a `kind-confirmation`, which offers Deny with the `can-deny`\n * attribute, Always allow with `can-always-allow` and Approve with `can-approve`, which fire `deny`, `alwaysallow` and\n * `approve` on the tool, as Confirmation's do.\n */\nexport function defineTool(): void {\n  if (customElements.get(\"kind-tool\")) return;\n  defineCodeBlock();\n  defineConfirmation();\n  defineMarkdown();\n  createParts(toolPrimitive, {\n    root: {\n      tag: \"kind-tool\",\n      kinds: APPROVAL_KINDS,\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => approvalOptions(el as KindToolElement),\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 api = instance.api as ToolApi;\n        const draw = () => {\n          const state = instance.get() as ToolState;\n          const { highlight } = state;\n          fillTitle(el.querySelector(\"kind-tool-title\"), state.title);\n          fill(el.querySelector(\"kind-tool-meta\"), stepDuration(state.durationSeconds));\n          // Filled only once there's an approval, so a tool without one holds no Confirmation.\n          const approval =\n            state.approval &&\n            fillParts(\n              el.querySelector(\"kind-tool-approval\"),\n              \"<kind-confirmation></kind-confirmation>\",\n            )?.querySelector<KindConfirmationElement>(\":scope > kind-confirmation\");\n          if (approval) writeApproval(approval, api.approvalState(state));\n          fill(el.querySelector(\"kind-tool-error\"), state.errorMessage);\n          const input = fillParts(el.querySelector(\"kind-tool-input\"), INPUT_PARTS);\n          const inputCode = input?.querySelector<KindCodeBlockElement>(\":scope > kind-code-block\");\n          label(inputCode, state.inputLabel);\n          if (inputCode) {\n            write(inputCode, \"code\", state.input);\n            write(inputCode, \"streaming\", state.inputStreaming);\n            write(inputCode, \"highlight\", highlight);\n          }\n          const output = fillParts(el.querySelector(\"kind-tool-output\"), OUTPUT_PARTS);\n          const code = output?.querySelector<KindCodeBlockElement>(\":scope > kind-code-block\");\n          label(code, state.outputLabel);\n          if (code) {\n            write(code, \"hidden\", state.output !== \"code\");\n            if (state.output === \"code\") {\n              write(code, \"code\", state.outputText);\n              write(code, \"streaming\", state.outputPreliminary);\n              write(code, \"highlight\", highlight);\n            }\n          }\n          const markdown = output?.querySelector<KindMarkdownElement>(\":scope > kind-markdown\");\n          if (markdown) {\n            write(markdown, \"hidden\", state.output !== \"markdown\");\n            if (state.output === \"markdown\") {\n              write(markdown, \"source\", state.outputText);\n              write(markdown, \"streaming\", state.outputPreliminary);\n              write(markdown, \"highlight\", highlight);\n            }\n          }\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        const arrivals = new MutationObserver((records) => {\n          // The lines and blocks the code blocks and Markdown view render aren't parts.\n          const arrived = records.some(\n            (record) =>\n              !(\n                record.target instanceof Element && record.target.closest(\"kind-code-block, 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-tool-trigger\", target: wrappedButton, display: \"contents\" },\n    icon: \"kind-tool-icon\",\n    title: \"kind-tool-title\",\n    meta: \"kind-tool-meta\",\n    approval: \"kind-tool-approval\",\n    content: \"kind-tool-content\",\n    input: \"kind-tool-input\",\n    \"input-copy\": { tag: \"kind-tool-input-copy\", target: wrappedButton, display: \"contents\" },\n    output: \"kind-tool-output\",\n    \"output-copy\": { tag: \"kind-tool-output-copy\", target: wrappedButton, display: \"contents\" },\n    error: \"kind-tool-error\",\n  });\n}\n\nexport type KindToolEvents = {\n  openchange: UiChangeEvent<boolean, StepOpenChangeReason>;\n  approve: KindConfirmationEvents[\"approve\"];\n  deny: KindConfirmationEvents[\"deny\"];\n  alwaysallow: KindConfirmationEvents[\"alwaysallow\"];\n};\n\nexport interface KindToolElement\n  extends UiElement<KindToolEvents>,\n    ApprovalAttributes,\n    AttributeProperties<typeof toolPrimitive, \"api\" | \"open\" | \"call\" | \"heading\" | \"error\" | \"highlight\"> {\n  open: boolean;\n  call?: ToolOptions[\"call\"];\n  /** A string or a function of the call's status and input; a property. */\n  heading?: ToolOptions[\"heading\"];\n  error?: ToolOptions[\"error\"];\n  highlight?: ToolOptions[\"highlight\"];\n  api: Pick<ToolApi, \"isOpen\" | \"setOpen\" | \"expand\" | \"collapse\" | \"toggle\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-tool\": KindToolElement;\n    \"kind-tool-trigger\": HTMLElement;\n    \"kind-tool-icon\": HTMLElement;\n    \"kind-tool-title\": HTMLElement;\n    \"kind-tool-meta\": HTMLElement;\n    \"kind-tool-approval\": HTMLElement;\n    \"kind-tool-content\": HTMLElement;\n    \"kind-tool-input\": HTMLElement;\n    \"kind-tool-input-copy\": HTMLElement;\n    \"kind-tool-output\": HTMLElement;\n    \"kind-tool-output-copy\": HTMLElement;\n    \"kind-tool-error\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/tool.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
