From ebc44d34c5d816690a5864805b657dce1ed878b9 Mon Sep 17 00:00:00 2001 From: rcholic Date: Fri, 2 Jan 2026 13:33:22 -0800 Subject: [PATCH 1/2] Phase 4: Modularize code --- sentience/__init__.py | 3 +- sentience/element_filter.py | 2 +- sentience/formatting.py | 62 ++------ sentience/utils/__init__.py | 41 ++++++ sentience/utils/browser.py | 47 +++++++ sentience/utils/element.py | 258 ++++++++++++++++++++++++++++++++++ sentience/utils/formatting.py | 60 ++++++++ 7 files changed, 418 insertions(+), 55 deletions(-) create mode 100644 sentience/utils/__init__.py create mode 100644 sentience/utils/browser.py create mode 100644 sentience/utils/element.py create mode 100644 sentience/utils/formatting.py diff --git a/sentience/__init__.py b/sentience/__init__.py index 61526a6..14b72fb 100644 --- a/sentience/__init__.py +++ b/sentience/__init__.py @@ -16,7 +16,7 @@ from .expect import expect # Formatting (v0.12.0+) -from .formatting import format_snapshot_for_llm +from .utils.formatting import format_snapshot_for_llm from .generator import ScriptGenerator, generate from .inspector import Inspector, inspect from .llm_provider import ( @@ -62,6 +62,7 @@ from .tracing import JsonlTraceSink, TraceEvent, Tracer, TraceSink # Utilities (v0.12.0+) +# Import from utils package (re-exports from submodules for backward compatibility) from .utils import ( canonical_snapshot_loose, canonical_snapshot_strict, diff --git a/sentience/element_filter.py b/sentience/element_filter.py index df117b9..6159115 100644 --- a/sentience/element_filter.py +++ b/sentience/element_filter.py @@ -64,7 +64,7 @@ def filter_by_importance( @staticmethod def filter_by_goal( snapshot: Snapshot, - goal: str | None, + goal: Optional[str], max_elements: int = 50, ) -> list[Element]: """ diff --git a/sentience/formatting.py b/sentience/formatting.py index f8961c5..b8dd653 100644 --- a/sentience/formatting.py +++ b/sentience/formatting.py @@ -1,59 +1,15 @@ """ Snapshot formatting utilities for LLM prompts. -Provides functions to convert Sentience snapshots into text format suitable -for LLM consumption. -""" - -from typing import List - -from .models import Snapshot - - -def format_snapshot_for_llm(snap: Snapshot, limit: int = 50) -> str: - """ - Convert snapshot elements to text format for LLM consumption. - - This is the canonical way Sentience formats DOM state for LLMs. - The format includes element ID, role, text preview, visual cues, - position, and importance score. +DEPRECATED: This module is maintained for backward compatibility only. +New code should import from sentience.utils.formatting or sentience directly: - Args: - snap: Snapshot object with elements - limit: Maximum number of elements to include (default: 50) - - Returns: - Formatted string with one element per line - - Example: - >>> snap = snapshot(browser) - >>> formatted = format_snapshot_for_llm(snap, limit=10) - >>> print(formatted) - [1]