Skip to content

An AI-powered search engine with a generative UI

Fix models.json loading in various environments

Yoshiki Miura81a6c9a00e47
diff --git a/lib/config/models.ts b/lib/config/models.ts
index 2211ae8a..eaa9f525 100644
--- a/lib/config/models.ts
+++ b/lib/config/models.ts
@@ -9,15 +9,51 @@ export function validateModel(model: any): model is Model {
typeof model.providerId === 'string' &&
typeof model.enabled === 'boolean' &&
(model.toolCallType === 'native' || model.toolCallType === 'manual') &&
- (model.toolCallModel === undefined || typeof model.toolCallModel === 'string')
+ (model.toolCallModel === undefined ||
+ typeof model.toolCallModel === 'string')
)
}
export async function getModels(): Promise<Model[]> {
try {
const headersList = await headers()
- const baseUrl = new URL(headersList.get('x-url') || 'http://localhost:3000')
- const modelUrl = new URL('/config/models.json', baseUrl.origin)
+ const baseUrl = headersList.get('x-base-url')
+ const url = headersList.get('x-url')
+ const host = headersList.get('x-host')
+ const protocol = headersList.get('x-protocol') || 'http:'
+
+ // Construct base URL using the headers
+ let baseUrlObj: URL
+
+ try {
+ // Try to use the pre-constructed base URL if available
+ if (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 URL
+ const modelUrl = new URL('/config/models.json', baseUrlObj)
+
+ console.log('Fetching models from:', modelUrl.toString())
const response = await fetch(modelUrl, {
cache: 'no-store'
@@ -30,6 +66,6 @@ export async function getModels(): Promise<Model[]> {
} catch (error) {
console.warn('Failed to load models:', error)
}
-
+
return []
}
diff --git a/middleware.ts b/middleware.ts
index 9beff3cb..e50a76c5 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -1,14 +1,26 @@
-import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
+import { NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
// Create a response
const response = NextResponse.next()
+ // Get the protocol from X-Forwarded-Proto header or request protocol
+ const protocol =
+ request.headers.get('x-forwarded-proto') || request.nextUrl.protocol
+
+ // Get the host from X-Forwarded-Host header or request host
+ const host =
+ request.headers.get('x-forwarded-host') || request.headers.get('host') || ''
+
+ // Construct the base URL - ensure protocol has :// format
+ const baseUrl = `${protocol}${protocol.endsWith(':') ? '//' : '://'}${host}`
+
// Add request information to response headers
response.headers.set('x-url', request.url)
- response.headers.set('x-host', request.headers.get('host') || '')
- response.headers.set('x-protocol', request.nextUrl.protocol)
+ response.headers.set('x-host', host)
+ response.headers.set('x-protocol', protocol)
+ response.headers.set('x-base-url', baseUrl)
return response
}