{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "confirmation-web",
  "title": "Confirmation (Web Components)",
  "description": "Approve or deny an agent's request, with an optional reason, then the recorded outcome.",
  "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/internal-textarea-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/confirmation.ts",
      "content": "import {\n  type ApprovalPartState,\n  type ConfirmationApi,\n  type ConfirmationCallback,\n  type ConfirmationDecision,\n  type ConfirmationOptions,\n  type ConfirmationState,\n  codeSpans,\n  confirmationMeta,\n  confirmationPrimitive,\n  WORDS,\n} from \"@kind-ui/agent-ui/core\";\nimport type { ActionDetails } from \"@ui-sdk/core\";\nimport { createParts, type UiActionDetail, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiElement } from \"@ui-sdk/web/events\";\nimport { defineInternalTextarea } from \"./internal/textarea\";\n\nconst PENDING_MARK = `<span data-kind-pending-mark=\"\" data-status-mark=\"\" data-status=\"running\" aria-hidden=\"true\"></span>`;\nconst DEFAULT_PARTS = `<kind-confirmation-title></kind-confirmation-title><kind-confirmation-meta></kind-confirmation-meta><kind-confirmation-risk><kind-confirmation-risk-icon></kind-confirmation-risk-icon><span data-confirmation-risk-text></span></kind-confirmation-risk><kind-confirmation-reason><kind-internal-textarea><ui-textarea><textarea aria-label=\"Reason for denying\" placeholder=\"Add a reason (optional)\" rows=\"2\"></textarea></ui-textarea></kind-internal-textarea></kind-confirmation-reason><kind-confirmation-choices><kind-confirmation-cancel><button>Cancel</button></kind-confirmation-cancel><kind-confirmation-deny><button>${PENDING_MARK}<span data-kind-label></span></button></kind-confirmation-deny><kind-confirmation-always><button>${PENDING_MARK}<span data-kind-label></span></button></kind-confirmation-always><kind-confirmation-approve><button>${PENDING_MARK}<span data-kind-label></span></button></kind-confirmation-approve><kind-confirmation-error>${WORDS.notSentRetry}</kind-confirmation-error></kind-confirmation-choices><kind-confirmation-detail></kind-confirmation-detail><kind-confirmation-outcome><kind-confirmation-outcome-icon></kind-confirmation-outcome-icon><span data-confirmation-label></span> <span data-confirmation-reason></span></kind-confirmation-outcome>`;\n\n// Parts this element wrote text into. A part someone wrote text into keeps it, 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\n// The text each part was last filled with, backticks included.\nconst filled = new WeakMap<Element, string>();\n\n// Writes text with its backticks as `code` elements.\nconst fill = (part: Element | null | undefined, text: string) => {\n  if (!part || !(owned.has(part) || !part.textContent)) return;\n  owned.add(part);\n  if (filled.get(part) === text) return;\n  filled.set(part, text);\n  part.replaceChildren(\n    ...codeSpans(text).map((piece, index) => {\n      if (!(index % 2)) return piece;\n      const code = document.createElement(\"code\");\n      code.textContent = piece;\n      return code;\n    }),\n  );\n};\n\n// A button the host wrote takes its label as its text.\nconst label = (button: Element | null) => button?.querySelector(\"[data-kind-label]\") ?? button;\n\nconst write = <Target extends object, Key extends keyof Target>(\n  target: Target | null,\n  key: Key,\n  value: Target[Key],\n) => {\n  if (target && target[key] !== value) target[key] = value;\n};\n\n/** The `can-*` attributes that offer an approval's choices, as Confirmation's. */\nexport const APPROVAL_KINDS = {\n  canApprove: \"boolean\",\n  canDeny: \"boolean\",\n  canAlwaysAllow: \"boolean\",\n} as const;\n\nexport type ApprovalAttributes = { canApprove: boolean; canDeny: boolean; canAlwaysAllow: boolean };\n\n/** Leaves out each approval callback whose attribute is off; with it, `@ui-sdk/web` fills it in with an event. */\nexport const approvalOptions = ({ canApprove, canDeny, canAlwaysAllow }: ApprovalAttributes) => ({\n  ...(!canApprove && { onApprove: undefined }),\n  ...(!canDeny && { onDeny: undefined }),\n  ...(!canAlwaysAllow && { onAlwaysAllow: undefined }),\n});\n\n/** Writes an approval part's props onto the `kind-confirmation` inside it, its callbacks as the handlers. */\nexport const writeApproval = (\n  confirmation: KindConfirmationElement,\n  { onApprove, onDeny, onAlwaysAllow, ...props }: ApprovalPartState,\n): void => {\n  const target = confirmation as unknown as Record<string, unknown>;\n  for (const [key, value] of Object.entries({\n    ...props,\n    approveHandler: onApprove,\n    denyHandler: onDeny,\n    alwaysAllowHandler: onAlwaysAllow,\n  }))\n    if (target[key] !== value) target[key] = value;\n};\n\n/**\n * `<kind-confirmation label=\"Allow `search_docs`?\">` fills in its title, meta, risk line, reason box, choices, detail\n * and outcome when it has no children. Set `approval` as a property. The `can-deny`, `can-always-allow` and\n * `can-approve` attributes show Deny, Always allow and Approve, which fire `deny`, `alwaysallow` and `approve` with\n * the decision as `detail.value`: call `event.detail.waitUntil(promise)` to keep the choices disabled until it\n * settles.\n */\nexport function defineConfirmation(): void {\n  if (customElements.get(\"kind-confirmation\")) return;\n  defineInternalTextarea();\n  createParts(confirmationPrimitive, {\n    root: {\n      tag: \"kind-confirmation\",\n      kinds: APPROVAL_KINDS,\n      // The handlers of a Tool, which offer the choices without the attributes.\n      properties: [\"approveHandler\", \"denyHandler\", \"alwaysAllowHandler\"],\n      // Without either, the callback is left out; with only the attribute, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const confirmation = el as KindConfirmationElement;\n        const { approveHandler, denyHandler, alwaysAllowHandler } = confirmation;\n        return {\n          ...approvalOptions(confirmation),\n          ...(approveHandler && { onApprove: approveHandler }),\n          ...(denyHandler && { onDeny: denyHandler }),\n          ...(alwaysAllowHandler && { onAlwaysAllow: alwaysAllowHandler }),\n        };\n      },\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const api = instance.api as ConfirmationApi;\n        const draw = () => {\n          const state = instance.get() as ConfirmationState;\n          fill(el.querySelector(\"kind-confirmation-title\"), state.title ?? \"\");\n          // Reads the live `deadline`, as React's meta part does.\n          fill(el.querySelector(\"kind-confirmation-meta\"), confirmationMeta(state));\n          fill(\n            el.querySelector(\"kind-confirmation-risk [data-confirmation-risk-text]\"),\n            state.riskText ?? \"\",\n          );\n          fill(label(el.querySelector(\"kind-confirmation-approve button\")), state.approveLabel);\n          fill(label(el.querySelector(\"kind-confirmation-always button\")), state.alwaysLabel);\n          fill(label(el.querySelector(\"kind-confirmation-deny button\")), state.denyLabel);\n          fill(el.querySelector(\"kind-confirmation-detail\"), state.alwaysDetail ?? \"\");\n          fill(el.querySelector(\"kind-confirmation-outcome [data-confirmation-label]\"), state.outcome ?? \"\");\n          fill(\n            el.querySelector(\"kind-confirmation-outcome [data-confirmation-reason]\"),\n            state.outcomeReason ?? \"\",\n          );\n          const { controller, readOnly } = api.reasonState(state);\n          write(\n            el.querySelector<Element & { controller?: unknown }>(\"kind-confirmation-reason ui-textarea\"),\n            \"controller\",\n            controller,\n          );\n          write(\n            el.querySelector<HTMLTextAreaElement>(\"kind-confirmation-reason textarea\"),\n            \"readOnly\",\n            readOnly,\n          );\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        const arrivals = new MutationObserver((records) => {\n          if (records.some((record) => [...record.addedNodes].some((node) => node instanceof Element)))\n            draw();\n        });\n        arrivals.observe(el, { childList: true, subtree: true });\n        return () => {\n          unsubscribe();\n          arrivals.disconnect();\n        };\n      },\n    },\n    title: \"kind-confirmation-title\",\n    reason: \"kind-confirmation-reason\",\n    choices: \"kind-confirmation-choices\",\n    meta: \"kind-confirmation-meta\",\n    risk: \"kind-confirmation-risk\",\n    \"risk-icon\": \"kind-confirmation-risk-icon\",\n    approve: { tag: \"kind-confirmation-approve\", target: wrappedButton, display: \"contents\" },\n    always: { tag: \"kind-confirmation-always\", target: wrappedButton, display: \"contents\" },\n    deny: { tag: \"kind-confirmation-deny\", target: wrappedButton, display: \"contents\" },\n    cancel: { tag: \"kind-confirmation-cancel\", target: wrappedButton, display: \"contents\" },\n    error: \"kind-confirmation-error\",\n    detail: \"kind-confirmation-detail\",\n    outcome: \"kind-confirmation-outcome\",\n    \"outcome-icon\": \"kind-confirmation-outcome-icon\",\n  });\n}\n\ntype DecisionEvent = CustomEvent<\n  { value: ConfirmationDecision } & ActionDetails & Pick<UiActionDetail, \"waitUntil\">\n>;\n\nexport type KindConfirmationEvents = {\n  approve: DecisionEvent;\n  deny: DecisionEvent;\n  alwaysallow: DecisionEvent;\n};\n\nexport interface KindConfirmationElement\n  extends UiElement<KindConfirmationEvents>,\n    AttributeProperties<typeof confirmationPrimitive, \"approval\" | \"focusFallback\"> {\n  canApprove: boolean;\n  canDeny: boolean;\n  canAlwaysAllow: boolean;\n  approval?: ConfirmationOptions[\"approval\"];\n  focusFallback?: ConfirmationOptions[\"focusFallback\"];\n  approveHandler?: ConfirmationCallback;\n  denyHandler?: ConfirmationCallback;\n  alwaysAllowHandler?: ConfirmationCallback;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-confirmation\": KindConfirmationElement;\n    \"kind-confirmation-title\": HTMLElement;\n    \"kind-confirmation-meta\": HTMLElement;\n    \"kind-confirmation-risk\": HTMLElement;\n    \"kind-confirmation-risk-icon\": HTMLElement;\n    \"kind-confirmation-reason\": HTMLElement;\n    \"kind-confirmation-choices\": HTMLElement;\n    \"kind-confirmation-approve\": HTMLElement;\n    \"kind-confirmation-always\": HTMLElement;\n    \"kind-confirmation-deny\": HTMLElement;\n    \"kind-confirmation-cancel\": HTMLElement;\n    \"kind-confirmation-error\": HTMLElement;\n    \"kind-confirmation-detail\": HTMLElement;\n    \"kind-confirmation-outcome\": HTMLElement;\n    \"kind-confirmation-outcome-icon\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/confirmation.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
