/** * Cross-Platform Code Translation Engine * Core competitive differentiator for AeThex Studio */ import { PlatformId, getPlatform } from './platforms'; import { toast } from 'sonner'; import { captureEvent, captureError } from './analytics'; export interface TranslationRequest { sourceCode: string; sourcePlatform: PlatformId; targetPlatform: PlatformId; context?: string; } export interface TranslationResult { success: boolean; translatedCode?: string; explanation?: string; warnings?: string[]; error?: string; } /** * Platform-specific translation prompts */ const getTranslationPrompt = ( sourceCode: string, sourcePlatform: PlatformId, targetPlatform: PlatformId, context?: string ): string => { const sourcePlat = getPlatform(sourcePlatform); const targetPlat = getPlatform(targetPlatform); return `You are an expert game developer specializing in cross-platform game development. **Task**: Translate the following ${sourcePlat.language} code (${sourcePlat.displayName}) to ${targetPlat.language} (${targetPlat.displayName}). **Source Platform**: ${sourcePlat.displayName} - Language: ${sourcePlat.language} - API Documentation: ${sourcePlat.apiDocs} **Target Platform**: ${targetPlat.displayName} - Language: ${targetPlat.language} - API Documentation: ${targetPlat.apiDocs} **Source Code**: \`\`\`${sourcePlat.language.toLowerCase()} ${sourceCode} \`\`\` ${context ? `**Additional Context**: ${context}\n` : ''} **Instructions**: 1. Translate the code to ${targetPlat.language} while preserving the logic and functionality 2. Use ${targetPlat.displayName}-native APIs and best practices 3. Add comments explaining platform-specific differences 4. Ensure the code follows ${targetPlat.language} conventions and style 5. If certain features don't have direct equivalents, provide the closest alternative and explain **Output Format**: Return ONLY the translated code wrapped in triple backticks with the language identifier. Then provide a brief explanation of key changes and any warnings. Example: \`\`\`${targetPlat.fileExtension.replace('.', '')} // Translated code here \`\`\` **Explanation**: [Brief explanation of translation] **Warnings**: [Any caveats or limitations, if applicable]`; }; /** * Platform-specific translation rules */ const platformTranslationRules: Record> = { 'roblox-to-uefn': [ 'game:GetService() → Use Verse imports', 'Instance.new() → object{} syntax in Verse', 'Connect() → Subscribe() in Verse', 'wait() → Sleep() in Verse', 'print() → Print() in Verse', ], 'uefn-to-roblox': [ 'Verse imports → game:GetService()', 'object{} → Instance.new()', 'Subscribe() → Connect()', 'Sleep() → wait()', 'Print() → print()', ], 'roblox-to-spatial': [ 'Lua → TypeScript syntax', 'game:GetService() → Spatial SDK imports', 'Instance.new() → new SpatialObject()', 'Connect() → addEventListener()', 'wait() → await setTimeout()', ], 'spatial-to-roblox': [ 'TypeScript → Lua syntax', 'Spatial SDK → game:GetService()', 'new SpatialObject() → Instance.new()', 'addEventListener() → Connect()', 'await setTimeout() → wait()', ], }; /** * Mock translation service (for development without API key) * Replace with actual Claude API call in production */ async function translateWithMockService( request: TranslationRequest ): Promise { const ruleKey = `${request.sourcePlatform}-to-${request.targetPlatform}`; const rules = platformTranslationRules[ruleKey] || []; return { success: true, translatedCode: `-- Translated from ${request.sourcePlatform} to ${request.targetPlatform} -- Translation Rules Applied: ${rules.map(r => `-- ${r}`).join('\n')} -- Original Code (needs actual translation): ${request.sourceCode} -- TODO: Replace with actual ${request.targetPlatform} implementation`, explanation: `This is a mock translation. The actual translation engine will use Claude API to intelligently convert ${request.sourcePlatform} code to ${request.targetPlatform}.`, warnings: [ 'Mock translation active - integrate Claude API for production', `Translation rules: ${rules.join(', ')}`, ], }; } /** * Translate code using Claude API * This is the production implementation */ async function translateWithClaudeAPI( request: TranslationRequest ): Promise { try { const prompt = getTranslationPrompt( request.sourceCode, request.sourcePlatform, request.targetPlatform, request.context ); // TODO: Replace with actual Claude API call // For now, using mock service // In production, use: // const response = await fetch('https://api.anthropic.com/v1/messages', { // method: 'POST', // headers: { // 'Content-Type': 'application/json', // 'x-api-key': process.env.CLAUDE_API_KEY, // 'anthropic-version': '2023-06-01', // }, // body: JSON.stringify({ // model: 'claude-3-5-sonnet-20241022', // max_tokens: 4096, // messages: [{ role: 'user', content: prompt }], // }), // }); return await translateWithMockService(request); } catch (error) { captureError(error as Error, { context: 'translation_api', sourcePlatform: request.sourcePlatform, targetPlatform: request.targetPlatform, }); return { success: false, error: `Translation failed: ${(error as Error).message}`, }; } } /** * Main translation function */ export async function translateCode( request: TranslationRequest ): Promise { try { // Validate platforms if (request.sourcePlatform === request.targetPlatform) { return { success: false, error: 'Source and target platforms must be different', }; } if (!request.sourceCode || request.sourceCode.trim() === '') { return { success: false, error: 'Source code cannot be empty', }; } // Log translation attempt captureEvent('translation_started', { sourcePlatform: request.sourcePlatform, targetPlatform: request.targetPlatform, codeLength: request.sourceCode.length, }); // Perform translation const result = await translateWithClaudeAPI(request); // Log result if (result.success) { captureEvent('translation_success', { sourcePlatform: request.sourcePlatform, targetPlatform: request.targetPlatform, }); toast.success( `Translated ${request.sourcePlatform} → ${request.targetPlatform}` ); } else { captureEvent('translation_failed', { sourcePlatform: request.sourcePlatform, targetPlatform: request.targetPlatform, error: result.error, }); toast.error(`Translation failed: ${result.error}`); } return result; } catch (error) { captureError(error as Error, { context: 'translate_code' }); return { success: false, error: `Unexpected error: ${(error as Error).message}`, }; } } /** * Get supported translation pairs */ export function getSupportedTranslations(): Array<{ source: PlatformId; target: PlatformId; }> { return [ { source: 'roblox', target: 'uefn' }, { source: 'uefn', target: 'roblox' }, { source: 'roblox', target: 'spatial' }, { source: 'spatial', target: 'roblox' }, { source: 'uefn', target: 'spatial' }, { source: 'spatial', target: 'uefn' }, ]; } /** * Check if translation is supported */ export function isTranslationSupported( source: PlatformId, target: PlatformId ): boolean { return getSupportedTranslations().some( (pair) => pair.source === source && pair.target === target ); }