Skip to content

Commit

Permalink
add NAVBACK
Browse files Browse the repository at this point in the history
  • Loading branch information
sameelarif committed Jan 24, 2025
1 parent 841e7c8 commit f412768
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
78 changes: 47 additions & 31 deletions execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export async function runStagehand({
| "CLOSE"
| "SCREENSHOT"
| "OBSERVE"
| "WAIT";
| "WAIT"
| "NAVBACK";
instruction?: string;
}) {
const stagehand = new Stagehand({
Expand All @@ -24,38 +25,53 @@ export async function runStagehand({
await stagehand.init();

const page = stagehand.page;
const context = stagehand.context;

if (method === "GOTO") {
await page.goto(instruction!, {
waitUntil: "domcontentloaded",
timeout: 60000,
});
}
if (method === "ACT") {
await page.act(instruction!);
}
if (method === "EXTRACT") {
const { extraction } = await page.extract(instruction!);
return extraction;
}
if (method === "OBSERVE") {
return await page.observe({
instruction,
useAccessibilityTree: true,
});
}
if (method === "CLOSE") {
await stagehand.close();
}
try {
switch (method) {
case "GOTO":
await page.goto(instruction!, {
waitUntil: "domcontentloaded",
timeout: 60000,
});
break;

if (method === "SCREENSHOT") {
const cdpSession = await page.context().newCDPSession(page);
const { data } = await cdpSession.send("Page.captureScreenshot");
return data;
}
case "ACT":
await page.act(instruction!);
break;

if (method === "WAIT") {
await new Promise((resolve) => setTimeout(resolve, Number(instruction)));
case "EXTRACT": {
const { extraction } = await page.extract(instruction!);
return extraction;
}

case "OBSERVE":
return await page.observe({
instruction,
useAccessibilityTree: true,
});

case "CLOSE":
await stagehand.close();
break;

case "SCREENSHOT": {
const cdpSession = await page.context().newCDPSession(page);
const { data } = await cdpSession.send("Page.captureScreenshot");
return data;
}

case "WAIT":
await new Promise((resolve) =>
setTimeout(resolve, Number(instruction))
);
break;

case "NAVBACK":
await page.goBack();
break;
}
} catch (error) {
await stagehand.close();
throw error;
}
}
12 changes: 10 additions & 2 deletions llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,21 @@ If the goal has been achieved, return "close".`,
.describe(
"The reasoning behind the tool call. If the tool is 'CLOSE', this should explain how and why the goal has been achieved."
),
tool: z.enum(["GOTO", "ACT", "EXTRACT", "OBSERVE", "CLOSE", "WAIT"])
.describe(`Tool guidelines:
tool: z.enum([
"GOTO",
"ACT",
"EXTRACT",
"OBSERVE",
"CLOSE",
"WAIT",
"NAVBACK",
]).describe(`Tool guidelines:
GOTO: Navigate to a new URL only if not accessible from current page or if you need to navigate to a page to start off
ACT: Perform a single action on the page
EXTRACT: Extract data from the page (don't rely on screenshots for text, use the EXTRACT tool and you'll be provided with the result)
OBSERVE: List available actions when unsure what to do next
WAIT: Wait for a number of milliseconds
NAVBACK: Navigate to the previously visited URL
CLOSE: Close browser when goal is achieved`),
instruction: z
.string()
Expand Down

0 comments on commit f412768

Please sign in to comment.