-
Notifications
You must be signed in to change notification settings - Fork 1.4k
variables for observe #1808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
variables for observe #1808
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
10dc220
variables for observe
filip-michalsky d57f72f
address feedback
filip-michalsky 5e6a963
merge commit
filip-michalsky 8f5efbe
update test
filip-michalsky 8664746
fix build typing
filip-michalsky 0e90124
use shared schema
filip-michalsky c053576
fix build
filip-michalsky 262d004
merge commit
filip-michalsky 007338b
abstract variables to a common schema
filip-michalsky c99a0a4
fix lint
filip-michalsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * This example shows how to use observe({ variables }) to plan a sensitive | ||
| * login flow, validate the returned placeholder actions, and then execute them | ||
| * with act(). | ||
| * | ||
| * observe() returns %variableName% placeholders in action arguments. That lets | ||
| * you review the planned actions before any real secret values are used. | ||
| */ | ||
| import { Action, Stagehand } from "../lib/v3/index.js"; | ||
| import chalk from "chalk"; | ||
|
|
||
| const variables = { | ||
| username: "test@browserbase.com", | ||
| password: "stagehand=goated", | ||
| }; | ||
|
|
||
| const loginInstruction = [ | ||
| "Fill the login form using the available variables.", | ||
| "Use %username% for the email field.", | ||
| "Use %password% for the password field.", | ||
| "Include the field name in each action description.", | ||
| ].join(" "); | ||
|
|
||
| function findValidatedAction( | ||
| observed: Action[], | ||
| placeholder: string, | ||
| keywords: string[], | ||
| ): Action { | ||
| const matches = observed.filter((action) => { | ||
| const description = action.description.toLowerCase(); | ||
| return ( | ||
| action.arguments?.includes(placeholder) === true && | ||
| keywords.some((keyword) => description.includes(keyword)) | ||
| ); | ||
| }); | ||
|
|
||
| if (matches.length !== 1) { | ||
| throw new Error( | ||
| `Expected exactly one safe action for ${placeholder}, found ${matches.length}`, | ||
| ); | ||
| } | ||
|
|
||
| return matches[0]; | ||
| } | ||
|
|
||
| async function observeVariablesLogin() { | ||
| const stagehand = new Stagehand({ | ||
| env: "BROWSERBASE", | ||
| verbose: 1, | ||
| }); | ||
|
|
||
| await stagehand.init(); | ||
|
|
||
| try { | ||
| const page = stagehand.context.pages()[0]; | ||
|
|
||
| await page.goto("https://v0-modern-login-flow.vercel.app/", { | ||
| waitUntil: "networkidle", | ||
| timeoutMs: 30000, | ||
miguelg719 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| const observed = await stagehand.observe(loginInstruction, { | ||
| variables, | ||
| }); | ||
|
|
||
| console.log( | ||
| `${chalk.green("Observe:")} Placeholder actions found:\n${observed | ||
miguelg719 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .map( | ||
| (action) => | ||
| `${chalk.yellow(action.description)} -> ${chalk.blue(action.arguments?.join(", ") || "no arguments")}`, | ||
| ) | ||
| .join("\n")}`, | ||
| ); | ||
|
|
||
| const emailAction = findValidatedAction(observed, "%username%", ["email"]); | ||
| const passwordAction = findValidatedAction(observed, "%password%", [ | ||
| "password", | ||
| ]); | ||
|
|
||
| console.log( | ||
| `\n${chalk.green("Validated:")} Safe actions to execute:\n${[ | ||
| emailAction, | ||
| passwordAction, | ||
| ] | ||
| .map( | ||
| (action) => | ||
| `${chalk.yellow(action.description)} -> ${chalk.blue(action.arguments?.[0] || "no value")}`, | ||
| ) | ||
| .join("\n")}`, | ||
| ); | ||
|
|
||
| await stagehand.act(emailAction, { variables }); | ||
| await stagehand.act(passwordAction, { variables }); | ||
|
|
||
| const [submitButton] = await stagehand.observe("find the sign in button"); | ||
|
|
||
| if (!submitButton) { | ||
| throw new Error("Could not find the sign in button"); | ||
| } | ||
|
|
||
| await stagehand.act(submitButton); | ||
| console.log( | ||
| chalk.green( | ||
| "\nSubmitted login form. Waiting 10 seconds before closing...", | ||
| ), | ||
| ); | ||
| await page.waitForTimeout(10000); | ||
| } finally { | ||
| await stagehand.close(); | ||
| } | ||
| } | ||
|
|
||
| (async () => { | ||
| await observeVariablesLogin(); | ||
| })(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { z } from "zod/v4"; | ||
| import type { VariableValue, Variables } from "./agent.js"; | ||
|
|
||
| type VariablePrimitive = string | number | boolean; | ||
|
|
||
| export const VariablePrimitiveSchema: z.ZodType<VariablePrimitive> = z | ||
| .union([z.string(), z.number(), z.boolean()]) | ||
| .meta({ id: "VariablePrimitive" }); | ||
|
|
||
| export const VariableValueSchema: z.ZodType<VariableValue> = z | ||
| .union([ | ||
| VariablePrimitiveSchema, | ||
| z | ||
| .object({ | ||
| value: VariablePrimitiveSchema, | ||
| description: z.string().optional(), | ||
| }) | ||
| .strict(), | ||
| ]) | ||
| .meta({ id: "VariableValue" }); | ||
|
|
||
| export const VariablesSchema: z.ZodType<Variables> = z | ||
| .record(z.string(), VariableValueSchema) | ||
| .meta({ id: "Variables" }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.