Skip to content

Agent Zero AI framework

threading, readline fixes

frdela4937de68a38
diff --git a/python/helpers/defer.py b/python/helpers/defer.py
index 2d26e77a5..274e00330 100644
--- a/python/helpers/defer.py
+++ b/python/helpers/defer.py
@@ -5,15 +5,16 @@ from concurrent.futures import Future
class DeferredTask:
def __init__(self, func, *args, **kwargs):
self._loop = asyncio.new_event_loop()
- # self._thread = None
self._task = None
self._future = Future()
+ self._task_initialized = threading.Event() # Event to signal task initialization
self._start_task(func, *args, **kwargs)
def _start_task(self, func, *args, **kwargs):
def run_in_thread(loop, func, args, kwargs):
asyncio.set_event_loop(loop)
self._task = loop.create_task(self._run(func, *args, **kwargs))
+ self._task_initialized.set() # Signal that the task has been initialized
loop.run_forever()
self._thread = threading.Thread(target=run_in_thread, args=(self._loop, func, args, kwargs))
@@ -32,15 +33,18 @@ class DeferredTask:
return self._future.done()
async def result(self, timeout=None):
- if self._task is None:
+ if not self._task_initialized.wait(timeout): # Wait until the task is initialized
raise RuntimeError("Task was not initialized properly.")
-
+
try:
return await asyncio.wait_for(asyncio.wrap_future(self._future), timeout)
except asyncio.TimeoutError:
raise TimeoutError("The task did not complete within the specified timeout.")
def result_sync(self, timeout=None):
+ if not self._task_initialized.wait(timeout): # Wait until the task is initialized
+ raise RuntimeError("Task was not initialized properly.")
+
try:
return self._future.result(timeout)
except TimeoutError:
diff --git a/python/helpers/timed_input.py b/python/helpers/timed_input.py
index 64c30d33b..923002dae 100644
--- a/python/helpers/timed_input.py
+++ b/python/helpers/timed_input.py
@@ -1,8 +1,9 @@
+import sys
from inputimeout import inputimeout, TimeoutOccurred
def timeout_input(prompt, timeout=10):
try:
- import readline
+ if sys.platform != "win32": import readline
user_input = inputimeout(prompt=prompt, timeout=timeout)
return user_input
except TimeoutOccurred:
diff --git a/run_cli.py b/run_cli.py
index e2f4406a0..6e50663d2 100644
--- a/run_cli.py
+++ b/run_cli.py
@@ -1,4 +1,5 @@
import asyncio
+import sys
import threading, time, models, os
from ansio import application_keypad, mouse_input, raw_input
from ansio.input import InputEvent, get_input_event
@@ -24,13 +25,13 @@ async def chat(context: AgentContext):
timeout = context.agent0.get_data("timeout") # how long the agent is willing to wait
if not timeout: # if agent wants to wait for user input forever
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('e' to leave):")
- import readline # this fixes arrow keys in terminal
+ if sys.platform != "win32": import readline # this fixes arrow keys in terminal
user_input = input("> ")
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
else: # otherwise wait for user input with a timeout
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'e' to leave):")
- import readline # this fixes arrow keys in terminal
+ if sys.platform != "win32": import readline # this fixes arrow keys in terminal
# user_input = timed_input("> ", timeout=timeout)
user_input = timeout_input("> ", timeout=timeout)
@@ -62,7 +63,7 @@ def intervention():
context.paused = True # stop agent streaming
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('e' to leave, empty to continue):")
- import readline # this fixes arrow keys in terminal
+ if sys.platform != "win32": import readline # this fixes arrow keys in terminal
user_input = input("> ").strip()
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")