1
- use std:: path:: PathBuf ;
2
-
3
1
use anyhow:: Result ;
4
2
use chrono:: Utc ;
5
3
use clap:: { Args , Subcommand } ;
6
4
use clap_complete:: engine:: ArgValueCompleter ;
7
- use etcetera:: BaseStrategy ;
8
5
use rullm_core:: { LlmError , SimpleLlm , SimpleLlmClient } ;
9
6
10
7
use crate :: {
@@ -74,12 +71,12 @@ impl ModelsArgs {
74
71
ModelsAction :: Update { model } => {
75
72
let model_str = resolve_model ( & cli. model , model, & cli_config. config . default_model ) ?;
76
73
let client = client:: from_model ( & model_str, cli, cli_config) ?;
77
- update_models ( & client, output_level)
74
+ update_models ( cli_config , & client, output_level)
78
75
. await
79
76
. map_err ( anyhow:: Error :: from) ?;
80
77
}
81
78
ModelsAction :: Clear => {
82
- clear_models_cache ( output_level) ?;
79
+ clear_models_cache ( cli_config , output_level) ?;
83
80
}
84
81
}
85
82
@@ -106,7 +103,7 @@ pub fn show_cached_models(cli_config: &CliConfig, output_level: OutputLevel) ->
106
103
}
107
104
108
105
// Check if cache is stale (older than 24 hours)
109
- if let Ok ( Some ( cache) ) = load_models_cache ( ) {
106
+ if let Ok ( Some ( cache) ) = load_models_cache ( cli_config ) {
110
107
let now = Utc :: now ( ) ;
111
108
let cache_age = now. signed_duration_since ( cache. last_updated ) ;
112
109
@@ -150,10 +147,10 @@ pub async fn set_default_model(
150
147
Ok ( ( ) )
151
148
}
152
149
153
- pub fn clear_models_cache ( output_level : OutputLevel ) -> Result < ( ) > {
150
+ pub fn clear_models_cache ( cli_config : & CliConfig , output_level : OutputLevel ) -> Result < ( ) > {
154
151
use std:: fs;
155
152
156
- let path = get_models_cache_path ( ) ? ;
153
+ let path = cli_config . data_base_path . join ( MODEL_FILE_NAME ) ;
157
154
158
155
if path. exists ( ) {
159
156
fs:: remove_file ( & path) ?;
@@ -166,6 +163,7 @@ pub fn clear_models_cache(output_level: OutputLevel) -> Result<()> {
166
163
}
167
164
168
165
pub async fn update_models (
166
+ cli_config : & mut CliConfig ,
169
167
client : & SimpleLlmClient ,
170
168
output_level : OutputLevel ,
171
169
) -> Result < ( ) , LlmError > {
@@ -183,7 +181,7 @@ pub async fn update_models(
183
181
& format ! ( "Fetched {} models. Caching" , models. len( ) ) ,
184
182
output_level,
185
183
) ;
186
- if let Err ( e) = cache_models ( client. provider_name ( ) , & models) {
184
+ if let Err ( e) = cache_models ( cli_config , client. provider_name ( ) , & models) {
187
185
crate :: output:: error ( & format ! ( "Failed to cache: {e}" ) , output_level) ;
188
186
}
189
187
}
@@ -196,10 +194,12 @@ pub async fn update_models(
196
194
Ok ( ( ) )
197
195
}
198
196
199
- fn cache_models ( provider_name : & str , models : & [ String ] ) -> Result < ( ) > {
197
+ fn cache_models ( cli_config : & CliConfig , provider_name : & str , models : & [ String ] ) -> Result < ( ) > {
200
198
use std:: fs;
201
199
202
- let path = get_models_cache_path ( ) ?;
200
+ let path = cli_config. data_base_path . join ( MODEL_FILE_NAME ) ;
201
+ // TODO: we shouldn't need to do this here, this should be done while cli_config is created
202
+ // TODO: Remove if we already do this.
203
203
if let Some ( parent) = path. parent ( ) {
204
204
fs:: create_dir_all ( parent) ?;
205
205
}
@@ -216,19 +216,10 @@ fn cache_models(provider_name: &str, models: &[String]) -> Result<()> {
216
216
Ok ( ( ) )
217
217
}
218
218
219
- /// Helper function to get models cache path, eliminating duplication
220
- fn get_models_cache_path ( ) -> Result < PathBuf > {
221
- use crate :: constants:: BINARY_NAME ;
222
- use etcetera:: choose_base_strategy;
223
-
224
- let strategy = choose_base_strategy ( ) ?;
225
- Ok ( strategy. data_dir ( ) . join ( BINARY_NAME ) . join ( MODEL_FILE_NAME ) )
226
- }
227
-
228
- pub ( crate ) fn load_models_cache ( ) -> Result < Option < ModelsCache > > {
219
+ pub ( crate ) fn load_models_cache ( cli_config : & CliConfig ) -> Result < Option < ModelsCache > > {
229
220
use std:: fs;
230
221
231
- let path = get_models_cache_path ( ) ? ;
222
+ let path = cli_config . data_base_path . join ( MODEL_FILE_NAME ) ;
232
223
233
224
if !path. exists ( ) {
234
225
return Ok ( None ) ;
@@ -244,24 +235,3 @@ pub(crate) fn load_models_cache() -> Result<Option<ModelsCache>> {
244
235
// Old format doesn't have timestamp info
245
236
Ok ( None )
246
237
}
247
-
248
- pub fn load_cached_models ( ) -> Result < Vec < String > > {
249
- use std:: fs;
250
-
251
- let path = get_models_cache_path ( ) ?;
252
-
253
- if !path. exists ( ) {
254
- return Ok ( Vec :: new ( ) ) ;
255
- }
256
-
257
- let content = fs:: read_to_string ( path) ?;
258
-
259
- // Try to parse as new format first
260
- if let Ok ( cache) = serde_json:: from_str :: < ModelsCache > ( & content) {
261
- return Ok ( cache. models ) ;
262
- }
263
-
264
- // Fallback to old format (simple array)
265
- let entries: Vec < String > = serde_json:: from_str ( & content) ?;
266
- Ok ( entries)
267
- }
0 commit comments