route-claudecode
Version:
Advanced routing and transformation system for Claude Code outputs to multiple AI providers
166 lines • 6.37 kB
JavaScript
;
/**
* Unified Input Processor Manager
* 自动检测请求格式并选择合适的处理器
* 项目所有者: Jason Zhang
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnifiedInputProcessor = void 0;
const types_1 = require("@/types");
const anthropic_1 = require("./anthropic");
const openai_1 = require("./openai");
const logger_1 = require("@/utils/logger");
class UnifiedInputProcessor {
name = 'unified';
processors;
constructor() {
this.processors = [
new openai_1.OpenAIInputProcessor(), // 优先检查OpenAI格式
new anthropic_1.AnthropicInputProcessor(), // 然后检查Anthropic格式
];
logger_1.logger.debug('UnifiedInputProcessor initialized with processors:', {
processorNames: this.processors.map(p => p.name)
});
}
/**
* Check if any processor can handle the request
*/
canProcess(request) {
try {
return this.processors.some(processor => processor.canProcess(request));
}
catch (error) {
logger_1.logger.debug('Error checking if request can be processed by any processor:', error);
return false;
}
}
/**
* Process the request using the appropriate processor
*/
async process(request) {
const requestId = request?.metadata?.requestId || `unified-${Date.now()}`;
try {
// Find the first processor that can handle this request
const suitableProcessor = this.processors.find(processor => {
try {
return processor.canProcess(request);
}
catch (error) {
logger_1.logger.debug(`Processor ${processor.name} failed canProcess check:`, error);
return false;
}
});
if (!suitableProcessor) {
const errorMsg = 'No suitable processor found for request format';
logger_1.logger.error(errorMsg, {
requestId,
requestKeys: Object.keys(request || {}),
hasMessages: Array.isArray(request?.messages),
messageCount: request?.messages?.length || 0,
hasTools: Array.isArray(request?.tools),
toolCount: request?.tools?.length || 0,
model: request?.model,
availableProcessors: this.processors.map(p => p.name)
});
throw new types_1.ValidationError(errorMsg);
}
logger_1.logger.debug(`Selected processor: ${suitableProcessor.name}`, {
requestId,
processorName: suitableProcessor.name,
requestFormat: this.detectRequestFormat(request)
});
// Process the request with the selected processor
const result = await suitableProcessor.process(request);
// Ensure processor name is recorded in metadata
if (result.metadata) {
result.metadata.selectedProcessor = suitableProcessor.name;
}
logger_1.logger.debug(`Request processed successfully by ${suitableProcessor.name}`, {
requestId,
processorName: suitableProcessor.name,
model: result.model,
messageCount: result.messages.length,
hasTools: !!result.tools?.length,
originalFormat: result.metadata?.originalFormat
});
return result;
}
catch (error) {
logger_1.logger.error('Error in unified input processing:', {
requestId,
error: error instanceof Error ? error.message : String(error),
requestFormat: this.detectRequestFormat(request),
availableProcessors: this.processors.map(p => p.name)
});
throw error;
}
}
/**
* Validate using all processors
*/
validate(request) {
try {
return this.processors.some(processor => {
try {
return processor.validate(request);
}
catch (error) {
logger_1.logger.debug(`Processor ${processor.name} validation failed:`, error);
return false;
}
});
}
catch (error) {
logger_1.logger.error('Validation error in unified processor:', error);
return false;
}
}
/**
* Detect request format for debugging
*/
detectRequestFormat(request) {
if (!request || typeof request !== 'object') {
return 'invalid';
}
const format = [];
// Check for OpenAI indicators
if (request.tools?.some((t) => t.type === 'function' && t.function?.parameters)) {
format.push('openai-tools');
}
if (request.messages?.some((m) => m.tool_calls)) {
format.push('openai-tool-calls');
}
if (request.tool_choice && typeof request.tool_choice === 'object' && request.tool_choice.function) {
format.push('openai-tool-choice');
}
// Check for Anthropic indicators
if (Array.isArray(request.system)) {
format.push('anthropic-system');
}
if (request.tools?.some((t) => t.input_schema && !t.function)) {
format.push('anthropic-tools');
}
if (request.tool_choice?.type === 'tool' && request.tool_choice.name) {
format.push('anthropic-tool-choice');
}
// Check for generic indicators
if (Array.isArray(request.messages)) {
format.push('has-messages');
}
if (typeof request.model === 'string') {
format.push('has-model');
}
return format.length > 0 ? format.join(',') : 'unknown';
}
/**
* Get information about available processors
*/
getProcessorInfo() {
return this.processors.map(processor => ({
name: processor.name,
canProcess: processor.canProcess.bind(processor)
}));
}
}
exports.UnifiedInputProcessor = UnifiedInputProcessor;
//# sourceMappingURL=unified-processor.js.map