-
Notifications
You must be signed in to change notification settings - Fork 63
/
ui.js
167 lines (147 loc) · 6.58 KB
/
ui.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
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
// This file is the UI for the user. It accepts a TASK from the user and uses AI to complete the task. Tasks are related with code.
const chalk = require('chalk');
const path = require('path');
const fs = require('fs');
const { getSummaries, chunkSummaries, maxSummaryTokenCount } = require('./modules/summaries');
const { saveOutput, logPath, updateFile, newLog } = require('./modules/fsOutput');
const { printGitDiff } = require('./modules/gitHelper');
const { getFiles } = require('./modules/fsInput');
const { generateAndWriteFileSummary } = require('./modules/summaries');
const { getOptions } = require('./modules/cliOptions');
const { runAgent } = require('./modules/interactiveAgent');
const { getTask } = require('./modules/interactiveTask');
const { indexGapFill } = require('./modules/interactiveGapFill');
const { reindexCodeBase } = require('./modules/interactiveReindexCodeBase');
const { suggestChanges } = require('./agents/coder');
const { ChangesAdvice } = require('./agents/advisor');
const { finalAdvisor } = require('./agents/finalAdvisor');
const { getRelevantFiles } = require('./agents/getFiles');
const { tokensUsage,resetTokens } = require('./modules/gpt')
/**
*
* @param {string} task - The task to be completed.
* @param {boolean} test - Setting for internal tests.
* @returns {Array} - Array with file and code
*/
async function main(task, test=false, suggestionMode) {
newLog();
const options = getOptions(task, test);
let codeBaseDirectory = options.dir;
// TODO: get rid of test parameter, should use normal functionality
if (test){
const testingDirectory = '/benchmarks';
codeBaseDirectory = codeBaseDirectory + testingDirectory
}
const interactive = options.interactive;
const reindex = options.reindex;
const indexGapFillOption = options.indexGapFill;
let autoApply;
if (interactive){
autoApply = false;
} else {
autoApply = options.autoApply;
}
// init, reindex, or gap fill
const { initCodeBase } = require('./modules/init');
await initCodeBase(codeBaseDirectory, interactive);
if (reindex){
await reindexCodeBase(codeBaseDirectory, process.env.INDEXER_MODEL, interactive);
}
if (indexGapFillOption && !reindex) {
console.log(chalk.yellow(`Checking for gaps between the DB and the codebase and reconciling them.`));
await indexGapFill(codeBaseDirectory, interactive);
}
// Make sure we have a task, ask user if needed
task = await getTask(task, options);
// reset tokens counter for this new task
resetTokens()
// Get the summaries of the files in the directory
const summaries = await getSummaries(codeBaseDirectory);
const chunkedSummaries = chunkSummaries(summaries, maxSummaryTokenCount);
console.log(`Split summaries into ${chalk.yellow(chunkedSummaries.length)} chunks of up to ${chalk.yellow(maxSummaryTokenCount)} tokens each. (an agent would run for each)`)
let relevantFiles=[]
const promises = chunkedSummaries.map(async (summaries) => {
// Decide which files are relevant to the task
const relevantFilesChunk = await runAgent(getRelevantFiles, task, summaries, interactive);
return relevantFilesChunk;
});
relevantFiles = await Promise.all(promises).then((results) => {
// Combine all the results into a single array
return results.flat();
});
const uniqueRelevantFiles = relevantFiles.reduce((acc, current) => {
const isDuplicate = acc.find(file => file.path === current.path);
if (!isDuplicate) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
console.log(`${chalk.yellow(uniqueRelevantFiles.length)} relevant files were identified by the agent:`);
const existingUniqueRelevantFiles = uniqueRelevantFiles.filter(file => {
filePathFull = path.posix.join(codeBaseDirectory, file.path);
fileFound = fs.existsSync(filePathFull);
if (!fileFound) {
console.log(`${chalk.red(file.path)}: ${file.reason}`);
}
return fileFound;
});
const fileReasons = existingUniqueRelevantFiles.map(file => `${chalk.yellow(file.path)}: ${file.reason}`).join('\n');
console.log(fileReasons+'\n');
// Fetch code files the agent has deemed relevant
let files;
try {
files = getFiles(codeBaseDirectory, existingUniqueRelevantFiles);
} catch (err) {
console.log(chalk.red(`The agent has identified files to fetch we couldn't find, please try again with a different task.`));
// TODO: find which files are the broken ones and only print them.
const fileReasons = existingUniqueRelevantFiles.map(file => `${chalk.yellow(file.path)}: ${file.reason}`).join('\n');
console.log(fileReasons);
console.log(`Codebase directory: ${codeBaseDirectory}`)
process.exit(1);
}
if (files.length == 0) {
console.log(chalk.red(`The agent has not identified any relevant files for the task: ${task}.\nPlease try again with a different task.`));
process.exit(1);
}
let solutions = [];
await Promise.all(files.map(async (file) => {
// For each file, ask the coder agent for a solution
if (!suggestionMode) {
const coderRes = await runAgent(suggestChanges, task, file, interactive);
for (const file of coderRes){
const filePathRelative = file.fileToUpdate;
const fileContent = file.content;
solutions.push({file:filePathRelative, code:fileContent})
if (autoApply){
// This actually applies the solution to the file
const filePathFull = path.posix.join(codeBaseDirectory, filePathRelative);
updateFile(filePathFull, fileContent);
await generateAndWriteFileSummary(codeBaseDirectory, filePathRelative, fileContent);
}
// TODO: get current diff and feed it back to the next agent
}
} else {
// Ask advice agent for a suggestion
const advice = await runAgent(ChangesAdvice, task, {relevantFiles, file}, interactive);
solutions.push({file:file.path, code:advice})
}
}));
// Call final advisor agent to product final answer based on solutions
if (suggestionMode) {
const finalAdvice = await runAgent(finalAdvisor, task, {solutions}, interactive);
return { solution: finalAdvice, tokensUsage: tokensUsage() }
}
if (autoApply && !suggestionMode){
// Sends the saved output to GPT and ask for the necessary changes to do the TASK
console.log(chalk.green("Solutions Auto applied:"));
printGitDiff(codeBaseDirectory);
} else {
const solutionsPath = saveOutput(solutions);
console.log(chalk.green("Solutions saved to:", solutionsPath));
}
console.log(chalk.green("Process Log:", logPath()));
return solutions
}
if (require.main === module) main();
module.exports = { main }