mrmd-js 2.0.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 (52) hide show
  1. package/README.md +842 -0
  2. package/dist/index.cjs +7613 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.js +7530 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/mrmd-js.iife.js +7618 -0
  7. package/dist/mrmd-js.iife.js.map +1 -0
  8. package/package.json +47 -0
  9. package/src/analysis/format.js +371 -0
  10. package/src/analysis/index.js +18 -0
  11. package/src/analysis/is-complete.js +394 -0
  12. package/src/constants.js +44 -0
  13. package/src/execute/css.js +205 -0
  14. package/src/execute/html.js +162 -0
  15. package/src/execute/index.js +41 -0
  16. package/src/execute/interface.js +144 -0
  17. package/src/execute/javascript.js +197 -0
  18. package/src/execute/registry.js +245 -0
  19. package/src/index.js +136 -0
  20. package/src/lsp/complete.js +353 -0
  21. package/src/lsp/format.js +310 -0
  22. package/src/lsp/hover.js +126 -0
  23. package/src/lsp/index.js +55 -0
  24. package/src/lsp/inspect.js +466 -0
  25. package/src/lsp/parse.js +455 -0
  26. package/src/lsp/variables.js +283 -0
  27. package/src/runtime.js +518 -0
  28. package/src/session/console-capture.js +181 -0
  29. package/src/session/context/iframe.js +407 -0
  30. package/src/session/context/index.js +12 -0
  31. package/src/session/context/interface.js +38 -0
  32. package/src/session/context/main.js +357 -0
  33. package/src/session/index.js +16 -0
  34. package/src/session/manager.js +327 -0
  35. package/src/session/session.js +678 -0
  36. package/src/transform/async.js +133 -0
  37. package/src/transform/extract.js +251 -0
  38. package/src/transform/index.js +10 -0
  39. package/src/transform/persistence.js +176 -0
  40. package/src/types/analysis.js +24 -0
  41. package/src/types/capabilities.js +44 -0
  42. package/src/types/completion.js +47 -0
  43. package/src/types/execution.js +62 -0
  44. package/src/types/index.js +16 -0
  45. package/src/types/inspection.js +39 -0
  46. package/src/types/session.js +32 -0
  47. package/src/types/streaming.js +74 -0
  48. package/src/types/variables.js +54 -0
  49. package/src/utils/ansi-renderer.js +301 -0
  50. package/src/utils/css-applicator.js +149 -0
  51. package/src/utils/html-renderer.js +355 -0
  52. package/src/utils/index.js +25 -0
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Execution Types
3
+ *
4
+ * Types for code execution (MRP /execute endpoints).
5
+ * @module types/execution
6
+ */
7
+
8
+ /**
9
+ * @typedef {Object} ExecuteOptions
10
+ * @property {string} [session='default'] - Session ID
11
+ * @property {string} [language] - Language override
12
+ * @property {boolean} [storeHistory=true] - Add to execution history
13
+ * @property {boolean} [silent=false] - Suppress output
14
+ * @property {string} [execId] - Unique execution identifier
15
+ * @property {string} [cellId] - Cell identifier (for linking)
16
+ * @property {Record<string, *>} [cellMeta] - Metadata from code fence
17
+ */
18
+
19
+ /**
20
+ * @typedef {Object} ExecutionResult
21
+ * @property {boolean} success - Whether execution succeeded
22
+ * @property {string} stdout - Standard output (console.log, etc.)
23
+ * @property {string} stderr - Standard error (console.error, etc.)
24
+ * @property {*} [result] - Return value (raw)
25
+ * @property {string} [resultString] - Return value (formatted string)
26
+ * @property {ExecutionError} [error] - Error information if failed
27
+ * @property {DisplayData[]} displayData - Rich display outputs
28
+ * @property {Asset[]} assets - Generated assets
29
+ * @property {number} executionCount - Execution count in session
30
+ * @property {number} duration - Execution duration in milliseconds
31
+ * @property {string[]} [imports] - Detected imports
32
+ */
33
+
34
+ /**
35
+ * @typedef {Object} ExecutionError
36
+ * @property {string} type - Error type/class name
37
+ * @property {string} message - Error message
38
+ * @property {string[]} [traceback] - Stack trace lines
39
+ * @property {number} [line] - Line number where error occurred
40
+ * @property {number} [column] - Column number where error occurred
41
+ */
42
+
43
+ /**
44
+ * @typedef {Object} DisplayData
45
+ * @property {Record<string, string>} data - MIME type → content mapping
46
+ * @property {Record<string, *>} metadata - Additional metadata
47
+ */
48
+
49
+ /**
50
+ * @typedef {'image' | 'html' | 'json' | 'other'} AssetType
51
+ */
52
+
53
+ /**
54
+ * @typedef {Object} Asset
55
+ * @property {string} path - Asset path/identifier
56
+ * @property {string} url - URL to access asset (blob URL in browser)
57
+ * @property {string} mimeType - MIME type
58
+ * @property {AssetType} assetType - Asset type category
59
+ * @property {number} [size] - Size in bytes
60
+ */
61
+
62
+ export {};
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Type Definitions
3
+ *
4
+ * All MRP type definitions exported from one place.
5
+ * @module types
6
+ */
7
+
8
+ // Re-export all types
9
+ export * from './capabilities.js';
10
+ export * from './session.js';
11
+ export * from './execution.js';
12
+ export * from './streaming.js';
13
+ export * from './completion.js';
14
+ export * from './inspection.js';
15
+ export * from './variables.js';
16
+ export * from './analysis.js';
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Inspection Types
3
+ *
4
+ * Types for symbol inspection (MRP /inspect and /hover endpoints).
5
+ * @module types/inspection
6
+ */
7
+
8
+ /**
9
+ * @typedef {Object} InspectOptions
10
+ * @property {string} [session] - Session ID
11
+ * @property {0 | 1 | 2} [detail=0] - Detail level: 0=signature, 1=+docs, 2=+source
12
+ */
13
+
14
+ /**
15
+ * @typedef {Object} InspectResult
16
+ * @property {boolean} found - Whether symbol was found
17
+ * @property {'runtime' | 'lsp' | 'static'} source - Where info came from
18
+ * @property {string} [name] - Symbol name
19
+ * @property {string} [kind] - Symbol kind
20
+ * @property {string} [type] - Type string
21
+ * @property {string} [signature] - Function/method signature
22
+ * @property {string} [docstring] - Documentation string
23
+ * @property {string} [sourceCode] - Source code (if available)
24
+ * @property {string} [file] - File where defined
25
+ * @property {number} [line] - Line number
26
+ * @property {string} [value] - Value preview
27
+ * @property {import('./variables.js').VariableInfo[]} [children] - Children for expandable
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} HoverResult
32
+ * @property {boolean} found - Whether info was found
33
+ * @property {string} [name] - Symbol name
34
+ * @property {string} [type] - Type string
35
+ * @property {string} [value] - Value preview
36
+ * @property {string} [signature] - Function signature
37
+ */
38
+
39
+ export {};
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Session Types
3
+ *
4
+ * Types for session management (MRP /sessions endpoints).
5
+ * @module types/session
6
+ */
7
+
8
+ /**
9
+ * @typedef {'iframe' | 'worker' | 'none'} IsolationMode
10
+ */
11
+
12
+ /**
13
+ * @typedef {Object} SessionInfo
14
+ * @property {string} id - Unique session identifier
15
+ * @property {string} language - Primary language for this session
16
+ * @property {string} created - ISO timestamp of creation
17
+ * @property {string} lastActivity - ISO timestamp of last activity
18
+ * @property {number} executionCount - Number of executions
19
+ * @property {number} variableCount - Number of variables in namespace
20
+ * @property {IsolationMode} isolation - Session isolation mode
21
+ */
22
+
23
+ /**
24
+ * @typedef {Object} CreateSessionOptions
25
+ * @property {string} [id] - Session ID (generated if not provided)
26
+ * @property {string} [language='javascript'] - Primary language
27
+ * @property {IsolationMode} [isolation='iframe'] - Isolation mode
28
+ * @property {boolean} [allowMainAccess=false] - Allow access to main document
29
+ * @property {Record<string, *>} [utilities] - Custom utilities to inject
30
+ */
31
+
32
+ export {};
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Streaming Types
3
+ *
4
+ * Types for streaming execution (MRP /execute/stream endpoint).
5
+ * @module types/streaming
6
+ */
7
+
8
+ /**
9
+ * @typedef {StartEvent | StdoutEvent | StderrEvent | StdinRequestEvent | DisplayEvent | AssetEvent | ResultEvent | ErrorEvent | DoneEvent} StreamEvent
10
+ */
11
+
12
+ /**
13
+ * @typedef {Object} StartEvent
14
+ * @property {'start'} type
15
+ * @property {string} execId
16
+ * @property {string} timestamp
17
+ */
18
+
19
+ /**
20
+ * @typedef {Object} StdoutEvent
21
+ * @property {'stdout'} type
22
+ * @property {string} content
23
+ * @property {string} accumulated
24
+ */
25
+
26
+ /**
27
+ * @typedef {Object} StderrEvent
28
+ * @property {'stderr'} type
29
+ * @property {string} content
30
+ * @property {string} accumulated
31
+ */
32
+
33
+ /**
34
+ * @typedef {Object} StdinRequestEvent
35
+ * @property {'stdin_request'} type
36
+ * @property {string} prompt
37
+ * @property {boolean} password
38
+ * @property {string} execId
39
+ */
40
+
41
+ /**
42
+ * @typedef {Object} DisplayEvent
43
+ * @property {'display'} type
44
+ * @property {Record<string, string>} data
45
+ * @property {Record<string, *>} metadata
46
+ */
47
+
48
+ /**
49
+ * @typedef {Object} AssetEvent
50
+ * @property {'asset'} type
51
+ * @property {string} path
52
+ * @property {string} url
53
+ * @property {string} mimeType
54
+ * @property {string} assetType
55
+ */
56
+
57
+ /**
58
+ * @typedef {Object} ResultEvent
59
+ * @property {'result'} type
60
+ * @property {import('./execution.js').ExecutionResult} result
61
+ */
62
+
63
+ /**
64
+ * @typedef {Object} ErrorEvent
65
+ * @property {'error'} type
66
+ * @property {import('./execution.js').ExecutionError} error
67
+ */
68
+
69
+ /**
70
+ * @typedef {Object} DoneEvent
71
+ * @property {'done'} type
72
+ */
73
+
74
+ export {};
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Variables Types
3
+ *
4
+ * Types for variable inspection (MRP /variables endpoints).
5
+ * @module types/variables
6
+ */
7
+
8
+ /**
9
+ * @typedef {Object} VariableFilter
10
+ * @property {string[]} [types] - Only include these types
11
+ * @property {string} [namePattern] - Name must match this regex
12
+ * @property {boolean} [excludePrivate] - Exclude names starting with _
13
+ */
14
+
15
+ /**
16
+ * @typedef {Object} VariableInfo
17
+ * @property {string} name - Variable name
18
+ * @property {string} type - Type string
19
+ * @property {string} value - Value preview (truncated)
20
+ * @property {string} [size] - Size description (e.g., "1.2 KB")
21
+ * @property {boolean} expandable - Whether this can be expanded
22
+ * @property {number[]} [shape] - Shape for arrays/matrices
23
+ * @property {string} [dtype] - Data type for typed arrays
24
+ * @property {number} [length] - Length for arrays/strings
25
+ * @property {string[]} [keys] - Keys for objects/maps
26
+ */
27
+
28
+ /**
29
+ * @typedef {Object} VariableDetailOptions
30
+ * @property {string} [session] - Session ID
31
+ * @property {string[]} [path] - Path to drill into
32
+ * @property {number} [maxChildren=100] - Max children to return
33
+ * @property {number} [maxValueLength=1000] - Max chars for value strings
34
+ */
35
+
36
+ /**
37
+ * @typedef {Object} VariableDetail
38
+ * @property {string} name - Variable name
39
+ * @property {string} type - Type string
40
+ * @property {string} value - Value preview
41
+ * @property {string} [size] - Size description
42
+ * @property {boolean} expandable - Whether expandable
43
+ * @property {number[]} [shape] - Shape for arrays
44
+ * @property {string} [dtype] - Data type
45
+ * @property {number} [length] - Length
46
+ * @property {string[]} [keys] - Keys
47
+ * @property {string} [fullValue] - Full value (up to maxValueLength)
48
+ * @property {VariableInfo[]} [children] - Child items
49
+ * @property {string[]} [methods] - Available methods
50
+ * @property {string[]} [attributes] - Available attributes
51
+ * @property {boolean} truncated - Whether results were truncated
52
+ */
53
+
54
+ export {};
@@ -0,0 +1,301 @@
1
+ /**
2
+ * ANSI to HTML Renderer
3
+ *
4
+ * Converts ANSI escape sequences to HTML with appropriate styling.
5
+ * Useful for rendering terminal output from code execution.
6
+ *
7
+ * @module utils/ansi-renderer
8
+ */
9
+
10
+ /**
11
+ * @typedef {Object} AnsiStyle
12
+ * @property {string} [color] - Foreground color
13
+ * @property {string} [background] - Background color
14
+ * @property {boolean} [bold] - Bold text
15
+ * @property {boolean} [dim] - Dim text
16
+ * @property {boolean} [italic] - Italic text
17
+ * @property {boolean} [underline] - Underlined text
18
+ * @property {boolean} [strikethrough] - Strikethrough text
19
+ * @property {boolean} [inverse] - Inverse colors
20
+ */
21
+
22
+ /**
23
+ * ANSI color codes to CSS colors
24
+ */
25
+ const COLORS = {
26
+ 30: '#000000', // Black
27
+ 31: '#cc0000', // Red
28
+ 32: '#00cc00', // Green
29
+ 33: '#cccc00', // Yellow
30
+ 34: '#0000cc', // Blue
31
+ 35: '#cc00cc', // Magenta
32
+ 36: '#00cccc', // Cyan
33
+ 37: '#cccccc', // White
34
+ 90: '#666666', // Bright Black (Gray)
35
+ 91: '#ff0000', // Bright Red
36
+ 92: '#00ff00', // Bright Green
37
+ 93: '#ffff00', // Bright Yellow
38
+ 94: '#0000ff', // Bright Blue
39
+ 95: '#ff00ff', // Bright Magenta
40
+ 96: '#00ffff', // Bright Cyan
41
+ 97: '#ffffff', // Bright White
42
+ };
43
+
44
+ const BG_COLORS = {
45
+ 40: '#000000',
46
+ 41: '#cc0000',
47
+ 42: '#00cc00',
48
+ 43: '#cccc00',
49
+ 44: '#0000cc',
50
+ 45: '#cc00cc',
51
+ 46: '#00cccc',
52
+ 47: '#cccccc',
53
+ 100: '#666666',
54
+ 101: '#ff0000',
55
+ 102: '#00ff00',
56
+ 103: '#ffff00',
57
+ 104: '#0000ff',
58
+ 105: '#ff00ff',
59
+ 106: '#00ffff',
60
+ 107: '#ffffff',
61
+ };
62
+
63
+ /**
64
+ * ANSI Renderer class
65
+ */
66
+ export class AnsiRenderer {
67
+ /** @type {boolean} */
68
+ #escapeHtml = true;
69
+
70
+ /**
71
+ * Create ANSI renderer
72
+ * @param {{ escapeHtml?: boolean }} [options]
73
+ */
74
+ constructor(options = {}) {
75
+ this.#escapeHtml = options.escapeHtml !== false;
76
+ }
77
+
78
+ /**
79
+ * Convert ANSI text to HTML
80
+ *
81
+ * @param {string} text - Text with ANSI escape sequences
82
+ * @returns {string} HTML string
83
+ */
84
+ render(text) {
85
+ if (!text) return '';
86
+
87
+ /** @type {AnsiStyle} */
88
+ let currentStyle = {};
89
+ const parts = [];
90
+ let currentText = '';
91
+
92
+ // ANSI escape sequence regex
93
+ const ansiRegex = /\x1b\[([0-9;]*)m/g;
94
+ let lastIndex = 0;
95
+ let match;
96
+
97
+ while ((match = ansiRegex.exec(text)) !== null) {
98
+ // Add text before this escape sequence
99
+ const beforeText = text.slice(lastIndex, match.index);
100
+ if (beforeText) {
101
+ currentText += beforeText;
102
+ }
103
+
104
+ // Parse codes
105
+ const codes = match[1].split(';').map(c => parseInt(c, 10) || 0);
106
+
107
+ // Flush current text with current style
108
+ if (currentText) {
109
+ parts.push(this.#wrapWithStyle(currentText, currentStyle));
110
+ currentText = '';
111
+ }
112
+
113
+ // Update style based on codes
114
+ currentStyle = this.#updateStyle(currentStyle, codes);
115
+
116
+ lastIndex = ansiRegex.lastIndex;
117
+ }
118
+
119
+ // Add remaining text
120
+ const remainingText = text.slice(lastIndex);
121
+ if (remainingText) {
122
+ currentText += remainingText;
123
+ }
124
+
125
+ if (currentText) {
126
+ parts.push(this.#wrapWithStyle(currentText, currentStyle));
127
+ }
128
+
129
+ return parts.join('');
130
+ }
131
+
132
+ /**
133
+ * Render to a DOM element
134
+ *
135
+ * @param {string} text - ANSI text
136
+ * @param {HTMLElement} container - Target container
137
+ * @param {{ clear?: boolean }} [options]
138
+ */
139
+ renderTo(text, container, options = {}) {
140
+ const html = this.render(text);
141
+
142
+ if (options.clear !== false) {
143
+ container.innerHTML = '';
144
+ }
145
+
146
+ const wrapper = document.createElement('pre');
147
+ wrapper.className = 'ansi-output';
148
+ wrapper.innerHTML = html;
149
+ container.appendChild(wrapper);
150
+ }
151
+
152
+ /**
153
+ * Update style based on ANSI codes
154
+ * @param {AnsiStyle} style
155
+ * @param {number[]} codes
156
+ * @returns {AnsiStyle}
157
+ */
158
+ #updateStyle(style, codes) {
159
+ const newStyle = { ...style };
160
+
161
+ for (const code of codes) {
162
+ if (code === 0) {
163
+ // Reset all
164
+ return {};
165
+ } else if (code === 1) {
166
+ newStyle.bold = true;
167
+ } else if (code === 2) {
168
+ newStyle.dim = true;
169
+ } else if (code === 3) {
170
+ newStyle.italic = true;
171
+ } else if (code === 4) {
172
+ newStyle.underline = true;
173
+ } else if (code === 7) {
174
+ newStyle.inverse = true;
175
+ } else if (code === 9) {
176
+ newStyle.strikethrough = true;
177
+ } else if (code === 22) {
178
+ newStyle.bold = false;
179
+ newStyle.dim = false;
180
+ } else if (code === 23) {
181
+ newStyle.italic = false;
182
+ } else if (code === 24) {
183
+ newStyle.underline = false;
184
+ } else if (code === 27) {
185
+ newStyle.inverse = false;
186
+ } else if (code === 29) {
187
+ newStyle.strikethrough = false;
188
+ } else if (code === 39) {
189
+ delete newStyle.color;
190
+ } else if (code === 49) {
191
+ delete newStyle.background;
192
+ } else if (code >= 30 && code <= 37) {
193
+ newStyle.color = COLORS[code];
194
+ } else if (code >= 40 && code <= 47) {
195
+ newStyle.background = BG_COLORS[code];
196
+ } else if (code >= 90 && code <= 97) {
197
+ newStyle.color = COLORS[code];
198
+ } else if (code >= 100 && code <= 107) {
199
+ newStyle.background = BG_COLORS[code];
200
+ }
201
+ // TODO: 256 color and RGB support (38;5;n and 38;2;r;g;b)
202
+ }
203
+
204
+ return newStyle;
205
+ }
206
+
207
+ /**
208
+ * Wrap text with style span
209
+ * @param {string} text
210
+ * @param {AnsiStyle} style
211
+ * @returns {string}
212
+ */
213
+ #wrapWithStyle(text, style) {
214
+ // Escape HTML if needed
215
+ let escaped = text;
216
+ if (this.#escapeHtml) {
217
+ escaped = text
218
+ .replace(/&/g, '&amp;')
219
+ .replace(/</g, '&lt;')
220
+ .replace(/>/g, '&gt;')
221
+ .replace(/"/g, '&quot;')
222
+ .replace(/'/g, '&#39;');
223
+ }
224
+
225
+ // No style needed
226
+ if (Object.keys(style).length === 0) {
227
+ return escaped;
228
+ }
229
+
230
+ // Build inline style
231
+ const styles = [];
232
+
233
+ if (style.color) {
234
+ styles.push(`color:${style.color}`);
235
+ }
236
+ if (style.background) {
237
+ styles.push(`background-color:${style.background}`);
238
+ }
239
+ if (style.bold) {
240
+ styles.push('font-weight:bold');
241
+ }
242
+ if (style.dim) {
243
+ styles.push('opacity:0.5');
244
+ }
245
+ if (style.italic) {
246
+ styles.push('font-style:italic');
247
+ }
248
+ if (style.underline) {
249
+ styles.push('text-decoration:underline');
250
+ }
251
+ if (style.strikethrough) {
252
+ if (style.underline) {
253
+ styles.push('text-decoration:underline line-through');
254
+ } else {
255
+ styles.push('text-decoration:line-through');
256
+ }
257
+ }
258
+
259
+ if (styles.length === 0) {
260
+ return escaped;
261
+ }
262
+
263
+ return `<span style="${styles.join(';')}">${escaped}</span>`;
264
+ }
265
+
266
+ /**
267
+ * Strip ANSI codes from text
268
+ * @param {string} text
269
+ * @returns {string}
270
+ */
271
+ static strip(text) {
272
+ return text.replace(/\x1b\[[0-9;]*m/g, '');
273
+ }
274
+ }
275
+
276
+ /**
277
+ * Convert ANSI text to HTML (convenience function)
278
+ * @param {string} text
279
+ * @returns {string}
280
+ */
281
+ export function ansiToHtml(text) {
282
+ return new AnsiRenderer().render(text);
283
+ }
284
+
285
+ /**
286
+ * Strip ANSI codes (convenience function)
287
+ * @param {string} text
288
+ * @returns {string}
289
+ */
290
+ export function stripAnsi(text) {
291
+ return AnsiRenderer.strip(text);
292
+ }
293
+
294
+ /**
295
+ * Create an ANSI renderer
296
+ * @param {{ escapeHtml?: boolean }} [options]
297
+ * @returns {AnsiRenderer}
298
+ */
299
+ export function createAnsiRenderer(options) {
300
+ return new AnsiRenderer(options);
301
+ }