@jantcz/webai 0.2.0 → 0.3.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.
- package/bin/webai-agent.js +20 -2
- package/package.json +1 -1
- package/src/agents/claude.js +39 -6
- package/src/bridge.js +2 -1
- package/src/cli.js +12 -1
- package/src/client/overlay.js +292 -10
- package/src/core.js +16 -2
- package/src/index.d.ts +18 -2
- package/src/mcp/endpoint.js +71 -0
- package/src/mcp/stdio.js +103 -0
- package/src/mcp/tools.js +126 -0
- package/src/pages.js +202 -0
- package/src/server.js +45 -4
- package/src/session.js +47 -5
- package/src/vite.js +103 -0
package/bin/webai-agent.js
CHANGED
|
@@ -295,7 +295,7 @@ async function main(opts, env, readPrompt) {
|
|
|
295
295
|
for (const candidate of candidates) {
|
|
296
296
|
if (candidate !== model) out({ type: 'status', text: `${model} is busy, switching to ${candidate}…` });
|
|
297
297
|
model = candidate;
|
|
298
|
-
const
|
|
298
|
+
const request = () => fetch(`${baseUrl}/chat/completions`, {
|
|
299
299
|
method: 'POST',
|
|
300
300
|
headers: { 'Content-Type': 'application/json', ...(key ? { Authorization: `Bearer ${key}` } : {}), 'X-Title': 'webai' },
|
|
301
301
|
body: JSON.stringify({
|
|
@@ -306,13 +306,21 @@ async function main(opts, env, readPrompt) {
|
|
|
306
306
|
...(baseUrl.includes('openrouter.ai') ? { usage: { include: true } } : {}),
|
|
307
307
|
}),
|
|
308
308
|
});
|
|
309
|
+
let res = await request();
|
|
310
|
+
// A short wait the provider asks for is cheaper than a weaker model: once, then move on.
|
|
311
|
+
const wait = res.status === 429 || res.status === 503 ? retryAfter(res.headers.get('retry-after')) : null;
|
|
312
|
+
if (wait !== null && wait <= RETRY_AFTER_MAX_S) {
|
|
313
|
+
await new Promise((resolve) => setTimeout(resolve, wait * 1000));
|
|
314
|
+
res = await request();
|
|
315
|
+
}
|
|
309
316
|
const data = await res.json().catch(() => ({}));
|
|
310
317
|
msg = res.ok ? data.choices?.[0]?.message : null;
|
|
311
318
|
if (msg) {
|
|
312
319
|
costUsd += data.usage?.cost ?? 0;
|
|
313
320
|
break;
|
|
314
321
|
}
|
|
315
|
-
|
|
322
|
+
// A rate limit or a server error: the next free model may well be free.
|
|
323
|
+
const busy = res.status >= 500 || res.status === 429;
|
|
316
324
|
if (!res.ok && !busy) throw new Error(apiError(res.status, data));
|
|
317
325
|
if (candidate === candidates.at(-1)) throw new Error(res.ok ? 'The model returned no response.' : apiError(res.status, data));
|
|
318
326
|
}
|
|
@@ -331,6 +339,16 @@ async function main(opts, env, readPrompt) {
|
|
|
331
339
|
out({ type: 'done', ok: false, error: `The agent didn't finish within ${MAX_STEPS} steps.`, costUsd });
|
|
332
340
|
}
|
|
333
341
|
|
|
342
|
+
/** Longest wait a rate-limited model may ask for before the agent moves on to the next one. */
|
|
343
|
+
export const RETRY_AFTER_MAX_S = 5;
|
|
344
|
+
|
|
345
|
+
/** Seconds a 429/503 asks to wait (`Retry-After`: seconds or an HTTP date); `null` when it says nothing usable. */
|
|
346
|
+
export function retryAfter(header, now = Date.now()) {
|
|
347
|
+
if (!header) return null;
|
|
348
|
+
const seconds = /^\d+(\.\d+)?$/.test(header.trim()) ? Number(header) : (Date.parse(header) - now) / 1000;
|
|
349
|
+
return Number.isNaN(seconds) ? null : Math.max(0, seconds);
|
|
350
|
+
}
|
|
351
|
+
|
|
334
352
|
/** One turn. Injectable for tests: argv, working folder, environment, prompt reader and event writer. */
|
|
335
353
|
export async function runAgent(argv = process.argv.slice(2), io = {}) {
|
|
336
354
|
const { cwd = process.cwd(), env = process.env, readPrompt = () => fs.readFileSync(0, 'utf8'), write = (s) => process.stdout.write(s) } = io;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jantcz/webai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Click an element on your site, tell an AI coding agent what to change: it edits the source. For any dev server: CLI, Vite, Astro, Next.js. Claude Code, Codex, Gemini CLI, OpenCode, Pi or a built-in agent with free models.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
package/src/agents/claude.js
CHANGED
|
@@ -11,8 +11,10 @@ import { PACK_DIR } from './pack.js';
|
|
|
11
11
|
* @param {object} [opts]
|
|
12
12
|
* @param {string} [opts.bin] executable, default `claude`
|
|
13
13
|
* @param {string} [opts.model]
|
|
14
|
-
* @param {string} [opts.permissionMode] default `acceptEdits` – may edit files
|
|
15
|
-
* @param {string[]} [opts.allowedTools] e.g. `['Bash(npm run check)']
|
|
14
|
+
* @param {string} [opts.permissionMode] default `acceptEdits` – may edit files; anything else asks in the panel
|
|
15
|
+
* @param {string[]} [opts.allowedTools] e.g. `['Bash(npm run check)']`, allowed without asking
|
|
16
|
+
* @param {boolean} [opts.interactive] ask in the panel before a tool that isn't allowed (Bash, MCP, …), default `true`;
|
|
17
|
+
* `false` denies such calls and doesn't offer Bash unless `allowedTools` has it
|
|
16
18
|
* @param {boolean} [opts.projectSettings] load `.claude/` and `.mcp.json` from the project, default `true`
|
|
17
19
|
* @param {string[]} [opts.extraArgs]
|
|
18
20
|
*/
|
|
@@ -40,8 +42,11 @@ export function claudeAgent(opts = {}) {
|
|
|
40
42
|
},
|
|
41
43
|
|
|
42
44
|
command({ sessionId, resume, systemPrompt, settings = {} }) {
|
|
45
|
+
const interactive = opts.interactive !== false;
|
|
43
46
|
const args = [
|
|
44
47
|
'-p',
|
|
48
|
+
// Interactive: the prompt is a stream-json message and stdin stays open for permission answers.
|
|
49
|
+
...(interactive ? ['--input-format', 'stream-json', '--permission-prompt-tool', 'stdio'] : []),
|
|
45
50
|
'--output-format', 'stream-json',
|
|
46
51
|
'--verbose',
|
|
47
52
|
'--include-partial-messages',
|
|
@@ -55,21 +60,47 @@ export function claudeAgent(opts = {}) {
|
|
|
55
60
|
if (settings.effort) args.push('--effort', settings.effort);
|
|
56
61
|
// Rules as separate arguments: a rule itself may contain a comma.
|
|
57
62
|
if (opts.allowedTools?.length) args.push('--allowedTools', ...opts.allowedTools);
|
|
58
|
-
// Without
|
|
59
|
-
//
|
|
60
|
-
if (!opts.allowedTools?.some((t) => t.startsWith('Bash'))) args.push('--disallowedTools', 'Bash');
|
|
63
|
+
// Without a way to ask, don't offer Bash unless it's allowed: every attempt would be
|
|
64
|
+
// denied and the agent would waste time looking for another way.
|
|
65
|
+
if (!interactive && !opts.allowedTools?.some((t) => t.startsWith('Bash'))) args.push('--disallowedTools', 'Bash');
|
|
61
66
|
// Skills and subagents from @jantcz/webai/agents (named webai:<skill>).
|
|
62
67
|
if (opts.skills !== false) args.push('--plugin-dir', PACK_DIR);
|
|
63
68
|
// No hooks, permissions or MCP servers from the project (bridge: the container writes to the project too).
|
|
64
69
|
if (opts.projectSettings === false) args.push('--setting-sources', 'user', '--strict-mcp-config');
|
|
65
70
|
args.push(...(opts.extraArgs ?? []));
|
|
66
71
|
// The prompt goes to stdin, not argv: it's often long and contains HTML.
|
|
67
|
-
return {
|
|
72
|
+
return {
|
|
73
|
+
cmd: bin,
|
|
74
|
+
args,
|
|
75
|
+
stdin: true,
|
|
76
|
+
...(interactive && {
|
|
77
|
+
// stdin stays open until the turn ends (see session.run): permission answers go through it.
|
|
78
|
+
interactive: true,
|
|
79
|
+
input: (text) => line({ type: 'user', message: { role: 'user', content: text } }),
|
|
80
|
+
}),
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
/** The answer to a `permission` event, as a line for the agent's stdin. */
|
|
85
|
+
answer({ requestId, input }, allow) {
|
|
86
|
+
const response = allow
|
|
87
|
+
? { behavior: 'allow', updatedInput: input }
|
|
88
|
+
: { behavior: 'deny', message: 'The user denied this in the WebAI panel.' };
|
|
89
|
+
return line({ type: 'control_response', response: { subtype: 'success', request_id: requestId, response } });
|
|
68
90
|
},
|
|
69
91
|
|
|
70
92
|
/** Converts one stream-json line into UI events. */
|
|
71
93
|
parse(msg, ctx) {
|
|
72
94
|
switch (msg.type) {
|
|
95
|
+
case 'control_request': {
|
|
96
|
+
const r = msg.request;
|
|
97
|
+
if (r?.subtype !== 'can_use_tool') return [];
|
|
98
|
+
const input = r.input ?? {};
|
|
99
|
+
return [{ type: 'permission', id: msg.request_id, name: r.tool_name, summary: summarizeTool({ name: r.tool_name, input }, ctx.root), input }];
|
|
100
|
+
}
|
|
101
|
+
// The agent gave up on the call (the turn was interrupted): the question is moot.
|
|
102
|
+
case 'control_cancel_request':
|
|
103
|
+
return [{ type: 'permission-answer', id: msg.request_id, allow: false, cancelled: true }];
|
|
73
104
|
case 'system':
|
|
74
105
|
return msg.subtype === 'init'
|
|
75
106
|
? [{ type: 'session', id: msg.session_id }, { type: 'status', text: `${msg.model ?? 'claude'} ready` }]
|
|
@@ -128,6 +159,8 @@ export function claudeAgent(opts = {}) {
|
|
|
128
159
|
};
|
|
129
160
|
}
|
|
130
161
|
|
|
162
|
+
const line = (msg) => JSON.stringify(msg) + '\n';
|
|
163
|
+
|
|
131
164
|
function rel(file, root) {
|
|
132
165
|
if (!file) return '';
|
|
133
166
|
return path.isAbsolute(file) ? path.relative(root, file) || file : file;
|
package/src/bridge.js
CHANGED
|
@@ -31,7 +31,8 @@ export async function startBridge({ root, port = DEFAULT_PORT, host = '127.0.0.1
|
|
|
31
31
|
...options,
|
|
32
32
|
// A custom command from the browser would mean an arbitrary command on the host.
|
|
33
33
|
custom: { editable: false, ...options.custom },
|
|
34
|
-
|
|
34
|
+
// No questions answered from the browser either: the container could approve its own Bash.
|
|
35
|
+
claude: { projectSettings: false, interactive: false, ...options.claude },
|
|
35
36
|
});
|
|
36
37
|
const session = new AgentSession({ root, agents, stateDir, logger });
|
|
37
38
|
await session.detect();
|
package/src/cli.js
CHANGED
|
@@ -6,6 +6,7 @@ import { parseArgs } from 'node:util';
|
|
|
6
6
|
import { DEFAULT_PORT as BRIDGE_PORT, startBridge } from './bridge.js';
|
|
7
7
|
import { CONFIG_FILE, consoleLogger, loadConfig } from './config.js';
|
|
8
8
|
import { createWebai } from './core.js';
|
|
9
|
+
import { serveMcp } from './mcp/stdio.js';
|
|
9
10
|
import { proxyTo } from './proxy.js';
|
|
10
11
|
import { staticFiles } from './static.js';
|
|
11
12
|
|
|
@@ -16,11 +17,14 @@ export const HELP = `webai: click an element on your site, tell an AI agent what
|
|
|
16
17
|
webai [dir] a folder of static files (default: the current one)
|
|
17
18
|
webai --target <url> a dev server that is already running, e.g. http://localhost:3000
|
|
18
19
|
webai bridge agents on this machine for a dev server in a container
|
|
20
|
+
webai mcp MCP server (stdio) that gives an agent harness the picked element,
|
|
21
|
+
console and DOM of the open page; add it to .mcp.json
|
|
19
22
|
|
|
20
23
|
Options:
|
|
21
24
|
--port <n> default ${DEFAULT_PORT} (bridge: ${BRIDGE_PORT})
|
|
22
25
|
--host <addr> default 127.0.0.1
|
|
23
26
|
--root <dir> the project the agent edits (default: the folder, or the current one with --target)
|
|
27
|
+
--bridge <url> mcp: use the bridge on the host instead of the dev server (Docker), e.g. http://127.0.0.1:4400
|
|
24
28
|
--config <file> options (default export), default <root>/${CONFIG_FILE}
|
|
25
29
|
--allow-remote let other machines use the overlay (in a container: the Docker network)
|
|
26
30
|
--open open the browser
|
|
@@ -30,7 +34,7 @@ Agents: Claude Code, Codex, Gemini CLI, OpenCode, Pi when installed, and the
|
|
|
30
34
|
built-in agent (free models, no key). Switch between them in the panel (⚙).`;
|
|
31
35
|
|
|
32
36
|
/** Runs the CLI; resolves to `{ url, close }` once it listens, or `null` after --help. */
|
|
33
|
-
export async function main(argv = process.argv.slice(2), { logger = consoleLogger, print = console.log } = {}) {
|
|
37
|
+
export async function main(argv = process.argv.slice(2), { logger = consoleLogger, print = console.log, input = process.stdin, output = process.stdout } = {}) {
|
|
34
38
|
const { values, positionals } = parseArgs({
|
|
35
39
|
args: argv,
|
|
36
40
|
allowPositionals: true,
|
|
@@ -39,6 +43,7 @@ export async function main(argv = process.argv.slice(2), { logger = consoleLogge
|
|
|
39
43
|
port: { type: 'string' },
|
|
40
44
|
host: { type: 'string', default: '127.0.0.1' },
|
|
41
45
|
root: { type: 'string' },
|
|
46
|
+
bridge: { type: 'string' },
|
|
42
47
|
config: { type: 'string' },
|
|
43
48
|
'allow-remote': { type: 'boolean', default: false },
|
|
44
49
|
open: { type: 'boolean', default: false },
|
|
@@ -50,6 +55,12 @@ export async function main(argv = process.argv.slice(2), { logger = consoleLogge
|
|
|
50
55
|
return null;
|
|
51
56
|
}
|
|
52
57
|
|
|
58
|
+
if (positionals[0] === 'mcp') {
|
|
59
|
+
// stdout is the protocol, so the logger (and everything else) stays off it.
|
|
60
|
+
const { close, done } = serveMcp({ root: path.resolve(values.root ?? '.'), bridge: values.bridge, input, output });
|
|
61
|
+
return { url: null, close, done };
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
if (positionals[0] === 'bridge') {
|
|
54
65
|
const root = path.resolve(values.root ?? positionals[1] ?? '.');
|
|
55
66
|
const { server, close } = await startBridge({ root, port: Number(values.port ?? BRIDGE_PORT), host: values.host, config: values.config, logger });
|
package/src/client/overlay.js
CHANGED
|
@@ -7,11 +7,14 @@
|
|
|
7
7
|
|
|
8
8
|
const STORE_OPEN = 'webai:open';
|
|
9
9
|
const STORE_TARGET = 'webai:target';
|
|
10
|
+
const STORE_DRAFT = 'webai:draft';
|
|
10
11
|
const STORE_SIDE = 'webai:side';
|
|
11
12
|
const STORE_GEOMETRY = 'webai:geometry';
|
|
12
13
|
const MIN_W = 300;
|
|
13
14
|
const MIN_H = 320;
|
|
14
15
|
const STORE_TRIGGERS = 'webai:triggers';
|
|
16
|
+
const STORE_TAB = 'webai:tab';
|
|
17
|
+
const STORE_NAV = 'webai:nav';
|
|
15
18
|
|
|
16
19
|
const TRIGGERS = [
|
|
17
20
|
['shift-click', 'Shift + click'],
|
|
@@ -19,12 +22,16 @@ const TRIGGERS = [
|
|
|
19
22
|
['contextmenu', 'Right click (Shift + right click = native menu)'],
|
|
20
23
|
];
|
|
21
24
|
|
|
25
|
+
const PING_MS = 15000; // the server's heartbeat (server.js)
|
|
26
|
+
|
|
22
27
|
const TOOL_ICONS = { Edit: '✎', MultiEdit: '✎', Write: '✚', Read: '◉', Bash: '$', Grep: '⌕', Glob: '⌕' };
|
|
23
28
|
|
|
24
29
|
export function start(cfg) {
|
|
25
30
|
if (window.__webai) return;
|
|
26
31
|
window.__webai = true;
|
|
27
|
-
|
|
32
|
+
// Right away, not after DOMContentLoaded: the console of the page's own start-up is what matters most.
|
|
33
|
+
const capture = cfg.transport ? null : installCapture(cfg.base);
|
|
34
|
+
const run = () => new Overlay(cfg, capture);
|
|
28
35
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', run, { once: true });
|
|
29
36
|
else run();
|
|
30
37
|
}
|
|
@@ -32,7 +39,7 @@ export function start(cfg) {
|
|
|
32
39
|
/**
|
|
33
40
|
* Transport to the dev server. Interface:
|
|
34
41
|
* - `request(method, path, body)` → data; failures throw an Error with a readable message
|
|
35
|
-
* - `subscribe({ onEvent, onReady, onLost })` → unsubscribe function; first
|
|
42
|
+
* - `subscribe({ onEvent, onReady, onLost, onPing, onCommand, tab })` → unsubscribe function (`onPing`, `onCommand`, `tab` are optional); first
|
|
36
43
|
* replays the whole history, then `onReady({ busy })`, then live events
|
|
37
44
|
*/
|
|
38
45
|
export function httpTransport(cfg) {
|
|
@@ -54,10 +61,12 @@ export function httpTransport(cfg) {
|
|
|
54
61
|
return data;
|
|
55
62
|
},
|
|
56
63
|
|
|
57
|
-
subscribe({ onEvent, onReady, onLost }) {
|
|
58
|
-
const es = new EventSource(`${cfg.base}/events?token=${cfg.token}`);
|
|
64
|
+
subscribe({ onEvent, onReady, onLost, onPing, onCommand, tab }) {
|
|
65
|
+
const es = new EventSource(`${cfg.base}/events?token=${cfg.token}${tab ? `&tab=${encodeURIComponent(tab)}` : ''}`);
|
|
66
|
+
es.addEventListener('command', (m) => onCommand?.(JSON.parse(m.data)));
|
|
59
67
|
es.onmessage = (m) => onEvent(JSON.parse(m.data));
|
|
60
68
|
es.addEventListener('ready', (m) => onReady(JSON.parse(m.data)));
|
|
69
|
+
es.addEventListener('ping', () => onPing?.());
|
|
61
70
|
es.onerror = () => {
|
|
62
71
|
if (es.readyState === EventSource.CLOSED) onLost('Lost connection to the dev server. Reload the page.');
|
|
63
72
|
};
|
|
@@ -67,7 +76,7 @@ export function httpTransport(cfg) {
|
|
|
67
76
|
}
|
|
68
77
|
|
|
69
78
|
class Overlay {
|
|
70
|
-
constructor(cfg) {
|
|
79
|
+
constructor(cfg, capture = null) {
|
|
71
80
|
this.cfg = cfg;
|
|
72
81
|
this.transport = cfg.transport ?? httpTransport(cfg);
|
|
73
82
|
this.target = null;
|
|
@@ -76,11 +85,25 @@ class Overlay {
|
|
|
76
85
|
this.unsubscribe = null;
|
|
77
86
|
this.mount();
|
|
78
87
|
this.bindInspector();
|
|
79
|
-
|
|
88
|
+
// A reload after an edit must not eat what is being typed.
|
|
89
|
+
this.el.input.value = sessionStorage.getItem(STORE_DRAFT) ?? '';
|
|
90
|
+
this.el.input.addEventListener('input', () => {
|
|
91
|
+
if (this.el.input.value) sessionStorage.setItem(STORE_DRAFT, this.el.input.value);
|
|
92
|
+
else sessionStorage.removeItem(STORE_DRAFT);
|
|
93
|
+
});
|
|
94
|
+
// Over HTTP the page also reports to the server (for MCP tools), so the connection is always on.
|
|
95
|
+
// Before the panel opens: it would otherwise connect without the tab.
|
|
96
|
+
if (capture) this.startLink(capture);
|
|
97
|
+
const open = sessionStorage.getItem(STORE_OPEN) === '1';
|
|
98
|
+
// Without the chat there is no panel to remember the selection by: it is restored on its own.
|
|
99
|
+
if (open || cfg.chat === false) {
|
|
80
100
|
const saved = readJson(sessionStorage.getItem(STORE_TARGET));
|
|
81
101
|
if (saved) this.setTarget(saved, findElement(saved, cfg.srcAttr));
|
|
82
|
-
this.open();
|
|
83
102
|
}
|
|
103
|
+
if (open) this.open();
|
|
104
|
+
this.watchTarget();
|
|
105
|
+
// No selection came back from the last page load: tell the server (a stale one would otherwise stay).
|
|
106
|
+
if (capture && !this.target) this.reportSelection();
|
|
84
107
|
}
|
|
85
108
|
|
|
86
109
|
// ---------- DOM ----------
|
|
@@ -103,6 +126,7 @@ class Overlay {
|
|
|
103
126
|
</header>
|
|
104
127
|
<div class="settings" hidden></div>
|
|
105
128
|
<div class="log"></div>
|
|
129
|
+
<div class="activity" hidden></div>
|
|
106
130
|
<div class="target" hidden><code></code><button title="Don't send the element">×</button></div>
|
|
107
131
|
<form>
|
|
108
132
|
<textarea rows="2" placeholder="What should happen to the element? Enter sends, Shift+Enter adds a new line"></textarea>
|
|
@@ -115,7 +139,7 @@ class Overlay {
|
|
|
115
139
|
const $ = (s) => root.querySelector(s);
|
|
116
140
|
this.el = {
|
|
117
141
|
hover: $('.hover'), label: $('.hover .label'), pick: $('.pick'), panel: $('.panel'),
|
|
118
|
-
agent: $('.agent'), log: $('.log'), target: $('.target'), targetCode: $('.target code'),
|
|
142
|
+
agent: $('.agent'), log: $('.log'), activity: $('.activity'), target: $('.target'), targetCode: $('.target code'),
|
|
119
143
|
form: $('form'), input: $('textarea'), send: $('.send'), fab: $('.fab'), settings: $('.settings'),
|
|
120
144
|
gear: $('.gear'), hint: $('.hint'),
|
|
121
145
|
versions: $('.versions'), vnum: $('.vnum'), back: $('.back'), fwd: $('.fwd'),
|
|
@@ -191,6 +215,7 @@ class Overlay {
|
|
|
191
215
|
}, true);
|
|
192
216
|
addEventListener('keydown', (e) => {
|
|
193
217
|
if (e.key === 'Escape' && !this.el.panel.hidden) this.close();
|
|
218
|
+
else if (e.key === 'Escape' && this.cfg.chat === false && this.target) this.setTarget(null); // clears the selection
|
|
194
219
|
});
|
|
195
220
|
}
|
|
196
221
|
|
|
@@ -215,10 +240,31 @@ class Overlay {
|
|
|
215
240
|
this.hideHover();
|
|
216
241
|
getSelection()?.removeAllRanges(); // Shift+click extends the text selection
|
|
217
242
|
this.setTarget(describe(el, this.cfg.srcAttr), el);
|
|
243
|
+
if (this.cfg.chat === false) return; // select only: the highlight stays, MCP tools can read it
|
|
218
244
|
this.open();
|
|
219
245
|
this.el.input.focus();
|
|
220
246
|
}
|
|
221
247
|
|
|
248
|
+
/**
|
|
249
|
+
* The selection survives a reload, but the page may draw the element later (an app that renders after its data
|
|
250
|
+
* arrives), replace it, or change it (hot reload): after the page settles, look for it again when it is missing and
|
|
251
|
+
* describe it again when it changed, so the server and the agent are told what is there now.
|
|
252
|
+
*/
|
|
253
|
+
watchTarget() {
|
|
254
|
+
let timer = null;
|
|
255
|
+
new MutationObserver(() => {
|
|
256
|
+
if (timer || !this.target) return;
|
|
257
|
+
timer = setTimeout(() => {
|
|
258
|
+
timer = null;
|
|
259
|
+
if (!this.target) return;
|
|
260
|
+
const el = this.targetEl?.isConnected ? this.targetEl : findElement(this.target, this.cfg.srcAttr);
|
|
261
|
+
if (!el) return;
|
|
262
|
+
const info = describe(el, this.cfg.srcAttr);
|
|
263
|
+
if (el !== this.targetEl || JSON.stringify(info) !== JSON.stringify(this.target)) this.setTarget(info, el);
|
|
264
|
+
}, 300);
|
|
265
|
+
}).observe(document.documentElement, { childList: true, subtree: true, characterData: true, attributes: true });
|
|
266
|
+
}
|
|
267
|
+
|
|
222
268
|
setTarget(info, el = null) {
|
|
223
269
|
this.target = info;
|
|
224
270
|
this.targetEl = el;
|
|
@@ -227,11 +273,12 @@ class Overlay {
|
|
|
227
273
|
this.el.target.hidden = !info;
|
|
228
274
|
if (info) this.el.targetCode.textContent = `<${info.tag.match(/^<?([\w-]+)/)?.[1] ?? '?'}> ${info.src ?? info.selector}`;
|
|
229
275
|
this.drawPick();
|
|
276
|
+
this.reportSelection();
|
|
230
277
|
}
|
|
231
278
|
|
|
232
279
|
drawPick() {
|
|
233
280
|
const el = this.targetEl;
|
|
234
|
-
if (!el || !el.isConnected || this.el.panel.hidden) {
|
|
281
|
+
if (!el || !el.isConnected || (this.el.panel.hidden && this.cfg.chat !== false)) {
|
|
235
282
|
this.el.pick.hidden = true;
|
|
236
283
|
return;
|
|
237
284
|
}
|
|
@@ -255,6 +302,7 @@ class Overlay {
|
|
|
255
302
|
this.el.pick.hidden = true;
|
|
256
303
|
this.el.fab.hidden = !this.cfg.fab;
|
|
257
304
|
this.showSettings(false);
|
|
305
|
+
if (this.tab) return; // the connection also carries MCP commands for this page
|
|
258
306
|
this.unsubscribe?.();
|
|
259
307
|
this.unsubscribe = null;
|
|
260
308
|
}
|
|
@@ -468,7 +516,10 @@ class Overlay {
|
|
|
468
516
|
this.el.log.textContent = '';
|
|
469
517
|
this.live = null;
|
|
470
518
|
this.unsubscribe = this.transport.subscribe({
|
|
471
|
-
onEvent: (ev) => this.handle(ev),
|
|
519
|
+
onEvent: (ev) => (this.lastEventAt = Date.now(), this.lastPingAt = Date.now(), this.handle(ev)),
|
|
520
|
+
onPing: () => (this.lastPingAt = Date.now()),
|
|
521
|
+
onCommand: (cmd) => this.command(cmd),
|
|
522
|
+
tab: this.tab,
|
|
472
523
|
onReady: ({ busy }) => {
|
|
473
524
|
this.caughtUp = true; // events from here on are new, not the replayed history
|
|
474
525
|
this.setBusy(busy);
|
|
@@ -528,6 +579,7 @@ class Overlay {
|
|
|
528
579
|
page: { url: location.pathname + location.search, title: document.title },
|
|
529
580
|
});
|
|
530
581
|
this.el.input.value = '';
|
|
582
|
+
sessionStorage.removeItem(STORE_DRAFT);
|
|
531
583
|
} catch (err) {
|
|
532
584
|
this.setBusy(false);
|
|
533
585
|
this.line('error', err.message);
|
|
@@ -553,9 +605,80 @@ class Overlay {
|
|
|
553
605
|
this.el.send.classList.toggle('stop', busy);
|
|
554
606
|
this.el.panel.classList.toggle('busy', busy);
|
|
555
607
|
this.el.fab.classList.toggle('busy', busy);
|
|
608
|
+
this.trackActivity(busy);
|
|
556
609
|
if (this.history) this.showHistory(this.history);
|
|
557
610
|
}
|
|
558
611
|
|
|
612
|
+
// ---------- link to the server (MCP tools) ----------
|
|
613
|
+
|
|
614
|
+
startLink(capture) {
|
|
615
|
+
// The same tab keeps its id across reloads, so its console buffer on the server stays one history.
|
|
616
|
+
let tab = sessionStorage.getItem(STORE_TAB);
|
|
617
|
+
if (!tab) sessionStorage.setItem(STORE_TAB, (tab = Math.random().toString(36).slice(2, 12)));
|
|
618
|
+
const nav = Number(sessionStorage.getItem(STORE_NAV) ?? 0) + 1;
|
|
619
|
+
sessionStorage.setItem(STORE_NAV, String(nav));
|
|
620
|
+
this.tab = tab;
|
|
621
|
+
capture.nav = nav;
|
|
622
|
+
const post = (path, body) => this.api('POST', path, { tab, ...body }).catch(() => {}); // a missed report is not an error to show
|
|
623
|
+
this.post = post;
|
|
624
|
+
const hello = () => post('/page/hello', {
|
|
625
|
+
url: location.href, title: document.title, nav, origin: location.origin,
|
|
626
|
+
viewport: { width: innerWidth, height: innerHeight }, visible: document.visibilityState === 'visible',
|
|
627
|
+
});
|
|
628
|
+
hello();
|
|
629
|
+
addEventListener('focus', () => post('/page/focus'));
|
|
630
|
+
document.addEventListener('visibilitychange', () => document.visibilityState === 'visible' && post('/page/focus'));
|
|
631
|
+
setInterval(() => {
|
|
632
|
+
const entries = capture.drain();
|
|
633
|
+
if (entries.length) post('/page/log', { entries });
|
|
634
|
+
}, 1000);
|
|
635
|
+
this.connect();
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
reportSelection() {
|
|
639
|
+
if (!this.post) return;
|
|
640
|
+
const page = { url: location.pathname + location.search, title: document.title };
|
|
641
|
+
this.post('/page/selection', { element: this.target, page });
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** A read-only command from an MCP tool; the answer goes back over POST. */
|
|
645
|
+
async command({ id, cmd, args }) {
|
|
646
|
+
let out;
|
|
647
|
+
try {
|
|
648
|
+
out = { ok: true, data: runCommand(cmd, args ?? {}, this.cfg.srcAttr) };
|
|
649
|
+
} catch (err) {
|
|
650
|
+
out = { ok: false, error: err.message };
|
|
651
|
+
}
|
|
652
|
+
this.post('/page/result', { id, ...out });
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// ---------- activity ----------
|
|
656
|
+
|
|
657
|
+
/** While the agent works: elapsed time, time since its last output, and whether the connection is alive. */
|
|
658
|
+
trackActivity(busy) {
|
|
659
|
+
clearInterval(this.activityTimer);
|
|
660
|
+
this.el.activity.hidden = !busy;
|
|
661
|
+
if (!busy) return;
|
|
662
|
+
this.busySince = Date.now();
|
|
663
|
+
this.lastTool = null;
|
|
664
|
+
this.lastEventAt = this.lastPingAt = Date.now();
|
|
665
|
+
this.activityTimer = setInterval(() => this.drawActivity(), 1000);
|
|
666
|
+
this.drawActivity();
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
drawActivity() {
|
|
670
|
+
const now = Date.now();
|
|
671
|
+
const lost = this.unsubscribe && now - this.lastPingAt > 2.5 * PING_MS;
|
|
672
|
+
const quiet = now - this.lastEventAt;
|
|
673
|
+
const parts = [`Working ${duration(now - this.busySince)}`];
|
|
674
|
+
parts.push(quiet < 3000 ? 'receiving output' : `no output for ${duration(quiet)}`);
|
|
675
|
+
if (this.lastTool) parts.push(`last: ${this.lastTool}`);
|
|
676
|
+
this.el.activity.textContent = lost
|
|
677
|
+
? 'No sign of life from the dev server for a while: it may have restarted. Reload the page.'
|
|
678
|
+
: parts.join(' · ') + (quiet > 60000 ? ' (the model or a tool may just be slow; Stop ends it)' : '');
|
|
679
|
+
this.el.activity.classList.toggle('lost', !!lost);
|
|
680
|
+
}
|
|
681
|
+
|
|
559
682
|
// ---------- agent events ----------
|
|
560
683
|
|
|
561
684
|
handle(ev) {
|
|
@@ -600,10 +723,43 @@ class Overlay {
|
|
|
600
723
|
case 'tool':
|
|
601
724
|
this.live = null;
|
|
602
725
|
this.line('tool', `${TOOL_ICONS[ev.name] ?? '•'} ${ev.name} ${ev.summary ?? ''}`);
|
|
726
|
+
this.lastTool = `${ev.name} ${ev.summary ?? ''}`.trim().slice(0, 60);
|
|
603
727
|
break;
|
|
604
728
|
case 'tool-error':
|
|
605
729
|
this.line('tool error', ev.text);
|
|
606
730
|
break;
|
|
731
|
+
case 'permission': {
|
|
732
|
+
const div = this.line('permission', '');
|
|
733
|
+
div.dataset.id = ev.id;
|
|
734
|
+
const ask = document.createElement('span');
|
|
735
|
+
ask.textContent = `${ev.name} ${ev.summary ?? ''}`.trim();
|
|
736
|
+
const allow = document.createElement('button');
|
|
737
|
+
allow.textContent = 'Allow';
|
|
738
|
+
const deny = document.createElement('button');
|
|
739
|
+
deny.textContent = 'Deny';
|
|
740
|
+
deny.className = 'deny';
|
|
741
|
+
for (const [button, ok] of [[allow, true], [deny, false]]) {
|
|
742
|
+
button.onclick = () => {
|
|
743
|
+
allow.disabled = deny.disabled = true;
|
|
744
|
+
this.api('POST', '/permission', { id: ev.id, allow: ok }).catch((err) => {
|
|
745
|
+
allow.disabled = deny.disabled = false;
|
|
746
|
+
this.line('error', err.message);
|
|
747
|
+
});
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
const label = document.createElement('b');
|
|
751
|
+
label.textContent = 'Allow this? ';
|
|
752
|
+
div.append(label, ask, allow, deny);
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
case 'permission-answer': {
|
|
756
|
+
const div = [...this.el.log.querySelectorAll('.msg.permission')].find((d) => d.dataset.id === ev.id);
|
|
757
|
+
if (!div) break;
|
|
758
|
+
div.querySelector('b').textContent = ev.cancelled ? 'Not answered: ' : ev.allow ? 'Allowed: ' : 'Denied: ';
|
|
759
|
+
div.classList.add(ev.allow ? 'allowed' : 'answered');
|
|
760
|
+
div.querySelectorAll('button').forEach((b) => b.remove());
|
|
761
|
+
break;
|
|
762
|
+
}
|
|
607
763
|
case 'denied':
|
|
608
764
|
this.line('denied', `Denied: ${ev.name} ${ev.summary ?? ''}. Allow it in options.claude.allowedTools.`);
|
|
609
765
|
break;
|
|
@@ -675,6 +831,124 @@ function describe(el, srcAttr) {
|
|
|
675
831
|
};
|
|
676
832
|
}
|
|
677
833
|
|
|
834
|
+
const DEFAULT_STYLES = ['display', 'position', 'width', 'height', 'color', 'background-color', 'font-size', 'font-weight', 'margin', 'padding', 'overflow', 'visibility'];
|
|
835
|
+
|
|
836
|
+
/** The commands MCP tools may run in the page: all of them only read. */
|
|
837
|
+
function runCommand(cmd, args, srcAttr) {
|
|
838
|
+
if (cmd === 'info') {
|
|
839
|
+
const d = document.documentElement;
|
|
840
|
+
return {
|
|
841
|
+
url: location.href, title: document.title, readyState: document.readyState,
|
|
842
|
+
viewport: { width: innerWidth, height: innerHeight, devicePixelRatio },
|
|
843
|
+
scroll: { x: Math.round(scrollX), y: Math.round(scrollY) },
|
|
844
|
+
document: { width: d.scrollWidth, height: d.scrollHeight },
|
|
845
|
+
lang: d.lang || undefined, visible: document.visibilityState === 'visible',
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
if (cmd === 'query') {
|
|
849
|
+
let found;
|
|
850
|
+
try {
|
|
851
|
+
found = document.querySelectorAll(args.selector);
|
|
852
|
+
} catch {
|
|
853
|
+
throw new Error(`"${args.selector}" is not a valid CSS selector.`);
|
|
854
|
+
}
|
|
855
|
+
const styles = args.styles?.length ? args.styles : DEFAULT_STYLES;
|
|
856
|
+
return {
|
|
857
|
+
count: found.length,
|
|
858
|
+
elements: [...found].slice(0, args.limit ?? 3).map((el) => {
|
|
859
|
+
const cs = getComputedStyle(el);
|
|
860
|
+
const r = el.getBoundingClientRect();
|
|
861
|
+
return {
|
|
862
|
+
...describe(el, srcAttr),
|
|
863
|
+
rect: { x: Math.round(r.x), y: Math.round(r.y), width: Math.round(r.width), height: Math.round(r.height) },
|
|
864
|
+
visible: r.width > 0 && r.height > 0 && cs.visibility !== 'hidden' && cs.display !== 'none',
|
|
865
|
+
styles: Object.fromEntries(styles.map((p) => [p, cs.getPropertyValue(p)])),
|
|
866
|
+
};
|
|
867
|
+
}),
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
throw new Error(`Unknown command ${cmd}.`);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
const LEVELS = ['debug', 'log', 'info', 'warn', 'error'];
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Collects console output, uncaught errors, rejected promises and failed requests
|
|
877
|
+
* in a queue the overlay sends to the server. WebAI's own requests are left out.
|
|
878
|
+
*/
|
|
879
|
+
function installCapture(base) {
|
|
880
|
+
const queue = [];
|
|
881
|
+
const cap = {
|
|
882
|
+
nav: 0,
|
|
883
|
+
drain: () => queue.splice(0),
|
|
884
|
+
add(level, kind, text, stack) {
|
|
885
|
+
queue.push({ level, kind, text: String(text).slice(0, 4000), stack: stack && String(stack).slice(0, 4000), nav: cap.nav });
|
|
886
|
+
if (queue.length > 300) queue.shift();
|
|
887
|
+
},
|
|
888
|
+
};
|
|
889
|
+
for (const level of LEVELS) {
|
|
890
|
+
const orig = console[level];
|
|
891
|
+
console[level] = function (...args) {
|
|
892
|
+
try {
|
|
893
|
+
cap.add(level, 'console', args.map(formatArg).join(' '), args.find((a) => a instanceof Error)?.stack);
|
|
894
|
+
} catch {} // capturing must never break the page's own logging
|
|
895
|
+
return orig.apply(this, args);
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
addEventListener('error', (e) => {
|
|
899
|
+
if (e.target && e.target !== window) {
|
|
900
|
+
// A resource that failed to load (the error event doesn't bubble, so only the capture phase sees it).
|
|
901
|
+
const t = e.target;
|
|
902
|
+
cap.add('error', 'network', `Failed to load <${t.tagName?.toLowerCase()}> ${t.src || t.href || ''}`);
|
|
903
|
+
} else {
|
|
904
|
+
cap.add('error', 'error', `${e.message} (${e.filename}:${e.lineno}:${e.colno})`, e.error?.stack);
|
|
905
|
+
}
|
|
906
|
+
}, true);
|
|
907
|
+
addEventListener('unhandledrejection', (e) => cap.add('error', 'error', `Unhandled promise rejection: ${formatArg(e.reason)}`, e.reason?.stack));
|
|
908
|
+
const realFetch = window.fetch;
|
|
909
|
+
window.fetch = async function (input, init) {
|
|
910
|
+
const url = typeof input === 'string' ? input : input?.url ?? String(input);
|
|
911
|
+
const method = (init?.method ?? input?.method ?? 'GET').toUpperCase();
|
|
912
|
+
if (url.includes(base)) return realFetch.call(this, input, init);
|
|
913
|
+
try {
|
|
914
|
+
const res = await realFetch.call(this, input, init);
|
|
915
|
+
if (!res.ok) cap.add('error', 'network', `${method} ${url} → ${res.status} ${res.statusText}`.trim());
|
|
916
|
+
return res;
|
|
917
|
+
} catch (err) {
|
|
918
|
+
if (err?.name !== 'AbortError') cap.add('error', 'network', `${method} ${url} failed: ${err?.message ?? err}`);
|
|
919
|
+
throw err;
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
return cap;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function formatArg(a) {
|
|
926
|
+
if (typeof a === 'string') return a;
|
|
927
|
+
if (a instanceof Error) return a.stack || `${a.name}: ${a.message}`;
|
|
928
|
+
if (a instanceof Element) return openTag(a);
|
|
929
|
+
if (a && typeof a === 'object') {
|
|
930
|
+
const seen = new WeakSet();
|
|
931
|
+
try {
|
|
932
|
+
return JSON.stringify(a, (k, v) => {
|
|
933
|
+
if (typeof v === 'bigint') return `${v}n`;
|
|
934
|
+
if (v && typeof v === 'object') {
|
|
935
|
+
if (seen.has(v)) return '[circular]';
|
|
936
|
+
seen.add(v);
|
|
937
|
+
}
|
|
938
|
+
return v;
|
|
939
|
+
}).slice(0, 1000);
|
|
940
|
+
} catch {
|
|
941
|
+
return Object.prototype.toString.call(a);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return String(a);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
function duration(ms) {
|
|
948
|
+
const s = Math.floor(ms / 1000);
|
|
949
|
+
return s < 60 ? `${s} s` : `${Math.floor(s / 60)} min ${s % 60} s`;
|
|
950
|
+
}
|
|
951
|
+
|
|
678
952
|
function findElement(info, srcAttr) {
|
|
679
953
|
if (info.src) {
|
|
680
954
|
const els = document.querySelectorAll(`[${srcAttr}="${CSS.escape(info.src)}"]`);
|
|
@@ -815,6 +1089,8 @@ const STYLES = `
|
|
|
815
1089
|
}
|
|
816
1090
|
.fab:hover { background: #8d71ff; opacity: 1; }
|
|
817
1091
|
.fab.left { right: auto; left: 16px; }
|
|
1092
|
+
.activity { margin: 0 10px; color: #8f8fa8; font: 11.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
1093
|
+
.activity.lost { color: #ff7a7a; }
|
|
818
1094
|
.fab.busy { animation: pulse 1s infinite; }
|
|
819
1095
|
.gear { display: inline-flex; align-items: center; justify-content: center; padding: 3px 6px; }
|
|
820
1096
|
.gear svg { width: 18px; height: 18px; }
|
|
@@ -863,6 +1139,12 @@ button:hover { background: #34344a; }
|
|
|
863
1139
|
.msg.meta { text-align: right; }
|
|
864
1140
|
.msg.error, .msg.tool.error { color: #ff7a7a; }
|
|
865
1141
|
.msg.denied { color: #ffb020; font-size: 12px; }
|
|
1142
|
+
.msg.permission { background: #2a2418; border: 1px solid #5c4a1e; border-radius: 8px; padding: 6px 10px; font-size: 12px; color: #ffd37a; }
|
|
1143
|
+
.msg.permission span { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }
|
|
1144
|
+
.msg.permission button { margin: 6px 6px 0 0; font: inherit; padding: 2px 12px; border: 0; border-radius: 6px; background: #7c5cff; color: #fff; cursor: pointer; }
|
|
1145
|
+
.msg.permission button.deny { background: #3a3a50; }
|
|
1146
|
+
.msg.permission button:disabled { opacity: 0.5; cursor: default; }
|
|
1147
|
+
.msg.permission.allowed, .msg.permission.answered { background: none; border-color: transparent; padding: 0; color: #8f8fa8; }
|
|
866
1148
|
code, pre { font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
867
1149
|
.msg code { background: #2d2d40; padding: 0 3px; border-radius: 3px; }
|
|
868
1150
|
pre.plain { margin: 0; background: transparent; padding: 0; white-space: pre-wrap; }
|