-
Notifications
You must be signed in to change notification settings - Fork 11
Added language detection and KV-based usage analytics #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { GoogleGenerativeAI } from '@google/generative-ai'; | |||||||||||||||
|
|
||||||||||||||||
| export interface Env { | ||||||||||||||||
| RATE_LIMIT: KVNamespace; | ||||||||||||||||
| LANG_TRANSLATION_ANALYTICS: KVNamespace; | ||||||||||||||||
| GEMINI_API_KEY: string; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -15,7 +16,7 @@ const MAX_REQUESTS_ALLOWED = 10; | |||||||||||||||
| const DURATION = 60_000; | ||||||||||||||||
|
|
||||||||||||||||
| async function checkRateLimit(ip: string, env: Env) { | ||||||||||||||||
| const key = `ip_key:${ip}`; | ||||||||||||||||
| const key = `ip_key:${ip}`.toLowerCase(); | ||||||||||||||||
| const now = Date.now(); | ||||||||||||||||
| let value = await env.RATE_LIMIT.get(key); | ||||||||||||||||
| let data = { count: 0, time: now }; | ||||||||||||||||
|
|
@@ -38,7 +39,25 @@ async function checkRateLimit(ip: string, env: Env) { | |||||||||||||||
|
|
||||||||||||||||
| return data.count <= MAX_REQUESTS_ALLOWED; | ||||||||||||||||
| } | ||||||||||||||||
| async function handleTranslate(request: Request, model: ReturnType<GoogleGenerativeAI['getGenerativeModel']>) { | ||||||||||||||||
|
|
||||||||||||||||
| async function updateAnalytics(source: string, dest: string, env: Env) { | ||||||||||||||||
| const key = `${source}-${dest}`; | ||||||||||||||||
| let value = await env.LANG_TRANSLATION_ANALYTICS.get(key); | ||||||||||||||||
|
|
||||||||||||||||
| let data = { count: 0 }; | ||||||||||||||||
| if (value) { | ||||||||||||||||
| try { | ||||||||||||||||
| data = JSON.parse(value); | ||||||||||||||||
| } catch { | ||||||||||||||||
| data = { count: 0 }; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| data.count += 1; // increment usage count | ||||||||||||||||
|
|
||||||||||||||||
| await env.LANG_TRANSLATION_ANALYTICS.put(key, JSON.stringify(data)); | ||||||||||||||||
| } | ||||||||||||||||
| async function handleTranslate(request: Request, model: ReturnType<GoogleGenerativeAI['getGenerativeModel']>, env: Env) { | ||||||||||||||||
| const { code, targetLanguage } = await request.json<{ code: string; targetLanguage: string }>(); | ||||||||||||||||
|
|
||||||||||||||||
| if (!code || !targetLanguage) { | ||||||||||||||||
|
|
@@ -47,7 +66,7 @@ async function handleTranslate(request: Request, model: ReturnType<GoogleGenerat | |||||||||||||||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const sourceLanguage = await detectLanguage(code, model); | ||||||||||||||||
| const prompt = `Translate the following code snippet to ${targetLanguage}. | ||||||||||||||||
| Do not add any explanation, commentary, or markdown formatting like \`\`\` around the code. | ||||||||||||||||
| **IMPORTANT: Preserve all original comments and their exact placement in the translated code. Do not add extra spaces in between.** | ||||||||||||||||
|
|
@@ -58,8 +77,8 @@ ${code}`; | |||||||||||||||
|
|
||||||||||||||||
| const result = await model.generateContent(prompt); | ||||||||||||||||
| const translatedCode = result.response.text(); | ||||||||||||||||
|
|
||||||||||||||||
| return new Response(JSON.stringify({ translation: translatedCode }), { | ||||||||||||||||
| await updateAnalytics(sourceLanguage, targetLanguage, env); | ||||||||||||||||
| return new Response(JSON.stringify({ translation: translatedCode, sourceLanguage }), { | ||||||||||||||||
| status: 200, | ||||||||||||||||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -102,23 +121,34 @@ export default { | |||||||||||||||
|
|
||||||||||||||||
| try { | ||||||||||||||||
| const ip = request.headers.get('CF-Connecting-IP') || 'unknown'; | ||||||||||||||||
| const allowed = await checkRateLimit(ip, env); | ||||||||||||||||
| if (!allowed) { | ||||||||||||||||
| return new Response(JSON.stringify({ error: "Too many requests. Try again later." }), { | ||||||||||||||||
| status: 429, | ||||||||||||||||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| const allowed = await checkRateLimit(ip, env); | ||||||||||||||||
| if (!allowed) { | ||||||||||||||||
| return new Response(JSON.stringify({ error: 'Too many requests. Try again later.' }), { | ||||||||||||||||
| status: 429, | ||||||||||||||||
| headers: { ...corsHeaders, 'Content-Type': 'application/json' }, | ||||||||||||||||
| }); | ||||||||||||||||
| } | ||||||||||||||||
| const url = new URL(request.url); | ||||||||||||||||
| const path = url.pathname; | ||||||||||||||||
| const genAI = new GoogleGenerativeAI(env.GEMINI_API_KEY); | ||||||||||||||||
| const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash' }); | ||||||||||||||||
| if (path === '/v1/analytics') { | ||||||||||||||||
| const list = await env.LANG_TRANSLATION_ANALYTICS.list(); | ||||||||||||||||
| const stats: Record<string, any> = {}; | ||||||||||||||||
| for (const key of list.keys) { | ||||||||||||||||
| const val = await env.LANG_TRANSLATION_ANALYTICS.get(key.name); | ||||||||||||||||
| stats[key.name] = JSON.parse(val || '{}'); | ||||||||||||||||
|
||||||||||||||||
| stats[key.name] = JSON.parse(val || '{}'); | |
| try { | |
| stats[key.name] = JSON.parse(val || '{}'); | |
| } catch (e) { | |
| console.error(`Failed to parse analytics value for key "${key.name}":`, e); | |
| stats[key.name] = {}; | |
| } |
Outdated
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The detectLanguage function should normalize the returned language name to lowercase to ensure consistent analytics keys, preventing duplicate entries like 'Python' vs 'python'.
| return result.response.text().trim(); | |
| return result.response.text().trim().toLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The analytics key should be normalized (lowercase and trimmed) to avoid duplicates, similar to how the rate limit key is handled on line 19.