in devai-vscode-extension/src/review.ts [60:118]
export async function generateReview() {
vscode.window.showInformationMessage('Generating code review...');
const modelName = vscode.workspace.getConfiguration().get<string>('google.gemini.textModel', 'models/gemini-1.5-pro');
// Get API Key from local user configuration
const apiKey = vscode.workspace.getConfiguration().get<string>('google.gemini.apiKey');
if (!apiKey) {
vscode.window.showErrorMessage('API key not configured. Check your settings.');
return;
}
const genai = new GoogleGenerativeAI(apiKey);
const model = genai.getGenerativeModel({model: modelName});
// Text selection
const editor = vscode.window.activeTextEditor;
if (!editor) {
console.debug('Abandon: no open text editor.');
return;
}
const selection = editor.selection;
const selectedCode = editor.document.getText(selection);
var DEVAI_REVIEW_PROMPT = await readSecret(REVIEW_PROMPT_SM);
if (DEVAI_REVIEW_PROMPT === null) {
// DEVAI_REVIEW_PROMPT is null, so assign the value of PROMPT to it
DEVAI_REVIEW_PROMPT = PROMPT;
}
// Build the full prompt using the template.
const fullPrompt = `${DEVAI_REVIEW_PROMPT}
${CODE_LABEL}
${selectedCode}
${REVIEW_LABEL}
`;
const result = await model.generateContent(fullPrompt);
const response = await result.response;
const comment = response.text();
// Insert before selection
editor.edit((editBuilder) => {
// Copy the indent from the first line of the selection.
const trimmed = selectedCode.trimStart();
const padding = selectedCode.substring(0, selectedCode.length - trimmed.length);
// TODO(you!): Support other comment styles.
const commentPrefix = '# ';
let pyComment = comment.split('\n').map((l: string) => `${padding}${commentPrefix}${l}`).join('\r');
if (pyComment.search(/\n$/) === -1) {
// Add a final newline if necessary.
pyComment += "\n";
}
let reviewIntro = padding + commentPrefix + "Devai Code review: (generated)\n";
editBuilder.insert(selection.start, reviewIntro);
editBuilder.insert(selection.start, pyComment);
});
}