suzmen commited on
Commit
595c165
·
verified ·
1 Parent(s): 43e0402

Upload 64 files

Browse files
Files changed (1) hide show
  1. app/services/browser.py +13 -3
app/services/browser.py CHANGED
@@ -182,10 +182,13 @@ class BrowserEngine(threading.Thread):
182
  unchanged_count = 0
183
  start_polling = asyncio.get_event_loop().time()
184
 
185
- while unchanged_count < 4:
186
- # Safety break: 90 seconds max for polling
 
 
 
187
  if asyncio.get_event_loop().time() - start_polling > 90:
188
- print("[PhantomAPI] ⚠️ Polling timeout reached (90s). Returning current text.")
189
  break
190
 
191
  elements = await page.query_selector_all(
@@ -193,6 +196,7 @@ class BrowserEngine(threading.Thread):
193
  )
194
  if elements:
195
  current_text = await elements[-1].inner_text()
 
196
  if current_text == last_text:
197
  unchanged_count += 1
198
  else:
@@ -200,6 +204,12 @@ class BrowserEngine(threading.Thread):
200
  print(f"[PhantomAPI] ⏳ Generating... ({len(current_text)} chars)")
201
  last_text = current_text
202
  unchanged_count = 0
 
 
 
 
 
 
203
 
204
  await asyncio.sleep(0.5)
205
 
 
182
  unchanged_count = 0
183
  start_polling = asyncio.get_event_loop().time()
184
 
185
+ # Smart stabilization:
186
+ # - If text is empty, wait longer to give it time to start typing.
187
+ # - If text exists, 2 seconds of silence is enough.
188
+ while True:
189
+ # Safety break: 90 seconds total
190
  if asyncio.get_event_loop().time() - start_polling > 90:
191
+ print("[PhantomAPI] ⚠️ Hard timeout reached.")
192
  break
193
 
194
  elements = await page.query_selector_all(
 
196
  )
197
  if elements:
198
  current_text = await elements[-1].inner_text()
199
+
200
  if current_text == last_text:
201
  unchanged_count += 1
202
  else:
 
204
  print(f"[PhantomAPI] ⏳ Generating... ({len(current_text)} chars)")
205
  last_text = current_text
206
  unchanged_count = 0
207
+
208
+ # Determine threshold
209
+ threshold = 20 if len(last_text.strip()) == 0 else 4
210
+
211
+ if unchanged_count >= threshold:
212
+ break
213
 
214
  await asyncio.sleep(0.5)
215