@d0nj/dsh-browser 0.0.0-stage → 0.1.1

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/dist/ws.js ADDED
@@ -0,0 +1,290 @@
1
+ /**
2
+ * Minimal RFC 6455 WebSocket client for the DevTools protocol.
3
+ *
4
+ * Chrome speaks CDP over one WebSocket, and the alternative to this file is a
5
+ * runtime dependency or a Node-version gamble on the global `WebSocket`. The
6
+ * `browser` tool runs inside whichever Node the Harness bundles, so the
7
+ * handshake and frame codec are implemented here against `node:http` and
8
+ * `node:stream`, with no protocol surface beyond what CDP uses: text frames,
9
+ * fragmentation, ping/pong, and close.
10
+ *
11
+ * @module @d0nj/dsh-browser/ws
12
+ */
13
+ import { createHash, randomBytes } from 'node:crypto';
14
+ import { request as httpsRequest } from 'node:https';
15
+ import { request as httpRequest } from 'node:http';
16
+ /** RFC 6455 handshake salt, appended to the client key before hashing. */
17
+ const ACCEPT_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
18
+ /** Refuse messages beyond this size instead of growing the heap without bound. */
19
+ const MAX_MESSAGE_BYTES = 128 * 1024 * 1024;
20
+ /** Opcodes this client understands. */
21
+ export const OPCODE = {
22
+ continuation: 0x0,
23
+ text: 0x1,
24
+ close: 0x8,
25
+ ping: 0x9,
26
+ pong: 0xa,
27
+ };
28
+ /** Thrown when the connection cannot be established or is used after closing. */
29
+ export class WebSocketError extends Error {
30
+ constructor(message, options) {
31
+ super(message, options);
32
+ this.name = 'WebSocketError';
33
+ }
34
+ }
35
+ /**
36
+ * Decode the framing layer from a byte stream.
37
+ *
38
+ * The decoder buffers across chunk boundaries, so callers may feed it whatever
39
+ * the socket delivers.
40
+ */
41
+ export class FrameDecoder {
42
+ #buffer = Buffer.alloc(0);
43
+ /**
44
+ * Consume one socket chunk.
45
+ * @param chunk - bytes received.
46
+ * @returns every complete frame now available.
47
+ */
48
+ push(chunk) {
49
+ this.#buffer = this.#buffer.length === 0 ? chunk : Buffer.concat([this.#buffer, chunk]);
50
+ const frames = [];
51
+ for (;;) {
52
+ const frame = this.#read();
53
+ if (frame === undefined)
54
+ return frames;
55
+ frames.push(frame);
56
+ }
57
+ }
58
+ #read() {
59
+ const buffer = this.#buffer;
60
+ if (buffer.length < 2)
61
+ return undefined;
62
+ const first = buffer[0];
63
+ const second = buffer[1];
64
+ const fin = (first & 0x80) !== 0;
65
+ const opcode = first & 0x0f;
66
+ const masked = (second & 0x80) !== 0;
67
+ let length = second & 0x7f;
68
+ let offset = 2;
69
+ if (length === 126) {
70
+ if (buffer.length < 4)
71
+ return undefined;
72
+ length = buffer.readUInt16BE(2);
73
+ offset = 4;
74
+ }
75
+ else if (length === 127) {
76
+ if (buffer.length < 10)
77
+ return undefined;
78
+ const wide = buffer.readBigUInt64BE(2);
79
+ if (wide > BigInt(MAX_MESSAGE_BYTES))
80
+ throw new WebSocketError(`frame of ${wide} bytes exceeds the ${MAX_MESSAGE_BYTES}-byte limit`);
81
+ length = Number(wide);
82
+ offset = 10;
83
+ }
84
+ const maskLength = masked ? 4 : 0;
85
+ if (buffer.length < offset + maskLength + length)
86
+ return undefined;
87
+ let payload = buffer.subarray(offset + maskLength, offset + maskLength + length);
88
+ if (masked) {
89
+ const mask = buffer.subarray(offset, offset + 4);
90
+ payload = Buffer.from(payload);
91
+ for (let index = 0; index < payload.length; index++) {
92
+ payload[index] = payload[index] ^ mask[index % 4];
93
+ }
94
+ }
95
+ this.#buffer = buffer.subarray(offset + maskLength + length);
96
+ return { fin, opcode, payload: Buffer.from(payload) };
97
+ }
98
+ }
99
+ /**
100
+ * Encode one frame.
101
+ * @param opcode - frame opcode.
102
+ * @param payload - frame payload.
103
+ * @param options - `fin` defaults to true; `mask` defaults to true (client frames).
104
+ * @returns the frame bytes.
105
+ */
106
+ export function encodeFrame(opcode, payload, options = {}) {
107
+ const fin = options.fin ?? true;
108
+ const mask = options.mask ?? true;
109
+ const length = payload.length;
110
+ const headerLength = length < 126 ? 2 : length < 65_536 ? 4 : 10;
111
+ const header = Buffer.alloc(headerLength + (mask ? 4 : 0));
112
+ header[0] = (fin ? 0x80 : 0) | opcode;
113
+ const maskBit = mask ? 0x80 : 0;
114
+ if (length < 126) {
115
+ header[1] = maskBit | length;
116
+ }
117
+ else if (length < 65_536) {
118
+ header[1] = maskBit | 126;
119
+ header.writeUInt16BE(length, 2);
120
+ }
121
+ else {
122
+ header[1] = maskBit | 127;
123
+ header.writeBigUInt64BE(BigInt(length), 2);
124
+ }
125
+ if (!mask)
126
+ return Buffer.concat([header, payload]);
127
+ const key = randomBytes(4);
128
+ key.copy(header, headerLength);
129
+ const masked = Buffer.alloc(length);
130
+ for (let index = 0; index < length; index++) {
131
+ masked[index] = payload[index] ^ key[index % 4];
132
+ }
133
+ return Buffer.concat([header, masked]);
134
+ }
135
+ /**
136
+ * Establish a WebSocket connection.
137
+ * @param url - `ws:` or `wss:` URL.
138
+ * @param timeoutMs - handshake budget in milliseconds.
139
+ * @param signal - optional cancellation for the handshake.
140
+ * @param listeners - message and close callbacks.
141
+ * @returns the open connection.
142
+ */
143
+ export async function openWebSocket(url, timeoutMs, listeners, signal) {
144
+ const target = new URL(url);
145
+ if (target.protocol !== 'ws:' && target.protocol !== 'wss:') {
146
+ throw new WebSocketError(`"${url}" is not a WebSocket URL`);
147
+ }
148
+ const key = randomBytes(16).toString('base64');
149
+ const expectedAccept = createHash('sha1').update(`${key}${ACCEPT_GUID}`).digest('base64');
150
+ const request = target.protocol === 'wss:' ? httpsRequest : httpRequest;
151
+ const socket = await new Promise((resolve, reject) => {
152
+ const req = request({
153
+ protocol: target.protocol === 'wss:' ? 'https:' : 'http:',
154
+ hostname: target.hostname,
155
+ port: target.port,
156
+ path: `${target.pathname}${target.search}`,
157
+ headers: {
158
+ Connection: 'Upgrade',
159
+ Upgrade: 'websocket',
160
+ 'Sec-WebSocket-Key': key,
161
+ 'Sec-WebSocket-Version': '13',
162
+ },
163
+ });
164
+ const timer = setTimeout(() => {
165
+ req.destroy();
166
+ reject(new WebSocketError(`WebSocket handshake with ${target.host} timed out after ${timeoutMs}ms`));
167
+ }, timeoutMs);
168
+ const onAbort = () => {
169
+ req.destroy();
170
+ reject(new WebSocketError('WebSocket handshake aborted'));
171
+ };
172
+ signal?.addEventListener('abort', onAbort, { once: true });
173
+ const settle = () => {
174
+ clearTimeout(timer);
175
+ signal?.removeEventListener('abort', onAbort);
176
+ };
177
+ req.on('upgrade', (response, upgraded, head) => {
178
+ settle();
179
+ if (response.headers['sec-websocket-accept'] !== expectedAccept) {
180
+ upgraded.destroy();
181
+ reject(new WebSocketError(`WebSocket handshake with ${target.host} failed: the server returned an unexpected Sec-WebSocket-Accept`));
182
+ return;
183
+ }
184
+ if (head.length > 0)
185
+ upgraded.unshift(head);
186
+ resolve(upgraded);
187
+ });
188
+ req.on('response', (response) => {
189
+ settle();
190
+ response.resume();
191
+ req.destroy();
192
+ reject(new WebSocketError(`WebSocket handshake with ${target.host} was refused with HTTP ${response.statusCode ?? 0}`));
193
+ });
194
+ req.on('error', (error) => {
195
+ settle();
196
+ reject(error instanceof WebSocketError ? error : new WebSocketError(`WebSocket connection to ${target.host} failed: ${error.message}`, { cause: error }));
197
+ });
198
+ req.end();
199
+ });
200
+ return new Connection(url, socket, listeners);
201
+ }
202
+ class Connection {
203
+ url;
204
+ #socket;
205
+ #listeners;
206
+ #decoder = new FrameDecoder();
207
+ #fragments = [];
208
+ #fragmentOpcode;
209
+ #closed = false;
210
+ constructor(url, socket, listeners) {
211
+ this.url = url;
212
+ this.#socket = socket;
213
+ this.#listeners = listeners;
214
+ socket.setNoDelay?.(true);
215
+ socket.on('data', (chunk) => this.#receive(chunk));
216
+ socket.on('error', (error) => this.#finish(1006, error.message));
217
+ socket.on('close', () => this.#finish(1006, 'connection closed'));
218
+ }
219
+ get closed() {
220
+ return this.#closed;
221
+ }
222
+ send(text) {
223
+ if (this.#closed)
224
+ throw new WebSocketError('cannot send on a closed WebSocket');
225
+ this.#socket.write(encodeFrame(OPCODE.text, Buffer.from(text, 'utf8')));
226
+ }
227
+ close(code = 1000, reason = '') {
228
+ if (this.#closed)
229
+ return;
230
+ const payload = Buffer.alloc(2 + Buffer.byteLength(reason));
231
+ payload.writeUInt16BE(code, 0);
232
+ payload.write(reason, 2);
233
+ try {
234
+ this.#socket.write(encodeFrame(OPCODE.close, payload));
235
+ }
236
+ catch {
237
+ // A dead socket needs no close frame.
238
+ }
239
+ this.#finish(code, reason);
240
+ }
241
+ #receive(chunk) {
242
+ let frames;
243
+ try {
244
+ frames = this.#decoder.push(chunk);
245
+ }
246
+ catch (error) {
247
+ this.#finish(1002, error instanceof Error ? error.message : 'malformed frame');
248
+ return;
249
+ }
250
+ for (const frame of frames) {
251
+ if (frame.opcode === OPCODE.ping) {
252
+ this.#socket.write(encodeFrame(OPCODE.pong, frame.payload));
253
+ continue;
254
+ }
255
+ if (frame.opcode === OPCODE.pong)
256
+ continue;
257
+ if (frame.opcode === OPCODE.close) {
258
+ const code = frame.payload.length >= 2 ? frame.payload.readUInt16BE(0) : 1005;
259
+ this.close(code, '');
260
+ return;
261
+ }
262
+ if (frame.opcode === OPCODE.continuation) {
263
+ if (this.#fragmentOpcode === undefined) {
264
+ this.#finish(1002, 'continuation frame without a start frame');
265
+ return;
266
+ }
267
+ this.#fragments.push(frame.payload);
268
+ }
269
+ else {
270
+ this.#fragmentOpcode = frame.opcode;
271
+ this.#fragments = [frame.payload];
272
+ }
273
+ if (!frame.fin)
274
+ continue;
275
+ const payload = Buffer.concat(this.#fragments);
276
+ this.#fragments = [];
277
+ this.#fragmentOpcode = undefined;
278
+ this.#listeners.message(payload.toString('utf8'));
279
+ }
280
+ }
281
+ #finish(code, reason) {
282
+ if (this.#closed)
283
+ return;
284
+ this.#closed = true;
285
+ this.#socket.removeAllListeners('data');
286
+ this.#socket.destroy();
287
+ this.#listeners.close(code, reason);
288
+ }
289
+ }
290
+ //# sourceMappingURL=ws.js.map
package/icon.svg ADDED
@@ -0,0 +1,15 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Browser use">
2
+ <defs>
3
+ <linearGradient id="glow" x1="0" y1="0" x2="1" y2="1">
4
+ <stop offset="0" stop-color="#38bdf8"/>
5
+ <stop offset="1" stop-color="#6366f1"/>
6
+ </linearGradient>
7
+ </defs>
8
+ <rect x="5" y="10" width="54" height="44" rx="9" fill="none" stroke="url(#glow)" stroke-width="2.6"/>
9
+ <path d="M5 22h54" stroke="url(#glow)" stroke-width="2.1" opacity="0.65"/>
10
+ <circle cx="12.6" cy="16" r="1.8" fill="#38bdf8"/>
11
+ <circle cx="18.6" cy="16" r="1.8" fill="#5aa9f7" opacity="0.8"/>
12
+ <circle cx="24.6" cy="16" r="1.8" fill="#6366f1" opacity="0.6"/>
13
+ <path d="M13 31h20M13 37h13" stroke="url(#glow)" stroke-width="2.2" stroke-linecap="round" opacity="0.55"/>
14
+ <path d="M35.5 33.5 51 43.2l-6.4 1.3 3.4 6.6-3.6 1.8-3.4-6.7-4.4 4.3z" fill="url(#glow)"/>
15
+ </svg>
package/locale/en.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Browser Use",
4
+ "description": "Gives the agent a real browser, headless by default: open a page, read it as a numbered accessibility tree, then click, type, scroll or screenshot by ref."
5
+ }
6
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "浏览器操作",
4
+ "description": "为智能体提供一个真实的浏览器(默认无窗口):打开页面、按可访问性树读取页面,并通过元素编号进行点击、输入、滚动与截图。"
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@d0nj/dsh-browser",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "description": "A real browser for DeepSeek Harness agents, headless by default: the `browser` tool navigates, reads the page as a numbered accessibility tree, and clicks, types and scrolls by ref.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/d0nj/dsh-plugins.git",
9
+ "directory": "packages/browser"
10
+ },
11
+ "keywords": [
12
+ "dsh-plugin",
13
+ "deepseek-harness",
14
+ "cordis",
15
+ "browser-use",
16
+ "browser-automation",
17
+ "chrome",
18
+ "cdp",
19
+ "accessibility-tree"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "type": "module",
25
+ "main": "dist/index.js",
26
+ "types": "dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "./locale/*.json": "./locale/*.json",
33
+ "./icon": "./icon.svg"
34
+ },
35
+ "files": [
36
+ "dist/**/*.js",
37
+ "dist/**/*.d.ts",
38
+ "cordis.patch.yml",
39
+ "locale/*.json",
40
+ "icon.svg",
41
+ "README.md"
42
+ ],
43
+ "dsh": {
44
+ "bundle": {
45
+ "patch": "./cordis.patch.yml"
46
+ }
47
+ },
48
+ "peerDependencies": {
49
+ "@deepseek-ai/cordis": "~4.1.0",
50
+ "@deepseek-ai/dsh-attachment": "^0.2.2-rc.1",
51
+ "@deepseek-ai/dsh-llm": "^0.2.2-rc.1",
52
+ "@deepseek-ai/dsh-system-prompt": "^0.2.2-rc.1",
53
+ "@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
54
+ "@deepseek-ai/schemastery": "~3.19.0"
55
+ },
56
+ "devDependencies": {
57
+ "@deepseek-ai/cordis": "~4.1.0",
58
+ "@deepseek-ai/dsh-attachment": "^0.2.2-rc.1",
59
+ "@deepseek-ai/dsh-llm": "^0.2.2-rc.1",
60
+ "@deepseek-ai/dsh-system-prompt": "^0.2.2-rc.1",
61
+ "@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
62
+ "@deepseek-ai/schemastery": "~3.19.0",
63
+ "@types/node": "^24.0.0",
64
+ "typescript": "^5.9.0"
65
+ },
66
+ "scripts": {
67
+ "build": "tsc -p tsconfig.build.json",
68
+ "typecheck": "tsc -p tsconfig.test.json --noEmit",
69
+ "pretest": "tsc -p tsconfig.test.json",
70
+ "test": "node --test \"dist-test/**/*.test.js\"",
71
+ "check": "node --check dist/index.js && node --check dist/tool.js && node --check dist/host.js && node --check dist/tab.js && node --check dist/cdp.js && node --check dist/ws.js && node --check dist/launch.js && node --check dist/idle.js",
72
+ "clean": "node -e \"const fs=require('node:fs');for(const d of ['dist','dist-test'])fs.rmSync(d,{recursive:true,force:true})\""
73
+ }
6
74
  }