-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
339 lines (276 loc) · 10.6 KB
/
Copy pathindex.js
File metadata and controls
339 lines (276 loc) · 10.6 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const codebolt = require('@codebolt/codeboltjs').default;
const { GoogleSearch } = require('./browser/serarch');
const os = require('os');
const osType = os.type();
const {
Answer,
Action,
Coder,
Decision,
Feature,
Formatter,
InternalMonologue,
Patcher,
Planner,
Reporter,
Runner,
Researcher
} = require('./agents');
const { ReadCode } = require('./utils/code');
const { processKeywords } = require('./utils/keyword');
const { getAllMessagesFormatted } = require('./utils/message')
//Agents
const coder = new Coder();
const decision = new Decision();
const actionAgent = new Action();
const reporter = new Reporter();
const formatter = new Formatter();
const patcher = new Patcher();
const planner = new Planner();
const answer = new Answer();
const internalMonologue = new InternalMonologue();
const feature = new Feature();
const researcher = new Researcher();
const runner = new Runner()
//agent
// codebolt.chat.eventEmitter.addListener()
// }
/**
*
* @param {*} url
* @returns
*/
async function openPage(url) {
await codebolt.waitForConnection();
await codebolt.browser.newPage();
await codebolt.browser.goToPage(url);
const { text } = await codebolt.browser.extractText();
await codebolt.browser.close();
return { data: text }
}
/**
*
* @param {*} queries
* @param {*} projectName
* @returns
*/
async function searchQueries(queries, projectName) {
let results = {};
let webSearch = new GoogleSearch();
for (let query of queries) {
query = query.trim().toLowerCase();
await webSearch.search(query);
const link = webSearch.getFirstLink()
console.log("\nLink :: ", link, '\n');
if (!link) {
continue;
}
const [data] = await openPage(link);
// emitAgent("screenshot", { "data": raw, "project_name": projectName }, false);
results[query] = await formatter.execute(data);
// this.logger.info(`got the search results for : ${query}`);
// knowledgeBase.addKnowledge(tag=query, contents=results[query]);
}
return results;
}
/**
*
* @param {*} prompt
* @param {*} projectName
*/
async function subsequentExecute(prompt, projectName) {
try {
// const newMessage = this.projectManager.newMessage();
// newMessage.message = prompt;
// newMessage.fromDevika = false;
// this.projectManager.addMessageFromUser(projectName, newMessage.message);
// codebolt.chat.processStarted();
const osSystem = osType;
// this.agentState.setAgentActive(projectName, true);
let projectPath;
const appState = await codebolt.cbstate.getApplicationState();
projectPath = appState.state.projectState.projectPath;
let { chats } = await codebolt.chat.getChatHistory();
const conversation = await getAllMessagesFormatted(chats);
conversation.push((`User: ${prompt}`))
// projectPath = await codebolt.cbstate.getApplicationState();
const { markdown } = await codebolt.codeutils.getAllFilesAsMarkDown()
let codeMarkdown = markdown;
console.log(codeMarkdown);
let { response, action } = await actionAgent.execute({ conversation: conversation });
console.log(response);
console.log("\naction :: ", action, '\n');
await codebolt.chat.sendMessage(response);
//
if (action === "run") {
await runner.execute({
conversation: conversation,
code_markdown: codeMarkdown,
system_os: osSystem,
projectPath,
projectName
});
}
else if (action === "answer") {
const response = await answer.execute({
conversation,
code_markdown: codeMarkdown,
projectName
});
codebolt.chat.sendMessage(response);
} else if (action === "deploy") {
// const deployMetadata = Netlify.deploy(projectName);
// const deployUrl = deployMetadata.deployUrl;
// const response = {
// message: "Done! I deployed your project on Netlify.",
// deployUrl
// };
// this.projectManager.addMessageFromDevika(projectName, JSON.stringify(response, null, 4));
} else if (action === "feature") {
const code = await feature.execute({
conversation,
code_markdown: codeMarkdown,
systemOs: osType,
projectName
});
console.log("\nfeature code :: ", code, '\n');
feature.saveCodeToProject(code, projectName);
codebolt.chat.sendMessage("I have added the new feature you asked for.")
codebolt.git.commit("newFeaturAdded")
} else if (action === "bug") {
const code = await patcher.execute({
conversation,
code_markdown: codeMarkdown,
commands: null,
error: prompt,
systemOs: osSystem,
projectName
});
console.log("\nbug code :: ", code, '\n');
patcher.saveCodeToProject(code, projectName);
codebolt.chat.sendMessage("I have resovled bug in your code")
} else if (action === "report") {
const markdown = await reporter.execute(conversation, codeMarkdown, projectName);
// const outPdfFile = PDF.markdownToPdf(markdown, projectName);
// const projectNameSpaceUrl = projectName.replace(" ", "%20");
// const pdfDownloadUrl = `http://127.0.0.1:1337/api/download-project-pdf?project_name=${projectNameSpaceUrl}`;
// const response = `I have generated the PDF document. You can download it from here: ${pdfDownloadUrl}`;
// // await this.openPage(projectName, pdfDownloadUrl);
// this.projectManager.addMessageFromDevika(projectName, response);
}
codebolt.chat.stopProcess();
// this.agentState.setAgentActive(projectName, false);
// this.agentState.setAgentCompleted(projectName, true);
} catch (error) {
console.log(error)
codebolt.chat.sendMessage("An error occurred. Please try again later.",error);
codebolt.chat.stopProcess();
}
}
// (async () => {
// await execute()
// })();
codebolt.chat.onActionMessage().on("userMessage", async (req, response) => {
try {
await codebolt.waitForConnection();
// Create State
let { payload } = await codebolt.cbstate.getAgentState();
if (payload && payload.mainTaskFinished) {
let prompt = req.message.userMessage;
// userChatLister.on("userMessage", (message) => {
subsequentExecute(prompt);
// })
}
else {
let prompt = req.message.userMessage;
// console.log(prompt)
// codebolt.chat.processStarted();
// this.agentState.createState({ project: projectName });
await codebolt.cbstate.addToAgentState('mainTaskFinished', false);
const plannerResponse = await planner.raw_execute({ prompt });
let validatePlannerResponse = await planner.validate_response(plannerResponse);
const { reply, focus, plans, summary } = validatePlannerResponse;
codebolt.chat.sendMessage(reply);
for (const plan in plans) {
codebolt.taskplaner.addTask(plans[plan])
}
codebolt.chat.sendNotificationEvent('Plan Has Been Generated','planner');
codebolt.chat.sendMessage(`In summary: ${summary}`);
// this.updateContextualKeywords(focus);
// console.log("\ncontext_keywords :: ", this.collectedContextKeywords, '\n');
// const internalMonologueResponse = internalMonologue.execute({ current_prompt: plan });
// console.log("\ninternal_monologue :: ", internalMonologueResponse, '\n');
// const newState = this.agentState.newState();
// newState.internalMonologue = internalMonologue;
// this.agentState.addToCurrentState(projectName, newState);
// const research = researcher.execute(plan, this.collectedContextKeywords, { projectName });
let keywords = await processKeywords(focus);
let keywords_str = keywords.map(k => k.charAt(0).toUpperCase() + k.slice(1)).join(", ");
let research = await researcher.execute({
"step_by_step_plan": plannerResponse,
"contextual_keywords": keywords_str
});
console.log("\nresearch :: ", research, '\n');
let { queries, ask_user } = research;
queries=[];
const queriesCombined = queries.join(", ");
// In case you missed this part in the original code
if ((queries && queries.length > 0) || ask_user !== "") {
codebolt.chat.sendMessage(
`I am browsing the web to research the following queries: ${queriesCombined}.
\n If I need anything, I will make sure to ask you.`
);
}
if (queries.length === 0) {
codebolt.chat.sendMessage(
"I think I can proceed without searching the web."
);
}
let askUserPrompt = "Nothing from the user.";
if (ask_user !== "" && ask_user !== null) {
// this.projectManager.addMessageFromDevika(projectName, askUser);
// this.agentState.setAgentActive(projectName, false);
// let gotUserQuery = false;
// while (!gotUserQuery) {
// this.logger.info("Waiting for user query...");
// const latestMessageFromUser = this.projectManager.getLatestMessageFromUser(projectName);
// const validateLastMessageIsFromUser = this.projectManager.validateLastMessageIsFromUser(projectName);
// if (latestMessageFromUser && validateLastMessageIsFromUser) {
// askUserPrompt = latestMessageFromUser.message;
// gotUserQuery = true;
// this.projectManager.addMessageFromDevika(projectName, "Thanks! 🙌");
// }
// setTimeout(() => { }, 5000);
// }
}
// this.agentState.setAgentActive(projectName, true);
const searchResults = queries && queries.length > 0 ? await searchQueries(queries) : {};
console.log(searchResults)
const code = await coder.execute({
"step_by_step_plan": plannerResponse,
"user_context": askUserPrompt,
"search_results": searchResults,
"query": prompt
});
// console.log("\ncode :: ", code, '\n');
coder.save_code_to_project(code);
// this.agentState.setAgentActive(projectName, false);
// this.agentState.setAgentCompleted(projectName, true);
codebolt.chat.sendMessage(`I have completed the my task. \n
if you would like me to do anything else, please let me know. \n`
);
codebolt.chat.stopProcess();
await codebolt.cbstate.addToAgentState('mainTaskFinished', true);
codebolt.git.commit("mainTaskFinished")
// let userChatLister = codebolt.chat.userMessageListener();
// userChatLister.on("userMessage", (message) => {
// subsequentExecute(message);
// })
}
}
catch (error) {
console.log(error)
codebolt.chat.sendMessage("An error occurred. Please try again later.",error);
codebolt.chat.stopProcess();
}
})