feat: add IP geolocation tracking and include location in prompts

- Add GeoLite2-City.mmdb database for IP lookup
- Create geoip.py module for IP location services
- Extract client IP from requests and log location info
- Pass location context to LLM prompts for enhanced responses
This commit is contained in:
2026-02-18 08:59:28 +08:00
parent 2b79f20e19
commit d2b64ad5d6
7 changed files with 181 additions and 13 deletions

View File

@@ -1,10 +1,30 @@
import { API_URL } from './config.js'
let cachedIP = null
async function getClientIP() {
if (cachedIP) return cachedIP
try {
const controller = new AbortController()
setTimeout(() => controller.abort(), 3000)
const res = await fetch('https://api.ipify.org?format=json', { signal: controller.signal })
const data = await res.json()
cachedIP = data.ip
return cachedIP
} catch {
return null
}
}
export async function fetchSuggestion(prefix, suffix, signal, apiUrl = API_URL) {
try {
const clientIP = await getClientIP()
const headers = { 'Content-Type': 'application/json' }
if (clientIP) headers['X-Client-IP'] = clientIP
const res = await fetch(apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers,
body: JSON.stringify({ prefix, suffix, languageId: 'markdown' }),
signal
})