-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
171 lines (155 loc) · 5.09 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { ObserveResult, Page } from "@browserbasehq/stagehand";
import boxen from "boxen";
import chalk from "chalk";
import fs from "fs/promises";
import { z } from "zod";
export function announce(message: string, title?: string) {
console.log(
boxen(message, {
padding: 1,
margin: 3,
title: title || "Stagehand",
})
);
}
/**
* Get an environment variable and throw an error if it's not found
* @param name - The name of the environment variable
* @returns The value of the environment variable
*/
export function getEnvVar(name: string, required = true): string | undefined {
const value = process.env[name];
if (!value && required) {
throw new Error(`${name} not found in environment variables`);
}
return value;
}
/**
* Validate a Zod schema against some data
* @param schema - The Zod schema to validate against
* @param data - The data to validate
* @returns Whether the data is valid against the schema
*/
export function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
try {
schema.parse(data);
return true;
} catch {
return false;
}
}
export async function drawObserveOverlay(page: Page, results: ObserveResult[]) {
// Convert single xpath to array for consistent handling
const xpathList = results.map((result) => result.selector);
// Filter out empty xpaths
const validXpaths = xpathList.filter((xpath) => xpath !== "xpath=");
await page.evaluate((selectors) => {
selectors.forEach((selector) => {
let element;
if (selector.startsWith("xpath=")) {
const xpath = selector.substring(6);
element = document.evaluate(
xpath,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
} else {
element = document.querySelector(selector);
}
if (element instanceof HTMLElement) {
const overlay = document.createElement("div");
overlay.setAttribute("stagehandObserve", "true");
const rect = element.getBoundingClientRect();
overlay.style.position = "absolute";
overlay.style.left = rect.left + "px";
overlay.style.top = rect.top + "px";
overlay.style.width = rect.width + "px";
overlay.style.height = rect.height + "px";
overlay.style.backgroundColor = "rgba(255, 255, 0, 0.3)";
overlay.style.pointerEvents = "none";
overlay.style.zIndex = "10000";
document.body.appendChild(overlay);
}
});
}, validXpaths);
}
export async function clearOverlays(page: Page) {
// remove existing stagehandObserve attributes
await page.evaluate(() => {
const elements = document.querySelectorAll('[stagehandObserve="true"]');
elements.forEach((el) => {
const parent = el.parentNode;
while (el.firstChild) {
parent?.insertBefore(el.firstChild, el);
}
parent?.removeChild(el);
});
});
}
export async function simpleCache(
instruction: string,
actionToCache: ObserveResult
) {
// Save action to cache.json
try {
// Read existing cache if it exists
let cache: Record<string, ObserveResult> = {};
try {
const existingCache = await fs.readFile("cache.json", "utf-8");
cache = JSON.parse(existingCache);
} catch (error) {
// File doesn't exist yet, use empty cache
}
// Add new action to cache
cache[instruction] = actionToCache;
// Write updated cache to file
await fs.writeFile("cache.json", JSON.stringify(cache, null, 2));
} catch (error) {
console.error(chalk.red("Failed to save to cache:"), error);
}
}
export async function readCache(
instruction: string
): Promise<ObserveResult | null> {
try {
const existingCache = await fs.readFile("cache.json", "utf-8");
const cache: Record<string, ObserveResult> = JSON.parse(existingCache);
return cache[instruction] || null;
} catch (error) {
return null;
}
}
/**
* This function is used to act with a cacheable action.
* It will first try to get the action from the cache.
* If not in cache, it will observe the page and cache the result.
* Then it will execute the action.
* @param instruction - The instruction to act with.
*/
export async function actWithCache(
page: Page,
instruction: string
): Promise<void> {
// Try to get action from cache first
const cachedAction = await readCache(instruction);
if (cachedAction) {
console.log(chalk.blue("Using cached action for:"), instruction);
await page.act(cachedAction);
return;
}
// If not in cache, observe the page and cache the result
const results = await page.observe(instruction);
console.log(chalk.blue("Got results:"), results);
// Cache the playwright action
const actionToCache = results[0];
console.log(chalk.blue("Taking cacheable action:"), actionToCache);
await simpleCache(instruction, actionToCache);
// OPTIONAL: Draw an overlay over the relevant xpaths
await drawObserveOverlay(page, results);
await page.waitForTimeout(1000); // Can delete this line, just a pause to see the overlay
await clearOverlays(page);
// Execute the action
await page.act(actionToCache);
}