๐ซ CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: terminal_human_in_the_loop (#2751)
Tao Sun84b44a678590
diff --git a/camel/toolkits/terminal_toolkit.py b/camel/toolkits/terminal_toolkit.pyindex d8d9254e9..c18b5d722 100644--- a/camel/toolkits/terminal_toolkit.py+++ b/camel/toolkits/terminal_toolkit.py@@ -84,6 +84,7 @@ class TerminalToolkit(BaseToolkit):self._file_initialized = Falseself.cloned_env_path = Noneself.use_shell_mode = use_shell_mode+ self._human_takeover_active = Falseself.python_executable = sys.executableself.is_macos = platform.system() == 'Darwin'@@ -705,59 +706,35 @@ class TerminalToolkit(BaseToolkit):elif command.startswith('pip'):command = command.replace('pip', pip_path, 1)- if self.is_macos:- # Type safe version - macOS uses subprocess.run- process = subprocess.run(- command,- shell=True,- cwd=self.working_dir,- capture_output=True,- text=True,- env=os.environ.copy(),- )-- # Process the output- output = process.stdout or ""- if process.stderr:- output += f"\nStderr Output:\n{process.stderr}"-- # Update session information and terminal- self.shell_sessions[id]["output"] = output- self._update_terminal_output(output + "\n")-- return output-- else:- # Non-macOS systems use the Popen method- proc = subprocess.Popen(- command,- shell=True,- cwd=self.working_dir,- stdout=subprocess.PIPE,- stderr=subprocess.PIPE,- stdin=subprocess.PIPE,- text=True,- bufsize=1,- universal_newlines=True,- env=os.environ.copy(),- )+ proc = subprocess.Popen(+ command,+ shell=True,+ cwd=self.working_dir,+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ stdin=subprocess.PIPE,+ text=True,+ bufsize=1,+ universal_newlines=True,+ env=os.environ.copy(),+ )- # Store the process and mark it as running- self.shell_sessions[id]["process"] = proc- self.shell_sessions[id]["running"] = True+ # Store the process and mark it as running+ self.shell_sessions[id]["process"] = proc+ self.shell_sessions[id]["running"] = True- # Get output- stdout, stderr = proc.communicate()+ # Get output+ stdout, stderr = proc.communicate()- output = stdout or ""- if stderr:- output += f"\nStderr Output:\n{stderr}"+ output = stdout or ""+ if stderr:+ output += f"\nStderr Output:\n{stderr}"- # Update session information and terminal- self.shell_sessions[id]["output"] = output- self._update_terminal_output(output + "\n")+ # Update session information and terminal+ self.shell_sessions[id]["output"] = output+ self._update_terminal_output(output + "\n")- return output+ return outputexcept Exception as e:error_msg = f"Command execution error: {e!s}"@@ -961,6 +938,169 @@ class TerminalToolkit(BaseToolkit):logger.error(f"Error killing process: {e}")return f"Error killing process: {e!s}"+ def ask_user_for_help(self, id: str) -> str:+ r"""Pauses agent execution to ask a human for help in the terminal.++ This function should be called when an agent is stuck or needs+ assistance with a task that requires manual intervention (e.g.,+ solving a CAPTCHA or complex debugging). The human will take over the+ specified terminal session to execute commands and then return control+ to the agent.++ Args:+ id (str): Identifier of the shell session for the human to+ interact with. If the session does not yet exist, it will be+ created automatically.++ Returns:+ str: A status message indicating that the human has finished,+ including the number of commands executed.+ """+ # Input validation+ if not id or not isinstance(id, str):+ return "Error: Invalid session ID provided"++ # Prevent concurrent human takeovers+ if (+ hasattr(self, '_human_takeover_active')+ and self._human_takeover_active+ ):+ return "Error: Human takeover already in progress"++ try:+ self._human_takeover_active = True++ # Ensure the session exists so that the human can reuse it+ if id not in self.shell_sessions:+ self.shell_sessions[id] = {+ "process": None,+ "output": "",+ "running": False,+ }++ command_count = 0+ error_occurred = False++ # Create clear banner message for user+ takeover_banner = (+ f"\n{'='*60}\n"+ f"๐ค CAMEL Agent needs human help! Session: {id}\n"+ f"๐ Working directory: {self.working_dir}\n"+ f"{'='*60}\n"+ f"๐ก Type commands or '/exit' to return control to agent.\n"+ f"{'='*60}\n"+ )++ # Print once to console for immediate visibility+ print(takeover_banner, flush=True)+ # Log for terminal output tracking+ self._update_terminal_output(takeover_banner)++ # Helper flag + event for coordination+ done_event = threading.Event()++ def _human_loop() -> None:+ r"""Blocking loop that forwards human input to shell_exec."""+ nonlocal command_count, error_occurred+ try:+ while True:+ try:+ # Clear, descriptive prompt for user input+ user_cmd = input(f"๐งโ๐ป [{id}]> ")+ if (+ user_cmd.strip()+ ): # Only count non-empty commands+ command_count += 1+ except EOFError:+ # e.g. Ctrl_D / stdin closed, treat as exit.+ break+ except (KeyboardInterrupt, Exception) as e:+ logger.warning(+ f"Input error during human takeover: {e}"+ )+ error_occurred = True+ break++ if user_cmd.strip() in {"/exit", "exit", "quit"}:+ break++ try:+ exec_result = self.shell_exec(id, user_cmd)+ # Show the result immediately to the user+ if exec_result.strip():+ print(exec_result)+ logger.info(+ f"Human command executed: {user_cmd[:50]}..."+ )+ # Auto-exit after successful command+ break+ except Exception as e:+ error_msg = f"Error executing command: {e}"+ logger.error(f"Error executing human command: {e}")+ print(error_msg) # Show error to user immediately+ self._update_terminal_output(f"{error_msg}\n")+ error_occurred = True++ except Exception as e:+ logger.error(f"Unexpected error in human loop: {e}")+ error_occurred = True+ finally:+ # Notify completion clearly+ finish_msg = (+ f"\n{'='*60}\n"+ f"โ Human assistance completed! "+ f"Commands: {command_count}\n"+ f"๐ค Returning control to CAMEL agent...\n"+ f"{'='*60}\n"+ )+ print(finish_msg, flush=True)+ self._update_terminal_output(finish_msg)+ done_event.set()++ # Start interactive thread (non-daemon for proper cleanup)+ thread = threading.Thread(target=_human_loop, daemon=False)+ thread.start()++ # Block until human signals completion with timeout+ if done_event.wait(timeout=600): # 10 minutes timeout+ thread.join(timeout=10) # Give thread time to cleanup++ # Generate detailed status message+ status = "completed successfully"+ if error_occurred:+ status = "completed with some errors"++ result_msg = (+ f"Human assistance {status} for session '{id}'. "+ f"Total commands executed: {command_count}. "+ f"Working directory: {self.working_dir}"+ )+ logger.info(result_msg)+ return result_msg+ else:+ timeout_msg = (+ f"Human takeover for session '{id}' timed out after 10 "+ "minutes"+ )+ logger.warning(timeout_msg)+ return timeout_msg++ except Exception as e:+ error_msg = f"Error during human takeover for session '{id}': {e}"+ logger.error(error_msg)+ # Notify user of the error clearly+ error_banner = (+ f"\n{'='*60}\n"+ f"โ Error in human takeover! Session: {id}\n"+ f"โ {e}\n"+ f"{'='*60}\n"+ )+ print(error_banner, flush=True)+ return error_msg+ finally:+ # Always reset the flag+ self._human_takeover_active = False+def __del__(self):r"""Clean up resources when the object is being destroyed.Terminates all running processes and closes any open file handles.@@ -1042,4 +1182,5 @@ class TerminalToolkit(BaseToolkit):FunctionTool(self.shell_wait),FunctionTool(self.shell_write_to_process),FunctionTool(self.shell_kill_process),+ FunctionTool(self.ask_user_for_help),]diff --git a/examples/toolkits/terminal_toolkit.py b/examples/toolkits/terminal_toolkit.pyindex 18a14390c..fb5ed051f 100644--- a/examples/toolkits/terminal_toolkit.py+++ b/examples/toolkits/terminal_toolkit.py@@ -212,3 +212,71 @@ usr_msg = "help me use uv pip install pptx, and create a ppt, and show me the"camel_agent.reset()response = camel_agent.step(usr_msg)print(str(response.info['tool_calls']))++usr_msg = (+ "Use the terminal tool to create a Python file named 'python_file', "+ "but first, you need to request user help using call_user_for_help. "+ "The user will assist you with some configurations. "+ "Then, create the Python file, and finally list the contents of the "+ "current folder."+)++# Get response information for checking the processes+camel_agent.reset()+response = camel_agent.step(usr_msg)+print(str(response.info['tool_calls']))+"""+===============================================================================+[ToolCallingRecord(tool_name='shell_exec', args={'id': '1', 'command': 'pip+install python-pptx'}, result='\nStderr Output:\n/Users/enrei/Desktop/+camel0605/camel/.venv/bin/python3: No module named pip\n',+tool_call_id='call_ogvH8cKGGBMlulMV6IOCyN4q')]++============================================================+๐ค CAMEL Agent needs human help! Session: terminal_session_1+๐ Working directory: /Users/enrei/Desktop/camel0605/camel/workspace+============================================================+๐ก Type commands or '/exit' to return control to agent.+============================================================++๐งโ๐ป [terminal_session_1]> uv pip install python-pptx++Stderr Output:+Using Python 3.10.16 environment at: /Users/enrei/Desktop/camel0605/camel/.venv+Audited 1 package in 16ms+++============================================================+โ Human assistance completed! Commands: 1+๐ค Returning control to CAMEL agent...+============================================================++[ToolCallingRecord(tool_name='ask_user_for_help', args={'id':+'terminal_session_1'}, result="Human assistance completed successfully for+session 'terminal_session_1'. Total commands executed: 1. Working directory: /+Users/enrei/Desktop/camel0605/camel/workspace",+tool_call_id='call_eYtpU38YncR6PnfdlBqouSoV'), ToolCallingRecord+(tool_name='file_find_by_name', args={'path': '/Users/enrei/Desktop/camel0605/+camel/workspace', 'glob': 'python_file'}, result='',+tool_call_id='call_pyuYHoNvpPvXFbMjfo9DMUNe'), ToolCallingRecord+(tool_name='file_find_by_name', args={'path': '/Users/enrei/Desktop/camel0605/+camel/workspace', 'glob': '*'}, result='/Users/enrei/Desktop/camel0605/camel/+workspace\n/Users/enrei/Desktop/camel0605/camel/workspace/.venv\n/Users/enrei/+Desktop/camel0605/camel/workspace/.venv/bin\n/Users/enrei/Desktop/camel0605/+camel/workspace/.venv/bin/python3\n/Users/enrei/Desktop/camel0605/camel/+workspace/.venv/bin/python\n/Users/enrei/Desktop/camel0605/camel/workspace/.+venv/bin/python3.12\n/Users/enrei/Desktop/camel0605/camel/workspace/.venv/+include\n/Users/enrei/Desktop/camel0605/camel/workspace/.venv/include/python3.+12\n/Users/enrei/Desktop/camel0605/camel/workspace/.venv/pyvenv.cfg\n/Users/+enrei/Desktop/camel0605/camel/workspace/.venv/lib\n/Users/enrei/Desktop/+camel0605/camel/workspace/.venv/lib/python3.12\n/Users/enrei/Desktop/camel0605/+camel/workspace/.venv/lib/python3.12/site-packages',+tool_call_id='call_WwB219FGm4tAssjFA9UsIrRT'), ToolCallingRecord+(tool_name='shell_exec', args={'id': 'terminal_session_1', 'command': 'touch+python_file.py'}, result='', tool_call_id='call_OBhkDr5i2mzBRpty0JiSF8Dj'),+ToolCallingRecord(tool_name='shell_exec', args={'id': 'terminal_session_1',+'command': 'ls -l'}, result='total 0\n-rw-r--r-- 1 enrei staff 0 Jun 27+17:26 python_file.py\n', tool_call_id='call_fllEujXWutTJmqpSWpHOOnIU')]+(.venv) enrei@192 camel %+===============================================================================+"""