-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPKG-INFO
More file actions
593 lines (452 loc) · 18.4 KB
/
PKG-INFO
File metadata and controls
593 lines (452 loc) · 18.4 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
Metadata-Version: 2.4
Name: sentience-python
Version: 0.12.0
Summary: Python SDK for Sentience AI Agent Browser Automation
Author: Sentience Team
License: MIT
Project-URL: Homepage, https://github.com/SentienceAPI/sentience-python
Project-URL: Repository, https://github.com/SentienceAPI/sentience-python
Project-URL: Issues, https://github.com/SentienceAPI/sentience-python/issues
Keywords: browser-automation,playwright,ai-agent,web-automation,sentience
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: playwright>=1.40.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: jsonschema>=4.0.0
Requires-Dist: requests>=2.31.0
Requires-Dist: playwright-stealth>=1.0.6
Requires-Dist: markdownify>=0.11.6
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Dynamic: license-file
# Sentience Python SDK
The SDK is open under ELv2; the core semantic geometry and reliability logic runs in Sentience-hosted services.
## Installation
```bash
pip install -e .
# Install Playwright browsers (required)
playwright install chromium
# For LLM Agent features (optional)
pip install openai # For OpenAI models
pip install anthropic # For Claude models
pip install transformers torch # For local LLMs
```
## Quick Start: Choose Your Abstraction Level
Sentience SDK offers **three abstraction levels** - use what fits your needs:
### 🎯 **Level 3: Natural Language (Easiest)** - For non-technical users
```python
from sentience import SentienceBrowser, ConversationalAgent
from sentience.llm_provider import OpenAIProvider
browser = SentienceBrowser()
llm = OpenAIProvider(api_key="your-key", model="gpt-4o")
agent = ConversationalAgent(browser, llm)
with browser:
response = agent.execute("Search for magic mouse on google.com")
print(response)
# → "I searched for 'magic mouse' and found several results.
# The top result is from amazon.com selling Magic Mouse 2 for $79."
```
**Best for:** End users, chatbots, no-code platforms
**Code required:** 3-5 lines
**Technical knowledge:** None
### ⚙️ **Level 2: Technical Commands (Recommended)** - For AI developers
```python
from sentience import SentienceBrowser, SentienceAgent
from sentience.llm_provider import OpenAIProvider
browser = SentienceBrowser()
llm = OpenAIProvider(api_key="your-key", model="gpt-4o")
agent = SentienceAgent(browser, llm)
with browser:
browser.page.goto("https://google.com")
agent.act("Click the search box")
agent.act("Type 'magic mouse' into the search field")
agent.act("Press Enter key")
```
**Best for:** Building AI agents, automation scripts
**Code required:** 10-15 lines
**Technical knowledge:** Medium (Python basics)
### 🔧 **Level 1: Direct SDK (Most Control)** - For production automation
```python
from sentience import SentienceBrowser, snapshot, find, click
with SentienceBrowser(headless=False) as browser:
browser.page.goto("https://example.com")
# Take snapshot - captures all interactive elements
snap = snapshot(browser)
print(f"Found {len(snap.elements)} elements")
# Find and click a link using semantic selectors
link = find(snap, "role=link text~'More information'")
if link:
result = click(browser, link.id)
print(f"Click success: {result.success}")
```
**Best for:** Maximum control, performance-critical apps
**Code required:** 20-50 lines
**Technical knowledge:** High (SDK API, selectors)
## Real-World Example: Amazon Shopping Bot
This example demonstrates navigating Amazon, finding products, and adding items to cart:
```python
from sentience import SentienceBrowser, snapshot, find, click
import time
with SentienceBrowser(headless=False) as browser:
# Navigate to Amazon Best Sellers
browser.goto("https://www.amazon.com/gp/bestsellers/", wait_until="domcontentloaded")
time.sleep(2) # Wait for dynamic content
# Take snapshot and find products
snap = snapshot(browser)
print(f"Found {len(snap.elements)} elements")
# Find first product in viewport using spatial filtering
products = [
el for el in snap.elements
if el.role == "link"
and el.visual_cues.is_clickable
and el.in_viewport
and not el.is_occluded
and el.bbox.y < 600 # First row
]
if products:
# Sort by position (left to right, top to bottom)
products.sort(key=lambda e: (e.bbox.y, e.bbox.x))
first_product = products[0]
print(f"Clicking: {first_product.text}")
result = click(browser, first_product.id)
# Wait for product page
browser.page.wait_for_load_state("networkidle")
time.sleep(2)
# Find and click "Add to Cart" button
product_snap = snapshot(browser)
add_to_cart = find(product_snap, "role=button text~'add to cart'")
if add_to_cart:
cart_result = click(browser, add_to_cart.id)
print(f"Added to cart: {cart_result.success}")
```
**See the complete tutorial**: [Amazon Shopping Guide](../docs/AMAZON_SHOPPING_GUIDE.md)
## Core Features
### Browser Control
- **`SentienceBrowser`** - Playwright browser with Sentience extension pre-loaded
- **`browser.goto(url)`** - Navigate with automatic extension readiness checks
- Automatic bot evasion and stealth mode
- Configurable headless/headed mode
### Snapshot - Intelligent Page Analysis
- **`snapshot(browser, screenshot=True)`** - Capture page state with AI-ranked elements
- Returns semantic elements with roles, text, importance scores, and bounding boxes
- Optional screenshot capture (PNG/JPEG)
- Pydantic models for type safety
- **`snapshot.save(filepath)`** - Export to JSON
**Example:**
```python
snap = snapshot(browser, screenshot=True)
# Access structured data
print(f"URL: {snap.url}")
print(f"Viewport: {snap.viewport.width}x{snap.viewport.height}")
print(f"Elements: {len(snap.elements)}")
# Iterate over elements
for element in snap.elements:
print(f"{element.role}: {element.text} (importance: {element.importance})")
```
### Query Engine - Semantic Element Selection
- **`query(snapshot, selector)`** - Find all matching elements
- **`find(snapshot, selector)`** - Find single best match (by importance)
- Powerful query DSL with multiple operators
**Query Examples:**
```python
# Find by role and text
button = find(snap, "role=button text='Sign in'")
# Substring match (case-insensitive)
link = find(snap, "role=link text~'more info'")
# Spatial filtering
top_left = find(snap, "bbox.x<=100 bbox.y<=200")
# Multiple conditions (AND logic)
primary_btn = find(snap, "role=button clickable=true visible=true importance>800")
# Prefix/suffix matching
starts_with = find(snap, "text^='Add'")
ends_with = find(snap, "text$='Cart'")
# Numeric comparisons
important = query(snap, "importance>=700")
first_row = query(snap, "bbox.y<600")
```
**📖 [Complete Query DSL Guide](docs/QUERY_DSL.md)** - All operators, fields, and advanced patterns
### Actions - Interact with Elements
- **`click(browser, element_id)`** - Click element by ID
- **`click_rect(browser, rect)`** - Click at center of rectangle (coordinate-based)
- **`type_text(browser, element_id, text)`** - Type into input fields
- **`press(browser, key)`** - Press keyboard keys (Enter, Escape, Tab, etc.)
All actions return `ActionResult` with success status, timing, and outcome:
```python
result = click(browser, element.id)
print(f"Success: {result.success}")
print(f"Outcome: {result.outcome}") # "navigated", "dom_updated", "error"
print(f"Duration: {result.duration_ms}ms")
print(f"URL changed: {result.url_changed}")
```
**Coordinate-based clicking:**
```python
from sentience import click_rect
# Click at center of rectangle (x, y, width, height)
click_rect(browser, {"x": 100, "y": 200, "w": 50, "h": 30})
# With visual highlight (default: red border for 2 seconds)
click_rect(browser, {"x": 100, "y": 200, "w": 50, "h": 30}, highlight=True, highlight_duration=2.0)
# Using element's bounding box
snap = snapshot(browser)
element = find(snap, "role=button")
if element:
click_rect(browser, {
"x": element.bbox.x,
"y": element.bbox.y,
"w": element.bbox.width,
"h": element.bbox.height
})
```
### Wait & Assertions
- **`wait_for(browser, selector, timeout=5.0, interval=None, use_api=None)`** - Wait for element to appear
- **`expect(browser, selector)`** - Assertion helper with fluent API
**Examples:**
```python
# Wait for element (auto-detects optimal interval based on API usage)
result = wait_for(browser, "role=button text='Submit'", timeout=10.0)
if result.found:
print(f"Found after {result.duration_ms}ms")
# Use local extension with fast polling (0.25s interval)
result = wait_for(browser, "role=button", timeout=5.0, use_api=False)
# Use remote API with network-friendly polling (1.5s interval)
result = wait_for(browser, "role=button", timeout=5.0, use_api=True)
# Custom interval override
result = wait_for(browser, "role=button", timeout=5.0, interval=0.5, use_api=False)
# Semantic wait conditions
wait_for(browser, "clickable=true", timeout=5.0) # Wait for clickable element
wait_for(browser, "importance>100", timeout=5.0) # Wait for important element
wait_for(browser, "role=link visible=true", timeout=5.0) # Wait for visible link
# Assertions
expect(browser, "role=button text='Submit'").to_exist(timeout=5.0)
expect(browser, "role=heading").to_be_visible()
expect(browser, "role=button").to_have_text("Submit")
expect(browser, "role=link").to_have_count(10)
```
### Content Reading
- **`read(browser, format="text|markdown|raw")`** - Extract page content
- `format="text"` - Plain text extraction
- `format="markdown"` - High-quality markdown conversion (uses markdownify)
- `format="raw"` - Cleaned HTML (default)
**Example:**
```python
from sentience import read
# Get markdown content
result = read(browser, format="markdown")
print(result["content"]) # Markdown text
# Get plain text
result = read(browser, format="text")
print(result["content"]) # Plain text
```
### Screenshots
- **`screenshot(browser, format="png|jpeg", quality=80)`** - Standalone screenshot capture
- Returns base64-encoded data URL
- PNG or JPEG format
- Quality control for JPEG (1-100)
**Example:**
```python
from sentience import screenshot
import base64
# Capture PNG screenshot
data_url = screenshot(browser, format="png")
# Save to file
image_data = base64.b64decode(data_url.split(",")[1])
with open("screenshot.png", "wb") as f:
f.write(image_data)
# JPEG with quality control (smaller file size)
data_url = screenshot(browser, format="jpeg", quality=85)
```
## Element Properties
Elements returned by `snapshot()` have the following properties:
```python
element.id # Unique identifier for interactions
element.role # ARIA role (button, link, textbox, heading, etc.)
element.text # Visible text content
element.importance # AI importance score (0-1000)
element.bbox # Bounding box (x, y, width, height)
element.visual_cues # Visual analysis (is_primary, is_clickable, background_color)
element.in_viewport # Is element visible in current viewport?
element.is_occluded # Is element covered by other elements?
element.z_index # CSS stacking order
```
## Query DSL Reference
### Basic Operators
| Operator | Description | Example |
|----------|-------------|---------|
| `=` | Exact match | `role=button` |
| `!=` | Exclusion | `role!=link` |
| `~` | Substring (case-insensitive) | `text~'sign in'` |
| `^=` | Prefix match | `text^='Add'` |
| `$=` | Suffix match | `text$='Cart'` |
| `>`, `>=` | Greater than | `importance>500` |
| `<`, `<=` | Less than | `bbox.y<600` |
### Supported Fields
- **Role**: `role=button|link|textbox|heading|...`
- **Text**: `text`, `text~`, `text^=`, `text$=`
- **Visibility**: `clickable=true|false`, `visible=true|false`
- **Importance**: `importance`, `importance>=N`, `importance<N`
- **Position**: `bbox.x`, `bbox.y`, `bbox.width`, `bbox.height`
- **Layering**: `z_index`
## Examples
See the `examples/` directory for complete working examples:
- **`hello.py`** - Extension bridge verification
- **`basic_agent.py`** - Basic snapshot and element inspection
- **`query_demo.py`** - Query engine demonstrations
- **`wait_and_click.py`** - Waiting for elements and performing actions
- **`read_markdown.py`** - Content extraction and markdown conversion
## Testing
```bash
# Run all tests
pytest tests/
# Run specific test file
pytest tests/test_snapshot.py
# Run with verbose output
pytest -v tests/
```
## Configuration
### Viewport Size
Default viewport is **1280x800** pixels. You can customize it using Playwright's API:
```python
with SentienceBrowser(headless=False) as browser:
# Set custom viewport before navigating
browser.page.set_viewport_size({"width": 1920, "height": 1080})
browser.goto("https://example.com")
```
### Headless Mode
```python
# Headed mode (default in dev, shows browser window)
browser = SentienceBrowser(headless=False)
# Headless mode (default in CI environments)
browser = SentienceBrowser(headless=True)
# Auto-detect based on environment
browser = SentienceBrowser() # headless=True if CI=true, else False
```
## Best Practices
### 1. Wait for Dynamic Content
```python
browser.goto("https://example.com", wait_until="domcontentloaded")
time.sleep(1) # Extra buffer for AJAX/animations
```
### 2. Use Multiple Strategies for Finding Elements
```python
# Try exact match first
btn = find(snap, "role=button text='Add to Cart'")
# Fallback to fuzzy match
if not btn:
btn = find(snap, "role=button text~='cart'")
```
### 3. Check Element Visibility Before Clicking
```python
if element.in_viewport and not element.is_occluded:
click(browser, element.id)
```
### 4. Handle Navigation
```python
result = click(browser, link_id)
if result.url_changed:
browser.page.wait_for_load_state("networkidle")
```
### 5. Use Screenshots Sparingly
```python
# Fast - no screenshot (only element data)
snap = snapshot(browser)
# Slower - with screenshot (for debugging/verification)
snap = snapshot(browser, screenshot=True)
```
## Troubleshooting
### "Extension failed to load"
**Solution:** Build the extension first:
```bash
cd sentience-chrome
./build.sh
```
### "Element not found"
**Solutions:**
- Ensure page is loaded: `browser.page.wait_for_load_state("networkidle")`
- Use `wait_for()`: `wait_for(browser, "role=button", timeout=10)`
- Debug elements: `print([el.text for el in snap.elements])`
### Button not clickable
**Solutions:**
- Check visibility: `element.in_viewport and not element.is_occluded`
- Scroll to element: `browser.page.evaluate(f"window.sentience_registry[{element.id}].scrollIntoView()")`
## Advanced Features (v0.12.0+)
### Agent Tracing & Debugging
The SDK now includes built-in tracing infrastructure for debugging and analyzing agent behavior:
```python
from sentience import SentienceBrowser, SentienceAgent
from sentience.llm_provider import OpenAIProvider
from sentience.tracing import Tracer, JsonlTraceSink
from sentience.agent_config import AgentConfig
# Create tracer to record agent execution
tracer = Tracer(
run_id="my-agent-run-123",
sink=JsonlTraceSink("trace.jsonl")
)
# Configure agent behavior
config = AgentConfig(
snapshot_limit=50,
temperature=0.0,
max_retries=1,
capture_screenshots=True
)
browser = SentienceBrowser()
llm = OpenAIProvider(api_key="your-key", model="gpt-4o")
# Pass tracer and config to agent
agent = SentienceAgent(browser, llm, tracer=tracer, config=config)
with browser:
browser.page.goto("https://example.com")
# All actions are automatically traced
agent.act("Click the sign in button")
agent.act("Type 'user@example.com' into email field")
# Trace events saved to trace.jsonl
# Events: step_start, snapshot, llm_query, action, step_end, error
```
**Trace Events Captured:**
- `step_start` - Agent begins executing a goal
- `snapshot` - Page state captured
- `llm_query` - LLM decision made (includes tokens, model, response)
- `action` - Action executed (click, type, press)
- `step_end` - Step completed successfully
- `error` - Error occurred during execution
**Use Cases:**
- Debug why agent failed or got stuck
- Analyze token usage and costs
- Replay agent sessions
- Train custom models from successful runs
- Monitor production agents
### Snapshot Utilities
New utility functions for working with snapshots:
```python
from sentience import snapshot
from sentience.utils import compute_snapshot_digests, canonical_snapshot_strict
from sentience.formatting import format_snapshot_for_llm
snap = snapshot(browser)
# Compute snapshot fingerprints (detect page changes)
digests = compute_snapshot_digests(snap.elements)
print(f"Strict digest: {digests['strict']}") # Changes when text changes
print(f"Loose digest: {digests['loose']}") # Only changes when layout changes
# Format snapshot for LLM prompts
llm_context = format_snapshot_for_llm(snap, limit=50)
print(llm_context)
# Output: [1] <button> "Sign In" {PRIMARY,CLICKABLE} @ (100,50) (Imp:10)
```
## Documentation
- **📖 [Amazon Shopping Guide](../docs/AMAZON_SHOPPING_GUIDE.md)** - Complete tutorial with real-world example
- **📖 [Query DSL Guide](docs/QUERY_DSL.md)** - Advanced query patterns and operators
- **📄 [API Contract](../spec/SNAPSHOT_V1.md)** - Snapshot API specification
- **📄 [Type Definitions](../spec/sdk-types.md)** - TypeScript/Python type definitions
## License
📜 **License**
This SDK is licensed under the **Elastic License 2.0 (ELv2)**.
The Elastic License 2.0 allows you to use, modify, and distribute this SDK for internal, research, and non-competitive purposes. It **does not permit offering this SDK or a derivative as a hosted or managed service**, nor using it to build a competing product or service.
### Important Notes
- This SDK is a **client-side library** that communicates with proprietary Sentience services and browser components.
- The Sentience backend services (including semantic geometry grounding, ranking, visual cues, and trace processing) are **not open source** and are governed by Sentience’s Terms of Service.
- Use of this SDK does **not** grant rights to operate, replicate, or reimplement Sentience’s hosted services.
For commercial usage, hosted offerings, or enterprise deployments, please contact Sentience to obtain a commercial license.
See the full license text in [`LICENSE`](./LICENSE.md).