|
f7eb47b…
|
lmata
|
1 |
"use strict"; |
|
f7eb47b…
|
lmata
|
2 |
var __create = Object.create; |
|
f7eb47b…
|
lmata
|
3 |
var __defProp = Object.defineProperty; |
|
f7eb47b…
|
lmata
|
4 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
|
f7eb47b…
|
lmata
|
5 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
|
f7eb47b…
|
lmata
|
6 |
var __getProtoOf = Object.getPrototypeOf; |
|
f7eb47b…
|
lmata
|
7 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
|
f7eb47b…
|
lmata
|
8 |
var __export = (target, all) => { |
|
f7eb47b…
|
lmata
|
9 |
for (var name in all) |
|
f7eb47b…
|
lmata
|
10 |
__defProp(target, name, { get: all[name], enumerable: true }); |
|
f7eb47b…
|
lmata
|
11 |
}; |
|
f7eb47b…
|
lmata
|
12 |
var __copyProps = (to, from, except, desc) => { |
|
f7eb47b…
|
lmata
|
13 |
if (from && typeof from === "object" || typeof from === "function") { |
|
f7eb47b…
|
lmata
|
14 |
for (let key of __getOwnPropNames(from)) |
|
f7eb47b…
|
lmata
|
15 |
if (!__hasOwnProp.call(to, key) && key !== except) |
|
f7eb47b…
|
lmata
|
16 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
|
f7eb47b…
|
lmata
|
17 |
} |
|
f7eb47b…
|
lmata
|
18 |
return to; |
|
f7eb47b…
|
lmata
|
19 |
}; |
|
f7eb47b…
|
lmata
|
20 |
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
|
f7eb47b…
|
lmata
|
21 |
// If the importer is in node compatibility mode or this is not an ESM |
|
f7eb47b…
|
lmata
|
22 |
// file that has been converted to a CommonJS file using a Babel- |
|
f7eb47b…
|
lmata
|
23 |
// compatible transform (i.e. "__esModule" has not been set), then set |
|
f7eb47b…
|
lmata
|
24 |
// "default" to the CommonJS "module.exports" for node compatibility. |
|
f7eb47b…
|
lmata
|
25 |
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
|
f7eb47b…
|
lmata
|
26 |
mod |
|
f7eb47b…
|
lmata
|
27 |
)); |
|
f7eb47b…
|
lmata
|
28 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
|
f7eb47b…
|
lmata
|
29 |
var daemon_exports = {}; |
|
f7eb47b…
|
lmata
|
30 |
__export(daemon_exports, { |
|
f7eb47b…
|
lmata
|
31 |
startMcpDaemonServer: () => startMcpDaemonServer |
|
f7eb47b…
|
lmata
|
32 |
}); |
|
f7eb47b…
|
lmata
|
33 |
module.exports = __toCommonJS(daemon_exports); |
|
f7eb47b…
|
lmata
|
34 |
var import_promises = __toESM(require("fs/promises")); |
|
f7eb47b…
|
lmata
|
35 |
var import_net = __toESM(require("net")); |
|
f7eb47b…
|
lmata
|
36 |
var import_os = __toESM(require("os")); |
|
f7eb47b…
|
lmata
|
37 |
var import_path = __toESM(require("path")); |
|
f7eb47b…
|
lmata
|
38 |
var import_url = __toESM(require("url")); |
|
f7eb47b…
|
lmata
|
39 |
var import_utilsBundle = require("playwright-core/lib/utilsBundle"); |
|
f7eb47b…
|
lmata
|
40 |
var import_socketConnection = require("./socketConnection"); |
|
f7eb47b…
|
lmata
|
41 |
var import_commands = require("./commands"); |
|
f7eb47b…
|
lmata
|
42 |
var import_command = require("./command"); |
|
f7eb47b…
|
lmata
|
43 |
const daemonDebug = (0, import_utilsBundle.debug)("pw:daemon"); |
|
f7eb47b…
|
lmata
|
44 |
async function socketExists(socketPath) { |
|
f7eb47b…
|
lmata
|
45 |
try { |
|
f7eb47b…
|
lmata
|
46 |
const stat = await import_promises.default.stat(socketPath); |
|
f7eb47b…
|
lmata
|
47 |
if (stat?.isSocket()) |
|
f7eb47b…
|
lmata
|
48 |
return true; |
|
f7eb47b…
|
lmata
|
49 |
} catch (e) { |
|
f7eb47b…
|
lmata
|
50 |
} |
|
f7eb47b…
|
lmata
|
51 |
return false; |
|
f7eb47b…
|
lmata
|
52 |
} |
|
f7eb47b…
|
lmata
|
53 |
async function startMcpDaemonServer(socketPath, serverBackendFactory) { |
|
f7eb47b…
|
lmata
|
54 |
if (import_os.default.platform() !== "win32" && await socketExists(socketPath)) { |
|
f7eb47b…
|
lmata
|
55 |
daemonDebug(`Socket already exists, removing: ${socketPath}`); |
|
f7eb47b…
|
lmata
|
56 |
try { |
|
f7eb47b…
|
lmata
|
57 |
await import_promises.default.unlink(socketPath); |
|
f7eb47b…
|
lmata
|
58 |
} catch (error) { |
|
f7eb47b…
|
lmata
|
59 |
daemonDebug(`Failed to remove existing socket: ${error}`); |
|
f7eb47b…
|
lmata
|
60 |
throw error; |
|
f7eb47b…
|
lmata
|
61 |
} |
|
f7eb47b…
|
lmata
|
62 |
} |
|
f7eb47b…
|
lmata
|
63 |
const backend = serverBackendFactory.create(); |
|
f7eb47b…
|
lmata
|
64 |
const cwd = import_url.default.pathToFileURL(process.cwd()).href; |
|
f7eb47b…
|
lmata
|
65 |
await backend.initialize?.({ |
|
f7eb47b…
|
lmata
|
66 |
name: "playwright-cli", |
|
f7eb47b…
|
lmata
|
67 |
version: "1.0.0", |
|
f7eb47b…
|
lmata
|
68 |
roots: [{ |
|
f7eb47b…
|
lmata
|
69 |
uri: cwd, |
|
f7eb47b…
|
lmata
|
70 |
name: "cwd" |
|
f7eb47b…
|
lmata
|
71 |
}], |
|
f7eb47b…
|
lmata
|
72 |
timestamp: Date.now() |
|
f7eb47b…
|
lmata
|
73 |
}); |
|
f7eb47b…
|
lmata
|
74 |
await import_promises.default.mkdir(import_path.default.dirname(socketPath), { recursive: true }); |
|
f7eb47b…
|
lmata
|
75 |
const server = import_net.default.createServer((socket) => { |
|
f7eb47b…
|
lmata
|
76 |
daemonDebug("new client connection"); |
|
f7eb47b…
|
lmata
|
77 |
const connection = new import_socketConnection.SocketConnection(socket); |
|
f7eb47b…
|
lmata
|
78 |
connection.onclose = () => { |
|
f7eb47b…
|
lmata
|
79 |
daemonDebug("client disconnected"); |
|
f7eb47b…
|
lmata
|
80 |
}; |
|
f7eb47b…
|
lmata
|
81 |
connection.onmessage = async (message) => { |
|
f7eb47b…
|
lmata
|
82 |
const { id, method, params } = message; |
|
f7eb47b…
|
lmata
|
83 |
try { |
|
f7eb47b…
|
lmata
|
84 |
daemonDebug("received command", method); |
|
f7eb47b…
|
lmata
|
85 |
if (method === "runCliCommand") { |
|
f7eb47b…
|
lmata
|
86 |
const { toolName, toolParams } = parseCliCommand(params.args); |
|
f7eb47b…
|
lmata
|
87 |
const response = await backend.callTool(toolName, toolParams, () => { |
|
f7eb47b…
|
lmata
|
88 |
}); |
|
f7eb47b…
|
lmata
|
89 |
await connection.send({ id, result: formatResult(response) }); |
|
f7eb47b…
|
lmata
|
90 |
} else { |
|
f7eb47b…
|
lmata
|
91 |
throw new Error(`Unknown method: ${method}`); |
|
f7eb47b…
|
lmata
|
92 |
} |
|
f7eb47b…
|
lmata
|
93 |
} catch (e) { |
|
f7eb47b…
|
lmata
|
94 |
daemonDebug("command failed", e); |
|
f7eb47b…
|
lmata
|
95 |
await connection.send({ id, error: e.message }); |
|
f7eb47b…
|
lmata
|
96 |
} |
|
f7eb47b…
|
lmata
|
97 |
}; |
|
f7eb47b…
|
lmata
|
98 |
}); |
|
f7eb47b…
|
lmata
|
99 |
return new Promise((resolve, reject) => { |
|
f7eb47b…
|
lmata
|
100 |
server.on("error", (error) => { |
|
f7eb47b…
|
lmata
|
101 |
daemonDebug(`server error: ${error.message}`); |
|
f7eb47b…
|
lmata
|
102 |
reject(error); |
|
f7eb47b…
|
lmata
|
103 |
}); |
|
f7eb47b…
|
lmata
|
104 |
server.listen(socketPath, () => { |
|
f7eb47b…
|
lmata
|
105 |
daemonDebug(`daemon server listening on ${socketPath}`); |
|
f7eb47b…
|
lmata
|
106 |
resolve(socketPath); |
|
f7eb47b…
|
lmata
|
107 |
}); |
|
f7eb47b…
|
lmata
|
108 |
}); |
|
f7eb47b…
|
lmata
|
109 |
} |
|
f7eb47b…
|
lmata
|
110 |
function formatResult(result) { |
|
f7eb47b…
|
lmata
|
111 |
const lines = []; |
|
f7eb47b…
|
lmata
|
112 |
for (const content of result.content) { |
|
f7eb47b…
|
lmata
|
113 |
if (content.type === "text") |
|
f7eb47b…
|
lmata
|
114 |
lines.push(content.text); |
|
f7eb47b…
|
lmata
|
115 |
else |
|
f7eb47b…
|
lmata
|
116 |
lines.push(`<${content.type} content>`); |
|
f7eb47b…
|
lmata
|
117 |
} |
|
f7eb47b…
|
lmata
|
118 |
return lines.join("\n"); |
|
f7eb47b…
|
lmata
|
119 |
} |
|
f7eb47b…
|
lmata
|
120 |
function parseCliCommand(args) { |
|
f7eb47b…
|
lmata
|
121 |
const command = import_commands.commands[args._[0]]; |
|
f7eb47b…
|
lmata
|
122 |
if (!command) |
|
f7eb47b…
|
lmata
|
123 |
throw new Error("Command is required"); |
|
f7eb47b…
|
lmata
|
124 |
return (0, import_command.parseCommand)(command, args); |
|
f7eb47b…
|
lmata
|
125 |
} |
|
f7eb47b…
|
lmata
|
126 |
// Annotate the CommonJS export names for ESM import in node: |
|
f7eb47b…
|
lmata
|
127 |
0 && (module.exports = { |
|
f7eb47b…
|
lmata
|
128 |
startMcpDaemonServer |
|
f7eb47b…
|
lmata
|
129 |
}); |