Skip to content

Commit 38ad279

Browse files
committed
Fix Examples
1 parent 6bd382a commit 38ad279

File tree

7 files changed

+66
-58
lines changed

7 files changed

+66
-58
lines changed

typescript/agent/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@radix-ui/react-tooltip": "^1.2.8",
2121
"@radix-ui/react-use-controllable-state": "^1.2.2",
2222
"ai": "^5.0.13",
23-
"browser-use-sdk": "0.1.1",
23+
"browser-use-sdk": "0.3.0",
2424
"class-variance-authority": "^0.7.1",
2525
"clsx": "^2.1.1",
2626
"embla-carousel-react": "^8.6.0",

typescript/agent/pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript/agent/src/app/api/chat/route.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type TaskStatus =
2626
| {
2727
status: "running";
2828

29-
lastStep: BrowserUse.Tasks.TaskView.Step;
29+
lastStep: BrowserUse.Tasks.TaskStepView;
3030
liveUrl: string | null;
3131
}
3232
| {
@@ -46,20 +46,20 @@ const tools = {
4646
}),
4747
async *execute({ task }, { abortSignal }) {
4848
// Create Task
49-
const rsp = await bu.tasks.create({ task: task });
5049

51-
console.log(`[agent] Task created: ${rsp.id}`);
50+
const res = await bu.tasks.create({ task }, { signal: abortSignal });
5251

53-
poll: do {
54-
// Wait for Task to Finish
55-
const status = (await bu.tasks.retrieve(
56-
rsp.id,
57-
{ statusOnly: false },
58-
{ signal: abortSignal },
59-
)) as BrowserUse.Tasks.TaskView;
52+
const gen = bu.tasks.stream(res.id, { signal: abortSignal });
53+
54+
for await (const event of gen) {
55+
const status = event.data;
6056

6157
console.log(`[agent] task status: ${status.status}`);
6258

59+
if (status.doneOutput) {
60+
console.log(`[agent] task output: ${status.doneOutput}`);
61+
}
62+
6363
switch (status.status) {
6464
case "started":
6565
case "paused":
@@ -81,12 +81,10 @@ const tools = {
8181
liveUrl: status.sessionLiveUrl ? status.sessionLiveUrl : null,
8282
} satisfies TaskStatus;
8383

84-
await new Promise((resolve) => setTimeout(resolve, 1000));
85-
8684
break;
8785

8886
case "finished":
89-
if (status.sessionLiveUrl == null) {
87+
if (status.sessionLiveUrl == null || status.doneOutput == null) {
9088
break;
9189
}
9290

@@ -97,12 +95,12 @@ const tools = {
9795
sessionId: status.sessionId,
9896
} satisfies TaskStatus;
9997

100-
break poll;
98+
break;
10199

102100
default:
103101
throw new Error(`Unknown status: ${status.status}`);
104102
}
105-
} while (true);
103+
}
106104
},
107105
}),
108106
} satisfies ToolSet;

typescript/discord/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
# Browser Use Discord
22

3+
> An example Discord Bot using Browser Use Cloud API to execute browser tasks.
4+
5+
<p align="center">
6+
<img src="media/embed.png" alt="Bot Embed Screenshot" width="600" fi />
7+
</p>
8+
9+
<p align="center">
10+
<img src="media/message.png" alt="Bot Completion Message Screenshot" height="400" />
11+
</p>
12+
13+
## Development Setup
14+
15+
```bash
16+
# Install dependencies
17+
pnpm i
18+
19+
# Start Dev
20+
pnpm dev
21+
```
22+
23+
### Getting Environment Variables
24+
25+
> Set up environment variables to successfully run this example locally.
26+
27+
- Browser Use Cloud API - [Get API Key](https://cloud.browser-use.com/billing)
28+
29+
Copy `.env.example` into `.env` and run the project!
30+
31+
### Useful Links
32+
333
- https://discord.com/developers/docs/quick-start/getting-started#step-0-project-setup
434
- https://discord.js.org/

typescript/discord/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"license": "ISC",
1414
"packageManager": "[email protected]",
1515
"dependencies": {
16-
"browser-use-sdk": "0.1.1",
16+
"browser-use-sdk": "0.3.0",
1717
"discord.js": "14.21.0",
1818
"express": "5.1.0"
1919
},

typescript/discord/pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typescript/discord/src/commands/run.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,19 @@ export const run: Command = {
2222
return;
2323
}
2424

25-
const tick: { current: number } = { current: 0 };
25+
await interaction.reply("Browser Use on the job!");
2626

27-
const task = await browseruse.tasks.create({
27+
const res = await browseruse.tasks.create({
2828
task: command,
2929
agentSettings: {
3030
llm: "o3",
3131
},
3232
});
3333

34-
const embed = new EmbedBuilder()
35-
.setTitle("🤖 Browser Use Task")
36-
.setDescription(`**Command:** ${command}\n**Task ID:** ${task.id}`)
37-
.setColor(0x0099ff)
38-
.addFields(
39-
{ name: "Status", value: "🔄 Starting...", inline: true },
40-
{ name: "Live Session", value: "⏳ Waiting...", inline: true },
41-
)
42-
.setTimestamp();
34+
const gen = browseruse.tasks.stream(res.id);
4335

44-
await interaction.reply({ embeds: [embed] });
45-
46-
poll: do {
47-
tick.current++;
48-
49-
const status = (await browseruse.tasks.retrieve(task.id)) as BrowserUse.TaskView;
36+
for await (const event of gen) {
37+
const status = event.data;
5038

5139
switch (status.status) {
5240
case "started":
@@ -57,7 +45,7 @@ export const run: Command = {
5745
const description: string[] = [];
5846

5947
description.push(`**Command:** ${command}`);
60-
description.push(`**Task ID:** ${task.id}`);
48+
description.push(`**Task ID:** ${status.id}`);
6149

6250
description.push("");
6351

@@ -92,32 +80,24 @@ export const run: Command = {
9280
case "finished": {
9381
const output: string[] = [];
9482

95-
output.push(`# Browser Use Task - ${task.id} ✅`);
83+
output.push(`# Browser Use Task - ${status.id} ✅`);
9684
output.push(`## Task`);
9785
output.push(command);
9886

9987
output.push("");
10088

10189
output.push(`## Output`);
102-
output.push(status.doneOutput);
90+
output.push(status.doneOutput ?? "No output");
10391

10492
await interaction.editReply({ content: output.join("\n"), embeds: [] });
10593

106-
break poll;
94+
break;
10795
}
10896
default:
10997
throw new ExhaustiveSwitchCheck(status.status);
11098
}
11199

112-
// LOGS
113-
114-
console.log(`[${status.id}] (${tick.current}) ${status.status}`);
115-
116-
// TIMER
117-
118-
await new Promise((resolve) => setTimeout(resolve, 1000));
119-
} while (true);
120-
121-
console.log("done");
100+
console.log(`[${status.id}] ${status.status}`);
101+
}
122102
},
123103
};

0 commit comments

Comments
 (0)