Skip to content

An AI-powered search engine with a generative UI

fix: Replace fs module with static JSON imports

Yoshiki Miura2e1e5b2a085c
diff --git a/lib/config/load-models-config.ts b/lib/config/load-models-config.ts
index fca0f0aa..f070f24c 100644
--- a/lib/config/load-models-config.ts
+++ b/lib/config/load-models-config.ts
@@ -1,6 +1,5 @@
-import fsSync from 'fs'
-import fs from 'fs/promises'
-import path from 'path'
+import cloudConfig from '@/config/models/cloud.json'
+import defaultConfig from '@/config/models/default.json'
import { ModelType } from '@/lib/types/model-type'
import { Model } from '@/lib/types/models'
@@ -20,14 +19,6 @@ let cachedProfile: string | null = null
const VALID_MODEL_TYPES: ModelType[] = ['speed', 'quality']
const VALID_SEARCH_MODES: SearchMode[] = ['quick', 'adaptive', 'planning']
-function resolveConfigPath(): string {
- const profile =
- process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true' ? 'cloud' : 'default'
- const file = `${profile}.json`
- const configPath = path.resolve(process.cwd(), 'config', 'models', file)
- return configPath
-}
-
function validateModelsConfigStructure(
json: unknown
): asserts json is ModelsConfig {
@@ -76,69 +67,29 @@ export async function loadModelsConfig(): Promise<ModelsConfig> {
return cachedConfig
}
- const filePath = resolveConfigPath()
- try {
- const raw = await fs.readFile(filePath, 'utf-8')
- const json = JSON.parse(raw)
-
- validateModelsConfigStructure(json)
+ const config = profile === 'cloud' ? cloudConfig : defaultConfig
+ validateModelsConfigStructure(config)
- cachedConfig = json as ModelsConfig
- cachedProfile = profile
- return cachedConfig
- } catch (err) {
- // If selected profile fails, try default as a safe fallback
- if (profile !== 'default') {
- const fallbackPath = path.resolve(
- process.cwd(),
- 'config',
- 'models',
- 'default.json'
- )
- const raw = await fs.readFile(fallbackPath, 'utf-8')
- const json = JSON.parse(raw)
- validateModelsConfigStructure(json)
- cachedConfig = json as ModelsConfig
- cachedProfile = 'default'
- return cachedConfig
- }
- throw err
- }
+ cachedConfig = config as ModelsConfig
+ cachedProfile = profile
+ return cachedConfig
}
// Synchronous load (for code paths that need sync access)
export function loadModelsConfigSync(): ModelsConfig {
const profile =
process.env.MORPHIC_CLOUD_DEPLOYMENT === 'true' ? 'cloud' : 'default'
+
if (cachedConfig && cachedProfile === profile) {
return cachedConfig
}
- const filePath = resolveConfigPath()
- try {
- const raw = fsSync.readFileSync(filePath, 'utf-8')
- const json = JSON.parse(raw)
- validateModelsConfigStructure(json)
- cachedConfig = json as ModelsConfig
- cachedProfile = profile
- return cachedConfig
- } catch (err) {
- if (profile !== 'default') {
- const fallbackPath = path.resolve(
- process.cwd(),
- 'config',
- 'models',
- 'default.json'
- )
- const raw = fsSync.readFileSync(fallbackPath, 'utf-8')
- const json = JSON.parse(raw)
- validateModelsConfigStructure(json)
- cachedConfig = json as ModelsConfig
- cachedProfile = 'default'
- return cachedConfig
- }
- throw err
- }
+ const config = profile === 'cloud' ? cloudConfig : defaultConfig
+ validateModelsConfigStructure(config)
+
+ cachedConfig = config as ModelsConfig
+ cachedProfile = profile
+ return cachedConfig
}
// Public accessor that ensures a config is available synchronously
diff --git a/lib/config/ollama-validator.ts b/lib/config/ollama-validator.ts
index a580e925..227c0cff 100644
--- a/lib/config/ollama-validator.ts
+++ b/lib/config/ollama-validator.ts
@@ -1,6 +1,3 @@
-'use server'
-
-import { loadModelsConfig } from '@/lib/config/load-models-config'
import { OllamaClient } from '@/lib/ollama/client'
import { Model } from '@/lib/types/models'
@@ -13,22 +10,31 @@ let validationError: Error | null = null
/**
* Extract all Ollama models from the configuration
+ * Uses dynamic import to avoid bundling fs module
*/
-function getConfiguredOllamaModels(config: any): Model[] {
+async function getConfiguredOllamaModels(): Promise<Model[]> {
const ollamaModels: Model[] = []
- // Check byMode models
- for (const mode of Object.values(config.models.byMode)) {
- for (const model of Object.values(mode as Record<string, Model>)) {
- if (model.providerId === 'ollama') {
- ollamaModels.push(model)
+ try {
+ // Dynamic import to avoid bundling server-only code
+ const { loadModelsConfig } = await import('@/lib/config/load-models-config')
+ const config = await loadModelsConfig()
+
+ // Check byMode models
+ for (const mode of Object.values(config.models.byMode)) {
+ for (const model of Object.values(mode as Record<string, Model>)) {
+ if (model.providerId === 'ollama') {
+ ollamaModels.push(model)
+ }
}
}
- }
- // Check relatedQuestions model
- if (config.models.relatedQuestions?.providerId === 'ollama') {
- ollamaModels.push(config.models.relatedQuestions)
+ // Check relatedQuestions model
+ if (config.models.relatedQuestions?.providerId === 'ollama') {
+ ollamaModels.push(config.models.relatedQuestions)
+ }
+ } catch (error) {
+ console.warn('Failed to load model configuration:', error)
}
return ollamaModels
@@ -90,8 +96,7 @@ export async function initializeOllamaValidation(): Promise<void> {
// Check configured models against validated models
try {
- const config = await loadModelsConfig()
- const configuredOllamaModels = getConfiguredOllamaModels(config)
+ const configuredOllamaModels = await getConfiguredOllamaModels()
if (configuredOllamaModels.length > 0) {
console.log(