@@ -8,33 +8,76 @@ export class ConverterService {
88 constructor ( ) {
99 this . model = new ChatOllama ( {
1010 baseUrl : process . env . OLLAMA_URL || 'http://localhost:11434' ,
11- model : 'qwen2.5-coder:1.5b' , // Lightweight and efficient for code translation
11+ model : 'qwen2.5-coder:1.5b' ,
1212 } ) ;
1313 }
1414
15- async convertCode ( code : string , from : string , to : string ) {
15+ // --- 1. CODE TRANSLATION ---
16+ async convertCode ( code : string , from : string , to : string ) {
17+ // Check if "to" is a migration preset
18+ const isMigration = to . includes ( '-' ) || from . includes ( '-' ) ;
19+
20+ const prompt = `
21+ You are an expert software architect specializing in ${ isMigration ? 'code migration' : 'code translation' } .
22+ Task: Convert the input from ${ from } to ${ to } .
23+
24+ ${ isMigration ? `
25+ SPECIFIC INSTRUCTIONS FOR MIGRATION:
26+ - If moving from Class to Functional components, use React Hooks (useState, useEffect).
27+ - If moving to TypeScript, add proper interfaces and types.
28+ - If moving between frameworks (e.g., React to Vue), map lifecycle methods and state management accurately.
29+ ` : '' }
30+
31+ RULES:
32+ - Return ONLY raw code.
33+ - No markdown blocks.
34+ - Maintain variable names and logic flow.
35+
36+ SOURCE CODE:
37+ ${ code }
38+ ` ;
39+ return this . executeInvoke ( prompt , 'translatedCode' ) ;
40+ }
41+
42+ // --- 2. CODE REVIEWER ---
43+ async reviewCode ( code : string , lang : string ) {
1644 const prompt = `
17- You are a high-performance code translation engine.
18- Translate the following ${ from } code into ${ to } .
19-
45+ You are a Senior Software Engineer. Review the following ${ lang } code.
46+ Check for: Performance, Security vulnerabilities, and Best Practices.
2047 RULES:
21- - Return ONLY the raw code.
22- - Do not include markdown code blocks (\`\`\`), code fences, backticks that are not part of the original code.
23- - Do not include explanations or comments like "Here is your code" unless it was in the original code.
24- - Maintain logic and variable names exactly.
25-
26- SOURCE CODE:
48+ - Provide a concise list of improvements.
49+ - Use professional, constructive language.
50+ - Use Markdown for formatting.
51+ CODE:
2752 ${ code }
2853 ` ;
54+ return this . executeInvoke ( prompt , 'reviewContent' ) ;
55+ }
56+
57+ // --- 3. BUG FIXER ---
58+ async fixBugs ( code : string , lang : string ) {
59+ const prompt = `
60+ You are a debugging expert. Analyze and fix the bugs in this ${ lang } code.
61+ RULES:
62+ - Return the fixed code first.
63+ - Follow the code with a brief "Explanation of Fixes" section.
64+ - Do not use markdown fences for the code part.
65+ CODE:
66+ ${ code }
67+ ` ;
68+ return this . executeInvoke ( prompt , 'fixedCode' ) ;
69+ }
2970
71+ // Private helper to avoid repeating try/catch blocks
72+ private async executeInvoke ( prompt : string , key : string ) {
3073 try {
3174 const response = await this . model . invoke ( prompt ) ;
3275 return {
3376 success : true ,
34- translatedCode : response . content
77+ [ key ] : response . content
3578 } ;
3679 } catch ( error ) {
37- console . error ( 'Error during code conversion:' , error ) ;
80+ console . error ( `AI Service Error:` , error ) ;
3881 throw new InternalServerErrorException ( 'Failed to connect to AI engine' ) ;
3982 }
4083 }
0 commit comments