Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Merge branch 'main' into codex/fix-url-negation-boundaries

Magnus Müllerad03ce5e3213
diff --cc browser_use/utils.py
index 2dec60bd7,92af31202..669bff172
--- a/browser_use/utils.py
+++ b/browser_use/utils.py
@@@ -47,14 -50,31 +51,36 @@@ def sanitize_url_candidate(url: str) -
# "https://example.com/search.\\n2. Next step". Those are task text,
# not part of the URL.
candidate = re.split(r'\\[nrt]', candidate, maxsplit=1)[0]
- return re.sub(r'[.,;:!?()\[\]]+$', '', candidate)
+
+ # Strip trailing prose punctuation, but keep a closing bracket the URL opened
+ # itself, e.g. /wiki/Python_(programming_language). A closing bracket is only
+ # prose when it has no opener inside the candidate, as in "(see https://x.com/a)".
+ # Bracket totals are counted once and decremented as characters are trimmed, so
+ # a candidate ending in many brackets stays linear.
+ bracket_counts = {bracket: candidate.count(bracket) for bracket in '()[]'}
+ end = len(candidate)
+ while end:
+ last_char = candidate[end - 1]
+ if last_char in _TRAILING_PROSE_PUNCTUATION:
+ if last_char in bracket_counts:
+ bracket_counts[last_char] -= 1
+ end -= 1
+ continue
+ opening_bracket = _CLOSING_TO_OPENING_BRACKET.get(last_char)
+ if opening_bracket is not None and bracket_counts[last_char] > bracket_counts[opening_bracket]:
+ bracket_counts[last_char] -= 1
+ end -= 1
+ continue
+ break
+
+ return candidate[:end]
+def has_url_negation(context: str) -> bool:
+ """Return whether nearby prose explicitly negates navigation to a URL."""
+ return URL_NEGATION_PATTERN.search(context) is not None
+
+
# Lazy import for error types
# Use sentinel to avoid retrying import when package is not installed
_IMPORT_NOT_FOUND: type = type('_ImportNotFound', (), {})