diff --git a/src/agentscope/web/studio/constants.py b/src/agentscope/web/studio/constants.py new file mode 100644 index 000000000..0d593113b --- /dev/null +++ b/src/agentscope/web/studio/constants.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +""" Some constants used in the AS studio""" + +_SPEAK = "**speak**" diff --git a/src/agentscope/web/studio/studio.py b/src/agentscope/web/studio/studio.py index 3fa4b34b3..58889dcf1 100644 --- a/src/agentscope/web/studio/studio.py +++ b/src/agentscope/web/studio/studio.py @@ -30,7 +30,9 @@ audio2text, send_reset_msg, thread_local_data, + cycle_dots, ) +from agentscope.web.studio.constants import _SPEAK MAX_NUM_DISPLAY_MSG = 20 FAIL_COUNT_DOWN = 30 @@ -42,24 +44,34 @@ def init_uid_list() -> list: glb_history_dict = defaultdict(init_uid_list) +glb_doing_signal_dict = defaultdict(init_uid_list) glb_signed_user = [] def reset_glb_var(uid: str) -> None: """Reset global variables for a given user ID.""" global glb_history_dict + global glb_doing_signal_dict glb_history_dict[uid] = init_uid_list() + glb_doing_signal_dict[uid] = init_uid_list() def get_chat(uid: str) -> list[list]: """Retrieve chat messages for a given user ID.""" uid = check_uuid(uid) global glb_history_dict + global glb_doing_signal_dict + line = get_chat_msg(uid=uid) # TODO: Optimize the display effect, currently there is a problem of # output display jumping if line: - glb_history_dict[uid] += [line] + if line[1] and line[1]["text"] == _SPEAK: + line[1]["text"] = "" + glb_doing_signal_dict[uid] = line + else: + glb_history_dict[uid] += [line] + glb_doing_signal_dict[uid] = [] dial_msg = [] for line in glb_history_dict[uid]: _, msg = line @@ -68,6 +80,14 @@ def get_chat(uid: str) -> list[list]: else: # User chat, format: (msg, None) dial_msg.append(line) + if glb_doing_signal_dict[uid]: + if glb_doing_signal_dict[uid][1]: + text = cycle_dots(glb_doing_signal_dict[uid][1]["text"]) + glb_doing_signal_dict[uid][1]["text"] = text + glb_doing_signal_dict[uid][1]["id"] = str(time.time()) + glb_doing_signal_dict[uid][1]["flushing"] = False + + dial_msg.append(glb_doing_signal_dict[uid]) return dial_msg[-MAX_NUM_DISPLAY_MSG:] diff --git a/src/agentscope/web/studio/utils.py b/src/agentscope/web/studio/utils.py index 47fd83e3c..36ac7f149 100644 --- a/src/agentscope/web/studio/utils.py +++ b/src/agentscope/web/studio/utils.py @@ -187,6 +187,15 @@ def audio2text(audio_path: str) -> str: return " ".join([s["text"] for s in result["output"]["sentence"]]) +def cycle_dots(text: str, num_dots: int = 3) -> str: + """display thinking dots before agent reply""" + current_dots = len(text) - len(text.rstrip(".")) + next_dots = (current_dots + 1) % (num_dots + 1) + if next_dots == 0: + next_dots = 1 + return text.rstrip(".") + "." * next_dots + + def user_input( prefix: str = "User input: ", timeout: Optional[int] = None,