UNPKG

sfcoe-ailabs

Version:

AI-powered code review tool with static analysis integration for comprehensive code quality assessment.

50 lines (49 loc) 1.74 kB
import { OpenAI } from 'openai'; import { Logger } from '../utils/observability.js'; import AIProvider from './aiProvider.js'; export default class OpenAiClient extends AIProvider { /** * Reviews code using OpenAI's GPT model * * @param codeSnippet - The code to be reviewed * @param hints - Array of review hints from static analysis tools * @returns Promise that resolves to the AI review response */ async reviewCode(codeSnippet, hints) { Logger.debug(`Starting OpenAI Client review with ${hints.length} hints`, hints.map((h) => `${h.ruleId}: ${h.message}`)); const openai = new OpenAI({ apiKey: this.getApiKey(), }); const completion = await openai.chat.completions.create({ model: this.getModel(), messages: [ { role: 'assistant', content: [ { type: 'text', text: OpenAiClient.getReviewInstructions(), }, ], }, { role: 'user', content: [ { type: 'text', text: OpenAiClient.generatePrompt(codeSnippet, hints), }, ], }, ], response_format: { type: 'json_object', }, }); const content = completion.choices[0].message.content; if (content === null) { throw new Error('OpenAI response content is null'); } return JSON.parse(content); } }