Skip to content

Commit 6758f06

Browse files
committed
fix issues
1 parent 4751c0f commit 6758f06

File tree

2 files changed

+63
-135
lines changed

2 files changed

+63
-135
lines changed

browserbase/src/tools/context.ts

Lines changed: 62 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,16 @@ async function handleCreateContext(
2828
context: Context,
2929
params: CreateContextInput
3030
): Promise<ToolResult> {
31-
const action = async (): Promise<ToolActionResult> => {
32-
try {
33-
const config = context.config;
34-
35-
if (!config.browserbaseApiKey || !config.browserbaseProjectId) {
36-
throw new Error("Browserbase API Key or Project ID is missing in the configuration");
37-
}
38-
39-
const bb = new Browserbase({
40-
apiKey: config.browserbaseApiKey,
41-
});
42-
43-
console.error("Creating new Browserbase context");
44-
const bbContext = await bb.contexts.create({
45-
projectId: config.browserbaseProjectId,
46-
});
47-
48-
console.error(`Successfully created context: ${bbContext.id}`);
49-
50-
// Store context ID with optional name if provided
51-
const contextName = params.name || bbContext.id;
52-
contexts.set(contextName, bbContext.id);
53-
54-
return {
55-
content: [
56-
{
57-
type: "text",
58-
text: `Created new Browserbase context with ID: ${bbContext.id}${params.name ? ` and name: ${params.name}` : ''}`,
59-
},
60-
],
61-
};
62-
} catch (error: any) {
63-
console.error(`CreateContext handle failed: ${error.message || error}`);
64-
throw new Error(`Failed to create Browserbase context: ${error.message || error}`);
31+
try {
32+
const config = context.config;
33+
34+
if (!config.browserbaseApiKey || !config.browserbaseProjectId) {
35+
throw new Error("Browserbase API Key or Project ID is missing in the configuration");
6536
}
66-
};
37+
38+
const bb = new Browserbase({
39+
apiKey: config.browserbaseApiKey,
40+
});
6741

6842
console.error("Creating new Browserbase context");
6943
const bbContext = await bb.contexts.create({
@@ -124,70 +98,64 @@ async function handleDeleteContext(
12498
context: Context,
12599
params: DeleteContextInput
126100
): Promise<ToolResult> {
127-
const action = async (): Promise<ToolActionResult> => {
128-
try {
129-
const config = context.config;
130-
131-
if (!config.browserbaseApiKey) {
132-
throw new Error("Browserbase API Key is missing in the configuration");
133-
}
134-
135-
if (!params.contextId && !params.name) {
136-
throw new Error("Missing required argument: either contextId or name must be provided");
137-
}
101+
try {
102+
const config = context.config;
103+
104+
if (!config.browserbaseApiKey) {
105+
throw new Error("Browserbase API Key is missing in the configuration");
106+
}
107+
108+
if (!params.contextId && !params.name) {
109+
throw new Error("Missing required argument: either contextId or name must be provided");
110+
}
138111

139-
// Resolve context ID either directly or by name
140-
let contextId = params.contextId;
141-
if (!contextId && params.name) {
142-
contextId = contexts.get(params.name);
143-
if (!contextId) {
144-
throw new Error(`Context with name "${params.name}" not found`);
145-
}
112+
// Resolve context ID either directly or by name
113+
let contextId = params.contextId;
114+
if (!contextId && params.name) {
115+
contextId = contexts.get(params.name);
116+
if (!contextId) {
117+
throw new Error(`Context with name "${params.name}" not found`);
146118
}
119+
}
147120

148-
console.error(`Deleting Browserbase context: ${contextId}`);
149-
150-
// Delete from Browserbase API
151-
// The SDK may not have a delete method directly, so we use the REST API
152-
const response = await fetch(`https://api.browserbase.com/v1/contexts/${contextId}`, {
153-
method: 'DELETE',
154-
headers: {
155-
'X-BB-API-Key': config.browserbaseApiKey,
156-
},
157-
});
158-
159-
if (response.status !== 204) {
160-
const errorText = await response.text();
161-
throw new Error(`Failed to delete context with status ${response.status}: ${errorText}`);
162-
}
163-
164-
// Remove from local store
165-
if (params.name) {
166-
contexts.delete(params.name);
167-
}
168-
169-
// Delete by ID too (in case it was stored multiple ways)
170-
for (const [name, id] of contexts.entries()) {
171-
if (id === contextId) {
172-
contexts.delete(name);
173-
}
121+
console.error(`Deleting Browserbase context: ${contextId}`);
122+
123+
// Delete from Browserbase API
124+
// The SDK may not have a delete method directly, so we use the REST API
125+
const response = await fetch(`https://api.browserbase.com/v1/contexts/${contextId}`, {
126+
method: 'DELETE',
127+
headers: {
128+
'X-BB-API-Key': config.browserbaseApiKey,
129+
},
130+
});
131+
132+
if (response.status !== 204) {
133+
const errorText = await response.text();
134+
throw new Error(`Failed to delete context with status ${response.status}: ${errorText}`);
135+
}
136+
137+
// Remove from local store
138+
if (params.name) {
139+
contexts.delete(params.name);
140+
}
141+
142+
// Delete by ID too (in case it was stored multiple ways)
143+
for (const [name, id] of contexts.entries()) {
144+
if (id === contextId) {
145+
contexts.delete(name);
174146
}
175-
176-
console.error(`Successfully deleted context: ${contextId}`);
177-
178-
return {
179-
content: [
180-
{
181-
type: "text",
182-
text: `Deleted Browserbase context with ID: ${contextId}`,
183-
},
184-
],
185-
};
186-
} catch (error: any) {
187-
console.error(`DeleteContext handle failed: ${error.message || error}`);
188-
throw new Error(`Failed to delete Browserbase context: ${error.message || error}`);
189147
}
190-
};
148+
149+
console.error(`Successfully deleted context: ${contextId}`);
150+
151+
const result: ToolActionResult = {
152+
content: [
153+
{
154+
type: "text",
155+
text: `Deleted Browserbase context with ID: ${contextId}`,
156+
},
157+
],
158+
};
191159

192160
return {
193161
resultOverride: result,

browserbase/src/tools/session.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -95,53 +95,13 @@ async function handleCreateSession(
9595
}
9696
};
9797

98-
<<<<<<< HEAD
9998
// Return the ToolResult structure expected by Context.run
10099
return {
101100
action: action,
102101
captureSnapshot: false,
103102
code: [],
104103
waitForNetwork: false,
105104
};
106-
=======
107-
108-
// Check if session creation/retrieval was successful
109-
if (!session || !session.browser || !session.page) {
110-
throw new Error(`SessionManager failed to return a valid session object for ID: ${targetSessionId}`);
111-
}
112-
113-
// Update context's current session ID to the one we targeted
114-
context.currentSessionId = targetSessionId;
115-
// console.error(`Successfully ensured session and set active ID: ${targetSessionId}`); // CHANGED to console.log or removed. Let's remove for now.
116-
117-
// Prepare the result
118-
const result: ToolActionResult = {
119-
content: [
120-
{
121-
type: "text",
122-
text: `Created and set active Browserbase session ID: ${targetSessionId}`,
123-
},
124-
],
125-
};
126-
127-
return {
128-
resultOverride: result,
129-
action: async () => {
130-
console.error("Create Session action");
131-
return result;
132-
},
133-
code: [],
134-
captureSnapshot: false, // No page state change yet
135-
waitForNetwork: false,
136-
};
137-
} catch (error: any) {
138-
console.error(`CreateSession handle failed: ${error.message || error}`);
139-
// Re-throw the error so the main run function in context.ts can handle it
140-
throw new Error(
141-
`Failed to create Browserbase session: ${error.message || error}`
142-
);
143-
}
144-
>>>>>>> d2b3800 (fixing flags + adding to readme + config add cookies support)
145105
}
146106

147107
// Define tool using handle
@@ -274,4 +234,4 @@ const closeSessionTool: Tool<typeof CloseSessionInputSchema> = {
274234
};
275235

276236
// Export an array of the tool objects as default
277-
export default [createSessionTool, closeSessionTool];
237+
export default [createSessionTool, closeSessionTool];

0 commit comments

Comments
 (0)