forked from mayt/BrowserGPT
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
285 lines (238 loc) · 7.68 KB
/
index.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {retry} from '@lifeomic/attempt';
import dotenv from 'dotenv';
import {chromium} from 'playwright';
import prompt from 'prompt';
// eslint-disable-next-line no-unused-vars
import colors from '@colors/colors';
import {parse} from 'node-html-parser';
import {Command} from 'commander';
import {ChatOpenAI} from 'langchain/chat_models/openai';
import {HumanMessage, SystemMessage} from 'langchain/schema';
import {JSDOM} from 'jsdom';
const {document} = new JSDOM(`...`).window;
dotenv.config();
const AsyncFunction = async function () {}.constructor;
const tagsToLog = [
'a',
'p',
'span',
'div',
'button',
'label',
'input',
'textarea',
'section',
'select',
'option',
'table',
'td',
'th',
'ul',
'ol',
'li',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
];
function createElement(node) {
const elem = document.createElement(node.tagName);
const dataAttributes = Object.entries(node.attributes).filter(
(a) =>
(tagsToLog.includes(node.tagName) &&
(a[0].startsWith('name') ||
a[0].startsWith('value') ||
a[0].startsWith('data-component') ||
a[0].startsWith('data-name') ||
a[0].startsWith('aria-') ||
a[0] === 'class' ||
a[0] === 'type' ||
a[0] === 'role')) ||
// always log these
a[0] === 'href' ||
a[0] === 'id'
);
dataAttributes.forEach(([attr, value]) => {
elem.setAttribute(attr, value);
});
return elem;
}
function createTextNode(text) {
const textContent = text.textContent.trim();
// Check if the text content is likely JSON and exceeds a certain length
if (
textContent.startsWith('{') &&
textContent.endsWith('}') &&
textContent.length > 500
) {
console.log(
`Skipping large JSON text node: ${textContent.substring(0, 100)}...`
);
return document.createTextNode(''); // Return an empty text node
}
// console.log(`Processing text node: ${textContent.substring(0, 100)}...`); // Log only the first 100 characters for brevity
return document.createTextNode(textContent);
}
function dfs(node, parentElem) {
node.childNodes.forEach((childNode) => {
if (childNode.nodeType === 1) {
const childElem = createElement(childNode);
parentElem.appendChild(childElem);
dfs(childNode, childElem);
} else if (childNode.nodeType === 3) {
if (!childNode.isWhitespace) {
const textElem = createTextNode(childNode);
parentElem.appendChild(textElem);
}
}
});
}
function getStructure(node) {
const rootElem = createElement(node);
dfs(node, rootElem);
return rootElem;
}
async function parseSite(page, options = {}) {
const html = await (await page.locator('body', {timeout: 1000})).innerHTML();
debugTokenLengthByTags(html);
const root = parse(html, {
blockTextElements: {
script: false,
noscript: false,
style: false,
pre: true, // keep text content when parsing
},
});
const structure = getStructure(root);
return structure.innerHTML;
}
function calculateTagTokenLength(html, tag) {
const regex = new RegExp(`<${tag}[^>]*>(.*?)</${tag}>`, 'gi');
let totalTokenLength = 0;
let match;
while ((match = regex.exec(html)) !== null) {
totalTokenLength += estimateTokenLength(match[0]);
}
return totalTokenLength;
}
function debugTokenLengthByTags(html) {
tagsToLog.forEach((tag) => {
const tokenLength = calculateTagTokenLength(html, tag);
// console.log(`Token length for <${tag}>: ${tokenLength}`);
});
}
function estimateTokenLength(text) {
// Roughly estimate token length; OpenAI uses BPE which is complex to replicate exactly
const words = text.split(/\s+/);
let tokenCount = 0;
words.forEach((word) => {
// Rough estimate: Add 1 token for the word, and additional tokens for every 4 characters
tokenCount += 1 + Math.ceil(word.length / 4);
});
return tokenCount;
}
async function queryGPT(chatApi, messages) {
const completion = await retry(async () => chatApi.call(messages));
console.log('Comands to be executed'.green);
let cleanedCommands = null;
try {
const codeRegex = /```(.*)(\r\n|\r|\n)(?<code>[\w\W\n]+)(\r\n|\r|\n)```/;
cleanedCommands = completion.text.match(codeRegex).groups.code.trim();
console.log(cleanedCommands);
} catch (e) {
console.log('No code found'.red);
}
console.log('EOF'.green);
return cleanedCommands;
}
async function doAction(chatApi, page, task, options = {}) {
const systemPrompt = `
You are a programmer and your job is to write code. You are working on a playwright file. You will write the commands necessary to execute the given input.
Context:
Your computer is a mac. Cmd is the meta key, META.
The browser is already open.
Current page url is ${await page.evaluate('location.href')}.
Current page title is ${await page.evaluate('document.title')}.
Here is the overview of the site. Format is in html:
${await parseSite(page, options)}
Your output should just be the code that is valid for PlayWright page api. When given the option to use a timeout option, use 1s. Except when using page.goto() use 10s.
For actions like click, especially in dynamic web applications like LinkedIn, use the force option to interact with elements that are not immediately visible, such as those hidden or nested within dynamic content sections. Ensure the script can handle elements that may need additional actions to become interactable.
User: click on show hn link
Assistant:
\`\`\`
const articleByText = 'Show HN';
await page.getByText(articleByText, { exact: true }).click(articleByText, {force: true, hidden: true});
\`\`\`
`;
// Calculate the token length of systemPrompt and task
const tokenLengthSystemPrompt = estimateTokenLength(systemPrompt);
const tokenLengthTask = estimateTokenLength(task);
const totalTokenLength = tokenLengthSystemPrompt + tokenLengthTask;
console.log(`Total token length: ${totalTokenLength}`); // For debugging
if (totalTokenLength > 128000) {
console.log(
'Error: Token limit exceeded. Please reduce the length of the messages.'
);
return;
}
let code = '';
try {
code = await queryGPT(chatApi, [
new SystemMessage(systemPrompt),
new HumanMessage(task),
]);
} catch (e) {
// Check if e.response and e.response.data exist before accessing e.response.data.error
if (e.response && e.response.data) {
console.log(e.response.data.error);
} else {
console.log('Error in queryGPT:', e);
}
}
try {
const func = AsyncFunction('page', code);
await func(page);
} catch (e) {
console.log('Error in executing code:', e);
}
}
async function main(options) {
const url = options.url;
const browser = await chromium.launch({headless: false});
const browserContext = await browser.newContext();
const page = await browserContext.newPage();
page.setDefaultTimeout(60000); // set timeout to 60s (default is 30s)
await page.goto(url);
prompt.message = 'BrowserGPT'.green;
prompt.delimiter = '>'.green;
prompt.start();
const chatApi = new ChatOpenAI({
temperature: 0.1,
modelName: options.model ? options.model : 'gpt-4-1106-preview',
});
// eslint-disable-next-line no-constant-condition
while (true) {
const {task} = await prompt.get({
properties: {
task: {
message: ' Input a task\n',
required: true,
},
},
});
try {
await doAction(chatApi, page, task, options);
} catch (e) {
console.log('Execution failed');
console.log(e);
}
}
}
const program = new Command();
program
.option('-u, --url <url>', 'url to start on', 'https://www.google.com')
.option('-m, --model <model>', 'openai model to use', 'gpt-4-1106-preview');
program.parse();
main(program.opts());