codecrucible-synth
Version:
Production-Ready AI Development Platform with Multi-Voice Synthesis, Smithery MCP Integration, Enterprise Security, and Zero-Timeout Reliability
472 lines • 16 kB
JavaScript
/**
* Response Types System for CodeCrucible Synth
* Provides structured response handling and validation
*/
export var ResponseType;
(function (ResponseType) {
ResponseType["CODE_GENERATION"] = "code_generation";
ResponseType["CODE_ANALYSIS"] = "code_analysis";
ResponseType["CODE_REVIEW"] = "code_review";
ResponseType["DOCUMENTATION"] = "documentation";
ResponseType["EXPLANATION"] = "explanation";
ResponseType["ERROR_DIAGNOSIS"] = "error_diagnosis";
ResponseType["REFACTORING"] = "refactoring";
ResponseType["TESTING"] = "testing";
ResponseType["GENERAL"] = "general";
})(ResponseType || (ResponseType = {}));
// Factory and Validator Classes
export class ResponseFactory {
/**
* Create a basic agent response (test-compatible)
*/
static createAgentResponse(content, options = {}) {
return {
success: true,
timestamp: Date.now(),
content,
confidence: options.confidence || 0.8,
voiceId: options.voiceId,
tokensUsed: options.tokensUsed || 0,
reasoning: options.reasoning || 'Agent response generated',
};
}
/**
* Create a synthesis response (test-compatible)
*/
static createSynthesisResponse(combinedContent, voicesUsed, options = {}) {
return {
success: true,
timestamp: Date.now(),
combinedContent,
voicesUsed,
confidence: options.confidence || 0.8,
qualityScore: options.qualityScore || 85,
synthesisMode: options.synthesisMode || 'competitive',
reasoning: options.reasoning || 'Multi-voice synthesis completed',
};
}
/**
* Create a tool response (test-compatible)
*/
static createToolResponse(toolName, result, options = {}) {
return {
success: true,
timestamp: Date.now(),
toolName,
result,
executionTime: options.executionTime,
retryCount: options.retryCount,
metadata: options.metadata,
};
}
/**
* Create a file response (test-compatible)
*/
static createFileResponse(path, operation, options = {}) {
return {
success: true,
timestamp: Date.now(),
path,
filePath: path, // For test compatibility
operation,
content: options.content,
size: options.size,
language: options.language,
metadata: options.metadata,
};
}
/**
* Create an error response (test-compatible)
*/
static createErrorResponse(code, message, details) {
return {
code,
message,
details,
stack: new Error().stack,
};
}
/**
* Create a legacy-style response for backward compatibility
*/
static createLegacyAgentResponse(content, type, metadata = {}) {
return {
content,
type,
quality: 0.8,
confidence: 0.8,
metadata: {
timestamp: new Date(),
duration: 0,
model: 'unknown',
tokens: 0,
...metadata,
},
};
}
/**
* Create a code generation response
*/
static createCodeGenerationResponse(code, language, metadata = {}, options = {}) {
return {
...this.createLegacyAgentResponse(`Generated ${language} code`, ResponseType.CODE_GENERATION, metadata),
code,
language,
framework: options.framework,
dependencies: options.dependencies || [],
tests: options.tests,
type: ResponseType.CODE_GENERATION,
};
}
/**
* Create a code analysis response
*/
static createCodeAnalysisResponse(content, analysis, metadata = {}) {
return {
...this.createLegacyAgentResponse(content, ResponseType.CODE_ANALYSIS, metadata),
analysis,
type: ResponseType.CODE_ANALYSIS,
};
}
/**
* Create a code review response
*/
static createCodeReviewResponse(content, review, metadata = {}) {
return {
...this.createLegacyAgentResponse(content, ResponseType.CODE_REVIEW, metadata),
review,
type: ResponseType.CODE_REVIEW,
};
}
/**
* Parse response type from content
*/
static inferResponseType(content) {
const lowerContent = content.toLowerCase();
if (lowerContent.includes('```') ||
lowerContent.includes('function') ||
lowerContent.includes('class')) {
return ResponseType.CODE_GENERATION;
}
if (lowerContent.includes('analysis') ||
lowerContent.includes('complexity') ||
lowerContent.includes('bug')) {
return ResponseType.CODE_ANALYSIS;
}
if (lowerContent.includes('review') ||
lowerContent.includes('approve') ||
lowerContent.includes('changes')) {
return ResponseType.CODE_REVIEW;
}
if (lowerContent.includes('error') ||
lowerContent.includes('exception') ||
lowerContent.includes('debug')) {
return ResponseType.ERROR_DIAGNOSIS;
}
if (lowerContent.includes('refactor') ||
lowerContent.includes('improve') ||
lowerContent.includes('optimize')) {
return ResponseType.REFACTORING;
}
if (lowerContent.includes('test') ||
lowerContent.includes('spec') ||
lowerContent.includes('assert')) {
return ResponseType.TESTING;
}
if (lowerContent.includes('document') ||
lowerContent.includes('readme') ||
lowerContent.includes('guide')) {
return ResponseType.DOCUMENTATION;
}
return ResponseType.GENERAL;
}
}
export class ResponseValidator {
/**
* Check if response is valid (test-compatible)
*/
static isValidResponse(response) {
return (response &&
typeof response === 'object' &&
typeof response.success === 'boolean' &&
typeof response.timestamp === 'number');
}
/**
* Check if response has error (test-compatible)
*/
static hasError(response) {
return response.success === false && !!response.error;
}
/**
* Get error message from response (test-compatible)
*/
static getErrorMessage(response) {
return response.error?.message || null;
}
/**
* Extract content from any response type (test-compatible)
*/
static extractContent(response) {
if ('content' in response) {
return response.content;
}
if ('combinedContent' in response) {
return response.combinedContent;
}
return '';
}
/**
* Validate any agent response
*/
static validateResponse(response) {
const errors = [];
const warnings = [];
// Basic validation
if (!response.content || response.content.trim().length === 0) {
errors.push({
field: 'content',
message: 'Response content cannot be empty',
severity: 'error',
});
}
if ((response.quality ?? 0) < 0 || (response.quality ?? 0) > 1) {
errors.push({
field: 'quality',
message: 'Quality score must be between 0 and 1',
severity: 'error',
});
}
if ((response.confidence ?? 0) < 0 || (response.confidence ?? 0) > 1) {
errors.push({
field: 'confidence',
message: 'Confidence score must be between 0 and 1',
severity: 'error',
});
}
// Metadata validation
if (!response.metadata?.timestamp) {
warnings.push({
field: 'metadata.timestamp',
message: 'Timestamp should be provided',
suggestion: 'Add timestamp for better tracking',
});
}
if ((response.metadata?.duration ?? 0) < 0) {
warnings.push({
field: 'metadata.duration',
message: 'Duration cannot be negative',
});
}
// Type-specific validation
if (response.type === ResponseType.CODE_GENERATION) {
this.validateCodeGenerationResponse(response, errors, warnings);
}
else if (response.type === ResponseType.CODE_ANALYSIS) {
this.validateCodeAnalysisResponse(response, errors, warnings);
}
else if (response.type === ResponseType.CODE_REVIEW) {
this.validateCodeReviewResponse(response, errors, warnings);
}
const score = this.calculateValidationScore(errors, warnings);
return {
isValid: errors.length === 0,
errors,
warnings,
score,
};
}
static validateCodeGenerationResponse(response, errors, warnings) {
if (!response.code || response.code.trim().length === 0) {
errors.push({
field: 'code',
message: 'Generated code cannot be empty',
severity: 'error',
});
}
if (!response.language) {
errors.push({
field: 'language',
message: 'Programming language must be specified',
severity: 'error',
});
}
if (!response.dependencies) {
warnings.push({
field: 'dependencies',
message: 'Dependencies array should be provided',
suggestion: 'Include empty array if no dependencies',
});
}
}
static validateCodeAnalysisResponse(response, errors, warnings) {
if (!response.analysis) {
errors.push({
field: 'analysis',
message: 'Analysis object is required',
severity: 'error',
});
return;
}
const { analysis } = response;
// Validate score ranges
const scores = ['complexity', 'maintainability', 'performance', 'security'];
scores.forEach(score => {
if (typeof analysis[score] !== 'number' ||
analysis[score] < 0 ||
analysis[score] > 100) {
errors.push({
field: `analysis.${score}`,
message: `${score} score must be a number between 0 and 100`,
severity: 'error',
});
}
});
if (!Array.isArray(analysis.bugs)) {
errors.push({
field: 'analysis.bugs',
message: 'Bugs must be an array',
severity: 'error',
});
}
if (!Array.isArray(analysis.suggestions)) {
errors.push({
field: 'analysis.suggestions',
message: 'Suggestions must be an array',
severity: 'error',
});
}
}
static validateCodeReviewResponse(response, errors, warnings) {
if (!response.review) {
errors.push({
field: 'review',
message: 'Review object is required',
severity: 'error',
});
return;
}
const { review } = response;
if (!review.overall || typeof review.overall.score !== 'number') {
errors.push({
field: 'review.overall',
message: 'Overall score is required',
severity: 'error',
});
}
if (!['approved', 'changes_requested', 'needs_review'].includes(review.approvalStatus)) {
errors.push({
field: 'review.approvalStatus',
message: 'Invalid approval status',
severity: 'error',
});
}
if (!Array.isArray(review.comments)) {
errors.push({
field: 'review.comments',
message: 'Comments must be an array',
severity: 'error',
});
}
}
static calculateValidationScore(errors, warnings) {
let score = 100;
score -= errors.length * 20; // -20 points per error
score -= warnings.length * 5; // -5 points per warning
return Math.max(0, score);
}
/**
* Sanitize response content
*/
static sanitizeResponse(response) {
const sanitized = { ...response };
// Remove potentially harmful content
sanitized.content = sanitized.content
.replace(/<script[^>]*>.*?<\/script>/gi, '')
.replace(/javascript:/gi, '')
.replace(/data:text\/html/gi, '');
// Ensure quality and confidence are in valid range
sanitized.quality = Math.max(0, Math.min(1, sanitized.quality ?? 0));
sanitized.confidence = Math.max(0, Math.min(1, sanitized.confidence ?? 0));
return sanitized;
}
}
// Utility functions for response processing
export class ResponseProcessor {
/**
* Extract code blocks from response content
*/
static extractCodeBlocks(content) {
const codeBlockRegex = /```(\w+)?\n([\s\S]*?)\n```/g;
const blocks = [];
let match;
while ((match = codeBlockRegex.exec(content)) !== null) {
blocks.push({
language: match[1] || 'text',
code: match[2],
});
}
return blocks;
}
/**
* Calculate response complexity
*/
static calculateComplexity(response) {
let complexity = 0;
// Length factor
complexity += Math.min(response.content.length / 1000, 5);
// Code presence
const codeBlocks = this.extractCodeBlocks(response.content);
complexity += codeBlocks.length * 2;
// Technical terms
const technicalTerms = [
'function',
'class',
'interface',
'async',
'await',
'database',
'api',
'http',
'json',
'xml',
'algorithm',
'optimization',
'performance',
];
technicalTerms.forEach(term => {
if (response.content.toLowerCase().includes(term)) {
complexity += 0.5;
}
});
return Math.min(complexity, 10);
}
/**
* Merge multiple responses
*/
static mergeResponses(responses) {
if (responses.length === 0) {
throw new Error('Cannot merge empty response array');
}
if (responses.length === 1) {
return responses[0];
}
const mergedContent = responses.map(r => r.content).join('\n\n---\n\n');
const avgQuality = responses.reduce((sum, r) => sum + (r.quality ?? 0), 0) / responses.length;
const avgConfidence = responses.reduce((sum, r) => sum + (r.confidence ?? 0), 0) / responses.length;
const totalDuration = responses.reduce((sum, r) => sum + (r.metadata?.duration ?? 0), 0);
const totalTokens = responses.reduce((sum, r) => sum + (r.metadata?.tokens ?? 0), 0);
return ResponseFactory.createLegacyAgentResponse(mergedContent, ResponseType.GENERAL, {
duration: totalDuration,
tokens: totalTokens,
model: responses[0]?.metadata?.model,
});
}
}
// Export everything for easy access
export default {
ResponseFactory,
ResponseValidator,
ResponseProcessor,
ResponseType,
};
//# sourceMappingURL=response-types.js.map