patchright-core 1.57.0 → 1.58.2

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.
Files changed (148) hide show
  1. package/ThirdPartyNotices.txt +3223 -308
  2. package/browsers.json +21 -22
  3. package/lib/cli/program.js +4 -5
  4. package/lib/client/api.js +3 -0
  5. package/lib/client/browser.js +3 -5
  6. package/lib/client/browserContext.js +40 -4
  7. package/lib/client/browserType.js +4 -3
  8. package/lib/client/connection.js +4 -0
  9. package/lib/client/elementHandle.js +3 -0
  10. package/lib/client/events.js +3 -0
  11. package/lib/client/fetch.js +3 -4
  12. package/lib/client/frame.js +10 -1
  13. package/lib/client/locator.js +8 -0
  14. package/lib/client/network.js +5 -1
  15. package/lib/client/page.js +29 -1
  16. package/lib/client/pageAgent.js +64 -0
  17. package/lib/client/platform.js +3 -0
  18. package/lib/client/tracing.js +1 -1
  19. package/lib/generated/injectedScriptSource.js +1 -1
  20. package/lib/generated/pollingRecorderSource.js +1 -1
  21. package/lib/mcpBundle.js +84 -0
  22. package/lib/mcpBundleImpl/index.js +147 -0
  23. package/lib/protocol/serializers.js +5 -0
  24. package/lib/protocol/validator.js +88 -4
  25. package/lib/remote/playwrightServer.js +1 -2
  26. package/lib/server/agent/actionRunner.js +335 -0
  27. package/lib/server/agent/actions.js +128 -0
  28. package/lib/server/agent/codegen.js +111 -0
  29. package/lib/server/agent/context.js +150 -0
  30. package/lib/server/agent/expectTools.js +156 -0
  31. package/lib/server/agent/pageAgent.js +204 -0
  32. package/lib/server/agent/performTools.js +262 -0
  33. package/lib/server/agent/tool.js +109 -0
  34. package/lib/server/artifact.js +1 -1
  35. package/lib/server/bidi/bidiBrowser.js +56 -12
  36. package/lib/server/bidi/bidiChromium.js +8 -12
  37. package/lib/server/bidi/bidiConnection.js +1 -0
  38. package/lib/server/bidi/bidiDeserializer.js +116 -0
  39. package/lib/server/bidi/bidiExecutionContext.js +75 -29
  40. package/lib/server/bidi/bidiFirefox.js +6 -8
  41. package/lib/server/bidi/bidiNetworkManager.js +1 -1
  42. package/lib/server/bidi/bidiPage.js +39 -28
  43. package/lib/server/bidi/third_party/bidiProtocolCore.js +1 -0
  44. package/lib/server/browserContext.js +34 -26
  45. package/lib/server/browserType.js +12 -4
  46. package/lib/server/chromium/chromium.js +14 -20
  47. package/lib/server/chromium/chromiumSwitches.js +2 -2
  48. package/lib/server/chromium/crBrowser.js +22 -12
  49. package/lib/server/chromium/crConnection.js +0 -5
  50. package/lib/server/chromium/crCoverage.js +13 -1
  51. package/lib/server/chromium/crDevTools.js +0 -2
  52. package/lib/server/chromium/crNetworkManager.js +92 -12
  53. package/lib/server/chromium/crPage.js +62 -116
  54. package/lib/server/codegen/javascript.js +6 -29
  55. package/lib/server/deviceDescriptorsSource.json +56 -56
  56. package/lib/server/dispatchers/browserContextDispatcher.js +3 -2
  57. package/lib/server/dispatchers/dispatcher.js +6 -13
  58. package/lib/server/dispatchers/frameDispatcher.js +1 -1
  59. package/lib/server/dispatchers/jsHandleDispatcher.js +2 -2
  60. package/lib/server/dispatchers/pageAgentDispatcher.js +96 -0
  61. package/lib/server/dispatchers/pageDispatcher.js +4 -0
  62. package/lib/server/dom.js +12 -3
  63. package/lib/server/electron/electron.js +5 -2
  64. package/lib/server/firefox/ffBrowser.js +10 -20
  65. package/lib/server/firefox/ffConnection.js +0 -5
  66. package/lib/server/firefox/ffNetworkManager.js +2 -2
  67. package/lib/server/firefox/ffPage.js +15 -18
  68. package/lib/server/firefox/firefox.js +6 -8
  69. package/lib/server/frameSelectors.js +16 -4
  70. package/lib/server/frames.js +251 -86
  71. package/lib/server/instrumentation.js +3 -0
  72. package/lib/server/javascript.js +8 -4
  73. package/lib/server/launchApp.js +2 -1
  74. package/lib/server/network.js +50 -12
  75. package/lib/server/page.js +61 -91
  76. package/lib/server/progress.js +26 -6
  77. package/lib/server/recorder/recorderApp.js +79 -100
  78. package/lib/server/registry/browserFetcher.js +6 -4
  79. package/lib/server/registry/index.js +172 -149
  80. package/lib/server/registry/oopDownloadBrowserMain.js +3 -0
  81. package/lib/server/screencast.js +190 -0
  82. package/lib/server/screenshotter.js +6 -0
  83. package/lib/server/trace/recorder/snapshotter.js +17 -8
  84. package/lib/server/trace/recorder/snapshotterInjected.js +30 -72
  85. package/lib/server/trace/recorder/tracing.js +29 -21
  86. package/lib/server/trace/viewer/traceParser.js +72 -0
  87. package/lib/server/trace/viewer/traceViewer.js +21 -17
  88. package/lib/server/utils/expectUtils.js +87 -2
  89. package/lib/server/utils/hostPlatform.js +15 -0
  90. package/lib/server/utils/httpServer.js +5 -20
  91. package/lib/server/utils/network.js +37 -28
  92. package/lib/server/utils/nodePlatform.js +6 -0
  93. package/lib/server/{chromium/videoRecorder.js → videoRecorder.js} +22 -13
  94. package/lib/server/webkit/webkit.js +4 -6
  95. package/lib/server/webkit/wkBrowser.js +2 -6
  96. package/lib/server/webkit/wkConnection.js +1 -6
  97. package/lib/server/webkit/wkInterceptableRequest.js +29 -1
  98. package/lib/server/webkit/wkPage.js +75 -46
  99. package/lib/utils/isomorphic/ariaSnapshot.js +60 -2
  100. package/lib/utils/isomorphic/lruCache.js +51 -0
  101. package/lib/utils/isomorphic/protocolMetainfo.js +9 -1
  102. package/lib/utils/isomorphic/stringUtils.js +49 -0
  103. package/lib/utils/isomorphic/trace/entries.js +16 -0
  104. package/lib/utils/isomorphic/trace/snapshotRenderer.js +499 -0
  105. package/lib/utils/isomorphic/trace/snapshotServer.js +120 -0
  106. package/lib/utils/isomorphic/trace/snapshotStorage.js +89 -0
  107. package/lib/utils/isomorphic/trace/traceLoader.js +131 -0
  108. package/lib/utils/isomorphic/trace/traceModel.js +365 -0
  109. package/lib/utils/isomorphic/trace/traceModernizer.js +400 -0
  110. package/lib/utils/isomorphic/trace/versions/traceV3.js +16 -0
  111. package/lib/utils/isomorphic/trace/versions/traceV4.js +16 -0
  112. package/lib/utils/isomorphic/trace/versions/traceV5.js +16 -0
  113. package/lib/utils/isomorphic/trace/versions/traceV6.js +16 -0
  114. package/lib/utils/isomorphic/trace/versions/traceV7.js +16 -0
  115. package/lib/utils/isomorphic/trace/versions/traceV8.js +16 -0
  116. package/lib/utils/isomorphic/yaml.js +84 -0
  117. package/lib/utils.js +2 -0
  118. package/lib/utilsBundle.js +2 -5
  119. package/lib/utilsBundleImpl/index.js +165 -165
  120. package/lib/vite/htmlReport/index.html +21 -21
  121. package/lib/vite/recorder/assets/codeMirrorModule-CFUTFUO7.js +32 -0
  122. package/lib/vite/{traceViewer/codeMirrorModule.C3UTv-Ge.css → recorder/assets/codeMirrorModule-DYBRYzYX.css} +1 -1
  123. package/lib/vite/recorder/assets/{index-Ri0uHF7I.css → index-BSjZa4pk.css} +1 -1
  124. package/lib/vite/recorder/assets/index-CVkBxsGf.js +193 -0
  125. package/lib/vite/recorder/index.html +2 -2
  126. package/lib/vite/traceViewer/assets/codeMirrorModule-BVA4h_ZY.js +32 -0
  127. package/lib/vite/traceViewer/assets/defaultSettingsView-CjfmcdOz.js +266 -0
  128. package/lib/vite/{recorder/assets/codeMirrorModule-C3UTv-Ge.css → traceViewer/codeMirrorModule.DYBRYzYX.css} +1 -1
  129. package/lib/vite/traceViewer/defaultSettingsView.7ch9cixO.css +1 -0
  130. package/lib/vite/traceViewer/index.BVu7tZDe.css +1 -0
  131. package/lib/vite/traceViewer/index.BtyWtaE-.js +2 -0
  132. package/lib/vite/traceViewer/index.html +4 -4
  133. package/lib/vite/traceViewer/sw.bundle.js +5 -3
  134. package/lib/vite/traceViewer/uiMode.fyrXARf2.js +5 -0
  135. package/lib/vite/traceViewer/uiMode.html +3 -3
  136. package/package.json +2 -1
  137. package/types/protocol.d.ts +738 -159
  138. package/types/types.d.ts +25 -38
  139. package/lib/server/bidi/third_party/bidiDeserializer.js +0 -98
  140. package/lib/server/trace/test/inMemorySnapshotter.js +0 -87
  141. package/lib/vite/recorder/assets/codeMirrorModule-CBbSe-ZI.js +0 -25
  142. package/lib/vite/recorder/assets/index-CpZVd2nA.js +0 -193
  143. package/lib/vite/traceViewer/assets/codeMirrorModule-DHz0wP2C.js +0 -25
  144. package/lib/vite/traceViewer/assets/defaultSettingsView-WsZP88O6.js +0 -266
  145. package/lib/vite/traceViewer/defaultSettingsView.ConWv5KN.css +0 -1
  146. package/lib/vite/traceViewer/index.C4Y3Aw8n.css +0 -1
  147. package/lib/vite/traceViewer/index.C8xAeo93.js +0 -2
  148. package/lib/vite/traceViewer/uiMode.BltraIJB.js +0 -5
@@ -0,0 +1,204 @@
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 pageAgent_exports = {};
30
+ __export(pageAgent_exports, {
31
+ pageAgentExpect: () => pageAgentExpect,
32
+ pageAgentExtract: () => pageAgentExtract,
33
+ pageAgentPerform: () => pageAgentPerform
34
+ });
35
+ module.exports = __toCommonJS(pageAgent_exports);
36
+ var import_fs = __toESM(require("fs"));
37
+ var import_path = __toESM(require("path"));
38
+ var import_tool = require("./tool");
39
+ var import_utilsBundle = require("../../utilsBundle");
40
+ var import_mcpBundle = require("../../mcpBundle");
41
+ var import_actionRunner = require("./actionRunner");
42
+ var import_performTools = __toESM(require("./performTools"));
43
+ var import_expectTools = __toESM(require("./expectTools"));
44
+ var actions = __toESM(require("./actions"));
45
+ async function pageAgentPerform(progress, context, userTask, callParams) {
46
+ const cacheKey = (callParams.cacheKey ?? userTask).trim();
47
+ if (await cachedPerform(progress, context, cacheKey))
48
+ return;
49
+ const task = `
50
+ ### Instructions
51
+ - Perform the following task on the page.
52
+ - Your reply should be a tool call that performs action the page".
53
+
54
+ ### Task
55
+ ${userTask}
56
+ `;
57
+ progress.disableTimeout();
58
+ await runLoop(progress, context, import_performTools.default, task, void 0, callParams);
59
+ await updateCache(context, cacheKey);
60
+ }
61
+ async function pageAgentExpect(progress, context, expectation, callParams) {
62
+ const cacheKey = (callParams.cacheKey ?? expectation).trim();
63
+ if (await cachedPerform(progress, context, cacheKey))
64
+ return;
65
+ const task = `
66
+ ### Instructions
67
+ - Call one of the "browser_expect_*" tools to verify / assert the condition.
68
+ - You can call exactly one tool and it can't be report_results, must be one of the assertion tools.
69
+
70
+ ### Expectation
71
+ ${expectation}
72
+ `;
73
+ progress.disableTimeout();
74
+ await runLoop(progress, context, import_expectTools.default, task, void 0, callParams);
75
+ await updateCache(context, cacheKey);
76
+ }
77
+ async function pageAgentExtract(progress, context, query, schema, callParams) {
78
+ const task = `
79
+ ### Instructions
80
+ Extract the following information from the page. Do not perform any actions, just extract the information.
81
+
82
+ ### Query
83
+ ${query}`;
84
+ const { result } = await runLoop(progress, context, [], task, schema, callParams);
85
+ return result;
86
+ }
87
+ async function runLoop(progress, context, toolDefinitions, userTask, resultSchema, params) {
88
+ if (!context.agentParams.api || !context.agentParams.model)
89
+ throw new Error(`This action requires the API and API key to be set on the page agent. Did you mean to --run-agents=missing?`);
90
+ if (!context.agentParams.apiKey)
91
+ throw new Error(`This action requires API key to be set on the page agent.`);
92
+ if (context.agentParams.apiEndpoint && !URL.canParse(context.agentParams.apiEndpoint))
93
+ throw new Error(`Agent API endpoint "${context.agentParams.apiEndpoint}" is not a valid URL.`);
94
+ const snapshot = await context.takeSnapshot(progress);
95
+ const { tools, callTool, reportedResult, refusedToPerformReason } = (0, import_tool.toolsForLoop)(progress, context, toolDefinitions, { resultSchema, refuseToPerform: "allow" });
96
+ const secrets = Object.fromEntries((context.agentParams.secrets || [])?.map((s) => [s.name, s.value]));
97
+ const apiCacheTextBefore = context.agentParams.apiCacheFile ? await import_fs.default.promises.readFile(context.agentParams.apiCacheFile, "utf-8").catch(() => "{}") : "{}";
98
+ const apiCacheBefore = JSON.parse(apiCacheTextBefore || "{}");
99
+ const loop = new import_mcpBundle.Loop({
100
+ api: context.agentParams.api,
101
+ apiEndpoint: context.agentParams.apiEndpoint,
102
+ apiKey: context.agentParams.apiKey,
103
+ apiTimeout: context.agentParams.apiTimeout ?? 0,
104
+ model: context.agentParams.model,
105
+ maxTokens: params.maxTokens ?? context.maxTokensRemaining(),
106
+ maxToolCalls: params.maxActions ?? context.agentParams.maxActions ?? 10,
107
+ maxToolCallRetries: params.maxActionRetries ?? context.agentParams.maxActionRetries ?? 3,
108
+ summarize: true,
109
+ debug: import_utilsBundle.debug,
110
+ callTool,
111
+ tools,
112
+ secrets,
113
+ cache: apiCacheBefore,
114
+ ...context.events
115
+ });
116
+ const task = [];
117
+ if (context.agentParams.systemPrompt) {
118
+ task.push("### System");
119
+ task.push(context.agentParams.systemPrompt);
120
+ task.push("");
121
+ }
122
+ task.push("### Task");
123
+ task.push(userTask);
124
+ if (context.history().length) {
125
+ task.push("### Context history");
126
+ task.push(context.history().map((h) => `- ${h.type}: ${h.description}`).join("\n"));
127
+ task.push("");
128
+ }
129
+ task.push("### Page snapshot");
130
+ task.push(snapshot);
131
+ task.push("");
132
+ const { error, usage } = await loop.run(task.join("\n"), { signal: progress.signal });
133
+ context.consumeTokens(usage.input + usage.output);
134
+ if (context.agentParams.apiCacheFile) {
135
+ const apiCacheAfter = { ...apiCacheBefore, ...loop.cache() };
136
+ const sortedCache = Object.fromEntries(Object.entries(apiCacheAfter).sort(([a], [b]) => a.localeCompare(b)));
137
+ const apiCacheTextAfter = JSON.stringify(sortedCache, void 0, 2);
138
+ if (apiCacheTextAfter !== apiCacheTextBefore) {
139
+ await import_fs.default.promises.mkdir(import_path.default.dirname(context.agentParams.apiCacheFile), { recursive: true });
140
+ await import_fs.default.promises.writeFile(context.agentParams.apiCacheFile, apiCacheTextAfter);
141
+ }
142
+ }
143
+ if (refusedToPerformReason())
144
+ throw new Error(`Agent refused to perform action: ${refusedToPerformReason()}`);
145
+ if (error)
146
+ throw new Error(`Agentic loop failed: ${error}`);
147
+ return { result: reportedResult ? reportedResult() : void 0 };
148
+ }
149
+ async function cachedPerform(progress, context, cacheKey) {
150
+ if (!context.agentParams?.cacheFile)
151
+ return;
152
+ const cache = await cachedActions(context.agentParams?.cacheFile);
153
+ const entry = cache.actions[cacheKey];
154
+ if (!entry)
155
+ return;
156
+ for (const action of entry.actions)
157
+ await (0, import_actionRunner.runAction)(progress, "run", context.page, action, context.agentParams.secrets ?? []);
158
+ return entry.actions;
159
+ }
160
+ async function updateCache(context, cacheKey) {
161
+ const cacheFile = context.agentParams?.cacheFile;
162
+ const cacheOutFile = context.agentParams?.cacheOutFile;
163
+ const cacheFileKey = cacheFile ?? cacheOutFile;
164
+ const cache = cacheFileKey ? await cachedActions(cacheFileKey) : { actions: {}, newActions: {} };
165
+ const newEntry = { actions: context.actions() };
166
+ cache.actions[cacheKey] = newEntry;
167
+ cache.newActions[cacheKey] = newEntry;
168
+ if (cacheOutFile) {
169
+ const entries = Object.entries(cache.newActions);
170
+ entries.sort((e1, e2) => e1[0].localeCompare(e2[0]));
171
+ await import_fs.default.promises.writeFile(cacheOutFile, JSON.stringify(Object.fromEntries(entries), void 0, 2));
172
+ } else if (cacheFile) {
173
+ const entries = Object.entries(cache.actions);
174
+ entries.sort((e1, e2) => e1[0].localeCompare(e2[0]));
175
+ await import_fs.default.promises.writeFile(cacheFile, JSON.stringify(Object.fromEntries(entries), void 0, 2));
176
+ }
177
+ }
178
+ const allCaches = /* @__PURE__ */ new Map();
179
+ async function cachedActions(cacheFile) {
180
+ let cache = allCaches.get(cacheFile);
181
+ if (!cache) {
182
+ const content = await import_fs.default.promises.readFile(cacheFile, "utf-8").catch(() => "");
183
+ let json;
184
+ try {
185
+ json = JSON.parse(content.trim() || "{}");
186
+ } catch (error) {
187
+ throw new Error(`Failed to parse cache file ${cacheFile}:
188
+ ${error.message}`);
189
+ }
190
+ const parsed = actions.cachedActionsSchema.safeParse(json);
191
+ if (parsed.error)
192
+ throw new Error(`Failed to parse cache file ${cacheFile}:
193
+ ${import_mcpBundle.z.prettifyError(parsed.error)}`);
194
+ cache = { actions: parsed.data, newActions: {} };
195
+ allCaches.set(cacheFile, cache);
196
+ }
197
+ return cache;
198
+ }
199
+ // Annotate the CommonJS export names for ESM import in node:
200
+ 0 && (module.exports = {
201
+ pageAgentExpect,
202
+ pageAgentExtract,
203
+ pageAgentPerform
204
+ });
@@ -0,0 +1,262 @@
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 performTools_exports = {};
20
+ __export(performTools_exports, {
21
+ default: () => performTools_default
22
+ });
23
+ module.exports = __toCommonJS(performTools_exports);
24
+ var import_mcpBundle = require("../../mcpBundle");
25
+ var import_tool = require("./tool");
26
+ const navigateSchema = import_mcpBundle.z.object({
27
+ url: import_mcpBundle.z.string().describe("URL to navigate to")
28
+ });
29
+ const navigate = (0, import_tool.defineTool)({
30
+ schema: {
31
+ name: "browser_navigate",
32
+ title: "Navigate to URL",
33
+ description: "Navigate to a URL",
34
+ inputSchema: navigateSchema
35
+ },
36
+ handle: async (progress, context, params) => {
37
+ return await context.runActionNoWait(progress, {
38
+ method: "navigate",
39
+ url: params.url
40
+ });
41
+ }
42
+ });
43
+ const snapshot = (0, import_tool.defineTool)({
44
+ schema: {
45
+ name: "browser_snapshot",
46
+ title: "Page snapshot",
47
+ description: "Capture accessibility snapshot of the current page, this is better than screenshot",
48
+ inputSchema: import_mcpBundle.z.object({})
49
+ },
50
+ handle: async (progress, context, params) => {
51
+ return await context.snapshotResult(progress);
52
+ }
53
+ });
54
+ const elementSchema = import_mcpBundle.z.object({
55
+ element: import_mcpBundle.z.string().describe("Human-readable element description used to obtain permission to interact with the element"),
56
+ ref: import_mcpBundle.z.string().describe("Exact target element reference from the page snapshot")
57
+ });
58
+ const clickSchema = elementSchema.extend({
59
+ doubleClick: import_mcpBundle.z.boolean().optional().describe("Whether to perform a double click instead of a single click"),
60
+ button: import_mcpBundle.z.enum(["left", "right", "middle"]).optional().describe("Button to click, defaults to left"),
61
+ modifiers: import_mcpBundle.z.array(import_mcpBundle.z.enum(["Alt", "Control", "ControlOrMeta", "Meta", "Shift"])).optional().describe("Modifier keys to press")
62
+ });
63
+ const click = (0, import_tool.defineTool)({
64
+ schema: {
65
+ name: "browser_click",
66
+ title: "Click",
67
+ description: "Perform click on a web page",
68
+ inputSchema: clickSchema
69
+ },
70
+ handle: async (progress, context, params) => {
71
+ const [selector] = await context.refSelectors(progress, [params]);
72
+ return await context.runActionAndWait(progress, {
73
+ method: "click",
74
+ selector,
75
+ button: params.button,
76
+ modifiers: params.modifiers,
77
+ clickCount: params.doubleClick ? 2 : void 0
78
+ });
79
+ }
80
+ });
81
+ const drag = (0, import_tool.defineTool)({
82
+ schema: {
83
+ name: "browser_drag",
84
+ title: "Drag mouse",
85
+ description: "Perform drag and drop between two elements",
86
+ inputSchema: import_mcpBundle.z.object({
87
+ startElement: import_mcpBundle.z.string().describe("Human-readable source element description used to obtain the permission to interact with the element"),
88
+ startRef: import_mcpBundle.z.string().describe("Exact source element reference from the page snapshot"),
89
+ endElement: import_mcpBundle.z.string().describe("Human-readable target element description used to obtain the permission to interact with the element"),
90
+ endRef: import_mcpBundle.z.string().describe("Exact target element reference from the page snapshot")
91
+ })
92
+ },
93
+ handle: async (progress, context, params) => {
94
+ const [sourceSelector, targetSelector] = await context.refSelectors(progress, [
95
+ { ref: params.startRef, element: params.startElement },
96
+ { ref: params.endRef, element: params.endElement }
97
+ ]);
98
+ return await context.runActionAndWait(progress, {
99
+ method: "drag",
100
+ sourceSelector,
101
+ targetSelector
102
+ });
103
+ }
104
+ });
105
+ const hoverSchema = elementSchema.extend({
106
+ modifiers: import_mcpBundle.z.array(import_mcpBundle.z.enum(["Alt", "Control", "ControlOrMeta", "Meta", "Shift"])).optional().describe("Modifier keys to press")
107
+ });
108
+ const hover = (0, import_tool.defineTool)({
109
+ schema: {
110
+ name: "browser_hover",
111
+ title: "Hover mouse",
112
+ description: "Hover over element on page",
113
+ inputSchema: hoverSchema
114
+ },
115
+ handle: async (progress, context, params) => {
116
+ const [selector] = await context.refSelectors(progress, [params]);
117
+ return await context.runActionAndWait(progress, {
118
+ method: "hover",
119
+ selector,
120
+ modifiers: params.modifiers
121
+ });
122
+ }
123
+ });
124
+ const selectOptionSchema = elementSchema.extend({
125
+ values: import_mcpBundle.z.array(import_mcpBundle.z.string()).describe("Array of values to select in the dropdown. This can be a single value or multiple values.")
126
+ });
127
+ const selectOption = (0, import_tool.defineTool)({
128
+ schema: {
129
+ name: "browser_select_option",
130
+ title: "Select option",
131
+ description: "Select an option in a dropdown",
132
+ inputSchema: selectOptionSchema
133
+ },
134
+ handle: async (progress, context, params) => {
135
+ const [selector] = await context.refSelectors(progress, [params]);
136
+ return await context.runActionAndWait(progress, {
137
+ method: "selectOption",
138
+ selector,
139
+ labels: params.values
140
+ });
141
+ }
142
+ });
143
+ const pressKey = (0, import_tool.defineTool)({
144
+ schema: {
145
+ name: "browser_press_key",
146
+ title: "Press a key",
147
+ description: "Press a key on the keyboard",
148
+ inputSchema: import_mcpBundle.z.object({
149
+ key: import_mcpBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`"),
150
+ modifiers: import_mcpBundle.z.array(import_mcpBundle.z.enum(["Alt", "Control", "ControlOrMeta", "Meta", "Shift"])).optional().describe("Modifier keys to press")
151
+ })
152
+ },
153
+ handle: async (progress, context, params) => {
154
+ return await context.runActionAndWait(progress, {
155
+ method: "pressKey",
156
+ key: params.modifiers ? [...params.modifiers, params.key].join("+") : params.key
157
+ });
158
+ }
159
+ });
160
+ const typeSchema = elementSchema.extend({
161
+ text: import_mcpBundle.z.string().describe("Text to type into the element"),
162
+ submit: import_mcpBundle.z.boolean().optional().describe("Whether to submit entered text (press Enter after)"),
163
+ slowly: import_mcpBundle.z.boolean().optional().describe("Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.")
164
+ });
165
+ const type = (0, import_tool.defineTool)({
166
+ schema: {
167
+ name: "browser_type",
168
+ title: "Type text",
169
+ description: "Type text into editable element",
170
+ inputSchema: typeSchema
171
+ },
172
+ handle: async (progress, context, params) => {
173
+ const [selector] = await context.refSelectors(progress, [params]);
174
+ if (params.slowly) {
175
+ return await context.runActionAndWait(progress, {
176
+ method: "pressSequentially",
177
+ selector,
178
+ text: params.text,
179
+ submit: params.submit
180
+ });
181
+ } else {
182
+ return await context.runActionAndWait(progress, {
183
+ method: "fill",
184
+ selector,
185
+ text: params.text,
186
+ submit: params.submit
187
+ });
188
+ }
189
+ }
190
+ });
191
+ const fillForm = (0, import_tool.defineTool)({
192
+ schema: {
193
+ name: "browser_fill_form",
194
+ title: "Fill form",
195
+ description: "Fill multiple form fields. Always use this tool when you can fill more than one field at a time.",
196
+ inputSchema: import_mcpBundle.z.object({
197
+ fields: import_mcpBundle.z.array(import_mcpBundle.z.object({
198
+ name: import_mcpBundle.z.string().describe("Human-readable field name"),
199
+ type: import_mcpBundle.z.enum(["textbox", "checkbox", "radio", "combobox", "slider"]).describe("Type of the field"),
200
+ ref: import_mcpBundle.z.string().describe("Exact target field reference from the page snapshot"),
201
+ value: import_mcpBundle.z.string().describe("Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.")
202
+ })).describe("Fields to fill in")
203
+ })
204
+ },
205
+ handle: async (progress, context, params) => {
206
+ const actions = [];
207
+ for (const field of params.fields) {
208
+ const [selector] = await context.refSelectors(progress, [{ ref: field.ref, element: field.name }]);
209
+ if (field.type === "textbox" || field.type === "slider") {
210
+ actions.push({
211
+ method: "fill",
212
+ selector,
213
+ text: field.value
214
+ });
215
+ } else if (field.type === "checkbox" || field.type === "radio") {
216
+ actions.push({
217
+ method: "setChecked",
218
+ selector,
219
+ checked: field.value === "true"
220
+ });
221
+ } else if (field.type === "combobox") {
222
+ actions.push({
223
+ method: "selectOption",
224
+ selector,
225
+ labels: [field.value]
226
+ });
227
+ }
228
+ }
229
+ return await context.runActionsAndWait(progress, actions);
230
+ }
231
+ });
232
+ const setCheckedSchema = elementSchema.extend({
233
+ checked: import_mcpBundle.z.boolean().describe("Whether to check the checkbox")
234
+ });
235
+ const setChecked = (0, import_tool.defineTool)({
236
+ schema: {
237
+ name: "browser_set_checked",
238
+ title: "Set checked",
239
+ description: "Set the checked state of a checkbox",
240
+ inputSchema: setCheckedSchema
241
+ },
242
+ handle: async (progress, context, params) => {
243
+ const [selector] = await context.refSelectors(progress, [params]);
244
+ return await context.runActionAndWait(progress, {
245
+ method: "setChecked",
246
+ selector,
247
+ checked: params.checked
248
+ });
249
+ }
250
+ });
251
+ var performTools_default = [
252
+ navigate,
253
+ snapshot,
254
+ click,
255
+ drag,
256
+ hover,
257
+ selectOption,
258
+ pressKey,
259
+ type,
260
+ fillForm,
261
+ setChecked
262
+ ];
@@ -0,0 +1,109 @@
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 tool_exports = {};
20
+ __export(tool_exports, {
21
+ defineTool: () => defineTool,
22
+ toolsForLoop: () => toolsForLoop
23
+ });
24
+ module.exports = __toCommonJS(tool_exports);
25
+ var import_mcpBundle = require("../../mcpBundle");
26
+ var import_stringUtils = require("../../utils/isomorphic/stringUtils");
27
+ function defineTool(tool) {
28
+ return tool;
29
+ }
30
+ function toolsForLoop(progress, context, toolDefinitions, options = {}) {
31
+ const tools = toolDefinitions.map((tool) => {
32
+ const result = {
33
+ name: tool.schema.name,
34
+ description: tool.schema.description,
35
+ inputSchema: import_mcpBundle.z.toJSONSchema(tool.schema.inputSchema)
36
+ };
37
+ return result;
38
+ });
39
+ if (options.resultSchema) {
40
+ tools.push({
41
+ name: "report_result",
42
+ description: "Report the result of the task.",
43
+ inputSchema: options.resultSchema
44
+ });
45
+ }
46
+ if (options.refuseToPerform === "allow") {
47
+ tools.push({
48
+ name: "refuse_to_perform",
49
+ description: "Refuse to perform action.",
50
+ inputSchema: {
51
+ type: "object",
52
+ properties: {
53
+ reason: {
54
+ type: "string",
55
+ description: `Call this when you believe that you can't perform the action because something is wrong with the page. The reason will be reported to the user.`
56
+ }
57
+ },
58
+ required: ["reason"]
59
+ }
60
+ });
61
+ }
62
+ let reportedResult;
63
+ let refusedToPerformReason;
64
+ const callTool = async (params) => {
65
+ if (params.name === "report_result") {
66
+ reportedResult = params.arguments;
67
+ return {
68
+ content: [{ type: "text", text: "Done" }],
69
+ isError: false
70
+ };
71
+ }
72
+ if (params.name === "refuse_to_perform") {
73
+ refusedToPerformReason = params.arguments.reason;
74
+ return {
75
+ content: [{ type: "text", text: "Done" }],
76
+ isError: false
77
+ };
78
+ }
79
+ const tool = toolDefinitions.find((t) => t.schema.name === params.name);
80
+ if (!tool) {
81
+ return {
82
+ content: [{
83
+ type: "text",
84
+ text: `Tool ${params.name} not found. Available tools: ${toolDefinitions.map((t) => t.schema.name)}`
85
+ }],
86
+ isError: true
87
+ };
88
+ }
89
+ try {
90
+ return await tool.handle(progress, context, params.arguments);
91
+ } catch (error) {
92
+ return {
93
+ content: [{ type: "text", text: (0, import_stringUtils.stripAnsiEscapes)(error.message) }],
94
+ isError: true
95
+ };
96
+ }
97
+ };
98
+ return {
99
+ tools,
100
+ callTool,
101
+ reportedResult: options.resultSchema ? () => reportedResult : void 0,
102
+ refusedToPerformReason: () => refusedToPerformReason
103
+ };
104
+ }
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ defineTool,
108
+ toolsForLoop
109
+ });
@@ -103,7 +103,7 @@ class Artifact extends import_instrumentation.SdkObject {
103
103
  if (!this._unaccessibleErrorMessage)
104
104
  await import_fs.default.promises.unlink(this._localPath).catch((e) => {
105
105
  });
106
- await this.reportFinished(new import_errors.TargetClosedError());
106
+ await this.reportFinished(new import_errors.TargetClosedError(this.closeReason()));
107
107
  }
108
108
  async reportFinished(error) {
109
109
  if (this._finished)