Repository navigation
Expand file tree
/
Copy pathAutonomousAgent.ts
More file actions
208 lines (178 loc) · 5.21 KB
/
Copy pathAutonomousAgent.ts
File metadata and controls
208 lines (178 loc) · 5.21 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
import type { Message } from "./ChatWindow";
import axios from "axios";
class AutonomousAgent {
name: string;
goal: string;
tasks: string[] = [];
customApiKey: string;
isRunning = true;
sendMessage: (message: Message) => void;
shutdown: () => void;
numLoops = 0;
constructor(
name: string,
goal: string,
addMessage: (message: Message) => void,
shutdown: () => void,
customApiKey: string
) {
this.name = name;
this.goal = goal;
this.sendMessage = addMessage;
this.shutdown = shutdown;
this.customApiKey = customApiKey;
}
async run() {
this.sendGoalMessage();
this.sendThinkingMessage();
// Initialize by getting tasks
try {
this.tasks = await this.getInitialTasks();
for (const task of this.tasks) {
await new Promise((r) => setTimeout(r, 800));
this.sendTaskMessage(task);
}
} catch (e) {
console.log(e);
this.sendErrorMessage(
`ERROR retrieving initial tasks array. Check your API key, make your goal more clear, or revise your goal such that it is within our model's policies to run. Shutting Down.`
);
this.shutdown();
return;
}
await this.loop();
}
async loop() {
console.log(`Loop ${this.numLoops}`);
console.log(this.tasks);
if (!this.isRunning) {
this.sendManualShutdownMessage();
this.shutdown();
return;
}
if (this.tasks.length === 0) {
this.sendCompletedMessage();
this.shutdown();
return;
}
this.numLoops += 1;
const maxLoops = this.customApiKey === "" ? 3 : 30;
if (this.numLoops > maxLoops) {
this.sendLoopMessage();
this.shutdown();
return;
}
// Wait before starting
await new Promise((r) => setTimeout(r, 1000));
// Execute first task
// Get and remove first task
const currentTask = this.tasks.shift();
this.sendThinkingMessage();
const result = await this.executeTask(currentTask as string);
this.sendExecutionMessage(currentTask as string, result);
// Wait before adding tasks
await new Promise((r) => setTimeout(r, 1000));
this.sendThinkingMessage();
// Add new tasks
try {
const newTasks = await this.getAdditionalTasks(
currentTask as string,
result
);
this.tasks = this.tasks.concat(newTasks);
for (const task of newTasks) {
await new Promise((r) => setTimeout(r, 800));
this.sendTaskMessage(task);
}
if (newTasks.length == 0) {
this.sendActionMessage("Task marked as complete!");
}
} catch (e) {
console.log(e);
this.sendErrorMessage(
`ERROR adding additional task(s). It might have been against our model's policies to run them. Continuing.`
);
this.sendActionMessage("Task marked as complete.");
}
await this.loop();
}
async getInitialTasks(): Promise<string[]> {
const res = await axios.post(`/api/chain`, {
customApiKey: this.customApiKey,
goal: this.goal,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument
return res.data.tasks as string[];
}
async getAdditionalTasks(
currentTask: string,
result: string
): Promise<string[]> {
const res = await axios.post(`/api/create`, {
customApiKey: this.customApiKey,
goal: this.goal,
tasks: this.tasks,
lastTask: currentTask,
result: result,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access
return res.data.tasks as string[];
}
async executeTask(task: string): Promise<string> {
const res = await axios.post(`/api/execute`, {
customApiKey: this.customApiKey,
goal: this.goal,
task: task,
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument
return res.data.response as string;
}
stopAgent() {
this.isRunning = false;
}
sendGoalMessage() {
this.sendMessage({ type: "goal", value: this.goal });
}
sendLoopMessage() {
this.sendMessage({
type: "system",
value: `We're sorry, because this is a demo, we cannot have our agents running for too long. Note, if you desire longer runs, please provide your own API key in Settings. Shutting down.`,
});
}
sendManualShutdownMessage() {
this.sendMessage({
type: "system",
value: `The agent has been manually shutdown.`,
});
}
sendCompletedMessage() {
this.sendMessage({
type: "system",
value: "All tasks completed. Shutting down.",
});
}
sendThinkingMessage() {
this.sendMessage({ type: "thinking", value: "" });
}
sendTaskMessage(task: string) {
this.sendMessage({ type: "task", value: task });
}
sendErrorMessage(error: string) {
this.sendMessage({ type: "system", value: error });
}
sendExecutionMessage(task: string, execution: string) {
this.sendMessage({
type: "action",
info: `Executing "${task}"`,
value: execution,
});
}
sendActionMessage(message: string) {
this.sendMessage({
type: "action",
info: message,
value: "",
});
}
}
export default AutonomousAgent;