An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix models.json loading in Vercel environment with fallback to default models
Yoshiki Miura517756c2ca8c
diff --git a/lib/config/default-models.json b/lib/config/default-models.jsonnew file mode 100644index 00000000..5f7ad18a--- /dev/null+++ b/lib/config/default-models.json@@ -0,0 +1,163 @@+{+ "models": [+ {+ "id": "o3-mini",+ "name": "o3 mini",+ "provider": "OpenAI",+ "providerId": "openai",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "gpt-4o",+ "name": "GPT-4o",+ "provider": "OpenAI",+ "providerId": "openai",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "gpt-4o-mini",+ "name": "GPT-4o mini",+ "provider": "OpenAI",+ "providerId": "openai",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "claude-3-7-sonnet-20250219",+ "name": "Claude 3.7 Sonnet",+ "provider": "Anthropic",+ "providerId": "anthropic",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "claude-3-5-sonnet-latest",+ "name": "Claude 3.5 Sonnet",+ "provider": "Anthropic",+ "providerId": "anthropic",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "claude-3-5-haiku-20241022",+ "name": "Claude 3.5 Haiku",+ "provider": "Anthropic",+ "providerId": "anthropic",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "<AZURE_DEPLOYMENT_NAME>",+ "name": "<AZURE_DEPLOYMENT_NAME>",+ "provider": "Azure",+ "providerId": "azure",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "gemini-2.0-flash",+ "name": "Gemini 2.0 Flash",+ "provider": "Google Generative AI",+ "providerId": "google",+ "enabled": true,+ "toolCallType": "manual"+ },+ {+ "id": "gemini-2.0-pro-exp-02-05",+ "name": "Gemini 2.0 Pro (Exp)",+ "provider": "Google Generative AI",+ "providerId": "google",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "gemini-2.0-flash"+ },+ {+ "id": "gemini-2.0-flash-thinking-exp-01-21",+ "name": "Gemini 2.0 Flash Thinking (Exp)",+ "provider": "Google Generative AI",+ "providerId": "google",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "gemini-2.0-flash"+ },+ {+ "id": "accounts/fireworks/models/deepseek-r1",+ "name": "DeepSeek R1",+ "provider": "Fireworks",+ "providerId": "fireworks",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "accounts/fireworks/models/llama-v3p1-8b-instruct"+ },+ {+ "id": "deepseek-reasoner",+ "name": "DeepSeek R1",+ "provider": "DeepSeek",+ "providerId": "deepseek",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "deepseek-chat"+ },+ {+ "id": "deepseek-chat",+ "name": "DeepSeek V3",+ "provider": "DeepSeek",+ "providerId": "deepseek",+ "enabled": true,+ "toolCallType": "manual"+ },+ {+ "id": "deepseek-r1-distill-llama-70b",+ "name": "DeepSeek R1 Distill Llama 70B",+ "provider": "Groq",+ "providerId": "groq",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "llama-3.1-8b-instant"+ },+ {+ "id": "deepseek-r1",+ "name": "DeepSeek R1",+ "provider": "Ollama",+ "providerId": "ollama",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "phi4"+ },+ {+ "id": "<OLLAMA_MODEL_ID>",+ "name": "<OLLAMA_MODEL_NAME>",+ "provider": "Ollama",+ "providerId": "ollama",+ "enabled": true,+ "toolCallType": "manual",+ "toolCallModel": "<OLLAMA_MODEL_ID>"+ },+ {+ "id": "grok-2-1212",+ "name": "Grok 2",+ "provider": "xAI",+ "providerId": "xai",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "grok-2-vision-1212",+ "name": "Grok 2 Vision",+ "provider": "xAI",+ "providerId": "xai",+ "enabled": true,+ "toolCallType": "native"+ },+ {+ "id": "<OPENAI_COMPATIBLE_MODEL>",+ "name": "<OPENAI_COMPATIBLE_MODEL>",+ "provider": "OpenAI Compatible",+ "providerId": "openai-compatible",+ "enabled": true,+ "toolCallType": "native"+ }+ ]+}diff --git a/lib/config/models.ts b/lib/config/models.tsindex eaa9f525..f3a9b688 100644--- a/lib/config/models.ts+++ b/lib/config/models.ts@@ -1,5 +1,6 @@import { Model } from '@/lib/types/models'import { headers } from 'next/headers'+import defaultModels from './default-models.json'export function validateModel(model: any): model is Model {return (@@ -28,44 +29,61 @@ export async function getModels(): Promise<Model[]> {try {// Try to use the pre-constructed base URL if availableif (baseUrl) {- console.log('Using x-base-url:', baseUrl)baseUrlObj = new URL(baseUrl)} else if (url) {- console.log('Using x-url:', url)baseUrlObj = new URL(url)} else if (host) {const constructedUrl = `${protocol}${protocol.endsWith(':') ? '//' : '://'}${host}`- console.log('Constructed URL from host and protocol:', constructedUrl)baseUrlObj = new URL(constructedUrl)} else {- console.log('Using default localhost URL')baseUrlObj = new URL('http://localhost:3000')}} catch (urlError) {// Fallback to default URL if any error occurs during URL construction- console.warn('Error constructing URL:', urlError)- console.log('Falling back to default localhost URL')baseUrlObj = new URL('http://localhost:3000')}// Construct the models.json URLconst modelUrl = new URL('/config/models.json', baseUrlObj)- console.log('Fetching models from:', modelUrl.toString())+ try {+ const response = await fetch(modelUrl, {+ cache: 'no-store',+ headers: {+ Accept: 'application/json'+ }+ })++ if (!response.ok) {+ throw new Error(`HTTP error! status: ${response.status}`)+ }++ const text = await response.text()- const response = await fetch(modelUrl, {- cache: 'no-store'- })- const config = await response.json()- if (Array.isArray(config.models) && config.models.every(validateModel)) {- return config.models+ // Check if the response starts with HTML doctype+ if (text.trim().toLowerCase().startsWith('<!doctype')) {+ throw new Error('Received HTML instead of JSON')+ }++ const config = JSON.parse(text)+ if (Array.isArray(config.models) && config.models.every(validateModel)) {+ return config.models+ }+ } catch (fetchError) {+ // Fallback to default models if fetch fails+ if (+ Array.isArray(defaultModels.models) &&+ defaultModels.models.every(validateModel)+ ) {+ return defaultModels.models+ }}- console.warn('Invalid model configuration')} catch (error) {console.warn('Failed to load models:', error)}+ // Last resort: return empty arrayreturn []}