feat: 配置页增加测试连接按钮,发送简单文本验证API是否可用
This commit is contained in:
@@ -26,6 +26,10 @@ pub struct FlomoAiApp {
|
||||
new_prompt_title: String,
|
||||
new_prompt_content: String,
|
||||
|
||||
test_status: String,
|
||||
test_is_loading: bool,
|
||||
pending_test: Option<std::thread::JoinHandle<Result<String, String>>>,
|
||||
|
||||
current_page: Page,
|
||||
theme_dirty: bool,
|
||||
}
|
||||
@@ -54,6 +58,10 @@ impl FlomoAiApp {
|
||||
new_prompt_title: String::new(),
|
||||
new_prompt_content: String::new(),
|
||||
|
||||
test_status: "点击测试连接".to_string(),
|
||||
test_is_loading: false,
|
||||
pending_test: None,
|
||||
|
||||
current_page: Page::Main,
|
||||
theme_dirty: false,
|
||||
}
|
||||
@@ -84,6 +92,28 @@ impl FlomoAiApp {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(handle) = self.pending_test.take() {
|
||||
if handle.is_finished() {
|
||||
match handle.join() {
|
||||
Ok(Ok(text)) => {
|
||||
self.test_status = format!("连接成功: {}", text.chars().take(30).collect::<String>());
|
||||
self.test_is_loading = false;
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
self.test_status = format!("连接失败: {}", e);
|
||||
self.test_is_loading = false;
|
||||
}
|
||||
Err(_) => {
|
||||
self.test_status = "测试线程错误".to_string();
|
||||
self.test_is_loading = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.pending_test = Some(handle);
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn send_request(&mut self, ctx: &egui::Context) {
|
||||
@@ -144,6 +174,38 @@ impl FlomoAiApp {
|
||||
self.settings.theme_config.mode = self.settings_selected_theme;
|
||||
let _ = save_settings(&self.settings);
|
||||
}
|
||||
|
||||
fn test_connection(&mut self, ctx: &egui::Context) {
|
||||
if self.test_is_loading {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.settings_base_url.is_empty() {
|
||||
self.test_status = "错误: Base URL 不能为空".to_string();
|
||||
return;
|
||||
}
|
||||
|
||||
self.test_status = "测试中...".to_string();
|
||||
self.test_is_loading = true;
|
||||
|
||||
let settings = self.settings.clone();
|
||||
let ctx_clone = ctx.clone();
|
||||
|
||||
let handle = std::thread::spawn(move || {
|
||||
let mut test_settings = settings.clone();
|
||||
test_settings.llm_config.model = if settings.llm_config.model.is_empty() {
|
||||
"gpt-4o".to_string()
|
||||
} else {
|
||||
settings.llm_config.model.clone()
|
||||
};
|
||||
|
||||
let result = crate::api::call_llm(&test_settings, "你好,请回复OK".to_string(), None);
|
||||
ctx_clone.request_repaint();
|
||||
result
|
||||
});
|
||||
|
||||
self.pending_test = Some(handle);
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for FlomoAiApp {
|
||||
@@ -415,6 +477,31 @@ impl FlomoAiApp {
|
||||
ui.text_edit_singleline(&mut self.settings_model);
|
||||
ui.add_space(12.0);
|
||||
|
||||
// Test connection button
|
||||
ui.horizontal(|ui| {
|
||||
let btn_text = if self.test_is_loading { "测试中..." } else { "测试连接" };
|
||||
let btn = egui::Button::new(btn_text)
|
||||
.fill(egui::Color32::from_rgb(100, 100, 255))
|
||||
.rounding(4.0)
|
||||
.min_size(egui::vec2(80.0, 32.0));
|
||||
|
||||
if ui.add(btn).clicked() && !self.test_is_loading {
|
||||
self.test_connection(ctx);
|
||||
}
|
||||
|
||||
let status_color = if self.test_is_loading {
|
||||
egui::Color32::from_rgb(255, 165, 0)
|
||||
} else if self.test_status.starts_with("连接成功") {
|
||||
egui::Color32::from_rgb(0, 180, 0)
|
||||
} else if self.test_status.starts_with("连接失败") || self.test_status.starts_with("错误") {
|
||||
egui::Color32::RED
|
||||
} else {
|
||||
egui::Color32::GRAY
|
||||
};
|
||||
|
||||
ui.label(egui::RichText::new(&self.test_status).size(11.0).color(status_color));
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label(egui::RichText::new("主题").size(14.0).strong());
|
||||
|
||||
Reference in New Issue
Block a user