browserBackend.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 browserBackend_exports = {};
  20. __export(browserBackend_exports, {
  21. createCustomMessageHandler: () => createCustomMessageHandler
  22. });
  23. module.exports = __toCommonJS(browserBackend_exports);
  24. var import_config = require("../browser/config");
  25. var import_browserServerBackend = require("../browser/browserServerBackend");
  26. var import_tab = require("../browser/tab");
  27. var import_util = require("../../util");
  28. function createCustomMessageHandler(testInfo, context) {
  29. let backend;
  30. return async (data) => {
  31. if (data.initialize) {
  32. if (backend)
  33. throw new Error("MCP backend is already initialized");
  34. backend = new import_browserServerBackend.BrowserServerBackend({ ...import_config.defaultConfig, capabilities: ["testing"] }, identityFactory(context));
  35. await backend.initialize(data.initialize.clientInfo);
  36. const pausedMessage = await generatePausedMessage(testInfo, context);
  37. return { initialize: { pausedMessage } };
  38. }
  39. if (data.listTools) {
  40. if (!backend)
  41. throw new Error("MCP backend is not initialized");
  42. return { listTools: await backend.listTools() };
  43. }
  44. if (data.callTool) {
  45. if (!backend)
  46. throw new Error("MCP backend is not initialized");
  47. return { callTool: await backend.callTool(data.callTool.name, data.callTool.arguments) };
  48. }
  49. if (data.close) {
  50. backend?.serverClosed();
  51. backend = void 0;
  52. return { close: {} };
  53. }
  54. throw new Error("Unknown MCP request");
  55. };
  56. }
  57. async function generatePausedMessage(testInfo, context) {
  58. const lines = [];
  59. if (testInfo.errors.length) {
  60. lines.push(`### Paused on error:`);
  61. for (const error of testInfo.errors)
  62. lines.push((0, import_util.stripAnsiEscapes)(error.message || ""));
  63. } else {
  64. lines.push(`### Paused at end of test. ready for interaction`);
  65. }
  66. for (let i = 0; i < context.pages().length; i++) {
  67. const page = context.pages()[i];
  68. const stateSuffix = context.pages().length > 1 ? i + 1 + " of " + context.pages().length : "state";
  69. lines.push(
  70. "",
  71. `### Page ${stateSuffix}`,
  72. `- Page URL: ${page.url()}`,
  73. `- Page Title: ${await page.title()}`.trim()
  74. );
  75. let console = testInfo.errors.length ? await import_tab.Tab.collectConsoleMessages(page) : [];
  76. console = console.filter((msg) => !msg.type || msg.type === "error");
  77. if (console.length) {
  78. lines.push("- Console Messages:");
  79. for (const message of console)
  80. lines.push(` - ${message.toString()}`);
  81. }
  82. lines.push(
  83. `- Page Snapshot:`,
  84. "```yaml",
  85. (await page._snapshotForAI()).full,
  86. "```"
  87. );
  88. }
  89. lines.push("");
  90. if (testInfo.errors.length)
  91. lines.push(`### Task`, `Try recovering from the error prior to continuing`);
  92. return lines.join("\n");
  93. }
  94. function identityFactory(browserContext) {
  95. return {
  96. createContext: async (clientInfo, abortSignal, toolName) => {
  97. return {
  98. browserContext,
  99. close: async () => {
  100. }
  101. };
  102. }
  103. };
  104. }
  105. // Annotate the CommonJS export names for ESM import in node:
  106. 0 && (module.exports = {
  107. createCustomMessageHandler
  108. });