bitwrench 2.0.22 → 2.0.24

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 (88) hide show
  1. package/LICENSE.txt +1 -1
  2. package/README.md +4 -3
  3. package/bin/bwmcp.js +3 -0
  4. package/dist/bitwrench-bccl.cjs.js +1 -1
  5. package/dist/bitwrench-bccl.cjs.min.js +1 -1
  6. package/dist/bitwrench-bccl.cjs.min.js.gz +0 -0
  7. package/dist/bitwrench-bccl.esm.js +1 -1
  8. package/dist/bitwrench-bccl.esm.min.js +1 -1
  9. package/dist/bitwrench-bccl.esm.min.js.gz +0 -0
  10. package/dist/bitwrench-bccl.umd.js +1 -1
  11. package/dist/bitwrench-bccl.umd.min.js +1 -1
  12. package/dist/bitwrench-bccl.umd.min.js.gz +0 -0
  13. package/dist/bitwrench-code-edit.cjs.js +1 -1
  14. package/dist/bitwrench-code-edit.cjs.min.js +1 -1
  15. package/dist/bitwrench-code-edit.es5.js +1 -1
  16. package/dist/bitwrench-code-edit.es5.min.js +1 -1
  17. package/dist/bitwrench-code-edit.esm.js +1 -1
  18. package/dist/bitwrench-code-edit.esm.min.js +1 -1
  19. package/dist/bitwrench-code-edit.umd.js +1 -1
  20. package/dist/bitwrench-code-edit.umd.min.js +1 -1
  21. package/dist/bitwrench-code-edit.umd.min.js.gz +0 -0
  22. package/dist/bitwrench-debug.js +1 -1
  23. package/dist/bitwrench-debug.min.js +1 -1
  24. package/dist/bitwrench-lean.cjs.js +3 -3
  25. package/dist/bitwrench-lean.cjs.min.js +2 -2
  26. package/dist/bitwrench-lean.cjs.min.js.gz +0 -0
  27. package/dist/bitwrench-lean.es5.js +3 -3
  28. package/dist/bitwrench-lean.es5.min.js +2 -2
  29. package/dist/bitwrench-lean.es5.min.js.gz +0 -0
  30. package/dist/bitwrench-lean.esm.js +3 -3
  31. package/dist/bitwrench-lean.esm.min.js +2 -2
  32. package/dist/bitwrench-lean.esm.min.js.gz +0 -0
  33. package/dist/bitwrench-lean.umd.js +3 -3
  34. package/dist/bitwrench-lean.umd.min.js +2 -2
  35. package/dist/bitwrench-lean.umd.min.js.gz +0 -0
  36. package/dist/bitwrench-util-css.cjs.js +1 -1
  37. package/dist/bitwrench-util-css.cjs.min.js +1 -1
  38. package/dist/bitwrench-util-css.es5.js +1 -1
  39. package/dist/bitwrench-util-css.es5.min.js +1 -1
  40. package/dist/bitwrench-util-css.esm.js +1 -1
  41. package/dist/bitwrench-util-css.esm.min.js +1 -1
  42. package/dist/bitwrench-util-css.umd.js +1 -1
  43. package/dist/bitwrench-util-css.umd.min.js +1 -1
  44. package/dist/bitwrench-util-css.umd.min.js.gz +0 -0
  45. package/dist/bitwrench.cjs.js +3 -3
  46. package/dist/bitwrench.cjs.min.js +2 -2
  47. package/dist/bitwrench.cjs.min.js.gz +0 -0
  48. package/dist/bitwrench.css +1 -1
  49. package/dist/bitwrench.es5.js +3 -3
  50. package/dist/bitwrench.es5.min.js +2 -2
  51. package/dist/bitwrench.es5.min.js.gz +0 -0
  52. package/dist/bitwrench.esm.js +3 -3
  53. package/dist/bitwrench.esm.min.js +2 -2
  54. package/dist/bitwrench.esm.min.js.gz +0 -0
  55. package/dist/bitwrench.umd.js +3 -3
  56. package/dist/bitwrench.umd.min.js +2 -2
  57. package/dist/bitwrench.umd.min.js.gz +0 -0
  58. package/dist/builds.json +65 -65
  59. package/dist/bwserve.cjs.js +2 -2
  60. package/dist/bwserve.esm.js +2 -2
  61. package/dist/sri.json +45 -45
  62. package/docs/README.md +76 -0
  63. package/docs/app-patterns.md +264 -0
  64. package/docs/bitwrench-mcp.md +426 -0
  65. package/docs/bitwrench_api.md +2232 -0
  66. package/docs/bw-attach.md +399 -0
  67. package/docs/bwserve.md +841 -0
  68. package/docs/cli.md +307 -0
  69. package/docs/component-cheatsheet.md +144 -0
  70. package/docs/component-library.md +1099 -0
  71. package/docs/framework-translation-table.md +33 -0
  72. package/docs/llm-bitwrench-guide.md +672 -0
  73. package/docs/routing.md +562 -0
  74. package/docs/state-management.md +767 -0
  75. package/docs/taco-format.md +373 -0
  76. package/docs/theming.md +309 -0
  77. package/docs/thinking-in-bitwrench.md +1457 -0
  78. package/docs/tutorial-bwserve.md +297 -0
  79. package/docs/tutorial-embedded.md +314 -0
  80. package/docs/tutorial-website.md +255 -0
  81. package/package.json +11 -3
  82. package/readme.html +5 -4
  83. package/src/mcp/knowledge.js +231 -0
  84. package/src/mcp/live.js +226 -0
  85. package/src/mcp/server.js +216 -0
  86. package/src/mcp/tools.js +369 -0
  87. package/src/mcp/transport.js +55 -0
  88. package/src/version.js +3 -3
@@ -0,0 +1,399 @@
1
+ # bwcli attach — Remote Debugging REPL for Bitwrench
2
+
3
+ ## What is bwcli attach?
4
+
5
+ `bwcli attach` is a built-in terminal-based debugger for any bitwrench page. It starts a bwserve server, waits for a browser to connect via a drop-in `<script>` tag, then gives you an interactive REPL where you can:
6
+
7
+ - **Evaluate JavaScript** in the connected browser and see results
8
+ - **Inspect the DOM** with `/tree` — a structured tree view of elements
9
+ - **Take screenshots** with `/screenshot` — capture any element to a PNG file
10
+ - **Listen to events** with `/listen` — watch clicks, inputs, and other DOM events in real time
11
+ - **Mount components** with `/mount` — drop BCCL components into the page
12
+ - **Render TACO** with `/render` — push TACO objects directly to any selector
13
+ - **Patch elements** with `/patch` — update element text by ID
14
+
15
+ Think of it as bitwrench's built-in Playwright — a terminal inspector that speaks the bwserve protocol.
16
+
17
+ ## Quick Start
18
+
19
+ ### 1. Start the attach server
20
+
21
+ ```bash
22
+ bwcli attach
23
+ ```
24
+
25
+ You'll see:
26
+
27
+ ```
28
+ bwcli attach v2.0.18
29
+ Server: http://localhost:7902
30
+ Drop-in: <script src="http://localhost:7902/bw/attach.js"></script>
31
+
32
+ Waiting for connection...
33
+ Type /help for commands, /quit to exit.
34
+ ```
35
+
36
+ ### 2. Connect a browser page
37
+
38
+ **Option A: Script tag** — Add this to any HTML page:
39
+
40
+ ```html
41
+ <script src="http://localhost:7902/bw/attach.js"></script>
42
+ ```
43
+
44
+ **Option B: DevTools console** — Paste this into the browser's developer console:
45
+
46
+ ```javascript
47
+ var s=document.createElement('script');s.src='http://localhost:7902/bw/attach.js';document.head.appendChild(s);
48
+ ```
49
+
50
+ **Option C: Existing bwserve page** — Any bwserve page already has bitwrench loaded. Just add the attach script.
51
+
52
+ Once the browser connects, you'll see:
53
+
54
+ ```
55
+ [connected] client att_k4j8m2x1
56
+ bw>
57
+ ```
58
+
59
+ ### 3. Start debugging
60
+
61
+ ```
62
+ bw> document.title
63
+ "My Page Title"
64
+
65
+ bw> bw.$('.bw-card').length
66
+ 3
67
+
68
+ bw> /tree #app 2
69
+ div#app
70
+ div.main-panel
71
+ h1#title
72
+ ul#list
73
+ div.footer
74
+ ```
75
+
76
+ ## Installation
77
+
78
+ `bwcli` is included with bitwrench. Install globally:
79
+
80
+ ```bash
81
+ npm install -g bitwrench
82
+ ```
83
+
84
+ Or use `npx`:
85
+
86
+ ```bash
87
+ npx bwcli attach
88
+ ```
89
+
90
+ ## CLI Options
91
+
92
+ ```
93
+ bwcli attach [options]
94
+
95
+ Options:
96
+ -p, --port <number> Server port (default: 7902)
97
+ --allow-screenshot Enable /screenshot command (loads html2canvas in browser)
98
+ -v, --verbose Verbose output (show protocol details)
99
+ -h, --help Print help
100
+ ```
101
+
102
+ ### Examples
103
+
104
+ ```bash
105
+ # Start on default port
106
+ bwcli attach
107
+
108
+ # Custom port
109
+ bwcli attach --port 3000
110
+
111
+ # Enable screenshots
112
+ bwcli attach --allow-screenshot
113
+
114
+ # Verbose mode (shows protocol messages)
115
+ bwcli attach -v
116
+ ```
117
+
118
+ ## REPL Command Reference
119
+
120
+ ### JavaScript Expressions
121
+
122
+ Any input that doesn't start with `/` is treated as a JavaScript expression. It is evaluated in the connected browser and the result is printed.
123
+
124
+ ```
125
+ bw> document.title
126
+ "My Page Title"
127
+
128
+ bw> window.innerWidth
129
+ 1440
130
+
131
+ bw> bw.$('.bw-card').length
132
+ 3
133
+
134
+ bw> bw.$('.bw-btn').map(function(b) { return b.textContent; })
135
+ ["Save", "Cancel", "Delete"]
136
+
137
+ bw> location.href
138
+ "http://localhost:3000/dashboard"
139
+ ```
140
+
141
+ **Expression wrapping**: Expressions are automatically wrapped in `return(...)` so the result comes back. Statements (starting with `var`, `let`, `const`, `if`, `for`, `while`, `function`, `try`, `switch`, `throw`, `class`, or `{`) are sent without wrapping.
142
+
143
+ ```
144
+ bw> var x = 42
145
+ undefined
146
+
147
+ bw> x
148
+ 42
149
+ ```
150
+
151
+ ### `/tree [selector] [depth]`
152
+
153
+ Shows a DOM tree summary. Defaults: selector = `body`, depth = `3`.
154
+
155
+ ```
156
+ bw> /tree
157
+ body
158
+ div#app
159
+ div.main-panel
160
+ h1#title
161
+ ul#list
162
+ div.footer
163
+
164
+ bw> /tree #app 2
165
+ div#app
166
+ div.main-panel
167
+ h1#title
168
+ ul#list
169
+ div.footer
170
+
171
+ bw> /tree .bw-card 1
172
+ div.bw-card
173
+ div.bw-card-header
174
+ div.bw-card-body
175
+ div.bw-card-footer
176
+ ```
177
+
178
+ The tree shows tag name, id, and CSS classes for each element. Child elements are indented. Limited to 20 children per level to prevent flooding.
179
+
180
+ ### `/screenshot [selector] [filename]`
181
+
182
+ Captures a screenshot of the specified element (or `body`). Saves as PNG. **Requires `--allow-screenshot` flag.**
183
+
184
+ ```
185
+ bw> /screenshot
186
+ Capturing body ...
187
+ Saved: screenshot-1710595200000.png (1440x900, 245832 bytes)
188
+
189
+ bw> /screenshot body page.png
190
+ Capturing body ...
191
+ Saved: page.png (1440x900, 245832 bytes)
192
+
193
+ bw> /screenshot .bw-card card.png
194
+ Capturing .bw-card ...
195
+ Saved: card.png (400x300, 48291 bytes)
196
+ ```
197
+
198
+ On the first screenshot, html2canvas (vendored, ~194KB) is lazy-loaded in the browser. Subsequent screenshots are fast.
199
+
200
+ ### `/mount <selector> <component> [json-props]`
201
+
202
+ Mounts a BCCL component at the specified selector. The component name is camelCased automatically (e.g., `card` → `makeCard`).
203
+
204
+ ```
205
+ bw> /mount #app card {"title":"Hello","content":"World"}
206
+ Mounted card at #app
207
+
208
+ bw> /mount #sidebar accordion {"items":[{"title":"Section 1","content":"Body 1"}]}
209
+ Mounted accordion at #sidebar
210
+
211
+ bw> /mount #main statCard {"value":"42","label":"Users","variant":"primary"}
212
+ Mounted statCard at #main
213
+ ```
214
+
215
+ ### `/render <selector> <taco-json>`
216
+
217
+ Renders a TACO object at the specified selector. Use for quick UI injection.
218
+
219
+ ```
220
+ bw> /render #app {"t":"h1","c":"Hello from REPL"}
221
+ Rendered at #app
222
+
223
+ bw> /render #app {"t":"div","a":{"class":"bw-alert bw-alert-info"},"c":"Injected alert"}
224
+ Rendered at #app
225
+ ```
226
+
227
+ ### `/patch <id> <content>`
228
+
229
+ Updates the text content of an element by ID. This is the simplest way to change what's displayed.
230
+
231
+ ```
232
+ bw> /patch counter 42
233
+ Patched counter
234
+
235
+ bw> /patch status Online
236
+ Patched status
237
+
238
+ bw> /patch greeting Hello World
239
+ Patched greeting
240
+ ```
241
+
242
+ ### `/listen <selector> <event>`
243
+
244
+ Starts listening for DOM events matching a CSS selector. Events are printed inline in the REPL without disrupting the prompt.
245
+
246
+ ```
247
+ bw> /listen button click
248
+ Listening for click on button
249
+
250
+ [event] click on button → BUTTON#save-btn "Save"
251
+ [event] click on button → BUTTON#cancel-btn "Cancel"
252
+
253
+ bw> /listen .bw-card mouseover
254
+ Listening for mouseover on .bw-card
255
+
256
+ [event] mouseover on .bw-card → DIV "Card Title"
257
+
258
+ bw> /listen input change
259
+ Listening for change on input
260
+
261
+ [event] change on input → INPUT#email
262
+ ```
263
+
264
+ Uses a delegated event listener on `document` with capture phase. Events that match the selector POST back to the server via `/bw/return/event/:clientId`.
265
+
266
+ Adding the same listener twice (same selector + event) is a no-op.
267
+
268
+ ### `/unlisten <selector> <event>`
269
+
270
+ Removes a previously added event listener.
271
+
272
+ ```
273
+ bw> /unlisten button click
274
+ Stopped listening for click on button
275
+ ```
276
+
277
+ ### `/exec <code>`
278
+
279
+ Executes JavaScript on the client without capturing the return value. Fire-and-forget.
280
+
281
+ ```
282
+ bw> /exec alert('Hello from the REPL!')
283
+ Executed.
284
+
285
+ bw> /exec document.body.style.background = 'lightyellow'
286
+ Executed.
287
+ ```
288
+
289
+ ### `/clients`
290
+
291
+ Lists all connected clients. The most recently connected client is marked as active — all commands target this client.
292
+
293
+ ```
294
+ bw> /clients
295
+ att_k4j8m2x1 (active)
296
+ att_m9n3p5q7
297
+ ```
298
+
299
+ ### `/help`, `/h`
300
+
301
+ Show the command reference.
302
+
303
+ ### `/quit`, `/q`
304
+
305
+ Exit the REPL and stop the server.
306
+
307
+ ## How It Works
308
+
309
+ ### Architecture
310
+
311
+ ```
312
+ ┌──────────────────────────────┐ ┌──────────────────────┐
313
+ │ bwcli attach │ │ Target Browser Page │
314
+ │ │ │ │
315
+ │ readline REPL ──────────────┼─SSE─┤ bwclient.attach() │
316
+ │ /screenshot, /tree, etc. │ │ _bw_query, _bw_tree │
317
+ │ JS expressions → query() │◄POST┤ respond() │
318
+ │ │ │ │
319
+ │ bwserve (port 7902) │ │ <script src= │
320
+ │ /bw/events/:id (SSE out) │ │ .../bw/attach.js> │
321
+ │ /bw/return/:r/:id (POST in) │ │ │
322
+ └──────────────────────────────┘ └──────────────────────┘
323
+ ```
324
+
325
+ ### Data Flow
326
+
327
+ 1. **Server starts**: `bwcli attach` creates a bwserve instance on the specified port (default 7902)
328
+ 2. **Drop-in script**: The browser loads `/bw/attach.js` which:
329
+ - Checks if bitwrench is already loaded; if not, injects the UMD bundle from `/bw/lib/bitwrench.umd.js`
330
+ - Evaluates the bwclient.js source inline (including builtins for query, tree, listen, screenshot)
331
+ - Calls `bw._bwClient.attach()` to connect via SSE
332
+ 3. **SSE connection**: The browser opens `GET /bw/events/:clientId` — the server registers the client
333
+ 4. **REPL interaction**: You type JS or slash commands → the server sends protocol messages via SSE
334
+ 5. **Response**: The client evaluates the code and POSTs the result back via `/bw/return/query/:clientId`
335
+ 6. **Events**: When `/listen` is active, DOM events POST back via `/bw/return/event/:clientId`
336
+
337
+ ### Protocol Messages Used
338
+
339
+ | REPL Action | Protocol | Route |
340
+ |-------------|----------|-------|
341
+ | JS expression | `call _bw_query` | `/bw/return/query/:id` |
342
+ | `/tree` | `call _bw_tree` | `/bw/return/query/:id` |
343
+ | `/screenshot` | `call _bw_screenshot` | `/bw/return/screenshot/:id` |
344
+ | `/mount` | `call _bw_mount` | `/bw/return/mount/:id` |
345
+ | `/render` | SSE `replace` | — (one-way) |
346
+ | `/patch` | SSE `patch` | — (one-way) |
347
+ | `/listen` | `call _bw_listen` | `/bw/return/event/:id` |
348
+ | `/exec` | SSE `exec` | — (one-way) |
349
+
350
+ ### Client Builtins
351
+
352
+ Three builtins are registered specifically for attach mode:
353
+
354
+ | Builtin | Purpose |
355
+ |---------|---------|
356
+ | `_bw_tree` | Walk the DOM from a selector to a configurable depth, return `{tag, id, cls, children}` |
357
+ | `_bw_listen` | Add a delegated event listener that POSTs event data back to the server |
358
+ | `_bw_unlisten` | Remove a previously added listener |
359
+
360
+ These are registered automatically when `bw._bwClient.attach()` is called.
361
+
362
+ ## Cross-Origin Usage
363
+
364
+ The attach script and all POST-back endpoints include `Access-Control-Allow-Origin: *` headers, enabling cross-origin debugging. To attach to a page on a different origin:
365
+
366
+ ```html
367
+ <script src="http://debugger-host:7902/bw/attach.js"></script>
368
+ ```
369
+
370
+ OPTIONS preflight requests on `/bw/return/*` are handled with a 204 response and appropriate CORS headers.
371
+
372
+ ## Security
373
+
374
+ - **allowExec is always true**: Attach mode is a debugging tool. The whole point is to execute arbitrary code on the client.
375
+ - **Local-only by default**: The server binds to `localhost`. Do not expose to a network without understanding the implications.
376
+ - **CORS headers**: All endpoints include `Access-Control-Allow-Origin: *` to support cross-origin attach. This is intentional for debugging workflows.
377
+ - **No authentication**: Anyone who can reach the port can attach. Use firewall rules if needed.
378
+
379
+ **Do not run `bwcli attach` on a production server or expose it to the public internet.** It provides full JavaScript execution in the browser and should only be used for local development and debugging.
380
+
381
+ ## Use Cases
382
+
383
+ - **Debugging bitwrench pages**: Inspect the DOM, check component state, test TACO rendering
384
+ - **Rapid prototyping**: Mount components and render TACO from the terminal without editing source files
385
+ - **Event debugging**: Listen to click, input, submit, and other DOM events in real time
386
+ - **Visual regression**: Take screenshots of specific components for comparison
387
+ - **Remote inspection**: Attach to a page running on another machine (within your local network)
388
+ - **AI-driven UI**: Script the REPL from another process for automated UI testing
389
+
390
+ ## Multiple Clients
391
+
392
+ When multiple browsers connect, the most recently connected client becomes the active target. All REPL commands are sent to the active client. Use `/clients` to see the list. When the active client disconnects, the next available client becomes active.
393
+
394
+ ## Related
395
+
396
+ - [bwserve Documentation](bwserve.md) — Full bwserve protocol reference
397
+ - [CLI Documentation](cli.md) — File conversion and `bwcli serve`
398
+ - [Protocol Reference Page](../pages/12-bwserve-protocol.html) — Interactive protocol reference
399
+ - [CLI Design Document](../dev/bw-cli-design.md) -- Architecture and implementation details