-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstagehand.config.ts
76 lines (71 loc) · 2.58 KB
/
stagehand.config.ts
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
import type { ConstructorParams, LogLine } from "@browserbasehq/stagehand";
import dotenv from "dotenv";
dotenv.config();
const StagehandConfig: ConstructorParams = {
env: "BROWSERBASE",
apiKey: process.env.BROWSERBASE_API_KEY /* API key for authentication */,
projectId: process.env.BROWSERBASE_PROJECT_ID /* Project identifier */,
debugDom: true /* Enable DOM debugging features */,
headless: false /* Run browser in headless mode */,
logger: (message: LogLine) =>
console.log(logLineToString(message)) /* Custom logging function */,
domSettleTimeoutMs: 30_000 /* Timeout for DOM to settle in milliseconds */,
browserbaseSessionCreateParams: {
projectId: process.env.BROWSERBASE_PROJECT_ID!,
},
enableCaching: false /* Enable caching functionality */,
browserbaseSessionID:
undefined /* Session ID for resuming Browserbase sessions */,
modelName: "gpt-4o" /* Name of the model to use */,
modelClientOptions: {
apiKey: process.env.OPENAI_API_KEY,
} /* Configuration options for the model client */,
};
export default StagehandConfig;
/**
* Custom logging function that you can use to filter logs.
*
* General pattern here is that `message` will always be unique with no params
* Any param you would put in a log is in `auxiliary`.
*
* For example, an error log looks like this:
*
* ```
* {
* category: "error",
* message: "Some specific error occurred",
* auxiliary: {
* message: { value: "Error message", type: "string" },
* trace: { value: "Error trace", type: "string" }
* }
* }
* ```
*
* You can then use `logLineToString` to filter for a specific log pattern like
*
* ```
* if (logLine.message === "Some specific error occurred") {
* console.log(logLineToString(logLine));
* }
* ```
*/
export function logLineToString(logLine: LogLine): string {
// If you want more detail, set this to false. However, this will make the logs
// more verbose and harder to read.
const HIDE_AUXILIARY = true;
try {
const timestamp = logLine.timestamp || new Date().toISOString();
if (logLine.auxiliary?.error) {
return `${timestamp}::[stagehand:${logLine.category}] ${logLine.message}\n ${logLine.auxiliary.error.value}\n ${logLine.auxiliary.trace.value}`;
}
// If we want to hide auxiliary information, we don't add it to the log
return `${timestamp}::[stagehand:${logLine.category}] ${logLine.message} ${
logLine.auxiliary && !HIDE_AUXILIARY
? JSON.stringify(logLine.auxiliary)
: ""
}`;
} catch (error) {
console.error(`Error logging line:`, error);
return "error logging line";
}
}