@@ -13,7 +13,6 @@ import {
13
13
} from "./logging.js" ;
14
14
import {
15
15
getSession ,
16
- getSessionReadOnly ,
17
16
defaultSessionId ,
18
17
type BrowserSession
19
18
} from "./sessionManager.js" ;
@@ -84,7 +83,6 @@ export class Context {
84
83
this . stagehands . delete ( sessionId ) ;
85
84
}
86
85
}
87
-
88
86
public async getActivePage ( ) : Promise < BrowserSession [ "page" ] | null > {
89
87
// Try to get page from Stagehand first (if available for this session)
90
88
const stagehand = this . stagehands . get ( this . currentSessionId ) ;
@@ -94,79 +92,21 @@ export class Context {
94
92
95
93
// Fallback to session manager
96
94
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 ;
138
97
}
139
- return session . browser ;
98
+
99
+ return null ;
140
100
}
141
101
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 ) ;
150
104
if ( ! session || ! session . browser || ! session . browser . isConnected ( ) ) {
151
105
return null ;
152
106
}
153
107
return session . browser ;
154
108
}
155
109
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
-
170
110
async run ( tool : any , args : any ) : Promise < CallToolResult > {
171
111
try {
172
112
log ( `Executing tool: ${ tool . schema . name } with args: ${ JSON . stringify ( args ) } ` , 'info' ) ;
@@ -203,6 +143,10 @@ export class Context {
203
143
}
204
144
}
205
145
146
+ /**
147
+ * List resources
148
+ * Documentation: https://modelcontextprotocol.io/docs/concepts/resources
149
+ */
206
150
listResources ( ) {
207
151
return listResources ( ) ;
208
152
}
0 commit comments