refactor: Update LlmType enum values and improve error message for OneUptime LLM server not reachable

This commit is contained in:
Simon Larsen 2024-09-04 11:51:56 +01:00
parent b8fad24329
commit 0559997c01
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
6 changed files with 89 additions and 67 deletions

View File

@ -1,7 +1,6 @@
import CodeRepositoryUtil, {
CodeRepositoryResult,
RepoScriptType,
ServiceToImproveResult,
} from "./Utils/CodeRepository";
import InitUtil from "./Utils/Init";
import ServiceCopilotCodeRepositoryUtil from "./Utils/ServiceRepository";
@ -26,9 +25,8 @@ import PullRequest from "Common/Types/CodeRepository/PullRequest";
import ServiceCopilotCodeRepository from "Common/Models/DatabaseModels/ServiceCopilotCodeRepository";
import CopilotActionProcessingException from "./Exceptions/CopilotActionProcessingException";
import CopilotPullRequest from "Common/Models/DatabaseModels/CopilotPullRequest";
import CopilotPullRequestService from "./Service/CopilotPullRequest";
import PullRequestState from "Common/Types/CodeRepository/PullRequestState";
// import ArrayUtil from "Common/Types/ArrayUtil";
import ProcessUtil from "./Utils/Process";
let currentFixCount: number = 1;
@ -36,7 +34,7 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
// check if copilot is disabled.
if (GetIsCopilotDisabled()) {
logger.info("Copilot is disabled. Exiting.");
haltProcessWithSuccess();
ProcessUtil.haltProcessWithSuccess();
}
logger.info(`Using ${GetLlmType()} as the AI model.`);
@ -48,33 +46,18 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
const codeRepositoryResult: CodeRepositoryResult = await InitUtil.init();
// before cloning the repo, check if there are any services to improve.
const servicesToImprove =
await ServiceCopilotCodeRepositoryUtil.getServicesToImprove(
codeRepositoryResult,
);
await cloneRepository({
codeRepositoryResult,
});
const openPullRequests: Array<CopilotPullRequest> = await getOpenPRs();
await setUpRepository();
const servicesToImproveResult: Array<ServiceToImproveResult> =
await CodeRepositoryUtil.getServicesToImproveCode({
codeRepository: codeRepositoryResult.codeRepository,
serviceRepositories: codeRepositoryResult.serviceRepositories,
openPullRequests: openPullRequests,
});
const servicesToImprove: Array<ServiceCopilotCodeRepository> =
servicesToImproveResult.map(
(serviceToImproveResult: ServiceToImproveResult) => {
return serviceToImproveResult.serviceRepository;
},
);
if (servicesToImprove.length === 0) {
logger.info("No services to improve. Exiting.");
haltProcessWithSuccess();
}
for (const serviceRepository of servicesToImprove) {
checkIfCurrentFixCountIsLessThanFixNumberOfCodeEventsInEachRun();
@ -281,44 +264,10 @@ const checkIfCurrentFixCountIsLessThanFixNumberOfCodeEventsInEachRun: VoidFuncti
logger.info(
`Copilot has fixed ${FixNumberOfCodeEventsInEachRun} code events. Thank you for using Copilot. If you wish to fix more code events, please run Copilot again.`,
);
haltProcessWithSuccess();
ProcessUtil.haltProcessWithSuccess();
};
const haltProcessWithSuccess: VoidFunction = (): void => {
process.exit(0);
};
type GetOpenPRFunction = () => Promise<Array<CopilotPullRequest>>;
const getOpenPRs: GetOpenPRFunction = async (): Promise<
Array<CopilotPullRequest>
> => {
const openPRs: Array<CopilotPullRequest> = [];
// get all open pull requests.
const openPullRequests: Array<CopilotPullRequest> =
await CopilotPullRequestService.getOpenPullRequestsFromDatabase();
for (const openPullRequest of openPullRequests) {
// refresh status of this PR.
if (!openPullRequest.pullRequestId) {
continue;
}
const pullRequestState: PullRequestState =
await CopilotPullRequestService.refreshPullRequestStatus({
copilotPullRequest: openPullRequest,
});
if (pullRequestState === PullRequestState.Open) {
openPRs.push(openPullRequest);
}
}
return openPRs;
};
const setUpRepository: PromiseVoidFunction = async (): Promise<void> => {
const isSetupProperly: boolean =
await CodeRepositoryUtil.isRepoSetupProperly();
@ -338,7 +287,7 @@ const setUpRepository: PromiseVoidFunction = async (): Promise<void> => {
logger.info(
`There's an open setup PR for this repository: ${setupPullRequest.pullRequestId}. Please merge this PR to continue using Copilot. Exiting...`,
);
haltProcessWithSuccess();
ProcessUtil.haltProcessWithSuccess();
return;
}
@ -351,7 +300,7 @@ const setUpRepository: PromiseVoidFunction = async (): Promise<void> => {
". Please megre this PR to continue using Copilot. Exiting..",
);
haltProcessWithSuccess();
ProcessUtil.haltProcessWithSuccess();
};
export default init;

View File

@ -1,5 +1,5 @@
enum LlmType {
LLM = "OneUptime LLM Server", // OneUptime custom LLM Server
ONEUPTIME_LLM = "OneUptime LLM Server", // OneUptime custom LLM Server
OpenAI = "OpenAI",
}

View File

@ -18,7 +18,7 @@ import logger from "Common/Server/Utils/Logger";
export default class InitUtil {
public static async init(): Promise<CodeRepositoryResult> {
if (GetLlmType() === LlmType.LLM) {
if (GetLlmType() === LlmType.ONEUPTIME_LLM) {
const llmServerUrl: URL = GetLlmServerUrl();
// check status of ll, server
const result: HTTPErrorResponse | HTTPResponse<JSONObject> =
@ -26,7 +26,7 @@ export default class InitUtil {
if (result instanceof HTTPErrorResponse) {
throw new BadDataException(
"Llama server is not reachable. Please check the server URL in the environment variables.",
"OneUptime LLM server is not reachable. Please check the server URL in the environment variables.",
);
}
}

5
Copilot/Utils/Process.ts Normal file
View File

@ -0,0 +1,5 @@
export default class ProcessUtil {
public static haltProcessWithSuccess(): void {
process.exit(0);
}
}

View File

@ -0,0 +1,32 @@
import CopilotPullRequest from "Common/Models/DatabaseModels/CopilotPullRequest";
import CopilotPullRequestService from "../Service/CopilotPullRequest";
import PullRequestState from "Common/Types/CodeRepository/PullRequestState";
export default class PullRequestUtil {
public static async getOpenPRs(): Promise<Array<CopilotPullRequest>> {
const openPRs: Array<CopilotPullRequest> = [];
// get all open pull requests.
const openPullRequests: Array<CopilotPullRequest> =
await CopilotPullRequestService.getOpenPullRequestsFromDatabase();
for (const openPullRequest of openPullRequests) {
// refresh status of this PR.
if (!openPullRequest.pullRequestId) {
continue;
}
const pullRequestState: PullRequestState =
await CopilotPullRequestService.refreshPullRequestStatus({
copilotPullRequest: openPullRequest,
});
if (pullRequestState === PullRequestState.Open) {
openPRs.push(openPullRequest);
}
}
return openPRs;
}
}

View File

@ -7,9 +7,45 @@ import CodeRepositoryFile from "Common/Server/Utils/CodeRepository/CodeRepositor
import LocalFile from "Common/Server/Utils/LocalFile";
import ServiceCopilotCodeRepository from "Common/Models/DatabaseModels/ServiceCopilotCodeRepository";
import ServiceLanguageUtil from "Common/Utils/TechStack";
import CodeRepositoryUtil from "./CodeRepository";
import CodeRepositoryUtil, {
CodeRepositoryResult,
ServiceToImproveResult,
} from "./CodeRepository";
import PullRequestUtil from "./PullRequest";
import CopilotPullRequest from "Common/Models/DatabaseModels/CopilotPullRequest";
import logger from "Common/Server/Utils/Logger";
import ProcessUtil from "./Process";
export default class ServiceCopilotCodeRepositoryUtil {
public static async getServicesToImprove(
codeRepositoryResult: CodeRepositoryResult,
): Promise<Array<ServiceCopilotCodeRepository>> {
// before cloning the repo, check if there are any services to improve.
const openPullRequests: Array<CopilotPullRequest> =
await PullRequestUtil.getOpenPRs();
const servicesToImproveResult: Array<ServiceToImproveResult> =
await CodeRepositoryUtil.getServicesToImproveCode({
codeRepository: codeRepositoryResult.codeRepository,
serviceRepositories: codeRepositoryResult.serviceRepositories,
openPullRequests: openPullRequests,
});
const servicesToImprove: Array<ServiceCopilotCodeRepository> =
servicesToImproveResult.map(
(serviceToImproveResult: ServiceToImproveResult) => {
return serviceToImproveResult.serviceRepository;
},
);
if (servicesToImprove.length === 0) {
logger.info("No services to improve. Exiting.");
ProcessUtil.haltProcessWithSuccess();
}
return servicesToImprove;
}
public static async getFileLanguage(data: {
filePath: string;
}): Promise<TechStack> {