sfcoe-ailabs
Version:
AI-powered code review tool with static analysis integration for comprehensive code quality assessment.
109 lines (108 loc) • 3.39 kB
JavaScript
import * as process from 'node:process';
import { Logger } from '../utils/observability.js';
import { COMMAND_DEFAULTS } from '../utils/constants.js';
import { reviewInstructions, reviewPrompt } from './aiPrompts.js';
export default class AIProvider {
apiKey;
model;
maxTokens = process.env.MAX_TOKENS_PER_REQUEST &&
!isNaN(Number(process.env.MAX_TOKENS_PER_REQUEST))
? Number(process.env.MAX_TOKENS_PER_REQUEST)
: COMMAND_DEFAULTS.DEFAULT_MAX_TOKENS;
apiEndpoint;
apiVersion;
/**
* Creates a new AI provider instance
*
* @param apiKey - The API key for the AI provider
* @param model - The model name to use for AI processing
*/
constructor(apiKey, model, apiEndpoint, apiVersion) {
this.apiKey = apiKey;
this.model = model;
this.apiEndpoint = apiEndpoint;
this.apiVersion = apiVersion;
}
/**
* Generates a prompt for AI code review based on code snippet and hints
*
* @param codeSnippet - The code to be reviewed
* @param hints - Array of review hints from static analysis tools
* @returns Formatted prompt string for AI review
*/
static generatePrompt(codeSnippet, hints) {
const hintsText = hints
.map((hint) => AIProvider.formatHints(hint))
.join('\n');
const aiPrompt = COMMAND_DEFAULTS.DEFAULT_PROMPT_TEMPLATE.replace('$prompt', reviewPrompt)
.replace('$codeSnippet', codeSnippet)
.replace('$hintsText', hintsText);
Logger.debug(`Prompt for AI review: \n ${aiPrompt}`, aiPrompt);
return aiPrompt;
}
/**
* Gets the review instructions for AI providers
*
* @returns The review instructions string
*/
static getReviewInstructions() {
return reviewInstructions;
}
/**
* Formats review hints into a readable string format
*
* @param hint - The review hint to format
* @returns Formatted hint string
*/
static formatHints(hint) {
return `- ${hint.ruleId}: ${hint.message}\n - Region\n - StartLine: ${hint.region.startLine ?? ''}\n - EndLine: ${hint.region.endLine ?? ''}\n - StartColumn: ${hint.region.startColumn ?? ''}\n - EndColumn: ${hint.region.endColumn ?? ''}\n - ClassName: ${hint.className}`;
}
/**
* Gets the API endpoint for the AI provider, if set
*
* @returns The API endpoint for the AI provider, if set
*/
getAPIEndpoint() {
return this.apiEndpoint;
}
/**
* Gets the API version for the AI provider, if set
*
* @returns The API version for the AI provider, if set
*/
getAPIVersion() {
return this.apiVersion;
}
/**
* Gets the AI model name
*
* @returns The model name
*/
getModel() {
return this.model;
}
/**
* Gets the API key
*
* @returns The API key
*/
getApiKey() {
return this.apiKey;
}
/**
* Gets the maximum number of tokens for AI requests
*
* @returns The maximum token count
*/
getMaxTokens() {
return this.maxTokens;
}
/**
* Sets the maximum number of tokens for AI requests
*
* @param maxTokens - The maximum token count to set
*/
setMaxTokens(maxTokens) {
this.maxTokens = maxTokens;
}
}