@wp-playground/mcp 3.1.61 → 3.1.62

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/client.js CHANGED
@@ -1,331 +1,359 @@
1
- import { c as b, t as S, a as E, s as p, b as _, f as w, g as v } from "./tool-executors-jX-K5l3h.js";
2
- import { logger as h } from "@php-wasm/logger";
3
- const N = 5e3;
4
- function J(n, o) {
5
- const i = crypto.randomUUID();
6
- let e = null, s = "", t = null, l = !1;
7
- function u(d) {
8
- const c = n.list(), f = JSON.stringify(c);
9
- f !== s && (s = f, d.send(JSON.stringify({ type: "register", tabId: i, sites: c })));
1
+ import { c as C, t as P, a as T, s as p, b as h, f as v, g as $ } from "./tool-executors-D1J1qKnB.js";
2
+ import { logger as S } from "@php-wasm/logger";
3
+ const I = 5e3;
4
+ function L(n, c) {
5
+ const d = crypto.randomUUID();
6
+ let t = null, r = "", u = null, o = !1;
7
+ function a(l) {
8
+ const s = n.list(), f = JSON.stringify(s);
9
+ f !== r && (r = f, l.send(JSON.stringify({ type: "register", tabId: d, sites: s })));
10
10
  }
11
- async function r() {
11
+ async function e() {
12
12
  try {
13
- const d = await fetch(
14
- `http://127.0.0.1:${o}/bridge-token`
13
+ const l = await fetch(
14
+ `http://127.0.0.1:${c}/bridge-token`
15
15
  );
16
- if (!d.ok) {
17
- a();
16
+ if (!l.ok) {
17
+ i();
18
18
  return;
19
19
  }
20
- const { token: c } = await d.json();
21
- e = new WebSocket(`ws://127.0.0.1:${o}?token=${c}`);
20
+ const { token: s } = await l.json();
21
+ t = new WebSocket(`ws://127.0.0.1:${c}?token=${s}`);
22
22
  } catch {
23
- a();
23
+ i();
24
24
  return;
25
25
  }
26
- e.addEventListener("open", () => {
27
- var d;
28
- s = "", u(e), (d = n.onConnect) == null || d.call(n);
29
- }), e.addEventListener("message", async (d) => {
30
- let c;
26
+ t.addEventListener("open", () => {
27
+ var l;
28
+ r = "", a(t), (l = n.onConnect) == null || l.call(n);
29
+ }), t.addEventListener("message", async (l) => {
30
+ let s;
31
31
  try {
32
- c = JSON.parse(d.data);
32
+ s = JSON.parse(l.data);
33
33
  } catch {
34
34
  return;
35
35
  }
36
- if (c.type !== "command")
36
+ if (s.type !== "command")
37
37
  return;
38
- const { id: f, method: P, args: $, siteSlug: x } = c;
38
+ const { id: f, method: b, args: y, siteSlug: _ } = s;
39
39
  try {
40
- const y = await W(
40
+ const m = await N(
41
41
  n,
42
- P,
43
- $ || [],
44
- x,
45
- o
42
+ b,
43
+ y || [],
44
+ _,
45
+ c
46
46
  );
47
- (e == null ? void 0 : e.readyState) === WebSocket.OPEN && e.send(JSON.stringify({ id: f, type: "response", value: y }));
48
- } catch (y) {
49
- const T = y instanceof Error ? y.message : String(y);
50
- (e == null ? void 0 : e.readyState) === WebSocket.OPEN && e.send(
47
+ (t == null ? void 0 : t.readyState) === WebSocket.OPEN && t.send(JSON.stringify({ id: f, type: "response", value: m }));
48
+ } catch (m) {
49
+ const E = m instanceof Error ? m.message : String(m);
50
+ (t == null ? void 0 : t.readyState) === WebSocket.OPEN && t.send(
51
51
  JSON.stringify({
52
52
  id: f,
53
53
  type: "response",
54
- error: T
54
+ error: E
55
55
  })
56
56
  );
57
57
  }
58
- }), e.addEventListener("close", () => {
59
- e = null, a();
60
- }), e.addEventListener("error", () => {
58
+ }), t.addEventListener("close", () => {
59
+ t = null, i();
60
+ }), t.addEventListener("error", () => {
61
61
  });
62
62
  }
63
- function a() {
64
- l || (t = setTimeout(r, N));
63
+ function i() {
64
+ o || (u = setTimeout(e, I));
65
65
  }
66
- return r(), {
66
+ return e(), {
67
67
  notifySitesChanged: () => {
68
- (e == null ? void 0 : e.readyState) === WebSocket.OPEN && u(e);
68
+ (t == null ? void 0 : t.readyState) === WebSocket.OPEN && a(t);
69
69
  },
70
70
  stop: () => {
71
- l = !0, t !== null && (clearTimeout(t), t = null), e && (e.close(), e = null);
71
+ o = !0, u !== null && (clearTimeout(u), u = null), t && (t.close(), t = null);
72
72
  }
73
73
  };
74
74
  }
75
- async function W(n, o, i, e, s) {
76
- if (o === "__open_site_in_new_tab") {
77
- const r = new URL(window.location.href);
78
- if (r.searchParams.set("mcp-port", String(s)), r.searchParams.set("site-slug", e), !window.open(r.toString(), "_blank"))
75
+ async function N(n, c, d, t, r) {
76
+ if (c === "__open_site_in_new_tab") {
77
+ const e = new URL(window.location.href);
78
+ if (e.searchParams.set("mcp-port", String(r)), e.searchParams.set("site-slug", t), !window.open(e.toString(), "_blank"))
79
79
  throw new Error(
80
80
  "Pop-up blocked by browser. The user must allow pop-ups for this site."
81
81
  );
82
82
  return !0;
83
83
  }
84
- if (o === "__rename_site") {
85
- const [r] = i;
86
- return await n.rename(r), !0;
84
+ if (c === "__rename_site") {
85
+ const [e] = d;
86
+ return await n.rename(e), !0;
87
87
  }
88
- if (o === "__save_site")
88
+ if (c === "__save_site")
89
89
  return await n.saveInBrowser();
90
- const t = n.getClient();
91
- if (!t)
92
- throw new Error(`No active client for site: ${e}`);
93
- const u = b(t)[o];
94
- if (typeof u != "function")
95
- throw new Error(`Unknown method: ${o}`);
96
- return await u(...i);
90
+ const u = n.getClient();
91
+ if (!u)
92
+ throw new Error(`No active client for site: ${t}`);
93
+ const a = C(u)[c];
94
+ if (typeof a != "function")
95
+ throw new Error(`Unknown method: ${c}`);
96
+ return await a(...d);
97
97
  }
98
- const g = v();
99
- let m = null;
100
- function k(n) {
101
- const i = n.list().find((e) => e.isActive);
102
- if (!i)
98
+ const g = $();
99
+ let w = null;
100
+ function W(n) {
101
+ const d = n.list().find((t) => t.isActive);
102
+ if (!d)
103
103
  throw new Error("No active Playground site");
104
- return i;
104
+ return d;
105
105
  }
106
- async function L(n) {
107
- const o = C();
108
- if (!o)
106
+ async function J(n) {
107
+ const c = x();
108
+ if (!c)
109
109
  return;
110
- m == null || m.abort(), m = new AbortController();
111
- const i = m, e = i.signal;
112
- function s() {
113
- const r = n.getClient();
114
- if (!r)
110
+ w == null || w.abort(), w = new AbortController();
111
+ const d = w, t = d.signal;
112
+ function r() {
113
+ const e = n.getClient();
114
+ if (!e)
115
115
  throw new Error("No client for active site");
116
- return r;
116
+ return e;
117
117
  }
118
- const t = Object.entries(S).map(
119
- ([r, a]) => ({
120
- name: r,
121
- description: a.description,
122
- inputSchema: _(a.params),
123
- annotations: a.annotations,
124
- execute: async (d) => {
118
+ const u = Object.entries(P).map(
119
+ ([e, i]) => ({
120
+ name: e,
121
+ description: i.description,
122
+ inputSchema: h(i.params),
123
+ annotations: i.annotations,
124
+ execute: async (l, s) => {
125
+ var f, b;
125
126
  try {
126
- const c = E[r];
127
- if (!c)
127
+ (f = s == null ? void 0 : s.signal) == null || f.throwIfAborted();
128
+ const y = T[e];
129
+ if (!y)
128
130
  return {
129
- error: `No executor for "${r}"`
131
+ error: `No executor for "${e}"`
130
132
  };
131
- const f = b(s());
132
- return await c(f, d);
133
- } catch (c) {
133
+ const _ = C(r()), m = await y(
134
+ _,
135
+ l,
136
+ s == null ? void 0 : s.signal
137
+ );
138
+ return (b = s == null ? void 0 : s.signal) == null || b.throwIfAborted(), m;
139
+ } catch (y) {
134
140
  return {
135
- error: `${a.errorPrefix}: ${p(c)}`
141
+ error: `${i.errorPrefix}: ${p(y)}`
136
142
  };
137
143
  }
138
144
  }
139
145
  })
140
146
  );
141
- t.push(...M(n));
142
- const l = await Promise.allSettled(
143
- t.map(async (r) => {
144
- await o.registerTool(r, { signal: e });
147
+ u.push(...A(n));
148
+ const o = await Promise.allSettled(
149
+ u.map(async (e) => {
150
+ await c.registerTool(e, { signal: t });
145
151
  })
146
- ), u = [];
147
- for (const [r, a] of l.entries())
148
- a.status === "rejected" && u.push({
149
- name: t[r].name,
150
- reason: a.reason
152
+ ), a = [];
153
+ for (const [e, i] of o.entries())
154
+ i.status === "rejected" && a.push({
155
+ name: u[e].name,
156
+ reason: i.reason
151
157
  });
152
- if (u.length > 0)
153
- throw i.abort(), m === i && (m = null), new Error(
154
- "Failed to register WebMCP tools: " + u.map(
155
- ({ name: r, reason: a }) => `${r} (${p(a)})`
158
+ if (a.length > 0)
159
+ throw d.abort(), w === d && (w = null), new Error(
160
+ "Failed to register WebMCP tools: " + a.map(
161
+ ({ name: e, reason: i }) => `${e} (${p(i)})`
156
162
  ).join(", ")
157
163
  );
158
164
  }
159
- function C() {
165
+ function x() {
160
166
  if (typeof document > "u")
161
167
  return;
162
168
  const n = document.modelContext;
163
169
  return typeof (n == null ? void 0 : n.registerTool) == "function" ? n : void 0;
164
170
  }
165
- function M(n) {
166
- const o = g.playground_list_sites, i = g.playground_save_in_browser, e = g.playground_rename_site, s = g.playground_get_website_url;
171
+ function A(n) {
172
+ const c = g.playground_list_sites, d = g.playground_save_in_browser, t = g.playground_rename_site, r = g.playground_get_website_url;
167
173
  return [
168
174
  {
169
175
  name: "playground_list_sites",
170
- description: o.description,
171
- annotations: o.annotations,
172
- execute: async () => {
176
+ description: c.description,
177
+ inputSchema: h(c.params),
178
+ annotations: c.annotations,
179
+ execute: async (u, o) => {
180
+ var a;
173
181
  try {
174
- return {
182
+ return (a = o == null ? void 0 : o.signal) == null || a.throwIfAborted(), {
175
183
  connectedTabs: 1,
176
- sites: n.list().map((t) => ({
177
- siteId: t.slug,
178
- name: t.name,
179
- storage: w(t.storage),
180
- isActive: t.isActive
184
+ sites: n.list().map((e) => ({
185
+ siteId: e.slug,
186
+ name: e.name,
187
+ storage: v(e.storage),
188
+ isActive: e.isActive
181
189
  }))
182
190
  };
183
- } catch (t) {
191
+ } catch (e) {
184
192
  return {
185
- error: `${o.errorPrefix}: ${p(t)}`
193
+ error: `${c.errorPrefix}: ${p(e)}`
186
194
  };
187
195
  }
188
196
  }
189
197
  },
190
198
  {
191
199
  name: "playground_save_in_browser",
192
- description: i.description,
193
- annotations: i.annotations,
194
- execute: async () => {
200
+ description: d.description,
201
+ inputSchema: h(d.params),
202
+ annotations: d.annotations,
203
+ execute: async (u, o) => {
204
+ var a, e;
195
205
  try {
196
- const t = k(n), l = w(t.storage);
206
+ (a = o == null ? void 0 : o.signal) == null || a.throwIfAborted();
207
+ const i = W(n), l = v(i.storage);
197
208
  if (l !== "temporary")
198
209
  return {
199
210
  success: !0,
200
211
  alreadySaved: !0,
201
- siteId: t.slug,
202
- name: t.name,
212
+ siteId: i.slug,
213
+ name: i.name,
203
214
  storage: l
204
215
  };
205
- const u = await n.saveInBrowser();
206
- return {
216
+ const s = await n.saveInBrowser();
217
+ return (e = o == null ? void 0 : o.signal) == null || e.throwIfAborted(), {
207
218
  success: !0,
208
219
  alreadySaved: !1,
209
- siteId: u.slug,
210
- name: t.name ?? u.slug,
211
- storage: w(u.storage)
220
+ siteId: s.slug,
221
+ name: i.name ?? s.slug,
222
+ storage: v(s.storage)
212
223
  };
213
- } catch (t) {
224
+ } catch (i) {
214
225
  return {
215
- error: `${i.errorPrefix}: ${p(t)}`
226
+ error: `${d.errorPrefix}: ${p(i)}`
216
227
  };
217
228
  }
218
229
  }
219
230
  },
220
231
  {
221
232
  name: "playground_rename_site",
222
- description: e.description,
223
- inputSchema: _(e.params),
224
- annotations: e.annotations,
225
- execute: async (t) => {
233
+ description: t.description,
234
+ inputSchema: h(t.params),
235
+ annotations: t.annotations,
236
+ execute: async (u, o) => {
237
+ var a, e;
226
238
  try {
227
- const l = t.newName, r = n.list().find((a) => a.isActive);
228
- return await n.rename(l), {
239
+ (a = o == null ? void 0 : o.signal) == null || a.throwIfAborted();
240
+ const i = u.newName, s = n.list().find((f) => f.isActive);
241
+ return await n.rename(i), (e = o == null ? void 0 : o.signal) == null || e.throwIfAborted(), {
229
242
  success: !0,
230
- siteId: r == null ? void 0 : r.slug,
231
- newName: l
243
+ siteId: s == null ? void 0 : s.slug,
244
+ newName: i
232
245
  };
233
- } catch (l) {
246
+ } catch (i) {
234
247
  return {
235
- error: `${e.errorPrefix}: ${p(l)}`
248
+ error: `${t.errorPrefix}: ${p(i)}`
236
249
  };
237
250
  }
238
251
  }
239
252
  },
240
253
  {
241
254
  name: "playground_get_website_url",
242
- description: s.description,
243
- annotations: s.annotations,
244
- execute: async () => {
255
+ description: r.description,
256
+ inputSchema: h(r.params),
257
+ annotations: r.annotations,
258
+ execute: async (u, o) => {
259
+ var a;
245
260
  try {
246
- return {
261
+ return (a = o == null ? void 0 : o.signal) == null || a.throwIfAborted(), {
247
262
  url: window.location.href
248
263
  };
249
- } catch (t) {
264
+ } catch (e) {
250
265
  return {
251
- error: `${s.errorPrefix}: ${p(t)}`
266
+ error: `${r.errorPrefix}: ${p(e)}`
252
267
  };
253
268
  }
254
269
  }
255
270
  }
256
271
  ];
257
272
  }
258
- function j(n) {
259
- const o = C();
260
- if (!o)
273
+ function U(n) {
274
+ const c = x();
275
+ if (!c)
261
276
  return { stop: () => {
262
277
  } };
263
- const i = o, e = O();
264
- let s = null, t = !1, l = "";
265
- async function u(r) {
266
- if (t)
278
+ const d = c, t = M();
279
+ let r = null, u = !1, o = "";
280
+ async function a(e) {
281
+ if (u)
267
282
  return;
268
- const a = JSON.stringify(r);
269
- if (a === l)
283
+ const i = JSON.stringify(e);
284
+ if (i === o)
270
285
  return;
271
- l = a, s == null || s.abort(), s = new AbortController();
272
- const { signal: d } = s;
273
- for (const c of r) {
274
- if (e.has(c.name)) {
275
- h.warn(
276
- `Skipping the WebMCP tool "${c.name}" registered by the site: Playground already provides a tool under that name.`
286
+ o = i, r == null || r.abort(), r = new AbortController();
287
+ const { signal: l } = r;
288
+ for (const s of e) {
289
+ if (t.has(s.name)) {
290
+ S.warn(
291
+ `Skipping the WebMCP tool "${s.name}" registered by the site: Playground already provides a tool under that name.`
277
292
  );
278
293
  continue;
279
294
  }
280
295
  try {
281
- await i.registerTool(A(n, c), {
282
- signal: d
296
+ await d.registerTool(k(n, s), {
297
+ signal: l
283
298
  });
284
299
  } catch (f) {
285
- h.warn(
286
- `Failed to register the site's WebMCP tool "${c.name}": ${p(f)}`
300
+ S.warn(
301
+ `Failed to register the site's WebMCP tool "${s.name}": ${p(f)}`
287
302
  );
288
303
  }
289
- if (d.aborted)
304
+ if (l.aborted)
290
305
  return;
291
306
  }
292
307
  }
293
- return n.onWebMCPToolsChanged((r) => {
294
- u(r);
295
- }).catch((r) => {
296
- h.warn("Failed to observe the site's WebMCP tools:", r);
308
+ return n.onWebMCPToolsChanged((e) => {
309
+ a(e);
310
+ }).catch((e) => {
311
+ S.warn("Failed to observe the site's WebMCP tools:", e);
297
312
  }), {
298
313
  stop() {
299
- t = !0, s == null || s.abort(), s = null, l = "";
314
+ u = !0, r == null || r.abort(), r = null, o = "";
300
315
  }
301
316
  };
302
317
  }
303
- function O() {
318
+ function M() {
304
319
  return /* @__PURE__ */ new Set([
305
- ...Object.keys(S),
306
- ...Object.keys(v())
320
+ ...Object.keys(P),
321
+ ...Object.keys($())
307
322
  ]);
308
323
  }
309
- function A(n, o) {
324
+ function k(n, c) {
310
325
  return {
311
- name: o.name,
312
- description: o.description,
313
- inputSchema: o.inputSchema,
314
- annotations: o.annotations,
315
- execute: async (i) => {
326
+ name: c.name,
327
+ description: c.description,
328
+ inputSchema: c.inputSchema,
329
+ annotations: c.annotations,
330
+ execute: async (d, t) => {
331
+ const r = t == null ? void 0 : t.signal, u = crypto.randomUUID(), o = () => {
332
+ n.cancelWebMCPTool(u).catch((a) => {
333
+ S.warn("Failed to cancel the site tool:", a);
334
+ });
335
+ };
316
336
  try {
317
- return await n.callWebMCPTool(o.name, i ?? {});
318
- } catch (e) {
337
+ r == null || r.throwIfAborted(), r == null || r.addEventListener("abort", o, { once: !0 });
338
+ const a = await n.callWebMCPTool(
339
+ c.name,
340
+ d ?? {},
341
+ u
342
+ );
343
+ return r == null || r.throwIfAborted(), a;
344
+ } catch (a) {
319
345
  return {
320
- error: `Error running the WordPress site's "${o.name}" tool: ${p(e)}`
346
+ error: `Error running the WordPress site's "${c.name}" tool: ${p(a)}`
321
347
  };
348
+ } finally {
349
+ r == null || r.removeEventListener("abort", o);
322
350
  }
323
351
  }
324
352
  };
325
353
  }
326
354
  export {
327
- L as registerWebMCPTools,
328
- J as startMcpBridge,
329
- j as startWebMCPSiteToolProxy
355
+ J as registerWebMCPTools,
356
+ L as startMcpBridge,
357
+ U as startWebMCPSiteToolProxy
330
358
  };
331
359
  //# sourceMappingURL=client.js.map