An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Move baseurl functionality to url.ts
Robert Douglassbfcc5a4ed057
diff --git a/lib/config/models.ts b/lib/config/models.tsindex 15a3e49f..fd15db08 100644--- a/lib/config/models.ts+++ b/lib/config/models.ts@@ -1,5 +1,5 @@import { Model } from '@/lib/types/models'-import { headers } from 'next/headers'+import { getBaseUrl } from '@/lib/utils/url'import defaultModels from './default-models.json'export function validateModel(model: any): model is Model {@@ -17,24 +17,8 @@ export function validateModel(model: any): model is Model {export async function getModels(): Promise<Model[]> {try {- // Check for NEXT_PUBLIC_BASE_URL environment variable first- const baseUrlEnv = process.env.NEXT_PUBLIC_BASE_URL || process.env.BASE_URL- let baseUrlObj: URL-- if (baseUrlEnv) {- try {- baseUrlObj = new URL(baseUrlEnv)- console.log('Using BASE_URL environment variable:', baseUrlEnv)- } catch (error) {- console.warn(- 'Invalid BASE_URL environment variable, falling back to headers'- )- baseUrlObj = await getBaseUrlFromHeaders()- }- } else {- // If BASE_URL is not set, use headers- baseUrlObj = await getBaseUrlFromHeaders()- }+ // Get the base URL using the centralized utility function+ const baseUrlObj = await getBaseUrl()// Construct the models.json URLconst modelUrl = new URL('/config/models.json', baseUrlObj)@@ -92,30 +76,3 @@ export async function getModels(): Promise<Model[]> {return []}-// Helper function to get base URL from headers-export async function getBaseUrlFromHeaders(): Promise<URL> {- const headersList = await headers()- 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:'-- try {- // Try to use the pre-constructed base URL if available- if (baseUrl) {- return new URL(baseUrl)- } else if (url) {- return new URL(url)- } else if (host) {- const constructedUrl = `${protocol}${- protocol.endsWith(':') ? '//' : '://'- }${host}`- return new URL(constructedUrl)- } else {- return new URL('http://localhost:3000')- }- } catch (urlError) {- // Fallback to default URL if any error occurs during URL construction- return new URL('http://localhost:3000')- }-}diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex 8d997d62..a5e35082 100644--- a/lib/tools/search.ts+++ b/lib/tools/search.ts@@ -1,6 +1,6 @@-import { getBaseUrlFromHeaders } from '@/lib/config/models'import { getSearchSchemaForModel } from '@/lib/schema/search'import { SearchResults } from '@/lib/types'+import { getBaseUrlString } from '@/lib/utils/url'import { tool } from 'ai'import { DEFAULT_PROVIDER, SearchProviderType, createSearchProvider } from './search/providers'@@ -47,15 +47,8 @@ export function createSearchTool(fullModel: string) {searchAPI === 'searxng' &&effectiveSearchDepthForAPI === 'advanced') {- // Check for NEXT_PUBLIC_BASE_URL first as an override- let baseUrl: string- if (process.env.NEXT_PUBLIC_BASE_URL) {- baseUrl = process.env.NEXT_PUBLIC_BASE_URL- } else {- // Use the base URL from headers- const baseUrlObj = await getBaseUrlFromHeaders()- baseUrl = baseUrlObj.toString()- }+ // Get the base URL using the centralized utility function+ const baseUrl = await getBaseUrlString()const response = await fetch(`${baseUrl}/api/advanced-search`, {method: 'POST',diff --git a/lib/utils/url.ts b/lib/utils/url.tsnew file mode 100644index 00000000..f0760c3f--- /dev/null+++ b/lib/utils/url.ts@@ -0,0 +1,68 @@+import { headers } from 'next/headers'++/**+ * Helper function to get base URL from headers+ * Extracts URL information from Next.js request headers+ */+export async function getBaseUrlFromHeaders(): Promise<URL> {+ const headersList = await headers()+ 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:'++ try {+ // Try to use the pre-constructed base URL if available+ if (baseUrl) {+ return new URL(baseUrl)+ } else if (url) {+ return new URL(url)+ } else if (host) {+ const constructedUrl = `${protocol}${+ protocol.endsWith(':') ? '//' : '://'+ }${host}`+ return new URL(constructedUrl)+ } else {+ return new URL('http://localhost:3000')+ }+ } catch (urlError) {+ // Fallback to default URL if any error occurs during URL construction+ return new URL('http://localhost:3000')+ }+}++/**+ * Resolves the base URL using environment variables or headers+ * Centralizes the base URL resolution logic used across the application+ * @returns A URL object representing the base URL+ */+export async function getBaseUrl(): Promise<URL> {+ // Check for environment variables first+ const baseUrlEnv = process.env.NEXT_PUBLIC_BASE_URL || process.env.BASE_URL++ if (baseUrlEnv) {+ try {+ const baseUrlObj = new URL(baseUrlEnv)+ console.log('Using BASE_URL environment variable:', baseUrlEnv)+ return baseUrlObj+ } catch (error) {+ console.warn(+ 'Invalid BASE_URL environment variable, falling back to headers'+ )+ // Fall back to headers if the environment variable is invalid+ }+ }++ // If no valid environment variable is available, use headers+ return await getBaseUrlFromHeaders()+}++/**+ * Gets the base URL as a string+ * Convenience wrapper around getBaseUrl that returns a string+ * @returns A string representation of the base URL+ */+export async function getBaseUrlString(): Promise<string> {+ const baseUrlObj = await getBaseUrl()+ return baseUrlObj.toString()+}\ No newline at end of file