Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/fix-glob-matching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@anthropic-ai/agent-browser-cli": patch
---
fix: implement glob pattern matching for wait --url and route matching
39 changes: 39 additions & 0 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ socket2 = "0.6"
similar = "2"
zip = { version = "8.2.0", default-features = false, features = ["deflate"] }
time = { version = "0.3", features = ["formatting"] }
regex = "1"

[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand Down
115 changes: 100 additions & 15 deletions cli/src/native/actions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use regex::Regex;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet};
use std::env;
Expand Down Expand Up @@ -104,6 +105,38 @@ pub struct RouteResponse {
pub headers: Option<HashMap<String, String>>,
}

fn glob_to_regex(pattern: &str) -> String {
let mut regex = String::from("^");
let mut chars = pattern.chars().peekable();

while let Some(ch) = chars.next() {
match ch {
'*' => {
if chars.peek() == Some(&'*') {
chars.next();
regex.push_str(".*");
} else {
regex.push_str("[^/]*");
}
}
'?' | '\\' | '.' | '+' | '(' | ')' | '|' | '^' | '$' | '{' | '}' | '[' | ']' => {
regex.push('\\');
regex.push(ch);
}
_ => regex.push(ch),
}
}

regex.push('$');
regex
}

fn glob_matches(pattern: &str, text: &str) -> bool {
Regex::new(&glob_to_regex(pattern))
.map(|regex| regex.is_match(text))
.unwrap_or(false)
}

#[derive(Clone, serde::Serialize)]
pub struct TrackedRequest {
pub url: String,
Expand Down Expand Up @@ -2879,9 +2912,10 @@ async fn wait_for_url(
pattern: &str,
timeout_ms: u64,
) -> Result<(), String> {
let regex = glob_to_regex(pattern);
let check_fn = format!(
"location.href.includes({})",
serde_json::to_string(pattern).unwrap_or_default()
"new RegExp({}).test(location.href)",
serde_json::to_string(&regex).unwrap_or_default()
);
poll_until_true(client, session_id, &check_fn, timeout_ms).await
}
Expand Down Expand Up @@ -5550,7 +5584,7 @@ async fn handle_responsebody(cmd: &Value, state: &DaemonState) -> Result<Value,
.and_then(|r| r.get("url"))
.and_then(|u| u.as_str())
{
if resp_url.contains(url_pattern) {
if glob_matches(url_pattern, resp_url) {
let request_id = event
.params
.get("requestId")
Expand Down Expand Up @@ -6252,18 +6286,7 @@ async fn resolve_fetch_paused(

// Route matching
for route in routes {
let matches = if route.url_pattern == "*" {
true
} else if route.url_pattern.contains('*') {
let parts: Vec<&str> = route.url_pattern.split('*').collect();
if parts.len() == 2 {
paused.url.starts_with(parts[0]) && paused.url.ends_with(parts[1])
} else {
paused.url.contains(&route.url_pattern)
}
} else {
paused.url.contains(&route.url_pattern)
};
let matches = glob_matches(&route.url_pattern, &paused.url);

if matches {
if route.abort {
Expand Down Expand Up @@ -8180,6 +8203,68 @@ mod tests {
);
}

#[test]
fn test_glob_to_regex_exact_match() {
assert_eq!(
super::glob_to_regex("https://example.com"),
"^https://example\\.com$"
);
}

#[test]
fn test_glob_to_regex_escapes_regex_metacharacters() {
assert_eq!(
super::glob_to_regex("https://example.com/a+b(c)[d]{e}|f^g$"),
"^https://example\\.com/a\\+b\\(c\\)\\[d\\]\\{e\\}\\|f\\^g\\$$"
);
}

#[test]
fn test_glob_matches_exact_match() {
assert!(super::glob_matches(
"https://example.com/path",
"https://example.com/path"
));
assert!(!super::glob_matches(
"https://example.com/path",
"https://example.com/path/extra"
));
}

#[test]
fn test_glob_matches_single_star_does_not_cross_slash() {
assert!(super::glob_matches(
"https://example.com/*.js",
"https://example.com/app.js"
));
assert!(!super::glob_matches(
"https://example.com/*.js",
"https://example.com/assets/app.js"
));
}

#[test]
fn test_glob_matches_double_star_crosses_slash() {
assert!(super::glob_matches(
"https://example.com/**/*.js",
"https://example.com/assets/app.js"
));
}

#[test]
fn test_glob_matches_question_mark_is_literal() {
// ? is the query string separator in URLs, not a glob wildcard
// Playwright's URL glob only supports * and **
assert!(super::glob_matches(
"https://example.com/page?id=1",
"https://example.com/page?id=1"
));
assert!(!super::glob_matches(
"https://example.com/page?id=1",
"https://example.com/pageXid=1"
));
}

#[test]
fn test_parse_key_chord_plain_key() {
let (key, mods) = parse_key_chord("a");
Expand Down