|
6 | 6 | <img src="media/app.png" alt="App Screenshot" height="400" />
|
7 | 7 | </p>
|
8 | 8 |
|
| 9 | +## TLDR |
| 10 | + |
| 11 | +### Integrating with Vercel AI SDK |
| 12 | + |
| 13 | +> [`src/app/api/chat/route.ts`](./src/app/api/chat/route.ts) |
| 14 | +
|
| 15 | +```ts |
| 16 | +const bu = new BrowserUse({ |
| 17 | + apiKey: process.env.BROWSER_USE_API_KEY, |
| 18 | +}); |
| 19 | + |
| 20 | +type TaskStatus = |
| 21 | + | { |
| 22 | + status: "starting"; |
| 23 | + liveUrl: string | null; |
| 24 | + } |
| 25 | + | { |
| 26 | + status: "running"; |
| 27 | + |
| 28 | + lastStep: BrowserUse.Tasks.TaskStepView; |
| 29 | + liveUrl: string | null; |
| 30 | + } |
| 31 | + | { |
| 32 | + status: "done"; |
| 33 | + |
| 34 | + output: string; |
| 35 | + liveUrl: string; |
| 36 | + |
| 37 | + sessionId: string; |
| 38 | + }; |
| 39 | + |
| 40 | +const tools = { |
| 41 | + runTask: tool({ |
| 42 | + description: "Run a task in a web browser.", |
| 43 | + inputSchema: z.object({ |
| 44 | + task: z.string(), |
| 45 | + }), |
| 46 | + async *execute({ task }, { abortSignal }) { |
| 47 | + // Create Task |
| 48 | + |
| 49 | + const res = await bu.tasks.create({ task }, { signal: abortSignal }); |
| 50 | + |
| 51 | + const gen = bu.tasks.stream(res.id, { signal: abortSignal }); |
| 52 | + |
| 53 | + for await (const event of gen) { |
| 54 | + const status = event.data; |
| 55 | + |
| 56 | + console.log(`[agent] task status: ${status.status}`); |
| 57 | + |
| 58 | + if (status.doneOutput) { |
| 59 | + console.log(`[agent] task output: ${status.doneOutput}`); |
| 60 | + } |
| 61 | + |
| 62 | + switch (status.status) { |
| 63 | + case "started": |
| 64 | + case "paused": |
| 65 | + case "stopped": |
| 66 | + if (status.steps == null || status.steps.length === 0) { |
| 67 | + yield { |
| 68 | + status: "starting", |
| 69 | + liveUrl: status.session.liveUrl ? status.session.liveUrl : null, |
| 70 | + } satisfies TaskStatus; |
| 71 | + |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + const lastStep = status.steps[status.steps.length - 1]; |
| 76 | + |
| 77 | + yield { |
| 78 | + status: "running", |
| 79 | + lastStep: lastStep, |
| 80 | + liveUrl: status.session.liveUrl ? status.session.liveUrl : null, |
| 81 | + } satisfies TaskStatus; |
| 82 | + |
| 83 | + break; |
| 84 | + |
| 85 | + case "finished": |
| 86 | + if (status.session.liveUrl == null || status.doneOutput == null) { |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + yield { |
| 91 | + status: "done", |
| 92 | + output: status.doneOutput, |
| 93 | + liveUrl: status.session.liveUrl, |
| 94 | + sessionId: status.sessionId, |
| 95 | + } satisfies TaskStatus; |
| 96 | + |
| 97 | + break; |
| 98 | + |
| 99 | + default: |
| 100 | + throw new Error(`Unknown status: ${status.status}`); |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + }), |
| 105 | +} satisfies ToolSet; |
| 106 | + |
| 107 | +export type ChatTools = InferUITools<typeof tools>; |
| 108 | + |
| 109 | +export type ChatMessage = UIMessage<never, UIDataTypes, ChatTools>; |
| 110 | + |
| 111 | +// ROUTE |
| 112 | + |
| 113 | +export async function POST(req: Request) { |
| 114 | + const { messages, model }: { messages: UIMessage[]; model: string } = await req.json(); |
| 115 | + |
| 116 | + const result = streamText({ |
| 117 | + model: model, |
| 118 | + messages: convertToModelMessages(messages), |
| 119 | + system: |
| 120 | + "You are a helpful assistant that can answer questions and help with tasks. You can use the tools provided to you to help you answer questions and help with tasks.", |
| 121 | + tools: tools, |
| 122 | + stopWhen: stepCountIs(15), |
| 123 | + }); |
| 124 | + |
| 125 | + // send sources and reasoning back to the client |
| 126 | + return result.toUIMessageStreamResponse({ |
| 127 | + sendSources: false, |
| 128 | + sendReasoning: false, |
| 129 | + }); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### Integrating with Vercel AI Chat |
| 134 | + |
| 135 | +> [`src/app/page.tsx`](./src/app/page.tsx). |
| 136 | +
|
| 137 | +```tsx |
| 138 | +const { messages, sendMessage, status } = useChat<ChatMessage>({ |
| 139 | + transport: new DefaultChatTransport({ |
| 140 | + api: "/api/chat", |
| 141 | + }), |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
9 | 145 | ## Development Setup
|
10 | 146 |
|
11 | 147 | ```bash
|
|
0 commit comments