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

113 lines • 5.74 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BedrockClient = void 0;
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
const utils_1 = require("./utils");
class BedrockClient {
constructor(config) {
this.maxRetries = 3;
this.retryDelay = 1000; // 1 second
if (!config.apiKey || !config.apiSecret) {
throw new Error("AWS credentials (apiKey and apiSecret) are required for Bedrock client");
}
this.config = config;
this.client = new client_bedrock_runtime_1.BedrockRuntimeClient({
region: process.env.AWS_REGION || config.region || "us-east-1",
credentials: {
accessKeyId: config.apiKey,
secretAccessKey: config.apiSecret,
},
});
// Use environment variable, config, or default model
this.model = process.env.AWS_BEDROCK_MODEL || config.model || "anthropic.claude-3-5-sonnet-20241022-v2:0";
}
sleep(ms) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(resolve => setTimeout(resolve, ms));
});
}
retryWithBackoff(operation_1) {
return __awaiter(this, arguments, void 0, function* (operation, retryCount = 0) {
var _a, _b, _c;
try {
return yield operation();
}
catch (error) {
const bedrockError = error;
// Check if error is retryable
const isRetryable = ((_a = bedrockError.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode) === 429 || // Rate limit
((_b = bedrockError.$metadata) === null || _b === void 0 ? void 0 : _b.httpStatusCode) === 500 || // Server error
((_c = bedrockError.$metadata) === null || _c === void 0 ? void 0 : _c.httpStatusCode) === 503; // Service unavailable
if (isRetryable && retryCount < this.maxRetries) {
const delay = this.retryDelay * Math.pow(2, retryCount);
console.log(`Retrying after ${delay}ms (attempt ${retryCount + 1}/${this.maxRetries})`);
yield this.sleep(delay);
return this.retryWithBackoff(operation, retryCount + 1);
}
throw error;
}
});
}
reviewCodeChange(change) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!(change === null || change === void 0 ? void 0 : change.trim())) {
throw new Error("Code change cannot be empty");
}
const [systemPrompt, codeReviewPrompt] = yield Promise.all([
this.config.systemPrompt || (0, utils_1.getSystemPrompt)(),
this.config.codeReviewPrompt || (0, utils_1.getCodeReviewPrompt)()
]);
const prompt = `${systemPrompt}\n\n${codeReviewPrompt}\n\n${change}`;
const command = new client_bedrock_runtime_1.InvokeModelCommand({
modelId: this.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
anthropic_version: "bedrock-2023-05-31",
max_tokens: 4096,
temperature: 0.7,
top_p: 0.95,
messages: [
{
role: "user",
content: prompt,
},
],
}),
});
try {
const response = yield this.retryWithBackoff(() => __awaiter(this, void 0, void 0, function* () {
var _c, _d;
const result = yield this.client.send(command);
const responseBody = JSON.parse(new TextDecoder().decode(result.body));
if (!((_d = (_c = responseBody === null || responseBody === void 0 ? void 0 : responseBody.content) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.text)) {
throw new Error("Invalid response format from Bedrock");
}
return responseBody.content[0].text;
}));
return response;
}
catch (error) {
const bedrockError = error;
console.error("Error calling Bedrock:", {
error: bedrockError.message,
statusCode: (_a = bedrockError.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode,
requestId: (_b = bedrockError.$metadata) === null || _b === void 0 ? void 0 : _b.requestId
});
throw new Error(`Failed to get code review from Bedrock: ${bedrockError.message}`);
}
});
}
}
exports.BedrockClient = BedrockClient;
//# sourceMappingURL=bedrock.js.map