chore: merge main
This commit is contained in:
@@ -594,13 +594,20 @@ def camofox_console(clear: bool = False, task_id: Optional[str] = None) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def cleanup_all_camofox_sessions() -> None:
|
||||
"""Close all active camofox sessions."""
|
||||
"""Close all active camofox sessions.
|
||||
|
||||
When managed persistence is enabled, only clears local tracking state
|
||||
without destroying server-side browser profiles (cookies, logins, etc.
|
||||
must survive). Ephemeral sessions are fully deleted on the server.
|
||||
"""
|
||||
managed = _managed_persistence_enabled()
|
||||
with _sessions_lock:
|
||||
sessions = list(_sessions.items())
|
||||
for task_id, session in sessions:
|
||||
try:
|
||||
_delete(f"/sessions/{session['user_id']}")
|
||||
except Exception:
|
||||
pass
|
||||
if not managed:
|
||||
for _task_id, session in sessions:
|
||||
try:
|
||||
_delete(f"/sessions/{session['user_id']}")
|
||||
except Exception:
|
||||
pass
|
||||
with _sessions_lock:
|
||||
_sessions.clear()
|
||||
|
||||
@@ -285,6 +285,26 @@ def _get_cloud_provider() -> Optional[CloudBrowserProvider]:
|
||||
return _cached_cloud_provider
|
||||
|
||||
|
||||
from hermes_constants import is_termux as _is_termux_environment
|
||||
|
||||
|
||||
def _browser_install_hint() -> str:
|
||||
if _is_termux_environment():
|
||||
return "npm install -g agent-browser && agent-browser install"
|
||||
return "npm install -g agent-browser && agent-browser install --with-deps"
|
||||
|
||||
|
||||
def _requires_real_termux_browser_install(browser_cmd: str) -> bool:
|
||||
return _is_termux_environment() and _is_local_mode() and browser_cmd.strip() == "npx agent-browser"
|
||||
|
||||
|
||||
def _termux_browser_install_error() -> str:
|
||||
return (
|
||||
"Local browser automation on Termux cannot rely on the bare npx fallback. "
|
||||
f"Install agent-browser explicitly first: {_browser_install_hint()}"
|
||||
)
|
||||
|
||||
|
||||
def _is_local_mode() -> bool:
|
||||
"""Return True when the browser tool will use a local browser backend."""
|
||||
if _get_cdp_override():
|
||||
@@ -796,7 +816,8 @@ def _find_agent_browser() -> str:
|
||||
return "npx agent-browser"
|
||||
|
||||
raise FileNotFoundError(
|
||||
"agent-browser CLI not found. Install it with: npm install -g agent-browser\n"
|
||||
"agent-browser CLI not found. Install it with: "
|
||||
f"{_browser_install_hint()}\n"
|
||||
"Or run 'npm install' in the repo root to install locally.\n"
|
||||
"Or ensure npx is available in your PATH."
|
||||
)
|
||||
@@ -852,6 +873,11 @@ def _run_browser_command(
|
||||
except FileNotFoundError as e:
|
||||
logger.warning("agent-browser CLI not found: %s", e)
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
if _requires_real_termux_browser_install(browser_cmd):
|
||||
error = _termux_browser_install_error()
|
||||
logger.warning("browser command blocked on Termux: %s", error)
|
||||
return {"success": False, "error": error}
|
||||
|
||||
from tools.interrupt import is_interrupted
|
||||
if is_interrupted():
|
||||
@@ -2040,10 +2066,17 @@ def check_browser_requirements() -> bool:
|
||||
|
||||
# The agent-browser CLI is always required
|
||||
try:
|
||||
_find_agent_browser()
|
||||
browser_cmd = _find_agent_browser()
|
||||
except FileNotFoundError:
|
||||
return False
|
||||
|
||||
# On Termux, the bare npx fallback is too fragile to treat as a satisfied
|
||||
# local browser dependency. Require a real install (global or local) so the
|
||||
# browser tool is not advertised as available when it will likely fail on
|
||||
# first use.
|
||||
if _requires_real_termux_browser_install(browser_cmd):
|
||||
return False
|
||||
|
||||
# In cloud mode, also require provider credentials
|
||||
provider = _get_cloud_provider()
|
||||
if provider is not None and not provider.is_configured():
|
||||
@@ -2073,10 +2106,13 @@ if __name__ == "__main__":
|
||||
else:
|
||||
print("❌ Missing requirements:")
|
||||
try:
|
||||
_find_agent_browser()
|
||||
browser_cmd = _find_agent_browser()
|
||||
if _requires_real_termux_browser_install(browser_cmd):
|
||||
print(" - bare npx fallback found (insufficient on Termux local mode)")
|
||||
print(f" Install: {_browser_install_hint()}")
|
||||
except FileNotFoundError:
|
||||
print(" - agent-browser CLI not found")
|
||||
print(" Install: npm install -g agent-browser && agent-browser install --with-deps")
|
||||
print(f" Install: {_browser_install_hint()}")
|
||||
if _cp is not None and not _cp.is_configured():
|
||||
print(f" - {_cp.provider_name()} credentials not configured")
|
||||
print(" Tip: set browser.cloud_provider to 'local' to use free local mode instead")
|
||||
|
||||
@@ -33,6 +33,7 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import shlex
|
||||
import signal
|
||||
import socket
|
||||
import subprocess
|
||||
@@ -246,9 +247,9 @@ def _call(tool_name, args):
|
||||
|
||||
_FILE_TRANSPORT_HEADER = '''\
|
||||
"""Auto-generated Hermes tools RPC stubs (file-based transport)."""
|
||||
import json, os, shlex, time
|
||||
import json, os, shlex, tempfile, time
|
||||
|
||||
_RPC_DIR = os.environ.get("HERMES_RPC_DIR", "/tmp/hermes_rpc")
|
||||
_RPC_DIR = os.environ.get("HERMES_RPC_DIR") or os.path.join(tempfile.gettempdir(), "hermes_rpc")
|
||||
_seq = 0
|
||||
''' + _COMMON_HELPERS + '''\
|
||||
|
||||
@@ -536,13 +537,30 @@ def _ship_file_to_remote(env, remote_path: str, content: str) -> None:
|
||||
quotes are fine.
|
||||
"""
|
||||
encoded = base64.b64encode(content.encode("utf-8")).decode("ascii")
|
||||
quoted_remote_path = shlex.quote(remote_path)
|
||||
env.execute(
|
||||
f"echo '{encoded}' | base64 -d > {remote_path}",
|
||||
f"echo '{encoded}' | base64 -d > {quoted_remote_path}",
|
||||
cwd="/",
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
|
||||
def _env_temp_dir(env: Any) -> str:
|
||||
"""Return a writable temp dir for env-backed execute_code sandboxes."""
|
||||
get_temp_dir = getattr(env, "get_temp_dir", None)
|
||||
if callable(get_temp_dir):
|
||||
try:
|
||||
temp_dir = get_temp_dir()
|
||||
if isinstance(temp_dir, str) and temp_dir.startswith("/"):
|
||||
return temp_dir.rstrip("/") or "/"
|
||||
except Exception as exc:
|
||||
logger.debug("Could not resolve execute_code env temp dir: %s", exc)
|
||||
candidate = tempfile.gettempdir()
|
||||
if isinstance(candidate, str) and candidate.startswith("/"):
|
||||
return candidate.rstrip("/") or "/"
|
||||
return "/tmp"
|
||||
|
||||
|
||||
def _rpc_poll_loop(
|
||||
env,
|
||||
rpc_dir: str,
|
||||
@@ -563,11 +581,12 @@ def _rpc_poll_loop(
|
||||
|
||||
poll_interval = 0.1 # 100 ms
|
||||
|
||||
quoted_rpc_dir = shlex.quote(rpc_dir)
|
||||
while not stop_event.is_set():
|
||||
try:
|
||||
# List pending request files (skip .tmp partials)
|
||||
ls_result = env.execute(
|
||||
f"ls -1 {rpc_dir}/req_* 2>/dev/null || true",
|
||||
f"ls -1 {quoted_rpc_dir}/req_* 2>/dev/null || true",
|
||||
cwd="/",
|
||||
timeout=10,
|
||||
)
|
||||
@@ -589,9 +608,10 @@ def _rpc_poll_loop(
|
||||
|
||||
call_start = time.monotonic()
|
||||
|
||||
quoted_req_file = shlex.quote(req_file)
|
||||
# Read request
|
||||
read_result = env.execute(
|
||||
f"cat {req_file}",
|
||||
f"cat {quoted_req_file}",
|
||||
cwd="/",
|
||||
timeout=10,
|
||||
)
|
||||
@@ -600,7 +620,7 @@ def _rpc_poll_loop(
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
logger.debug("Malformed RPC request in %s", req_file)
|
||||
# Remove bad request to avoid infinite retry
|
||||
env.execute(f"rm -f {req_file}", cwd="/", timeout=5)
|
||||
env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
|
||||
continue
|
||||
|
||||
tool_name = request.get("tool", "")
|
||||
@@ -608,6 +628,7 @@ def _rpc_poll_loop(
|
||||
seq = request.get("seq", 0)
|
||||
seq_str = f"{seq:06d}"
|
||||
res_file = f"{rpc_dir}/res_{seq_str}"
|
||||
quoted_res_file = shlex.quote(res_file)
|
||||
|
||||
# Enforce allow-list
|
||||
if tool_name not in allowed_tools:
|
||||
@@ -665,14 +686,14 @@ def _rpc_poll_loop(
|
||||
tool_result.encode("utf-8")
|
||||
).decode("ascii")
|
||||
env.execute(
|
||||
f"echo '{encoded_result}' | base64 -d > {res_file}.tmp"
|
||||
f" && mv {res_file}.tmp {res_file}",
|
||||
f"echo '{encoded_result}' | base64 -d > {quoted_res_file}.tmp"
|
||||
f" && mv {quoted_res_file}.tmp {quoted_res_file}",
|
||||
cwd="/",
|
||||
timeout=60,
|
||||
)
|
||||
|
||||
# Remove the request file
|
||||
env.execute(f"rm -f {req_file}", cwd="/", timeout=5)
|
||||
env.execute(f"rm -f {quoted_req_file}", cwd="/", timeout=5)
|
||||
|
||||
except Exception as e:
|
||||
if not stop_event.is_set():
|
||||
@@ -707,7 +728,10 @@ def _execute_remote(
|
||||
env, env_type = _get_or_create_env(effective_task_id)
|
||||
|
||||
sandbox_id = uuid.uuid4().hex[:12]
|
||||
sandbox_dir = f"/tmp/hermes_exec_{sandbox_id}"
|
||||
temp_dir = _env_temp_dir(env)
|
||||
sandbox_dir = f"{temp_dir}/hermes_exec_{sandbox_id}"
|
||||
quoted_sandbox_dir = shlex.quote(sandbox_dir)
|
||||
quoted_rpc_dir = shlex.quote(f"{sandbox_dir}/rpc")
|
||||
|
||||
tool_call_log: list = []
|
||||
tool_call_counter = [0]
|
||||
@@ -735,7 +759,7 @@ def _execute_remote(
|
||||
|
||||
# Create sandbox directory on remote
|
||||
env.execute(
|
||||
f"mkdir -p {sandbox_dir}/rpc", cwd="/", timeout=10,
|
||||
f"mkdir -p {quoted_rpc_dir}", cwd="/", timeout=10,
|
||||
)
|
||||
|
||||
# Generate and ship files
|
||||
@@ -759,7 +783,7 @@ def _execute_remote(
|
||||
|
||||
# Build environment variable prefix for the script
|
||||
env_prefix = (
|
||||
f"HERMES_RPC_DIR={sandbox_dir}/rpc "
|
||||
f"HERMES_RPC_DIR={shlex.quote(f'{sandbox_dir}/rpc')} "
|
||||
f"PYTHONDONTWRITEBYTECODE=1"
|
||||
)
|
||||
tz = os.getenv("HERMES_TIMEZONE", "").strip()
|
||||
@@ -770,7 +794,7 @@ def _execute_remote(
|
||||
logger.info("Executing code on %s backend (task %s)...",
|
||||
env_type, effective_task_id[:8])
|
||||
script_result = env.execute(
|
||||
f"cd {sandbox_dir} && {env_prefix} python3 script.py",
|
||||
f"cd {quoted_sandbox_dir} && {env_prefix} python3 script.py",
|
||||
timeout=timeout,
|
||||
)
|
||||
|
||||
@@ -807,7 +831,7 @@ def _execute_remote(
|
||||
# Clean up remote sandbox dir
|
||||
try:
|
||||
env.execute(
|
||||
f"rm -rf {sandbox_dir}", cwd="/", timeout=15,
|
||||
f"rm -rf {quoted_sandbox_dir}", cwd="/", timeout=15,
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("Failed to clean up remote sandbox %s", sandbox_dir)
|
||||
|
||||
@@ -226,14 +226,24 @@ class BaseEnvironment(ABC):
|
||||
# Snapshot creation timeout (override for slow cold-starts).
|
||||
_snapshot_timeout: int = 30
|
||||
|
||||
def get_temp_dir(self) -> str:
|
||||
"""Return the backend temp directory used for session artifacts.
|
||||
|
||||
Most sandboxed backends use ``/tmp`` inside the target environment.
|
||||
LocalEnvironment overrides this on platforms like Termux where ``/tmp``
|
||||
may be missing and ``TMPDIR`` is the portable writable location.
|
||||
"""
|
||||
return "/tmp"
|
||||
|
||||
def __init__(self, cwd: str, timeout: int, env: dict = None):
|
||||
self.cwd = cwd
|
||||
self.timeout = timeout
|
||||
self.env = env or {}
|
||||
|
||||
self._session_id = uuid.uuid4().hex[:12]
|
||||
self._snapshot_path = f"/tmp/hermes-snap-{self._session_id}.sh"
|
||||
self._cwd_file = f"/tmp/hermes-cwd-{self._session_id}.txt"
|
||||
temp_dir = self.get_temp_dir().rstrip("/") or "/"
|
||||
self._snapshot_path = f"{temp_dir}/hermes-snap-{self._session_id}.sh"
|
||||
self._cwd_file = f"{temp_dir}/hermes-cwd-{self._session_id}.txt"
|
||||
self._cwd_marker = _cwd_marker(self._session_id)
|
||||
self._snapshot_ready = False
|
||||
self._last_sync_time: float | None = (
|
||||
|
||||
@@ -5,6 +5,7 @@ import platform
|
||||
import shutil
|
||||
import signal
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from tools.environments.base import BaseEnvironment, _pipe_stdin
|
||||
|
||||
@@ -209,6 +210,32 @@ class LocalEnvironment(BaseEnvironment):
|
||||
super().__init__(cwd=cwd or os.getcwd(), timeout=timeout, env=env)
|
||||
self.init_session()
|
||||
|
||||
def get_temp_dir(self) -> str:
|
||||
"""Return a shell-safe writable temp dir for local execution.
|
||||
|
||||
Termux does not provide /tmp by default, but exposes a POSIX TMPDIR.
|
||||
Prefer POSIX-style env vars when available, keep using /tmp on regular
|
||||
Unix systems, and only fall back to tempfile.gettempdir() when it also
|
||||
resolves to a POSIX path.
|
||||
|
||||
Check the environment configured for this backend first so callers can
|
||||
override the temp root explicitly (for example via terminal.env or a
|
||||
custom TMPDIR), then fall back to the host process environment.
|
||||
"""
|
||||
for env_var in ("TMPDIR", "TMP", "TEMP"):
|
||||
candidate = self.env.get(env_var) or os.environ.get(env_var)
|
||||
if candidate and candidate.startswith("/"):
|
||||
return candidate.rstrip("/") or "/"
|
||||
|
||||
if os.path.isdir("/tmp") and os.access("/tmp", os.W_OK | os.X_OK):
|
||||
return "/tmp"
|
||||
|
||||
candidate = tempfile.gettempdir()
|
||||
if candidate.startswith("/"):
|
||||
return candidate.rstrip("/") or "/"
|
||||
|
||||
return "/tmp"
|
||||
|
||||
def _run_bash(self, cmd_string: str, *, login: bool = False,
|
||||
timeout: int = 120,
|
||||
stdin_data: str | None = None) -> subprocess.Popen:
|
||||
|
||||
@@ -183,6 +183,19 @@ class ProcessRegistry:
|
||||
|
||||
# ----- Spawn -----
|
||||
|
||||
@staticmethod
|
||||
def _env_temp_dir(env: Any) -> str:
|
||||
"""Return the writable sandbox temp dir for env-backed background tasks."""
|
||||
get_temp_dir = getattr(env, "get_temp_dir", None)
|
||||
if callable(get_temp_dir):
|
||||
try:
|
||||
temp_dir = get_temp_dir()
|
||||
if isinstance(temp_dir, str) and temp_dir.startswith("/"):
|
||||
return temp_dir.rstrip("/") or "/"
|
||||
except Exception as exc:
|
||||
logger.debug("Could not resolve environment temp dir: %s", exc)
|
||||
return "/tmp"
|
||||
|
||||
def spawn_local(
|
||||
self,
|
||||
command: str,
|
||||
@@ -327,12 +340,20 @@ class ProcessRegistry:
|
||||
)
|
||||
|
||||
# Run the command in the sandbox with output capture
|
||||
log_path = f"/tmp/hermes_bg_{session.id}.log"
|
||||
pid_path = f"/tmp/hermes_bg_{session.id}.pid"
|
||||
temp_dir = self._env_temp_dir(env)
|
||||
log_path = f"{temp_dir}/hermes_bg_{session.id}.log"
|
||||
pid_path = f"{temp_dir}/hermes_bg_{session.id}.pid"
|
||||
exit_path = f"{temp_dir}/hermes_bg_{session.id}.exit"
|
||||
quoted_command = shlex.quote(command)
|
||||
quoted_temp_dir = shlex.quote(temp_dir)
|
||||
quoted_log_path = shlex.quote(log_path)
|
||||
quoted_pid_path = shlex.quote(pid_path)
|
||||
quoted_exit_path = shlex.quote(exit_path)
|
||||
bg_command = (
|
||||
f"nohup bash -c {quoted_command} > {log_path} 2>&1 & "
|
||||
f"echo $! > {pid_path} && cat {pid_path}"
|
||||
f"mkdir -p {quoted_temp_dir} && "
|
||||
f"( nohup bash -lc {quoted_command} > {quoted_log_path} 2>&1; "
|
||||
f"rc=$?; printf '%s\\n' \"$rc\" > {quoted_exit_path} ) & "
|
||||
f"echo $! > {quoted_pid_path} && cat {quoted_pid_path}"
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -353,7 +374,7 @@ class ProcessRegistry:
|
||||
# Start a poller thread that periodically reads the log file
|
||||
reader = threading.Thread(
|
||||
target=self._env_poller_loop,
|
||||
args=(session, env, log_path, pid_path),
|
||||
args=(session, env, log_path, pid_path, exit_path),
|
||||
daemon=True,
|
||||
name=f"proc-poller-{session.id}",
|
||||
)
|
||||
@@ -397,14 +418,17 @@ class ProcessRegistry:
|
||||
self._move_to_finished(session)
|
||||
|
||||
def _env_poller_loop(
|
||||
self, session: ProcessSession, env: Any, log_path: str, pid_path: str
|
||||
self, session: ProcessSession, env: Any, log_path: str, pid_path: str, exit_path: str
|
||||
):
|
||||
"""Background thread: poll a sandbox log file for non-local backends."""
|
||||
quoted_log_path = shlex.quote(log_path)
|
||||
quoted_pid_path = shlex.quote(pid_path)
|
||||
quoted_exit_path = shlex.quote(exit_path)
|
||||
while not session.exited:
|
||||
time.sleep(2) # Poll every 2 seconds
|
||||
try:
|
||||
# Read new output from the log file
|
||||
result = env.execute(f"cat {log_path} 2>/dev/null", timeout=10)
|
||||
result = env.execute(f"cat {quoted_log_path} 2>/dev/null", timeout=10)
|
||||
new_output = result.get("output", "")
|
||||
if new_output:
|
||||
with session._lock:
|
||||
@@ -414,14 +438,14 @@ class ProcessRegistry:
|
||||
|
||||
# Check if process is still running
|
||||
check = env.execute(
|
||||
f"kill -0 $(cat {pid_path} 2>/dev/null) 2>/dev/null; echo $?",
|
||||
f"kill -0 \"$(cat {quoted_pid_path} 2>/dev/null)\" 2>/dev/null; echo $?",
|
||||
timeout=5,
|
||||
)
|
||||
check_output = check.get("output", "").strip()
|
||||
if check_output and check_output.splitlines()[-1].strip() != "0":
|
||||
# Process has exited -- get exit code
|
||||
# Process has exited -- get exit code captured by the wrapper shell.
|
||||
exit_result = env.execute(
|
||||
f"wait $(cat {pid_path} 2>/dev/null) 2>/dev/null; echo $?",
|
||||
f"cat {quoted_exit_path} 2>/dev/null",
|
||||
timeout=5,
|
||||
)
|
||||
exit_str = exit_result.get("output", "").strip()
|
||||
@@ -711,6 +735,29 @@ class ProcessRegistry:
|
||||
"""Send data + newline to a running process's stdin (like pressing Enter)."""
|
||||
return self.write_stdin(session_id, data + "\n")
|
||||
|
||||
def close_stdin(self, session_id: str) -> dict:
|
||||
"""Close a running process's stdin / send EOF without killing the process."""
|
||||
session = self.get(session_id)
|
||||
if session is None:
|
||||
return {"status": "not_found", "error": f"No process with ID {session_id}"}
|
||||
if session.exited:
|
||||
return {"status": "already_exited", "error": "Process has already finished"}
|
||||
|
||||
if hasattr(session, '_pty') and session._pty:
|
||||
try:
|
||||
session._pty.sendeof()
|
||||
return {"status": "ok", "message": "EOF sent"}
|
||||
except Exception as e:
|
||||
return {"status": "error", "error": str(e)}
|
||||
|
||||
if not session.process or not session.process.stdin:
|
||||
return {"status": "error", "error": "Process stdin not available (non-local backend or stdin closed)"}
|
||||
try:
|
||||
session.process.stdin.close()
|
||||
return {"status": "ok", "message": "stdin closed"}
|
||||
except Exception as e:
|
||||
return {"status": "error", "error": str(e)}
|
||||
|
||||
def list_sessions(self, task_id: str = None) -> list:
|
||||
"""List all running and recently-finished processes."""
|
||||
with self._lock:
|
||||
@@ -926,14 +973,14 @@ PROCESS_SCHEMA = {
|
||||
"Actions: 'list' (show all), 'poll' (check status + new output), "
|
||||
"'log' (full output with pagination), 'wait' (block until done or timeout), "
|
||||
"'kill' (terminate), 'write' (send raw stdin data without newline), "
|
||||
"'submit' (send data + Enter, for answering prompts)."
|
||||
"'submit' (send data + Enter, for answering prompts), 'close' (close stdin/send EOF)."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["list", "poll", "log", "wait", "kill", "write", "submit"],
|
||||
"enum": ["list", "poll", "log", "wait", "kill", "write", "submit", "close"],
|
||||
"description": "Action to perform on background processes"
|
||||
},
|
||||
"session_id": {
|
||||
@@ -973,7 +1020,7 @@ def _handle_process(args, **kw):
|
||||
|
||||
if action == "list":
|
||||
return _json.dumps({"processes": process_registry.list_sessions(task_id=task_id)}, ensure_ascii=False)
|
||||
elif action in ("poll", "log", "wait", "kill", "write", "submit"):
|
||||
elif action in ("poll", "log", "wait", "kill", "write", "submit", "close"):
|
||||
if not session_id:
|
||||
return tool_error(f"session_id is required for {action}")
|
||||
if action == "poll":
|
||||
@@ -989,7 +1036,9 @@ def _handle_process(args, **kw):
|
||||
return _json.dumps(process_registry.write_stdin(session_id, str(args.get("data", ""))), ensure_ascii=False)
|
||||
elif action == "submit":
|
||||
return _json.dumps(process_registry.submit_stdin(session_id, str(args.get("data", ""))), ensure_ascii=False)
|
||||
return tool_error(f"Unknown process action: {action}. Use: list, poll, log, wait, kill, write, submit")
|
||||
elif action == "close":
|
||||
return _json.dumps(process_registry.close_stdin(session_id), ensure_ascii=False)
|
||||
return tool_error(f"Unknown process action: {action}. Use: list, poll, log, wait, kill, write, submit, close")
|
||||
|
||||
|
||||
registry.register(
|
||||
|
||||
@@ -322,6 +322,13 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None,
|
||||
|
||||
media_files = media_files or []
|
||||
|
||||
if platform == Platform.SLACK and message:
|
||||
try:
|
||||
slack_adapter = SlackAdapter.__new__(SlackAdapter)
|
||||
message = slack_adapter.format_message(message)
|
||||
except Exception:
|
||||
logger.debug("Failed to apply Slack mrkdwn formatting in _send_to_platform", exc_info=True)
|
||||
|
||||
# Platform message length limits (from adapter class attributes)
|
||||
_MAX_LENGTHS = {
|
||||
Platform.TELEGRAM: TelegramAdapter.MAX_MESSAGE_LENGTH,
|
||||
@@ -548,10 +555,13 @@ async def _send_discord(token, chat_id, message):
|
||||
except ImportError:
|
||||
return {"error": "aiohttp not installed. Run: pip install aiohttp"}
|
||||
try:
|
||||
from gateway.platforms.base import resolve_proxy_url, proxy_kwargs_for_aiohttp
|
||||
_proxy = resolve_proxy_url(platform_env_var="DISCORD_PROXY")
|
||||
_sess_kw, _req_kw = proxy_kwargs_for_aiohttp(_proxy)
|
||||
url = f"https://discord.com/api/v10/channels/{chat_id}/messages"
|
||||
headers = {"Authorization": f"Bot {token}", "Content-Type": "application/json"}
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30)) as session:
|
||||
async with session.post(url, headers=headers, json={"content": message}) as resp:
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30), **_sess_kw) as session:
|
||||
async with session.post(url, headers=headers, json={"content": message}, **_req_kw) as resp:
|
||||
if resp.status not in (200, 201):
|
||||
body = await resp.text()
|
||||
return _error(f"Discord API error ({resp.status}): {body}")
|
||||
@@ -568,10 +578,14 @@ async def _send_slack(token, chat_id, message):
|
||||
except ImportError:
|
||||
return {"error": "aiohttp not installed. Run: pip install aiohttp"}
|
||||
try:
|
||||
from gateway.platforms.base import resolve_proxy_url, proxy_kwargs_for_aiohttp
|
||||
_proxy = resolve_proxy_url()
|
||||
_sess_kw, _req_kw = proxy_kwargs_for_aiohttp(_proxy)
|
||||
url = "https://slack.com/api/chat.postMessage"
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30)) as session:
|
||||
async with session.post(url, headers=headers, json={"channel": chat_id, "text": message}) as resp:
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30), **_sess_kw) as session:
|
||||
payload = {"channel": chat_id, "text": message, "mrkdwn": True}
|
||||
async with session.post(url, headers=headers, json=payload, **_req_kw) as resp:
|
||||
data = await resp.json()
|
||||
if data.get("ok"):
|
||||
return {"success": True, "platform": "slack", "chat_id": chat_id, "message_id": data.get("ts")}
|
||||
@@ -704,18 +718,21 @@ async def _send_sms(auth_token, chat_id, message):
|
||||
message = message.strip()
|
||||
|
||||
try:
|
||||
from gateway.platforms.base import resolve_proxy_url, proxy_kwargs_for_aiohttp
|
||||
_proxy = resolve_proxy_url()
|
||||
_sess_kw, _req_kw = proxy_kwargs_for_aiohttp(_proxy)
|
||||
creds = f"{account_sid}:{auth_token}"
|
||||
encoded = base64.b64encode(creds.encode("ascii")).decode("ascii")
|
||||
url = f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json"
|
||||
headers = {"Authorization": f"Basic {encoded}"}
|
||||
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30)) as session:
|
||||
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=30), **_sess_kw) as session:
|
||||
form_data = aiohttp.FormData()
|
||||
form_data.add_field("From", from_number)
|
||||
form_data.add_field("To", chat_id)
|
||||
form_data.add_field("Body", message)
|
||||
|
||||
async with session.post(url, data=form_data, headers=headers) as resp:
|
||||
async with session.post(url, data=form_data, headers=headers, **_req_kw) as resp:
|
||||
body = await resp.json()
|
||||
if resp.status >= 400:
|
||||
error_msg = body.get("message", str(body))
|
||||
|
||||
@@ -1112,6 +1112,21 @@ def _interpret_exit_code(command: str, exit_code: int) -> str | None:
|
||||
return None
|
||||
|
||||
|
||||
def _command_requires_pipe_stdin(command: str) -> bool:
|
||||
"""Return True when PTY mode would break stdin-driven commands.
|
||||
|
||||
Some CLIs change behavior when stdin is a TTY. In particular,
|
||||
`gh auth login --with-token` expects the token to arrive via piped stdin and
|
||||
waits for EOF; when we launch it under a PTY, `process.submit()` only sends a
|
||||
newline, so the command appears to hang forever with no visible progress.
|
||||
"""
|
||||
normalized = " ".join(command.lower().split())
|
||||
return (
|
||||
normalized.startswith("gh auth login")
|
||||
and "--with-token" in normalized
|
||||
)
|
||||
|
||||
|
||||
def terminal_tool(
|
||||
command: str,
|
||||
background: bool = False,
|
||||
@@ -1332,6 +1347,17 @@ def terminal_tool(
|
||||
}, ensure_ascii=False)
|
||||
|
||||
# Prepare command for execution
|
||||
pty_disabled_reason = None
|
||||
effective_pty = pty
|
||||
if pty and _command_requires_pipe_stdin(command):
|
||||
effective_pty = False
|
||||
pty_disabled_reason = (
|
||||
"PTY disabled for this command because it expects piped stdin/EOF "
|
||||
"(for example gh auth login --with-token). For local background "
|
||||
"processes, call process(action='close') after writing so it receives "
|
||||
"EOF."
|
||||
)
|
||||
|
||||
if background:
|
||||
# Spawn a tracked background process via the process registry.
|
||||
# For local backends: uses subprocess.Popen with output buffering.
|
||||
@@ -1349,7 +1375,7 @@ def terminal_tool(
|
||||
task_id=effective_task_id,
|
||||
session_key=session_key,
|
||||
env_vars=env.env if hasattr(env, 'env') else None,
|
||||
use_pty=pty,
|
||||
use_pty=effective_pty,
|
||||
)
|
||||
else:
|
||||
proc_session = process_registry.spawn_via_env(
|
||||
@@ -1369,6 +1395,8 @@ def terminal_tool(
|
||||
}
|
||||
if approval_note:
|
||||
result_data["approval"] = approval_note
|
||||
if pty_disabled_reason:
|
||||
result_data["pty_note"] = pty_disabled_reason
|
||||
|
||||
# Transparent timeout clamping note
|
||||
max_timeout = effective_timeout
|
||||
|
||||
@@ -9,9 +9,11 @@ Defense against context-window overflow operates at three levels:
|
||||
2. **Per-result persistence** (maybe_persist_tool_result): After a tool
|
||||
returns, if its output exceeds the tool's registered threshold
|
||||
(registry.get_max_result_size), the full output is written INTO THE
|
||||
SANDBOX at /tmp/hermes-results/{tool_use_id}.txt via env.execute().
|
||||
The in-context content is replaced with a preview + file path reference.
|
||||
The model can read_file to access the full output on any backend.
|
||||
SANDBOX temp dir (for example /tmp/hermes-results/{tool_use_id}.txt on
|
||||
standard Linux, or $TMPDIR/hermes-results/{tool_use_id}.txt on Termux)
|
||||
via env.execute(). The in-context content is replaced with a preview +
|
||||
file path reference. The model can read_file to access the full output
|
||||
on any backend.
|
||||
|
||||
3. **Per-turn aggregate budget** (enforce_turn_budget): After all tool
|
||||
results in a single assistant turn are collected, if the total exceeds
|
||||
@@ -21,6 +23,7 @@ Defense against context-window overflow operates at three levels:
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from tools.budget_config import (
|
||||
@@ -37,6 +40,22 @@ HEREDOC_MARKER = "HERMES_PERSIST_EOF"
|
||||
_BUDGET_TOOL_NAME = "__budget_enforcement__"
|
||||
|
||||
|
||||
def _resolve_storage_dir(env) -> str:
|
||||
"""Return the best temp-backed storage dir for this environment."""
|
||||
if env is not None:
|
||||
get_temp_dir = getattr(env, "get_temp_dir", None)
|
||||
if callable(get_temp_dir):
|
||||
try:
|
||||
temp_dir = get_temp_dir()
|
||||
except Exception as exc:
|
||||
logger.debug("Could not resolve env temp dir: %s", exc)
|
||||
else:
|
||||
if temp_dir:
|
||||
temp_dir = temp_dir.rstrip("/") or "/"
|
||||
return f"{temp_dir}/hermes-results"
|
||||
return STORAGE_DIR
|
||||
|
||||
|
||||
def generate_preview(content: str, max_chars: int = DEFAULT_PREVIEW_SIZE_CHARS) -> tuple[str, bool]:
|
||||
"""Truncate at last newline within max_chars. Returns (preview, has_more)."""
|
||||
if len(content) <= max_chars:
|
||||
@@ -58,8 +77,9 @@ def _heredoc_marker(content: str) -> str:
|
||||
def _write_to_sandbox(content: str, remote_path: str, env) -> bool:
|
||||
"""Write content into the sandbox via env.execute(). Returns True on success."""
|
||||
marker = _heredoc_marker(content)
|
||||
storage_dir = os.path.dirname(remote_path)
|
||||
cmd = (
|
||||
f"mkdir -p {STORAGE_DIR} && cat > {remote_path} << '{marker}'\n"
|
||||
f"mkdir -p {storage_dir} && cat > {remote_path} << '{marker}'\n"
|
||||
f"{content}\n"
|
||||
f"{marker}"
|
||||
)
|
||||
@@ -125,7 +145,8 @@ def maybe_persist_tool_result(
|
||||
if len(content) <= effective_threshold:
|
||||
return content
|
||||
|
||||
remote_path = f"{STORAGE_DIR}/{tool_use_id}.txt"
|
||||
storage_dir = _resolve_storage_dir(env)
|
||||
remote_path = f"{storage_dir}/{tool_use_id}.txt"
|
||||
preview, has_more = generate_preview(content, max_chars=config.preview_size)
|
||||
|
||||
if env is not None:
|
||||
|
||||
@@ -48,6 +48,47 @@ def _audio_available() -> bool:
|
||||
return False
|
||||
|
||||
|
||||
from hermes_constants import is_termux as _is_termux_environment
|
||||
|
||||
|
||||
def _voice_capture_install_hint() -> str:
|
||||
if _is_termux_environment():
|
||||
return "pkg install python-numpy portaudio && python -m pip install sounddevice"
|
||||
return "pip install sounddevice numpy"
|
||||
|
||||
|
||||
def _termux_microphone_command() -> Optional[str]:
|
||||
if not _is_termux_environment():
|
||||
return None
|
||||
return shutil.which("termux-microphone-record")
|
||||
|
||||
|
||||
def _termux_media_player_command() -> Optional[str]:
|
||||
if not _is_termux_environment():
|
||||
return None
|
||||
return shutil.which("termux-media-player")
|
||||
|
||||
|
||||
def _termux_api_app_installed() -> bool:
|
||||
if not _is_termux_environment():
|
||||
return False
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["pm", "list", "packages", "com.termux.api"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
check=False,
|
||||
)
|
||||
return "package:com.termux.api" in (result.stdout or "")
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _termux_voice_capture_available() -> bool:
|
||||
return _termux_microphone_command() is not None and _termux_api_app_installed()
|
||||
|
||||
|
||||
def detect_audio_environment() -> dict:
|
||||
"""Detect if the current environment supports audio I/O.
|
||||
|
||||
@@ -57,6 +98,9 @@ def detect_audio_environment() -> dict:
|
||||
"""
|
||||
warnings = [] # hard-fail: these block voice mode
|
||||
notices = [] # informational: logged but don't block
|
||||
termux_mic_cmd = _termux_microphone_command()
|
||||
termux_app_installed = _termux_api_app_installed()
|
||||
termux_capture = bool(termux_mic_cmd and termux_app_installed)
|
||||
|
||||
# SSH detection
|
||||
if any(os.environ.get(v) for v in ('SSH_CLIENT', 'SSH_TTY', 'SSH_CONNECTION')):
|
||||
@@ -89,23 +133,48 @@ def detect_audio_environment() -> dict:
|
||||
try:
|
||||
devices = sd.query_devices()
|
||||
if not devices:
|
||||
warnings.append("No audio input/output devices detected")
|
||||
if termux_capture:
|
||||
notices.append("No PortAudio devices detected, but Termux:API microphone capture is available")
|
||||
else:
|
||||
warnings.append("No audio input/output devices detected")
|
||||
except Exception:
|
||||
# In WSL with PulseAudio, device queries can fail even though
|
||||
# recording/playback works fine. Don't block if PULSE_SERVER is set.
|
||||
if os.environ.get('PULSE_SERVER'):
|
||||
notices.append("Audio device query failed but PULSE_SERVER is set -- continuing")
|
||||
elif termux_capture:
|
||||
notices.append("PortAudio device query failed, but Termux:API microphone capture is available")
|
||||
else:
|
||||
warnings.append("Audio subsystem error (PortAudio cannot query devices)")
|
||||
except ImportError:
|
||||
warnings.append("Audio libraries not installed (pip install sounddevice numpy)")
|
||||
if termux_capture:
|
||||
notices.append("Termux:API microphone recording available (sounddevice not required)")
|
||||
elif termux_mic_cmd and not termux_app_installed:
|
||||
warnings.append(
|
||||
"Termux:API Android app is not installed. Install/update the Termux:API app to use termux-microphone-record."
|
||||
)
|
||||
else:
|
||||
warnings.append(f"Audio libraries not installed ({_voice_capture_install_hint()})")
|
||||
except OSError:
|
||||
warnings.append(
|
||||
"PortAudio system library not found -- install it first:\n"
|
||||
" Linux: sudo apt-get install libportaudio2\n"
|
||||
" macOS: brew install portaudio\n"
|
||||
"Then retry /voice on."
|
||||
)
|
||||
if termux_capture:
|
||||
notices.append("Termux:API microphone recording available (PortAudio not required)")
|
||||
elif termux_mic_cmd and not termux_app_installed:
|
||||
warnings.append(
|
||||
"Termux:API Android app is not installed. Install/update the Termux:API app to use termux-microphone-record."
|
||||
)
|
||||
elif _is_termux_environment():
|
||||
warnings.append(
|
||||
"PortAudio system library not found -- install it first:\n"
|
||||
" Termux: pkg install portaudio\n"
|
||||
"Then retry /voice on."
|
||||
)
|
||||
else:
|
||||
warnings.append(
|
||||
"PortAudio system library not found -- install it first:\n"
|
||||
" Linux: sudo apt-get install libportaudio2\n"
|
||||
" macOS: brew install portaudio\n"
|
||||
"Then retry /voice on."
|
||||
)
|
||||
|
||||
return {
|
||||
"available": not warnings,
|
||||
@@ -174,6 +243,134 @@ def play_beep(frequency: int = 880, duration: float = 0.12, count: int = 1) -> N
|
||||
logger.debug("Beep playback failed: %s", e)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Termux Audio Recorder
|
||||
# ============================================================================
|
||||
class TermuxAudioRecorder:
|
||||
"""Recorder backend that uses Termux:API microphone capture commands."""
|
||||
|
||||
supports_silence_autostop = False
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._lock = threading.Lock()
|
||||
self._recording = False
|
||||
self._start_time = 0.0
|
||||
self._recording_path: Optional[str] = None
|
||||
self._current_rms = 0
|
||||
|
||||
@property
|
||||
def is_recording(self) -> bool:
|
||||
return self._recording
|
||||
|
||||
@property
|
||||
def elapsed_seconds(self) -> float:
|
||||
if not self._recording:
|
||||
return 0.0
|
||||
return time.monotonic() - self._start_time
|
||||
|
||||
@property
|
||||
def current_rms(self) -> int:
|
||||
return self._current_rms
|
||||
|
||||
def start(self, on_silence_stop=None) -> None:
|
||||
del on_silence_stop # Termux:API does not expose live silence callbacks.
|
||||
mic_cmd = _termux_microphone_command()
|
||||
if not mic_cmd:
|
||||
raise RuntimeError(
|
||||
"Termux voice capture requires the termux-api package and app.\n"
|
||||
"Install with: pkg install termux-api\n"
|
||||
"Then install/update the Termux:API Android app."
|
||||
)
|
||||
if not _termux_api_app_installed():
|
||||
raise RuntimeError(
|
||||
"Termux voice capture requires the Termux:API Android app.\n"
|
||||
"Install/update the Termux:API app, then retry /voice on."
|
||||
)
|
||||
|
||||
with self._lock:
|
||||
if self._recording:
|
||||
return
|
||||
os.makedirs(_TEMP_DIR, exist_ok=True)
|
||||
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
||||
self._recording_path = os.path.join(_TEMP_DIR, f"recording_{timestamp}.aac")
|
||||
|
||||
command = [
|
||||
mic_cmd,
|
||||
"-f", self._recording_path,
|
||||
"-l", "0",
|
||||
"-e", "aac",
|
||||
"-r", str(SAMPLE_RATE),
|
||||
"-c", str(CHANNELS),
|
||||
]
|
||||
try:
|
||||
subprocess.run(command, capture_output=True, text=True, timeout=15, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
details = (e.stderr or e.stdout or str(e)).strip()
|
||||
raise RuntimeError(f"Termux microphone start failed: {details}") from e
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Termux microphone start failed: {e}") from e
|
||||
|
||||
with self._lock:
|
||||
self._start_time = time.monotonic()
|
||||
self._recording = True
|
||||
self._current_rms = 0
|
||||
logger.info("Termux voice recording started")
|
||||
|
||||
def _stop_termux_recording(self) -> None:
|
||||
mic_cmd = _termux_microphone_command()
|
||||
if not mic_cmd:
|
||||
return
|
||||
subprocess.run([mic_cmd, "-q"], capture_output=True, text=True, timeout=15, check=False)
|
||||
|
||||
def stop(self) -> Optional[str]:
|
||||
with self._lock:
|
||||
if not self._recording:
|
||||
return None
|
||||
self._recording = False
|
||||
path = self._recording_path
|
||||
self._recording_path = None
|
||||
started_at = self._start_time
|
||||
self._current_rms = 0
|
||||
|
||||
self._stop_termux_recording()
|
||||
if not path or not os.path.isfile(path):
|
||||
return None
|
||||
if time.monotonic() - started_at < 0.3:
|
||||
try:
|
||||
os.unlink(path)
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
if os.path.getsize(path) <= 0:
|
||||
try:
|
||||
os.unlink(path)
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
logger.info("Termux voice recording stopped: %s", path)
|
||||
return path
|
||||
|
||||
def cancel(self) -> None:
|
||||
with self._lock:
|
||||
path = self._recording_path
|
||||
self._recording = False
|
||||
self._recording_path = None
|
||||
self._current_rms = 0
|
||||
try:
|
||||
self._stop_termux_recording()
|
||||
except Exception:
|
||||
pass
|
||||
if path and os.path.isfile(path):
|
||||
try:
|
||||
os.unlink(path)
|
||||
except OSError:
|
||||
pass
|
||||
logger.info("Termux voice recording cancelled")
|
||||
|
||||
def shutdown(self) -> None:
|
||||
self.cancel()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# AudioRecorder
|
||||
# ============================================================================
|
||||
@@ -193,6 +390,8 @@ class AudioRecorder:
|
||||
the user is silent for ``silence_duration`` seconds and calls the callback.
|
||||
"""
|
||||
|
||||
supports_silence_autostop = True
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._lock = threading.Lock()
|
||||
self._stream: Any = None
|
||||
@@ -526,6 +725,13 @@ class AudioRecorder:
|
||||
return wav_path
|
||||
|
||||
|
||||
def create_audio_recorder() -> AudioRecorder | TermuxAudioRecorder:
|
||||
"""Return the best recorder backend for the current environment."""
|
||||
if _termux_voice_capture_available():
|
||||
return TermuxAudioRecorder()
|
||||
return AudioRecorder()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Whisper hallucination filter
|
||||
# ============================================================================
|
||||
@@ -734,7 +940,8 @@ def check_voice_requirements() -> Dict[str, Any]:
|
||||
stt_available = stt_enabled and stt_provider != "none"
|
||||
|
||||
missing: List[str] = []
|
||||
has_audio = _audio_available()
|
||||
termux_capture = _termux_voice_capture_available()
|
||||
has_audio = _audio_available() or termux_capture
|
||||
|
||||
if not has_audio:
|
||||
missing.extend(["sounddevice", "numpy"])
|
||||
@@ -745,10 +952,12 @@ def check_voice_requirements() -> Dict[str, Any]:
|
||||
available = has_audio and stt_available and env_check["available"]
|
||||
details_parts = []
|
||||
|
||||
if has_audio:
|
||||
if termux_capture:
|
||||
details_parts.append("Audio capture: OK (Termux:API microphone)")
|
||||
elif has_audio:
|
||||
details_parts.append("Audio capture: OK")
|
||||
else:
|
||||
details_parts.append("Audio capture: MISSING (pip install sounddevice numpy)")
|
||||
details_parts.append(f"Audio capture: MISSING ({_voice_capture_install_hint()})")
|
||||
|
||||
if not stt_enabled:
|
||||
details_parts.append("STT provider: DISABLED in config (stt.enabled: false)")
|
||||
|
||||
Reference in New Issue
Block a user