-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathprogram.ts
More file actions
100 lines (90 loc) · 3.18 KB
/
program.ts
File metadata and controls
100 lines (90 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { program } from "commander";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
import createServerFunction from "./index.js";
import { ServerList } from "./server.js";
import { startHttpTransport, startStdioTransport } from "./transport.js";
import { resolveConfig } from "./config.js";
let __filename: string;
let __dirname: string;
try {
// Try ES modules first
__filename = fileURLToPath(import.meta.url);
__dirname = path.dirname(__filename);
} catch {
// Fallback for CommonJS or when import.meta is not available
__filename =
(globalThis as { __filename: string }).__filename ||
process.cwd() + "/dist/program.js";
__dirname = path.dirname(__filename);
}
// Load package.json using fs
const packageJSONPath = path.resolve(__dirname, "../package.json");
const packageJSONBuffer = fs.readFileSync(packageJSONPath);
const packageJSON = JSON.parse(packageJSONBuffer.toString());
program
.version("Version " + packageJSON.version)
.name(packageJSON.name)
.option("--browserbaseApiKey <key>", "The Browserbase API Key to use")
.option("--browserbaseProjectId <id>", "The Browserbase Project ID to use")
.option("--proxies", "Use Browserbase proxies.")
.option(
"--advancedStealth",
"Use advanced stealth mode. Only available to Browserbase Scale Plan users.",
)
.option("--contextId <contextId>", "Browserbase Context ID to use.")
.option(
"--persist [boolean]",
"Whether to persist the Browserbase context",
true,
)
.option("--port <port>", "Port to listen on for SHTTP transport.")
.option(
"--host <host>",
"Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.",
)
.option("--browserWidth <width>", "Browser width to use for the browser.")
.option("--browserHeight <height>", "Browser height to use for the browser.")
.option(
"--modelName <model>",
"The model to use for Stagehand (default: gemini-2.0-flash)",
)
.option(
"--modelApiKey <key>",
"API key for the custom model provider (required when using custom models)",
)
.option(
"--baseUrl <url>",
"Base URL for the custom model provider API (for proxies/compatible endpoints)",
)
.option("--keepAlive", "Enable Browserbase Keep Alive Session")
.option("--experimental", "Enable experimental features")
.action(async (options) => {
const config = await resolveConfig(options);
const serverList = new ServerList(async () =>
createServerFunction({
config: config,
}),
);
setupExitWatchdog(serverList);
if (options.port)
startHttpTransport(+options.port, options.host, serverList);
else await startStdioTransport(serverList, config);
});
function setupExitWatchdog(serverList: ServerList) {
const handleExit = async () => {
setTimeout(() => process.exit(0), 15000);
try {
// SessionManager within each server handles session cleanup
await serverList.closeAll();
} catch (error) {
console.error("Error during cleanup:", error);
}
process.exit(0);
};
process.stdin.on("close", handleExit);
process.on("SIGINT", handleExit);
process.on("SIGTERM", handleExit);
}
program.parse(process.argv);