-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
run_cli.py
116 lines (90 loc) · 5.04 KB
/
run_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
from agent import AgentContext, UserMessage
from python.helpers.print_style import PrintStyle
from python.helpers.files import read_file
from python.helpers import files
import python.helpers.timed_input as timed_input
from initialize import initialize
from python.helpers.dotenv import load_dotenv
context: AgentContext = None # type: ignore
input_lock = threading.Lock()
# Main conversation loop
async def chat(context: AgentContext):
# start the conversation loop
while True:
# ask user for message
with input_lock:
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):")
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):")
if sys.platform != "win32": import readline # this fixes arrow keys in terminal
# user_input = timed_input("> ", timeout=timeout)
user_input = timeout_input("> ", timeout=timeout)
if not user_input:
user_input = context.agent0.read_prompt("fw.msg_timeout.md")
PrintStyle(font_color="white", padding=False).stream(f"{user_input}")
else:
user_input = user_input.strip()
if user_input.lower()=="w": # the user needs more time
user_input = input("> ").strip()
PrintStyle(font_color="white", padding=False, log_only=True).print(f"> {user_input}")
# exit the conversation when the user types 'exit'
if user_input.lower() == 'e': break
# send message to agent0,
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:")
PrintStyle(font_color="white").print(f"{assistant_response}")
# User intervention during agent streaming
def intervention():
if context.streaming_agent and not context.paused:
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):")
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}")
if user_input.lower() == 'e': os._exit(0) # exit the conversation when the user types 'exit'
if user_input: context.streaming_agent.intervention = UserMessage(user_input, []) # set intervention message if non-empty
context.paused = False # continue agent streaming
# Capture keyboard input to trigger user intervention
def capture_keys():
global input_lock
intervent=False
while True:
if intervent: intervention()
intervent = False
time.sleep(0.1)
if context.streaming_agent:
# with raw_input, application_keypad, mouse_input:
with input_lock, raw_input, application_keypad:
event: InputEvent | None = get_input_event(timeout=0.1)
if event and (event.shortcut.isalpha() or event.shortcut.isspace()):
intervent=True
continue
# User input with timeout
def timeout_input(prompt, timeout=10):
return timed_input.timeout_input(prompt=prompt, timeout=timeout)
def run():
global context
PrintStyle.standard("Initializing framework...")
#load env vars
load_dotenv()
# initialize context
config = initialize()
context = AgentContext(config)
# Start the key capture thread for user intervention during agent streaming
threading.Thread(target=capture_keys, daemon=True).start()
#start the chat
asyncio.run(chat(context))
if __name__ == "__main__":
PrintStyle.standard("\n\n!!! run_cli.py is now discontinued. run_ui.py serves as both UI and API endpoint !!!\n\n")
run()