threading, readline fixes
frdel committed
Sep 5, 2024 at 01:00 UTC
a4937de68a38802d489d829fa755bbd9ffea3030
3 files changed
+13
-7
python/helpers/defer.py
+7
-3
@@ -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
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))
@@ -32,15 +33,18 @@ class DeferredTask:
33
return self._future.done()
34
35
async def result(self, timeout=None):
35
- if self._task is None:
36
+ if not self._task_initialized.wait(timeout): # Wait until the task is initialized
37
raise RuntimeError("Task was not initialized properly.")
37
-
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
+2
-1
@@ -1,8 +1,9 @@
1
+import sys
2
from inputimeout import inputimeout, TimeoutOccurred
3
4
def timeout_input(prompt, timeout=10):
5
try:
5
- import readline
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
+4
-3
@@ -1,4 +1,5 @@
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
@@ -24,13 +25,13 @@ async def chat(context: AgentContext):
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):")
27
- import readline # this fixes arrow keys in terminal
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):")
33
- import readline # this fixes arrow keys in terminal
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
@@ -62,7 +63,7 @@ def intervention():
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
65
- import readline # this fixes arrow keys in terminal
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