attemptWithRecovery()
Runs a function, and if it throws, optionally runs an LLM-powered popup recovery agent and retries the function once.
How it works
- Calls
fn(). - If
fn()succeeds, returns the result immediately. - If
fn()throws andllmClientis not provided, re-throws the error. - If
fn()throws andllmClientis provided, runsexecuteRecoveryAgent()with the instruction"Look at the page to see if there is a popup blocking the screen. If so, close the popup." - After recovery, calls
fn()a second time. If it throws again, that error propagates.
Parameters
The Playwright
Page where the action is being attempted.The async function to run. This is the step you want to protect — typically a sequence of Playwright actions.
Optional logger. Recovery attempts and outcomes are logged at
info level.Optional LLM client. When omitted, errors are re-thrown without any recovery attempt. When provided, the recovery agent runs on failure.
Example
executeRecoveryAgent()
Runs a vision-based LLM agent to handle page state issues. The agent takes a screenshot, sends it to the LLM with your instruction, executes the suggested browser action, and repeats for up to 3 steps until the action type is "done".
When
llmClient is not provided, executeRecoveryAgent returns immediately without doing anything.Parameters
The Playwright
Page to operate on.A natural-language description of what the agent should do. For example:
"Dismiss any cookie consent banner or modal that is blocking the page."Optional logger. Each recovery step’s reasoning and action are logged.
Optional LLM client. The backing model must support vision (multimodal) input.
Example
detectSubmissionError()
Uses a CDP screenshot and LLM vision to detect whether a visible error appeared on the page after a form submission or other action. Useful when the site shows errors inline rather than throwing JavaScript exceptions.
knownErrors entry matches what the LLM sees on screen, detectSubmissionError returns a DetectedSubmissionError. If no known error matches, it re-throws the original error.
The screenshot is captured via CDP so it works even when the page is unresponsive (e.g. during a loading spinner or frozen UI). If the CDP screenshot itself fails, the original error is re-thrown.
Parameters
The Playwright
Page to screenshot.The original error that triggered the detection call. Re-thrown if no known error matches.
A string describing the operation being performed. Included in log output and passed to the LLM as context.
An LLM client. The model must support vision input.
An optional list of known error definitions to match against. Defaults to
[].Optional logger.
KnownSubmissionError
A stable identifier for this error type. Returned in
DetectedSubmissionError.errorId.Descriptions of what the LLM should look for on screen to match this error. For example:
["'Email already in use' message", "duplicate account warning"].A human-friendly message returned in
DetectedSubmissionError.message when this error is matched.DetectedSubmissionError
Always
true — indicates a known error was detected.The
id from the matched KnownSubmissionError.The
userMessage from the matched KnownSubmissionError.