@jantcz/webai 0.0.0-stage → 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/LICENSE +135 -0
- package/README.md +24 -2
- package/agents/.claude-plugin/plugin.json +4 -0
- package/agents/README.md +17 -0
- package/agents/agents/a11y-reviewer.md +16 -0
- package/agents/agents/copy-editor.md +14 -0
- package/agents/skills/czech-copy/SKILL.md +18 -0
- package/agents/skills/element-edit/SKILL.md +51 -0
- package/agents/skills/stock-images/SKILL.md +25 -0
- package/agents/skills/verify-change/SKILL.md +19 -0
- package/agents/skills/web-styling/SKILL.md +20 -0
- package/bin/webai-agent.js +373 -0
- package/bin/webai-bridge.js +5 -0
- package/bin/webai.js +4 -0
- package/package.json +85 -4
- package/src/agents/claude.js +194 -0
- package/src/agents/codex.js +111 -0
- package/src/agents/custom.js +64 -0
- package/src/agents/docker.js +46 -0
- package/src/agents/gemini.js +102 -0
- package/src/agents/index.js +25 -0
- package/src/agents/opencode.js +117 -0
- package/src/agents/pack.js +99 -0
- package/src/agents/pi.js +143 -0
- package/src/agents/webai.js +66 -0
- package/src/astro/annotate.js +163 -0
- package/src/astro/index.d.ts +5 -0
- package/src/astro/index.js +40 -0
- package/src/bridge.js +63 -0
- package/src/cli.js +119 -0
- package/src/client/overlay.js +1169 -0
- package/src/config.js +23 -0
- package/src/core.js +116 -0
- package/src/history.js +108 -0
- package/src/html.js +43 -0
- package/src/index.d.ts +244 -0
- package/src/index.js +10 -0
- package/src/inject.js +80 -0
- package/src/mcp/endpoint.js +71 -0
- package/src/mcp/stdio.js +103 -0
- package/src/mcp/tools.js +126 -0
- package/src/next/index.d.ts +8 -0
- package/src/next/index.js +59 -0
- package/src/next/script.d.ts +3 -0
- package/src/next/script.js +13 -0
- package/src/pages.js +202 -0
- package/src/prompt.js +17 -0
- package/src/proxy.js +68 -0
- package/src/server.js +252 -0
- package/src/session.js +497 -0
- package/src/static.js +64 -0
- package/src/vite.d.ts +5 -0
- package/src/vite.js +145 -0
package/src/proxy.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
3
|
+
import net from 'node:net';
|
|
4
|
+
import tls from 'node:tls';
|
|
5
|
+
|
|
6
|
+
// Hop-by-hop headers belong to one connection, not to the forwarded request.
|
|
7
|
+
const HOP = new Set(['connection', 'keep-alive', 'proxy-connection', 'transfer-encoding', 'te', 'trailer', 'upgrade']);
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Reverse proxy to a running dev server (Next.js, Django, Rails, Laravel,
|
|
11
|
+
* Hugo, …), for `webai --target <url>`. Pages are requested uncompressed so the
|
|
12
|
+
* overlay can be added to them; WebSockets (the upstream's hot reload) pass through.
|
|
13
|
+
*/
|
|
14
|
+
export function proxyTo(target) {
|
|
15
|
+
target = new URL(target);
|
|
16
|
+
const client = target.protocol === 'https:' ? https : http;
|
|
17
|
+
|
|
18
|
+
const handler = (req, res) => {
|
|
19
|
+
const headers = forwardHeaders(req, target);
|
|
20
|
+
if ((req.headers.accept ?? '').includes('text/html')) headers['accept-encoding'] = 'identity';
|
|
21
|
+
const upstream = client.request(
|
|
22
|
+
{ protocol: target.protocol, hostname: target.hostname, port: target.port, method: req.method, path: req.url, headers },
|
|
23
|
+
(up) => {
|
|
24
|
+
const out = {};
|
|
25
|
+
for (const [k, v] of Object.entries(up.headers)) if (!HOP.has(k)) out[k] = v;
|
|
26
|
+
// Redirects to the upstream's own address stay on the proxy.
|
|
27
|
+
if (out.location?.startsWith(target.origin)) out.location = out.location.slice(target.origin.length) || '/';
|
|
28
|
+
res.writeHead(up.statusCode, up.statusMessage, out);
|
|
29
|
+
up.pipe(res);
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
upstream.on('error', (err) => {
|
|
33
|
+
if (res.headersSent) return res.destroy();
|
|
34
|
+
res.writeHead(502, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
35
|
+
res.end(`<!doctype html><title>webai</title><p>The dev server at ${target.origin} is not responding (${err.code ?? err.message}). Is it running?</p>`);
|
|
36
|
+
});
|
|
37
|
+
req.pipe(upstream);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** For the HTTP server's `upgrade` event. */
|
|
41
|
+
handler.upgrade = (req, socket, head) => {
|
|
42
|
+
const connect = target.protocol === 'https:'
|
|
43
|
+
? () => tls.connect({ host: target.hostname, port: target.port || 443, servername: net.isIP(target.hostname) ? undefined : target.hostname })
|
|
44
|
+
: () => net.connect({ host: target.hostname, port: target.port || 80 });
|
|
45
|
+
const up = connect();
|
|
46
|
+
const headers = { ...forwardHeaders(req, target), connection: 'Upgrade', upgrade: req.headers.upgrade };
|
|
47
|
+
up.on(target.protocol === 'https:' ? 'secureConnect' : 'connect', () => {
|
|
48
|
+
const lines = [`${req.method} ${req.url} HTTP/1.1`, ...Object.entries(headers).map(([k, v]) => `${k}: ${v}`)];
|
|
49
|
+
up.write(`${lines.join('\r\n')}\r\n\r\n`);
|
|
50
|
+
if (head?.length) up.write(head);
|
|
51
|
+
up.pipe(socket).pipe(up);
|
|
52
|
+
});
|
|
53
|
+
const close = () => (up.destroy(), socket.destroy());
|
|
54
|
+
up.on('error', close);
|
|
55
|
+
socket.on('error', close);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return handler;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function forwardHeaders(req, target) {
|
|
62
|
+
const headers = {};
|
|
63
|
+
for (const [k, v] of Object.entries(req.headers)) if (!HOP.has(k)) headers[k] = v;
|
|
64
|
+
headers.host = target.host;
|
|
65
|
+
// Dev servers that check Origin (CSRF, WebSockets) see their own address.
|
|
66
|
+
if (headers.origin) headers.origin = target.origin;
|
|
67
|
+
return headers;
|
|
68
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { callTool } from './mcp/tools.js';
|
|
5
|
+
import { PageHub } from './pages.js';
|
|
6
|
+
|
|
7
|
+
export const BASE = '/__webai';
|
|
8
|
+
export const BRIDGE_TOKEN_FILE = 'bridge-token';
|
|
9
|
+
|
|
10
|
+
const PING_MS = 15000;
|
|
11
|
+
|
|
12
|
+
const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The overlay's HTTP API. The endpoints run an agent with write access to the
|
|
16
|
+
* sources, so they are protected by:
|
|
17
|
+
* - a token from the injected script,
|
|
18
|
+
* - Origin/Sec-Fetch-Site against requests from foreign pages,
|
|
19
|
+
* - Host against DNS rebinding (a foreign domain pointed at 127.0.0.1),
|
|
20
|
+
* - client address: this machine only, even when `astro dev --host` exposes the site to the network.
|
|
21
|
+
*
|
|
22
|
+
* `allowRemote` disables the last two checks. A dev server in a container
|
|
23
|
+
* needs that, since the browser comes in over the Docker network.
|
|
24
|
+
* `trustProxy`: requests come through a reverse proxy on this machine (Next.js
|
|
25
|
+
* rewrites), so the browser's address and host are in X-Forwarded-For/-Host.
|
|
26
|
+
*/
|
|
27
|
+
export function createMiddleware(session, token, { allowRemote = false, trustProxy = false, mcpToken = null } = {}) {
|
|
28
|
+
const api = createApi(session);
|
|
29
|
+
return (req, res, next) => {
|
|
30
|
+
const route = routeOf(req);
|
|
31
|
+
if (route == null) return next();
|
|
32
|
+
// MCP tools: a local process (not a page) with its own token.
|
|
33
|
+
if (route.startsWith('/mcp/')) return toolAllowed(req, mcpToken) ? api(req, res, route) : json(res, 403, { error: 'forbidden' });
|
|
34
|
+
if (!browserAllowed(req, token, { allowRemote, trustProxy })) return json(res, 403, { error: 'forbidden' });
|
|
35
|
+
return api(req, res, route);
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Variant for a dev server in a container: checks the browser like
|
|
41
|
+
* `createMiddleware`, but forwards the request to the bridge on the host, where the agents run.
|
|
42
|
+
* The bridge token is read on every request so it survives a bridge restart.
|
|
43
|
+
*/
|
|
44
|
+
export function createProxyMiddleware({ token, bridge, root, stateDir, allowRemote = true, trustProxy = false }) {
|
|
45
|
+
const tokenFile = path.resolve(root, stateDir, BRIDGE_TOKEN_FILE);
|
|
46
|
+
const down = `The agent bridge on the host is not responding (${bridge}). Start it on the host: npx webai-bridge`;
|
|
47
|
+
return (req, res, next) => {
|
|
48
|
+
const route = routeOf(req);
|
|
49
|
+
if (route == null) return next();
|
|
50
|
+
if (!browserAllowed(req, token, { allowRemote, trustProxy })) return json(res, 403, { error: 'forbidden' });
|
|
51
|
+
|
|
52
|
+
let bridgeToken;
|
|
53
|
+
try {
|
|
54
|
+
bridgeToken = fs.readFileSync(tokenFile, 'utf8').trim();
|
|
55
|
+
} catch {
|
|
56
|
+
return json(res, 502, { error: `${down} (missing ${path.relative(root, tokenFile)})` });
|
|
57
|
+
}
|
|
58
|
+
const url = new URL(BASE + route + new URL(req.url, 'http://x').search, bridge);
|
|
59
|
+
const headers = { 'x-webai-token': bridgeToken };
|
|
60
|
+
for (const h of ['content-type', 'content-length', 'last-event-id']) if (req.headers[h]) headers[h] = req.headers[h];
|
|
61
|
+
|
|
62
|
+
const upstream = http.request(url, { method: req.method, headers }, (up) => {
|
|
63
|
+
if (up.statusCode === 403) {
|
|
64
|
+
up.resume();
|
|
65
|
+
return json(res, 502, { error: 'The bridge rejected the token. Is it running for this project (--root)?' });
|
|
66
|
+
}
|
|
67
|
+
res.writeHead(up.statusCode, up.headers);
|
|
68
|
+
// The bridge going away mid-stream (SSE) must end the browser's response too.
|
|
69
|
+
up.on('aborted', () => res.destroy());
|
|
70
|
+
up.pipe(res);
|
|
71
|
+
});
|
|
72
|
+
upstream.on('error', () => res.headersSent ? res.end() : json(res, 502, { error: down }));
|
|
73
|
+
// SSE: when the browser leaves, close the bridge connection too.
|
|
74
|
+
res.on('close', () => upstream.destroy());
|
|
75
|
+
req.pipe(upstream);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Bridge on the host: called only by the dev server (not the browser), with a token. */
|
|
80
|
+
export function createBridgeHandler(session, token, describe) {
|
|
81
|
+
const api = createApi(session);
|
|
82
|
+
return (req, res) => {
|
|
83
|
+
const route = routeOf(req);
|
|
84
|
+
if (route == null) return json(res, 404, { error: 'not found' });
|
|
85
|
+
// No token: the dev server only reports to the terminal what the bridge offers.
|
|
86
|
+
if (route === '/health' && req.method === 'GET') return json(res, 200, { agents: describe() });
|
|
87
|
+
// Browsers send Origin, the dev server doesn't: no direct access from web pages.
|
|
88
|
+
if (req.headers.origin || req.headers['x-webai-token'] !== token) return json(res, 403, { error: 'forbidden' });
|
|
89
|
+
return api(req, res, route);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function createApi(session) {
|
|
94
|
+
return async (req, res, route) => {
|
|
95
|
+
const url = new URL(req.url, 'http://localhost');
|
|
96
|
+
try {
|
|
97
|
+
if (req.method === 'GET' && route === '/state') return json(res, 200, session.snapshot());
|
|
98
|
+
if (req.method === 'GET' && route === '/events') {
|
|
99
|
+
// After a drop EventSource sends Last-Event-ID, the `since` query stays the original.
|
|
100
|
+
const since = Math.max(Number(url.searchParams.get('since') ?? 0), Number(req.headers['last-event-id'] ?? 0));
|
|
101
|
+
return events(req, res, session, since, url.searchParams.get('tab'));
|
|
102
|
+
}
|
|
103
|
+
if (req.method === 'POST' && route === '/send') {
|
|
104
|
+
const body = await readJson(req);
|
|
105
|
+
if (typeof body.message !== 'string' || !body.message.trim()) return json(res, 400, { error: 'Empty message.' });
|
|
106
|
+
if (session.busy) return json(res, 409, { error: 'The agent is still working on the previous message.' });
|
|
107
|
+
session.send(body);
|
|
108
|
+
return json(res, 202, { ok: true });
|
|
109
|
+
}
|
|
110
|
+
if (req.method === 'POST' && route === '/settings') {
|
|
111
|
+
return json(res, 200, { settings: session.setSettings(await readJson(req)) });
|
|
112
|
+
}
|
|
113
|
+
if (req.method === 'POST' && route === '/permission') {
|
|
114
|
+
const { id, allow } = await readJson(req);
|
|
115
|
+
if (typeof id !== 'string' || typeof allow !== 'boolean') return json(res, 400, { error: 'id is the request, allow is true or false.' });
|
|
116
|
+
session.answerPermission(id, allow);
|
|
117
|
+
return json(res, 200, { ok: true });
|
|
118
|
+
}
|
|
119
|
+
// What the overlay reports about its page, and its answers to MCP tool commands (see pages.js).
|
|
120
|
+
if (req.method === 'POST' && route.startsWith('/page/')) {
|
|
121
|
+
const body = await readJson(req);
|
|
122
|
+
const pages = session.pages;
|
|
123
|
+
if (route === '/page/hello') pages.hello(body);
|
|
124
|
+
else if (route === '/page/log') pages.log(body.tab, body.entries);
|
|
125
|
+
else if (route === '/page/selection') pages.selection(body.tab, body.element, body.page);
|
|
126
|
+
else if (route === '/page/focus') pages.focus(body.tab);
|
|
127
|
+
else if (route === '/page/result') return json(res, 200, { ok: pages.result(body.tab, body) });
|
|
128
|
+
else return json(res, 404, { error: 'not found' });
|
|
129
|
+
return json(res, 200, { ok: true });
|
|
130
|
+
}
|
|
131
|
+
if (req.method === 'POST' && route === '/mcp/call') {
|
|
132
|
+
const { tool, args } = await readJson(req);
|
|
133
|
+
return json(res, 200, { result: await callTool(session, tool, args) });
|
|
134
|
+
}
|
|
135
|
+
if (req.method === 'POST' && route === '/stop') return json(res, 200, { stopped: session.stop() });
|
|
136
|
+
if (req.method === 'GET' && route === '/history') return json(res, 200, session.history.list());
|
|
137
|
+
if (req.method === 'POST' && route === '/history/restore') {
|
|
138
|
+
const { index } = await readJson(req);
|
|
139
|
+
if (!Number.isInteger(index)) return json(res, 400, { error: 'index is a version number.' });
|
|
140
|
+
return json(res, 200, session.restoreVersion(index));
|
|
141
|
+
}
|
|
142
|
+
if (req.method === 'POST' && route === '/reset') {
|
|
143
|
+
session.reset();
|
|
144
|
+
return json(res, 200, { ok: true });
|
|
145
|
+
}
|
|
146
|
+
return json(res, 404, { error: 'not found' });
|
|
147
|
+
} catch (err) {
|
|
148
|
+
return json(res, err.status ?? (err instanceof BadRequest ? 400 : 500), { error: err.message });
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function routeOf(req) {
|
|
154
|
+
const { pathname } = new URL(req.url, 'http://localhost');
|
|
155
|
+
return pathname.startsWith(BASE + '/') ? pathname.slice(BASE.length) : null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** A tool call: this machine only, token, and not from a page (browsers always send Origin or Sec-Fetch-Site). */
|
|
159
|
+
function toolAllowed(req, token) {
|
|
160
|
+
return !!token && isLoopback(req.socket.remoteAddress)
|
|
161
|
+
&& !req.headers.origin && !req.headers['sec-fetch-site']
|
|
162
|
+
&& req.headers['x-webai-token'] === token;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function browserAllowed(req, token, opts) {
|
|
166
|
+
const url = new URL(req.url, 'http://localhost');
|
|
167
|
+
return pageAllowed(req, opts) && (req.headers['x-webai-token'] ?? url.searchParams.get('token')) === token;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Whether a request may come from the page in the browser: this machine (unless
|
|
172
|
+
* `allowRemote`) and the same origin. Also guards the script that carries the token.
|
|
173
|
+
*/
|
|
174
|
+
export function pageAllowed(req, { allowRemote = false, trustProxy = false } = {}) {
|
|
175
|
+
const forwarded = trustProxy && isLoopback(req.socket.remoteAddress);
|
|
176
|
+
const host = (forwarded && req.headers['x-forwarded-host']?.split(',')[0].trim()) || req.headers.host;
|
|
177
|
+
const addr = (forwarded && req.headers['x-forwarded-for']?.split(',')[0].trim()) || req.socket.remoteAddress;
|
|
178
|
+
if (!allowRemote && !(isLoopback(addr) && LOOPBACK.has(hostname(host)))) return false;
|
|
179
|
+
return sameOrigin(req, host);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function isLoopback(addr = '') {
|
|
183
|
+
return addr === '::1' || addr.startsWith('127.') || addr.startsWith('::ffff:127.');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function hostname(host = '') {
|
|
187
|
+
try {
|
|
188
|
+
return new URL(`http://${host}`).hostname;
|
|
189
|
+
} catch {
|
|
190
|
+
return '';
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Another port on localhost is `same-site`, not `same-origin`: neither may read the token.
|
|
195
|
+
function sameOrigin(req, host) {
|
|
196
|
+
if (['cross-site', 'same-site'].includes(req.headers['sec-fetch-site'])) return false;
|
|
197
|
+
const origin = req.headers.origin;
|
|
198
|
+
if (!origin) return req.method === 'GET';
|
|
199
|
+
try {
|
|
200
|
+
return new URL(origin).host === host;
|
|
201
|
+
} catch {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function events(req, res, session, since, tab) {
|
|
207
|
+
res.writeHead(200, {
|
|
208
|
+
'Content-Type': 'text/event-stream',
|
|
209
|
+
// no-transform: proxies in between (Next.js rewrites) must not gzip and so buffer the stream.
|
|
210
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
211
|
+
'X-Accel-Buffering': 'no',
|
|
212
|
+
Connection: 'keep-alive',
|
|
213
|
+
});
|
|
214
|
+
const write = (ev) => res.write(`id: ${ev.seq}\ndata: ${JSON.stringify(ev)}\n\n`);
|
|
215
|
+
for (const ev of session.events) if (ev.seq > since) write(ev);
|
|
216
|
+
res.write(`event: ready\ndata: ${JSON.stringify({ busy: session.busy })}\n\n`);
|
|
217
|
+
const unsubscribe = session.subscribe(write);
|
|
218
|
+
// The same stream carries the commands MCP tools send to this page, so a tab holds one connection.
|
|
219
|
+
const detach = PageHub.validId(tab)
|
|
220
|
+
? session.pages.attach(tab, (cmd) => res.write(`event: command\ndata: ${JSON.stringify(cmd)}\n\n`))
|
|
221
|
+
: null;
|
|
222
|
+
// The comment keeps proxies awake; the named event lets the panel see the connection is alive.
|
|
223
|
+
const ping = setInterval(() => res.write(`: ping\nevent: ping\ndata: ${JSON.stringify({ busy: session.busy })}\n\n`), PING_MS);
|
|
224
|
+
req.on('close', () => {
|
|
225
|
+
clearInterval(ping);
|
|
226
|
+
unsubscribe();
|
|
227
|
+
detach?.();
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function json(res, status, body) {
|
|
232
|
+
res.writeHead(status, { 'Content-Type': 'application/json' });
|
|
233
|
+
res.end(JSON.stringify(body));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
class BadRequest extends Error {}
|
|
237
|
+
|
|
238
|
+
async function readJson(req) {
|
|
239
|
+
let raw = '';
|
|
240
|
+
for await (const chunk of req) {
|
|
241
|
+
raw += chunk;
|
|
242
|
+
if (raw.length > 1_000_000) throw new BadRequest('Request is too large.');
|
|
243
|
+
}
|
|
244
|
+
let body;
|
|
245
|
+
try {
|
|
246
|
+
body = JSON.parse(raw || '{}');
|
|
247
|
+
} catch {
|
|
248
|
+
throw new BadRequest('Request body is not valid JSON.');
|
|
249
|
+
}
|
|
250
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) throw new BadRequest('Expected a JSON object.');
|
|
251
|
+
return body;
|
|
252
|
+
}
|