From 2415f97ec636dbf9deceb849de7d2804da53198c Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Sat, 22 Jun 2024 19:40:18 +0100 Subject: [PATCH] refactor: Add NotAcceptedFileExtentionForCopilotAction exception handling --- .../Exceptions/NotAcceptedFileExtention.ts | 9 +++++ Copilot/Init.ts | 39 ++++++++++++------- .../CopilotActions/CopilotActionsBase.ts | 22 ++++++++++- .../CopilotActions/FixGrammarAndSpelling.ts | 12 ++++-- .../Service/CopilotActions/ImproveComments.ts | 1 + 5 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 Copilot/Exceptions/NotAcceptedFileExtention.ts diff --git a/Copilot/Exceptions/NotAcceptedFileExtention.ts b/Copilot/Exceptions/NotAcceptedFileExtention.ts new file mode 100644 index 00000000000..63942c64b2c --- /dev/null +++ b/Copilot/Exceptions/NotAcceptedFileExtention.ts @@ -0,0 +1,9 @@ +import Exception from "Common/Types/Exception/Exception"; +import ExceptionCode from "Common/Types/Exception/ExceptionCode"; + + +export default class NotAcceptedFileExtentionForCopilotAction extends Exception { + public constructor(message: string) { + super(ExceptionCode.BadDataException, message); + } +} diff --git a/Copilot/Init.ts b/Copilot/Init.ts index d0cfe37982d..a28785ebce0 100644 --- a/Copilot/Init.ts +++ b/Copilot/Init.ts @@ -14,6 +14,7 @@ import CopilotActionService, { CopilotExecutionResult, } from "./Service/CopilotActions/Index"; import CopilotActionStatus from "Common/Types/Copilot/CopilotActionStatus"; +import NotAcceptedFileExtentionForCopilotAction from "./Exceptions/NotAcceptedFileExtention"; let currentFixCount: number = 1; @@ -29,8 +30,7 @@ const init: PromiseVoidFunction = async (): Promise => { }); logger.info( - `Files found in ${serviceRepository.serviceCatalog?.name}: ${ - Object.keys(filesInService).length + `Files found in ${serviceRepository.serviceCatalog?.name}: ${Object.keys(filesInService).length }`, ); @@ -72,20 +72,31 @@ const init: PromiseVoidFunction = async (): Promise => { continue; } - const executionResult: CopilotExecutionResult = - await CopilotActionService.execute({ - serviceRepository: serviceRepository, - copilotActionType: nextEventToFix, - vars: { - code: await ServiceRepositoryUtil.getFileContent({ + let executionResult: CopilotExecutionResult | null = null + + try { + + executionResult = + await CopilotActionService.execute({ + serviceRepository: serviceRepository, + copilotActionType: nextEventToFix, + vars: { + code: await ServiceRepositoryUtil.getFileContent({ + filePath: file.filePath, + }), filePath: file.filePath, - }), - filePath: file.filePath, - fileCommitHash: file.gitCommitHash, - }, - }); + fileCommitHash: file.gitCommitHash, + }, + }); + } catch (e) { + if (e instanceof NotAcceptedFileExtentionForCopilotAction) { + logger.info(e.message); + } else { + throw e; + } + } - if (executionResult.status === CopilotActionStatus.PR_CREATED) { + if (executionResult && executionResult.status === CopilotActionStatus.PR_CREATED) { currentFixCount++; } } diff --git a/Copilot/Service/CopilotActions/CopilotActionsBase.ts b/Copilot/Service/CopilotActions/CopilotActionsBase.ts index 3de5195a0ab..95dbec0547b 100644 --- a/Copilot/Service/CopilotActions/CopilotActionsBase.ts +++ b/Copilot/Service/CopilotActions/CopilotActionsBase.ts @@ -5,6 +5,7 @@ import BadDataException from "Common/Types/Exception/BadDataException"; import LLM from "../LLM/LLM"; import { GetLlmType } from "../../Config"; import Text from "Common/Types/Text"; +import NotAcceptedFileExtentionForCopilotAction from "../../Exceptions/NotAcceptedFileExtention"; export interface CopilotActionRunResult { code: string; @@ -22,18 +23,37 @@ export interface CopilotActionVars { } export default class CopilotActionBase { + public llmType: LlmType = LlmType.Llama; + public copilotActionType: CopilotActionType = CopilotActionType.IMPROVE_COMMENTS; // temp value which will be overridden in the constructor - public constructor(data: { copilotActionType: CopilotActionType }) { + public acceptFileExtentions: string[] = []; + + public constructor(data: { + copilotActionType: CopilotActionType + acceptFileExtentions: string[] + }) { this.llmType = GetLlmType(); this.copilotActionType = data.copilotActionType; + this.acceptFileExtentions = data.acceptFileExtentions; } public async onBeforeExecute(data: { vars: CopilotActionVars; }): Promise { + + // check if the file extension is accepted or not + + if (!this.acceptFileExtentions.includes(data.vars.filePath.split(".").pop() ?? "")) { + throw new NotAcceptedFileExtentionForCopilotAction( + `The file extension ${data.vars.filePath.split(".").pop()} is not accepted by the copilot action ${this.copilotActionType}. Ignore this file...`, + ); + } + + + return data.vars; } diff --git a/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts b/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts index a3131d26dd0..f2584320a42 100644 --- a/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts +++ b/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts @@ -9,6 +9,7 @@ export default class FixGrammarAndSpelling extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.FIX_GRAMMAR_AND_SPELLING, + acceptFileExtentions: [".ts", ".js", ".tsx", ".jsx", '.md'], }); } @@ -24,17 +25,20 @@ export default class FixGrammarAndSpelling extends CopilotActionBase { } protected override async _getPrompt(): Promise { - const prompt: string = `Please fix grammar and spelling in this code. + const prompt: string = `Please fix grammar and spelling in this file. - If you think the code is good and has no grammar or spelling mistakes, please reply with the following text: + If you think the file is good and has no grammar or spelling mistakes, please reply with the following text: --all-good-- - Here is the code: + Here is the file content: {{code}} `; - const systemPrompt: string = `You are an expert programmer.`; + const systemPrompt: string = `You are an expert programmer. Here are your instructions: +- You will follow the instructions given by the user strictly. +- You will not deviate from the instructions given by the user. +- You will not change the code unnecessarily. For example you will not change the code structure, logic, quotes around strings, or functionality.`; return { prompt: prompt, diff --git a/Copilot/Service/CopilotActions/ImproveComments.ts b/Copilot/Service/CopilotActions/ImproveComments.ts index 31cec7c046e..3d15bf7aa2d 100644 --- a/Copilot/Service/CopilotActions/ImproveComments.ts +++ b/Copilot/Service/CopilotActions/ImproveComments.ts @@ -9,6 +9,7 @@ export default class ImproveComments extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.IMPROVE_COMMENTS, + acceptFileExtentions: [".ts", ".js", ".tsx", ".jsx"], }); }