@weapp-vite/mcp 1.4.3 → 1.4.5

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/dist/index.d.mts CHANGED
@@ -113,7 +113,9 @@ interface MiniProgramLike {
113
113
  reLaunch: (url: string) => Promise<MiniProgramPage>;
114
114
  switchTab: (url: string) => Promise<MiniProgramPage>;
115
115
  navigateBack: () => Promise<MiniProgramPage>;
116
- screenshot: () => Promise<string | Uint8Array>;
116
+ screenshot: (options?: {
117
+ timeout?: number;
118
+ }) => Promise<string | Uint8Array>;
117
119
  callWxMethod: (method: string, ...args: unknown[]) => Promise<unknown>;
118
120
  }
119
121
  interface RuntimeConnectionInput {
package/dist/index.d.ts CHANGED
@@ -113,7 +113,9 @@ interface MiniProgramLike {
113
113
  reLaunch: (url: string) => Promise<MiniProgramPage>;
114
114
  switchTab: (url: string) => Promise<MiniProgramPage>;
115
115
  navigateBack: () => Promise<MiniProgramPage>;
116
- screenshot: () => Promise<string | Uint8Array>;
116
+ screenshot: (options?: {
117
+ timeout?: number;
118
+ }) => Promise<string | Uint8Array>;
117
119
  callWxMethod: (method: string, ...args: unknown[]) => Promise<unknown>;
118
120
  }
119
121
  interface RuntimeConnectionInput {
package/dist/index.mjs CHANGED
@@ -622,6 +622,8 @@ async function registerServerResources(server, options) {
622
622
  });
623
623
  }
624
624
 
625
+ const DEFAULT_SCREENSHOT_TIMEOUT = 6e4;
626
+ const SCREENSHOT_RETRY_DELAY = 500;
625
627
  z.object({
626
628
  projectPath: z.string().trim().min(1).describe("\u5C0F\u7A0B\u5E8F\u9879\u76EE\u8DEF\u5F84\uFF1B\u652F\u6301 workspaceRoot \u76F8\u5BF9\u8DEF\u5F84"),
627
629
  timeout: z.number().int().positive().optional(),
@@ -752,6 +754,29 @@ function buildUrl(pagePath, query) {
752
754
  }
753
755
  return `${normalizedPath}${normalizedPath.includes("?") ? "&" : "?"}${search}`;
754
756
  }
757
+ function sleep(ms) {
758
+ return new Promise((resolve) => setTimeout(resolve, ms));
759
+ }
760
+ function isCaptureScreenshotProtocolTimeout(error) {
761
+ return error instanceof Error && "code" in error && error.code === "DEVTOOLS_PROTOCOL_TIMEOUT" && "method" in error && error.method === "App.captureScreenshot";
762
+ }
763
+ async function captureMiniProgramScreenshot(miniProgram, timeout = DEFAULT_SCREENSHOT_TIMEOUT) {
764
+ let lastError;
765
+ for (let attempt = 1; attempt <= 2; attempt += 1) {
766
+ try {
767
+ const screenshot = await miniProgram.screenshot({ timeout });
768
+ return typeof screenshot === "string" ? Buffer.from(screenshot, "base64") : Buffer.from(screenshot);
769
+ } catch (error) {
770
+ lastError = error;
771
+ if (!isCaptureScreenshotProtocolTimeout(error) || attempt === 2) {
772
+ throw error;
773
+ }
774
+ await miniProgram.currentPage().catch(() => void 0);
775
+ await sleep(SCREENSHOT_RETRY_DELAY);
776
+ }
777
+ }
778
+ throw lastError;
779
+ }
755
780
  function parseSelectorWithIndex(selector) {
756
781
  const match = selector.match(/^(.+?)\[index=(\d+)\]$/);
757
782
  if (!match?.[1] || !match[2]) {
@@ -1066,8 +1091,10 @@ function registerDevtoolsRuntimeTools(server, manager) {
1066
1091
  }, async ({ outputPath, ...connection }) => {
1067
1092
  try {
1068
1093
  const result = await manager.withMiniProgram(connection, async (miniProgram) => {
1069
- const screenshot = await miniProgram.screenshot();
1070
- const buffer = typeof screenshot === "string" ? Buffer.from(screenshot, "base64") : Buffer.from(screenshot);
1094
+ const buffer = await captureMiniProgramScreenshot(
1095
+ miniProgram,
1096
+ connection.timeout ?? DEFAULT_SCREENSHOT_TIMEOUT
1097
+ );
1071
1098
  if (outputPath) {
1072
1099
  const resolvedOutputPath = manager.resolveWorkspacePath(outputPath);
1073
1100
  await fs$1.mkdir(path.dirname(resolvedOutputPath), { recursive: true });
@@ -2181,8 +2208,10 @@ async function handlePageStack(req, res, context, manager) {
2181
2208
  async function handleCapture(req, res, manager) {
2182
2209
  const input = captureBodySchema.parse(await readJsonBody(req));
2183
2210
  const result = await manager.withMiniProgram(input, async (miniProgram) => {
2184
- const screenshot = await miniProgram.screenshot();
2185
- const buffer = typeof screenshot === "string" ? Buffer.from(screenshot, "base64") : Buffer.from(screenshot);
2211
+ const buffer = await captureMiniProgramScreenshot(
2212
+ miniProgram,
2213
+ input.timeout ?? DEFAULT_SCREENSHOT_TIMEOUT
2214
+ );
2186
2215
  if (input.outputPath) {
2187
2216
  const resolvedOutputPath = manager.resolveWorkspacePath(input.outputPath);
2188
2217
  await fs$1.mkdir(path.dirname(resolvedOutputPath), { recursive: true });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/mcp",
3
3
  "type": "module",
4
- "version": "1.4.3",
4
+ "version": "1.4.5",
5
5
  "description": "mcp",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "ISC",
@@ -33,7 +33,7 @@
33
33
  "dependencies": {
34
34
  "@modelcontextprotocol/sdk": "^1.29.0",
35
35
  "zod": "^4.4.3",
36
- "@weapp-vite/devtools-runtime": "0.4.3"
36
+ "@weapp-vite/devtools-runtime": "0.4.5"
37
37
  },
38
38
  "scripts": {
39
39
  "dev": "unbuild --stub",