Skip to content

Commit 2ef8a06

Browse files
authored
Merge pull request #61 from browserbase/fm/stg-397-improve-context-cookies-handling
consistent return action
2 parents 349f38c + b81a093 commit 2ef8a06

File tree

1 file changed

+108
-104
lines changed

1 file changed

+108
-104
lines changed

browserbase/src/tools/context.ts

Lines changed: 108 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,49 @@ async function handleCreateContext(
2828
context: Context,
2929
params: CreateContextInput
3030
): Promise<ToolResult> {
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");
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}`);
3665
}
37-
38-
const bb = new Browserbase({
39-
apiKey: config.browserbaseApiKey,
40-
});
41-
42-
console.error("Creating new Browserbase context");
43-
const bbContext = await bb.contexts.create({
44-
projectId: config.browserbaseProjectId,
45-
});
46-
47-
console.error(`Successfully created context: ${bbContext.id}`);
48-
49-
// Store context ID with optional name if provided
50-
const contextName = params.name || bbContext.id;
51-
contexts.set(contextName, bbContext.id);
52-
53-
const result: ToolActionResult = {
54-
content: [
55-
{
56-
type: "text",
57-
text: `Created new Browserbase context with ID: ${bbContext.id}${params.name ? ` and name: ${params.name}` : ''}`,
58-
},
59-
],
60-
};
61-
62-
return {
63-
resultOverride: result,
64-
code: [],
65-
captureSnapshot: false,
66-
waitForNetwork: false,
67-
};
68-
} catch (error: any) {
69-
console.error(`CreateContext handle failed: ${error.message || error}`);
70-
throw new Error(`Failed to create Browserbase context: ${error.message || error}`);
71-
}
66+
};
67+
68+
return {
69+
action,
70+
code: [],
71+
captureSnapshot: false,
72+
waitForNetwork: false,
73+
};
7274
}
7375

7476
// --- Tool: Delete Context ---
@@ -94,75 +96,77 @@ async function handleDeleteContext(
9496
context: Context,
9597
params: DeleteContextInput
9698
): Promise<ToolResult> {
97-
try {
98-
const config = context.config;
99-
100-
if (!config.browserbaseApiKey) {
101-
throw new Error("Browserbase API Key is missing in the configuration");
102-
}
103-
104-
if (!params.contextId && !params.name) {
105-
throw new Error("Missing required argument: either contextId or name must be provided");
106-
}
99+
const action = async (): Promise<ToolActionResult> => {
100+
try {
101+
const config = context.config;
102+
103+
if (!config.browserbaseApiKey) {
104+
throw new Error("Browserbase API Key is missing in the configuration");
105+
}
106+
107+
if (!params.contextId && !params.name) {
108+
throw new Error("Missing required argument: either contextId or name must be provided");
109+
}
107110

108-
// Resolve context ID either directly or by name
109-
let contextId = params.contextId;
110-
if (!contextId && params.name) {
111-
contextId = contexts.get(params.name);
112-
if (!contextId) {
113-
throw new Error(`Context with name "${params.name}" not found`);
111+
// Resolve context ID either directly or by name
112+
let contextId = params.contextId;
113+
if (!contextId && params.name) {
114+
contextId = contexts.get(params.name);
115+
if (!contextId) {
116+
throw new Error(`Context with name "${params.name}" not found`);
117+
}
114118
}
115-
}
116119

117-
console.error(`Deleting Browserbase context: ${contextId}`);
118-
119-
// Delete from Browserbase API
120-
// The SDK may not have a delete method directly, so we use the REST API
121-
const response = await fetch(`https://api.browserbase.com/v1/contexts/${contextId}`, {
122-
method: 'DELETE',
123-
headers: {
124-
'X-BB-API-Key': config.browserbaseApiKey,
125-
},
126-
});
127-
128-
if (response.status !== 204) {
129-
const errorText = await response.text();
130-
throw new Error(`Failed to delete context with status ${response.status}: ${errorText}`);
131-
}
132-
133-
// Remove from local store
134-
if (params.name) {
135-
contexts.delete(params.name);
136-
}
137-
138-
// Delete by ID too (in case it was stored multiple ways)
139-
for (const [name, id] of contexts.entries()) {
140-
if (id === contextId) {
141-
contexts.delete(name);
120+
console.error(`Deleting Browserbase context: ${contextId}`);
121+
122+
// Delete from Browserbase API
123+
// The SDK may not have a delete method directly, so we use the REST API
124+
const response = await fetch(`https://api.browserbase.com/v1/contexts/${contextId}`, {
125+
method: 'DELETE',
126+
headers: {
127+
'X-BB-API-Key': config.browserbaseApiKey,
128+
},
129+
});
130+
131+
if (response.status !== 204) {
132+
const errorText = await response.text();
133+
throw new Error(`Failed to delete context with status ${response.status}: ${errorText}`);
134+
}
135+
136+
// Remove from local store
137+
if (params.name) {
138+
contexts.delete(params.name);
142139
}
140+
141+
// Delete by ID too (in case it was stored multiple ways)
142+
for (const [name, id] of contexts.entries()) {
143+
if (id === contextId) {
144+
contexts.delete(name);
145+
}
146+
}
147+
148+
console.error(`Successfully deleted context: ${contextId}`);
149+
150+
return {
151+
content: [
152+
{
153+
type: "text",
154+
text: `Deleted Browserbase context with ID: ${contextId}`,
155+
},
156+
],
157+
};
158+
} catch (error: any) {
159+
console.error(`DeleteContext handle failed: ${error.message || error}`);
160+
throw new Error(`Failed to delete Browserbase context: ${error.message || error}`);
143161
}
144-
145-
console.error(`Successfully deleted context: ${contextId}`);
146-
147-
const result: ToolActionResult = {
148-
content: [
149-
{
150-
type: "text",
151-
text: `Deleted Browserbase context with ID: ${contextId}`,
152-
},
153-
],
154-
};
155-
156-
return {
157-
resultOverride: result,
158-
code: [],
159-
captureSnapshot: false,
160-
waitForNetwork: false,
161-
};
162-
} catch (error: any) {
163-
console.error(`DeleteContext handle failed: ${error.message || error}`);
164-
throw new Error(`Failed to delete Browserbase context: ${error.message || error}`);
165-
}
162+
};
163+
164+
return {
165+
action,
166+
code: [],
167+
captureSnapshot: false,
168+
waitForNetwork: false,
169+
};
166170
}
167171

168172
// Helper function to get a context ID from name or direct ID (exported for use by session.ts)

0 commit comments

Comments
 (0)