route-claudecode
Version:
Advanced routing and transformation system for Claude Code outputs to multiple AI providers
116 lines • 4.55 kB
JavaScript
;
/**
* Gemini Response Converter Module
* 响应格式转换器,负责Gemini到Anthropic的响应转换
* Project owner: Jason Zhang
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeminiResponseConverter = void 0;
exports.convertGeminiToAnthropicResponse = convertGeminiToAnthropicResponse;
const logger_1 = require("@/utils/logger");
const gemini_1 = require("@/transformers/gemini");
/**
* Gemini响应转换器类
*/
class GeminiResponseConverter {
requestId;
constructor(requestId = 'unknown') {
this.requestId = requestId;
}
/**
* 转换Gemini响应为Anthropic格式
*/
convertResponse(response, originalModel) {
try {
logger_1.logger.debug('Converting Gemini response to Anthropic format', {
requestId: this.requestId,
originalModel,
candidateCount: response.candidates?.length,
hasUsage: !!response.usageMetadata
});
// 使用统一的转换器
const anthropicResponse = (0, gemini_1.transformGeminiToAnthropic)(response, originalModel, this.requestId);
logger_1.logger.debug('Successfully converted to Anthropic format', {
requestId: this.requestId,
stopReason: anthropicResponse.stop_reason,
contentBlockCount: anthropicResponse.content?.length,
hasToolUse: Array.isArray(anthropicResponse.content) &&
anthropicResponse.content.some(block => block.type === 'tool_use'),
hasUsage: !!anthropicResponse.usage
});
return anthropicResponse;
}
catch (error) {
logger_1.logger.error('Failed to convert Gemini response to Anthropic format', {
requestId: this.requestId,
error: error.message,
originalModel,
candidateCount: response.candidates?.length
});
throw error;
}
}
/**
* 验证转换后的响应格式
*/
validateConvertedResponse(response) {
try {
const isValid = (response &&
typeof response.id === 'string' &&
response.type === 'message' &&
response.role === 'assistant' &&
Array.isArray(response.content) &&
typeof response.stop_reason === 'string' &&
typeof response.model === 'string');
if (!isValid) {
logger_1.logger.warn('Converted Anthropic response validation failed', {
requestId: this.requestId,
hasId: !!response?.id,
hasType: response?.type === 'message',
hasRole: response?.role === 'assistant',
hasContent: Array.isArray(response?.content),
hasStopReason: !!response?.stop_reason,
hasModel: !!response?.model
});
}
return isValid;
}
catch (error) {
logger_1.logger.error('Error validating converted response', {
requestId: this.requestId,
error: error.message
});
return false;
}
}
/**
* 创建包含调试信息的转换结果
*/
createConversionResult(anthropicResponse) {
const content = Array.isArray(anthropicResponse.content) ? anthropicResponse.content : [];
return {
response: anthropicResponse,
debug: {
requestId: this.requestId,
timestamp: Date.now(),
stopReason: anthropicResponse.stop_reason || 'unknown',
hasToolUse: content.some(block => block.type === 'tool_use'),
contentBlocks: content.length,
hasUsage: !!anthropicResponse.usage
}
};
}
}
exports.GeminiResponseConverter = GeminiResponseConverter;
/**
* 便捷函数:转换响应并返回验证结果
*/
function convertGeminiToAnthropicResponse(response, originalModel, requestId = 'unknown') {
const converter = new GeminiResponseConverter(requestId);
const anthropicResponse = converter.convertResponse(response, originalModel);
if (!converter.validateConvertedResponse(anthropicResponse)) {
throw new Error('Converted Anthropic response failed validation');
}
return anthropicResponse;
}
//# sourceMappingURL=response-converter.js.map