Repository navigation
Expand file tree
/
Copy pathmain_agent.py
More file actions
889 lines (782 loc) · 47.1 KB
/
Copy pathmain_agent.py
File metadata and controls
889 lines (782 loc) · 47.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
import sys
import os
import time
import random
import re
import json
# Ensure stdout and stderr support UTF-8 encoding to avoid Windows charmap encoding crashes
if sys.stdout.encoding != 'utf-8':
try:
sys.stdout.reconfigure(encoding='utf-8')
except Exception:
pass
if sys.stderr.encoding != 'utf-8':
try:
sys.stderr.reconfigure(encoding='utf-8')
except Exception:
pass
from selenium.webdriver.common.by import By
# 🛠️ NAYA FIX: Saari files ab ek hi level par hain, isliye seedha import hoga
from chrome_launch import launch_browser
from cookie_manager import inject_cookies
from config import PAGE_LOAD_PAUSE, ACTION_DELAY_RANGE
from agent_brain import extract_goal_and_url, decide_action, get_element_mapping, verify_form_with_gemma
from dom_utils import get_page_snapshot, execute_action
from cdp_graph_generator import capture_cdp_and_generate_graph
import ai_interpret
def execute_agent_action(driver, action) -> str:
act_type = str(action.get("action", "")).lower().strip()
thought_str = str(action.get("thought", "")).lower()
element_id = action.get("element_id")
# 1. Programmatic reCAPTCHA detection inside iframe (Check only visible iframes)
has_recaptcha = False
try:
iframes = driver.find_elements(By.TAG_NAME, "iframe")
for iframe in iframes:
src = str(iframe.get_attribute("src") or "").lower()
if "recaptcha" in src or "hcaptcha" in src or "challenge" in src or "captcha" in src:
if iframe.is_displayed():
has_recaptcha = True
break
except:
pass
captcha_action_keywords = ["solve_captcha", "resolve_captcha", "complete_captcha", "wait_manual", "complete_recaptcha"]
is_captcha_act = (
act_type in captcha_action_keywords
or "captcha" in thought_str
or "recaptcha" in thought_str
or "captcha" in act_type
or "recaptcha" in act_type
)
# If recaptcha is physically on the page, and the agent tries to submit
is_blocked_by_captcha = False
if has_recaptcha:
# Intercept submit buttons (like element 7) or terms (like element 8) or any captcha actions
if is_captcha_act or (act_type == "click" and element_id in [7, 8]):
is_blocked_by_captcha = True
if is_captcha_act or is_blocked_by_captcha:
print("\n" + "="*80)
print("🚨 [CAPTCHA / MANUAL VERIFICATION DETECTED]")
print("Flickr has presented a verification challenge. Attempting auto-solving with GPT-4o...")
print("="*80 + "\n")
max_captcha_attempts = 3
solved = False
for attempt in range(1, max_captcha_attempts + 1):
print(f"🔄 [CAPTCHA ATTEMPT {attempt}/{max_captcha_attempts}] Capturing viewport screenshot...")
time.sleep(2.0) # Wait a bit for captcha widget to fully settle
captcha_img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), f"captcha_attempt_{attempt}.png")
try:
driver.save_screenshot(captcha_img_path)
except Exception as shot_err:
print(f"⚠️ Failed to save captcha screenshot: {shot_err}")
captcha_img_path = None
if captcha_img_path:
prompt_details = f"URL: {driver.current_url}\nTITLE: {driver.title}\nCAPTCHA ATTEMPT: {attempt}"
solve_result = ai_interpret.solve_captcha_visually(prompt_details, captcha_img_path)
# Delete temporary screenshot to keep directory clean
try:
if os.path.exists(captcha_img_path):
os.remove(captcha_img_path)
except:
pass
if solve_result.get("found"):
cx = solve_result.get("x")
cy = solve_result.get("y")
reason = solve_result.get("reason", "")
print(f"🎯 GPT-4o found CAPTCHA target at ({cx}, {cy}). Reason: {reason}")
# Perform coordinate click
click_action = {"action": "click_coordinates", "x": cx, "y": cy}
exec_res = execute_action(driver, click_action)
print(f"🎬 Coordinate click result: {exec_res}")
time.sleep(4.0) # Wait for captcha state to process after click
# Verify if captcha is gone programmatically
still_has_recaptcha = False
try:
iframes = driver.find_elements(By.TAG_NAME, "iframe")
for iframe in iframes:
src = str(iframe.get_attribute("src") or "").lower()
if "recaptcha" in src or "hcaptcha" in src or "challenge" in src or "captcha" in src:
if iframe.is_displayed():
still_has_recaptcha = True
break
except:
pass
if not still_has_recaptcha:
print("✅ CAPTCHA iframe is not displayed or has disappeared! Solved successfully.")
solved = True
break
else:
print("⚠️ CAPTCHA iframe is still displayed. Trying next visual solve attempt...")
else:
print(f"❌ GPT-4o did not locate target. Reason: {solve_result.get('reason')}")
# Double check if iframe is still displayed
still_has_recaptcha = False
try:
iframes = driver.find_elements(By.TAG_NAME, "iframe")
for iframe in iframes:
src = str(iframe.get_attribute("src") or "").lower()
if "recaptcha" in src or "hcaptcha" in src or "challenge" in src or "captcha" in src:
if iframe.is_displayed():
still_has_recaptcha = True
break
except:
pass
if not still_has_recaptcha:
print("✅ CAPTCHA iframe is gone/not displayed anymore!")
solved = True
break
else:
print("⚠️ Skipping visual attempt because screenshot failed.")
if not solved:
print("\n" + "="*80)
print("🚨 [AUTO-SOLVE FAILED] GPT-4o could not solve the CAPTCHA automatically.")
print("Please solve the CAPTCHA manually in the open browser window.")
print("="*80 + "\n")
try:
input("⌨️ Press ENTER in this terminal after solving the CAPTCHA to resume the agent...")
time.sleep(2.0)
if act_type == "click" and element_id:
print(f"🔄 Re-executing original action: Click element {element_id}")
return execute_action(driver, action)
return "ok"
except EOFError:
print("⚠️ Running in headless/non-interactive mode. Waiting 30 seconds for CAPTCHA to resolve...")
time.sleep(30)
if act_type == "click" and element_id:
print(f"🔄 Re-executing original action: Click element {element_id}")
return execute_action(driver, action)
return "ok"
else:
print("🎉 CAPTCHA auto-solve successful! Bypassed verification.")
if act_type == "click" and element_id:
print(f"🔄 Re-executing original action: Click element {element_id}")
return execute_action(driver, action)
return "ok"
return execute_action(driver, action)
MAX_TOTAL_ACTIONS = 9999
def _generate_selectors_from_dom_line(dom_line: str) -> list:
selectors = []
if not dom_line: return selectors
tag = ""
tag_match = re.search(r'<([a-zA-Z0-9]+)', dom_line)
if tag_match: tag = tag_match.group(1)
hint_match = re.search(r'Hint: "(.*?)"', dom_line)
if tag and hint_match:
hint = hint_match.group(1)
if len(hint) > 2:
safe_hint = hint.replace("'", "")
selectors.append(f"{tag}[placeholder*='{safe_hint}']")
selectors.append(f"{tag}[aria-label*='{safe_hint}']")
selectors.append(f"//{tag}[contains(@placeholder, '{safe_hint}') or contains(@aria-label, '{safe_hint}')]")
if tag == "input" and "file" in dom_line.lower():
selectors.append("input[type='file']")
if tag in ["button", "div", "span"]:
text_match = re.search(r'>:\s*(.*)', dom_line)
if text_match:
raw_text = text_match.group(1).replace("[EMPTY]", "").strip()
raw_text = re.sub(r'\(Hint:.*?\)', '', raw_text).strip()
if len(raw_text) > 2:
safe_text = raw_text.replace("'", "")
selectors.append(f"//{tag}[contains(., '{safe_text}')]")
return selectors
def check_and_run_audit(driver, action, snapshot, action_history, goal) -> dict:
"""
Checks if the action is a publish/save/share or task_done action.
If so, runs the GPT-4o validation audit to ensure everything is correct.
If audit fails, returns a corrective action or a wait action.
"""
if getattr(driver, "audit_has_run", False):
return action
act_type = str(action.get("action", "")).lower().strip()
element_id = action.get("element_id")
is_publish_click = False
# 1. Direct task completion
if act_type in ["task_done", "step_done"]:
is_publish_click = True
# 2. Click actions (by element ID or coordinates)
elif act_type in ["click", "click_coordinates"]:
# Check element text if element_id is present
el_text = ""
if element_id:
try:
# Find the element description in the DOM snapshot
pattern = rf"\[{element_id}\]\s*<[^>]*>:\s*(.*)"
match = re.search(pattern, snapshot, re.IGNORECASE)
if match:
el_text = match.group(1).lower()
except:
pass
# Check if element text indicates a publish/save/share action
if el_text:
if any(kw in el_text for kw in ["publish", "save", "share", "post", "submit"]):
is_publish_click = True
# Also check the decided thought/action content to see if the AI intends to publish/save/share
thought_str = str(action.get("thought", "")).lower()
action_str = str(action.get("action", "")).lower()
heavy_keywords = ["publish", "save", "share", "post now", "publish now", "post to", "share pin", "publish pin", "publish story"]
exclude_keywords = ["what's on your mind", "create post", "photo/video", "live video", "reel", "learn more"]
if any(kw in thought_str for kw in heavy_keywords) or any(kw in action_str for kw in heavy_keywords):
if not any(ex in thought_str for ex in exclude_keywords):
is_publish_click = True
if is_publish_click:
print("\n🔍 [AUDIT] Pre-Publish action detected. Running GPT-4o validation audit...")
verify_img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "publish_verify.png")
try:
driver.save_screenshot(verify_img_path)
except Exception as shot_err:
print(f"⚠️ Failed to save audit screenshot: {shot_err}")
audit_prompt = (
f"GOAL: {goal}\n"
f"CURRENT URL: {driver.current_url}\n"
f"PAGE TITLE: {driver.title}\n"
f"CURRENT DOM SNAPSHOT:\n{snapshot}\n"
f"ACTION HISTORY:\n"
)
for h in action_history:
audit_prompt += f" - {h.get('action')} | {h.get('element_id')} | {h.get('text', h.get('file_path', ''))}\n"
audit_result = ai_interpret.verify_pre_publish(audit_prompt, verify_img_path)
if not audit_result.get("ready", True):
print(f"❌ [AUDIT FAILED] GPT-4o flagged the page as NOT ready: {audit_result.get('reason')}")
try:
driver.force_lightning_ai = True
print("🚨 [FORCE LIGHTNING AI ACTIVATED] Future entries will be decided only by GPT-4o.")
except Exception as set_err:
print(f"⚠️ Failed to set force_lightning_ai on driver: {set_err}")
corr_action = audit_result.get("corrective_action")
if corr_action and isinstance(corr_action, dict):
corr_action = apply_duplicate_guard(corr_action, action_history, driver=driver, goal=goal, image_path=verify_img_path)
print(f"🔧 [AUDIT CORRECTION] Executing corrective action: {corr_action}")
corr_action["thought"] = f"Audit failed: {audit_result.get('reason')}. Correcting: " + corr_action.get("thought", "")
return corr_action
else:
print("⚠️ No corrective action suggested by GPT-4o. Skipping publish click for safety.")
return {"action": "wait", "wait_time": 3, "thought": f"Publish blocked by audit: {audit_result.get('reason')}"}
else:
print("✅ [AUDIT SUCCESS] GPT-4o verified page is ready to publish!")
driver.audit_has_run = True
return action
def run_post_verification(driver, goal, action_history) -> dict:
"""
Runs the post-publish verification using GPT-4o.
If success, returns {"verified": True}.
If failed, returns {"verified": False, "corrective_action": action_dict}.
"""
print("\n🔍 [AUDIT] Running Post-Publish verification audit...")
verify_img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "post_publish_verify.png")
try:
driver.save_screenshot(verify_img_path)
except Exception as shot_err:
print(f"⚠️ Failed to save post-verification screenshot: {shot_err}")
prompt = (
f"GOAL: {goal}\n"
f"CURRENT URL: {driver.current_url}\n"
f"PAGE TITLE: {driver.title}\n"
f"ACTION HISTORY:\n"
)
for h in action_history:
prompt += f" - {h.get('action')} | {h.get('element_id')} | {h.get('text', h.get('file_path', ''))}\n"
audit_result = ai_interpret.verify_post_publish(prompt, verify_img_path)
if audit_result.get("success", True):
print("✅ [POST-AUDIT SUCCESS] GPT-4o confirmed publication was successful!")
return {"verified": True}
else:
print(f"❌ [POST-AUDIT FAILED] GPT-4o flagged post as NOT published: {audit_result.get('reason')}")
try:
driver.force_lightning_ai = True
print("🚨 [FORCE LIGHTNING AI ACTIVATED] Future entries will be decided only by GPT-4o.")
except Exception as set_err:
print(f"⚠️ Failed to set force_lightning_ai on driver: {set_err}")
corr_action = audit_result.get("corrective_action")
if corr_action and isinstance(corr_action, dict):
corr_action = apply_duplicate_guard(corr_action, action_history, driver=driver, goal=goal, image_path=verify_img_path)
print(f"🔧 [POST-AUDIT CORRECTION] Suggested corrective action: {corr_action}")
return {"verified": False, "corrective_action": corr_action}
else:
print("⚠️ No corrective action suggested. Waiting for 5 seconds...")
return {"verified": False, "corrective_action": {"action": "wait", "wait_time": 5, "thought": f"Post-publish verification failed: {audit_result.get('reason')}"}}
def apply_duplicate_guard(action, action_history, driver=None, goal=None, image_path=None) -> dict:
"""
Guards against duplicate typing into search bars and repeated actions on search pages.
"""
act_type = str(action.get("action", "")).lower().strip()
if act_type in ["type", "type_and_enter"]:
text_to_type = str(action.get("text", "") or action.get("value", "") or action.get("content", "")).strip()
if not text_to_type:
return action
# 1. Search page interceptor: If current URL already contains '/search/' or 'keywords=', do not re-type the search term!
if driver:
try:
curr_url = (driver.current_url or "").lower()
if "/search/" in curr_url or "keywords=" in curr_url or "query=" in curr_url:
already_searched = any(
str(h.get("text", "")).lower().strip() == text_to_type.lower()
for h in action_history if not h.get("_failed") and h.get("action") in ["type", "type_and_enter"]
)
if already_searched or "search" in str(action.get("thought", "")).lower():
print(f"🛡️ [SEARCH GUARD] Blocked re-typing '{text_to_type}' on search page. Converting action to 'scroll_down'.")
return {
"action": "scroll_down",
"wait_time": 2,
"thought": "Search keyword is already entered and search results are loaded. Scrolling down to view candidates."
}
except Exception as guard_err:
print(f"⚠️ Search guard error: {guard_err}")
# 2. General duplicate text matching check
is_duplicate = False
for h in action_history:
if not h.get("_failed"):
h_act = str(h.get("action", "")).lower().strip()
if h_act in ["type", "type_and_enter"]:
h_text = str(h.get("text", "") or h.get("value", "") or h.get("content", "")).strip()
if text_to_type == h_text or (len(text_to_type) > 15 and text_to_type in h_text) or (len(h_text) > 15 and h_text in text_to_type):
is_duplicate = True
break
# 2. Visual Q&A confirmation check (with GPT-4o) if driver, goal and image/screenshot are available
if not is_duplicate and driver and goal:
# If no screenshot is provided, take a temporary one
temp_img = False
curr_image_path = image_path
if not curr_image_path:
curr_image_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "temp_qa_screenshot.png")
try:
driver.save_screenshot(curr_image_path)
temp_img = True
except Exception as shot_err:
print(f"⚠️ QA Guard failed to take screenshot: {shot_err}")
curr_image_path = None
if curr_image_path:
qa_prompt = (
f"GOAL: {goal}\n"
f"CURRENT URL: {driver.current_url}\n"
f"PROPOSED ACTION: {json.dumps(action)}\n"
f"ACTION HISTORY:\n"
)
for h in action_history:
qa_prompt += f" - {h.get('action')} | {h.get('element_id')} | {h.get('text', h.get('file_path', ''))}\n"
try:
print(f"🕵️♂️ [DUPLICATE GUARD QA] Calling visual Q&A validation...")
qa_res = ai_interpret.verify_typing_safety_qa(qa_prompt, curr_image_path)
if qa_res.get("is_already_filled", False) or qa_res.get("qa_checklist", {}).get("is_duplicate_entry", False):
print(f"⚠️ [DUPLICATE GUARD QA] Visual Q&A flagged field as already filled: {qa_res.get('reason')}")
is_duplicate = True
except Exception as qa_err:
print(f"⚠️ QA verification failed: {qa_err}")
if temp_img and curr_image_path and os.path.exists(curr_image_path):
try:
os.remove(curr_image_path)
except:
pass
if is_duplicate:
print(f"⚠️ [DUPLICATE GUARD] Blocked duplicate typing for text: '{text_to_type[:40]}...'")
return {
"action": "scroll_up",
"pixels": 400,
"wait_time": 2,
"thought": "Duplicate typing blocked by safety guard (Q&A/History check). Scrolling up to restore visibility of filled fields."
}
return action
def run_web_agent(user_prompt: str, headless: bool = False) -> dict:
logs = []
local_image_path = None
try: plan = extract_goal_and_url(user_prompt)
except Exception as e: return {"success": False, "summary": f"Planning failed: {e}", "logs": []}
page_name = plan.get("page_name")
# 🚀 FIX: Pass the EXACT user prompt as the goal to retain full details (like paths and descriptions).
goal = user_prompt
start_url = plan.get("start_url", "https://www.google.com/")
# Initialize Debug Log file
import json
debug_log_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "agent_step_debug.log")
with open(debug_log_path, "w", encoding="utf-8") as f:
f.write(f"=== AGENT RUN STARTED AT {time.strftime('%Y-%m-%d %H:%M:%S')} ===\n")
f.write(f"GOAL: {goal}\n")
f.write(f"START URL: {start_url}\n")
f.write("=" * 80 + "\n\n")
# 🕵️ ACCOUNT ACCESS & FALLBACK VERIFICATION 🕵️
# ✅ LIVE PIPELINE: Removed token injection checks.
# The agent now solely relies on the Persistent ChromeProfile.
print(f"\n🚀 Browser launch ho raha hai using Persistent Profile...")
driver = launch_browser(profile_name="Default")
driver.audit_has_run = False
# time.sleep(1000)
# 📏 Viewport Standardization: Set window size to 1280x800 for consistent coordinates
try:
print("📏 Setting window size to 1280x800...")
driver.set_window_size(1280, 800)
except Exception as size_err:
print(f"⚠️ Failed to set window size: {size_err}")
try:
driver.get(start_url)
time.sleep(PAGE_LOAD_PAUSE) # Configured initial load wait
# ✅ LIVE PIPELINE: No cookies injected. Relying on Profile session.
# 🔄 WAIT FOR PAGE LOAD AND REFRESH IF NEEDED
print("⏳ Checking if page is fully loaded...")
max_retries = 3
for i in range(max_retries):
state = driver.execute_script("return document.readyState")
inputs = driver.find_elements(By.CSS_SELECTOR, "input, button, textarea")
# Pinterest and most SPAs will have inputs/buttons when fully rendered
if state == "complete" and len(inputs) > 0:
print("✅ Page fully loaded!")
break
else:
print(f"⚠️ Page load incomplete or stuck (Attempt {i+1}/{max_retries}). Refreshing...")
driver.refresh()
time.sleep(PAGE_LOAD_PAUSE + 2) # Wait a bit longer after refresh
if state != "complete" or len(inputs) == 0:
print("⚠️ Warning: Page still doesn't seem fully loaded, but proceeding anyway.")
# ⏳ Special Hardcoded Wait for LinkedIn Search Results
if "linkedin.com" in start_url:
print("⏳ [LINKEDIN SPECIAL WAIT] Waiting 15 seconds for search results and 'Message' buttons to fully render...")
time.sleep(15)
action_history = []
payload = plan.get("payload", {})
local_image_path = payload.get("image_path") # 🗑️ Track for cleanup after publish
print(f"\n{'='*60}\n🎯 GOAL: {goal}\n{'='*60}")
consec_fails = 0
for attempt in range(1, MAX_TOTAL_ACTIONS + 1):
try:
cdp_res = capture_cdp_and_generate_graph(driver, os.path.dirname(os.path.abspath(__file__)))
snapshot = cdp_res["llm_compact_tree"]
except Exception as cdp_err:
print(f"⚠️ CDP snapshot fallback to JS snapshot due to: {cdp_err}")
snapshot = get_page_snapshot(driver)
# Take step screenshot for GPT-4o real-time vision guidance
step_img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "step_screenshot.png")
try:
driver.save_screenshot(step_img_path)
except Exception as shot_err:
print(f"⚠️ Failed to save step screenshot: {shot_err}")
step_img_path = None
# --- 🧠 AI DECISION (STEP-BY-STEP) ---
force_ai = getattr(driver, "force_lightning_ai", False)
action = decide_action(goal, snapshot, action_history, attempt, MAX_TOTAL_ACTIONS, image_path=step_img_path, force_lightning_ai=force_ai)
action = apply_duplicate_guard(action, action_history, driver=driver, goal=goal, image_path=step_img_path)
print("action", action)
# Extract Dynamic Wait Time
dynamic_wait = action.get("wait_time")
act_type = str(action.get("action", "")).lower()
element_id = action.get("element_id")
# Write step details to debug log BEFORE execution
with open(debug_log_path, "a", encoding="utf-8") as f:
f.write(f"⚡ ACTION ATTEMPT: {attempt}/{MAX_TOTAL_ACTIONS}\n")
f.write(f"🌐 CURRENT URL: {driver.current_url}\n")
f.write(f"📄 PAGE TITLE: {driver.title}\n")
f.write("-" * 40 + "\n")
f.write("📋 DOM SNAPSHOT FOR THIS STEP:\n")
f.write(snapshot + "\n")
f.write("-" * 40 + "\n")
f.write(f"🧠 DECIDED ACTION:\n{json.dumps(action, indent=2)}\n")
f.write("-" * 40 + "\n")
# --- 🔍 PRE-PUBLISH VALIDATION AUDIT ---
action = check_and_run_audit(driver, action, snapshot, action_history, goal)
act_type = str(action.get("action", "")).lower()
element_id = action.get("element_id")
# Execute
url_before = driver.current_url
result = execute_agent_action(driver, action)
# If URL changed (SPA navigation)
if "linkedin.com" in driver.current_url and driver.current_url != url_before:
print(f"⏳ [SPA NAVIGATION DETECTED] URL changed from {url_before} to {driver.current_url}. Waiting 4 seconds for page elements to render...")
time.sleep(4.0)
# Write execution result to debug log AFTER execution
with open(debug_log_path, "a", encoding="utf-8") as f:
f.write(f"🎬 EXECUTION RESULT: {result}\n")
f.write("=" * 80 + "\n\n")
if result.startswith("failed"):
action["_failed"] = True
action["_error"] = result
consec_fails += 1
print(f" ❌ Feedback to AI: {result}")
# --- 🛠️ GPT-4o VISUAL RECOVERY SUB-LOOP ---
print("\n🛠️ [RECOVERY] Action failed. Entering GPT-4o visual recovery sub-loop...")
recovery_attempts = 0
max_recovery_attempts = 3
step_recovered = False
while recovery_attempts < max_recovery_attempts and not step_recovered:
recovery_attempts += 1
print(f"🔄 [RECOVERY STEP {recovery_attempts}/{max_recovery_attempts}] capturing screenshot...")
error_img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "error_screenshot.png")
try:
driver.save_screenshot(error_img_path)
except Exception as shot_err:
print(f"⚠️ Failed to save recovery screenshot: {shot_err}")
try:
rec_res = capture_cdp_and_generate_graph(driver, os.path.dirname(os.path.abspath(__file__)))
rec_snapshot = rec_res["llm_compact_tree"]
except Exception as cdp_err:
rec_snapshot = get_page_snapshot(driver)
rec_prompt = (
f"GOAL: {goal}\n"
f"CURRENT URL: {driver.current_url}\n"
f"PAGE TITLE: {driver.title}\n"
f"LAST ACTION ATTEMPTED: {json.dumps(action)}\n"
f"LAST ACTION ERROR: {result}\n"
f"CURRENT DOM SNAPSHOT:\n{rec_snapshot}\n"
f"ACTION HISTORY:\n"
)
for h in action_history:
rec_prompt += f" - {h.get('action')} | {h.get('element_id')} | {h.get('text', h.get('file_path', ''))}\n"
rec_decision = ai_interpret.call_litai_visual_recovery(rec_prompt, error_img_path)
if rec_decision.get("step_complete"):
print("✅ [RECOVERY SUCCESS] GPT-4o indicates the step is now completed successfully!")
step_recovered = True
break
rec_action = rec_decision
rec_action = apply_duplicate_guard(rec_action, action_history, driver=driver, goal=goal, image_path=error_img_path)
rec_action = check_and_run_audit(driver, rec_action, rec_snapshot, action_history, goal)
rec_action = apply_duplicate_guard(rec_action, action_history, driver=driver, goal=goal, image_path=error_img_path)
rec_act_type = str(rec_action.get("action", "")).lower()
print(f"🔧 [RECOVERY ACTION] GPT-4o suggested: {json.dumps(rec_action)}")
# Execute recovery action
rec_url_before = driver.current_url
rec_result = execute_agent_action(driver, rec_action)
print(f"🎬 [RECOVERY EXECUTION RESULT]: {rec_result}")
# If URL changed (SPA navigation in recovery)
if "linkedin.com" in driver.current_url and driver.current_url != rec_url_before:
print(f"⏳ [SPA NAVIGATION DETECTED] URL changed during recovery from {rec_url_before} to {driver.current_url}. Waiting 4 seconds for page elements to render...")
time.sleep(4.0)
rec_action["_recovery_step"] = recovery_attempts
if rec_result.startswith("failed"):
rec_action["_failed"] = True
rec_action["_error"] = rec_result
action_history.append(rec_action)
else:
consec_fails = 0
if rec_act_type in ["type", "type_and_enter", "upload_file"]:
driver.audit_has_run = False
action_history.append(rec_action)
if rec_act_type == "upload_file":
time.sleep(5.0)
time.sleep(2.0)
print(f"🚪 [RECOVERY EXIT] Exited recovery sub-loop. Recovered={step_recovered}")
if step_recovered:
result = "ok"
else:
consec_fails = 0
if not result.startswith("failed"):
if act_type in ["type", "type_and_enter", "upload_file"]:
driver.audit_has_run = False
# Check for Terminal Publish Click (Dead-end auto-complete)
if act_type == "click" and element_id:
try:
# Extract the specific line in DOM snapshot corresponding to the element_id
pattern = rf"\[{element_id}\]\s*<[^>]*>:\s*(.*)"
match = re.search(pattern, snapshot, re.IGNORECASE)
if match:
el_text = match.group(1).lower()
# It must contain terminal keywords and be inside the popup/modal or composer URL
terminal_keywords = ["publish", "post", "share", "submit", "save"]
is_inside_popup = "inside popup" in el_text or "post" in driver.current_url.lower() or "create" in driver.current_url.lower() or "pin-builder" in driver.current_url.lower()
# Medium specific: exclude the header "Publish" button (which is not inside popup)
if "medium.com" in driver.current_url.lower() and "inside popup" not in el_text:
if el_text.strip() in ["publish", "publish dropdown"]:
is_inside_popup = False
# Exclude feed-level or non-terminal triggers
exclude_keywords = ["what's on your mind", "create post", "photo/video", "live video", "reel"]
if (is_inside_popup
and any(kw in el_text for kw in terminal_keywords)
and not any(ex in el_text for ex in exclude_keywords)):
print(f"\n🎉 [AUTO TERMINAL COMPLETE] Final publish click detected on element ID {element_id} ('{el_text}')!")
print("⏳ Waiting 8 seconds to ensure backend request has fully processed before closing...")
time.sleep(8.0)
result = "task_done"
except Exception:
pass
# 🚀 SMART WAIT FOR PUBLISH/POST/UPLOAD ACTIONS
is_heavy_action = False
if act_type == "upload_file":
is_heavy_action = True
print("⏳ [HEAVY ACTION] File upload detected. Waiting 5 seconds for file upload to process...", flush=True)
elif act_type == "click":
# 1. Check in decided action dict (thought/keywords)
act_str = str(action).lower()
heavy_keywords = ["publish", "post", "upload", "save", "share", "create", "done", "next", "submit"]
if any(kw in act_str for kw in heavy_keywords):
is_heavy_action = True
print(f"⏳ [HEAVY ACTION] Click matches heavy/publish keywords in action payload! Waiting 5 seconds for backend to process...", flush=True)
# 2. Fallback check inside DOM snapshot
elif element_id:
try:
# Extract the specific line in DOM snapshot corresponding to the element_id
pattern = rf"\[{element_id}\]\s*<[^>]*>:\s*(.*)"
match = re.search(pattern, snapshot, re.IGNORECASE)
if match:
el_text = match.group(1).lower()
if any(kw in el_text for kw in heavy_keywords):
is_heavy_action = True
print(f"⏳ [HEAVY ACTION] Click matches heavy/publish keyword in snapshot: '{el_text}'. Waiting 5 seconds for backend to process...", flush=True)
except Exception as e:
pass
if is_heavy_action:
time.sleep(5.0)
action_history.append(action)
logs.append(f"Action: {act_type} | Status: {result}")
if result == "task_done":
post_res = run_post_verification(driver, goal, action_history)
if post_res["verified"]:
print(f"\n✅ TASK COMPLETE! Gemma ne apna goal achieve kar liya.")
break
else:
print(f"🔧 [POST-AUDIT CORRECTION] Task not complete yet. Executing corrective action: {post_res['corrective_action']}")
action = post_res["corrective_action"]
act_type = str(action.get("action", "")).lower()
element_id = action.get("element_id")
result = execute_agent_action(driver, action)
with open(debug_log_path, "a", encoding="utf-8") as f:
f.write(f"🎬 POST-AUDIT CORRECTION RESULT: {result}\n")
f.write("=" * 80 + "\n\n")
if result.startswith("failed"):
action["_failed"] = True
action["_error"] = result
consec_fails += 1
else:
consec_fails = 0
# ═══════════════════════════════════════════════════
# 🧠 SMART AUTO-COMPLETION DETECTION (Gemma pe depend nahi)
# ═══════════════════════════════════════════════════
# Check if URL changed significantly after a click or type_and_enter
if attempt > 1 and act_type in ["click", "type_and_enter"] and not result.startswith("failed"):
# Pinterest specific: agar builder se redirect hoke /pin/ pe gaya
current_url = driver.current_url
if current_url != start_url and ("pin-builder" in start_url and "pin-builder" not in current_url):
print(f"\n✅ AUTO TASK_DONE: URL redirect detected after action!")
print(f" {start_url} → {current_url}")
post_res = run_post_verification(driver, goal, action_history)
if post_res["verified"]:
logs.append("AUTO: URL redirect detected — task complete")
break
else:
print(f"🔧 [POST-AUDIT CORRECTION] Redirect autocomplete aborted. Executing: {post_res['corrective_action']}")
action = post_res["corrective_action"]
act_type = str(action.get("action", "")).lower()
element_id = action.get("element_id")
result = execute_agent_action(driver, action)
with open(debug_log_path, "a", encoding="utf-8") as f:
f.write(f"🎬 POST-AUDIT CORRECTION RESULT: {result}\n")
f.write("=" * 80 + "\n\n")
if result.startswith("failed"):
action["_failed"] = True
action["_error"] = result
consec_fails += 1
else:
consec_fails = 0
# LinkedIn specific: connection request sent (DISABLED TO PREVENT PREMATURE AUTOMATIC TERMINATIONS)
# if "linkedin.com" in current_url and act_type == "click":
# # Check if previous actions include form filling or clicks and the page has changed state
# has_prior_interactions = any(a.get("action") in ["type", "type_and_enter", "click"] for a in action_history[:-1])
# if has_prior_interactions and action_history[-1].get("action") == "click":
# # Let's see if the success toast is present in the DOM snapshot
# if "PAGE TEXT/STATUS" in snapshot and ("Pending" in snapshot or "Sent" in snapshot or "Saved" in snapshot):
# print(f"\n✅ AUTO TASK_DONE: Success status found in DOM after click!")
# post_res = run_post_verification(driver, goal, action_history)
# if post_res["verified"]:
# logs.append("AUTO: Success status detected — task complete")
# break
# else:
# print(f"🔧 [POST-AUDIT CORRECTION] LinkedIn autocomplete aborted. Executing: {post_res['corrective_action']}")
# action = post_res["corrective_action"]
# act_type = str(action.get("action", "")).lower()
# element_id = action.get("element_id")
# result = execute_action(driver, action)
# with open(debug_log_path, "a", encoding="utf-8") as f:
# f.write(f"🎬 POST-AUDIT CORRECTION RESULT: {result}\n")
# f.write("=" * 80 + "\n\n")
# if result.startswith("failed"):
# action["_failed"] = True
# action["_error"] = result
# consec_fails += 1
# else:
# consec_fails = 0
pass
# SAME BUTTON CLICKED TWICE → Agent stuck after publish
if act_type == "click" and not result.startswith("failed") and len(action_history) >= 2:
prev = action_history[-2]
if (str(prev.get("action", "")).lower() == "click"
and prev.get("element_id") == element_id
and not prev.get("_failed")):
# Check: kya pehle form fill hua tha?
has_form_actions = any(
a.get("action") in ("type", "type_and_enter", "upload_file") and not a.get("_failed")
for a in action_history[:-2]
)
if has_form_actions:
print(f"\n✅ AUTO TASK_DONE: Same button (ID:{element_id}) clicked twice — assuming task is complete!")
post_res = run_post_verification(driver, goal, action_history)
if post_res["verified"]:
logs.append("AUTO: Repeated final click detected — task complete")
# ⏳ Wait 5 seconds to ensure the final request has completed before closing the browser
print("⏳ [POST-PUBLISH AUTO WAIT] Waiting 5 seconds for the publish request to finalize before closing the browser...", flush=True)
time.sleep(5.0)
break
else:
print(f"🔧 [POST-AUDIT CORRECTION] Repeated click autocomplete aborted. Executing: {post_res['corrective_action']}")
action = post_res["corrective_action"]
act_type = str(action.get("action", "")).lower()
element_id = action.get("element_id")
result = execute_action(driver, action)
with open(debug_log_path, "a", encoding="utf-8") as f:
f.write(f"🎬 POST-AUDIT CORRECTION RESULT: {result}\n")
f.write("=" * 80 + "\n\n")
if result.startswith("failed"):
action["_failed"] = True
action["_error"] = result
consec_fails += 1
else:
consec_fails = 0
if consec_fails >= 4:
print("\n⚠️ Agent is stuck. Stopping execution.")
break
# 🚀 No sleep pauses: execution runs at maximum speed back-to-back.
pass
return {"success": True, "summary": "Execution Finished", "logs": logs}
except KeyboardInterrupt:
print("\n⛔ Interrupted by user.")
return {"success": False, "summary": "Interrupted.", "logs": logs}
except Exception as e:
print(f"\n❌ Agent error: {e}")
return {"success": False, "summary": str(e), "logs": logs}
finally:
print("\n🛑 Browser close ho raha hai...")
driver.quit()
# 🗑️ Cleanup: publish ke baad local image and all screenshots/images in the directory delete karo
if local_image_path and os.path.exists(local_image_path):
try:
os.remove(local_image_path)
print(f"🗑️ Local image deleted: {local_image_path}")
except Exception as del_err:
print(f"⚠️ Local image delete nahi hui: {del_err}")
try:
import glob
dir_path = os.path.dirname(os.path.abspath(__file__))
extensions = ["*.png", "*.jpg", "*.jpeg", "*.webp", "*.gif"]
for ext in extensions:
for img_file in glob.glob(os.path.join(dir_path, ext)):
try:
if os.path.exists(img_file):
os.remove(img_file)
print(f"🗑️ Cleaned up image: {os.path.basename(img_file)}")
except Exception as del_err:
print(f"⚠️ Failed to delete image {os.path.basename(img_file)}: {del_err}")
except Exception as glob_err:
print(f"⚠️ Failed to scan images for cleanup: {glob_err}")
# ---------------- TESTING ----------------
if __name__ == "__main__":
if len(sys.argv) > 1:
task = sys.argv[1]
print(f"📋 Running agent with command-line task: {task}")
else:
task = (
"Go to LinkedIn (https://www.linkedin.com/). Search for 'web developer' in the search bar. "
"Filter results by 'People' if not already on the People search tab. "
"Find and send connection requests to 5 DIFFERENT web developers. "
"CRITICAL RULES TO AVOID GETTING STUCK IN A LOOP:\n"
"1. DO NOT search again if search results for 'web developer' are already visible on screen.\n"
"2. Locate a 'Connect' button for a person you have NOT connected with yet.\n"
"3. Click 'Connect'. If a popup/modal appears asking 'Add a note' or 'Send without a note', click 'Send without a note' (or 'Send').\n"
"4. Once a connection request is sent to a person, scroll down to find the NEXT (different) person. DO NOT click the same person or button again!\n"
"5. Keep track of how many connection requests you have sent (Count: 1, 2, 3, 4, 5).\n"
"6. Repeat this until you have sent connection requests to 5 DIFFERENT web developers.\n"
"7. As soon as 5 connection requests have been successfully sent, call 'task_done' immediately."
)
run_web_agent(task, headless=False)