@bobmatnyc/ai-code-review
Version:
A TypeScript-based tool for automated code reviews using AI models from Google Gemini, Anthropic Claude, and OpenRouter
94 lines (93 loc) • 4.36 kB
TypeScript
/**
* @fileoverview API utilities for interacting with AI services.
*
* This module provides utilities for interacting with AI services,
* including API key management, request formatting, and response processing.
*/
/**
* Check if an API key is available for a specific provider
*
* This function checks if an API key is available for the specified provider
* by calling the getApiKeyForProvider function from the config module.
*
* @param provider Provider name (gemini, openrouter, anthropic, openai)
* @returns True if an API key is available and non-empty, false otherwise
* @example
* hasApiKey('gemini') // Returns true if AI_CODE_REVIEW_GOOGLE_API_KEY is set
* hasApiKey('anthropic') // Returns true if AI_CODE_REVIEW_ANTHROPIC_API_KEY is set
*/
export declare function hasApiKey(provider: string): boolean;
/**
* Get the available API key type based on the model specified in environment variables
*
* This function determines which AI provider to use based on:
* 1. The model adapter specified in the AI_CODE_REVIEW_MODEL environment variable
* 2. The availability of API keys for different providers
*
* The function first checks if a specific adapter is specified in the model name
* (e.g., 'gemini:gemini-1.5-pro' or 'anthropic:claude-3-opus'). If so, it checks
* if the corresponding API key is available. If not, or if no adapter is specified,
* it falls back to checking for any available API key in a specific order.
*
* @returns The type of API key available ('OpenRouter', 'Google', 'Anthropic', 'OpenAI', or null if none)
* @example
* // If AI_CODE_REVIEW_MODEL='gemini:gemini-1.5-pro' and Google API key is available
* getApiKeyType() // Returns 'Google'
*
* // If no model is specified but Anthropic API key is available
* getApiKeyType() // Returns 'Anthropic'
*/
export declare function getApiKeyType(): 'OpenRouter' | 'Google' | 'Anthropic' | 'OpenAI' | null;
/**
* Get the API key type based on available environment variables (lowercase version)
*
* This is an alternative version of getApiKeyType that returns lowercase strings
* and 'none' instead of null. This function is maintained for internal usage
* within the api utilities module.
*
* @returns The type of API key available ('google', 'openrouter', 'anthropic', 'openai', or 'none')
* @internal
*/
export declare function getApiKeyTypeLowerCase(): 'google' | 'openrouter' | 'anthropic' | 'openai' | 'none';
/**
* Format an error message for API errors
*
* This function takes an error object and a provider name and returns a formatted
* error message that is more user-friendly and provides context about the error.
* It detects common error patterns like authentication issues, rate limits, and
* server errors, and formats them appropriately.
*
* @param error Error object or any value that can be converted to a string
* @param provider Provider name (e.g., 'Google', 'OpenRouter', 'Anthropic')
* @returns Formatted error message with provider context
* @example
* // For an authentication error
* formatApiError(new Error('401 Unauthorized'), 'Google')
* // Returns 'Google API key is invalid or expired. Please check your API key.'
*
* // For a generic error
* formatApiError(new Error('Something went wrong'), 'Anthropic')
* // Returns 'Anthropic API error: Something went wrong'
*/
export declare function formatApiError(error: any, provider: string): string;
/**
* Log API request details for debugging purposes
*
* This function logs the details of an API request, including the provider,
* endpoint, and request parameters. It automatically redacts sensitive data
* like API keys to prevent them from appearing in logs.
*
* The function only logs when debug logging is enabled, so it's safe to call
* in production code without generating excessive log output.
*
* @param provider Provider name (e.g., 'Google', 'OpenRouter', 'Anthropic')
* @param endpoint Endpoint being called (e.g., '/v1/chat/completions')
* @param params Request parameters object containing the request data
* @example
* logApiRequest('OpenAI', '/v1/chat/completions', {
* model: 'gpt-4',
* messages: [{ role: 'user', content: 'Hello' }],
* apiKey: 'sk-1234' // This will be redacted in the logs
* });
*/
export declare function logApiRequest(provider: string, endpoint: string, params: any): void;