@parvez3019/ai-code-review-gitlab-plugin
Version:

108 lines • 4.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Gemini = void 0;
const axios_1 = __importDefault(require("axios"));
const utils_1 = require("./utils");
const SAFETY_SETTINGS = [
{
category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_HATE_SPEECH',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_HARASSMENT',
threshold: 'BLOCK_NONE',
},
{
category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
threshold: 'BLOCK_NONE',
},
];
class Gemini {
constructor(config) {
if (!config.apiKey) {
throw new Error("API key is required for Gemini client");
}
this.apiKey = config.apiKey;
this.apiUrl = config.apiUrl || process.env.GEMINI_API_URL || "https://generativelanguage.googleapis.com";
this.model = config.model || process.env.GEMINI_MODEL || utils_1.geminiCompletionsConfig.model;
this.config = config;
this.apiClient = axios_1.default.create({
baseURL: this.apiUrl,
});
}
reviewCodeChange(change) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
if (!(change === null || change === void 0 ? void 0 : change.trim())) {
throw new Error("Code change cannot be empty");
}
const url = `/v1beta/models/${this.model}:generateContent?key=${this.apiKey}`;
const headers = {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41',
};
const [systemPrompt, codeReviewPrompt] = yield Promise.all([
this.config.systemPrompt || (0, utils_1.getSystemPrompt)(),
this.config.codeReviewPrompt || (0, utils_1.getCodeReviewPrompt)()
]);
const body = {
contents: [
{
role: 'user',
parts: [
{
text: change
}
]
}
],
systemInstruction: {
parts: [
{
text: systemPrompt
},
{
text: codeReviewPrompt
}
]
},
safetySettings: SAFETY_SETTINGS,
};
try {
const response = yield this.apiClient.post(url, body, {
headers: headers,
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Request failed with status ${response.status}`);
}
const data = response.data;
if (!((_e = (_d = (_c = (_b = (_a = data === null || data === void 0 ? void 0 : data.candidates) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.content) === null || _c === void 0 ? void 0 : _c.parts) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.text)) {
throw new Error("Invalid response format from Gemini");
}
return data.candidates[0].content.parts[0].text;
}
catch (error) {
console.error("Error calling Gemini:", error);
throw new Error(`Failed to get code review from Gemini: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
});
}
}
exports.Gemini = Gemini;
//# sourceMappingURL=gemini.js.map