|
1 | 1 | Metadata-Version: 2.4 |
2 | 2 | Name: sentience-python |
3 | | -Version: 0.11.0 |
| 3 | +Version: 0.12.0 |
4 | 4 | Summary: Python SDK for Sentience AI Agent Browser Automation |
5 | 5 | Author: Sentience Team |
6 | 6 | License: MIT |
@@ -485,6 +485,86 @@ cd sentience-chrome |
485 | 485 | - Check visibility: `element.in_viewport and not element.is_occluded` |
486 | 486 | - Scroll to element: `browser.page.evaluate(f"window.sentience_registry[{element.id}].scrollIntoView()")` |
487 | 487 |
|
| 488 | +## Advanced Features (v0.12.0+) |
| 489 | + |
| 490 | +### Agent Tracing & Debugging |
| 491 | + |
| 492 | +The SDK now includes built-in tracing infrastructure for debugging and analyzing agent behavior: |
| 493 | + |
| 494 | +```python |
| 495 | +from sentience import SentienceBrowser, SentienceAgent |
| 496 | +from sentience.llm_provider import OpenAIProvider |
| 497 | +from sentience.tracing import Tracer, JsonlTraceSink |
| 498 | +from sentience.agent_config import AgentConfig |
| 499 | + |
| 500 | +# Create tracer to record agent execution |
| 501 | +tracer = Tracer( |
| 502 | + run_id="my-agent-run-123", |
| 503 | + sink=JsonlTraceSink("trace.jsonl") |
| 504 | +) |
| 505 | + |
| 506 | +# Configure agent behavior |
| 507 | +config = AgentConfig( |
| 508 | + snapshot_limit=50, |
| 509 | + temperature=0.0, |
| 510 | + max_retries=1, |
| 511 | + capture_screenshots=True |
| 512 | +) |
| 513 | + |
| 514 | +browser = SentienceBrowser() |
| 515 | +llm = OpenAIProvider(api_key="your-key", model="gpt-4o") |
| 516 | + |
| 517 | +# Pass tracer and config to agent |
| 518 | +agent = SentienceAgent(browser, llm, tracer=tracer, config=config) |
| 519 | + |
| 520 | +with browser: |
| 521 | + browser.page.goto("https://example.com") |
| 522 | + |
| 523 | + # All actions are automatically traced |
| 524 | + agent.act("Click the sign in button") |
| 525 | + agent.act("Type 'user@example.com' into email field") |
| 526 | + |
| 527 | +# Trace events saved to trace.jsonl |
| 528 | +# Events: step_start, snapshot, llm_query, action, step_end, error |
| 529 | +``` |
| 530 | + |
| 531 | +**Trace Events Captured:** |
| 532 | +- `step_start` - Agent begins executing a goal |
| 533 | +- `snapshot` - Page state captured |
| 534 | +- `llm_query` - LLM decision made (includes tokens, model, response) |
| 535 | +- `action` - Action executed (click, type, press) |
| 536 | +- `step_end` - Step completed successfully |
| 537 | +- `error` - Error occurred during execution |
| 538 | + |
| 539 | +**Use Cases:** |
| 540 | +- Debug why agent failed or got stuck |
| 541 | +- Analyze token usage and costs |
| 542 | +- Replay agent sessions |
| 543 | +- Train custom models from successful runs |
| 544 | +- Monitor production agents |
| 545 | + |
| 546 | +### Snapshot Utilities |
| 547 | + |
| 548 | +New utility functions for working with snapshots: |
| 549 | + |
| 550 | +```python |
| 551 | +from sentience import snapshot |
| 552 | +from sentience.utils import compute_snapshot_digests, canonical_snapshot_strict |
| 553 | +from sentience.formatting import format_snapshot_for_llm |
| 554 | + |
| 555 | +snap = snapshot(browser) |
| 556 | + |
| 557 | +# Compute snapshot fingerprints (detect page changes) |
| 558 | +digests = compute_snapshot_digests(snap.elements) |
| 559 | +print(f"Strict digest: {digests['strict']}") # Changes when text changes |
| 560 | +print(f"Loose digest: {digests['loose']}") # Only changes when layout changes |
| 561 | + |
| 562 | +# Format snapshot for LLM prompts |
| 563 | +llm_context = format_snapshot_for_llm(snap, limit=50) |
| 564 | +print(llm_context) |
| 565 | +# Output: [1] <button> "Sign In" {PRIMARY,CLICKABLE} @ (100,50) (Imp:10) |
| 566 | +``` |
| 567 | + |
488 | 568 | ## Documentation |
489 | 569 |
|
490 | 570 | - **📖 [Amazon Shopping Guide](../docs/AMAZON_SHOPPING_GUIDE.md)** - Complete tutorial with real-world example |
|
0 commit comments