@@ -113,7 +113,7 @@ async def stream_claim_analysis_exp(
113113 claim = await claim_service .get_claim (claim_id = claim_id , user_id = current_user .id )
114114
115115 session = claim_service ._claim_repo ._session
116-
116+
117117 await session .rollback ()
118118
119119 async def event_generator ():
@@ -128,59 +128,57 @@ async def event_generator():
128128 # THE HEALTH CHECK LOOP
129129 # ---------------------------------------------------------
130130 next_event_task = None
131-
131+
132132 while True :
133133 # Only create a new task if we don't already have one waiting
134134 if next_event_task is None :
135135 next_event_task = asyncio .create_task (anext (orchestrator_stream ))
136136
137137 # Wait for the task to finish, but only wait 15 seconds
138138 done , pending = await asyncio .wait (
139- [next_event_task ],
140- timeout = 15.0 ,
141- return_when = asyncio .FIRST_COMPLETED
139+ [next_event_task ], timeout = 15.0 , return_when = asyncio .FIRST_COMPLETED
142140 )
143-
141+
144142 if next_event_task in done :
145143 # The LLM yielded a chunk! Let's process it.
146144 try :
147145 event = next_event_task .result ()
148146 if isinstance (event , dict ):
149147 yield f"data: { json .dumps (event )} \n \n "
150-
148+
151149 # Reset the task so we grab the next chunk on the next loop
152- next_event_task = None
153-
150+ next_event_task = None
151+
154152 except StopAsyncIteration :
155153 # The stream finished normally!
156154 break
157155 except Exception as e :
158156 # If the orchestrator crashed, catch it here
159157 raise e
160-
158+
161159 else :
162160 # The task is in 'pending'. 15 seconds passed, but the LLM is still thinking.
163- # We yield a heartbeat, but we DO NOT reset next_event_task.
161+ # We yield a heartbeat, but we DO NOT reset next_event_task.
164162 # It will keep running safely in the background on the next loop!
165163 logger .debug ("Stream idle for 15s. Sending health check ping..." )
166164 yield ": healthcheck\n \n "
167-
165+
168166 yield "data: [DONE]\n \n "
169167
170168 except asyncio .CancelledError :
171169 logger .warning (f"Client disconnected during stream for claim { claim_id } " )
172170 raise
173-
171+
174172 # async for event in analysis_orchestrator.analyze_claim_stream(
175- # claim=claim, user_id=current_user.id, default=False
176- # ):
177- # if isinstance(event, dict):
178- # yield f"data: {json.dumps(event)}\n\n"
173+ # claim=claim, user_id=current_user.id, default=False
174+ # ):
175+ # if isinstance(event, dict):
176+ # yield f"data: {json.dumps(event)}\n\n"
179177
180- # yield "data: [DONE]\n\n"
178+ # yield "data: [DONE]\n\n"
181179
182180 # except asyncio.CancelledError:
183- # # THE FIX: The user closed their browser!
181+ # # THE FIX: The user closed their browser!
184182 # logger.info(f"Client disconnected during stream for claim {claim_id}")
185183 # await session.rollback() # Explicitly release the lock!
186184 # raise
@@ -196,7 +194,7 @@ async def event_generator():
196194 # logger.error(f"Force rollback failed: {e}")
197195 # finally:
198196 # await session.close()
199-
197+
200198 # # Fire and forget. FastAPI cannot cancel this!
201199 # asyncio.create_task(force_cleanup())
202200 if next_event_task and not next_event_task .done ():
0 commit comments