-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathconfig.ts
More file actions
executable file
·146 lines (131 loc) · 4.27 KB
/
config.ts
File metadata and controls
executable file
·146 lines (131 loc) · 4.27 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type { Config } from "../config.d.ts";
export type ToolCapability = "core" | string;
// Define Command Line Options Structure
export type CLIOptions = {
proxies?: boolean;
advancedStealth?: boolean;
contextId?: string;
persist?: boolean;
port?: number;
host?: string;
browserWidth?: number;
browserHeight?: number;
modelName?: string;
modelApiKey?: string;
keepAlive?: boolean;
experimental?: boolean;
};
// Default Configuration Values
const defaultConfig: Config = {
browserbaseApiKey: process.env.BROWSERBASE_API_KEY ?? "",
browserbaseProjectId: process.env.BROWSERBASE_PROJECT_ID ?? "",
proxies: false,
server: {
port: undefined,
host: undefined,
},
viewPort: {
browserWidth: 1024,
browserHeight: 768,
},
modelName: "gemini-2.0-flash", // Default Model
};
// Resolve final configuration by merging defaults, file config, and CLI options
export async function resolveConfig(cliOptions: CLIOptions): Promise<Config> {
const cliConfig = await configFromCLIOptions(cliOptions);
// Order: Defaults < File Config < CLI Overrides
const mergedConfig = mergeConfig(defaultConfig, cliConfig);
// --- Add Browserbase Env Vars ---
if (!mergedConfig.modelApiKey) {
mergedConfig.modelApiKey =
process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
}
// --------------------------------
// Basic validation for Browserbase keys - provide dummy values if not set
if (!mergedConfig.browserbaseApiKey) {
console.warn(
"Warning: BROWSERBASE_API_KEY environment variable not set. Using dummy value.",
);
mergedConfig.browserbaseApiKey = "dummy-browserbase-api-key";
}
if (!mergedConfig.browserbaseProjectId) {
console.warn(
"Warning: BROWSERBASE_PROJECT_ID environment variable not set. Using dummy value.",
);
mergedConfig.browserbaseProjectId = "dummy-browserbase-project-id";
}
if (!mergedConfig.modelApiKey) {
console.warn(
"Warning: MODEL_API_KEY environment variable not set. Using dummy value.",
);
mergedConfig.modelApiKey = "dummy-api-key";
}
return mergedConfig;
}
// Create Config structure based on CLI options
export async function configFromCLIOptions(
cliOptions: CLIOptions,
): Promise<Config> {
return {
browserbaseApiKey: process.env.BROWSERBASE_API_KEY ?? "",
browserbaseProjectId: process.env.BROWSERBASE_PROJECT_ID ?? "",
server: {
port: cliOptions.port,
host: cliOptions.host,
},
proxies: cliOptions.proxies,
context: {
contextId: cliOptions.contextId,
persist: cliOptions.persist,
},
viewPort: {
browserWidth: cliOptions.browserWidth,
browserHeight: cliOptions.browserHeight,
},
advancedStealth: cliOptions.advancedStealth,
modelName: cliOptions.modelName,
modelApiKey: cliOptions.modelApiKey,
keepAlive: cliOptions.keepAlive,
experimental: cliOptions.experimental,
};
}
// Helper function to merge config objects, excluding undefined values
function pickDefined<T extends object>(obj: T | undefined): Partial<T> {
if (!obj) return {};
return Object.fromEntries(
Object.entries(obj).filter(([, v]) => v !== undefined),
) as Partial<T>;
}
// Merge two configuration objects (overrides takes precedence)
function mergeConfig(base: Config, overrides: Config): Config {
const baseFiltered = pickDefined(base);
const overridesFiltered = pickDefined(overrides);
// Create the result object
const result = { ...baseFiltered } as Config;
// For each property in overrides
for (const [key, value] of Object.entries(overridesFiltered)) {
if (key === "context" && value && result.context) {
// Special handling for context object to ensure deep merge
result.context = {
...result.context,
...(value as Config["context"]),
};
} else if (
value &&
typeof value === "object" &&
!Array.isArray(value) &&
result[key as keyof Config] &&
typeof result[key as keyof Config] === "object"
) {
// Deep merge for other nested objects
result[key as keyof Config] = {
...(result[key as keyof Config] as object),
...value,
} as unknown;
} else {
// Simple override for primitives, arrays, etc.
result[key as keyof Config] = value as unknown;
}
}
return result;
}