suzmen commited on
Commit
64cfada
·
verified ·
1 Parent(s): bd113f3

Upload 64 files

Browse files
Files changed (1) hide show
  1. app/services/browser.py +41 -9
app/services/browser.py CHANGED
@@ -136,17 +136,48 @@ class BrowserEngine(threading.Thread):
136
  print("[PhantomAPI] ⌨️ Waiting for input box...")
137
  await page.wait_for_selector("#prompt-textarea", timeout=45000)
138
  await page.fill("#prompt-textarea", prompt)
139
- await asyncio.sleep(0.5)
 
 
 
140
  await page.press("#prompt-textarea", "Enter")
141
-
142
- # Wait for the assistant to start responding
 
 
 
 
 
 
 
 
 
143
  print("[PhantomAPI] 🤖 Waiting for response...")
144
- await page.wait_for_selector(
145
- '[data-message-author-role="assistant"]',
146
- timeout=settings.BROWSER_TIMEOUT,
147
- )
148
-
149
- # Poll until the response stabilises (no new text for ~2 seconds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  last_text = ""
151
  unchanged_count = 0
152
  while unchanged_count < 4:
@@ -162,6 +193,7 @@ class BrowserEngine(threading.Thread):
162
  unchanged_count = 0
163
  await asyncio.sleep(0.5)
164
 
 
165
  return last_text.strip()
166
 
167
  except Exception as exc:
 
136
  print("[PhantomAPI] ⌨️ Waiting for input box...")
137
  await page.wait_for_selector("#prompt-textarea", timeout=45000)
138
  await page.fill("#prompt-textarea", prompt)
139
+ await asyncio.sleep(1.0)
140
+
141
+ # Robust send: Press Enter AND click the send button
142
+ print("[PhantomAPI] 📤 Sending prompt...")
143
  await page.press("#prompt-textarea", "Enter")
144
+
145
+ # Fallback: Click the send button if Enter didn't trigger it
146
+ try:
147
+ send_button = await page.wait_for_selector('[data-testid="send-button"]', timeout=3000)
148
+ if send_button and await send_button.is_enabled():
149
+ await send_button.click()
150
+ print("[PhantomAPI] 🖱️ Clicked Send button.")
151
+ except Exception:
152
+ pass # Already sent via Enter, or button not found
153
+
154
+ # Wait for assistant response OR error message
155
  print("[PhantomAPI] 🤖 Waiting for response...")
156
+ try:
157
+ # Wait for the first assistant bubble or an error
158
+ await page.wait_for_selector(
159
+ '[data-message-author-role="assistant"]',
160
+ timeout=settings.BROWSER_TIMEOUT,
161
+ )
162
+ print("[PhantomAPI] ✅ Assistant began responding.")
163
+ except Exception as e:
164
+ # Diagnostics: What is actually on the page?
165
+ print(f"[PhantomAPI] ❌ Response timeout/error: {e}")
166
+
167
+ # Check for common error messages
168
+ page_text = await page.evaluate("document.body.innerText")
169
+ if "Something went wrong" in page_text:
170
+ print("[PhantomAPI] ⛔ detected: 'Something went wrong'")
171
+ elif "Rate limit" in page_text:
172
+ print("[PhantomAPI] ⛔ detected: 'Rate limit'")
173
+ elif "Verify you are human" in page_text:
174
+ print("[PhantomAPI] ⛔ detected: 'Cloudflare / CAPTCHA'")
175
+ else:
176
+ print(f"[PhantomAPI] 📍 Diagnostic Text (first 300 chars): {page_text[:300]}")
177
+
178
+ raise
179
+
180
+ # Poll until the response stabilises
181
  last_text = ""
182
  unchanged_count = 0
183
  while unchanged_count < 4:
 
193
  unchanged_count = 0
194
  await asyncio.sleep(0.5)
195
 
196
+ print(f"[PhantomAPI] ✨ Response complete ({len(last_text)} chars).")
197
  return last_text.strip()
198
 
199
  except Exception as exc: