tryaii-mcp-server
Version:
TryAII MCP Server - 15+ AI models with comparison, cost tracking, and collective intelligence
139 lines • 5.58 kB
JavaScript
/**
* Cost Validation Utility - Prevents Undercharging
* Ensures pricing integrity across all tool executions
*/
import { logger } from './logger.js';
/**
* Validate and correct cost calculations to prevent undercharging
*/
export function validateAndCorrectCost(toolExecution, calculatedCost) {
const warnings = [];
const appliedCorrections = [];
let finalCost = calculatedCost;
const originalCost = calculatedCost;
// Define minimum acceptable costs per tool
const minimumCosts = {
'brains': 0.08, // Minimum for 5 top models
'compare_models': 0.04, // Minimum for multi-model comparison
'chat_with_model': 0.015, // Minimum for single model
'list_available_models': 0, // Free
'get_model_info': 0 // Free
};
// Define maximum reasonable costs per tool (prevents overcharging)
const maximumCosts = {
'brains': 0.50, // Maximum for 5 top models
'compare_models': 1.00, // Maximum for many models
'chat_with_model': 0.25, // Maximum for single model with lots of tokens
'list_available_models': 0, // Free
'get_model_info': 0 // Free
};
const minCost = minimumCosts[toolExecution.toolName] || 0.01;
const maxCost = maximumCosts[toolExecution.toolName] || 0.50;
// Validation 1: Ensure minimum cost compliance
if (finalCost < minCost && toolExecution.hasSuccessfulResponse) {
warnings.push(`Cost below minimum threshold: $${finalCost} < $${minCost}`);
appliedCorrections.push(`Applied minimum cost: $${minCost}`);
finalCost = minCost;
}
// Validation 2: Ensure maximum cost compliance (prevent overcharging)
if (finalCost > maxCost) {
warnings.push(`Cost above maximum threshold: $${finalCost} > $${maxCost}`);
appliedCorrections.push(`Applied maximum cost cap: $${maxCost}`);
finalCost = maxCost;
}
// Validation 3: Zero cost validation for paid tools
if (finalCost === 0 && minCost > 0 && toolExecution.hasSuccessfulResponse) {
warnings.push('Zero cost detected for paid tool with successful response');
appliedCorrections.push(`Applied emergency minimum cost: $${minCost}`);
finalCost = minCost;
}
// Validation 4: Model count validation for multi-model tools
if (toolExecution.toolName === 'compare_models' && toolExecution.modelCount) {
const expectedMinCost = toolExecution.modelCount * 0.015; // $0.015 per model
if (finalCost < expectedMinCost) {
warnings.push(`Multi-model cost below expected: $${finalCost} < $${expectedMinCost} for ${toolExecution.modelCount} models`);
appliedCorrections.push(`Applied model-based minimum: $${expectedMinCost}`);
finalCost = Math.max(finalCost, expectedMinCost);
}
}
// Validation 5: Execution time validation (longer executions should cost more)
if (toolExecution.executionTime > 30000 && finalCost < 0.05) { // 30+ seconds
const timeBasedMinimum = 0.05;
warnings.push(`Long execution time (${toolExecution.executionTime}ms) with low cost`);
appliedCorrections.push(`Applied time-based minimum: $${timeBasedMinimum}`);
finalCost = Math.max(finalCost, timeBasedMinimum);
}
const result = {
isValid: warnings.length === 0,
finalCost,
warnings,
appliedCorrections,
originalCost
};
// Log validation results
if (warnings.length > 0 || appliedCorrections.length > 0) {
logger.warn('Cost validation applied corrections', {
toolName: toolExecution.toolName,
originalCost,
finalCost,
warnings,
appliedCorrections,
validationResult: result
});
}
else {
logger.debug('Cost validation passed', {
toolName: toolExecution.toolName,
finalCost,
modelCount: toolExecution.modelCount
});
}
return result;
}
/**
* Quick cost validation for critical path operations
*/
export function quickCostValidation(toolName, cost) {
const minimums = {
'brains': 0.08,
'compare_models': 0.04,
'chat_with_model': 0.015,
'list_available_models': 0,
'get_model_info': 0
};
const minimum = minimums[toolName] || 0.01;
if (cost < minimum && minimum > 0) {
logger.warn('Quick cost validation: applying minimum cost', {
toolName,
originalCost: cost,
appliedMinimum: minimum
});
return minimum;
}
return cost;
}
/**
* Cost monitoring for analytics and alerting
*/
export function recordCostMetrics(toolName, finalCost, validationResult) {
// Record metrics for monitoring and analytics
const metrics = {
toolName,
finalCost,
hadCorrections: validationResult.appliedCorrections.length > 0,
hadWarnings: validationResult.warnings.length > 0,
correctionsMade: validationResult.appliedCorrections.length,
timestamp: new Date().toISOString()
};
logger.info('Cost metrics recorded', metrics);
// If there were significant corrections, alert for review
if (validationResult.appliedCorrections.length > 0) {
logger.warn('Cost correction alert - review pricing logic', {
toolName,
originalCost: validationResult.originalCost,
correctedCost: finalCost,
corrections: validationResult.appliedCorrections
});
}
}
//# sourceMappingURL=costValidator.js.map