|
9 | 9 | * successfully navigated as far as possible without credentials. |
10 | 10 | */ |
11 | 11 |
|
| 12 | +import type { SnapshotElement, PredicateSpec } from './plan-models'; |
| 13 | + |
12 | 14 | /** |
13 | 15 | * Configuration for authentication boundary detection. |
14 | 16 | */ |
@@ -226,3 +228,119 @@ export function isCheckoutElement( |
226 | 228 |
|
227 | 229 | return false; |
228 | 230 | } |
| 231 | + |
| 232 | +function normalizeIntentText(value: string | undefined | null): string { |
| 233 | + return (value || '').toLowerCase().replace(/[_-]+/g, ' ').replace(/\s+/g, ' ').trim(); |
| 234 | +} |
| 235 | + |
| 236 | +function extractUrlSignals(url: string): string[] { |
| 237 | + if (!url) { |
| 238 | + return []; |
| 239 | + } |
| 240 | + |
| 241 | + try { |
| 242 | + const parsed = new URL(url); |
| 243 | + const signals = [ |
| 244 | + parsed.pathname, |
| 245 | + parsed.search, |
| 246 | + parsed.searchParams.get('q') || '', |
| 247 | + parsed.searchParams.get('query') || '', |
| 248 | + parsed.searchParams.get('search') || '', |
| 249 | + parsed.searchParams.get('keyword') || '', |
| 250 | + parsed.searchParams.get('keywords') || '', |
| 251 | + parsed.searchParams.get('term') || '', |
| 252 | + parsed.searchParams.get('s') || '', |
| 253 | + ]; |
| 254 | + return signals.map(signal => normalizeIntentText(signal)).filter(Boolean); |
| 255 | + } catch { |
| 256 | + return [normalizeIntentText(url)]; |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +function queryTerms(text: string | undefined | null): string[] { |
| 261 | + return normalizeIntentText(text) |
| 262 | + .split(/\s+/) |
| 263 | + .filter(term => term.length >= 3); |
| 264 | +} |
| 265 | + |
| 266 | +function urlPredicateSignals(verify: PredicateSpec[] | undefined): string[] { |
| 267 | + if (!Array.isArray(verify)) { |
| 268 | + return []; |
| 269 | + } |
| 270 | + |
| 271 | + const signals: string[] = []; |
| 272 | + for (const predicate of verify) { |
| 273 | + if ( |
| 274 | + predicate && |
| 275 | + typeof predicate.predicate === 'string' && |
| 276 | + (predicate.predicate === 'url_contains' || predicate.predicate === 'url_matches') |
| 277 | + ) { |
| 278 | + const firstArg = predicate.args?.[0]; |
| 279 | + if (typeof firstArg === 'string' && firstArg.trim()) { |
| 280 | + signals.push(normalizeIntentText(firstArg)); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + return signals; |
| 286 | +} |
| 287 | + |
| 288 | +export function isSearchLikeTypeAndSubmit( |
| 289 | + step: { action?: string; intent?: string; input?: string; verify?: PredicateSpec[] }, |
| 290 | + element?: Pick<SnapshotElement, 'role' | 'text' | 'name' | 'ariaLabel'> | null |
| 291 | +): boolean { |
| 292 | + if ((step.action || '').toUpperCase() !== 'TYPE_AND_SUBMIT') { |
| 293 | + return false; |
| 294 | + } |
| 295 | + |
| 296 | + const cues = [ |
| 297 | + normalizeIntentText(step.intent), |
| 298 | + normalizeIntentText(step.input), |
| 299 | + normalizeIntentText(element?.role), |
| 300 | + normalizeIntentText(element?.text), |
| 301 | + normalizeIntentText(element?.name), |
| 302 | + normalizeIntentText(element?.ariaLabel), |
| 303 | + ...urlPredicateSignals(step.verify), |
| 304 | + ].filter(Boolean); |
| 305 | + |
| 306 | + return cues.some(cue => |
| 307 | + /\b(search|searchbox|find|lookup|look up|query|keywords?|results?)\b/.test(cue) |
| 308 | + ); |
| 309 | +} |
| 310 | + |
| 311 | +export function isUrlChangeRelevantToIntent( |
| 312 | + previousUrl: string, |
| 313 | + nextUrl: string, |
| 314 | + step: { action?: string; intent?: string; input?: string; verify?: PredicateSpec[] }, |
| 315 | + element?: Pick<SnapshotElement, 'role' | 'text' | 'name' | 'ariaLabel'> | null |
| 316 | +): boolean { |
| 317 | + const normalizedPrevious = normalizeIntentText(previousUrl).replace(/\/+$/, ''); |
| 318 | + const normalizedNext = normalizeIntentText(nextUrl).replace(/\/+$/, ''); |
| 319 | + if (!normalizedNext || normalizedNext === normalizedPrevious) { |
| 320 | + return false; |
| 321 | + } |
| 322 | + |
| 323 | + const predicateSignals = urlPredicateSignals(step.verify); |
| 324 | + const nextSignals = extractUrlSignals(nextUrl); |
| 325 | + if ( |
| 326 | + predicateSignals.length > 0 && |
| 327 | + predicateSignals.every(signal => nextSignals.some(nextSignal => nextSignal.includes(signal))) |
| 328 | + ) { |
| 329 | + return true; |
| 330 | + } |
| 331 | + |
| 332 | + if (!isSearchLikeTypeAndSubmit(step, element)) { |
| 333 | + return true; |
| 334 | + } |
| 335 | + |
| 336 | + const searchTerms = [ |
| 337 | + ...queryTerms(step.input), |
| 338 | + ...queryTerms(step.intent), |
| 339 | + 'search', |
| 340 | + 'query', |
| 341 | + 'results', |
| 342 | + 'find', |
| 343 | + ]; |
| 344 | + |
| 345 | + return searchTerms.some(term => nextSignals.some(signal => signal.includes(term))); |
| 346 | +} |
0 commit comments