Update the package correctly

This commit is contained in:
Aymeric Gaurat-Apelli 2024-10-02 16:02:33 +10:00
parent 6d9b91bb76
commit d1491f0083
3 changed files with 1304 additions and 1310 deletions

6
dist/index.js vendored
View file

@ -51,6 +51,7 @@ const minimatch_1 = __importDefault(__nccwpck_require__(2002));
const GITHUB_TOKEN = core.getInput("GITHUB_TOKEN");
const OPENAI_API_KEY = core.getInput("OPENAI_API_KEY");
const OPENAI_API_MODEL = core.getInput("OPENAI_API_MODEL");
const OPENAI_JSON_MODE = core.getInput("OPENAI_JSON_MODE");
const octokit = new rest_1.Octokit({ auth: GITHUB_TOKEN });
const openai = new openai_1.default({
apiKey: OPENAI_API_KEY,
@ -107,7 +108,7 @@ function analyzeCode(parsedDiff, prDetails) {
}
function createPrompt(file, chunk, prDetails) {
return `Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
- Reply in the JSON format without triple backticks: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
- Do not give positive comments or compliments.
- Provide comments and suggestions ONLY if there is something to improve, otherwise "reviews" should be an empty array.
- Write the comment in GitHub Markdown format.
@ -146,7 +147,7 @@ function getAIResponse(prompt) {
presence_penalty: 0,
};
try {
const response = yield openai.chat.completions.create(Object.assign(Object.assign(Object.assign({}, queryConfig), (OPENAI_API_MODEL === "gpt-4-1106-preview"
const response = yield openai.chat.completions.create(Object.assign(Object.assign(Object.assign({}, queryConfig), (OPENAI_JSON_MODE === "true"
? { response_format: { type: "json_object" } }
: {})), { messages: [
{
@ -155,6 +156,7 @@ function getAIResponse(prompt) {
},
] }));
const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "{}";
console.log("AI Response:", res);
return JSON.parse(res).reviews;
}
catch (error) {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -140,16 +140,8 @@ async function getAIResponse(prompt: string): Promise<Array<{
});
const res = response.choices[0].message?.content?.trim() || "{}";
// Remove lines ```json and ``` from the response
const jsonStart = res.indexOf("{");
const jsonEnd = res.lastIndexOf("}");
if (jsonStart === -1 || jsonEnd === -1) {
console.error("Error: JSON not found in response: " + res);
return null;
}
const jsonRes = res.substring(jsonStart, jsonEnd + 1);
console.log("AI Response:", jsonRes);
return JSON.parse(jsonRes).reviews;
console.log("AI Response:", res);
return JSON.parse(res).reviews;
} catch (error) {
console.error("Error:", error);
return null;