@browserbasehq/sdk 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@browserbasehq/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Browserbase JS SDK",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "tsc -p tsconfig.json",
8
9
  "test": "tsx --test test/*"
package/src/index.ts CHANGED
@@ -45,6 +45,10 @@ export type CreateSessionOptions = {
45
45
  width?: number
46
46
  height?: number
47
47
  }
48
+ context?: {
49
+ id: string;
50
+ persist: boolean;
51
+ };
48
52
  }
49
53
  keepAlive?: boolean
50
54
  // duration in seconds. Minimum 60 (1 minute), maximum 21600 (6 hours)
@@ -370,6 +374,24 @@ export default class Browserbase {
370
374
  await browser.close()
371
375
  return screenshot
372
376
  }
377
+
378
+ async createContext(): Promise<{ id: string }> {
379
+ const response = await fetch(`${this.baseAPIURL}/v1/contexts`, {
380
+ method: 'POST',
381
+ headers: {
382
+ 'x-bb-api-key': this.apiKey,
383
+ 'Content-Type': 'application/json',
384
+ },
385
+ body: JSON.stringify({ projectId: this.projectId }),
386
+ });
387
+
388
+ if (!response.ok) {
389
+ throw new Error('Failed to create context');
390
+ }
391
+
392
+ const data = await response.json();
393
+ return data;
394
+ }
373
395
  }
374
396
 
375
397
  export { Browserbase, BrowserbaseAISDK }
package/test/lib.test.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { describe, it, beforeEach } from 'node:test'
2
2
  import { expect } from 'chai'
3
- import { Browserbase, BrowserbaseAISDK } from '../src'
3
+ import { Browserbase, BrowserbaseAISDK, CreateSessionOptions } from '../src'
4
4
 
5
5
  describe('Browserbase', () => {
6
6
  let browserbase: Browserbase
@@ -73,4 +73,21 @@ describe('Browserbase', () => {
73
73
  const { page } = await ai.execute({ url: 'https://example.com' })
74
74
  expect(page).contain('Example Domain')
75
75
  })
76
+
77
+ it('should create a session with dynamically created context', async () => {
78
+ const createContextResponse = await browserbase.createContext();
79
+ const contextId = createContextResponse.id;
80
+
81
+ // Use the created context ID in session creation
82
+ const sessionOptions: CreateSessionOptions = {
83
+ browserSettings: {
84
+ context: {
85
+ id: contextId,
86
+ persist: true
87
+ }
88
+ }
89
+ };
90
+ const { id } = await browserbase.createSession(sessionOptions);
91
+ const session = await browserbase.getSession(id);
92
+ })
76
93
  })