@adonis-agora/agent 0.71.0 → 0.74.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 (213) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/configure.d.ts +5 -4
  3. package/dist/configure.d.ts.map +1 -1
  4. package/dist/configure.js +8 -4
  5. package/dist/configure.js.map +1 -1
  6. package/dist/providers/agent_provider.d.ts.map +1 -1
  7. package/dist/providers/agent_provider.js +5 -1
  8. package/dist/providers/agent_provider.js.map +1 -1
  9. package/dist/src/a2ui/adapter.d.ts +13 -2
  10. package/dist/src/a2ui/adapter.d.ts.map +1 -1
  11. package/dist/src/a2ui/adapter.js +73 -4
  12. package/dist/src/a2ui/adapter.js.map +1 -1
  13. package/dist/src/a2ui/core.d.ts +79 -2
  14. package/dist/src/a2ui/core.d.ts.map +1 -1
  15. package/dist/src/a2ui/core.js +136 -4
  16. package/dist/src/a2ui/core.js.map +1 -1
  17. package/dist/src/a2ui/index.d.ts +2 -2
  18. package/dist/src/a2ui/index.d.ts.map +1 -1
  19. package/dist/src/a2ui/index.js +2 -2
  20. package/dist/src/a2ui/index.js.map +1 -1
  21. package/dist/src/action-proposal-executor.d.ts.map +1 -1
  22. package/dist/src/action-proposal-executor.js +3 -0
  23. package/dist/src/action-proposal-executor.js.map +1 -1
  24. package/dist/src/ag-ui/adapter.d.ts +6 -1
  25. package/dist/src/ag-ui/adapter.d.ts.map +1 -1
  26. package/dist/src/ag-ui/adapter.js +9 -2
  27. package/dist/src/ag-ui/adapter.js.map +1 -1
  28. package/dist/src/ag-ui/core/encoder.d.ts.map +1 -1
  29. package/dist/src/ag-ui/core/encoder.js +3 -0
  30. package/dist/src/ag-ui/core/encoder.js.map +1 -1
  31. package/dist/src/agent-loop.d.ts.map +1 -1
  32. package/dist/src/agent-loop.js +18 -0
  33. package/dist/src/agent-loop.js.map +1 -1
  34. package/dist/src/agent-service.d.ts +6 -0
  35. package/dist/src/agent-service.d.ts.map +1 -1
  36. package/dist/src/agent-service.js +5 -3
  37. package/dist/src/agent-service.js.map +1 -1
  38. package/dist/src/channels/adapters/evolution_api.d.ts.map +1 -1
  39. package/dist/src/channels/adapters/evolution_api.js +35 -0
  40. package/dist/src/channels/adapters/evolution_api.js.map +1 -1
  41. package/dist/src/channels/adapters/telegram.d.ts.map +1 -1
  42. package/dist/src/channels/adapters/telegram.js +25 -7
  43. package/dist/src/channels/adapters/telegram.js.map +1 -1
  44. package/dist/src/channels/adapters/whatsapp_cloud.d.ts.map +1 -1
  45. package/dist/src/channels/adapters/whatsapp_cloud.js +44 -0
  46. package/dist/src/channels/adapters/whatsapp_cloud.js.map +1 -1
  47. package/dist/src/channels/handler.d.ts +21 -1
  48. package/dist/src/channels/handler.d.ts.map +1 -1
  49. package/dist/src/channels/handler.js +230 -9
  50. package/dist/src/channels/handler.js.map +1 -1
  51. package/dist/src/channels/index.d.ts +2 -2
  52. package/dist/src/channels/index.d.ts.map +1 -1
  53. package/dist/src/channels/index.js.map +1 -1
  54. package/dist/src/channels/types.d.ts +40 -0
  55. package/dist/src/channels/types.d.ts.map +1 -1
  56. package/dist/src/genui/actions.d.ts +26 -0
  57. package/dist/src/genui/actions.d.ts.map +1 -1
  58. package/dist/src/genui/actions.js +76 -0
  59. package/dist/src/genui/actions.js.map +1 -1
  60. package/dist/src/genui/catalog.d.ts +12 -1
  61. package/dist/src/genui/catalog.d.ts.map +1 -1
  62. package/dist/src/genui/catalog.js +12 -1
  63. package/dist/src/genui/catalog.js.map +1 -1
  64. package/dist/src/genui/channels.d.ts +206 -0
  65. package/dist/src/genui/channels.d.ts.map +1 -0
  66. package/dist/src/genui/channels.js +322 -0
  67. package/dist/src/genui/channels.js.map +1 -0
  68. package/dist/src/genui/chart-image.d.ts +46 -0
  69. package/dist/src/genui/chart-image.d.ts.map +1 -0
  70. package/dist/src/genui/chart-image.js +155 -0
  71. package/dist/src/genui/chart-image.js.map +1 -0
  72. package/dist/src/genui/factory.d.ts +23 -1
  73. package/dist/src/genui/factory.d.ts.map +1 -1
  74. package/dist/src/genui/factory.js +13 -1
  75. package/dist/src/genui/factory.js.map +1 -1
  76. package/dist/src/genui/index.d.ts.map +1 -1
  77. package/dist/src/genui/index.js +64 -3
  78. package/dist/src/genui/index.js.map +1 -1
  79. package/dist/src/genui/kit/adonis.d.ts +4 -0
  80. package/dist/src/genui/kit/adonis.d.ts.map +1 -0
  81. package/dist/src/genui/kit/adonis.js +38 -0
  82. package/dist/src/genui/kit/adonis.js.map +1 -0
  83. package/dist/src/genui/kit/bundle.d.ts +25 -0
  84. package/dist/src/genui/kit/bundle.d.ts.map +1 -0
  85. package/dist/src/genui/kit/bundle.js +144 -0
  86. package/dist/src/genui/kit/bundle.js.map +1 -0
  87. package/dist/src/genui/kit/discover.d.ts +41 -0
  88. package/dist/src/genui/kit/discover.d.ts.map +1 -0
  89. package/dist/src/genui/kit/discover.js +100 -0
  90. package/dist/src/genui/kit/discover.js.map +1 -0
  91. package/dist/src/genui/kit/docs.d.ts +22 -0
  92. package/dist/src/genui/kit/docs.d.ts.map +1 -0
  93. package/dist/src/genui/kit/docs.js +237 -0
  94. package/dist/src/genui/kit/docs.js.map +1 -0
  95. package/dist/src/genui/kit/files.d.ts +27 -0
  96. package/dist/src/genui/kit/files.d.ts.map +1 -0
  97. package/dist/src/genui/kit/files.js +126 -0
  98. package/dist/src/genui/kit/files.js.map +1 -0
  99. package/dist/src/genui/kit/index.d.ts +12 -0
  100. package/dist/src/genui/kit/index.d.ts.map +1 -0
  101. package/dist/src/genui/kit/index.js +12 -0
  102. package/dist/src/genui/kit/index.js.map +1 -0
  103. package/dist/src/genui/kit/plugin.d.ts +52 -0
  104. package/dist/src/genui/kit/plugin.d.ts.map +1 -0
  105. package/dist/src/genui/kit/plugin.js +314 -0
  106. package/dist/src/genui/kit/plugin.js.map +1 -0
  107. package/dist/src/genui/kit/write.d.ts +14 -0
  108. package/dist/src/genui/kit/write.d.ts.map +1 -0
  109. package/dist/src/genui/kit/write.js +39 -0
  110. package/dist/src/genui/kit/write.js.map +1 -0
  111. package/dist/src/genui/progressive.d.ts +3 -1
  112. package/dist/src/genui/progressive.d.ts.map +1 -1
  113. package/dist/src/genui/progressive.js +23 -2
  114. package/dist/src/genui/progressive.js.map +1 -1
  115. package/dist/src/genui/public.d.ts +8 -4
  116. package/dist/src/genui/public.d.ts.map +1 -1
  117. package/dist/src/genui/public.js +6 -2
  118. package/dist/src/genui/public.js.map +1 -1
  119. package/dist/src/genui/sandbox-jsx.d.ts +29 -0
  120. package/dist/src/genui/sandbox-jsx.d.ts.map +1 -0
  121. package/dist/src/genui/sandbox-jsx.js +656 -0
  122. package/dist/src/genui/sandbox-jsx.js.map +1 -0
  123. package/dist/src/genui/sandbox-kit.d.ts +88 -0
  124. package/dist/src/genui/sandbox-kit.d.ts.map +1 -0
  125. package/dist/src/genui/sandbox-kit.js +89 -0
  126. package/dist/src/genui/sandbox-kit.js.map +1 -0
  127. package/dist/src/genui/sandbox-theme.d.ts +61 -0
  128. package/dist/src/genui/sandbox-theme.d.ts.map +1 -0
  129. package/dist/src/genui/sandbox-theme.js +255 -0
  130. package/dist/src/genui/sandbox-theme.js.map +1 -0
  131. package/dist/src/genui/sandbox.d.ts +73 -2
  132. package/dist/src/genui/sandbox.d.ts.map +1 -1
  133. package/dist/src/genui/sandbox.js +87 -14
  134. package/dist/src/genui/sandbox.js.map +1 -1
  135. package/dist/src/genui/text.d.ts.map +1 -1
  136. package/dist/src/genui/text.js +33 -1
  137. package/dist/src/genui/text.js.map +1 -1
  138. package/dist/src/genui/tools.d.ts +26 -0
  139. package/dist/src/genui/tools.d.ts.map +1 -1
  140. package/dist/src/genui/tools.js +94 -14
  141. package/dist/src/genui/tools.js.map +1 -1
  142. package/dist/src/genui/tree.d.ts +25 -2
  143. package/dist/src/genui/tree.d.ts.map +1 -1
  144. package/dist/src/genui/tree.js +182 -7
  145. package/dist/src/genui/tree.js.map +1 -1
  146. package/dist/src/hooks/genui_kit.d.ts +20 -0
  147. package/dist/src/hooks/genui_kit.d.ts.map +1 -0
  148. package/dist/src/hooks/genui_kit.js +55 -0
  149. package/dist/src/hooks/genui_kit.js.map +1 -0
  150. package/dist/src/index.d.ts +2 -2
  151. package/dist/src/index.d.ts.map +1 -1
  152. package/dist/src/index.js +2 -2
  153. package/dist/src/index.js.map +1 -1
  154. package/dist/src/react/core/components/message-item.d.ts.map +1 -1
  155. package/dist/src/react/core/components/message-item.js +6 -3
  156. package/dist/src/react/core/components/message-item.js.map +1 -1
  157. package/dist/src/react/core/genui/index.d.ts +2 -1
  158. package/dist/src/react/core/genui/index.d.ts.map +1 -1
  159. package/dist/src/react/core/genui/index.js +2 -0
  160. package/dist/src/react/core/genui/index.js.map +1 -1
  161. package/dist/src/react/core/genui/sandbox-assets.d.ts +32 -0
  162. package/dist/src/react/core/genui/sandbox-assets.d.ts.map +1 -0
  163. package/dist/src/react/core/genui/sandbox-assets.js +174 -0
  164. package/dist/src/react/core/genui/sandbox-assets.js.map +1 -0
  165. package/dist/src/react/core/genui/sandbox.d.ts +9 -0
  166. package/dist/src/react/core/genui/sandbox.d.ts.map +1 -1
  167. package/dist/src/react/core/genui/sandbox.js +115 -19
  168. package/dist/src/react/core/genui/sandbox.js.map +1 -1
  169. package/dist/src/react/core/genui/ui-action-chip.d.ts +21 -0
  170. package/dist/src/react/core/genui/ui-action-chip.d.ts.map +1 -0
  171. package/dist/src/react/core/genui/ui-action-chip.js +18 -0
  172. package/dist/src/react/core/genui/ui-action-chip.js.map +1 -0
  173. package/dist/src/react/core/index.d.ts +2 -2
  174. package/dist/src/react/core/index.d.ts.map +1 -1
  175. package/dist/src/react/core/index.js +1 -1
  176. package/dist/src/react/core/index.js.map +1 -1
  177. package/dist/src/react/core/presentation/phrasing.d.ts +3 -2
  178. package/dist/src/react/core/presentation/phrasing.d.ts.map +1 -1
  179. package/dist/src/react/core/presentation/phrasing.js +4 -27
  180. package/dist/src/react/core/presentation/phrasing.js.map +1 -1
  181. package/dist/src/react/core/presentation/tool-activity.d.ts +8 -0
  182. package/dist/src/react/core/presentation/tool-activity.d.ts.map +1 -1
  183. package/dist/src/react/core/presentation/tool-activity.js +28 -1
  184. package/dist/src/react/core/presentation/tool-activity.js.map +1 -1
  185. package/dist/src/react/core/transcript/index.d.ts +1 -1
  186. package/dist/src/react/core/transcript/index.d.ts.map +1 -1
  187. package/dist/src/react/core/transcript/index.js.map +1 -1
  188. package/dist/src/react/core/transcript/model.d.ts +15 -0
  189. package/dist/src/react/core/transcript/model.d.ts.map +1 -1
  190. package/dist/src/react/core/transcript/model.js +9 -1
  191. package/dist/src/react/core/transcript/model.js.map +1 -1
  192. package/dist/src/react/core/transcript/use-chat-transcript.d.ts +11 -1
  193. package/dist/src/react/core/transcript/use-chat-transcript.d.ts.map +1 -1
  194. package/dist/src/react/core/transcript/use-chat-transcript.js +11 -1
  195. package/dist/src/react/core/transcript/use-chat-transcript.js.map +1 -1
  196. package/dist/src/spi/tool.d.ts +7 -0
  197. package/dist/src/spi/tool.d.ts.map +1 -1
  198. package/dist/src/tool-presentation.d.ts +15 -0
  199. package/dist/src/tool-presentation.d.ts.map +1 -1
  200. package/dist/src/tool-presentation.js +48 -0
  201. package/dist/src/tool-presentation.js.map +1 -1
  202. package/dist/src/tool-registry.d.ts.map +1 -1
  203. package/dist/src/tool-registry.js +2 -0
  204. package/dist/src/tool-registry.js.map +1 -1
  205. package/dist/src/types.d.ts +10 -0
  206. package/dist/src/types.d.ts.map +1 -1
  207. package/dist/src/vite/index.d.ts +24 -0
  208. package/dist/src/vite/index.d.ts.map +1 -0
  209. package/dist/src/vite/index.js +29 -0
  210. package/dist/src/vite/index.js.map +1 -0
  211. package/package.json +38 -2
  212. package/skills/agent-setup/SKILL.md +3 -1
  213. package/skills/agent-tools/SKILL.md +5 -3
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The sandbox's JSX: a small JavaScript + JSX → `React.createElement` transpiler that runs INSIDE
3
+ * the sandbox frame (it is inlined into the frame's document, so the frame stays network-less and
4
+ * needs no `unsafe-eval`), and the partial mode that lets a view the model is still writing be drawn
5
+ * as it streams — only once what has arrived so far closes into something that parses.
6
+ *
7
+ * Every function here is self-contained (no imports, no module state): the frame runtime is built
8
+ * from their source text (`Function.prototype.toString`), so the same code is unit-tested in Node and
9
+ * run in the frame.
10
+ */
11
+ /**
12
+ * JavaScript with JSX → plain JavaScript calling `React.createElement`. No TypeScript, no JSX
13
+ * namespaces. `class` and `for` attributes become `className` / `htmlFor`.
14
+ *
15
+ * `partial: true` reads a prefix of a program the model is still writing: an element, attribute,
16
+ * expression or bracket cut off at the end is closed (an attribute or token cut half-way is dropped,
17
+ * back to the last point the code was whole), so the result is a complete program drawing what has
18
+ * arrived. Without it, malformed input throws a `SyntaxError`.
19
+ */
20
+ export declare function transpileJsx(source: string, options?: {
21
+ partial?: boolean;
22
+ }): string;
23
+ /**
24
+ * A sandbox program as the model writes it, made runnable in the frame: `import` lines dropped (the
25
+ * kit, React and its hooks are already in scope) and `export` / `export default` taken off the
26
+ * declarations — `export default function App()` is `function App()`.
27
+ */
28
+ export declare function prepareSandboxJsx(source: string): string;
29
+ //# sourceMappingURL=sandbox-jsx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandbox-jsx.d.ts","sourceRoot":"","sources":["../../../src/genui/sandbox-jsx.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,MAAM,CA2kBpF;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAMxD"}
@@ -0,0 +1,656 @@
1
+ /**
2
+ * The sandbox's JSX: a small JavaScript + JSX → `React.createElement` transpiler that runs INSIDE
3
+ * the sandbox frame (it is inlined into the frame's document, so the frame stays network-less and
4
+ * needs no `unsafe-eval`), and the partial mode that lets a view the model is still writing be drawn
5
+ * as it streams — only once what has arrived so far closes into something that parses.
6
+ *
7
+ * Every function here is self-contained (no imports, no module state): the frame runtime is built
8
+ * from their source text (`Function.prototype.toString`), so the same code is unit-tested in Node and
9
+ * run in the frame.
10
+ */
11
+ /**
12
+ * JavaScript with JSX → plain JavaScript calling `React.createElement`. No TypeScript, no JSX
13
+ * namespaces. `class` and `for` attributes become `className` / `htmlFor`.
14
+ *
15
+ * `partial: true` reads a prefix of a program the model is still writing: an element, attribute,
16
+ * expression or bracket cut off at the end is closed (an attribute or token cut half-way is dropped,
17
+ * back to the last point the code was whole), so the result is a complete program drawing what has
18
+ * arrived. Without it, malformed input throws a `SyntaxError`.
19
+ */
20
+ export function transpileJsx(source, options) {
21
+ const src = source;
22
+ const n = src.length;
23
+ const partial = options?.partial === true;
24
+ let i = 0;
25
+ /** The last significant token: '' (start), 'val', 'id:<word>' or a punctuation character. */
26
+ let last = '';
27
+ /** A partial read hit the end of the input: every open construct is being closed. */
28
+ let eof = false;
29
+ const EXPR_KEYWORDS = [
30
+ 'return',
31
+ 'case',
32
+ 'typeof',
33
+ 'in',
34
+ 'of',
35
+ 'new',
36
+ 'delete',
37
+ 'void',
38
+ 'throw',
39
+ 'yield',
40
+ 'await',
41
+ 'else',
42
+ 'do',
43
+ 'instanceof',
44
+ ];
45
+ /** Keywords after which the code cannot end. */
46
+ const OPEN_KEYWORDS = [
47
+ 'const',
48
+ 'let',
49
+ 'var',
50
+ 'function',
51
+ 'if',
52
+ 'for',
53
+ 'while',
54
+ 'switch',
55
+ 'new',
56
+ 'typeof',
57
+ 'in',
58
+ 'of',
59
+ 'instanceof',
60
+ 'class',
61
+ 'extends',
62
+ 'case',
63
+ 'do',
64
+ 'else',
65
+ 'try',
66
+ 'catch',
67
+ 'finally',
68
+ 'throw',
69
+ 'delete',
70
+ 'void',
71
+ 'await',
72
+ 'yield',
73
+ 'async',
74
+ 'import',
75
+ 'export',
76
+ ];
77
+ const ENTITIES = {
78
+ amp: '&',
79
+ lt: '<',
80
+ gt: '>',
81
+ quot: '"',
82
+ apos: "'",
83
+ nbsp: ' ',
84
+ middot: '·',
85
+ times: '×',
86
+ divide: '÷',
87
+ ndash: '–',
88
+ mdash: '—',
89
+ hellip: '…',
90
+ copy: '©',
91
+ reg: '®',
92
+ deg: '°',
93
+ euro: '€',
94
+ larr: '←',
95
+ rarr: '→',
96
+ uarr: '↑',
97
+ darr: '↓',
98
+ check: '✓',
99
+ };
100
+ const fail = (message) => {
101
+ throw new SyntaxError(`${message} (at ${i})`);
102
+ };
103
+ const isIdStart = (c) => c !== undefined && /[A-Za-z_$]/.test(c);
104
+ const isId = (c) => c !== undefined && /[\w$]/.test(c);
105
+ const exprStart = () => {
106
+ if (last === '')
107
+ return true;
108
+ if (last === 'val')
109
+ return false;
110
+ if (last.startsWith('id:'))
111
+ return EXPR_KEYWORDS.indexOf(last.slice(3)) >= 0;
112
+ return last !== ')' && last !== ']' && last !== '}';
113
+ };
114
+ /** Can the code end right after the last token (closing what is open)? */
115
+ const complete = () => {
116
+ if (last === '' || last === 'val' || last === ')' || last === ']' || last === '}')
117
+ return true;
118
+ if (last === '{' || last === '[' || last === ',' || last === ';')
119
+ return true;
120
+ if (last.startsWith('id:'))
121
+ return OPEN_KEYWORDS.indexOf(last.slice(3)) < 0;
122
+ return false;
123
+ };
124
+ const decode = (text) => text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z]+);/g, (whole, name) => {
125
+ if (name[0] === '#') {
126
+ const code = name[1] === 'x' ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
127
+ return Number.isFinite(code) ? String.fromCodePoint(code) : whole;
128
+ }
129
+ return ENTITIES[name] ?? whole;
130
+ });
131
+ /** JSX text as React reads it: lines trimmed where they meet, blank lines dropped. */
132
+ const jsxText = (raw) => {
133
+ const lines = raw.split(/\r\n|\n|\r/);
134
+ if (lines.length === 1)
135
+ return decode(raw);
136
+ const kept = [];
137
+ lines.forEach((line, index) => {
138
+ let value = line;
139
+ if (index > 0)
140
+ value = value.replace(/^\s+/, '');
141
+ if (index < lines.length - 1)
142
+ value = value.replace(/\s+$/, '');
143
+ if (value !== '')
144
+ kept.push(value);
145
+ });
146
+ return decode(kept.join(' '));
147
+ };
148
+ const propKey = (name) => {
149
+ const mapped = name === 'class' ? 'className' : name === 'for' ? 'htmlFor' : name;
150
+ return /^[A-Za-z_$][\w$]*$/.test(mapped) ? mapped : JSON.stringify(mapped);
151
+ };
152
+ const skipWs = () => {
153
+ while (i < n && /\s/.test(src[i]))
154
+ i++;
155
+ };
156
+ /** A quoted string. `null` when a partial read ends inside it. */
157
+ const str = (quote) => {
158
+ let j = i + 1;
159
+ while (j < n) {
160
+ const c = src[j];
161
+ if (c === '\\') {
162
+ j += 2;
163
+ continue;
164
+ }
165
+ if (c === quote) {
166
+ const text = src.slice(i, j + 1);
167
+ i = j + 1;
168
+ return text;
169
+ }
170
+ if (c === '\n')
171
+ fail('Unterminated string');
172
+ j++;
173
+ }
174
+ if (partial) {
175
+ eof = true;
176
+ i = n;
177
+ return null;
178
+ }
179
+ return fail('Unterminated string');
180
+ };
181
+ /** A template literal, its `${…}` read as code (JSX included). `null` when cut off. */
182
+ const template = () => {
183
+ let out = '`';
184
+ i++;
185
+ while (i < n) {
186
+ const c = src[i];
187
+ if (c === '\\') {
188
+ out += src.slice(i, i + 2);
189
+ i += 2;
190
+ continue;
191
+ }
192
+ if (c === '`') {
193
+ i++;
194
+ return `${out}\``;
195
+ }
196
+ if (c === '$' && src[i + 1] === '{') {
197
+ i += 2;
198
+ const saved = last;
199
+ last = '';
200
+ const inner = js(true);
201
+ last = saved;
202
+ if (eof)
203
+ return null;
204
+ out += `\${${inner}}`;
205
+ i++;
206
+ continue;
207
+ }
208
+ out += c;
209
+ i++;
210
+ }
211
+ if (partial) {
212
+ eof = true;
213
+ return null;
214
+ }
215
+ return fail('Unterminated template');
216
+ };
217
+ /** A regular expression literal. `null` when cut off. */
218
+ const regex = () => {
219
+ let j = i + 1;
220
+ let inClass = false;
221
+ while (j < n) {
222
+ const c = src[j];
223
+ if (c === '\\') {
224
+ j += 2;
225
+ continue;
226
+ }
227
+ if (c === '\n')
228
+ fail('Unterminated regular expression');
229
+ if (inClass) {
230
+ if (c === ']')
231
+ inClass = false;
232
+ }
233
+ else if (c === '[')
234
+ inClass = true;
235
+ else if (c === '/') {
236
+ j++;
237
+ while (j < n && /[a-z]/.test(src[j]))
238
+ j++;
239
+ const text = src.slice(i, j);
240
+ i = j;
241
+ return text;
242
+ }
243
+ j++;
244
+ }
245
+ if (partial) {
246
+ eof = true;
247
+ i = n;
248
+ return null;
249
+ }
250
+ return fail('Unterminated regular expression');
251
+ };
252
+ /** `{ … }` inside JSX: an expression, read as code. `null` when cut off with nothing whole in it. */
253
+ const braced = () => {
254
+ i++;
255
+ const saved = last;
256
+ last = '';
257
+ const inner = js(true);
258
+ last = saved;
259
+ if (eof)
260
+ return inner.trim() === '' ? null : inner;
261
+ i++;
262
+ return inner;
263
+ };
264
+ const create = (tag, props, children) => `React.createElement(${tag}, ${props}${children.length > 0 ? `, ${children.join(', ')}` : ''})`;
265
+ /** One JSX element (or fragment), at its `<`. `null` when a partial read cut its name. */
266
+ const element = () => {
267
+ i++;
268
+ skipWs();
269
+ let raw = '';
270
+ let tag = 'React.Fragment';
271
+ const segments = [];
272
+ let pairs = [];
273
+ const flush = () => {
274
+ if (pairs.length > 0)
275
+ segments.push(`{${pairs.join(', ')}}`);
276
+ pairs = [];
277
+ };
278
+ const props = () => {
279
+ flush();
280
+ if (segments.length === 0)
281
+ return 'null';
282
+ if (segments.length === 1 && segments[0].startsWith('{'))
283
+ return segments[0];
284
+ return `Object.assign({}, ${segments.join(', ')})`;
285
+ };
286
+ if (src[i] === '>') {
287
+ i++;
288
+ }
289
+ else {
290
+ let j = i;
291
+ while (j < n && /[\w$.:-]/.test(src[j]))
292
+ j++;
293
+ raw = src.slice(i, j);
294
+ if (j >= n && partial) {
295
+ eof = true;
296
+ i = n;
297
+ return null;
298
+ }
299
+ if (raw === '')
300
+ fail('Expected a tag name');
301
+ i = j;
302
+ tag = /^[a-z][\w-]*$/.test(raw) ? JSON.stringify(raw) : raw;
303
+ for (;;) {
304
+ skipWs();
305
+ if (i >= n) {
306
+ if (partial) {
307
+ eof = true;
308
+ return create(tag, props(), []);
309
+ }
310
+ fail('Unterminated JSX tag');
311
+ }
312
+ if (src[i] === '/' && src[i + 1] === '>') {
313
+ i += 2;
314
+ return create(tag, props(), []);
315
+ }
316
+ if (src[i] === '/' && i + 1 >= n && partial) {
317
+ eof = true;
318
+ i = n;
319
+ return create(tag, props(), []);
320
+ }
321
+ if (src[i] === '>') {
322
+ i++;
323
+ break;
324
+ }
325
+ if (src[i] === '{') {
326
+ const start = i;
327
+ i++;
328
+ skipWs();
329
+ if (src.slice(i, i + 3) !== '...') {
330
+ if (partial && i + 3 > n) {
331
+ eof = true;
332
+ i = n;
333
+ return create(tag, props(), []);
334
+ }
335
+ i = start;
336
+ fail('Expected a spread attribute');
337
+ }
338
+ i += 3;
339
+ const saved = last;
340
+ last = '';
341
+ const spread = js(true);
342
+ last = saved;
343
+ if (eof)
344
+ return create(tag, props(), []);
345
+ i++;
346
+ flush();
347
+ segments.push(spread);
348
+ continue;
349
+ }
350
+ let k = i;
351
+ while (k < n && /[\w$:-]/.test(src[k]))
352
+ k++;
353
+ const name = src.slice(i, k);
354
+ if (name === '')
355
+ fail('Expected an attribute');
356
+ i = k;
357
+ skipWs();
358
+ if (i >= n && partial) {
359
+ eof = true;
360
+ return create(tag, props(), []);
361
+ }
362
+ if (src[i] !== '=') {
363
+ pairs.push(`${propKey(name)}: true`);
364
+ continue;
365
+ }
366
+ i++;
367
+ skipWs();
368
+ const quote = src[i];
369
+ if (quote === '"' || quote === "'") {
370
+ const end = src.indexOf(quote, i + 1);
371
+ if (end < 0) {
372
+ if (partial) {
373
+ eof = true;
374
+ i = n;
375
+ return create(tag, props(), []);
376
+ }
377
+ fail('Unterminated attribute');
378
+ }
379
+ pairs.push(`${propKey(name)}: ${JSON.stringify(decode(src.slice(i + 1, end)))}`);
380
+ i = end + 1;
381
+ continue;
382
+ }
383
+ if (quote === '{') {
384
+ const value = braced();
385
+ // A value cut off is not the value: the attribute waits until it is whole.
386
+ if (eof)
387
+ return create(tag, props(), []);
388
+ pairs.push(`${propKey(name)}: ${(value ?? '').trim()}`);
389
+ continue;
390
+ }
391
+ if (quote === '<') {
392
+ const value = element();
393
+ if (value !== null)
394
+ pairs.push(`${propKey(name)}: ${value}`);
395
+ if (eof)
396
+ return create(tag, props(), []);
397
+ continue;
398
+ }
399
+ if (i >= n && partial) {
400
+ eof = true;
401
+ return create(tag, props(), []);
402
+ }
403
+ fail('Expected an attribute value');
404
+ }
405
+ }
406
+ const children = [];
407
+ for (;;) {
408
+ if (i >= n) {
409
+ if (partial) {
410
+ eof = true;
411
+ return create(tag, props(), children);
412
+ }
413
+ fail('Unterminated JSX element');
414
+ }
415
+ if (src[i] === '<' && (src[i + 1] === '/' || (i + 1 >= n && partial))) {
416
+ const close = src.indexOf('>', i);
417
+ if (close < 0) {
418
+ if (partial) {
419
+ eof = true;
420
+ i = n;
421
+ return create(tag, props(), children);
422
+ }
423
+ fail('Unterminated closing tag');
424
+ }
425
+ const name = src.slice(i + 2, close).trim();
426
+ if (name !== raw)
427
+ fail(`Expected </${raw}>, found </${name}>`);
428
+ i = close + 1;
429
+ return create(tag, props(), children);
430
+ }
431
+ if (src[i] === '<') {
432
+ const child = element();
433
+ if (child !== null)
434
+ children.push(child);
435
+ if (eof)
436
+ return create(tag, props(), children);
437
+ continue;
438
+ }
439
+ if (src[i] === '{') {
440
+ const value = braced();
441
+ if (value !== null && value.replace(/\/\*[\s\S]*?\*\//g, '').trim() !== '')
442
+ children.push(value);
443
+ if (eof)
444
+ return create(tag, props(), children);
445
+ continue;
446
+ }
447
+ let j = i;
448
+ while (j < n && src[j] !== '<' && src[j] !== '{')
449
+ j++;
450
+ // A partial read never shows half an entity.
451
+ let rawText = src.slice(i, j);
452
+ if (j >= n && partial)
453
+ rawText = rawText.replace(/&[#\w]*$/, '');
454
+ const text = jsxText(rawText);
455
+ if (text !== '')
456
+ children.push(JSON.stringify(text));
457
+ i = j;
458
+ }
459
+ };
460
+ /**
461
+ * Code, until the end (or, for an embedded expression, the `}` that closes it — not consumed).
462
+ * A partial read ending here closes the brackets still open, after going back to the last token
463
+ * the code could end on.
464
+ */
465
+ function js(stopAtBrace) {
466
+ let out = '';
467
+ const stack = [];
468
+ let safeLength = 0;
469
+ let safeStack = [];
470
+ /** Conditional operators (`a ? b`) still waiting for their `:`, per bracket depth. */
471
+ const ternaries = [0];
472
+ /** The code may end right here (closing what is open). */
473
+ let whole = true;
474
+ const mark = (ok = true) => {
475
+ whole = ok && complete() && ternaries.every((count) => count === 0);
476
+ if (whole) {
477
+ safeLength = out.length;
478
+ safeStack = stack.slice();
479
+ }
480
+ };
481
+ const closing = (open) => (open === '{' ? '}' : open === '(' ? ')' : ']');
482
+ const finish = () => {
483
+ const body = whole ? out : out.slice(0, safeLength);
484
+ const open = whole ? stack : safeStack;
485
+ return body + open.slice().reverse().map(closing).join('');
486
+ };
487
+ while (i < n) {
488
+ const c = src[i];
489
+ if (c === '}' && stack.length === 0 && stopAtBrace)
490
+ return out;
491
+ if (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
492
+ out += c;
493
+ i++;
494
+ continue;
495
+ }
496
+ if (c === '/' && src[i + 1] === '/') {
497
+ const end = src.indexOf('\n', i);
498
+ const stop = end < 0 ? n : end;
499
+ out += src.slice(i, stop);
500
+ i = stop;
501
+ continue;
502
+ }
503
+ if (c === '/' && src[i + 1] === '*') {
504
+ const end = src.indexOf('*/', i + 2);
505
+ if (end < 0) {
506
+ if (partial) {
507
+ eof = true;
508
+ i = n;
509
+ break;
510
+ }
511
+ fail('Unterminated comment');
512
+ }
513
+ out += src.slice(i, end + 2);
514
+ i = end + 2;
515
+ continue;
516
+ }
517
+ if (c === '"' || c === "'") {
518
+ const text = str(c);
519
+ if (text === null)
520
+ break;
521
+ out += text;
522
+ last = 'val';
523
+ mark();
524
+ continue;
525
+ }
526
+ if (c === '`') {
527
+ const text = template();
528
+ if (text === null)
529
+ break;
530
+ out += text;
531
+ last = 'val';
532
+ mark();
533
+ continue;
534
+ }
535
+ if (c === '/' && exprStart()) {
536
+ const text = regex();
537
+ if (text === null)
538
+ break;
539
+ out += text;
540
+ last = 'val';
541
+ mark();
542
+ continue;
543
+ }
544
+ if (c === '<' &&
545
+ exprStart() &&
546
+ (isIdStart(src[i + 1]) || src[i + 1] === '>' || (partial && i + 1 >= n))) {
547
+ if (i + 1 >= n) {
548
+ eof = true;
549
+ i = n;
550
+ break;
551
+ }
552
+ const jsx = element();
553
+ if (jsx !== null) {
554
+ out += jsx;
555
+ last = 'val';
556
+ mark();
557
+ }
558
+ if (eof)
559
+ break;
560
+ continue;
561
+ }
562
+ if (isIdStart(c)) {
563
+ let j = i + 1;
564
+ while (j < n && isId(src[j]))
565
+ j++;
566
+ if (j >= n && partial) {
567
+ // The word may be cut (`ret` of `return`): it does not count until something follows.
568
+ eof = true;
569
+ i = n;
570
+ break;
571
+ }
572
+ const word = src.slice(i, j);
573
+ out += word;
574
+ i = j;
575
+ last = `id:${word}`;
576
+ mark();
577
+ continue;
578
+ }
579
+ if (/[0-9]/.test(c) || (c === '.' && /[0-9]/.test(src[i + 1] ?? ''))) {
580
+ let j = i + 1;
581
+ while (j < n && /[\w.]/.test(src[j]))
582
+ j++;
583
+ if (j >= n && partial) {
584
+ eof = true;
585
+ i = n;
586
+ break;
587
+ }
588
+ out += src.slice(i, j);
589
+ i = j;
590
+ last = 'val';
591
+ mark();
592
+ continue;
593
+ }
594
+ if (c === '?' &&
595
+ (src[i + 1] === '?' || (src[i + 1] === '.' && !/[0-9]/.test(src[i + 2] ?? '')))) {
596
+ // `??` and `?.`: one token, no conditional to close.
597
+ out += src.slice(i, i + 2);
598
+ i += 2;
599
+ last = '?';
600
+ mark(false);
601
+ continue;
602
+ }
603
+ if (c === '?')
604
+ ternaries[stack.length] = (ternaries[stack.length] ?? 0) + 1;
605
+ else if (c === ':' && (ternaries[stack.length] ?? 0) > 0)
606
+ ternaries[stack.length] = (ternaries[stack.length] ?? 0) - 1;
607
+ // Whether the code may end right after this bracket: `[` opening an array literal and `(`
608
+ // opening a call may (`[]`, `f()`); `v[`, a grouping `(`, `if (` and an empty `()` (arrow
609
+ // parameters still to come) may not.
610
+ let ok = true;
611
+ if (c === '[')
612
+ ok = exprStart();
613
+ else if (c === '(') {
614
+ const keyword = last.startsWith('id:') ? last.slice(3) : '';
615
+ ok =
616
+ !exprStart() &&
617
+ ['if', 'for', 'while', 'switch', 'catch', 'function', 'with'].indexOf(keyword) < 0;
618
+ }
619
+ else if (c === ')')
620
+ ok = last !== '(';
621
+ if (c === '{' || c === '(' || c === '[') {
622
+ stack.push(c);
623
+ ternaries[stack.length] = 0;
624
+ }
625
+ else if (c === '}' || c === ')' || c === ']') {
626
+ ternaries.length = stack.length;
627
+ stack.pop();
628
+ }
629
+ out += c;
630
+ i++;
631
+ last = c;
632
+ mark(ok);
633
+ }
634
+ if (i >= n && partial) {
635
+ eof = true;
636
+ return finish();
637
+ }
638
+ if (stopAtBrace)
639
+ fail('Unexpected end of input');
640
+ return out;
641
+ }
642
+ return js(false);
643
+ }
644
+ /**
645
+ * A sandbox program as the model writes it, made runnable in the frame: `import` lines dropped (the
646
+ * kit, React and its hooks are already in scope) and `export` / `export default` taken off the
647
+ * declarations — `export default function App()` is `function App()`.
648
+ */
649
+ export function prepareSandboxJsx(source) {
650
+ return source
651
+ .replace(/^\s*import\s[^;\n]*(?:from\s*['"][^'"\n]*['"])?\s*;?[ \t]*$/gm, '')
652
+ .replace(/^(\s*)export\s+default\s+(?=(async\s+)?function|class)/gm, '$1')
653
+ .replace(/^(\s*)export\s+default\s+([A-Za-z_$][\w$]*)\s*;?\s*$/gm, '$1')
654
+ .replace(/^(\s*)export\s+(?=(const|let|var|function|class|async)\b)/gm, '$1');
655
+ }
656
+ //# sourceMappingURL=sandbox-jsx.js.map