Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHallen122 committed Feb 6, 2025
1 parent b1d2bbf commit 59af234
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export class FileOperationManager {
}

private operationCount = 0;
async executeOperations(operations: FileOperation[]): Promise<void> {
async executeOperations(operations: FileOperation[]): Promise<string | null> {
// if (operations.length > 5) {
// throw new Error('Maximum 5 operations per fix');
// }

let newFilePath: string | null = null;

for (const op of operations) {
try {
switch (op.action) {
Expand All @@ -33,6 +35,7 @@ export class FileOperationManager {
break;
case 'rename':
await this.handleRename(op);
newFilePath = op.renamePath || null;
break;
}
} catch (error) {
Expand All @@ -42,6 +45,8 @@ export class FileOperationManager {
throw error;
}
}

return newFilePath;
}

private async handleWrite(op: FileOperation): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class FixResponseParser {
})
.filter(Boolean);

this.logger.log('Extracted operations:', operations);
// this.logger.log('Extracted operations:', operations);
return operations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class FrontendQueueProcessor {

constructor(
private validator: FrontendCodeValidator, // Path to your frontend project
private queue: CodeTaskQueue,
private context: BuilderContext, // The queue of files to process
private queue: CodeTaskQueue, // The queue of files to process
private context: BuilderContext,
private frontendPath: string,
) {}

Expand Down Expand Up @@ -53,7 +53,7 @@ export class FrontendQueueProcessor {
private async processSingleTask(task: FileTask): Promise<void> {
this.logger.log(`Processing file task: ${task.filePath}`);

const currentFullFilePath = normalizePath(
let currentFullFilePath = normalizePath(
path.resolve(this.frontendPath, task.filePath),
);

Expand Down Expand Up @@ -83,12 +83,24 @@ export class FrontendQueueProcessor {

// 3. Fix the file
try {
await this.fixFileGeneric(
const newFilePath = await this.fixFileGeneric(
currentFullFilePath,
task.filePath,
task,
validationResult.error ?? '',
task.dependenciesPath,
);

if (newFilePath !== null) {
this.logger.log(
`File was renamed: ${task.filePath}${newFilePath}`,
);
task.filePath = newFilePath;
currentFullFilePath = normalizePath(
path.resolve(this.frontendPath, newFilePath),
);
this.logger.log(
`Updated currentFullFilePath: ${currentFullFilePath}`,
);
}
} catch (error) {
this.logger.error(
'Fix File Generic failed, get error: ' + error.messages,
Expand All @@ -113,14 +125,15 @@ export class FrontendQueueProcessor {
*/
private async fixFileGeneric(
currentFullFilePath: string,
filePath: string,
task: FileTask,
rawErrorText: string,
dependenciesPath: string,
) {
): Promise<string | null> {
try {
this.logger.log(`Generic fix attempt for file: ${currentFullFilePath}`);
const originalContent = readFileSync(currentFullFilePath, 'utf-8');

this.logger.debug('raw error: ' + rawErrorText);

const fixPrompt = generateFileOperationPrompt();
const commonIssuePrompt = generateCommonErrorPrompt();

Expand All @@ -138,12 +151,12 @@ export class FrontendQueueProcessor {
{ role: 'system', content: fixPrompt },
{
role: 'user',
content: ` Current file path that need to be fix: \n ${filePath}`,
content: ` Current file path that need to be fix: \n ${task.filePath}`,
},
{ role: 'user', content: ` Error messages: \n ${rawErrorText}` },
{
role: 'user',
content: ` dependency file Paths: \n ${dependenciesPath}`,
content: ` dependency file Paths: \n ${task.dependenciesPath}`,
},
{
role: 'user',
Expand All @@ -163,14 +176,22 @@ export class FrontendQueueProcessor {
);

this.logger.debug('Fix Response: ' + fixResponse);
this.logger.debug('dependency file Paths ' + dependenciesPath);
this.logger.debug('dependency file Paths ' + task.dependenciesPath);
const parsed_fixResponse = removeCodeBlockFences(fixResponse);

const operations = parser.parse(parsed_fixResponse, filePath);
const operations = parser.parse(parsed_fixResponse, task.filePath);

const newFilePath =
await fileOperationManager.executeOperations(operations);

await fileOperationManager.executeOperations(operations);
this.logger.log(`Generic fix applied to file: ${task.filePath}`);

if (newFilePath) {
this.logger.log(`File was renamed: ${task.filePath}${newFilePath}`);
return newFilePath;
}

this.logger.log(`Generic fix applied to file: ${filePath}`);
return null;
} catch (error) {
this.logger.error('Generic Fix file: ' + error.message);
}
Expand Down

0 comments on commit 59af234

Please sign in to comment.