quality-mcp
Version:
An MCP server that analyzes to your codebase, with plugin support for DCD and Simian. 🏍️ "The only Zen you find on the tops of mountains is the Zen you bring up there."
140 lines (119 loc) • 3.67 kB
JavaScript
/**
* Response Optimizer for AI Agent Interface
* Optimizes payload size for AI token efficiency
*/
import { createLogger } from '../utils/logger.js';
const logger = createLogger('response-optimizer');
/**
* Response optimization context types
*/
export const CONTEXT_TYPES = {
OVERVIEW: 'overview',
DETAILED: 'detailed',
};
/**
* Configuration for payload size optimization
*/
const PAYLOAD_CONFIG = {
// Minimum duplicate count where overview becomes beneficial
MIN_DUPLICATES_FOR_OVERVIEW: 5,
// Maximum duplicate count where overview is always better
MAX_DUPLICATES_FOR_OVERVIEW: 20,
// Size threshold (characters) where overview becomes beneficial
OVERVIEW_SIZE_THRESHOLD: 1000,
// Overview message template
OVERVIEW_MESSAGE: "Say 'show details' for complete analysis",
};
/**
* Response Optimizer for AI Token Efficiency
* Always returns the smaller payload to minimize AI processing costs
*/
export class ResponseOptimizer {
constructor(config = {}) {
this.config = { ...PAYLOAD_CONFIG, ...config };
}
/**
* Detect user context from request parameters
* Simplified to focus on explicit requests for details
*/
detectContext(_params = {}, agentMessage = '') {
// Ensure agentMessage is a string
const message = (agentMessage || '').toString().toLowerCase();
// Explicit requests for details
if (
message.includes('show details') ||
message.includes('detailed') ||
message.includes('complete') ||
message.includes('full') ||
message.includes('verbose')
) {
return CONTEXT_TYPES.DETAILED;
}
// Default to overview for efficiency
return CONTEXT_TYPES.OVERVIEW;
}
/**
* Optimize response based on payload size and context
* Always returns the smaller payload to minimize AI token usage
*/
optimizeResponse(result, context = {}) {
// If user explicitly requested details, honor that request
if (context.context === CONTEXT_TYPES.DETAILED) {
logger.debug('User requested detailed response - honoring request');
return this.createDetailedResponse(result);
}
// Calculate payload sizes
const overviewSize = this.calculateOverviewSize(result);
const detailedSize = JSON.stringify(result).length;
logger.debug(
`Payload sizes - Overview: ${overviewSize} chars, Detailed: ${detailedSize} chars`
);
// Always return the smaller payload for AI efficiency
if (overviewSize <= detailedSize) {
logger.debug('Returning overview response (smaller payload)');
return this.createOverviewResponse(result);
} else {
logger.debug('Returning detailed response (smaller payload)');
return this.createDetailedResponse(result);
}
}
/**
* Calculate size of overview response
*/
calculateOverviewSize(result) {
const overview = this.createOverviewResponse(result);
return JSON.stringify(overview).length;
}
/**
* Create optimized overview response
*/
createOverviewResponse(result) {
const { duplicates = [], summary = {} } = result;
return {
summary: {
...summary,
duplicateCount: duplicates.length,
message: this.config.OVERVIEW_MESSAGE,
},
duplicates: [], // Empty array to minimize payload
optimized: true,
context: CONTEXT_TYPES.OVERVIEW,
};
}
/**
* Create detailed response with all data
*/
createDetailedResponse(result) {
return {
...result,
optimized: false,
context: CONTEXT_TYPES.DETAILED,
};
}
/**
* Get configuration for testing/debugging
*/
getConfig() {
return { ...this.config };
}
}