@usecrow/client 0.1.36 → 0.1.38

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.
@@ -1,235 +0,0 @@
1
- let d = null, h = null;
2
- function B(w) {
3
- d = w;
4
- }
5
- async function _() {
6
- if (d)
7
- return d;
8
- if (!h)
9
- try {
10
- h = await import("./PageController-BweWYS-Z.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 h.PageController;
17
- }
18
- class T {
19
- constructor(e) {
20
- this.pageController = null, this.sessionId = null, this.maxSteps = 20, this.aborted = !1, this.config = e;
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 e = await _();
30
- this.pageController = new e({
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 s = this.pageController.mask;
39
- return s != null && s.wrapper && (s.wrapper.style.pointerEvents = "none"), console.log("[CrowBrowserUse] PageController initialized with non-blocking pointer"), this.pageController;
40
- } catch (e) {
41
- throw console.error("[CrowBrowserUse] Failed to initialize PageController:", e), 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(e) {
50
- var s, n, r, o, c, g, f, m, C, y, x, U, b, S;
51
- if (console.log("[CrowBrowserUse] Starting task:", e), this.config.onConfirmation && !await this.config.onConfirmation(e))
52
- return console.log("[CrowBrowserUse] User declined browser automation"), (n = (s = this.config).onProgress) == null || n.call(s, -1, this.maxSteps), {
53
- status: "error",
54
- error: "User declined browser automation",
55
- data: { declined: !0 }
56
- };
57
- try {
58
- const i = await this.initPageController(), P = await this.startSession(e);
59
- this.sessionId = P.session_id, this.maxSteps = P.max_steps, console.log("[CrowBrowserUse] Session started:", this.sessionId);
60
- let l = 0, a;
61
- for (; l < this.maxSteps; ) {
62
- if (this.aborted)
63
- return console.log("[CrowBrowserUse] Task cancelled by user"), await this.cleanup(), (o = (r = this.config).onProgress) == null || o.call(r, -1, this.maxSteps), {
64
- status: "error",
65
- error: "Task cancelled by user"
66
- };
67
- l++;
68
- const k = await i.getBrowserState(), u = i.mask;
69
- u != null && u.wrapper && (u.wrapper.style.pointerEvents = "none");
70
- const t = await this.processStep(k, a);
71
- if (t.needs_user_input && t.question) {
72
- if (console.log("[CrowBrowserUse] Asking user:", t.question), !this.config.onQuestion) {
73
- a = "User input not available - no callback provided", console.warn("[CrowBrowserUse] No onQuestion callback provided");
74
- continue;
75
- }
76
- try {
77
- const p = await this.config.onQuestion(t.question);
78
- a = `User answered: ${p}`, console.log("[CrowBrowserUse] User answered:", p);
79
- } catch (p) {
80
- if (a = "User cancelled or failed to respond", console.log("[CrowBrowserUse] User cancelled or error:", p), this.aborted)
81
- return console.log("[CrowBrowserUse] Aborted after user cancelled"), await this.cleanup(), (g = (c = this.config).onProgress) == null || g.call(c, -1, this.maxSteps), {
82
- status: "error",
83
- error: "Task cancelled by user"
84
- };
85
- }
86
- continue;
87
- }
88
- if (t.done)
89
- return console.log("[CrowBrowserUse] Task completed:", t.message), await this.cleanup(), (m = (f = this.config).onProgress) == null || m.call(f, l, this.maxSteps), {
90
- status: t.success ? "success" : "error",
91
- data: {
92
- message: t.message,
93
- steps: l
94
- },
95
- error: t.success ? void 0 : t.message
96
- };
97
- if (t.error)
98
- return console.error("[CrowBrowserUse] Error:", t.error), await this.cleanup(), (y = (C = this.config).onProgress) == null || y.call(C, -1, this.maxSteps), {
99
- status: "error",
100
- error: t.error
101
- };
102
- t.action && (a = await this.executeAction(i, t.action), console.log(`[CrowBrowserUse] Step ${l}:`, a)), t.reflection && console.log("[CrowBrowserUse] Reflection:", t.reflection.next_goal);
103
- }
104
- return await this.cleanup(), (U = (x = this.config).onProgress) == null || U.call(x, -1, this.maxSteps), {
105
- status: "error",
106
- error: `Task incomplete after ${this.maxSteps} steps`
107
- };
108
- } catch (i) {
109
- return console.error("[CrowBrowserUse] Error:", i), await this.cleanup(), (S = (b = this.config).onProgress) == null || S.call(b, -1, this.maxSteps), {
110
- status: "error",
111
- error: i instanceof Error ? i.message : String(i)
112
- };
113
- }
114
- }
115
- /**
116
- * Start a browser-use session on the server
117
- */
118
- async startSession(e) {
119
- const s = await fetch(`${this.config.apiUrl}/api/browser-use/start`, {
120
- method: "POST",
121
- headers: { "Content-Type": "application/json" },
122
- body: JSON.stringify({
123
- product_id: this.config.productId,
124
- task: e
125
- })
126
- });
127
- if (!s.ok) {
128
- const n = await s.json().catch(() => ({ detail: "Unknown error" }));
129
- throw new Error(n.detail || `Failed to start session: ${s.status}`);
130
- }
131
- return s.json();
132
- }
133
- /**
134
- * Process a step on the server
135
- */
136
- async processStep(e, s) {
137
- const n = await fetch(`${this.config.apiUrl}/api/browser-use/step`, {
138
- method: "POST",
139
- headers: { "Content-Type": "application/json" },
140
- body: JSON.stringify({
141
- session_id: this.sessionId,
142
- product_id: this.config.productId,
143
- browser_state: e,
144
- action_result: s
145
- })
146
- });
147
- if (!n.ok) {
148
- const r = await n.json().catch(() => ({ detail: "Unknown error" }));
149
- throw new Error(r.detail || `Failed to process step: ${n.status}`);
150
- }
151
- return n.json();
152
- }
153
- /**
154
- * Execute an action using PageController
155
- */
156
- async executeAction(e, s) {
157
- const n = Object.keys(s)[0], r = s[n];
158
- try {
159
- switch (n) {
160
- case "click_element_by_index":
161
- return (await e.clickElement(r.index)).message;
162
- case "input_text":
163
- return (await e.inputText(
164
- r.index,
165
- r.text
166
- )).message;
167
- case "select_dropdown_option":
168
- return (await e.selectOption(
169
- r.index,
170
- r.text
171
- )).message;
172
- case "scroll":
173
- return (await e.scroll({
174
- down: r.down,
175
- numPages: r.num_pages,
176
- pixels: r.pixels,
177
- index: r.index
178
- })).message;
179
- case "scroll_horizontally":
180
- return (await e.scrollHorizontally({
181
- right: r.right,
182
- pixels: r.pixels,
183
- index: r.index
184
- })).message;
185
- case "wait": {
186
- const o = r.seconds || 1;
187
- return await new Promise((c) => setTimeout(c, o * 1e3)), `Waited ${o} seconds`;
188
- }
189
- case "done":
190
- return "Task completed";
191
- default:
192
- return `Unknown action: ${n}`;
193
- }
194
- } catch (o) {
195
- return `Action failed: ${o instanceof Error ? o.message : String(o)}`;
196
- }
197
- }
198
- /**
199
- * Cleanup resources
200
- */
201
- async cleanup() {
202
- if (this.pageController) {
203
- try {
204
- await this.pageController.hideMask(), await this.pageController.cleanUpHighlights(), this.pageController.dispose();
205
- } catch (e) {
206
- console.warn("[CrowBrowserUse] Cleanup error:", e);
207
- }
208
- this.pageController = null;
209
- }
210
- if (this.sessionId) {
211
- try {
212
- await fetch(`${this.config.apiUrl}/api/browser-use/end`, {
213
- method: "POST",
214
- headers: { "Content-Type": "application/json" },
215
- body: JSON.stringify({
216
- session_id: this.sessionId,
217
- product_id: this.config.productId
218
- })
219
- });
220
- } catch {
221
- }
222
- this.sessionId = null;
223
- }
224
- }
225
- /**
226
- * Stop the current task
227
- */
228
- async stop() {
229
- this.aborted = !0, await this.cleanup();
230
- }
231
- }
232
- export {
233
- T as C,
234
- B as s
235
- };