Skip to content

Commit 40aa834

Browse files
committed
Initial push
1 parent 0925420 commit 40aa834

10 files changed

+1783
-0
lines changed

LLMTask.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const openai = require('openai');
2+
3+
openai.apiKey = 'YOUR_OPENAI_API_KEY';
4+
5+
class LLMTask {
6+
constructor(prompt, ws = null) {
7+
this.prompt = prompt;
8+
this.ws = ws;
9+
}
10+
11+
async perform() {
12+
const response = await openai.Completion.create({
13+
engine: 'text-davinci-002',
14+
prompt: this.prompt,
15+
max_tokens: 60
16+
});
17+
18+
const instruction = JSON.parse(response.choices[0].text);
19+
20+
console.log('Instruction:', instruction);
21+
22+
// If WebSocket connection is available, send the instruction to the client
23+
if (this.ws && this.ws.readyState === this.ws.OPEN) {
24+
this.ws.send(`Instruction: ${JSON.stringify(instruction)}`);
25+
}
26+
27+
return instruction;
28+
}
29+
}
30+
31+
module.exports = LLMTask;

agent.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class BasicAgent {
2+
async run(tasks) {
3+
// execute tasks sequentially
4+
for (const task of tasks) {
5+
await task.perform();
6+
}
7+
}
8+
}
9+
10+
module.exports = BasicAgent;

browseTask.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const puppeteer = require('puppeteer');
2+
3+
class BrowseTask {
4+
constructor(url, headless = "new", ws = null) {
5+
this.url = url;
6+
this.headless = headless;
7+
this.ws = ws;
8+
}
9+
10+
async perform() {
11+
const browser = await puppeteer.launch({ headless: this.headless });
12+
const page = await browser.newPage();
13+
await page.goto(this.url, { waitUntil: 'networkidle2' }); // waits until all network requests are finished
14+
const content = await page.content();
15+
16+
await browser.close();
17+
18+
console.log('Content:', content);
19+
20+
// If WebSocket connection is available, send the content to the client
21+
if (this.ws && this.ws.readyState === this.ws.OPEN) {
22+
this.ws.send(`Content: ${content}`);
23+
}
24+
25+
return content;
26+
}
27+
}
28+
29+
module.exports = BrowseTask;

main.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const BasicAgent = require('./agent');
2+
const TerminalTask = require('./task');
3+
const BrowseTask = require('./browseTask');
4+
const TaskQueue = require('./taskQueue');
5+
6+
const taskQueue = new TaskQueue();
7+
taskQueue.addTask(new TerminalTask('ls'));
8+
taskQueue.addTask(new BrowseTask('https://example.com'));
9+
10+
// const agent = new BasicAgent();
11+
// agent.run([new TerminalTask('ls')]);
12+
// agent.run([new TerminalTask('pwd')]);
13+
// const agent = new BasicAgent();
14+
// agent.run([new BrowseTask('https://example.com')]);
15+
16+
const agent = new BasicAgent();
17+
agent.run(taskQueue.queue).then(() => {
18+
console.log('All tasks have been executed');
19+
});

0 commit comments

Comments
 (0)