You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

websocket_connection_manager.py 6.1 kB

Support for Human Input Mode in AutoGen Studio (#3484) * bump version, add claude default model * Move WebSocketConnectionManager into its own file. * Update the AutoGenStudio to use Async code throughout the call stack Update *WorkflowManager* classes: - Add async `a_send_message_function` parameter to mirror `send_message_function` param. - Add async `a_process_message` coroutine to mirror the synchronous `process_message` function. - Add async `a_run` coroutine to mirror the `run` function - Add async `_a_run_workflow` coroutine to mirror the synchronous `_run_workflow` function. Update *ExtendedConversableAgent* and *ExtendedGroupChatManager* classes: - Override the async `a_receive` coroutines Update *AutoGenChatManager*: - Add async `a_send` and `a_chat` coroutines to mirror their sync counterparts. - Accept the `WebSocketManager` instance as a parameter, so it can do Async comms directly. Update *app.py* - Provide the `WebSocketManager` instance to the *AutoGenChatManager* constructor - Await the manager's `a_chat` coroutine, rather than calling the synchronous `chat` function. * Add Human Input Support Updates to *ExtendedConversableAgent* and *ExtendedGroupChatManager* classes - override the `get_human_input` function and async `a_get_human_input` coroutine Updates to *WorkflowManager* classes: - add parameters `a_human_input_function` and `a_human_input_timeout` and pass along on to the ExtendedConversableAgent and ExtendedGroupChatManager - fix for invalid configuration passed from UI when human input mode is not NEVER and no model is attached Updates to *AutoGenChatManager* class: - add parameter `human_input_timeout` and pass it along to *WorkflowManager* classes - add async `a_prompt_for_input` coroutine that relies on `websocket_manager.get_input` coroutine (which snuck into last commit) Updates to *App.py* - global var HUMAN_INPUT_TIMEOUT_SECONDS = 180, we can replace this with a configurable value in the future * add formatting/precommit fixes * version bump --------- Co-authored-by: Joe Landers <sailorjoe6@gmail.com>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import asyncio
  2. from typing import Any, Dict, List, Optional, Tuple, Union
  3. import websockets
  4. from fastapi import WebSocket, WebSocketDisconnect
  5. class WebSocketConnectionManager:
  6. """
  7. Manages WebSocket connections including sending, broadcasting, and managing the lifecycle of connections.
  8. """
  9. def __init__(
  10. self,
  11. active_connections: List[Tuple[WebSocket, str]] = None,
  12. active_connections_lock: asyncio.Lock = None,
  13. ) -> None:
  14. """
  15. Initializes WebSocketConnectionManager with an optional list of active WebSocket connections.
  16. :param active_connections: A list of tuples, each containing a WebSocket object and its corresponding client_id.
  17. """
  18. if active_connections is None:
  19. active_connections = []
  20. self.active_connections_lock = active_connections_lock
  21. self.active_connections: List[Tuple[WebSocket, str]] = active_connections
  22. async def connect(self, websocket: WebSocket, client_id: str) -> None:
  23. """
  24. Accepts a new WebSocket connection and appends it to the active connections list.
  25. :param websocket: The WebSocket instance representing a client connection.
  26. :param client_id: A string representing the unique identifier of the client.
  27. """
  28. await websocket.accept()
  29. async with self.active_connections_lock:
  30. self.active_connections.append((websocket, client_id))
  31. print(f"New Connection: {client_id}, Total: {len(self.active_connections)}")
  32. async def disconnect(self, websocket: WebSocket) -> None:
  33. """
  34. Disconnects and removes a WebSocket connection from the active connections list.
  35. :param websocket: The WebSocket instance to remove.
  36. """
  37. async with self.active_connections_lock:
  38. try:
  39. self.active_connections = [conn for conn in self.active_connections if conn[0] != websocket]
  40. print(f"Connection Closed. Total: {len(self.active_connections)}")
  41. except ValueError:
  42. print("Error: WebSocket connection not found")
  43. async def disconnect_all(self) -> None:
  44. """
  45. Disconnects all active WebSocket connections.
  46. """
  47. for connection, _ in self.active_connections[:]:
  48. await self.disconnect(connection)
  49. async def send_message(self, message: Union[Dict, str], websocket: WebSocket) -> None:
  50. """
  51. Sends a JSON message to a single WebSocket connection.
  52. :param message: A JSON serializable dictionary containing the message to send.
  53. :param websocket: The WebSocket instance through which to send the message.
  54. """
  55. try:
  56. async with self.active_connections_lock:
  57. await websocket.send_json(message)
  58. except WebSocketDisconnect:
  59. print("Error: Tried to send a message to a closed WebSocket")
  60. await self.disconnect(websocket)
  61. except websockets.exceptions.ConnectionClosedOK:
  62. print("Error: WebSocket connection closed normally")
  63. await self.disconnect(websocket)
  64. except Exception as e:
  65. print(f"Error in sending message: {str(e)}", message)
  66. await self.disconnect(websocket)
  67. async def get_input(self, prompt: Union[Dict, str], websocket: WebSocket, timeout: int = 60) -> str:
  68. """
  69. Sends a JSON message to a single WebSocket connection as a prompt for user input.
  70. Waits on a user response or until the given timeout elapses.
  71. :param prompt: A JSON serializable dictionary containing the message to send.
  72. :param websocket: The WebSocket instance through which to send the message.
  73. """
  74. response = "Error: Unexpected response.\nTERMINATE"
  75. try:
  76. async with self.active_connections_lock:
  77. await websocket.send_json(prompt)
  78. result = await asyncio.wait_for(websocket.receive_json(), timeout=timeout)
  79. data = result.get("data")
  80. if data:
  81. response = data.get("content", "Error: Unexpected response format\nTERMINATE")
  82. else:
  83. response = "Error: Unexpected response format\nTERMINATE"
  84. except asyncio.TimeoutError:
  85. response = f"The user was timed out after {timeout} seconds of inactivity.\nTERMINATE"
  86. except WebSocketDisconnect:
  87. print("Error: Tried to send a message to a closed WebSocket")
  88. await self.disconnect(websocket)
  89. response = "The user was disconnected\nTERMINATE"
  90. except websockets.exceptions.ConnectionClosedOK:
  91. print("Error: WebSocket connection closed normally")
  92. await self.disconnect(websocket)
  93. response = "The user was disconnected\nTERMINATE"
  94. except Exception as e:
  95. print(f"Error in sending message: {str(e)}", prompt)
  96. await self.disconnect(websocket)
  97. response = f"Error: {e}\nTERMINATE"
  98. return response
  99. async def broadcast(self, message: Dict) -> None:
  100. """
  101. Broadcasts a JSON message to all active WebSocket connections.
  102. :param message: A JSON serializable dictionary containing the message to broadcast.
  103. """
  104. # Create a message dictionary with the desired format
  105. message_dict = {"message": message}
  106. for connection, _ in self.active_connections[:]:
  107. try:
  108. if connection.client_state == websockets.protocol.State.OPEN:
  109. # Call send_message method with the message dictionary and current WebSocket connection
  110. await self.send_message(message_dict, connection)
  111. else:
  112. print("Error: WebSocket connection is closed")
  113. await self.disconnect(connection)
  114. except (WebSocketDisconnect, websockets.exceptions.ConnectionClosedOK) as e:
  115. print(f"Error: WebSocket disconnected or closed({str(e)})")
  116. await self.disconnect(connection)