Switch to a different session: restore it from core, reload messages, update state
(
&mut self,
new_session_id: &str,
session_id: &mut String,
chat_state: &mut ChatState,
chat_view: &mut ChatView,
rt_handle: &tokio::runtime::Handle,
| 2758 | |
| 2759 | /// Switch to a different session: restore it from core, reload messages, update state |
| 2760 | fn switch_to_session( |
| 2761 | &mut self, |
| 2762 | new_session_id: &str, |
| 2763 | session_id: &mut String, |
| 2764 | chat_state: &mut ChatState, |
| 2765 | chat_view: &mut ChatView, |
| 2766 | rt_handle: &tokio::runtime::Handle, |
| 2767 | ) -> Result<()> { |
| 2768 | let agent = self.agent.clone(); |
| 2769 | let sid = new_session_id.to_string(); |
| 2770 | let agent_type = self.agent_type.clone(); |
| 2771 | let workspace = self.workspace.clone(); |
| 2772 | |
| 2773 | let (new_state, restored_agent_type) = tokio::task::block_in_place(|| { |
| 2774 | rt_handle.block_on(async { |
| 2775 | // Restore session in core |
| 2776 | agent.restore_session(&sid).await?; |
| 2777 | |
| 2778 | // Get session info for agent_type and workspace |
| 2779 | let workspace_path = agent.workspace_path_buf(); |
| 2780 | let sessions = agent |
| 2781 | .coordinator() |
| 2782 | .list_sessions(&workspace_path) |
| 2783 | .await |
| 2784 | .unwrap_or_default(); |
| 2785 | let session_summary = sessions.iter().find(|s| s.session_id == sid); |
| 2786 | let restored_agent_type = session_summary |
| 2787 | .map(|s| s.agent_type.clone()) |
| 2788 | .unwrap_or_else(|| agent_type.clone()); |
| 2789 | let session_name = session_summary |
| 2790 | .map(|s| s.session_name.clone()) |
| 2791 | .unwrap_or_else(|| "Restored Session".to_string()); |
| 2792 | |
| 2793 | // Use the current workspace filtered by the session list; fall back to the |
| 2794 | // workspace supplied when this chat view was created. |
| 2795 | let effective_workspace = workspace |
| 2796 | .clone() |
| 2797 | .or_else(|| Some(workspace_path.to_string_lossy().to_string())); |
| 2798 | |
| 2799 | // Sync global workspace path from restored session |
| 2800 | if let Some(ref ws) = effective_workspace { |
| 2801 | agent |
| 2802 | .set_workspace_path(Some(std::path::PathBuf::from(ws))) |
| 2803 | .await; |
| 2804 | } |
| 2805 | |
| 2806 | // Load historical messages from core. |
| 2807 | let messages = agent |
| 2808 | .coordinator() |
| 2809 | .get_messages(&sid) |
| 2810 | .await |
| 2811 | .unwrap_or_default(); |
| 2812 | |
| 2813 | let state = ChatState::from_core_messages( |
| 2814 | sid.clone(), |
| 2815 | session_name, |
| 2816 | restored_agent_type.clone(), |
| 2817 | effective_workspace, |
no test coverage detected