Fix empty Language and AI Model dropdowns
Browse files- Update CORS configuration to allow HuggingFace Space domain and use regex for *.hf.space
- Improve API URL logic in frontend to correctly detect production vs dev environment
- Fix EventSource and WebSocket URL construction to work in both environments
- Allow requests from localhost:7860 for Docker Space internal communication
Files changed (2)
- backend_api.py+11-2
- frontend/src/lib/api.ts+37-15
33 unmodified lines34353637373839404142434445464740484950515253545533 unmodified lines
app = FastAPI(title="AnyCoder API", version="1.0.0")
# Configure CORS# Configure CORS - allow all origins in production, specific in dev# In Docker Space, requests come from the same domain via Next.js proxyALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "*").split(",") if os.getenv("ALLOWED_ORIGINS") else [ "http://localhost:3000", "http://localhost:3001", "http://localhost:7860", f"https://{SPACE_HOST}" if SPACE_HOST and not SPACE_HOST.startswith("localhost") else "http://localhost:7860"]
app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:3000", "http://localhost:3001"], # Frontend URLs allow_origins=ALLOWED_ORIGINS if ALLOWED_ORIGINS != ["*"] else ["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], allow_origin_regex=r"https://.*\.hf\.space" if os.getenv("SPACE_HOST") else None,)
# OAuth configuration10 unmodified lines1112131415161718141516171819202122232425262728293031323334353637383960 unmodified lines10010110210385868788899091929310410510610710810911011111211311411511632 unmodified lines14915015115215313215415515615710 unmodified lines} from '@/types';
// Use relative URLs in production (Next.js rewrites will proxy to backend)// In local dev without rewrites, use localhost:8000const API_URL = process.env.NEXT_PUBLIC_API_URL || (typeof window !== 'undefined' && window.location.hostname !== 'localhost' ? '' // Use relative URLs in production (proxied by Next.js) : 'http://localhost:8000'); // Local development// In local dev, use localhost:8000 for direct backend accessconst getApiUrl = () => { // If explicitly set via env var, use it if (process.env.NEXT_PUBLIC_API_URL) { return process.env.NEXT_PUBLIC_API_URL; } // For server-side rendering, always use relative URLs if (typeof window === 'undefined') { return ''; } // On localhost (dev mode), use direct backend URL const hostname = window.location.hostname; if (hostname === 'localhost' || hostname === '127.0.0.1') { return 'http://localhost:8000'; } // In production (HF Space), use relative URLs (Next.js proxies to backend) return '';};
const API_URL = getApiUrl();
class ApiClient { private client: AxiosInstance;60 unmodified lines onComplete: (code: string) => void, onError: (error: string) => void ): () => void { // Build the URL correctly whether we have a base URL or not const url = new URL('/api/generate', API_URL); const eventSource = new EventSource( url.toString() + '?' + new URLSearchParams({ query: request.query, language: request.language, model_id: request.model_id, provider: request.provider, }) ); const baseUrl = API_URL || window.location.origin; const url = new URL('/api/generate', baseUrl); url.search = new URLSearchParams({ query: request.query, language: request.language, model_id: request.model_id, provider: request.provider, }).toString(); const eventSource = new EventSource(url.toString());
eventSource.onmessage = (event) => { try {32 unmodified lines onComplete: (code: string) => void, onError: (error: string) => void ): WebSocket { // Build WebSocket URL correctly for both dev and production const baseUrl = API_URL || window.location.origin; const wsUrl = API_URL.replace('http', 'ws') + '/ws/generate'; const wsUrl = baseUrl.replace('http', 'ws') + '/ws/generate'; const ws = new WebSocket(wsUrl);
ws.onopen = () => {backend_api.py+11-2
frontend/src/lib/api.ts+37-15