@@ -88,8 +88,9 @@ async def create_container_tool(
8888}}"""
8989
9090 # Make API call to generate Dockerfile with streaming
91- await _emit_ws_message (websocket , "status" , "🐳 Generating Dockerfile..." )
92- print ("🐳 Generating Dockerfile... (streaming response)\n " )
91+ websocket_active = await _emit_ws_message (websocket , "status" , "🐳 Generating Dockerfile..." )
92+ if websocket_active :
93+ print ("🐳 Generating Dockerfile... (streaming response)\n " )
9394
9495 response = await client .chat .completions .create (
9596 model = "gpt-4o-mini" , # Using GPT-4 for better code generation
@@ -110,7 +111,8 @@ async def create_container_tool(
110111
111112 # Collect the streaming response and print in real-time
112113 dockerfile_response = ""
113- await _emit_ws_message (websocket , "stream_start" , "Starting generation..." )
114+ if websocket_active :
115+ websocket_active = await _emit_ws_message (websocket , "stream_start" , "Starting generation..." )
114116 print ("📝 Response:" )
115117 print ("-" * 50 )
116118
@@ -119,12 +121,14 @@ async def create_container_tool(
119121 content = chunk .choices [0 ].delta .content
120122 print (content , end = "" , flush = True )
121123 dockerfile_response += content
122- # Emit each chunk to WebSocket clients
123- await _emit_ws_message (websocket , "chunk" , content )
124+ # Only emit chunks if WebSocket is still active
125+ if websocket_active :
126+ websocket_active = await _emit_ws_message (websocket , "chunk" , content )
124127
125128 print ("\n " + "-" * 50 )
126129 print ("✅ Generation complete!\n " )
127- await _emit_ws_message (websocket , "status" , "✅ Generation complete!" )
130+ if websocket_active :
131+ await _emit_ws_message (websocket , "status" , "✅ Generation complete!" )
128132
129133 # Try to parse as JSON, fallback to plain text if needed
130134 try :
@@ -197,12 +201,20 @@ async def create_container_tool(
197201 "error" : str (e ),
198202 "project_name" : project_name or "unknown-project"
199203 }
200- await _emit_ws_message (websocket , "error" , str (e ))
204+ # Only send error message if WebSocket might still be active
205+ try :
206+ await _emit_ws_message (websocket , "error" , str (e ))
207+ except :
208+ # WebSocket is definitely closed, just log the error
209+ print (f"Could not send error to WebSocket: { e } " )
201210 return error_result
202211
203212
204- async def _emit_ws_message (websocket : Optional [Any ], message_type : str , content : str ) -> None :
205- """Helper function to emit WebSocket messages safely."""
213+ async def _emit_ws_message (websocket : Optional [Any ], message_type : str , content : str ) -> bool :
214+ """
215+ Helper function to emit WebSocket messages safely.
216+ Returns True if message was sent successfully, False if WebSocket is closed or error occurred.
217+ """
206218 if websocket is not None :
207219 try :
208220 message = {
@@ -211,8 +223,12 @@ async def _emit_ws_message(websocket: Optional[Any], message_type: str, content:
211223 "timestamp" : asyncio .get_event_loop ().time ()
212224 }
213225 await websocket .send_text (json .dumps (message ))
226+ return True
214227 except Exception as e :
215- print (f"WebSocket error: { e } " )
228+ # WebSocket is likely closed, stop trying to send messages
229+ print (f"WebSocket closed or error occurred: { e } " )
230+ return False
231+ return False
216232
217233
218234def run_create_container (
0 commit comments