{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "checkpoint-web",
  "title": "Checkpoint (Web Components)",
  "description": "A marker in the conversation that restores to that point, after a dialog asks what to restore and says what's lost.",
  "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-alert-dialog-web",
    "@kind-ui/internal-radio-web",
    "@kind-ui/internal-tooltip-web"
  ],
  "files": [
    {
      "path": "packages/agent-ui/src/web/checkpoint.ts",
      "content": "import {\n  type CheckpointApi,\n  type CheckpointRestoreDetails,\n  type CheckpointScopeState,\n  type CheckpointState,\n  checkpointPrimitive,\n} from \"@kind-ui/agent-ui/core\";\nimport type { ActionDetails } from \"@ui-sdk/core\";\nimport type { AlertDialogOpenChangeReason } from \"@ui-sdk/core/alert-dialog\";\nimport { createParts, type UiActionDetail, wrappedButton } from \"@ui-sdk/web\";\nimport type { AttributeProperties, UiChangeEvent, UiElement } from \"@ui-sdk/web/events\";\nimport { defineInternalAlertDialog } from \"./internal/alert-dialog\";\nimport { defineInternalRadio } from \"./internal/radio\";\nimport { defineInternalTooltip } from \"./internal/tooltip\";\n\nconst DEFAULT_PARTS = `<kind-checkpoint-label></kind-checkpoint-label><kind-checkpoint-description></kind-checkpoint-description><kind-checkpoint-line></kind-checkpoint-line><kind-checkpoint-restore><kind-internal-alert-dialog><ui-alert-dialog><kind-internal-tooltip><ui-tooltip type=\"label\"><ui-tooltip-trigger><ui-alert-dialog-trigger><button data-checkpoint-trigger data-kind-icon-button></button></ui-alert-dialog-trigger></ui-tooltip-trigger><ui-tooltip-content hidden>Restore checkpoint</ui-tooltip-content></ui-tooltip></kind-internal-tooltip><ui-alert-dialog-backdrop hidden></ui-alert-dialog-backdrop><ui-alert-dialog-content hidden><ui-alert-dialog-title></ui-alert-dialog-title><p data-checkpoint-point></p><kind-checkpoint-scope></kind-checkpoint-scope><ui-alert-dialog-description data-checkpoint-loss><span data-checkpoint-loss-text></span><span data-checkpoint-note></span></ui-alert-dialog-description><div data-checkpoint-answers><kind-checkpoint-compare><button><span data-checkpoint-compare-icon aria-hidden=\"true\"></span>Compare</button></kind-checkpoint-compare><kind-checkpoint-error>Couldn't restore. Try again.</kind-checkpoint-error><kind-checkpoint-cancel><button>Cancel</button></kind-checkpoint-cancel><kind-checkpoint-confirm><button><span data-kind-pending-mark=\"\" data-status-mark=\"\" data-status=\"running\" aria-hidden=\"true\"></span>Restore</button></kind-checkpoint-confirm></div></ui-alert-dialog-content></ui-alert-dialog></kind-internal-alert-dialog></kind-checkpoint-restore><kind-checkpoint-restored><span aria-hidden=\"true\" data-status-mark data-status=\"complete\"></span><span data-checkpoint-outcome></span><span data-checkpoint-outcome-meta></span></kind-checkpoint-restored>`;\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\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 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\ntype DialogElement = Element & { controller?: unknown; dismissible?: boolean };\ntype GroupElement = HTMLElement & { controller?: unknown; defaultValue?: string | null };\n\n// The scope's label and radios, built from its data and before the group connects, since a Radio group reads\n// `defaultValue` as it connects. Drawn again only for a new list of scopes.\nconst drawn = new WeakMap<Element, CheckpointScopeState[\"options\"]>();\nconst drawScope = (part: Element | null, scope: CheckpointScopeState) => {\n  if (!part || drawn.get(part) === scope.options || !(drawn.has(part) || !part.firstElementChild)) return;\n  drawn.set(part, scope.options);\n  const document = part.ownerDocument;\n  const template = document.createElement(\"template\");\n  template.innerHTML = '<span aria-hidden=\"true\" data-checkpoint-scope-label></span>';\n  const label = template.content.firstElementChild as Element;\n  label.textContent = scope.label;\n  const group = document.createElement(\"ui-radio-group\") as GroupElement;\n  group.controller = scope.controller;\n  group.defaultValue = scope.defaultValue;\n  group.ariaLabel = scope.label;\n  group.setAttribute(\"data-agent-hidden\", \"\");\n  for (const option of scope.options) {\n    const row = document.createElement(\"label\");\n    row.setAttribute(\"data-checkpoint-option\", \"\");\n    const radio = document.createElement(\"ui-radio\");\n    radio.setAttribute(\"value\", option.value);\n    radio.append(document.createElement(\"ui-radio-indicator\"));\n    row.append(radio, option.label);\n    group.append(row);\n  }\n  const wrapper = document.createElement(\"kind-internal-radio\");\n  wrapper.append(group);\n  part.replaceChildren(label, wrapper);\n};\ntype ContentElement = Element & { initialFocus?: string; ariaBusy: string | null };\n\n/**\n * `<kind-checkpoint label=\"Checkpoint\" description=\"Before editing auth.ts\">` fills in its parts when it has no\n * children. The `can-restore` attribute shows Restore, which opens a dialog; the dialog's Restore fires `restore`\n * with the chosen `scope`: call `event.detail.waitUntil(promise)` to keep the dialog open until it settles.\n * `can-compare` shows Compare in the dialog, which fires `compare`. `scopes`, `changes` and the counts are properties.\n */\nexport function defineCheckpoint(): void {\n  if (customElements.get(\"kind-checkpoint\")) return;\n  defineInternalAlertDialog();\n  defineInternalTooltip();\n  defineInternalRadio();\n  createParts(checkpointPrimitive, {\n    root: {\n      tag: \"kind-checkpoint\",\n      kinds: { canRestore: \"boolean\", canCompare: \"boolean\" },\n      defaults: { agentConfirm: true },\n      // Without the attribute, the callback is left out; with it, `@ui-sdk/web` fills it in with an event.\n      options: (el) => {\n        const { canRestore, canCompare } = el as KindCheckpointElement;\n        return {\n          ...(canRestore ? {} : { onRestore: undefined }),\n          ...(canCompare ? {} : { onCompare: undefined }),\n        };\n      },\n      state: {\n        open: {\n          get: (api) => api.isOpen(),\n          set: (api, open) => api.setOpen(!!open),\n        },\n      },\n      api: [\"isOpen\", \"setOpen\"],\n      render: (el, instance) => {\n        if (!el.firstElementChild) el.innerHTML = DEFAULT_PARTS;\n        const api = instance.api as CheckpointApi;\n        const draw = () => {\n          const state = instance.get() as CheckpointState;\n          fill(el.querySelector(\"kind-checkpoint-label\"), state.label);\n          fill(el.querySelector(\"kind-checkpoint-description\"), state.description ?? \"\");\n          const { controller, dismissible, busy, initialFocus, title, point, description, note } =\n            api.dialogState(state);\n          const dialog = el.querySelector<DialogElement>(\"kind-checkpoint-restore ui-alert-dialog\");\n          write(dialog, \"controller\", controller);\n          write(dialog, \"dismissible\", dismissible);\n          const content = el.querySelector<ContentElement>(\"ui-alert-dialog-content\");\n          write(content, \"initialFocus\", initialFocus);\n          write(content, \"ariaBusy\", busy ? \"true\" : null);\n          fill(el.querySelector(\"ui-alert-dialog-title\"), title);\n          fill(el.querySelector(\"[data-checkpoint-point]\"), point);\n          fill(el.querySelector(\"[data-checkpoint-loss-text]\"), description);\n          fill(el.querySelector(\"[data-checkpoint-note]\"), note);\n          if (state.scopes.length > 1)\n            drawScope(el.querySelector(\"kind-checkpoint-scope\"), api.scopeState(state));\n          fill(el.querySelector(\"[data-checkpoint-outcome]\"), state.outcome);\n          fill(el.querySelector(\"[data-checkpoint-outcome-meta]\"), state.outcomeMeta);\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    label: \"kind-checkpoint-label\",\n    description: \"kind-checkpoint-description\",\n    line: \"kind-checkpoint-line\",\n    restore: { tag: \"kind-checkpoint-restore\", display: \"contents\" },\n    restored: \"kind-checkpoint-restored\",\n    scope: \"kind-checkpoint-scope\",\n    compare: { tag: \"kind-checkpoint-compare\", target: wrappedButton, display: \"contents\" },\n    error: \"kind-checkpoint-error\",\n    cancel: { tag: \"kind-checkpoint-cancel\", target: wrappedButton, display: \"contents\" },\n    confirm: { tag: \"kind-checkpoint-confirm\", target: wrappedButton, display: \"contents\" },\n  });\n}\n\nexport type KindCheckpointEvents = {\n  openchange: UiChangeEvent<boolean, AlertDialogOpenChangeReason>;\n  restore: CustomEvent<{ value: CheckpointRestoreDetails } & Pick<UiActionDetail, \"waitUntil\">>;\n  compare: CustomEvent<{ value: ActionDetails } & Pick<UiActionDetail, \"waitUntil\">>;\n};\n\nexport interface KindCheckpointElement\n  extends UiElement<KindCheckpointEvents>,\n    AttributeProperties<typeof checkpointPrimitive, \"api\" | \"open\"> {\n  canRestore: boolean;\n  canCompare: boolean;\n  open: boolean;\n  api: Pick<CheckpointApi, \"isOpen\" | \"setOpen\">;\n}\n\ndeclare global {\n  interface HTMLElementTagNameMap {\n    \"kind-checkpoint\": KindCheckpointElement;\n    \"kind-checkpoint-label\": HTMLElement;\n    \"kind-checkpoint-description\": HTMLElement;\n    \"kind-checkpoint-line\": HTMLElement;\n    \"kind-checkpoint-restore\": HTMLElement;\n    \"kind-checkpoint-restored\": HTMLElement;\n    \"kind-checkpoint-scope\": HTMLElement;\n    \"kind-checkpoint-compare\": HTMLElement;\n    \"kind-checkpoint-error\": HTMLElement;\n    \"kind-checkpoint-cancel\": HTMLElement;\n    \"kind-checkpoint-confirm\": HTMLElement;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/web/checkpoint.ts"
    }
  ],
  "meta": {
    "layer": "web"
  },
  "type": "registry:ui"
}
