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

159 lines • 6.85 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLineObj = exports.getDiffBlocks = exports.delay = exports.geminiCompletionsConfig = exports.codeReviewPrompt = exports.systemPrompt = exports.getCodeReviewPrompt = exports.getSystemPrompt = exports.loadPrompt = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const client_s3_1 = require("@aws-sdk/client-s3");
const DEFAULT_SYSTEM_PROMPT_PATH = path.join(__dirname, 'prompts/system_prompt.txt');
const DEFAULT_CODE_REVIEW_PROMPT_PATH = path.join(__dirname, 'prompts/code_review_prompt.txt');
const isS3Path = (path) => {
return path.startsWith('s3://');
};
const parseS3Path = (s3Path) => {
const match = s3Path.match(/^s3:\/\/([^\/]+)\/(.+)$/);
if (!match) {
throw new Error(`Invalid S3 path format: ${s3Path}`);
}
return {
bucket: match[1],
key: match[2],
region: process.env.S3_REGION,
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY
};
};
const loadFromS3 = (config) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const s3Client = new client_s3_1.S3Client({
region: config.region || process.env.AWS_REGION || 'us-east-1',
credentials: config.accessKeyId && config.secretAccessKey ? {
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey
} : undefined
});
try {
const command = new client_s3_1.GetObjectCommand({
Bucket: config.bucket,
Key: config.key
});
const response = yield s3Client.send(command);
const bodyContents = yield ((_a = response.Body) === null || _a === void 0 ? void 0 : _a.transformToString());
if (!bodyContents) {
throw new Error('Empty response from S3');
}
return bodyContents.trim();
}
catch (error) {
throw new Error(`Failed to load from S3: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
});
const loadFromFile = (filePath) => {
try {
return fs.readFileSync(filePath, 'utf-8').trim();
}
catch (error) {
throw new Error(`Failed to load from file: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
};
const loadPrompt = (defaultPath, customPath) => __awaiter(void 0, void 0, void 0, function* () {
if (!customPath) {
return loadFromFile(defaultPath);
}
try {
if (isS3Path(customPath)) {
const s3Config = parseS3Path(customPath);
return yield loadFromS3(s3Config);
}
else {
return loadFromFile(customPath);
}
}
catch (error) {
console.warn(`Failed to load prompt from ${customPath}, falling back to default: ${error instanceof Error ? error.message : 'Unknown error'}`);
return loadFromFile(defaultPath);
}
});
exports.loadPrompt = loadPrompt;
const getSystemPrompt = (customPath) => __awaiter(void 0, void 0, void 0, function* () {
return (0, exports.loadPrompt)(DEFAULT_SYSTEM_PROMPT_PATH, customPath);
});
exports.getSystemPrompt = getSystemPrompt;
const getCodeReviewPrompt = (customPath) => __awaiter(void 0, void 0, void 0, function* () {
return (0, exports.loadPrompt)(DEFAULT_CODE_REVIEW_PROMPT_PATH, customPath);
});
exports.getCodeReviewPrompt = getCodeReviewPrompt;
// For backward compatibility - these will be deprecated
exports.systemPrompt = fs.readFileSync(DEFAULT_SYSTEM_PROMPT_PATH, 'utf-8').trim();
exports.codeReviewPrompt = fs.readFileSync(DEFAULT_CODE_REVIEW_PROMPT_PATH, 'utf-8').trim();
exports.geminiCompletionsConfig = {
temperature: 1,
topP: 0.95,
topK: 64,
maxOutputTokens: 8192,
responseMimeType: "text/plain",
model: "gemini-1.5-flash",
};
const delay = (time) => {
return new Promise(resolve => setTimeout(resolve, time));
};
exports.delay = delay;
const getDiffBlocks = (diff) => {
const regex = /(?=@@\s-\d+(?:,\d+)?\s\+\d+(?:,\d+)?\s@@)/g;
const diffBlocks = diff.split(regex);
return diffBlocks;
};
exports.getDiffBlocks = getDiffBlocks;
const getLineObj = (matches, item) => {
var _a, _b, _c;
const lineObj = {};
const lastLine = (_c = (_b = (_a = item.split(/\r?\n/)) === null || _a === void 0 ? void 0 : _a.reverse()) === null || _b === void 0 ? void 0 : _b[1]) === null || _c === void 0 ? void 0 : _c.trim();
const oldLineStart = +matches[1];
const oldLineEnd = +matches[2] || 0;
const newLineStart = +matches[3];
const newLineEnd = +matches[4] || 0;
if ((lastLine === null || lastLine === void 0 ? void 0 : lastLine[0]) === '+') {
lineObj.new_line = newLineStart + newLineEnd - 1;
}
else if ((lastLine === null || lastLine === void 0 ? void 0 : lastLine[0]) === '-') {
lineObj.old_line = oldLineStart + oldLineEnd - 1;
}
else {
lineObj.new_line = newLineStart + newLineEnd - 1;
lineObj.old_line = oldLineStart + oldLineEnd - 1;
}
return lineObj;
};
exports.getLineObj = getLineObj;
//# sourceMappingURL=utils.js.map