-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathsession.ts
More file actions
executable file
·226 lines (201 loc) · 8.17 KB
/
session.ts
File metadata and controls
executable file
·226 lines (201 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { z } from "zod";
import type { Tool, ToolSchema, ToolResult } from "./tool.js";
import type { Context } from "../context.js";
import type { ToolActionResult } from "../types/types.js";
import { Browserbase } from "@browserbasehq/sdk";
import { createUIResource } from "@mcp-ui/server";
import type { BrowserSession } from "../types/types.js";
import { TextContent } from "@modelcontextprotocol/sdk/types.js";
// --- Tool: Create Session ---
const CreateSessionInputSchema = z.object({
// Keep sessionId optional
sessionId: z
.string()
.optional()
.describe(
"Optional session ID to use/reuse. If not provided or invalid, a new session is created.",
),
});
type CreateSessionInput = z.infer<typeof CreateSessionInputSchema>;
const createSessionSchema: ToolSchema<typeof CreateSessionInputSchema> = {
name: "browserbase_session_create",
description:
"Create or reuse a Browserbase browser session and set it as active.",
inputSchema: CreateSessionInputSchema,
};
// Handle function for CreateSession using SessionManager
async function handleCreateSession(
context: Context,
params: CreateSessionInput,
): Promise<ToolResult> {
const action = async (): Promise<ToolActionResult> => {
try {
const sessionManager = context.getSessionManager();
const config = context.config; // Get config from context
let targetSessionId: string;
// Session ID Strategy: Use raw sessionId for both internal tracking and Browserbase operations
// Default session uses generated ID with timestamp/UUID, user sessions use provided ID as-is
if (params.sessionId) {
targetSessionId = params.sessionId;
process.stderr.write(
`[tool.createSession] Attempting to create/assign session with specified ID: ${targetSessionId}\n`,
);
} else {
targetSessionId = sessionManager.getDefaultSessionId();
}
let session: BrowserSession;
const defaultSessionId = sessionManager.getDefaultSessionId();
if (targetSessionId === defaultSessionId) {
session = await sessionManager.ensureDefaultSessionInternal(config);
} else {
// When user provides a sessionId, we want to resume that Browserbase session
// Note: targetSessionId is used for internal tracking in SessionManager
// while params.sessionId is the Browserbase session ID to resume
session = await sessionManager.createNewBrowserSession(
targetSessionId, // Internal session ID for tracking
config,
params.sessionId, // Browserbase session ID to resume
);
}
if (
!session ||
!session.page ||
!session.sessionId ||
!session.stagehand
) {
throw new Error(
`SessionManager failed to return a valid session object with actualSessionId for ID: ${targetSessionId}`,
);
}
// Note: No need to set context.currentSessionId - SessionManager handles this
// and context.currentSessionId is a getter that delegates to SessionManager
const bb = new Browserbase({
apiKey: config.browserbaseApiKey,
});
const browserbaseSessionId = session.stagehand.browserbaseSessionId;
if (!browserbaseSessionId) {
throw new Error(
"Browserbase session ID not found in Stagehand instance",
);
}
const debugUrl = (await bb.sessions.debug(browserbaseSessionId))
.debuggerFullscreenUrl;
return {
content: [
{
type: "text",
text: `Browserbase Live Session View URL: https://www.browserbase.com/sessions/${browserbaseSessionId}`,
},
{
type: "text",
text: `Browserbase Live Debugger URL: ${debugUrl}`,
},
createUIResource({
uri: "ui://analytics-dashboard/main",
content: { type: "externalUrl", iframeUrl: debugUrl },
encoding: "text",
}) as unknown as TextContent,
],
};
} catch (error: unknown) {
const errorMessage =
error instanceof Error ? error.message : String(error);
process.stderr.write(
`[tool.createSession] Action failed: ${errorMessage}\n`,
);
// Re-throw to be caught by Context.run's error handling for actions
throw new Error(`Failed to create Browserbase session: ${errorMessage}`);
}
};
// Return the ToolResult structure expected by Context.run
return {
action: action,
waitForNetwork: false,
};
}
// Define tool using handle
const createSessionTool: Tool<typeof CreateSessionInputSchema> = {
capability: "core", // Add capability
schema: createSessionSchema,
handle: handleCreateSession,
};
// --- Tool: Close Session ---
const CloseSessionInputSchema = z.object({});
const closeSessionSchema: ToolSchema<typeof CloseSessionInputSchema> = {
name: "browserbase_session_close",
description:
"Close the current Browserbase session and reset the active context.",
inputSchema: CloseSessionInputSchema,
};
async function handleCloseSession(context: Context): Promise<ToolResult> {
const action = async (): Promise<ToolActionResult> => {
// Store the current session ID before cleanup
const previousSessionId = context.currentSessionId;
let cleanupSuccessful = false;
let cleanupErrorMessage = "";
// Step 1: Get session info before cleanup
let browserbaseSessionId: string | undefined;
const sessionManager = context.getSessionManager();
try {
const session = await sessionManager.getSession(
previousSessionId,
context.config,
false,
);
if (session && session.stagehand) {
// Store the actual Browserbase session ID for the replay URL
browserbaseSessionId = session.sessionId;
// cleanupSession handles both closing Stagehand and cleanup (idempotent)
await sessionManager.cleanupSession(previousSessionId);
cleanupSuccessful = true;
} else {
process.stderr.write(
`[tool.closeSession] No session found for ID: ${previousSessionId || "default/unknown"}\n`,
);
}
} catch (error: unknown) {
cleanupErrorMessage =
error instanceof Error ? error.message : String(error);
process.stderr.write(
`[tool.closeSession] Error cleaning up session (ID was ${previousSessionId || "default/unknown"}): ${cleanupErrorMessage}\n`,
);
}
// Step 2: SessionManager automatically resets to default on cleanup
// Context.currentSessionId getter will reflect the new active session
const oldContextSessionId = previousSessionId;
process.stderr.write(
`[tool.closeSession] Session context reset to default. Previous context session ID was ${oldContextSessionId || "default/unknown"}.\n`,
);
// Step 3: Determine the result message
const defaultSessionId = sessionManager.getDefaultSessionId();
if (cleanupErrorMessage && !cleanupSuccessful) {
throw new Error(
`Failed to cleanup session (session ID was ${previousSessionId || "default/unknown"}). Error: ${cleanupErrorMessage}. Session context has been reset to default.`,
);
}
if (cleanupSuccessful) {
let successMessage = `Browserbase session (${previousSessionId || "default"}) closed successfully. Context reset to default.`;
if (browserbaseSessionId && previousSessionId !== defaultSessionId) {
successMessage += ` View replay at https://www.browserbase.com/sessions/${browserbaseSessionId}`;
}
return { content: [{ type: "text", text: successMessage }] };
}
// No session was found
let infoMessage =
"No active session found to close. Session context has been reset to default.";
if (previousSessionId && previousSessionId !== defaultSessionId) {
infoMessage = `No active session found for session ID '${previousSessionId}'. The context has been reset to default.`;
}
return { content: [{ type: "text", text: infoMessage }] };
};
return {
action: action,
waitForNetwork: false,
};
}
const closeSessionTool: Tool<typeof CloseSessionInputSchema> = {
capability: "core",
schema: closeSessionSchema,
handle: handleCloseSession,
};
export default [createSessionTool, closeSessionTool];