Skip to content

Commit a69629c

Browse files
committed
Simplify Agent Example
1 parent 927d76a commit a69629c

File tree

2 files changed

+37
-65
lines changed

2 files changed

+37
-65
lines changed

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

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const bu = new BrowserUse({
1919
});
2020

2121
type TaskStatus =
22+
| {
23+
status: "starting";
24+
liveUrl: string | null;
25+
}
2226
| {
2327
status: "running";
2428

@@ -40,73 +44,32 @@ const tools = {
4044
inputSchema: z.object({
4145
task: z.string(),
4246
}),
43-
async *execute({ task }) {
47+
async *execute({ task }, { abortSignal }) {
4448
// Create Task
4549
const rsp = await bu.tasks.create({ task: task });
4650

47-
poll: do {
48-
// Wait for Task to Finish
49-
const status = (await bu.tasks.retrieve(rsp.id, { statusOnly: false })) as BrowserUse.Tasks.TaskView;
50-
51-
switch (status.status) {
52-
case "started":
53-
case "paused":
54-
case "stopped":
55-
if (status.steps == null || status.steps.length === 0) {
56-
break;
57-
}
58-
59-
const lastStep = status.steps[status.steps.length - 1];
60-
61-
yield {
62-
status: "running",
63-
lastStep: lastStep,
64-
liveUrl: status.sessionLiveUrl ? status.sessionLiveUrl : null,
65-
} satisfies TaskStatus;
66-
67-
await new Promise((resolve) => setTimeout(resolve, 1000));
68-
69-
break;
70-
71-
case "finished":
72-
if (status.sessionLiveUrl == null) {
73-
break;
74-
}
75-
76-
yield {
77-
status: "done",
78-
output: status.doneOutput,
79-
liveUrl: status.sessionLiveUrl,
80-
sessionId: status.sessionId,
81-
} satisfies TaskStatus;
82-
83-
break poll;
84-
85-
default:
86-
throw new Error(`Unknown status: ${status.status}`);
87-
}
88-
} while (true);
89-
},
90-
}),
91-
continueTask: tool({
92-
description: "Continue a task in a web browser.",
93-
inputSchema: z.object({
94-
sessionId: z.string(),
95-
task: z.string(),
96-
}),
97-
async *execute({ sessionId, task }) {
98-
// Create Task
99-
const rsp = await bu.tasks.create({ task: task, browserSettings: { sessionId: sessionId } });
51+
console.log(`[agent] Task created: ${rsp.id}`);
10052

10153
poll: do {
10254
// Wait for Task to Finish
103-
const status = (await bu.tasks.retrieve(rsp.id, { statusOnly: false })) as BrowserUse.Tasks.TaskView;
55+
const status = (await bu.tasks.retrieve(
56+
rsp.id,
57+
{ statusOnly: false },
58+
{ signal: abortSignal },
59+
)) as BrowserUse.Tasks.TaskView;
60+
61+
console.log(`[agent] task status: ${status.status}`);
10462

10563
switch (status.status) {
10664
case "started":
10765
case "paused":
10866
case "stopped":
10967
if (status.steps == null || status.steps.length === 0) {
68+
yield {
69+
status: "starting",
70+
liveUrl: status.sessionLiveUrl ? status.sessionLiveUrl : null,
71+
} satisfies TaskStatus;
72+
11073
break;
11174
}
11275

typescript/agent/src/app/page.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,22 @@ export default function Home() {
7272
const liveUrl = useMemo(() => {
7373
let url: string | undefined = undefined;
7474

75-
for (const message of messages) {
76-
for (const part of message.parts) {
77-
if (part.type === "tool-runTask" && part.output != null) {
78-
const output = part.output;
79-
80-
if (output.liveUrl != null) {
81-
url = output.liveUrl;
82-
}
75+
if (messages.length === 0) {
76+
return undefined;
77+
}
78+
79+
const lastMessage = messages[messages.length - 1];
80+
81+
if (lastMessage.role === "user") {
82+
return undefined;
83+
}
84+
85+
for (const part of lastMessage.parts) {
86+
if (part.type === "tool-runTask" && part.output != null) {
87+
const output = part.output;
88+
89+
if (output.liveUrl != null) {
90+
url = output.liveUrl;
8391
}
8492
}
8593
}
@@ -116,16 +124,17 @@ export default function Home() {
116124
switch (part.type) {
117125
case "text":
118126
return <Response key={`${message.id}-${i}`}>{part.text}</Response>;
127+
119128
case "reasoning":
120129
return (
121130
<Reasoning key={`${message.id}-${i}`} className="w-full" isStreaming={status === "streaming"}>
122131
<ReasoningTrigger />
123132
<ReasoningContent>{part.text}</ReasoningContent>
124133
</Reasoning>
125134
);
126-
case "tool-continueTask":
135+
127136
case "tool-runTask": {
128-
if (part.output == null) {
137+
if (part.output == null || part.output.status === "starting") {
129138
return (
130139
<Reasoning
131140
key={`${message.id}-${i}`}

0 commit comments

Comments
 (0)