@@ -68,19 +68,32 @@ async function handleTranslate(request: Request, model: ReturnType<GoogleGenerat
6868 headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
6969 } ) ;
7070 }
71- const sourceLanguage = await detectLanguage ( code , model ) ;
71+ // const sourceLanguage = await detectLanguage(code, model);
7272 const prompt = `Translate the following code snippet to ${ targetLanguage } .
73- Do not add any explanation, commentary, or markdown formatting like \`\`\` around the code.
74- **IMPORTANT: Preserve all original comments and their exact placement in the translated code. Do not add extra spaces in between.**
75- Only provide the raw, translated code itself.
76-
73+ Return only in this format:
74+ <TranslatedLanguage>
75+ ...translated code...
76+ </TranslatedLanguage>
77+ <SourceLanguage>
78+ ...detected language...
79+ </SourceLanguage>
80+
81+ Do not include any markdown, comments, or extra text.
7782Original Code:
7883${ code } `;
7984
8085 const result = await model . generateContent ( prompt ) ;
81- const translatedCode = result . response . text ( ) ;
86+ const text = result . response . text ( ) ;
87+
88+ const translatedMatch = text . match ( / < T r a n s l a t e d L a n g u a g e > ( [ \s \S ] * ?) < \/ T r a n s l a t e d L a n g u a g e > / ) ;
89+ const sourceMatch = text . match ( / < S o u r c e L a n g u a g e > ( [ \s \S ] * ?) < \/ S o u r c e L a n g u a g e > / ) ;
90+
91+ const translation = translatedMatch ? translatedMatch [ 1 ] . trim ( ) : "" ;
92+ const sourceLanguage = sourceMatch ? sourceMatch [ 1 ] . trim ( ) : "" ;
93+
8294 await updateAnalytics ( sourceLanguage , targetLanguage , env ) ;
83- return new Response ( JSON . stringify ( { translation : translatedCode } ) , {
95+
96+ return new Response ( JSON . stringify ( { translation, sourceLanguage } ) , {
8497 status : 200 ,
8598 headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
8699 } ) ;
@@ -176,13 +189,13 @@ export default {
176189 } ,
177190} ;
178191
179- async function detectLanguage ( code : string , model : ReturnType < GoogleGenerativeAI [ 'getGenerativeModel' ] > ) {
180- const prompt = `Identify the programming language of the following code.
181- Only respond with the exact language name (e.g., "python", "javascript", "c++", "java", etc.) without any extra text or punctuation.
192+ // async function detectLanguage(code: string, model: ReturnType<GoogleGenerativeAI['getGenerativeModel']>) {
193+ // const prompt = `Identify the programming language of the following code.
194+ // Only respond with the exact language name (e.g., "python", "javascript", "c++", "java", etc.) without any extra text or punctuation.
182195
183- Code:
184- ${ code } `;
196+ // Code:
197+ // ${code}`;
185198
186- const result = await model . generateContent ( prompt ) ;
187- return result . response . text ( ) . trim ( ) . toLowerCase ( ) ;
188- }
199+ // const result = await model.generateContent(prompt);
200+ // return result.response.text().trim().toLowerCase();
201+ // }
0 commit comments