@lark-apaas/nestjs-capability 0.0.1-alpha.11 → 0.0.1-alpha.13

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.cjs CHANGED
@@ -41,7 +41,8 @@ __export(index_exports, {
41
41
  PluginLoaderService: () => PluginLoaderService,
42
42
  PluginNotFoundError: () => PluginNotFoundError,
43
43
  TemplateEngineService: () => TemplateEngineService,
44
- WebhookController: () => WebhookController
44
+ WebhookController: () => WebhookController,
45
+ migrationAdaptor: () => migrationAdaptor
45
46
  });
46
47
  module.exports = __toCommonJS(index_exports);
47
48
 
@@ -79,9 +80,9 @@ var TemplateEngineService = class {
79
80
  __name(this, "TemplateEngineService");
80
81
  }
81
82
  // 匹配 {{input.xxx}} 或 {{input.xxx.yyy}} 表达式
82
- EXPR_REGEX = /\{\{input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*)\}\}/g;
83
+ EXPR_REGEX = /\{\{\s*input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*\s*)\}\}/g;
83
84
  // 匹配整串单个表达式
84
- WHOLE_STRING_EXPR_REGEX = /^\{\{input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*)\}\}$/;
85
+ WHOLE_STRING_EXPR_REGEX = /^\{\{\s*input\.([a-zA-Z_][a-zA-Z_0-9]*(?:\.[a-zA-Z_][a-zA-Z_0-9]*)*)\s*\}\}$/;
85
86
  resolve(template, input) {
86
87
  if (typeof template === "string") {
87
88
  return this.resolveString(template, input);
@@ -154,9 +155,11 @@ var PluginNotFoundError = class extends Error {
154
155
  static {
155
156
  __name(this, "PluginNotFoundError");
156
157
  }
158
+ pluginKey;
157
159
  constructor(pluginKey) {
158
160
  super(`Plugin not found: ${pluginKey}`);
159
161
  this.name = "PluginNotFoundError";
162
+ this.pluginKey = pluginKey;
160
163
  }
161
164
  };
162
165
  var PluginLoadError = class extends Error {
@@ -300,6 +303,43 @@ function stringifyForLog(value, maxLength = DEFAULT_MAX_LENGTH) {
300
303
  }
301
304
  __name(stringifyForLog, "stringifyForLog");
302
305
 
306
+ // src/utils/migration-adaptor.ts
307
+ async function migrationAdaptor(promise) {
308
+ try {
309
+ const result = await promise;
310
+ return {
311
+ code: 0,
312
+ data: {
313
+ output: result,
314
+ outcome: "success"
315
+ }
316
+ };
317
+ } catch (error) {
318
+ const errorMessage = error instanceof Error ? error.message : String(error);
319
+ const appStatusCode = error instanceof Error && "code" in error ? String(error.code) : "EXECUTION_ERROR";
320
+ return {
321
+ code: -1,
322
+ data: {
323
+ outcome: "error",
324
+ failureOutput: {
325
+ response: {
326
+ status: {
327
+ protocolStatusCode: "500",
328
+ appStatusCode
329
+ },
330
+ body: JSON.stringify({
331
+ error: errorMessage,
332
+ stack: error instanceof Error ? error.stack : void 0
333
+ })
334
+ }
335
+ }
336
+ },
337
+ message: errorMessage
338
+ };
339
+ }
340
+ }
341
+ __name(migrationAdaptor, "migrationAdaptor");
342
+
303
343
  // src/services/capability.service.ts
304
344
  function _ts_decorate3(decorators, target, key, desc) {
305
345
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -322,18 +362,24 @@ var CapabilityNotFoundError = class extends Error {
322
362
  static {
323
363
  __name(this, "CapabilityNotFoundError");
324
364
  }
365
+ capabilityId;
325
366
  constructor(capabilityId) {
326
367
  super(`Capability not found: ${capabilityId}`);
327
368
  this.name = "CapabilityNotFoundError";
369
+ this.capabilityId = capabilityId;
328
370
  }
329
371
  };
330
372
  var ActionNotFoundError = class extends Error {
331
373
  static {
332
374
  __name(this, "ActionNotFoundError");
333
375
  }
376
+ pluginKey;
377
+ actionName;
334
378
  constructor(pluginKey, actionName) {
335
379
  super(`Action '${actionName}' not found in plugin ${pluginKey}`);
336
380
  this.name = "ActionNotFoundError";
381
+ this.pluginKey = pluginKey;
382
+ this.actionName = actionName;
337
383
  }
338
384
  };
339
385
  var CapabilityService = class _CapabilityService {
@@ -429,6 +475,14 @@ var CapabilityService = class _CapabilityService {
429
475
  this.logger.log(`Capability file added: ${filePath}`);
430
476
  try {
431
477
  this.loadCapabilityFromFile(filePath);
478
+ const capabilityId = this.filePathToId.get(filePath);
479
+ if (capabilityId) {
480
+ const config = this.capabilities.get(capabilityId);
481
+ if (config?.pluginKey) {
482
+ this.pluginLoaderService.clearCache(config.pluginKey);
483
+ this.logger.log(`Cleared plugin cache for new capability: ${config.pluginKey}`);
484
+ }
485
+ }
432
486
  } catch (error) {
433
487
  this.logger.error(`Failed to load new capability: ${filePath}`, error);
434
488
  }
@@ -802,19 +856,27 @@ var DebugController = class _DebugController {
802
856
  this.pluginLoaderService = pluginLoaderService;
803
857
  this.templateEngineService = templateEngineService;
804
858
  }
805
- list() {
806
- const capabilities = this.capabilityService.listCapabilities();
807
- return {
808
- status_code: ErrorCodes.SUCCESS,
809
- data: {
810
- capabilities: capabilities.map((c) => ({
811
- id: c.id,
812
- name: c.name,
813
- pluginID: c.pluginKey,
814
- pluginVersion: c.pluginVersion
815
- }))
816
- }
817
- };
859
+ list(res) {
860
+ try {
861
+ const capabilities = this.capabilityService.listCapabilities();
862
+ res.status(import_common4.HttpStatus.OK).json({
863
+ status_code: ErrorCodes.SUCCESS,
864
+ data: {
865
+ capabilities: capabilities.map((c) => ({
866
+ id: c.id,
867
+ name: c.name,
868
+ pluginID: c.pluginKey,
869
+ pluginVersion: c.pluginVersion
870
+ }))
871
+ }
872
+ });
873
+ } catch (error) {
874
+ this.logger.error("Failed to list capabilities", error);
875
+ res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json({
876
+ status_code: ErrorCodes.EXECUTION_ERROR,
877
+ error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
878
+ });
879
+ }
818
880
  }
819
881
  /**
820
882
  * 获取 capability 配置
@@ -826,11 +888,7 @@ var DebugController = class _DebugController {
826
888
  }
827
889
  const config = this.capabilityService.getCapability(capabilityId);
828
890
  if (!config) {
829
- throw new import_common4.HttpException({
830
- code: 1,
831
- message: `Capability not found: ${capabilityId}`,
832
- error: "CAPABILITY_NOT_FOUND"
833
- }, import_common4.HttpStatus.NOT_FOUND);
891
+ throw new CapabilityNotFoundError(capabilityId);
834
892
  }
835
893
  return config;
836
894
  }
@@ -845,25 +903,21 @@ var DebugController = class _DebugController {
845
903
  const pluginInstance = await this.pluginLoaderService.loadPlugin(pluginKey);
846
904
  const actions = pluginInstance.listActions();
847
905
  if (actions.length === 0) {
848
- throw new import_common4.HttpException({
849
- code: 1,
850
- message: `Plugin ${pluginKey} has no actions`,
851
- error: "NO_ACTIONS"
852
- }, import_common4.HttpStatus.BAD_REQUEST);
906
+ throw new Error(`Plugin ${pluginKey} has no actions`);
853
907
  }
854
908
  return actions[0];
855
909
  }
856
- async debug(capabilityId, body) {
910
+ async debug(capabilityId, body, res) {
857
911
  const startTime = Date.now();
858
912
  const params = body.params ?? {};
859
- const config = this.getCapabilityConfig(capabilityId, body.capability);
860
- const action = await this.getActionName(config.pluginKey, body.action);
861
- const resolvedParams = this.templateEngineService.resolve(config.formValue, params);
862
913
  try {
914
+ const config = this.getCapabilityConfig(capabilityId, body.capability);
915
+ const action = await this.getActionName(config.pluginKey, body.action);
916
+ const resolvedParams = this.templateEngineService.resolve(config.formValue, params);
863
917
  const result = await this.capabilityService.loadWithConfig(config).call(action, params, {
864
918
  isDebug: true
865
919
  });
866
- return {
920
+ res.status(import_common4.HttpStatus.OK).json({
867
921
  status_code: ErrorCodes.SUCCESS,
868
922
  data: {
869
923
  output: result,
@@ -875,31 +929,38 @@ var DebugController = class _DebugController {
875
929
  action
876
930
  }
877
931
  }
878
- };
932
+ });
879
933
  } catch (error) {
880
- if (error instanceof CapabilityNotFoundError) {
881
- throw new import_common4.HttpException({
882
- status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
883
- error_msg: `Capability not found: ${capabilityId}`
884
- }, import_common4.HttpStatus.NOT_FOUND);
885
- }
886
- if (error instanceof PluginNotFoundError) {
887
- throw new import_common4.HttpException({
888
- status_code: ErrorCodes.PLUGIN_NOT_FOUND,
889
- error_msg: `Plugin not found: ${config.pluginKey}`
890
- }, import_common4.HttpStatus.INTERNAL_SERVER_ERROR);
891
- }
892
- if (error instanceof ActionNotFoundError) {
893
- throw new import_common4.HttpException({
894
- status_code: ErrorCodes.ACTION_NOT_FOUND,
895
- error_msg: `Action '${action}' not found in plugin ${config.pluginKey}`
896
- }, import_common4.HttpStatus.BAD_REQUEST);
897
- }
898
- throw new import_common4.HttpException({
899
- status_code: ErrorCodes.EXECUTION_ERROR,
900
- error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
901
- }, import_common4.HttpStatus.INTERNAL_SERVER_ERROR);
934
+ this.logger.error("Debug execution failed", error);
935
+ res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
936
+ }
937
+ }
938
+ /**
939
+ * 构建错误响应
940
+ */
941
+ buildErrorResponse(error) {
942
+ if (error instanceof CapabilityNotFoundError) {
943
+ return {
944
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
945
+ error_msg: `Capability not found: ${error.capabilityId}`
946
+ };
947
+ }
948
+ if (error instanceof PluginNotFoundError) {
949
+ return {
950
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
951
+ error_msg: `Plugin not found, please install plugin: ${error.pluginKey}`
952
+ };
953
+ }
954
+ if (error instanceof ActionNotFoundError) {
955
+ return {
956
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
957
+ error_msg: `Action '${error.actionName}' not found in plugin ${error.pluginKey}`
958
+ };
902
959
  }
960
+ return {
961
+ status_code: ErrorCodes.EXECUTION_ERROR,
962
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
963
+ };
903
964
  }
904
965
  async debugStream(capabilityId, body, res) {
905
966
  const params = body.params ?? {};
@@ -1017,18 +1078,23 @@ var DebugController = class _DebugController {
1017
1078
  };
1018
1079
  _ts_decorate4([
1019
1080
  (0, import_common4.Get)("list"),
1081
+ _ts_param2(0, (0, import_common4.Res)()),
1020
1082
  _ts_metadata2("design:type", Function),
1021
- _ts_metadata2("design:paramtypes", []),
1022
- _ts_metadata2("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
1083
+ _ts_metadata2("design:paramtypes", [
1084
+ typeof Response === "undefined" ? Object : Response
1085
+ ]),
1086
+ _ts_metadata2("design:returntype", void 0)
1023
1087
  ], DebugController.prototype, "list", null);
1024
1088
  _ts_decorate4([
1025
1089
  (0, import_common4.Post)("debug/:capability_id"),
1026
1090
  _ts_param2(0, (0, import_common4.Param)("capability_id")),
1027
1091
  _ts_param2(1, (0, import_common4.Body)()),
1092
+ _ts_param2(2, (0, import_common4.Res)()),
1028
1093
  _ts_metadata2("design:type", Function),
1029
1094
  _ts_metadata2("design:paramtypes", [
1030
1095
  String,
1031
- typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody
1096
+ typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody,
1097
+ typeof Response === "undefined" ? Object : Response
1032
1098
  ]),
1033
1099
  _ts_metadata2("design:returntype", Promise)
1034
1100
  ], DebugController.prototype, "debug", null);
@@ -1083,53 +1149,68 @@ var WebhookController = class _WebhookController {
1083
1149
  constructor(capabilityService) {
1084
1150
  this.capabilityService = capabilityService;
1085
1151
  }
1086
- list() {
1087
- const capabilities = this.capabilityService.listCapabilities();
1088
- return {
1089
- status_code: ErrorCodes.SUCCESS,
1090
- data: {
1091
- capabilities: capabilities.map((c) => ({
1092
- id: c.id,
1093
- name: c.name,
1094
- pluginID: c.pluginKey,
1095
- pluginVersion: c.pluginVersion
1096
- }))
1097
- }
1098
- };
1152
+ list(res) {
1153
+ try {
1154
+ const capabilities = this.capabilityService.listCapabilities();
1155
+ res.status(import_common5.HttpStatus.OK).json({
1156
+ status_code: ErrorCodes.SUCCESS,
1157
+ data: {
1158
+ capabilities: capabilities.map((c) => ({
1159
+ id: c.id,
1160
+ name: c.name,
1161
+ pluginID: c.pluginKey,
1162
+ pluginVersion: c.pluginVersion
1163
+ }))
1164
+ }
1165
+ });
1166
+ } catch (error) {
1167
+ this.logger.error("Failed to list capabilities", error);
1168
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json({
1169
+ status_code: ErrorCodes.EXECUTION_ERROR,
1170
+ error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
1171
+ });
1172
+ }
1099
1173
  }
1100
- async execute(capabilityId, body) {
1174
+ async execute(capabilityId, body, res) {
1101
1175
  try {
1102
1176
  const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
1103
- return {
1177
+ res.status(import_common5.HttpStatus.OK).json({
1104
1178
  status_code: ErrorCodes.SUCCESS,
1105
1179
  data: {
1106
1180
  output: result
1107
1181
  }
1108
- };
1182
+ });
1109
1183
  } catch (error) {
1110
- if (error instanceof CapabilityNotFoundError) {
1111
- throw new import_common5.HttpException({
1112
- status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
1113
- error_msg: `Capability not found: ${capabilityId}`
1114
- }, import_common5.HttpStatus.NOT_FOUND);
1115
- }
1116
- if (error instanceof PluginNotFoundError) {
1117
- throw new import_common5.HttpException({
1118
- status_code: ErrorCodes.PLUGIN_NOT_FOUND,
1119
- error_msg: `Plugin not found`
1120
- }, import_common5.HttpStatus.INTERNAL_SERVER_ERROR);
1121
- }
1122
- if (error instanceof ActionNotFoundError) {
1123
- throw new import_common5.HttpException({
1124
- status_code: ErrorCodes.ACTION_NOT_FOUND,
1125
- error_msg: `Action '${body.action}' not found`
1126
- }, import_common5.HttpStatus.BAD_REQUEST);
1127
- }
1128
- throw new import_common5.HttpException({
1129
- status_code: ErrorCodes.EXECUTION_ERROR,
1130
- error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
1131
- }, import_common5.HttpStatus.INTERNAL_SERVER_ERROR);
1184
+ this.logger.error("Capability execution failed", error);
1185
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1186
+ }
1187
+ }
1188
+ /**
1189
+ * 构建错误响应
1190
+ */
1191
+ buildErrorResponse(error) {
1192
+ if (error instanceof CapabilityNotFoundError) {
1193
+ return {
1194
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
1195
+ error_msg: `Capability not found: ${error.capabilityId}`
1196
+ };
1197
+ }
1198
+ if (error instanceof PluginNotFoundError) {
1199
+ return {
1200
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
1201
+ error_msg: `Plugin not found, please install plugin: ${error.pluginKey}`
1202
+ };
1203
+ }
1204
+ if (error instanceof ActionNotFoundError) {
1205
+ return {
1206
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
1207
+ error_msg: `Action '${error.actionName}' not found in plugin ${error.pluginKey}`
1208
+ };
1132
1209
  }
1210
+ return {
1211
+ status_code: ErrorCodes.EXECUTION_ERROR,
1212
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
1213
+ };
1133
1214
  }
1134
1215
  async executeStream(capabilityId, body, res) {
1135
1216
  const loggerContext = {
@@ -1241,18 +1322,23 @@ var WebhookController = class _WebhookController {
1241
1322
  };
1242
1323
  _ts_decorate5([
1243
1324
  (0, import_common5.Get)("list"),
1325
+ _ts_param3(0, (0, import_common5.Res)()),
1244
1326
  _ts_metadata3("design:type", Function),
1245
- _ts_metadata3("design:paramtypes", []),
1246
- _ts_metadata3("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
1327
+ _ts_metadata3("design:paramtypes", [
1328
+ typeof Response === "undefined" ? Object : Response
1329
+ ]),
1330
+ _ts_metadata3("design:returntype", void 0)
1247
1331
  ], WebhookController.prototype, "list", null);
1248
1332
  _ts_decorate5([
1249
1333
  (0, import_common5.Post)(":capability_id"),
1250
1334
  _ts_param3(0, (0, import_common5.Param)("capability_id")),
1251
1335
  _ts_param3(1, (0, import_common5.Body)()),
1336
+ _ts_param3(2, (0, import_common5.Res)()),
1252
1337
  _ts_metadata3("design:type", Function),
1253
1338
  _ts_metadata3("design:paramtypes", [
1254
1339
  String,
1255
- typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody
1340
+ typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody,
1341
+ typeof Response === "undefined" ? Object : Response
1256
1342
  ]),
1257
1343
  _ts_metadata3("design:returntype", Promise)
1258
1344
  ], WebhookController.prototype, "execute", null);
@@ -1369,6 +1455,7 @@ CapabilityModule = _ts_decorate6([
1369
1455
  PluginLoaderService,
1370
1456
  PluginNotFoundError,
1371
1457
  TemplateEngineService,
1372
- WebhookController
1458
+ WebhookController,
1459
+ migrationAdaptor
1373
1460
  });
1374
1461
  //# sourceMappingURL=index.cjs.map