wu-framework 1.1.6 → 1.1.8

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 (90) hide show
  1. package/README.md +511 -977
  2. package/dist/wu-framework.cjs.js +3 -1
  3. package/dist/wu-framework.cjs.js.map +1 -0
  4. package/dist/wu-framework.dev.js +7533 -2761
  5. package/dist/wu-framework.dev.js.map +1 -1
  6. package/dist/wu-framework.esm.js +3 -0
  7. package/dist/wu-framework.esm.js.map +1 -0
  8. package/dist/wu-framework.umd.js +3 -1
  9. package/dist/wu-framework.umd.js.map +1 -0
  10. package/integrations/astro/README.md +127 -0
  11. package/integrations/astro/WuApp.astro +63 -0
  12. package/integrations/astro/WuShell.astro +39 -0
  13. package/integrations/astro/index.js +68 -0
  14. package/integrations/astro/package.json +38 -0
  15. package/integrations/astro/types.d.ts +53 -0
  16. package/package.json +94 -74
  17. package/src/adapters/angular/ai.js +30 -0
  18. package/src/adapters/angular/index.d.ts +154 -0
  19. package/src/adapters/angular/index.js +932 -0
  20. package/src/adapters/angular.d.ts +3 -154
  21. package/src/adapters/angular.js +3 -813
  22. package/src/adapters/index.js +35 -24
  23. package/src/adapters/lit/ai.js +20 -0
  24. package/src/adapters/lit/index.d.ts +120 -0
  25. package/src/adapters/lit/index.js +721 -0
  26. package/src/adapters/lit.d.ts +3 -120
  27. package/src/adapters/lit.js +3 -726
  28. package/src/adapters/preact/ai.js +33 -0
  29. package/src/adapters/preact/index.d.ts +108 -0
  30. package/src/adapters/preact/index.js +661 -0
  31. package/src/adapters/preact.d.ts +3 -108
  32. package/src/adapters/preact.js +3 -665
  33. package/src/adapters/react/ai.js +135 -0
  34. package/src/adapters/react/index.d.ts +246 -0
  35. package/src/adapters/react/index.js +689 -0
  36. package/src/adapters/react.d.ts +3 -212
  37. package/src/adapters/react.js +3 -513
  38. package/src/adapters/shared.js +64 -0
  39. package/src/adapters/solid/ai.js +32 -0
  40. package/src/adapters/solid/index.d.ts +101 -0
  41. package/src/adapters/solid/index.js +586 -0
  42. package/src/adapters/solid.d.ts +3 -101
  43. package/src/adapters/solid.js +3 -591
  44. package/src/adapters/svelte/ai.js +31 -0
  45. package/src/adapters/svelte/index.d.ts +166 -0
  46. package/src/adapters/svelte/index.js +798 -0
  47. package/src/adapters/svelte.d.ts +3 -166
  48. package/src/adapters/svelte.js +3 -803
  49. package/src/adapters/vanilla/ai.js +30 -0
  50. package/src/adapters/vanilla/index.d.ts +179 -0
  51. package/src/adapters/vanilla/index.js +785 -0
  52. package/src/adapters/vanilla.d.ts +3 -179
  53. package/src/adapters/vanilla.js +3 -791
  54. package/src/adapters/vue/ai.js +52 -0
  55. package/src/adapters/vue/index.d.ts +299 -0
  56. package/src/adapters/vue/index.js +608 -0
  57. package/src/adapters/vue.d.ts +3 -299
  58. package/src/adapters/vue.js +3 -611
  59. package/src/ai/wu-ai-actions.js +261 -0
  60. package/src/ai/wu-ai-browser.js +663 -0
  61. package/src/ai/wu-ai-context.js +332 -0
  62. package/src/ai/wu-ai-conversation.js +554 -0
  63. package/src/ai/wu-ai-permissions.js +381 -0
  64. package/src/ai/wu-ai-provider.js +605 -0
  65. package/src/ai/wu-ai-schema.js +225 -0
  66. package/src/ai/wu-ai-triggers.js +396 -0
  67. package/src/ai/wu-ai.js +474 -0
  68. package/src/core/wu-app.js +50 -8
  69. package/src/core/wu-cache.js +1 -1
  70. package/src/core/wu-core.js +645 -677
  71. package/src/core/wu-html-parser.js +121 -211
  72. package/src/core/wu-iframe-sandbox.js +328 -0
  73. package/src/core/wu-mcp-bridge.js +647 -0
  74. package/src/core/wu-overrides.js +510 -0
  75. package/src/core/wu-prefetch.js +414 -0
  76. package/src/core/wu-proxy-sandbox.js +398 -75
  77. package/src/core/wu-sandbox.js +86 -268
  78. package/src/core/wu-script-executor.js +79 -182
  79. package/src/core/wu-snapshot-sandbox.js +149 -106
  80. package/src/core/wu-strategies.js +13 -0
  81. package/src/core/wu-style-bridge.js +0 -2
  82. package/src/index.js +139 -665
  83. package/dist/wu-framework.hex.js +0 -23
  84. package/dist/wu-framework.min.js +0 -1
  85. package/dist/wu-framework.obf.js +0 -1
  86. package/scripts/build-protected.js +0 -366
  87. package/scripts/build.js +0 -212
  88. package/scripts/rollup-plugin-hex.js +0 -143
  89. package/src/core/wu-registry.js +0 -60
  90. package/src/core/wu-sandbox-pool.js +0 -390
@@ -0,0 +1,663 @@
1
+ /**
2
+ * WU-AI Browser Actions
3
+ *
4
+ * Registers browser automation tools into wu.ai so any LLM provider
5
+ * (OpenAI, Claude, Gemini, Ollama, etc.) can autonomously see and
6
+ * control the page — no human intervention required.
7
+ *
8
+ * Tools registered:
9
+ * browser_screenshot — Capture page/element as PNG (Canvas API)
10
+ * browser_click — Click element by selector or visible text
11
+ * browser_type — Type into inputs (React/Vue/framework compatible)
12
+ * browser_snapshot — Get accessibility tree of the DOM
13
+ * browser_navigate — Navigate SPA routes
14
+ * browser_network — View captured HTTP requests (fetch + XHR)
15
+ * browser_console — View captured console messages
16
+ * browser_info — Get page state: apps, store, URL, viewport
17
+ * browser_select — Select option in dropdowns
18
+ * browser_scroll — Scroll page or element
19
+ *
20
+ * @example
21
+ * // Auto-registered when wu.ai initializes
22
+ * // Any LLM connected via wu.ai.provider can now use these tools:
23
+ * const tools = wu.ai.tools();
24
+ * // → includes browser_screenshot, browser_click, etc.
25
+ */
26
+
27
+ // ── Shared capture buffers ──
28
+ const networkLog = [];
29
+ const MAX_NETWORK_LOG = 300;
30
+ const consoleLog = [];
31
+ const MAX_CONSOLE_LOG = 500;
32
+ let interceptorsInstalled = false;
33
+
34
+ /**
35
+ * Register all browser automation actions into a WuAI instance.
36
+ *
37
+ * @param {object} ai - The WuAI instance (wu.ai)
38
+ * @param {object} wu - The Wu Framework instance (window.wu)
39
+ */
40
+ export function registerBrowserActions(ai, wu) {
41
+ // Install interceptors only once
42
+ if (!interceptorsInstalled) {
43
+ _installNetworkInterceptor();
44
+ _installConsoleInterceptor();
45
+ interceptorsInstalled = true;
46
+ }
47
+
48
+ // ════════════════════════════════════════════
49
+ // SCREENSHOT — Canvas API (SVG foreignObject)
50
+ // ════════════════════════════════════════════
51
+
52
+ ai.action('browser_screenshot', {
53
+ description: 'Take a screenshot of the current page or a specific element. Returns a base64 PNG image. Use this to SEE what the user sees.',
54
+ parameters: {
55
+ selector: {
56
+ type: 'string',
57
+ description: 'CSS selector of the element to capture. Empty = full visible page.',
58
+ required: false,
59
+ },
60
+ },
61
+ handler: async (params) => {
62
+ const target = params.selector
63
+ ? document.querySelector(params.selector)
64
+ : document.documentElement;
65
+
66
+ if (!target) return { error: `Element not found: ${params.selector}` };
67
+
68
+ const rect = target.getBoundingClientRect();
69
+ const w = Math.ceil(Math.min(rect.width || window.innerWidth, 1920));
70
+ const h = Math.ceil(Math.min(rect.height || window.innerHeight, 1080));
71
+
72
+ // Clone and inline styles for accurate rendering
73
+ const clone = target.cloneNode(true);
74
+ _inlineComputedStyles(target, clone);
75
+
76
+ const serializer = new XMLSerializer();
77
+ const xhtml = serializer.serializeToString(clone);
78
+
79
+ const svgStr = [
80
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}">`,
81
+ '<foreignObject width="100%" height="100%">',
82
+ `<div xmlns="http://www.w3.org/1999/xhtml" style="width:${w}px;height:${h}px;overflow:hidden;">`,
83
+ xhtml,
84
+ '</div>',
85
+ '</foreignObject>',
86
+ '</svg>',
87
+ ].join('');
88
+
89
+ const svgBlob = new Blob([svgStr], { type: 'image/svg+xml;charset=utf-8' });
90
+ const url = URL.createObjectURL(svgBlob);
91
+
92
+ const dataUrl = await new Promise((resolve) => {
93
+ const img = new Image();
94
+ img.onload = () => {
95
+ const canvas = document.createElement('canvas');
96
+ canvas.width = w;
97
+ canvas.height = h;
98
+ const ctx = canvas.getContext('2d');
99
+ ctx.drawImage(img, 0, 0);
100
+ URL.revokeObjectURL(url);
101
+ resolve(canvas.toDataURL('image/png', 0.8));
102
+ };
103
+ img.onerror = () => {
104
+ URL.revokeObjectURL(url);
105
+ resolve(null);
106
+ };
107
+ img.src = url;
108
+ });
109
+
110
+ if (!dataUrl) return { error: 'Canvas rendering failed' };
111
+
112
+ const base64 = dataUrl.split(',')[1];
113
+ return {
114
+ width: w,
115
+ height: h,
116
+ format: 'png',
117
+ base64,
118
+ sizeKB: Math.round((base64.length * 3) / 4 / 1024),
119
+ };
120
+ },
121
+ permissions: [],
122
+ });
123
+
124
+ // ════════════════════════════════════════════
125
+ // CLICK
126
+ // ════════════════════════════════════════════
127
+
128
+ ai.action('browser_click', {
129
+ description: 'Click an element on the page. Find by CSS selector or by visible text content. Use this to interact with buttons, links, tabs, etc.',
130
+ parameters: {
131
+ selector: {
132
+ type: 'string',
133
+ description: 'CSS selector (e.g. "#submit-btn", ".nav-link", "button[type=submit]")',
134
+ required: false,
135
+ },
136
+ text: {
137
+ type: 'string',
138
+ description: 'Visible text to find and click (e.g. "Submit", "Next", "Guardar"). Searches buttons, links, and clickable elements.',
139
+ required: false,
140
+ },
141
+ },
142
+ handler: async (params, api) => {
143
+ let el = null;
144
+
145
+ if (params.selector) {
146
+ el = document.querySelector(params.selector);
147
+ }
148
+
149
+ if (!el && params.text) {
150
+ const candidates = document.querySelectorAll(
151
+ 'button, a, [role="button"], input[type="submit"], input[type="button"], [data-click], label, [onclick]'
152
+ );
153
+ const searchText = params.text.toLowerCase();
154
+ for (const candidate of candidates) {
155
+ if (candidate.textContent?.trim().toLowerCase().includes(searchText)) {
156
+ el = candidate;
157
+ break;
158
+ }
159
+ }
160
+ }
161
+
162
+ if (!el) return { error: `Element not found: ${params.selector || `text="${params.text}"`}` };
163
+
164
+ el.scrollIntoView({ behavior: 'instant', block: 'center' });
165
+ el.click();
166
+
167
+ const tag = el.tagName?.toLowerCase();
168
+ const id = el.id ? `#${el.id}` : '';
169
+ api.emit?.('browser:clicked', { selector: params.selector, text: params.text });
170
+
171
+ return {
172
+ clicked: `${tag}${id}`,
173
+ text: el.textContent?.trim().slice(0, 100) || '',
174
+ };
175
+ },
176
+ permissions: ['emitEvents'],
177
+ });
178
+
179
+ // ════════════════════════════════════════════
180
+ // TYPE
181
+ // ════════════════════════════════════════════
182
+
183
+ ai.action('browser_type', {
184
+ description: 'Type text into an input, textarea, or contenteditable element. Works with React, Vue, Angular, and other frameworks. Can optionally clear existing text first and submit the form.',
185
+ parameters: {
186
+ selector: {
187
+ type: 'string',
188
+ description: 'CSS selector of the input (e.g. "#email", "input[name=search]", "textarea.comment")',
189
+ required: true,
190
+ },
191
+ text: {
192
+ type: 'string',
193
+ description: 'Text to type into the element',
194
+ required: true,
195
+ },
196
+ clear: {
197
+ type: 'boolean',
198
+ description: 'Clear existing value before typing (default: false)',
199
+ required: false,
200
+ },
201
+ submit: {
202
+ type: 'boolean',
203
+ description: 'Submit the form or press Enter after typing (default: false)',
204
+ required: false,
205
+ },
206
+ },
207
+ handler: async (params, api) => {
208
+ const el = document.querySelector(params.selector);
209
+ if (!el) return { error: `Element not found: ${params.selector}` };
210
+
211
+ el.focus();
212
+
213
+ if (params.clear) {
214
+ el.value = '';
215
+ el.dispatchEvent(new Event('input', { bubbles: true }));
216
+ }
217
+
218
+ // Use native setter to trigger framework reactivity (React, Vue, etc.)
219
+ const nativeSetter = Object.getOwnPropertyDescriptor(
220
+ window.HTMLInputElement.prototype, 'value'
221
+ )?.set || Object.getOwnPropertyDescriptor(
222
+ window.HTMLTextAreaElement.prototype, 'value'
223
+ )?.set;
224
+
225
+ const newValue = params.clear ? params.text : (el.value || '') + params.text;
226
+ if (nativeSetter) {
227
+ nativeSetter.call(el, newValue);
228
+ } else {
229
+ el.value = newValue;
230
+ }
231
+
232
+ el.dispatchEvent(new Event('input', { bubbles: true }));
233
+ el.dispatchEvent(new Event('change', { bubbles: true }));
234
+
235
+ if (params.submit) {
236
+ const form = el.closest('form');
237
+ if (form) {
238
+ form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }));
239
+ } else {
240
+ el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', bubbles: true }));
241
+ }
242
+ }
243
+
244
+ api.emit?.('browser:typed', { selector: params.selector, length: params.text.length });
245
+
246
+ return {
247
+ selector: params.selector,
248
+ typed: params.text,
249
+ currentValue: el.value?.slice(0, 200),
250
+ submitted: !!params.submit,
251
+ };
252
+ },
253
+ permissions: ['emitEvents'],
254
+ });
255
+
256
+ // ════════════════════════════════════════════
257
+ // SELECT (dropdowns)
258
+ // ════════════════════════════════════════════
259
+
260
+ ai.action('browser_select', {
261
+ description: 'Select an option in a <select> dropdown or a custom dropdown component.',
262
+ parameters: {
263
+ selector: {
264
+ type: 'string',
265
+ description: 'CSS selector of the <select> element',
266
+ required: true,
267
+ },
268
+ value: {
269
+ type: 'string',
270
+ description: 'The value attribute of the option to select. Use "text:" prefix to match by visible text (e.g. "text:Mexico")',
271
+ required: true,
272
+ },
273
+ },
274
+ handler: async (params, api) => {
275
+ const el = document.querySelector(params.selector);
276
+ if (!el) return { error: `Element not found: ${params.selector}` };
277
+
278
+ if (el.tagName?.toLowerCase() === 'select') {
279
+ const options = Array.from(el.options);
280
+ let option;
281
+
282
+ if (params.value.startsWith('text:')) {
283
+ const searchText = params.value.slice(5).toLowerCase();
284
+ option = options.find((o) => o.textContent.trim().toLowerCase().includes(searchText));
285
+ } else {
286
+ option = options.find((o) => o.value === params.value);
287
+ }
288
+
289
+ if (!option) return { error: `Option not found: ${params.value}` };
290
+
291
+ el.value = option.value;
292
+ el.dispatchEvent(new Event('change', { bubbles: true }));
293
+ el.dispatchEvent(new Event('input', { bubbles: true }));
294
+
295
+ api.emit?.('browser:selected', { selector: params.selector, value: option.value });
296
+ return { selected: option.value, text: option.textContent.trim() };
297
+ }
298
+
299
+ // Custom dropdown: try clicking the trigger, then the option
300
+ el.click();
301
+ return { clicked: params.selector, note: 'Custom dropdown — clicked trigger. Use browser_click to select an option from the opened menu.' };
302
+ },
303
+ permissions: ['emitEvents'],
304
+ });
305
+
306
+ // ════════════════════════════════════════════
307
+ // SCROLL
308
+ // ════════════════════════════════════════════
309
+
310
+ ai.action('browser_scroll', {
311
+ description: 'Scroll the page or a specific element. Use to reveal content that is not visible.',
312
+ parameters: {
313
+ direction: {
314
+ type: 'string',
315
+ description: 'Direction: "up", "down", "top", "bottom"',
316
+ required: true,
317
+ },
318
+ selector: {
319
+ type: 'string',
320
+ description: 'CSS selector of scrollable container (empty = page)',
321
+ required: false,
322
+ },
323
+ amount: {
324
+ type: 'number',
325
+ description: 'Pixels to scroll (default: 500). Ignored for "top"/"bottom".',
326
+ required: false,
327
+ },
328
+ },
329
+ handler: async (params) => {
330
+ const target = params.selector
331
+ ? document.querySelector(params.selector)
332
+ : window;
333
+ const amount = params.amount || 500;
334
+
335
+ if (params.selector && !target) return { error: `Element not found: ${params.selector}` };
336
+
337
+ const scrollEl = target === window ? document.documentElement : target;
338
+
339
+ switch (params.direction) {
340
+ case 'up': scrollEl.scrollBy({ top: -amount, behavior: 'smooth' }); break;
341
+ case 'down': scrollEl.scrollBy({ top: amount, behavior: 'smooth' }); break;
342
+ case 'top': scrollEl.scrollTo({ top: 0, behavior: 'smooth' }); break;
343
+ case 'bottom': scrollEl.scrollTo({ top: scrollEl.scrollHeight, behavior: 'smooth' }); break;
344
+ default: return { error: `Invalid direction: ${params.direction}` };
345
+ }
346
+
347
+ return {
348
+ scrolled: params.direction,
349
+ amount: params.direction === 'top' || params.direction === 'bottom' ? 'max' : amount,
350
+ currentScroll: scrollEl.scrollTop,
351
+ };
352
+ },
353
+ permissions: [],
354
+ });
355
+
356
+ // ════════════════════════════════════════════
357
+ // SNAPSHOT — Accessibility tree
358
+ // ════════════════════════════════════════════
359
+
360
+ ai.action('browser_snapshot', {
361
+ description: 'Get a text representation of the visible DOM structure (accessibility tree). Use this to understand what elements are on the page, their roles, IDs, and text content. Cheaper and faster than a screenshot.',
362
+ parameters: {
363
+ selector: {
364
+ type: 'string',
365
+ description: 'CSS selector to snapshot (empty = full page). Use "[data-wu-app=appName]" for a specific micro-app.',
366
+ required: false,
367
+ },
368
+ depth: {
369
+ type: 'number',
370
+ description: 'Max depth to traverse (default: 5)',
371
+ required: false,
372
+ },
373
+ },
374
+ handler: async (params) => {
375
+ const target = params.selector
376
+ ? document.querySelector(params.selector)
377
+ : document.body;
378
+
379
+ if (!target) return { error: `Element not found: ${params.selector}` };
380
+
381
+ const tree = _buildA11yTree(target, 0, params.depth || 5);
382
+ return { snapshot: tree };
383
+ },
384
+ permissions: [],
385
+ });
386
+
387
+ // ════════════════════════════════════════════
388
+ // NAVIGATE
389
+ // ════════════════════════════════════════════
390
+
391
+ ai.action('browser_navigate', {
392
+ description: 'Navigate to a route within the SPA application. Emits a shell:navigate event and updates the store.',
393
+ parameters: {
394
+ route: {
395
+ type: 'string',
396
+ description: 'Route path (e.g. "/dashboard", "/users", "/pos/cotizador")',
397
+ required: true,
398
+ },
399
+ },
400
+ handler: async (params, api) => {
401
+ api.emit?.('shell:navigate', { route: params.route });
402
+ api.setState?.('currentPath', params.route);
403
+ return { navigated: params.route };
404
+ },
405
+ permissions: ['emitEvents', 'writeStore'],
406
+ });
407
+
408
+ // ════════════════════════════════════════════
409
+ // NETWORK — Captured HTTP requests
410
+ // ════════════════════════════════════════════
411
+
412
+ ai.action('browser_network', {
413
+ description: 'View captured HTTP network requests (fetch and XHR). Shows URL, method, status code, duration, and size. Use to debug API calls, check for errors, or monitor performance.',
414
+ parameters: {
415
+ method: {
416
+ type: 'string',
417
+ description: 'Filter by HTTP method: GET, POST, PUT, DELETE (empty = all)',
418
+ required: false,
419
+ },
420
+ status: {
421
+ type: 'string',
422
+ description: 'Filter: "2" (2xx success), "4" (4xx errors), "5" (5xx errors), "error" (all failures)',
423
+ required: false,
424
+ },
425
+ limit: {
426
+ type: 'number',
427
+ description: 'Max requests to return (default: 30)',
428
+ required: false,
429
+ },
430
+ },
431
+ handler: async (params) => {
432
+ let filtered = networkLog;
433
+ if (params.method) {
434
+ filtered = filtered.filter((r) => r.method === params.method.toUpperCase());
435
+ }
436
+ if (params.status) {
437
+ if (params.status === 'error') {
438
+ filtered = filtered.filter((r) => r.status === 0 || r.status >= 400);
439
+ } else {
440
+ filtered = filtered.filter((r) => String(r.status).startsWith(params.status));
441
+ }
442
+ }
443
+ const limit = params.limit || 30;
444
+ return {
445
+ requests: filtered.slice(-limit),
446
+ total: networkLog.length,
447
+ showing: Math.min(filtered.length, limit),
448
+ };
449
+ },
450
+ permissions: [],
451
+ });
452
+
453
+ // ════════════════════════════════════════════
454
+ // CONSOLE — Captured logs
455
+ // ════════════════════════════════════════════
456
+
457
+ ai.action('browser_console', {
458
+ description: 'View captured browser console messages (log, warn, error). Use to check for errors, warnings, or debug output.',
459
+ parameters: {
460
+ level: {
461
+ type: 'string',
462
+ description: 'Filter by level: "log", "warn", "error" (empty = all)',
463
+ required: false,
464
+ },
465
+ limit: {
466
+ type: 'number',
467
+ description: 'Max messages to return (default: 30)',
468
+ required: false,
469
+ },
470
+ },
471
+ handler: async (params) => {
472
+ const filtered = params.level
473
+ ? consoleLog.filter((m) => m.level === params.level)
474
+ : consoleLog;
475
+ const limit = params.limit || 30;
476
+ return {
477
+ messages: filtered.slice(-limit),
478
+ total: consoleLog.length,
479
+ showing: Math.min(filtered.length, limit),
480
+ };
481
+ },
482
+ permissions: [],
483
+ });
484
+
485
+ // ════════════════════════════════════════════
486
+ // INFO — Page state overview
487
+ // ════════════════════════════════════════════
488
+
489
+ ai.action('browser_info', {
490
+ description: 'Get an overview of the current page state: URL, viewport size, mounted micro-apps, store keys, visible elements summary. Use this FIRST to understand the page before taking actions.',
491
+ parameters: {},
492
+ handler: async (params, api) => {
493
+ const apps = [];
494
+
495
+ // Discover mounted apps
496
+ if (wu._apps) {
497
+ for (const [name, app] of Object.entries(wu._apps)) {
498
+ apps.push({
499
+ name,
500
+ mounted: app.mounted || app.isMounted || false,
501
+ status: app.status || 'unknown',
502
+ });
503
+ }
504
+ }
505
+ if (apps.length === 0) {
506
+ document.querySelectorAll('[data-wu-app]').forEach((el) => {
507
+ apps.push({ name: el.getAttribute('data-wu-app'), mounted: true });
508
+ });
509
+ }
510
+
511
+ const storeData = api.getState?.('') || {};
512
+ const storeKeys = typeof storeData === 'object' ? Object.keys(storeData) : [];
513
+
514
+ return {
515
+ url: window.location.href,
516
+ title: document.title,
517
+ viewport: { width: window.innerWidth, height: window.innerHeight },
518
+ apps,
519
+ storeKeys,
520
+ networkRequests: networkLog.length,
521
+ consoleMessages: consoleLog.length,
522
+ consoleErrors: consoleLog.filter((m) => m.level === 'error').length,
523
+ };
524
+ },
525
+ permissions: ['readStore'],
526
+ });
527
+ }
528
+
529
+ // ── Private: Inline computed styles for Canvas rendering ──
530
+
531
+ function _inlineComputedStyles(source, clone) {
532
+ const props = ['color', 'background', 'background-color', 'font-family',
533
+ 'font-size', 'font-weight', 'border', 'border-radius', 'padding', 'margin',
534
+ 'display', 'flex-direction', 'align-items', 'justify-content', 'gap',
535
+ 'width', 'height', 'max-width', 'max-height', 'overflow', 'opacity',
536
+ 'box-shadow', 'text-align', 'line-height', 'position', 'top', 'left',
537
+ 'right', 'bottom', 'z-index', 'transform', 'visibility'];
538
+
539
+ try {
540
+ const style = window.getComputedStyle(source);
541
+ for (const prop of props) {
542
+ const val = style.getPropertyValue(prop);
543
+ if (val) clone.style?.setProperty(prop, val);
544
+ }
545
+ } catch (_) { /* skip */ }
546
+
547
+ const srcKids = source.children || [];
548
+ const cloneKids = clone.children || [];
549
+ const max = Math.min(srcKids.length, cloneKids.length, 200);
550
+ for (let i = 0; i < max; i++) {
551
+ _inlineComputedStyles(srcKids[i], cloneKids[i]);
552
+ }
553
+ }
554
+
555
+ // ── Private: Build accessibility tree ──
556
+
557
+ function _buildA11yTree(el, depth, maxDepth) {
558
+ if (depth > maxDepth || !el) return '';
559
+
560
+ const indent = ' '.repeat(depth);
561
+ const tag = el.tagName?.toLowerCase() || '';
562
+ const role = el.getAttribute?.('role') || '';
563
+ const ariaLabel = el.getAttribute?.('aria-label') || '';
564
+ const text = el.childNodes?.length === 1 && el.childNodes[0].nodeType === 3
565
+ ? el.textContent?.trim().slice(0, 80) : '';
566
+
567
+ let line = `${indent}<${tag}`;
568
+ if (el.id) line += ` id="${el.id}"`;
569
+ if (role) line += ` role="${role}"`;
570
+ if (ariaLabel) line += ` aria-label="${ariaLabel}"`;
571
+ if (el.className && typeof el.className === 'string') {
572
+ const cls = el.className.trim().slice(0, 60);
573
+ if (cls) line += ` class="${cls}"`;
574
+ }
575
+ line += '>';
576
+ if (text) line += ` "${text}"`;
577
+
578
+ let result = line + '\n';
579
+ const root = el.shadowRoot || el;
580
+ const children = root.children || [];
581
+
582
+ for (let i = 0; i < children.length && i < 50; i++) {
583
+ result += _buildA11yTree(children[i], depth + 1, maxDepth);
584
+ }
585
+ return result;
586
+ }
587
+
588
+ // ── Private: Network interceptor ──
589
+
590
+ function _installNetworkInterceptor() {
591
+ // Intercept fetch
592
+ const originalFetch = window.fetch;
593
+ window.fetch = async function (...args) {
594
+ const start = Date.now();
595
+ const req = args[0];
596
+ const url = typeof req === 'string' ? req : req?.url || '';
597
+ const method = (args[1]?.method || req?.method || 'GET').toUpperCase();
598
+
599
+ try {
600
+ const response = await originalFetch.apply(window, args);
601
+ const size = parseInt(response.headers?.get('content-length') || '0', 10);
602
+ networkLog.push({
603
+ type: 'fetch', method, url,
604
+ status: response.status, statusText: response.statusText,
605
+ duration: Date.now() - start, size, timestamp: start,
606
+ });
607
+ if (networkLog.length > MAX_NETWORK_LOG) networkLog.shift();
608
+ return response;
609
+ } catch (err) {
610
+ networkLog.push({
611
+ type: 'fetch', method, url,
612
+ status: 0, error: err.message,
613
+ duration: Date.now() - start, timestamp: start,
614
+ });
615
+ if (networkLog.length > MAX_NETWORK_LOG) networkLog.shift();
616
+ throw err;
617
+ }
618
+ };
619
+
620
+ // Intercept XMLHttpRequest
621
+ const origOpen = XMLHttpRequest.prototype.open;
622
+ const origSend = XMLHttpRequest.prototype.send;
623
+
624
+ XMLHttpRequest.prototype.open = function (method, url, ...rest) {
625
+ this._wuAi = { method: (method || 'GET').toUpperCase(), url: String(url) };
626
+ return origOpen.call(this, method, url, ...rest);
627
+ };
628
+
629
+ XMLHttpRequest.prototype.send = function (...args) {
630
+ if (this._wuAi) {
631
+ this._wuAi.start = Date.now();
632
+ this.addEventListener('loadend', () => {
633
+ networkLog.push({
634
+ type: 'xhr', method: this._wuAi.method, url: this._wuAi.url,
635
+ status: this.status, statusText: this.statusText,
636
+ duration: Date.now() - this._wuAi.start,
637
+ size: parseInt(this.getResponseHeader('content-length') || '0', 10),
638
+ timestamp: this._wuAi.start,
639
+ });
640
+ if (networkLog.length > MAX_NETWORK_LOG) networkLog.shift();
641
+ });
642
+ }
643
+ return origSend.apply(this, args);
644
+ };
645
+ }
646
+
647
+ // ── Private: Console interceptor ──
648
+
649
+ function _installConsoleInterceptor() {
650
+ const levels = ['log', 'warn', 'error'];
651
+ for (const level of levels) {
652
+ const original = console[level];
653
+ console[level] = (...args) => {
654
+ consoleLog.push({
655
+ level,
656
+ message: args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' '),
657
+ timestamp: Date.now(),
658
+ });
659
+ if (consoleLog.length > MAX_CONSOLE_LOG) consoleLog.shift();
660
+ original.apply(console, args);
661
+ };
662
+ }
663
+ }