1- import { useCallback , useEffect } from "react" ;
1+ import { useCallback , useEffect , useMemo , useState } from "react" ;
22import { usePersistedState , readPersistedState , updatePersistedState } from "./usePersistedState" ;
33import { MODEL_ABBREVIATIONS } from "@/browser/utils/slashCommands/registry" ;
44import { defaultModel } from "@/common/utils/ai/models" ;
@@ -42,13 +42,15 @@ export function getDefaultModel(): string {
4242 * Hook to manage a Least Recently Used (LRU) cache of AI models.
4343 * Stores up to 8 recently used models in localStorage.
4444 * Initializes with default abbreviated models if empty.
45+ * Also includes custom models configured in Settings.
4546 */
4647export function useModelLRU ( ) {
4748 const [ recentModels , setRecentModels ] = usePersistedState < string [ ] > (
4849 LRU_KEY ,
4950 DEFAULT_MODELS . slice ( 0 , MAX_LRU_SIZE ) ,
5051 { listener : true }
5152 ) ;
53+ const [ customModels , setCustomModels ] = useState < string [ ] > ( [ ] ) ;
5254
5355 const [ defaultModel , setDefaultModel ] = usePersistedState < string > (
5456 DEFAULT_MODEL_KEY ,
@@ -70,6 +72,44 @@ export function useModelLRU() {
7072 // eslint-disable-next-line react-hooks/exhaustive-deps
7173 } , [ ] ) ; // Only run once on mount
7274
75+ // Fetch custom models from providers config
76+ useEffect ( ( ) => {
77+ const fetchCustomModels = async ( ) => {
78+ try {
79+ const config = await window . api . providers . getConfig ( ) ;
80+ const models : string [ ] = [ ] ;
81+ for ( const [ provider , providerConfig ] of Object . entries ( config ) ) {
82+ if ( providerConfig . models ) {
83+ for ( const modelId of providerConfig . models ) {
84+ // Format as provider:modelId for consistency
85+ models . push ( `${ provider } :${ modelId } ` ) ;
86+ }
87+ }
88+ }
89+ setCustomModels ( models ) ;
90+ } catch {
91+ // Ignore errors fetching custom models
92+ }
93+ } ;
94+ void fetchCustomModels ( ) ;
95+
96+ // Listen for settings changes via custom event
97+ const handleSettingsChange = ( ) => void fetchCustomModels ( ) ;
98+ window . addEventListener ( "providers-config-changed" , handleSettingsChange ) ;
99+ return ( ) => window . removeEventListener ( "providers-config-changed" , handleSettingsChange ) ;
100+ } , [ ] ) ;
101+
102+ // Combine LRU models with custom models (custom models appended, deduplicated)
103+ const allModels = useMemo ( ( ) => {
104+ const combined = [ ...recentModels ] ;
105+ for ( const model of customModels ) {
106+ if ( ! combined . includes ( model ) ) {
107+ combined . push ( model ) ;
108+ }
109+ }
110+ return combined ;
111+ } , [ recentModels , customModels ] ) ;
112+
73113 /**
74114 * Add a model to the LRU cache. If it already exists, move it to the front.
75115 * If the cache is full, remove the least recently used model.
@@ -94,8 +134,8 @@ export function useModelLRU() {
94134 * Get the list of recently used models, most recent first.
95135 */
96136 const getRecentModels = useCallback ( ( ) => {
97- return recentModels ;
98- } , [ recentModels ] ) ;
137+ return allModels ;
138+ } , [ allModels ] ) ;
99139
100140 const evictModel = useCallback ( ( modelString : string ) => {
101141 if ( ! modelString . trim ( ) ) {
@@ -108,7 +148,7 @@ export function useModelLRU() {
108148 addModel,
109149 evictModel,
110150 getRecentModels,
111- recentModels,
151+ recentModels : allModels ,
112152 defaultModel,
113153 setDefaultModel,
114154 } ;
0 commit comments