forked from efugier/smartcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
327 lines (263 loc) · 10.3 KB
/
mod.rs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
pub mod api;
pub mod prompt;
pub mod voice;
use std::{path::PathBuf, process::Command};
use self::{
api::{api_keys_path, generate_api_keys_file, get_api_config},
prompt::{generate_prompts_file, get_prompts, prompts_path},
voice::{generate_voice_file, voice_config_path},
};
use crate::utils::is_interactive;
pub const PLACEHOLDER_TOKEN: &str = "#[<input>]";
const DEFAULT_CONFIG_PATH: &str = ".config/smartcat/";
const CUSTOM_CONFIG_ENV_VAR: &str = "SMARTCAT_CONFIG_PATH";
fn resolve_config_path() -> PathBuf {
if let Ok(custom_path) = std::env::var(CUSTOM_CONFIG_ENV_VAR) {
PathBuf::from(custom_path)
} else if let Ok(home_dir) = std::env::var("HOME") {
PathBuf::from(home_dir).join(DEFAULT_CONFIG_PATH)
} else {
panic!(
"Could not determine default config path. Set either ${CUSTOM_CONFIG_ENV_VAR} or $HOME"
)
}
}
pub fn ensure_config_files() -> std::io::Result<()> {
let interactive = is_interactive();
if !prompts_path().exists() {
if interactive {
println!(
"Prompt config file not found at {:?}, generating one.\n...",
prompts_path()
);
}
generate_prompts_file()?
}
if !voice_config_path().exists() {
println!(
"Voice config file not found at {:?}, generating one.\n...",
()
);
generate_voice_file().expect("Unable to generate config files");
};
if !api_keys_path().exists() {
println!(
"API config file not found at {:?}, generating one.\n...",
api_keys_path()
);
generate_api_keys_file().expect("Unable to generate config files");
if interactive {
ensure_config_usable();
}
};
Ok(())
}
pub fn ensure_config_usable() {
let interactive = is_interactive();
// check if any config has an API key;
let third_parth_config_usable = get_prompts().iter().any(|(_, prompt)| {
let api = get_api_config(&prompt.api.to_string());
api.api_key.is_some() || api.api_key_command.is_some()
});
if !third_parth_config_usable {
println!(
"No API key is configured.\n\
How to configure your API keys:\n\
https://github.com/efugier/smartcat/#configuration\n"
);
}
// check if local execution is possible with Ollama
if !is_executable_in_path("ollama") {
println!(
"Ollama not found in PATH.\n\
How to setup Ollama:\n\
https://github.com/efugier/smartcat#ollama-setup"
);
}
// nothing is setup
if interactive && !third_parth_config_usable && !is_executable_in_path("ollama") {
println!("\nInstall Ollama or set an api key for at least one of the providers to get started, then come back!");
std::process::exit(1);
}
}
fn is_executable_in_path(executable_name: &str) -> bool {
Command::new("which")
.arg(executable_name)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use crate::{
config::{
api::{api_keys_path, Api, ApiConfig},
ensure_config_files,
prompt::{prompts_path, Prompt},
resolve_config_path,
voice::{voice_config_path, VoiceConfig},
CUSTOM_CONFIG_ENV_VAR, DEFAULT_CONFIG_PATH,
},
utils::IS_NONINTERACTIVE_ENV_VAR,
};
use serial_test::serial;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
#[test]
#[serial]
fn resolver_custom_config_path() {
let temp_path = "/tmp/custom_path";
let original_value = env::var(CUSTOM_CONFIG_ENV_VAR);
env::set_var(CUSTOM_CONFIG_ENV_VAR, temp_path);
let result = resolve_config_path();
match original_value {
Ok(val) => env::set_var(CUSTOM_CONFIG_ENV_VAR, val),
Err(_) => env::remove_var(CUSTOM_CONFIG_ENV_VAR),
}
assert_eq!(result, Path::new(temp_path));
}
#[test]
#[serial]
fn resolve_default_config_path() {
let original_value = env::var(CUSTOM_CONFIG_ENV_VAR);
env::remove_var(CUSTOM_CONFIG_ENV_VAR);
let home_dir = env::var("HOME").expect("HOME not defined");
let default_path = PathBuf::new().join(home_dir).join(DEFAULT_CONFIG_PATH);
let result = resolve_config_path();
match original_value {
Ok(val) => env::set_var(CUSTOM_CONFIG_ENV_VAR, val),
Err(_) => env::remove_var(CUSTOM_CONFIG_ENV_VAR),
}
assert_eq!(result, Path::new(&default_path));
}
#[test]
#[serial]
fn test_ensure_config_files_not_existing() -> std::io::Result<()> {
let temp_dir = tempfile::TempDir::new()?;
let original_value = env::var(CUSTOM_CONFIG_ENV_VAR);
env::set_var(CUSTOM_CONFIG_ENV_VAR, temp_dir.path());
env::set_var(IS_NONINTERACTIVE_ENV_VAR, "1");
let api_keys_path = api_keys_path();
let prompts_path = prompts_path();
let voice_path = voice_config_path();
assert!(!api_keys_path.exists());
assert!(!prompts_path.exists());
assert!(!voice_path.exists());
let result = ensure_config_files();
match original_value {
Ok(val) => env::set_var(CUSTOM_CONFIG_ENV_VAR, val),
Err(_) => env::remove_var(CUSTOM_CONFIG_ENV_VAR),
}
result?;
assert!(api_keys_path.exists());
assert!(prompts_path.exists());
assert!(voice_path.exists());
Ok(())
}
#[test]
#[serial]
fn test_ensure_config_files_already_existing() -> std::io::Result<()> {
let temp_dir = tempfile::TempDir::new()?;
let original_value = env::var(CUSTOM_CONFIG_ENV_VAR);
env::set_var(CUSTOM_CONFIG_ENV_VAR, temp_dir.path());
env::set_var(IS_NONINTERACTIVE_ENV_VAR, "1");
let api_keys_path = api_keys_path();
let prompts_path = prompts_path();
let voice_path = voice_config_path();
// Precreate files with some content
let mut api_keys_file = fs::File::create(&api_keys_path)?;
api_keys_file.write_all(b"Some API key data")?;
let mut prompts_file = fs::File::create(&prompts_path)?;
prompts_file.write_all(b"Some prompts data")?;
let mut voice_file = fs::File::create(&voice_path)?;
voice_file.write_all(b"Some voice data")?;
let result = ensure_config_files();
// Restoring the original environment variable
match original_value {
Ok(val) => env::set_var(CUSTOM_CONFIG_ENV_VAR, val),
Err(_) => env::remove_var(CUSTOM_CONFIG_ENV_VAR),
}
result?;
// Check if files still exist
assert!(api_keys_path.exists());
assert!(prompts_path.exists());
assert!(voice_path.exists());
// Check if the contents remain unchanged
let mut api_keys_content = String::new();
fs::File::open(&api_keys_path)?.read_to_string(&mut api_keys_content)?;
assert_eq!(api_keys_content, "Some API key data".to_string());
let mut prompts_content = String::new();
fs::File::open(&prompts_path)?.read_to_string(&mut prompts_content)?;
assert_eq!(prompts_content, "Some prompts data".to_string());
let mut voice_content = String::new();
fs::File::open(&voice_path)?.read_to_string(&mut voice_content)?;
assert_eq!(voice_content, "Some voice data".to_string());
Ok(())
}
#[test]
#[serial]
fn test_ensure_config_files_serialization() -> std::io::Result<()> {
// Setup paths
let temp_dir = tempfile::TempDir::new()?;
let original_value = env::var(CUSTOM_CONFIG_ENV_VAR);
env::set_var(CUSTOM_CONFIG_ENV_VAR, temp_dir.path());
env::set_var(IS_NONINTERACTIVE_ENV_VAR, "1");
let api_keys_path = api_keys_path();
let prompts_path = prompts_path();
let voice_path = voice_config_path();
assert!(!api_keys_path.exists());
assert!(!prompts_path.exists());
assert!(!voice_path.exists());
let result = ensure_config_files();
match original_value {
Ok(val) => env::set_var(CUSTOM_CONFIG_ENV_VAR, val),
Err(_) => env::remove_var(CUSTOM_CONFIG_ENV_VAR),
}
result?;
// Read back the files and deserialize
let api_config_contents = fs::read_to_string(&api_keys_path)?;
let prompts_config_contents = fs::read_to_string(&prompts_path)?;
let voice_file_content = fs::read_to_string(&voice_path)?;
// Deserialize contents to expected data structures
// TODO: would be better to use `get_config` and `get_prompts` but
// current implementation does not allow for error management that would
// enable safe environement variable manipulation
let api_config: HashMap<String, ApiConfig> =
toml::from_str(&api_config_contents).expect("Failed to deserialize API config");
let prompt_config: HashMap<String, Prompt> =
toml::from_str(&prompts_config_contents).expect("Failed to deserialize prompts config");
let voice_config: VoiceConfig =
toml::from_str(&voice_file_content).expect("Failed to deserialize voice config");
// Check if the content matches the default values
// API
assert_eq!(
api_config.get(&Prompt::default().api.to_string()),
Some(&ApiConfig::default())
);
assert_eq!(
api_config.get(&Api::Mistral.to_string()),
Some(&ApiConfig::mistral())
);
assert_eq!(
api_config.get(&Api::Groq.to_string()),
Some(&ApiConfig::groq())
);
assert_eq!(
api_config.get(&Api::Anthropic.to_string()),
Some(&ApiConfig::anthropic())
);
// Prompts
let default_prompt = Prompt::default();
assert_eq!(prompt_config.get("default"), Some(&default_prompt));
let empty_prompt = Prompt::empty();
assert_eq!(prompt_config.get("empty"), Some(&empty_prompt));
// Voice
assert_eq!(voice_config, VoiceConfig::default());
Ok(())
}
}