@@ -13,7 +13,6 @@ import {
1313} from "./logging.js" ;
1414import {
1515 getSession ,
16- getSessionReadOnly ,
1716 defaultSessionId ,
1817 type BrowserSession
1918} from "./sessionManager.js" ;
@@ -84,7 +83,6 @@ export class Context {
8483 this . stagehands . delete ( sessionId ) ;
8584 }
8685 }
87-
8886 public async getActivePage ( ) : Promise < BrowserSession [ "page" ] | null > {
8987 // Try to get page from Stagehand first (if available for this session)
9088 const stagehand = this . stagehands . get ( this . currentSessionId ) ;
@@ -94,79 +92,21 @@ export class Context {
9492
9593 // Fallback to session manager
9694 const session = await getSession ( this . currentSessionId , this . config ) ;
97- if ( ! session || ! session . page || session . page . isClosed ( ) ) {
98- try {
99- const currentSession = await getSession (
100- this . currentSessionId ,
101- this . config
102- ) ;
103- if (
104- ! currentSession ||
105- ! currentSession . page ||
106- currentSession . page . isClosed ( )
107- ) {
108- return null ;
109- }
110- return currentSession . page ;
111- } catch ( refreshError ) {
112- return null ;
113- }
114- }
115- return session . page ;
116- }
117-
118- // Will create a new default session if one doesn't exist
119- public async getActiveBrowser ( ) : Promise < BrowserSession [ "browser" ] | null > {
120- const session = await getSession ( this . currentSessionId , this . config ) ;
121- if ( ! session || ! session . browser || ! session . browser . isConnected ( ) ) {
122- try {
123- const currentSession = await getSession (
124- this . currentSessionId ,
125- this . config
126- ) ;
127- if (
128- ! currentSession ||
129- ! currentSession . browser ||
130- ! currentSession . browser . isConnected ( )
131- ) {
132- return null ;
133- }
134- return currentSession . browser ;
135- } catch ( refreshError ) {
136- return null ;
137- }
95+ if ( session && session . page && ! session . page . isClosed ( ) ) {
96+ return session . page ;
13897 }
139- return session . browser ;
98+
99+ return null ;
140100 }
141101
142- /**
143- * Get the active browser without triggering session creation.
144- * This is a read-only operation used when we need to check for an existing browser
145- * without side effects (e.g., during close operations).
146- * @returns The browser if it exists and is connected, null otherwise
147- */
148- public getActiveBrowserReadOnly ( ) : BrowserSession [ "browser" ] | null {
149- const session = getSessionReadOnly ( this . currentSessionId ) ;
102+ public async getActiveBrowser ( createIfMissing : boolean = true ) : Promise < BrowserSession [ "browser" ] | null > {
103+ const session = await getSession ( this . currentSessionId , this . config , createIfMissing ) ;
150104 if ( ! session || ! session . browser || ! session . browser . isConnected ( ) ) {
151105 return null ;
152106 }
153107 return session . browser ;
154108 }
155109
156- /**
157- * Get the active page without triggering session creation.
158- * This is a read-only operation used when we need to check for an existing page
159- * without side effects.
160- * @returns The page if it exists and is not closed, null otherwise
161- */
162- public getActivePageReadOnly ( ) : BrowserSession [ "page" ] | null {
163- const session = getSessionReadOnly ( this . currentSessionId ) ;
164- if ( ! session || ! session . page || session . page . isClosed ( ) ) {
165- return null ;
166- }
167- return session . page ;
168- }
169-
170110 async run ( tool : any , args : any ) : Promise < CallToolResult > {
171111 try {
172112 log ( `Executing tool: ${ tool . schema . name } with args: ${ JSON . stringify ( args ) } ` , 'info' ) ;
@@ -203,6 +143,10 @@ export class Context {
203143 }
204144 }
205145
146+ /**
147+ * List resources
148+ * Documentation: https://modelcontextprotocol.io/docs/concepts/resources
149+ */
206150 listResources ( ) {
207151 return listResources ( ) ;
208152 }
0 commit comments