@@ -554,6 +554,14 @@ def handle_output_message(self, msg):
554554
555555 footer = self .query_one (MainFooter )
556556 footer .update_mode (msg .get ("mode" , "code" ))
557+ elif msg_type == "switch_agent" :
558+ target_uuid = msg ["uuid" ]
559+ # Ensure the target container exists before switching
560+ primary_uuid = str (self .worker .coder .uuid )
561+ if target_uuid != primary_uuid and target_uuid not in self ._sub_agent_containers :
562+ self .show_error ("Agent container not found. Cannot switch." )
563+ else :
564+ self ._switch_to_container (target_uuid )
557565
558566 def add_output (self , text , task_id = None ):
559567 """Add output to the output container."""
@@ -678,6 +686,8 @@ def on_input_area_text_changed(self, message: InputArea.TextChanged):
678686
679687 def on_input_area_submit (self , message : InputArea .Submit ):
680688 """Handle input submission."""
689+ from cecli .helpers .agents .service import AgentService
690+
681691 user_input = message .value
682692
683693 if not user_input .strip ():
@@ -703,6 +713,63 @@ def on_input_area_submit(self, message: InputArea.Submit):
703713 self ._open_editor_suspended (initial_content )
704714 return
705715
716+ # Intercept /switch-agent command to handle immediately without LLM processing
717+ if stripped .startswith ("/switch-agent" ):
718+ parts = stripped .split (maxsplit = 1 )
719+ agent_name = parts [1 ].strip () if len (parts ) > 1 else ""
720+
721+ input_area = self .query_one ("#input" , InputArea )
722+ input_area .value = ""
723+
724+ if not agent_name :
725+ self .show_error ("Usage: /switch-agent <agent-name>" )
726+ return
727+
728+ # Resolve agent name to UUID
729+ agent_service = AgentService .get_instance (self .worker .coder )
730+ primary_uuid = str (self .worker .coder .uuid )
731+
732+ target_uuid = None
733+ if agent_name == "primary" :
734+ target_uuid = primary_uuid
735+ else :
736+ # Try parsing "name (uuid)" format
737+ if agent_name .endswith (")" ) and " (" in agent_name :
738+ try :
739+ # Extract uuid prefix from "name (prefix)"
740+ uuid_prefix = agent_name .rsplit (" (" , 1 )[1 ][:- 1 ]
741+ for uuid , info in agent_service .sub_agents .items ():
742+ if uuid .startswith (uuid_prefix ):
743+ target_uuid = uuid
744+ break
745+ except IndexError :
746+ pass # Not the format we expected
747+
748+ # If not found via "name (uuid)", try matching by name directly
749+ if target_uuid is None :
750+ for uuid , info in agent_service .sub_agents .items ():
751+ if info .name == agent_name :
752+ target_uuid = uuid
753+ break
754+
755+ # If still not found, try matching by uuid prefix directly
756+ if target_uuid is None :
757+ for uuid , info in agent_service .sub_agents .items ():
758+ if uuid .startswith (agent_name ):
759+ target_uuid = uuid
760+ break
761+
762+ if target_uuid is None :
763+ self .show_error (f"Agent '{ agent_name } ' not found." )
764+ return
765+
766+ if target_uuid != primary_uuid and target_uuid not in self ._sub_agent_containers :
767+ self .show_error (f"Agent container for '{ agent_name } ' not found." )
768+ return
769+
770+ self ._switch_to_container (target_uuid )
771+ return
772+
706773 # Save to history before clearing
707774 input_area = self .query_one ("#input" , InputArea )
708775 input_area .save_to_history (user_input )
@@ -976,6 +1043,12 @@ def _switch_to_container(self, uuid: str) -> None:
9761043 agent_service = AgentService .get_instance (self .worker .coder )
9771044 primary_uuid = str (self .worker .coder .uuid )
9781045
1046+ # Check if the target container exists
1047+ if uuid != primary_uuid and uuid not in self ._sub_agent_containers :
1048+ # Sub-agent container not found, fall back to primary
1049+ self .show_error (f"Agent container for UUID { uuid } not found. Switching to primary." )
1050+ uuid = primary_uuid
1051+
9791052 if uuid == primary_uuid :
9801053 # Switch to primary agent
9811054 agent_service .foreground_uuid = None
0 commit comments