@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.
Files changed (53) hide show
  1. package/LICENSE +135 -0
  2. package/README.md +24 -2
  3. package/agents/.claude-plugin/plugin.json +4 -0
  4. package/agents/README.md +17 -0
  5. package/agents/agents/a11y-reviewer.md +16 -0
  6. package/agents/agents/copy-editor.md +14 -0
  7. package/agents/skills/czech-copy/SKILL.md +18 -0
  8. package/agents/skills/element-edit/SKILL.md +51 -0
  9. package/agents/skills/stock-images/SKILL.md +25 -0
  10. package/agents/skills/verify-change/SKILL.md +19 -0
  11. package/agents/skills/web-styling/SKILL.md +20 -0
  12. package/bin/webai-agent.js +373 -0
  13. package/bin/webai-bridge.js +5 -0
  14. package/bin/webai.js +4 -0
  15. package/package.json +85 -4
  16. package/src/agents/claude.js +194 -0
  17. package/src/agents/codex.js +111 -0
  18. package/src/agents/custom.js +64 -0
  19. package/src/agents/docker.js +46 -0
  20. package/src/agents/gemini.js +102 -0
  21. package/src/agents/index.js +25 -0
  22. package/src/agents/opencode.js +117 -0
  23. package/src/agents/pack.js +99 -0
  24. package/src/agents/pi.js +143 -0
  25. package/src/agents/webai.js +66 -0
  26. package/src/astro/annotate.js +163 -0
  27. package/src/astro/index.d.ts +5 -0
  28. package/src/astro/index.js +40 -0
  29. package/src/bridge.js +63 -0
  30. package/src/cli.js +119 -0
  31. package/src/client/overlay.js +1169 -0
  32. package/src/config.js +23 -0
  33. package/src/core.js +116 -0
  34. package/src/history.js +108 -0
  35. package/src/html.js +43 -0
  36. package/src/index.d.ts +244 -0
  37. package/src/index.js +10 -0
  38. package/src/inject.js +80 -0
  39. package/src/mcp/endpoint.js +71 -0
  40. package/src/mcp/stdio.js +103 -0
  41. package/src/mcp/tools.js +126 -0
  42. package/src/next/index.d.ts +8 -0
  43. package/src/next/index.js +59 -0
  44. package/src/next/script.d.ts +3 -0
  45. package/src/next/script.js +13 -0
  46. package/src/pages.js +202 -0
  47. package/src/prompt.js +17 -0
  48. package/src/proxy.js +68 -0
  49. package/src/server.js +252 -0
  50. package/src/session.js +497 -0
  51. package/src/static.js +64 -0
  52. package/src/vite.d.ts +5 -0
  53. package/src/vite.js +145 -0
package/src/vite.js ADDED
@@ -0,0 +1,145 @@
1
+ import { createRequire } from 'node:module';
2
+ import path from 'node:path';
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
+ }
67
+
68
+ /**
69
+ * Vite plugin: `plugins: [webai()]` in vite.config, for React, Vue, Svelte,
70
+ * SvelteKit, Solid, Qwik… on Vite's dev server. Only in `vite dev`. The
71
+ * overlay goes into every HTML page the dev server sends, SPA or server-rendered.
72
+ *
73
+ * @param {import('./index').WebAiOptions & { root?: string }} [options]
74
+ * @returns {import('vite').Plugin}
75
+ */
76
+ export default function webai(options = {}) {
77
+ let w = null;
78
+ return {
79
+ name: '@jantcz/webai',
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',
83
+ // For other plugins (and tests): the running instance, `null` before the config is resolved.
84
+ api: {
85
+ get webai() {
86
+ return w;
87
+ },
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
+ },
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
+ }
126
+ w = createWebai({ ...options, root: options.root ?? config.root });
127
+ w.setLogger(viteLogger(config.logger));
128
+ },
129
+ configureServer(server) {
130
+ // Before Vite's own middlewares: the HTML they send passes the injector.
131
+ server.middlewares.use(w.middleware);
132
+ server.middlewares.use(w.injector);
133
+ w.ready().then((line) => server.config.logger.info(`[webai] ${line}`, { timestamp: true }));
134
+ server.httpServer?.once('close', () => w.dispose());
135
+ },
136
+ };
137
+ }
138
+
139
+ function viteLogger(logger) {
140
+ return {
141
+ info: (msg) => logger.info(`[webai] ${msg}`, { timestamp: true }),
142
+ warn: (msg) => logger.warn(`[webai] ${msg}`, { timestamp: true }),
143
+ error: (msg) => logger.error(`[webai] ${msg}`, { timestamp: true }),
144
+ };
145
+ }