browser-use-sdk 3.3.1 → 3.3.2
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/README.md +12 -193
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/v3.cjs +248 -4
- package/dist/v3.cjs.map +1 -1
- package/dist/v3.d.cts +662 -6
- package/dist/v3.d.ts +662 -6
- package/dist/v3.js +246 -2
- package/dist/v3.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,213 +1,32 @@
|
|
|
1
1
|
# browser-use-sdk
|
|
2
2
|
|
|
3
|
-
Official TypeScript SDK for
|
|
3
|
+
Official TypeScript SDK for [Browser Use Cloud](https://browser-use.com).
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install browser-use-sdk
|
|
9
|
-
# or
|
|
10
|
-
pnpm add browser-use-sdk
|
|
11
|
-
# or
|
|
12
|
-
yarn add browser-use-sdk
|
|
13
|
-
# or
|
|
14
|
-
bun add browser-use-sdk
|
|
15
9
|
```
|
|
16
10
|
|
|
17
|
-
## Quick Start
|
|
11
|
+
## Quick Start
|
|
18
12
|
|
|
19
|
-
|
|
13
|
+
Get your API key at [cloud.browser-use.com/settings](https://cloud.browser-use.com/settings?tab=api-keys&new=1).
|
|
20
14
|
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const client = new BrowserUse();
|
|
25
|
-
|
|
26
|
-
const result = await client.run("Find the top post on Hacker News");
|
|
27
|
-
console.log(result.output);
|
|
28
|
-
console.log(result.id, result.status);
|
|
15
|
+
```bash
|
|
16
|
+
export BROWSER_USE_API_KEY=your_key
|
|
29
17
|
```
|
|
30
18
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
The v3 API uses a unified session model. Import from `browser-use-sdk/v3`:
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import { BrowserUse } from "browser-use-sdk/v3";
|
|
19
|
+
```typescript
|
|
20
|
+
import { BrowserUse } from "browser-use-sdk";
|
|
37
21
|
|
|
38
22
|
const client = new BrowserUse();
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
console.log(output);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## Client Options
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
const client = new BrowserUse({
|
|
48
|
-
apiKey: "bu_...", // Or set BROWSER_USE_API_KEY env var
|
|
49
|
-
baseUrl: "https://...", // Override API base URL
|
|
50
|
-
maxRetries: 3, // Retry on 429 (default: 3)
|
|
51
|
-
timeout: 30_000, // Request timeout in ms (default: 30s)
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
## Structured Output
|
|
56
|
-
|
|
57
|
-
Pass a Zod schema to get typed results:
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
import { z } from "zod";
|
|
61
|
-
|
|
62
|
-
const Product = z.object({ name: z.string(), price: z.number() });
|
|
63
|
-
|
|
64
|
-
const result = await client.run("Find the price of the MacBook Air", { schema: Product });
|
|
65
|
-
console.log(`${result.output.name}: $${result.output.price}`);
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## Polling and Streaming
|
|
69
|
-
|
|
70
|
-
`client.run()` returns a dual-purpose handle: `await` it for the output, or `for await...of` it for step-by-step progress.
|
|
71
|
-
|
|
72
|
-
### Wait for Result
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
const result = await client.run("Search for AI news");
|
|
76
|
-
console.log(result.output, result.id, result.status);
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Stream Steps
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
for await (const step of client.run("Search for AI news")) {
|
|
83
|
-
console.log(`[${step.number}] ${step.nextGoal} — ${step.url}`);
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## V2 API Reference
|
|
88
|
-
|
|
89
|
-
### Billing
|
|
90
|
-
|
|
91
|
-
| Method | Description |
|
|
92
|
-
|-----------------------|--------------------------------------|
|
|
93
|
-
| `billing.account()` | Get account info and credit balance |
|
|
94
|
-
|
|
95
|
-
### Tasks
|
|
96
|
-
|
|
97
|
-
| Method | Description |
|
|
98
|
-
|------------------------------|-----------------------------------------|
|
|
99
|
-
| `tasks.create(body)` | Create and start a new task |
|
|
100
|
-
| `tasks.list(params?)` | List tasks with optional filtering |
|
|
101
|
-
| `tasks.get(taskId)` | Get detailed task information |
|
|
102
|
-
| `tasks.stop(taskId)` | Stop a running task |
|
|
103
|
-
| `tasks.status(taskId)` | Lightweight status for polling |
|
|
104
|
-
| `tasks.wait(taskId, opts?)` | Poll until terminal, return TaskView |
|
|
105
|
-
| `tasks.logs(taskId)` | Get download URL for task logs |
|
|
106
|
-
|
|
107
|
-
### Sessions
|
|
108
|
-
|
|
109
|
-
| Method | Description |
|
|
110
|
-
|------------------------------------|--------------------------------------|
|
|
111
|
-
| `sessions.create(body?)` | Create a new session |
|
|
112
|
-
| `sessions.list(params?)` | List sessions |
|
|
113
|
-
| `sessions.get(sessionId)` | Get session details |
|
|
114
|
-
| `sessions.stop(sessionId)` | Stop session and running tasks |
|
|
115
|
-
| `sessions.delete(sessionId)` | Delete session and all tasks |
|
|
116
|
-
| `sessions.getShare(sessionId)` | Get public share info |
|
|
117
|
-
| `sessions.createShare(sessionId)` | Create public share link |
|
|
118
|
-
| `sessions.deleteShare(sessionId)` | Remove public share |
|
|
119
|
-
|
|
120
|
-
### Files
|
|
121
|
-
|
|
122
|
-
| Method | Description |
|
|
123
|
-
|-----------------------------------------|----------------------------------------|
|
|
124
|
-
| `files.sessionUrl(sessionId, body)` | Presigned URL for session file upload |
|
|
125
|
-
| `files.browserUrl(sessionId, body)` | Presigned URL for browser file upload |
|
|
126
|
-
| `files.taskOutput(taskId, fileId)` | Download URL for task output file |
|
|
127
|
-
|
|
128
|
-
### Profiles
|
|
129
|
-
|
|
130
|
-
| Method | Description |
|
|
131
|
-
|-----------------------------------|------------------------------|
|
|
132
|
-
| `profiles.create(body?)` | Create a browser profile |
|
|
133
|
-
| `profiles.list(params?)` | List profiles |
|
|
134
|
-
| `profiles.get(profileId)` | Get profile details |
|
|
135
|
-
| `profiles.update(profileId, body)` | Update a profile |
|
|
136
|
-
| `profiles.delete(profileId)` | Delete a profile |
|
|
137
|
-
|
|
138
|
-
### Browsers
|
|
139
|
-
|
|
140
|
-
| Method | Description |
|
|
141
|
-
|-------------------------------|------------------------------------|
|
|
142
|
-
| `browsers.create(body)` | Create a browser session |
|
|
143
|
-
| `browsers.list(params?)` | List browser sessions |
|
|
144
|
-
| `browsers.get(sessionId)` | Get browser session details |
|
|
145
|
-
| `browsers.stop(sessionId)` | Stop a browser session |
|
|
146
|
-
|
|
147
|
-
### Skills
|
|
148
|
-
|
|
149
|
-
| Method | Description |
|
|
150
|
-
|------------------------------------------------|--------------------------------------|
|
|
151
|
-
| `skills.create(body)` | Create a skill via generation |
|
|
152
|
-
| `skills.list(params?)` | List skills |
|
|
153
|
-
| `skills.get(skillId)` | Get skill details |
|
|
154
|
-
| `skills.delete(skillId)` | Delete a skill |
|
|
155
|
-
| `skills.update(skillId, body)` | Update skill metadata |
|
|
156
|
-
| `skills.cancel(skillId)` | Cancel in-progress generation |
|
|
157
|
-
| `skills.execute(skillId, body)` | Execute a skill |
|
|
158
|
-
| `skills.refine(skillId, body)` | Refine a skill with feedback |
|
|
159
|
-
| `skills.rollback(skillId)` | Rollback to previous version |
|
|
160
|
-
| `skills.executions(skillId, params?)` | List skill executions |
|
|
161
|
-
| `skills.executionOutput(skillId, executionId)` | Download execution output |
|
|
162
|
-
|
|
163
|
-
### Marketplace
|
|
164
|
-
|
|
165
|
-
| Method | Description |
|
|
166
|
-
|-------------------------------------|-------------------------------------|
|
|
167
|
-
| `marketplace.list(params?)` | List public marketplace skills |
|
|
168
|
-
| `marketplace.get(skillSlug)` | Get marketplace skill by slug |
|
|
169
|
-
| `marketplace.clone(skillId)` | Clone a marketplace skill |
|
|
170
|
-
| `marketplace.execute(skillId, body)` | Execute a marketplace skill |
|
|
171
|
-
|
|
172
|
-
## V3 API Reference
|
|
173
|
-
|
|
174
|
-
### Sessions
|
|
175
|
-
|
|
176
|
-
| Method | Description |
|
|
177
|
-
|--------------------------------------|--------------------------------------|
|
|
178
|
-
| `sessions.create(body)` | Create session and run a task |
|
|
179
|
-
| `sessions.list(params?)` | List sessions |
|
|
180
|
-
| `sessions.get(sessionId)` | Get session details |
|
|
181
|
-
| `sessions.stop(sessionId)` | Stop a session |
|
|
182
|
-
| `sessions.files(sessionId, params?)` | List files in session workspace |
|
|
183
|
-
|
|
184
|
-
## Error Handling
|
|
185
|
-
|
|
186
|
-
All API errors throw `BrowserUseError`:
|
|
187
|
-
|
|
188
|
-
```ts
|
|
189
|
-
import { BrowserUse, BrowserUseError } from "browser-use-sdk";
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
await client.tasks.get("nonexistent-id");
|
|
193
|
-
} catch (err) {
|
|
194
|
-
if (err instanceof BrowserUseError) {
|
|
195
|
-
console.error(err.statusCode); // 404
|
|
196
|
-
console.error(err.message); // "Task not found"
|
|
197
|
-
console.error(err.detail); // Raw error body
|
|
198
|
-
}
|
|
199
|
-
}
|
|
23
|
+
const result = await client.run("Find the top 3 trending repos on GitHub today");
|
|
24
|
+
console.log(result.output);
|
|
200
25
|
```
|
|
201
26
|
|
|
202
|
-
|
|
27
|
+
## Docs
|
|
203
28
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
Types are re-exported from `browser-use-sdk` and `browser-use-sdk/v3`. The SDK ships with full type definitions for all request/response shapes.
|
|
207
|
-
|
|
208
|
-
```ts
|
|
209
|
-
import type { TaskView, CreateTaskRequest } from "browser-use-sdk";
|
|
210
|
-
```
|
|
29
|
+
[docs.browser-use.com](https://docs.browser-use.com)
|
|
211
30
|
|
|
212
31
|
## License
|
|
213
32
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1788,10 +1788,11 @@ interface components {
|
|
|
1788
1788
|
* CREATED: Task has been created but not yet started.
|
|
1789
1789
|
* STARTED: Task has been started and is currently running.
|
|
1790
1790
|
* FINISHED: Task has finished and the agent has completed the task.
|
|
1791
|
+
* FAILED: Task execution failed due to an error.
|
|
1791
1792
|
* STOPPED: Task execution has been manually stopped (cannot be resumed).
|
|
1792
1793
|
* @enum {string}
|
|
1793
1794
|
*/
|
|
1794
|
-
TaskStatus: "created" | "started" | "finished" | "stopped";
|
|
1795
|
+
TaskStatus: "created" | "started" | "finished" | "failed" | "stopped";
|
|
1795
1796
|
/**
|
|
1796
1797
|
* TaskStatusView
|
|
1797
1798
|
* @description Lightweight view optimized for polling. Use GET /tasks/{id}/status for efficient polling
|
package/dist/index.d.ts
CHANGED
|
@@ -1788,10 +1788,11 @@ interface components {
|
|
|
1788
1788
|
* CREATED: Task has been created but not yet started.
|
|
1789
1789
|
* STARTED: Task has been started and is currently running.
|
|
1790
1790
|
* FINISHED: Task has finished and the agent has completed the task.
|
|
1791
|
+
* FAILED: Task execution failed due to an error.
|
|
1791
1792
|
* STOPPED: Task execution has been manually stopped (cannot be resumed).
|
|
1792
1793
|
* @enum {string}
|
|
1793
1794
|
*/
|
|
1794
|
-
TaskStatus: "created" | "started" | "finished" | "stopped";
|
|
1795
|
+
TaskStatus: "created" | "started" | "finished" | "failed" | "stopped";
|
|
1795
1796
|
/**
|
|
1796
1797
|
* TaskStatusView
|
|
1797
1798
|
* @description Lightweight view optimized for polling. Use GET /tasks/{id}/status for efficient polling
|
package/dist/v3.cjs
CHANGED
|
@@ -6,6 +6,71 @@ var _chunkRIRVOEIUcjs = require('./chunk-RIRVOEIU.cjs');
|
|
|
6
6
|
// src/v3/client.ts
|
|
7
7
|
var _zod = require('zod');
|
|
8
8
|
|
|
9
|
+
// src/v3/resources/billing.ts
|
|
10
|
+
var Billing = class {
|
|
11
|
+
constructor(http) {
|
|
12
|
+
this.http = http;
|
|
13
|
+
}
|
|
14
|
+
/** Get account billing information. */
|
|
15
|
+
account() {
|
|
16
|
+
return this.http.get("/billing/account");
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/v3/resources/browsers.ts
|
|
21
|
+
var Browsers = class {
|
|
22
|
+
constructor(http) {
|
|
23
|
+
this.http = http;
|
|
24
|
+
}
|
|
25
|
+
/** Create a standalone browser session. */
|
|
26
|
+
create(body) {
|
|
27
|
+
return this.http.post("/browsers", body);
|
|
28
|
+
}
|
|
29
|
+
/** List browser sessions for the authenticated project. */
|
|
30
|
+
list(params) {
|
|
31
|
+
return this.http.get("/browsers", params);
|
|
32
|
+
}
|
|
33
|
+
/** Get browser session details. */
|
|
34
|
+
get(sessionId) {
|
|
35
|
+
return this.http.get(`/browsers/${sessionId}`);
|
|
36
|
+
}
|
|
37
|
+
/** Update a browser session (e.g. stop it). */
|
|
38
|
+
update(sessionId, body) {
|
|
39
|
+
return this.http.patch(`/browsers/${sessionId}`, body);
|
|
40
|
+
}
|
|
41
|
+
/** Stop a browser session. Convenience wrapper around update. */
|
|
42
|
+
stop(sessionId) {
|
|
43
|
+
return this.update(sessionId, { action: "stop" });
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/v3/resources/profiles.ts
|
|
48
|
+
var Profiles = class {
|
|
49
|
+
constructor(http) {
|
|
50
|
+
this.http = http;
|
|
51
|
+
}
|
|
52
|
+
/** Create a browser profile. */
|
|
53
|
+
create(body) {
|
|
54
|
+
return this.http.post("/profiles", body);
|
|
55
|
+
}
|
|
56
|
+
/** List profiles for the authenticated project. */
|
|
57
|
+
list(params) {
|
|
58
|
+
return this.http.get("/profiles", params);
|
|
59
|
+
}
|
|
60
|
+
/** Get profile details. */
|
|
61
|
+
get(profileId) {
|
|
62
|
+
return this.http.get(`/profiles/${profileId}`);
|
|
63
|
+
}
|
|
64
|
+
/** Update a profile. */
|
|
65
|
+
update(profileId, body) {
|
|
66
|
+
return this.http.patch(`/profiles/${profileId}`, body);
|
|
67
|
+
}
|
|
68
|
+
/** Delete a profile. */
|
|
69
|
+
delete(profileId) {
|
|
70
|
+
return this.http.delete(`/profiles/${profileId}`);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
9
74
|
// src/v3/resources/sessions.ts
|
|
10
75
|
var Sessions = class {
|
|
11
76
|
constructor(http) {
|
|
@@ -13,7 +78,7 @@ var Sessions = class {
|
|
|
13
78
|
}
|
|
14
79
|
/** Create a session and optionally dispatch a task. */
|
|
15
80
|
create(body) {
|
|
16
|
-
return this.http.post("/sessions", body);
|
|
81
|
+
return this.http.post("/sessions", _nullishCoalesce(body, () => ( {})));
|
|
17
82
|
}
|
|
18
83
|
/** List sessions for the authenticated project. */
|
|
19
84
|
list(params) {
|
|
@@ -49,9 +114,69 @@ var Sessions = class {
|
|
|
49
114
|
params
|
|
50
115
|
);
|
|
51
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Poll until recording URLs are available. Returns presigned MP4 URLs.
|
|
119
|
+
*
|
|
120
|
+
* Returns an empty array if no recording was produced (e.g. the agent
|
|
121
|
+
* answered without opening a browser, or recording was not enabled).
|
|
122
|
+
*/
|
|
123
|
+
async waitForRecording(sessionId, options) {
|
|
124
|
+
const timeout = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.timeout]), () => ( 15e3));
|
|
125
|
+
const interval = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.interval]), () => ( 2e3));
|
|
126
|
+
const deadline = Date.now() + timeout;
|
|
127
|
+
while (Date.now() < deadline) {
|
|
128
|
+
const session = await this.get(sessionId);
|
|
129
|
+
if (_optionalChain([session, 'access', _3 => _3.recordingUrls, 'optionalAccess', _4 => _4.length])) return session.recordingUrls;
|
|
130
|
+
const remaining = deadline - Date.now();
|
|
131
|
+
if (remaining <= 0) break;
|
|
132
|
+
await new Promise((r) => setTimeout(r, Math.min(interval, remaining)));
|
|
133
|
+
}
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
52
136
|
};
|
|
53
137
|
|
|
54
138
|
// src/v3/resources/workspaces.ts
|
|
139
|
+
var _fs = require('fs');
|
|
140
|
+
var _path = require('path');
|
|
141
|
+
function safeJoin(base, untrusted) {
|
|
142
|
+
const baseResolved = _path.resolve.call(void 0, base) + "/";
|
|
143
|
+
const resolved = _path.resolve.call(void 0, base, untrusted);
|
|
144
|
+
if (resolved !== _path.resolve.call(void 0, base) && !resolved.startsWith(baseResolved)) {
|
|
145
|
+
throw new Error(`Path traversal detected: ${untrusted}`);
|
|
146
|
+
}
|
|
147
|
+
return resolved;
|
|
148
|
+
}
|
|
149
|
+
var MIME_TYPES = {
|
|
150
|
+
".csv": "text/csv",
|
|
151
|
+
".json": "application/json",
|
|
152
|
+
".txt": "text/plain",
|
|
153
|
+
".md": "text/markdown",
|
|
154
|
+
".html": "text/html",
|
|
155
|
+
".xml": "application/xml",
|
|
156
|
+
".yaml": "application/yaml",
|
|
157
|
+
".yml": "application/yaml",
|
|
158
|
+
".pdf": "application/pdf",
|
|
159
|
+
".png": "image/png",
|
|
160
|
+
".jpg": "image/jpeg",
|
|
161
|
+
".jpeg": "image/jpeg",
|
|
162
|
+
".gif": "image/gif",
|
|
163
|
+
".webp": "image/webp",
|
|
164
|
+
".svg": "image/svg+xml",
|
|
165
|
+
".mp4": "video/mp4",
|
|
166
|
+
".mp3": "audio/mpeg",
|
|
167
|
+
".wav": "audio/wav",
|
|
168
|
+
".zip": "application/zip",
|
|
169
|
+
".gz": "application/gzip",
|
|
170
|
+
".tar": "application/x-tar",
|
|
171
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
172
|
+
".xls": "application/vnd.ms-excel",
|
|
173
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
174
|
+
".doc": "application/msword",
|
|
175
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
|
176
|
+
};
|
|
177
|
+
function guessContentType(path) {
|
|
178
|
+
return _nullishCoalesce(MIME_TYPES[_path.extname.call(void 0, path).toLowerCase()], () => ( "application/octet-stream"));
|
|
179
|
+
}
|
|
55
180
|
var Workspaces = class {
|
|
56
181
|
constructor(http) {
|
|
57
182
|
this.http = http;
|
|
@@ -99,6 +224,79 @@ var Workspaces = class {
|
|
|
99
224
|
size(workspaceId) {
|
|
100
225
|
return this.http.get(`/workspaces/${workspaceId}/size`);
|
|
101
226
|
}
|
|
227
|
+
/**
|
|
228
|
+
* Upload local files to a workspace. Returns the list of remote paths.
|
|
229
|
+
*
|
|
230
|
+
* ```ts
|
|
231
|
+
* await client.workspaces.upload(wsId, "data.csv", "config.json");
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
async upload(workspaceId, ...paths) {
|
|
235
|
+
const items = paths.map((p) => ({
|
|
236
|
+
name: _path.basename.call(void 0, p),
|
|
237
|
+
contentType: guessContentType(p),
|
|
238
|
+
size: _fs.statSync.call(void 0, p).size
|
|
239
|
+
}));
|
|
240
|
+
const resp = await this.uploadFiles(workspaceId, { files: items });
|
|
241
|
+
for (let i = 0; i < paths.length; i++) {
|
|
242
|
+
const body = _fs.readFileSync.call(void 0, paths[i]);
|
|
243
|
+
const res = await fetch(resp.files[i].uploadUrl, {
|
|
244
|
+
method: "PUT",
|
|
245
|
+
headers: { "Content-Type": items[i].contentType },
|
|
246
|
+
body
|
|
247
|
+
});
|
|
248
|
+
if (!res.ok) throw new Error(`Upload failed: ${res.status} ${res.statusText}`);
|
|
249
|
+
}
|
|
250
|
+
return resp.files.map((f) => f.path);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Download a single file from a workspace. Returns the local path.
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* const local = await client.workspaces.download(wsId, "uploads/data.csv", { to: "./data.csv" });
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
async download(workspaceId, path, options) {
|
|
260
|
+
const fileList = await this.files(workspaceId, { prefix: path, includeUrls: true });
|
|
261
|
+
const match = _optionalChain([fileList, 'access', _5 => _5.files, 'optionalAccess', _6 => _6.find, 'call', _7 => _7((f) => f.path === path)]);
|
|
262
|
+
if (!match) throw new Error(`File not found in workspace: ${path}`);
|
|
263
|
+
const dest = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _8 => _8.to]), () => ( _path.basename.call(void 0, match.path)));
|
|
264
|
+
_fs.mkdirSync.call(void 0, _path.dirname.call(void 0, dest), { recursive: true });
|
|
265
|
+
const resp = await fetch(match.url);
|
|
266
|
+
if (!resp.ok) throw new Error(`Download failed: ${resp.status}`);
|
|
267
|
+
_fs.writeFileSync.call(void 0, dest, Buffer.from(await resp.arrayBuffer()));
|
|
268
|
+
return dest;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Download all files from a workspace. Returns list of local paths.
|
|
272
|
+
*
|
|
273
|
+
* ```ts
|
|
274
|
+
* const paths = await client.workspaces.downloadAll(wsId, { to: "./output" });
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
async downloadAll(workspaceId, options) {
|
|
278
|
+
const destDir = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _9 => _9.to]), () => ( "."));
|
|
279
|
+
_fs.mkdirSync.call(void 0, destDir, { recursive: true });
|
|
280
|
+
const results = [];
|
|
281
|
+
let cursor;
|
|
282
|
+
do {
|
|
283
|
+
const fileList = await this.files(workspaceId, {
|
|
284
|
+
prefix: _optionalChain([options, 'optionalAccess', _10 => _10.prefix]),
|
|
285
|
+
includeUrls: true,
|
|
286
|
+
cursor
|
|
287
|
+
});
|
|
288
|
+
for (const f of _nullishCoalesce(fileList.files, () => ( []))) {
|
|
289
|
+
const local = safeJoin(destDir, f.path);
|
|
290
|
+
_fs.mkdirSync.call(void 0, _path.dirname.call(void 0, local), { recursive: true });
|
|
291
|
+
const resp = await fetch(f.url);
|
|
292
|
+
if (!resp.ok) throw new Error(`Download failed for ${f.path}: ${resp.status}`);
|
|
293
|
+
_fs.writeFileSync.call(void 0, local, Buffer.from(await resp.arrayBuffer()));
|
|
294
|
+
results.push(local);
|
|
295
|
+
}
|
|
296
|
+
cursor = fileList.hasMore ? _nullishCoalesce(fileList.nextCursor, () => ( void 0)) : void 0;
|
|
297
|
+
} while (cursor);
|
|
298
|
+
return results;
|
|
299
|
+
}
|
|
102
300
|
};
|
|
103
301
|
|
|
104
302
|
// src/v3/helpers.ts
|
|
@@ -115,8 +313,8 @@ var SessionRun = (_class = class {
|
|
|
115
313
|
this._createPromise = createPromise;
|
|
116
314
|
this._sessions = sessions;
|
|
117
315
|
this._schema = schema;
|
|
118
|
-
this._timeout = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
119
|
-
this._interval = _nullishCoalesce(_optionalChain([options, 'optionalAccess',
|
|
316
|
+
this._timeout = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _11 => _11.timeout]), () => ( 144e5));
|
|
317
|
+
this._interval = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _12 => _12.interval]), () => ( 2e3));
|
|
120
318
|
}
|
|
121
319
|
/** The session ID, available after task creation resolves. */
|
|
122
320
|
get sessionId() {
|
|
@@ -158,6 +356,40 @@ var SessionRun = (_class = class {
|
|
|
158
356
|
`Session ${sessionId} did not complete within ${this._timeout}ms`
|
|
159
357
|
);
|
|
160
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Enable `for await (const msg of client.run(...))` — yields messages as they appear.
|
|
361
|
+
* After iteration, `.result` contains the final SessionResult.
|
|
362
|
+
*/
|
|
363
|
+
async *[Symbol.asyncIterator]() {
|
|
364
|
+
const sessionId = await this._ensureSessionId();
|
|
365
|
+
let cursor;
|
|
366
|
+
const deadline = Date.now() + this._timeout;
|
|
367
|
+
while (Date.now() < deadline) {
|
|
368
|
+
const resp = await this._sessions.messages(sessionId, { after: cursor, limit: 100 });
|
|
369
|
+
for (const msg of resp.messages) {
|
|
370
|
+
yield msg;
|
|
371
|
+
cursor = msg.id;
|
|
372
|
+
}
|
|
373
|
+
const session = await this._sessions.get(sessionId);
|
|
374
|
+
if (TERMINAL_STATUSES.has(session.status)) {
|
|
375
|
+
while (true) {
|
|
376
|
+
const page = await this._sessions.messages(sessionId, { after: cursor, limit: 100 });
|
|
377
|
+
if (!page.messages.length) break;
|
|
378
|
+
for (const msg of page.messages) {
|
|
379
|
+
yield msg;
|
|
380
|
+
cursor = msg.id;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
const { output, ...rest } = session;
|
|
384
|
+
this._result = { ...rest, output: this._parseOutput(output) };
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const remaining = deadline - Date.now();
|
|
388
|
+
if (remaining <= 0) break;
|
|
389
|
+
await new Promise((r) => setTimeout(r, Math.min(this._interval, remaining)));
|
|
390
|
+
}
|
|
391
|
+
throw new Error(`Session ${sessionId} did not complete within ${this._timeout}ms`);
|
|
392
|
+
}
|
|
161
393
|
_parseOutput(output) {
|
|
162
394
|
if (output == null) return null;
|
|
163
395
|
if (!this._schema) return output;
|
|
@@ -172,6 +404,9 @@ var BrowserUse = class {
|
|
|
172
404
|
|
|
173
405
|
|
|
174
406
|
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
|
|
175
410
|
constructor(options = {}) {
|
|
176
411
|
const apiKey = _nullishCoalesce(_nullishCoalesce(options.apiKey, () => ( process.env.BROWSER_USE_API_KEY)), () => ( ""));
|
|
177
412
|
if (!apiKey) {
|
|
@@ -185,6 +420,9 @@ var BrowserUse = class {
|
|
|
185
420
|
maxRetries: options.maxRetries,
|
|
186
421
|
timeout: options.timeout
|
|
187
422
|
});
|
|
423
|
+
this.billing = new Billing(this.http);
|
|
424
|
+
this.browsers = new Browsers(this.http);
|
|
425
|
+
this.profiles = new Profiles(this.http);
|
|
188
426
|
this.sessions = new Sessions(this.http);
|
|
189
427
|
this.workspaces = new Workspaces(this.http);
|
|
190
428
|
}
|
|
@@ -194,6 +432,9 @@ var BrowserUse = class {
|
|
|
194
432
|
if (schema) {
|
|
195
433
|
body.outputSchema = _zod.z.toJSONSchema(schema);
|
|
196
434
|
}
|
|
435
|
+
if (body.sessionId && body.keepAlive === void 0) {
|
|
436
|
+
body.keepAlive = true;
|
|
437
|
+
}
|
|
197
438
|
const promise = this.sessions.create(body);
|
|
198
439
|
return new SessionRun(promise, this.sessions, schema, { timeout, interval });
|
|
199
440
|
}
|
|
@@ -204,5 +445,8 @@ var BrowserUse = class {
|
|
|
204
445
|
|
|
205
446
|
|
|
206
447
|
|
|
207
|
-
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
exports.Billing = Billing; exports.BrowserUse = BrowserUse; exports.BrowserUseError = _chunkRIRVOEIUcjs.BrowserUseError; exports.Browsers = Browsers; exports.Profiles = Profiles; exports.SessionRun = SessionRun; exports.Sessions = Sessions; exports.Workspaces = Workspaces;
|
|
208
452
|
//# sourceMappingURL=v3.cjs.map
|