Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add displaying cycle dots before agent reply in AS studio #179

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/agentscope/web/studio/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
""" Some constants used in the AS studio"""

_SPEAK = "**speak**"
22 changes: 21 additions & 1 deletion src/agentscope/web/studio/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:]


Expand Down
9 changes: 9 additions & 0 deletions src/agentscope/web/studio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down