import { z } from "zod";
import { client, agentTask, Sessions } from "anchorbrowser";
client.setConfig({ auth: () => process.env.ANCHORBROWSER_API_KEY });
const Game = z.object({
opponent: z.string(),
bucks_score: z.number(),
opponent_score: z.number(),
date: z.string(),
});
const Article = z.object({
headline: z.string(),
author: z.string(),
url: z.string(),
});
const session = await Sessions.createSession();
const sessionId = session.data.id;
try {
const gameTask = await agentTask(
"Find the most recent NBA game played by the Milwaukee Bucks. Return the opponent, the final score for both teams, and the date of the game.",
{
sessionId,
taskOptions: {
url: "https://www.nba.com/bucks/schedule",
outputSchema: z.toJSONSchema(Game),
},
}
);
if (gameTask.status !== "success") throw new Error(`Task failed: ${gameTask.result}`);
const game = Game.parse(JSON.parse(gameTask.data.result));
console.log(game);
const articleTask = await agentTask(
`Find a news article that covers the Milwaukee Bucks game against the ${game.opponent} on ${game.date}. Return the article headline, the author's name, and the article URL.`,
{
sessionId,
taskOptions: { outputSchema: z.toJSONSchema(Article) },
}
);
if (articleTask.status !== "success") throw new Error(`Task failed: ${articleTask.result}`);
console.log(Article.parse(JSON.parse(articleTask.data.result)));
} finally {
await Sessions.deleteSession({ path: { session_id: sessionId } });
}