@usecrow/client 0.1.28 → 0.1.30
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.
- package/dist/browser.cjs +1 -1
- package/dist/browser.d.ts +7 -4
- package/dist/browser.js +26 -17
- package/dist/browserUse-B-b3zqW1.cjs +9 -0
- package/dist/browserUse-D5NUCzN4.js +1616 -0
- package/dist/index.cjs +7 -3
- package/dist/index.d.ts +5 -4
- package/dist/index.js +71 -48
- package/package.json +1 -1
- package/dist/PageController-72owMK9b.cjs +0 -9
- package/dist/PageController-BT9YJiz0.js +0 -1379
- package/dist/browserUse-CMYea2D_.cjs +0 -1
- package/dist/browserUse-CMq8wG4u.js +0 -259
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";let p=null,u=null;function w(l){p=l}async function d(){if(p)return p;if(!u)try{u=await Promise.resolve().then(()=>require("./PageController-72owMK9b.cjs"))}catch{throw new Error('PageController not available. Either import from "@usecrow/client/browser" or use the bundled version.')}return u.PageController}class h{constructor(t){this.pageController=null,this.sessionId=null,this.maxSteps=20,this.config=t}async initPageController(){if(this.pageController)return this.pageController;try{const t=await d();this.pageController=new t({enableMask:!0,viewportExpansion:500,highlightLabelOpacity:0,highlightOpacity:0}),await this.pageController.showMask();const e=this.pageController.mask;return e!=null&&e.wrapper&&(e.wrapper.style.pointerEvents="none"),console.log("[CrowBrowserUse] PageController initialized with non-blocking pointer"),this.pageController}catch(t){throw console.error("[CrowBrowserUse] Failed to initialize PageController:",t),new Error("Failed to initialize browser automation. Please import from @usecrow/client/browser.")}}async execute(t){if(console.log("[CrowBrowserUse] Starting task:",t),this.config.onConfirmation&&!await this.config.onConfirmation(t))return console.log("[CrowBrowserUse] User declined browser automation"),{status:"error",error:"User declined browser automation",data:{declined:!0}};try{const e=await this.initPageController(),o=await this.startSession(t);this.sessionId=o.session_id,this.maxSteps=o.max_steps,console.log("[CrowBrowserUse] Session started:",this.sessionId);let s=0,n;for(;s<this.maxSteps;){s++,this.config.onProgress&&this.config.onProgress(s,this.maxSteps);const a=await e.getBrowserState(),r=await this.processStep(a,n);if(r.needs_user_input&&r.question)if(console.log("[CrowBrowserUse] Agent asking user:",r.question),this.config.onQuestion){const c=await this.config.onQuestion(r.question);console.log("[CrowBrowserUse] User responded:",c);const i=await this.submitUserResponse(c,a);if(i.done)return console.log("[CrowBrowserUse] Task completed after user response:",i.message),await this.cleanup(),{status:i.success?"success":"error",data:{message:i.message,steps:s},error:i.success?void 0:i.message};i.action&&(n=await this.executeAction(e,i.action),console.log(`[CrowBrowserUse] Step ${s} (after user response):`,n));continue}else return console.log("[CrowBrowserUse] No question handler, returning needs_input"),await this.cleanup(),{status:"needs_input",error:r.question,data:{question:r.question,sessionId:this.sessionId}};if(r.done)return console.log("[CrowBrowserUse] Task completed:",r.message),await this.cleanup(),{status:r.success?"success":"error",data:{message:r.message,steps:s},error:r.success?void 0:r.message};if(r.error)return console.error("[CrowBrowserUse] Error:",r.error),await this.cleanup(),{status:"error",error:r.error};if(r.action&&(n=await this.executeAction(e,r.action),console.log(`[CrowBrowserUse] Step ${s}:`,n),this.config.onProgress)){const c=Object.keys(r.action)[0];this.config.onProgress(s,this.maxSteps,c)}r.reflection&&console.log("[CrowBrowserUse] Reflection:",r.reflection.next_goal)}return await this.cleanup(),{status:"error",error:`Task incomplete after ${this.maxSteps} steps`}}catch(e){return console.error("[CrowBrowserUse] Error:",e),await this.cleanup(),{status:"error",error:e instanceof Error?e.message:String(e)}}}async startSession(t){const e=await fetch(`${this.config.apiUrl}/api/browser-use/start`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({product_id:this.config.productId,task:t})});if(!e.ok){const o=await e.json().catch(()=>({detail:"Unknown error"}));throw new Error(o.detail||`Failed to start session: ${e.status}`)}return e.json()}async processStep(t,e){const o=await fetch(`${this.config.apiUrl}/api/browser-use/step`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({session_id:this.sessionId,product_id:this.config.productId,browser_state:t,action_result:e})});if(!o.ok){const s=await o.json().catch(()=>({detail:"Unknown error"}));throw new Error(s.detail||`Failed to process step: ${o.status}`)}return o.json()}async submitUserResponse(t,e){const o=await fetch(`${this.config.apiUrl}/api/browser-use/user-response`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({session_id:this.sessionId,product_id:this.config.productId,user_response:t,browser_state:e})});if(!o.ok){const s=await o.json().catch(()=>({detail:"Unknown error"}));throw new Error(s.detail||`Failed to submit user response: ${o.status}`)}return o.json()}async executeAction(t,e){const o=Object.keys(e)[0],s=e[o];try{switch(o){case"click_element_by_index":return(await t.clickElement(s.index)).message;case"input_text":return(await t.inputText(s.index,s.text)).message;case"select_dropdown_option":return(await t.selectOption(s.index,s.text)).message;case"scroll":return(await t.scroll({down:s.down,numPages:s.num_pages,pixels:s.pixels,index:s.index})).message;case"scroll_horizontally":return(await t.scrollHorizontally({right:s.right,pixels:s.pixels,index:s.index})).message;case"wait":{const n=s.seconds||1;return await new Promise(a=>setTimeout(a,n*1e3)),`Waited ${n} seconds`}case"done":return"Task completed";default:return`Unknown action: ${o}`}}catch(n){return`Action failed: ${n instanceof Error?n.message:String(n)}`}}async cleanup(){if(this.pageController){try{await this.pageController.hideMask(),await this.pageController.cleanUpHighlights(),this.pageController.dispose()}catch(t){console.warn("[CrowBrowserUse] Cleanup error:",t)}this.pageController=null}if(this.sessionId){try{await fetch(`${this.config.apiUrl}/api/browser-use/end`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({session_id:this.sessionId,product_id:this.config.productId})})}catch{}this.sessionId=null}}async stop(){await this.cleanup()}}exports.CrowBrowserUse=h;exports.setPageController=w;
|
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
let p = null, u = null;
|
|
2
|
-
function d(l) {
|
|
3
|
-
p = l;
|
|
4
|
-
}
|
|
5
|
-
async function w() {
|
|
6
|
-
if (p)
|
|
7
|
-
return p;
|
|
8
|
-
if (!u)
|
|
9
|
-
try {
|
|
10
|
-
u = await import("./PageController-BT9YJiz0.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 u.PageController;
|
|
17
|
-
}
|
|
18
|
-
class h {
|
|
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 w();
|
|
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
|
-
if (console.log("[CrowBrowserUse] Starting task:", t), this.config.onConfirmation && !await this.config.onConfirmation(t))
|
|
51
|
-
return console.log("[CrowBrowserUse] User declined browser automation"), {
|
|
52
|
-
status: "error",
|
|
53
|
-
error: "User declined browser automation",
|
|
54
|
-
data: { declined: !0 }
|
|
55
|
-
};
|
|
56
|
-
try {
|
|
57
|
-
const e = await this.initPageController(), o = await this.startSession(t);
|
|
58
|
-
this.sessionId = o.session_id, this.maxSteps = o.max_steps, console.log("[CrowBrowserUse] Session started:", this.sessionId);
|
|
59
|
-
let s = 0, n;
|
|
60
|
-
for (; s < this.maxSteps; ) {
|
|
61
|
-
s++, this.config.onProgress && this.config.onProgress(s, this.maxSteps);
|
|
62
|
-
const a = await e.getBrowserState(), r = await this.processStep(a, n);
|
|
63
|
-
if (r.needs_user_input && r.question)
|
|
64
|
-
if (console.log("[CrowBrowserUse] Agent asking user:", r.question), this.config.onQuestion) {
|
|
65
|
-
const c = await this.config.onQuestion(r.question);
|
|
66
|
-
console.log("[CrowBrowserUse] User responded:", c);
|
|
67
|
-
const i = await this.submitUserResponse(c, a);
|
|
68
|
-
if (i.done)
|
|
69
|
-
return console.log("[CrowBrowserUse] Task completed after user response:", i.message), await this.cleanup(), {
|
|
70
|
-
status: i.success ? "success" : "error",
|
|
71
|
-
data: {
|
|
72
|
-
message: i.message,
|
|
73
|
-
steps: s
|
|
74
|
-
},
|
|
75
|
-
error: i.success ? void 0 : i.message
|
|
76
|
-
};
|
|
77
|
-
i.action && (n = await this.executeAction(e, i.action), console.log(`[CrowBrowserUse] Step ${s} (after user response):`, n));
|
|
78
|
-
continue;
|
|
79
|
-
} else
|
|
80
|
-
return console.log("[CrowBrowserUse] No question handler, returning needs_input"), await this.cleanup(), {
|
|
81
|
-
status: "needs_input",
|
|
82
|
-
error: r.question,
|
|
83
|
-
data: {
|
|
84
|
-
question: r.question,
|
|
85
|
-
sessionId: this.sessionId
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
if (r.done)
|
|
89
|
-
return console.log("[CrowBrowserUse] Task completed:", r.message), await this.cleanup(), {
|
|
90
|
-
status: r.success ? "success" : "error",
|
|
91
|
-
data: {
|
|
92
|
-
message: r.message,
|
|
93
|
-
steps: s
|
|
94
|
-
},
|
|
95
|
-
error: r.success ? void 0 : r.message
|
|
96
|
-
};
|
|
97
|
-
if (r.error)
|
|
98
|
-
return console.error("[CrowBrowserUse] Error:", r.error), await this.cleanup(), {
|
|
99
|
-
status: "error",
|
|
100
|
-
error: r.error
|
|
101
|
-
};
|
|
102
|
-
if (r.action && (n = await this.executeAction(e, r.action), console.log(`[CrowBrowserUse] Step ${s}:`, n), this.config.onProgress)) {
|
|
103
|
-
const c = Object.keys(r.action)[0];
|
|
104
|
-
this.config.onProgress(s, this.maxSteps, c);
|
|
105
|
-
}
|
|
106
|
-
r.reflection && console.log("[CrowBrowserUse] Reflection:", r.reflection.next_goal);
|
|
107
|
-
}
|
|
108
|
-
return await this.cleanup(), {
|
|
109
|
-
status: "error",
|
|
110
|
-
error: `Task incomplete after ${this.maxSteps} steps`
|
|
111
|
-
};
|
|
112
|
-
} catch (e) {
|
|
113
|
-
return console.error("[CrowBrowserUse] Error:", e), await this.cleanup(), {
|
|
114
|
-
status: "error",
|
|
115
|
-
error: e instanceof Error ? e.message : String(e)
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Start a browser-use session on the server
|
|
121
|
-
*/
|
|
122
|
-
async startSession(t) {
|
|
123
|
-
const e = await fetch(`${this.config.apiUrl}/api/browser-use/start`, {
|
|
124
|
-
method: "POST",
|
|
125
|
-
headers: { "Content-Type": "application/json" },
|
|
126
|
-
body: JSON.stringify({
|
|
127
|
-
product_id: this.config.productId,
|
|
128
|
-
task: t
|
|
129
|
-
})
|
|
130
|
-
});
|
|
131
|
-
if (!e.ok) {
|
|
132
|
-
const o = await e.json().catch(() => ({ detail: "Unknown error" }));
|
|
133
|
-
throw new Error(o.detail || `Failed to start session: ${e.status}`);
|
|
134
|
-
}
|
|
135
|
-
return e.json();
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Process a step on the server
|
|
139
|
-
*/
|
|
140
|
-
async processStep(t, e) {
|
|
141
|
-
const o = await fetch(`${this.config.apiUrl}/api/browser-use/step`, {
|
|
142
|
-
method: "POST",
|
|
143
|
-
headers: { "Content-Type": "application/json" },
|
|
144
|
-
body: JSON.stringify({
|
|
145
|
-
session_id: this.sessionId,
|
|
146
|
-
product_id: this.config.productId,
|
|
147
|
-
browser_state: t,
|
|
148
|
-
action_result: e
|
|
149
|
-
})
|
|
150
|
-
});
|
|
151
|
-
if (!o.ok) {
|
|
152
|
-
const s = await o.json().catch(() => ({ detail: "Unknown error" }));
|
|
153
|
-
throw new Error(s.detail || `Failed to process step: ${o.status}`);
|
|
154
|
-
}
|
|
155
|
-
return o.json();
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Submit user's response to an ask_user question
|
|
159
|
-
*/
|
|
160
|
-
async submitUserResponse(t, e) {
|
|
161
|
-
const o = await fetch(`${this.config.apiUrl}/api/browser-use/user-response`, {
|
|
162
|
-
method: "POST",
|
|
163
|
-
headers: { "Content-Type": "application/json" },
|
|
164
|
-
body: JSON.stringify({
|
|
165
|
-
session_id: this.sessionId,
|
|
166
|
-
product_id: this.config.productId,
|
|
167
|
-
user_response: t,
|
|
168
|
-
browser_state: e
|
|
169
|
-
})
|
|
170
|
-
});
|
|
171
|
-
if (!o.ok) {
|
|
172
|
-
const s = await o.json().catch(() => ({ detail: "Unknown error" }));
|
|
173
|
-
throw new Error(s.detail || `Failed to submit user response: ${o.status}`);
|
|
174
|
-
}
|
|
175
|
-
return o.json();
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Execute an action using PageController
|
|
179
|
-
*/
|
|
180
|
-
async executeAction(t, e) {
|
|
181
|
-
const o = Object.keys(e)[0], s = e[o];
|
|
182
|
-
try {
|
|
183
|
-
switch (o) {
|
|
184
|
-
case "click_element_by_index":
|
|
185
|
-
return (await t.clickElement(s.index)).message;
|
|
186
|
-
case "input_text":
|
|
187
|
-
return (await t.inputText(
|
|
188
|
-
s.index,
|
|
189
|
-
s.text
|
|
190
|
-
)).message;
|
|
191
|
-
case "select_dropdown_option":
|
|
192
|
-
return (await t.selectOption(
|
|
193
|
-
s.index,
|
|
194
|
-
s.text
|
|
195
|
-
)).message;
|
|
196
|
-
case "scroll":
|
|
197
|
-
return (await t.scroll({
|
|
198
|
-
down: s.down,
|
|
199
|
-
numPages: s.num_pages,
|
|
200
|
-
pixels: s.pixels,
|
|
201
|
-
index: s.index
|
|
202
|
-
})).message;
|
|
203
|
-
case "scroll_horizontally":
|
|
204
|
-
return (await t.scrollHorizontally({
|
|
205
|
-
right: s.right,
|
|
206
|
-
pixels: s.pixels,
|
|
207
|
-
index: s.index
|
|
208
|
-
})).message;
|
|
209
|
-
case "wait": {
|
|
210
|
-
const n = s.seconds || 1;
|
|
211
|
-
return await new Promise((a) => setTimeout(a, n * 1e3)), `Waited ${n} seconds`;
|
|
212
|
-
}
|
|
213
|
-
case "done":
|
|
214
|
-
return "Task completed";
|
|
215
|
-
default:
|
|
216
|
-
return `Unknown action: ${o}`;
|
|
217
|
-
}
|
|
218
|
-
} catch (n) {
|
|
219
|
-
return `Action failed: ${n instanceof Error ? n.message : String(n)}`;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Cleanup resources
|
|
224
|
-
*/
|
|
225
|
-
async cleanup() {
|
|
226
|
-
if (this.pageController) {
|
|
227
|
-
try {
|
|
228
|
-
await this.pageController.hideMask(), await this.pageController.cleanUpHighlights(), this.pageController.dispose();
|
|
229
|
-
} catch (t) {
|
|
230
|
-
console.warn("[CrowBrowserUse] Cleanup error:", t);
|
|
231
|
-
}
|
|
232
|
-
this.pageController = null;
|
|
233
|
-
}
|
|
234
|
-
if (this.sessionId) {
|
|
235
|
-
try {
|
|
236
|
-
await fetch(`${this.config.apiUrl}/api/browser-use/end`, {
|
|
237
|
-
method: "POST",
|
|
238
|
-
headers: { "Content-Type": "application/json" },
|
|
239
|
-
body: JSON.stringify({
|
|
240
|
-
session_id: this.sessionId,
|
|
241
|
-
product_id: this.config.productId
|
|
242
|
-
})
|
|
243
|
-
});
|
|
244
|
-
} catch {
|
|
245
|
-
}
|
|
246
|
-
this.sessionId = null;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Stop the current task
|
|
251
|
-
*/
|
|
252
|
-
async stop() {
|
|
253
|
-
await this.cleanup();
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
export {
|
|
257
|
-
h as C,
|
|
258
|
-
d as s
|
|
259
|
-
};
|