@weapp-vite/mcp 1.4.6 → 1.4.8

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
@@ -115,7 +115,7 @@ interface MiniProgramLike {
115
115
  navigateBack: () => Promise<MiniProgramPage>;
116
116
  screenshot: (options?: {
117
117
  timeout?: number;
118
- }) => Promise<string | Uint8Array>;
118
+ }) => Promise<string | Uint8Array | undefined>;
119
119
  callWxMethod: (method: string, ...args: unknown[]) => Promise<unknown>;
120
120
  }
121
121
  interface RuntimeConnectionInput {
@@ -146,8 +146,10 @@ declare class RuntimeSessionManager {
146
146
  resolveWorkspacePath(filePath: string): string;
147
147
  withMiniProgram<T>(input: RuntimeConnectionInput, runner: (miniProgram: MiniProgramLike) => Promise<T>): Promise<T>;
148
148
  withPage<T>(input: RuntimeConnectionInput, runner: (page: MiniProgramPage, miniProgram: MiniProgramLike) => Promise<T>): Promise<T>;
149
+ rememberPage(input: RuntimeConnectionInput, page: MiniProgramPage | null | undefined): void;
149
150
  private resolveSessionKey;
150
151
  private attach;
152
+ private resolveCurrentPage;
151
153
  private detach;
152
154
  private pushLog;
153
155
  }
package/dist/index.d.ts CHANGED
@@ -115,7 +115,7 @@ interface MiniProgramLike {
115
115
  navigateBack: () => Promise<MiniProgramPage>;
116
116
  screenshot: (options?: {
117
117
  timeout?: number;
118
- }) => Promise<string | Uint8Array>;
118
+ }) => Promise<string | Uint8Array | undefined>;
119
119
  callWxMethod: (method: string, ...args: unknown[]) => Promise<unknown>;
120
120
  }
121
121
  interface RuntimeConnectionInput {
@@ -146,8 +146,10 @@ declare class RuntimeSessionManager {
146
146
  resolveWorkspacePath(filePath: string): string;
147
147
  withMiniProgram<T>(input: RuntimeConnectionInput, runner: (miniProgram: MiniProgramLike) => Promise<T>): Promise<T>;
148
148
  withPage<T>(input: RuntimeConnectionInput, runner: (page: MiniProgramPage, miniProgram: MiniProgramLike) => Promise<T>): Promise<T>;
149
+ rememberPage(input: RuntimeConnectionInput, page: MiniProgramPage | null | undefined): void;
149
150
  private resolveSessionKey;
150
151
  private attach;
152
+ private resolveCurrentPage;
151
153
  private detach;
152
154
  private pushLog;
153
155
  }
package/dist/index.mjs CHANGED
@@ -681,23 +681,29 @@ class RuntimeSessionManager {
681
681
  this.attach(sessionKey, miniProgram);
682
682
  try {
683
683
  return await runner(miniProgram);
684
- } catch (error) {
685
- this.detach(sessionKey);
686
- await closeSharedMiniProgram(projectPath, input.sessionId || input.port);
687
- throw error;
688
684
  } finally {
689
685
  releaseSharedMiniProgram(projectPath, input.sessionId || input.port);
690
686
  }
691
687
  }
692
688
  async withPage(input, runner) {
689
+ const projectPath = this.resolveProjectPath(input.projectPath);
690
+ const sessionKey = this.resolveSessionKey(projectPath, input);
693
691
  return await this.withMiniProgram(input, async (miniProgram) => {
694
- const page = await miniProgram.currentPage();
692
+ const page = await this.resolveCurrentPage(sessionKey, miniProgram);
695
693
  if (!page) {
696
694
  throw new Error("\u5F53\u524D\u6CA1\u6709\u6D3B\u52A8\u9875\u9762\uFF0C\u8BF7\u5148\u8C03\u7528 weapp_devtools_connect \u786E\u8BA4 DevTools \u4F1A\u8BDD\u3002");
697
695
  }
698
696
  return await runner(page, miniProgram);
699
697
  });
700
698
  }
699
+ rememberPage(input, page) {
700
+ if (!page) {
701
+ return;
702
+ }
703
+ const projectPath = this.resolveProjectPath(input.projectPath);
704
+ const sessionKey = this.resolveSessionKey(projectPath, input);
705
+ this.attachedSessions.get(sessionKey).currentPage = page;
706
+ }
701
707
  resolveSessionKey(projectPath, input) {
702
708
  return resolveSharedMiniProgramSessionKey({
703
709
  port: input.port,
@@ -725,6 +731,19 @@ class RuntimeSessionManager {
725
731
  onException
726
732
  });
727
733
  }
734
+ async resolveCurrentPage(sessionKey, miniProgram) {
735
+ try {
736
+ const page = await miniProgram.currentPage();
737
+ this.attachedSessions.get(sessionKey).currentPage = page;
738
+ return page;
739
+ } catch (error) {
740
+ const cachedPage = this.attachedSessions.get(sessionKey)?.currentPage;
741
+ if (cachedPage && isCurrentPageRecoverableError(error)) {
742
+ return cachedPage;
743
+ }
744
+ throw error;
745
+ }
746
+ }
728
747
  detach(projectPath) {
729
748
  const attached = this.attachedSessions.get(projectPath);
730
749
  if (!attached) {
@@ -760,11 +779,23 @@ function sleep(ms) {
760
779
  function isCaptureScreenshotProtocolTimeout(error) {
761
780
  return error instanceof Error && "code" in error && error.code === "DEVTOOLS_PROTOCOL_TIMEOUT" && "method" in error && error.method === "App.captureScreenshot";
762
781
  }
782
+ function isCurrentPageRecoverableError(error) {
783
+ if (!(error instanceof Error)) {
784
+ return false;
785
+ }
786
+ if ("code" in error && error.code === "DEVTOOLS_PROTOCOL_TIMEOUT" && "method" in error) {
787
+ return error.method === "App.getCurrentPage" || error.method === "App.getPageStack" || error.method === "App.callFunction";
788
+ }
789
+ return error.message.includes("page is not on top of page stack");
790
+ }
763
791
  async function captureMiniProgramScreenshot(miniProgram, timeout = DEFAULT_SCREENSHOT_TIMEOUT) {
764
792
  let lastError;
765
793
  for (let attempt = 1; attempt <= 2; attempt += 1) {
766
794
  try {
767
795
  const screenshot = await miniProgram.screenshot({ timeout });
796
+ if (screenshot == null) {
797
+ throw new Error("DevTools screenshot returned no data.");
798
+ }
768
799
  return typeof screenshot === "string" ? Buffer.from(screenshot, "base64") : Buffer.from(screenshot);
769
800
  } catch (error) {
770
801
  lastError = error;
@@ -829,14 +860,14 @@ async function queryElements(page, selectorInput) {
829
860
  return [elements[index]];
830
861
  }
831
862
  async function summarizeElement(element, withWxml = false) {
832
- const [text, value, size, offset, scrollWidth, scrollHeight, outerWxml] = await Promise.all([
863
+ const [text, value, size, offset, scrollWidth, scrollHeight, markup] = await Promise.all([
833
864
  callMaybe(element, "text"),
834
865
  callMaybe(element, "value"),
835
866
  callMaybe(element, "size"),
836
867
  callMaybe(element, "offset"),
837
868
  callMaybe(element, "scrollWidth"),
838
869
  callMaybe(element, "scrollHeight"),
839
- withWxml ? callMaybe(element, "outerWxml") : Promise.resolve(void 0)
870
+ withWxml ? readElementMarkup(element).catch(() => null) : Promise.resolve(null)
840
871
  ]);
841
872
  return compactObject({
842
873
  tagName: readProperty(element, "tagName"),
@@ -846,9 +877,27 @@ async function summarizeElement(element, withWxml = false) {
846
877
  offset,
847
878
  scrollWidth,
848
879
  scrollHeight,
849
- outerWxml
880
+ outerWxml: withWxml && markup?.type === "outerWxml" ? markup.wxml : null,
881
+ wxml: withWxml && markup?.type !== "outerWxml" ? markup?.wxml ?? null : void 0,
882
+ wxmlType: withWxml ? markup?.type ?? null : void 0
850
883
  });
851
884
  }
885
+ async function readElementMarkup(element, outer = false) {
886
+ if (outer) {
887
+ const outerWxml = await callMaybe(element, "outerWxml");
888
+ if (typeof outerWxml === "string") {
889
+ return {
890
+ type: "outerWxml",
891
+ wxml: outerWxml
892
+ };
893
+ }
894
+ }
895
+ const wxml = await callRequiredMethod(element, "wxml");
896
+ return {
897
+ type: outer ? "wxml-fallback" : "wxml",
898
+ wxml: typeof wxml === "string" ? wxml : String(wxml ?? "")
899
+ };
900
+ }
852
901
  function toSerializableValue(value) {
853
902
  return toDevtoolsSerializableValue(value);
854
903
  }
@@ -984,6 +1033,7 @@ function registerDevtoolsRuntimeTools(server, manager) {
984
1033
  }
985
1034
  const result = await manager.withMiniProgram(connection, async (miniProgram) => {
986
1035
  const page = await miniProgram.currentPage().catch(() => null);
1036
+ manager.rememberPage(connection, page);
987
1037
  const systemInfo = await miniProgram.systemInfo().catch(() => null);
988
1038
  return {
989
1039
  connected: true,
@@ -1007,6 +1057,7 @@ function registerDevtoolsRuntimeTools(server, manager) {
1007
1057
  const result = await manager.withMiniProgram(connection, async (miniProgram) => {
1008
1058
  if (transition === "navigateBack") {
1009
1059
  const page2 = await miniProgram.navigateBack();
1060
+ manager.rememberPage(connection, page2);
1010
1061
  if (waitMs && page2) {
1011
1062
  await page2.waitFor(waitMs);
1012
1063
  }
@@ -1024,6 +1075,7 @@ function registerDevtoolsRuntimeTools(server, manager) {
1024
1075
  transition,
1025
1076
  url
1026
1077
  );
1078
+ manager.rememberPage(connection, page);
1027
1079
  if (waitMs && page) {
1028
1080
  await page.waitFor(waitMs);
1029
1081
  }
@@ -1354,12 +1406,12 @@ function registerRuntimeNodeTools(server, manager) {
1354
1406
  try {
1355
1407
  const result = await manager.withPage(connection, async (page) => {
1356
1408
  const element = await resolveElement(page, selector, innerSelector);
1357
- const method = outer ? "outerWxml" : "wxml";
1409
+ const markup = await readElementMarkup(element, outer);
1358
1410
  return {
1359
1411
  selector,
1360
1412
  innerSelector: innerSelector ?? null,
1361
- type: method,
1362
- wxml: toSerializableValue(await callRequiredMethod(element, method))
1413
+ type: markup.type,
1414
+ wxml: toSerializableValue(markup.wxml)
1363
1415
  };
1364
1416
  });
1365
1417
  return toToolResult(result);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/mcp",
3
3
  "type": "module",
4
- "version": "1.4.6",
4
+ "version": "1.4.8",
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.6"
36
+ "@weapp-vite/devtools-runtime": "0.4.8"
37
37
  },
38
38
  "scripts": {
39
39
  "dev": "unbuild --stub",