@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/LICENSE +24 -0
- package/README.md +69 -2
- package/cordis.patch.yml +11 -0
- package/dist/cdp.d.ts +84 -0
- package/dist/cdp.js +222 -0
- package/dist/console.d.ts +49 -0
- package/dist/console.js +72 -0
- package/dist/host.d.ts +71 -0
- package/dist/host.js +222 -0
- package/dist/idle.d.ts +22 -0
- package/dist/idle.js +47 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +112 -0
- package/dist/keys.d.ts +43 -0
- package/dist/keys.js +124 -0
- package/dist/launch.d.ts +108 -0
- package/dist/launch.js +260 -0
- package/dist/snapshot.d.ts +66 -0
- package/dist/snapshot.js +220 -0
- package/dist/tab.d.ts +183 -0
- package/dist/tab.js +580 -0
- package/dist/tool.d.ts +71 -0
- package/dist/tool.js +444 -0
- package/dist/ws.d.ts +66 -0
- package/dist/ws.js +290 -0
- package/icon.svg +15 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 d0nj
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
16
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
17
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
19
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
20
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
21
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
22
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
23
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
24
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @d0nj/dsh-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A real browser for DeepSeek Harness agents, headless by default. One `browser` tool opens a page, reads it as a numbered accessibility outline, and clicks, types, and scrolls by the refs in that outline.
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
open https://github.com/login
|
|
7
|
+
<page tab="main" url="https://github.com/login" title="Sign in to GitHub">
|
|
8
|
+
- main
|
|
9
|
+
- heading "Sign in to GitHub" [level=1]
|
|
10
|
+
- textbox "Username or email address" [ref=e3]
|
|
11
|
+
- textbox "Password" [ref=e4]
|
|
12
|
+
- link "Forgot password?" [ref=e5] [url=https://github.com/password_reset]
|
|
13
|
+
- button "Sign in" [ref=e9]
|
|
14
|
+
</page>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`type` into `e4`, `click` `e9`, and the next result is the page that came back.
|
|
18
|
+
|
|
19
|
+
## Why it exists
|
|
20
|
+
|
|
21
|
+
`web_fetch` returns HTML for one URL; it cannot log in, submit a form, wait for a client-rendered page, or read what a screen reader would announce. `browser` drives a real Chromium-family browser over the DevTools protocol with no third-party runtime dependency: the WebSocket client, frame codec, launch, and page operations are in this package, so it works on whichever Node the Harness bundles.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
plugin_manager({ action: "install_bundle", target: "<absolute path to this repo>/packages/browser" })
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Nothing else is needed when Chrome, Chromium, Brave, or Edge is installed: the plugin finds it, starts it on first use with a profile of its own (`$DSH_HOME/browser/default`), closes it a minute after the last browser call, and stops it when the plugin unloads. It starts headless, so agent work never puts a window in front of you; set `headless: false` for one session when you need to watch it or sign in to a site, and the persistent profile keeps that session afterwards.
|
|
30
|
+
|
|
31
|
+
## Configuration
|
|
32
|
+
|
|
33
|
+
Set any of these on the `browser` row of the profile's `cordis.patch.yml`; every key has a working default.
|
|
34
|
+
|
|
35
|
+
| Key | Default | What it does |
|
|
36
|
+
|---|---|---|
|
|
37
|
+
| `toolName` | `browser` | Registered tool name. |
|
|
38
|
+
| `executablePath` | auto-detect | Browser binary. Auto-detection covers Program Files, `%LOCALAPPDATA%`, Scoop, Chocolatey, `/Applications`, and Linux package paths; `CHROME_PATH` is honored too. |
|
|
39
|
+
| `cdpUrl` | — | Attach to a browser the user already started with `--remote-debugging-port=9222` instead of launching one. Accepts an HTTP origin or a `ws://` URL. |
|
|
40
|
+
| `headless` | `true` | No window. Set `false` to watch the browser, or to sign in to a site once. A headed tab must stay foregrounded: Chrome does not deliver coordinate clicks to an occluded tab. |
|
|
41
|
+
| `userDataDir` / `profile` | `$DSH_HOME/browser/default` | Profile directory. It persists, so logging in once keeps the session. |
|
|
42
|
+
| `width` / `height` | `1280` / `900` | Window and viewport size. |
|
|
43
|
+
| `dialogs` | `accept` | Answer `alert`/`confirm`/`prompt` automatically, or `dismiss`. |
|
|
44
|
+
| `launchArgs` | `[]` | Extra browser flags, e.g. a proxy. |
|
|
45
|
+
| `navigateTimeoutMs` / `startupTimeoutMs` | `20000` / `20000` | Load and launch budgets. |
|
|
46
|
+
| `maxSnapshotChars` | `12000` | Default outline budget; `maxChars` per call overrides it. |
|
|
47
|
+
| `idleCloseSeconds` | `60` | Close the browser this long after its last command. `0` keeps it until the plugin unloads; never applies to a `cdpUrl` browser the user owns. |
|
|
48
|
+
|
|
49
|
+
## Actions
|
|
50
|
+
|
|
51
|
+
`open`, `snapshot`, `click`, `type`, `press`, `hover`, `select`, `scroll`, `wait`, `evaluate`, `screenshot`, `console`, `tabs`, `close`, `back`, `forward`, `reload`.
|
|
52
|
+
|
|
53
|
+
Every page action returns a fresh snapshot unless `snapshot` is `false`, so a click and its result cost one call. Refs are numbered per snapshot and cleared on navigation; a stale ref fails with the message to take a new one. `snapshot` can be scoped with `selector` to keep large pages small. `screenshot` attaches the PNG when the model accepts images and writes it to a temp file otherwise.
|
|
54
|
+
|
|
55
|
+
## Security
|
|
56
|
+
|
|
57
|
+
- Page text, console output, and `evaluate` results are untrusted data. The plugin adds a system-prompt section that says so, and every page body is wrapped in `<page …>` so the model can tell it from tool prose.
|
|
58
|
+
- The browser profile holds whatever the user logged into while using this window; it is not the everyday browser profile, and the everyday browser keeps running untouched.
|
|
59
|
+
- `evaluate` runs in the page's own world with the page's own permissions, so it cannot reach files or the host process.
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
pnpm --filter @d0nj/dsh-browser build # tsc → dist/
|
|
65
|
+
pnpm --filter @d0nj/dsh-browser test # compile dist-test/, then node --test
|
|
66
|
+
pnpm --filter @d0nj/dsh-browser typecheck
|
|
67
|
+
pnpm --filter @d0nj/dsh-browser check # node --check on emitted entries
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Tests are offline: the transport suites run against an in-process WebSocket server, and the page-outline suites run on recorded accessibility trees. Driving a real browser is a manual check, not a test.
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Registers the `browser` tool: one Chromium-family browser the agent drives
|
|
2
|
+
# over the DevTools protocol, plus the prompt section that marks page text as data.
|
|
3
|
+
#
|
|
4
|
+
# The row is inserted at the composition root rather than into an agent preset,
|
|
5
|
+
# so every preset sees the tool. The browser starts headless, on the first
|
|
6
|
+
# browser call, with its own persistent profile directory, and stays alive until
|
|
7
|
+
# the plugin is disposed; set `headless: false` to watch it or sign in, or
|
|
8
|
+
# `cdpUrl` in the profile's own patch to attach to a browser already running.
|
|
9
|
+
- insert:
|
|
10
|
+
- id: browser
|
|
11
|
+
name: '@d0nj/dsh-browser'
|
package/dist/cdp.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** Parameters of one CDP command. */
|
|
2
|
+
export type CdpParams = Record<string, unknown>;
|
|
3
|
+
/** A failure reported by Chrome or by the transport. */
|
|
4
|
+
export declare class CdpError extends Error {
|
|
5
|
+
readonly method: string | undefined;
|
|
6
|
+
readonly code: number | undefined;
|
|
7
|
+
constructor(message: string, options?: {
|
|
8
|
+
method?: string;
|
|
9
|
+
code?: number;
|
|
10
|
+
cause?: unknown;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/** One event emitted by Chrome. */
|
|
14
|
+
export interface CdpEvent {
|
|
15
|
+
readonly method: string;
|
|
16
|
+
readonly params: CdpParams;
|
|
17
|
+
readonly sessionId: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
type EventListener = (event: CdpEvent) => void;
|
|
20
|
+
/**
|
|
21
|
+
* A connection to one browser's DevTools endpoint.
|
|
22
|
+
*
|
|
23
|
+
* Commands carry an optional `sessionId`: omitting it addresses the browser
|
|
24
|
+
* itself (`Target.*`, `Browser.*`), passing it addresses one attached tab.
|
|
25
|
+
*/
|
|
26
|
+
export declare class CdpClient {
|
|
27
|
+
#private;
|
|
28
|
+
private constructor();
|
|
29
|
+
/**
|
|
30
|
+
* Open a connection to a DevTools WebSocket.
|
|
31
|
+
* @param url - the browser-level WebSocket URL.
|
|
32
|
+
* @param options - handshake budget.
|
|
33
|
+
* @returns the connected client.
|
|
34
|
+
*/
|
|
35
|
+
static connect(url: string, options?: {
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
}): Promise<CdpClient>;
|
|
39
|
+
get closed(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Observe every command this client sends.
|
|
42
|
+
*
|
|
43
|
+
* Commands, not events: page chatter a tab produces on its own must not read as
|
|
44
|
+
* someone still using the browser.
|
|
45
|
+
* @param listener - called before each command is written.
|
|
46
|
+
* @returns the unsubscribe function.
|
|
47
|
+
*/
|
|
48
|
+
onActivity(listener: () => void): () => void;
|
|
49
|
+
/**
|
|
50
|
+
* Send one command.
|
|
51
|
+
* @param method - CDP method name.
|
|
52
|
+
* @param params - command parameters.
|
|
53
|
+
* @param sessionId - target session, when the command addresses a tab.
|
|
54
|
+
* @param timeoutMs - per-command budget.
|
|
55
|
+
* @returns the command result.
|
|
56
|
+
*/
|
|
57
|
+
send<T = CdpParams>(method: string, params?: CdpParams, sessionId?: string, timeoutMs?: number): Promise<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Subscribe to one event.
|
|
60
|
+
* @param method - CDP event name.
|
|
61
|
+
* @param listener - called for every matching event.
|
|
62
|
+
* @param sessionId - restrict to one tab session.
|
|
63
|
+
* @returns the unsubscribe function.
|
|
64
|
+
*/
|
|
65
|
+
on(method: string, listener: EventListener, sessionId?: string): () => void;
|
|
66
|
+
/**
|
|
67
|
+
* Resolve with the next matching event.
|
|
68
|
+
* @param method - CDP event name.
|
|
69
|
+
* @param options - session filter, predicate, and budget.
|
|
70
|
+
* @returns the matching event.
|
|
71
|
+
*/
|
|
72
|
+
waitFor(method: string, options?: {
|
|
73
|
+
sessionId?: string;
|
|
74
|
+
timeoutMs?: number;
|
|
75
|
+
predicate?: (params: CdpParams) => boolean;
|
|
76
|
+
signal?: AbortSignal;
|
|
77
|
+
}): Promise<CdpEvent>;
|
|
78
|
+
/** Close the connection and fail outstanding commands. */
|
|
79
|
+
close(): void;
|
|
80
|
+
private failAll;
|
|
81
|
+
dispatch(text: string): void;
|
|
82
|
+
}
|
|
83
|
+
export {};
|
|
84
|
+
//# sourceMappingURL=cdp.d.ts.map
|
package/dist/cdp.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chrome DevTools Protocol client: request/response correlation and events over
|
|
3
|
+
* one flattened WebSocket.
|
|
4
|
+
*
|
|
5
|
+
* @module @d0nj/dsh-browser/cdp
|
|
6
|
+
*/
|
|
7
|
+
import { openWebSocket } from './ws.js';
|
|
8
|
+
/** A failure reported by Chrome or by the transport. */
|
|
9
|
+
export class CdpError extends Error {
|
|
10
|
+
method;
|
|
11
|
+
code;
|
|
12
|
+
constructor(message, options = {}) {
|
|
13
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
14
|
+
this.name = 'CdpError';
|
|
15
|
+
this.method = options.method;
|
|
16
|
+
this.code = options.code;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Default per-command budget; page commands that need longer pass their own. */
|
|
20
|
+
const DEFAULT_COMMAND_TIMEOUT_MS = 30_000;
|
|
21
|
+
/**
|
|
22
|
+
* A connection to one browser's DevTools endpoint.
|
|
23
|
+
*
|
|
24
|
+
* Commands carry an optional `sessionId`: omitting it addresses the browser
|
|
25
|
+
* itself (`Target.*`, `Browser.*`), passing it addresses one attached tab.
|
|
26
|
+
*/
|
|
27
|
+
export class CdpClient {
|
|
28
|
+
#connection;
|
|
29
|
+
#nextId = 1;
|
|
30
|
+
#pending = new Map();
|
|
31
|
+
#listeners = new Set();
|
|
32
|
+
#activity = new Set();
|
|
33
|
+
#closedReason;
|
|
34
|
+
constructor(connection) {
|
|
35
|
+
this.#connection = connection;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Open a connection to a DevTools WebSocket.
|
|
39
|
+
* @param url - the browser-level WebSocket URL.
|
|
40
|
+
* @param options - handshake budget.
|
|
41
|
+
* @returns the connected client.
|
|
42
|
+
*/
|
|
43
|
+
static async connect(url, options = {}) {
|
|
44
|
+
let client;
|
|
45
|
+
const socket = await openWebSocket(url, options.timeoutMs ?? 10_000, {
|
|
46
|
+
message: (text) => client?.dispatch(text),
|
|
47
|
+
close: (code, reason) => client?.failAll(`DevTools connection closed (${reason === '' ? String(code) : reason})`),
|
|
48
|
+
}, options.signal);
|
|
49
|
+
client = new CdpClient(socket);
|
|
50
|
+
return client;
|
|
51
|
+
}
|
|
52
|
+
get closed() {
|
|
53
|
+
return this.#closedReason !== undefined;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Observe every command this client sends.
|
|
57
|
+
*
|
|
58
|
+
* Commands, not events: page chatter a tab produces on its own must not read as
|
|
59
|
+
* someone still using the browser.
|
|
60
|
+
* @param listener - called before each command is written.
|
|
61
|
+
* @returns the unsubscribe function.
|
|
62
|
+
*/
|
|
63
|
+
onActivity(listener) {
|
|
64
|
+
this.#activity.add(listener);
|
|
65
|
+
return () => {
|
|
66
|
+
this.#activity.delete(listener);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Send one command.
|
|
71
|
+
* @param method - CDP method name.
|
|
72
|
+
* @param params - command parameters.
|
|
73
|
+
* @param sessionId - target session, when the command addresses a tab.
|
|
74
|
+
* @param timeoutMs - per-command budget.
|
|
75
|
+
* @returns the command result.
|
|
76
|
+
*/
|
|
77
|
+
async send(method, params = {}, sessionId, timeoutMs = DEFAULT_COMMAND_TIMEOUT_MS) {
|
|
78
|
+
if (this.#closedReason !== undefined)
|
|
79
|
+
throw new CdpError(`${method} failed: ${this.#closedReason}`, { method });
|
|
80
|
+
for (const listener of this.#activity) {
|
|
81
|
+
try {
|
|
82
|
+
listener();
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Activity listeners must not break dispatch.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const id = this.#nextId++;
|
|
89
|
+
const envelope = { id, method, params };
|
|
90
|
+
if (sessionId !== undefined)
|
|
91
|
+
envelope.sessionId = sessionId;
|
|
92
|
+
const promise = new Promise((resolve, reject) => {
|
|
93
|
+
const timer = setTimeout(() => {
|
|
94
|
+
this.#pending.delete(id);
|
|
95
|
+
reject(new CdpError(`${method} timed out after ${timeoutMs}ms`, { method }));
|
|
96
|
+
}, timeoutMs);
|
|
97
|
+
this.#pending.set(id, {
|
|
98
|
+
method,
|
|
99
|
+
resolve: (result) => resolve(result),
|
|
100
|
+
reject,
|
|
101
|
+
timer,
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
this.#connection.send(JSON.stringify(envelope));
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
this.#pending.delete(id);
|
|
109
|
+
reject(error instanceof Error ? error : new CdpError(String(error), { method }));
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return promise;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Subscribe to one event.
|
|
116
|
+
* @param method - CDP event name.
|
|
117
|
+
* @param listener - called for every matching event.
|
|
118
|
+
* @param sessionId - restrict to one tab session.
|
|
119
|
+
* @returns the unsubscribe function.
|
|
120
|
+
*/
|
|
121
|
+
on(method, listener, sessionId) {
|
|
122
|
+
const entry = { method, sessionId, listener };
|
|
123
|
+
this.#listeners.add(entry);
|
|
124
|
+
return () => {
|
|
125
|
+
this.#listeners.delete(entry);
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Resolve with the next matching event.
|
|
130
|
+
* @param method - CDP event name.
|
|
131
|
+
* @param options - session filter, predicate, and budget.
|
|
132
|
+
* @returns the matching event.
|
|
133
|
+
*/
|
|
134
|
+
async waitFor(method, options = {}) {
|
|
135
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_COMMAND_TIMEOUT_MS;
|
|
136
|
+
const signal = options.signal;
|
|
137
|
+
return new Promise((resolve, reject) => {
|
|
138
|
+
const settle = () => {
|
|
139
|
+
clearTimeout(timer);
|
|
140
|
+
unsubscribe();
|
|
141
|
+
signal?.removeEventListener('abort', onAbort);
|
|
142
|
+
};
|
|
143
|
+
const onAbort = () => {
|
|
144
|
+
settle();
|
|
145
|
+
reject(new CdpError(`${method} wait aborted`, { method }));
|
|
146
|
+
};
|
|
147
|
+
const timer = setTimeout(() => {
|
|
148
|
+
settle();
|
|
149
|
+
reject(new CdpError(`${method} was not emitted within ${timeoutMs}ms`, { method }));
|
|
150
|
+
}, timeoutMs);
|
|
151
|
+
const unsubscribe = this.on(method, (event) => {
|
|
152
|
+
if (options.predicate !== undefined && !options.predicate(event.params))
|
|
153
|
+
return;
|
|
154
|
+
settle();
|
|
155
|
+
resolve(event);
|
|
156
|
+
}, options.sessionId);
|
|
157
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** Close the connection and fail outstanding commands. */
|
|
161
|
+
close() {
|
|
162
|
+
this.failAll('DevTools connection closed by the client');
|
|
163
|
+
this.#connection.close();
|
|
164
|
+
}
|
|
165
|
+
failAll(reason) {
|
|
166
|
+
if (this.#closedReason === undefined)
|
|
167
|
+
this.#closedReason = reason;
|
|
168
|
+
for (const [id, pending] of this.#pending) {
|
|
169
|
+
clearTimeout(pending.timer);
|
|
170
|
+
this.#pending.delete(id);
|
|
171
|
+
pending.reject(new CdpError(`${pending.method} failed: ${reason}`, { method: pending.method }));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
dispatch(text) {
|
|
175
|
+
let message;
|
|
176
|
+
try {
|
|
177
|
+
message = JSON.parse(text);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const id = message.id;
|
|
183
|
+
if (typeof id === 'number') {
|
|
184
|
+
const pending = this.#pending.get(id);
|
|
185
|
+
if (pending === undefined)
|
|
186
|
+
return;
|
|
187
|
+
this.#pending.delete(id);
|
|
188
|
+
clearTimeout(pending.timer);
|
|
189
|
+
const error = message.error;
|
|
190
|
+
if (error !== undefined) {
|
|
191
|
+
pending.reject(new CdpError(`${pending.method}: ${error.message ?? 'unknown error'}`, {
|
|
192
|
+
method: pending.method,
|
|
193
|
+
...(error.code === undefined ? {} : { code: error.code }),
|
|
194
|
+
}));
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
pending.resolve(message.result ?? {});
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const method = message.method;
|
|
201
|
+
if (typeof method !== 'string')
|
|
202
|
+
return;
|
|
203
|
+
const event = {
|
|
204
|
+
method,
|
|
205
|
+
params: message.params ?? {},
|
|
206
|
+
sessionId: typeof message.sessionId === 'string' ? message.sessionId : undefined,
|
|
207
|
+
};
|
|
208
|
+
for (const entry of this.#listeners) {
|
|
209
|
+
if (entry.method !== method)
|
|
210
|
+
continue;
|
|
211
|
+
if (entry.sessionId !== undefined && entry.sessionId !== event.sessionId)
|
|
212
|
+
continue;
|
|
213
|
+
try {
|
|
214
|
+
entry.listener(event);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// A listener must not break event delivery for the others.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=cdp.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-tab console and page-error capture.
|
|
3
|
+
*
|
|
4
|
+
* A failing page is the most common reason an agent's next action misbehaves,
|
|
5
|
+
* and the console is where that failure is stated plainly. The buffer is
|
|
6
|
+
* bounded so a chatty page can never grow the plugin's memory.
|
|
7
|
+
*
|
|
8
|
+
* @module @d0nj/dsh-browser/console
|
|
9
|
+
*/
|
|
10
|
+
/** One captured console line. */
|
|
11
|
+
export interface ConsoleEntry {
|
|
12
|
+
readonly level: string;
|
|
13
|
+
readonly text: string;
|
|
14
|
+
readonly time: number;
|
|
15
|
+
}
|
|
16
|
+
/** A CDP `Runtime.RemoteObject`, reduced to the fields that matter here. */
|
|
17
|
+
export interface RemoteObjectLike {
|
|
18
|
+
readonly type?: string;
|
|
19
|
+
readonly subtype?: string;
|
|
20
|
+
readonly className?: string;
|
|
21
|
+
readonly value?: unknown;
|
|
22
|
+
readonly unserializableValue?: string;
|
|
23
|
+
readonly description?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Render a remote object the way a console shows it.
|
|
27
|
+
* @param object - the CDP remote object.
|
|
28
|
+
* @returns one readable line.
|
|
29
|
+
*/
|
|
30
|
+
export declare function describeRemoteObject(object: RemoteObjectLike | undefined): string;
|
|
31
|
+
/** Bounded console ring. */
|
|
32
|
+
export declare class ConsoleBuffer {
|
|
33
|
+
#private;
|
|
34
|
+
constructor(limit?: number);
|
|
35
|
+
/**
|
|
36
|
+
* Append one line, dropping the oldest when full.
|
|
37
|
+
* @param level - `log`, `error`, `warning`, `dialog`, `pageerror`, …
|
|
38
|
+
* @param text - the rendered line.
|
|
39
|
+
* @param time - capture timestamp.
|
|
40
|
+
*/
|
|
41
|
+
push(level: string, text: string, time?: number): void;
|
|
42
|
+
/** Every captured line, oldest first. */
|
|
43
|
+
entries(): readonly ConsoleEntry[];
|
|
44
|
+
/** Captured errors and uncaught exceptions. */
|
|
45
|
+
errors(): readonly ConsoleEntry[];
|
|
46
|
+
/** Drop what was captured so far. */
|
|
47
|
+
clear(): void;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=console.d.ts.map
|
package/dist/console.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const MAX_LINE_CHARS = 600;
|
|
2
|
+
/**
|
|
3
|
+
* Render a remote object the way a console shows it.
|
|
4
|
+
* @param object - the CDP remote object.
|
|
5
|
+
* @returns one readable line.
|
|
6
|
+
*/
|
|
7
|
+
export function describeRemoteObject(object) {
|
|
8
|
+
if (object === undefined)
|
|
9
|
+
return '';
|
|
10
|
+
let text;
|
|
11
|
+
if (object.unserializableValue !== undefined) {
|
|
12
|
+
text = object.unserializableValue;
|
|
13
|
+
}
|
|
14
|
+
else if (object.value !== undefined) {
|
|
15
|
+
if (typeof object.value === 'string')
|
|
16
|
+
text = object.value;
|
|
17
|
+
else {
|
|
18
|
+
try {
|
|
19
|
+
text = JSON.stringify(object.value) ?? String(object.value);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
text = String(object.value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (object.description !== undefined) {
|
|
27
|
+
text = object.description;
|
|
28
|
+
}
|
|
29
|
+
else if (object.className !== undefined) {
|
|
30
|
+
text = `[${object.className}]`;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
text = object.type === undefined ? '' : `[${object.type}]`;
|
|
34
|
+
}
|
|
35
|
+
const collapsed = text.replace(/\s+/gu, ' ').trim();
|
|
36
|
+
return collapsed.length > MAX_LINE_CHARS ? `${collapsed.slice(0, MAX_LINE_CHARS - 1)}…` : collapsed;
|
|
37
|
+
}
|
|
38
|
+
/** Bounded console ring. */
|
|
39
|
+
export class ConsoleBuffer {
|
|
40
|
+
#limit;
|
|
41
|
+
#entries = [];
|
|
42
|
+
constructor(limit = 200) {
|
|
43
|
+
this.#limit = limit;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Append one line, dropping the oldest when full.
|
|
47
|
+
* @param level - `log`, `error`, `warning`, `dialog`, `pageerror`, …
|
|
48
|
+
* @param text - the rendered line.
|
|
49
|
+
* @param time - capture timestamp.
|
|
50
|
+
*/
|
|
51
|
+
push(level, text, time = Date.now()) {
|
|
52
|
+
const trimmed = text.trim();
|
|
53
|
+
if (trimmed === '')
|
|
54
|
+
return;
|
|
55
|
+
this.#entries.push({ level, text: trimmed, time });
|
|
56
|
+
if (this.#entries.length > this.#limit)
|
|
57
|
+
this.#entries.splice(0, this.#entries.length - this.#limit);
|
|
58
|
+
}
|
|
59
|
+
/** Every captured line, oldest first. */
|
|
60
|
+
entries() {
|
|
61
|
+
return this.#entries;
|
|
62
|
+
}
|
|
63
|
+
/** Captured errors and uncaught exceptions. */
|
|
64
|
+
errors() {
|
|
65
|
+
return this.#entries.filter((entry) => entry.level === 'error' || entry.level === 'pageerror');
|
|
66
|
+
}
|
|
67
|
+
/** Drop what was captured so far. */
|
|
68
|
+
clear() {
|
|
69
|
+
this.#entries = [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=console.js.map
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Tab } from './tab.js';
|
|
2
|
+
import type { PageInfo, TabSettings } from './tab.js';
|
|
3
|
+
/** Resolved, validated configuration. */
|
|
4
|
+
export interface BrowserSettings {
|
|
5
|
+
readonly toolName: string;
|
|
6
|
+
readonly executablePath: string | undefined;
|
|
7
|
+
readonly cdpUrl: string | undefined;
|
|
8
|
+
readonly userDataDir: string;
|
|
9
|
+
readonly headless: boolean;
|
|
10
|
+
readonly viewport: {
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly height: number;
|
|
13
|
+
};
|
|
14
|
+
readonly dialogs: 'accept' | 'dismiss';
|
|
15
|
+
readonly launchArgs: readonly string[];
|
|
16
|
+
readonly navigateTimeoutMs: number;
|
|
17
|
+
readonly startupTimeoutMs: number;
|
|
18
|
+
readonly maxSnapshotChars: number;
|
|
19
|
+
readonly idleCloseMs: number;
|
|
20
|
+
}
|
|
21
|
+
/** Ambient dependencies, injected so the host can be tested without spawning. */
|
|
22
|
+
export interface BrowserHostDeps {
|
|
23
|
+
readonly logger: {
|
|
24
|
+
warn(message: string): void;
|
|
25
|
+
debug?(message: string): void;
|
|
26
|
+
};
|
|
27
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
28
|
+
readonly home: string;
|
|
29
|
+
readonly exists: (path: string) => Promise<boolean>;
|
|
30
|
+
}
|
|
31
|
+
/** A tab as reported to the model. */
|
|
32
|
+
export interface TabListing extends PageInfo {
|
|
33
|
+
readonly name: string;
|
|
34
|
+
}
|
|
35
|
+
export declare class BrowserHost {
|
|
36
|
+
#private;
|
|
37
|
+
constructor(settings: BrowserSettings, deps: BrowserHostDeps);
|
|
38
|
+
/** The tab settings every new tab receives. */
|
|
39
|
+
get tabSettings(): TabSettings;
|
|
40
|
+
/**
|
|
41
|
+
* The named tab, created on first use.
|
|
42
|
+
* @param name - model-facing tab name.
|
|
43
|
+
* @returns the live tab.
|
|
44
|
+
*/
|
|
45
|
+
tab(name: string): Promise<Tab>;
|
|
46
|
+
/**
|
|
47
|
+
* Every open tab with its current page.
|
|
48
|
+
* @returns the tab listing.
|
|
49
|
+
*/
|
|
50
|
+
listTabs(): Promise<TabListing[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Close one tab, or all of them.
|
|
53
|
+
* @param name - tab name to close; ignored when `all` is true.
|
|
54
|
+
* @param all - close every tab this plugin opened.
|
|
55
|
+
* @returns the names that were closed.
|
|
56
|
+
*/
|
|
57
|
+
closeTab(name: string | undefined, all: boolean): Promise<string[]>;
|
|
58
|
+
/** Close a browser nothing has used for the configured idle period. */
|
|
59
|
+
closeIdleBrowser(): Promise<void>;
|
|
60
|
+
/** Stop every tab and the browser this plugin manages. */
|
|
61
|
+
dispose(): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Absolute profile directory for one configured profile name.
|
|
65
|
+
* @param userDataDir - configured root, when the operator set one.
|
|
66
|
+
* @param dshHome - harness home.
|
|
67
|
+
* @param profile - profile name.
|
|
68
|
+
* @returns the directory Chrome stores its profile in.
|
|
69
|
+
*/
|
|
70
|
+
export declare function resolveUserDataDir(userDataDir: string | undefined, dshHome: string, profile: string): string;
|
|
71
|
+
//# sourceMappingURL=host.d.ts.map
|