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" ;
@@ -46,13 +46,15 @@ export function getDefaultModelFromLRU(): string {
4646 * Hook to manage a Least Recently Used (LRU) cache of AI models.
4747 * Stores up to 8 recently used models in localStorage.
4848 * Initializes with default abbreviated models if empty.
49+ * Also includes custom models configured in Settings.
4950 */
5051export function useModelLRU ( ) {
5152 const [ recentModels , setRecentModels ] = usePersistedState < string [ ] > (
5253 LRU_KEY ,
5354 DEFAULT_MODELS . slice ( 0 , MAX_LRU_SIZE ) ,
5455 { listener : true }
5556 ) ;
57+ const [ customModels , setCustomModels ] = useState < string [ ] > ( [ ] ) ;
5658
5759 // Merge any new defaults from MODEL_ABBREVIATIONS (only once on mount)
5860 useEffect ( ( ) => {
@@ -68,6 +70,44 @@ export function useModelLRU() {
6870 // eslint-disable-next-line react-hooks/exhaustive-deps
6971 } , [ ] ) ; // Only run once on mount
7072
73+ // Fetch custom models from providers config
74+ useEffect ( ( ) => {
75+ const fetchCustomModels = async ( ) => {
76+ try {
77+ const config = await window . api . providers . getConfig ( ) ;
78+ const models : string [ ] = [ ] ;
79+ for ( const [ provider , providerConfig ] of Object . entries ( config ) ) {
80+ if ( providerConfig . models ) {
81+ for ( const modelId of providerConfig . models ) {
82+ // Format as provider:modelId for consistency
83+ models . push ( `${ provider } :${ modelId } ` ) ;
84+ }
85+ }
86+ }
87+ setCustomModels ( models ) ;
88+ } catch {
89+ // Ignore errors fetching custom models
90+ }
91+ } ;
92+ void fetchCustomModels ( ) ;
93+
94+ // Listen for settings changes via custom event
95+ const handleSettingsChange = ( ) => void fetchCustomModels ( ) ;
96+ window . addEventListener ( "providers-config-changed" , handleSettingsChange ) ;
97+ return ( ) => window . removeEventListener ( "providers-config-changed" , handleSettingsChange ) ;
98+ } , [ ] ) ;
99+
100+ // Combine LRU models with custom models (custom models appended, deduplicated)
101+ const allModels = useMemo ( ( ) => {
102+ const combined = [ ...recentModels ] ;
103+ for ( const model of customModels ) {
104+ if ( ! combined . includes ( model ) ) {
105+ combined . push ( model ) ;
106+ }
107+ }
108+ return combined ;
109+ } , [ recentModels , customModels ] ) ;
110+
71111 /**
72112 * Add a model to the LRU cache. If it already exists, move it to the front.
73113 * If the cache is full, remove the least recently used model.
@@ -92,8 +132,8 @@ export function useModelLRU() {
92132 * Get the list of recently used models, most recent first.
93133 */
94134 const getRecentModels = useCallback ( ( ) => {
95- return recentModels ;
96- } , [ recentModels ] ) ;
135+ return allModels ;
136+ } , [ allModels ] ) ;
97137
98138 const evictModel = useCallback ( ( modelString : string ) => {
99139 if ( ! modelString . trim ( ) ) {
@@ -106,6 +146,6 @@ export function useModelLRU() {
106146 addModel,
107147 evictModel,
108148 getRecentModels,
109- recentModels,
149+ recentModels : allModels ,
110150 } ;
111151}
0 commit comments