import "dotenv/config";
import { Stagehand } from "@browserbasehq/stagehand";
import Exa from "exa-js";
import { z } from "zod";
const exa = new Exa(process.env.EXA_API_KEY);
const companies = await exa.searchAndContents("AI startups in SF", {
category: "company",
text: true,
type: "auto",
livecrawl: "fallback",
numResults: 5,
});
const company = companies.results[0];
if (!company?.url) {
throw new Error("No matching company found");
}
const companyDomain = new URL(company.url).hostname.replace("www.", "");
const careers = await exa.searchAndContents(`${companyDomain} careers page`, {
context: true,
excludeDomains: ["linkedin.com"],
numResults: 5,
text: true,
type: "deep",
livecrawl: "fallback",
});
const careersUrl = careers.results[0]?.url;
if (!careersUrl) {
throw new Error("No careers page found");
}
const stagehand = new Stagehand({
env: "BROWSERBASE",
model: "google/gemini-2.5-pro",
});
try {
await stagehand.init();
const page = stagehand.context.pages()[0];
await page.goto(careersUrl);
const jobDescription = await stagehand.extract(
"Extract the job title, requirements, responsibilities, and other important details from this page.",
z.object({
jobTitle: z.string(),
requirements: z.array(z.string()),
responsibilities: z.array(z.string()),
details: z.string(),
}),
);
const agent = stagehand.agent({
mode: "hybrid",
model: "google/gemini-3-flash-preview",
systemPrompt: "Interact with the page without submitting an application.",
});
const result = await agent.execute({
instruction: `Review this job posting and identify the next application step. Job details: ${JSON.stringify(jobDescription)}`,
maxSteps: 10,
});
console.log(result);
} finally {
await stagehand.close();
}