forked from mayt/BrowserGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactWithPage.js
81 lines (64 loc) · 2.25 KB
/
interactWithPage.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
import {retry} from '@lifeomic/attempt';
import {HumanMessage, SystemMessage} from 'langchain/schema';
import {expect} from '@playwright/test';
import {
parseSite,
preprocessJsonInput,
appendToTestFile,
} from '../util/index.js';
const AsyncFunction = async function () {}.constructor;
export async function interactWithPage(chatApi, page, task, options) {
const code = await getPlayWrightCode(page, chatApi, task);
if (options.outputFilePath) {
appendToTestFile(task, code, options.outputFilePath);
}
return execPlayWrightCode(page, code);
}
async function queryGPT(chatApi, messages) {
const completion = await retry(async () => chatApi.call(messages));
console.log('Commands to be executed'.green);
let cleanedCommands = null;
try {
cleanedCommands = preprocessJsonInput(completion.text);
console.log(cleanedCommands);
} catch (e) {
console.log('No code found'.red);
}
console.log('EOF'.green);
return cleanedCommands;
}
async function getPlayWrightCode(page, chatApi, task) {
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)}
\`\`\`
Your output should just be the code that is valid for PlayWright page api.
If the content is inside nested iframes, use 'frameLocator' function to select each layer starting from the top level page.
User: click on show hn link
Assistant:
\`\`\`
const articleByText = 'Show HN';
await page.getByText(articleByText, { exact: true }).click(articleByText);
\`\`\`
`;
return await queryGPT(chatApi, [
new SystemMessage(systemPrompt),
new HumanMessage(task),
]);
}
async function execPlayWrightCode(page, code) {
const dependencies = [
{param: 'page', value: page},
{param: 'expect', value: expect},
];
const func = AsyncFunction(...dependencies.map((d) => d.param), code);
const args = dependencies.map((d) => d.value);
return await func(...args);
}