Skip to content

Commit

Permalink
refactor: Add NotAcceptedFileExtentionForCopilotAction exception hand…
Browse files Browse the repository at this point in the history
…ling
  • Loading branch information
simlarsen committed Jun 22, 2024
1 parent b6cabf1 commit 2415f97
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Copilot/Exceptions/NotAcceptedFileExtention.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 25 additions & 14 deletions Copilot/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,8 +30,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
});

logger.info(
`Files found in ${serviceRepository.serviceCatalog?.name}: ${
Object.keys(filesInService).length
`Files found in ${serviceRepository.serviceCatalog?.name}: ${Object.keys(filesInService).length
}`,
);

Expand Down Expand Up @@ -72,20 +72,31 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
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++;
}
}
Expand Down
22 changes: 21 additions & 1 deletion Copilot/Service/CopilotActions/CopilotActionsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CopilotActionVars> {

// 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;
}

Expand Down
12 changes: 8 additions & 4 deletions Copilot/Service/CopilotActions/FixGrammarAndSpelling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
}

Expand All @@ -24,17 +25,20 @@ export default class FixGrammarAndSpelling extends CopilotActionBase {
}

protected override async _getPrompt(): Promise<CopilotActionPrompt> {
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,
Expand Down
1 change: 1 addition & 0 deletions Copilot/Service/CopilotActions/ImproveComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class ImproveComments extends CopilotActionBase {
public constructor() {
super({
copilotActionType: CopilotActionType.IMPROVE_COMMENTS,
acceptFileExtentions: [".ts", ".js", ".tsx", ".jsx"],
});
}

Expand Down

0 comments on commit 2415f97

Please sign in to comment.