* Clean up a session by closing the browser and removing it from tracking. * This method handles both closing Stagehand and cleanup, and is idempotent. * * @param sessionId The session ID to clean up
(sessionId: string)
| 416 | * @param sessionId The session ID to clean up |
| 417 | */ |
| 418 | async cleanupSession(sessionId: string): Promise<void> { |
| 419 | process.stderr.write( |
| 420 | `[SessionManager] Cleaning up session: ${sessionId}\n`, |
| 421 | ); |
| 422 | |
| 423 | // Get the session to close it gracefully |
| 424 | const session = this.browsers.get(sessionId); |
| 425 | if (session) { |
| 426 | await this.closeBrowserGracefully(session, sessionId); |
| 427 | } |
| 428 | |
| 429 | // Remove from browsers map |
| 430 | this.browsers.delete(sessionId); |
| 431 | |
| 432 | // Clear default session reference if this was the default |
| 433 | if (sessionId === this.defaultSessionId && this.defaultBrowserSession) { |
| 434 | this.defaultBrowserSession = null; |
| 435 | } |
| 436 | |
| 437 | // Reset active session to default if this was the active one |
| 438 | if (this.activeSessionId === sessionId) { |
| 439 | process.stderr.write( |
| 440 | `[SessionManager] Cleaned up active session ${sessionId}, resetting to default.\n`, |
| 441 | ); |
| 442 | this.setActiveSessionId(this.defaultSessionId); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Function to close all managed browser sessions gracefully |
| 447 | async closeAllSessions(): Promise<void> { |
no test coverage detected