Profiles

Here's how you can use profiles with HyperAgent. You can see more information about using profiles with Hyperbrowser here

import "dotenv/config";
import { HyperAgent } from "@hyperbrowser/agent";
import { Hyperbrowser } from "@hyperbrowser/sdk";

import chalk from "chalk";
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";

const TASK =
  "Navigate to imdb.com, search for 'The Matrix', and extract the director, release year, and rating";

const client = new Hyperbrowser();

async function runEval() {
  const profile = await client.profiles.create({
    name: "test-hyperagent-profile",
  });
  // or get your profile if you have already have a profile.
  //
  // const _profile = await client.profiles.get(
  //  "12345678-0000-4000-0000-000000000000"
  // );

  const llm = new ChatOpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    model: "gpt-4o",
  });

  const agent = new HyperAgent({
    llm: llm,
    browserProvider: "Hyperbrowser",
    hyperbrowserConfig: {
      hyperbrowserSessionOptions: {
        profile: {
          id: profile.id,
          persistChanges: true,
        },
      },
    },
  });

  const result = await agent.executeTask(TASK, {
    debugOnAgentOutput: (agentOutput) => {
      console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
      console.dir(agentOutput, { depth: null, colors: true });
      console.log(chalk.cyan.bold("===============") + "\n");
    },
    onStep: (step) => {
      console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
      console.dir(step, { depth: null, colors: true });
      console.log(chalk.cyan.bold("===============") + "\n");
    },
    outputSchema: z.object({
      ip: z
        .string()
        .describe("The public ip of the computer as listed on the webpage"),
    }),
  });
  await agent.closeAgent();
  console.log(chalk.green.bold("\nResult:"));
  console.log(chalk.white(result.output));
  return result;
}

(async () => {
  await runEval();
})().catch((error) => {
  console.error(chalk.red("Error:"), error);
  process.exit(1);
});

Last updated