-
Notifications
You must be signed in to change notification settings - Fork 51
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Refactoring Plan: Merge SessionManager into SessionTracker
Current Structure Analysis
We currently have two classes handling browser session management with overlapping responsibilities:
SessionTracker
- Located in:
packages/agent/src/tools/session/SessionTracker.ts
- Primary responsibility: Tracking browser session metadata and status
- Maintains a registry of browser sessions with their status (running, completed, error, terminated)
- Does not directly handle browser instances but calls SessionManager for cleanup
- Used by the browser tools (sessionStart, sessionMessage, listSessions)
SessionManager
- Located in:
packages/agent/src/tools/session/lib/SessionManager.ts
- Primary responsibility: Creating and managing browser sessions
- Handles the actual browser instances and pages
- Manages browser cleanup and shutdown
- Is stored globally via
__BROWSER_MANAGER__
- Used by SessionTracker for cleanup operations
Problem Statement
Having two separate classes for session management creates unnecessary complexity:
- Duplicated session tracking (both classes maintain their own maps of sessions)
- Shared responsibilities across different files
- Indirect communication between components
- Global state management via
__BROWSER_MANAGER__
andbrowserSessions
Refactoring Goals
- Merge SessionManager functionality into SessionTracker
- Eliminate global state in favor of a singleton pattern
- Simplify the API for browser session management
- Maintain backward compatibility with existing tools
- Improve error handling and cleanup processes
Implementation Plan
Step 1: Enhance SessionTracker with SessionManager Capabilities
-
Update
SessionInfo
interface to include browser and page objects:export interface SessionInfo { id: string; status: SessionStatus; startTime: Date; endTime?: Date; browser?: Browser; page?: Page; metadata: { url?: string; contentLength?: number; closedExplicitly?: boolean; error?: string; actionType?: string; }; }
-
Add browser creation and management methods to SessionTracker:
createSession(config?: BrowserConfig): Promise<SessionInfo>
closeSession(sessionId: string): Promise<void>
closeAllSessions(): Promise<void>
-
Implement browser setup and cleanup handlers in SessionTracker
Step 2: Implement Singleton Pattern for SessionTracker
- Convert SessionTracker to use a singleton pattern similar to BrowserAutomation
- Provide a static getInstance() method
- Maintain backward compatibility with constructor-based instantiation
Step 3: Update Tool Implementation
- Update
sessionStart.ts
to use SessionTracker directly instead of browserSessions - Update
sessionMessage.ts
to use SessionTracker for all browser operations - Update
listSessions.ts
to use the enhanced SessionTracker capabilities
Step 4: Cleanup and Deprecation
- Mark SessionManager as deprecated
- Remove global
__BROWSER_MANAGER__
reference - Remove or deprecate the global
browserSessions
map - Update documentation to reflect the new architecture
Migration Strategy
- Implement the enhanced SessionTracker without removing existing code
- Update tools to use the new API while maintaining compatibility with old code
- Add deprecation warnings for old methods and classes
- After confirming functionality, remove deprecated code in a future release
Testing Strategy
- Unit tests for the enhanced SessionTracker
- Integration tests for browser session creation and management
- End-to-end tests for the browser tools
- Stress tests for cleanup and error handling
Timeline
- Implementation: 1-2 days
- Testing: 1 day
- Code review: 1 day
- Documentation: 0.5 day
Related Components
BrowserAutomation.ts
- Will need to be updated to use the enhanced SessionTrackerPageController.ts
- May need adjustments based on the new structure
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request