@@ -4,63 +4,50 @@ export interface Env {
44 GEMINI_API_KEY : string ;
55}
66
7- export default {
8- async fetch ( request : Request , env : Env , ctx : ExecutionContext ) : Promise < Response > {
9- const corsHeaders = {
10- 'Access-Control-Allow-Origin' : '*' ,
11- 'Access-Control-Allow-Methods' : 'POST, OPTIONS' ,
12- 'Access-Control-Allow-Headers' : 'Content-Type' ,
13- } ;
7+ const corsHeaders = {
8+ 'Access-Control-Allow-Origin' : '*' ,
9+ 'Access-Control-Allow-Methods' : 'POST, OPTIONS' ,
10+ 'Access-Control-Allow-Headers' : 'Content-Type' ,
11+ } ;
1412
15- if ( request . method === 'OPTIONS' ) {
16- return new Response ( null , { headers : corsHeaders } ) ;
17- }
13+ async function handleTranslate ( request : Request , model : ReturnType < GoogleGenerativeAI [ 'getGenerativeModel' ] > ) {
14+ const { code, targetLanguage } = await request . json < { code : string ; targetLanguage : string } > ( ) ;
1815
19- const url = new URL ( request . url ) ;
20- const path = url . pathname ;
21- const genAI = new GoogleGenerativeAI ( env . GEMINI_API_KEY ) ;
22- const model = genAI . getGenerativeModel ( { model : 'gemini-2.0-flash' } ) ;
16+ if ( ! code || ! targetLanguage ) {
17+ return new Response ( JSON . stringify ( { error : "Missing 'code' or 'targetLanguage' in request body." } ) , {
18+ status : 400 ,
19+ headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
20+ } ) ;
21+ }
2322
24- try {
25- //Function for handling the translation
26- const handleTranslate = async ( ) => {
27- const { code, targetLanguage } = await request . json < { code : string ; targetLanguage : string } > ( ) ;
28-
29- if ( ! code || ! targetLanguage ) {
30- return new Response ( JSON . stringify ( { error : "Missing 'code' or 'targetLanguage' in request body." } ) , {
31- status : 400 ,
32- headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
33- } ) ;
34- }
35-
36- const prompt = `Translate the following code snippet to ${ targetLanguage } .
23+ const prompt = `Translate the following code snippet to ${ targetLanguage } .
3724Do not add any explanation, commentary, or markdown formatting like \`\`\` around the code.
3825**IMPORTANT: Preserve all original comments and their exact placement in the translated code. Do not add extra spaces in between.**
3926Only provide the raw, translated code itself.
4027
4128Original Code:
4229${ code } `;
4330
44- const result = await model . generateContent ( prompt ) ;
45- const translatedCode = result . response . text ( ) ;
46-
47- return new Response ( JSON . stringify ( { translation : translatedCode } ) , {
48- status : 200 ,
49- headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
50- } ) ;
51- } ;
52- //Function for handling the explanation
53- const handleExplain = async ( ) => {
54- const { code } = await request . json < { code : string } > ( ) ;
55-
56- if ( ! code ) {
57- return new Response ( JSON . stringify ( { error : "Missing 'code' in request body." } ) , {
58- status : 400 ,
59- headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
60- } ) ;
61- }
62-
63- const prompt = `Explain the following code snippet in detail:
31+ const result = await model . generateContent ( prompt ) ;
32+ const translatedCode = result . response . text ( ) ;
33+
34+ return new Response ( JSON . stringify ( { translation : translatedCode } ) , {
35+ status : 200 ,
36+ headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
37+ } ) ;
38+ }
39+
40+ async function handleExplain ( request : Request , model : ReturnType < GoogleGenerativeAI [ 'getGenerativeModel' ] > ) {
41+ const { code } = await request . json < { code : string } > ( ) ;
42+
43+ if ( ! code ) {
44+ return new Response ( JSON . stringify ( { error : "Missing 'code' in request body." } ) , {
45+ status : 400 ,
46+ headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
47+ } ) ;
48+ }
49+
50+ const prompt = `Explain the following code snippet in detail:
64511. Provide a clear breakdown of what each part (functions, variables, logic blocks) does.
65522. If applicable, describe the overall purpose or intent of the code.
66533. Offer a step-by-step explanation of how the code executes.
@@ -70,24 +57,35 @@ ${code}`;
7057Code:
7158${ code } `;
7259
73- const result = await model . generateContent ( prompt ) ;
74- const explanation = result . response . text ( ) ;
60+ const result = await model . generateContent ( prompt ) ;
61+ const explanation = result . response . text ( ) ;
7562
76- return new Response ( JSON . stringify ( { explanation } ) , {
77- status : 200 ,
78- headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
79- } ) ;
80- } ;
81- if ( path === '/' ) {
82- return handleTranslate ( ) ;
83- }
84- if ( path === '/v1/translate' ) {
85- return handleTranslate ( ) ;
63+ return new Response ( JSON . stringify ( { explanation } ) , {
64+ status : 200 ,
65+ headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
66+ } ) ;
67+ }
68+
69+ export default {
70+ async fetch ( request : Request , env : Env , ctx : ExecutionContext ) : Promise < Response > {
71+ if ( request . method === 'OPTIONS' ) {
72+ return new Response ( null , { headers : corsHeaders } ) ;
73+ }
74+
75+ try {
76+ const url = new URL ( request . url ) ;
77+ const path = url . pathname ;
78+ const genAI = new GoogleGenerativeAI ( env . GEMINI_API_KEY ) ;
79+ const model = genAI . getGenerativeModel ( { model : 'gemini-2.0-flash' } ) ;
80+
81+ if ( path === '/' || path === '/v1/translate' ) {
82+ return await handleTranslate ( request , model ) ;
8683 }
84+
8785 if ( path === '/v1/explain' ) {
88- return handleExplain ( ) ;
86+ return await handleExplain ( request , model ) ;
8987 }
90- // Fallback for unknown paths
88+
9189 return new Response ( JSON . stringify ( { error : 'Route not found.' } ) , {
9290 status : 404 ,
9391 headers : { ...corsHeaders , 'Content-Type' : 'application/json' } ,
0 commit comments