@bobmatnyc/ai-code-review
Version:
A TypeScript-based tool for automated code reviews using AI models from Google Gemini, Anthropic Claude, and OpenRouter
140 lines (139 loc) • 6.38 kB
TypeScript
/**
* @fileoverview Utilities for estimating token usage and costs for code reviews.
*
* This module provides functions for estimating token usage and costs for code reviews
* based on file content and model selection. It uses the tokenCounter utilities for
* basic token counting and cost calculation, and adds specialized functions for
* estimating review-specific token usage patterns.
*/
import { FileInfo } from '../types/review';
/**
* Estimate the number of output tokens based on input tokens and review type
* @param inputTokens Number of input tokens
* @param reviewType Type of review
* @returns Estimated number of output tokens
*/
export declare function estimateOutputTokens(inputTokens: number, reviewType: string): number;
/**
* Estimate token usage and cost for a set of files
*
* This function calculates the estimated token usage and cost for reviewing a set of files
* using a specified AI model. It considers:
* - The content of each file (code, comments, etc.)
* - The type of review being performed (quick, security, architectural, etc.)
* - The specific AI model being used and its pricing structure
*
* The estimation includes overhead tokens for system prompts and instructions that are
* included in every review, in addition to the tokens from the file content itself.
*
* @param files Array of file information objects containing file content and metadata
* @param reviewType Type of review (quick, security, architectural, performance)
* @param modelName Name of the model to use (e.g., 'gemini:gemini-1.5-pro')
* @returns Estimated token usage and cost information including:
* - inputTokens: Number of tokens in the input (files + prompts)
* - outputTokens: Estimated number of tokens in the AI response
* - totalTokens: Total token usage (input + output)
* - estimatedCost: Estimated cost in USD
* - formattedCost: Cost formatted as a string (e.g., '$0.12 USD')
* - fileCount: Number of files being reviewed
* - totalFileSize: Total size of all files in bytes
*/
export declare function estimateReviewCost(files: FileInfo[], reviewType: string, modelName?: string): Promise<{
inputTokens: number;
outputTokens: number;
totalTokens: number;
estimatedCost: number;
formattedCost: string;
fileCount: number;
totalFileSize: number;
}>;
/**
* Format estimation results as a human-readable string
*
* This function takes the estimation results from estimateReviewCost or estimateFromFilePaths
* and formats them into a user-friendly string that can be displayed to the user.
*
* The formatted string includes:
* - Number of files being reviewed
* - Total file size
* - Input token count
* - Estimated output token count
* - Total token usage
* - Estimated cost in USD
*
* @param estimation Estimation results from estimateReviewCost or estimateFromFilePaths
* @param reviewType Type of review (quick, security, architectural, performance)
* @param modelName Name of the model being used
* @returns Formatted estimation string ready for display
* @example
* // Example output:
* // Estimation for 5 files (25.5 KB) using gemini-1.5-pro:
* // - Review type: quick
* // - Input tokens: 5,000
* // - Output tokens: 2,500 (estimated)
* // - Total tokens: 7,500
* // - Estimated Cost: $0.015 USD
*/
export declare function formatEstimation(estimation: ReturnType<typeof estimateReviewCost> extends Promise<infer T> ? T : never, reviewType: string, modelName: string): string;
/**
* Estimate token usage and cost for a set of file paths
*
* This function is a convenience wrapper around estimateReviewCost that takes file paths
* instead of FileInfo objects. It reads the content of each file and then calls
* estimateReviewCost to calculate the token usage and cost.
*
* This is particularly useful for the --estimate command line flag, which needs to
* estimate costs before actually performing a review.
*
* @param filePaths Array of file paths to estimate token usage for
* @param reviewType Type of review (quick, security, architectural, performance)
* @param modelName Name of the model to use (e.g., 'gemini:gemini-1.5-pro')
* @returns Estimated token usage and cost information (same as estimateReviewCost)
* @throws Error if any file cannot be read
* @see estimateReviewCost for details on the return value
*/
export declare function estimateFromFilePaths(filePaths: string[], reviewType: string, modelName?: string): Promise<ReturnType<typeof estimateReviewCost> extends Promise<infer T> ? T : never>;
/**
* Estimate token usage and cost for a multi-pass review
*
* This function extends the standard token estimation by accounting for the overhead
* of multi-pass reviews, including context maintenance between passes and additional
* overhead for each pass.
*
* @param files Array of file information objects containing file content and metadata
* @param reviewType Type of review (quick, security, architectural, performance)
* @param modelName Name of the model to use (e.g., 'gemini:gemini-1.5-pro')
* @param options Additional estimation options
* @param options.passCount Number of passes to estimate (if known)
* @param options.contextMaintenanceFactor Factor for context maintenance overhead (0-1)
* @returns Estimated token usage and cost information for multi-pass review
*/
export declare function estimateMultiPassReviewCost(files: FileInfo[], reviewType: string, modelName?: string, options?: {
passCount?: number;
contextMaintenanceFactor?: number;
}): Promise<{
inputTokens: number;
outputTokens: number;
totalTokens: number;
estimatedCost: number;
formattedCost: string;
fileCount: number;
totalFileSize: number;
passCount: number;
perPassCosts: {
passNumber: number;
inputTokens: number;
outputTokens: number;
totalTokens: number;
estimatedCost: number;
}[];
}>;
/**
* Format multi-pass estimation results as a human-readable string
*
* @param estimation Multi-pass estimation results
* @param reviewType Type of review
* @param modelName Name of the model being used
* @returns Formatted estimation string
*/
export declare function formatMultiPassEstimation(estimation: ReturnType<typeof estimateMultiPassReviewCost> extends Promise<infer T> ? T : never, reviewType: string, modelName: string): string;