Spaces:
Paused
Paused
frdel commited on
Commit ·
d471359
1
Parent(s): 7e6a29d
threading, readline fixes
Browse files- python/helpers/defer.py +7 -3
- python/helpers/timed_input.py +2 -1
- run_cli.py +4 -3
python/helpers/defer.py
CHANGED
|
@@ -5,15 +5,16 @@ from concurrent.futures import Future
|
|
| 5 |
class DeferredTask:
|
| 6 |
def __init__(self, func, *args, **kwargs):
|
| 7 |
self._loop = asyncio.new_event_loop()
|
| 8 |
-
# self._thread = None
|
| 9 |
self._task = None
|
| 10 |
self._future = Future()
|
|
|
|
| 11 |
self._start_task(func, *args, **kwargs)
|
| 12 |
|
| 13 |
def _start_task(self, func, *args, **kwargs):
|
| 14 |
def run_in_thread(loop, func, args, kwargs):
|
| 15 |
asyncio.set_event_loop(loop)
|
| 16 |
self._task = loop.create_task(self._run(func, *args, **kwargs))
|
|
|
|
| 17 |
loop.run_forever()
|
| 18 |
|
| 19 |
self._thread = threading.Thread(target=run_in_thread, args=(self._loop, func, args, kwargs))
|
|
@@ -32,15 +33,18 @@ class DeferredTask:
|
|
| 32 |
return self._future.done()
|
| 33 |
|
| 34 |
async def result(self, timeout=None):
|
| 35 |
-
if self.
|
| 36 |
raise RuntimeError("Task was not initialized properly.")
|
| 37 |
-
|
| 38 |
try:
|
| 39 |
return await asyncio.wait_for(asyncio.wrap_future(self._future), timeout)
|
| 40 |
except asyncio.TimeoutError:
|
| 41 |
raise TimeoutError("The task did not complete within the specified timeout.")
|
| 42 |
|
| 43 |
def result_sync(self, timeout=None):
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
return self._future.result(timeout)
|
| 46 |
except TimeoutError:
|
|
|
|
| 5 |
class DeferredTask:
|
| 6 |
def __init__(self, func, *args, **kwargs):
|
| 7 |
self._loop = asyncio.new_event_loop()
|
|
|
|
| 8 |
self._task = None
|
| 9 |
self._future = Future()
|
| 10 |
+
self._task_initialized = threading.Event() # Event to signal task initialization
|
| 11 |
self._start_task(func, *args, **kwargs)
|
| 12 |
|
| 13 |
def _start_task(self, func, *args, **kwargs):
|
| 14 |
def run_in_thread(loop, func, args, kwargs):
|
| 15 |
asyncio.set_event_loop(loop)
|
| 16 |
self._task = loop.create_task(self._run(func, *args, **kwargs))
|
| 17 |
+
self._task_initialized.set() # Signal that the task has been initialized
|
| 18 |
loop.run_forever()
|
| 19 |
|
| 20 |
self._thread = threading.Thread(target=run_in_thread, args=(self._loop, func, args, kwargs))
|
|
|
|
| 33 |
return self._future.done()
|
| 34 |
|
| 35 |
async def result(self, timeout=None):
|
| 36 |
+
if not self._task_initialized.wait(timeout): # Wait until the task is initialized
|
| 37 |
raise RuntimeError("Task was not initialized properly.")
|
| 38 |
+
|
| 39 |
try:
|
| 40 |
return await asyncio.wait_for(asyncio.wrap_future(self._future), timeout)
|
| 41 |
except asyncio.TimeoutError:
|
| 42 |
raise TimeoutError("The task did not complete within the specified timeout.")
|
| 43 |
|
| 44 |
def result_sync(self, timeout=None):
|
| 45 |
+
if not self._task_initialized.wait(timeout): # Wait until the task is initialized
|
| 46 |
+
raise RuntimeError("Task was not initialized properly.")
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
return self._future.result(timeout)
|
| 50 |
except TimeoutError:
|
python/helpers/timed_input.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
|
|
| 1 |
from inputimeout import inputimeout, TimeoutOccurred
|
| 2 |
|
| 3 |
def timeout_input(prompt, timeout=10):
|
| 4 |
try:
|
| 5 |
-
import readline
|
| 6 |
user_input = inputimeout(prompt=prompt, timeout=timeout)
|
| 7 |
return user_input
|
| 8 |
except TimeoutOccurred:
|
|
|
|
| 1 |
+
import sys
|
| 2 |
from inputimeout import inputimeout, TimeoutOccurred
|
| 3 |
|
| 4 |
def timeout_input(prompt, timeout=10):
|
| 5 |
try:
|
| 6 |
+
if sys.platform != "win32": import readline
|
| 7 |
user_input = inputimeout(prompt=prompt, timeout=timeout)
|
| 8 |
return user_input
|
| 9 |
except TimeoutOccurred:
|
run_cli.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import threading, time, models, os
|
| 3 |
from ansio import application_keypad, mouse_input, raw_input
|
| 4 |
from ansio.input import InputEvent, get_input_event
|
|
@@ -24,13 +25,13 @@ async def chat(context: AgentContext):
|
|
| 24 |
timeout = context.agent0.get_data("timeout") # how long the agent is willing to wait
|
| 25 |
if not timeout: # if agent wants to wait for user input forever
|
| 26 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('e' to leave):")
|
| 27 |
-
import readline # this fixes arrow keys in terminal
|
| 28 |
user_input = input("> ")
|
| 29 |
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
|
| 30 |
|
| 31 |
else: # otherwise wait for user input with a timeout
|
| 32 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'e' to leave):")
|
| 33 |
-
import readline # this fixes arrow keys in terminal
|
| 34 |
# user_input = timed_input("> ", timeout=timeout)
|
| 35 |
user_input = timeout_input("> ", timeout=timeout)
|
| 36 |
|
|
@@ -62,7 +63,7 @@ def intervention():
|
|
| 62 |
context.paused = True # stop agent streaming
|
| 63 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('e' to leave, empty to continue):")
|
| 64 |
|
| 65 |
-
import readline # this fixes arrow keys in terminal
|
| 66 |
user_input = input("> ").strip()
|
| 67 |
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
|
| 68 |
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import sys
|
| 3 |
import threading, time, models, os
|
| 4 |
from ansio import application_keypad, mouse_input, raw_input
|
| 5 |
from ansio.input import InputEvent, get_input_event
|
|
|
|
| 25 |
timeout = context.agent0.get_data("timeout") # how long the agent is willing to wait
|
| 26 |
if not timeout: # if agent wants to wait for user input forever
|
| 27 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ('e' to leave):")
|
| 28 |
+
if sys.platform != "win32": import readline # this fixes arrow keys in terminal
|
| 29 |
user_input = input("> ")
|
| 30 |
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
|
| 31 |
|
| 32 |
else: # otherwise wait for user input with a timeout
|
| 33 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User message ({timeout}s timeout, 'w' to wait, 'e' to leave):")
|
| 34 |
+
if sys.platform != "win32": import readline # this fixes arrow keys in terminal
|
| 35 |
# user_input = timed_input("> ", timeout=timeout)
|
| 36 |
user_input = timeout_input("> ", timeout=timeout)
|
| 37 |
|
|
|
|
| 63 |
context.paused = True # stop agent streaming
|
| 64 |
PrintStyle(background_color="#6C3483", font_color="white", bold=True, padding=True).print(f"User intervention ('e' to leave, empty to continue):")
|
| 65 |
|
| 66 |
+
if sys.platform != "win32": import readline # this fixes arrow keys in terminal
|
| 67 |
user_input = input("> ").strip()
|
| 68 |
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
|
| 69 |
|