Skip to content

Commit c820bfc

Browse files
handle close session (#528)
1 parent 44cf7cc commit c820bfc

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Diff for: .changeset/pretty-parrots-wave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
handle attempt to close session that has already been closed when using the api

Diff for: lib/api.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ export class StagehandAPI {
112112
});
113113
}
114114

115-
async end(): Promise<void> {
116-
return this.execute<void>({
117-
method: "end",
115+
async end(): Promise<Response> {
116+
const url = `/sessions/${this.sessionId}/end`;
117+
return await this.request(url, {
118+
method: "POST",
118119
});
119120
}
120121

Diff for: lib/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { scriptContent } from "./dom/build/scriptContent";
3232
import { LLMClient } from "./llm/LLMClient";
3333
import { LLMProvider } from "./llm/LLMProvider";
3434
import { logLineToString, isRunningInBun } from "./utils";
35+
import { ApiResponse, ErrorResponse } from "@/types/api";
3536

3637
dotenv.config({ path: ".env" });
3738

@@ -711,7 +712,20 @@ export class Stagehand {
711712

712713
async close(): Promise<void> {
713714
if (this.apiClient) {
714-
await this.apiClient.end();
715+
const response = await this.apiClient.end();
716+
const body: ApiResponse<unknown> = await response.json();
717+
if (!body.success) {
718+
if (response.status == 409) {
719+
this.log({
720+
category: "close",
721+
message:
722+
"Warning: attempted to end a session that is not currently active",
723+
level: 0,
724+
});
725+
} else {
726+
throw new Error((body as ErrorResponse).message);
727+
}
728+
}
715729
return;
716730
} else {
717731
await this.context.close();

0 commit comments

Comments
 (0)