Skip to content

Commit 598cae2

Browse files
authored
auto delete ctx (#253)
* cleanup function * changeset * update readme with close * close() return type * update changeset
1 parent a855e1f commit 598cae2

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

.changeset/fuzzy-chairs-boil.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
clean up contexts after use

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const contributor = await stagehand.extract({
118118
url: z.string(),
119119
}),
120120
});
121+
await stagehand.close();
121122
console.log(`Our favorite contributor is ${contributor.username}`);
122123
```
123124

@@ -297,6 +298,15 @@ If you are looking for a specific element, you can also pass in an instruction t
297298
const actions = await stagehand.observe();
298299
```
299300

301+
#### `close()`
302+
303+
`close()` is a cleanup method to remove the temporary files created by Stagehand. It's highly recommended that you call this when you're done with your automation.
304+
305+
- **Example:**
306+
```javascript
307+
await stagehand.close();
308+
```
309+
300310
#### `page` and `context`
301311

302312
`page` and `context` are instances of Playwright's `Page` and `BrowserContext` respectively. Use these methods to interact with the Playwright instance that Stagehand is using. Most commonly, you'll use `page.goto()` to navigate to a URL.

examples/example.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ async function example() {
1919
url: z.string(),
2020
}),
2121
});
22+
2223
console.log(`Our favorite contributor is ${contributor.username}`);
24+
25+
await stagehand.close();
2326
}
2427

2528
(async () => {

lib/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ async function getBrowser(
198198
},
199199
});
200200

201-
const tmpDirPath = path.join(os.tmpdir(), "ctx_");
201+
const tmpDirPath = path.join(os.tmpdir(), "stagehand");
202202
if (!fs.existsSync(tmpDirPath)) {
203203
fs.mkdirSync(tmpDirPath, { recursive: true });
204204
}
@@ -250,7 +250,7 @@ async function getBrowser(
250250

251251
await applyStealthScripts(context);
252252

253-
return { context };
253+
return { context, contextPath: tmpDir };
254254
}
255255
}
256256

@@ -309,6 +309,7 @@ export class Stagehand {
309309
private enableCaching: boolean;
310310
private variables: { [key: string]: any };
311311
private browserbaseResumeSessionID?: string;
312+
private contextPath?: string;
312313

313314
private actHandler?: StagehandActHandler;
314315
private extractHandler?: StagehandExtractHandler;
@@ -364,7 +365,7 @@ export class Stagehand {
364365
const llmClient = modelName
365366
? this.llmProvider.getClient(modelName, modelClientOptions)
366367
: this.llmClient;
367-
const { context, debugUrl, sessionUrl } = await getBrowser(
368+
const { context, debugUrl, sessionUrl, contextPath } = await getBrowser(
368369
this.apiKey,
369370
this.projectId,
370371
this.env,
@@ -380,6 +381,7 @@ export class Stagehand {
380381
sessionUrl: undefined,
381382
} as BrowserResult;
382383
});
384+
this.contextPath = contextPath;
383385
this.context = context;
384386
this.page = context.pages()[0];
385387
// Redundant but needed for users who are re-connecting to a previously-created session
@@ -877,6 +879,18 @@ export class Stagehand {
877879
throw e;
878880
});
879881
}
882+
883+
async close(): Promise<void> {
884+
await this.context.close();
885+
886+
if (this.contextPath) {
887+
try {
888+
fs.rmSync(this.contextPath, { recursive: true, force: true });
889+
} catch (e) {
890+
console.error("Error deleting context directory:", e);
891+
}
892+
}
893+
}
880894
}
881895

882896
export * from "../types/browser";

types/browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export interface BrowserResult {
55
context: BrowserContext;
66
debugUrl?: string;
77
sessionUrl?: string;
8+
contextPath?: string;
89
}

0 commit comments

Comments
 (0)