# ๐Ÿ” OpenRouter Model Loading Debug Guide ## ๐Ÿ› **Issue**: Cannot load models via OpenRouter ### ๐Ÿ“Š **Debugging Steps Added** I've added comprehensive logging to help identify the issue: 1. **Hook Debugging** (`/hooks/useOpenRouterModels.ts`): - Console logs for fetch start/end - Response status logging - Model count logging 2. **API Route Debugging** (`/app/api/openrouter/models/route.ts`): - Request logging - API key status logging - Success/error logging 3. **OpenRouter Client Debugging** (`/lib/openrouter.ts`): - Request headers logging - Response status logging - Error details logging 4. **Component Debugging** (`/components/openrouter-model-selector/index.tsx`): - Render state logging ### ๐Ÿ”ง **Fixes Applied** 1. **Fixed Infinite Loop**: Removed dependency array issue in useOpenRouterModels hook 2. **Updated Referer**: Changed from `deepsite.hf.co` to `localhost:3000` for local development 3. **Enhanced Error Handling**: Better error messages and logging ### ๐Ÿงช **How to Debug** #### **Step 1: Open Browser Console** 1. Go to `http://localhost:3000` 2. Open Developer Tools (F12) 3. Go to Console tab #### **Step 2: Test Settings Panel** 1. Open the AI chat settings 2. Toggle "Use Custom OpenRouter Models" ON 3. Watch console for logs starting with: - ๐Ÿ”„ (fetch start) - ๐Ÿ“ก (request details) - ๐Ÿ“ฅ (response status) - โœ… (success) or โŒ (error) #### **Step 3: Test Model Selector** 1. Click "Select Model" button 2. Watch console for: - ๐Ÿ” OpenRouterModelSelector render logs - Loading state changes - Model count updates #### **Step 4: Test Direct API** 1. Go to `http://localhost:3000/test-openrouter.html` 2. Click "Test API" button 3. Check both page result and console logs ### ๐Ÿ” **Common Issues to Look For** #### **Network Issues** - Check Console โ†’ Network tab - Look for `/api/openrouter/models` request - Status should be 200, not 404/500 #### **CORS Issues** - Error messages mentioning CORS - OpenRouter API blocking requests - Referer header issues #### **API Rate Limiting** - OpenRouter returning 429 status - "Too many requests" errors #### **Environment Issues** - Missing environment variables - Local development configuration #### **Code Issues** - JavaScript errors in console - Component not mounting - Hook not calling fetch ### ๐Ÿ“ **Expected Console Output** **Successful Flow:** ``` ๐Ÿ”„ Fetching OpenRouter models... ๐Ÿ“ก Requesting: http://localhost:3000/api/openrouter/models ๐Ÿ”„ OpenRouter models API called ๐Ÿ”‘ API key provided: false ๐Ÿ“ก Fetching models from OpenRouter... ๐Ÿ”— Headers: ['Content-Type', 'HTTP-Referer', 'X-Title'] ๐Ÿ“ฅ OpenRouter API response status: 200 โœ… OpenRouter API returned 200+ models โœ… Successfully fetched 200+ models from OpenRouter ๐Ÿ“ฅ Response status: 200 ๐Ÿ“‹ Response data: { success: true, data: [...] } โœ… Successfully fetched 200+ models ๐Ÿ” OpenRouterModelSelector render: { modelsCount: 200+, loading: false, error: null } ``` **Error Flow Examples:** ``` โŒ Error fetching models: Failed to fetch โŒ OpenRouter API error: 403 Forbidden โŒ Network error: fetch failed ``` ### ๐Ÿšจ **If Still Not Working** #### **Check 1: API Endpoint Accessibility** Try: `http://localhost:3000/api/openrouter/models` Should return JSON with `success: true` #### **Check 2: OpenRouter API Direct** Try: `https://openrouter.ai/api/v1/models` Should return list of models #### **Check 3: Network Tab** - Failed requests (red) - CORS errors - Timeout issues #### **Check 4: Console Errors** - JavaScript compilation errors - React component errors - Network fetch errors Let me know what you see in the console and I'll help identify the specific issue! ๐Ÿ”ง