An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: migrate model configuration to JSON files
Yoshiki Miura7999c29693fa
diff --git a/config/models/cloud.json b/config/models/cloud.jsonnew file mode 100644index 00000000..e9f6c587--- /dev/null+++ b/config/models/cloud.json@@ -0,0 +1,36 @@+{+ "version": 1,+ "models": {+ "types": {+ "speed": {+ "id": "gpt-5-mini",+ "name": "GPT-5 mini",+ "provider": "OpenAI",+ "providerId": "openai",+ "providerOptions": {+ "openai": {+ "reasoningEffort": "low",+ "reasoningSummary": "auto"+ }+ }+ },+ "quality": {+ "id": "anthropic/claude-sonnet-4",+ "name": "Claude Sonnet 4",+ "provider": "Vercel AI Gateway",+ "providerId": "gateway",+ "providerOptions": {+ "gateway": {+ "order": ["anthropic", "bedrock", "vertex"]+ }+ }+ }+ },+ "relatedQuestions": {+ "id": "gemini-2.0-flash",+ "name": "Gemini 2.0 Flash",+ "provider": "Google",+ "providerId": "google"+ }+ }+}diff --git a/config/models/default.json b/config/models/default.jsonnew file mode 100644index 00000000..26fbfe70--- /dev/null+++ b/config/models/default.json@@ -0,0 +1,37 @@+{+ "version": 1,+ "models": {+ "types": {+ "speed": {+ "id": "gpt-5-mini",+ "name": "GPT-5 mini",+ "provider": "OpenAI",+ "providerId": "openai",+ "providerOptions": {+ "openai": {+ "reasoningEffort": "low",+ "reasoningSummary": "auto"+ }+ }+ },+ "quality": {+ "id": "gpt-5",+ "name": "GPT-5",+ "provider": "OpenAI",+ "providerId": "openai",+ "providerOptions": {+ "openai": {+ "reasoningEffort": "low",+ "reasoningSummary": "auto"+ }+ }+ }+ },+ "relatedQuestions": {+ "id": "gpt-4o-mini",+ "name": "GPT-4o mini",+ "provider": "OpenAI",+ "providerId": "openai"+ }+ }+}diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.tsindex 1d67d45e..11784d35 100644--- a/lib/agents/generate-related-questions.ts+++ b/lib/agents/generate-related-questions.ts@@ -1,7 +1,7 @@import { generateObject, type ModelMessage } from 'ai'import { z } from 'zod'-import { RELATED_QUESTIONS_MODEL_CONFIG } from '../config/model-types'+import { getRelatedQuestionsModel } from '../config/model-types'import { getModel } from '../utils/registry'import { isTracingEnabled } from '../utils/telemetry'@@ -22,8 +22,9 @@ export async function generateRelatedQuestions(abortSignal?: AbortSignal,parentTraceId?: string) {- // Use the related questions model configuration- const modelId = `${RELATED_QUESTIONS_MODEL_CONFIG.providerId}:${RELATED_QUESTIONS_MODEL_CONFIG.id}`+ // Use the related questions model configuration from JSON+ const relatedModel = getRelatedQuestionsModel()+ const modelId = `${relatedModel.providerId}:${relatedModel.id}`const { object } = await generateObject({model: getModel(modelId),diff --git a/lib/config/load-models-config.ts b/lib/config/load-models-config.tsnew file mode 100644index 00000000..155783e9--- /dev/null+++ b/lib/config/load-models-config.ts@@ -0,0 +1,112 @@+import fsSync from 'fs'+import fs from 'fs/promises'+import path from 'path'++import { ModelType } from '@/lib/types/model-type'+import { Model } from '@/lib/types/models'++export interface ModelsConfig {+ version: number+ models: {+ types: Record<ModelType, Model>+ relatedQuestions: Model+ }+}++let cachedConfig: ModelsConfig | null = null+let cachedProfile: string | null = null++function resolveConfigPath(): string {+ const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'+ const file = `${profile}.json`+ const configPath = path.resolve(process.cwd(), 'config', 'models', file)+ return configPath+}++export async function loadModelsConfig(): Promise<ModelsConfig> {+ const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'++ if (cachedConfig && cachedProfile === profile) {+ return cachedConfig+ }++ const filePath = resolveConfigPath()+ try {+ const raw = await fs.readFile(filePath, 'utf-8')+ const json = JSON.parse(raw)++ // Minimal validation+ if (!json || typeof json !== 'object') {+ throw new Error('Invalid models config: not an object')+ }+ if (typeof json.version !== 'number') {+ throw new Error('Invalid models config: missing version')+ }+ if (!json.models || typeof json.models !== 'object') {+ throw new Error('Invalid models config: missing models')+ }+ if (!json.models.types || !json.models.relatedQuestions) {+ throw new Error('Invalid models config: missing required sections')+ }++ 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)+ cachedConfig = json as ModelsConfig+ cachedProfile = 'default'+ return cachedConfig+ }+ throw err+ }+}++// Synchronous load (for code paths that need sync access)+export function loadModelsConfigSync(): ModelsConfig {+ const profile = process.env.MORPHIC_MODELS_PROFILE?.trim() || 'default'+ if (cachedConfig && cachedProfile === profile) {+ return cachedConfig+ }++ const filePath = resolveConfigPath()+ try {+ const raw = fsSync.readFileSync(filePath, 'utf-8')+ const json = JSON.parse(raw)+ 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)+ cachedConfig = json as ModelsConfig+ cachedProfile = 'default'+ return cachedConfig+ }+ throw err+ }+}++// Public accessor that ensures a config is available synchronously+export function getModelsConfig(): ModelsConfig {+ if (!cachedConfig) {+ return loadModelsConfigSync()+ }+ return cachedConfig+}diff --git a/lib/config/model-types.ts b/lib/config/model-types.tsindex 009f5f5d..80aa9c0e 100644--- a/lib/config/model-types.ts+++ b/lib/config/model-types.ts@@ -1,42 +1,16 @@import { ModelType } from '@/lib/types/model-type'import { Model } from '@/lib/types/models'-// Model configurations for each model type-export const MODEL_TYPE_CONFIGS: Record<ModelType, Model> = {- speed: {- id: 'gpt-5-mini',- name: 'GPT-5 mini',- provider: 'OpenAI',- providerId: 'openai',- providerOptions: {- openai: {- reasoningEffort: 'low',- reasoningSummary: 'auto'- }- }- },- quality: {- id: 'anthropic/claude-sonnet-4',- name: 'Claude Sonnet 4',- provider: 'Vercel AI Gateway',- providerId: 'gateway',- providerOptions: {- gateway: {- order: ['anthropic', 'bedrock', 'vertext']- }- }- }-}+import { getModelsConfig } from './load-models-config'-// Model configuration for related questions generation-export const RELATED_QUESTIONS_MODEL_CONFIG: Model = {- id: 'gemini-2.0-flash',- name: 'Gemini 2.0 Flash',- provider: 'Google',- providerId: 'google'+// Get model for a specific type from JSON config+export function getModelForType(type: ModelType): Model {+ const cfg = getModelsConfig()+ return cfg.models.types[type]}-// Helper function to get model for a specific type-export function getModelForType(type: ModelType): Model {- return MODEL_TYPE_CONFIGS[type]+// Get model for related questions generation from JSON config+export function getRelatedQuestionsModel(): Model {+ const cfg = getModelsConfig()+ return cfg.models.relatedQuestions}