Multi-Page actions

HyperAgent is built around playwright, and offers a natural way to extend the functionality of playwright to also perform tasks like an independent web-agent.

This can be done simply by using the .ai() method available on all page objects.

Here is a simple example showing how to do this:

const agent = new HyperAgent();

const page1 = await agent.newPage();
const page2 = await agent.newPage();

// Execute tasks on specific pages
const page1Response = await page1.ai(
  "Go to google.com/travel/explore and set the starting ___location to New York. Then, return to me the first recommended destination that shows up. Return to me only the name of the ___location."
);
const page2Response = await page2.ai(
  `I want to plan a trip to ${page1Response.output}. Recommend me places to visit there.`
);

console.log(page2Response.output);

Each page can act as a separate web-agent by itself.

All page.ai calls can take in two parameters:

  • task : A string describing the task to be completed on this page

  • params (Optional): An object containing the parameters of the task. A list of accepted parameters can be found in the HyperAgent Types page

Extracting info from a page

HyperAgent also offers a quick method of extracting information from a page using the .extract() method

const agent = new HyperAgent();

const page = await agent.newPage();

// Execute tasks on specific pages
await page.goto("https://en.wikipedia.org/wiki/2014_FIFA_World_Cup");
const pageResponse = await page.extract("Tell me the countries that participated in the 2014 FIFA world cup");

console.log(pageResponse);

All page.extract calls can take in two parameters:

  • task: A string describing what needs to be extracted from the current page

  • outputSchema (optional): A zod object describing the schema of the output expected.

Last updated