@jackwener/opencli 0.7.5 → 0.7.8
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/.agents/skills/cross-project-adapter-migration/SKILL.md +249 -0
- package/.agents/workflows/cross-project-adapter-migration.md +54 -0
- package/dist/browser/discover.d.ts +8 -0
- package/dist/browser/discover.js +83 -0
- package/dist/browser/errors.d.ts +21 -0
- package/dist/browser/errors.js +54 -0
- package/dist/browser/index.d.ts +22 -0
- package/dist/browser/index.js +22 -0
- package/dist/browser/mcp.d.ts +33 -0
- package/dist/browser/mcp.js +304 -0
- package/dist/browser/page.d.ts +41 -0
- package/dist/browser/page.js +142 -0
- package/dist/browser/tabs.d.ts +13 -0
- package/dist/browser/tabs.js +70 -0
- package/dist/browser.test.js +1 -1
- package/dist/completion.d.ts +21 -0
- package/dist/completion.js +116 -0
- package/dist/doctor.js +7 -7
- package/dist/engine.js +6 -4
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +42 -0
- package/dist/logger.d.ts +22 -0
- package/dist/logger.js +47 -0
- package/dist/main.js +37 -2
- package/dist/pipeline/executor.js +8 -8
- package/dist/pipeline/steps/browser.d.ts +7 -7
- package/dist/pipeline/steps/intercept.d.ts +1 -1
- package/dist/pipeline/steps/tap.d.ts +1 -1
- package/dist/setup.js +1 -1
- package/package.json +4 -3
- package/scripts/clean-yaml.cjs +19 -0
- package/scripts/copy-yaml.cjs +21 -0
- package/scripts/postinstall.js +200 -0
- package/src/bilibili.ts +1 -1
- package/src/browser/discover.ts +90 -0
- package/src/browser/errors.ts +89 -0
- package/src/browser/index.ts +26 -0
- package/src/browser/mcp.ts +305 -0
- package/src/browser/page.ts +152 -0
- package/src/browser/tabs.ts +76 -0
- package/src/browser.test.ts +1 -1
- package/src/completion.ts +129 -0
- package/src/doctor.ts +13 -1
- package/src/engine.ts +9 -4
- package/src/errors.ts +48 -0
- package/src/logger.ts +57 -0
- package/src/main.ts +39 -3
- package/src/pipeline/executor.ts +8 -7
- package/src/pipeline/steps/browser.ts +18 -18
- package/src/pipeline/steps/intercept.ts +8 -8
- package/src/pipeline/steps/tap.ts +2 -2
- package/src/setup.ts +1 -1
- package/tsconfig.json +1 -2
- package/dist/browser.d.ts +0 -105
- package/dist/browser.js +0 -644
- package/src/browser.ts +0 -698
package/src/doctor.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { createInterface } from 'node:readline/promises';
|
|
|
6
6
|
import { stdin as input, stdout as output } from 'node:process';
|
|
7
7
|
import chalk from 'chalk';
|
|
8
8
|
import type { IPage } from './types.js';
|
|
9
|
-
import { PlaywrightMCP, getTokenFingerprint } from './browser.js';
|
|
9
|
+
import { PlaywrightMCP, getTokenFingerprint } from './browser/index.js';
|
|
10
10
|
import { browserSession } from './runtime.js';
|
|
11
11
|
|
|
12
12
|
const PLAYWRIGHT_SERVER_NAME = 'playwright';
|
|
@@ -325,6 +325,8 @@ export function discoverExtensionToken(): string | null {
|
|
|
325
325
|
if (platform === 'darwin') {
|
|
326
326
|
bases.push(
|
|
327
327
|
path.join(home, 'Library', 'Application Support', 'Google', 'Chrome'),
|
|
328
|
+
path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Dev'),
|
|
329
|
+
path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Beta'),
|
|
328
330
|
path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Canary'),
|
|
329
331
|
path.join(home, 'Library', 'Application Support', 'Chromium'),
|
|
330
332
|
path.join(home, 'Library', 'Application Support', 'Microsoft Edge'),
|
|
@@ -332,6 +334,8 @@ export function discoverExtensionToken(): string | null {
|
|
|
332
334
|
} else if (platform === 'linux') {
|
|
333
335
|
bases.push(
|
|
334
336
|
path.join(home, '.config', 'google-chrome'),
|
|
337
|
+
path.join(home, '.config', 'google-chrome-unstable'),
|
|
338
|
+
path.join(home, '.config', 'google-chrome-beta'),
|
|
335
339
|
path.join(home, '.config', 'chromium'),
|
|
336
340
|
path.join(home, '.config', 'microsoft-edge'),
|
|
337
341
|
);
|
|
@@ -339,6 +343,8 @@ export function discoverExtensionToken(): string | null {
|
|
|
339
343
|
const appData = process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local');
|
|
340
344
|
bases.push(
|
|
341
345
|
path.join(appData, 'Google', 'Chrome', 'User Data'),
|
|
346
|
+
path.join(appData, 'Google', 'Chrome Dev', 'User Data'),
|
|
347
|
+
path.join(appData, 'Google', 'Chrome Beta', 'User Data'),
|
|
342
348
|
path.join(appData, 'Microsoft', 'Edge', 'User Data'),
|
|
343
349
|
);
|
|
344
350
|
}
|
|
@@ -451,6 +457,8 @@ export function checkExtensionInstalled(): { installed: boolean; browsers: strin
|
|
|
451
457
|
if (platform === 'darwin') {
|
|
452
458
|
browserDirs.push(
|
|
453
459
|
{ name: 'Chrome', base: path.join(home, 'Library', 'Application Support', 'Google', 'Chrome') },
|
|
460
|
+
{ name: 'Chrome Dev', base: path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Dev') },
|
|
461
|
+
{ name: 'Chrome Beta', base: path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Beta') },
|
|
454
462
|
{ name: 'Chrome Canary', base: path.join(home, 'Library', 'Application Support', 'Google', 'Chrome Canary') },
|
|
455
463
|
{ name: 'Chromium', base: path.join(home, 'Library', 'Application Support', 'Chromium') },
|
|
456
464
|
{ name: 'Edge', base: path.join(home, 'Library', 'Application Support', 'Microsoft Edge') },
|
|
@@ -458,6 +466,8 @@ export function checkExtensionInstalled(): { installed: boolean; browsers: strin
|
|
|
458
466
|
} else if (platform === 'linux') {
|
|
459
467
|
browserDirs.push(
|
|
460
468
|
{ name: 'Chrome', base: path.join(home, '.config', 'google-chrome') },
|
|
469
|
+
{ name: 'Chrome Dev', base: path.join(home, '.config', 'google-chrome-unstable') },
|
|
470
|
+
{ name: 'Chrome Beta', base: path.join(home, '.config', 'google-chrome-beta') },
|
|
461
471
|
{ name: 'Chromium', base: path.join(home, '.config', 'chromium') },
|
|
462
472
|
{ name: 'Edge', base: path.join(home, '.config', 'microsoft-edge') },
|
|
463
473
|
);
|
|
@@ -465,6 +475,8 @@ export function checkExtensionInstalled(): { installed: boolean; browsers: strin
|
|
|
465
475
|
const appData = process.env.LOCALAPPDATA || path.join(home, 'AppData', 'Local');
|
|
466
476
|
browserDirs.push(
|
|
467
477
|
{ name: 'Chrome', base: path.join(appData, 'Google', 'Chrome', 'User Data') },
|
|
478
|
+
{ name: 'Chrome Dev', base: path.join(appData, 'Google', 'Chrome Dev', 'User Data') },
|
|
479
|
+
{ name: 'Chrome Beta', base: path.join(appData, 'Google', 'Chrome Beta', 'User Data') },
|
|
468
480
|
{ name: 'Edge', base: path.join(appData, 'Microsoft', 'Edge', 'User Data') },
|
|
469
481
|
);
|
|
470
482
|
}
|
package/src/engine.ts
CHANGED
|
@@ -14,6 +14,8 @@ import yaml from 'js-yaml';
|
|
|
14
14
|
import { type CliCommand, type InternalCliCommand, type Arg, Strategy, registerCommand } from './registry.js';
|
|
15
15
|
import type { IPage } from './types.js';
|
|
16
16
|
import { executePipeline } from './pipeline.js';
|
|
17
|
+
import { log } from './logger.js';
|
|
18
|
+
import { AdapterLoadError } from './errors.js';
|
|
17
19
|
|
|
18
20
|
/** Set of TS module paths that have been loaded */
|
|
19
21
|
const _loadedModules = new Set<string>();
|
|
@@ -84,7 +86,7 @@ function loadFromManifest(manifestPath: string, clisDir: string): void {
|
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
} catch (err: any) {
|
|
87
|
-
|
|
89
|
+
log.warn(`Failed to load manifest ${manifestPath}: ${err.message}`);
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
|
|
@@ -107,7 +109,7 @@ async function discoverClisFromFs(dir: string): Promise<void> {
|
|
|
107
109
|
) {
|
|
108
110
|
promises.push(
|
|
109
111
|
import(`file://${filePath}`).catch((err: any) => {
|
|
110
|
-
|
|
112
|
+
log.warn(`Failed to load module ${filePath}: ${err.message}`);
|
|
111
113
|
})
|
|
112
114
|
);
|
|
113
115
|
}
|
|
@@ -158,7 +160,7 @@ function registerYamlCli(filePath: string, defaultSite: string): void {
|
|
|
158
160
|
|
|
159
161
|
registerCommand(cmd);
|
|
160
162
|
} catch (err: any) {
|
|
161
|
-
|
|
163
|
+
log.warn(`Failed to load ${filePath}: ${err.message}`);
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
166
|
|
|
@@ -180,7 +182,10 @@ export async function executeCommand(
|
|
|
180
182
|
await import(`file://${modulePath}`);
|
|
181
183
|
_loadedModules.add(modulePath);
|
|
182
184
|
} catch (err: any) {
|
|
183
|
-
throw new
|
|
185
|
+
throw new AdapterLoadError(
|
|
186
|
+
`Failed to load adapter module ${modulePath}: ${err.message}`,
|
|
187
|
+
'Check that the adapter file exists and has no syntax errors.',
|
|
188
|
+
);
|
|
184
189
|
}
|
|
185
190
|
}
|
|
186
191
|
// After loading, the module's cli() call will have updated the registry
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified error types for opencli.
|
|
3
|
+
*
|
|
4
|
+
* All errors thrown by the framework should extend CliError so that
|
|
5
|
+
* the top-level handler in main.ts can render consistent, helpful output.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export class CliError extends Error {
|
|
9
|
+
/** Machine-readable error code (e.g. 'BROWSER_CONNECT', 'ADAPTER_LOAD') */
|
|
10
|
+
readonly code: string;
|
|
11
|
+
/** Human-readable hint on how to fix the problem */
|
|
12
|
+
readonly hint?: string;
|
|
13
|
+
|
|
14
|
+
constructor(code: string, message: string, hint?: string) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'CliError';
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.hint = hint;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class BrowserConnectError extends CliError {
|
|
23
|
+
constructor(message: string, hint?: string) {
|
|
24
|
+
super('BROWSER_CONNECT', message, hint);
|
|
25
|
+
this.name = 'BrowserConnectError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class AdapterLoadError extends CliError {
|
|
30
|
+
constructor(message: string, hint?: string) {
|
|
31
|
+
super('ADAPTER_LOAD', message, hint);
|
|
32
|
+
this.name = 'AdapterLoadError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class CommandExecutionError extends CliError {
|
|
37
|
+
constructor(message: string, hint?: string) {
|
|
38
|
+
super('COMMAND_EXEC', message, hint);
|
|
39
|
+
this.name = 'CommandExecutionError';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class ConfigError extends CliError {
|
|
44
|
+
constructor(message: string, hint?: string) {
|
|
45
|
+
super('CONFIG', message, hint);
|
|
46
|
+
this.name = 'ConfigError';
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified logging for opencli.
|
|
3
|
+
*
|
|
4
|
+
* All framework output (warnings, debug info, errors) should go through
|
|
5
|
+
* this module so that verbosity levels are respected consistently.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import chalk from 'chalk';
|
|
9
|
+
|
|
10
|
+
function isVerbose(): boolean {
|
|
11
|
+
return !!process.env.OPENCLI_VERBOSE;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isDebug(): boolean {
|
|
15
|
+
return !!process.env.DEBUG?.includes('opencli');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const log = {
|
|
19
|
+
/** Informational message (always shown) */
|
|
20
|
+
info(msg: string): void {
|
|
21
|
+
process.stderr.write(`${chalk.blue('ℹ')} ${msg}\n`);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
/** Warning (always shown) */
|
|
25
|
+
warn(msg: string): void {
|
|
26
|
+
process.stderr.write(`${chalk.yellow('⚠')} ${msg}\n`);
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
/** Error (always shown) */
|
|
30
|
+
error(msg: string): void {
|
|
31
|
+
process.stderr.write(`${chalk.red('✖')} ${msg}\n`);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/** Verbose output (only when OPENCLI_VERBOSE is set or -v flag) */
|
|
35
|
+
verbose(msg: string): void {
|
|
36
|
+
if (isVerbose()) {
|
|
37
|
+
process.stderr.write(`${chalk.dim('[verbose]')} ${msg}\n`);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/** Debug output (only when DEBUG includes 'opencli') */
|
|
42
|
+
debug(msg: string): void {
|
|
43
|
+
if (isDebug()) {
|
|
44
|
+
process.stderr.write(`${chalk.dim('[debug]')} ${msg}\n`);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/** Step-style debug (for pipeline steps, etc.) */
|
|
49
|
+
step(stepNum: number, total: number, op: string, preview: string = ''): void {
|
|
50
|
+
process.stderr.write(` ${chalk.dim(`[${stepNum}/${total}]`)} ${chalk.bold.cyan(op)}${preview}\n`);
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
/** Step result summary */
|
|
54
|
+
stepResult(summary: string): void {
|
|
55
|
+
process.stderr.write(` ${chalk.dim(`→ ${summary}`)}\n`);
|
|
56
|
+
},
|
|
57
|
+
};
|
package/src/main.ts
CHANGED
|
@@ -11,9 +11,11 @@ import chalk from 'chalk';
|
|
|
11
11
|
import { discoverClis, executeCommand } from './engine.js';
|
|
12
12
|
import { type CliCommand, fullName, getRegistry, strategyLabel } from './registry.js';
|
|
13
13
|
import { render as renderOutput } from './output.js';
|
|
14
|
-
import { PlaywrightMCP } from './browser.js';
|
|
14
|
+
import { PlaywrightMCP } from './browser/index.js';
|
|
15
15
|
import { browserSession, DEFAULT_BROWSER_COMMAND_TIMEOUT, runWithTimeout } from './runtime.js';
|
|
16
16
|
import { PKG_VERSION } from './version.js';
|
|
17
|
+
import { getCompletions, printCompletionScript } from './completion.js';
|
|
18
|
+
import { CliError } from './errors.js';
|
|
17
19
|
|
|
18
20
|
const __filename = fileURLToPath(import.meta.url);
|
|
19
21
|
const __dirname = path.dirname(__filename);
|
|
@@ -22,6 +24,27 @@ const USER_CLIS = path.join(os.homedir(), '.opencli', 'clis');
|
|
|
22
24
|
|
|
23
25
|
await discoverClis(BUILTIN_CLIS, USER_CLIS);
|
|
24
26
|
|
|
27
|
+
// ── Fast-path: handle --get-completions before commander parses ─────────
|
|
28
|
+
// Usage: opencli --get-completions --cursor <N> [word1 word2 ...]
|
|
29
|
+
const getCompIdx = process.argv.indexOf('--get-completions');
|
|
30
|
+
if (getCompIdx !== -1) {
|
|
31
|
+
const rest = process.argv.slice(getCompIdx + 1);
|
|
32
|
+
let cursor: number | undefined;
|
|
33
|
+
const words: string[] = [];
|
|
34
|
+
for (let i = 0; i < rest.length; i++) {
|
|
35
|
+
if (rest[i] === '--cursor' && i + 1 < rest.length) {
|
|
36
|
+
cursor = parseInt(rest[i + 1], 10);
|
|
37
|
+
i++; // skip the value
|
|
38
|
+
} else {
|
|
39
|
+
words.push(rest[i]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (cursor === undefined) cursor = words.length;
|
|
43
|
+
const candidates = getCompletions(words, cursor);
|
|
44
|
+
process.stdout.write(candidates.join('\n') + '\n');
|
|
45
|
+
process.exit(0);
|
|
46
|
+
}
|
|
47
|
+
|
|
25
48
|
const program = new Command();
|
|
26
49
|
program.name('opencli').description('Make any website your CLI. Zero setup. AI-powered.').version(PKG_VERSION);
|
|
27
50
|
|
|
@@ -128,6 +151,13 @@ program.command('setup')
|
|
|
128
151
|
await runSetup({ cliVersion: PKG_VERSION, token: opts.token });
|
|
129
152
|
});
|
|
130
153
|
|
|
154
|
+
program.command('completion')
|
|
155
|
+
.description('Output shell completion script')
|
|
156
|
+
.argument('<shell>', 'Shell type: bash, zsh, or fish')
|
|
157
|
+
.action((shell) => {
|
|
158
|
+
printCompletionScript(shell);
|
|
159
|
+
});
|
|
160
|
+
|
|
131
161
|
// ── Dynamic site commands ──────────────────────────────────────────────────
|
|
132
162
|
|
|
133
163
|
const registry = getRegistry();
|
|
@@ -183,8 +213,14 @@ for (const [, cmd] of registry) {
|
|
|
183
213
|
}
|
|
184
214
|
renderOutput(result, { fmt: actionOpts.format, columns: cmd.columns, title: `${cmd.site}/${cmd.name}`, elapsed: (Date.now() - startTime) / 1000, source: fullName(cmd) });
|
|
185
215
|
} catch (err: any) {
|
|
186
|
-
if (
|
|
187
|
-
|
|
216
|
+
if (err instanceof CliError) {
|
|
217
|
+
console.error(chalk.red(`Error [${err.code}]: ${err.message}`));
|
|
218
|
+
if (err.hint) console.error(chalk.yellow(`Hint: ${err.hint}`));
|
|
219
|
+
} else if (actionOpts.verbose && err.stack) {
|
|
220
|
+
console.error(chalk.red(err.stack));
|
|
221
|
+
} else {
|
|
222
|
+
console.error(chalk.red(`Error: ${err.message ?? err}`));
|
|
223
|
+
}
|
|
188
224
|
process.exitCode = 1;
|
|
189
225
|
}
|
|
190
226
|
});
|
package/src/pipeline/executor.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { stepFetch } from './steps/fetch.js';
|
|
|
9
9
|
import { stepSelect, stepMap, stepFilter, stepSort, stepLimit } from './steps/transform.js';
|
|
10
10
|
import { stepIntercept } from './steps/intercept.js';
|
|
11
11
|
import { stepTap } from './steps/tap.js';
|
|
12
|
+
import { log } from '../logger.js';
|
|
12
13
|
|
|
13
14
|
export interface PipelineContext {
|
|
14
15
|
args?: Record<string, any>;
|
|
@@ -57,7 +58,7 @@ export async function executePipeline(
|
|
|
57
58
|
if (handler) {
|
|
58
59
|
data = await handler(page, params, data, args);
|
|
59
60
|
} else {
|
|
60
|
-
if (debug)
|
|
61
|
+
if (debug) log.warn(`Unknown step: ${op}`);
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
if (debug) debugStepResult(op, data);
|
|
@@ -73,21 +74,21 @@ function debugStepStart(stepNum: number, total: number, op: string, params: any)
|
|
|
73
74
|
} else if (params && typeof params === 'object' && !Array.isArray(params)) {
|
|
74
75
|
preview = ` (${Object.keys(params).join(', ')})`;
|
|
75
76
|
}
|
|
76
|
-
|
|
77
|
+
log.step(stepNum, total, op, preview);
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
function debugStepResult(op: string, data: any): void {
|
|
80
81
|
if (data === null || data === undefined) {
|
|
81
|
-
|
|
82
|
+
log.stepResult('(no data)');
|
|
82
83
|
} else if (Array.isArray(data)) {
|
|
83
|
-
|
|
84
|
+
log.stepResult(`${data.length} items`);
|
|
84
85
|
} else if (typeof data === 'object') {
|
|
85
86
|
const keys = Object.keys(data).slice(0, 5);
|
|
86
|
-
|
|
87
|
+
log.stepResult(`dict (${keys.join(', ')}${Object.keys(data).length > 5 ? '...' : ''})`);
|
|
87
88
|
} else if (typeof data === 'string') {
|
|
88
89
|
const p = data.slice(0, 60).replace(/\n/g, '\\n');
|
|
89
|
-
|
|
90
|
+
log.stepResult(`"${p}${data.length > 60 ? '...' : ''}"`);
|
|
90
91
|
} else {
|
|
91
|
-
|
|
92
|
+
log.stepResult(`${typeof data}`);
|
|
92
93
|
}
|
|
93
94
|
}
|
|
@@ -6,53 +6,53 @@
|
|
|
6
6
|
import type { IPage } from '../../types.js';
|
|
7
7
|
import { render, normalizeEvaluateSource } from '../template.js';
|
|
8
8
|
|
|
9
|
-
export async function stepNavigate(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
9
|
+
export async function stepNavigate(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
10
10
|
const url = render(params, { args, data });
|
|
11
|
-
await page
|
|
11
|
+
await page!.goto(String(url));
|
|
12
12
|
return data;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export async function stepClick(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
16
|
-
await page
|
|
15
|
+
export async function stepClick(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
16
|
+
await page!.click(String(render(params, { args, data })).replace(/^@/, ''));
|
|
17
17
|
return data;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export async function stepType(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
20
|
+
export async function stepType(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
21
21
|
if (typeof params === 'object' && params) {
|
|
22
22
|
const ref = String(render(params.ref ?? '', { args, data })).replace(/^@/, '');
|
|
23
23
|
const text = String(render(params.text ?? '', { args, data }));
|
|
24
|
-
await page
|
|
25
|
-
if (params.submit) await page
|
|
24
|
+
await page!.typeText(ref, text);
|
|
25
|
+
if (params.submit) await page!.pressKey('Enter');
|
|
26
26
|
}
|
|
27
27
|
return data;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
export async function stepWait(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
31
|
-
if (typeof params === 'number') await page
|
|
30
|
+
export async function stepWait(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
31
|
+
if (typeof params === 'number') await page!.wait(params);
|
|
32
32
|
else if (typeof params === 'object' && params) {
|
|
33
33
|
if ('text' in params) {
|
|
34
|
-
await page
|
|
34
|
+
await page!.wait({
|
|
35
35
|
text: String(render(params.text, { args, data })),
|
|
36
36
|
timeout: params.timeout
|
|
37
37
|
});
|
|
38
|
-
} else if ('time' in params) await page
|
|
39
|
-
} else if (typeof params === 'string') await page
|
|
38
|
+
} else if ('time' in params) await page!.wait(Number(params.time));
|
|
39
|
+
} else if (typeof params === 'string') await page!.wait(Number(render(params, { args, data })));
|
|
40
40
|
return data;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export async function stepPress(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
44
|
-
await page
|
|
43
|
+
export async function stepPress(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
44
|
+
await page!.pressKey(String(render(params, { args, data })));
|
|
45
45
|
return data;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export async function stepSnapshot(page: IPage, params: any, _data: any, _args: Record<string, any>): Promise<any> {
|
|
48
|
+
export async function stepSnapshot(page: IPage | null, params: any, _data: any, _args: Record<string, any>): Promise<any> {
|
|
49
49
|
const opts = (typeof params === 'object' && params) ? params : {};
|
|
50
|
-
return page
|
|
50
|
+
return page!.snapshot({ interactive: opts.interactive ?? false, compact: opts.compact ?? false, maxDepth: opts.max_depth, raw: opts.raw ?? false });
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
export async function stepEvaluate(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
53
|
+
export async function stepEvaluate(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
54
54
|
const js = String(render(params, { args, data }));
|
|
55
|
-
let result = await page
|
|
55
|
+
let result = await page!.evaluate(normalizeEvaluateSource(js));
|
|
56
56
|
// MCP may return JSON as a string — auto-parse it
|
|
57
57
|
if (typeof result === 'string') {
|
|
58
58
|
const trimmed = result.trim();
|
|
@@ -6,7 +6,7 @@ import type { IPage } from '../../types.js';
|
|
|
6
6
|
import { render } from '../template.js';
|
|
7
7
|
import { generateInterceptorJs, generateReadInterceptedJs } from '../../interceptor.js';
|
|
8
8
|
|
|
9
|
-
export async function stepIntercept(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
9
|
+
export async function stepIntercept(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
10
10
|
const cfg = typeof params === 'object' ? params : {};
|
|
11
11
|
const trigger = cfg.trigger ?? '';
|
|
12
12
|
const capturePattern = cfg.capture ?? '';
|
|
@@ -16,28 +16,28 @@ export async function stepIntercept(page: IPage, params: any, data: any, args: R
|
|
|
16
16
|
if (!capturePattern) return data;
|
|
17
17
|
|
|
18
18
|
// Step 1: Inject fetch/XHR interceptor BEFORE trigger
|
|
19
|
-
await page
|
|
19
|
+
await page!.evaluate(generateInterceptorJs(JSON.stringify(capturePattern)));
|
|
20
20
|
|
|
21
21
|
// Step 2: Execute the trigger action
|
|
22
22
|
if (trigger.startsWith('navigate:')) {
|
|
23
23
|
const url = render(trigger.slice('navigate:'.length), { args, data });
|
|
24
|
-
await page
|
|
24
|
+
await page!.goto(String(url));
|
|
25
25
|
} else if (trigger.startsWith('evaluate:')) {
|
|
26
26
|
const js = trigger.slice('evaluate:'.length);
|
|
27
27
|
const { normalizeEvaluateSource } = await import('../template.js');
|
|
28
|
-
await page
|
|
28
|
+
await page!.evaluate(normalizeEvaluateSource(render(js, { args, data }) as string));
|
|
29
29
|
} else if (trigger.startsWith('click:')) {
|
|
30
30
|
const ref = render(trigger.slice('click:'.length), { args, data });
|
|
31
|
-
await page
|
|
31
|
+
await page!.click(String(ref).replace(/^@/, ''));
|
|
32
32
|
} else if (trigger === 'scroll') {
|
|
33
|
-
await page
|
|
33
|
+
await page!.scroll('down');
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
// Step 3: Wait a bit for network requests to fire
|
|
37
|
-
await page
|
|
37
|
+
await page!.wait(Math.min(timeout, 3));
|
|
38
38
|
|
|
39
39
|
// Step 4: Retrieve captured data
|
|
40
|
-
const matchingResponses = await page
|
|
40
|
+
const matchingResponses = await page!.evaluate(generateReadInterceptedJs());
|
|
41
41
|
|
|
42
42
|
// Step 5: Select from response if specified
|
|
43
43
|
let result = matchingResponses.length === 1 ? matchingResponses[0] :
|
|
@@ -13,7 +13,7 @@ import type { IPage } from '../../types.js';
|
|
|
13
13
|
import { render } from '../template.js';
|
|
14
14
|
import { generateTapInterceptorJs } from '../../interceptor.js';
|
|
15
15
|
|
|
16
|
-
export async function stepTap(page: IPage, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
16
|
+
export async function stepTap(page: IPage | null, params: any, data: any, args: Record<string, any>): Promise<any> {
|
|
17
17
|
const cfg = typeof params === 'object' ? params : {};
|
|
18
18
|
const storeName = String(render(cfg.store ?? '', { args, data }));
|
|
19
19
|
const actionName = String(render(cfg.action ?? '', { args, data }));
|
|
@@ -96,5 +96,5 @@ export async function stepTap(page: IPage, params: any, data: any, args: Record<
|
|
|
96
96
|
}
|
|
97
97
|
`;
|
|
98
98
|
|
|
99
|
-
return page
|
|
99
|
+
return page!.evaluate(js);
|
|
100
100
|
}
|
package/src/setup.ts
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
upsertTomlConfigToken,
|
|
25
25
|
writeFileWithMkdir,
|
|
26
26
|
} from './doctor.js';
|
|
27
|
-
import { getTokenFingerprint } from './browser.js';
|
|
27
|
+
import { getTokenFingerprint } from './browser/index.js';
|
|
28
28
|
import { type CheckboxItem, checkboxPrompt } from './tui.js';
|
|
29
29
|
|
|
30
30
|
export async function runSetup(opts: { cliVersion?: string; token?: string } = {}) {
|
package/tsconfig.json
CHANGED
package/dist/browser.d.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser interaction via Playwright MCP Bridge extension.
|
|
3
|
-
* Connects to an existing Chrome browser through the extension.
|
|
4
|
-
*/
|
|
5
|
-
import { withTimeoutMs } from './runtime.js';
|
|
6
|
-
type ConnectFailureKind = 'missing-token' | 'extension-timeout' | 'extension-not-installed' | 'mcp-init' | 'process-exit' | 'unknown';
|
|
7
|
-
type PlaywrightMCPState = 'idle' | 'connecting' | 'connected' | 'closing' | 'closed';
|
|
8
|
-
type ConnectFailureInput = {
|
|
9
|
-
kind: ConnectFailureKind;
|
|
10
|
-
timeout: number;
|
|
11
|
-
hasExtensionToken: boolean;
|
|
12
|
-
tokenFingerprint?: string | null;
|
|
13
|
-
stderr?: string;
|
|
14
|
-
exitCode?: number | null;
|
|
15
|
-
rawMessage?: string;
|
|
16
|
-
};
|
|
17
|
-
export declare function getTokenFingerprint(token: string | undefined): string | null;
|
|
18
|
-
export declare function formatBrowserConnectError(input: ConnectFailureInput): Error;
|
|
19
|
-
declare function createJsonRpcRequest(method: string, params?: Record<string, any>): {
|
|
20
|
-
id: number;
|
|
21
|
-
message: string;
|
|
22
|
-
};
|
|
23
|
-
import type { IPage } from './types.js';
|
|
24
|
-
/**
|
|
25
|
-
* Page abstraction wrapping JSON-RPC calls to Playwright MCP.
|
|
26
|
-
*/
|
|
27
|
-
export declare class Page implements IPage {
|
|
28
|
-
private _request;
|
|
29
|
-
constructor(_request: (method: string, params?: Record<string, any>) => Promise<any>);
|
|
30
|
-
call(method: string, params?: Record<string, any>): Promise<any>;
|
|
31
|
-
goto(url: string): Promise<void>;
|
|
32
|
-
evaluate(js: string): Promise<any>;
|
|
33
|
-
snapshot(opts?: {
|
|
34
|
-
interactive?: boolean;
|
|
35
|
-
compact?: boolean;
|
|
36
|
-
maxDepth?: number;
|
|
37
|
-
raw?: boolean;
|
|
38
|
-
}): Promise<any>;
|
|
39
|
-
click(ref: string): Promise<void>;
|
|
40
|
-
typeText(ref: string, text: string): Promise<void>;
|
|
41
|
-
pressKey(key: string): Promise<void>;
|
|
42
|
-
wait(options: number | {
|
|
43
|
-
text?: string;
|
|
44
|
-
time?: number;
|
|
45
|
-
timeout?: number;
|
|
46
|
-
}): Promise<void>;
|
|
47
|
-
tabs(): Promise<any>;
|
|
48
|
-
closeTab(index?: number): Promise<void>;
|
|
49
|
-
newTab(): Promise<void>;
|
|
50
|
-
selectTab(index: number): Promise<void>;
|
|
51
|
-
networkRequests(includeStatic?: boolean): Promise<any>;
|
|
52
|
-
consoleMessages(level?: string): Promise<any>;
|
|
53
|
-
scroll(direction?: string, _amount?: number): Promise<void>;
|
|
54
|
-
autoScroll(options?: {
|
|
55
|
-
times?: number;
|
|
56
|
-
delayMs?: number;
|
|
57
|
-
}): Promise<void>;
|
|
58
|
-
installInterceptor(pattern: string): Promise<void>;
|
|
59
|
-
getInterceptedRequests(): Promise<any[]>;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Playwright MCP process manager.
|
|
63
|
-
*/
|
|
64
|
-
export declare class PlaywrightMCP {
|
|
65
|
-
private static _activeInsts;
|
|
66
|
-
private static _cleanupRegistered;
|
|
67
|
-
private static _registerGlobalCleanup;
|
|
68
|
-
private _proc;
|
|
69
|
-
private _buffer;
|
|
70
|
-
private _pending;
|
|
71
|
-
private _initialTabIdentities;
|
|
72
|
-
private _closingPromise;
|
|
73
|
-
private _state;
|
|
74
|
-
private _page;
|
|
75
|
-
get state(): PlaywrightMCPState;
|
|
76
|
-
private _sendRequest;
|
|
77
|
-
private _rejectPendingRequests;
|
|
78
|
-
private _resetAfterFailedConnect;
|
|
79
|
-
connect(opts?: {
|
|
80
|
-
timeout?: number;
|
|
81
|
-
}): Promise<Page>;
|
|
82
|
-
close(): Promise<void>;
|
|
83
|
-
}
|
|
84
|
-
declare function extractTabEntries(raw: any): Array<{
|
|
85
|
-
index: number;
|
|
86
|
-
identity: string;
|
|
87
|
-
}>;
|
|
88
|
-
declare function diffTabIndexes(initialIdentities: string[], currentTabs: Array<{
|
|
89
|
-
index: number;
|
|
90
|
-
identity: string;
|
|
91
|
-
}>): number[];
|
|
92
|
-
declare function appendLimited(current: string, chunk: string, limit: number): string;
|
|
93
|
-
declare function buildMcpArgs(input: {
|
|
94
|
-
mcpPath: string;
|
|
95
|
-
executablePath?: string | null;
|
|
96
|
-
}): string[];
|
|
97
|
-
export declare const __test__: {
|
|
98
|
-
createJsonRpcRequest: typeof createJsonRpcRequest;
|
|
99
|
-
extractTabEntries: typeof extractTabEntries;
|
|
100
|
-
diffTabIndexes: typeof diffTabIndexes;
|
|
101
|
-
appendLimited: typeof appendLimited;
|
|
102
|
-
buildMcpArgs: typeof buildMcpArgs;
|
|
103
|
-
withTimeoutMs: typeof withTimeoutMs;
|
|
104
|
-
};
|
|
105
|
-
export {};
|