File tree 3 files changed +24
-4
lines changed
3 files changed +24
-4
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @browserbasehq/stagehand " : patch
3
+ ---
4
+
5
+ handle attempt to close session that has already been closed when using the api
Original file line number Diff line number Diff line change @@ -112,9 +112,10 @@ export class StagehandAPI {
112
112
} ) ;
113
113
}
114
114
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" ,
118
119
} ) ;
119
120
}
120
121
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ import { scriptContent } from "./dom/build/scriptContent";
32
32
import { LLMClient } from "./llm/LLMClient" ;
33
33
import { LLMProvider } from "./llm/LLMProvider" ;
34
34
import { logLineToString , isRunningInBun } from "./utils" ;
35
+ import { ApiResponse , ErrorResponse } from "@/types/api" ;
35
36
36
37
dotenv . config ( { path : ".env" } ) ;
37
38
@@ -711,7 +712,20 @@ export class Stagehand {
711
712
712
713
async close ( ) : Promise < void > {
713
714
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
+ }
715
729
return ;
716
730
} else {
717
731
await this . context . close ( ) ;
You can’t perform that action at this time.
0 commit comments