Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

chore: enhance terminal_toolkit and add docker backend example (#3193)

Tao Sun87aedb44cfb7
diff --git a/camel/toolkits/terminal_toolkit/terminal_toolkit.py b/camel/toolkits/terminal_toolkit/terminal_toolkit.py
index df603832d..36cc788c2 100644
--- a/camel/toolkits/terminal_toolkit/terminal_toolkit.py
+++ b/camel/toolkits/terminal_toolkit/terminal_toolkit.py
@@ -15,6 +15,7 @@ import atexit
import os
import platform
import select
+import shlex
import subprocess
import sys
import threading
@@ -191,6 +192,20 @@ class TerminalToolkit(BaseToolkit):
f"Successfully attached to Docker container "
f"'{docker_container_name}'."
)
+ # Ensure the working directory exists inside the container
+ if self.docker_workdir:
+ try:
+ quoted_dir = shlex.quote(self.docker_workdir)
+ mkdir_cmd = f'sh -lc "mkdir -p -- {quoted_dir}"'
+ _init = self.docker_api_client.exec_create(
+ self.container.id, mkdir_cmd
+ )
+ self.docker_api_client.exec_start(_init['Id'])
+ except Exception as e:
+ logger.warning(
+ f"[Docker] Failed to ensure workdir "
+ f"'{self.docker_workdir}': {e}"
+ )
except NotFound:
raise RuntimeError(
f"Docker container '{docker_container_name}' not found."
diff --git a/examples/toolkits/terminal_toolkit.py b/examples/toolkits/terminal_toolkit.py
index af4c4834b..f6cfce305 100644
--- a/examples/toolkits/terminal_toolkit.py
+++ b/examples/toolkits/terminal_toolkit.py
@@ -280,3 +280,30 @@ ToolCallingRecord(tool_name='shell_exec', args={'id': 'terminal_session_1',
(.venv) enrei@192 camel %
===============================================================================
"""
+
+# ------Docker backend------
+tools = TerminalToolkit(
+ use_docker_backend=True,
+ # use your own container name
+ docker_container_name="example_container",
+ safe_mode=False,
+).get_tools()
+
+sys_msg = "You are a helpful assistant."
+
+agent = ChatAgent(system_message=sys_msg, model=model, tools=tools)
+agent.reset()
+
+usr_msg = "list all files in the current directory"
+response = agent.step(usr_msg)
+print(str(response.info['tool_calls'])[:1000])
+
+"""
+===============================================================================
+[ToolCallingRecord(tool_name='shell_exec', args={'id': 'sess1', 'command':
+'ls -la', 'block': True}, result='total 12\ndrwxr-xr-x 3 root root 4096 Sep
+23 16:48 .\ndrwxr-xr-x 1 root root 4096 Sep 23 16:47 ..\ndrwxr-xr-x 2 root
+root 4096 Sep 23 16:49 logs', tool_call_id='call_YRYlz8KozpxXE2uGkcHIUnZU',
+images=None)]
+===============================================================================
+"""