@frontmcp/ui 0.12.2 → 1.0.0-beta.1

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 (310) hide show
  1. package/README.md +279 -72
  2. package/bridge/adapters/claude.adapter.d.ts.map +1 -1
  3. package/bridge/adapters/gemini.adapter.d.ts.map +1 -1
  4. package/bridge/index.js +2 -36
  5. package/components/Alert/Alert.d.ts +11 -0
  6. package/components/Alert/Alert.d.ts.map +1 -0
  7. package/components/Alert/index.d.ts +4 -0
  8. package/components/Alert/index.d.ts.map +1 -0
  9. package/components/Alert/index.js +61 -0
  10. package/components/Avatar/Avatar.d.ts +10 -0
  11. package/components/Avatar/Avatar.d.ts.map +1 -0
  12. package/components/Avatar/index.d.ts +4 -0
  13. package/components/Avatar/index.d.ts.map +1 -0
  14. package/components/Avatar/index.js +43 -0
  15. package/components/Badge/Badge.d.ts +13 -0
  16. package/components/Badge/Badge.d.ts.map +1 -0
  17. package/components/Badge/index.d.ts +4 -0
  18. package/components/Badge/index.d.ts.map +1 -0
  19. package/{render → components/Badge}/index.js +54 -42
  20. package/components/Button/Button.d.ts +16 -0
  21. package/components/Button/Button.d.ts.map +1 -0
  22. package/components/Button/index.d.ts +4 -0
  23. package/components/Button/index.d.ts.map +1 -0
  24. package/components/Button/index.js +91 -0
  25. package/components/Card/Card.d.ts +25 -0
  26. package/components/Card/Card.d.ts.map +1 -0
  27. package/components/Card/index.d.ts +4 -0
  28. package/components/Card/index.d.ts.map +1 -0
  29. package/components/Card/index.js +110 -0
  30. package/components/List/List.d.ts +15 -0
  31. package/components/List/List.d.ts.map +1 -0
  32. package/components/List/index.d.ts +4 -0
  33. package/components/List/index.d.ts.map +1 -0
  34. package/components/List/index.js +58 -0
  35. package/components/Loader/Loader.d.ts +28 -0
  36. package/components/Loader/Loader.d.ts.map +1 -0
  37. package/components/Loader/LoaderContext.d.ts +20 -0
  38. package/components/Loader/LoaderContext.d.ts.map +1 -0
  39. package/components/Loader/index.d.ts +6 -0
  40. package/components/Loader/index.d.ts.map +1 -0
  41. package/components/Loader/index.js +174 -0
  42. package/components/Modal/Modal.d.ts +22 -0
  43. package/components/Modal/Modal.d.ts.map +1 -0
  44. package/components/Modal/index.d.ts +4 -0
  45. package/components/Modal/index.d.ts.map +1 -0
  46. package/components/Modal/index.js +80 -0
  47. package/components/Select/Select.d.ts +21 -0
  48. package/components/Select/Select.d.ts.map +1 -0
  49. package/components/Select/index.d.ts +4 -0
  50. package/components/Select/index.d.ts.map +1 -0
  51. package/components/Select/index.js +78 -0
  52. package/components/Table/Table.d.ts +15 -0
  53. package/components/Table/Table.d.ts.map +1 -0
  54. package/components/Table/index.d.ts +4 -0
  55. package/components/Table/index.d.ts.map +1 -0
  56. package/components/Table/index.js +70 -0
  57. package/components/TextField/TextField.d.ts +19 -0
  58. package/components/TextField/TextField.d.ts.map +1 -0
  59. package/components/TextField/index.d.ts +4 -0
  60. package/components/TextField/index.d.ts.map +1 -0
  61. package/components/TextField/index.js +77 -0
  62. package/components/index.d.ts +22 -28
  63. package/components/index.d.ts.map +1 -1
  64. package/components/index.js +523 -2521
  65. package/esm/bridge/index.mjs +2 -6
  66. package/esm/components/Alert/index.mjs +28 -0
  67. package/esm/components/Avatar/index.mjs +10 -0
  68. package/esm/components/Badge/index.mjs +56 -0
  69. package/esm/components/Button/index.mjs +58 -0
  70. package/esm/components/Card/index.mjs +77 -0
  71. package/esm/components/List/index.mjs +25 -0
  72. package/esm/components/Loader/index.mjs +141 -0
  73. package/esm/components/Modal/index.mjs +47 -0
  74. package/esm/components/Select/index.mjs +45 -0
  75. package/esm/components/Table/index.mjs +37 -0
  76. package/esm/components/TextField/index.mjs +44 -0
  77. package/esm/components/index.mjs +513 -2415
  78. package/esm/index.mjs +62 -4652
  79. package/esm/package.json +372 -16
  80. package/esm/react/index.mjs +2 -285
  81. package/esm/renderer/charts/index.mjs +336 -0
  82. package/esm/renderer/common/index.mjs +135 -0
  83. package/esm/renderer/csv/index.mjs +193 -0
  84. package/esm/renderer/flow/index.mjs +259 -0
  85. package/esm/renderer/html/index.mjs +99 -0
  86. package/esm/renderer/image/index.mjs +125 -0
  87. package/esm/renderer/index.mjs +2173 -0
  88. package/esm/renderer/maps/index.mjs +217 -0
  89. package/esm/renderer/math/index.mjs +229 -0
  90. package/esm/renderer/mdx/index.mjs +261 -0
  91. package/esm/renderer/media/index.mjs +235 -0
  92. package/esm/renderer/mermaid/index.mjs +220 -0
  93. package/esm/renderer/pdf/index.mjs +229 -0
  94. package/esm/renderer/react/index.mjs +230 -0
  95. package/esm/runtime/index.mjs +194 -0
  96. package/esm/theme/index.mjs +93 -0
  97. package/index.d.ts +10 -18
  98. package/index.d.ts.map +1 -1
  99. package/index.js +63 -4806
  100. package/package.json +372 -16
  101. package/react/index.d.ts +8 -54
  102. package/react/index.d.ts.map +1 -1
  103. package/react/index.js +2 -295
  104. package/renderer/auto-detect.d.ts +39 -0
  105. package/renderer/auto-detect.d.ts.map +1 -0
  106. package/renderer/charts/index.d.ts +22 -0
  107. package/renderer/charts/index.d.ts.map +1 -0
  108. package/renderer/charts/index.js +367 -0
  109. package/renderer/common/index.d.ts +5 -0
  110. package/renderer/common/index.d.ts.map +1 -0
  111. package/renderer/common/index.js +158 -0
  112. package/renderer/common/inject-stylesheet.d.ts +9 -0
  113. package/renderer/common/inject-stylesheet.d.ts.map +1 -0
  114. package/renderer/common/lazy-import.d.ts +85 -0
  115. package/renderer/common/lazy-import.d.ts.map +1 -0
  116. package/renderer/common/use-lazy-module.d.ts +13 -0
  117. package/renderer/common/use-lazy-module.d.ts.map +1 -0
  118. package/renderer/common/use-renderer-theme.d.ts +35 -0
  119. package/renderer/common/use-renderer-theme.d.ts.map +1 -0
  120. package/renderer/csv/index.d.ts +12 -0
  121. package/renderer/csv/index.d.ts.map +1 -0
  122. package/renderer/csv/index.js +224 -0
  123. package/renderer/flow/index.d.ts +40 -0
  124. package/renderer/flow/index.d.ts.map +1 -0
  125. package/renderer/flow/index.js +290 -0
  126. package/renderer/html/index.d.ts +12 -0
  127. package/renderer/html/index.d.ts.map +1 -0
  128. package/renderer/html/index.js +130 -0
  129. package/renderer/image/index.d.ts +11 -0
  130. package/renderer/image/index.d.ts.map +1 -0
  131. package/renderer/image/index.js +156 -0
  132. package/renderer/index.d.ts +32 -0
  133. package/renderer/index.d.ts.map +1 -0
  134. package/renderer/index.js +2206 -0
  135. package/renderer/maps/index.d.ts +27 -0
  136. package/renderer/maps/index.d.ts.map +1 -0
  137. package/renderer/maps/index.js +248 -0
  138. package/renderer/math/index.d.ts +11 -0
  139. package/renderer/math/index.d.ts.map +1 -0
  140. package/renderer/math/index.js +260 -0
  141. package/renderer/mdx/index.d.ts +10 -0
  142. package/renderer/mdx/index.d.ts.map +1 -0
  143. package/renderer/mdx/index.js +292 -0
  144. package/renderer/media/index.d.ts +20 -0
  145. package/renderer/media/index.d.ts.map +1 -0
  146. package/renderer/media/index.js +266 -0
  147. package/renderer/mermaid/index.d.ts +11 -0
  148. package/renderer/mermaid/index.d.ts.map +1 -0
  149. package/renderer/mermaid/index.js +251 -0
  150. package/renderer/pdf/index.d.ts +10 -0
  151. package/renderer/pdf/index.d.ts.map +1 -0
  152. package/renderer/pdf/index.js +260 -0
  153. package/renderer/react/index.d.ts +45 -0
  154. package/renderer/react/index.d.ts.map +1 -0
  155. package/renderer/react/index.js +261 -0
  156. package/renderer/types.d.ts +24 -0
  157. package/renderer/types.d.ts.map +1 -0
  158. package/runtime/babel-runtime.d.ts +70 -0
  159. package/runtime/babel-runtime.d.ts.map +1 -0
  160. package/runtime/content-detector.d.ts +43 -0
  161. package/runtime/content-detector.d.ts.map +1 -0
  162. package/runtime/index.d.ts +10 -0
  163. package/runtime/index.d.ts.map +1 -0
  164. package/runtime/index.js +217 -0
  165. package/theme/FrontMcpThemeProvider.d.ts +4 -0
  166. package/theme/FrontMcpThemeProvider.d.ts.map +1 -0
  167. package/theme/create-theme.d.ts +9 -0
  168. package/theme/create-theme.d.ts.map +1 -0
  169. package/theme/index.d.ts +5 -0
  170. package/theme/index.d.ts.map +1 -0
  171. package/theme/index.js +126 -0
  172. package/theme/types.d.ts +28 -0
  173. package/theme/types.d.ts.map +1 -0
  174. package/theme/use-theme.d.ts +3 -0
  175. package/theme/use-theme.d.ts.map +1 -0
  176. package/bundler/browser-components.d.ts +0 -42
  177. package/bundler/browser-components.d.ts.map +0 -1
  178. package/bundler/bundler.d.ts +0 -282
  179. package/bundler/bundler.d.ts.map +0 -1
  180. package/bundler/index.d.ts +0 -43
  181. package/bundler/index.d.ts.map +0 -1
  182. package/bundler/index.js +0 -3168
  183. package/bundler/types.d.ts +0 -883
  184. package/bundler/types.d.ts.map +0 -1
  185. package/components/alert.d.ts +0 -83
  186. package/components/alert.d.ts.map +0 -1
  187. package/components/alert.schema.d.ts +0 -98
  188. package/components/alert.schema.d.ts.map +0 -1
  189. package/components/avatar.d.ts +0 -77
  190. package/components/avatar.d.ts.map +0 -1
  191. package/components/avatar.schema.d.ts +0 -170
  192. package/components/avatar.schema.d.ts.map +0 -1
  193. package/components/badge.d.ts +0 -78
  194. package/components/badge.d.ts.map +0 -1
  195. package/components/badge.schema.d.ts +0 -91
  196. package/components/badge.schema.d.ts.map +0 -1
  197. package/components/button.d.ts +0 -100
  198. package/components/button.d.ts.map +0 -1
  199. package/components/button.schema.d.ts +0 -120
  200. package/components/button.schema.d.ts.map +0 -1
  201. package/components/card.d.ts +0 -76
  202. package/components/card.d.ts.map +0 -1
  203. package/components/card.schema.d.ts +0 -93
  204. package/components/card.schema.d.ts.map +0 -1
  205. package/components/form.d.ts +0 -227
  206. package/components/form.d.ts.map +0 -1
  207. package/components/form.schema.d.ts +0 -365
  208. package/components/form.schema.d.ts.map +0 -1
  209. package/components/list.d.ts +0 -121
  210. package/components/list.d.ts.map +0 -1
  211. package/components/list.schema.d.ts +0 -129
  212. package/components/list.schema.d.ts.map +0 -1
  213. package/components/modal.d.ts +0 -100
  214. package/components/modal.d.ts.map +0 -1
  215. package/components/modal.schema.d.ts +0 -151
  216. package/components/modal.schema.d.ts.map +0 -1
  217. package/components/table.d.ts +0 -91
  218. package/components/table.d.ts.map +0 -1
  219. package/components/table.schema.d.ts +0 -123
  220. package/components/table.schema.d.ts.map +0 -1
  221. package/esm/bundler/index.mjs +0 -3136
  222. package/esm/layouts/index.mjs +0 -409
  223. package/esm/render/index.mjs +0 -45
  224. package/esm/renderers/index.mjs +0 -621
  225. package/esm/universal/index.mjs +0 -1946
  226. package/esm/web-components/index.mjs +0 -2023
  227. package/layouts/base.d.ts +0 -86
  228. package/layouts/base.d.ts.map +0 -1
  229. package/layouts/index.d.ts +0 -8
  230. package/layouts/index.d.ts.map +0 -1
  231. package/layouts/index.js +0 -437
  232. package/layouts/presets.d.ts +0 -134
  233. package/layouts/presets.d.ts.map +0 -1
  234. package/react/Alert.d.ts +0 -101
  235. package/react/Alert.d.ts.map +0 -1
  236. package/react/Badge.d.ts +0 -100
  237. package/react/Badge.d.ts.map +0 -1
  238. package/react/Button.d.ts +0 -108
  239. package/react/Button.d.ts.map +0 -1
  240. package/react/Card.d.ts +0 -103
  241. package/react/Card.d.ts.map +0 -1
  242. package/react/types.d.ts +0 -105
  243. package/react/types.d.ts.map +0 -1
  244. package/render/index.d.ts +0 -8
  245. package/render/index.d.ts.map +0 -1
  246. package/render/prerender.d.ts +0 -57
  247. package/render/prerender.d.ts.map +0 -1
  248. package/renderers/index.d.ts +0 -26
  249. package/renderers/index.d.ts.map +0 -1
  250. package/renderers/index.js +0 -666
  251. package/renderers/mdx.renderer.d.ts +0 -99
  252. package/renderers/mdx.renderer.d.ts.map +0 -1
  253. package/renderers/react.adapter.d.ts +0 -70
  254. package/renderers/react.adapter.d.ts.map +0 -1
  255. package/renderers/react.renderer.d.ts +0 -105
  256. package/renderers/react.renderer.d.ts.map +0 -1
  257. package/renderers/transpiler.d.ts +0 -49
  258. package/renderers/transpiler.d.ts.map +0 -1
  259. package/universal/UniversalApp.d.ts +0 -108
  260. package/universal/UniversalApp.d.ts.map +0 -1
  261. package/universal/cached-runtime.d.ts +0 -139
  262. package/universal/cached-runtime.d.ts.map +0 -1
  263. package/universal/context.d.ts +0 -122
  264. package/universal/context.d.ts.map +0 -1
  265. package/universal/index.d.ts +0 -57
  266. package/universal/index.d.ts.map +0 -1
  267. package/universal/index.js +0 -2032
  268. package/universal/renderers/html.renderer.d.ts +0 -36
  269. package/universal/renderers/html.renderer.d.ts.map +0 -1
  270. package/universal/renderers/index.d.ts +0 -112
  271. package/universal/renderers/index.d.ts.map +0 -1
  272. package/universal/renderers/markdown.renderer.d.ts +0 -33
  273. package/universal/renderers/markdown.renderer.d.ts.map +0 -1
  274. package/universal/renderers/mdx.renderer.d.ts +0 -38
  275. package/universal/renderers/mdx.renderer.d.ts.map +0 -1
  276. package/universal/renderers/react.renderer.d.ts +0 -46
  277. package/universal/renderers/react.renderer.d.ts.map +0 -1
  278. package/universal/runtime-builder.d.ts +0 -33
  279. package/universal/runtime-builder.d.ts.map +0 -1
  280. package/universal/store.d.ts +0 -135
  281. package/universal/store.d.ts.map +0 -1
  282. package/universal/types.d.ts +0 -199
  283. package/universal/types.d.ts.map +0 -1
  284. package/web-components/core/attribute-parser.d.ts +0 -82
  285. package/web-components/core/attribute-parser.d.ts.map +0 -1
  286. package/web-components/core/base-element.d.ts +0 -197
  287. package/web-components/core/base-element.d.ts.map +0 -1
  288. package/web-components/core/index.d.ts +0 -9
  289. package/web-components/core/index.d.ts.map +0 -1
  290. package/web-components/elements/fmcp-alert.d.ts +0 -46
  291. package/web-components/elements/fmcp-alert.d.ts.map +0 -1
  292. package/web-components/elements/fmcp-badge.d.ts +0 -47
  293. package/web-components/elements/fmcp-badge.d.ts.map +0 -1
  294. package/web-components/elements/fmcp-button.d.ts +0 -117
  295. package/web-components/elements/fmcp-button.d.ts.map +0 -1
  296. package/web-components/elements/fmcp-card.d.ts +0 -53
  297. package/web-components/elements/fmcp-card.d.ts.map +0 -1
  298. package/web-components/elements/fmcp-input.d.ts +0 -96
  299. package/web-components/elements/fmcp-input.d.ts.map +0 -1
  300. package/web-components/elements/fmcp-select.d.ts +0 -100
  301. package/web-components/elements/fmcp-select.d.ts.map +0 -1
  302. package/web-components/elements/index.d.ts +0 -13
  303. package/web-components/elements/index.d.ts.map +0 -1
  304. package/web-components/index.d.ts +0 -49
  305. package/web-components/index.d.ts.map +0 -1
  306. package/web-components/index.js +0 -2058
  307. package/web-components/register.d.ts +0 -57
  308. package/web-components/register.d.ts.map +0 -1
  309. package/web-components/types.d.ts +0 -122
  310. package/web-components/types.d.ts.map +0 -1
@@ -1,1946 +0,0 @@
1
- // libs/ui/src/universal/types.ts
2
- var DEFAULT_FRONTMCP_STATE = {
3
- toolName: null,
4
- input: null,
5
- output: null,
6
- content: null,
7
- structuredContent: null,
8
- loading: false,
9
- error: null
10
- };
11
- var UNIVERSAL_CDN = {
12
- esm: {
13
- reactMarkdown: "https://esm.sh/react-markdown@9",
14
- mdxReact: "https://esm.sh/@mdx-js/react@3",
15
- remarkGfm: "https://esm.sh/remark-gfm@4"
16
- }
17
- // Note: These libraries are not available on cdnjs
18
- // For Claude, we use inline implementations
19
- };
20
- function detectContentType(source) {
21
- if (typeof source === "function") {
22
- return "react";
23
- }
24
- if (typeof source !== "string") {
25
- return "html";
26
- }
27
- const hasModuleSyntax = /^import\s+/m.test(source) || /^export\s+(default\s+)?/m.test(source) || /^const\s+\w+\s*=\s*\([^)]*\)\s*=>/m.test(source) || // Arrow function components
28
- /^function\s+\w+\s*\(/m.test(source);
29
- const hasJsxTags = /<[A-Z][a-zA-Z]*/.test(source);
30
- const hasMarkdown = /^#{1,6}\s/m.test(source) || /^\*\s/m.test(source) || /^-\s/m.test(source) || /^\d+\.\s/m.test(source);
31
- if (hasModuleSyntax && hasJsxTags) {
32
- return "react";
33
- }
34
- if (hasJsxTags && hasMarkdown && !hasModuleSyntax) {
35
- return "mdx";
36
- }
37
- if (hasMarkdown || /\*\*[^*]+\*\*/.test(source) || /\[[^\]]+\]\([^)]+\)/.test(source)) {
38
- return "markdown";
39
- }
40
- if (hasJsxTags && !hasModuleSyntax) {
41
- return "mdx";
42
- }
43
- return "html";
44
- }
45
-
46
- // libs/ui/src/universal/store.ts
47
- import { useSyncExternalStore } from "react";
48
- function createFrontMCPStore(initialState) {
49
- let state = {
50
- ...DEFAULT_FRONTMCP_STATE,
51
- ...initialState
52
- };
53
- const listeners = /* @__PURE__ */ new Set();
54
- const getState = () => state;
55
- const getServerState = () => state;
56
- const setState = (partial) => {
57
- const hasChanged = Object.keys(partial).some(
58
- (key) => partial[key] !== state[key]
59
- );
60
- if (hasChanged) {
61
- state = { ...state, ...partial };
62
- listeners.forEach((listener) => listener());
63
- }
64
- };
65
- const subscribe = (listener) => {
66
- listeners.add(listener);
67
- return () => {
68
- listeners.delete(listener);
69
- };
70
- };
71
- const reset = () => {
72
- setState({
73
- ...DEFAULT_FRONTMCP_STATE,
74
- ...initialState
75
- });
76
- };
77
- return {
78
- getState,
79
- getServerState,
80
- setState,
81
- subscribe,
82
- reset
83
- };
84
- }
85
- var globalStore = null;
86
- function getGlobalStore() {
87
- if (!globalStore) {
88
- globalStore = createFrontMCPStore();
89
- }
90
- return globalStore;
91
- }
92
- function setGlobalStore(store) {
93
- globalStore = store;
94
- }
95
- function resetGlobalStore(initialState) {
96
- globalStore = createFrontMCPStore(initialState);
97
- }
98
- function useFrontMCPStore(store) {
99
- const targetStore = store ?? getGlobalStore();
100
- return useSyncExternalStore(targetStore.subscribe, targetStore.getState, targetStore.getServerState);
101
- }
102
- function useToolOutput(store) {
103
- const state = useFrontMCPStore(store);
104
- return state.output;
105
- }
106
- function useToolInput(store) {
107
- const state = useFrontMCPStore(store);
108
- return state.input;
109
- }
110
- function useContent(store) {
111
- const state = useFrontMCPStore(store);
112
- return state.content;
113
- }
114
- function useToolName(store) {
115
- const state = useFrontMCPStore(store);
116
- return state.toolName;
117
- }
118
- function useLoadingState(store) {
119
- const state = useFrontMCPStore(store);
120
- return { loading: state.loading, error: state.error };
121
- }
122
- function initializeStoreFromWindow(store) {
123
- const targetStore = store ?? getGlobalStore();
124
- const windowData = typeof window !== "undefined" ? window.__frontmcp : void 0;
125
- if (windowData?.context) {
126
- targetStore.setState({
127
- toolName: windowData.context.toolName ?? null,
128
- input: windowData.context.toolInput ?? null,
129
- output: windowData.context.toolOutput ?? null,
130
- structuredContent: windowData.context.structuredContent ?? null,
131
- loading: false,
132
- error: null
133
- });
134
- }
135
- }
136
- function createStoreSelector(selector) {
137
- return (store) => {
138
- const state = useFrontMCPStore(store);
139
- return selector(state);
140
- };
141
- }
142
-
143
- // libs/ui/src/universal/context.tsx
144
- import { createContext, useContext, useEffect, useMemo, useRef } from "react";
145
- import { jsx } from "react/jsx-runtime";
146
- var FrontMCPContext = createContext(null);
147
- var ComponentsContext = createContext({});
148
- function FrontMCPProvider({ store, initialState, children }) {
149
- const storeRef = useRef(null);
150
- if (!storeRef.current) {
151
- storeRef.current = store ?? createFrontMCPStore(initialState);
152
- }
153
- const actualStore = storeRef.current;
154
- useEffect(() => {
155
- if (typeof window !== "undefined" && !store) {
156
- initializeStoreFromWindow(actualStore);
157
- }
158
- }, [actualStore, store]);
159
- return /* @__PURE__ */ jsx(FrontMCPContext.Provider, { value: actualStore, children });
160
- }
161
- function ComponentsProvider({ components, children }) {
162
- const parentComponents = useContext(ComponentsContext);
163
- const mergedComponents = useMemo(
164
- () => ({
165
- ...parentComponents,
166
- ...components
167
- }),
168
- [parentComponents, components]
169
- );
170
- return /* @__PURE__ */ jsx(ComponentsContext.Provider, { value: mergedComponents, children });
171
- }
172
- function useFrontMCPContext() {
173
- const store = useContext(FrontMCPContext);
174
- if (!store) {
175
- throw new Error("useFrontMCPContext must be used within a FrontMCPProvider");
176
- }
177
- return store;
178
- }
179
- function useComponents() {
180
- return useContext(ComponentsContext);
181
- }
182
- function useFrontMCPContextSafe() {
183
- return useContext(FrontMCPContext);
184
- }
185
- function UniversalProvider({
186
- store,
187
- initialState,
188
- components = {},
189
- children
190
- }) {
191
- return /* @__PURE__ */ jsx(FrontMCPProvider, { store, initialState, children: /* @__PURE__ */ jsx(ComponentsProvider, { components, children }) });
192
- }
193
- function withFrontMCP(Component) {
194
- const WrappedComponent = (props) => {
195
- const store = useFrontMCPContext();
196
- const state = store.getState();
197
- return /* @__PURE__ */ jsx(Component, { ...props, state });
198
- };
199
- WrappedComponent.displayName = `withFrontMCP(${Component.displayName || Component.name || "Component"})`;
200
- return WrappedComponent;
201
- }
202
-
203
- // libs/ui/src/universal/renderers/html.renderer.ts
204
- import React2 from "react";
205
- import { sanitizeHtmlContent } from "@frontmcp/uipack/runtime";
206
- var sanitizeHtml = sanitizeHtmlContent;
207
- var htmlRenderer = {
208
- type: "html",
209
- priority: 0,
210
- // Lowest priority (fallback)
211
- canHandle(content) {
212
- return content.type === "html" || typeof content.source === "string";
213
- },
214
- render(content, _context) {
215
- const source = content.source;
216
- if (typeof source !== "string") {
217
- return React2.createElement("div", { className: "frontmcp-error" }, "HTML renderer requires a string source");
218
- }
219
- return React2.createElement("div", {
220
- className: "frontmcp-html-content",
221
- dangerouslySetInnerHTML: { __html: source }
222
- });
223
- }
224
- };
225
- var safeHtmlRenderer = {
226
- type: "html",
227
- priority: 0,
228
- canHandle(content) {
229
- return content.type === "html" && typeof content.source === "string";
230
- },
231
- render(content, _context) {
232
- const source = content.source;
233
- if (typeof source !== "string") {
234
- return React2.createElement("div", { className: "frontmcp-error" }, "HTML renderer requires a string source");
235
- }
236
- const sanitized = sanitizeHtml(source);
237
- return React2.createElement("div", {
238
- className: "frontmcp-html-content",
239
- dangerouslySetInnerHTML: { __html: sanitized }
240
- });
241
- }
242
- };
243
-
244
- // libs/ui/src/universal/renderers/markdown.renderer.ts
245
- import React3 from "react";
246
- import { escapeHtml } from "@frontmcp/uipack/utils";
247
- function isSafeUrl(url) {
248
- if (!url) return false;
249
- const lower = url.toLowerCase().trim();
250
- return lower.startsWith("http://") || lower.startsWith("https://") || lower.startsWith("/") || lower.startsWith("#") || lower.startsWith("mailto:") || lower.startsWith("tel:");
251
- }
252
- function getReactMarkdown() {
253
- if (typeof window !== "undefined" && window.ReactMarkdown) {
254
- return window.ReactMarkdown;
255
- }
256
- return null;
257
- }
258
- function parseMarkdownToHtml(markdown) {
259
- let html = escapeHtml(markdown);
260
- html = html.replace(/^######\s+(.*)$/gm, "<h6>$1</h6>");
261
- html = html.replace(/^#####\s+(.*)$/gm, "<h5>$1</h5>");
262
- html = html.replace(/^####\s+(.*)$/gm, "<h4>$1</h4>");
263
- html = html.replace(/^###\s+(.*)$/gm, "<h3>$1</h3>");
264
- html = html.replace(/^##\s+(.*)$/gm, "<h2>$1</h2>");
265
- html = html.replace(/^#\s+(.*)$/gm, "<h1>$1</h1>");
266
- html = html.replace(/\*\*\*(.+?)\*\*\*/g, "<strong><em>$1</em></strong>");
267
- html = html.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
268
- html = html.replace(/\*(.+?)\*/g, "<em>$1</em>");
269
- html = html.replace(/___(.+?)___/g, "<strong><em>$1</em></strong>");
270
- html = html.replace(/__(.+?)__/g, "<strong>$1</strong>");
271
- html = html.replace(/_(.+?)_/g, "<em>$1</em>");
272
- html = html.replace(/`([^`]+)`/g, "<code>$1</code>");
273
- html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_match, text, url) => {
274
- return isSafeUrl(url) ? `<a href="${url}">${text}</a>` : text;
275
- });
276
- html = html.replace(/^[-*]\s+(.*)$/gm, "<li>$1</li>");
277
- html = html.replace(/(<li>.*<\/li>\n?)+/g, "<ul>$&</ul>");
278
- html = html.replace(/^\d+\.\s+(.*)$/gm, "<li>$1</li>");
279
- html = html.replace(/\n\n+/g, "</p><p>");
280
- html = "<p>" + html + "</p>";
281
- html = html.replace(/<p>\s*<\/p>/g, "");
282
- html = html.replace(/<p>\s*(<h[1-6]>)/g, "$1");
283
- html = html.replace(/(<\/h[1-6]>)\s*<\/p>/g, "$1");
284
- html = html.replace(/<p>\s*(<ul>)/g, "$1");
285
- html = html.replace(/(<\/ul>)\s*<\/p>/g, "$1");
286
- return html;
287
- }
288
- var markdownRenderer = {
289
- type: "markdown",
290
- priority: 10,
291
- // Medium priority
292
- canHandle(content) {
293
- if (content.type === "markdown") {
294
- return true;
295
- }
296
- if (typeof content.source === "string") {
297
- const source = content.source;
298
- const hasMarkdown = /^#{1,6}\s/m.test(source) || // Headers
299
- /^\*\s/m.test(source) || // Unordered list
300
- /^-\s/m.test(source) || // Unordered list
301
- /^\d+\.\s/m.test(source) || // Ordered list
302
- /\*\*[^*]+\*\*/.test(source) || // Bold
303
- /\[[^\]]+\]\([^)]+\)/.test(source);
304
- const hasJsx = /<[A-Z][a-zA-Z]*/.test(source);
305
- return hasMarkdown && !hasJsx;
306
- }
307
- return false;
308
- },
309
- render(content, context) {
310
- const source = content.source;
311
- if (typeof source !== "string") {
312
- return React3.createElement("div", { className: "frontmcp-error" }, "Markdown renderer requires a string source");
313
- }
314
- const ReactMarkdown = getReactMarkdown();
315
- if (ReactMarkdown) {
316
- const components = {
317
- ...context.components,
318
- ...content.components
319
- };
320
- return React3.createElement(ReactMarkdown, { components }, source);
321
- }
322
- const html = parseMarkdownToHtml(source);
323
- return React3.createElement("div", {
324
- className: "frontmcp-markdown-content prose",
325
- dangerouslySetInnerHTML: { __html: html }
326
- });
327
- }
328
- };
329
- function createMarkdownRenderer(defaultComponents) {
330
- return {
331
- ...markdownRenderer,
332
- render(content, context) {
333
- const enhancedContext = {
334
- ...context,
335
- components: {
336
- ...defaultComponents,
337
- ...context.components
338
- }
339
- };
340
- return markdownRenderer.render(content, enhancedContext);
341
- }
342
- };
343
- }
344
-
345
- // libs/ui/src/universal/renderers/react.renderer.ts
346
- import React4 from "react";
347
- var reactRenderer = {
348
- type: "react",
349
- priority: 30,
350
- // Highest priority for function components
351
- canHandle(content) {
352
- return content.type === "react" || typeof content.source === "function";
353
- },
354
- render(content, context) {
355
- const Component = content.source;
356
- if (typeof Component !== "function") {
357
- return React4.createElement("div", {
358
- className: "frontmcp-error",
359
- children: "React renderer requires a component function"
360
- });
361
- }
362
- const props = {
363
- // Default props from context
364
- output: context.output,
365
- input: context.input,
366
- state: context.state,
367
- // Override with content-specific props
368
- ...content.props
369
- };
370
- return React4.createElement(Component, props);
371
- }
372
- };
373
- function isReactComponent(value) {
374
- if (typeof value !== "function") {
375
- return false;
376
- }
377
- const fn = value;
378
- const typeofSymbol = fn.$$typeof;
379
- if (typeofSymbol) {
380
- const symbolString = typeofSymbol.toString();
381
- return symbolString.includes("react.memo") || symbolString.includes("react.forward_ref") || symbolString.includes("react.lazy");
382
- }
383
- if (fn.prototype?.isReactComponent) {
384
- return true;
385
- }
386
- if (fn.name && /^[A-Z]/.test(fn.name)) {
387
- return true;
388
- }
389
- return false;
390
- }
391
-
392
- // libs/ui/src/universal/renderers/mdx.renderer.ts
393
- import React5 from "react";
394
- import { escapeHtml as escapeHtml2 } from "@frontmcp/uipack/utils";
395
- function getMDXRuntime() {
396
- if (typeof window !== "undefined" && window.MDXProvider) {
397
- return { MDXProvider: window.MDXProvider };
398
- }
399
- return null;
400
- }
401
- function containsMdxSyntax(source) {
402
- if (/<[A-Z][a-zA-Z]*/.test(source)) {
403
- return true;
404
- }
405
- if (/\{[^}"']+\}/.test(source)) {
406
- return true;
407
- }
408
- if (/^(import|export)\s/m.test(source)) {
409
- return true;
410
- }
411
- if (/\s(className|onClick|onChange)=/.test(source)) {
412
- return true;
413
- }
414
- return false;
415
- }
416
- var mdxRenderer = {
417
- type: "mdx",
418
- priority: 20,
419
- // Higher than markdown, lower than React
420
- canHandle(content) {
421
- if (content.type === "mdx") {
422
- return true;
423
- }
424
- if (typeof content.source === "string") {
425
- const source = content.source;
426
- const hasMarkdown = /^#{1,6}\s/m.test(source) || /^\*\s/m.test(source) || /^-\s/m.test(source) || /^\d+\.\s/m.test(source);
427
- return hasMarkdown && containsMdxSyntax(source);
428
- }
429
- return false;
430
- },
431
- render(content, context) {
432
- const source = content.source;
433
- if (typeof source !== "string") {
434
- return React5.createElement("div", { className: "frontmcp-error" }, "MDX renderer requires a string source");
435
- }
436
- const mdxRuntime = getMDXRuntime();
437
- if (!mdxRuntime) {
438
- console.warn("[FrontMCP] MDX runtime not available. Content will be displayed as-is.");
439
- const escapedContent = escapeHtml2(source).replace(/\n/g, "<br>");
440
- return React5.createElement(
441
- "div",
442
- { className: "frontmcp-mdx-fallback" },
443
- React5.createElement(
444
- "div",
445
- {
446
- key: "warning",
447
- className: "frontmcp-warning bg-yellow-50 border border-yellow-200 rounded p-2 mb-4 text-sm text-yellow-800"
448
- },
449
- "MDX rendering is not available on this platform. Content is shown as raw text."
450
- ),
451
- React5.createElement("pre", {
452
- key: "content",
453
- className: "bg-gray-100 p-4 rounded overflow-auto",
454
- dangerouslySetInnerHTML: { __html: escapedContent }
455
- })
456
- );
457
- }
458
- const components = {
459
- ...context.components,
460
- ...content.components
461
- };
462
- if (typeof content.compiledContent === "function") {
463
- const CompiledContent = content.compiledContent;
464
- return React5.createElement(
465
- mdxRuntime.MDXProvider,
466
- { components },
467
- React5.createElement(CompiledContent, {
468
- output: context.output,
469
- input: context.input
470
- })
471
- );
472
- }
473
- console.warn("[FrontMCP] MDX content needs to be pre-compiled. Raw MDX string rendering is not supported.");
474
- return React5.createElement(
475
- "div",
476
- { className: "frontmcp-mdx-uncompiled" },
477
- React5.createElement("pre", { className: "bg-gray-100 p-4 rounded overflow-auto text-sm" }, source)
478
- );
479
- }
480
- };
481
- function isMdxSupported() {
482
- return getMDXRuntime() !== null;
483
- }
484
- function createMdxRenderer(defaultComponents) {
485
- return {
486
- ...mdxRenderer,
487
- render(content, context) {
488
- const enhancedContext = {
489
- ...context,
490
- components: {
491
- ...defaultComponents,
492
- ...context.components
493
- }
494
- };
495
- return mdxRenderer.render(content, enhancedContext);
496
- }
497
- };
498
- }
499
-
500
- // libs/ui/src/universal/renderers/index.ts
501
- var RendererRegistry = class {
502
- renderers = /* @__PURE__ */ new Map();
503
- sortedRenderers = [];
504
- constructor() {
505
- this.register(htmlRenderer);
506
- this.register(markdownRenderer);
507
- this.register(reactRenderer);
508
- this.register(mdxRenderer);
509
- }
510
- /**
511
- * Register a renderer.
512
- * Renderers are sorted by priority (highest first) for auto-detection.
513
- */
514
- register(renderer) {
515
- this.renderers.set(renderer.type, renderer);
516
- this.updateSortedList();
517
- }
518
- /**
519
- * Unregister a renderer by type.
520
- */
521
- unregister(type) {
522
- const removed = this.renderers.delete(type);
523
- if (removed) {
524
- this.updateSortedList();
525
- }
526
- return removed;
527
- }
528
- /**
529
- * Get a renderer by type.
530
- */
531
- get(type) {
532
- return this.renderers.get(type);
533
- }
534
- /**
535
- * Check if a renderer type is registered.
536
- */
537
- has(type) {
538
- return this.renderers.has(type);
539
- }
540
- /**
541
- * Get all registered renderer types.
542
- */
543
- getTypes() {
544
- return Array.from(this.renderers.keys());
545
- }
546
- /**
547
- * Auto-detect the best renderer for content.
548
- *
549
- * Checks renderers in priority order (highest first).
550
- * Falls back to HTML renderer if no match.
551
- */
552
- detect(content) {
553
- if (content.type) {
554
- const explicit = this.renderers.get(content.type);
555
- if (explicit) {
556
- return explicit;
557
- }
558
- }
559
- for (const renderer of this.sortedRenderers) {
560
- if (renderer.canHandle(content)) {
561
- return renderer;
562
- }
563
- }
564
- return htmlRenderer;
565
- }
566
- /**
567
- * Render content using auto-detection.
568
- */
569
- render(content, context) {
570
- const renderer = this.detect(content);
571
- return renderer.render(content, context);
572
- }
573
- /**
574
- * Render with a specific renderer type.
575
- */
576
- renderWith(type, content, context) {
577
- const renderer = this.renderers.get(type);
578
- if (!renderer) {
579
- throw new Error(`Renderer '${type}' not registered`);
580
- }
581
- return renderer.render(content, context);
582
- }
583
- /**
584
- * Get registry statistics.
585
- */
586
- getStats() {
587
- return {
588
- registeredTypes: this.getTypes(),
589
- priorityOrder: this.sortedRenderers.map((r) => ({
590
- type: r.type,
591
- priority: r.priority
592
- }))
593
- };
594
- }
595
- /**
596
- * Update the sorted renderer list by priority.
597
- */
598
- updateSortedList() {
599
- this.sortedRenderers = Array.from(this.renderers.values()).sort((a, b) => b.priority - a.priority);
600
- }
601
- };
602
- var rendererRegistry = new RendererRegistry();
603
- function detectRenderer(content) {
604
- return rendererRegistry.detect(content);
605
- }
606
- function renderContent(content, context) {
607
- return rendererRegistry.render(content, context);
608
- }
609
- function createContent(source, options) {
610
- return {
611
- type: options?.type ?? detectContentType(source),
612
- source,
613
- props: options?.props,
614
- components: options?.components
615
- };
616
- }
617
-
618
- // libs/ui/src/universal/UniversalApp.tsx
619
- import React6 from "react";
620
- import { escapeHtml as escapeHtml3 } from "@frontmcp/uipack/utils";
621
- function LoadingSpinner() {
622
- return React6.createElement(
623
- "div",
624
- { className: "frontmcp-loading flex items-center justify-center min-h-[200px]" },
625
- React6.createElement("div", {
626
- className: "frontmcp-spinner w-6 h-6 border-2 border-gray-200 border-t-blue-500 rounded-full animate-spin"
627
- })
628
- );
629
- }
630
- function ErrorDisplay({ error }) {
631
- return React6.createElement(
632
- "div",
633
- {
634
- className: "frontmcp-error bg-red-50 border border-red-200 rounded-lg p-4 text-red-800"
635
- },
636
- [
637
- React6.createElement("div", { key: "title", className: "font-medium" }, "Error"),
638
- React6.createElement("div", { key: "message", className: "text-sm mt-1" }, escapeHtml3(error))
639
- ]
640
- );
641
- }
642
- function EmptyState() {
643
- return React6.createElement(
644
- "div",
645
- {
646
- className: "frontmcp-empty text-gray-500 text-center py-8"
647
- },
648
- "No content to display"
649
- );
650
- }
651
- function UniversalRenderer({
652
- content,
653
- state
654
- }) {
655
- const components = useComponents();
656
- const context = {
657
- output: state.output,
658
- input: state.input,
659
- components: {
660
- ...components,
661
- ...content.components
662
- },
663
- state
664
- };
665
- const rendered = renderContent(content, context);
666
- return React6.createElement("div", { className: "frontmcp-content" }, rendered);
667
- }
668
- function UniversalApp({
669
- content: contentOverride,
670
- components,
671
- fallback,
672
- errorFallback: ErrorFallback = ErrorDisplay
673
- }) {
674
- const state = useFrontMCPStore();
675
- if (state.loading) {
676
- return fallback ? React6.createElement(React6.Fragment, null, fallback) : React6.createElement(LoadingSpinner, null);
677
- }
678
- if (state.error) {
679
- return React6.createElement(ErrorFallback, { error: state.error });
680
- }
681
- const content = contentOverride ?? state.content;
682
- if (!content) {
683
- return React6.createElement(EmptyState, null);
684
- }
685
- if (components) {
686
- return React6.createElement(
687
- ComponentsProvider,
688
- { components },
689
- React6.createElement(UniversalRenderer, { content, state })
690
- );
691
- }
692
- return React6.createElement(UniversalRenderer, { content, state });
693
- }
694
- function UniversalAppWithProvider({
695
- initialState,
696
- components,
697
- ...appProps
698
- }) {
699
- return React6.createElement(
700
- FrontMCPProvider,
701
- { initialState },
702
- components ? React6.createElement(ComponentsProvider, { components }, React6.createElement(UniversalApp, appProps)) : React6.createElement(UniversalApp, appProps)
703
- );
704
- }
705
-
706
- // libs/ui/src/universal/runtime-builder.ts
707
- function buildStoreRuntime() {
708
- return `
709
- // FrontMCP Store
710
- (function() {
711
- var state = {
712
- toolName: null,
713
- input: null,
714
- output: null,
715
- content: null,
716
- structuredContent: null,
717
- loading: false,
718
- error: null
719
- };
720
-
721
- var listeners = new Set();
722
-
723
- window.__frontmcp = {
724
- // Store methods
725
- getState: function() { return state; },
726
- setState: function(partial) {
727
- state = Object.assign({}, state, partial);
728
- listeners.forEach(function(fn) { fn(); });
729
- },
730
- subscribe: function(fn) {
731
- listeners.add(fn);
732
- return function() { listeners.delete(fn); };
733
- },
734
- reset: function() {
735
- state = {
736
- toolName: null,
737
- input: null,
738
- output: null,
739
- content: null,
740
- structuredContent: null,
741
- loading: false,
742
- error: null
743
- };
744
- },
745
-
746
- // Context (legacy support)
747
- context: state,
748
- setContext: function(ctx) {
749
- this.setState(ctx);
750
- }
751
- };
752
-
753
- // Hooks for React components
754
- window.useFrontMCPStore = function() {
755
- var store = window.__frontmcp;
756
- return React.useSyncExternalStore(
757
- store.subscribe,
758
- store.getState,
759
- store.getState
760
- );
761
- };
762
-
763
- window.useToolOutput = function() {
764
- return window.useFrontMCPStore().output;
765
- };
766
-
767
- window.useToolInput = function() {
768
- return window.useFrontMCPStore().input;
769
- };
770
-
771
- window.useContent = function() {
772
- return window.useFrontMCPStore().content;
773
- };
774
- })();
775
- `;
776
- }
777
- function buildInlineMarkdownParser(options) {
778
- const allowUnsafeLinks = options?.contentSecurity?.bypassSanitization || options?.contentSecurity?.allowUnsafeLinks;
779
- return `
780
- // Inline Markdown Parser
781
- (function() {
782
- // XSS protection settings (configured at build time)
783
- var __allowUnsafeLinks = ${allowUnsafeLinks ? "true" : "false"};
784
-
785
- // URL scheme validation to prevent XSS via javascript: URLs
786
- function isSafeUrl(url) {
787
- // If unsafe links are allowed, all URLs are considered safe
788
- if (__allowUnsafeLinks) return true;
789
- if (!url) return false;
790
- var lower = url.toLowerCase().trim();
791
- return lower.startsWith('http://') ||
792
- lower.startsWith('https://') ||
793
- lower.startsWith('/') ||
794
- lower.startsWith('#') ||
795
- lower.startsWith('mailto:');
796
- }
797
-
798
- function parseMarkdown(md) {
799
- var html = md;
800
- // Escape HTML
801
- html = html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
802
- // Headers
803
- html = html.replace(/^######\\s+(.*)$/gm, '<h6>$1</h6>');
804
- html = html.replace(/^#####\\s+(.*)$/gm, '<h5>$1</h5>');
805
- html = html.replace(/^####\\s+(.*)$/gm, '<h4>$1</h4>');
806
- html = html.replace(/^###\\s+(.*)$/gm, '<h3>$1</h3>');
807
- html = html.replace(/^##\\s+(.*)$/gm, '<h2>$1</h2>');
808
- html = html.replace(/^#\\s+(.*)$/gm, '<h1>$1</h1>');
809
- // Bold and italic
810
- html = html.replace(/\\*\\*\\*(.+?)\\*\\*\\*/g, '<strong><em>$1</em></strong>');
811
- html = html.replace(/\\*\\*(.+?)\\*\\*/g, '<strong>$1</strong>');
812
- html = html.replace(/\\*(.+?)\\*/g, '<em>$1</em>');
813
- // Inline code
814
- html = html.replace(/\`([^\`]+)\`/g, '<code>$1</code>');
815
- // Links - validate URL scheme to prevent XSS (unless bypassed)
816
- html = html.replace(/\\[([^\\]]+)\\]\\(([^)]+)\\)/g, function(match, text, url) {
817
- return isSafeUrl(url) ? '<a href="' + url + '">' + text + '</a>' : text;
818
- });
819
- // Lists
820
- html = html.replace(/^[-*]\\s+(.*)$/gm, '<li>$1</li>');
821
- // Paragraphs
822
- html = html.replace(/\\n\\n+/g, '</p><p>');
823
- html = '<p>' + html + '</p>';
824
- return html;
825
- }
826
-
827
- window.__frontmcp.parseMarkdown = parseMarkdown;
828
-
829
- // Simple ReactMarkdown replacement
830
- window.ReactMarkdown = function(props) {
831
- var html = parseMarkdown(props.children || '');
832
- return React.createElement('div', {
833
- className: 'frontmcp-markdown prose',
834
- dangerouslySetInnerHTML: { __html: html }
835
- });
836
- };
837
- })();
838
- `;
839
- }
840
- function buildRenderersRuntime(options) {
841
- const bypassSanitization = options?.contentSecurity?.bypassSanitization;
842
- const allowInlineScripts = bypassSanitization || options?.contentSecurity?.allowInlineScripts;
843
- return `
844
- // Universal Renderers
845
- (function() {
846
- var renderers = {};
847
-
848
- // XSS protection settings (configured at build time)
849
- var __allowInlineScripts = ${allowInlineScripts ? "true" : "false"};
850
-
851
- // HTML Renderer
852
- renderers.html = {
853
- type: 'html',
854
- priority: 0,
855
- canHandle: function(c) { return c.type === 'html'; },
856
- render: function(c, ctx) {
857
- var html = c.source;
858
- // Apply XSS protection unless bypassed
859
- if (!__allowInlineScripts) {
860
- // Remove script tags and event handlers
861
- html = html.replace(/<script[^>]*>[\\s\\S]*?<\\/script>/gi, '');
862
- html = html.replace(/\\s+on\\w+\\s*=/gi, ' data-removed-handler=');
863
- }
864
- return React.createElement('div', {
865
- className: 'frontmcp-html-content',
866
- dangerouslySetInnerHTML: { __html: html }
867
- });
868
- }
869
- };
870
-
871
- // Markdown Renderer
872
- renderers.markdown = {
873
- type: 'markdown',
874
- priority: 10,
875
- canHandle: function(c) {
876
- if (c.type === 'markdown') return true;
877
- if (typeof c.source !== 'string') return false;
878
- var s = c.source;
879
- return /^#{1,6}\\s/m.test(s) || /^[-*]\\s/m.test(s) || /\\*\\*[^*]+\\*\\*/.test(s);
880
- },
881
- render: function(c, ctx) {
882
- if (window.ReactMarkdown) {
883
- return React.createElement(window.ReactMarkdown, {
884
- children: c.source,
885
- components: Object.assign({}, ctx.components, c.components)
886
- });
887
- }
888
- // Fallback to inline parser
889
- var html = window.__frontmcp.parseMarkdown ? window.__frontmcp.parseMarkdown(c.source) : c.source;
890
- return React.createElement('div', {
891
- className: 'frontmcp-markdown prose',
892
- dangerouslySetInnerHTML: { __html: html }
893
- });
894
- }
895
- };
896
-
897
- // React Renderer
898
- renderers.react = {
899
- type: 'react',
900
- priority: 30,
901
- canHandle: function(c) { return c.type === 'react' || typeof c.source === 'function'; },
902
- render: function(c, ctx) {
903
- var Component = c.source;
904
- var props = Object.assign({
905
- output: ctx.output,
906
- input: ctx.input,
907
- state: ctx.state
908
- }, c.props);
909
- return React.createElement(Component, props);
910
- }
911
- };
912
-
913
- // MDX Renderer
914
- renderers.mdx = {
915
- type: 'mdx',
916
- priority: 20,
917
- canHandle: function(c) {
918
- if (c.type === 'mdx') return true;
919
- if (typeof c.source !== 'string') return false;
920
- var s = c.source;
921
- return /<[A-Z][a-zA-Z]*/.test(s) && /^#{1,6}\\s/m.test(s);
922
- },
923
- render: function(c, ctx) {
924
- // MDX requires pre-compilation, fallback to showing source
925
- if (typeof c.compiledContent === 'function') {
926
- var MDXContent = c.compiledContent;
927
- return React.createElement(MDXContent, {
928
- output: ctx.output,
929
- input: ctx.input,
930
- components: Object.assign({}, ctx.components, c.components)
931
- });
932
- }
933
- // Show warning
934
- return React.createElement('div', { className: 'frontmcp-mdx-fallback' }, [
935
- React.createElement('div', {
936
- key: 'warn',
937
- className: 'bg-yellow-50 border border-yellow-200 rounded p-2 mb-2 text-sm text-yellow-800'
938
- }, 'MDX requires pre-compilation. Showing raw content.'),
939
- React.createElement('pre', {
940
- key: 'pre',
941
- className: 'bg-gray-100 p-4 rounded text-sm overflow-auto'
942
- }, c.source)
943
- ]);
944
- }
945
- };
946
-
947
- // Sorted by priority
948
- var sortedRenderers = [renderers.react, renderers.mdx, renderers.markdown, renderers.html];
949
-
950
- // Detect renderer
951
- window.__frontmcp.detectRenderer = function(content) {
952
- if (content.type && renderers[content.type]) {
953
- return renderers[content.type];
954
- }
955
- for (var i = 0; i < sortedRenderers.length; i++) {
956
- if (sortedRenderers[i].canHandle(content)) {
957
- return sortedRenderers[i];
958
- }
959
- }
960
- return renderers.html;
961
- };
962
-
963
- // Render content
964
- window.__frontmcp.renderContent = function(content, context) {
965
- var renderer = window.__frontmcp.detectRenderer(content);
966
- return renderer.render(content, context);
967
- };
968
-
969
- window.__frontmcp.renderers = renderers;
970
- })();
971
- `;
972
- }
973
- function buildUniversalAppRuntime() {
974
- return `
975
- // Universal App Component
976
- (function() {
977
- var LoadingSpinner = function() {
978
- return React.createElement('div', {
979
- className: 'frontmcp-loading flex items-center justify-center min-h-[200px]'
980
- }, React.createElement('div', {
981
- className: 'frontmcp-spinner w-6 h-6 border-2 border-gray-200 border-t-blue-500 rounded-full animate-spin'
982
- }));
983
- };
984
-
985
- var ErrorDisplay = function(props) {
986
- return React.createElement('div', {
987
- className: 'frontmcp-error bg-red-50 border border-red-200 rounded-lg p-4 text-red-800'
988
- }, [
989
- React.createElement('div', { key: 'title', className: 'font-medium' }, 'Error'),
990
- React.createElement('div', { key: 'msg', className: 'text-sm mt-1' }, props.error)
991
- ]);
992
- };
993
-
994
- var EmptyState = function() {
995
- return React.createElement('div', {
996
- className: 'frontmcp-empty text-gray-500 text-center py-8'
997
- }, 'No content to display');
998
- };
999
-
1000
- window.__frontmcp.UniversalApp = function(props) {
1001
- var state = window.useFrontMCPStore();
1002
-
1003
- if (state.loading) {
1004
- return props.fallback || React.createElement(LoadingSpinner);
1005
- }
1006
-
1007
- if (state.error) {
1008
- var ErrorComp = props.errorFallback || ErrorDisplay;
1009
- return React.createElement(ErrorComp, { error: state.error });
1010
- }
1011
-
1012
- var content = props.content || state.content;
1013
-
1014
- if (!content) {
1015
- return React.createElement(EmptyState);
1016
- }
1017
-
1018
- var context = {
1019
- output: state.output,
1020
- input: state.input,
1021
- state: state,
1022
- components: props.components || {}
1023
- };
1024
-
1025
- var rendered = window.__frontmcp.renderContent(content, context);
1026
- return React.createElement('div', { className: 'frontmcp-content' }, rendered);
1027
- };
1028
-
1029
- window.__frontmcp.LoadingSpinner = LoadingSpinner;
1030
- window.__frontmcp.ErrorDisplay = ErrorDisplay;
1031
- window.__frontmcp.EmptyState = EmptyState;
1032
- })();
1033
- `;
1034
- }
1035
- function buildCdnImports(options) {
1036
- const parts = [];
1037
- if (options.cdnType === "esm") {
1038
- if (options.includeMarkdown) {
1039
- parts.push(`
1040
- <script type="module">
1041
- import ReactMarkdown from '${UNIVERSAL_CDN.esm.reactMarkdown}';
1042
- window.ReactMarkdown = ReactMarkdown;
1043
- </script>`);
1044
- }
1045
- if (options.includeMdx) {
1046
- parts.push(`
1047
- <script type="module">
1048
- import { MDXProvider } from '${UNIVERSAL_CDN.esm.mdxReact}';
1049
- window.MDXProvider = MDXProvider;
1050
- </script>`);
1051
- }
1052
- }
1053
- return parts.join("\n");
1054
- }
1055
- function buildUniversalRuntime(options) {
1056
- const parts = [];
1057
- parts.push(buildStoreRuntime());
1058
- if (options.cdnType === "umd" || options.includeMarkdown) {
1059
- parts.push(buildInlineMarkdownParser(options));
1060
- }
1061
- parts.push(buildRenderersRuntime(options));
1062
- parts.push(buildUniversalAppRuntime());
1063
- if (options.customComponents) {
1064
- parts.push(`
1065
- // Custom Components
1066
- (function() {
1067
- ${options.customComponents}
1068
- })();
1069
- `);
1070
- }
1071
- let script = parts.join("\n");
1072
- if (options.minify) {
1073
- script = script.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/[^\n]*$/gm, "").replace(/\n\s*\n/g, "\n").replace(/^\s+/gm, "").trim();
1074
- }
1075
- return {
1076
- script,
1077
- cdnImports: buildCdnImports(options),
1078
- size: script.length
1079
- };
1080
- }
1081
- function buildMinimalRuntime(options) {
1082
- let script = buildStoreRuntime();
1083
- if (options.minify) {
1084
- script = script.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/[^\n]*$/gm, "").replace(/\n\s*\n/g, "\n").replace(/^\s+/gm, "").trim();
1085
- }
1086
- return script;
1087
- }
1088
-
1089
- // libs/ui/src/universal/cached-runtime.ts
1090
- import { getMCPBridgeScript } from "@frontmcp/uipack/runtime";
1091
- import { buildUIComponentsRuntime as buildBrowserUIComponents } from "@frontmcp/uipack/build";
1092
- var RUNTIME_PLACEHOLDERS = {
1093
- /** Placeholder for transpiled component code */
1094
- COMPONENT_CODE: "/*__FRONTMCP_COMPONENT_CODE__*/",
1095
- /** Placeholder for data injection */
1096
- DATA_INJECTION: "/*__FRONTMCP_DATA_INJECTION__*/",
1097
- /** Placeholder for custom components */
1098
- CUSTOM_COMPONENTS: "/*__FRONTMCP_CUSTOM_COMPONENTS__*/"
1099
- };
1100
- var runtimeCache = /* @__PURE__ */ new Map();
1101
- var DEFAULT_CACHE_CONFIG = {
1102
- maxEntries: 10,
1103
- ttl: 0
1104
- // Forever by default
1105
- };
1106
- function generateCacheKey(options) {
1107
- const securityKey = options.contentSecurity ? [
1108
- options.contentSecurity.allowUnsafeLinks ? "unsafeLinks" : "",
1109
- options.contentSecurity.allowInlineScripts ? "unsafeScripts" : "",
1110
- options.contentSecurity.bypassSanitization ? "bypass" : ""
1111
- ].filter(Boolean).join("+") || "secure" : "secure";
1112
- return [
1113
- options.cdnType,
1114
- options.includeMarkdown ? "md" : "",
1115
- options.includeMdx ? "mdx" : "",
1116
- options.minify ? "min" : "",
1117
- options.includeBridge ? "bridge" : "",
1118
- securityKey
1119
- ].filter(Boolean).join(":");
1120
- }
1121
- function buildStoreRuntime2() {
1122
- return `
1123
- // FrontMCP Store (Vendor)
1124
- (function() {
1125
- var state = {
1126
- toolName: null,
1127
- input: null,
1128
- output: null,
1129
- content: null,
1130
- structuredContent: null,
1131
- loading: false,
1132
- error: null
1133
- };
1134
-
1135
- var listeners = new Set();
1136
-
1137
- window.__frontmcp = {
1138
- getState: function() { return state; },
1139
- setState: function(partial) {
1140
- state = Object.assign({}, state, partial);
1141
- listeners.forEach(function(fn) { fn(); });
1142
- },
1143
- subscribe: function(fn) {
1144
- listeners.add(fn);
1145
- return function() { listeners.delete(fn); };
1146
- },
1147
- reset: function() {
1148
- state = {
1149
- toolName: null,
1150
- input: null,
1151
- output: null,
1152
- content: null,
1153
- structuredContent: null,
1154
- loading: false,
1155
- error: null
1156
- };
1157
- },
1158
- context: state,
1159
- setContext: function(ctx) {
1160
- this.setState(ctx);
1161
- },
1162
- // Dynamic mode: update output and re-render
1163
- updateOutput: function(output) {
1164
- this.setState({ output: output, loading: false });
1165
- // Also update the global window variable for compatibility
1166
- window.__mcpToolOutput = output;
1167
- },
1168
- // Dynamic mode: update input and re-render
1169
- updateInput: function(input) {
1170
- this.setState({ input: input });
1171
- window.__mcpToolInput = input;
1172
- }
1173
- };
1174
-
1175
- // React hooks
1176
- window.useFrontMCPStore = function() {
1177
- var store = window.__frontmcp;
1178
- return React.useSyncExternalStore(
1179
- store.subscribe,
1180
- store.getState,
1181
- store.getState
1182
- );
1183
- };
1184
-
1185
- window.useToolOutput = function() {
1186
- return window.useFrontMCPStore().output;
1187
- };
1188
-
1189
- window.useToolInput = function() {
1190
- return window.useFrontMCPStore().input;
1191
- };
1192
-
1193
- window.useContent = function() {
1194
- return window.useFrontMCPStore().content;
1195
- };
1196
-
1197
- // Connect to MCP Bridge for platform data detection
1198
- function initFromBridge() {
1199
- // Check for data from mcpBridge (handles OpenAI, ext-apps, etc.)
1200
- if (window.mcpBridge && window.mcpBridge.toolOutput != null) {
1201
- window.__frontmcp.setState({
1202
- output: window.mcpBridge.toolOutput,
1203
- loading: false
1204
- });
1205
- }
1206
-
1207
- // Subscribe to bridge updates via onToolResult
1208
- if (window.mcpBridge && window.mcpBridge.onToolResult) {
1209
- window.mcpBridge.onToolResult(function(result) {
1210
- window.__frontmcp.updateOutput(result);
1211
- });
1212
- }
1213
- }
1214
-
1215
- // Initialize from bridge when ready
1216
- if (window.mcpBridge) {
1217
- initFromBridge();
1218
- } else {
1219
- // Wait for bridge to be ready
1220
- window.addEventListener('mcp:bridge-ready', initFromBridge);
1221
- }
1222
- })();
1223
- `;
1224
- }
1225
- function buildRequireShim() {
1226
- return `
1227
- // Module Require Shim (Vendor)
1228
- (function() {
1229
- window.__moduleCache = {};
1230
- window.require = function(moduleName) {
1231
- if (window.__moduleCache[moduleName]) {
1232
- return window.__moduleCache[moduleName];
1233
- }
1234
-
1235
- var moduleMap = {
1236
- 'react': function() { return window.React; },
1237
- 'react-dom': function() { return window.ReactDOM; },
1238
- 'react-dom/client': function() { return window.ReactDOM; },
1239
- 'react/jsx-runtime': function() { return window.jsx_runtime_namespaceObject; },
1240
- 'react/jsx-dev-runtime': function() { return window.jsx_runtime_namespaceObject; },
1241
- '@frontmcp/ui': function() { return window.frontmcp_ui_namespaceObject; },
1242
- '@frontmcp/ui/react': function() { return window.frontmcp_ui_namespaceObject; }
1243
- };
1244
-
1245
- var resolver = moduleMap[moduleName];
1246
- if (resolver) {
1247
- var mod = resolver();
1248
- window.__moduleCache[moduleName] = mod;
1249
- return mod;
1250
- }
1251
-
1252
- console.warn('[FrontMCP] Unknown module:', moduleName);
1253
- return {};
1254
- };
1255
- })();
1256
- `;
1257
- }
1258
- function buildInlineMarkdownParser2(options) {
1259
- const allowUnsafeLinks = options?.contentSecurity?.bypassSanitization || options?.contentSecurity?.allowUnsafeLinks;
1260
- return `
1261
- // Inline Markdown Parser (Vendor)
1262
- (function() {
1263
- // XSS protection settings (configured at build time)
1264
- // Set to true if contentSecurity.allowUnsafeLinks or bypassSanitization is enabled
1265
- var __allowUnsafeLinks = ${allowUnsafeLinks ? "true" : "false"};
1266
-
1267
- // URL scheme validation to prevent XSS via javascript: URLs
1268
- function isSafeUrl(url) {
1269
- // If unsafe links are explicitly allowed, skip validation
1270
- if (__allowUnsafeLinks) return true;
1271
- if (!url) return false;
1272
- var lower = url.toLowerCase().trim();
1273
- return lower.startsWith('http://') ||
1274
- lower.startsWith('https://') ||
1275
- lower.startsWith('/') ||
1276
- lower.startsWith('#') ||
1277
- lower.startsWith('mailto:');
1278
- }
1279
-
1280
- function parseMarkdown(md) {
1281
- var html = md;
1282
- html = html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
1283
- html = html.replace(/^######\\s+(.*)$/gm, '<h6>$1</h6>');
1284
- html = html.replace(/^#####\\s+(.*)$/gm, '<h5>$1</h5>');
1285
- html = html.replace(/^####\\s+(.*)$/gm, '<h4>$1</h4>');
1286
- html = html.replace(/^###\\s+(.*)$/gm, '<h3>$1</h3>');
1287
- html = html.replace(/^##\\s+(.*)$/gm, '<h2>$1</h2>');
1288
- html = html.replace(/^#\\s+(.*)$/gm, '<h1>$1</h1>');
1289
- html = html.replace(/\\*\\*\\*(.+?)\\*\\*\\*/g, '<strong><em>$1</em></strong>');
1290
- html = html.replace(/\\*\\*(.+?)\\*\\*/g, '<strong>$1</strong>');
1291
- html = html.replace(/\\*(.+?)\\*/g, '<em>$1</em>');
1292
- html = html.replace(/\`([^\`]+)\`/g, '<code>$1</code>');
1293
- // Links - validate URL scheme to prevent XSS (unless bypassed)
1294
- html = html.replace(/\\[([^\\]]+)\\]\\(([^)]+)\\)/g, function(match, text, url) {
1295
- return isSafeUrl(url) ? '<a href="' + url + '">' + text + '</a>' : text;
1296
- });
1297
- html = html.replace(/^[-*]\\s+(.*)$/gm, '<li>$1</li>');
1298
- html = html.replace(/\\n\\n+/g, '</p><p>');
1299
- html = '<p>' + html + '</p>';
1300
- return html;
1301
- }
1302
-
1303
- window.__frontmcp.parseMarkdown = parseMarkdown;
1304
-
1305
- window.ReactMarkdown = function(props) {
1306
- var html = parseMarkdown(props.children || '');
1307
- return React.createElement('div', {
1308
- className: 'frontmcp-markdown prose',
1309
- dangerouslySetInnerHTML: { __html: html }
1310
- });
1311
- };
1312
- })();
1313
- `;
1314
- }
1315
- function buildRenderersRuntime2(options) {
1316
- const bypassSanitization = options?.contentSecurity?.bypassSanitization;
1317
- const allowInlineScripts = bypassSanitization || options?.contentSecurity?.allowInlineScripts;
1318
- return `
1319
- // Universal Renderers (Vendor)
1320
- (function() {
1321
- var renderers = {};
1322
-
1323
- // XSS protection settings (configured at build time)
1324
- // Set to true if contentSecurity.allowInlineScripts or bypassSanitization is enabled
1325
- var __allowInlineScripts = ${allowInlineScripts ? "true" : "false"};
1326
-
1327
- renderers.html = {
1328
- type: 'html',
1329
- priority: 0,
1330
- canHandle: function(c) { return c.type === 'html'; },
1331
- render: function(c, ctx) {
1332
- var html = c.source;
1333
- // Apply XSS protection unless bypassed
1334
- if (!__allowInlineScripts) {
1335
- // Remove script tags and event handlers to prevent XSS
1336
- html = html.replace(/<script[^>]*>[\\s\\S]*?<\\/script>/gi, '');
1337
- html = html.replace(/\\s+on\\w+\\s*=/gi, ' data-removed-handler=');
1338
- }
1339
- return React.createElement('div', {
1340
- className: 'frontmcp-html-content',
1341
- dangerouslySetInnerHTML: { __html: html }
1342
- });
1343
- }
1344
- };
1345
-
1346
- renderers.markdown = {
1347
- type: 'markdown',
1348
- priority: 10,
1349
- canHandle: function(c) {
1350
- if (c.type === 'markdown') return true;
1351
- if (typeof c.source !== 'string') return false;
1352
- var s = c.source;
1353
- return /^#{1,6}\\s/m.test(s) || /^[-*]\\s/m.test(s) || /\\*\\*[^*]+\\*\\*/.test(s);
1354
- },
1355
- render: function(c, ctx) {
1356
- if (window.ReactMarkdown) {
1357
- return React.createElement(window.ReactMarkdown, {
1358
- children: c.source,
1359
- components: Object.assign({}, ctx.components, c.components)
1360
- });
1361
- }
1362
- var html = window.__frontmcp.parseMarkdown ? window.__frontmcp.parseMarkdown(c.source) : c.source;
1363
- return React.createElement('div', {
1364
- className: 'frontmcp-markdown prose',
1365
- dangerouslySetInnerHTML: { __html: html }
1366
- });
1367
- }
1368
- };
1369
-
1370
- renderers.react = {
1371
- type: 'react',
1372
- priority: 30,
1373
- canHandle: function(c) { return c.type === 'react' || typeof c.source === 'function'; },
1374
- render: function(c, ctx) {
1375
- var Component = c.source;
1376
- var props = Object.assign({
1377
- output: ctx.output,
1378
- input: ctx.input,
1379
- state: ctx.state,
1380
- data: ctx.output // Alias for convenience
1381
- }, c.props);
1382
- return React.createElement(Component, props);
1383
- }
1384
- };
1385
-
1386
- renderers.mdx = {
1387
- type: 'mdx',
1388
- priority: 20,
1389
- canHandle: function(c) {
1390
- if (c.type === 'mdx') return true;
1391
- if (typeof c.source !== 'string') return false;
1392
- var s = c.source;
1393
- return /<[A-Z][a-zA-Z]*/.test(s) && /^#{1,6}\\s/m.test(s);
1394
- },
1395
- render: function(c, ctx) {
1396
- if (typeof c.compiledContent === 'function') {
1397
- var MDXContent = c.compiledContent;
1398
- return React.createElement(MDXContent, {
1399
- output: ctx.output,
1400
- input: ctx.input,
1401
- components: Object.assign({}, ctx.components, c.components)
1402
- });
1403
- }
1404
- return React.createElement('div', { className: 'frontmcp-mdx-fallback' }, [
1405
- React.createElement('div', {
1406
- key: 'warn',
1407
- className: 'bg-yellow-50 border border-yellow-200 rounded p-2 mb-2 text-sm text-yellow-800'
1408
- }, 'MDX requires pre-compilation. Showing raw content.'),
1409
- React.createElement('pre', {
1410
- key: 'pre',
1411
- className: 'bg-gray-100 p-4 rounded text-sm overflow-auto'
1412
- }, c.source)
1413
- ]);
1414
- }
1415
- };
1416
-
1417
- var sortedRenderers = [renderers.react, renderers.mdx, renderers.markdown, renderers.html];
1418
-
1419
- window.__frontmcp.detectRenderer = function(content) {
1420
- if (content.type && renderers[content.type]) {
1421
- return renderers[content.type];
1422
- }
1423
- for (var i = 0; i < sortedRenderers.length; i++) {
1424
- if (sortedRenderers[i].canHandle(content)) {
1425
- return sortedRenderers[i];
1426
- }
1427
- }
1428
- return renderers.html;
1429
- };
1430
-
1431
- window.__frontmcp.renderContent = function(content, context) {
1432
- var renderer = window.__frontmcp.detectRenderer(content);
1433
- return renderer.render(content, context);
1434
- };
1435
-
1436
- window.__frontmcp.renderers = renderers;
1437
- })();
1438
- `;
1439
- }
1440
- function buildUIComponentsRuntime() {
1441
- return buildBrowserUIComponents();
1442
- }
1443
- function buildUniversalAppRuntime2() {
1444
- return `
1445
- // Universal App (Vendor)
1446
- (function() {
1447
- var LoadingSpinner = function() {
1448
- return React.createElement('div', {
1449
- className: 'frontmcp-loading flex items-center justify-center min-h-[200px]'
1450
- }, React.createElement('div', {
1451
- className: 'frontmcp-spinner w-6 h-6 border-2 border-gray-200 border-t-blue-500 rounded-full animate-spin'
1452
- }));
1453
- };
1454
-
1455
- var ErrorDisplay = function(props) {
1456
- return React.createElement('div', {
1457
- className: 'frontmcp-error bg-red-50 border border-red-200 rounded-lg p-4 text-red-800'
1458
- }, [
1459
- React.createElement('div', { key: 'title', className: 'font-medium' }, 'Error'),
1460
- React.createElement('div', { key: 'msg', className: 'text-sm mt-1' }, props.error)
1461
- ]);
1462
- };
1463
-
1464
- var EmptyState = function() {
1465
- return React.createElement('div', {
1466
- className: 'frontmcp-empty text-gray-500 text-center py-8'
1467
- }, 'No content to display');
1468
- };
1469
-
1470
- window.__frontmcp.UniversalApp = function(props) {
1471
- var state = window.useFrontMCPStore();
1472
-
1473
- if (state.loading) {
1474
- return props.fallback || React.createElement(LoadingSpinner);
1475
- }
1476
-
1477
- if (state.error) {
1478
- var ErrorComp = props.errorFallback || ErrorDisplay;
1479
- return React.createElement(ErrorComp, { error: state.error });
1480
- }
1481
-
1482
- var content = props.content || state.content;
1483
-
1484
- if (!content) {
1485
- return React.createElement(EmptyState);
1486
- }
1487
-
1488
- var context = {
1489
- output: state.output,
1490
- input: state.input,
1491
- state: state,
1492
- components: props.components || {}
1493
- };
1494
-
1495
- var rendered = window.__frontmcp.renderContent(content, context);
1496
- return React.createElement('div', { className: 'frontmcp-content' }, rendered);
1497
- };
1498
-
1499
- window.__frontmcp.LoadingSpinner = LoadingSpinner;
1500
- window.__frontmcp.ErrorDisplay = ErrorDisplay;
1501
- window.__frontmcp.EmptyState = EmptyState;
1502
- })();
1503
- `;
1504
- }
1505
- function buildComponentWrapper() {
1506
- return `
1507
- // Component Execution (App Chunk)
1508
- (function() {
1509
- ${RUNTIME_PLACEHOLDERS.COMPONENT_CODE}
1510
- })();
1511
- `;
1512
- }
1513
- function buildDataInjectionWrapper() {
1514
- return `
1515
- // Data Injection (App Chunk)
1516
- (function() {
1517
- ${RUNTIME_PLACEHOLDERS.DATA_INJECTION}
1518
- })();
1519
- `;
1520
- }
1521
- function buildCustomComponentsWrapper() {
1522
- return `
1523
- // Custom Components (App Chunk)
1524
- (function() {
1525
- ${RUNTIME_PLACEHOLDERS.CUSTOM_COMPONENTS}
1526
- })();
1527
- `;
1528
- }
1529
- function buildCdnImports2(options) {
1530
- const parts = [];
1531
- if (options.cdnType === "esm") {
1532
- if (options.includeMarkdown) {
1533
- parts.push(`
1534
- <script type="module">
1535
- import ReactMarkdown from '${UNIVERSAL_CDN.esm.reactMarkdown}';
1536
- window.ReactMarkdown = ReactMarkdown;
1537
- </script>`);
1538
- }
1539
- if (options.includeMdx) {
1540
- parts.push(`
1541
- <script type="module">
1542
- import { MDXProvider } from '${UNIVERSAL_CDN.esm.mdxReact}';
1543
- window.MDXProvider = MDXProvider;
1544
- </script>`);
1545
- }
1546
- }
1547
- return parts.join("\n");
1548
- }
1549
- function getCachedRuntime(options, config = {}) {
1550
- const cacheKey = generateCacheKey(options);
1551
- const cacheConfig = { ...DEFAULT_CACHE_CONFIG, ...config };
1552
- const cached = runtimeCache.get(cacheKey);
1553
- if (cached) {
1554
- if (cacheConfig.ttl === 0 || Date.now() - cached.cachedAt < cacheConfig.ttl) {
1555
- return {
1556
- vendorScript: cached.script,
1557
- appTemplate: buildAppTemplate(),
1558
- cdnImports: cached.cdnImports,
1559
- vendorSize: cached.size,
1560
- cached: true,
1561
- cacheKey
1562
- };
1563
- }
1564
- runtimeCache.delete(cacheKey);
1565
- }
1566
- const vendorParts = [];
1567
- if (options.includeBridge) {
1568
- vendorParts.push(getMCPBridgeScript());
1569
- }
1570
- vendorParts.push(buildStoreRuntime2(), buildRequireShim());
1571
- if (options.cdnType === "umd" || options.includeMarkdown) {
1572
- vendorParts.push(buildInlineMarkdownParser2(options));
1573
- }
1574
- vendorParts.push(buildRenderersRuntime2(options));
1575
- vendorParts.push(buildUIComponentsRuntime());
1576
- vendorParts.push(buildUniversalAppRuntime2());
1577
- let vendorScript = vendorParts.join("\n");
1578
- if (options.minify) {
1579
- vendorScript = minifyScript(vendorScript);
1580
- }
1581
- const cdnImports = buildCdnImports2(options);
1582
- const entry = {
1583
- script: vendorScript,
1584
- cdnImports,
1585
- size: vendorScript.length,
1586
- cacheKey,
1587
- cachedAt: Date.now()
1588
- };
1589
- if (runtimeCache.size >= cacheConfig.maxEntries) {
1590
- const oldestKey = runtimeCache.keys().next().value;
1591
- if (oldestKey) {
1592
- runtimeCache.delete(oldestKey);
1593
- }
1594
- }
1595
- runtimeCache.set(cacheKey, entry);
1596
- return {
1597
- vendorScript,
1598
- appTemplate: buildAppTemplate(),
1599
- cdnImports,
1600
- vendorSize: vendorScript.length,
1601
- cached: false,
1602
- cacheKey
1603
- };
1604
- }
1605
- function buildAppTemplate() {
1606
- return [buildCustomComponentsWrapper(), buildComponentWrapper(), buildDataInjectionWrapper()].join("\n");
1607
- }
1608
- function minifyScript(script) {
1609
- return script.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/[^\n]*$/gm, "").replace(/\n\s*\n/g, "\n").replace(/^\s+/gm, "").trim();
1610
- }
1611
- function clearRuntimeCache() {
1612
- runtimeCache.clear();
1613
- }
1614
- function getRuntimeCacheStats() {
1615
- let totalSize = 0;
1616
- const keys = [];
1617
- for (const [key, entry] of runtimeCache) {
1618
- keys.push(key);
1619
- totalSize += entry.size;
1620
- }
1621
- return {
1622
- entries: runtimeCache.size,
1623
- totalSize,
1624
- keys
1625
- };
1626
- }
1627
- function buildAppScript(appTemplate, componentCode, dataInjection, customComponents = "") {
1628
- return appTemplate.replace(RUNTIME_PLACEHOLDERS.CUSTOM_COMPONENTS, customComponents || "// No custom components").replace(RUNTIME_PLACEHOLDERS.COMPONENT_CODE, componentCode || "// No component code").replace(RUNTIME_PLACEHOLDERS.DATA_INJECTION, dataInjection);
1629
- }
1630
- var DEFAULT_OUTPUT_PLACEHOLDER = "__FRONTMCP_OUTPUT_PLACEHOLDER__";
1631
- var DEFAULT_INPUT_PLACEHOLDER = "__FRONTMCP_INPUT_PLACEHOLDER__";
1632
- function buildDataInjectionCode(toolName, input, output, structuredContent, contentType, source, hasComponent, options) {
1633
- const buildMode = options?.buildMode ?? "static";
1634
- const cdnType = options?.cdnType ?? "esm";
1635
- switch (buildMode) {
1636
- case "dynamic":
1637
- return buildDynamicDataInjectionCode(
1638
- toolName,
1639
- input,
1640
- output,
1641
- structuredContent,
1642
- contentType,
1643
- source,
1644
- hasComponent,
1645
- cdnType,
1646
- options?.dynamicOptions
1647
- );
1648
- case "hybrid":
1649
- return buildHybridDataInjectionCode(
1650
- toolName,
1651
- structuredContent,
1652
- contentType,
1653
- source,
1654
- hasComponent,
1655
- options?.hybridOptions
1656
- );
1657
- default:
1658
- return buildStaticDataInjectionCode(
1659
- toolName,
1660
- input,
1661
- output,
1662
- structuredContent,
1663
- contentType,
1664
- source,
1665
- hasComponent
1666
- );
1667
- }
1668
- }
1669
- function buildStaticDataInjectionCode(toolName, input, output, structuredContent, contentType, source, hasComponent) {
1670
- const safeJson = (value) => {
1671
- try {
1672
- return JSON.stringify(value);
1673
- } catch {
1674
- return "null";
1675
- }
1676
- };
1677
- if (hasComponent) {
1678
- return `
1679
- // Static Mode - Data baked at build time
1680
- window.__frontmcp.setState({
1681
- toolName: ${safeJson(toolName)},
1682
- input: ${safeJson(input ?? null)},
1683
- output: ${safeJson(output ?? null)},
1684
- structuredContent: ${safeJson(structuredContent ?? null)},
1685
- content: {
1686
- type: 'react',
1687
- source: window.__frontmcp_component
1688
- },
1689
- loading: false,
1690
- error: null
1691
- });`;
1692
- }
1693
- return `
1694
- // Static Mode - Data baked at build time
1695
- window.__frontmcp.setState({
1696
- toolName: ${safeJson(toolName)},
1697
- input: ${safeJson(input ?? null)},
1698
- output: ${safeJson(output ?? null)},
1699
- structuredContent: ${safeJson(structuredContent ?? null)},
1700
- content: {
1701
- type: ${safeJson(contentType)},
1702
- source: ${safeJson(source)}
1703
- },
1704
- loading: false,
1705
- error: null
1706
- });`;
1707
- }
1708
- function buildDynamicDataInjectionCode(toolName, input, output, structuredContent, contentType, source, hasComponent, cdnType, dynamicOptions) {
1709
- if (cdnType === "umd") {
1710
- return buildDynamicWithPlaceholdersCode(
1711
- toolName,
1712
- structuredContent,
1713
- contentType,
1714
- source,
1715
- hasComponent,
1716
- dynamicOptions
1717
- );
1718
- }
1719
- return buildDynamicWithSubscriptionCode(
1720
- toolName,
1721
- input,
1722
- output,
1723
- structuredContent,
1724
- contentType,
1725
- source,
1726
- hasComponent,
1727
- dynamicOptions
1728
- );
1729
- }
1730
- function buildDynamicWithPlaceholdersCode(toolName, structuredContent, contentType, source, hasComponent, dynamicOptions) {
1731
- const safeJson = (value) => {
1732
- try {
1733
- return JSON.stringify(value);
1734
- } catch {
1735
- return "null";
1736
- }
1737
- };
1738
- const outputPlaceholder = DEFAULT_OUTPUT_PLACEHOLDER;
1739
- const inputPlaceholder = DEFAULT_INPUT_PLACEHOLDER;
1740
- const includeInitialData = dynamicOptions?.includeInitialData ?? true;
1741
- const contentBlock = hasComponent ? `content: { type: 'react', source: window.__frontmcp_component }` : `content: { type: ${safeJson(contentType)}, source: ${safeJson(source)} }`;
1742
- return `
1743
- // Dynamic Mode - Placeholder-based for non-OpenAI platforms
1744
- var __outputRaw = "${outputPlaceholder}";
1745
- var __inputRaw = "${inputPlaceholder}";
1746
- var __output = null;
1747
- var __input = null;
1748
- var __error = null;
1749
- var __outputNotReplaced = false;
1750
- var __includeInitialData = ${includeInitialData};
1751
-
1752
- // Parse output placeholder
1753
- if (typeof __outputRaw === 'string' && __outputRaw !== "${outputPlaceholder}") {
1754
- try { __output = JSON.parse(__outputRaw); } catch (e) {
1755
- console.warn('[FrontMCP] Failed to parse output:', e);
1756
- __error = 'Failed to parse output data';
1757
- }
1758
- } else if (__outputRaw === "${outputPlaceholder}") {
1759
- __outputNotReplaced = true;
1760
- }
1761
-
1762
- // Parse input placeholder
1763
- if (typeof __inputRaw === 'string' && __inputRaw !== "${inputPlaceholder}") {
1764
- try { __input = JSON.parse(__inputRaw); } catch (e) { console.warn('[FrontMCP] Failed to parse input:', e); }
1765
- }
1766
-
1767
- // Handle placeholder not replaced - show error if expecting initial data
1768
- if (__outputNotReplaced && __includeInitialData) {
1769
- __error = 'No data provided. The output placeholder was not replaced.';
1770
- }
1771
-
1772
- window.__frontmcp.setState({
1773
- toolName: ${safeJson(toolName)},
1774
- input: __input,
1775
- output: __output,
1776
- structuredContent: ${safeJson(structuredContent ?? null)},
1777
- ${contentBlock},
1778
- loading: !__includeInitialData && __output === null && !__error,
1779
- error: __error
1780
- });`;
1781
- }
1782
- function buildDynamicWithSubscriptionCode(toolName, input, output, structuredContent, contentType, source, hasComponent, dynamicOptions) {
1783
- const safeJson = (value) => {
1784
- try {
1785
- return JSON.stringify(value);
1786
- } catch {
1787
- return "null";
1788
- }
1789
- };
1790
- const includeInitialData = dynamicOptions?.includeInitialData ?? true;
1791
- const subscribeToUpdates = dynamicOptions?.subscribeToUpdates ?? true;
1792
- const contentBlock = hasComponent ? `content: { type: 'react', source: window.__frontmcp_component }` : `content: { type: ${safeJson(contentType)}, source: ${safeJson(source)} }`;
1793
- const initialState = includeInitialData ? `{
1794
- toolName: ${safeJson(toolName)},
1795
- input: ${safeJson(input ?? null)},
1796
- output: ${safeJson(output ?? null)},
1797
- structuredContent: ${safeJson(structuredContent ?? null)},
1798
- ${contentBlock},
1799
- loading: false,
1800
- error: null
1801
- }` : `{
1802
- toolName: ${safeJson(toolName)},
1803
- input: ${safeJson(input ?? null)},
1804
- output: null,
1805
- structuredContent: ${safeJson(structuredContent ?? null)},
1806
- ${contentBlock},
1807
- loading: true,
1808
- error: null
1809
- }`;
1810
- const subscriptionBlock = subscribeToUpdates ? `
1811
- // Subscribe to platform tool result events
1812
- (function() {
1813
- function subscribeToUpdates() {
1814
- if (window.openai && window.openai.canvas && window.openai.canvas.onToolResult) {
1815
- window.openai.canvas.onToolResult(function(result) {
1816
- window.__frontmcp.updateOutput(result);
1817
- window.dispatchEvent(new CustomEvent('frontmcp:toolResult', { detail: result }));
1818
- });
1819
- }
1820
- }
1821
- if (document.readyState === 'loading') {
1822
- document.addEventListener('DOMContentLoaded', subscribeToUpdates);
1823
- } else {
1824
- subscribeToUpdates();
1825
- }
1826
- })();` : "";
1827
- return `
1828
- // Dynamic Mode - OpenAI Subscription
1829
- window.__frontmcp.setState(${initialState});
1830
- ${subscriptionBlock}`;
1831
- }
1832
- function buildHybridDataInjectionCode(toolName, structuredContent, contentType, source, hasComponent, hybridOptions) {
1833
- const safeJson = (value) => {
1834
- try {
1835
- return JSON.stringify(value);
1836
- } catch {
1837
- return "null";
1838
- }
1839
- };
1840
- const outputPlaceholder = hybridOptions?.placeholder ?? DEFAULT_OUTPUT_PLACEHOLDER;
1841
- const inputPlaceholder = hybridOptions?.inputPlaceholder ?? DEFAULT_INPUT_PLACEHOLDER;
1842
- const contentBlock = hasComponent ? `content: { type: 'react', source: window.__frontmcp_component }` : `content: { type: ${safeJson(contentType)}, source: ${safeJson(source)} }`;
1843
- return `
1844
- // Hybrid Mode - Placeholders replaced at runtime
1845
- var __outputRaw = "${outputPlaceholder}";
1846
- var __inputRaw = "${inputPlaceholder}";
1847
- var __output = null;
1848
- var __input = null;
1849
- var __error = null;
1850
- var __outputNotReplaced = false;
1851
-
1852
- // Parse output placeholder
1853
- if (typeof __outputRaw === 'string' && __outputRaw !== "${outputPlaceholder}") {
1854
- try { __output = JSON.parse(__outputRaw); } catch (e) {
1855
- console.warn('[FrontMCP] Failed to parse output:', e);
1856
- __error = 'Failed to parse output data';
1857
- }
1858
- } else if (__outputRaw === "${outputPlaceholder}") {
1859
- // Placeholder not replaced - no data was injected
1860
- __outputNotReplaced = true;
1861
- }
1862
-
1863
- // Parse input placeholder
1864
- if (typeof __inputRaw === 'string' && __inputRaw !== "${inputPlaceholder}") {
1865
- try { __input = JSON.parse(__inputRaw); } catch (e) { console.warn('[FrontMCP] Failed to parse input:', e); }
1866
- }
1867
-
1868
- // Set error if output placeholder was not replaced (no data provided)
1869
- if (__outputNotReplaced) {
1870
- __error = 'No data provided. The output placeholder was not replaced.';
1871
- }
1872
-
1873
- window.__frontmcp.setState({
1874
- toolName: ${safeJson(toolName)},
1875
- input: __input,
1876
- output: __output,
1877
- structuredContent: ${safeJson(structuredContent ?? null)},
1878
- ${contentBlock},
1879
- loading: false,
1880
- error: __error
1881
- });`;
1882
- }
1883
- function buildComponentCode(transpiledCode) {
1884
- return `
1885
- // CommonJS module shim
1886
- var module = { exports: {} };
1887
- var exports = module.exports;
1888
-
1889
- // Execute transpiled component
1890
- ${transpiledCode}
1891
-
1892
- // Capture component
1893
- window.__frontmcp_component = module.exports.default || module.exports;`;
1894
- }
1895
- export {
1896
- ComponentsProvider,
1897
- DEFAULT_FRONTMCP_STATE,
1898
- EmptyState,
1899
- ErrorDisplay,
1900
- FrontMCPProvider,
1901
- LoadingSpinner,
1902
- RUNTIME_PLACEHOLDERS,
1903
- RendererRegistry,
1904
- UNIVERSAL_CDN,
1905
- UniversalApp,
1906
- UniversalAppWithProvider,
1907
- UniversalProvider,
1908
- buildAppScript,
1909
- buildComponentCode,
1910
- buildDataInjectionCode,
1911
- buildMinimalRuntime,
1912
- buildUniversalRuntime,
1913
- clearRuntimeCache,
1914
- createContent,
1915
- createFrontMCPStore,
1916
- createMarkdownRenderer,
1917
- createMdxRenderer,
1918
- createStoreSelector,
1919
- detectContentType,
1920
- detectRenderer,
1921
- getCachedRuntime,
1922
- getGlobalStore,
1923
- getRuntimeCacheStats,
1924
- htmlRenderer,
1925
- initializeStoreFromWindow,
1926
- isMdxSupported,
1927
- isReactComponent,
1928
- markdownRenderer,
1929
- mdxRenderer,
1930
- reactRenderer,
1931
- renderContent,
1932
- rendererRegistry,
1933
- resetGlobalStore,
1934
- safeHtmlRenderer,
1935
- setGlobalStore,
1936
- useComponents,
1937
- useContent,
1938
- useFrontMCPContext,
1939
- useFrontMCPContextSafe,
1940
- useFrontMCPStore,
1941
- useLoadingState,
1942
- useToolInput,
1943
- useToolName,
1944
- useToolOutput,
1945
- withFrontMCP
1946
- };