From 59763b1bf4debb67d885f97906c2de2797332bbd Mon Sep 17 00:00:00 2001 From: root Date: Fri, 8 Aug 2025 20:17:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(comfyui):=20=E4=BF=AE=E5=A4=8DComfyUI=20V2?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复comfyui_v2_connect命令,使用ServiceManager正确管理连接状态 - 修复comfyui_v2_disconnect命令,通过ServiceManager断开连接 - 修复comfyui_v2_get_connection_status命令,返回真实的连接状态 - 解决'获取队列状态失败: 客户端未连接'错误 - 确保连接的管理器实例被正确保存到AppState中 问题原因: 之前的实现创建了临时的ComfyUIManager实例进行连接, 但没有将已连接的实例保存到AppState中,导致后续调用 comfyui_v2_get_queue_status时使用的是未连接的管理器。 解决方案: 通过ServiceManager统一管理ComfyUI连接状态,确保所有 命令使用同一个管理器实例,保持连接状态的一致性。 --- .../commands/comfyui_v2_commands.rs | 108 ++++++++++++++---- 1 file changed, 88 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src-tauri/src/presentation/commands/comfyui_v2_commands.rs b/apps/desktop/src-tauri/src/presentation/commands/comfyui_v2_commands.rs index 99bf7e2..f3027ed 100644 --- a/apps/desktop/src-tauri/src/presentation/commands/comfyui_v2_commands.rs +++ b/apps/desktop/src-tauri/src/presentation/commands/comfyui_v2_commands.rs @@ -180,17 +180,31 @@ pub async fn comfyui_v2_connect( format!("更新配置失败: {}", e) })?; - // 创建 ComfyUI 管理器 - let manager = ComfyUIManager::new(converted_config) + // 获取或初始化服务管理器 + let service_manager = state.get_comfyui_manager().await .map_err(|e| { - error!("创建管理器失败: {}", e); - format!("创建管理器失败: {}", e) + error!("获取服务管理器失败: {}", e); + format!("获取服务管理器失败: {}", e) + })?; + + // 通过服务管理器重新配置并连接 + service_manager.reconfigure(converted_config).await + .map_err(|e| { + error!("重新配置服务失败: {}", e); + format!("重新配置服务失败: {}", e) + })?; + + // 获取 ComfyUI 管理器并尝试连接 + let comfyui_manager = service_manager.get_comfyui_manager().await + .map_err(|e| { + error!("获取ComfyUI管理器失败: {}", e); + format!("获取ComfyUI管理器失败: {}", e) })?; // 尝试连接 - match manager.connect().await { + match comfyui_manager.connect().await { Ok(_) => { - let stats = manager.get_connection_stats().await; + let stats = comfyui_manager.get_connection_stats().await; info!("ComfyUI 连接成功"); Ok(ConnectionStatusResponse { @@ -216,10 +230,32 @@ pub async fn comfyui_v2_disconnect( ) -> Result { info!("Command: comfyui_v2_disconnect"); - // TODO: 从应用状态获取管理器实例 - // 这里需要在应用状态中维护管理器实例 - - Ok("连接已断开".to_string()) + // 获取服务管理器 + let service_manager = state.get_comfyui_manager().await + .map_err(|e| { + error!("获取服务管理器失败: {}", e); + format!("获取服务管理器失败: {}", e) + })?; + + // 获取 ComfyUI 管理器并断开连接 + match service_manager.get_comfyui_manager().await { + Ok(comfyui_manager) => { + match comfyui_manager.disconnect().await { + Ok(_) => { + info!("ComfyUI 连接已断开"); + Ok("连接已断开".to_string()) + } + Err(e) => { + error!("断开连接失败: {}", e); + Err(format!("断开连接失败: {}", e)) + } + } + } + Err(e) => { + error!("获取ComfyUI管理器失败: {}", e); + Err(format!("获取ComfyUI管理器失败: {}", e)) + } + } } /// 检查连接状态 @@ -229,16 +265,48 @@ pub async fn comfyui_v2_get_connection_status( ) -> Result { info!("Command: comfyui_v2_get_connection_status"); - // TODO: 从应用状态获取管理器实例 - // 临时返回默认状态 - Ok(ConnectionStatusResponse { - connected: false, - status: "未连接".to_string(), - base_url: "http://localhost:8188".to_string(), - last_health_check: None, - timeout_seconds: 300, - retry_attempts: 3, - }) + // 获取服务管理器 + let service_manager = match state.get_comfyui_manager().await { + Ok(manager) => manager, + Err(e) => { + error!("获取服务管理器失败: {}", e); + // 如果服务管理器未初始化,返回默认的未连接状态 + return Ok(ConnectionStatusResponse { + connected: false, + status: "未初始化".to_string(), + base_url: "http://localhost:8188".to_string(), + last_health_check: None, + timeout_seconds: 300, + retry_attempts: 3, + }); + } + }; + + // 获取 ComfyUI 管理器并检查连接状态 + match service_manager.get_comfyui_manager().await { + Ok(comfyui_manager) => { + let stats = comfyui_manager.get_connection_stats().await; + Ok(ConnectionStatusResponse { + connected: comfyui_manager.is_connected().await, + status: format!("{:?}", stats.status), + base_url: stats.base_url, + last_health_check: stats.last_health_check.map(|t| format!("{:?}", t)), + timeout_seconds: stats.timeout_seconds, + retry_attempts: stats.retry_attempts, + }) + } + Err(e) => { + error!("获取ComfyUI管理器失败: {}", e); + Ok(ConnectionStatusResponse { + connected: false, + status: "管理器未初始化".to_string(), + base_url: "http://localhost:8188".to_string(), + last_health_check: None, + timeout_seconds: 300, + retry_attempts: 3, + }) + } + } } /// 健康检查