diff --git a/Copilot/Service/CopilotActions/CopilotActionsBase.ts b/Copilot/Service/CopilotActions/CopilotActionsBase.ts index d103dfc825..6d31a66317 100644 --- a/Copilot/Service/CopilotActions/CopilotActionsBase.ts +++ b/Copilot/Service/CopilotActions/CopilotActionsBase.ts @@ -142,11 +142,11 @@ If you have any feedback or suggestions, please let us know. We would love to h return actionResult; } - public async isNoOperation(_data: { + public async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - return false; + }): Promise { + return Promise.resolve(data.result); } public async execute(data: { @@ -160,9 +160,18 @@ If you have any feedback or suggestions, please let us know. We would love to h vars: data.vars, }); - const result: CopilotActionRunResult = await LLM.getResponse(prompt); + let result: CopilotActionRunResult = await LLM.getResponse(prompt); - if (await this.isNoOperation({ vars: data.vars, result: result })) { + if (Object.keys(result.files).length === 0) { + return null; + } + + result = await this.filterNoOperation({ + vars: data.vars, + result: result, + }); + + if (Object.keys(result.files).length === 0) { return null; } diff --git a/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts b/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts index ecadd6d754..8f4664776e 100644 --- a/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts +++ b/Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts @@ -4,38 +4,45 @@ import CopilotActionBase, { CopilotActionRunResult, CopilotActionVars, } from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; export default class FixGrammarAndSpelling extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.FIX_GRAMMAR_AND_SPELLING, - acceptFileExtentions: [".ts", ".js", ".tsx", ".jsx", ".md"], + acceptFileExtentions: [ + ...CodeRepositoryUtil.getCodeFileExtentions(), + ...CodeRepositoryUtil.getReadmeFileExtentions(), + ], }); } - public override async isNoOperation(data: { + public override async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - if (data.result.code.includes("--all-good--")) { - return true; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + if(data.result.files[filePath]?.fileContent.includes("does not contain") && data.result.files[filePath]?.fileContent.includes("spelling mistakes")) { + continue; + } + + if(data.result.files[filePath]?.fileContent.includes("does not contain") && data.result.files[filePath]?.fileContent.includes("grammar")) { + continue; + } + + finalResult.files[filePath] = data.result.files[filePath]!; } - if ( - data.result.code.includes("does not contain") && - data.result.code.includes("spelling mistakes") - ) { - return true; - } - - if ( - data.result.code.includes("does not contain") && - data.result.code.includes("grammar") - ) { - return true; - } - - return false; + return finalResult; } protected override async _getPrompt(): Promise { diff --git a/Copilot/Service/CopilotActions/ImproveComments.ts b/Copilot/Service/CopilotActions/ImproveComments.ts index c6a669db7d..a9d0d6b597 100644 --- a/Copilot/Service/CopilotActions/ImproveComments.ts +++ b/Copilot/Service/CopilotActions/ImproveComments.ts @@ -4,38 +4,13 @@ import CopilotActionBase, { CopilotActionRunResult, CopilotActionVars, } from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; export default class ImproveComments extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.IMPROVE_COMMENTS, - acceptFileExtentions: [ - ".ts", - ".js", - ".tsx", - ".jsx", - ".py", - ".go", - ".java", - ".c", - ".cpp", - ".cs", - ".swift", - ".php", - ".rb", - ".rs", - ".kt", - ".dart", - ".sh", - ".pl", - ".lua", - ".r", - ".scala", - ".ts", - ".js", - ".tsx", - ".jsx", - ], + acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(), }); } @@ -46,15 +21,26 @@ export default class ImproveComments extends CopilotActionBase { return Promise.resolve(data.result); } - public override async isNoOperation(data: { + public override async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - if (data.result.code.includes("--all-good--")) { - return true; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + + finalResult.files[filePath] = data.result.files[filePath]!; + } - return false; + return finalResult; } protected override async _getPrompt(): Promise { diff --git a/Copilot/Service/CopilotActions/ImproveVariableNames.ts b/Copilot/Service/CopilotActions/ImproveVariableNames.ts new file mode 100644 index 0000000000..b2d9c5e829 --- /dev/null +++ b/Copilot/Service/CopilotActions/ImproveVariableNames.ts @@ -0,0 +1,61 @@ +import CopilotActionType from "Common/Types/Copilot/CopilotActionType"; +import CopilotActionBase, { + CopilotActionPrompt, + CopilotActionRunResult, + CopilotActionVars, +} from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; + + +export default class ImproveVariableNames extends CopilotActionBase { + public constructor() { + super({ + copilotActionType: CopilotActionType.IMPROVE_VARIABLE_NAMES, + acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(), + }); + } + + public override async filterNoOperation(data: { + vars: CopilotActionVars; + result: CopilotActionRunResult; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + + finalResult.files[filePath] = data.result.files[filePath]!; + + } + + return finalResult; + } + + protected override async _getPrompt(): Promise { + const prompt: string = `Please improve this readme. + + If you think the readme is already well commented, please reply with the following text: + --all-good-- + + Here is the readme content. This is in {{fileLanguage}}: + + {{code}} + `; + + 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, + systemPrompt: systemPrompt, + }; + } +} diff --git a/Copilot/Service/CopilotActions/ImroveReadme.ts b/Copilot/Service/CopilotActions/ImroveReadme.ts index 2849f7a3e8..1d1cd505d2 100644 --- a/Copilot/Service/CopilotActions/ImroveReadme.ts +++ b/Copilot/Service/CopilotActions/ImroveReadme.ts @@ -4,24 +4,36 @@ import CopilotActionBase, { CopilotActionRunResult, CopilotActionVars, } from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; export default class ImproveReadme extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.IMRPOVE_README, - acceptFileExtentions: [".md"], + acceptFileExtentions: CodeRepositoryUtil.getReadmeFileExtentions(), }); } - public override async isNoOperation(data: { + public override async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - if (data.result.code.includes("--all-good--")) { - return true; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + + finalResult.files[filePath] = data.result.files[filePath]!; + } - return false; + return finalResult; } protected override async _getPrompt(): Promise { diff --git a/Copilot/Service/CopilotActions/RefactorCode.ts b/Copilot/Service/CopilotActions/RefactorCode.ts index 390bfdf8e2..d2a38d6b45 100644 --- a/Copilot/Service/CopilotActions/RefactorCode.ts +++ b/Copilot/Service/CopilotActions/RefactorCode.ts @@ -4,50 +4,36 @@ import CopilotActionBase, { CopilotActionRunResult, CopilotActionVars, } from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; export default class RefactorCode extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.REFACTOR_CODE, - acceptFileExtentions: [ - ".ts", - ".js", - ".tsx", - ".jsx", - ".py", - ".go", - ".java", - ".c", - ".cpp", - ".cs", - ".swift", - ".php", - ".rb", - ".rs", - ".kt", - ".dart", - ".sh", - ".pl", - ".lua", - ".r", - ".scala", - ".ts", - ".js", - ".tsx", - ".jsx", - ], + acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(), }); } - public override async isNoOperation(data: { + public override async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - if (data.result.code.includes("--all-good--")) { - return true; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + + finalResult.files[filePath] = data.result.files[filePath]!; + } - return false; + return finalResult; } protected override async _getPrompt(): Promise { diff --git a/Copilot/Service/CopilotActions/WriteUnitTests.ts b/Copilot/Service/CopilotActions/WriteUnitTests.ts index 9f09b3ab19..bd0cdc752f 100644 --- a/Copilot/Service/CopilotActions/WriteUnitTests.ts +++ b/Copilot/Service/CopilotActions/WriteUnitTests.ts @@ -4,50 +4,36 @@ import CopilotActionBase, { CopilotActionRunResult, CopilotActionVars, } from "./CopilotActionsBase"; +import CodeRepositoryUtil from "../../Utils/CodeRepository"; export default class WriteUnitTests extends CopilotActionBase { public constructor() { super({ copilotActionType: CopilotActionType.WRITE_UNIT_TESTS, - acceptFileExtentions: [ - ".ts", - ".js", - ".tsx", - ".jsx", - ".py", - ".go", - ".java", - ".c", - ".cpp", - ".cs", - ".swift", - ".php", - ".rb", - ".rs", - ".kt", - ".dart", - ".sh", - ".pl", - ".lua", - ".r", - ".scala", - ".ts", - ".js", - ".tsx", - ".jsx", - ], + acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(), }); } - public override async isNoOperation(data: { + public override async filterNoOperation(data: { vars: CopilotActionVars; result: CopilotActionRunResult; - }): Promise { - if (data.result.code.includes("--all-good--")) { - return true; + }): Promise { + + const finalResult: CopilotActionRunResult = { + files: {}, + }; + + for(const filePath in data.result.files) { + if(data.result.files[filePath]?.fileContent.includes("--all-good--")) { + continue; + } + + + finalResult.files[filePath] = data.result.files[filePath]!; + } - return false; + return finalResult; } protected override async _getPrompt(): Promise { diff --git a/Copilot/Utils/CodeRepository.ts b/Copilot/Utils/CodeRepository.ts index ad8d054a16..7c04b76d7c 100644 --- a/Copilot/Utils/CodeRepository.ts +++ b/Copilot/Utils/CodeRepository.ts @@ -477,4 +477,39 @@ export default class CodeRepositoryUtil { }, ); } + + + public static getCodeFileExtentions(): Array { + return [ + ".ts", + ".js", + ".tsx", + ".jsx", + ".py", + ".go", + ".java", + ".c", + ".cpp", + ".cs", + ".swift", + ".php", + ".rb", + ".rs", + ".kt", + ".dart", + ".sh", + ".pl", + ".lua", + ".r", + ".scala", + ".ts", + ".js", + ".tsx", + ".jsx", + ]; + } + + public static getReadmeFileExtentions(): Array { + return [".md"]; + } }