MCPcopy Create free account
hub / github.com/AQ-MedAI/MedMemoryBench / SyncWebSocketInterface

Class SyncWebSocketInterface

methods/letta/server/ws_api/interface.py:55–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53
54
55class SyncWebSocketInterface(BaseWebSocketInterface):
56 def __init__(self):
57 super().__init__()
58 self.clients = set()
59 self.loop = asyncio.new_event_loop() # Create a new event loop
60 self.thread = threading.Thread(target=self._run_event_loop, daemon=True)
61 self.thread.start()
62
63 def _run_event_loop(self):
64 """Run the dedicated event loop and handle its closure."""
65 asyncio.set_event_loop(self.loop)
66 try:
67 self.loop.run_forever()
68 finally:
69 # Run the cleanup tasks in the event loop
70 self.loop.run_until_complete(self.loop.shutdown_asyncgens())
71 self.loop.close()
72
73 def _run_async(self, coroutine):
74 """Schedule coroutine to be run in the dedicated event loop."""
75 if not self.loop.is_closed():
76 asyncio.run_coroutine_threadsafe(coroutine, self.loop)
77
78 async def _send_to_all_clients(self, clients, msg):
79 """Asynchronously sends a message to all clients."""
80 if clients:
81 await asyncio.gather(*(client.send_text(msg) for client in clients))
82
83 def user_message(self, msg):
84 """Handle reception of a user message"""
85 # Logic to process the user message and possibly trigger agent's response
86
87 def internal_monologue(self, msg):
88 """Handle the agent's internal monologue"""
89 print(msg)
90 if self.clients:
91 self._run_async(self._send_to_all_clients(self.clients, protocol.server_agent_internal_monologue(msg)))
92
93 def assistant_message(self, msg):
94 """Handle the agent sending a message"""
95 print(msg)
96 if self.clients:
97 self._run_async(self._send_to_all_clients(self.clients, protocol.server_agent_assistant_message(msg)))
98
99 def function_message(self, msg):
100 """Handle the agent calling a function"""
101 print(msg)
102 if self.clients:
103 self._run_async(self._send_to_all_clients(self.clients, protocol.server_agent_function_message(msg)))
104
105 def close(self):
106 """Shut down the WebSocket interface and its event loop."""
107 self.loop.call_soon_threadsafe(self.loop.stop) # Signal the loop to stop
108 self.thread.join() # Wait for the thread to finish

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected