camel-ai 0.2.76a4__py3-none-any.whl → 0.2.76a5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

Files changed (35) hide show
  1. camel/__init__.py +1 -1
  2. camel/agents/chat_agent.py +276 -21
  3. camel/configs/__init__.py +3 -0
  4. camel/configs/cometapi_config.py +104 -0
  5. camel/interpreters/docker/Dockerfile +3 -12
  6. camel/memories/blocks/chat_history_block.py +4 -1
  7. camel/memories/records.py +52 -8
  8. camel/messages/base.py +1 -1
  9. camel/models/__init__.py +2 -0
  10. camel/models/cometapi_model.py +83 -0
  11. camel/models/model_factory.py +2 -0
  12. camel/retrievers/auto_retriever.py +1 -0
  13. camel/societies/workforce/workforce.py +9 -7
  14. camel/storages/key_value_storages/json.py +15 -2
  15. camel/storages/vectordb_storages/tidb.py +8 -6
  16. camel/toolkits/__init__.py +4 -0
  17. camel/toolkits/dingtalk.py +1135 -0
  18. camel/toolkits/edgeone_pages_mcp_toolkit.py +11 -31
  19. camel/toolkits/google_drive_mcp_toolkit.py +12 -31
  20. camel/toolkits/message_integration.py +3 -0
  21. camel/toolkits/notion_mcp_toolkit.py +16 -26
  22. camel/toolkits/origene_mcp_toolkit.py +8 -49
  23. camel/toolkits/playwright_mcp_toolkit.py +12 -31
  24. camel/toolkits/resend_toolkit.py +168 -0
  25. camel/toolkits/terminal_toolkit/__init__.py +18 -0
  26. camel/toolkits/terminal_toolkit/terminal_toolkit.py +909 -0
  27. camel/toolkits/terminal_toolkit/utils.py +580 -0
  28. camel/types/enums.py +109 -0
  29. camel/types/unified_model_type.py +5 -0
  30. camel/utils/commons.py +2 -0
  31. {camel_ai-0.2.76a4.dist-info → camel_ai-0.2.76a5.dist-info}/METADATA +25 -6
  32. {camel_ai-0.2.76a4.dist-info → camel_ai-0.2.76a5.dist-info}/RECORD +34 -28
  33. camel/toolkits/terminal_toolkit.py +0 -1798
  34. {camel_ai-0.2.76a4.dist-info → camel_ai-0.2.76a5.dist-info}/WHEEL +0 -0
  35. {camel_ai-0.2.76a4.dist-info → camel_ai-0.2.76a5.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,909 @@
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
+ import atexit
15
+ import os
16
+ import platform
17
+ import select
18
+ import subprocess
19
+ import sys
20
+ import threading
21
+ import time
22
+ from queue import Empty, Queue
23
+ from typing import Any, Dict, List, Optional
24
+
25
+ from camel.logger import get_logger
26
+ from camel.toolkits.base import BaseToolkit
27
+ from camel.toolkits.function_tool import FunctionTool
28
+ from camel.toolkits.terminal_toolkit.utils import (
29
+ check_nodejs_availability,
30
+ clone_current_environment,
31
+ ensure_uv_available,
32
+ sanitize_command,
33
+ setup_initial_env_with_uv,
34
+ setup_initial_env_with_venv,
35
+ )
36
+ from camel.utils import MCPServer
37
+
38
+ logger = get_logger(__name__)
39
+
40
+ # Try to import docker, but don't make it a hard requirement
41
+ try:
42
+ import docker
43
+ from docker.errors import APIError, NotFound
44
+ from docker.models.containers import Container
45
+ except ImportError:
46
+ docker = None
47
+ NotFound = None
48
+ APIError = None
49
+ Container = None
50
+
51
+
52
+ def _to_plain(text: str) -> str:
53
+ r"""Convert ANSI text to plain text using rich if available."""
54
+ try:
55
+ from rich.text import Text as _RichText
56
+
57
+ return _RichText.from_ansi(text).plain
58
+ except Exception:
59
+ return text
60
+
61
+
62
+ @MCPServer()
63
+ class TerminalToolkit(BaseToolkit):
64
+ r"""A toolkit for LLM agents to execute and interact with terminal commands
65
+ in either a local or a sandboxed Docker environment.
66
+
67
+ Args:
68
+ timeout (Optional[float]): The default timeout in seconds for blocking
69
+ commands. Defaults to 20.0.
70
+ working_directory (Optional[str]): The base directory for operations.
71
+ For the local backend, this acts as a security sandbox.
72
+ For the Docker backend, this sets the working directory inside
73
+ the container.
74
+ If not specified, defaults to "./workspace" for local and
75
+ "/workspace" for Docker.
76
+ use_docker_backend (bool): If True, all commands are executed in a
77
+ Docker container. Defaults to False.
78
+ docker_container_name (Optional[str]): The name of the Docker
79
+ container to use. Required if use_docker_backend is True.
80
+ session_logs_dir (Optional[str]): The directory to store session
81
+ logs. Defaults to a 'terminal_logs' subfolder in the
82
+ working directory.
83
+ safe_mode (bool): Whether to apply security checks to commands.
84
+ Defaults to True.
85
+ allowed_commands (Optional[List[str]]): List of allowed commands
86
+ when safe_mode is True. If None, uses default safety rules.
87
+ clone_current_env (bool): Whether to clone the current Python
88
+ environment for local execution. Defaults to False.
89
+ """
90
+
91
+ def __init__(
92
+ self,
93
+ timeout: Optional[float] = 20.0,
94
+ working_directory: Optional[str] = None,
95
+ use_docker_backend: bool = False,
96
+ docker_container_name: Optional[str] = None,
97
+ session_logs_dir: Optional[str] = None,
98
+ safe_mode: bool = True,
99
+ allowed_commands: Optional[List[str]] = None,
100
+ clone_current_env: bool = False,
101
+ ):
102
+ self.use_docker_backend = use_docker_backend
103
+ self.timeout = timeout
104
+ self.shell_sessions: Dict[str, Dict[str, Any]] = {}
105
+ # Thread-safe guard for concurrent access to
106
+ # shell_sessions and session state
107
+ self._session_lock = threading.RLock()
108
+
109
+ # Initialize docker_workdir with proper type
110
+ self.docker_workdir: Optional[str] = None
111
+
112
+ if self.use_docker_backend:
113
+ # For Docker backend, working_directory is path inside container
114
+ if working_directory:
115
+ self.docker_workdir = working_directory
116
+ else:
117
+ self.docker_workdir = "/workspace"
118
+ # For logs and local file operations, use a local workspace
119
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
120
+ if camel_workdir:
121
+ self.working_dir = os.path.abspath(camel_workdir)
122
+ else:
123
+ self.working_dir = os.path.abspath("./workspace")
124
+ else:
125
+ # For local backend, working_directory is the local path
126
+ if working_directory:
127
+ self.working_dir = os.path.abspath(working_directory)
128
+ else:
129
+ camel_workdir = os.environ.get("CAMEL_WORKDIR")
130
+ if camel_workdir:
131
+ self.working_dir = os.path.abspath(camel_workdir)
132
+ else:
133
+ self.working_dir = os.path.abspath("./workspace")
134
+
135
+ # Only create local directory for logs and local backend operations
136
+ if not os.path.exists(self.working_dir):
137
+ os.makedirs(self.working_dir, exist_ok=True)
138
+ self.safe_mode = safe_mode
139
+
140
+ # Initialize whitelist of allowed commands if provided
141
+ self.allowed_commands = (
142
+ set(allowed_commands) if allowed_commands else None
143
+ )
144
+
145
+ # Environment management attributes
146
+ self.clone_current_env = clone_current_env
147
+ self.cloned_env_path: Optional[str] = None
148
+ self.initial_env_path: Optional[str] = None
149
+ self.python_executable = sys.executable
150
+
151
+ atexit.register(self.__del__)
152
+
153
+ self.log_dir = os.path.abspath(
154
+ session_logs_dir or os.path.join(self.working_dir, "terminal_logs")
155
+ )
156
+ self.blocking_log_file = os.path.join(
157
+ self.log_dir, "blocking_commands.log"
158
+ )
159
+ self.os_type = platform.system()
160
+
161
+ os.makedirs(self.log_dir, exist_ok=True)
162
+
163
+ # Clean the file in terminal_logs folder
164
+ for file in os.listdir(self.log_dir):
165
+ if file.endswith(".log"):
166
+ os.remove(os.path.join(self.log_dir, file))
167
+
168
+ if self.use_docker_backend:
169
+ if docker is None:
170
+ raise ImportError(
171
+ "The 'docker' library is required to use the "
172
+ "Docker backend. Please install it with "
173
+ "'pip install docker'."
174
+ )
175
+ if not docker_container_name:
176
+ raise ValueError(
177
+ "docker_container_name must be "
178
+ "provided when using Docker backend."
179
+ )
180
+ try:
181
+ # APIClient is used for operations that need a timeout,
182
+ # like exec_start
183
+ self.docker_api_client = docker.APIClient(
184
+ base_url='unix://var/run/docker.sock', timeout=self.timeout
185
+ )
186
+ self.docker_client = docker.from_env()
187
+ self.container = self.docker_client.containers.get(
188
+ docker_container_name
189
+ )
190
+ logger.info(
191
+ f"Successfully attached to Docker container "
192
+ f"'{docker_container_name}'."
193
+ )
194
+ except NotFound:
195
+ raise RuntimeError(
196
+ f"Docker container '{docker_container_name}' not found."
197
+ )
198
+ except APIError as e:
199
+ raise RuntimeError(f"Failed to connect to Docker daemon: {e}")
200
+
201
+ # Set up environments (only for local backend)
202
+ if not self.use_docker_backend:
203
+ if self.clone_current_env:
204
+ self._setup_cloned_environment()
205
+ else:
206
+ # Default: set up initial environment with Python 3.10
207
+ self._setup_initial_environment()
208
+ elif self.clone_current_env:
209
+ logger.info(
210
+ "[ENV CLONE] Skipping environment setup for Docker backend "
211
+ "- container is already isolated"
212
+ )
213
+
214
+ def _setup_cloned_environment(self):
215
+ r"""Set up a cloned Python environment."""
216
+ self.cloned_env_path = os.path.join(self.working_dir, ".venv")
217
+
218
+ def update_callback(msg: str):
219
+ logger.info(f"[ENV CLONE] {msg.strip()}")
220
+
221
+ success = clone_current_environment(
222
+ self.cloned_env_path, self.working_dir, update_callback
223
+ )
224
+
225
+ if success:
226
+ # Update python executable to use the cloned environment
227
+ if self.os_type == 'Windows':
228
+ self.python_executable = os.path.join(
229
+ self.cloned_env_path, "Scripts", "python.exe"
230
+ )
231
+ else:
232
+ self.python_executable = os.path.join(
233
+ self.cloned_env_path, "bin", "python"
234
+ )
235
+ else:
236
+ logger.info(
237
+ "[ENV CLONE] Failed to create cloned environment, "
238
+ "using system Python"
239
+ )
240
+
241
+ def _setup_initial_environment(self):
242
+ r"""Set up an initial environment with Python 3.10."""
243
+ self.initial_env_path = os.path.join(self.working_dir, ".initial_env")
244
+
245
+ def update_callback(msg: str):
246
+ logger.info(f"[ENV INIT] {msg.strip()}")
247
+
248
+ # Try to ensure uv is available first
249
+ success, uv_path = ensure_uv_available(update_callback)
250
+
251
+ if success and uv_path:
252
+ success = setup_initial_env_with_uv(
253
+ self.initial_env_path,
254
+ uv_path,
255
+ self.working_dir,
256
+ update_callback,
257
+ )
258
+ else:
259
+ update_callback(
260
+ "Falling back to standard venv for environment setup\n"
261
+ )
262
+ success = setup_initial_env_with_venv(
263
+ self.initial_env_path, self.working_dir, update_callback
264
+ )
265
+
266
+ if success:
267
+ # Update python executable to use the initial environment
268
+ if self.os_type == 'Windows':
269
+ self.python_executable = os.path.join(
270
+ self.initial_env_path, "Scripts", "python.exe"
271
+ )
272
+ else:
273
+ self.python_executable = os.path.join(
274
+ self.initial_env_path, "bin", "python"
275
+ )
276
+
277
+ # Check Node.js availability
278
+ check_nodejs_availability(update_callback)
279
+ else:
280
+ logger.info(
281
+ "[ENV INIT] Failed to create initial environment, "
282
+ "using system Python"
283
+ )
284
+
285
+ def _adapt_command_for_environment(self, command: str) -> str:
286
+ r"""Adapt command to use virtual environment if available."""
287
+ # Only adapt for local backend
288
+ if self.use_docker_backend:
289
+ return command
290
+
291
+ # Check if we have any virtual environment (cloned or initial)
292
+ env_path = None
293
+ if self.cloned_env_path and os.path.exists(self.cloned_env_path):
294
+ env_path = self.cloned_env_path
295
+ elif self.initial_env_path and os.path.exists(self.initial_env_path):
296
+ env_path = self.initial_env_path
297
+
298
+ if not env_path:
299
+ return command
300
+
301
+ # Check if command starts with python or pip
302
+ command_lower = command.strip().lower()
303
+ if command_lower.startswith('python'):
304
+ # Replace 'python' with the virtual environment python
305
+ return command.replace('python', f'"{self.python_executable}"', 1)
306
+ elif command_lower.startswith('pip'):
307
+ # Replace 'pip' with python -m pip from virtual environment
308
+ return command.replace(
309
+ 'pip', f'"{self.python_executable}" -m pip', 1
310
+ )
311
+
312
+ return command
313
+
314
+ def _write_to_log(self, log_file: str, content: str) -> None:
315
+ r"""Write content to log file with optional ANSI stripping.
316
+
317
+ Args:
318
+ log_file (str): Path to the log file
319
+ content (str): Content to write
320
+ """
321
+ # Convert ANSI escape sequences to plain text
322
+ with open(log_file, "a", encoding="utf-8") as f:
323
+ f.write(_to_plain(content) + "\n")
324
+
325
+ def _sanitize_command(self, command: str) -> tuple[bool, str]:
326
+ r"""A comprehensive command sanitizer for both local and
327
+ Docker backends."""
328
+ return sanitize_command(
329
+ command=command,
330
+ use_docker_backend=self.use_docker_backend,
331
+ safe_mode=self.safe_mode,
332
+ working_dir=self.working_dir,
333
+ allowed_commands=self.allowed_commands,
334
+ )
335
+
336
+ def _start_output_reader_thread(self, session_id: str):
337
+ r"""Starts a thread to read stdout from a non-blocking process."""
338
+ with self._session_lock:
339
+ session = self.shell_sessions[session_id]
340
+
341
+ def reader():
342
+ try:
343
+ if session["backend"] == "local":
344
+ # For local processes, read line by line from stdout
345
+ try:
346
+ for line in iter(
347
+ session["process"].stdout.readline, ''
348
+ ):
349
+ session["output_stream"].put(line)
350
+ self._write_to_log(session["log_file"], line)
351
+ finally:
352
+ session["process"].stdout.close()
353
+ elif session["backend"] == "docker":
354
+ # For Docker, read from the raw socket
355
+ socket = session["process"]._sock
356
+ while True:
357
+ # Check if the socket is still open before reading
358
+ if socket.fileno() == -1:
359
+ break
360
+ try:
361
+ ready, _, _ = select.select([socket], [], [], 0.1)
362
+ except (ValueError, OSError):
363
+ # Socket may have been closed by another thread
364
+ break
365
+ if ready:
366
+ data = socket.recv(4096)
367
+ if not data:
368
+ break
369
+ decoded_data = data.decode(
370
+ 'utf-8', errors='ignore'
371
+ )
372
+ session["output_stream"].put(decoded_data)
373
+ self._write_to_log(
374
+ session["log_file"], decoded_data
375
+ )
376
+ # Check if the process is still running
377
+ if not self.docker_api_client.exec_inspect(
378
+ session["exec_id"]
379
+ )['Running']:
380
+ break
381
+ except Exception as e:
382
+ # Log the exception for diagnosis and store it on the session
383
+ logger.exception(f"[SESSION {session_id}] Reader thread error")
384
+ try:
385
+ with self._session_lock:
386
+ if session_id in self.shell_sessions:
387
+ self.shell_sessions[session_id]["error"] = str(e)
388
+ except Exception:
389
+ # Swallow any secondary errors during cleanup
390
+ pass
391
+ finally:
392
+ try:
393
+ with self._session_lock:
394
+ if session_id in self.shell_sessions:
395
+ self.shell_sessions[session_id]["running"] = False
396
+ except Exception:
397
+ pass
398
+
399
+ thread = threading.Thread(target=reader, daemon=True)
400
+ thread.start()
401
+
402
+ def _collect_output_until_idle(
403
+ self,
404
+ id: str,
405
+ idle_duration: float = 0.5,
406
+ check_interval: float = 0.1,
407
+ max_wait: float = 5.0,
408
+ ) -> str:
409
+ r"""Collects output from a session until it's idle or a max wait time
410
+ is reached.
411
+
412
+ Args:
413
+ id (str): The session ID.
414
+ idle_duration (float): How long the stream must be empty to be
415
+ considered idle.(default: 0.5)
416
+ check_interval (float): The time to sleep between checks.
417
+ (default: 0.1)
418
+ max_wait (float): The maximum total time to wait for the process
419
+ to go idle. (default: 5.0)
420
+
421
+ Returns:
422
+ str: The collected output. If max_wait is reached while
423
+ the process is still outputting, a warning is appended.
424
+ """
425
+ with self._session_lock:
426
+ if id not in self.shell_sessions:
427
+ return f"Error: No session found with ID '{id}'."
428
+
429
+ output_parts = []
430
+ idle_time = 0.0
431
+ start_time = time.time()
432
+
433
+ while time.time() - start_time < max_wait:
434
+ new_output = self.shell_view(id)
435
+
436
+ # Check for terminal state messages from shell_view
437
+ if "--- SESSION TERMINATED ---" in new_output:
438
+ # Append the final output before the termination message
439
+ final_part = new_output.replace(
440
+ "--- SESSION TERMINATED ---", ""
441
+ ).strip()
442
+ if final_part:
443
+ output_parts.append(final_part)
444
+ # Session is dead, return what we have plus the message
445
+ return "".join(output_parts) + "\n--- SESSION TERMINATED ---"
446
+
447
+ if new_output.startswith("Error: No session found"):
448
+ return new_output
449
+
450
+ if new_output:
451
+ output_parts.append(new_output)
452
+ idle_time = 0.0 # Reset idle timer
453
+ else:
454
+ idle_time += check_interval
455
+ if idle_time >= idle_duration:
456
+ # Process is idle, success
457
+ return "".join(output_parts)
458
+ time.sleep(check_interval)
459
+
460
+ # If we exit the loop, it means max_wait was reached.
461
+ # Check one last time for any final output.
462
+ final_output = self.shell_view(id)
463
+ if final_output:
464
+ output_parts.append(final_output)
465
+
466
+ warning_message = (
467
+ "\n--- WARNING: Process is still actively outputting "
468
+ "after max wait time. Consider using shell_wait() "
469
+ "before sending the next command. ---"
470
+ )
471
+ return "".join(output_parts) + warning_message
472
+
473
+ def shell_exec(self, id: str, command: str, block: bool = True) -> str:
474
+ r"""This function executes a shell command. The command can run in
475
+ blocking mode (waits for completion) or non-blocking mode
476
+ (runs in the background). A unique session ID is created for
477
+ each session.
478
+
479
+ Args:
480
+ command (str): The command to execute.
481
+ block (bool): If True, the command runs synchronously,
482
+ waiting for it to complete or time out, and returns
483
+ its full output. If False, the command runs
484
+ asynchronously in the background.
485
+ id (Optional[str]): A specific ID for the session. If not provided,
486
+ a unique ID is generated for non-blocking sessions.
487
+
488
+ Returns:
489
+ str: If block is True, returns the complete stdout and stderr.
490
+ If block is False, returns a message containing the new
491
+ session ID and the initial output from the command after
492
+ it goes idle.
493
+ """
494
+ if self.safe_mode:
495
+ is_safe, message = self._sanitize_command(command)
496
+ if not is_safe:
497
+ return f"Error: {message}"
498
+ command = message
499
+ else:
500
+ command = command
501
+
502
+ if self.use_docker_backend:
503
+ # For Docker, we always run commands in a shell
504
+ # to support complex commands
505
+ command = f'bash -c "{command}"'
506
+ else:
507
+ # For local execution, check if we need to use cloned environment
508
+ command = self._adapt_command_for_environment(command)
509
+
510
+ session_id = id
511
+
512
+ if block:
513
+ # --- BLOCKING EXECUTION ---
514
+ log_entry = (
515
+ f"--- Executing blocking command at "
516
+ f"{time.ctime()} ---\n> {command}\n"
517
+ )
518
+ output = ""
519
+ try:
520
+ if not self.use_docker_backend:
521
+ # LOCAL BLOCKING
522
+ result = subprocess.run(
523
+ command,
524
+ capture_output=True,
525
+ text=True,
526
+ shell=True,
527
+ timeout=self.timeout,
528
+ cwd=self.working_dir,
529
+ encoding="utf-8",
530
+ )
531
+ stdout = result.stdout or ""
532
+ stderr = result.stderr or ""
533
+ output = stdout + (
534
+ f"\nSTDERR:\n{stderr}" if stderr else ""
535
+ )
536
+ else:
537
+ # DOCKER BLOCKING
538
+ assert (
539
+ self.docker_workdir is not None
540
+ ) # Docker backend always has workdir
541
+ exec_instance = self.docker_api_client.exec_create(
542
+ self.container.id, command, workdir=self.docker_workdir
543
+ )
544
+ exec_output = self.docker_api_client.exec_start(
545
+ exec_instance['Id']
546
+ )
547
+ output = exec_output.decode('utf-8', errors='ignore')
548
+
549
+ log_entry += f"--- Output ---\n{output}\n"
550
+ return _to_plain(output)
551
+ except subprocess.TimeoutExpired:
552
+ error_msg = (
553
+ f"Error: Command timed out after {self.timeout} seconds."
554
+ )
555
+ log_entry += f"--- Error ---\n{error_msg}\n"
556
+ return error_msg
557
+ except Exception as e:
558
+ if "Read timed out" in str(e):
559
+ error_msg = (
560
+ f"Error: Command timed out after "
561
+ f"{self.timeout} seconds."
562
+ )
563
+ else:
564
+ error_msg = f"Error executing command: {e}"
565
+ log_entry += f"--- Error ---\n{error_msg}\n"
566
+ return error_msg
567
+ finally:
568
+ self._write_to_log(self.blocking_log_file, log_entry + "\n")
569
+ else:
570
+ # --- NON-BLOCKING EXECUTION ---
571
+ session_log_file = os.path.join(
572
+ self.log_dir, f"session_{session_id}.log"
573
+ )
574
+
575
+ self._write_to_log(
576
+ session_log_file,
577
+ f"--- Starting non-blocking session at {time.ctime()} ---\n"
578
+ f"> {command}\n",
579
+ )
580
+
581
+ with self._session_lock:
582
+ self.shell_sessions[session_id] = {
583
+ "id": session_id,
584
+ "process": None,
585
+ "output_stream": Queue(),
586
+ "command_history": [command],
587
+ "running": True,
588
+ "log_file": session_log_file,
589
+ "backend": "docker"
590
+ if self.use_docker_backend
591
+ else "local",
592
+ }
593
+
594
+ try:
595
+ if not self.use_docker_backend:
596
+ process = subprocess.Popen(
597
+ command,
598
+ stdin=subprocess.PIPE,
599
+ stdout=subprocess.PIPE,
600
+ stderr=subprocess.STDOUT,
601
+ shell=True,
602
+ text=True,
603
+ cwd=self.working_dir,
604
+ encoding="utf-8",
605
+ )
606
+ with self._session_lock:
607
+ self.shell_sessions[session_id]["process"] = process
608
+ else:
609
+ assert (
610
+ self.docker_workdir is not None
611
+ ) # Docker backend always has workdir
612
+ exec_instance = self.docker_api_client.exec_create(
613
+ self.container.id,
614
+ command,
615
+ stdin=True,
616
+ tty=True,
617
+ workdir=self.docker_workdir,
618
+ )
619
+ exec_id = exec_instance['Id']
620
+ exec_socket = self.docker_api_client.exec_start(
621
+ exec_id, tty=True, stream=True, socket=True
622
+ )
623
+ with self._session_lock:
624
+ self.shell_sessions[session_id]["process"] = (
625
+ exec_socket
626
+ )
627
+ self.shell_sessions[session_id]["exec_id"] = exec_id
628
+
629
+ self._start_output_reader_thread(session_id)
630
+
631
+ # time.sleep(0.1)
632
+ initial_output = self._collect_output_until_idle(session_id)
633
+
634
+ return (
635
+ f"Session started with ID: {session_id}\n\n"
636
+ f"[Initial Output]:\n{initial_output}"
637
+ )
638
+
639
+ except Exception as e:
640
+ with self._session_lock:
641
+ if session_id in self.shell_sessions:
642
+ self.shell_sessions[session_id]["running"] = False
643
+ error_msg = f"Error starting non-blocking command: {e}"
644
+ self._write_to_log(
645
+ session_log_file, f"--- Error ---\n{error_msg}\n"
646
+ )
647
+ return error_msg
648
+
649
+ def shell_write_to_process(self, id: str, command: str) -> str:
650
+ r"""This function sends command to a running non-blocking
651
+ process and returns the resulting output after the process
652
+ becomes idle again. A newline \n is automatically appended
653
+ to the input command.
654
+
655
+ Args:
656
+ id (str): The unique session ID of the non-blocking process.
657
+ command (str): The text to write to the process's standard input.
658
+
659
+ Returns:
660
+ str: The output from the process after the command is sent.
661
+ """
662
+ with self._session_lock:
663
+ if (
664
+ id not in self.shell_sessions
665
+ or not self.shell_sessions[id]["running"]
666
+ ):
667
+ return (
668
+ f"Error: No active non-blocking "
669
+ f"session found with ID '{id}'."
670
+ )
671
+ session = self.shell_sessions[id]
672
+
673
+ # Flush any lingering output from previous commands.
674
+ self._collect_output_until_idle(id, idle_duration=0.3, max_wait=2.0)
675
+
676
+ with self._session_lock:
677
+ session["command_history"].append(command)
678
+ log_file = session["log_file"]
679
+ backend = session["backend"]
680
+ process = session["process"]
681
+
682
+ # Log command to the raw log file
683
+ self._write_to_log(log_file, f"> {command}\n")
684
+
685
+ try:
686
+ if backend == "local":
687
+ process.stdin.write(command + '\n')
688
+ process.stdin.flush()
689
+ else: # docker
690
+ socket = process._sock
691
+ socket.sendall((command + '\n').encode('utf-8'))
692
+
693
+ # Wait for and collect the new output
694
+ output = self._collect_output_until_idle(id)
695
+
696
+ return output
697
+
698
+ except Exception as e:
699
+ return f"Error writing to session '{id}': {e}"
700
+
701
+ def shell_view(self, id: str) -> str:
702
+ r"""This function retrieves any new output from a non-blocking session
703
+ since the last time this function was called. If the process has
704
+ terminated, it drains the output queue and appends a termination
705
+ message. If the process is still running, it simply returns any
706
+ new output.
707
+
708
+ Args:
709
+ id (str): The unique session ID of the non-blocking process.
710
+
711
+ Returns:
712
+ str: The new output from the process's stdout and stderr. Returns
713
+ an empty string if there is no new output.
714
+ """
715
+ with self._session_lock:
716
+ if id not in self.shell_sessions:
717
+ return f"Error: No session found with ID '{id}'."
718
+ session = self.shell_sessions[id]
719
+ is_running = session["running"]
720
+
721
+ # If session is terminated, drain the queue and return
722
+ # with a status message.
723
+ if not is_running:
724
+ final_output = []
725
+ try:
726
+ while True:
727
+ final_output.append(session["output_stream"].get_nowait())
728
+ except Empty:
729
+ pass
730
+ return "".join(final_output) + "\n--- SESSION TERMINATED ---"
731
+
732
+ # Otherwise, just drain the queue for a live session.
733
+ output = []
734
+ try:
735
+ while True:
736
+ output.append(session["output_stream"].get_nowait())
737
+ except Empty:
738
+ pass
739
+
740
+ return "".join(output)
741
+
742
+ def shell_wait(self, id: str, wait_seconds: float = 5.0) -> str:
743
+ r"""This function waits for a specified duration for a
744
+ non-blocking process to produce more output or terminate.
745
+
746
+ Args:
747
+ id (str): The unique session ID of the non-blocking process.
748
+ wait_seconds (float): The maximum number of seconds to wait.
749
+
750
+ Returns:
751
+ str: All output collected during the wait period.
752
+ """
753
+ with self._session_lock:
754
+ if id not in self.shell_sessions:
755
+ return f"Error: No session found with ID '{id}'."
756
+ session = self.shell_sessions[id]
757
+ if not session["running"]:
758
+ return (
759
+ "Session is no longer running. "
760
+ "Use shell_view to get final output."
761
+ )
762
+
763
+ output_collected = []
764
+ end_time = time.time() + wait_seconds
765
+ while time.time() < end_time and session["running"]:
766
+ new_output = self.shell_view(id)
767
+ if new_output:
768
+ output_collected.append(new_output)
769
+ time.sleep(0.2)
770
+
771
+ return "".join(output_collected)
772
+
773
+ def shell_kill_process(self, id: str) -> str:
774
+ r"""This function forcibly terminates a running non-blocking process.
775
+
776
+ Args:
777
+ id (str): The unique session ID of the process to kill.
778
+
779
+ Returns:
780
+ str: A confirmation message indicating the process was terminated.
781
+ """
782
+ with self._session_lock:
783
+ if (
784
+ id not in self.shell_sessions
785
+ or not self.shell_sessions[id]["running"]
786
+ ):
787
+ return f"Error: No active session found with ID '{id}'."
788
+ session = self.shell_sessions[id]
789
+ try:
790
+ if session["backend"] == "local":
791
+ session["process"].terminate()
792
+ time.sleep(0.5)
793
+ if session["process"].poll() is None:
794
+ session["process"].kill()
795
+ # Ensure stdio streams are closed to unblock reader thread
796
+ try:
797
+ if getattr(session["process"], "stdin", None):
798
+ session["process"].stdin.close()
799
+ except Exception:
800
+ pass
801
+ try:
802
+ if getattr(session["process"], "stdout", None):
803
+ session["process"].stdout.close()
804
+ except Exception:
805
+ pass
806
+ else: # docker
807
+ # Docker exec processes stop when the socket is closed.
808
+ session["process"].close()
809
+ with self._session_lock:
810
+ if id in self.shell_sessions:
811
+ self.shell_sessions[id]["running"] = False
812
+ return f"Process in session '{id}' has been terminated."
813
+ except Exception as e:
814
+ return f"Error killing process in session '{id}': {e}"
815
+
816
+ def shell_ask_user_for_help(self, id: str, prompt: str) -> str:
817
+ r"""This function pauses execution and asks a human for help
818
+ with an interactive session.
819
+
820
+ This method can handle different scenarios:
821
+ 1. If session exists: Shows session output and allows interaction
822
+ 2. If session doesn't exist: Creates a temporary session for help
823
+
824
+ Args:
825
+ id (str): The session ID of the interactive process needing help.
826
+ Can be empty string for general help without session context.
827
+ prompt (str): The question or instruction from the LLM to show the
828
+ human user (e.g., "The program is asking for a filename. Please
829
+ enter 'config.json'.").
830
+
831
+ Returns:
832
+ str: The output from the shell session after the user's command has
833
+ been executed, or help information for general queries.
834
+ """
835
+ logger.info("\n" + "=" * 60)
836
+ logger.info("🤖 LLM Agent needs your help!")
837
+ logger.info(f"PROMPT: {prompt}")
838
+
839
+ # Case 1: Session doesn't exist - offer to create one
840
+ if id not in self.shell_sessions:
841
+ try:
842
+ user_input = input("Your response: ").strip()
843
+ if not user_input:
844
+ return "No user response."
845
+ else:
846
+ logger.info(
847
+ f"Creating session '{id}' and executing command..."
848
+ )
849
+ result = self.shell_exec(id, user_input, block=True)
850
+ return (
851
+ f"Session '{id}' created and "
852
+ f"executed command:\n{result}"
853
+ )
854
+ except EOFError:
855
+ return f"User input interrupted for session '{id}' creation."
856
+
857
+ # Case 2: Session exists - show context and interact
858
+ else:
859
+ # Get the latest output to show the user the current state
860
+ last_output = self._collect_output_until_idle(id)
861
+
862
+ logger.info(f"SESSION: '{id}' (active)")
863
+ logger.info("=" * 60)
864
+ logger.info("--- LAST OUTPUT ---")
865
+ logger.info(
866
+ last_output.strip()
867
+ if last_output.strip()
868
+ else "(no recent output)"
869
+ )
870
+ logger.info("-------------------")
871
+
872
+ try:
873
+ user_input = input("Your input: ").strip()
874
+ if not user_input:
875
+ return f"User provided no input for session '{id}'."
876
+ else:
877
+ # Send input to the existing session
878
+ return self.shell_write_to_process(id, user_input)
879
+ except EOFError:
880
+ return f"User input interrupted for session '{id}'."
881
+
882
+ def __del__(self):
883
+ # Clean up any sessions
884
+ with self._session_lock:
885
+ session_ids = list(self.shell_sessions.keys())
886
+ for session_id in session_ids:
887
+ with self._session_lock:
888
+ is_running = self.shell_sessions.get(session_id, {}).get(
889
+ "running", False
890
+ )
891
+ if is_running:
892
+ self.shell_kill_process(session_id)
893
+
894
+ def get_tools(self) -> List[FunctionTool]:
895
+ r"""Returns a list of FunctionTool objects representing the functions
896
+ in the toolkit.
897
+
898
+ Returns:
899
+ List[FunctionTool]: A list of FunctionTool objects representing the
900
+ functions in the toolkit.
901
+ """
902
+ return [
903
+ FunctionTool(self.shell_exec),
904
+ FunctionTool(self.shell_view),
905
+ FunctionTool(self.shell_wait),
906
+ FunctionTool(self.shell_write_to_process),
907
+ FunctionTool(self.shell_kill_process),
908
+ FunctionTool(self.shell_ask_user_for_help),
909
+ ]