-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
100 lines (83 loc) · 2.66 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
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 {Command} from 'commander';
import {ChatOpenAI} from 'langchain/chat_models/openai';
import {doActionWithAutoGPT} from './autogpt/index.js';
import {interactWithPage} from './actions/index.js';
import {createTestFile, gracefulExit, logPageScreenshot} from './util/index.js';
dotenv.config();
async function main(options) {
const url = options.url;
const browser = await chromium.launch({headless: options.headless});
// Parse the viewport option
const [width, height] = options.viewport.split(',').map(Number);
const browserContext = await browser.newContext({
viewport: {width, height},
});
const page = await browserContext.newPage();
await page.goto(url);
prompt.message = 'BrowserGPT'.green;
const promptOptions = [];
if (options.autogpt) {
promptOptions.push('+AutoGPT');
}
if (options.headless) {
promptOptions.push('+headless');
}
if (promptOptions.length > 0) {
prompt.message += ` (${promptOptions.join(' ')})`.green;
}
prompt.delimiter = '>'.green;
prompt.start();
const chatApi = new ChatOpenAI({
temperature: 0.1,
modelName: options.model ? options.model : 'gpt-4-1106-preview',
});
if (options.outputFilePath) {
createTestFile(options.outputFilePath);
}
process.on('exit', () => {
gracefulExit(options);
});
// eslint-disable-next-line no-constant-condition
while (true) {
const {task} = await prompt.get({
properties: {
task: {
message: ' Input a task\n',
required: false,
},
},
});
if (task === '') {
console.log('Please input a task or press CTRL+C to exit'.red);
} else {
try {
if (options.autogpt) {
await doActionWithAutoGPT(page, chatApi, task, options);
} else {
await interactWithPage(chatApi, page, task, options);
}
if (options.headless) {
await logPageScreenshot(page);
}
} catch (e) {
console.log('Execution failed');
console.log(e);
}
}
}
}
const program = new Command();
program
.option('-a, --autogpt', 'run with autogpt', false)
.option('-m, --model <model>', 'openai model to use', 'gpt-4-1106-preview')
.option('-o, --outputFilePath <outputFilePath>', 'path to store test code')
.option('-u, --url <url>', 'url to start on', 'https://www.google.com')
.option('-v, --viewport <viewport>', 'viewport size to use', '1280,720')
.option('-h, --headless', 'run in headless mode', false);
program.parse();
main(program.opts());