{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "plan-web",
  "title": "Plan (Web Components)",
  "description": "The steps an agent plans to take, with a progress bar and an Accept button.",
  "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/plan.ts",
      "content": "import \"@ui-sdk/web/progress\";\nimport {\n  type PlanApi,\n  type PlanOptions,\n  type PlanState,\n  planPrimitive,\n  type StepOpenChangeReason,\n} from \"@kind-ui/agent-ui/core\";\nimport { createParts, type UiActionDetail, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement, UiPartElement } from \"@ui-sdk/web/events\";\n\ntype PlanItem = PlanState[\"items\"][number];\n\nconst DEFAULT_PARTS = `<kind-plan-trigger><button><kind-plan-icon></kind-plan-icon><kind-plan-title></kind-plan-title><kind-plan-meta></kind-plan-meta></button></kind-plan-trigger><kind-plan-content><kind-plan-list></kind-plan-list></kind-plan-content><kind-plan-accept><button><span data-kind-pending-mark data-status-mark data-status=\"running\" aria-hidden=\"true\"></span>Accept plan</button></kind-plan-accept><kind-plan-outcome><kind-plan-outcome-icon></kind-plan-outcome-icon>Accepted</kind-plan-outcome>`;\nconst PROGRESS_PARTS = `<ui-progress><ui-progress-label></ui-progress-label><ui-progress-track><ui-progress-indicator></ui-progress-indicator></ui-progress-track></ui-progress>`;\nconst ITEM_PARTS = `<kind-plan-item-icon></kind-plan-item-icon><kind-plan-item-title></kind-plan-item-title><kind-plan-item-description></kind-plan-item-description>`;\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\ntype Movable = Element & { moveBefore?: (node: Node, child: Node | null) => void };\n\n// `moveBefore` keeps a focused item focused where the browser has it; `insertBefore` blurs it.\nfunction move(list: Movable, item: Element, before: Element | null): void {\n  if (list.moveBefore && item.isConnected) list.moveBefore(item, before);\n  else list.insertBefore(item, before);\n}\n\n/**\n * Adds an item for each one the list lacks, in order, removes the items it added for ones that left or that a\n * written item replaced, and fills in every item's parts.\n */\nconst drawItems = (list: Movable, items: PlanItem[]) => {\n  const children = new Map<string | null, Element>();\n  for (const child of [...list.children]) {\n    const value = child.getAttribute(\"value\");\n    const earlier = children.get(value);\n    if (earlier && owned.has(child)) {\n      child.remove();\n      continue;\n    }\n    if (earlier && owned.has(earlier)) earlier.remove();\n    if (!earlier || owned.has(earlier)) children.set(value, child);\n  }\n  const ids = new Set<string | null>(items.map((item) => item.id));\n  for (const [id, child] of children) if (owned.has(child) && !ids.has(id)) child.remove();\n  let previous: Element | null = null;\n  for (const item of items) {\n    let child = children.get(item.id);\n    if (!child) {\n      child = list.ownerDocument.createElement(\"kind-plan-item\");\n      child.setAttribute(\"value\", item.id);\n      owned.add(child);\n    }\n    const before: Element | null = previous ? previous.nextElementSibling : list.firstElementChild;\n    if (owned.has(child) && before !== child) move(list, child, before);\n    previous = child;\n    if (!child.firstElementChild) child.innerHTML = ITEM_PARTS;\n    fill(child.querySelector(\"kind-plan-item-title\"), item.title);\n    fill(child.querySelector(\"kind-plan-item-description\"), item.description ?? \"\");\n  }\n};\n\n/** Fills an empty progress part with a UI SDK Progress, and keeps the one it wrote current. */\nconst drawProgress = (part: Element | null, { done, total, percent }: PlanState[\"progress\"]) => {\n  if (!part || !(owned.has(part) || !part.firstChild)) return;\n  if (!owned.has(part)) {\n    owned.add(part);\n    part.innerHTML = PROGRESS_PARTS;\n  }\n  const bar = part.querySelector(\"ui-progress\") as (Element & { value: number | null }) | null;\n  if (bar && bar.value !== percent) bar.value = percent;\n  const label = part.querySelector(\"ui-progress-label\");\n  const text = `${done} of ${total} ${total === 1 ? \"step\" : \"steps\"}`;\n  if (label && label.textContent !== text) label.textContent = text;\n};\n\n/**\n * `<kind-plan label=\"…\">` fills in its trigger, content, Accept button and \"Accepted\" when it has no children, and\n * an empty `kind-plan-progress` you write with a UI SDK Progress. Set `items` as a property: its list gets a\n * `kind-plan-item` for each item it lacks, in order, and every item's empty parts get the item's mark, title and\n * description. Write a `<kind-plan-item value=\"…\">` to render an item yourself. The `can-accept` attribute\n * shows Accept, which fires `accept`: call `event.detail.waitUntil(promise)` to keep it pending.\n */\nexport function definePlan(): void {\n  if (customElements.get(\"kind-plan\")) return;\n  createParts(planPrimitive, {\n    root: {\n      tag: \"kind-plan\",\n      kinds: { canAccept: \"boolean\" },\n      // Without the attribute the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => ((el as KindPlanElement).canAccept ? {} : { onAccept: undefined }),\n      defaults: { agentConfirm: true },\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n        // An accepted plan put back in the page stays accepted.\n        accepted: {\n          get: (api) => api.isAccepted(),\n          set: (api, accepted) => api.setAccepted(!!accepted),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\", \"expand\", \"collapse\", \"toggle\", \"getItems\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const draw = () => {\n          const { title, progress, items } = instance.get() as PlanState;\n          fill(el.querySelector(\"kind-plan-title\"), title);\n          fill(el.querySelector(\"kind-plan-meta\"), `${progress.done} of ${progress.total}`);\n          drawProgress(el.querySelector(\"kind-plan-progress\"), progress);\n          const list = el.querySelector(\"kind-plan-list\");\n          if (list) drawItems(list, items);\n        };\n        draw();\n        const unsubscribe = instance.subscribe(draw);\n        // Parts and items written or removed after the element connects redraw it.\n        const arrivals = new MutationObserver((records) => {\n          const changed = records.some((record) =>\n            [...record.addedNodes, ...record.removedNodes].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-plan-trigger\", target: wrappedButton, display: \"contents\" },\n    icon: \"kind-plan-icon\",\n    title: \"kind-plan-title\",\n    meta: \"kind-plan-meta\",\n    content: \"kind-plan-content\",\n    progress: \"kind-plan-progress\",\n    list: \"kind-plan-list\",\n    item: \"kind-plan-item\",\n    \"item-icon\": \"kind-plan-item-icon\",\n    \"item-title\": \"kind-plan-item-title\",\n    \"item-description\": \"kind-plan-item-description\",\n    accept: { tag: \"kind-plan-accept\", target: wrappedButton, display: \"contents\" },\n    outcome: \"kind-plan-outcome\",\n    \"outcome-icon\": \"kind-plan-outcome-icon\",\n  });\n}\n\nexport type KindPlanEvents = {\n  openchange: UiChangeEvent<boolean, StepOpenChangeReason>;\n  accept: CustomEvent<Pick<UiActionDetail, \"waitUntil\">>;\n};\n\nexport interface KindPlanElement\n  extends UiElement<KindPlanEvents>,\n    AttributeProperties<typeof planPrimitive, \"api\" | \"open\" | \"accepted\" | \"items\"> {\n  open: boolean;\n  accepted: boolean;\n  canAccept: boolean;\n  items?: PlanOptions[\"items\"];\n  api: Pick<PlanApi, \"isOpen\" | \"setOpen\" | \"expand\" | \"collapse\" | \"toggle\" | \"getItems\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-plan\": KindPlanElement;\n    \"kind-plan-trigger\": HTMLElement;\n    \"kind-plan-icon\": HTMLElement;\n    \"kind-plan-title\": HTMLElement;\n    \"kind-plan-meta\": HTMLElement;\n    \"kind-plan-content\": HTMLElement;\n    \"kind-plan-progress\": HTMLElement;\n    \"kind-plan-list\": HTMLElement;\n    \"kind-plan-item\": UiPartElement<typeof planPrimitive, \"item\", \"value\">;\n    \"kind-plan-item-icon\": HTMLElement;\n    \"kind-plan-item-title\": HTMLElement;\n    \"kind-plan-item-description\": HTMLElement;\n    \"kind-plan-accept\": HTMLElement;\n    \"kind-plan-outcome\": HTMLElement;\n    \"kind-plan-outcome-icon\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/plan.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
