@kitn.ai/ui 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (366) hide show
  1. package/README.md +9 -0
  2. package/bin/mcp.js +22 -0
  3. package/dist/bash-InADTalH.js +6 -1
  4. package/dist/components/artifact.d.ts +58 -0
  5. package/dist/components/attachments.d.ts +67 -0
  6. package/dist/components/card-fallback.d.ts +10 -0
  7. package/dist/components/card-renderer.d.ts +13 -0
  8. package/dist/components/card.d.ts +28 -0
  9. package/dist/components/chain-of-thought.d.ts +29 -0
  10. package/dist/components/chat-container.d.ts +19 -0
  11. package/dist/components/chat-scope-picker.d.ts +9 -0
  12. package/dist/components/chat-thread.d.ts +93 -0
  13. package/dist/components/checkpoint.d.ts +20 -0
  14. package/dist/components/choice-card.d.ts +77 -0
  15. package/dist/components/code-block.d.ts +16 -0
  16. package/dist/components/confirm-card.d.ts +57 -0
  17. package/dist/components/context.d.ts +65 -0
  18. package/dist/components/conversation-item.d.ts +8 -0
  19. package/dist/components/conversation-list.d.ts +11 -0
  20. package/dist/components/embed.d.ts +21 -0
  21. package/dist/components/empty.d.ts +43 -0
  22. package/dist/components/feedback-bar.d.ts +45 -0
  23. package/dist/components/file-tree.d.ts +57 -0
  24. package/dist/components/file-upload.d.ts +16 -0
  25. package/dist/components/form-widgets.d.ts +31 -0
  26. package/dist/components/form.d.ts +103 -0
  27. package/dist/components/image.d.ts +12 -0
  28. package/dist/components/link-preview.d.ts +23 -0
  29. package/dist/components/loader.d.ts +60 -0
  30. package/dist/components/markdown.d.ts +8 -0
  31. package/dist/components/message-skills.d.ts +15 -0
  32. package/dist/components/message.d.ts +54 -0
  33. package/dist/components/model-switcher.d.ts +8 -0
  34. package/dist/components/prompt-input.d.ts +38 -0
  35. package/dist/components/prompt-suggestion.d.ts +13 -0
  36. package/dist/components/reasoning.d.ts +20 -0
  37. package/dist/components/response-stream.d.ts +14 -0
  38. package/dist/components/scroll-button.d.ts +7 -0
  39. package/dist/components/slash-command.d.ts +15 -0
  40. package/dist/components/source.d.ts +23 -0
  41. package/dist/components/tasks-card.d.ts +71 -0
  42. package/dist/components/text-shimmer.d.ts +9 -0
  43. package/dist/components/thinking-bar.d.ts +10 -0
  44. package/dist/components/tool.d.ts +16 -0
  45. package/dist/components/use-card-resolution.d.ts +12 -0
  46. package/dist/components/voice-input.d.ts +7 -0
  47. package/dist/core-AYMC6_lb.js +5874 -12
  48. package/dist/css-M7EaDHN_.js +6 -1
  49. package/dist/elements/artifact.d.ts +1 -0
  50. package/dist/elements/attachments.d.ts +1 -0
  51. package/dist/elements/card.d.ts +1 -0
  52. package/dist/elements/cards.d.ts +0 -0
  53. package/dist/elements/chain-of-thought.d.ts +13 -0
  54. package/dist/elements/chat-scope-picker.d.ts +1 -0
  55. package/dist/elements/chat-types.d.ts +41 -0
  56. package/dist/elements/chat-workspace.d.ts +1 -0
  57. package/dist/elements/chat.d.ts +1 -0
  58. package/dist/elements/checkpoint.d.ts +1 -0
  59. package/dist/elements/choice.d.ts +1 -0
  60. package/dist/elements/code-block.d.ts +1 -0
  61. package/dist/elements/confirm-card.d.ts +1 -0
  62. package/dist/elements/context-meter.d.ts +1 -0
  63. package/dist/elements/conversation-list.d.ts +11 -0
  64. package/dist/elements/css.d.ts +1 -0
  65. package/dist/elements/default-input.d.ts +40 -0
  66. package/dist/elements/define.d.ts +42 -0
  67. package/dist/elements/embed.d.ts +1 -0
  68. package/dist/elements/empty.d.ts +1 -0
  69. package/dist/elements/feedback-bar.d.ts +1 -0
  70. package/dist/elements/file-tree.d.ts +1 -0
  71. package/dist/elements/file-upload.d.ts +1 -0
  72. package/dist/elements/form.d.ts +1 -0
  73. package/dist/elements/image.d.ts +1 -0
  74. package/dist/elements/link-preview.d.ts +1 -0
  75. package/dist/elements/loader.d.ts +1 -0
  76. package/dist/elements/markdown.d.ts +1 -0
  77. package/dist/elements/message-skills.d.ts +17 -0
  78. package/dist/elements/message.d.ts +1 -0
  79. package/dist/elements/model-switcher.d.ts +11 -0
  80. package/dist/elements/popover.d.ts +1 -0
  81. package/dist/elements/prompt-input.d.ts +9 -0
  82. package/dist/elements/prompt-suggestions.d.ts +7 -0
  83. package/dist/elements/reasoning.d.ts +1 -0
  84. package/dist/elements/register-impl.d.ts +0 -0
  85. package/dist/elements/register.d.ts +3 -0
  86. package/dist/elements/remote.d.ts +1 -0
  87. package/dist/elements/resizable.d.ts +17 -0
  88. package/dist/elements/response-stream.d.ts +1 -0
  89. package/dist/elements/scroll-button.d.ts +1 -0
  90. package/dist/elements/source.d.ts +18 -0
  91. package/dist/elements/switch.d.ts +1 -0
  92. package/dist/elements/tasks.d.ts +1 -0
  93. package/dist/elements/text-shimmer.d.ts +1 -0
  94. package/dist/elements/thinking-bar.d.ts +1 -0
  95. package/dist/elements/tool.d.ts +1 -0
  96. package/dist/elements/voice-input.d.ts +1 -0
  97. package/dist/elements.d.ts +732 -0
  98. package/dist/engine-javascript-vq0WuIJl.js +2516 -14
  99. package/dist/github-dark-dimmed-DUshB20C.js +4 -1
  100. package/dist/github-light-JYsPkUQd.js +4 -1
  101. package/dist/html-CPZ3oZQ7.js +10 -1
  102. package/dist/index.d.ts +112 -2
  103. package/dist/index.js +13551 -0
  104. package/dist/javascript-C25yR2R2.js +6 -1
  105. package/dist/json-DxJze_jm.js +6 -1
  106. package/dist/kitn-chat.es.js +1 -77
  107. package/dist/llms/llms-full.txt +1 -1
  108. package/dist/llms/llms.txt +1 -1
  109. package/dist/mcp.es.js +2140 -0
  110. package/dist/primitives/card-contract.d.ts +103 -0
  111. package/dist/primitives/card-host.d.ts +12 -0
  112. package/dist/primitives/card-registry.d.ts +17 -0
  113. package/dist/primitives/card-resolution.d.ts +8 -0
  114. package/dist/primitives/card-routing.d.ts +12 -0
  115. package/dist/primitives/card-validate.d.ts +25 -0
  116. package/dist/primitives/chat-config.d.ts +31 -0
  117. package/dist/primitives/controllable.d.ts +13 -0
  118. package/dist/primitives/embed-providers.d.ts +65 -0
  119. package/dist/primitives/highlighter.d.ts +23 -0
  120. package/dist/primitives/link-preview.d.ts +47 -0
  121. package/dist/primitives/pdf-preview.d.ts +50 -0
  122. package/dist/primitives/use-auto-resize.d.ts +8 -0
  123. package/dist/primitives/use-resize-observer.d.ts +1 -0
  124. package/dist/primitives/use-stick-to-bottom.d.ts +5 -0
  125. package/dist/primitives/use-text-stream.d.ts +19 -0
  126. package/dist/primitives/use-voice-recorder.d.ts +9 -0
  127. package/dist/react/index.d.ts +1118 -0
  128. package/dist/react/runtime.d.ts +15 -0
  129. package/dist/react.js +265 -0
  130. package/dist/register-impl-wCsFPoh9.js +77 -0
  131. package/dist/register-ujhr1nBS.js +1 -0
  132. package/dist/remote/host-embed.d.ts +33 -0
  133. package/dist/remote/index.d.ts +2 -0
  134. package/dist/remote/origin.d.ts +5 -0
  135. package/dist/remote/provider-runtime.d.ts +27 -0
  136. package/dist/remote/provider.d.ts +2 -0
  137. package/dist/remote/validate.d.ts +4 -0
  138. package/dist/remote/version.d.ts +4 -0
  139. package/dist/remote/wire.d.ts +53 -0
  140. package/dist/svelte--5p79yCD.js +15 -1
  141. package/dist/tsx-B8rCNbgL.js +6 -1
  142. package/dist/types.d.ts +42 -0
  143. package/dist/typescript-RycA9KXf.js +6 -1
  144. package/dist/ui/action-icons.d.ts +11 -0
  145. package/dist/ui/avatar.d.ts +8 -0
  146. package/dist/ui/badge.d.ts +9 -0
  147. package/dist/ui/button.d.ts +10 -0
  148. package/dist/ui/collapsible.d.ts +27 -0
  149. package/dist/ui/dropdown.d.ts +20 -0
  150. package/dist/ui/hover-card.d.ts +28 -0
  151. package/dist/ui/overlay.d.ts +67 -0
  152. package/dist/ui/popover.d.ts +32 -0
  153. package/dist/ui/resizable.d.ts +76 -0
  154. package/dist/ui/scroll-area.d.ts +5 -0
  155. package/dist/ui/separator.d.ts +5 -0
  156. package/dist/ui/skeleton.d.ts +5 -0
  157. package/dist/ui/switch.d.ts +19 -0
  158. package/dist/ui/textarea.d.ts +6 -0
  159. package/dist/ui/tooltip.d.ts +8 -0
  160. package/dist/utils/cn.d.ts +2 -0
  161. package/dist/vue-BmIZj4XD.js +33 -1
  162. package/frameworks/react/index.tsx +4 -0
  163. package/frameworks/react/runtime.tsx +16 -2
  164. package/llms-full.txt +1 -1
  165. package/llms.txt +1 -1
  166. package/package.json +29 -11
  167. package/src/agent-tooling/archetypes.ts +46 -0
  168. package/src/agent-tooling/integrations/cloudflare.ts +88 -0
  169. package/src/agent-tooling/integrations/langgraph.ts +60 -0
  170. package/src/agent-tooling/integrations/mastra.ts +27 -0
  171. package/src/agent-tooling/integrations/mock.ts +34 -0
  172. package/src/agent-tooling/integrations/ollama.ts +47 -0
  173. package/src/agent-tooling/integrations/openrouter.ts +26 -0
  174. package/src/agent-tooling/integrations/pi.ts +40 -0
  175. package/src/agent-tooling/integrations/pydantic-ai.ts +52 -0
  176. package/src/agent-tooling/integrations/vercel-ai-sdk.ts +44 -0
  177. package/src/agent-tooling/mcp/manifest.ts +128 -0
  178. package/src/agent-tooling/mcp/server.ts +59 -0
  179. package/src/agent-tooling/mcp/stdio.ts +18 -0
  180. package/src/agent-tooling/mcp/tools/debug.ts +305 -0
  181. package/src/agent-tooling/mcp/tools/reference.ts +132 -0
  182. package/src/agent-tooling/mcp/tools/scaffold.ts +1627 -0
  183. package/src/agent-tooling/mcp/tools/theme.ts +340 -0
  184. package/src/agent-tooling/mcp/tools/types.ts +14 -0
  185. package/src/agent-tooling/registry.ts +41 -0
  186. package/src/agent-tooling/types.ts +30 -0
  187. package/src/elements/compiled.css +1 -1
  188. package/src/elements/define.tsx +7 -1
  189. package/src/elements/element-types.d.ts +2 -1
  190. package/src/elements/register-impl.ts +55 -0
  191. package/src/elements/register.ts +17 -51
  192. package/theme.css +4 -2
  193. package/src/components/artifact.stories.tsx +0 -138
  194. package/src/components/attachments.stories.tsx +0 -303
  195. package/src/components/attachments.test.tsx +0 -64
  196. package/src/components/chain-of-thought.stories.tsx +0 -211
  197. package/src/components/chat-container.stories.tsx +0 -187
  198. package/src/components/chat-thread.test.tsx +0 -58
  199. package/src/components/checkpoint.stories.tsx +0 -102
  200. package/src/components/code-block.stories.tsx +0 -150
  201. package/src/components/confirm-card.stories.tsx +0 -74
  202. package/src/components/context.stories.tsx +0 -229
  203. package/src/components/context.test.tsx +0 -133
  204. package/src/components/conversation-item.stories.tsx +0 -125
  205. package/src/components/conversation-list.stories.tsx +0 -133
  206. package/src/components/default-input.test.tsx +0 -111
  207. package/src/components/empty.stories.tsx +0 -434
  208. package/src/components/feedback-bar.stories.tsx +0 -138
  209. package/src/components/feedback-bar.test.tsx +0 -103
  210. package/src/components/file-tree.stories.tsx +0 -73
  211. package/src/components/file-upload.stories.tsx +0 -156
  212. package/src/components/image.stories.tsx +0 -89
  213. package/src/components/loader.stories.tsx +0 -181
  214. package/src/components/markdown.stories.tsx +0 -180
  215. package/src/components/message-action-bar.test.tsx +0 -128
  216. package/src/components/message-narrow.stories.tsx +0 -329
  217. package/src/components/message-skills.stories.tsx +0 -211
  218. package/src/components/message.stories.tsx +0 -281
  219. package/src/components/model-switcher.stories.tsx +0 -123
  220. package/src/components/model-switcher.test.tsx +0 -65
  221. package/src/components/prompt-input.stories.tsx +0 -222
  222. package/src/components/prompt-suggestion.stories.tsx +0 -142
  223. package/src/components/reasoning.stories.tsx +0 -140
  224. package/src/components/resizable.test.tsx +0 -226
  225. package/src/components/scroll-button.stories.tsx +0 -100
  226. package/src/components/slash-command.stories.tsx +0 -163
  227. package/src/components/source-list.test.tsx +0 -152
  228. package/src/components/source.stories.tsx +0 -124
  229. package/src/components/tasks-card.stories.tsx +0 -78
  230. package/src/components/text-shimmer.stories.tsx +0 -87
  231. package/src/components/thinking-bar.stories.tsx +0 -87
  232. package/src/components/tool.stories.tsx +0 -153
  233. package/src/components/voice-input.stories.tsx +0 -83
  234. package/src/elements/artifact.stories.tsx +0 -506
  235. package/src/elements/attachments.stories.tsx +0 -190
  236. package/src/elements/card.stories.tsx +0 -113
  237. package/src/elements/cards.stories.tsx +0 -59
  238. package/src/elements/catalog.stories.tsx +0 -491
  239. package/src/elements/chain-of-thought.declarative.test.tsx +0 -153
  240. package/src/elements/chain-of-thought.stories.tsx +0 -113
  241. package/src/elements/chat-scope-picker.stories.tsx +0 -73
  242. package/src/elements/chat-workspace.stories.tsx +0 -288
  243. package/src/elements/chat.stories.tsx +0 -151
  244. package/src/elements/checkpoint.stories.tsx +0 -72
  245. package/src/elements/choice.stories.tsx +0 -240
  246. package/src/elements/code-block.stories.tsx +0 -83
  247. package/src/elements/composed-shell.stories.tsx +0 -316
  248. package/src/elements/confirm-card.stories.tsx +0 -227
  249. package/src/elements/context-meter.stories.tsx +0 -151
  250. package/src/elements/conversation-list.declarative.test.tsx +0 -137
  251. package/src/elements/conversation-list.stories.tsx +0 -243
  252. package/src/elements/embed.stories.tsx +0 -213
  253. package/src/elements/empty.stories.tsx +0 -111
  254. package/src/elements/feedback-bar.stories.tsx +0 -114
  255. package/src/elements/file-tree.stories.tsx +0 -134
  256. package/src/elements/file-upload.stories.tsx +0 -82
  257. package/src/elements/form.stories.tsx +0 -248
  258. package/src/elements/image.stories.tsx +0 -71
  259. package/src/elements/link-preview.stories.tsx +0 -209
  260. package/src/elements/loader.stories.tsx +0 -88
  261. package/src/elements/markdown.stories.tsx +0 -76
  262. package/src/elements/message-skills.declarative.test.tsx +0 -104
  263. package/src/elements/message-skills.stories.tsx +0 -114
  264. package/src/elements/message.stories.tsx +0 -218
  265. package/src/elements/model-switcher.declarative.test.tsx +0 -130
  266. package/src/elements/model-switcher.stories.tsx +0 -198
  267. package/src/elements/popover.stories.tsx +0 -80
  268. package/src/elements/prompt-input-slash-command.test.tsx +0 -128
  269. package/src/elements/prompt-input-stoppable.test.tsx +0 -88
  270. package/src/elements/prompt-input-toolbar-actions.test.tsx +0 -120
  271. package/src/elements/prompt-input.stories.tsx +0 -331
  272. package/src/elements/prompt-suggestions.declarative.test.tsx +0 -164
  273. package/src/elements/prompt-suggestions.stories.tsx +0 -213
  274. package/src/elements/reasoning.stories.tsx +0 -77
  275. package/src/elements/remote.stories.tsx +0 -314
  276. package/src/elements/resizable.stories.tsx +0 -426
  277. package/src/elements/response-stream.stories.tsx +0 -79
  278. package/src/elements/scroll-button.stories.tsx +0 -202
  279. package/src/elements/scroll-button.test.tsx +0 -148
  280. package/src/elements/source-list.stories.tsx +0 -143
  281. package/src/elements/source.stories.tsx +0 -88
  282. package/src/elements/switch.stories.tsx +0 -56
  283. package/src/elements/tasks.stories.tsx +0 -237
  284. package/src/elements/text-shimmer.stories.tsx +0 -64
  285. package/src/elements/thinking-bar.stories.tsx +0 -72
  286. package/src/elements/tool.stories.tsx +0 -89
  287. package/src/elements/voice-input.stories.tsx +0 -88
  288. package/src/primitives/controllable.test.ts +0 -47
  289. package/src/stories/chat-panel-layout.stories.tsx +0 -145
  290. package/src/stories/chat-scene.tsx +0 -571
  291. package/src/stories/checkpoint-restore.stories.tsx +0 -256
  292. package/src/stories/context-usage.stories.tsx +0 -212
  293. package/src/stories/conversation-with-reasoning.stories.tsx +0 -182
  294. package/src/stories/conversation-with-sources.stories.tsx +0 -471
  295. package/src/stories/docs/Accessibility.mdx +0 -119
  296. package/src/stories/docs/ChildElements.mdx +0 -269
  297. package/src/stories/docs/ForAIAgents.mdx +0 -162
  298. package/src/stories/docs/GettingStarted.mdx +0 -65
  299. package/src/stories/docs/Installation.mdx +0 -83
  300. package/src/stories/docs/Introduction.mdx +0 -50
  301. package/src/stories/docs/SolidJsAdvanced.mdx +0 -17
  302. package/src/stories/docs/Theming.mdx +0 -85
  303. package/src/stories/docs/element-controls.ts +0 -121
  304. package/src/stories/docs/frameworks/Angular.mdx +0 -245
  305. package/src/stories/docs/frameworks/Html.mdx +0 -227
  306. package/src/stories/docs/frameworks/Overview.mdx +0 -47
  307. package/src/stories/docs/frameworks/React.mdx +0 -227
  308. package/src/stories/docs/frameworks/Solid.mdx +0 -173
  309. package/src/stories/docs/frameworks/Svelte.mdx +0 -194
  310. package/src/stories/docs/frameworks/Vue.mdx +0 -238
  311. package/src/stories/docs/generative-ui-overview.mdx +0 -186
  312. package/src/stories/docs/recipes/SpeechToText.mdx +0 -7
  313. package/src/stories/docs/recipes/Streaming.mdx +0 -68
  314. package/src/stories/docs/recipes/TextToSpeech.mdx +0 -35
  315. package/src/stories/docs/theme-editor/canvas.tsx +0 -32
  316. package/src/stories/docs/theme-editor/inspector.tsx +0 -66
  317. package/src/stories/docs/theme-editor/presets.test.ts +0 -32
  318. package/src/stories/docs/theme-editor/presets.ts +0 -64
  319. package/src/stories/docs/theme-editor/theme-css.test.ts +0 -19
  320. package/src/stories/docs/theme-editor/theme-css.ts +0 -15
  321. package/src/stories/docs/theme-editor/theme-editor.tsx +0 -146
  322. package/src/stories/docs/theme-tokens.tsx +0 -174
  323. package/src/stories/examples/ChoosingComponents.mdx +0 -106
  324. package/src/stories/examples/sample-data.ts +0 -79
  325. package/src/stories/examples/usage/checkpoint-restore.ts +0 -156
  326. package/src/stories/examples/usage/context-usage.ts +0 -591
  327. package/src/stories/examples/usage/conversation-with-reasoning.ts +0 -224
  328. package/src/stories/examples/usage/conversation-with-sources.ts +0 -589
  329. package/src/stories/examples/usage/empty-state.ts +0 -144
  330. package/src/stories/examples/usage/full-chat-app.ts +0 -277
  331. package/src/stories/examples/usage/index.ts +0 -63
  332. package/src/stories/examples/usage/message-actions.ts +0 -704
  333. package/src/stories/examples/usage/pattern-centered-conversation.ts +0 -446
  334. package/src/stories/examples/usage/pattern-chat-panel-layout.ts +0 -442
  335. package/src/stories/examples/usage/pattern-docked-widget.ts +0 -479
  336. package/src/stories/examples/usage/prompt-input-variants.ts +0 -1356
  337. package/src/stories/examples/usage/streaming-response.ts +0 -687
  338. package/src/stories/examples/usage/types.ts +0 -33
  339. package/src/stories/full-chat.stories.tsx +0 -18
  340. package/src/stories/message-actions.stories.tsx +0 -230
  341. package/src/stories/pattern-centered-conversation.stories.tsx +0 -93
  342. package/src/stories/pattern-docked-widget.stories.tsx +0 -93
  343. package/src/stories/pattern-empty-state.stories.tsx +0 -76
  344. package/src/stories/prompt-input-variants.stories.tsx +0 -456
  345. package/src/stories/streaming-response.stories.tsx +0 -438
  346. package/src/stories/theme-editor.stories.tsx +0 -16
  347. package/src/stories/token-reference.stories.tsx +0 -18
  348. package/src/stories/typography.stories.tsx +0 -78
  349. package/src/ui/avatar.stories.tsx +0 -103
  350. package/src/ui/badge.stories.tsx +0 -86
  351. package/src/ui/button.stories.tsx +0 -145
  352. package/src/ui/collapsible.stories.tsx +0 -69
  353. package/src/ui/dropdown.stories.tsx +0 -59
  354. package/src/ui/hover-card.stories.tsx +0 -77
  355. package/src/ui/hover-card.test.tsx +0 -30
  356. package/src/ui/overlay.stories.tsx +0 -115
  357. package/src/ui/popover.stories.tsx +0 -81
  358. package/src/ui/popover.test.tsx +0 -99
  359. package/src/ui/resizable.stories.tsx +0 -236
  360. package/src/ui/scroll-area.stories.tsx +0 -50
  361. package/src/ui/separator.stories.tsx +0 -81
  362. package/src/ui/skeleton.stories.tsx +0 -337
  363. package/src/ui/switch.stories.tsx +0 -51
  364. package/src/ui/switch.test.tsx +0 -59
  365. package/src/ui/textarea.stories.tsx +0 -76
  366. package/src/ui/tooltip.stories.tsx +0 -74
@@ -0,0 +1,128 @@
1
+ /**
2
+ * manifest.ts — reads dist/custom-elements.json (a Custom Elements Manifest)
3
+ * and exposes helpers for the component_reference tool.
4
+ *
5
+ * Resolution strategy (dual-context):
6
+ * 1. Bundled bin: dist/mcp.es.js lives in dist/, so custom-elements.json is
7
+ * a sibling → try ./custom-elements.json relative to import.meta.url.
8
+ * 2. Vitest (source): manifest.ts lives in src/agent-tooling/mcp/, so we walk
9
+ * up parent directories looking for <dir>/dist/custom-elements.json.
10
+ */
11
+
12
+ import { readFileSync, existsSync } from 'node:fs';
13
+ import { fileURLToPath } from 'node:url';
14
+ import { join, dirname } from 'node:path';
15
+
16
+ // ── CEM types ────────────────────────────────────────────────────────────────
17
+
18
+ export interface CemType {
19
+ text: string;
20
+ }
21
+
22
+ export interface CemMember {
23
+ kind: 'field' | 'method';
24
+ privacy?: 'public' | 'private' | 'protected';
25
+ name: string;
26
+ type?: CemType;
27
+ description?: string;
28
+ }
29
+
30
+ export interface CemAttribute {
31
+ name: string;
32
+ fieldName?: string;
33
+ type?: CemType;
34
+ description?: string;
35
+ }
36
+
37
+ export interface CemEvent {
38
+ name: string;
39
+ type?: CemType;
40
+ description?: string;
41
+ }
42
+
43
+ export interface CemCssProperty {
44
+ name: string;
45
+ description?: string;
46
+ default?: string;
47
+ }
48
+
49
+ export interface Declaration {
50
+ tagName?: string;
51
+ name: string;
52
+ kind: string;
53
+ description?: string;
54
+ members?: CemMember[];
55
+ attributes?: CemAttribute[];
56
+ events?: CemEvent[];
57
+ cssProperties?: CemCssProperty[];
58
+ }
59
+
60
+ interface CemModule {
61
+ path?: string;
62
+ declarations?: Declaration[];
63
+ }
64
+
65
+ interface CustomElementsManifest {
66
+ modules: CemModule[];
67
+ }
68
+
69
+ // ── Manifest resolution ───────────────────────────────────────────────────────
70
+
71
+ function resolveManifestPath(): string {
72
+ const thisFile = fileURLToPath(import.meta.url);
73
+ const thisDir = dirname(thisFile);
74
+
75
+ // 1. Bundled bin: sibling in the same directory
76
+ const sibling = join(thisDir, 'custom-elements.json');
77
+ if (existsSync(sibling)) {
78
+ return sibling;
79
+ }
80
+
81
+ // 2. Source/Vitest: walk up parent directories looking for dist/custom-elements.json
82
+ let dir = thisDir;
83
+ for (let i = 0; i < 10; i++) {
84
+ const candidate = join(dir, 'dist', 'custom-elements.json');
85
+ if (existsSync(candidate)) {
86
+ return candidate;
87
+ }
88
+ const parent = dirname(dir);
89
+ if (parent === dir) break; // reached filesystem root
90
+ dir = parent;
91
+ }
92
+
93
+ throw new Error(
94
+ `Could not find custom-elements.json. Searched from: ${thisDir}`,
95
+ );
96
+ }
97
+
98
+ // ── Parsed manifest (module-level cache) ─────────────────────────────────────
99
+
100
+ let _manifest: CustomElementsManifest | undefined;
101
+
102
+ function getManifest(): CustomElementsManifest {
103
+ if (!_manifest) {
104
+ const path = resolveManifestPath();
105
+ const raw = readFileSync(path, 'utf-8');
106
+ _manifest = JSON.parse(raw) as CustomElementsManifest;
107
+ }
108
+ return _manifest;
109
+ }
110
+
111
+ function getDeclarations(): Declaration[] {
112
+ return getManifest().modules.flatMap((m) => m.declarations ?? []);
113
+ }
114
+
115
+ // ── Public API ────────────────────────────────────────────────────────────────
116
+
117
+ /** Returns the Declaration for a given custom-element tag, or undefined. */
118
+ export function getElement(tag: string): Declaration | undefined {
119
+ return getDeclarations().find((d) => d.tagName === tag);
120
+ }
121
+
122
+ /** Returns all custom-element tagNames, sorted alphabetically. */
123
+ export function listElements(): string[] {
124
+ return getDeclarations()
125
+ .filter((d) => d.tagName)
126
+ .map((d) => d.tagName!)
127
+ .sort();
128
+ }
@@ -0,0 +1,59 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import {
3
+ CallToolRequestSchema,
4
+ ListToolsRequestSchema,
5
+ } from '@modelcontextprotocol/sdk/types.js';
6
+ import { z } from 'zod';
7
+ import { reference } from './tools/reference';
8
+ import { scaffold } from './tools/scaffold';
9
+ import { theme } from './tools/theme';
10
+ import { debug } from './tools/debug';
11
+ import type { Tool } from './tools/types';
12
+
13
+ const tools: Tool[] = [reference, scaffold, theme, debug];
14
+
15
+ /**
16
+ * The AI/UI MCP server. A stdio server (see ./stdio.ts) exposing the AI/UI tools
17
+ * to any MCP harness. Registers four tools; Tasks 2–5 fill in each handler.
18
+ *
19
+ * The returned `Server` is augmented with `__listToolsForTest()`, a test-only
20
+ * helper that returns the registered tool names without touching the transport.
21
+ */
22
+ export interface AiUiServer extends Server {
23
+ __listToolsForTest(): string[];
24
+ }
25
+
26
+ export function createServer(): AiUiServer {
27
+ const server = new Server(
28
+ { name: '@kitn.ai/ui', version: '0.15.0' },
29
+ { capabilities: { tools: {} } },
30
+ ) as AiUiServer;
31
+
32
+ const byName = new Map(tools.map((t) => [t.name, t]));
33
+
34
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
35
+ tools: tools.map((t) => ({
36
+ name: t.name,
37
+ description: t.description,
38
+ // The protocol requires a JSON Schema. We use zod 4's native converter
39
+ // (the repo is on zod 4); the SDK-bundled zod-to-json-schema only
40
+ // understands zod 3 and emits an empty schema for our zod 4 objects.
41
+ inputSchema: z.toJSONSchema(t.inputSchema) as Record<string, unknown>,
42
+ })),
43
+ }));
44
+
45
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
46
+ const tool = byName.get(request.params.name);
47
+ if (!tool) {
48
+ return {
49
+ isError: true,
50
+ content: [{ type: 'text', text: `Unknown tool: ${request.params.name}` }],
51
+ };
52
+ }
53
+ return tool.handler(request.params.arguments ?? {});
54
+ });
55
+
56
+ server.__listToolsForTest = () => tools.map((t) => t.name);
57
+
58
+ return server;
59
+ }
@@ -0,0 +1,18 @@
1
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
2
+ import { createServer } from './server';
3
+
4
+ /**
5
+ * Build entry for the AI/UI MCP server. Connects the server (./server.ts) to a
6
+ * stdio transport so any MCP harness (Claude Code, Codex, …) can drive it over
7
+ * stdin/stdout. Compiled to dist/mcp.es.js by vite.config.mcp.ts and launched by
8
+ * bin/mcp.js, which handles fatal errors / exit (this file stays free of Node
9
+ * globals so it typechecks under the repo's vite/client-only tsconfig).
10
+ */
11
+ export async function startStdioServer(): Promise<void> {
12
+ const server = createServer();
13
+ const transport = new StdioServerTransport();
14
+ await server.connect(transport);
15
+ }
16
+
17
+ // Auto-start when imported as the entry (the only consumer is bin/mcp.js).
18
+ startStdioServer();
@@ -0,0 +1,305 @@
1
+ import { z } from 'zod';
2
+ import type { Tool } from './types';
3
+
4
+ /**
5
+ * debug — diagnose common AI/UI (kai-*) integration failures.
6
+ *
7
+ * Given a `symptom` (prose) and/or a `snippet` (code), returns the likely
8
+ * cause + fix for the classic `kai-*` failure modes so a harness can
9
+ * self-correct without a human in the loop.
10
+ *
11
+ * Rule set sourced from:
12
+ * - docs-site/src/content/docs/guides/for-ai-agents.mdx §"What agents most commonly get wrong"
13
+ * - context7.json `rules` array (5 contract rules)
14
+ */
15
+
16
+ interface Rule {
17
+ id: string;
18
+ test: (text: string) => boolean;
19
+ title: string;
20
+ cause: string;
21
+ fix: string;
22
+ }
23
+
24
+ const RULES: Rule[] = [
25
+ {
26
+ // Rule 1 — array/object data set as an HTML attribute
27
+ // Source: for-ai-agents.mdx §1; context7.json rule 2
28
+ id: 'array-as-attribute',
29
+ test: (t) =>
30
+ /\b(messages|models|context|suggestions|slashCommands)\s*=\s*["']/.test(t),
31
+ title: 'Array/object prop set as an HTML attribute (silent failure)',
32
+ cause:
33
+ 'An HTML attribute is always a string. Passing `messages`, `models`, `context`, ' +
34
+ '`suggestions`, or `slashCommands` as an HTML attribute silently fails — ' +
35
+ 'the element receives a stringified value it cannot parse.',
36
+ fix:
37
+ 'Set the property in JavaScript, not as an HTML attribute. ' +
38
+ 'Only scalar props (`placeholder`, `loading`, `theme`) work as attributes.\n\n' +
39
+ '```js\n' +
40
+ "// ✅ Works — set messages in JavaScript as a property\n" +
41
+ "const chat = document.querySelector('kai-chat');\n" +
42
+ "chat.messages = [{ id: '1', role: 'assistant', content: 'Hello!' }];\n" +
43
+ '```\n\n' +
44
+ '```html\n' +
45
+ '<!-- ❌ Fails — messages cannot be an HTML attribute -->\n' +
46
+ '<kai-chat messages="[...]"></kai-chat>\n' +
47
+ '```',
48
+ },
49
+ {
50
+ // Rule 2 — in-place mutation → no re-render
51
+ // Source: for-ai-agents.mdx §3; context7.json rule 4
52
+ id: 'in-place-mutation',
53
+ test: (t) =>
54
+ /don'?t\s+update|doesn'?t\s+re.?render|no\s+re.?render|\.push\(|\bpush(?:es|ing)?\s+(?:to|into|onto)\b|mutate|in.place/.test(t),
55
+ title: 'In-place mutation does not trigger a re-render',
56
+ cause:
57
+ 'Mutating an existing message object or array in place (e.g. `chat.messages.push(…)` ' +
58
+ 'or `chat.messages[i].content = …`) does not trigger a re-render. ' +
59
+ 'The element only reacts when it detects a new array/object reference.',
60
+ fix:
61
+ 'Assign a NEW array (and a new object) on every change — never mutate in place.\n\n' +
62
+ '```js\n' +
63
+ '// ✅ Triggers re-render — new array + new object reference\n' +
64
+ 'chat.messages = [\n' +
65
+ ' ...chat.messages,\n' +
66
+ " { id: crypto.randomUUID(), role: 'user', content: userText },\n" +
67
+ '];\n\n' +
68
+ '// ✅ During streaming — replace with a new array + new object on every chunk\n' +
69
+ 'chat.messages = chat.messages.map((m) =>\n' +
70
+ ' m.id === assistantId ? { ...m, content: accumulated } : m\n' +
71
+ ');\n\n' +
72
+ '// ❌ Does NOT trigger re-render\n' +
73
+ 'chat.messages.push(newMsg);\n' +
74
+ 'chat.messages[i].content = accumulated;\n' +
75
+ '```',
76
+ },
77
+ {
78
+ // Rule 3 — listening for events on a parent / wrong element
79
+ // Source: for-ai-agents.mdx §2; context7.json rule 3
80
+ id: 'event-bubbling',
81
+ test: (t) =>
82
+ /event.*not\s+fir|not\s+fir.*event|listen.*document|document.*listen|listen.*parent|parent.*listen|event.*bubbl/i.test(
83
+ t,
84
+ ),
85
+ title: 'Listening for events on the wrong element (events are non-bubbling)',
86
+ cause:
87
+ '`kai-*` events are non-bubbling CustomEvents. Adding a listener to `document`, ' +
88
+ '`window`, or a parent container will never fire because the event does not bubble up.',
89
+ fix:
90
+ 'Listen directly on the `kai-*` element. ' +
91
+ 'The submit event is `kai-submit` with `event.detail.value`.\n\n' +
92
+ '```js\n' +
93
+ "const chat = document.querySelector('kai-chat');\n\n" +
94
+ "// ✅ Listen directly on the element\n" +
95
+ "chat.addEventListener('kai-submit', (e) => {\n" +
96
+ ' console.log(e.detail.value); // the text the user typed\n' +
97
+ '});\n\n' +
98
+ "// ❌ Never fires — kai-submit does not bubble\n" +
99
+ "document.addEventListener('kai-submit', handler);\n" +
100
+ '```\n\n' +
101
+ 'Common events: `kai-submit`, `kai-feedback`, `kai-model-change`, `kai-new-chat`, `kai-select`.',
102
+ },
103
+ {
104
+ // Rule 4 — wrong element prefix kitn-
105
+ // Source: context7.json rule 1
106
+ id: 'wrong-prefix',
107
+ test: (t) => /\bkitn-/.test(t),
108
+ title: 'Wrong element prefix `kitn-` (should be `kai-`)',
109
+ cause:
110
+ 'The custom element prefix is `kai-` (e.g. `<kai-chat>`, `<kai-artifact>`). ' +
111
+ '`kitn-chat` is only a bundle filename — it is not a registered element name. ' +
112
+ 'Using `<kitn-chat>` results in an unknown element that renders nothing.',
113
+ fix:
114
+ 'Replace the `kitn-` prefix with `kai-` everywhere.\n\n' +
115
+ '```html\n' +
116
+ '<!-- ✅ Correct element prefix -->\n' +
117
+ '<kai-chat></kai-chat>\n' +
118
+ '<kai-artifact></kai-artifact>\n\n' +
119
+ '<!-- ❌ Wrong — kitn-chat is a bundle filename, not an element -->\n' +
120
+ '<kitn-chat></kitn-chat>\n' +
121
+ '```',
122
+ },
123
+ {
124
+ // Rule 6 — custom elements not registered / renders nothing (React #1 failure)
125
+ // Source: field-test reports; for-ai-agents.mdx §"Import order matters"
126
+ id: 'elements-not-registered',
127
+ test: (t) => {
128
+ // Core render-nothing / unregistered-element signals
129
+ if (
130
+ /renders?\s+nothing|nothing\s+renders?|not\s+registered|unregistered|not\s+upgraded|unknown\s+element|customElements\.get|undefined\s+element|no\s+shadow\s+root/.test(t)
131
+ )
132
+ return true;
133
+ // "empty" / "blank" / "doesn't render" / "won't render" only fire when
134
+ // a render/element/component context is also present to avoid false positives
135
+ if (
136
+ /\b(empty|blank)\b/.test(t) &&
137
+ /render|element|component|kai-|<[a-z]+-|shadow/.test(t)
138
+ )
139
+ return true;
140
+ if (
141
+ /doesn'?t\s+render|won'?t\s+render/.test(t) &&
142
+ /kai-|element|component|custom.?element/.test(t)
143
+ )
144
+ return true;
145
+ return false;
146
+ },
147
+ title: 'Custom elements not registered — renders nothing / empty box',
148
+ cause:
149
+ 'The `@kitn.ai/ui/react` wrappers (and bare `<kai-*>` tags) do NOT register the ' +
150
+ 'custom elements by themselves. Without the element-registration side-effect import, ' +
151
+ '`<kai-chat>` / `<Chat>` is an un-upgraded unknown element — an empty box. ' +
152
+ '`customElements.get(\'kai-chat\') === undefined`.',
153
+ fix:
154
+ 'Import the elements bundle for its side effect BEFORE your first render — ' +
155
+ 'it must run before the component mounts.\n\n' +
156
+ '```tsx\n' +
157
+ "import '@kitn.ai/ui/elements' // registers <kai-*> — REQUIRED, must come first\n" +
158
+ "import { Chat } from '@kitn.ai/ui/react'\n" +
159
+ "import '@kitn.ai/ui/theme.css'\n" +
160
+ '```\n\n' +
161
+ 'In plain HTML: `import \'@kitn.ai/ui/elements\'` in your module script. ' +
162
+ 'The import is a side effect — keep it even if your linter flags it as "unused".',
163
+ },
164
+ {
165
+ // Rule 7 — tsc errors inside node_modules/@kitn.ai/ui/src (SolidJS source pulled in)
166
+ // Source: field-test reports; packaging gap (tracked upstream)
167
+ id: 'tsc-source-pull',
168
+ test: (t) =>
169
+ /node_modules\/@kitn\.ai\/ui/.test(t) &&
170
+ /tsc|TS2786|cannot\s+be\s+used\s+as\s+a\s+jsx\s+component|Show\b|Portal\b|Dynamic\b|error\s+TS|type\s+error/.test(
171
+ t,
172
+ ),
173
+ title: 'tsc errors inside node_modules/@kitn.ai/ui/src (SolidJS source compiled under React)',
174
+ cause:
175
+ 'The package currently ships TypeScript/TSX source, and a type entry value-re-exports ' +
176
+ 'from it, so the consumer\'s `tsc` resolves and compiles the library\'s SolidJS internals ' +
177
+ '(`src/ui/*.tsx`) under the app\'s React JSX config — `Show`/`Portal`/`Dynamic` aren\'t ' +
178
+ 'React components, causing TS2786 / "cannot be used as a JSX component" errors. ' +
179
+ '`vite`/esbuild build fine (they strip types); only `tsc` breaks. ' +
180
+ '`skipLibCheck` does not help (these are `.tsx` source, not `.d.ts`).',
181
+ fix:
182
+ 'Redirect the type resolution for that subpath in your tsconfig ' +
183
+ '(Vite ignores tsconfig `paths`, so runtime is unaffected):\n\n' +
184
+ '```jsonc\n' +
185
+ '// tsconfig (app)\n' +
186
+ '"baseUrl": ".",\n' +
187
+ '"paths": { "@kitn.ai/ui/elements": ["./src/stubs/kitn-elements.d.ts"] }\n' +
188
+ '```\n\n' +
189
+ '```ts\n' +
190
+ '// src/stubs/kitn-elements.d.ts\n' +
191
+ 'export {}\n' +
192
+ '```\n\n' +
193
+ '(This is a known packaging gap being tracked upstream.)',
194
+ },
195
+ {
196
+ // Rule 8 — fetch('/api/chat') 404 in a Vite SPA (no server-side routes)
197
+ // Source: field-test reports; common scaffold confusion
198
+ id: 'vite-api-404',
199
+ test: (t) => {
200
+ // /api/chat 404
201
+ if (/\/api\/chat/.test(t) && /\b404\b|not\s+found/i.test(t)) return true;
202
+ // Vite + missing API route / route handler / POST
203
+ if (/\bvite\b/.test(t) && /api\s+route|route\s+handler|\bPOST\b.*not\s+work/.test(t)) return true;
204
+ // Next.js route handler used in a Vite app
205
+ if (/next\.?js.*route|route.*next\.?js/.test(t) && /\bvite\b/.test(t)) return true;
206
+ return false;
207
+ },
208
+ title: 'fetch(\'/api/chat\') 404 — Vite SPA has no server-side API routes',
209
+ cause:
210
+ 'A plain Vite/CRA React SPA has no server — there are no `/api` routes. ' +
211
+ 'A scaffolded Next.js route handler (`export async function POST`) does not run there, ' +
212
+ 'so `fetch(\'/api/chat\')` 404s.',
213
+ fix:
214
+ 'Either run the backend somewhere real, or skip it entirely for local dev:\n\n' +
215
+ '```ts\n' +
216
+ '// Option A — use Next.js where route handlers are supported\n' +
217
+ "// app/api/chat/route.ts: export async function POST(req) { ... }\n\n" +
218
+ '// Option B — add a Vite dev-server middleware/proxy\n' +
219
+ "// vite.config.ts: server: { proxy: { '/api': 'http://localhost:3001' } }\n\n" +
220
+ '// Option C — run a separate Express/Hono server\n' +
221
+ "// framework: 'express' in your harness config\n\n" +
222
+ '// Option D — zero-config local dev with mock integration (no backend needed)\n' +
223
+ "// Use `integration: 'mock'` in the scaffold tool\n" +
224
+ '```',
225
+ },
226
+ {
227
+ // Rule 5 — SSR / server component / document is not defined
228
+ // Source: for-ai-agents.mdx (client-only import); context7.json rule 2 (property rule requires DOM)
229
+ id: 'ssr-server-component',
230
+ test: (t) => {
231
+ // Core SSR signals — always trigger
232
+ if (/\bssr\b|server\s+component|document\s+is\s+not\s+defined|window\s+is\s+not\s+defined|next\.?js.*server|server.*next\.?js/.test(t)) return true;
233
+ // "hydration" only triggers when a web-component / kai context is also present
234
+ if (/hydration/.test(t) && /kai|web.?component|custom.?element|<[a-z]+-/.test(t)) return true;
235
+ return false;
236
+ },
237
+ title: 'SSR / server-side rendering — element requires the browser DOM',
238
+ cause:
239
+ '`kai-*` elements are client-side web components. They require `document` and ' +
240
+ '`customElements` to register and render. Importing them in a server component ' +
241
+ '(Next.js App Router server component, Nuxt SSR, etc.) throws ' +
242
+ '"document is not defined" or silently produces no output.',
243
+ fix:
244
+ 'Register the element on the client only. ' +
245
+ 'Use your framework\'s "client-only" / island / dynamic-import pattern.\n\n' +
246
+ '```js\n' +
247
+ "// ✅ Plain HTML / vanilla — import in a <script type=\"module\">\n" +
248
+ "import '@kitn.ai/ui/elements';\n\n" +
249
+ '// ✅ Next.js App Router — mark the component with "use client"\n' +
250
+ "'use client';\n" +
251
+ "import '@kitn.ai/ui/elements';\n\n" +
252
+ '// ✅ Next.js — dynamic import with ssr: false\n' +
253
+ "import dynamic from 'next/dynamic';\n" +
254
+ "const KaiChat = dynamic(() => import('@kitn.ai/ui/elements').then(() => 'kai-chat'), { ssr: false });\n\n" +
255
+ '// ✅ React wrapper (already client-safe)\n' +
256
+ "import { Chat } from '@kitn.ai/ui/react';\n" +
257
+ '```',
258
+ },
259
+ ];
260
+
261
+ function buildText(matched: Rule[]): string {
262
+ if (matched.length === 0) {
263
+ return (
264
+ 'No known failure pattern matched. Suggested next steps:\n\n' +
265
+ '1. Use the `component_reference` tool to look up the real API for the element ' +
266
+ '(prop names, event names, attribute vs. property distinction).\n' +
267
+ '2. Check the **Streaming recipe** in `llms-full.txt` ' +
268
+ '(`node_modules/@kitn.ai/ui/llms-full.txt` or https://ui.kitn.ai/llms-full.txt) ' +
269
+ 'for correct streaming wiring.\n' +
270
+ '3. Paste `https://ui.kitn.ai/llms.txt` into your prompt for a compact orientation.'
271
+ );
272
+ }
273
+
274
+ const sections = matched.map((rule, i) => {
275
+ const n = i + 1;
276
+ return `## ${n}. ${rule.title}\n\n**Cause:** ${rule.cause}\n\n**Fix:** ${rule.fix}`;
277
+ });
278
+
279
+ const header =
280
+ matched.length === 1
281
+ ? '1 likely cause found:\n\n'
282
+ : `${matched.length} likely causes found:\n\n`;
283
+
284
+ return header + sections.join('\n\n---\n\n');
285
+ }
286
+
287
+ export const debug: Tool = {
288
+ name: 'debug',
289
+ description:
290
+ 'Diagnose common AI/UI (kai-*) integration issues. ' +
291
+ 'Provide a `symptom` (prose description) and/or a `snippet` (code) ' +
292
+ 'and receive the likely cause + fix for classic kai-* failure modes.',
293
+ inputSchema: z.object({
294
+ symptom: z.string().optional(),
295
+ snippet: z.string().optional(),
296
+ }),
297
+ handler: async (args) => {
298
+ const combined = `${(args.symptom as string | undefined) ?? ''} ${(args.snippet as string | undefined) ?? ''}`;
299
+ const matched = RULES.filter((rule) => rule.test(combined));
300
+
301
+ return {
302
+ content: [{ type: 'text', text: buildText(matched) }],
303
+ };
304
+ },
305
+ };
@@ -0,0 +1,132 @@
1
+ import { z } from 'zod';
2
+ import type { Tool } from './types';
3
+ import { getElement, listElements } from '../manifest';
4
+
5
+ /**
6
+ * component_reference — look up AI/UI (kai-*) web components, their props,
7
+ * events, attributes, and CSS custom properties from the live Custom Elements
8
+ * Manifest (dist/custom-elements.json).
9
+ *
10
+ * Backed by manifest.ts which resolves the CEM in both the bundled bin
11
+ * (dist/mcp.es.js sibling) and the Vitest source context (walks up to find
12
+ * dist/custom-elements.json in the repo root).
13
+ */
14
+
15
+ /** Types that warrant the JS-property contract note. */
16
+ const JS_ONLY_TYPE_PATTERNS = /\[\]|\{|Record</;
17
+
18
+ function isJsOnlyType(typeText: string | undefined): boolean {
19
+ return typeText ? JS_ONLY_TYPE_PATTERNS.test(typeText) : false;
20
+ }
21
+
22
+ function formatReference(tag: string): string {
23
+ const el = getElement(tag);
24
+
25
+ if (!el) {
26
+ const all = listElements();
27
+ const sample = all.slice(0, 5).join(', ');
28
+ return (
29
+ `Unknown element: ${tag}\n\n` +
30
+ `Valid tags include: ${sample} (and ${Math.max(0, all.length - 5)} more).\n` +
31
+ `Call component_reference with no name (or name: "list") to list all ${all.length} elements.`
32
+ );
33
+ }
34
+
35
+ const lines: string[] = [];
36
+
37
+ // ── Header ─────────────────────────────────────────────────────────────────
38
+ lines.push(`## <${tag}>`);
39
+ if (el.description) {
40
+ lines.push('', el.description.trim());
41
+ }
42
+
43
+ // ── Contract note ──────────────────────────────────────────────────────────
44
+ lines.push(
45
+ '',
46
+ '### AI/UI contract',
47
+ '`kai-*` elements accept **array and object data as JavaScript properties** ' +
48
+ '(set in JavaScript via `el.property = value`, not as HTML attributes). ' +
49
+ 'Events are native CustomEvents — listen with `el.addEventListener("event-name", handler)` ' +
50
+ 'and read `event.detail` for the payload.',
51
+ );
52
+
53
+ // ── Public JS properties ───────────────────────────────────────────────────
54
+ const publicFields = (el.members ?? []).filter(
55
+ (m) => m.kind === 'field' && m.privacy === 'public',
56
+ );
57
+
58
+ if (publicFields.length > 0) {
59
+ lines.push('', '### Props (JavaScript properties)');
60
+ for (const field of publicFields) {
61
+ const type = field.type?.text ?? 'unknown';
62
+ const jsOnly = isJsOnlyType(type);
63
+ const desc = field.description?.trim() ?? '';
64
+ const note = jsOnly ? ' ⚑ set as a JS property, not an HTML attribute' : '';
65
+ lines.push(`- **${field.name}** \`${type}\`${note}`);
66
+ if (desc) {
67
+ lines.push(` ${desc}`);
68
+ }
69
+ }
70
+ }
71
+
72
+ // ── HTML attributes ────────────────────────────────────────────────────────
73
+ const attrs = el.attributes ?? [];
74
+ if (attrs.length > 0) {
75
+ lines.push('', '### Attributes (HTML-safe)');
76
+ for (const attr of attrs) {
77
+ const type = attr.type?.text ?? 'unknown';
78
+ const desc = attr.description?.trim() ?? '';
79
+ lines.push(`- **${attr.name}** \`${type}\``);
80
+ if (desc) {
81
+ lines.push(` ${desc}`);
82
+ }
83
+ }
84
+ }
85
+
86
+ // ── Events ────────────────────────────────────────────────────────────────
87
+ const events = el.events ?? [];
88
+ if (events.length > 0) {
89
+ lines.push('', '### Events (CustomEvent, listen via addEventListener)');
90
+ for (const ev of events) {
91
+ const desc = ev.description?.trim() ?? '';
92
+ lines.push(`- **${ev.name}** — ${desc}`);
93
+ }
94
+ }
95
+
96
+ // ── CSS custom properties ─────────────────────────────────────────────────
97
+ const cssProps = el.cssProperties ?? [];
98
+ if (cssProps.length > 0) {
99
+ lines.push('', '### CSS custom properties');
100
+ for (const prop of cssProps) {
101
+ const desc = prop.description?.trim() ?? '';
102
+ const def = prop.default ? ` (default: ${prop.default})` : '';
103
+ lines.push(`- **${prop.name}**${def}${desc ? ` — ${desc}` : ''}`);
104
+ }
105
+ }
106
+
107
+ return lines.join('\n');
108
+ }
109
+
110
+ export const reference: Tool = {
111
+ name: 'component_reference',
112
+ description:
113
+ 'Look up AI/UI (kai-*) web components: their tags, props, events, and usage examples.',
114
+ inputSchema: z.object({ name: z.string().optional() }),
115
+ handler: async (args: Record<string, unknown>) => {
116
+ const name = typeof args.name === 'string' ? args.name.trim() : undefined;
117
+
118
+ let text: string;
119
+
120
+ if (!name || name === 'list') {
121
+ const tags = listElements();
122
+ text =
123
+ `AI/UI elements (${tags.length} total):\n\n` +
124
+ tags.map((t) => ` ${t}`).join('\n') +
125
+ '\n\nCall component_reference with a specific name (e.g. { name: "kai-chat" }) for full API details.';
126
+ } else {
127
+ text = formatReference(name);
128
+ }
129
+
130
+ return { content: [{ type: 'text' as const, text }] };
131
+ },
132
+ };