playwright-core 1.58.0-alpha-2025-12-06 → 1.58.0-alpha-2025-12-08

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/browsers.json CHANGED
@@ -45,7 +45,7 @@
45
45
  },
46
46
  {
47
47
  "name": "webkit",
48
- "revision": "2232",
48
+ "revision": "2235",
49
49
  "installByDefault": true,
50
50
  "revisionOverrides": {
51
51
  "debian11-x64": "2105",
@@ -62,6 +62,7 @@ class Page extends import_channelOwner.ChannelOwner {
62
62
  this._closeWasCalled = false;
63
63
  this._harRouters = [];
64
64
  this._locatorHandlers = /* @__PURE__ */ new Map();
65
+ this._instrumentation.onPage(this);
65
66
  this._browserContext = parent;
66
67
  this._timeoutSettings = new import_timeoutSettings.TimeoutSettings(this._platform, this._browserContext._timeoutSettings);
67
68
  this.keyboard = new import_input.Keyboard(this);
@@ -673,6 +674,13 @@ class Page extends import_channelOwner.ChannelOwner {
673
674
  }
674
675
  return result.pdf;
675
676
  }
677
+ async perform(task, options = {}) {
678
+ await this._channel.perform({ task, ...options });
679
+ }
680
+ async extract(query, schema, options = {}) {
681
+ const { result } = await this._channel.extract({ query, schema: this._platform.zodToJsonSchema(schema), ...options });
682
+ return result;
683
+ }
676
684
  async _snapshotForAI(options = {}) {
677
685
  return await this._channel.snapshotForAI({ timeout: this._timeoutSettings.timeout(options), track: options.track });
678
686
  }
@@ -66,6 +66,9 @@ const emptyPlatform = {
66
66
  streamWritable: (channel) => {
67
67
  throw new Error("Streams are not available");
68
68
  },
69
+ zodToJsonSchema: (schema) => {
70
+ throw new Error("Zod is not available");
71
+ },
69
72
  zones: { empty: noopZone, current: () => noopZone }
70
73
  };
71
74
  // Annotate the CommonJS export names for ESM import in node:
@@ -608,6 +608,12 @@ import_validatorPrimitives.scheme.BrowserTypeLaunchPersistentContextParams = (0,
608
608
  serviceWorkers: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["allow", "block"])),
609
609
  selectorEngines: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)((0, import_validatorPrimitives.tType)("SelectorEngine"))),
610
610
  testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
611
+ agent: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
612
+ provider: import_validatorPrimitives.tString,
613
+ model: import_validatorPrimitives.tString,
614
+ cacheFile: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
615
+ cacheMode: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["ignore", "force", "auto"]))
616
+ })),
611
617
  userDataDir: import_validatorPrimitives.tString,
612
618
  slowMo: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tFloat)
613
619
  });
@@ -700,6 +706,12 @@ import_validatorPrimitives.scheme.BrowserNewContextParams = (0, import_validator
700
706
  serviceWorkers: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["allow", "block"])),
701
707
  selectorEngines: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)((0, import_validatorPrimitives.tType)("SelectorEngine"))),
702
708
  testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
709
+ agent: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
710
+ provider: import_validatorPrimitives.tString,
711
+ model: import_validatorPrimitives.tString,
712
+ cacheFile: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
713
+ cacheMode: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["ignore", "force", "auto"]))
714
+ })),
703
715
  proxy: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
704
716
  server: import_validatorPrimitives.tString,
705
717
  bypass: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
@@ -771,6 +783,12 @@ import_validatorPrimitives.scheme.BrowserNewContextForReuseParams = (0, import_v
771
783
  serviceWorkers: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["allow", "block"])),
772
784
  selectorEngines: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)((0, import_validatorPrimitives.tType)("SelectorEngine"))),
773
785
  testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
786
+ agent: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
787
+ provider: import_validatorPrimitives.tString,
788
+ model: import_validatorPrimitives.tString,
789
+ cacheFile: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
790
+ cacheMode: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["ignore", "force", "auto"]))
791
+ })),
774
792
  proxy: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
775
793
  server: import_validatorPrimitives.tString,
776
794
  bypass: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
@@ -886,7 +904,13 @@ import_validatorPrimitives.scheme.BrowserContextInitializer = (0, import_validat
886
904
  strictSelectors: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tBoolean),
887
905
  serviceWorkers: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["allow", "block"])),
888
906
  selectorEngines: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)((0, import_validatorPrimitives.tType)("SelectorEngine"))),
889
- testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString)
907
+ testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
908
+ agent: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
909
+ provider: import_validatorPrimitives.tString,
910
+ model: import_validatorPrimitives.tString,
911
+ cacheFile: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
912
+ cacheMode: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["ignore", "force", "auto"]))
913
+ }))
890
914
  })
891
915
  });
892
916
  import_validatorPrimitives.scheme.BrowserContextBindingCallEvent = (0, import_validatorPrimitives.tObject)({
@@ -1488,6 +1512,20 @@ import_validatorPrimitives.scheme.PageUpdateSubscriptionParams = (0, import_vali
1488
1512
  enabled: import_validatorPrimitives.tBoolean
1489
1513
  });
1490
1514
  import_validatorPrimitives.scheme.PageUpdateSubscriptionResult = (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({}));
1515
+ import_validatorPrimitives.scheme.PagePerformParams = (0, import_validatorPrimitives.tObject)({
1516
+ task: import_validatorPrimitives.tString,
1517
+ key: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
1518
+ maxTurns: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tInt)
1519
+ });
1520
+ import_validatorPrimitives.scheme.PagePerformResult = (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({}));
1521
+ import_validatorPrimitives.scheme.PageExtractParams = (0, import_validatorPrimitives.tObject)({
1522
+ query: import_validatorPrimitives.tString,
1523
+ schema: import_validatorPrimitives.tAny,
1524
+ maxTurns: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tInt)
1525
+ });
1526
+ import_validatorPrimitives.scheme.PageExtractResult = (0, import_validatorPrimitives.tObject)({
1527
+ result: import_validatorPrimitives.tAny
1528
+ });
1491
1529
  import_validatorPrimitives.scheme.FrameInitializer = (0, import_validatorPrimitives.tObject)({
1492
1530
  url: import_validatorPrimitives.tString,
1493
1531
  name: import_validatorPrimitives.tString,
@@ -2776,6 +2814,12 @@ import_validatorPrimitives.scheme.AndroidDeviceLaunchBrowserParams = (0, import_
2776
2814
  serviceWorkers: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["allow", "block"])),
2777
2815
  selectorEngines: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)((0, import_validatorPrimitives.tType)("SelectorEngine"))),
2778
2816
  testIdAttributeName: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
2817
+ agent: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
2818
+ provider: import_validatorPrimitives.tString,
2819
+ model: import_validatorPrimitives.tString,
2820
+ cacheFile: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
2821
+ cacheMode: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tEnum)(["ignore", "force", "auto"]))
2822
+ })),
2779
2823
  pkg: (0, import_validatorPrimitives.tOptional)(import_validatorPrimitives.tString),
2780
2824
  args: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tArray)(import_validatorPrimitives.tString)),
2781
2825
  proxy: (0, import_validatorPrimitives.tOptional)((0, import_validatorPrimitives.tObject)({
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var actionRunner_exports = {};
20
+ __export(actionRunner_exports, {
21
+ runAction: () => runAction
22
+ });
23
+ module.exports = __toCommonJS(actionRunner_exports);
24
+ async function runAction(progress, page, action) {
25
+ const frame = page.mainFrame();
26
+ switch (action.method) {
27
+ case "click":
28
+ await frame.click(progress, action.selector, { ...action.options, ...strictTrue });
29
+ break;
30
+ case "drag":
31
+ await frame.dragAndDrop(progress, action.sourceSelector, action.targetSelector, { ...strictTrue });
32
+ break;
33
+ case "hover":
34
+ await frame.hover(progress, action.selector, { ...action.options, ...strictTrue });
35
+ break;
36
+ case "selectOption":
37
+ await frame.selectOption(progress, action.selector, [], action.values.map((a) => ({ value: a })), { ...strictTrue });
38
+ break;
39
+ case "pressKey":
40
+ await page.keyboard.press(progress, action.key);
41
+ break;
42
+ case "pressSequentially":
43
+ await frame.type(progress, action.selector, action.text, { ...strictTrue });
44
+ if (action.submit)
45
+ await page.keyboard.press(progress, "Enter");
46
+ break;
47
+ case "fill":
48
+ await frame.fill(progress, action.selector, action.text, { ...strictTrue });
49
+ if (action.submit)
50
+ await page.keyboard.press(progress, "Enter");
51
+ break;
52
+ }
53
+ }
54
+ const strictTrue = { strict: true };
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ runAction
58
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var actions_exports = {};
16
+ module.exports = __toCommonJS(actions_exports);
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var agent_exports = {};
30
+ __export(agent_exports, {
31
+ pageExtract: () => pageExtract,
32
+ pagePerform: () => pagePerform
33
+ });
34
+ module.exports = __toCommonJS(agent_exports);
35
+ var import_fs = __toESM(require("fs"));
36
+ var import_backend = require("./backend");
37
+ var import_utilsBundle = require("../../utilsBundle");
38
+ var import_mcpBundle = require("../../mcpBundle");
39
+ var import_actionRunner = require("./actionRunner");
40
+ var import_context = require("./context");
41
+ async function pagePerform(progress, page, options) {
42
+ const context = new import_context.Context(progress, page);
43
+ if (await cachedPerform(context, options))
44
+ return;
45
+ await perform(context, options.task, (0, import_mcpBundle.zodToJsonSchema)(import_mcpBundle.z.object({
46
+ error: import_mcpBundle.z.string().optional().describe("An error message if the task could not be completed successfully")
47
+ })), options);
48
+ await updateCache(context, options);
49
+ }
50
+ async function pageExtract(progress, page, options) {
51
+ const context = new import_context.Context(progress, page);
52
+ const task = `
53
+ ### Instructions
54
+ Extract the following information from the page. Do not perform any actions, just extract the information.
55
+
56
+ ### Query
57
+ ${options.query}`;
58
+ return await perform(context, task, options.schema, options);
59
+ }
60
+ async function perform(context, userTask, resultSchema, options = {}) {
61
+ const { progress, page } = context;
62
+ const browserContext = page.browserContext;
63
+ if (!browserContext._options.agent)
64
+ throw new Error(`page.perform() and page.extract() require the agent to be set on the browser context`);
65
+ const { full } = await page.snapshotForAI(progress);
66
+ const { tools, callTool } = (0, import_backend.toolsForLoop)(context);
67
+ const loop = new import_mcpBundle.Loop(browserContext._options.agent.provider, {
68
+ model: browserContext._options.agent.model,
69
+ summarize: true,
70
+ debug: import_utilsBundle.debug,
71
+ callTool,
72
+ tools,
73
+ ...options
74
+ });
75
+ const task = `${userTask}
76
+
77
+ ### Page snapshot
78
+ ${full}
79
+ `;
80
+ return await loop.run(task, {
81
+ resultSchema
82
+ });
83
+ }
84
+ const allCaches = /* @__PURE__ */ new Map();
85
+ async function cachedPerform(context, options) {
86
+ const agentSettings = context.page.browserContext._options.agent;
87
+ if (!agentSettings?.cacheFile || agentSettings.cacheMode === "ignore")
88
+ return false;
89
+ const cache = await cachedActions(agentSettings.cacheFile);
90
+ const cacheKey = options.key ?? options.task;
91
+ const actions = cache[cacheKey];
92
+ if (!actions) {
93
+ if (agentSettings.cacheMode === "force")
94
+ throw new Error(`No cached actions for key "${cacheKey}", but cache mode is set to "force"`);
95
+ return false;
96
+ }
97
+ for (const action of actions)
98
+ await (0, import_actionRunner.runAction)(context.progress, context.page, action);
99
+ return true;
100
+ }
101
+ async function updateCache(context, options) {
102
+ const cacheFile = context.page.browserContext._options.agent?.cacheFile;
103
+ if (!cacheFile)
104
+ return;
105
+ const cache = await cachedActions(cacheFile);
106
+ const cacheKey = options.key ?? options.task;
107
+ cache[cacheKey] = context.actions;
108
+ await import_fs.default.promises.writeFile(cacheFile, JSON.stringify(cache, void 0, 2));
109
+ }
110
+ async function cachedActions(cacheFile) {
111
+ let cache = allCaches.get(cacheFile);
112
+ if (!cache) {
113
+ const text = await import_fs.default.promises.readFile(cacheFile, "utf-8").catch(() => "{}");
114
+ cache = JSON.parse(text);
115
+ allCaches.set(cacheFile, cache);
116
+ }
117
+ return cache;
118
+ }
119
+ // Annotate the CommonJS export names for ESM import in node:
120
+ 0 && (module.exports = {
121
+ pageExtract,
122
+ pagePerform
123
+ });
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var backend_exports = {};
30
+ __export(backend_exports, {
31
+ toolsForLoop: () => toolsForLoop
32
+ });
33
+ module.exports = __toCommonJS(backend_exports);
34
+ var import_tools = __toESM(require("./tools"));
35
+ var import_progress = require("../progress");
36
+ var import_mcpBundle = require("../../mcpBundle");
37
+ function toolsForLoop(context) {
38
+ const tools = import_tools.default.map((tool) => {
39
+ const result = {
40
+ name: tool.schema.name,
41
+ description: tool.schema.description,
42
+ inputSchema: (0, import_mcpBundle.zodToJsonSchema)(tool.schema.inputSchema)
43
+ };
44
+ return result;
45
+ });
46
+ const callTool = async (params) => {
47
+ const tool = import_tools.default.find((t) => t.schema.name === params.name);
48
+ if (!tool) {
49
+ return {
50
+ content: [{
51
+ type: "text",
52
+ text: `Tool ${params.name} not found. Available tools: ${import_tools.default.map((t) => t.schema.name)}`
53
+ }],
54
+ isError: true
55
+ };
56
+ }
57
+ const progressController = new import_progress.ProgressController();
58
+ try {
59
+ return await progressController.run(async (progress) => {
60
+ return await tool.handle(context, params.arguments);
61
+ });
62
+ } catch (error) {
63
+ return {
64
+ content: [{ type: "text", text: error.message }],
65
+ isError: true
66
+ };
67
+ }
68
+ };
69
+ return {
70
+ tools,
71
+ callTool
72
+ };
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ toolsForLoop
77
+ });
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var context_exports = {};
20
+ __export(context_exports, {
21
+ Context: () => Context
22
+ });
23
+ module.exports = __toCommonJS(context_exports);
24
+ var import_browserContext = require("../browserContext");
25
+ var import_actionRunner = require("./actionRunner");
26
+ class Context {
27
+ constructor(progress, page) {
28
+ this.actions = [];
29
+ this.progress = progress;
30
+ this.page = page;
31
+ }
32
+ async runAction(action) {
33
+ await this.waitForCompletion(() => (0, import_actionRunner.runAction)(this.progress, this.page, action));
34
+ this.actions.push(action);
35
+ return await this.snapshotResult();
36
+ }
37
+ async waitForCompletion(callback) {
38
+ const requests = [];
39
+ const requestListener = (request) => requests.push(request);
40
+ const disposeListeners = () => {
41
+ this.page.browserContext.off(import_browserContext.BrowserContext.Events.Request, requestListener);
42
+ };
43
+ let result;
44
+ try {
45
+ result = await callback();
46
+ await this.progress.wait(500);
47
+ } finally {
48
+ disposeListeners();
49
+ }
50
+ const requestedNavigation = requests.some((request) => request.isNavigationRequest());
51
+ if (requestedNavigation) {
52
+ await this.page.performActionPreChecks(this.progress);
53
+ return result;
54
+ }
55
+ const fiveSeconds = new Promise((resolve) => setTimeout(resolve, 1e3));
56
+ for (const request of requests) {
57
+ if (request.failure())
58
+ continue;
59
+ const response = Promise.race([request.response(), fiveSeconds]);
60
+ await this.progress.race(response);
61
+ }
62
+ return result;
63
+ }
64
+ async snapshotResult() {
65
+ const { full } = await this.page.snapshotForAI(this.progress);
66
+ const text = [`# Page snapshot
67
+ ${full}`];
68
+ return {
69
+ _meta: {
70
+ "dev.lowire/state": {
71
+ "Page snapshot": full
72
+ }
73
+ },
74
+ content: [{ type: "text", text: text.join("\n\n") }]
75
+ };
76
+ }
77
+ async refSelectors(params) {
78
+ return Promise.all(params.map(async (param) => {
79
+ try {
80
+ const { resolvedSelector } = await this.page.mainFrame().resolveSelector(this.progress, `aria-ref=${param.ref}`);
81
+ return resolvedSelector;
82
+ } catch (e) {
83
+ throw new Error(`Ref ${param.ref} not found in the current page snapshot. Try capturing new snapshot.`);
84
+ }
85
+ }));
86
+ }
87
+ }
88
+ // Annotate the CommonJS export names for ESM import in node:
89
+ 0 && (module.exports = {
90
+ Context
91
+ });