@co0ontty/wand 0.2.1 → 0.4.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 (48) hide show
  1. package/README.md +25 -5
  2. package/dist/acp-protocol.d.ts +67 -0
  3. package/dist/acp-protocol.js +291 -0
  4. package/dist/avatar.d.ts +14 -0
  5. package/dist/avatar.js +110 -0
  6. package/dist/claude-pty-bridge.d.ts +137 -0
  7. package/dist/claude-pty-bridge.js +619 -0
  8. package/dist/claude-stream-adapter.d.ts +35 -0
  9. package/dist/claude-stream-adapter.js +153 -0
  10. package/dist/claude-structured-runner.d.ts +27 -0
  11. package/dist/claude-structured-runner.js +106 -0
  12. package/dist/cli.d.ts +1 -1
  13. package/dist/cli.js +10 -2
  14. package/dist/config.js +8 -4
  15. package/dist/message-parser.js +16 -150
  16. package/dist/message-queue.d.ts +57 -0
  17. package/dist/message-queue.js +127 -0
  18. package/dist/middleware/path-safety.d.ts +6 -0
  19. package/dist/middleware/path-safety.js +19 -0
  20. package/dist/middleware/rate-limit.d.ts +8 -0
  21. package/dist/middleware/rate-limit.js +37 -0
  22. package/dist/process-manager.d.ts +82 -27
  23. package/dist/process-manager.js +1445 -822
  24. package/dist/pty-text-utils.d.ts +13 -0
  25. package/dist/pty-text-utils.js +84 -0
  26. package/dist/pwa.d.ts +5 -0
  27. package/dist/pwa.js +118 -0
  28. package/dist/server.js +511 -409
  29. package/dist/session-lifecycle.d.ts +81 -0
  30. package/dist/session-lifecycle.js +181 -0
  31. package/dist/session-logger.d.ts +13 -3
  32. package/dist/session-logger.js +56 -5
  33. package/dist/storage.d.ts +9 -0
  34. package/dist/storage.js +73 -7
  35. package/dist/types.d.ts +112 -6
  36. package/dist/web-ui/content/icon-192.png +0 -0
  37. package/dist/web-ui/content/icon-512.png +0 -0
  38. package/dist/web-ui/content/scripts.js +3770 -852
  39. package/dist/web-ui/content/styles.css +5505 -2779
  40. package/dist/web-ui/index.js +8 -5
  41. package/dist/web-ui/scripts.js +8 -1
  42. package/dist/ws-broadcast.d.ts +27 -0
  43. package/dist/ws-broadcast.js +160 -0
  44. package/package.json +2 -9
  45. package/dist/web-ui/utils.d.ts +0 -4
  46. package/dist/web-ui/utils.js +0 -12
  47. package/dist/web-ui.d.ts +0 -1
  48. package/dist/web-ui.js +0 -2
@@ -12,20 +12,23 @@ export function renderApp(configPath) {
12
12
  <html lang="zh-CN">
13
13
  <head>
14
14
  <meta charset="utf-8" />
15
- <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
15
+ <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no" />
16
16
  <title>Wand Console</title>
17
17
  <meta name="description" content="Local CLI Console for Vibe Coding - Manage terminal sessions from your browser" />
18
18
  <meta name="theme-color" content="#f6f1e8" media="(prefers-color-scheme: light)" />
19
19
  <meta name="theme-color" content="#1f1b17" media="(prefers-color-scheme: dark)" />
20
20
  <meta name="apple-mobile-web-app-capable" content="yes" />
21
- <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
21
+ <meta name="apple-mobile-web-app-status-bar-style" content="default" />
22
22
  <meta name="apple-mobile-web-app-title" content="Wand" />
23
+ <meta name="mobile-web-app-capable" content="yes" />
24
+ <meta name="application-name" content="Wand" />
25
+ <meta name="format-detection" content="telephone=no" />
26
+ <meta name="msapplication-TileColor" content="#c5653d" />
27
+ <meta name="msapplication-tap-highlight" content="no" />
28
+ <link rel="icon" href="/icon.svg" type="image/svg+xml" />
23
29
  <link rel="apple-touch-icon" href="/icon-192.png" />
24
30
  <link rel="manifest" href="/manifest.json" />
25
31
  <link rel="stylesheet" href="/vendor/xterm/css/xterm.css" />
26
- <link rel="preconnect" href="https://fonts.googleapis.com">
27
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
28
- <link href="https://fonts.googleapis.com/css2?family=Geist+Mono:wght@400;500;600&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
29
32
  <style>
30
33
  ${cssStyles}
31
34
  </style>
@@ -1,7 +1,14 @@
1
1
  import * as fs from "node:fs";
2
2
  import * as path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { escapeHtml } from "./utils.js";
4
+ function escapeHtml(value) {
5
+ return String(value)
6
+ .replace(/&/g, "&amp;")
7
+ .replace(/</g, "&lt;")
8
+ .replace(/>/g, "&gt;")
9
+ .replace(/"/g, "&quot;")
10
+ .replace(/'/g, "&#39;");
11
+ }
5
12
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
13
  // Cache the script content
7
14
  let _scriptCache = null;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * WebSocket broadcast manager for process events.
3
+ * Handles debounced output events, backpressure control, and client subscriptions.
4
+ */
5
+ import { WebSocketServer } from "ws";
6
+ import type { SessionSnapshot } from "./types.js";
7
+ export interface ProcessEvent {
8
+ type: "output" | "status" | "started" | "ended" | "usage" | "task";
9
+ sessionId: string;
10
+ data?: unknown;
11
+ }
12
+ export declare class WsBroadcastManager {
13
+ private wss;
14
+ private clients;
15
+ private outputDebounceCache;
16
+ private eventEmitter;
17
+ constructor(wss: WebSocketServer);
18
+ /** Set up connection handling. Should be called once during server startup. */
19
+ setup(getSession: (id: string) => SessionSnapshot | null): void;
20
+ /** Emit a process event to all subscribed WebSocket clients. */
21
+ emitEvent(event: ProcessEvent): void;
22
+ /** Flush any pending debounced output for a session (e.g., before session close). */
23
+ flushOutput(sessionId: string): void;
24
+ private broadcast;
25
+ private processWsQueue;
26
+ private readSessionCookie;
27
+ }
@@ -0,0 +1,160 @@
1
+ /**
2
+ * WebSocket broadcast manager for process events.
3
+ * Handles debounced output events, backpressure control, and client subscriptions.
4
+ */
5
+ import { WebSocket } from "ws";
6
+ import { EventEmitter } from "node:events";
7
+ import { validateSession } from "./auth.js";
8
+ // ── Constants ──
9
+ const MAX_QUEUE_SIZE = 500;
10
+ const OUTPUT_DEBOUNCE_MS = 16;
11
+ // ── Manager ──
12
+ export class WsBroadcastManager {
13
+ wss;
14
+ clients = new Set();
15
+ outputDebounceCache = new Map();
16
+ eventEmitter = new EventEmitter();
17
+ constructor(wss) {
18
+ this.wss = wss;
19
+ }
20
+ /** Set up connection handling. Should be called once during server startup. */
21
+ setup(getSession) {
22
+ this.wss.on("connection", (ws, req) => {
23
+ const sessionToken = this.readSessionCookie(req);
24
+ if (!sessionToken || !validateSession(sessionToken)) {
25
+ ws.close(1008, "Unauthorized");
26
+ return;
27
+ }
28
+ const client = {
29
+ ws,
30
+ sendQueue: [],
31
+ sendInProgress: false,
32
+ backpressurePaused: false,
33
+ lastOutputBySession: new Map(),
34
+ };
35
+ this.clients.add(client);
36
+ ws.on("close", () => {
37
+ this.clients.delete(client);
38
+ });
39
+ ws.on("error", () => {
40
+ // Already closed, ignore
41
+ });
42
+ ws.on("message", (data) => {
43
+ try {
44
+ const msg = JSON.parse(data.toString());
45
+ if (msg.type === "subscribe" && msg.sessionId) {
46
+ const snapshot = getSession(msg.sessionId);
47
+ if (snapshot) {
48
+ ws.send(JSON.stringify({
49
+ type: "init",
50
+ sessionId: msg.sessionId,
51
+ data: { ...snapshot, messages: snapshot.messages, output: snapshot.output },
52
+ }));
53
+ }
54
+ else {
55
+ ws.send(JSON.stringify({
56
+ type: "error",
57
+ sessionId: msg.sessionId,
58
+ error: "Session not found",
59
+ }));
60
+ }
61
+ }
62
+ }
63
+ catch {
64
+ // Ignore malformed messages
65
+ }
66
+ });
67
+ });
68
+ }
69
+ /** Emit a process event to all subscribed WebSocket clients. */
70
+ emitEvent(event) {
71
+ // Debounce output events to reduce flicker during rapid streaming
72
+ if (event.type === "output") {
73
+ const existing = this.outputDebounceCache.get(event.sessionId);
74
+ if (existing) {
75
+ clearTimeout(existing.timer);
76
+ // Accumulate chunk data across debounce window so the browser can
77
+ // write incrementally instead of doing a full terminal reset.
78
+ const prevData = existing.event.data;
79
+ const curData = event.data;
80
+ const prevChunk = prevData?.chunk;
81
+ const curChunk = curData?.chunk;
82
+ if (prevChunk && curChunk) {
83
+ event = { ...event, data: { ...curData, chunk: prevChunk + curChunk } };
84
+ }
85
+ else if (prevChunk && !curChunk) {
86
+ event = { ...event, data: { ...curData, chunk: prevChunk } };
87
+ }
88
+ }
89
+ const timer = setTimeout(() => {
90
+ this.outputDebounceCache.delete(event.sessionId);
91
+ this.broadcast(event);
92
+ }, OUTPUT_DEBOUNCE_MS);
93
+ this.outputDebounceCache.set(event.sessionId, { event, timer });
94
+ return;
95
+ }
96
+ // Non-output events are sent immediately
97
+ this.broadcast(event);
98
+ }
99
+ /** Flush any pending debounced output for a session (e.g., before session close). */
100
+ flushOutput(sessionId) {
101
+ const existing = this.outputDebounceCache.get(sessionId);
102
+ if (existing) {
103
+ clearTimeout(existing.timer);
104
+ this.outputDebounceCache.delete(sessionId);
105
+ this.broadcast(existing.event);
106
+ }
107
+ }
108
+ // ── Internal ──
109
+ broadcast(event) {
110
+ const message = JSON.stringify(event);
111
+ for (const client of this.clients) {
112
+ if (client.ws.readyState !== WebSocket.OPEN)
113
+ continue;
114
+ // Apply backpressure if queue is too large
115
+ if (client.sendQueue.length >= MAX_QUEUE_SIZE) {
116
+ client.backpressurePaused = true;
117
+ continue;
118
+ }
119
+ if (client.backpressurePaused)
120
+ continue;
121
+ client.sendQueue.push(message);
122
+ this.processWsQueue(client);
123
+ }
124
+ }
125
+ processWsQueue(client) {
126
+ if (client.sendInProgress || client.sendQueue.length === 0) {
127
+ return;
128
+ }
129
+ if (client.backpressurePaused) {
130
+ if (client.sendQueue.length < MAX_QUEUE_SIZE * 0.8) {
131
+ client.backpressurePaused = false;
132
+ }
133
+ else {
134
+ return;
135
+ }
136
+ }
137
+ // Check socket state before dequeuing to avoid dropping messages
138
+ if (client.ws.readyState !== WebSocket.OPEN) {
139
+ // Socket closed — discard remaining queue and remove client
140
+ client.sendQueue.length = 0;
141
+ this.clients.delete(client);
142
+ return;
143
+ }
144
+ client.sendInProgress = true;
145
+ const message = client.sendQueue.shift();
146
+ client.ws.send(message, (err) => {
147
+ client.sendInProgress = false;
148
+ if (err)
149
+ return;
150
+ this.processWsQueue(client);
151
+ });
152
+ }
153
+ readSessionCookie(req) {
154
+ const cookie = req.headers.cookie;
155
+ if (!cookie)
156
+ return undefined;
157
+ const match = cookie.split(";").map((part) => part.trim()).find((part) => part.startsWith("wand_session="));
158
+ return match?.slice("wand_session=".length);
159
+ }
160
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@co0ontty/wand",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "A web terminal for local CLI tools like Claude.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -19,13 +19,7 @@
19
19
  "build": "tsc -p tsconfig.json && npm run build:copy-content",
20
20
  "build:copy-content": "cp -r src/web-ui/content dist/web-ui/",
21
21
  "dev": "tsx src/cli.ts web",
22
- "check": "tsc --noEmit -p tsconfig.json",
23
- "pre-release-test": "node scripts/pre-release-test.js",
24
- "test": "npm run pre-release-test",
25
- "test:e2e": "playwright test",
26
- "test:e2e:ui": "playwright test --ui",
27
- "test:e2e:debug": "playwright test --debug",
28
- "test:e2e:report": "playwright show-report"
22
+ "check": "tsc --noEmit -p tsconfig.json"
29
23
  },
30
24
  "keywords": [
31
25
  "cli",
@@ -46,7 +40,6 @@
46
40
  "xterm": "^5.3.0"
47
41
  },
48
42
  "devDependencies": {
49
- "@playwright/test": "^1.58.2",
50
43
  "@types/express": "^4.17.21",
51
44
  "@types/node": "^22.13.14",
52
45
  "@types/ws": "^8.18.1",
@@ -1,4 +0,0 @@
1
- /**
2
- * Escape HTML special characters to prevent XSS
3
- */
4
- export declare function escapeHtml(value: string): string;
@@ -1,12 +0,0 @@
1
- // Shared utilities for web-ui module
2
- /**
3
- * Escape HTML special characters to prevent XSS
4
- */
5
- export function escapeHtml(value) {
6
- return String(value)
7
- .replace(/&/g, "&amp;")
8
- .replace(/</g, "&lt;")
9
- .replace(/>/g, "&gt;")
10
- .replace(/"/g, "&quot;")
11
- .replace(/'/g, "&#39;");
12
- }
package/dist/web-ui.d.ts DELETED
@@ -1 +0,0 @@
1
- export { renderApp } from "./web-ui/index.js";
package/dist/web-ui.js DELETED
@@ -1,2 +0,0 @@
1
- // Web UI entry point - re-exports from modularized web-ui directory
2
- export { renderApp } from "./web-ui/index.js";