Skip to content

Commit c1cf345

Browse files
authored
Remove duplicate logging and export page/context types (#344)
1 parent 96a5b2f commit c1cf345

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

Diff for: .changeset/tender-years-crash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Remove duplicate logging and expose Page/BrowserContext types

Diff for: lib/index.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Browserbase } from "@browserbasehq/sdk";
2-
import { type BrowserContext, chromium } from "@playwright/test";
2+
import { chromium } from "@playwright/test";
33
import { randomUUID } from "crypto";
44
import dotenv from "dotenv";
55
import fs from "fs";
@@ -9,7 +9,7 @@ import { z } from "zod";
99
import { BrowserResult } from "../types/browser";
1010
import { LogLine } from "../types/log";
1111
import { GotoOptions } from "../types/playwright";
12-
import { Page } from "../types/page";
12+
import { Page, BrowserContext } from "../types/page";
1313
import {
1414
ActOptions,
1515
ActResult,
@@ -303,6 +303,10 @@ async function applyStealthScripts(context: BrowserContext) {
303303
});
304304
}
305305

306+
const defaultLogger = async (logLine: LogLine) => {
307+
console.log(logLineToString(logLine));
308+
};
309+
306310
export class Stagehand {
307311
private stagehandPage!: StagehandPage;
308312
private stagehandContext!: StagehandContext;
@@ -319,7 +323,8 @@ export class Stagehand {
319323
private internalLogger: (logLine: LogLine) => void;
320324
private apiKey: string | undefined;
321325
private projectId: string | undefined;
322-
private externalLogger?: (logLine: LogLine) => void;
326+
// We want external logger to accept async functions
327+
private externalLogger?: (logLine: LogLine) => void | Promise<void>;
323328
private browserbaseSessionCreateParams?: Browserbase.Sessions.SessionCreateParams;
324329
public variables: { [key: string]: unknown };
325330
private contextPath?: string;
@@ -348,7 +353,7 @@ export class Stagehand {
348353
env: "BROWSERBASE",
349354
},
350355
) {
351-
this.externalLogger = logger;
356+
this.externalLogger = logger || defaultLogger;
352357
this.internalLogger = this.log.bind(this);
353358
this.enableCaching =
354359
enableCaching ??
@@ -372,10 +377,7 @@ export class Stagehand {
372377

373378
public get logger(): (logLine: LogLine) => void {
374379
return (logLine: LogLine) => {
375-
this.internalLogger(logLine);
376-
if (this.externalLogger) {
377-
this.externalLogger(logLine);
378-
}
380+
this.log(logLine);
379381
};
380382
}
381383

@@ -504,9 +506,6 @@ export class Stagehand {
504506
// Normal Logging
505507
if (this.externalLogger) {
506508
this.externalLogger(logObj);
507-
} else {
508-
const logMessage = logLineToString(logObj);
509-
console.log(logMessage);
510509
}
511510

512511
// Add the logs to the browserbase session
@@ -613,3 +612,4 @@ export * from "../types/log";
613612
export * from "../types/model";
614613
export * from "../types/playwright";
615614
export * from "../types/stagehand";
615+
export * from "../types/page";

Diff for: types/page.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Page as PlaywrightPage } from "@playwright/test";
2+
import type { BrowserContext as PlaywrightContext } from "@playwright/test";
23
import type { ActResult } from "./act";
34
import type {
45
ActOptions,
@@ -15,3 +16,6 @@ export interface Page extends PlaywrightPage {
1516
) => Promise<ExtractResult<T>>;
1617
observe: (options?: ObserveOptions) => Promise<ObserveResult[]>;
1718
}
19+
20+
// Empty type for now, but will be used in the future
21+
export type BrowserContext = PlaywrightContext;

Diff for: types/stagehand.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ConstructorParams {
1313
debugDom?: boolean;
1414
llmProvider?: LLMProvider;
1515
headless?: boolean;
16-
logger?: (message: LogLine) => void;
16+
logger?: (message: LogLine) => void | Promise<void>;
1717
domSettleTimeoutMs?: number;
1818
browserbaseSessionCreateParams?: Browserbase.Sessions.SessionCreateParams;
1919
enableCaching?: boolean;

0 commit comments

Comments
 (0)