@usecrow/client 0.1.36 → 0.1.37
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/PageController-Cu6KUkcn.cjs +9 -0
- package/dist/{PageController-BweWYS-Z.js → PageController-D3uwrwlG.js} +588 -382
- package/dist/browser.cjs +1 -1
- package/dist/browser.d.ts +65 -0
- package/dist/browser.js +2 -2
- package/dist/{browserUse-Btg7osSj.js → browserUse-Cioetz2-.js} +1 -1
- package/dist/{browserUse-BjeJDX8x.cjs → browserUse-DYW8fqvT.cjs} +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +415 -0
- package/dist/index.js +208 -78
- package/package.json +1 -1
- package/dist/PageController-BDcmu8Xe.cjs +0 -9
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as
|
|
1
|
+
import { C as D } from "./browserUse-Cioetz2-.js";
|
|
2
2
|
class _ {
|
|
3
3
|
constructor() {
|
|
4
4
|
this.state = {
|
|
@@ -71,7 +71,7 @@ class _ {
|
|
|
71
71
|
t(e);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
class
|
|
74
|
+
class v {
|
|
75
75
|
constructor() {
|
|
76
76
|
this.handlers = {};
|
|
77
77
|
}
|
|
@@ -115,15 +115,15 @@ class k {
|
|
|
115
115
|
const r = await s(t);
|
|
116
116
|
return console.log(`[Crow] Tool ${e} completed:`, r), r;
|
|
117
117
|
} catch (r) {
|
|
118
|
-
const
|
|
118
|
+
const o = r instanceof Error ? r.message : String(r);
|
|
119
119
|
return console.error(`[Crow] Tool ${e} failed:`, r), {
|
|
120
120
|
status: "error",
|
|
121
|
-
error:
|
|
121
|
+
error: o
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
|
-
const
|
|
126
|
+
const k = "crow_conv_";
|
|
127
127
|
class C {
|
|
128
128
|
constructor(e, t) {
|
|
129
129
|
this.currentId = null, this.productId = e, this.apiUrl = t, this.currentId = this.loadFromStorage();
|
|
@@ -132,7 +132,7 @@ class C {
|
|
|
132
132
|
* Get localStorage key for this product
|
|
133
133
|
*/
|
|
134
134
|
getStorageKey() {
|
|
135
|
-
return `${
|
|
135
|
+
return `${k}${this.productId}`;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
138
|
* Load conversation ID from localStorage
|
|
@@ -269,11 +269,11 @@ class C {
|
|
|
269
269
|
return e;
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
|
-
function
|
|
273
|
-
if (
|
|
272
|
+
function S(a) {
|
|
273
|
+
if (a === "[DONE]")
|
|
274
274
|
return { type: "done" };
|
|
275
275
|
try {
|
|
276
|
-
const e = JSON.parse(
|
|
276
|
+
const e = JSON.parse(a);
|
|
277
277
|
switch (e.type) {
|
|
278
278
|
case "verification_status":
|
|
279
279
|
return {
|
|
@@ -350,18 +350,18 @@ function b(o) {
|
|
|
350
350
|
return null;
|
|
351
351
|
}
|
|
352
352
|
} catch {
|
|
353
|
-
return console.error("[Crow] Failed to parse SSE data:",
|
|
353
|
+
return console.error("[Crow] Failed to parse SSE data:", a), null;
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
|
-
function*
|
|
357
|
-
const e =
|
|
356
|
+
function* b(a) {
|
|
357
|
+
const e = a.split(`
|
|
358
358
|
`);
|
|
359
359
|
for (const t of e)
|
|
360
360
|
t.startsWith("data: ") && (yield t.slice(6).trim());
|
|
361
361
|
}
|
|
362
|
-
async function*
|
|
363
|
-
var
|
|
364
|
-
const t = (
|
|
362
|
+
async function* T(a, e) {
|
|
363
|
+
var o;
|
|
364
|
+
const t = (o = a.body) == null ? void 0 : o.getReader();
|
|
365
365
|
if (!t)
|
|
366
366
|
throw new Error("Response body is not readable");
|
|
367
367
|
const s = new TextDecoder();
|
|
@@ -372,12 +372,12 @@ async function* M(o, e) {
|
|
|
372
372
|
t.cancel();
|
|
373
373
|
return;
|
|
374
374
|
}
|
|
375
|
-
const { done:
|
|
376
|
-
if (
|
|
377
|
-
const
|
|
378
|
-
for (const
|
|
379
|
-
const
|
|
380
|
-
if (
|
|
375
|
+
const { done: n, value: u } = await t.read();
|
|
376
|
+
if (n) break;
|
|
377
|
+
const d = s.decode(u);
|
|
378
|
+
for (const l of b(d)) {
|
|
379
|
+
const h = S(l);
|
|
380
|
+
if (h && (h.type === "content" ? (r += h.text, yield { ...h, accumulated: r }) : yield h, h.type === "done"))
|
|
381
381
|
return;
|
|
382
382
|
}
|
|
383
383
|
}
|
|
@@ -385,20 +385,20 @@ async function* M(o, e) {
|
|
|
385
385
|
t.releaseLock();
|
|
386
386
|
}
|
|
387
387
|
}
|
|
388
|
-
async function
|
|
388
|
+
async function $() {
|
|
389
389
|
try {
|
|
390
390
|
return window.location.reload(), { status: "success", data: { refreshed: !0 } };
|
|
391
|
-
} catch (
|
|
391
|
+
} catch (a) {
|
|
392
392
|
return {
|
|
393
393
|
status: "error",
|
|
394
|
-
error:
|
|
394
|
+
error: a instanceof Error ? a.message : "Failed to refresh page"
|
|
395
395
|
};
|
|
396
396
|
}
|
|
397
397
|
}
|
|
398
|
-
async function
|
|
399
|
-
var
|
|
398
|
+
async function x() {
|
|
399
|
+
var a;
|
|
400
400
|
try {
|
|
401
|
-
const e = document.title, t = window.location.href, s = window.location.pathname, r = (((
|
|
401
|
+
const e = document.title, t = window.location.href, s = window.location.pathname, r = (((a = document.body) == null ? void 0 : a.innerText) || "").slice(0, 2e3).trim();
|
|
402
402
|
return {
|
|
403
403
|
status: "success",
|
|
404
404
|
data: {
|
|
@@ -416,19 +416,19 @@ async function T() {
|
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
418
|
const w = {
|
|
419
|
-
refreshPage:
|
|
420
|
-
whatsOnScreen:
|
|
421
|
-
},
|
|
422
|
-
class
|
|
419
|
+
refreshPage: $,
|
|
420
|
+
whatsOnScreen: x
|
|
421
|
+
}, I = Object.keys(w), E = "https://api.usecrow.org", M = "claude-sonnet-4-20250514";
|
|
422
|
+
class U {
|
|
423
423
|
constructor(e) {
|
|
424
424
|
this.context = {}, this.abortController = null, this.callbacks = {}, this._messages = [], this.messageListeners = /* @__PURE__ */ new Set(), this._isLoading = !1, this.loadingListeners = /* @__PURE__ */ new Set(), this.config = {
|
|
425
425
|
productId: e.productId,
|
|
426
426
|
apiUrl: e.apiUrl || E,
|
|
427
|
-
model: e.model ||
|
|
428
|
-
}, this.identity = new _(), this.tools = new
|
|
427
|
+
model: e.model || M
|
|
428
|
+
}, this.identity = new _(), this.tools = new v(), this.conversations = new C(
|
|
429
429
|
this.config.productId,
|
|
430
430
|
this.config.apiUrl
|
|
431
|
-
), this.tools.register(w), console.log("[Crow] Default tools registered:",
|
|
431
|
+
), this.tools.register(w), console.log("[Crow] Default tools registered:", I.join(", ")), this.identity.subscribe((t) => {
|
|
432
432
|
var s, r;
|
|
433
433
|
(r = (s = this.callbacks).onVerificationStatus) == null || r.call(s, t.isVerified);
|
|
434
434
|
});
|
|
@@ -585,7 +585,7 @@ class x {
|
|
|
585
585
|
updateMessage(e, t) {
|
|
586
586
|
var s, r;
|
|
587
587
|
this._messages = this._messages.map(
|
|
588
|
-
(
|
|
588
|
+
(o) => o.id === e ? { ...o, ...t } : o
|
|
589
589
|
), this.notifyMessages(), (r = (s = this.callbacks).onMessageUpdate) == null || r.call(s, e, t);
|
|
590
590
|
}
|
|
591
591
|
generateMessageId(e) {
|
|
@@ -635,7 +635,7 @@ class x {
|
|
|
635
635
|
* Returns an async generator of stream events
|
|
636
636
|
*/
|
|
637
637
|
async *sendMessage(e) {
|
|
638
|
-
var
|
|
638
|
+
var n, u, d, l, h, g, p, f, m, y;
|
|
639
639
|
if (!e.trim())
|
|
640
640
|
return;
|
|
641
641
|
const t = this.generateMessageId("user");
|
|
@@ -652,7 +652,7 @@ class x {
|
|
|
652
652
|
role: "assistant",
|
|
653
653
|
timestamp: /* @__PURE__ */ new Date()
|
|
654
654
|
}), this.setLoading(!0), this.abortController = new AbortController();
|
|
655
|
-
let r = "",
|
|
655
|
+
let r = "", o = "";
|
|
656
656
|
try {
|
|
657
657
|
const c = await fetch(`${this.config.apiUrl}/api/chat/message`, {
|
|
658
658
|
method: "POST",
|
|
@@ -669,53 +669,53 @@ class x {
|
|
|
669
669
|
});
|
|
670
670
|
if (!c.ok)
|
|
671
671
|
throw new Error(`HTTP error: ${c.status}`);
|
|
672
|
-
for await (const
|
|
673
|
-
switch (
|
|
672
|
+
for await (const i of T(c, this.abortController.signal)) {
|
|
673
|
+
switch (i.type) {
|
|
674
674
|
case "content":
|
|
675
|
-
r =
|
|
675
|
+
r = i.accumulated, this.updateMessage(s, { content: r });
|
|
676
676
|
break;
|
|
677
677
|
case "thinking":
|
|
678
|
-
|
|
678
|
+
o += i.content, this.updateMessage(s, { thinking: o });
|
|
679
679
|
break;
|
|
680
680
|
case "thinking_complete":
|
|
681
681
|
this.updateMessage(s, { thinkingComplete: !0 });
|
|
682
682
|
break;
|
|
683
683
|
case "citations":
|
|
684
|
-
this.updateMessage(s, { citations:
|
|
684
|
+
this.updateMessage(s, { citations: i.citations });
|
|
685
685
|
break;
|
|
686
686
|
case "verification_status":
|
|
687
|
-
this.identity.setVerified(
|
|
687
|
+
this.identity.setVerified(i.isVerified);
|
|
688
688
|
break;
|
|
689
689
|
case "conversation_id":
|
|
690
|
-
this.conversations.setCurrentId(
|
|
690
|
+
this.conversations.setCurrentId(i.conversationId);
|
|
691
691
|
break;
|
|
692
692
|
case "client_tool_call":
|
|
693
|
-
await this.tools.execute(
|
|
693
|
+
await this.tools.execute(i.toolName, i.arguments), (u = (n = this.callbacks).onToolCall) == null || u.call(n, i);
|
|
694
694
|
break;
|
|
695
695
|
case "tool_call_start":
|
|
696
696
|
case "tool_call_complete":
|
|
697
|
-
(
|
|
697
|
+
(l = (d = this.callbacks).onToolCall) == null || l.call(d, i);
|
|
698
698
|
break;
|
|
699
699
|
case "workflow_started":
|
|
700
700
|
case "workflow_todo_updated":
|
|
701
701
|
case "workflow_ended":
|
|
702
702
|
case "workflow_complete_prompt":
|
|
703
|
-
(g = (
|
|
703
|
+
(g = (h = this.callbacks).onWorkflow) == null || g.call(h, i);
|
|
704
704
|
break;
|
|
705
705
|
case "error":
|
|
706
|
-
this.updateMessage(s, { content:
|
|
706
|
+
this.updateMessage(s, { content: i.message }), (f = (p = this.callbacks).onError) == null || f.call(p, new Error(i.message));
|
|
707
707
|
break;
|
|
708
708
|
}
|
|
709
|
-
yield
|
|
709
|
+
yield i;
|
|
710
710
|
}
|
|
711
711
|
} catch (c) {
|
|
712
712
|
if (c instanceof Error && c.name === "AbortError") {
|
|
713
|
-
r ? this.updateMessage(s, { content: r }) : (this._messages = this._messages.filter((
|
|
713
|
+
r ? this.updateMessage(s, { content: r }) : (this._messages = this._messages.filter((i) => i.id !== s), this.notifyMessages());
|
|
714
714
|
return;
|
|
715
715
|
}
|
|
716
716
|
console.error("[Crow] Error:", c), this.updateMessage(s, {
|
|
717
717
|
content: "Sorry, I encountered an error. Please try again."
|
|
718
|
-
}), (
|
|
718
|
+
}), (y = (m = this.callbacks).onError) == null || y.call(m, c instanceof Error ? c : new Error(String(c)));
|
|
719
719
|
} finally {
|
|
720
720
|
this.setLoading(!1), this.abortController = null;
|
|
721
721
|
}
|
|
@@ -747,50 +747,50 @@ class x {
|
|
|
747
747
|
this.stop(), this.messageListeners.clear(), this.loadingListeners.clear();
|
|
748
748
|
}
|
|
749
749
|
}
|
|
750
|
-
function
|
|
751
|
-
const s =
|
|
752
|
-
(
|
|
750
|
+
function L(a, e, t) {
|
|
751
|
+
const s = a.find(
|
|
752
|
+
(o) => o.name.toLowerCase() === e.toLowerCase()
|
|
753
753
|
);
|
|
754
754
|
if (!s) return null;
|
|
755
755
|
let r = s.path;
|
|
756
756
|
if (t)
|
|
757
|
-
for (const [
|
|
758
|
-
r = r.replace(`:${
|
|
757
|
+
for (const [o, n] of Object.entries(t))
|
|
758
|
+
r = r.replace(`:${o}`, String(n));
|
|
759
759
|
return r;
|
|
760
760
|
}
|
|
761
|
-
function O(
|
|
761
|
+
function O(a, e) {
|
|
762
762
|
return async (t) => {
|
|
763
763
|
try {
|
|
764
|
-
const s = t.page, r = t.params,
|
|
765
|
-
let
|
|
764
|
+
const s = t.page, r = t.params, o = t.url;
|
|
765
|
+
let n = null;
|
|
766
766
|
if (s) {
|
|
767
|
-
if (
|
|
767
|
+
if (n = L(a, s, r), !n)
|
|
768
768
|
return {
|
|
769
769
|
status: "error",
|
|
770
|
-
error: `Unknown page: "${s}". Available pages: ${
|
|
770
|
+
error: `Unknown page: "${s}". Available pages: ${a.map((d) => d.name).join(", ")}`
|
|
771
771
|
};
|
|
772
|
-
} else if (
|
|
773
|
-
|
|
772
|
+
} else if (o)
|
|
773
|
+
n = o;
|
|
774
774
|
else
|
|
775
775
|
return {
|
|
776
776
|
status: "error",
|
|
777
777
|
error: 'Either "page" or "url" parameter is required'
|
|
778
778
|
};
|
|
779
|
-
const
|
|
780
|
-
return
|
|
779
|
+
const u = n.match(/:([a-zA-Z_][a-zA-Z0-9_]*)/g);
|
|
780
|
+
return u ? {
|
|
781
781
|
status: "error",
|
|
782
|
-
error: `Missing parameters: ${
|
|
783
|
-
} : e ? (e(
|
|
782
|
+
error: `Missing parameters: ${u.join(", ")}. Please provide values for these parameters.`
|
|
783
|
+
} : e ? (e(n), {
|
|
784
784
|
status: "success",
|
|
785
785
|
data: {
|
|
786
|
-
navigated_to:
|
|
786
|
+
navigated_to: n,
|
|
787
787
|
page: s || void 0,
|
|
788
788
|
method: "spa_router"
|
|
789
789
|
}
|
|
790
|
-
}) : (window.location.href =
|
|
790
|
+
}) : (window.location.href = n, {
|
|
791
791
|
status: "success",
|
|
792
792
|
data: {
|
|
793
|
-
navigated_to:
|
|
793
|
+
navigated_to: n,
|
|
794
794
|
page: s || void 0,
|
|
795
795
|
method: "full_navigation"
|
|
796
796
|
}
|
|
@@ -803,17 +803,147 @@ function O(o, e) {
|
|
|
803
803
|
}
|
|
804
804
|
};
|
|
805
805
|
}
|
|
806
|
+
class R {
|
|
807
|
+
constructor(e, t = {}) {
|
|
808
|
+
this.controller = e, this.config = {
|
|
809
|
+
waitTimeout: t.waitTimeout ?? 3e3,
|
|
810
|
+
pollInterval: t.pollInterval ?? 300,
|
|
811
|
+
stepDelay: t.stepDelay ?? 500,
|
|
812
|
+
stopOnFailure: t.stopOnFailure ?? !1,
|
|
813
|
+
onStepProgress: t.onStepProgress ?? (() => {
|
|
814
|
+
})
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Execute a recorded workflow with variable substitution.
|
|
819
|
+
*/
|
|
820
|
+
async execute(e, t = {}) {
|
|
821
|
+
const s = [];
|
|
822
|
+
let r = 0;
|
|
823
|
+
console.log(`[WorkflowExecutor] Starting workflow: ${e.name} (${e.steps.length} steps)`);
|
|
824
|
+
for (let n = 0; n < e.steps.length; n++) {
|
|
825
|
+
const u = e.steps[n], d = this.resolveVariables(u, t);
|
|
826
|
+
console.log(`[WorkflowExecutor] Step ${n + 1}/${e.steps.length}: ${d.description}`);
|
|
827
|
+
const l = await this.executeStep(d, n);
|
|
828
|
+
if (s.push(l), this.config.onStepProgress(l), !l.success && (r++, console.warn(`[WorkflowExecutor] Step ${n + 1} failed: ${l.message}`), this.config.stopOnFailure))
|
|
829
|
+
return {
|
|
830
|
+
success: !1,
|
|
831
|
+
workflow_name: e.name,
|
|
832
|
+
total_steps: e.steps.length,
|
|
833
|
+
completed_steps: n + 1,
|
|
834
|
+
failed_steps: r,
|
|
835
|
+
step_results: s,
|
|
836
|
+
error: `Stopped at step ${n + 1}: ${l.message}`
|
|
837
|
+
};
|
|
838
|
+
n < e.steps.length - 1 && await this.delay(this.config.stepDelay);
|
|
839
|
+
}
|
|
840
|
+
const o = r === 0;
|
|
841
|
+
return console.log(
|
|
842
|
+
`[WorkflowExecutor] Workflow "${e.name}" ${o ? "completed successfully" : `completed with ${r} failures`}`
|
|
843
|
+
), {
|
|
844
|
+
success: o,
|
|
845
|
+
workflow_name: e.name,
|
|
846
|
+
total_steps: e.steps.length,
|
|
847
|
+
completed_steps: e.steps.length,
|
|
848
|
+
failed_steps: r,
|
|
849
|
+
step_results: s
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Execute a single step.
|
|
854
|
+
*/
|
|
855
|
+
async executeStep(e, t) {
|
|
856
|
+
const s = {
|
|
857
|
+
step_index: t,
|
|
858
|
+
step_type: e.type,
|
|
859
|
+
description: e.description,
|
|
860
|
+
success: !1,
|
|
861
|
+
message: ""
|
|
862
|
+
};
|
|
863
|
+
try {
|
|
864
|
+
if (e.type === "navigation") {
|
|
865
|
+
if (!e.url)
|
|
866
|
+
return { ...s, message: "Navigation step has no URL" };
|
|
867
|
+
const o = await this.controller.navigateToUrl(e.url);
|
|
868
|
+
return await this.delay(1e3), { ...s, success: o.success, message: o.message };
|
|
869
|
+
}
|
|
870
|
+
await this.controller.updateTree();
|
|
871
|
+
let r = this.controller.findElementByStrategies(e.selector_strategies);
|
|
872
|
+
if (r === null && e.selector_strategies.length > 0 && (console.log(`[WorkflowExecutor] Element not found, waiting up to ${this.config.waitTimeout}ms...`), r = await this.controller.waitForElement(
|
|
873
|
+
e.selector_strategies,
|
|
874
|
+
this.config.waitTimeout,
|
|
875
|
+
this.config.pollInterval
|
|
876
|
+
)), r === null && e.target_text && (await this.controller.updateTree(), r = this.controller.findElementByText(e.target_text, !0), r === null && (r = this.controller.findElementByText(e.target_text, !1))), r === null)
|
|
877
|
+
return {
|
|
878
|
+
...s,
|
|
879
|
+
message: `Element not found for step: ${e.description} (target_text: "${e.target_text}")`
|
|
880
|
+
};
|
|
881
|
+
switch (s.element_index = r, e.type) {
|
|
882
|
+
case "click": {
|
|
883
|
+
const o = await this.controller.clickElement(r);
|
|
884
|
+
return { ...s, success: o.success, message: o.message };
|
|
885
|
+
}
|
|
886
|
+
case "input": {
|
|
887
|
+
if (e.value === void 0 || e.value === null)
|
|
888
|
+
return { ...s, message: "Input step has no value" };
|
|
889
|
+
const o = await this.controller.inputText(r, e.value);
|
|
890
|
+
return { ...s, success: o.success, message: o.message };
|
|
891
|
+
}
|
|
892
|
+
case "select": {
|
|
893
|
+
if (!e.value)
|
|
894
|
+
return { ...s, message: "Select step has no value" };
|
|
895
|
+
const o = await this.controller.selectOption(r, e.value);
|
|
896
|
+
return { ...s, success: o.success, message: o.message };
|
|
897
|
+
}
|
|
898
|
+
case "keypress": {
|
|
899
|
+
if (!e.value)
|
|
900
|
+
return { ...s, message: "Keypress step has no key" };
|
|
901
|
+
const o = await this.controller.pressKey(e.value, r);
|
|
902
|
+
return { ...s, success: o.success, message: o.message };
|
|
903
|
+
}
|
|
904
|
+
default:
|
|
905
|
+
return { ...s, message: `Unknown step type: ${e.type}` };
|
|
906
|
+
}
|
|
907
|
+
} catch (r) {
|
|
908
|
+
return {
|
|
909
|
+
...s,
|
|
910
|
+
message: `Error executing step: ${r instanceof Error ? r.message : String(r)}`
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
/**
|
|
915
|
+
* Replace {variable_name} placeholders in step fields with actual values.
|
|
916
|
+
*/
|
|
917
|
+
resolveVariables(e, t) {
|
|
918
|
+
const s = (r) => !r || !r.includes("{") ? r : r.replace(/\{(\w+)\}/g, (o, n) => n in t ? String(t[n]) : o);
|
|
919
|
+
return {
|
|
920
|
+
...e,
|
|
921
|
+
description: s(e.description) || e.description,
|
|
922
|
+
target_text: s(e.target_text) || e.target_text,
|
|
923
|
+
value: s(e.value),
|
|
924
|
+
url: s(e.url),
|
|
925
|
+
selector_strategies: e.selector_strategies.map((r) => ({
|
|
926
|
+
...r,
|
|
927
|
+
value: s(r.value) || r.value
|
|
928
|
+
}))
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
delay(e) {
|
|
932
|
+
return new Promise((t) => setTimeout(t, e));
|
|
933
|
+
}
|
|
934
|
+
}
|
|
806
935
|
export {
|
|
807
936
|
C as ConversationManager,
|
|
808
|
-
|
|
809
|
-
|
|
937
|
+
D as CrowBrowserUse,
|
|
938
|
+
U as CrowClient,
|
|
810
939
|
w as DEFAULT_TOOLS,
|
|
811
|
-
|
|
940
|
+
I as DEFAULT_TOOL_NAMES,
|
|
812
941
|
_ as IdentityManager,
|
|
813
|
-
|
|
942
|
+
v as ToolManager,
|
|
943
|
+
R as WorkflowExecutor,
|
|
814
944
|
O as createNavigateToPageTool,
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
945
|
+
b as parseSSEChunk,
|
|
946
|
+
S as parseSSEData,
|
|
947
|
+
L as resolveRoute,
|
|
948
|
+
T as streamResponse
|
|
819
949
|
};
|
package/package.json
CHANGED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function waitFor(e){await new Promise(n=>setTimeout(n,e*1e3))}async function movePointerToElement(e){const n=e.getBoundingClientRect(),s=n.left+n.width/2,o=n.top+n.height/2;window.dispatchEvent(new CustomEvent("PageAgent::MovePointerTo",{detail:{x:s,y:o}})),await waitFor(.3)}function getElementByIndex(e,n){const s=e.get(n);if(!s)throw new Error(`No interactive element found at index ${n}`);const o=s.ref;if(!o)throw new Error(`Element at index ${n} does not have a reference`);if(!(o instanceof HTMLElement))throw new Error(`Element at index ${n} is not an HTMLElement`);return o}let lastClickedElement=null;function blurLastClickedElement(){lastClickedElement&&(lastClickedElement.blur(),lastClickedElement.dispatchEvent(new MouseEvent("mouseout",{bubbles:!0,cancelable:!0})),lastClickedElement=null)}async function clickElement(e){blurLastClickedElement(),lastClickedElement=e,await scrollIntoViewIfNeeded(e),await movePointerToElement(e),window.dispatchEvent(new CustomEvent("PageAgent::ClickPointer")),await waitFor(.1),e.dispatchEvent(new MouseEvent("mouseenter",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("mouseover",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("mousedown",{bubbles:!0,cancelable:!0})),e.focus(),e.dispatchEvent(new MouseEvent("mouseup",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("click",{bubbles:!0,cancelable:!0})),await waitFor(.1)}let _nativeInputValueSetter,_nativeTextAreaValueSetter;function getNativeInputValueSetter(){return _nativeInputValueSetter||(_nativeInputValueSetter=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value").set),_nativeInputValueSetter}function getNativeTextAreaValueSetter(){return _nativeTextAreaValueSetter||(_nativeTextAreaValueSetter=Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,"value").set),_nativeTextAreaValueSetter}async function inputTextElement(e,n){if(!(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement))throw new Error("Element is not an input or textarea");await clickElement(e),e instanceof HTMLTextAreaElement?getNativeTextAreaValueSetter().call(e,n):getNativeInputValueSetter().call(e,n);const s=new Event("input",{bubbles:!0});e.dispatchEvent(s),await waitFor(.1),blurLastClickedElement()}async function selectOptionElement(e,n){if(!(e instanceof HTMLSelectElement))throw new Error("Element is not a select element");const o=Array.from(e.options).find(w=>{var d;return((d=w.textContent)==null?void 0:d.trim())===n.trim()});if(!o)throw new Error(`Option with text "${n}" not found in select element`);e.value=o.value,e.dispatchEvent(new Event("change",{bubbles:!0})),await waitFor(.1)}async function scrollIntoViewIfNeeded(e){const n=e;n.scrollIntoViewIfNeeded?n.scrollIntoViewIfNeeded():n.scrollIntoView({behavior:"auto",block:"center",inline:"nearest"})}async function scrollVertically(e,n,s){if(s){const l=s;console.log("[SCROLL DEBUG] Starting direct container scroll for element:",l.tagName);let h=l,T=!1,b=null,u=0,f=0;const N=n;for(;h&&f<10;){const D=window.getComputedStyle(h),z=/(auto|scroll|overlay)/.test(D.overflowY),_=h.scrollHeight>h.clientHeight;if(console.log("[SCROLL DEBUG] Checking element:",h.tagName,"hasScrollableY:",z,"canScrollVertically:",_,"scrollHeight:",h.scrollHeight,"clientHeight:",h.clientHeight),z&&_){const R=h.scrollTop,U=h.scrollHeight-h.clientHeight;let H=N/3;H>0?H=Math.min(H,U-R):H=Math.max(H,-R),h.scrollTop=R+H;const V=h.scrollTop,O=V-R;if(console.log("[SCROLL DEBUG] Scroll attempt:",h.tagName,"before:",R,"after:",V,"delta:",O),Math.abs(O)>.5){T=!0,b=h,u=O,console.log("[SCROLL DEBUG] Successfully scrolled container:",h.tagName,"delta:",O);break}}if(h===document.body||h===document.documentElement)break;h=h.parentElement,f++}return T?`Scrolled container (${b==null?void 0:b.tagName}) by ${u}px`:`No scrollable container found for element (${l.tagName})`}const o=n,w=l=>l.clientHeight>=window.innerHeight*.5,d=l=>l&&/(auto|scroll|overlay)/.test(getComputedStyle(l).overflowY)&&l.scrollHeight>l.clientHeight&&w(l);let r=document.activeElement;for(;r&&!d(r)&&r!==document.body;)r=r.parentElement;if(r=d(r)?r:Array.from(document.querySelectorAll("*")).find(d)||document.scrollingElement||document.documentElement,r===document.scrollingElement||r===document.documentElement||r===document.body){const l=window.scrollY,h=document.documentElement.scrollHeight-window.innerHeight;window.scrollBy(0,o);const T=window.scrollY,b=T-l;if(Math.abs(b)<1)return o>0?"⚠️ Already at the bottom of the page, cannot scroll down further.":"⚠️ Already at the top of the page, cannot scroll up further.";const u=o>0&&T>=h-1,f=o<0&&T<=1;return u?`✅ Scrolled page by ${b}px. Reached the bottom of the page.`:f?`✅ Scrolled page by ${b}px. Reached the top of the page.`:`✅ Scrolled page by ${b}px.`}else{const l=r.scrollTop,h=r.scrollHeight-r.clientHeight;r.scrollBy({top:o,behavior:"smooth"}),await waitFor(.1);const T=r.scrollTop,b=T-l;if(Math.abs(b)<1)return o>0?`⚠️ Already at the bottom of container (${r.tagName}), cannot scroll down further.`:`⚠️ Already at the top of container (${r.tagName}), cannot scroll up further.`;const u=o>0&&T>=h-1,f=o<0&&T<=1;return u?`✅ Scrolled container (${r.tagName}) by ${b}px. Reached the bottom.`:f?`✅ Scrolled container (${r.tagName}) by ${b}px. Reached the top.`:`✅ Scrolled container (${r.tagName}) by ${b}px.`}}async function scrollHorizontally(e,n,s){if(s){const l=s;console.log("[SCROLL DEBUG] Starting direct container scroll for element:",l.tagName);let h=l,T=!1,b=null,u=0,f=0;const N=e?n:-n;for(;h&&f<10;){const D=window.getComputedStyle(h),z=/(auto|scroll|overlay)/.test(D.overflowX),_=h.scrollWidth>h.clientWidth;if(console.log("[SCROLL DEBUG] Checking element:",h.tagName,"hasScrollableX:",z,"canScrollHorizontally:",_,"scrollWidth:",h.scrollWidth,"clientWidth:",h.clientWidth),z&&_){const R=h.scrollLeft,U=h.scrollWidth-h.clientWidth;let H=N/3;H>0?H=Math.min(H,U-R):H=Math.max(H,-R),h.scrollLeft=R+H;const V=h.scrollLeft,O=V-R;if(console.log("[SCROLL DEBUG] Scroll attempt:",h.tagName,"before:",R,"after:",V,"delta:",O),Math.abs(O)>.5){T=!0,b=h,u=O,console.log("[SCROLL DEBUG] Successfully scrolled container:",h.tagName,"delta:",O);break}}if(h===document.body||h===document.documentElement)break;h=h.parentElement,f++}return T?`Scrolled container (${b==null?void 0:b.tagName}) horizontally by ${u}px`:`No horizontally scrollable container found for element (${l.tagName})`}const o=e?n:-n,w=l=>l.clientWidth>=window.innerWidth*.5,d=l=>l&&/(auto|scroll|overlay)/.test(getComputedStyle(l).overflowX)&&l.scrollWidth>l.clientWidth&&w(l);let r=document.activeElement;for(;r&&!d(r)&&r!==document.body;)r=r.parentElement;if(r=d(r)?r:Array.from(document.querySelectorAll("*")).find(d)||document.scrollingElement||document.documentElement,r===document.scrollingElement||r===document.documentElement||r===document.body){const l=window.scrollX,h=document.documentElement.scrollWidth-window.innerWidth;window.scrollBy(o,0);const T=window.scrollX,b=T-l;if(Math.abs(b)<1)return o>0?"⚠️ Already at the right edge of the page, cannot scroll right further.":"⚠️ Already at the left edge of the page, cannot scroll left further.";const u=o>0&&T>=h-1,f=o<0&&T<=1;return u?`✅ Scrolled page by ${b}px. Reached the right edge of the page.`:f?`✅ Scrolled page by ${b}px. Reached the left edge of the page.`:`✅ Scrolled page horizontally by ${b}px.`}else{const l=r.scrollLeft,h=r.scrollWidth-r.clientWidth;r.scrollBy({left:o,behavior:"smooth"}),await waitFor(.1);const T=r.scrollLeft,b=T-l;if(Math.abs(b)<1)return o>0?`⚠️ Already at the right edge of container (${r.tagName}), cannot scroll right further.`:`⚠️ Already at the left edge of container (${r.tagName}), cannot scroll left further.`;const u=o>0&&T>=h-1,f=o<0&&T<=1;return u?`✅ Scrolled container (${r.tagName}) by ${b}px. Reached the right edge.`:f?`✅ Scrolled container (${r.tagName}) by ${b}px. Reached the left edge.`:`✅ Scrolled container (${r.tagName}) horizontally by ${b}px.`}}const VIEWPORT_EXPANSION=-1,domTree=(e={doHighlightElements:!0,focusHighlightIndex:-1,viewportExpansion:0,debugMode:!1,interactiveBlacklist:[],interactiveWhitelist:[],highlightOpacity:.1,highlightLabelOpacity:.5})=>{const{interactiveBlacklist:n,interactiveWhitelist:s,highlightOpacity:o,highlightLabelOpacity:w}=e,{doHighlightElements:d,focusHighlightIndex:r,viewportExpansion:l,debugMode:h}=e;let T=0;const b=new WeakMap;function u(t,i){!t||t.nodeType!==Node.ELEMENT_NODE||b.set(t,{...b.get(t),...i})}const f={boundingRects:new WeakMap,clientRects:new WeakMap,computedStyles:new WeakMap,clearCache:()=>{f.boundingRects=new WeakMap,f.clientRects=new WeakMap,f.computedStyles=new WeakMap}};function N(t){if(!t)return null;if(f.boundingRects.has(t))return f.boundingRects.get(t);const i=t.getBoundingClientRect();return i&&f.boundingRects.set(t,i),i}function D(t){if(!t)return null;if(f.computedStyles.has(t))return f.computedStyles.get(t);const i=window.getComputedStyle(t);return i&&f.computedStyles.set(t,i),i}function z(t){if(!t)return null;if(f.clientRects.has(t))return f.clientRects.get(t);const i=t.getClientRects();return i&&f.clientRects.set(t,i),i}const _={},R={current:0},U="playwright-highlight-container";function H(t,i,p=null){if(!t)return i;const c=[];let a=null,x=20,E=16,I=null;try{let m=document.getElementById(U);m||(m=document.createElement("div"),m.id=U,m.style.position="fixed",m.style.pointerEvents="none",m.style.top="0",m.style.left="0",m.style.width="100%",m.style.height="100%",m.style.zIndex="2147483640",m.style.backgroundColor="transparent",document.body.appendChild(m));const A=t.getClientRects();if(!A||A.length===0)return i;const $=["#FF0000","#00FF00","#0000FF","#FFA500","#800080","#008080","#FF69B4","#4B0082","#FF4500","#2E8B57","#DC143C","#4682B4"],g=i%$.length;let y=$[g];const C=y+Math.floor(o*255).toString(16).padStart(2,"0");y=y+Math.floor(w*255).toString(16).padStart(2,"0");let M={x:0,y:0};if(p){const S=p.getBoundingClientRect();M.x=S.left,M.y=S.top}const B=document.createDocumentFragment();for(const S of A){if(S.width===0||S.height===0)continue;const k=document.createElement("div");k.style.position="fixed",k.style.border=`2px solid ${y}`,k.style.backgroundColor=C,k.style.pointerEvents="none",k.style.boxSizing="border-box";const v=S.top+M.y,W=S.left+M.x;k.style.top=`${v}px`,k.style.left=`${W}px`,k.style.width=`${S.width}px`,k.style.height=`${S.height}px`,B.appendChild(k),c.push({element:k,initialRect:S})}const G=A[0];if(w>0){a=document.createElement("div"),a.className="playwright-highlight-label",a.style.position="fixed",a.style.background=y,a.style.color="white",a.style.padding="1px 4px",a.style.borderRadius="4px",a.style.fontSize=`${Math.min(12,Math.max(8,G.height/2))}px`,a.textContent=i.toString(),x=a.offsetWidth>0?a.offsetWidth:x,E=a.offsetHeight>0?a.offsetHeight:E;const S=G.top+M.y,k=G.left+M.x;let v=S+2,W=k+G.width-x-2;(G.width<x+4||G.height<E+4)&&(v=S-E-2,W=k+G.width-x,W<M.x&&(W=k)),v=Math.max(0,Math.min(v,window.innerHeight-E)),W=Math.max(0,Math.min(W,window.innerWidth-x)),a.style.top=`${v}px`,a.style.left=`${W}px`,B.appendChild(a)}const j=((S,k)=>{let v=0;return(...W)=>{const P=performance.now();if(!(P-v<k))return v=P,S(...W)}})(()=>{const S=t.getClientRects();let k={x:0,y:0};if(p){const v=p.getBoundingClientRect();k.x=v.left,k.y=v.top}if(c.forEach((v,W)=>{if(W<S.length){const P=S[W],Z=P.top+k.y,J=P.left+k.x;v.element.style.top=`${Z}px`,v.element.style.left=`${J}px`,v.element.style.width=`${P.width}px`,v.element.style.height=`${P.height}px`,v.element.style.display=P.width===0||P.height===0?"none":"block"}else v.element.style.display="none"}),S.length<c.length)for(let v=S.length;v<c.length;v++)c[v].element.style.display="none";if(a&&S.length>0){const v=S[0],W=v.top+k.y,P=v.left+k.x;let Z=W+2,J=P+v.width-x-2;(v.width<x+4||v.height<E+4)&&(Z=W-E-2,J=P+v.width-x,J<k.x&&(J=P)),Z=Math.max(0,Math.min(Z,window.innerHeight-E)),J=Math.max(0,Math.min(J,window.innerWidth-x)),a.style.top=`${Z}px`,a.style.left=`${J}px`,a.style.display="block"}else a&&(a.style.display="none")},16);return window.addEventListener("scroll",j,!0),window.addEventListener("resize",j),I=()=>{window.removeEventListener("scroll",j,!0),window.removeEventListener("resize",j),c.forEach(S=>S.element.remove()),a&&a.remove()},m.appendChild(B),i+1}finally{I&&(window._highlightCleanupFunctions=window._highlightCleanupFunctions||[]).push(I)}}function V(t){if(!t||t.nodeType!==Node.ELEMENT_NODE)return null;const i=D(t);if(!i)return null;const p=i.display;if(p==="inline"||p==="inline-block")return null;const c=i.overflowX,a=i.overflowY,x=c==="auto"||c==="scroll",E=a==="auto"||a==="scroll";if(!x&&!E)return null;const I=t.scrollWidth-t.clientWidth,m=t.scrollHeight-t.clientHeight,A=4;if(I<A&&m<A||!E&&I<A||!x&&m<A)return null;const $=t.scrollTop,g=t.scrollLeft,y=t.scrollWidth-t.clientWidth-t.scrollLeft,C=t.scrollHeight-t.clientHeight-t.scrollTop,M={top:$,right:y,bottom:C,left:g};return u(t,{scrollable:!0,scrollData:M}),M}function O(t){try{if(l===-1){const E=t.parentElement;if(!E)return!1;try{return E.checkVisibility({checkOpacity:!0,checkVisibilityCSS:!0})}catch{const m=window.getComputedStyle(E);return m.display!=="none"&&m.visibility!=="hidden"&&m.opacity!=="0"}}const i=document.createRange();i.selectNodeContents(t);const p=i.getClientRects();if(!p||p.length===0)return!1;let c=!1,a=!1;for(const E of p)if(E.width>0&&E.height>0&&(c=!0,!(E.bottom<-l||E.top>window.innerHeight+l||E.right<-l||E.left>window.innerWidth+l))){a=!0;break}if(!c||!a)return!1;const x=t.parentElement;if(!x)return!1;try{return x.checkVisibility({checkOpacity:!0,checkVisibilityCSS:!0})}catch{const I=window.getComputedStyle(x);return I.display!=="none"&&I.visibility!=="hidden"&&I.opacity!=="0"}}catch(i){return console.warn("Error checking text node visibility:",i),!1}}function it(t){if(!t||!t.tagName)return!1;const i=new Set(["body","div","main","article","section","nav","header","footer"]),p=t.tagName.toLowerCase();return i.has(p)?!0:!new Set(["svg","script","style","link","meta","noscript","template"]).has(p)}function X(t){const i=D(t);return t.offsetWidth>0&&t.offsetHeight>0&&(i==null?void 0:i.visibility)!=="hidden"&&(i==null?void 0:i.display)!=="none"}function L(t){var C,M;if(!t||t.nodeType!==Node.ELEMENT_NODE||n.includes(t))return!1;if(s.includes(t))return!0;const i=t.tagName.toLowerCase(),p=D(t),c=new Set(["pointer","move","text","grab","grabbing","cell","copy","alias","all-scroll","col-resize","context-menu","crosshair","e-resize","ew-resize","help","n-resize","ne-resize","nesw-resize","ns-resize","nw-resize","nwse-resize","row-resize","s-resize","se-resize","sw-resize","vertical-text","w-resize","zoom-in","zoom-out"]),a=new Set(["not-allowed","no-drop","wait","progress","initial","inherit"]);function x(B){return B.tagName.toLowerCase()==="html"?!1:!!(p!=null&&p.cursor&&c.has(p.cursor))}if(x(t))return!0;const I=new Set(["a","button","input","select","textarea","details","summary","label","option","optgroup","fieldset","legend"]),m=new Set(["disabled","readonly"]);if(I.has(i)){if(p!=null&&p.cursor&&a.has(p.cursor))return!1;for(const B of m)if(t.hasAttribute(B)||t.getAttribute(B)==="true"||t.getAttribute(B)==="")return!1;return!(t.disabled||t.readOnly||t.inert)}const A=t.getAttribute("role"),$=t.getAttribute("aria-role");if(t.getAttribute("contenteditable")==="true"||t.isContentEditable||t.classList&&(t.classList.contains("button")||t.classList.contains("dropdown-toggle")||t.getAttribute("data-index")||t.getAttribute("data-toggle")==="dropdown"||t.getAttribute("aria-haspopup")==="true"))return!0;const g=new Set(["button","menu","menubar","menuitem","menuitemradio","menuitemcheckbox","radio","checkbox","tab","switch","slider","spinbutton","combobox","searchbox","textbox","listbox","option","scrollbar"]);if(I.has(i)||A&&g.has(A)||$&&g.has($))return!0;try{if(typeof getEventListeners=="function"){const q=getEventListeners(t),nt=["click","mousedown","mouseup","dblclick"];for(const j of nt)if(q[j]&&q[j].length>0)return!0}const B=((M=(C=t==null?void 0:t.ownerDocument)==null?void 0:C.defaultView)==null?void 0:M.getEventListenersForNode)||window.getEventListenersForNode;if(typeof B=="function"){const q=B(t),nt=["click","mousedown","mouseup","keydown","keyup","submit","change","input","focus","blur"];for(const j of nt)for(const S of q)if(S.type===j)return!0}const G=["onclick","onmousedown","onmouseup","ondblclick"];for(const q of G)if(t.hasAttribute(q)||typeof t[q]=="function")return!0}catch{}return!!V(t)}function et(t){if(l===-1)return!0;const i=z(t);if(!i||i.length===0)return!1;let p=!1;for(const m of i)if(m.width>0&&m.height>0&&!(m.bottom<-l||m.top>window.innerHeight+l||m.right<-l||m.left>window.innerWidth+l)){p=!0;break}if(!p)return!1;if(t.ownerDocument!==window.document)return!0;let a=Array.from(i).find(m=>m.width>0&&m.height>0);if(!a)return!1;const x=t.getRootNode();if(x instanceof ShadowRoot){const m=a.left+a.width/2,A=a.top+a.height/2;try{const $=x.elementFromPoint(m,A);if(!$)return!1;let g=$;for(;g&&g!==x;){if(g===t)return!0;g=g.parentElement}return!1}catch{return!0}}const E=5;return[{x:a.left+a.width/2,y:a.top+a.height/2},{x:a.left+E,y:a.top+E},{x:a.right-E,y:a.bottom-E}].some(({x:m,y:A})=>{try{const $=document.elementFromPoint(m,A);if(!$)return!1;let g=$;for(;g&&g!==document.documentElement;){if(g===t)return!0;g=g.parentElement}return!1}catch{return!0}})}function ot(t,i){if(i===-1)return!0;const p=t.getClientRects();if(!p||p.length===0){const c=N(t);return!c||c.width===0||c.height===0?!1:!(c.bottom<-i||c.top>window.innerHeight+i||c.right<-i||c.left>window.innerWidth+i)}for(const c of p)if(!(c.width===0||c.height===0)&&!(c.bottom<-i||c.top>window.innerHeight+i||c.right<-i||c.left>window.innerWidth+i))return!0;return!1}function F(t){if(!t||t.nodeType!==Node.ELEMENT_NODE)return!1;const i=t.tagName.toLowerCase();return new Set(["a","button","input","select","textarea","details","summary","label"]).has(i)?!0:t.hasAttribute("onclick")||t.hasAttribute("role")||t.hasAttribute("tabindex")||t.hasAttribute("aria-")||t.hasAttribute("data-action")||t.getAttribute("contenteditable")==="true"}const Y=new Set(["a","button","input","select","textarea","summary","details","label","option"]),K=new Set(["button","link","menuitem","menuitemradio","menuitemcheckbox","radio","checkbox","tab","switch","slider","spinbutton","combobox","searchbox","textbox","listbox","option","scrollbar"]);function tt(t){if(!t||t.nodeType!==Node.ELEMENT_NODE||!X(t))return!1;const i=t.hasAttribute("role")||t.hasAttribute("tabindex")||t.hasAttribute("onclick")||typeof t.onclick=="function",p=/\b(btn|clickable|menu|item|entry|link)\b/i.test(t.className||""),c=!!t.closest('button,a,[role="button"],.menu,.dropdown,.list,.toolbar'),a=[...t.children].some(X),x=t.parentElement&&t.parentElement.isSameNode(document.body);return(L(t)||i||p)&&a&&c&&!x}function rt(t){var c,a;if(!t||t.nodeType!==Node.ELEMENT_NODE)return!1;const i=t.tagName.toLowerCase(),p=t.getAttribute("role");if(i==="iframe"||Y.has(i)||p&&K.has(p)||t.isContentEditable||t.getAttribute("contenteditable")==="true"||t.hasAttribute("data-testid")||t.hasAttribute("data-cy")||t.hasAttribute("data-test")||t.hasAttribute("onclick")||typeof t.onclick=="function")return!0;try{const x=((a=(c=t==null?void 0:t.ownerDocument)==null?void 0:c.defaultView)==null?void 0:a.getEventListenersForNode)||window.getEventListenersForNode;if(typeof x=="function"){const I=x(t),m=["click","mousedown","mouseup","keydown","keyup","submit","change","input","focus","blur"];for(const A of m)for(const $ of I)if($.type===A)return!0}if(["onmousedown","onmouseup","onkeydown","onkeyup","onsubmit","onchange","oninput","onfocus","onblur"].some(I=>t.hasAttribute(I)))return!0}catch{}return!!tt(t)}function st(t,i,p,c){if(!t.isInteractive)return!1;let a=!1;return c?rt(i)?a=!0:a=!1:a=!0,a&&(t.isInViewport=ot(i,l),(t.isInViewport||l===-1)&&(t.highlightIndex=T++,d))?(r>=0?r===t.highlightIndex&&H(i,t.highlightIndex,p):H(i,t.highlightIndex,p),!0):!1}function Q(t,i=null,p=!1){var E,I,m,A,$;if(!t||t.id===U||t.nodeType!==Node.ELEMENT_NODE&&t.nodeType!==Node.TEXT_NODE||!t||t.id===U||((E=t.dataset)==null?void 0:E.browserUseIgnore)==="true"||t.getAttribute&&t.getAttribute("aria-hidden")==="true")return null;if(t===document.body){const g={tagName:"body",attributes:{},xpath:"/body",children:[]};for(const C of t.childNodes){const M=Q(C,i,!1);M&&g.children.push(M)}const y=`${R.current++}`;return _[y]=g,y}if(t.nodeType!==Node.ELEMENT_NODE&&t.nodeType!==Node.TEXT_NODE)return null;if(t.nodeType===Node.TEXT_NODE){const g=(I=t.textContent)==null?void 0:I.trim();if(!g)return null;const y=t.parentElement;if(!y||y.tagName.toLowerCase()==="script")return null;const C=`${R.current++}`;return _[C]={type:"TEXT_NODE",text:g,isVisible:O(t)},C}if(t.nodeType===Node.ELEMENT_NODE&&!it(t))return null;if(l!==-1&&!t.shadowRoot){const g=N(t),y=D(t),C=y&&(y.position==="fixed"||y.position==="sticky"),M=t.offsetWidth>0||t.offsetHeight>0;if(!g||!C&&!M&&(g.bottom<-l||g.top>window.innerHeight+l||g.right<-l||g.left>window.innerWidth+l))return null}const c={tagName:t.tagName.toLowerCase(),attributes:{},children:[]};if(F(t)||t.tagName.toLowerCase()==="iframe"||t.tagName.toLowerCase()==="body"){const g=((m=t.getAttributeNames)==null?void 0:m.call(t))||[];for(const y of g){const C=t.getAttribute(y);c.attributes[y]=C}t.tagName.toLowerCase()==="input"&&(t.type==="checkbox"||t.type==="radio")&&(c.attributes.checked=t.checked?"true":"false")}let a=!1;if(t.nodeType===Node.ELEMENT_NODE&&(c.isVisible=X(t),c.isVisible)){c.isTopElement=et(t);const g=t.getAttribute("role"),y=g==="menu"||g==="menubar"||g==="listbox";(c.isTopElement||y)&&(c.isInteractive=L(t),a=st(c,t,i,p),c.ref=t)}if(t.tagName){const g=t.tagName.toLowerCase();if(g==="iframe")try{const y=t.contentDocument||((A=t.contentWindow)==null?void 0:A.document);if(y)for(const C of y.childNodes){const M=Q(C,t,!1);M&&c.children.push(M)}}catch(y){console.warn("Unable to access iframe:",y)}else if(t.isContentEditable||t.getAttribute("contenteditable")==="true"||t.id==="tinymce"||t.classList.contains("mce-content-body")||g==="body"&&(($=t.getAttribute("data-id"))!=null&&$.startsWith("mce_")))for(const y of t.childNodes){const C=Q(y,i,a);C&&c.children.push(C)}else{if(t.shadowRoot){c.shadowRoot=!0;for(const y of t.shadowRoot.childNodes){const C=Q(y,i,a);C&&c.children.push(C)}}for(const y of t.childNodes){const M=Q(y,i,a||p);M&&c.children.push(M)}}}if(c.tagName==="a"&&c.children.length===0&&!c.attributes.href){const g=N(t);if(!(g&&g.width>0&&g.height>0||t.offsetWidth>0||t.offsetHeight>0))return null}c.extra=b.get(t)||null;const x=`${R.current++}`;return _[x]=c,x}const lt=Q(document.body);return f.clearCache(),{rootId:lt,map:_}},newElementsCache=new WeakMap;function getFlatTree(e){const n=[];for(const d of e.interactiveBlacklist||[])typeof d=="function"?n.push(d()):n.push(d);const s=[];for(const d of e.interactiveWhitelist||[])typeof d=="function"?s.push(d()):s.push(d);const o=domTree({doHighlightElements:!0,debugMode:!0,focusHighlightIndex:-1,viewportExpansion:VIEWPORT_EXPANSION,interactiveBlacklist:n,interactiveWhitelist:s,highlightOpacity:e.highlightOpacity??0,highlightLabelOpacity:e.highlightLabelOpacity??.1}),w=window.location.href;for(const d in o.map){const r=o.map[d];if(r.isInteractive&&r.ref){const l=r.ref;newElementsCache.has(l)||(newElementsCache.set(l,w),r.isNew=!0)}}return o}function flatTreeToString(e,n){const s=["title","type","checked","name","role","value","placeholder","data-date-format","alt","aria-label","aria-expanded","data-state","aria-checked","id","for","target","aria-haspopup","aria-controls","aria-owns"],o=[...n||[],...s],w=(u,f)=>u.length>f?u.substring(0,f)+"...":u,d=u=>{const f=e.map[u];if(!f)return null;if(f.type==="TEXT_NODE"){const N=f;return{type:"text",text:N.text,isVisible:N.isVisible,parent:null,children:[]}}else{const N=f,D=[];if(N.children)for(const z of N.children){const _=d(z);_&&(_.parent=null,D.push(_))}return{type:"element",tagName:N.tagName,attributes:N.attributes??{},isVisible:N.isVisible??!1,isInteractive:N.isInteractive??!1,isTopElement:N.isTopElement??!1,isNew:N.isNew??!1,highlightIndex:N.highlightIndex,parent:null,children:D,extra:N.extra??{}}}},r=(u,f=null)=>{u.parent=f;for(const N of u.children)r(N,u)},l=d(e.rootId);if(!l)return"";r(l);const h=u=>{let f=u.parent;for(;f;){if(f.type==="element"&&f.highlightIndex!==void 0)return!0;f=f.parent}return!1},T=(u,f,N)=>{var _,R,U,H;let D=f;const z=" ".repeat(f);if(u.type==="element"){if(u.highlightIndex!==void 0){D+=1;const V=getAllTextTillNextClickableElement(u);let O="";if(o.length>0&&u.attributes){const L={};for(const F of o){const Y=u.attributes[F];Y&&Y.trim()!==""&&(L[F]=Y.trim())}const et=o.filter(F=>F in L);if(et.length>1){const F=new Set,Y={};for(const K of et){const tt=L[K];tt.length>5&&(tt in Y?F.add(K):Y[tt]=K)}for(const K of F)delete L[K]}L.role===u.tagName&&delete L.role;const ot=["aria-label","placeholder","title"];for(const F of ot)L[F]&&L[F].toLowerCase().trim()===V.toLowerCase().trim()&&delete L[F];Object.keys(L).length>0&&(O=Object.entries(L).map(([F,Y])=>`${F}=${w(Y,20)}`).join(" "))}const it=u.isNew?`*[${u.highlightIndex}]`:`[${u.highlightIndex}]`;let X=`${z}${it}<${u.tagName??""}`;if(O&&(X+=` ${O}`),u.extra&&u.extra.scrollable){let L="";(_=u.extra.scrollData)!=null&&_.left&&(L+=`left=${u.extra.scrollData.left}, `),(R=u.extra.scrollData)!=null&&R.top&&(L+=`top=${u.extra.scrollData.top}, `),(U=u.extra.scrollData)!=null&&U.right&&(L+=`right=${u.extra.scrollData.right}, `),(H=u.extra.scrollData)!=null&&H.bottom&&(L+=`bottom=${u.extra.scrollData.bottom}`),X+=` data-scrollable="${L}"`}if(V){const L=V.trim();O||(X+=" "),X+=`>${L}`}else O||(X+=" ");X+=" />",N.push(X)}for(const V of u.children)T(V,D,N)}else if(u.type==="text"){if(h(u))return;u.parent&&u.parent.type==="element"&&u.parent.isVisible&&u.parent.isTopElement&&N.push(`${z}${u.text??""}`)}},b=[];return T(l,0,b),b.join(`
|
|
2
|
-
`)}const getAllTextTillNextClickableElement=(e,n=-1)=>{const s=[],o=(w,d)=>{if(!(n!==-1&&d>n)&&!(w.type==="element"&&w!==e&&w.highlightIndex!==void 0)){if(w.type==="text"&&w.text)s.push(w.text);else if(w.type==="element")for(const r of w.children)o(r,d+1)}};return o(e,0),s.join(`
|
|
3
|
-
`).trim()};function getSelectorMap(e){const n=new Map,s=Object.keys(e.map);for(const o of s){const w=e.map[o];w.isInteractive&&typeof w.highlightIndex=="number"&&n.set(w.highlightIndex,w)}return n}function getElementTextMap(e){const n=e.split(`
|
|
4
|
-
`).map(o=>o.trim()).filter(o=>o.length>0),s=new Map;for(const o of n){const d=/^\[(\d+)\]<[^>]+>([^<]*)/.exec(o);if(d){const r=parseInt(d[1],10);s.set(r,o)}}return s}function cleanUpHighlights(){const e=window._highlightCleanupFunctions||[];for(const n of e)typeof n=="function"&&n();window._highlightCleanupFunctions=[]}window.addEventListener("popstate",()=>{cleanUpHighlights()});window.addEventListener("hashchange",()=>{cleanUpHighlights()});window.addEventListener("beforeunload",()=>{cleanUpHighlights()});const navigation=window.navigation;if(navigation&&typeof navigation.addEventListener=="function")navigation.addEventListener("navigate",()=>{cleanUpHighlights()});else{let e=window.location.href;setInterval(()=>{window.location.href!==e&&(e=window.location.href,cleanUpHighlights())},500)}function getPageInfo(){const e=window.innerWidth,n=window.innerHeight,s=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth||0),o=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight||0),w=window.scrollX||window.pageXOffset||document.documentElement.scrollLeft||0,d=window.scrollY||window.pageYOffset||document.documentElement.scrollTop||0,r=Math.max(0,o-(window.innerHeight+d)),l=Math.max(0,s-(window.innerWidth+w));return{viewport_width:e,viewport_height:n,page_width:s,page_height:o,scroll_x:w,scroll_y:d,pixels_above:d,pixels_below:r,pages_above:n>0?d/n:0,pages_below:n>0?r/n:0,total_pages:n>0?o/n:0,current_page_position:d/Math.max(1,o-n),pixels_left:w,pixels_right:l}}function patchReact(e){const n=document.querySelectorAll('[data-reactroot], [data-reactid], [data-react-checksum], #root, #app, [id^="root-"], [id^="app-"], #adex-wrapper, #adex-root');for(const s of n)s.setAttribute("data-page-agent-not-interactive","true")}class PageController extends EventTarget{constructor(e={}){super(),this.flatTree=null,this.selectorMap=new Map,this.elementTextMap=new Map,this.simplifiedHTML="<EMPTY>",this.lastTimeUpdate=0,this.isIndexed=!1,this.mask=null,this.maskReady=null,this.config=e,patchReact(),e.enableMask&&this.initMask()}initMask(){this.maskReady===null&&(this.maskReady=(async()=>{const{SimulatorMask:e}=await Promise.resolve().then(()=>require("./SimulatorMask-jCkPs2Tt.cjs"));this.mask=new e})())}async getCurrentUrl(){return window.location.href}async getLastUpdateTime(){return this.lastTimeUpdate}async getBrowserState(){const e=window.location.href,n=document.title,s=getPageInfo(),o=this.config.viewportExpansion??VIEWPORT_EXPANSION;await this.updateTree();const w=this.simplifiedHTML,d=`Current Page: [${n}](${e})`,r=`Page info: ${s.viewport_width}x${s.viewport_height}px viewport, ${s.page_width}x${s.page_height}px total page size, ${s.pages_above.toFixed(1)} pages above, ${s.pages_below.toFixed(1)} pages below, ${s.total_pages.toFixed(1)} total pages, at ${(s.current_page_position*100).toFixed(0)}% of page`,l=o===-1?"Interactive elements from top layer of the current page (full page):":"Interactive elements from top layer of the current page inside the viewport:",T=s.pixels_above>4&&o!==-1?`... ${s.pixels_above} pixels above (${s.pages_above.toFixed(1)} pages) - scroll to see more ...`:"[Start of page]",b=`${d}
|
|
5
|
-
${r}
|
|
6
|
-
|
|
7
|
-
${l}
|
|
8
|
-
|
|
9
|
-
${T}`,f=s.pixels_below>4&&o!==-1?`... ${s.pixels_below} pixels below (${s.pages_below.toFixed(1)} pages) - scroll to see more ...`:"[End of page]";return{url:e,title:n,header:b,content:w,footer:f}}async updateTree(){this.dispatchEvent(new Event("beforeUpdate")),this.lastTimeUpdate=Date.now(),this.mask&&(this.mask.wrapper.style.pointerEvents="none"),cleanUpHighlights();const e=[...this.config.interactiveBlacklist||[],...document.querySelectorAll("[data-page-agent-not-interactive]").values()];return this.flatTree=getFlatTree({...this.config,interactiveBlacklist:e}),this.simplifiedHTML=flatTreeToString(this.flatTree,this.config.include_attributes),this.selectorMap.clear(),this.selectorMap=getSelectorMap(this.flatTree),this.elementTextMap.clear(),this.elementTextMap=getElementTextMap(this.simplifiedHTML),this.isIndexed=!0,this.mask&&(this.mask.wrapper.style.pointerEvents="auto"),this.dispatchEvent(new Event("afterUpdate")),this.simplifiedHTML}async cleanUpHighlights(){cleanUpHighlights()}assertIndexed(){if(!this.isIndexed)throw new Error("DOM tree not indexed yet. Can not perform actions on elements.")}async clickElement(e){try{this.assertIndexed();const n=getElementByIndex(this.selectorMap,e),s=this.elementTextMap.get(e);return await clickElement(n),n instanceof HTMLAnchorElement&&n.target==="_blank"?{success:!0,message:`✅ Clicked element (${s??e}). ⚠️ Link opens in a new tab. You are not capable of reading new tabs.`}:{success:!0,message:`✅ Clicked element (${s??e}).`}}catch(n){return{success:!1,message:`❌ Failed to click element: ${n}`}}}async inputText(e,n){try{this.assertIndexed();const s=getElementByIndex(this.selectorMap,e),o=this.elementTextMap.get(e);return await inputTextElement(s,n),{success:!0,message:`✅ Input text (${n}) into element (${o??e}).`}}catch(s){return{success:!1,message:`❌ Failed to input text: ${s}`}}}async selectOption(e,n){try{this.assertIndexed();const s=getElementByIndex(this.selectorMap,e),o=this.elementTextMap.get(e);return await selectOptionElement(s,n),{success:!0,message:`✅ Selected option (${n}) in element (${o??e}).`}}catch(s){return{success:!1,message:`❌ Failed to select option: ${s}`}}}async scroll(e){try{const{down:n,numPages:s,pixels:o,index:w}=e;this.assertIndexed();const d=o??s*(n?1:-1)*window.innerHeight,r=w!==void 0?getElementByIndex(this.selectorMap,w):null;return{success:!0,message:await scrollVertically(n,d,r)}}catch(n){return{success:!1,message:`❌ Failed to scroll: ${n}`}}}async scrollHorizontally(e){try{const{right:n,pixels:s,index:o}=e;this.assertIndexed();const w=s*(n?1:-1),d=o!==void 0?getElementByIndex(this.selectorMap,o):null;return{success:!0,message:await scrollHorizontally(n,w,d)}}catch(n){return{success:!1,message:`❌ Failed to scroll horizontally: ${n}`}}}async executeJavascript(script){try{const asyncFunction=eval(`(async () => { ${script} })`),result=await asyncFunction();return{success:!0,message:`✅ Executed JavaScript. Result: ${result}`}}catch(e){return{success:!1,message:`❌ Error executing JavaScript: ${e}`}}}async showMask(){var e;await this.maskReady,(e=this.mask)==null||e.show()}async hideMask(){var e;await this.maskReady,(e=this.mask)==null||e.hide()}dispose(){var e;cleanUpHighlights(),this.flatTree=null,this.selectorMap.clear(),this.elementTextMap.clear(),this.simplifiedHTML="<EMPTY>",this.isIndexed=!1,(e=this.mask)==null||e.dispose(),this.mask=null}}exports.PageController=PageController;
|