mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
refactor: Refactor code into smaller functions/methods
This commit is contained in:
@@ -19,7 +19,7 @@ import CopilotActionStatus from "Common/Types/Copilot/CopilotActionStatus";
|
||||
import PullRequest from "Common/Types/CodeRepository/PullRequest";
|
||||
import ServiceRepository from "Model/Models/ServiceRepository";
|
||||
import CopilotActionProcessingException from "./Exceptions/CopilotActionProcessingException";
|
||||
import ArrayUtil from "Common/Types/ArrayUtil";
|
||||
// import ArrayUtil from "Common/Types/ArrayUtil";
|
||||
|
||||
let currentFixCount: number = 1;
|
||||
|
||||
@@ -43,9 +43,11 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
|
||||
}`,
|
||||
);
|
||||
|
||||
const files: Array<CodeRepositoryFile> = ArrayUtil.shuffle(
|
||||
Object.values(filesInService),
|
||||
); // shuffle the files to avoid fixing the same file in each run.
|
||||
// const files: Array<CodeRepositoryFile> = ArrayUtil.shuffle(
|
||||
// Object.values(filesInService),
|
||||
// ); // shuffle the files to avoid fixing the same file in each run.
|
||||
|
||||
const files: Array<CodeRepositoryFile> = Object.values(filesInService);
|
||||
|
||||
for (const file of files) {
|
||||
checkIfCurrentFixCountIsLessThanFixNumberOfCodeEventsInEachRun();
|
||||
@@ -114,7 +116,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
|
||||
executionResult = await CopilotActionService.execute({
|
||||
serviceRepository: serviceRepository,
|
||||
copilotActionType: nextEventToFix,
|
||||
vars: {
|
||||
input: {
|
||||
currentFilePath: file.filePath, // this is the file path where optimization is needed or should start from.
|
||||
files: filesInService,
|
||||
},
|
||||
|
||||
@@ -4,11 +4,12 @@ import CopilotActionType from "Common/Types/Copilot/CopilotActionType";
|
||||
import LLM from "../LLM/LLM";
|
||||
import { GetLlmType } from "../../Config";
|
||||
import Text from "Common/Types/Text";
|
||||
import NotAcceptedFileExtentionForCopilotAction from "../../Exceptions/NotAcceptedFileExtention";
|
||||
import LocalFile from "CommonServer/Utils/LocalFile";
|
||||
import CodeRepositoryFile from "CommonServer/Utils/CodeRepository/CodeRepositoryFile";
|
||||
import Dictionary from "Common/Types/Dictionary";
|
||||
import { CopilotPromptResult } from "../LLM/LLMBase";
|
||||
import BadDataException from "Common/Types/Exception/BadDataException";
|
||||
import logger from "CommonServer/Utils/Logger";
|
||||
|
||||
export interface CopilotActionRunResult {
|
||||
files: Dictionary<CodeRepositoryFile>;
|
||||
@@ -37,16 +38,16 @@ export default class CopilotActionBase {
|
||||
|
||||
public acceptFileExtentions: string[] = [];
|
||||
|
||||
public constructor(data: {
|
||||
copilotActionType: CopilotActionType;
|
||||
acceptFileExtentions: string[];
|
||||
}) {
|
||||
public constructor() {
|
||||
this.llmType = GetLlmType();
|
||||
this.copilotActionType = data.copilotActionType;
|
||||
this.acceptFileExtentions = data.acceptFileExtentions;
|
||||
}
|
||||
|
||||
public async onBeforeExecute(data: CopilotProcess): Promise<CopilotProcess> {
|
||||
public async validateExecutionStep(data: CopilotProcess): Promise<boolean> {
|
||||
|
||||
if(!this.copilotActionType){
|
||||
throw new BadDataException("Copilot Action Type is not set");
|
||||
}
|
||||
|
||||
// check if the file extension is accepted or not
|
||||
|
||||
if (
|
||||
@@ -56,12 +57,14 @@ export default class CopilotActionBase {
|
||||
);
|
||||
})
|
||||
) {
|
||||
throw new NotAcceptedFileExtentionForCopilotAction(
|
||||
logger.info(
|
||||
`The file extension ${data.input.currentFilePath.split(".").pop()} is not accepted by the copilot action ${this.copilotActionType}. Ignore this file...`,
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public async onAfterExecute(data: CopilotProcess): Promise<CopilotProcess> {
|
||||
@@ -69,6 +72,11 @@ export default class CopilotActionBase {
|
||||
return data;
|
||||
}
|
||||
|
||||
public async onBeforeExecute(data: CopilotProcess): Promise<CopilotProcess> {
|
||||
// do nothing
|
||||
return data;
|
||||
}
|
||||
|
||||
public async getBranchName(): Promise<string> {
|
||||
const randomText: string = Text.generateRandomText(5);
|
||||
const bracnhName: string = `${Text.pascalCaseToDashes(this.copilotActionType).toLowerCase()}-${randomText}`;
|
||||
@@ -116,6 +124,10 @@ If you have any feedback or suggestions, please let us know. We would love to h
|
||||
}
|
||||
|
||||
public async execute(data: CopilotProcess): Promise<CopilotProcess | null> {
|
||||
|
||||
logger.info("Executing Copilot Action: " + this.copilotActionType);
|
||||
logger.info("Current File Path: " + data.input.currentFilePath);
|
||||
|
||||
data = await this.onBeforeExecute(data);
|
||||
|
||||
if (!data.result) {
|
||||
@@ -131,6 +143,14 @@ If you have any feedback or suggestions, please let us know. We would love to h
|
||||
let isActionComplete: boolean = false;
|
||||
|
||||
while (!isActionComplete) {
|
||||
|
||||
if(!await this.validateExecutionStep(data)){
|
||||
// execution step not valid
|
||||
// return data as it is
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
data = await this.onExecutionStep(data);
|
||||
|
||||
isActionComplete = await this.isActionComplete(data);
|
||||
|
||||
@@ -1,55 +1,18 @@
|
||||
import CopilotActionType from "Common/Types/Copilot/CopilotActionType";
|
||||
import CopilotActionBase, {
|
||||
CopilotActionPrompt,
|
||||
CopilotActionRunResult,
|
||||
CopilotProcess,
|
||||
} from "./CopilotActionsBase";
|
||||
import CodeRepositoryUtil from "../../Utils/CodeRepository";
|
||||
|
||||
export default class FixGrammarAndSpelling extends CopilotActionBase {
|
||||
public constructor() {
|
||||
super({
|
||||
copilotActionType: CopilotActionType.FIX_GRAMMAR_AND_SPELLING,
|
||||
acceptFileExtentions: [
|
||||
...CodeRepositoryUtil.getCodeFileExtentions(),
|
||||
...CodeRepositoryUtil.getReadmeFileExtentions(),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
public override async filterNoOperation(
|
||||
data: CopilotProcess,
|
||||
): Promise<CopilotProcess> {
|
||||
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]!;
|
||||
}
|
||||
|
||||
return {
|
||||
...data,
|
||||
result: finalResult,
|
||||
};
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.FIX_GRAMMAR_AND_SPELLING;
|
||||
this.acceptFileExtentions = [
|
||||
...CodeRepositoryUtil.getCodeFileExtentions(),
|
||||
...CodeRepositoryUtil.getReadmeFileExtentions(),
|
||||
];
|
||||
}
|
||||
|
||||
public override async getPrompt(
|
||||
|
||||
@@ -12,10 +12,10 @@ export default class ImproveComments extends CopilotActionBase {
|
||||
public isRequirementsMet: boolean = false;
|
||||
|
||||
public constructor() {
|
||||
super({
|
||||
copilotActionType: CopilotActionType.IMPROVE_COMMENTS,
|
||||
acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(),
|
||||
});
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.IMPROVE_COMMENTS;
|
||||
this.acceptFileExtentions = CodeRepositoryUtil.getCodeFileExtentions();
|
||||
|
||||
}
|
||||
|
||||
public override isActionComplete(_data: CopilotProcess): Promise<boolean> {
|
||||
|
||||
@@ -1,35 +1,16 @@
|
||||
import CopilotActionType from "Common/Types/Copilot/CopilotActionType";
|
||||
import CopilotActionBase, {
|
||||
CopilotActionPrompt,
|
||||
CopilotActionRunResult,
|
||||
CopilotProcess,
|
||||
} 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: CopilotProcess,
|
||||
): Promise<CopilotProcess> {
|
||||
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 { ...data, result: finalResult };
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.IMPROVE_VARIABLE_NAMES;
|
||||
this.acceptFileExtentions = CodeRepositoryUtil.getCodeFileExtentions();
|
||||
|
||||
}
|
||||
|
||||
public override async getPrompt(
|
||||
|
||||
@@ -7,10 +7,9 @@ import CodeRepositoryUtil from "../../Utils/CodeRepository";
|
||||
|
||||
export default class ImproveReadme extends CopilotActionBase {
|
||||
public constructor() {
|
||||
super({
|
||||
copilotActionType: CopilotActionType.IMRPOVE_README,
|
||||
acceptFileExtentions: CodeRepositoryUtil.getReadmeFileExtentions(),
|
||||
});
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.IMRPOVE_README;
|
||||
this.acceptFileExtentions = CodeRepositoryUtil.getReadmeFileExtentions();
|
||||
}
|
||||
|
||||
public override async getPrompt(
|
||||
|
||||
@@ -47,15 +47,13 @@ export default class CopilotActionService {
|
||||
|
||||
await CodeRepositoryUtil.pullChanges();
|
||||
|
||||
if (!actionDictionary[data.copilotActionType]) {
|
||||
const actionType: typeof CopilotActionBase | undefined = actionDictionary[data.copilotActionType];
|
||||
|
||||
if (!actionType) {
|
||||
throw new BadDataException("Invalid CopilotActionType");
|
||||
}
|
||||
|
||||
logger.info("Executing Copilot Action: " + data.copilotActionType);
|
||||
|
||||
const action: CopilotActionBase = new actionDictionary[
|
||||
data.copilotActionType
|
||||
]() as CopilotActionBase;
|
||||
const action: CopilotActionBase = new actionType() as CopilotActionBase;
|
||||
|
||||
const processResult: CopilotProcess | null = await action.execute({
|
||||
input: data.input,
|
||||
|
||||
@@ -1,38 +1,15 @@
|
||||
import CopilotActionType from "Common/Types/Copilot/CopilotActionType";
|
||||
import CopilotActionBase, {
|
||||
CopilotActionPrompt,
|
||||
CopilotActionRunResult,
|
||||
CopilotProcess,
|
||||
} from "./CopilotActionsBase";
|
||||
import CodeRepositoryUtil from "../../Utils/CodeRepository";
|
||||
|
||||
export default class RefactorCode extends CopilotActionBase {
|
||||
public constructor() {
|
||||
super({
|
||||
copilotActionType: CopilotActionType.REFACTOR_CODE,
|
||||
acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(),
|
||||
});
|
||||
}
|
||||
|
||||
public override async filterNoOperation(
|
||||
data: CopilotProcess,
|
||||
): Promise<CopilotProcess> {
|
||||
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 {
|
||||
...data,
|
||||
result: finalResult,
|
||||
};
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.REFACTOR_CODE;
|
||||
this.acceptFileExtentions = CodeRepositoryUtil.getCodeFileExtentions();
|
||||
}
|
||||
|
||||
public override async getPrompt(
|
||||
|
||||
@@ -4,10 +4,9 @@ import CodeRepositoryUtil from "../../Utils/CodeRepository";
|
||||
|
||||
export default class WriteUnitTests extends CopilotActionBase {
|
||||
public constructor() {
|
||||
super({
|
||||
copilotActionType: CopilotActionType.WRITE_UNIT_TESTS,
|
||||
acceptFileExtentions: CodeRepositoryUtil.getCodeFileExtentions(),
|
||||
});
|
||||
super();
|
||||
this.copilotActionType = CopilotActionType.WRITE_UNIT_TESTS;
|
||||
this.acceptFileExtentions = CodeRepositoryUtil.getCodeFileExtentions();
|
||||
}
|
||||
|
||||
public override async getPrompt(): Promise<CopilotActionPrompt> {
|
||||
|
||||
@@ -24,7 +24,7 @@ export default class Llama extends LlmBase {
|
||||
const serverUrl: URL = GetLlamaServerUrl();
|
||||
|
||||
const response: HTTPErrorResponse | HTTPResponse<JSONObject> =
|
||||
await API.post(URL.fromString(serverUrl.toString()).addRoute("/prompt"), {
|
||||
await API.post(URL.fromString(serverUrl.toString()).addRoute("/prompt/"), {
|
||||
messages: [
|
||||
{ role: "system", content: data.systemPrompt },
|
||||
{ role: "user", content: data.prompt },
|
||||
|
||||
Reference in New Issue
Block a user