redact the chat settings API key

This commit is contained in:
Clément Renault 2025-05-21 21:18:18 +02:00 committed by Kerollmops
parent 1eb8249a51
commit 7bfb7b7d58
No known key found for this signature in database
GPG Key ID: F250A4C4E3AE5F5F

View File

@ -24,10 +24,11 @@ async fn get_settings(
Data<IndexScheduler>,
>,
) -> Result<HttpResponse, ResponseError> {
let settings = match index_scheduler.chat_settings()? {
let mut settings = match index_scheduler.chat_settings()? {
Some(value) => serde_json::from_value(value).unwrap(),
None => GlobalChatSettings::default(),
};
settings.hide_secrets();
Ok(HttpResponse::Ok().json(settings))
}
@ -80,6 +81,33 @@ pub struct GlobalChatSettings {
pub prompts: Setting<ChatPrompts>,
}
impl GlobalChatSettings {
pub fn hide_secrets(&mut self) {
match &mut self.api_key {
Setting::Set(key) => Self::hide_secret(key),
Setting::Reset => (),
Setting::NotSet => (),
}
}
fn hide_secret(secret: &mut String) {
match secret.len() {
x if x < 10 => {
secret.replace_range(.., "XXX...");
}
x if x < 20 => {
secret.replace_range(2.., "XXXX...");
}
x if x < 30 => {
secret.replace_range(3.., "XXXXX...");
}
_x => {
secret.replace_range(5.., "XXXXXX...");
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct ChatPrompts {