mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-19 07:59:39 +00:00
122 lines
4.5 KiB
Python
122 lines
4.5 KiB
Python
import asyncio
|
|
import logging
|
|
import os
|
|
import threading
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from typing import Optional
|
|
from camel.toolkits.terminal_toolkit import TerminalToolkit as BaseTerminalToolkit
|
|
from camel.toolkits.terminal_toolkit.terminal_toolkit import _to_plain
|
|
from app.component.environment import env
|
|
from app.service.task import Action, ActionTerminalData, Agents, get_task_lock
|
|
from app.utils.listen.toolkit_listen import auto_listen_toolkit
|
|
from app.utils.toolkit.abstract_toolkit import AbstractToolkit
|
|
from app.service.task import process_task
|
|
|
|
|
|
@auto_listen_toolkit(BaseTerminalToolkit)
|
|
class TerminalToolkit(BaseTerminalToolkit, AbstractToolkit):
|
|
agent_name: str = Agents.developer_agent
|
|
_thread_pool: Optional[ThreadPoolExecutor] = None
|
|
_thread_local = threading.local()
|
|
|
|
def __init__(
|
|
self,
|
|
api_task_id: str,
|
|
agent_name: str | None = None,
|
|
timeout: float | None = None,
|
|
working_directory: str | None = None,
|
|
use_docker_backend: bool = False,
|
|
docker_container_name: str | None = None,
|
|
session_logs_dir: str | None = None,
|
|
safe_mode: bool = True,
|
|
allowed_commands: list[str] | None = None,
|
|
clone_current_env: bool = False,
|
|
):
|
|
self.api_task_id = api_task_id
|
|
if agent_name is not None:
|
|
self.agent_name = agent_name
|
|
if working_directory is None:
|
|
working_directory = env("file_save_path", os.path.expanduser("~/.eigent/terminal/"))
|
|
if TerminalToolkit._thread_pool is None:
|
|
TerminalToolkit._thread_pool = ThreadPoolExecutor(
|
|
max_workers=1,
|
|
thread_name_prefix="terminal_toolkit"
|
|
)
|
|
super().__init__(
|
|
timeout=timeout,
|
|
working_directory=working_directory,
|
|
use_docker_backend=use_docker_backend,
|
|
docker_container_name=docker_container_name,
|
|
session_logs_dir=session_logs_dir,
|
|
safe_mode=safe_mode,
|
|
allowed_commands=allowed_commands,
|
|
clone_current_env=clone_current_env,
|
|
)
|
|
|
|
def _write_to_log(self, log_file: str, content: str) -> None:
|
|
r"""Write content to log file with optional ANSI stripping.
|
|
|
|
Args:
|
|
log_file (str): Path to the log file
|
|
content (str): Content to write
|
|
"""
|
|
# Convert ANSI escape sequences to plain text
|
|
super()._write_to_log(log_file, content)
|
|
self._update_terminal_output(_to_plain(content))
|
|
|
|
def _update_terminal_output(self, output: str):
|
|
task_lock = get_task_lock(self.api_task_id)
|
|
process_task_id = process_task.get("")
|
|
|
|
# Create the coroutine
|
|
coro = task_lock.put_queue(
|
|
ActionTerminalData(
|
|
action=Action.terminal,
|
|
process_task_id=process_task_id,
|
|
data=output,
|
|
)
|
|
)
|
|
|
|
# Try to get the current event loop, if none exists, create a new one in a thread
|
|
try:
|
|
loop = asyncio.get_running_loop()
|
|
# If we're in an async context, schedule the coroutine
|
|
task = loop.create_task(coro)
|
|
if hasattr(task_lock, "add_background_task"):
|
|
task_lock.add_background_task(task)
|
|
except RuntimeError:
|
|
self._thread_pool.submit(self._run_coro_in_thread, coro,task_lock)
|
|
|
|
@staticmethod
|
|
def _run_coro_in_thread(coro,task_lock):
|
|
"""
|
|
Execute coro in the thread pool, with each thread bound to a long-term event loop
|
|
"""
|
|
if not hasattr(TerminalToolkit._thread_local, "loop"):
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
TerminalToolkit._thread_local.loop = loop
|
|
else:
|
|
loop = TerminalToolkit._thread_local.loop
|
|
|
|
if loop.is_closed():
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
TerminalToolkit._thread_local.loop = loop
|
|
|
|
try:
|
|
task = loop.create_task(coro)
|
|
if hasattr(task_lock, "add_background_task"):
|
|
task_lock.add_background_task(task)
|
|
loop.run_until_complete(task)
|
|
except Exception as e:
|
|
logging.error(
|
|
f"Failed to execute coroutine in thread pool: {str(e)}",
|
|
exc_info=True
|
|
)
|
|
|
|
@classmethod
|
|
def shutdown(cls):
|
|
if cls._thread_pool:
|
|
cls._thread_pool.shutdown(wait=True)
|
|
cls._thread_pool = None
|