@browserbasehq/stagehand 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ import { BaseCache, CacheEntry } from "./BaseCache";
2
+
3
+ export class LLMCache extends BaseCache<CacheEntry> {
4
+ constructor(
5
+ logger: (message: {
6
+ category?: string;
7
+ message: string;
8
+ level?: number;
9
+ }) => void,
10
+ cacheDir?: string,
11
+ cacheFile?: string,
12
+ ) {
13
+ super(logger, cacheDir, cacheFile || "llm_calls.json");
14
+ }
15
+
16
+ /**
17
+ * Overrides the get method to track used hashes by requestId.
18
+ * @param options - The options used to generate the cache key.
19
+ * @param requestId - The identifier for the current request.
20
+ * @returns The cached data if available, otherwise null.
21
+ */
22
+ public async get(
23
+ options: Record<string, any>,
24
+ requestId: string,
25
+ ): Promise<any | null> {
26
+ const data = await super.get(options, requestId);
27
+ return data;
28
+ }
29
+
30
+ /**
31
+ * Overrides the set method to include cache cleanup logic.
32
+ * @param options - The options used to generate the cache key.
33
+ * @param data - The data to be cached.
34
+ * @param requestId - The identifier for the current request.
35
+ */
36
+ public async set(
37
+ options: Record<string, any>,
38
+ data: any,
39
+ requestId: string,
40
+ ): Promise<void> {
41
+ await super.set(options, data, requestId);
42
+ this.logger({
43
+ category: "llm_cache",
44
+ message: "Cache miss - saved new response",
45
+ level: 1,
46
+ });
47
+ }
48
+ }
package/lib/cache.ts ADDED
@@ -0,0 +1,99 @@
1
+ const fs = require("fs");
2
+ const crypto = require("crypto");
3
+ const observationsPath = "./.cache/observations.json";
4
+ const actionsPath = "./.cache/actions.json";
5
+
6
+ /**
7
+ * A file system cache to skip inference when repeating steps
8
+ * It also acts as the source of truth for identifying previously seen actions and observatiosn
9
+ */
10
+ class Cache {
11
+ disabled: boolean;
12
+
13
+ constructor({ disabled = false } = {}) {
14
+ this.disabled = disabled;
15
+ if (!this.disabled) {
16
+ this.initCache();
17
+ }
18
+ }
19
+
20
+ readObservations() {
21
+ if (this.disabled) {
22
+ return {};
23
+ }
24
+ try {
25
+ return JSON.parse(fs.readFileSync(observationsPath, "utf8"));
26
+ } catch (error) {
27
+ console.error("Error reading from observations.json", error);
28
+ return {};
29
+ }
30
+ }
31
+
32
+ readActions() {
33
+ if (this.disabled) {
34
+ return {};
35
+ }
36
+ try {
37
+ return JSON.parse(fs.readFileSync(actionsPath, "utf8"));
38
+ } catch (error) {
39
+ console.error("Error reading from actions.json", error);
40
+ return {};
41
+ }
42
+ }
43
+
44
+ writeObservations({
45
+ key,
46
+ value,
47
+ }: {
48
+ key: string;
49
+ value: { id: string; result: string };
50
+ }) {
51
+ if (this.disabled) {
52
+ return;
53
+ }
54
+
55
+ const observations = this.readObservations();
56
+ observations[key] = value;
57
+ fs.writeFileSync(observationsPath, JSON.stringify(observations, null, 2));
58
+ }
59
+
60
+ writeActions({
61
+ key,
62
+ value,
63
+ }: {
64
+ key: string;
65
+ value: { id: string; result: string };
66
+ }) {
67
+ if (this.disabled) {
68
+ return;
69
+ }
70
+
71
+ const actions = this.readActions();
72
+ actions[key] = value;
73
+ fs.writeFileSync(actionsPath, JSON.stringify(actions, null, 2));
74
+ }
75
+
76
+ evictCache() {
77
+ throw new Error("implement me");
78
+ }
79
+
80
+ private initCache() {
81
+ if (this.disabled) {
82
+ return;
83
+ }
84
+ const cacheDir = ".cache";
85
+
86
+ if (!fs.existsSync(cacheDir)) {
87
+ fs.mkdirSync(cacheDir);
88
+ }
89
+ if (!fs.existsSync(actionsPath)) {
90
+ fs.writeFileSync(actionsPath, JSON.stringify({}));
91
+ }
92
+
93
+ if (!fs.existsSync(observationsPath)) {
94
+ fs.writeFileSync(observationsPath, JSON.stringify({}));
95
+ }
96
+ }
97
+ }
98
+
99
+ export default Cache;