bidi-browser-mcp 0.1.9
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 +21 -0
- package/README.md +343 -0
- package/dist/bin.d.ts +6 -0
- package/dist/bin.js +37 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.js +122 -0
- package/dist/connection/bidi-client.d.ts +29 -0
- package/dist/connection/bidi-client.js +298 -0
- package/dist/connection/browser-discovery.d.ts +30 -0
- package/dist/connection/browser-discovery.js +393 -0
- package/dist/connection/chromium-mapper.d.ts +55 -0
- package/dist/connection/chromium-mapper.js +293 -0
- package/dist/connection/event-pipeline.d.ts +37 -0
- package/dist/connection/event-pipeline.js +167 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +66 -0
- package/dist/profile/profile-store.d.ts +88 -0
- package/dist/profile/profile-store.js +350 -0
- package/dist/session/context-manager.d.ts +17 -0
- package/dist/session/context-manager.js +179 -0
- package/dist/session/session-manager.d.ts +31 -0
- package/dist/session/session-manager.js +366 -0
- package/dist/snapshot/cache.d.ts +15 -0
- package/dist/snapshot/cache.js +37 -0
- package/dist/snapshot/dom-snapshot.d.ts +5 -0
- package/dist/snapshot/dom-snapshot.js +270 -0
- package/dist/snapshot/element-resolver.d.ts +13 -0
- package/dist/snapshot/element-resolver.js +25 -0
- package/dist/snapshot/token-optimizer.d.ts +16 -0
- package/dist/snapshot/token-optimizer.js +103 -0
- package/dist/tools/base-tool.d.ts +34 -0
- package/dist/tools/base-tool.js +55 -0
- package/dist/tools/browser.d.ts +1 -0
- package/dist/tools/browser.js +75 -0
- package/dist/tools/cleanup.d.ts +1 -0
- package/dist/tools/cleanup.js +64 -0
- package/dist/tools/console.d.ts +1 -0
- package/dist/tools/console.js +87 -0
- package/dist/tools/context.d.ts +1 -0
- package/dist/tools/context.js +156 -0
- package/dist/tools/observe.d.ts +1 -0
- package/dist/tools/observe.js +92 -0
- package/dist/tools/page.d.ts +1 -0
- package/dist/tools/page.js +139 -0
- package/dist/tools/ping.d.ts +1 -0
- package/dist/tools/ping.js +71 -0
- package/dist/tools/registry.d.ts +13 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/spaces.d.ts +1 -0
- package/dist/tools/spaces.js +98 -0
- package/dist/tools/tabs.d.ts +1 -0
- package/dist/tools/tabs.js +155 -0
- package/dist/types.d.ts +277 -0
- package/dist/types.js +5 -0
- package/dist/utils/assert.d.ts +12 -0
- package/dist/utils/assert.js +74 -0
- package/dist/utils/errors.d.ts +24 -0
- package/dist/utils/errors.js +60 -0
- package/dist/utils/result.d.ts +14 -0
- package/dist/utils/result.js +37 -0
- package/package.json +86 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { assertFirefoxFamily, defineTool, requireAction } from './base-tool.js';
|
|
2
|
+
import { ActionableError } from '../utils/errors.js';
|
|
3
|
+
import { ok } from '../utils/result.js';
|
|
4
|
+
import { joinContainerNames, readContainers, readSpaces } from '../profile/profile-store.js';
|
|
5
|
+
const ACTIONS = ['list'];
|
|
6
|
+
const INPUT_SCHEMA = {
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties: {
|
|
9
|
+
action: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
enum: [...ACTIONS],
|
|
12
|
+
description: 'list: Zen spaces with their bound containers',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
required: ['action'],
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
};
|
|
18
|
+
const OUTPUT_SCHEMA = {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
ok: { type: 'boolean' },
|
|
22
|
+
action: { type: 'string' },
|
|
23
|
+
data: { type: ['object', 'null'] },
|
|
24
|
+
error: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
code: { type: 'string' },
|
|
28
|
+
message: { type: 'string' },
|
|
29
|
+
fix: { type: 'string' },
|
|
30
|
+
details: { type: 'object' },
|
|
31
|
+
},
|
|
32
|
+
required: ['code', 'message'],
|
|
33
|
+
},
|
|
34
|
+
warnings: { type: 'array', items: { type: 'string' } },
|
|
35
|
+
},
|
|
36
|
+
required: ['ok', 'action'],
|
|
37
|
+
additionalProperties: false,
|
|
38
|
+
};
|
|
39
|
+
export const spacesTool = defineTool({
|
|
40
|
+
name: 'spaces',
|
|
41
|
+
title: 'Zen spaces',
|
|
42
|
+
description: "List Zen spaces (workspaces) from the profile session store, including each space's bound container. Read-only; space data is read from disk, never written. The store can contain open-tab URLs and form state — only derived metadata (uuid, name, theme, container) is returned.",
|
|
43
|
+
toolset: 'core',
|
|
44
|
+
actions: ACTIONS,
|
|
45
|
+
inputSchema: INPUT_SCHEMA,
|
|
46
|
+
outputSchema: OUTPUT_SCHEMA,
|
|
47
|
+
annotations: {
|
|
48
|
+
readOnlyHint: true,
|
|
49
|
+
destructiveHint: false,
|
|
50
|
+
idempotentHint: true,
|
|
51
|
+
openWorldHint: false,
|
|
52
|
+
},
|
|
53
|
+
handler: async (args, ctx) => {
|
|
54
|
+
const action = requireAction(args, 'spaces', ACTIONS);
|
|
55
|
+
const session = ctx.session;
|
|
56
|
+
const runtime = session?.runtime;
|
|
57
|
+
if (session === undefined || runtime === undefined) {
|
|
58
|
+
throw new ActionableError('CONNECTION_FAILED', 'No live browser session.', {
|
|
59
|
+
fix: 'Call browser.connect first.',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const status = await session.status();
|
|
63
|
+
assertFirefoxFamily(status.browser?.browserName, 'Zen spaces');
|
|
64
|
+
const profilePath = status.browser?.['moz:profile'];
|
|
65
|
+
if (typeof profilePath !== 'string' || profilePath === '') {
|
|
66
|
+
throw new ActionableError('UNSUPPORTED', 'The browser did not report its profile path; Zen spaces cannot be read.', { fix: 'Use a Firefox-family browser launched with --remote-debugging-port.' });
|
|
67
|
+
}
|
|
68
|
+
const { spaces: spaceList, warnings } = readSpaces(profilePath);
|
|
69
|
+
const containers = readContainers(profilePath);
|
|
70
|
+
const ids = (await session.request('browser.getUserContexts', {})).userContexts
|
|
71
|
+
?.map((entry) => entry.userContext)
|
|
72
|
+
.filter((id) => typeof id === 'string') ?? [];
|
|
73
|
+
const { names, aligned } = joinContainerNames(ids, containers);
|
|
74
|
+
const spaces = spaceList.map((space) => {
|
|
75
|
+
const identity = containers.find((candidate) => candidate.userContextId === space.containerId);
|
|
76
|
+
const bidi = [...names.entries()].find(([, joined]) => joined?.userContextId === space.containerId)?.[0];
|
|
77
|
+
return {
|
|
78
|
+
uuid: space.uuid,
|
|
79
|
+
name: space.name,
|
|
80
|
+
...(space.themeType === undefined ? {} : { themeType: space.themeType }),
|
|
81
|
+
...(space.containerId === undefined
|
|
82
|
+
? {}
|
|
83
|
+
: {
|
|
84
|
+
containerId: space.containerId,
|
|
85
|
+
...(identity?.name === undefined ? {} : { containerName: identity.name }),
|
|
86
|
+
...(bidi === undefined ? {} : { containerUserContext: bidi }),
|
|
87
|
+
}),
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
return ok(action, { spaces }, aligned
|
|
91
|
+
? warnings
|
|
92
|
+
: [
|
|
93
|
+
...warnings,
|
|
94
|
+
'Container names were omitted: the number of profile containers did not match the number of live user contexts.',
|
|
95
|
+
]);
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
//# sourceMappingURL=spaces.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const tabsTool: import("../types.js").ToolDefinition;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { COMMON_PARAMS, defineTool, requireAction } from './base-tool.js';
|
|
2
|
+
import { ActionableError, toContextError } from '../utils/errors.js';
|
|
3
|
+
import { assertNavigableUrl } from '../utils/assert.js';
|
|
4
|
+
import { ok } from '../utils/result.js';
|
|
5
|
+
const ACTIONS = ['list', 'new', 'activate', 'close', 'info', 'select'];
|
|
6
|
+
const INPUT_SCHEMA = {
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties: {
|
|
9
|
+
action: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
enum: [...ACTIONS],
|
|
12
|
+
description: 'list: open tabs; new: open a tab; activate: focus a tab; close: close a tab; info: one tab; select: choose the default tab',
|
|
13
|
+
},
|
|
14
|
+
context: COMMON_PARAMS.context,
|
|
15
|
+
url: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: 'URL for tabs.new. Only http:, https:, and about:blank are allowed. Default about:blank.',
|
|
18
|
+
},
|
|
19
|
+
userContext: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: "Container id for tabs.new ('default' or uuid from context.list).",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
required: ['action'],
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
};
|
|
27
|
+
const OUTPUT_SCHEMA = {
|
|
28
|
+
type: 'object',
|
|
29
|
+
properties: {
|
|
30
|
+
ok: { type: 'boolean' },
|
|
31
|
+
action: { type: 'string' },
|
|
32
|
+
data: { type: ['object', 'null'] },
|
|
33
|
+
error: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
code: { type: 'string' },
|
|
37
|
+
message: { type: 'string' },
|
|
38
|
+
fix: { type: 'string' },
|
|
39
|
+
details: { type: 'object' },
|
|
40
|
+
},
|
|
41
|
+
required: ['code', 'message'],
|
|
42
|
+
},
|
|
43
|
+
warnings: { type: 'array', items: { type: 'string' } },
|
|
44
|
+
},
|
|
45
|
+
required: ['ok', 'action'],
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
};
|
|
48
|
+
export const tabsTool = defineTool({
|
|
49
|
+
name: 'tabs',
|
|
50
|
+
title: 'Tabs',
|
|
51
|
+
description: 'List, open, focus, close, and pick tabs. Closing is per-tab; this tool never closes the browser.',
|
|
52
|
+
toolset: 'core',
|
|
53
|
+
actions: ACTIONS,
|
|
54
|
+
inputSchema: INPUT_SCHEMA,
|
|
55
|
+
outputSchema: OUTPUT_SCHEMA,
|
|
56
|
+
annotations: {
|
|
57
|
+
readOnlyHint: false,
|
|
58
|
+
destructiveHint: true,
|
|
59
|
+
idempotentHint: false,
|
|
60
|
+
openWorldHint: false,
|
|
61
|
+
},
|
|
62
|
+
handler: async (args, ctx) => {
|
|
63
|
+
const action = requireAction(args, 'tabs', ACTIONS);
|
|
64
|
+
const session = ctx.session;
|
|
65
|
+
const runtime = session?.runtime;
|
|
66
|
+
if (session === undefined || runtime === undefined) {
|
|
67
|
+
throw new ActionableError('CONNECTION_FAILED', 'No live browser session.', {
|
|
68
|
+
fix: 'Call browser.connect first.',
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const { contexts } = runtime;
|
|
72
|
+
switch (action) {
|
|
73
|
+
case 'list':
|
|
74
|
+
return ok(action, { tabs: await contexts.list() });
|
|
75
|
+
case 'new': {
|
|
76
|
+
const url = args.url === undefined
|
|
77
|
+
? 'about:blank'
|
|
78
|
+
: assertNavigableUrl(args.url, 'tabs.new url');
|
|
79
|
+
const params = { type: 'tab', url };
|
|
80
|
+
if (typeof args.userContext === 'string' && args.userContext !== '') {
|
|
81
|
+
params.userContext = args.userContext;
|
|
82
|
+
}
|
|
83
|
+
const created = (await session.request('browsingContext.create', params));
|
|
84
|
+
if (typeof created.context !== 'string') {
|
|
85
|
+
throw new ActionableError('SESSION_FAILED', 'The browser did not return a new tab.');
|
|
86
|
+
}
|
|
87
|
+
const tab = await contexts.select(created.context);
|
|
88
|
+
return ok(action, {
|
|
89
|
+
tab,
|
|
90
|
+
url: created.url ?? url,
|
|
91
|
+
...(typeof created.userContext === 'string' ? { userContext: created.userContext } : {}),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
case 'activate': {
|
|
95
|
+
const context = typeof args.context === 'string' ? args.context : undefined;
|
|
96
|
+
if (context === undefined) {
|
|
97
|
+
throw new ActionableError('INVALID_ARGUMENT', 'tabs.activate requires context.', {
|
|
98
|
+
fix: 'Pass the tab ID from tabs.list.',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
await session.request('browsingContext.activate', { context });
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
throw toContextError(error);
|
|
106
|
+
}
|
|
107
|
+
return ok(action, { tab: await contexts.select(context) });
|
|
108
|
+
}
|
|
109
|
+
case 'close': {
|
|
110
|
+
const context = typeof args.context === 'string' ? args.context : undefined;
|
|
111
|
+
if (context === undefined) {
|
|
112
|
+
throw new ActionableError('INVALID_ARGUMENT', 'tabs.close requires context.', {
|
|
113
|
+
fix: 'Pass the tab ID from tabs.list.',
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
await session.request('browsingContext.close', { context });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
throw toContextError(error);
|
|
121
|
+
}
|
|
122
|
+
contexts.forget(context);
|
|
123
|
+
return ok(action, { closed: context });
|
|
124
|
+
}
|
|
125
|
+
case 'info': {
|
|
126
|
+
const context = typeof args.context === 'string' ? args.context : undefined;
|
|
127
|
+
if (context === undefined) {
|
|
128
|
+
throw new ActionableError('INVALID_ARGUMENT', 'tabs.info requires context.', {
|
|
129
|
+
fix: 'Pass the tab ID from tabs.list.',
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const tabs = await contexts.list();
|
|
133
|
+
const tab = tabs.find((candidate) => candidate.context === context);
|
|
134
|
+
if (tab === undefined) {
|
|
135
|
+
throw new ActionableError('TAB_NOT_FOUND', `Unknown context: ${context}.`, {
|
|
136
|
+
fix: 'Call tabs.list to see open contexts.',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return ok(action, { tab });
|
|
140
|
+
}
|
|
141
|
+
case 'select': {
|
|
142
|
+
const context = typeof args.context === 'string' ? args.context : undefined;
|
|
143
|
+
if (context === undefined) {
|
|
144
|
+
throw new ActionableError('INVALID_ARGUMENT', 'tabs.select requires context.', {
|
|
145
|
+
fix: 'Pass the tab ID from tabs.list.',
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return ok(action, { tab: await contexts.select(context) });
|
|
149
|
+
}
|
|
150
|
+
default:
|
|
151
|
+
throw new ActionableError('INVALID_ACTION', `Unknown tabs action: ${action}.`);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
//# sourceMappingURL=tabs.js.map
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared contracts. Keep this file free of runtime side effects.
|
|
3
|
+
*/
|
|
4
|
+
export type Toolset = 'core' | 'network' | 'devtools';
|
|
5
|
+
export type OutputMode = 'full' | 'inline' | 'file';
|
|
6
|
+
export interface Config {
|
|
7
|
+
readonly host: string;
|
|
8
|
+
readonly port: number;
|
|
9
|
+
readonly wsUrl?: string;
|
|
10
|
+
readonly profileDir?: string;
|
|
11
|
+
readonly timeoutMs: number;
|
|
12
|
+
readonly toolset: Toolset;
|
|
13
|
+
readonly outputMode: OutputMode;
|
|
14
|
+
readonly maxInlineLen: number;
|
|
15
|
+
readonly tempDir: string;
|
|
16
|
+
readonly appDataRoot: string;
|
|
17
|
+
/** Local app-data root (Chromium data dirs live here on Windows). */
|
|
18
|
+
readonly localAppDataRoot: string;
|
|
19
|
+
readonly allowExtensionInstall: boolean;
|
|
20
|
+
readonly allowBrowserClose: boolean;
|
|
21
|
+
readonly allowRemote: boolean;
|
|
22
|
+
readonly discoveryRoots: readonly string[];
|
|
23
|
+
/** Chromium user-data dirs to look for a `DevToolsActivePort` file in. */
|
|
24
|
+
readonly chromiumDataDirs: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
export interface ServerContext {
|
|
27
|
+
readonly version: string;
|
|
28
|
+
readonly config: Config;
|
|
29
|
+
readonly startedAt: number;
|
|
30
|
+
readonly session?: SessionManagerLike;
|
|
31
|
+
}
|
|
32
|
+
export interface JsonSchema {
|
|
33
|
+
type: 'object';
|
|
34
|
+
properties?: Record<string, unknown>;
|
|
35
|
+
required?: string[];
|
|
36
|
+
additionalProperties?: boolean;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
export interface ToolAnnotations {
|
|
40
|
+
title?: string;
|
|
41
|
+
readOnlyHint?: boolean;
|
|
42
|
+
destructiveHint?: boolean;
|
|
43
|
+
idempotentHint?: boolean;
|
|
44
|
+
openWorldHint?: boolean;
|
|
45
|
+
}
|
|
46
|
+
export type ToolHandler = (args: Record<string, unknown>, ctx: ServerContext) => Promise<ToolResult>;
|
|
47
|
+
export interface ToolDefinition {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly title: string;
|
|
50
|
+
readonly description: string;
|
|
51
|
+
readonly toolset: Toolset;
|
|
52
|
+
readonly actions: readonly string[];
|
|
53
|
+
readonly inputSchema: JsonSchema;
|
|
54
|
+
readonly outputSchema?: JsonSchema;
|
|
55
|
+
readonly annotations?: ToolAnnotations;
|
|
56
|
+
readonly handler: ToolHandler;
|
|
57
|
+
}
|
|
58
|
+
export type ActionableErrorCode = 'INVALID_CONFIG' | 'INVALID_ARGUMENT' | 'INVALID_ACTION' | 'TOOL_NOT_FOUND' | 'DISCOVERY_FAILED' | 'SESSION_BUSY' | 'SESSION_FAILED' | 'CONNECTION_FAILED' | 'CONNECTION_CLOSED' | 'TIMEOUT' | 'CONTEXT_NOT_FOUND' | 'TAB_NOT_FOUND' | 'UID_NOT_FOUND' | 'NAVIGATION_FAILED' | 'SCRIPT_ERROR' | 'UNSUPPORTED' | 'MISSING_DEPENDENCY' | 'NETWORK_ERROR' | 'EXTENSION_ERROR' | 'INTERNAL_ERROR';
|
|
59
|
+
export interface ActionableErrorShape {
|
|
60
|
+
code: ActionableErrorCode;
|
|
61
|
+
message: string;
|
|
62
|
+
fix?: string;
|
|
63
|
+
details?: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
export interface ToolResult {
|
|
66
|
+
ok: boolean;
|
|
67
|
+
action: string;
|
|
68
|
+
data?: unknown;
|
|
69
|
+
error?: ActionableErrorShape;
|
|
70
|
+
warnings?: string[];
|
|
71
|
+
}
|
|
72
|
+
export interface BiDiCommand {
|
|
73
|
+
id: number;
|
|
74
|
+
method: string;
|
|
75
|
+
params?: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
export interface BiDiSuccess {
|
|
78
|
+
type: 'success';
|
|
79
|
+
id: number;
|
|
80
|
+
result: Record<string, unknown>;
|
|
81
|
+
}
|
|
82
|
+
export interface BiDiError {
|
|
83
|
+
type: 'error';
|
|
84
|
+
id: number;
|
|
85
|
+
/** BiDi error code, e.g. "invalid argument" (flat string, per spec). */
|
|
86
|
+
error: string;
|
|
87
|
+
/** Human-readable description. */
|
|
88
|
+
message: string;
|
|
89
|
+
stacktrace?: string;
|
|
90
|
+
}
|
|
91
|
+
export interface BiDiEvent {
|
|
92
|
+
type: 'event';
|
|
93
|
+
method: string;
|
|
94
|
+
params: Record<string, unknown>;
|
|
95
|
+
}
|
|
96
|
+
export type BiDiResponse = BiDiSuccess | BiDiError;
|
|
97
|
+
export type BiDiMessage = BiDiResponse | BiDiEvent;
|
|
98
|
+
export type DiscoverySource = 'env' | 'profile-file' | 'profiles-ini' | 'process-cmdline' | 'port-probe' | 'chromium-devtools-port';
|
|
99
|
+
/**
|
|
100
|
+
* How the endpoint must be spoken to. Firefox-family browsers serve WebDriver
|
|
101
|
+
* BiDi natively; Chromium serves CDP only, so the chromium-bidi mapper is
|
|
102
|
+
* hosted in this process and translates.
|
|
103
|
+
*/
|
|
104
|
+
export type BrowserFamily = 'firefox' | 'chromium';
|
|
105
|
+
export interface DiscoveryCandidate {
|
|
106
|
+
readonly endpoint: string;
|
|
107
|
+
readonly source: DiscoverySource;
|
|
108
|
+
readonly profilePath?: string;
|
|
109
|
+
readonly processId?: number;
|
|
110
|
+
readonly browserFamily?: BrowserFamily;
|
|
111
|
+
}
|
|
112
|
+
export interface BrowserCapabilities {
|
|
113
|
+
readonly browserName?: string;
|
|
114
|
+
readonly browserVersion?: string;
|
|
115
|
+
readonly platformName?: string;
|
|
116
|
+
readonly setWindowRect?: boolean;
|
|
117
|
+
readonly userAgent?: string;
|
|
118
|
+
readonly [key: string]: unknown;
|
|
119
|
+
}
|
|
120
|
+
export interface SessionStatus {
|
|
121
|
+
readonly connected: boolean;
|
|
122
|
+
readonly sessionId?: string;
|
|
123
|
+
readonly browser?: BrowserCapabilities;
|
|
124
|
+
readonly profile?: string;
|
|
125
|
+
readonly contexts?: number;
|
|
126
|
+
}
|
|
127
|
+
export interface SessionManagerLike {
|
|
128
|
+
status(): Promise<SessionStatus>;
|
|
129
|
+
profiles(): Promise<readonly DiscoveryCandidate[]>;
|
|
130
|
+
version(): Promise<{
|
|
131
|
+
serverVersion: string;
|
|
132
|
+
browserVersion?: string;
|
|
133
|
+
}>;
|
|
134
|
+
connect(): Promise<SessionStatus>;
|
|
135
|
+
disconnect(): Promise<void>;
|
|
136
|
+
/** Send a BiDi command on the live session. */
|
|
137
|
+
request(method: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
138
|
+
/** Present while a session is live; undefined after disconnect. */
|
|
139
|
+
readonly runtime?: ConnectionRuntimeLike;
|
|
140
|
+
}
|
|
141
|
+
/** Minimal transport surface the connection/session layer needs. */
|
|
142
|
+
export interface BiDiClientLike {
|
|
143
|
+
request(method: string, params?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
144
|
+
onEvent(handler: (event: BiDiEvent) => void): () => void;
|
|
145
|
+
}
|
|
146
|
+
/** A top-level browsing context (tab) as seen by the context manager. */
|
|
147
|
+
export interface TabInfo {
|
|
148
|
+
readonly context: string;
|
|
149
|
+
readonly url: string;
|
|
150
|
+
/** BiDi user context id ('default' or a uuid) when the browser reports it. */
|
|
151
|
+
readonly userContext?: string;
|
|
152
|
+
/** The tab tools default to; heuristic plus explicit `select`. */
|
|
153
|
+
readonly selected?: boolean;
|
|
154
|
+
readonly children?: number;
|
|
155
|
+
}
|
|
156
|
+
/** A browser container (user context): BiDi id joined with profile identity. */
|
|
157
|
+
export interface ContainerInfo {
|
|
158
|
+
/** BiDi id: 'default' or a uuid. */
|
|
159
|
+
readonly userContext: string;
|
|
160
|
+
readonly name?: string;
|
|
161
|
+
/** How the name was derived — Firefox exposes no id bridge, so it is positional. */
|
|
162
|
+
readonly nameSource?: 'positional';
|
|
163
|
+
readonly profileUserContextId?: number;
|
|
164
|
+
readonly icon?: string;
|
|
165
|
+
readonly color?: string;
|
|
166
|
+
readonly tabCount?: number;
|
|
167
|
+
}
|
|
168
|
+
/** A Zen space joined with its bound container where possible. */
|
|
169
|
+
export interface SpaceListing {
|
|
170
|
+
readonly uuid: string;
|
|
171
|
+
readonly name: string;
|
|
172
|
+
readonly themeType?: string;
|
|
173
|
+
readonly containerId?: number;
|
|
174
|
+
readonly containerName?: string;
|
|
175
|
+
readonly containerUserContext?: string;
|
|
176
|
+
}
|
|
177
|
+
/** A buffered console (log.entryAdded) entry. */
|
|
178
|
+
export interface ConsoleEntry {
|
|
179
|
+
readonly channel: string;
|
|
180
|
+
readonly level: string;
|
|
181
|
+
readonly text: string;
|
|
182
|
+
readonly context?: string;
|
|
183
|
+
readonly receivedAt: number;
|
|
184
|
+
}
|
|
185
|
+
/** A buffered raw BiDi event. */
|
|
186
|
+
export interface EventRecord {
|
|
187
|
+
readonly method: string;
|
|
188
|
+
readonly params: Record<string, unknown>;
|
|
189
|
+
readonly receivedAt: number;
|
|
190
|
+
}
|
|
191
|
+
export interface EventFilter {
|
|
192
|
+
readonly methods?: readonly string[];
|
|
193
|
+
readonly context?: string;
|
|
194
|
+
readonly limit?: number;
|
|
195
|
+
/** Remove returned events from the buffer when true. */
|
|
196
|
+
readonly drain?: boolean;
|
|
197
|
+
}
|
|
198
|
+
export interface RuntimeStats {
|
|
199
|
+
readonly subscribed: readonly string[];
|
|
200
|
+
readonly buffered: Readonly<Record<string, number>>;
|
|
201
|
+
readonly dropped: Readonly<Record<string, number>>;
|
|
202
|
+
}
|
|
203
|
+
export interface EventPipelineLike {
|
|
204
|
+
/** Buffer one incoming event. Called by the session wiring, not tools. */
|
|
205
|
+
record(event: BiDiEvent): void;
|
|
206
|
+
/** Return buffered events matching the filter; optionally drain them. */
|
|
207
|
+
events(filter?: EventFilter): readonly EventRecord[];
|
|
208
|
+
/** Buffered console entries mapped from log.eventAdded frames. */
|
|
209
|
+
consoleEntries(filter?: {
|
|
210
|
+
level?: string;
|
|
211
|
+
context?: string;
|
|
212
|
+
limit?: number;
|
|
213
|
+
}): readonly ConsoleEntry[];
|
|
214
|
+
/** Drop buffered console entries only. */
|
|
215
|
+
clearConsole(): void;
|
|
216
|
+
stats(): RuntimeStats;
|
|
217
|
+
/** Unhook from the client. Safe to call more than once. */
|
|
218
|
+
detach(): void;
|
|
219
|
+
}
|
|
220
|
+
export interface ContextManagerLike {
|
|
221
|
+
/** Authoritative tab list (fresh getTree) merged with local selection. */
|
|
222
|
+
list(): Promise<readonly TabInfo[]>;
|
|
223
|
+
/** Resolve a tool's target context: explicit id, else the selected tab. */
|
|
224
|
+
ensureContext(context?: string): Promise<string>;
|
|
225
|
+
select(context: string): Promise<TabInfo>;
|
|
226
|
+
/** Handle one incoming event. Called by the session wiring, not tools. */
|
|
227
|
+
handleEvent(event: BiDiEvent): void;
|
|
228
|
+
forget(context: string): void;
|
|
229
|
+
}
|
|
230
|
+
export interface ConnectionRuntimeLike {
|
|
231
|
+
readonly events: EventPipelineLike;
|
|
232
|
+
readonly contexts: ContextManagerLike;
|
|
233
|
+
readonly snapshots: SnapshotCacheLike;
|
|
234
|
+
}
|
|
235
|
+
/** One interactive element in a page snapshot. */
|
|
236
|
+
export interface ElementInfo {
|
|
237
|
+
readonly uid: string;
|
|
238
|
+
readonly role: string;
|
|
239
|
+
readonly name: string;
|
|
240
|
+
readonly tag: string;
|
|
241
|
+
readonly bounds: readonly [number, number, number, number];
|
|
242
|
+
/** CSS path within its document or shadow root. */
|
|
243
|
+
readonly selector: string;
|
|
244
|
+
/** Iframe chain (CSS paths), outermost frame first. */
|
|
245
|
+
readonly frame?: readonly string[];
|
|
246
|
+
/** CSS path of the shadow host, when the element lives in a shadow root. */
|
|
247
|
+
readonly shadowHost?: string;
|
|
248
|
+
/** Non-default states only (focused, disabled, checked, ...). */
|
|
249
|
+
readonly states?: Readonly<Record<string, string | number | boolean>>;
|
|
250
|
+
}
|
|
251
|
+
/** A parsed page snapshot for one browsing context. */
|
|
252
|
+
export interface PageSnapshot {
|
|
253
|
+
readonly context: string;
|
|
254
|
+
readonly url: string;
|
|
255
|
+
readonly title: string;
|
|
256
|
+
readonly text: string;
|
|
257
|
+
readonly elements: readonly ElementInfo[];
|
|
258
|
+
/** Interactive elements the DOM cap dropped before serialization. */
|
|
259
|
+
readonly droppedElements?: number;
|
|
260
|
+
readonly builtAt: number;
|
|
261
|
+
}
|
|
262
|
+
/** Budget-fitting metadata for large snapshots. */
|
|
263
|
+
export interface TruncationMeta {
|
|
264
|
+
readonly truncated: boolean;
|
|
265
|
+
readonly originalChars: number;
|
|
266
|
+
readonly omittedElements: number;
|
|
267
|
+
readonly omittedTextChars: number;
|
|
268
|
+
}
|
|
269
|
+
export interface SnapshotCacheLike {
|
|
270
|
+
get(context: string): PageSnapshot | undefined;
|
|
271
|
+
set(snapshot: PageSnapshot): void;
|
|
272
|
+
invalidate(context: string): void;
|
|
273
|
+
/** Handle one incoming event (navigation/close invalidation). */
|
|
274
|
+
handleEvent(event: BiDiEvent): void;
|
|
275
|
+
clear(): void;
|
|
276
|
+
readonly size: number;
|
|
277
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
2
|
+
export declare function assertRecord(value: unknown, name: string): Record<string, unknown>;
|
|
3
|
+
export declare function assertString(value: unknown, name: string): string;
|
|
4
|
+
export declare function assertEnum<T extends string>(value: unknown, name: string, allowed: readonly T[]): T;
|
|
5
|
+
export declare function assertBoolean(value: unknown, name: string): boolean;
|
|
6
|
+
export declare function assertPositiveInt(value: unknown, name: string): number;
|
|
7
|
+
/**
|
|
8
|
+
* Guard a URL a tool is asked to open. `file:`, `data:`, `javascript:`, and
|
|
9
|
+
* `blob:` turn the browser into a local-file or script reader, so only
|
|
10
|
+
* http(s) and about:blank are allowed.
|
|
11
|
+
*/
|
|
12
|
+
export declare function assertNavigableUrl(value: unknown, name: string): string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { ActionableError } from './errors.js';
|
|
2
|
+
export function isRecord(value) {
|
|
3
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
export function assertRecord(value, name) {
|
|
6
|
+
if (!isRecord(value)) {
|
|
7
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} must be an object.`, {
|
|
8
|
+
fix: `Pass ${name} as a JSON object.`,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
export function assertString(value, name) {
|
|
14
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
15
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} must be a non-empty string.`, {
|
|
16
|
+
fix: `Pass ${name} as a string.`,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
export function assertEnum(value, name, allowed) {
|
|
22
|
+
const text = assertString(value, name);
|
|
23
|
+
if (!allowed.includes(text)) {
|
|
24
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} has an unknown value "${text}".`, {
|
|
25
|
+
fix: `Use one of: ${allowed.join(', ')}.`,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return text;
|
|
29
|
+
}
|
|
30
|
+
export function assertBoolean(value, name) {
|
|
31
|
+
if (typeof value !== 'boolean') {
|
|
32
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} must be a boolean.`, {
|
|
33
|
+
fix: `Pass ${name} as true or false.`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
export function assertPositiveInt(value, name) {
|
|
39
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
40
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} must be a positive integer.`, {
|
|
41
|
+
fix: `Pass ${name} as a whole number greater than zero.`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
/** Schemes a tool may open. Local and inline schemes are read paths. */
|
|
47
|
+
const NAVIGABLE_PROTOCOLS = ['http:', 'https:'];
|
|
48
|
+
/**
|
|
49
|
+
* Guard a URL a tool is asked to open. `file:`, `data:`, `javascript:`, and
|
|
50
|
+
* `blob:` turn the browser into a local-file or script reader, so only
|
|
51
|
+
* http(s) and about:blank are allowed.
|
|
52
|
+
*/
|
|
53
|
+
export function assertNavigableUrl(value, name) {
|
|
54
|
+
const text = assertString(value, name);
|
|
55
|
+
if (text === 'about:blank') {
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
let parsed;
|
|
59
|
+
try {
|
|
60
|
+
parsed = new URL(text);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} must be an absolute URL.`, {
|
|
64
|
+
fix: 'Pass a full URL such as https://example.com.',
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!NAVIGABLE_PROTOCOLS.includes(parsed.protocol)) {
|
|
68
|
+
throw new ActionableError('INVALID_ARGUMENT', `${name} uses the blocked scheme "${parsed.protocol}".`, {
|
|
69
|
+
fix: 'Only http:, https:, and about:blank can be opened. file:, data:, javascript:, and blob: can read local data.',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return text;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=assert.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ActionableErrorCode, ActionableErrorShape } from '../types.js';
|
|
2
|
+
export interface ActionableErrorOptions {
|
|
3
|
+
fix?: string;
|
|
4
|
+
details?: Record<string, unknown>;
|
|
5
|
+
cause?: unknown;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* An error with a stable code and a concrete fix. Every user-facing failure
|
|
9
|
+
* should be one of these; raw errors are converted at the boundary.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ActionableError extends Error {
|
|
12
|
+
readonly code: ActionableErrorCode;
|
|
13
|
+
readonly fix?: string;
|
|
14
|
+
readonly details?: Record<string, unknown>;
|
|
15
|
+
constructor(code: ActionableErrorCode, message: string, options?: ActionableErrorOptions);
|
|
16
|
+
toJSON(): ActionableErrorShape;
|
|
17
|
+
}
|
|
18
|
+
export declare function toActionableError(error: unknown): ActionableError;
|
|
19
|
+
export declare function formatError(error: unknown): string;
|
|
20
|
+
/**
|
|
21
|
+
* Turn the browser's "no such context" rejection into CONTEXT_NOT_FOUND, so
|
|
22
|
+
* callers get the documented code instead of a raw BiDi message.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toContextError(error: unknown): ActionableError;
|