Skip to content

Agent Zero AI framework

RFC & SSH exchange for development

frdel46689d6477d5
diff --git a/agent.py b/agent.py
index 4e218d2e8..0293dacd3 100644
--- a/agent.py
+++ b/agent.py
@@ -129,13 +129,9 @@ class AgentConfig:
rate_limit_requests: int = 15
rate_limit_input_tokens: int = 0
rate_limit_output_tokens: int = 0
- msgs_keep_max: int = 25
- msgs_keep_start: int = 5
- msgs_keep_end: int = 10
response_timeout_seconds: int = 60
- max_tool_response_length: int = 3000
- code_exec_docker_enabled: bool = True
- code_exec_docker_name: str = "agent-zero-dev"
+ code_exec_docker_enabled: bool = False
+ code_exec_docker_name: str = "A0-dev"
code_exec_docker_image: str = "frdel/agent-zero-run:development"
code_exec_docker_ports: dict[str, int] = field(
default_factory=lambda: {"22/tcp": 55022, "80/tcp": 55080}
@@ -150,7 +146,7 @@ class AgentConfig:
code_exec_ssh_addr: str = "localhost"
code_exec_ssh_port: int = 55022
code_exec_ssh_user: str = "root"
- code_exec_ssh_pass: str = "toor"
+ code_exec_ssh_pass: str = ""
additional: Dict[str, Any] = field(default_factory=dict)
diff --git a/docker/run/Dockerfile b/docker/run/Dockerfile
index a640f4aa6..33a9418ae 100644
--- a/docker/run/Dockerfile
+++ b/docker/run/Dockerfile
@@ -4,31 +4,16 @@ FROM debian:bookworm-slim
# Check if the argument is provided, else throw an error
ARG BRANCH
RUN if [ -z "$BRANCH" ]; then echo "ERROR: BRANCH is not set!" >&2; exit 1; fi
-
-# Update and install necessary packages
-RUN apt-get update && apt-get install -y \
- python3 \
- python3-pip \
- python3-venv \
- nodejs \
- npm \
- openssh-server \
- sudo \
- curl \
- wget \
- git \
- ffmpeg
-
-# Set up SSH
-RUN mkdir /var/run/sshd && \
- echo 'root:toor' | chpasswd && \
- sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
+ENV BRANCH=$BRANCH
# Copy contents of the project to /a0
COPY ./fs/ /
+# pre installation steps
+RUN bash /ins/pre_install.sh $BRANCH
+
# install additional software
-RUN bash /ins/install_searxng.sh
+RUN bash /ins/install_additional.sh $BRANCH
# install A0
RUN bash /ins/install_A0.sh $BRANCH
@@ -37,12 +22,11 @@ RUN bash /ins/install_A0.sh $BRANCH
ARG CACHE_DATE=none
RUN echo "cache buster $CACHE_DATE" && bash /ins/install_A02.sh $BRANCH
-# Cleanup package list
-RUN rm -rf /var/lib/apt/lists/*
-RUN apt-get clean
+# post installation steps
+RUN bash /ins/post_install.sh $BRANCH
# Expose ports
EXPOSE 22 80
# initialize runtime
-CMD ["/bin/bash", "/exe/initialize.sh", "$BRANCH"]
\ No newline at end of file
+CMD ["/bin/bash", "-c", "/bin/bash /exe/initialize.sh $BRANCH"]
\ No newline at end of file
diff --git a/docker/run/fs/exe/initialize.sh b/docker/run/fs/exe/initialize.sh
index 86a5be8e8..03cfdaf4f 100644
--- a/docker/run/fs/exe/initialize.sh
+++ b/docker/run/fs/exe/initialize.sh
@@ -18,11 +18,7 @@ chmod 444 /root/.profile
apt-get update &
# Start SSH service in background
-if [ "$BRANCH" != "development" ]; then
- /usr/sbin/sshd -D -o ListenAddress=127.0.0.1 &
-else
- /usr/sbin/sshd -D &
-fi
+/usr/sbin/sshd -D &
# Start searxng server in background
sudo -H -u searxng -i bash /exe/run_searxng.sh &
diff --git a/docker/run/fs/exe/run_A0.sh b/docker/run/fs/exe/run_A0.sh
index 697272510..2576d5a6a 100644
--- a/docker/run/fs/exe/run_A0.sh
+++ b/docker/run/fs/exe/run_A0.sh
@@ -28,6 +28,7 @@ function clone_repo() {
# setup and preload A0
setup_venv
clone_repo
+python /a0/prepare.py
python /a0/preload.py
# Loop to restart the Python script when it finishes
@@ -38,14 +39,15 @@ while true; do
echo "Starting A0..."
python /a0/run_ui.py \
+ --dockerized=true \
--port=80 \
--host="0.0.0.0" \
--code_exec_docker_enabled=false \
--code_exec_ssh_enabled=true \
- --code_exec_ssh_addr="localhost" \
- --code_exec_ssh_port=22 \
- --code_exec_ssh_user="root" \
- --code_exec_ssh_pass="toor"
+ # --code_exec_ssh_addr="localhost" \
+ # --code_exec_ssh_port=22 \
+ # --code_exec_ssh_user="root" \
+ # --code_exec_ssh_pass="toor"
# Check the exit status
if [ $? -ne 0 ]; then
diff --git a/docker/run/fs/ins/install_additional.sh b/docker/run/fs/ins/install_additional.sh
new file mode 100644
index 000000000..8d017d160
--- /dev/null
+++ b/docker/run/fs/ins/install_additional.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+# run the original install script again
+bash /ins/install_searxng.sh "$@"
\ No newline at end of file
diff --git a/docker/run/fs/ins/post_install.sh b/docker/run/fs/ins/post_install.sh
new file mode 100644
index 000000000..410ff19a7
--- /dev/null
+++ b/docker/run/fs/ins/post_install.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+# Cleanup package list
+rm -rf /var/lib/apt/lists/*
+apt-get clean
\ No newline at end of file
diff --git a/docker/run/fs/ins/pre_install.sh b/docker/run/fs/ins/pre_install.sh
new file mode 100644
index 000000000..74a78844f
--- /dev/null
+++ b/docker/run/fs/ins/pre_install.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+# Update and install necessary packages
+apt-get update && apt-get install -y \
+ python3 \
+ python3-pip \
+ python3-venv \
+ nodejs \
+ npm \
+ openssh-server \
+ sudo \
+ curl \
+ wget \
+ git \
+ ffmpeg
+
+# prepare SSH daemon
+bash /ins/setup_ssh.sh "$@"
\ No newline at end of file
diff --git a/docker/run/fs/ins/setup_ssh.sh b/docker/run/fs/ins/setup_ssh.sh
new file mode 100644
index 000000000..958323623
--- /dev/null
+++ b/docker/run/fs/ins/setup_ssh.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+# Set up SSH
+mkdir /var/run/sshd && \
+ # echo 'root:toor' | chpasswd && \
+ sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
\ No newline at end of file
diff --git a/initialize.py b/initialize.py
index 5f19e38b8..127e9f130 100644
--- a/initialize.py
+++ b/initialize.py
@@ -1,6 +1,7 @@
+import asyncio
import models
from agent import AgentConfig
-from python.helpers import files, runtime, settings
+from python.helpers import dotenv, files, rfc_exchange, runtime, settings, docker, log
def initialize():
@@ -49,28 +50,35 @@ def initialize():
rate_limit_requests=30,
# rate_limit_input_tokens = 0,
# rate_limit_output_tokens = 0,
- # msgs_keep_max = 25,
- # msgs_keep_start = 5,
- # msgs_keep_end = 10,
- max_tool_response_length=3000,
# response_timeout_seconds = 60,
- # code_exec_docker_enabled = True,
- # code_exec_docker_name = "agent-zero-dev",
+ code_exec_docker_enabled = False,
+ # code_exec_docker_name = "A0-dev",
# code_exec_docker_image = "frdel/agent-zero-run:development",
# code_exec_docker_ports = { "22/tcp": 55022, "80/tcp": 55080 }
# code_exec_docker_volumes = {
- # files.get_base_dir(): {"bind": "/a0", "mode": "rw"},
- # files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"},
+ # files.get_base_dir(): {"bind": "/a0", "mode": "rw"},
+ # files.get_abs_path("work_dir"): {"bind": "/root", "mode": "rw"},
# },
# code_exec_ssh_enabled = True,
# code_exec_ssh_addr = "localhost",
# code_exec_ssh_port = 55022,
# code_exec_ssh_user = "root",
- # code_exec_ssh_pass = "toor",
+ # code_exec_ssh_pass = "",
# additional = {},
)
- # update config with kwargs
+ # update SSH and docker settings
+ set_runtime_config(config, current_settings)
+
+ # update config with runtime args
+ args_override(config)
+
+ # return config object
+ return config
+
+
+def args_override(config):
+ # update config with runtime args
for key, value in runtime.args.items():
if hasattr(config, key):
# conversion based on type of config[key]
@@ -93,5 +101,25 @@ def initialize():
setattr(config, key, value)
- # return config object
- return config
+
+def set_runtime_config(config: AgentConfig, set: settings.Settings):
+ ssh_conf = settings.get_runtime_config(set)
+ for key, value in ssh_conf.items():
+ if hasattr(config, key):
+ setattr(config, key, value)
+
+ # if config.code_exec_docker_enabled:
+ # config.code_exec_docker_ports["22/tcp"] = ssh_conf["code_exec_ssh_port"]
+ # config.code_exec_docker_ports["80/tcp"] = ssh_conf["code_exec_http_port"]
+ # config.code_exec_docker_name = f"{config.code_exec_docker_name}-{ssh_conf['code_exec_ssh_port']}-{ssh_conf['code_exec_http_port']}"
+
+ # dman = docker.DockerContainerManager(
+ # logger=log.Log(),
+ # name=config.code_exec_docker_name,
+ # image=config.code_exec_docker_image,
+ # ports=config.code_exec_docker_ports,
+ # volumes=config.code_exec_docker_volumes,
+ # )
+ # dman.start_container()
+
+ # config.code_exec_ssh_pass = asyncio.run(rfc_exchange.get_root_password())
diff --git a/preload.py b/preload.py
index f820d5cab..2a8523c6a 100644
--- a/preload.py
+++ b/preload.py
@@ -1,3 +1,6 @@
from python.helpers import whisper
-whisper.preload() # preload transcription model
\ No newline at end of file
+print("Running preload...")
+
+# preload transcription model
+whisper.preload()
\ No newline at end of file
diff --git a/prepare.py b/prepare.py
new file mode 100644
index 000000000..e0c71f5d1
--- /dev/null
+++ b/prepare.py
@@ -0,0 +1,14 @@
+import subprocess
+from python.helpers import dotenv, runtime, settings
+import string
+import random
+
+print("Preparing environment...")
+
+# generate random root password if not set (for SSH)
+root_pass = dotenv.get_dotenv_value(dotenv.KEY_ROOT_PASSWORD)
+if not root_pass:
+ root_pass = ''.join(random.choices(string.ascii_letters + string.digits, k=32))
+ dotenv.save_dotenv_value(dotenv.KEY_ROOT_PASSWORD, root_pass)
+print("Changing root password...")
+subprocess.run(f"echo 'root:{root_pass}' | chpasswd", shell=True, check=True)
diff --git a/python/helpers/crypto.py b/python/helpers/crypto.py
new file mode 100644
index 000000000..a4fe8f209
--- /dev/null
+++ b/python/helpers/crypto.py
@@ -0,0 +1,66 @@
+import hashlib
+import hmac
+from cryptography.hazmat.primitives.asymmetric import rsa, padding
+from cryptography.hazmat.primitives import serialization, hashes
+import os
+
+
+def hash_data(data: str, password: str):
+ return hmac.new(password.encode(), data.encode(), hashlib.sha256).hexdigest()
+
+
+def verify_data(data: str, hash: str, password: str):
+ return hash_data(data, password) == hash
+
+
+def _generate_private_key():
+ return rsa.generate_private_key(
+ public_exponent=65537,
+ key_size=2048,
+ )
+
+
+def _generate_public_key(private_key: rsa.RSAPrivateKey):
+ return (
+ private_key.public_key()
+ .public_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PublicFormat.SubjectPublicKeyInfo,
+ )
+ .hex()
+ )
+
+def _decode_public_key(public_key: str) -> rsa.RSAPublicKey:
+ # Decode hex string back to bytes
+ pem_bytes = bytes.fromhex(public_key)
+ # Load the PEM public key
+ key = serialization.load_pem_public_key(pem_bytes)
+ if not isinstance(key, rsa.RSAPublicKey):
+ raise TypeError("The provided key is not an RSAPublicKey")
+ return key
+
+def encrypt_data(data: str, public_key_pem: str):
+ return _encrypt_data(data.encode("utf-8"), _decode_public_key(public_key_pem))
+
+def _encrypt_data(data: bytes, public_key: rsa.RSAPublicKey):
+ b = public_key.encrypt(
+ data,
+ padding.OAEP(
+ mgf=padding.MGF1(algorithm=hashes.SHA256()),
+ algorithm=hashes.SHA256(),
+ label=None,
+ ),
+ )
+ return b.hex()
+
+def decrypt_data(data: str, private_key: rsa.RSAPrivateKey):
+ b = private_key.decrypt(
+ bytes.fromhex(data),
+ padding.OAEP(
+ mgf=padding.MGF1(algorithm=hashes.SHA256()),
+ algorithm=hashes.SHA256(),
+ label=None,
+ ),
+ )
+ return b.decode("utf-8")
+
diff --git a/python/helpers/docker.py b/python/helpers/docker.py
index b358405e9..fc4b9661b 100644
--- a/python/helpers/docker.py
+++ b/python/helpers/docker.py
@@ -93,7 +93,7 @@ class DockerContainerManager:
name=self.name,
volumes=self.volumes, # type: ignore
)
- atexit.register(self.cleanup_container)
+ # atexit.register(self.cleanup_container)
print(f"Started container with ID: {self.container.id}")
if self.logger: self.logger.log(type="info", content=f"Started container with ID: {self.container.id}")
time.sleep(5) # this helps to get SSH ready
diff --git a/python/helpers/dotenv.py b/python/helpers/dotenv.py
index f96f3aba1..be9e01cbc 100644
--- a/python/helpers/dotenv.py
+++ b/python/helpers/dotenv.py
@@ -1,11 +1,13 @@
import os
import re
+
from .files import get_abs_path
from dotenv import load_dotenv as _load_dotenv
KEY_AUTH_LOGIN = "AUTH_LOGIN"
KEY_AUTH_PASSWORD = "AUTH_PASSWORD"
KEY_RFC_PASSWORD = "RFC_PASSWORD"
+KEY_ROOT_PASSWORD = "ROOT_PASSWORD"
def load_dotenv():
_load_dotenv(get_dotenv_file_path(), override=True)
@@ -15,7 +17,7 @@ def get_dotenv_file_path():
return get_abs_path(".env")
def get_dotenv_value(key: str):
- # load_dotenv()
+ # load_dotenv()
return os.getenv(key)
def save_dotenv_value(key: str, value: str):
diff --git a/python/helpers/messages.py b/python/helpers/messages.py
index b9f66ad43..ebcaf6be8 100644
--- a/python/helpers/messages.py
+++ b/python/helpers/messages.py
@@ -5,7 +5,7 @@ import json
def truncate_text(agent, output, threshold=1000):
threshold = int(threshold)
- if len(output) <= threshold:
+ if not threshold or len(output) <= threshold:
return output
# Adjust the file path as needed
diff --git a/python/helpers/rfc.py b/python/helpers/rfc.py
index bd356f4d1..eb7125d81 100644
--- a/python/helpers/rfc.py
+++ b/python/helpers/rfc.py
@@ -3,8 +3,7 @@ import inspect
import json
from typing import Any, TypedDict
import aiohttp
-import hmac
-import hashlib
+from python.helpers import crypto
from python.helpers import dotenv
@@ -36,14 +35,14 @@ async def call_rfc(
kwargs=kwargs,
)
call = RFCCall(
- rfc_input=json.dumps(input), hash=hash_data(json.dumps(input), password)
+ rfc_input=json.dumps(input), hash=crypto.hash_data(json.dumps(input), password)
)
result = await _send_json_data(url, call)
return result
async def handle_rfc(rfc_call: RFCCall, password: str):
- if not verify_data(rfc_call["rfc_input"], rfc_call["hash"], password):
+ if not crypto.verify_data(rfc_call["rfc_input"], rfc_call["hash"], password):
raise Exception("Invalid RFC hash")
input: RFCInput = json.loads(rfc_call["rfc_input"])
@@ -80,11 +79,3 @@ async def _send_json_data(url: str, data):
else:
error = await response.text()
raise Exception(error)
-
-
-def hash_data(data: str, password: str):
- return hmac.new(password.encode(), data.encode(), hashlib.sha256).hexdigest()
-
-
-def verify_data(data: str, hash: str, password: str):
- return hash_data(data, password) == hash
diff --git a/python/helpers/rfc_exchange.py b/python/helpers/rfc_exchange.py
new file mode 100644
index 000000000..440631fbb
--- /dev/null
+++ b/python/helpers/rfc_exchange.py
@@ -0,0 +1,19 @@
+from python.helpers import runtime, crypto, dotenv
+
+async def get_root_password():
+ if runtime.is_dockerized():
+ pswd = _get_root_password()
+ else:
+ priv = crypto._generate_private_key()
+ pub = crypto._generate_public_key(priv)
+ enc = await runtime.call_development_function(_provide_root_password, pub)
+ pswd = crypto.decrypt_data(enc, priv)
+ return pswd
+
+def _provide_root_password(public_key_pem: str):
+ pswd = _get_root_password()
+ enc = crypto.encrypt_data(pswd, public_key_pem)
+ return enc
+
+def _get_root_password():
+ return dotenv.get_dotenv_value(dotenv.KEY_ROOT_PASSWORD) or ""
\ No newline at end of file
diff --git a/python/helpers/runtime.py b/python/helpers/runtime.py
index 03a2285e3..44eb519e3 100644
--- a/python/helpers/runtime.py
+++ b/python/helpers/runtime.py
@@ -35,10 +35,15 @@ def get_arg(name: str):
global args
return args.get(name, None)
+def has_arg(name: str):
+ global args
+ return name in args
-def is_development() -> bool:
- return get_arg("development") == True
+def is_dockerized() -> bool:
+ return get_arg("dockerized")
+def is_development() -> bool:
+ return not is_dockerized()
async def call_development_function(func: Callable, *args, **kwargs):
if is_development():
@@ -71,8 +76,12 @@ def _get_rfc_password() -> str:
def _get_rfc_url() -> str:
- url = settings.get_settings()["rfc_url"]
- if not url.endswith("/"):
- url += "/"
- url += "rfc"
+ set = settings.get_settings()
+ url = set["rfc_url"]
+ if not "://" in url:
+ url = "http://"+url
+ if url.endswith("/"):
+ url = url[:-1]
+ url = url+":"+str(set["rfc_port_http"])
+ url += "/rfc"
return url
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index e57a087e0..b81bb9ed2 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -1,10 +1,12 @@
+import asyncio
import json
import os
import re
+import subprocess
from typing import Any, Literal, Optional, TypedDict
import models
-from python.helpers import whisper
+from python.helpers import rfc_exchange, runtime, whisper
from . import files, dotenv
from models import get_model, ModelProvider, ModelType
from langchain_core.language_models.chat_models import BaseChatModel
@@ -36,9 +38,13 @@ class Settings(TypedDict):
auth_login: str
auth_password: str
+ root_password: str
+ rfc_auto_docker: bool
rfc_url: str
rfc_password: str
+ rfc_port_http: int
+ rfc_port_ssh: int
stt_model_size: str
stt_language: str
@@ -46,6 +52,7 @@ class Settings(TypedDict):
stt_silence_duration: int
stt_waiting_timeout: int
+
class PartialSettings(Settings, total=False):
pass
@@ -77,6 +84,8 @@ class SettingsOutput(TypedDict):
sections: list[SettingsSection]
+PASSWORD_PLACEHOLDER = "****PSWD****"
+
SETTINGS_FILE = files.get_abs_path("tmp/settings.json")
_settings: Settings | None = None
@@ -290,8 +299,8 @@ def convert_out(settings: Settings) -> SettingsOutput:
auth_fields.append(
{
"id": "auth_login",
- "title": "Login",
- "description": "User name",
+ "title": "UI Login",
+ "description": "Set user name for web UI",
"type": "input",
"value": dotenv.get_dotenv_value(dotenv.KEY_AUTH_LOGIN) or "",
}
@@ -300,13 +309,28 @@ def convert_out(settings: Settings) -> SettingsOutput:
auth_fields.append(
{
"id": "auth_password",
- "title": "Password",
- "description": "User password",
+ "title": "UI Password",
+ "description": "Set user password for web UI",
"type": "password",
- "value": dotenv.get_dotenv_value(dotenv.KEY_AUTH_PASSWORD) or "",
+ "value": (
+ PASSWORD_PLACEHOLDER
+ if dotenv.get_dotenv_value(dotenv.KEY_AUTH_PASSWORD)
+ else ""
+ ),
}
)
+ if runtime.is_dockerized():
+ auth_fields.append(
+ {
+ "id": "root_password",
+ "title": "root Password",
+ "description": "Change linux root password in docker container. This password can be used for SSH access. Original password was randomly generated during setup.",
+ "type": "password",
+ "value": "",
+ }
+ )
+
auth_section: SettingsSection = {
"title": "Authentication",
"description": "Settings for authentication to use Agent Zero Web UI.",
@@ -393,90 +417,136 @@ def convert_out(settings: Settings) -> SettingsOutput:
dev_fields: list[SettingsField] = []
- dev_fields.append(
- {
- "id": "rfc_url",
- "title": "RFC Destination URL",
- "description": "URL for remote function calls. RFCs are used to call functions on another A0 instance. You can develop and debug A0 natively on your local system while redirecting some functions to A0 instance in docker.",
- "type": "input",
- "value": settings["rfc_url"],
- }
- )
+ if runtime.is_development():
+ # dev_fields.append(
+ # {
+ # "id": "rfc_auto_docker",
+ # "title": "RFC Auto Docker Management",
+ # "description": "Automatically create dockerized instance of A0 for RFCs using this instance's code base and, settings and .env.",
+ # "type": "input",
+ # "value": settings["rfc_auto_docker"],
+ # }
+ # )
+
+ dev_fields.append(
+ {
+ "id": "rfc_url",
+ "title": "RFC Destination URL",
+ "description": "URL of dockerized A0 instance for remote function calls. Do not specify port here.",
+ "type": "input",
+ "value": settings["rfc_url"],
+ }
+ )
dev_fields.append(
{
"id": "rfc_password",
"title": "RFC Password",
- "description": "Password for remote function calls. Passwords must match on both systems. RFCs can not be used with empty password.",
+ "description": "Password for remote function calls. Passwords must match on both instances. RFCs can not be used with empty password.",
"type": "password",
- "value": dotenv.get_dotenv_value(dotenv.KEY_RFC_PASSWORD) or "",
+ "value": (
+ PASSWORD_PLACEHOLDER
+ if dotenv.get_dotenv_value(dotenv.KEY_RFC_PASSWORD)
+ else ""
+ ),
}
)
+ if runtime.is_development():
+ dev_fields.append(
+ {
+ "id": "rfc_port_http",
+ "title": "RFC HTTP port",
+ "description": "HTTP port for dockerized instance of A0.",
+ "type": "input",
+ "value": settings["rfc_port_http"],
+ }
+ )
+
+ dev_fields.append(
+ {
+ "id": "rfc_port_ssh",
+ "title": "RFC SSH port",
+ "description": "SSH port for dockerized instance of A0.",
+ "type": "input",
+ "value": settings["rfc_port_ssh"],
+ }
+ )
+
dev_section: SettingsSection = {
"title": "Development",
- "description": "Parameters for A0 framework development.",
+ "description": "Parameters for A0 framework development. RFCs (remote function calls) are used to call functions on another A0 instance. You can develop and debug A0 natively on your local system while redirecting some functions to A0 instance in docker. This is crucial for development as A0 needs to run in standardized environment to support all features.",
"fields": dev_fields,
}
# Speech to text section
stt_fields: list[SettingsField] = []
-
- stt_fields.append({
- "id": "stt_model_size",
- "title": "Model Size",
- "description": "Select the speech recognition model size",
- "type": "select",
- "value": settings["stt_model_size"],
- "options": [
- {"value": "tiny", "label": "Tiny (39M, English)"},
- {"value": "base", "label": "Base (74M, English)"},
- {"value": "small", "label": "Small (244M, English)"},
- {"value": "medium", "label": "Medium (769M, English)"},
- {"value": "large", "label": "Large (1.5B, Multilingual)"},
- {"value": "turbo", "label": "Turbo (Multilingual)"}
- ]
- })
-
- stt_fields.append({
- "id": "stt_language",
- "title": "Language Code",
- "description": "Language code (e.g. en, fr, it)",
- "type": "input",
- "value": settings["stt_language"]
- })
-
- stt_fields.append({
- "id": "stt_silence_threshold",
- "title": "Silence threshold",
- "description": "Silence detection threshold. Lower values are more sensitive.",
- "type": "range",
- "min": 0,
- "max": 1,
- "step": 0.01,
- "value": settings["stt_silence_threshold"]
- })
-
- stt_fields.append({
- "id": "stt_silence_duration",
- "title": "Silence duration (ms)",
- "description": "Duration of silence before the server considers speaking to have ended.",
- "type": "input",
- "value": settings["stt_silence_duration"]
- })
-
- stt_fields.append({
- "id": "stt_waiting_timeout",
- "title": "Waiting timeout (ms)",
- "description": "Duration before the server closes the microphone.",
- "type": "input",
- "value": settings["stt_waiting_timeout"]
- })
+
+ stt_fields.append(
+ {
+ "id": "stt_model_size",
+ "title": "Model Size",
+ "description": "Select the speech recognition model size",
+ "type": "select",
+ "value": settings["stt_model_size"],
+ "options": [
+ {"value": "tiny", "label": "Tiny (39M, English)"},
+ {"value": "base", "label": "Base (74M, English)"},
+ {"value": "small", "label": "Small (244M, English)"},
+ {"value": "medium", "label": "Medium (769M, English)"},
+ {"value": "large", "label": "Large (1.5B, Multilingual)"},
+ {"value": "turbo", "label": "Turbo (Multilingual)"},
+ ],
+ }
+ )
+
+ stt_fields.append(
+ {
+ "id": "stt_language",
+ "title": "Language Code",
+ "description": "Language code (e.g. en, fr, it)",
+ "type": "input",
+ "value": settings["stt_language"],
+ }
+ )
+
+ stt_fields.append(
+ {
+ "id": "stt_silence_threshold",
+ "title": "Silence threshold",
+ "description": "Silence detection threshold. Lower values are more sensitive.",
+ "type": "range",
+ "min": 0,
+ "max": 1,
+ "step": 0.01,
+ "value": settings["stt_silence_threshold"],
+ }
+ )
+
+ stt_fields.append(
+ {
+ "id": "stt_silence_duration",
+ "title": "Silence duration (ms)",
+ "description": "Duration of silence before the server considers speaking to have ended.",
+ "type": "input",
+ "value": settings["stt_silence_duration"],
+ }
+ )
+
+ stt_fields.append(
+ {
+ "id": "stt_waiting_timeout",
+ "title": "Waiting timeout (ms)",
+ "description": "Duration before the server closes the microphone.",
+ "type": "input",
+ "value": settings["stt_waiting_timeout"],
+ }
+ )
stt_section: SettingsSection = {
"title": "Speech to Text",
"description": "Voice transcription preferences and server turn detection settings.",
- "fields": stt_fields
+ "fields": stt_fields,
}
# Add the section to the result
@@ -501,7 +571,7 @@ def _get_api_key_field(settings: Settings, provider: str, title: str) -> Setting
"id": f"api_key_{provider}",
"title": title,
"type": "password",
- "value": key if key != "None" else "",
+ "value": (PASSWORD_PLACEHOLDER if key and key != "None" else ""),
}
@@ -510,12 +580,13 @@ def convert_in(settings: dict) -> Settings:
for section in settings["sections"]:
if "fields" in section:
for field in section["fields"]:
- if field["id"].endswith("_kwargs"):
- current[field["id"]] = _env_to_dict(field["value"])
- elif field["id"].startswith("api_key_"):
- current["api_keys"][field["id"]] = field["value"]
- else:
- current[field["id"]] = field["value"]
+ if field["value"] != PASSWORD_PLACEHOLDER:
+ if field["id"].endswith("_kwargs"):
+ current[field["id"]] = _env_to_dict(field["value"])
+ elif field["id"].startswith("api_key_"):
+ current["api_keys"][field["id"]] = field["value"]
+ else:
+ current[field["id"]] = field["value"]
return current
@@ -584,6 +655,7 @@ def get_embedding_model(settings: Settings | None = None) -> Embeddings:
**settings["embed_model_kwargs"],
)
+
def _read_settings_file() -> Settings | None:
if os.path.exists(SETTINGS_FILE):
content = files.read_file(SETTINGS_FILE)
@@ -605,14 +677,23 @@ def _remove_sensitive_settings(settings: Settings):
settings["auth_login"] = ""
settings["auth_password"] = ""
settings["rfc_password"] = ""
+ settings["root_password"] = ""
def _write_sensitive_settings(settings: Settings):
for key, val in settings["api_keys"].items():
dotenv.save_dotenv_value(key.upper(), val)
+
dotenv.save_dotenv_value(dotenv.KEY_AUTH_LOGIN, settings["auth_login"])
- dotenv.save_dotenv_value(dotenv.KEY_AUTH_PASSWORD, settings["auth_password"])
- dotenv.save_dotenv_value(dotenv.KEY_RFC_PASSWORD, settings["rfc_password"])
+ if settings["auth_password"]:
+ dotenv.save_dotenv_value(dotenv.KEY_AUTH_PASSWORD, settings["auth_password"])
+ if settings["rfc_password"]:
+ dotenv.save_dotenv_value(dotenv.KEY_RFC_PASSWORD, settings["rfc_password"])
+
+ if settings["root_password"]:
+ dotenv.save_dotenv_value(dotenv.KEY_ROOT_PASSWORD, settings["root_password"])
+ if settings["root_password"]:
+ dotenv.save_dotenv_value(dotenv.KEY_ROOT_PASSWORD, settings["root_password"])
def _get_default_settings() -> Settings:
@@ -633,11 +714,15 @@ def _get_default_settings() -> Settings:
api_keys={},
auth_login="",
auth_password="",
+ root_password="",
agent_prompts_subdir="default",
agent_memory_subdir="default",
agent_knowledge_subdir="custom",
- rfc_url="http://localhost:55080",
+ rfc_auto_docker=True,
+ rfc_url="localhost",
rfc_password="",
+ rfc_port_http=55080,
+ rfc_port_ssh=55022,
stt_model_size="base",
stt_language="en",
stt_silence_threshold=0.3,
@@ -663,6 +748,7 @@ def _apply_settings():
# reload whisper model if necessary
whisper.preload()
+
def _env_to_dict(data: str):
env_dict = {}
line_pattern = re.compile(r"\s*([^#][^=]*)\s*=\s*(.*)")
@@ -685,3 +771,33 @@ def _dict_to_env(data_dict):
value = f'"{value}"'
lines.append(f"{key}={value}")
return "\n".join(lines)
+
+
+def set_root_password(password: str):
+ if not runtime.is_dockerized():
+ raise Exception("root password can only be set in dockerized environments")
+ subprocess.run(["echo", "root:" + password, "|", "chpasswd"], shell=True)
+
+
+def get_runtime_config(set: Settings):
+ if runtime.is_dockerized():
+ return {
+ "code_exec_ssh_addr": "localhost",
+ "code_exec_ssh_port": 22,
+ "code_exec_http_port": 80,
+ "code_exec_ssh_user": "root",
+ }
+ else:
+ host = set["rfc_url"]
+ if "//" in host:
+ host = host.split("//")[1]
+ if ":" in host:
+ host, port = host.split(":")
+ if host.endswith("/"):
+ host = host[:-1]
+ return {
+ "code_exec_ssh_addr": host,
+ "code_exec_ssh_port": set["rfc_port_ssh"],
+ "code_exec_http_port": set["rfc_port_http"],
+ "code_exec_ssh_user": "root",
+ }
diff --git a/python/helpers/tool.py b/python/helpers/tool.py
index 8e96b790a..f0c9cba49 100644
--- a/python/helpers/tool.py
+++ b/python/helpers/tool.py
@@ -31,7 +31,7 @@ class Tool:
PrintStyle().print()
async def after_execution(self, response: Response, **kwargs):
- text = messages.truncate_text(self.agent, response.message.strip(), self.agent.config.max_tool_response_length)
+ text = response.message.strip()
await self.agent.hist_add_tool_result(self.name, text)
PrintStyle(font_color="#1B4F72", background_color="white", padding=True, bold=True).print(f"{self.agent.agent_name}: Response from tool '{self.name}'")
PrintStyle(font_color="#85C1E9").print(response.message)
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.py
index e9fc8b35d..7bf246d68 100644
--- a/python/tools/code_execution_tool.py
+++ b/python/tools/code_execution_tool.py
@@ -3,7 +3,7 @@ from dataclasses import dataclass
import shlex
import time
from python.helpers.tool import Tool, Response
-from python.helpers import files
+from python.helpers import files, rfc_exchange
from python.helpers.print_style import PrintStyle
from python.helpers.shell_local import LocalInteractiveSession
from python.helpers.shell_ssh import SSHInteractiveSession
@@ -93,12 +93,13 @@ class CodeExecution(Tool):
# initialize local or remote interactive shell insterface
if self.agent.config.code_exec_ssh_enabled:
+ pswd = self.agent.config.code_exec_ssh_pass if self.agent.config.code_exec_ssh_pass else await rfc_exchange.get_root_password()
shell = SSHInteractiveSession(
self.agent.context.log,
self.agent.config.code_exec_ssh_addr,
self.agent.config.code_exec_ssh_port,
self.agent.config.code_exec_ssh_user,
- self.agent.config.code_exec_ssh_pass,
+ pswd,
)
else:
shell = LocalInteractiveSession()
diff --git a/run_cli.py b/run_cli.py
index 390cec9e3..4045ebec3 100644
--- a/run_cli.py
+++ b/run_cli.py
@@ -3,7 +3,7 @@ import sys
import threading, time, models, os
from ansio import application_keypad, mouse_input, raw_input
from ansio.input import InputEvent, get_input_event
-from agent import AgentContext
+from agent import AgentContext, UserMessage
from python.helpers.print_style import PrintStyle
from python.helpers.files import read_file
from python.helpers import files
@@ -51,7 +51,7 @@ async def chat(context: AgentContext):
if user_input.lower() == 'e': break
# send message to agent0,
- assistant_response = await context.communicate(user_input).result()
+ assistant_response = await context.communicate(UserMessage(user_input, [])).result()
# print agent0 response
PrintStyle(font_color="white",background_color="#1D8348", bold=True, padding=True).print(f"{context.agent0.agent_name}: reponse:")
@@ -69,7 +69,7 @@ def intervention():
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
if user_input.lower() == 'e': os._exit(0) # exit the conversation when the user types 'exit'
- if user_input: context.streaming_agent.intervention_message = user_input # set intervention message if non-empty
+ if user_input: context.streaming_agent.intervention = UserMessage(user_input, []) # set intervention message if non-empty
context.paused = False # continue agent streaming
@@ -112,4 +112,5 @@ def run():
asyncio.run(chat(context))
if __name__ == "__main__":
+ print("\n\n!!! run_cli.py is now discontinued. run_ui.py serves as both UI and API endpoint !!!\n\n")
run()
\ No newline at end of file