Skip to content

Agent Zero AI framework

Docker image, code exe, callstack

frdel763ec9e22819
diff --git a/agent.py b/agent.py
index 672a42cf7..a086f7534 100644
--- a/agent.py
+++ b/agent.py
@@ -23,9 +23,14 @@ class AgentContext:
_counter: int = 0
def __init__(
- self, config: "AgentConfig", id: str | None = None, name: str | None = None, agent0: "Agent|None" = None,
+ self,
+ config: "AgentConfig",
+ id: str | None = None,
+ name: str | None = None,
+ agent0: "Agent|None" = None,
log: Log.Log | None = None,
- paused: bool = False, streaming_agent: "Agent|None" = None,
+ paused: bool = False,
+ streaming_agent: "Agent|None" = None,
):
# build context
self.id = id or str(uuid.uuid4())
@@ -38,7 +43,7 @@ class AgentContext:
self.process: DeferredTask | None = None
AgentContext._counter += 1
self.no = AgentContext._counter
-
+
self._contexts[self.id] = self
@staticmethod
@@ -69,12 +74,12 @@ class AgentContext:
def communicate(self, msg: str, broadcast_level: int = 1):
self.paused = False # unpause if paused
- if self.process and self.process.is_alive():
- if self.streaming_agent:
- current_agent = self.streaming_agent
- else:
- current_agent = self.agent0
+ if self.streaming_agent:
+ current_agent = self.streaming_agent
+ else:
+ current_agent = self.agent0
+ if self.process and self.process.is_alive():
# set intervention messages to agent(s):
intervention_agent = current_agent
while intervention_agent and broadcast_level != 0:
@@ -82,10 +87,30 @@ class AgentContext:
broadcast_level -= 1
intervention_agent = intervention_agent.data.get("superior", None)
else:
- self.process = DeferredTask(self.agent0.monologue, msg)
+
+ # self.process = DeferredTask(current_agent.monologue, msg)
+ self.process = DeferredTask(self._process_chain, current_agent, msg)
return self.process
+ # this wrapper ensures that superior agents are called back if the chat was loaded from file and original callstack is gone
+ async def _process_chain(self, agent: 'Agent', msg: str, user=True):
+ try:
+ msg_template = (
+ agent.read_prompt("fw.user_message.md", message=msg)
+ if user
+ else agent.read_prompt(
+ "fw.tool_response.md",
+ tool_name="call_subordinate",
+ tool_response=msg,
+ )
+ )
+ response = await agent.monologue(msg_template)
+ superior = agent.data.get("superior", None)
+ if superior:
+ await self._process_chain(superior, response, False)
+ except Exception as e:
+ agent.handle_critical_exception(e)
@dataclass
class AgentConfig:
@@ -220,9 +245,7 @@ class Agent:
await self.call_extensions("monologue_start", loop_data=loop_data)
printer = PrintStyle(italic=True, font_color="#b3ffd9", padding=False)
- user_message = self.read_prompt(
- "fw.user_message.md", message=loop_data.message
- )
+ user_message = loop_data.message
await self.append_message(user_message, human=True)
# let the agent run message loop until he stops it with a response tool
diff --git a/docker/exe/Dockerfile b/docker/exe/Dockerfile
index 6afc9f2d7..e51788e11 100644
--- a/docker/exe/Dockerfile
+++ b/docker/exe/Dockerfile
@@ -13,7 +13,12 @@ RUN apt-get update && apt-get install -y \
npm \
openssh-server \
sudo \
- && rm -rf /var/lib/apt/lists/*
+ curl \
+ wget \
+ git
+
+# Cleanup package list
+RUN rm -rf /var/lib/apt/lists/*
# Set up SSH
RUN mkdir /var/run/sshd && \
@@ -24,16 +29,21 @@ RUN mkdir /var/run/sshd && \
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
-# Copy initial .bashrc with virtual environment activation to a temporary location
-COPY .bashrc /etc/skel/.bashrc
-
# Copy the script to ensure .bashrc is in the root directory
COPY initialize.sh /usr/local/bin/initialize.sh
RUN chmod +x /usr/local/bin/initialize.sh
+# Copy contents of filesystem directory to /
+COPY ./fs/ /
+
# Ensure the virtual environment and pip setup
RUN $VIRTUAL_ENV/bin/pip install --upgrade pip
+# Install additional python packages
+RUN $VIRTUAL_ENV/bin/pip install \
+ ipython \
+ requests
+
# Expose SSH port
EXPOSE 22
diff --git a/docker/exe/build.txt b/docker/exe/build.txt
index 37d76f0cb..647037929 100644
--- a/docker/exe/build.txt
+++ b/docker/exe/build.txt
@@ -1 +1,2 @@
+docker login
docker buildx build --platform linux/amd64,linux/arm64 -t frdel/agent-zero-exe:latest --push .
\ No newline at end of file
diff --git a/docker/exe/fs/exe/node_eval.js b/docker/exe/fs/exe/node_eval.js
new file mode 100644
index 000000000..18bb773c1
--- /dev/null
+++ b/docker/exe/fs/exe/node_eval.js
@@ -0,0 +1,41 @@
+#!/usr/bin/env node
+
+const vm = require('vm');
+const path = require('path');
+
+// Create a comprehensive context with all important global objects
+const context = vm.createContext({
+ ...global,
+ require: require,
+ __filename: path.join(process.cwd(), 'eval.js'),
+ __dirname: process.cwd(),
+ module: { exports: {} },
+ exports: module.exports,
+ console: console,
+ process: process,
+ Buffer: Buffer,
+ setTimeout: setTimeout,
+ setInterval: setInterval,
+ setImmediate: setImmediate,
+ clearTimeout: clearTimeout,
+ clearInterval: clearInterval,
+ clearImmediate: clearImmediate
+});
+
+const code = process.argv[2];
+const wrappedCode = `
+ (async function() {
+ try {
+ const __result__ = await eval(${JSON.stringify(code)});
+ if (__result__ !== undefined) console.log('Out[1]:', __result__);
+ } catch (error) {
+ console.error(error);
+ }
+ })();
+`;
+
+vm.runInContext(wrappedCode, context, {
+ filename: 'eval.js',
+ lineOffset: -2,
+ columnOffset: 0,
+}).catch(console.error);
\ No newline at end of file
diff --git a/docker/exe/.bashrc b/docker/exe/fs/root/.bashrc
similarity index 100%
rename from docker/exe/.bashrc
rename to docker/exe/fs/root/.bashrc
diff --git a/docker/exe/fs/root/.profile b/docker/exe/fs/root/.profile
new file mode 100644
index 000000000..88a273c5f
--- /dev/null
+++ b/docker/exe/fs/root/.profile
@@ -0,0 +1,9 @@
+# .bashrc
+
+# Source global definitions
+if [ -f /etc/bashrc ]; then
+ . /etc/bashrc
+fi
+
+# Activate the virtual environment
+source /opt/venv/bin/activate
diff --git a/docker/exe/initialize.sh b/docker/exe/initialize.sh
index a0a377698..fdb10bc4a 100644
--- a/docker/exe/initialize.sh
+++ b/docker/exe/initialize.sh
@@ -1,18 +1,18 @@
#!/bin/bash
-# Ensure .bashrc is in the root directory
-if [ ! -f /root/.bashrc ]; then
- cp /etc/skel/.bashrc /root/.bashrc
- chmod 444 /root/.bashrc
-fi
+# # Ensure .bashrc is in the root directory
+# if [ ! -f /root/.bashrc ]; then
+# cp /etc/skel/.bashrc /root/.bashrc
+# chmod 444 /root/.bashrc
+# fi
-# Ensure .profile is in the root directory
-if [ ! -f /root/.profile ]; then
- cp /etc/skel/.bashrc /root/.profile
- chmod 444 /root/.profile
-fi
+# # Ensure .profile is in the root directory
+# if [ ! -f /root/.profile ]; then
+# cp /etc/skel/.bashrc /root/.profile
+# chmod 444 /root/.profile
+# fi
apt-get update
# Start SSH service
-exec /usr/sbin/sshd -D
+exec /usr/sbin/sshd -D
\ No newline at end of file
diff --git a/python/helpers/persist_chat.py b/python/helpers/persist_chat.py
index 05a0ed2c1..b170e7935 100644
--- a/python/helpers/persist_chat.py
+++ b/python/helpers/persist_chat.py
@@ -102,7 +102,7 @@ def _deserialize_context(data):
context = AgentContext(
config=config,
- id=data.get("id", None),
+ # id=data.get("id", None), #get new id
name=data.get("name", None),
log=log,
paused=False,
@@ -143,6 +143,7 @@ def _deserialize_agents(
if prev:
prev.set_data("subordinate", current)
current.set_data("superior", prev)
+ prev = current
return zero or Agent(0, config, context)
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.py
index d5847c2ba..f26c3671b 100644
--- a/python/tools/code_execution_tool.py
+++ b/python/tools/code_execution_tool.py
@@ -112,12 +112,12 @@ class CodeExecution(Tool):
async def execute_python_code(self, code: str, reset: bool = False):
escaped_code = shlex.quote(code)
- command = f"python3 -c {escaped_code}"
+ command = f"ipython -c {escaped_code}"
return await self.terminal_session(command, reset)
async def execute_nodejs_code(self, code: str, reset: bool = False):
escaped_code = shlex.quote(code)
- command = f"node -e {escaped_code}"
+ command = f"node /exe/node_eval.js {escaped_code}"
return await self.terminal_session(command, reset)
async def execute_terminal_command(self, command: str, reset: bool = False):