2025-03-21 10:58:58 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2025-04-28 15:04:59 -07:00
|
|
|
import fs from 'fs';
|
|
|
|
|
2025-03-21 10:58:58 -07:00
|
|
|
import { program } from 'commander';
|
|
|
|
|
2025-03-25 14:46:39 -07:00
|
|
|
import { createServer } from './index';
|
2025-03-28 13:24:45 -07:00
|
|
|
import { ServerList } from './server';
|
2025-03-21 10:58:58 -07:00
|
|
|
|
2025-04-28 11:11:31 +02:00
|
|
|
import { startHttpTransport, startStdioTransport } from './transport';
|
2025-03-21 10:58:58 -07:00
|
|
|
|
2025-04-28 15:04:59 -07:00
|
|
|
import type { Config, ToolCapability } from '../config';
|
|
|
|
|
2025-03-21 10:58:58 -07:00
|
|
|
const packageJSON = require('../package.json');
|
|
|
|
|
|
|
|
program
|
|
|
|
.version('Version ' + packageJSON.version)
|
|
|
|
.name(packageJSON.name)
|
2025-04-01 10:26:48 -07:00
|
|
|
.option('--browser <browser>', 'Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
|
2025-04-04 17:14:30 -07:00
|
|
|
.option('--caps <caps>', 'Comma-separated list of capabilities to enable, possible values: tabs, pdf, history, wait, files, install. Default is all.')
|
2025-04-01 10:26:48 -07:00
|
|
|
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
|
|
|
|
.option('--executable-path <path>', 'Path to the browser executable.')
|
2025-03-21 10:58:58 -07:00
|
|
|
.option('--headless', 'Run browser in headless mode, headed by default')
|
2025-04-28 15:04:59 -07:00
|
|
|
.option('--user-data-dir <path>', 'Path to the user data directory')
|
2025-04-01 10:26:48 -07:00
|
|
|
.option('--port <port>', 'Port to listen on for SSE transport.')
|
2025-04-24 14:04:00 +08:00
|
|
|
.option('--host <host>', 'Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
|
2025-03-21 10:58:58 -07:00
|
|
|
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
2025-04-28 15:04:59 -07:00
|
|
|
.option('--config <path>', 'Path to the configuration file.')
|
2025-03-21 10:58:58 -07:00
|
|
|
.action(async options => {
|
2025-04-28 15:04:59 -07:00
|
|
|
const cliOverrides: Config = {
|
|
|
|
browser: {
|
|
|
|
type: options.browser,
|
|
|
|
userDataDir: options.userDataDir,
|
|
|
|
headless: options.headless,
|
|
|
|
executablePath: options.executablePath,
|
|
|
|
cdpEndpoint: options.cdpEndpoint,
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
port: options.port,
|
|
|
|
host: options.host,
|
|
|
|
},
|
2025-04-04 17:14:30 -07:00
|
|
|
capabilities: options.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
|
2025-04-28 15:04:59 -07:00
|
|
|
vision: !!options.vision,
|
|
|
|
};
|
|
|
|
const config = await loadConfig(options.config, cliOverrides);
|
|
|
|
const serverList = new ServerList(() => createServer(config));
|
2025-03-28 13:24:45 -07:00
|
|
|
setupExitWatchdog(serverList);
|
2025-03-25 14:46:39 -07:00
|
|
|
|
2025-04-28 11:11:31 +02:00
|
|
|
if (options.port)
|
|
|
|
startHttpTransport(+options.port, options.host, serverList);
|
|
|
|
else
|
|
|
|
await startStdioTransport(serverList);
|
2025-03-21 10:58:58 -07:00
|
|
|
});
|
|
|
|
|
2025-04-28 15:04:59 -07:00
|
|
|
async function loadConfig(configFile: string | undefined, cliOverrides: Config): Promise<Config> {
|
|
|
|
if (!configFile)
|
|
|
|
return cliOverrides;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const config = JSON.parse(await fs.promises.readFile(configFile, 'utf8'));
|
|
|
|
return {
|
|
|
|
...config,
|
|
|
|
...cliOverrides,
|
|
|
|
browser: {
|
|
|
|
...config.browser,
|
|
|
|
...cliOverrides.browser,
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
...config.server,
|
|
|
|
...cliOverrides.server,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error loading config file ${configFile}: ${e}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-28 13:24:45 -07:00
|
|
|
function setupExitWatchdog(serverList: ServerList) {
|
2025-04-07 23:51:57 +02:00
|
|
|
const handleExit = async () => {
|
2025-03-21 10:58:58 -07:00
|
|
|
setTimeout(() => process.exit(0), 15000);
|
2025-03-28 13:24:45 -07:00
|
|
|
await serverList.closeAll();
|
2025-03-21 10:58:58 -07:00
|
|
|
process.exit(0);
|
2025-04-07 23:51:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
process.stdin.on('close', handleExit);
|
|
|
|
process.on('SIGINT', handleExit);
|
|
|
|
process.on('SIGTERM', handleExit);
|
2025-03-21 10:58:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
program.parse(process.argv);
|