wu-framework 1.1.15 → 1.1.17
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.
- package/README.md +52 -20
- package/dist/wu-framework.cjs.js +1 -1
- package/dist/wu-framework.cjs.js.map +1 -1
- package/dist/wu-framework.dev.js +15511 -15146
- package/dist/wu-framework.dev.js.map +1 -1
- package/dist/wu-framework.esm.js +1 -1
- package/dist/wu-framework.esm.js.map +1 -1
- package/dist/wu-framework.umd.js +1 -1
- package/dist/wu-framework.umd.js.map +1 -1
- package/package.json +166 -161
- package/src/adapters/angular/ai.js +30 -30
- package/src/adapters/angular/index.d.ts +154 -154
- package/src/adapters/angular/index.js +932 -932
- package/src/adapters/angular.d.ts +3 -3
- package/src/adapters/angular.js +3 -3
- package/src/adapters/index.js +168 -168
- package/src/adapters/lit/ai.js +20 -20
- package/src/adapters/lit/index.d.ts +120 -120
- package/src/adapters/lit/index.js +721 -721
- package/src/adapters/lit.d.ts +3 -3
- package/src/adapters/lit.js +3 -3
- package/src/adapters/preact/ai.js +33 -33
- package/src/adapters/preact/index.d.ts +108 -108
- package/src/adapters/preact/index.js +661 -661
- package/src/adapters/preact.d.ts +3 -3
- package/src/adapters/preact.js +3 -3
- package/src/adapters/react/index.js +48 -54
- package/src/adapters/react.d.ts +3 -3
- package/src/adapters/react.js +3 -3
- package/src/adapters/shared.js +64 -64
- package/src/adapters/solid/ai.js +32 -32
- package/src/adapters/solid/index.d.ts +101 -101
- package/src/adapters/solid/index.js +586 -586
- package/src/adapters/solid.d.ts +3 -3
- package/src/adapters/solid.js +3 -3
- package/src/adapters/svelte/ai.js +31 -31
- package/src/adapters/svelte/index.d.ts +166 -166
- package/src/adapters/svelte/index.js +798 -798
- package/src/adapters/svelte.d.ts +3 -3
- package/src/adapters/svelte.js +3 -3
- package/src/adapters/vanilla/ai.js +30 -30
- package/src/adapters/vanilla/index.d.ts +179 -179
- package/src/adapters/vanilla/index.js +785 -785
- package/src/adapters/vanilla.d.ts +3 -3
- package/src/adapters/vanilla.js +3 -3
- package/src/adapters/vue/ai.js +52 -52
- package/src/adapters/vue/index.d.ts +299 -299
- package/src/adapters/vue/index.js +610 -610
- package/src/adapters/vue.d.ts +3 -3
- package/src/adapters/vue.js +3 -3
- package/src/ai/wu-ai-actions.js +261 -261
- package/src/ai/wu-ai-agent.js +546 -546
- package/src/ai/wu-ai-browser-primitives.js +354 -354
- package/src/ai/wu-ai-browser.js +380 -380
- package/src/ai/wu-ai-context.js +332 -332
- package/src/ai/wu-ai-conversation.js +613 -613
- package/src/ai/wu-ai-orchestrate.js +1021 -1021
- package/src/ai/wu-ai-permissions.js +381 -381
- package/src/ai/wu-ai-provider.js +700 -700
- package/src/ai/wu-ai-schema.js +225 -225
- package/src/ai/wu-ai-triggers.js +396 -396
- package/src/ai/wu-ai.js +804 -804
- package/src/core/wu-app.js +236 -236
- package/src/core/wu-cache.js +498 -477
- package/src/core/wu-core.js +1412 -1398
- package/src/core/wu-error-boundary.js +396 -382
- package/src/core/wu-event-bus.js +390 -348
- package/src/core/wu-hooks.js +350 -350
- package/src/core/wu-html-parser.js +199 -190
- package/src/core/wu-iframe-sandbox.js +328 -328
- package/src/core/wu-loader.js +385 -273
- package/src/core/wu-logger.js +142 -134
- package/src/core/wu-manifest.js +532 -509
- package/src/core/wu-mcp-bridge.js +432 -432
- package/src/core/wu-overrides.js +510 -510
- package/src/core/wu-performance.js +228 -228
- package/src/core/wu-plugin.js +401 -348
- package/src/core/wu-prefetch.js +414 -414
- package/src/core/wu-proxy-sandbox.js +477 -476
- package/src/core/wu-sandbox.js +779 -779
- package/src/core/wu-script-executor.js +161 -113
- package/src/core/wu-snapshot-sandbox.js +227 -227
- package/src/core/wu-store.js +13 -3
- package/src/core/wu-strategies.js +256 -256
- package/src/core/wu-style-bridge.js +477 -477
- package/src/index.d.ts +317 -0
- package/src/index.js +234 -224
- package/src/utils/dependency-resolver.js +327 -327
package/src/ai/wu-ai-browser.js
CHANGED
|
@@ -1,380 +1,380 @@
|
|
|
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
|
-
import {
|
|
28
|
-
ensureInterceptors,
|
|
29
|
-
networkLog,
|
|
30
|
-
consoleLog,
|
|
31
|
-
captureScreenshot,
|
|
32
|
-
buildA11yTree,
|
|
33
|
-
clickElement,
|
|
34
|
-
typeIntoElement,
|
|
35
|
-
getFilteredNetwork,
|
|
36
|
-
getFilteredConsole,
|
|
37
|
-
} from './wu-ai-browser-primitives.js';
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Register all browser automation actions into a WuAI instance.
|
|
41
|
-
*
|
|
42
|
-
* @param {object} ai - The WuAI instance (wu.ai)
|
|
43
|
-
* @param {object} wu - The Wu Framework instance (window.wu)
|
|
44
|
-
*/
|
|
45
|
-
export function registerBrowserActions(ai, wu) {
|
|
46
|
-
ensureInterceptors();
|
|
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) => captureScreenshot(params.selector),
|
|
62
|
-
permissions: [],
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// ════════════════════════════════════════════
|
|
66
|
-
// CLICK
|
|
67
|
-
// ════════════════════════════════════════════
|
|
68
|
-
|
|
69
|
-
ai.action('browser_click', {
|
|
70
|
-
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.',
|
|
71
|
-
parameters: {
|
|
72
|
-
selector: {
|
|
73
|
-
type: 'string',
|
|
74
|
-
description: 'CSS selector (e.g. "#submit-btn", ".nav-link", "button[type=submit]")',
|
|
75
|
-
required: false,
|
|
76
|
-
},
|
|
77
|
-
text: {
|
|
78
|
-
type: 'string',
|
|
79
|
-
description: 'Visible text to find and click (e.g. "Submit", "Next", "Guardar"). Searches buttons, links, and clickable elements.',
|
|
80
|
-
required: false,
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
handler: async (params, api) => {
|
|
84
|
-
const result = clickElement(params.selector, params.text);
|
|
85
|
-
if (!result.error) {
|
|
86
|
-
api.emit?.('browser:clicked', { selector: params.selector, text: params.text });
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
},
|
|
90
|
-
permissions: ['emitEvents'],
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// ════════════════════════════════════════════
|
|
94
|
-
// TYPE
|
|
95
|
-
// ════════════════════════════════════════════
|
|
96
|
-
|
|
97
|
-
ai.action('browser_type', {
|
|
98
|
-
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.',
|
|
99
|
-
parameters: {
|
|
100
|
-
selector: {
|
|
101
|
-
type: 'string',
|
|
102
|
-
description: 'CSS selector of the input (e.g. "#email", "input[name=search]", "textarea.comment")',
|
|
103
|
-
required: true,
|
|
104
|
-
},
|
|
105
|
-
text: {
|
|
106
|
-
type: 'string',
|
|
107
|
-
description: 'Text to type into the element',
|
|
108
|
-
required: true,
|
|
109
|
-
},
|
|
110
|
-
clear: {
|
|
111
|
-
type: 'boolean',
|
|
112
|
-
description: 'Clear existing value before typing (default: false)',
|
|
113
|
-
required: false,
|
|
114
|
-
},
|
|
115
|
-
submit: {
|
|
116
|
-
type: 'boolean',
|
|
117
|
-
description: 'Submit the form or press Enter after typing (default: false)',
|
|
118
|
-
required: false,
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
handler: async (params, api) => {
|
|
122
|
-
const result = typeIntoElement(params.selector, params.text, {
|
|
123
|
-
clear: params.clear,
|
|
124
|
-
submit: params.submit,
|
|
125
|
-
});
|
|
126
|
-
if (!result.error) {
|
|
127
|
-
api.emit?.('browser:typed', { selector: params.selector, length: params.text.length });
|
|
128
|
-
}
|
|
129
|
-
return result;
|
|
130
|
-
},
|
|
131
|
-
permissions: ['emitEvents'],
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// ════════════════════════════════════════════
|
|
135
|
-
// SELECT (dropdowns)
|
|
136
|
-
// ════════════════════════════════════════════
|
|
137
|
-
|
|
138
|
-
ai.action('browser_select', {
|
|
139
|
-
description: 'Select an option in a <select> dropdown or a custom dropdown component.',
|
|
140
|
-
parameters: {
|
|
141
|
-
selector: {
|
|
142
|
-
type: 'string',
|
|
143
|
-
description: 'CSS selector of the <select> element',
|
|
144
|
-
required: true,
|
|
145
|
-
},
|
|
146
|
-
value: {
|
|
147
|
-
type: 'string',
|
|
148
|
-
description: 'The value attribute of the option to select. Use "text:" prefix to match by visible text (e.g. "text:Mexico")',
|
|
149
|
-
required: true,
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
handler: async (params, api) => {
|
|
153
|
-
const el = document.querySelector(params.selector);
|
|
154
|
-
if (!el) return { error: `Element not found: ${params.selector}` };
|
|
155
|
-
|
|
156
|
-
if (el.tagName?.toLowerCase() === 'select') {
|
|
157
|
-
const options = Array.from(el.options);
|
|
158
|
-
let option;
|
|
159
|
-
|
|
160
|
-
if (params.value.startsWith('text:')) {
|
|
161
|
-
const searchText = params.value.slice(5).toLowerCase();
|
|
162
|
-
option = options.find((o) => o.textContent.trim().toLowerCase().includes(searchText));
|
|
163
|
-
} else {
|
|
164
|
-
option = options.find((o) => o.value === params.value);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (!option) return { error: `Option not found: ${params.value}` };
|
|
168
|
-
|
|
169
|
-
el.value = option.value;
|
|
170
|
-
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
171
|
-
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
172
|
-
|
|
173
|
-
api.emit?.('browser:selected', { selector: params.selector, value: option.value });
|
|
174
|
-
return { selected: option.value, text: option.textContent.trim() };
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// Custom dropdown: try clicking the trigger, then the option
|
|
178
|
-
el.click();
|
|
179
|
-
return { clicked: params.selector, note: 'Custom dropdown — clicked trigger. Use browser_click to select an option from the opened menu.' };
|
|
180
|
-
},
|
|
181
|
-
permissions: ['emitEvents'],
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// ════════════════════════════════════════════
|
|
185
|
-
// SCROLL
|
|
186
|
-
// ════════════════════════════════════════════
|
|
187
|
-
|
|
188
|
-
ai.action('browser_scroll', {
|
|
189
|
-
description: 'Scroll the page or a specific element. Use to reveal content that is not visible.',
|
|
190
|
-
parameters: {
|
|
191
|
-
direction: {
|
|
192
|
-
type: 'string',
|
|
193
|
-
description: 'Direction: "up", "down", "top", "bottom"',
|
|
194
|
-
required: true,
|
|
195
|
-
},
|
|
196
|
-
selector: {
|
|
197
|
-
type: 'string',
|
|
198
|
-
description: 'CSS selector of scrollable container (empty = page)',
|
|
199
|
-
required: false,
|
|
200
|
-
},
|
|
201
|
-
amount: {
|
|
202
|
-
type: 'number',
|
|
203
|
-
description: 'Pixels to scroll (default: 500). Ignored for "top"/"bottom".',
|
|
204
|
-
required: false,
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
handler: async (params) => {
|
|
208
|
-
const target = params.selector
|
|
209
|
-
? document.querySelector(params.selector)
|
|
210
|
-
: window;
|
|
211
|
-
const amount = params.amount || 500;
|
|
212
|
-
|
|
213
|
-
if (params.selector && !target) return { error: `Element not found: ${params.selector}` };
|
|
214
|
-
|
|
215
|
-
const scrollEl = target === window ? document.documentElement : target;
|
|
216
|
-
|
|
217
|
-
switch (params.direction) {
|
|
218
|
-
case 'up': scrollEl.scrollBy({ top: -amount, behavior: 'smooth' }); break;
|
|
219
|
-
case 'down': scrollEl.scrollBy({ top: amount, behavior: 'smooth' }); break;
|
|
220
|
-
case 'top': scrollEl.scrollTo({ top: 0, behavior: 'smooth' }); break;
|
|
221
|
-
case 'bottom': scrollEl.scrollTo({ top: scrollEl.scrollHeight, behavior: 'smooth' }); break;
|
|
222
|
-
default: return { error: `Invalid direction: ${params.direction}` };
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return {
|
|
226
|
-
scrolled: params.direction,
|
|
227
|
-
amount: params.direction === 'top' || params.direction === 'bottom' ? 'max' : amount,
|
|
228
|
-
currentScroll: scrollEl.scrollTop,
|
|
229
|
-
};
|
|
230
|
-
},
|
|
231
|
-
permissions: [],
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
// ════════════════════════════════════════════
|
|
235
|
-
// SNAPSHOT — Accessibility tree
|
|
236
|
-
// ════════════════════════════════════════════
|
|
237
|
-
|
|
238
|
-
ai.action('browser_snapshot', {
|
|
239
|
-
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.',
|
|
240
|
-
parameters: {
|
|
241
|
-
selector: {
|
|
242
|
-
type: 'string',
|
|
243
|
-
description: 'CSS selector to snapshot (empty = full page). Use "[data-wu-app=appName]" for a specific micro-app.',
|
|
244
|
-
required: false,
|
|
245
|
-
},
|
|
246
|
-
depth: {
|
|
247
|
-
type: 'number',
|
|
248
|
-
description: 'Max depth to traverse (default: 5)',
|
|
249
|
-
required: false,
|
|
250
|
-
},
|
|
251
|
-
},
|
|
252
|
-
handler: async (params) => {
|
|
253
|
-
const target = params.selector
|
|
254
|
-
? document.querySelector(params.selector)
|
|
255
|
-
: document.body;
|
|
256
|
-
|
|
257
|
-
if (!target) return { error: `Element not found: ${params.selector}` };
|
|
258
|
-
|
|
259
|
-
const tree = buildA11yTree(target, 0, params.depth || 5);
|
|
260
|
-
return { snapshot: tree };
|
|
261
|
-
},
|
|
262
|
-
permissions: [],
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
// ════════════════════════════════════════════
|
|
266
|
-
// NAVIGATE
|
|
267
|
-
// ════════════════════════════════════════════
|
|
268
|
-
|
|
269
|
-
ai.action('browser_navigate', {
|
|
270
|
-
description: 'Navigate to a route within the SPA application. Emits a shell:navigate event and updates the store.',
|
|
271
|
-
parameters: {
|
|
272
|
-
route: {
|
|
273
|
-
type: 'string',
|
|
274
|
-
description: 'Route path (e.g. "/dashboard", "/users", "/pos/cotizador")',
|
|
275
|
-
required: true,
|
|
276
|
-
},
|
|
277
|
-
},
|
|
278
|
-
handler: async (params, api) => {
|
|
279
|
-
api.emit?.('shell:navigate', { route: params.route });
|
|
280
|
-
api.setState?.('currentPath', params.route);
|
|
281
|
-
return { navigated: params.route };
|
|
282
|
-
},
|
|
283
|
-
permissions: ['emitEvents', 'writeStore'],
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
// ════════════════════════════════════════════
|
|
287
|
-
// NETWORK — Captured HTTP requests
|
|
288
|
-
// ════════════════════════════════════════════
|
|
289
|
-
|
|
290
|
-
ai.action('browser_network', {
|
|
291
|
-
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.',
|
|
292
|
-
parameters: {
|
|
293
|
-
method: {
|
|
294
|
-
type: 'string',
|
|
295
|
-
description: 'Filter by HTTP method: GET, POST, PUT, DELETE (empty = all)',
|
|
296
|
-
required: false,
|
|
297
|
-
},
|
|
298
|
-
status: {
|
|
299
|
-
type: 'string',
|
|
300
|
-
description: 'Filter: "2" (2xx success), "4" (4xx errors), "5" (5xx errors), "error" (all failures)',
|
|
301
|
-
required: false,
|
|
302
|
-
},
|
|
303
|
-
limit: {
|
|
304
|
-
type: 'number',
|
|
305
|
-
description: 'Max requests to return (default: 30)',
|
|
306
|
-
required: false,
|
|
307
|
-
},
|
|
308
|
-
},
|
|
309
|
-
handler: async (params) => getFilteredNetwork(params.method, params.status, params.limit),
|
|
310
|
-
permissions: [],
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
// ════════════════════════════════════════════
|
|
314
|
-
// CONSOLE — Captured logs
|
|
315
|
-
// ════════════════════════════════════════════
|
|
316
|
-
|
|
317
|
-
ai.action('browser_console', {
|
|
318
|
-
description: 'View captured browser console messages (log, warn, error). Use to check for errors, warnings, or debug output.',
|
|
319
|
-
parameters: {
|
|
320
|
-
level: {
|
|
321
|
-
type: 'string',
|
|
322
|
-
description: 'Filter by level: "log", "warn", "error" (empty = all)',
|
|
323
|
-
required: false,
|
|
324
|
-
},
|
|
325
|
-
limit: {
|
|
326
|
-
type: 'number',
|
|
327
|
-
description: 'Max messages to return (default: 30)',
|
|
328
|
-
required: false,
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
handler: async (params) => getFilteredConsole(params.level, params.limit),
|
|
332
|
-
permissions: [],
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
// ════════════════════════════════════════════
|
|
336
|
-
// INFO — Page state overview
|
|
337
|
-
// ════════════════════════════════════════════
|
|
338
|
-
|
|
339
|
-
ai.action('browser_info', {
|
|
340
|
-
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.',
|
|
341
|
-
parameters: {},
|
|
342
|
-
handler: async (params, api) => {
|
|
343
|
-
const apps = [];
|
|
344
|
-
|
|
345
|
-
// Discover mounted apps
|
|
346
|
-
if (wu._apps) {
|
|
347
|
-
for (const [name, app] of Object.entries(wu._apps)) {
|
|
348
|
-
apps.push({
|
|
349
|
-
name,
|
|
350
|
-
mounted: app.mounted || app.isMounted || false,
|
|
351
|
-
status: app.status || 'unknown',
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
if (apps.length === 0) {
|
|
356
|
-
document.querySelectorAll('[data-wu-app]').forEach((el) => {
|
|
357
|
-
apps.push({ name: el.getAttribute('data-wu-app'), mounted: true });
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
const storeData = api.getState?.('') || {};
|
|
362
|
-
const storeKeys = typeof storeData === 'object' ? Object.keys(storeData) : [];
|
|
363
|
-
|
|
364
|
-
return {
|
|
365
|
-
url: window.location.href,
|
|
366
|
-
title: document.title,
|
|
367
|
-
viewport: { width: window.innerWidth, height: window.innerHeight },
|
|
368
|
-
apps,
|
|
369
|
-
storeKeys,
|
|
370
|
-
networkRequests: networkLog.length,
|
|
371
|
-
consoleMessages: consoleLog.length,
|
|
372
|
-
consoleErrors: consoleLog.filter((m) => m.level === 'error').length,
|
|
373
|
-
};
|
|
374
|
-
},
|
|
375
|
-
permissions: ['readStore'],
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
// All private helpers (buildA11yTree, inlineComputedStyles, interceptors)
|
|
380
|
-
// are now in wu-ai-browser-primitives.js — single source of truth.
|
|
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
|
+
import {
|
|
28
|
+
ensureInterceptors,
|
|
29
|
+
networkLog,
|
|
30
|
+
consoleLog,
|
|
31
|
+
captureScreenshot,
|
|
32
|
+
buildA11yTree,
|
|
33
|
+
clickElement,
|
|
34
|
+
typeIntoElement,
|
|
35
|
+
getFilteredNetwork,
|
|
36
|
+
getFilteredConsole,
|
|
37
|
+
} from './wu-ai-browser-primitives.js';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Register all browser automation actions into a WuAI instance.
|
|
41
|
+
*
|
|
42
|
+
* @param {object} ai - The WuAI instance (wu.ai)
|
|
43
|
+
* @param {object} wu - The Wu Framework instance (window.wu)
|
|
44
|
+
*/
|
|
45
|
+
export function registerBrowserActions(ai, wu) {
|
|
46
|
+
ensureInterceptors();
|
|
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) => captureScreenshot(params.selector),
|
|
62
|
+
permissions: [],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ════════════════════════════════════════════
|
|
66
|
+
// CLICK
|
|
67
|
+
// ════════════════════════════════════════════
|
|
68
|
+
|
|
69
|
+
ai.action('browser_click', {
|
|
70
|
+
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.',
|
|
71
|
+
parameters: {
|
|
72
|
+
selector: {
|
|
73
|
+
type: 'string',
|
|
74
|
+
description: 'CSS selector (e.g. "#submit-btn", ".nav-link", "button[type=submit]")',
|
|
75
|
+
required: false,
|
|
76
|
+
},
|
|
77
|
+
text: {
|
|
78
|
+
type: 'string',
|
|
79
|
+
description: 'Visible text to find and click (e.g. "Submit", "Next", "Guardar"). Searches buttons, links, and clickable elements.',
|
|
80
|
+
required: false,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
handler: async (params, api) => {
|
|
84
|
+
const result = clickElement(params.selector, params.text);
|
|
85
|
+
if (!result.error) {
|
|
86
|
+
api.emit?.('browser:clicked', { selector: params.selector, text: params.text });
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
},
|
|
90
|
+
permissions: ['emitEvents'],
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// ════════════════════════════════════════════
|
|
94
|
+
// TYPE
|
|
95
|
+
// ════════════════════════════════════════════
|
|
96
|
+
|
|
97
|
+
ai.action('browser_type', {
|
|
98
|
+
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.',
|
|
99
|
+
parameters: {
|
|
100
|
+
selector: {
|
|
101
|
+
type: 'string',
|
|
102
|
+
description: 'CSS selector of the input (e.g. "#email", "input[name=search]", "textarea.comment")',
|
|
103
|
+
required: true,
|
|
104
|
+
},
|
|
105
|
+
text: {
|
|
106
|
+
type: 'string',
|
|
107
|
+
description: 'Text to type into the element',
|
|
108
|
+
required: true,
|
|
109
|
+
},
|
|
110
|
+
clear: {
|
|
111
|
+
type: 'boolean',
|
|
112
|
+
description: 'Clear existing value before typing (default: false)',
|
|
113
|
+
required: false,
|
|
114
|
+
},
|
|
115
|
+
submit: {
|
|
116
|
+
type: 'boolean',
|
|
117
|
+
description: 'Submit the form or press Enter after typing (default: false)',
|
|
118
|
+
required: false,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
handler: async (params, api) => {
|
|
122
|
+
const result = typeIntoElement(params.selector, params.text, {
|
|
123
|
+
clear: params.clear,
|
|
124
|
+
submit: params.submit,
|
|
125
|
+
});
|
|
126
|
+
if (!result.error) {
|
|
127
|
+
api.emit?.('browser:typed', { selector: params.selector, length: params.text.length });
|
|
128
|
+
}
|
|
129
|
+
return result;
|
|
130
|
+
},
|
|
131
|
+
permissions: ['emitEvents'],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// ════════════════════════════════════════════
|
|
135
|
+
// SELECT (dropdowns)
|
|
136
|
+
// ════════════════════════════════════════════
|
|
137
|
+
|
|
138
|
+
ai.action('browser_select', {
|
|
139
|
+
description: 'Select an option in a <select> dropdown or a custom dropdown component.',
|
|
140
|
+
parameters: {
|
|
141
|
+
selector: {
|
|
142
|
+
type: 'string',
|
|
143
|
+
description: 'CSS selector of the <select> element',
|
|
144
|
+
required: true,
|
|
145
|
+
},
|
|
146
|
+
value: {
|
|
147
|
+
type: 'string',
|
|
148
|
+
description: 'The value attribute of the option to select. Use "text:" prefix to match by visible text (e.g. "text:Mexico")',
|
|
149
|
+
required: true,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
handler: async (params, api) => {
|
|
153
|
+
const el = document.querySelector(params.selector);
|
|
154
|
+
if (!el) return { error: `Element not found: ${params.selector}` };
|
|
155
|
+
|
|
156
|
+
if (el.tagName?.toLowerCase() === 'select') {
|
|
157
|
+
const options = Array.from(el.options);
|
|
158
|
+
let option;
|
|
159
|
+
|
|
160
|
+
if (params.value.startsWith('text:')) {
|
|
161
|
+
const searchText = params.value.slice(5).toLowerCase();
|
|
162
|
+
option = options.find((o) => o.textContent.trim().toLowerCase().includes(searchText));
|
|
163
|
+
} else {
|
|
164
|
+
option = options.find((o) => o.value === params.value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!option) return { error: `Option not found: ${params.value}` };
|
|
168
|
+
|
|
169
|
+
el.value = option.value;
|
|
170
|
+
el.dispatchEvent(new Event('change', { bubbles: true }));
|
|
171
|
+
el.dispatchEvent(new Event('input', { bubbles: true }));
|
|
172
|
+
|
|
173
|
+
api.emit?.('browser:selected', { selector: params.selector, value: option.value });
|
|
174
|
+
return { selected: option.value, text: option.textContent.trim() };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Custom dropdown: try clicking the trigger, then the option
|
|
178
|
+
el.click();
|
|
179
|
+
return { clicked: params.selector, note: 'Custom dropdown — clicked trigger. Use browser_click to select an option from the opened menu.' };
|
|
180
|
+
},
|
|
181
|
+
permissions: ['emitEvents'],
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// ════════════════════════════════════════════
|
|
185
|
+
// SCROLL
|
|
186
|
+
// ════════════════════════════════════════════
|
|
187
|
+
|
|
188
|
+
ai.action('browser_scroll', {
|
|
189
|
+
description: 'Scroll the page or a specific element. Use to reveal content that is not visible.',
|
|
190
|
+
parameters: {
|
|
191
|
+
direction: {
|
|
192
|
+
type: 'string',
|
|
193
|
+
description: 'Direction: "up", "down", "top", "bottom"',
|
|
194
|
+
required: true,
|
|
195
|
+
},
|
|
196
|
+
selector: {
|
|
197
|
+
type: 'string',
|
|
198
|
+
description: 'CSS selector of scrollable container (empty = page)',
|
|
199
|
+
required: false,
|
|
200
|
+
},
|
|
201
|
+
amount: {
|
|
202
|
+
type: 'number',
|
|
203
|
+
description: 'Pixels to scroll (default: 500). Ignored for "top"/"bottom".',
|
|
204
|
+
required: false,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
handler: async (params) => {
|
|
208
|
+
const target = params.selector
|
|
209
|
+
? document.querySelector(params.selector)
|
|
210
|
+
: window;
|
|
211
|
+
const amount = params.amount || 500;
|
|
212
|
+
|
|
213
|
+
if (params.selector && !target) return { error: `Element not found: ${params.selector}` };
|
|
214
|
+
|
|
215
|
+
const scrollEl = target === window ? document.documentElement : target;
|
|
216
|
+
|
|
217
|
+
switch (params.direction) {
|
|
218
|
+
case 'up': scrollEl.scrollBy({ top: -amount, behavior: 'smooth' }); break;
|
|
219
|
+
case 'down': scrollEl.scrollBy({ top: amount, behavior: 'smooth' }); break;
|
|
220
|
+
case 'top': scrollEl.scrollTo({ top: 0, behavior: 'smooth' }); break;
|
|
221
|
+
case 'bottom': scrollEl.scrollTo({ top: scrollEl.scrollHeight, behavior: 'smooth' }); break;
|
|
222
|
+
default: return { error: `Invalid direction: ${params.direction}` };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
scrolled: params.direction,
|
|
227
|
+
amount: params.direction === 'top' || params.direction === 'bottom' ? 'max' : amount,
|
|
228
|
+
currentScroll: scrollEl.scrollTop,
|
|
229
|
+
};
|
|
230
|
+
},
|
|
231
|
+
permissions: [],
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
// ════════════════════════════════════════════
|
|
235
|
+
// SNAPSHOT — Accessibility tree
|
|
236
|
+
// ════════════════════════════════════════════
|
|
237
|
+
|
|
238
|
+
ai.action('browser_snapshot', {
|
|
239
|
+
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.',
|
|
240
|
+
parameters: {
|
|
241
|
+
selector: {
|
|
242
|
+
type: 'string',
|
|
243
|
+
description: 'CSS selector to snapshot (empty = full page). Use "[data-wu-app=appName]" for a specific micro-app.',
|
|
244
|
+
required: false,
|
|
245
|
+
},
|
|
246
|
+
depth: {
|
|
247
|
+
type: 'number',
|
|
248
|
+
description: 'Max depth to traverse (default: 5)',
|
|
249
|
+
required: false,
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
handler: async (params) => {
|
|
253
|
+
const target = params.selector
|
|
254
|
+
? document.querySelector(params.selector)
|
|
255
|
+
: document.body;
|
|
256
|
+
|
|
257
|
+
if (!target) return { error: `Element not found: ${params.selector}` };
|
|
258
|
+
|
|
259
|
+
const tree = buildA11yTree(target, 0, params.depth || 5);
|
|
260
|
+
return { snapshot: tree };
|
|
261
|
+
},
|
|
262
|
+
permissions: [],
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// ════════════════════════════════════════════
|
|
266
|
+
// NAVIGATE
|
|
267
|
+
// ════════════════════════════════════════════
|
|
268
|
+
|
|
269
|
+
ai.action('browser_navigate', {
|
|
270
|
+
description: 'Navigate to a route within the SPA application. Emits a shell:navigate event and updates the store.',
|
|
271
|
+
parameters: {
|
|
272
|
+
route: {
|
|
273
|
+
type: 'string',
|
|
274
|
+
description: 'Route path (e.g. "/dashboard", "/users", "/pos/cotizador")',
|
|
275
|
+
required: true,
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
handler: async (params, api) => {
|
|
279
|
+
api.emit?.('shell:navigate', { route: params.route });
|
|
280
|
+
api.setState?.('currentPath', params.route);
|
|
281
|
+
return { navigated: params.route };
|
|
282
|
+
},
|
|
283
|
+
permissions: ['emitEvents', 'writeStore'],
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// ════════════════════════════════════════════
|
|
287
|
+
// NETWORK — Captured HTTP requests
|
|
288
|
+
// ════════════════════════════════════════════
|
|
289
|
+
|
|
290
|
+
ai.action('browser_network', {
|
|
291
|
+
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.',
|
|
292
|
+
parameters: {
|
|
293
|
+
method: {
|
|
294
|
+
type: 'string',
|
|
295
|
+
description: 'Filter by HTTP method: GET, POST, PUT, DELETE (empty = all)',
|
|
296
|
+
required: false,
|
|
297
|
+
},
|
|
298
|
+
status: {
|
|
299
|
+
type: 'string',
|
|
300
|
+
description: 'Filter: "2" (2xx success), "4" (4xx errors), "5" (5xx errors), "error" (all failures)',
|
|
301
|
+
required: false,
|
|
302
|
+
},
|
|
303
|
+
limit: {
|
|
304
|
+
type: 'number',
|
|
305
|
+
description: 'Max requests to return (default: 30)',
|
|
306
|
+
required: false,
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
handler: async (params) => getFilteredNetwork(params.method, params.status, params.limit),
|
|
310
|
+
permissions: [],
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// ════════════════════════════════════════════
|
|
314
|
+
// CONSOLE — Captured logs
|
|
315
|
+
// ════════════════════════════════════════════
|
|
316
|
+
|
|
317
|
+
ai.action('browser_console', {
|
|
318
|
+
description: 'View captured browser console messages (log, warn, error). Use to check for errors, warnings, or debug output.',
|
|
319
|
+
parameters: {
|
|
320
|
+
level: {
|
|
321
|
+
type: 'string',
|
|
322
|
+
description: 'Filter by level: "log", "warn", "error" (empty = all)',
|
|
323
|
+
required: false,
|
|
324
|
+
},
|
|
325
|
+
limit: {
|
|
326
|
+
type: 'number',
|
|
327
|
+
description: 'Max messages to return (default: 30)',
|
|
328
|
+
required: false,
|
|
329
|
+
},
|
|
330
|
+
},
|
|
331
|
+
handler: async (params) => getFilteredConsole(params.level, params.limit),
|
|
332
|
+
permissions: [],
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
// ════════════════════════════════════════════
|
|
336
|
+
// INFO — Page state overview
|
|
337
|
+
// ════════════════════════════════════════════
|
|
338
|
+
|
|
339
|
+
ai.action('browser_info', {
|
|
340
|
+
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.',
|
|
341
|
+
parameters: {},
|
|
342
|
+
handler: async (params, api) => {
|
|
343
|
+
const apps = [];
|
|
344
|
+
|
|
345
|
+
// Discover mounted apps
|
|
346
|
+
if (wu._apps) {
|
|
347
|
+
for (const [name, app] of Object.entries(wu._apps)) {
|
|
348
|
+
apps.push({
|
|
349
|
+
name,
|
|
350
|
+
mounted: app.mounted || app.isMounted || false,
|
|
351
|
+
status: app.status || 'unknown',
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (apps.length === 0) {
|
|
356
|
+
document.querySelectorAll('[data-wu-app]').forEach((el) => {
|
|
357
|
+
apps.push({ name: el.getAttribute('data-wu-app'), mounted: true });
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const storeData = api.getState?.('') || {};
|
|
362
|
+
const storeKeys = typeof storeData === 'object' ? Object.keys(storeData) : [];
|
|
363
|
+
|
|
364
|
+
return {
|
|
365
|
+
url: window.location.href,
|
|
366
|
+
title: document.title,
|
|
367
|
+
viewport: { width: window.innerWidth, height: window.innerHeight },
|
|
368
|
+
apps,
|
|
369
|
+
storeKeys,
|
|
370
|
+
networkRequests: networkLog.length,
|
|
371
|
+
consoleMessages: consoleLog.length,
|
|
372
|
+
consoleErrors: consoleLog.filter((m) => m.level === 'error').length,
|
|
373
|
+
};
|
|
374
|
+
},
|
|
375
|
+
permissions: ['readStore'],
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// All private helpers (buildA11yTree, inlineComputedStyles, interceptors)
|
|
380
|
+
// are now in wu-ai-browser-primitives.js — single source of truth.
|