MCPcopy Create free account
hub / github.com/GCWing/BitFun / switch_to_session

Method switch_to_session

src/apps/cli/src/modes/chat.rs:2760–2839  ·  view source on GitHub ↗

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,
  

Source from the content-addressed store, hash-verified

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,

Callers 1

apply_exit_reasonMethod · 0.80

Calls 11

iterMethod · 0.80
set_workspace_pathMethod · 0.80
scroll_to_bottomMethod · 0.80
cloneMethod · 0.45
restore_sessionMethod · 0.45
workspace_path_bufMethod · 0.45
list_sessionsMethod · 0.45
coordinatorMethod · 0.45
get_messagesMethod · 0.45
set_statusMethod · 0.45

Tested by

no test coverage detected