@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/src/server.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import fs from 'node:fs';
2
2
  import http from 'node:http';
3
3
  import path from 'node:path';
4
+ import { callTool } from './mcp/tools.js';
5
+ import { PageHub } from './pages.js';
4
6
 
5
7
  export const BASE = '/__webai';
6
8
  export const BRIDGE_TOKEN_FILE = 'bridge-token';
7
9
 
10
+ const PING_MS = 15000;
11
+
8
12
  const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
9
13
 
10
14
  /**
@@ -20,11 +24,13 @@ const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
20
24
  * `trustProxy`: requests come through a reverse proxy on this machine (Next.js
21
25
  * rewrites), so the browser's address and host are in X-Forwarded-For/-Host.
22
26
  */
23
- export function createMiddleware(session, token, { allowRemote = false, trustProxy = false } = {}) {
27
+ export function createMiddleware(session, token, { allowRemote = false, trustProxy = false, mcpToken = null } = {}) {
24
28
  const api = createApi(session);
25
29
  return (req, res, next) => {
26
30
  const route = routeOf(req);
27
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' });
28
34
  if (!browserAllowed(req, token, { allowRemote, trustProxy })) return json(res, 403, { error: 'forbidden' });
29
35
  return api(req, res, route);
30
36
  };
@@ -92,7 +98,7 @@ function createApi(session) {
92
98
  if (req.method === 'GET' && route === '/events') {
93
99
  // After a drop EventSource sends Last-Event-ID, the `since` query stays the original.
94
100
  const since = Math.max(Number(url.searchParams.get('since') ?? 0), Number(req.headers['last-event-id'] ?? 0));
95
- return events(req, res, session, since);
101
+ return events(req, res, session, since, url.searchParams.get('tab'));
96
102
  }
97
103
  if (req.method === 'POST' && route === '/send') {
98
104
  const body = await readJson(req);
@@ -104,6 +110,28 @@ function createApi(session) {
104
110
  if (req.method === 'POST' && route === '/settings') {
105
111
  return json(res, 200, { settings: session.setSettings(await readJson(req)) });
106
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
+ }
107
135
  if (req.method === 'POST' && route === '/stop') return json(res, 200, { stopped: session.stop() });
108
136
  if (req.method === 'GET' && route === '/history') return json(res, 200, session.history.list());
109
137
  if (req.method === 'POST' && route === '/history/restore') {
@@ -127,6 +155,13 @@ export function routeOf(req) {
127
155
  return pathname.startsWith(BASE + '/') ? pathname.slice(BASE.length) : null;
128
156
  }
129
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
+
130
165
  function browserAllowed(req, token, opts) {
131
166
  const url = new URL(req.url, 'http://localhost');
132
167
  return pageAllowed(req, opts) && (req.headers['x-webai-token'] ?? url.searchParams.get('token')) === token;
@@ -168,7 +203,7 @@ function sameOrigin(req, host) {
168
203
  }
169
204
  }
170
205
 
171
- function events(req, res, session, since) {
206
+ function events(req, res, session, since, tab) {
172
207
  res.writeHead(200, {
173
208
  'Content-Type': 'text/event-stream',
174
209
  // no-transform: proxies in between (Next.js rewrites) must not gzip and so buffer the stream.
@@ -180,10 +215,16 @@ function events(req, res, session, since) {
180
215
  for (const ev of session.events) if (ev.seq > since) write(ev);
181
216
  res.write(`event: ready\ndata: ${JSON.stringify({ busy: session.busy })}\n\n`);
182
217
  const unsubscribe = session.subscribe(write);
183
- const ping = setInterval(() => res.write(': ping\n\n'), 20000);
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);
184
224
  req.on('close', () => {
185
225
  clearInterval(ping);
186
226
  unsubscribe();
227
+ detach?.();
187
228
  });
188
229
  }
189
230
 
package/src/session.js CHANGED
@@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto';
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
5
5
  import { History } from './history.js';
6
+ import { PageHub } from './pages.js';
6
7
  import { buildPrompt } from './prompt.js';
7
8
 
8
9
  const MAX_EVENTS = 1000;
@@ -15,8 +16,11 @@ the data-webai-src attribute). Without a source location, find the element in
15
16
  the sources by its text, classes and structure first.
16
17
  Make the change directly in the source files, keep the edit minimal and in
17
18
  the style of the surrounding code. The page reloads by itself, never start
18
- a dev server or a build. When done, answer briefly (one or two sentences)
19
- in the user's language, saying what you changed and where.`;
19
+ a dev server or a build. Don't write code that imports a package which isn't
20
+ installed: install it first, or use something else. If a tool you need is denied
21
+ or missing, say so and never claim something ran that didn't.
22
+ When done, answer briefly (one or two sentences) in the user's language,
23
+ saying what you changed and where.`;
20
24
 
21
25
  /**
22
26
  * Conversations with agents. Each agent (Claude, Codex, …) has its own session;
@@ -28,8 +32,9 @@ in the user's language, saying what you changed and where.`;
28
32
  export { buildPrompt };
29
33
 
30
34
  export class AgentSession {
31
- constructor({ root, agents, stateDir, logger }) {
35
+ constructor({ root, agents, stateDir, logger, onOrigin }) {
32
36
  this.root = root;
37
+ this.pages = new PageHub({ onOrigin }); // the browser's side: selection, console, commands
33
38
  this.agents = agents; // { name: adapter }
34
39
  this.logger = logger;
35
40
  this.dir = path.resolve(root, stateDir);
@@ -37,6 +42,7 @@ export class AgentSession {
37
42
  this.seq = 0;
38
43
  this.listeners = new Set();
39
44
  this.proc = null;
45
+ this.pending = new Map(); // permission requests waiting for an answer: id -> { input }
40
46
  this.available = {}; // name -> version, or null when the CLI is missing
41
47
  this.state = { sessions: {} };
42
48
  this.settings = { agent: Object.keys(agents)[0] };
@@ -113,6 +119,11 @@ export class AgentSession {
113
119
  this.seq = Math.max(this.seq, ev.seq);
114
120
  } catch {} // truncated line after a crash: skip it, the rest of the history is valid
115
121
  }
122
+ // A question left over from a previous run can't be answered any more.
123
+ const answered = new Set(this.events.filter((e) => e.type === 'permission-answer').map((e) => e.id));
124
+ for (const e of this.events.filter((e) => e.type === 'permission' && !answered.has(e.id))) {
125
+ this.events.push({ type: 'permission-answer', id: e.id, allow: false, cancelled: true, seq: ++this.seq, ts: Date.now() });
126
+ }
116
127
  // The file only grows; on startup trim it to what gets loaded anyway.
117
128
  if (lines.length > MAX_EVENTS) fs.writeFileSync(file, this.events.map((ev) => JSON.stringify(ev) + '\n').join(''));
118
129
  }
@@ -255,7 +266,7 @@ export class AgentSession {
255
266
  this.emit({ type: 'done', ok: false, error: err.message });
256
267
  return;
257
268
  }
258
- const { cmd, args, stdin, promptInArgs, env, cleanup } = spec;
269
+ const { cmd, args, stdin, promptInArgs, env, cleanup, interactive, input } = spec;
259
270
  this.emit({ type: 'status', text: `${agent.label} is working…`, busy: true });
260
271
  const ctx = { root: this.root };
261
272
 
@@ -303,9 +314,19 @@ export class AgentSession {
303
314
  this.saveState();
304
315
  continue;
305
316
  }
317
+ if (ev.type === 'permission') {
318
+ // The tool's input (a whole file for Write) stays here: the answer must echo it back.
319
+ this.pending.set(ev.id, { input: ev.input });
320
+ ev = { ...ev, input: undefined };
321
+ }
322
+ if (ev.type === 'permission-answer') {
323
+ if (!this.pending.delete(ev.id)) continue;
324
+ }
306
325
  if (ev.type === 'done') {
307
326
  finished = true;
308
327
  ev = this.priceTurn(ev, session);
328
+ // The agent waits for more input on stdin; the turn is over.
329
+ if (interactive) proc.stdin.end();
309
330
  }
310
331
  this.emit(ev);
311
332
  }
@@ -337,6 +358,7 @@ export class AgentSession {
337
358
  proc.on('close', guard((code, signal) => {
338
359
  if (!current()) return;
339
360
  this.proc = null;
361
+ this.cancelPending();
340
362
  if (!retried && resume && agent.isMissingSession?.(stderr)) {
341
363
  // The session disappeared from disk (deleted history, another machine): start a new one.
342
364
  Object.assign(session, this.freshSession());
@@ -358,10 +380,28 @@ export class AgentSession {
358
380
  }
359
381
  }));
360
382
 
361
- if (stdin) proc.stdin.end(text);
383
+ // stdin errors (EPIPE after the agent exited) must not crash the dev server.
384
+ proc.stdin.on('error', () => {});
385
+ if (interactive) proc.stdin.write(input(text));
386
+ else if (stdin) proc.stdin.end(text);
362
387
  else proc.stdin.end();
363
388
  }
364
389
 
390
+ /** The user's answer to a `permission` event; the agent continues or tries something else. */
391
+ answerPermission(id, allow) {
392
+ const request = this.pending.get(id);
393
+ if (!request || !this.proc) throw Object.assign(new Error('That question is no longer open.'), { status: 404 });
394
+ this.pending.delete(id);
395
+ this.proc.stdin.write(this.agent.answer({ requestId: id, input: request.input }, !!allow));
396
+ this.emit({ type: 'permission-answer', id, allow: !!allow });
397
+ }
398
+
399
+ /** Questions that can't be answered any more (the turn ended, Stop, a new conversation). */
400
+ cancelPending() {
401
+ for (const id of this.pending.keys()) this.emit({ type: 'permission-answer', id, allow: false, cancelled: true });
402
+ this.pending.clear();
403
+ }
404
+
365
405
  /** Once per server run: links the skills the agent looks for in the project. */
366
406
  prepare(agent) {
367
407
  if (!agent.prepare || this.prepared.has(agent.name)) return;
@@ -400,6 +440,7 @@ export class AgentSession {
400
440
  // Synchronous: also called from process.on('exit'), where async work never finishes.
401
441
  if (this.proc) killGroup(this.proc, 'SIGTERM', { sync: true });
402
442
  this.proc = null;
443
+ this.pages.dispose();
403
444
  process.off('exit', this.onExit);
404
445
  }
405
446
 
@@ -407,6 +448,7 @@ export class AgentSession {
407
448
  reset() {
408
449
  this.stop();
409
450
  this.proc = null; // this discards the exiting process's output (see current() in run)
451
+ this.pending.clear();
410
452
  this.state = { sessions: {} };
411
453
  this.saveState();
412
454
  this.events = [];
package/src/vite.js CHANGED
@@ -1,4 +1,69 @@
1
+ import { createRequire } from 'node:module';
2
+ import path from 'node:path';
1
3
  import { createWebai } from './core.js';
4
+ import { SRC_ATTR } from './html.js';
5
+
6
+ const JSX_RUNTIME = 'react/jsx-dev-runtime';
7
+ const JSX_FILE = /\.[jt]sx$/;
8
+ const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
9
+
10
+ /** The numbers of one source map segment (base64 VLQ). */
11
+ function vlq(segment) {
12
+ const out = [];
13
+ let value = 0;
14
+ let shift = 0;
15
+ for (const ch of segment) {
16
+ const digit = B64.indexOf(ch);
17
+ value += (digit & 31) << shift;
18
+ if (digit & 32) {
19
+ shift += 5;
20
+ continue;
21
+ }
22
+ out.push(value & 1 ? -(value >> 1) : value >> 1);
23
+ value = shift = 0;
24
+ }
25
+ return out;
26
+ }
27
+
28
+ /**
29
+ * How many lines something put in front of the file's own code (plugin-react's Fast Refresh header is
30
+ * 19 of them): the first mapped line of the source map minus the source line it maps to. esbuild numbers
31
+ * the JSX of the code it is given, so without this the places it reports are that much too high.
32
+ */
33
+ export function leadingOffset(mappings) {
34
+ const lines = typeof mappings === 'string' ? mappings.split(';') : [];
35
+ const first = lines.findIndex((l) => l !== '');
36
+ if (first < 1) return 0;
37
+ const [, , srcLine] = vlq(lines[first].split(',')[0]);
38
+ return srcLine === undefined || srcLine < 0 ? 0 : Math.max(0, first - srcLine); // a broken map says nothing
39
+ }
40
+ const VIRTUAL = '\0webai-jsx-dev-runtime';
41
+
42
+ /**
43
+ * React 19 dropped `_debugSource`, but the JSX transform still hands `jsxDEV` the place of every
44
+ * element ({ fileName, lineNumber, columnNumber }) and React ignores it. This stand-in for
45
+ * `react/jsx-dev-runtime` writes it to the host elements as `data-webai-src`, like Astro's compiler does.
46
+ */
47
+ export function jsxRuntimeModule(root, attr = SRC_ATTR) {
48
+ const base = root.replace(/\\/g, '/').replace(/\/?$/, '/');
49
+ return `import * as real from '${JSX_RUNTIME}';
50
+ const ROOT = ${JSON.stringify(base)};
51
+ const ATTR = ${JSON.stringify(attr)};
52
+ export const Fragment = real.Fragment;
53
+ export const jsx = real.jsx;
54
+ export const jsxs = real.jsxs;
55
+ export function jsxDEV(type, props, key, isStatic, source, self) {
56
+ const file = source?.fileName?.replace(/\\\\/g, '/');
57
+ if (typeof type === 'string' && file && !file.includes('/node_modules/') && props && !(ATTR in props)) {
58
+ const rel = file.startsWith(ROOT) ? file.slice(ROOT.length) : file;
59
+ // What the transforms before esbuild put in front of the file (see leadingOffset in vite.js).
60
+ const line = source.lineNumber - (globalThis.__webaiLineOffsets?.[source.fileName] ?? 0);
61
+ props = { ...props, [ATTR]: line >= 1 ? rel + ':' + line + ':' + source.columnNumber : rel };
62
+ }
63
+ return real.jsxDEV(type, props, key, isStatic, source, self);
64
+ }
65
+ `;
66
+ }
2
67
 
3
68
  /**
4
69
  * Vite plugin: `plugins: [webai()]` in vite.config, for React, Vue, Svelte,
@@ -13,13 +78,51 @@ export default function webai(options = {}) {
13
78
  return {
14
79
  name: '@jantcz/webai',
15
80
  apply: 'serve',
81
+ // Before Vite's own resolver: React's dev runtime is swapped for the one that records source places.
82
+ enforce: 'pre',
16
83
  // For other plugins (and tests): the running instance, `null` before the config is resolved.
17
84
  api: {
18
85
  get webai() {
19
86
  return w;
20
87
  },
21
88
  },
89
+ config(config) {
90
+ // The stand-in imports the real runtime, which must be pre-bundled up front (a discovery at run time reloads the page).
91
+ const from = path.resolve(options.root ?? config.root ?? process.cwd(), 'package.json');
92
+ try {
93
+ createRequire(from).resolve(JSX_RUNTIME);
94
+ } catch {
95
+ return; // not a React project
96
+ }
97
+ return { optimizeDeps: { include: [JSX_RUNTIME] } };
98
+ },
99
+ // After plugin-react (it must come first in `plugins`): tell the runtime how far its header pushed the lines down.
100
+ transform(code, id) {
101
+ const file = id.split('?')[0];
102
+ if (!JSX_FILE.test(file) || file.includes('/node_modules/')) return;
103
+ let map;
104
+ try {
105
+ map = this.getCombinedSourcemap();
106
+ } catch {
107
+ return; // no source map to read it from: the places stay as esbuild gave them
108
+ }
109
+ const offset = leadingOffset(map?.mappings);
110
+ if (!offset) return;
111
+ // One line, in front of the first: it must not move any line.
112
+ const note = `globalThis.__webaiLineOffsets = { ...globalThis.__webaiLineOffsets, [${JSON.stringify(file)}]: ${offset} };`;
113
+ return { code: note + code, map: null };
114
+ },
115
+ resolveId(source, importer) {
116
+ if (source === JSX_RUNTIME && importer !== VIRTUAL) return VIRTUAL;
117
+ },
118
+ load(id) {
119
+ if (id === VIRTUAL) return jsxRuntimeModule(w.root);
120
+ },
22
121
  configResolved(config) {
122
+ const names = config.plugins?.map((p) => p.name) ?? [];
123
+ if (names.indexOf('vite:react-babel') > names.indexOf('@jantcz/webai') && names.includes('@jantcz/webai')) {
124
+ config.logger.warn('[webai] List webai() after react() in plugins: otherwise the line numbers of React elements are off by the length of its Fast Refresh header.');
125
+ }
23
126
  w = createWebai({ ...options, root: options.root ?? config.root });
24
127
  w.setLogger(viteLogger(config.logger));
25
128
  },