@lark-apaas/nestjs-capability 0.1.4-beta.7 → 0.1.4-beta.9

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/README.md CHANGED
@@ -101,6 +101,7 @@ const result = await this.capabilityService
101
101
  userId: 'custom-user-id',
102
102
  tenantId: 'custom-tenant-id',
103
103
  appId: 'custom-app-id',
104
+ isSystemAccount: false,
104
105
  },
105
106
  });
106
107
 
@@ -426,6 +427,7 @@ interface PluginActionContext {
426
427
  userId: string; // 用户 ID
427
428
  tenantId: string; // 租户 ID
428
429
  appId: string; // 应用 ID
430
+ isSystemAccount: boolean; // 是否为系统账号
429
431
  };
430
432
  isDebug: boolean; // 是否为调试模式
431
433
  }
package/dist/index.cjs CHANGED
@@ -582,6 +582,100 @@ async function migrationAdaptor(promise) {
582
582
  }
583
583
  __name(migrationAdaptor, "migrationAdaptor");
584
584
 
585
+ // src/utils/plugin-error-format.ts
586
+ var PLUGIN_ERROR_CODES = /* @__PURE__ */ new Set([
587
+ "INVALID_INPUT",
588
+ "EXECUTION_ERROR",
589
+ "INVALID_OUTPUT",
590
+ "INVALID_CONFIG",
591
+ "ACTION_NOT_FOUND"
592
+ ]);
593
+ function isPluginError(error) {
594
+ if (!error || typeof error !== "object") return false;
595
+ const code = error.code;
596
+ return typeof code === "string" && PLUGIN_ERROR_CODES.has(code);
597
+ }
598
+ __name(isPluginError, "isPluginError");
599
+ function formatPluginError(error, displayName) {
600
+ if (!isPluginError(error)) return null;
601
+ const err = error;
602
+ const label = displayName ? `\u300C${displayName}\u300D` : "";
603
+ switch (err.code) {
604
+ case "INVALID_INPUT":
605
+ return `${label}\u63D2\u4EF6\u8F93\u5165\u6821\u9A8C\u5931\u8D25: ${formatZodIssuesChinese(err.details)}`;
606
+ case "INVALID_OUTPUT":
607
+ return `${label}\u63D2\u4EF6\u8F93\u51FA\u6821\u9A8C\u5931\u8D25: ${formatZodIssuesChinese(err.details)}`;
608
+ case "INVALID_CONFIG":
609
+ return `${label}\u63D2\u4EF6\u914D\u7F6E\u6821\u9A8C\u5931\u8D25: ${formatZodIssuesChinese(err.details)}`;
610
+ case "ACTION_NOT_FOUND":
611
+ return `${label}\u63D2\u4EF6\u64CD\u4F5C\u4E0D\u5B58\u5728`;
612
+ case "EXECUTION_ERROR": {
613
+ const inner = err.originalError instanceof Error ? err.originalError.message : "";
614
+ return inner ? `${label}\u63D2\u4EF6\u6267\u884C\u5931\u8D25: ${inner}` : `${label}\u63D2\u4EF6\u6267\u884C\u5931\u8D25`;
615
+ }
616
+ default:
617
+ return `${label}\u63D2\u4EF6\u6267\u884C\u5931\u8D25`;
618
+ }
619
+ }
620
+ __name(formatPluginError, "formatPluginError");
621
+ function formatZodIssuesChinese(details) {
622
+ if (!Array.isArray(details) || details.length === 0) return "\u53C2\u6570\u683C\u5F0F\u6821\u9A8C\u51FA\u9519";
623
+ return details.map(translateZodIssue).join("\uFF1B");
624
+ }
625
+ __name(formatZodIssuesChinese, "formatZodIssuesChinese");
626
+ function translateZodIssue(issue) {
627
+ const field = issue.path?.length > 0 ? `\u300C${issue.path.join(".")}\u300D` : "";
628
+ switch (issue.code) {
629
+ case "invalid_type": {
630
+ const received = issue.received;
631
+ if (received === "undefined" || received === "null") {
632
+ return `${field}\u5B57\u6BB5\u662F\u5FC5\u586B\u7684`;
633
+ }
634
+ return `${field}\u5B57\u6BB5\u7C7B\u578B\u9519\u8BEF\uFF0C\u671F\u671B${translateType(issue.expected)}`;
635
+ }
636
+ case "too_small": {
637
+ if (issue.type === "string") {
638
+ if (issue.minimum === 1) return `${field}\u5B57\u6BB5\u4E0D\u80FD\u4E3A\u7A7A`;
639
+ return `${field}\u5B57\u6BB5\u957F\u5EA6\u4E0D\u80FD\u5C11\u4E8E ${issue.minimum} \u4E2A\u5B57\u7B26`;
640
+ }
641
+ if (issue.type === "number") return `${field}\u5B57\u6BB5\u4E0D\u80FD\u5C0F\u4E8E ${issue.minimum}`;
642
+ if (issue.type === "array") return `${field}\u5B57\u6BB5\u81F3\u5C11\u9700\u8981 ${issue.minimum} \u9879`;
643
+ return `${field}\u5B57\u6BB5\u503C\u8FC7\u5C0F`;
644
+ }
645
+ case "too_big": {
646
+ if (issue.type === "string") return `${field}\u5B57\u6BB5\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7 ${issue.maximum} \u4E2A\u5B57\u7B26`;
647
+ if (issue.type === "number") return `${field}\u5B57\u6BB5\u4E0D\u80FD\u5927\u4E8E ${issue.maximum}`;
648
+ if (issue.type === "array") return `${field}\u5B57\u6BB5\u6700\u591A ${issue.maximum} \u9879`;
649
+ return `${field}\u5B57\u6BB5\u503C\u8FC7\u5927`;
650
+ }
651
+ case "invalid_string": {
652
+ if (issue.validation === "email") return `${field}\u5B57\u6BB5\u4E0D\u662F\u6709\u6548\u7684\u90AE\u7BB1\u5730\u5740`;
653
+ if (issue.validation === "url") return `${field}\u5B57\u6BB5\u4E0D\u662F\u6709\u6548\u7684 URL`;
654
+ if (issue.validation === "regex") return `${field}\u5B57\u6BB5\u683C\u5F0F\u4E0D\u6B63\u786E`;
655
+ return `${field}\u5B57\u6BB5\u683C\u5F0F\u6821\u9A8C\u51FA\u9519`;
656
+ }
657
+ case "invalid_enum_value":
658
+ return `${field}\u5B57\u6BB5\u503C\u4E0D\u5728\u5141\u8BB8\u7684\u8303\u56F4\u5185`;
659
+ case "unrecognized_keys":
660
+ return `\u5305\u542B\u672A\u77E5\u5B57\u6BB5: ${issue.keys?.join(", ")}`;
661
+ default:
662
+ return field ? `${field}\u5B57\u6BB5\u683C\u5F0F\u6821\u9A8C\u51FA\u9519` : "\u8F93\u5165\u683C\u5F0F\u6821\u9A8C\u51FA\u9519";
663
+ }
664
+ }
665
+ __name(translateZodIssue, "translateZodIssue");
666
+ function translateType(type) {
667
+ const map = {
668
+ string: "\u6587\u672C",
669
+ number: "\u6570\u5B57",
670
+ boolean: "\u5E03\u5C14\u503C",
671
+ array: "\u6570\u7EC4",
672
+ object: "\u5BF9\u8C61",
673
+ integer: "\u6574\u6570"
674
+ };
675
+ return map[type] || type;
676
+ }
677
+ __name(translateType, "translateType");
678
+
585
679
  // src/services/capability.service.ts
586
680
  function _ts_decorate4(decorators, target, key, desc) {
587
681
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -646,8 +740,7 @@ var CapabilityService = class _CapabilityService {
646
740
  this.pluginLoaderService = pluginLoaderService;
647
741
  this.templateEngineService = templateEngineService;
648
742
  this.telemetryService = telemetryService;
649
- const isDev = process.env.NODE_ENV === "development";
650
- this.capabilitiesDir = path2.join(process.cwd(), isDev ? "server/capabilities" : "capabilities");
743
+ this.capabilitiesDir = path2.join(process.cwd(), "server/capabilities");
651
744
  }
652
745
  /**
653
746
  * 设置模块配置
@@ -918,6 +1011,11 @@ var CapabilityService = class _CapabilityService {
918
1011
  });
919
1012
  const errorCode = String(error?.code ?? "EXECUTION_ERROR");
920
1013
  this.reportCallEvent(config.pluginKey, false, durationMs, isStream, contextOverride, errorCode);
1014
+ const displayName = this.pluginLoaderService.getManifest(config.pluginKey)?.displayName;
1015
+ const formattedMessage = formatPluginError(error, displayName);
1016
+ if (formattedMessage && error instanceof Error) {
1017
+ error.message = formattedMessage;
1018
+ }
921
1019
  throw error;
922
1020
  }
923
1021
  }
@@ -973,6 +1071,11 @@ var CapabilityService = class _CapabilityService {
973
1071
  });
974
1072
  const errorCode = String(error?.code ?? "EXECUTION_ERROR");
975
1073
  this.reportCallEvent(config.pluginKey, false, durationMs, true, contextOverride, errorCode);
1074
+ const displayName = this.pluginLoaderService.getManifest(config.pluginKey)?.displayName;
1075
+ const formattedMessage = formatPluginError(error, displayName);
1076
+ if (formattedMessage && error instanceof Error) {
1077
+ error.message = formattedMessage;
1078
+ }
976
1079
  throw error;
977
1080
  }
978
1081
  }
@@ -1067,7 +1170,7 @@ var CapabilityService = class _CapabilityService {
1067
1170
  this.reportCallEvent(config.pluginKey, false, durationMs, true, contextOverride, errorCode);
1068
1171
  yield {
1069
1172
  type: "error",
1070
- error: this.extractErrorInfo(error)
1173
+ error: this.extractErrorInfo(error, config.pluginKey)
1071
1174
  };
1072
1175
  }
1073
1176
  }
@@ -1090,8 +1193,10 @@ var CapabilityService = class _CapabilityService {
1090
1193
  /**
1091
1194
  * 从错误对象提取错误信息
1092
1195
  * 支持 PluginError 和 RateLimitError
1196
+ * @param error - 捕获到的错误
1197
+ * @param pluginKey - 插件 key,用于获取 displayName 格式化 PluginError 消息
1093
1198
  */
1094
- extractErrorInfo(error) {
1199
+ extractErrorInfo(error, pluginKey) {
1095
1200
  const err = error;
1096
1201
  const code = err?.code ?? "EXECUTION_ERROR";
1097
1202
  const message = err instanceof Error ? err.message : String(err);
@@ -1103,6 +1208,16 @@ var CapabilityService = class _CapabilityService {
1103
1208
  rateLimitMessage: err.rateLimitMessage
1104
1209
  };
1105
1210
  }
1211
+ if (pluginKey) {
1212
+ const displayName = this.pluginLoaderService.getManifest(pluginKey)?.displayName;
1213
+ const formattedMessage = formatPluginError(error, displayName);
1214
+ if (formattedMessage) {
1215
+ return {
1216
+ code,
1217
+ message: formattedMessage
1218
+ };
1219
+ }
1220
+ }
1106
1221
  return {
1107
1222
  code,
1108
1223
  message
@@ -1152,7 +1267,8 @@ var CapabilityService = class _CapabilityService {
1152
1267
  return {
1153
1268
  appId: ctx?.appId ?? "",
1154
1269
  userId: ctx?.userId ?? "",
1155
- tenantId: ctx?.tenantId ?? ""
1270
+ tenantId: ctx?.tenantId ?? "",
1271
+ isSystemAccount: ctx?.isSystemAccount ?? false
1156
1272
  };
1157
1273
  }
1158
1274
  };