-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpratyush_workspace.py
More file actions
530 lines (428 loc) · 22.6 KB
/
Copy pathpratyush_workspace.py
File metadata and controls
530 lines (428 loc) · 22.6 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
#!/usr/bin/env python3
"""
Anay's experimental workspace.
Full REPL with all standard commands PLUS experimental features.
"""
from main import BrowserAgent, run_repl
from browser import console
from commands import build_command_registry
import sys
class ExperimentalAgent(BrowserAgent):
"""
Extends the standard BrowserAgent with experimental features.
Inherits ALL standard functionality + adds your test features.
"""
def wiki_test(self, search_term="Python programming language"):
"""
Simple test: Navigate to Wikipedia and search.
Usage: wiki_test "search term"
"""
console.print(f"[yellow]Testing Wikipedia navigation with: {search_term}[/yellow]")
try:
# Go to Wikipedia
self.go_to("wikipedia.org")
console.print("[green]Step 1: Navigated to Wikipedia[/green]")
# Wait a moment for page to load
self.wait_for_load()
console.print("[green]Step 2: Page loaded[/green]")
# Scan for inputs
self.scan("inputs")
console.print("[green]Step 3: Scanned page[/green]")
# Type in search box (usually element 1)
self.type(1, search_term)
console.print(f"[green]Step 4: Typed '{search_term}'[/green]")
# Press Enter to search
self.press_key("Enter")
console.print("[green]Step 5: Pressed Enter[/green]")
# Wait for results
self.wait_for_load()
console.print("[green]Test complete![/green]")
return True
except Exception as e:
console.print(f"[red]Test failed:[/red] {e}")
return False
def screenshot(self, filename: str = None):
"""
Take a full page screenshot.
Usage: screenshot [filename]
"""
try:
import os
import re
from datetime import datetime
from urllib.parse import urlparse
# Create screenshots folder if it doesn't exist
screenshots_dir = "screenshots"
if not os.path.exists(screenshots_dir):
os.makedirs(screenshots_dir)
# Generate filename from URL if not provided
if filename is None:
# Get current URL
current_url = self.page.url
# Parse URL to get domain and path
parsed_url = urlparse(current_url)
domain = parsed_url.netloc.replace('www.', '')
path = parsed_url.path.strip('/')
# Create a clean filename from domain and path
# Replace non-alphanumeric characters with underscores
clean_domain = re.sub(r'[^\w\-]', '_', domain)
clean_path = re.sub(r'[^\w\-]', '_', path) if path else 'home'
# Add timestamp to ensure uniqueness
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Combine into filename
if clean_path and clean_path != 'home':
filename = f"{clean_domain}_{clean_path}_{timestamp}.png"
else:
filename = f"{clean_domain}_{timestamp}.png"
elif not filename.endswith('.png'):
filename += '.png'
# Full path to save in screenshots folder
filepath = os.path.join(screenshots_dir, filename)
# Take screenshot
self.page.screenshot(path=filepath, full_page=True)
console.print(f"[green]Screenshot saved:[/green] {filepath}")
self.log_action("screenshot", filepath, success=True)
return True
except Exception as e:
console.print(f"[red]Screenshot failed:[/red] {e}")
self.log_action("screenshot", str(e), success=False)
return False
def clear_cache(self):
"""
Clear browser cache and cookies.
Usage: clear_cache
"""
try:
console.print("[yellow]Clearing cache and cookies...[/yellow]")
# Store current URL to reload after clearing
current_url = self.page.url if self.page else None
# Clear cookies
self.context.clear_cookies()
# Clear storage (localStorage, sessionStorage, etc.)
if self.page:
self.page.evaluate("""
() => {
localStorage.clear();
sessionStorage.clear();
}
""")
console.print("[green]Cache and cookies cleared[/green]")
# Reload current page if there was one
if current_url and current_url != "about:blank":
console.print(f"[dim]Reloading: {current_url}[/dim]")
self.page.reload()
self.log_action("clear_cache", "success", success=True)
return True
except Exception as e:
console.print(f"[red]Failed to clear cache:[/red] {e}")
self.log_action("clear_cache", str(e), success=False)
return False
def read_page(self, focus: str = "overview", save: bool = True):
"""
Extract page content with intelligent filtering.
Args:
focus: What to extract
- "overview" - Title, headings, first paragraphs (auto-filtered by importance)
- "forms" - Form fields, labels, inputs, buttons
- "content" - Main article/text content (full)
- "navigation" - Menus, links, site structure
- "all" - Everything (no limit)
save: Whether to save to file (default True)
Usage:
read_page - Overview (smart filtered)
read_page content - Full content
read_page forms - Just forms
read_page all - Everything
"""
try:
import os
import re
from datetime import datetime
# JavaScript extraction with importance scoring
content_data = self.page.evaluate(f"""
(focus) => {{
const result = {{
title: document.title || 'Untitled',
url: window.location.href,
focus: focus,
sections: []
}};
// Importance scoring for elements
const getImportance = (element) => {{
let score = 0;
const tag = element.tagName.toLowerCase();
// Headers are important
if (/^h[1-6]$/.test(tag)) {{
score = 10 - parseInt(tag[1]); // h1=9, h2=8, etc.
}}
// Main content areas
if (element.closest('main, article, [role="main"]')) score += 5;
// Forms and inputs are important
if (tag === 'form' || tag === 'input' || tag === 'textarea') score += 8;
if (element.closest('form')) score += 3;
// First elements matter more
const rect = element.getBoundingClientRect();
if (rect.top < 500) score += 2; // Above fold
// Text length matters
const text = element.innerText?.trim() || '';
if (text.length > 100 && text.length < 500) score += 2;
if (text.length > 500) score += 1;
return score;
}};
// Get main content area
const mainArea = document.querySelector('main, article, [role="main"]') || document.body;
// Focus-specific extraction
if (focus === 'forms') {{
// Extract form fields with context
const forms = Array.from(document.querySelectorAll('form, input, textarea, select, button'));
const formData = forms.map(el => {{
const label = el.labels?.[0]?.innerText ||
el.getAttribute('placeholder') ||
el.getAttribute('aria-label') ||
el.name || 'unlabeled';
return {{
type: el.tagName.toLowerCase(),
label: label.substring(0, 100),
importance: 10
}};
}});
result.sections = [{{
level: 1,
title: 'Form Elements',
content: formData.map(f => `[${{f.type}}] ${{f.label}}`),
importance: 10
}}];
}} else if (focus === 'navigation') {{
// Extract navigation structure
const navs = Array.from(document.querySelectorAll('nav, [role="navigation"], header'));
const links = navs.flatMap(nav =>
Array.from(nav.querySelectorAll('a')).map(a => a.innerText.trim())
).filter(t => t.length > 0 && t.length < 50);
result.sections = [{{
level: 1,
title: 'Navigation',
content: links,
importance: 8
}}];
}} else {{
// Extract content without cloning (prevents losing elements)
const sections = [];
let currentSection = null;
// Get all potentially interesting elements
const elements = Array.from(mainArea.querySelectorAll('h1, h2, h3, h4, h5, h6, p, div, ul, ol, blockquote, article, section'));
// Filter out navigation/noise early
const contentElements = elements.filter(el => {{
// Skip if inside nav, header, footer, ads
if (el.closest('nav, header, footer, .ad, [role="navigation"], aside')) {{
return false;
}}
// Skip if it's just a wrapper with no direct text
const directText = Array.from(el.childNodes)
.filter(n => n.nodeType === Node.TEXT_NODE)
.map(n => n.textContent.trim())
.join('');
const tag = el.tagName.toLowerCase();
// Always include headers
if (/^h[1-6]$/.test(tag)) {{
return el.innerText.trim().length > 0;
}}
// For other elements, need meaningful text
const text = el.innerText?.trim() || '';
return text.length >= 10;
}});
// Process elements
const processedElements = new Set();
for (const el of contentElements) {{
// Skip if we already processed this as part of a parent
if (processedElements.has(el)) continue;
const tagName = el.tagName.toLowerCase();
const importance = getImportance(el);
// Headers create sections
if (/^h[1-6]$/.test(tagName)) {{
const level = parseInt(tagName[1]);
const text = el.innerText.trim();
if (text.length > 0) {{
currentSection = {{
level: level,
title: text,
content: [],
importance: importance
}};
sections.push(currentSection);
processedElements.add(el);
}}
}}
// Content elements
else {{
// Get direct text content (avoid nested duplicates)
let text = el.innerText.trim();
// Skip if any child was already processed
const children = Array.from(el.querySelectorAll('*'));
if (children.some(child => processedElements.has(child))) {{
continue;
}}
if (text.length >= 10) {{
// Create default section if none exists
if (!currentSection) {{
currentSection = {{
level: 1,
title: 'Content',
content: [],
importance: 5
}};
sections.push(currentSection);
}}
currentSection.content.push({{
text: text,
importance: importance
}});
processedElements.add(el);
// Mark children as processed to avoid duplicates
children.forEach(child => processedElements.add(child));
}}
}}
}}
// Sort sections by importance if overview
if (focus === 'overview') {{
sections.sort((a, b) => b.importance - a.importance);
}}
result.sections = sections;
}}
return result;
}}
""", focus)
if not content_data or len(content_data.get('sections', [])) == 0:
console.print("[yellow]No content found - page might be empty or JS-heavy[/yellow]")
console.print(f"[dim]Title: {self.page.title()}, URL: {self.page.url}[/dim]")
return "No content found on page"
# Build markdown
lines = []
lines.append(f"# {content_data['title']}\n\n")
lines.append(f"**Source:** {content_data['url']}\n")
lines.append(f"**Focus:** {focus}\n")
lines.append(f"**Extracted:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
lines.append("---\n\n")
total_chars = 0
sections_included = 0
# Apply filtering based on focus
for section in content_data['sections']:
# For overview, only include high-importance sections
if focus == "overview" and section.get('importance', 0) < 5:
continue
level = section['level']
title = section['title']
content = section['content']
lines.append(f"{'#' * level} {title}\n\n")
# Handle content (could be strings or dicts with importance)
for item in content:
if isinstance(item, dict):
text = item['text']
importance = item.get('importance', 0)
# Skip low-importance content in overview
if focus == "overview" and importance < 3:
continue
else:
text = item
if len(text.strip()) >= 10:
clean = ' '.join(text.split())
lines.append(f"{clean}\n\n")
total_chars += len(clean)
sections_included += 1
# Stop at reasonable limit for overview
if focus == "overview" and total_chars > 2000:
lines.append("\n*[Overview truncated - use 'read_page content' for full text]*\n")
break
result_text = ''.join(lines)
# Save to file if requested
if save:
exports_dir = "text_exports"
if not os.path.exists(exports_dir):
os.makedirs(exports_dir)
# Generate filename
url_clean = re.sub(r'^https?://', '', content_data['url'])
url_clean = re.sub(r'[^\w\-_.]', '_', url_clean)
url_clean = re.sub(r'_+', '_', url_clean).strip('_')[:100]
filename = f"{url_clean}_{focus}.md"
filepath = os.path.join(exports_dir, filename)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(result_text)
console.print(f"[green]✓ Saved:[/green] {filepath}")
console.print(f"[dim]Sections: {sections_included}, Chars: {total_chars}[/dim]")
# Return string for LLM
console.print(f"[green]✓ Extracted {total_chars} chars ({sections_included} sections)[/green]")
return result_text
except Exception as e:
console.print(f"[red]Failed to read page:[/red] {e}")
import traceback
console.print(f"[dim]{traceback.format_exc()}[/dim]")
return f"Error reading page: {e}"
def run_experimental_repl(agent: ExperimentalAgent):
"""REPL with experimental commands added."""
commands = build_command_registry(agent)
# Add experimental commands
commands['wiki_test'] = agent.wiki_test # Add this line
commands['screenshot'] = agent.screenshot
commands['new_tab'] = agent.new_tab
commands['close_tab'] = agent.close_tab
commands['switch_tab'] = agent.switch_tab
commands['tabs'] = agent.tabs
commands['clear_cache'] = agent.clear_cache
commands['read_page'] = agent.read_page
console.print("\n[bold magenta] Experimental Workspace[/bold magenta]")
console.print("[yellow]Extra commands: wiki_test, screenshot, new_tab, close_tab, switch_tab, tabs, clear_cache, text_all[/yellow]")
console.print("[dim]Type 'help' for standard commands, 'exit' to quit[/dim]\n")
while True:
try:
command_line = console.input("[bold magenta]exp> [/bold magenta]").strip()
if not command_line:
continue
parts = agent._parse_command_line(command_line)
if parts is None:
continue
if not parts:
continue
cmd = parts[0].lower()
args = parts[1:]
if cmd in ['exit', 'quit', 'q']:
agent.log_command(cmd, args, success=True)
console.print("[yellow]Shutting down...[/yellow]")
break
if cmd in commands:
try:
commands[cmd](*args)
agent.log_command(cmd, args, success=True)
except TypeError as e:
console.print(f"[red]Invalid arguments:[/red] {e}")
agent.log_command(cmd, args, success=False, error=str(e))
except Exception as e:
console.print(f"[red]Command failed:[/red] {e}")
agent.log_command(cmd, args, success=False, error=str(e))
else:
console.print(f"[red]Unknown command:[/red] {cmd}")
console.print("[dim]Type 'help' to see commands[/dim]")
agent.log_command(cmd, args, success=False, error="Unknown command")
except KeyboardInterrupt:
console.print("\n[yellow]Use 'exit' to quit properly[/yellow]")
except EOFError:
console.print("\n[yellow]Shutting down...[/yellow]")
break
except Exception as e:
console.print(f"[red]Unexpected error:[/red] {e}")
import traceback
console.print("[dim]" + traceback.format_exc() + "[/dim]")
def main():
"""Entry point."""
headless = '--headless' in sys.argv
try:
with ExperimentalAgent(headless=headless) as agent:
run_experimental_repl(agent)
except KeyboardInterrupt:
console.print("\n[yellow]Interrupted[/yellow]")
sys.exit(130)
except Exception as e:
console.print(f"[bold red]Fatal error:[/bold red] {e}")
import traceback
console.print("[dim]" + traceback.format_exc() + "[/dim]")
sys.exit(1)
if __name__ == '__main__':
main()