Skip to content

Commit

Permalink
added request_user_input_with_note
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardusrendy committed Jun 18, 2024
1 parent 8ae443b commit afec48d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions alab_management/user_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ def get_all_pending_requests(self) -> list:
self._input_collection.find({"status": UserRequestStatus.PENDING.value}),
)

def retrieve_user_input_with_note(self, request_id: ObjectId) -> tuple[str, str]:
"""
Retrive response from user for a given request. Blocks until request is marked as completed.
Returns the user response, which is one of a list of options
"""
status = UserRequestStatus.PENDING
try:
while status == UserRequestStatus.PENDING:
request = self._input_collection.find_one({"_id": request_id})
if request is None:
raise ValueError(
f"User input request id {request_id} does not exist!"
)
status = UserRequestStatus(request["status"])
time.sleep(0.5)
except: # noqa: E722
self._input_collection.update_one(
{"_id": request_id}, {"$set": {"status": UserRequestStatus.ERROR.name}}
)
raise
return request["response"], request["note"]

def request_user_input(
task_id: ObjectId | None,
Expand Down Expand Up @@ -185,3 +207,30 @@ def request_maintenance_input(prompt: str, options: list[str]):
maintenance=True,
category="Maintenance",
)

def request_user_input_with_note(
task_id: ObjectId | None,
prompt: str,
options: list[str],
maintenance: bool = False,
category: str = "Unknown Category",
) -> tuple[str, str]:
"""
Request user input through the dashboard. Blocks until response is given.
task_id (ObjectId): task id requesting user input
prompt (str): prompt to give user
options (List[str]): response options to give user
maintenance (bool): if true, mark this as a request for overall system maintenance
Returns user response as string.
"""
user_input_view = UserInputView()
request_id = user_input_view.insert_request(
task_id=task_id,
prompt=prompt,
options=options,
maintenance=maintenance,
category=category,
)
return user_input_view.retrieve_user_input_with_note(request_id=request_id)

0 comments on commit afec48d

Please sign in to comment.