-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path05-headless-browser.js
More file actions
214 lines (182 loc) · 6.29 KB
/
Copy path05-headless-browser.js
File metadata and controls
214 lines (182 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
import chalk from 'chalk';
import { randomUUID } from 'crypto';
import {
chromium
} from 'playwright';
const modelId = "us.anthropic.claude-3-5-haiku-20241022-v1:0";
const initialPrompt = "Navigate to AWS homepage and take a screenshot. Do the same for Anthropic homepage";
function printUser(s) {
console.log(chalk.blue(JSON.stringify(s, null, 2)));
}
function printAssistant(s) {
console.log(chalk.red(JSON.stringify(s, null, 2)));
}
function printSystem(s) {
console.log(chalk.green(JSON.stringify(s, null, 2)));
}
// Define web interaction tools - simplified to essential properties
const webTools = [
{
"toolSpec": {
"name": "navigate",
"description": "Navigate to a specified URL",
"inputSchema": {
"json": {
"type": "object",
"properties": {
"url": { "type": "string" }
},
"required": ["url"]
}
}
}
},
{
"toolSpec": {
"name": "screenshot",
"description": "Take a screenshot of the current page",
"inputSchema": {
"json": {
"type": "object",
"properties": {},
"required": []
}
}
}
}
];
async function navigate(page, url) {
printSystem(`Navigating to ${url}`);
await page.goto(url, { timeout: 80000, waitUntil: 'networkidle' });
const title = await page.title();
return {
title: title
};
}
async function takeScreenshot(page) {
const filename = `screenshots/screenshot_${randomUUID()}.png`;
printSystem(`Taking screenshot ${filename}`);
await page.screenshot({ path: filename });
// Return the filename for later use
return {
filename
};
}
async function getPageInfo(page) {
try {
const title = await page.title();
const url = page.url();
return { title, url };
} catch (error) {
printSystem(`Error getting page info: ${error.message}`);
return { title: "Unknown", url: "Unknown" };
}
}
async function run() {
// Initialize browser - minimal setup
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();
try {
// Set up AWS Bedrock client
const bedrockClient = new BedrockRuntimeClient({ region: "us-west-2" });
const messages = [
{
role: "user",
content: [
{
text: initialPrompt
}
]
}
];
let nbRequest = 1;
// Send to model
printSystem(`Sending request ${nbRequest} to Bedrock with ${messages.length} messages...`)
printUser(`User prompt: ${messages[0]['content'][0]['text']}`)
let response = await bedrockClient.send(
new ConverseCommand({
modelId,
messages: messages,
toolConfig: {
tools: webTools
}
})
);
// Process response
let outputMessage = response.output.message;
let stopReason = response.stopReason;
printAssistant(outputMessage);
messages.push(outputMessage);
// Process tool requests - simplified loop
while (stopReason === "tool_use") {
const toolContent = [];
for (const content of outputMessage.content) {
if (content.toolUse) {
const tool = content.toolUse;
const toolId = tool.toolUseId;
const toolInput = tool.input || {};
if (typeof toolInput === 'string') {
toolInput = JSON.parse(toolInput);
}
// Execute requested tool
let result = {};
if (tool.name === "navigate") {
const url = toolInput.url;
result = await navigate(page, url);
} else if (tool.name === "screenshot") {
result = await takeScreenshot(page);
}
// Concatenate tool content that will be sent back to the model
toolContent.push({
toolResult: {
toolUseId: toolId,
content: [{
json: result
}]
}
});
}
}
// Browser context content - safely get page info
const pageInfo = await getPageInfo(page);
const browserContent = { text: `Current page: Title: '${pageInfo['title']}', URL: '${pageInfo['url']}'` };
printSystem(`Browser context: ${browserContent}`);
// Add browser context to message
toolContent.push(browserContent);
// Send result back to model
const toolResultMessage = {
role: "user",
content: [
...toolContent
]
};
messages.push(toolResultMessage);
nbRequest++;
printSystem(`Sending request ${nbRequest} to Bedrock with ${messages.length} messages...`);
printUser(messages.at(-1));
// Continue conversation
response = await bedrockClient.send(
new ConverseCommand({
modelId,
messages: messages,
toolConfig: {
tools: webTools
}
})
);
outputMessage = response.output.message;
messages.push(outputMessage);
printAssistant(outputMessage);
stopReason = response.stopReason;
}
} catch (error) {
console.error("Error invoking Bedrock model:", error);
} finally {
await browser.close();
}
}
// Main entry point
printSystem("AWS Bedrock Web Tools Minimal Example")
printSystem("------------------------------------")
run();