mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
refactor: Extract code from CopilotActionBase to separate functions
This commit is contained in:
@@ -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<boolean> {
|
||||
return false;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<boolean> {
|
||||
if (data.result.code.includes("--all-good--")) {
|
||||
return true;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
|
||||
@@ -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<boolean> {
|
||||
if (data.result.code.includes("--all-good--")) {
|
||||
return true;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
|
||||
61
Copilot/Service/CopilotActions/ImproveVariableNames.ts
Normal file
61
Copilot/Service/CopilotActions/ImproveVariableNames.ts
Normal file
@@ -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<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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<boolean> {
|
||||
if (data.result.code.includes("--all-good--")) {
|
||||
return true;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
|
||||
@@ -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<boolean> {
|
||||
if (data.result.code.includes("--all-good--")) {
|
||||
return true;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
|
||||
@@ -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<boolean> {
|
||||
if (data.result.code.includes("--all-good--")) {
|
||||
return true;
|
||||
}): Promise<CopilotActionRunResult> {
|
||||
|
||||
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<CopilotActionPrompt> {
|
||||
|
||||
@@ -477,4 +477,39 @@ export default class CodeRepositoryUtil {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static getCodeFileExtentions(): Array<string> {
|
||||
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<string> {
|
||||
return [".md"];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user