Skip to content

Commit 43084dc

Browse files
committed
feat: supports front-end visualization of task-solving about tool-using
1 parent 5a08369 commit 43084dc

File tree

17,073 files changed

+41188
-3590789
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

17,073 files changed

+41188
-3590789
lines changed

AgentVerseIO/BaseIO.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from AgentVerseIO.input.CommandLineInput import CommandLineInput
2+
from AgentVerseIO.input.base import BaseInput
3+
from AgentVerseIO.output.CommandLineOutput import CommandLineOutput
4+
from AgentVerseIO.output.base import BaseOutput
5+
6+
7+
class AgentVerseIO:
8+
def __init__(self, input: BaseInput, output: BaseOutput) -> None:
9+
if input is None:
10+
self.Input = CommandLineInput()
11+
else:
12+
if not isinstance(input, BaseInput):
13+
raise TypeError("input must be a BaseInput instance")
14+
15+
self.Input = input
16+
17+
if output is None:
18+
self.Output = CommandLineOutput()
19+
else:
20+
if not isinstance(output, BaseOutput):
21+
raise TypeError("output must be a BaseOutput instance")
22+
23+
self.Output = output
24+
25+
def set_logger(self, logger):
26+
self.logger = logger
27+
self.Input.set_logger(logger)
28+
self.Output.set_logger(logger)
29+
30+
def close(self):
31+
self.Input.close()
32+
self.Output.close()

AgentVerseIO/exception.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
class AgentVerseIOError(Exception):
2+
"""Base class for exceptions in this module."""
3+
4+
pass
5+
6+
7+
class AgentVerseIOInterruptError(AgentVerseIOError):
8+
"""Exception raised for errors in the input.
9+
10+
Attributes:
11+
message -- explanation of the error
12+
"""
13+
14+
def __init__(self, message="AgentVerse IO Interrupt!"):
15+
self.message = message
16+
super().__init__(self.message)
17+
18+
19+
class AgentVerseIOTimeoutError(AgentVerseIOError):
20+
"""Exception raised for errors in the input.
21+
22+
Attributes:
23+
message -- explanation of the error
24+
"""
25+
26+
def __init__(self, message="AgentVerse IO Timeout!"):
27+
self.message = message
28+
super().__init__(self.message)
29+
30+
31+
class AgentVerseIOCloseError(AgentVerseIOError):
32+
"""Exception raised for errors in the input.
33+
34+
Attributes:
35+
message -- explanation of the error
36+
"""
37+
38+
def __init__(self, message="AgentVerse IO Close!"):
39+
self.message = message
40+
super().__init__(self.message)
41+
42+
43+
class AgentVerseIOWebSocketError(AgentVerseIOError):
44+
"""Exception raised for errors in the input.
45+
46+
Attributes:
47+
message -- explanation of the error
48+
"""
49+
50+
def __init__(self, message="AgentVerse IO WebSocket Error!"):
51+
self.message = message
52+
super().__init__(self.message)
53+
54+
55+
class AgentVerseIOWebSocketTimeoutError(AgentVerseIOWebSocketError):
56+
"""Exception raised for errors in the input.
57+
58+
Attributes:
59+
message -- explanation of the error
60+
"""
61+
62+
def __init__(self, message="AgentVerse IO WebSocket Timeout!"):
63+
self.message = message
64+
super().__init__(self.message)
65+
66+
67+
class AgentVerseIOWebSocketDisconnectError(AgentVerseIOWebSocketError):
68+
"""Exception raised for errors in the input.
69+
70+
Attributes:
71+
message -- explanation of the error
72+
"""
73+
74+
def __init__(self, message="AgentVerse IO WebSocket Disconnect!"):
75+
self.message = message
76+
super().__init__(self.message)
77+
78+
79+
class AgentVerseIOWebSocketConnectError(AgentVerseIOWebSocketError):
80+
"""Exception raised for errors in the input.
81+
82+
Attributes:
83+
message -- explanation of the error
84+
"""
85+
86+
def __init__(self, message="AgentVerse IO WebSocket Connect Error!"):
87+
self.message = message
88+
super().__init__(self.message)
89+
90+
91+
class AgentVerseIOWebSocketCloseError(AgentVerseIOWebSocketError):
92+
"""Exception raised for errors in the input.
93+
94+
Attributes:
95+
message -- explanation of the error
96+
"""
97+
98+
def __init__(self, message="AgentVerse IO WebSocket Close!"):
99+
self.message = message
100+
super().__init__(self.message)
101+
102+
103+
class AgentVerseIOWebSocketSendError(AgentVerseIOWebSocketError):
104+
"""Exception raised for errors in the input.
105+
106+
Attributes:
107+
message -- explanation of the error
108+
"""
109+
110+
def __init__(self, message="AgentVerse IO WebSocket Send Error!"):
111+
self.message = message
112+
super().__init__(self.message)
113+
114+
115+
class AgentVerseIOWebSocketReceiveError(AgentVerseIOWebSocketError):
116+
"""Exception raised for errors in the input.
117+
118+
Attributes:
119+
message -- explanation of the error
120+
"""
121+
122+
def __init__(self, message="AgentVerse IO WebSocket Receive Error!"):
123+
self.message = message
124+
super().__init__(self.message)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import asyncio
2+
import functools
3+
import time
4+
from colorama import Fore
5+
from AgentVerseIO.exception import AgentVerseIOCloseError, AgentVerseIOTimeoutError
6+
from AgentVerseIO.input.base import BaseInput
7+
from inputimeout import inputimeout, TimeoutOccurred
8+
import math
9+
10+
11+
def timer(func):
12+
@functools.wraps(func)
13+
def wrapper(*args, **kwargs):
14+
try:
15+
start_time = time.time()
16+
result = func(*args, **kwargs)
17+
end_time = time.time()
18+
except:
19+
pass
20+
21+
return wrapper
22+
23+
24+
class CommandLineInput(BaseInput):
25+
def __init__(self, do_interrupt: bool = False, max_wait_seconds: int = 600):
26+
super().__init__(do_interrupt, max_wait_seconds)
27+
28+
async def run(self, input_data):
29+
if self.do_interrupt:
30+
data = await self.interrupt(input_data)
31+
else:
32+
data = input_data
33+
return data
34+
35+
async def get_each_input(self, key, value, res, timeout):
36+
self.logger.typewriter_log(
37+
f"Now, ASK For {key}, Origin Input: {value}", Fore.RED, f""
38+
)
39+
self.logger.typewriter_log(
40+
f"Now, you can modify the current field by entering some information, and then press 'Enter' to continue, if you want to keep the original input, please enter '-1' and then press 'Enter':",
41+
Fore.GREEN,
42+
)
43+
temp = inputimeout(
44+
prompt=f"You have {timeout} seconds to input:\n", timeout=timeout
45+
)
46+
if temp == "-1":
47+
return value
48+
else:
49+
return temp
50+
51+
async def get_input(self, origin_data):
52+
self.logger.typewriter_log(
53+
"Next, you can start modifying the original input by typing 'Y/y/yes' or skip this step by typing 'N/n/no' and then press 'Enter' to continue the loop:",
54+
Fore.RED,
55+
)
56+
update = inputimeout(
57+
prompt=f"You have to make a decision within 60 seconds:\n", timeout=60
58+
)
59+
res = {"args": {}}
60+
if update in ["y", "Y", "yes"]:
61+
execute_time = self.max_wait_seconds
62+
if isinstance(origin_data, dict):
63+
args = origin_data.get("args", "")
64+
self.logger.typewriter_log(
65+
f"Next, you will have a total of {self.max_wait_seconds} seconds to modify each option:",
66+
Fore.RED,
67+
)
68+
for key, value in args.items():
69+
if key == "done":
70+
res[key] = False
71+
continue
72+
start_time = time.time()
73+
res["args"][key] = await self.get_each_input(
74+
key, value, res, execute_time
75+
)
76+
end_time = time.time()
77+
execute_time = math.floor(execute_time - (end_time - start_time))
78+
self.logger.info(f"modify the input, receive the data: {res}")
79+
else:
80+
res = origin_data
81+
self.logger.info("skip this step")
82+
self.logger.info("continue the loop")
83+
res["done"] = True
84+
return res
85+
86+
async def interrupt(self, input_data):
87+
try:
88+
data = await self.get_input(input_data)
89+
return data
90+
except TimeoutOccurred:
91+
self.logger.error(f"Waiting timemout, close connection!")
92+
raise AgentVerseIOTimeoutError("timeout!")
93+
94+
def close(self):
95+
raise AgentVerseIOCloseError("close connection!")

AgentVerseIO/input/HttpInput.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from AgentVerseIO.input.base import BaseInput
2+
3+
4+
class HttpInput(BaseInput):
5+
def __init__(self):
6+
super().__init__()
7+
8+
def run(self):
9+
raise NotImplementedError

AgentVerseIO/input/RestApiInput.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from AgentVerseIO.input.base import BaseInput
2+
3+
4+
class RestApiInput(BaseInput):
5+
def __init__(self):
6+
super().__init__()
7+
8+
def run(self):
9+
raise NotImplementedError
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import asyncio
2+
import json
3+
4+
from fastapi import WebSocket, WebSocketDisconnect
5+
6+
from AgentVerseIO.exception import (
7+
AgentVerseIOWebSocketDisconnectError,
8+
AgentVerseIOWebSocketTimeoutError,
9+
)
10+
from AgentVerseIO.input.base import BaseInput
11+
from AgentVerseServer.loggers.logs import Logger
12+
from AgentVerseServer.response_body import WebsocketResponseBody
13+
14+
15+
class WebSocketInput(BaseInput):
16+
def __init__(
17+
self,
18+
websocket: WebSocket,
19+
do_interrupt: bool = False,
20+
max_wait_seconds: int = 600,
21+
):
22+
super().__init__(do_interrupt, max_wait_seconds)
23+
self.websocket = websocket
24+
25+
def set_logger(self, logger: Logger):
26+
self.logger = logger
27+
28+
def set_interrupt(self, do_interrupt: bool = True):
29+
self.do_interrupt = do_interrupt
30+
31+
async def interrupt(self):
32+
wait = 0
33+
while wait < self.max_wait_seconds:
34+
print(
35+
f"\r waiting for {wait} second, remaining {self.max_wait_seconds - wait} second",
36+
end="",
37+
)
38+
try:
39+
data = await asyncio.wait_for(self.auto_receive(), 1)
40+
if isinstance(data, dict):
41+
data_type = data.get("type", None)
42+
# if data_type == "ping":
43+
# await self.websocket.send_json({"type": "pong"})
44+
# continue
45+
if data_type == "data":
46+
self.logger.info(f"Receiving data change request...")
47+
self.logger.info(f"Received :{data}")
48+
wait = 0
49+
return data
50+
else:
51+
pass
52+
except asyncio.TimeoutError:
53+
wait += 1
54+
self.logger.error(f"Wait timeout, close.")
55+
self.websocket.send_text(
56+
WebsocketResponseBody(
57+
data=None, status="failed", message="Wait timeout, close."
58+
).to_text()
59+
)
60+
raise AgentVerseIOWebSocketTimeoutError
61+
62+
async def auto_receive(self):
63+
data = await self.websocket.receive_json()
64+
return data
65+
66+
async def run(self, input):
67+
if self.do_interrupt:
68+
data = await self.interrupt()
69+
return data
70+
else:
71+
return input

AgentVerseIO/input/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from abc import ABCMeta, abstractmethod
2+
import asyncio
3+
4+
from AgentVerseIO.exception import AgentVerseIOCloseError
5+
6+
7+
class BaseInput(metaclass=ABCMeta):
8+
def __init__(self, do_interrupt: bool = False, max_wait_seconds: int = 600):
9+
self.do_interrupt = do_interrupt
10+
if self.do_interrupt:
11+
self.max_wait_seconds = max_wait_seconds
12+
13+
def set_wait(self, do_interrupt: bool = True):
14+
self.do_interrupt = do_interrupt
15+
16+
def set_logger(self, logger):
17+
self.logger = logger
18+
19+
async def interrupt(self):
20+
raise NotImplementedError
21+
22+
def run(self):
23+
raise NotImplementedError
24+
25+
def close(self):
26+
# raise AgentVerseIOCloseError("close connection!")
27+
pass
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from AgentVerseIO.exception import AgentVerseIOCloseError
2+
from AgentVerseIO.output.base import BaseOutput
3+
4+
5+
class CommandLineOutput(BaseOutput):
6+
def __init__(self):
7+
super().__init__()
8+
9+
async def run(self, output):
10+
pass
11+
12+
def close(self):
13+
raise AgentVerseIOCloseError("close connection!")

AgentVerseIO/output/HttpOutput.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from AgentVerseIO.output.base import BaseOutput
2+
3+
4+
class HttpOutput(BaseOutput):
5+
def __init__(self):
6+
super().__init__()
7+
8+
def run(self, output):
9+
print(output)

0 commit comments

Comments
 (0)