Skip to content

Commit c4fbbe0

Browse files
committed
refactor session management
1 parent 0201dc4 commit c4fbbe0

File tree

11 files changed

+354
-746
lines changed

11 files changed

+354
-746
lines changed

src/context.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
33
import type { Config } from "../config.d.ts";
44
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { listResources, readResource } from "./mcp/resources.js";
6-
import { getSession, defaultSessionId } from "./sessionManager.js";
7-
import type { MCPTool, BrowserSession } from "./types/types.js";
6+
import { store } from "./tools/helpers/session.js";
7+
import type { MCPTool, StagehandSession } from "./types/types.js";
88

99
export class Context {
1010
public readonly config: Config;
1111
private server: Server;
12-
public currentSessionId: string = defaultSessionId;
12+
private _currentSession: StagehandSession | null = null;
1313

1414
constructor(server: Server, config: Config) {
1515
this.server = server;
@@ -21,40 +21,35 @@ export class Context {
2121
}
2222

2323
/**
24-
* Gets the Stagehand instance for the current session from SessionManager
24+
* Gets the Stagehand instance for the current session
2525
*/
26-
public async getStagehand(
27-
sessionId: string = this.currentSessionId,
28-
): Promise<Stagehand> {
29-
const session = await getSession(sessionId, this.config);
30-
if (!session) {
31-
throw new Error(`No session found for ID: ${sessionId}`);
32-
}
26+
public async getStagehand(): Promise<Stagehand> {
27+
const session = await this.getCurrentSession();
3328
return session.stagehand;
3429
}
3530

36-
public async getActivePage(): Promise<BrowserSession["page"] | null> {
37-
// Get page from session manager
38-
const session = await getSession(this.currentSessionId, this.config);
39-
if (session && session.page && !session.page.isClosed()) {
40-
return session.page;
31+
public async getActivePage(): Promise<StagehandSession["page"] | null> {
32+
try {
33+
const session = await this.getCurrentSession();
34+
if (session.page && !session.page.isClosed()) {
35+
return session.page;
36+
}
37+
} catch {
38+
// Session not available
4139
}
42-
4340
return null;
4441
}
4542

46-
public async getActiveBrowser(
47-
createIfMissing: boolean = true,
48-
): Promise<BrowserSession["browser"] | null> {
49-
const session = await getSession(
50-
this.currentSessionId,
51-
this.config,
52-
createIfMissing,
53-
);
54-
if (!session || !session.browser || !session.browser.isConnected()) {
55-
return null;
43+
public async getActiveBrowser(): Promise<StagehandSession["browser"] | null> {
44+
try {
45+
const session = await this.getCurrentSession();
46+
if (session.browser && session.browser.isConnected()) {
47+
return session.browser;
48+
}
49+
} catch {
50+
// Session not available
5651
}
57-
return session.browser;
52+
return null;
5853
}
5954

6055
async run(tool: MCPTool, args: unknown): Promise<CallToolResult> {
@@ -122,4 +117,15 @@ export class Context {
122117
readResource(uri: string) {
123118
return readResource(uri);
124119
}
120+
121+
/**
122+
* Gets or creates the current session
123+
*/
124+
private async getCurrentSession(): Promise<StagehandSession> {
125+
if (!this._currentSession) {
126+
const sessionStore = store(this.config);
127+
this._currentSession = await sessionStore.create();
128+
}
129+
return this._currentSession;
130+
}
125131
}

src/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath } from "url";
66
import createServerFunction from "./index.js";
77
import { ServerList } from "./server.js";
88
import { startHttpTransport, startStdioTransport } from "./transport.js";
9-
import * as stagehandStore from "./stagehandStore.js";
9+
// Session cleanup will be handled by the Context cleanup
1010

1111
import { resolveConfig } from "./config.js";
1212

@@ -83,7 +83,7 @@ function setupExitWatchdog(serverList: ServerList) {
8383
const handleExit = async () => {
8484
setTimeout(() => process.exit(0), 15000);
8585
try {
86-
await Promise.all([stagehandStore.removeAll(), serverList.closeAll()]);
86+
await serverList.closeAll();
8787
} catch (error) {
8888
console.error("Error during cleanup:", error);
8989
}

0 commit comments

Comments
 (0)