UNPKG

ds-sfcoe-ailabs

Version:

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

49 lines (48 loc) 1.9 kB
import { Anthropic } from '@anthropic-ai/sdk'; import { Logger } from '../utils/observability.js'; import AIProvider from './aiProvider.js'; export default class AnthropicAiClient extends AIProvider { /** * Reviews code using Anthropic's Claude 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 * @throws Error if the API request fails or response is invalid * @example * ```typescript * const response = await client.reviewCode(codeSnippet, hints); * console.log(response.comments); * ``` */ async reviewCode(codeSnippet, hints) { Logger.debug(`Starting Anthropic Client review with ${hints.length} hints`, hints.map((h) => `${h.ruleId}: ${h.message}`)); const anthropic = new Anthropic({ apiKey: this.getApiKey() }); const message = await anthropic.messages.create({ model: this.getModel(), max_tokens: this.getMaxTokens(), system: AnthropicAiClient.getReviewInstructions(), messages: [ { role: 'assistant', content: [ { type: 'text', text: AnthropicAiClient.getReviewInstructions(), }, ], }, { role: 'user', content: AnthropicAiClient.generatePrompt(codeSnippet, hints), }, ], }); // get result from message response and return it const content = message.content[0].text; if (content === null) { throw new Error('Anthropic response content is null'); } return JSON.parse(content); } }