@usecrow/client 0.1.24 → 0.1.26

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,205 @@
1
+ let c = null, l = null;
2
+ function w(i) {
3
+ c = i;
4
+ }
5
+ async function p() {
6
+ if (c)
7
+ return c;
8
+ if (!l)
9
+ try {
10
+ l = await import("./PageController-KoeqDxMP.js");
11
+ } catch {
12
+ throw new Error(
13
+ 'PageController not available. Either import from "@usecrow/client/browser" or use the bundled version.'
14
+ );
15
+ }
16
+ return l.PageController;
17
+ }
18
+ class u {
19
+ constructor(t) {
20
+ this.pageController = null, this.sessionId = null, this.maxSteps = 20, this.config = t;
21
+ }
22
+ /**
23
+ * Initialize PageController with non-blocking pointer
24
+ */
25
+ async initPageController() {
26
+ if (this.pageController)
27
+ return this.pageController;
28
+ try {
29
+ const t = await p();
30
+ this.pageController = new t({
31
+ enableMask: !0,
32
+ viewportExpansion: 500,
33
+ highlightLabelOpacity: 0,
34
+ // Hide numbered labels from users
35
+ highlightOpacity: 0
36
+ // Hide highlight boxes from users
37
+ }), await this.pageController.showMask();
38
+ const e = this.pageController.mask;
39
+ return e != null && e.wrapper && (e.wrapper.style.pointerEvents = "none"), console.log("[CrowBrowserUse] PageController initialized with non-blocking pointer"), this.pageController;
40
+ } catch (t) {
41
+ throw console.error("[CrowBrowserUse] Failed to initialize PageController:", t), new Error(
42
+ "Failed to initialize browser automation. Please import from @usecrow/client/browser."
43
+ );
44
+ }
45
+ }
46
+ /**
47
+ * Execute a browser automation task
48
+ */
49
+ async execute(t) {
50
+ console.log("[CrowBrowserUse] Starting task:", t);
51
+ try {
52
+ const e = await this.initPageController(), o = await this.startSession(t);
53
+ this.sessionId = o.session_id, this.maxSteps = o.max_steps, console.log("[CrowBrowserUse] Session started:", this.sessionId);
54
+ let r = 0, s;
55
+ for (; r < this.maxSteps; ) {
56
+ r++;
57
+ const a = await e.getBrowserState(), n = await this.processStep(a, s);
58
+ if (n.done)
59
+ return console.log("[CrowBrowserUse] Task completed:", n.message), await this.cleanup(), {
60
+ status: n.success ? "success" : "error",
61
+ data: {
62
+ message: n.message,
63
+ steps: r
64
+ },
65
+ error: n.success ? void 0 : n.message
66
+ };
67
+ if (n.error)
68
+ return console.error("[CrowBrowserUse] Error:", n.error), await this.cleanup(), {
69
+ status: "error",
70
+ error: n.error
71
+ };
72
+ n.action && (s = await this.executeAction(e, n.action), console.log(`[CrowBrowserUse] Step ${r}:`, s)), n.reflection && console.log("[CrowBrowserUse] Reflection:", n.reflection.next_goal);
73
+ }
74
+ return await this.cleanup(), {
75
+ status: "error",
76
+ error: `Task incomplete after ${this.maxSteps} steps`
77
+ };
78
+ } catch (e) {
79
+ return console.error("[CrowBrowserUse] Error:", e), await this.cleanup(), {
80
+ status: "error",
81
+ error: e instanceof Error ? e.message : String(e)
82
+ };
83
+ }
84
+ }
85
+ /**
86
+ * Start a browser-use session on the server
87
+ */
88
+ async startSession(t) {
89
+ const e = await fetch(`${this.config.apiUrl}/api/browser-use/start`, {
90
+ method: "POST",
91
+ headers: { "Content-Type": "application/json" },
92
+ body: JSON.stringify({
93
+ product_id: this.config.productId,
94
+ task: t
95
+ })
96
+ });
97
+ if (!e.ok) {
98
+ const o = await e.json().catch(() => ({ detail: "Unknown error" }));
99
+ throw new Error(o.detail || `Failed to start session: ${e.status}`);
100
+ }
101
+ return e.json();
102
+ }
103
+ /**
104
+ * Process a step on the server
105
+ */
106
+ async processStep(t, e) {
107
+ const o = await fetch(`${this.config.apiUrl}/api/browser-use/step`, {
108
+ method: "POST",
109
+ headers: { "Content-Type": "application/json" },
110
+ body: JSON.stringify({
111
+ session_id: this.sessionId,
112
+ product_id: this.config.productId,
113
+ browser_state: t,
114
+ action_result: e
115
+ })
116
+ });
117
+ if (!o.ok) {
118
+ const r = await o.json().catch(() => ({ detail: "Unknown error" }));
119
+ throw new Error(r.detail || `Failed to process step: ${o.status}`);
120
+ }
121
+ return o.json();
122
+ }
123
+ /**
124
+ * Execute an action using PageController
125
+ */
126
+ async executeAction(t, e) {
127
+ const o = Object.keys(e)[0], r = e[o];
128
+ try {
129
+ switch (o) {
130
+ case "click_element_by_index":
131
+ return (await t.clickElement(r.index)).message;
132
+ case "input_text":
133
+ return (await t.inputText(
134
+ r.index,
135
+ r.text
136
+ )).message;
137
+ case "select_dropdown_option":
138
+ return (await t.selectOption(
139
+ r.index,
140
+ r.text
141
+ )).message;
142
+ case "scroll":
143
+ return (await t.scroll({
144
+ down: r.down,
145
+ numPages: r.num_pages,
146
+ pixels: r.pixels,
147
+ index: r.index
148
+ })).message;
149
+ case "scroll_horizontally":
150
+ return (await t.scrollHorizontally({
151
+ right: r.right,
152
+ pixels: r.pixels,
153
+ index: r.index
154
+ })).message;
155
+ case "wait": {
156
+ const s = r.seconds || 1;
157
+ return await new Promise((a) => setTimeout(a, s * 1e3)), `Waited ${s} seconds`;
158
+ }
159
+ case "done":
160
+ return "Task completed";
161
+ default:
162
+ return `Unknown action: ${o}`;
163
+ }
164
+ } catch (s) {
165
+ return `Action failed: ${s instanceof Error ? s.message : String(s)}`;
166
+ }
167
+ }
168
+ /**
169
+ * Cleanup resources
170
+ */
171
+ async cleanup() {
172
+ if (this.pageController) {
173
+ try {
174
+ await this.pageController.hideMask(), await this.pageController.cleanUpHighlights(), this.pageController.dispose();
175
+ } catch (t) {
176
+ console.warn("[CrowBrowserUse] Cleanup error:", t);
177
+ }
178
+ this.pageController = null;
179
+ }
180
+ if (this.sessionId) {
181
+ try {
182
+ await fetch(`${this.config.apiUrl}/api/browser-use/end`, {
183
+ method: "POST",
184
+ headers: { "Content-Type": "application/json" },
185
+ body: JSON.stringify({
186
+ session_id: this.sessionId,
187
+ product_id: this.config.productId
188
+ })
189
+ });
190
+ } catch {
191
+ }
192
+ this.sessionId = null;
193
+ }
194
+ }
195
+ /**
196
+ * Stop the current task
197
+ */
198
+ async stop() {
199
+ await this.cleanup();
200
+ }
201
+ }
202
+ export {
203
+ u as C,
204
+ w as s
205
+ };