Repository navigation
Expand file tree
/
Copy pathsettings.ts
More file actions
84 lines (73 loc) · 2.89 KB
/
Copy pathsettings.ts
File metadata and controls
84 lines (73 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import prisma from '@/lib/db'
// Module-level caches — avoids hundreds of DB roundtrips per pipeline run
let _cachedModel: string | null = null
let _modelCacheExpiry = 0
let _cachedProvider: 'anthropic' | 'openai' | 'minimax' | null = null
let _providerCacheExpiry = 0
let _cachedOpenAIModel: string | null = null
let _openAIModelCacheExpiry = 0
let _cachedMiniMaxModel: string | null = null
let _miniMaxModelCacheExpiry = 0
const CACHE_TTL = 5 * 60 * 1000
/**
* Get the configured Anthropic model from settings (cached for 5 minutes).
*/
export async function getAnthropicModel(): Promise<string> {
if (_cachedModel && Date.now() < _modelCacheExpiry) return _cachedModel
const setting = await prisma.setting.findUnique({ where: { key: 'anthropicModel' } })
_cachedModel = setting?.value ?? 'claude-haiku-4-5-20251001'
_modelCacheExpiry = Date.now() + CACHE_TTL
return _cachedModel
}
/**
* Get the active AI provider (cached for 5 minutes).
*/
export async function getProvider(): Promise<'anthropic' | 'openai' | 'minimax'> {
if (_cachedProvider && Date.now() < _providerCacheExpiry) return _cachedProvider
const setting = await prisma.setting.findUnique({ where: { key: 'aiProvider' } })
const val = setting?.value
_cachedProvider = val === 'openai' ? 'openai' : val === 'minimax' ? 'minimax' : 'anthropic'
_providerCacheExpiry = Date.now() + CACHE_TTL
return _cachedProvider
}
/**
* Get the configured OpenAI model from settings (cached for 5 minutes).
*/
export async function getOpenAIModel(): Promise<string> {
if (_cachedOpenAIModel && Date.now() < _openAIModelCacheExpiry) return _cachedOpenAIModel
const setting = await prisma.setting.findUnique({ where: { key: 'openaiModel' } })
_cachedOpenAIModel = setting?.value ?? 'gpt-4.1-mini'
_openAIModelCacheExpiry = Date.now() + CACHE_TTL
return _cachedOpenAIModel
}
/**
* Get the configured MiniMax model from settings (cached for 5 minutes).
*/
export async function getMiniMaxModel(): Promise<string> {
if (_cachedMiniMaxModel && Date.now() < _miniMaxModelCacheExpiry) return _cachedMiniMaxModel
const setting = await prisma.setting.findUnique({ where: { key: 'minimaxModel' } })
_cachedMiniMaxModel = setting?.value ?? 'MiniMax-M2.7'
_miniMaxModelCacheExpiry = Date.now() + CACHE_TTL
return _cachedMiniMaxModel
}
/**
* Get the model for the currently active provider.
*/
export async function getActiveModel(): Promise<string> {
const provider = await getProvider()
if (provider === 'minimax') return getMiniMaxModel()
return provider === 'openai' ? getOpenAIModel() : getAnthropicModel()
}
/**
* Clear all settings caches (call after settings are changed).
*/
export function invalidateSettingsCache(): void {
_cachedModel = null
_modelCacheExpiry = 0
_cachedProvider = null
_providerCacheExpiry = 0
_cachedOpenAIModel = null
_openAIModelCacheExpiry = 0
_cachedMiniMaxModel = null
_miniMaxModelCacheExpiry = 0
}