@lark-apaas/nestjs-capability 0.1.4-beta.5 → 0.1.4-beta.6

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
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  PluginLoadError: () => PluginLoadError,
41
41
  PluginLoaderService: () => PluginLoaderService,
42
42
  PluginNotFoundError: () => PluginNotFoundError,
43
+ TelemetryService: () => TelemetryService,
43
44
  TemplateEngineService: () => TemplateEngineService,
44
45
  WebhookController: () => WebhookController,
45
46
  migrationAdaptor: () => migrationAdaptor
@@ -430,12 +431,100 @@ PluginLoaderService = _ts_decorate2([
430
431
  ], PluginLoaderService);
431
432
 
432
433
  // src/services/capability.service.ts
433
- var import_common3 = require("@nestjs/common");
434
- var import_nestjs_common = require("@lark-apaas/nestjs-common");
434
+ var import_common4 = require("@nestjs/common");
435
+ var import_nestjs_common2 = require("@lark-apaas/nestjs-common");
435
436
  var fs2 = __toESM(require("fs"), 1);
436
437
  var path2 = __toESM(require("path"), 1);
437
438
  var chokidar = __toESM(require("chokidar"), 1);
438
439
 
440
+ // src/services/telemetry.service.ts
441
+ var import_common3 = require("@nestjs/common");
442
+ var import_nestjs_common = require("@lark-apaas/nestjs-common");
443
+ function _ts_decorate3(decorators, target, key, desc) {
444
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
445
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
446
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
447
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
448
+ }
449
+ __name(_ts_decorate3, "_ts_decorate");
450
+ function _ts_metadata(k, v) {
451
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
452
+ }
453
+ __name(_ts_metadata, "_ts_metadata");
454
+ function _ts_param(paramIndex, decorator) {
455
+ return function(target, key) {
456
+ decorator(target, key, paramIndex);
457
+ };
458
+ }
459
+ __name(_ts_param, "_ts_param");
460
+ var TELEMETRY_PATH = "/api/v1/studio/innerapi/resource_events";
461
+ var TelemetryService = class _TelemetryService {
462
+ static {
463
+ __name(this, "TelemetryService");
464
+ }
465
+ client;
466
+ logger = new import_common3.Logger(_TelemetryService.name);
467
+ constructor(client) {
468
+ this.client = client;
469
+ }
470
+ /**
471
+ * 上报事件到平台
472
+ *
473
+ * @param events - 事件列表
474
+ * @returns 是否上报成功
475
+ */
476
+ async reportEvents(events) {
477
+ if (events.length === 0) {
478
+ return true;
479
+ }
480
+ try {
481
+ const response = await this.client.post(TELEMETRY_PATH, {
482
+ events
483
+ });
484
+ if (!response.ok) {
485
+ this.logger.warn(`Failed to report events: ${response.status} ${response.statusText}`);
486
+ return false;
487
+ }
488
+ const result = await response.json();
489
+ if (result.status_code !== "0") {
490
+ this.logger.warn(`API error: ${result.message}`);
491
+ return false;
492
+ }
493
+ this.logger.debug(`Reported ${events.length} event(s) successfully`);
494
+ return true;
495
+ } catch (error) {
496
+ this.logger.warn(`Failed to report events: ${error instanceof Error ? error.message : error}`);
497
+ return false;
498
+ }
499
+ }
500
+ /**
501
+ * 上报插件调用事件
502
+ *
503
+ * @param pluginKey - 插件标识
504
+ * @param details - 调用详情
505
+ */
506
+ async reportCall(pluginKey, details) {
507
+ await this.reportEvents([
508
+ {
509
+ resourceType: "plugin",
510
+ resourceKey: pluginKey,
511
+ eventType: "call",
512
+ details: {
513
+ ...details
514
+ }
515
+ }
516
+ ]);
517
+ }
518
+ };
519
+ TelemetryService = _ts_decorate3([
520
+ (0, import_common3.Injectable)(),
521
+ _ts_param(0, (0, import_common3.Inject)(import_nestjs_common.PLATFORM_HTTP_CLIENT)),
522
+ _ts_metadata("design:type", Function),
523
+ _ts_metadata("design:paramtypes", [
524
+ typeof PlatformHttpClient === "undefined" ? Object : PlatformHttpClient
525
+ ])
526
+ ], TelemetryService);
527
+
439
528
  // src/utils/log-utils.ts
440
529
  var DEFAULT_MAX_LENGTH = 1e3;
441
530
  function truncateString(str, maxLength) {
@@ -494,23 +583,23 @@ async function migrationAdaptor(promise) {
494
583
  __name(migrationAdaptor, "migrationAdaptor");
495
584
 
496
585
  // src/services/capability.service.ts
497
- function _ts_decorate3(decorators, target, key, desc) {
586
+ function _ts_decorate4(decorators, target, key, desc) {
498
587
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
499
588
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
500
589
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
501
590
  return c > 3 && r && Object.defineProperty(target, key, r), r;
502
591
  }
503
- __name(_ts_decorate3, "_ts_decorate");
504
- function _ts_metadata(k, v) {
592
+ __name(_ts_decorate4, "_ts_decorate");
593
+ function _ts_metadata2(k, v) {
505
594
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
506
595
  }
507
- __name(_ts_metadata, "_ts_metadata");
508
- function _ts_param(paramIndex, decorator) {
596
+ __name(_ts_metadata2, "_ts_metadata");
597
+ function _ts_param2(paramIndex, decorator) {
509
598
  return function(target, key) {
510
599
  decorator(target, key, paramIndex);
511
600
  };
512
601
  }
513
- __name(_ts_param, "_ts_param");
602
+ __name(_ts_param2, "_ts_param");
514
603
  var CapabilityNotFoundError = class extends Error {
515
604
  static {
516
605
  __name(this, "CapabilityNotFoundError");
@@ -543,18 +632,20 @@ var CapabilityService = class _CapabilityService {
543
632
  httpClientFactory;
544
633
  pluginLoaderService;
545
634
  templateEngineService;
546
- logger = new import_common3.Logger(_CapabilityService.name);
635
+ telemetryService;
636
+ logger = new import_common4.Logger(_CapabilityService.name);
547
637
  capabilities = /* @__PURE__ */ new Map();
548
638
  /** 文件路径到 capability id 的映射,用于文件删除时查找 */
549
639
  filePathToId = /* @__PURE__ */ new Map();
550
640
  capabilitiesDir;
551
641
  fileWatcher = null;
552
642
  options = {};
553
- constructor(requestContextService, httpClientFactory, pluginLoaderService, templateEngineService) {
643
+ constructor(requestContextService, httpClientFactory, pluginLoaderService, templateEngineService, telemetryService) {
554
644
  this.requestContextService = requestContextService;
555
645
  this.httpClientFactory = httpClientFactory;
556
646
  this.pluginLoaderService = pluginLoaderService;
557
647
  this.templateEngineService = templateEngineService;
648
+ this.telemetryService = telemetryService;
558
649
  const isDev = process.env.NODE_ENV === "development";
559
650
  this.capabilitiesDir = path2.join(process.cwd(), isDev ? "server/capabilities" : "capabilities");
560
651
  }
@@ -787,13 +878,14 @@ var CapabilityService = class _CapabilityService {
787
878
  plugin_key: config.pluginKey,
788
879
  action: actionName
789
880
  };
881
+ let isStream = false;
790
882
  try {
791
883
  const { pluginInstance, resolvedParams } = await this.loadPluginAndResolveParams(config, input);
792
884
  if (!pluginInstance.hasAction(actionName)) {
793
885
  throw new ActionNotFoundError(config.pluginKey, actionName);
794
886
  }
795
887
  const context = this.buildActionContext(config.pluginKey, contextOverride);
796
- const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
888
+ isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
797
889
  this.logger.log("Executing capability (call)", {
798
890
  ...loggerContext,
799
891
  is_stream: isStream,
@@ -809,18 +901,23 @@ var CapabilityService = class _CapabilityService {
809
901
  } else {
810
902
  result = await pluginInstance.run(actionName, context, resolvedParams);
811
903
  }
904
+ const durationMs = Date.now() - startTime;
812
905
  this.logger.log("Capability (call) executed successfully", {
813
906
  ...loggerContext,
814
- duration_ms: Date.now() - startTime,
907
+ duration_ms: durationMs,
815
908
  output: stringifyForLog(result)
816
909
  });
910
+ this.reportCallEvent(config.pluginKey, true, durationMs, isStream, contextOverride);
817
911
  return result;
818
912
  } catch (error) {
913
+ const durationMs = Date.now() - startTime;
819
914
  this.logger.error("Capability (call) execution failed", {
820
915
  ...loggerContext,
821
- duration_ms: Date.now() - startTime,
916
+ duration_ms: durationMs,
822
917
  error: error instanceof Error ? error.message : String(error)
823
918
  });
919
+ const errorCode = String(error?.code ?? "EXECUTION_ERROR");
920
+ this.reportCallEvent(config.pluginKey, false, durationMs, isStream, contextOverride, errorCode);
824
921
  throw error;
825
922
  }
826
923
  }
@@ -859,18 +956,23 @@ var CapabilityService = class _CapabilityService {
859
956
  chunks.push(result);
860
957
  yield result;
861
958
  }
959
+ const durationMs = Date.now() - startTime;
862
960
  const aggregatedResult = pluginInstance.aggregate ? pluginInstance.aggregate(actionName, chunks) : chunks;
863
961
  this.logger.log("Capability (stream) executed successfully", {
864
962
  ...loggerContext,
865
- duration_ms: Date.now() - startTime,
963
+ duration_ms: durationMs,
866
964
  output: stringifyForLog(aggregatedResult)
867
965
  });
966
+ this.reportCallEvent(config.pluginKey, true, durationMs, isStream, contextOverride);
868
967
  } catch (error) {
968
+ const durationMs = Date.now() - startTime;
869
969
  this.logger.error("Capability (stream) execution failed", {
870
970
  ...loggerContext,
871
- duration_ms: Date.now() - startTime,
971
+ duration_ms: durationMs,
872
972
  error: error instanceof Error ? error.message : String(error)
873
973
  });
974
+ const errorCode = String(error?.code ?? "EXECUTION_ERROR");
975
+ this.reportCallEvent(config.pluginKey, false, durationMs, true, contextOverride, errorCode);
874
976
  throw error;
875
977
  }
876
978
  }
@@ -906,6 +1008,8 @@ var CapabilityService = class _CapabilityService {
906
1008
  yield event;
907
1009
  } else if (event.type === "done") {
908
1010
  const aggregatedResult2 = pluginInstance.aggregate ? pluginInstance.aggregate(actionName, chunks) : chunks;
1011
+ const durationMs = Date.now() - startTime;
1012
+ this.reportCallEvent(config.pluginKey, true, durationMs, isStream, contextOverride);
909
1013
  yield {
910
1014
  type: "done",
911
1015
  metadata: {
@@ -935,11 +1039,13 @@ var CapabilityService = class _CapabilityService {
935
1039
  };
936
1040
  }
937
1041
  const aggregatedResult2 = pluginInstance.aggregate ? pluginInstance.aggregate(actionName, chunks) : chunks;
1042
+ const durationMs = Date.now() - startTime;
1043
+ this.reportCallEvent(config.pluginKey, true, durationMs, isStream, contextOverride);
938
1044
  yield {
939
1045
  type: "done",
940
1046
  metadata: {
941
1047
  chunks: chunks.length,
942
- duration: Date.now() - startTime,
1048
+ duration: durationMs,
943
1049
  aggregated: aggregatedResult2
944
1050
  }
945
1051
  };
@@ -951,11 +1057,14 @@ var CapabilityService = class _CapabilityService {
951
1057
  output: stringifyForLog(aggregatedResult)
952
1058
  });
953
1059
  } catch (error) {
1060
+ const durationMs = Date.now() - startTime;
954
1061
  this.logger.error("Capability (streamWithEvents) execution failed", {
955
1062
  ...loggerContext,
956
- duration_ms: Date.now() - startTime,
1063
+ duration_ms: durationMs,
957
1064
  error: error instanceof Error ? error.message : String(error)
958
1065
  });
1066
+ const errorCode = String(error?.code ?? "EXECUTION_ERROR");
1067
+ this.reportCallEvent(config.pluginKey, false, durationMs, true, contextOverride, errorCode);
959
1068
  yield {
960
1069
  type: "error",
961
1070
  error: this.extractErrorInfo(error)
@@ -963,6 +1072,22 @@ var CapabilityService = class _CapabilityService {
963
1072
  }
964
1073
  }
965
1074
  /**
1075
+ * 上报调用事件(fire-and-forget)
1076
+ */
1077
+ reportCallEvent(pluginKey, success, durationMs, isStream, contextOverride, statusCode = "0") {
1078
+ const callerType = contextOverride?.caller ?? (contextOverride?.isDebug ? "debug" : "server");
1079
+ const manifest = this.pluginLoaderService.getManifest(pluginKey);
1080
+ this.telemetryService.reportCall(pluginKey, {
1081
+ version: manifest?.version ?? "",
1082
+ success: String(success),
1083
+ duration: String(durationMs),
1084
+ is_stream: String(isStream),
1085
+ caller_type: String(callerType),
1086
+ status_code: statusCode
1087
+ }).catch(() => {
1088
+ });
1089
+ }
1090
+ /**
966
1091
  * 从错误对象提取错误信息
967
1092
  * 支持 PluginError 和 RateLimitError
968
1093
  */
@@ -989,7 +1114,8 @@ var CapabilityService = class _CapabilityService {
989
1114
  logger: this.logger,
990
1115
  platformHttpClient: this.createPluginHttpClient(pluginKey),
991
1116
  userContext: override?.userContext ?? this.getUserContext(),
992
- isDebug: override?.isDebug ?? false
1117
+ isDebug: override?.isDebug ?? false,
1118
+ caller: override?.caller
993
1119
  };
994
1120
  }
995
1121
  /**
@@ -1030,38 +1156,39 @@ var CapabilityService = class _CapabilityService {
1030
1156
  };
1031
1157
  }
1032
1158
  };
1033
- CapabilityService = _ts_decorate3([
1034
- (0, import_common3.Injectable)(),
1035
- _ts_param(1, (0, import_common3.Inject)(import_nestjs_common.HTTP_CLIENT_FACTORY)),
1036
- _ts_metadata("design:type", Function),
1037
- _ts_metadata("design:paramtypes", [
1038
- typeof import_nestjs_common.RequestContextService === "undefined" ? Object : import_nestjs_common.RequestContextService,
1159
+ CapabilityService = _ts_decorate4([
1160
+ (0, import_common4.Injectable)(),
1161
+ _ts_param2(1, (0, import_common4.Inject)(import_nestjs_common2.HTTP_CLIENT_FACTORY)),
1162
+ _ts_metadata2("design:type", Function),
1163
+ _ts_metadata2("design:paramtypes", [
1164
+ typeof import_nestjs_common2.RequestContextService === "undefined" ? Object : import_nestjs_common2.RequestContextService,
1039
1165
  typeof HttpClientFactory === "undefined" ? Object : HttpClientFactory,
1040
1166
  typeof PluginLoaderService === "undefined" ? Object : PluginLoaderService,
1041
- typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService
1167
+ typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService,
1168
+ typeof TelemetryService === "undefined" ? Object : TelemetryService
1042
1169
  ])
1043
1170
  ], CapabilityService);
1044
1171
 
1045
1172
  // src/controllers/debug.controller.ts
1046
- var import_common4 = require("@nestjs/common");
1173
+ var import_common5 = require("@nestjs/common");
1047
1174
  var import_swagger = require("@nestjs/swagger");
1048
- function _ts_decorate4(decorators, target, key, desc) {
1175
+ function _ts_decorate5(decorators, target, key, desc) {
1049
1176
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1050
1177
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1051
1178
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1052
1179
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1053
1180
  }
1054
- __name(_ts_decorate4, "_ts_decorate");
1055
- function _ts_metadata2(k, v) {
1181
+ __name(_ts_decorate5, "_ts_decorate");
1182
+ function _ts_metadata3(k, v) {
1056
1183
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1057
1184
  }
1058
- __name(_ts_metadata2, "_ts_metadata");
1059
- function _ts_param2(paramIndex, decorator) {
1185
+ __name(_ts_metadata3, "_ts_metadata");
1186
+ function _ts_param3(paramIndex, decorator) {
1060
1187
  return function(target, key) {
1061
1188
  decorator(target, key, paramIndex);
1062
1189
  };
1063
1190
  }
1064
- __name(_ts_param2, "_ts_param");
1191
+ __name(_ts_param3, "_ts_param");
1065
1192
  var DebugController = class _DebugController {
1066
1193
  static {
1067
1194
  __name(this, "DebugController");
@@ -1069,7 +1196,7 @@ var DebugController = class _DebugController {
1069
1196
  capabilityService;
1070
1197
  pluginLoaderService;
1071
1198
  templateEngineService;
1072
- logger = new import_common4.Logger(_DebugController.name);
1199
+ logger = new import_common5.Logger(_DebugController.name);
1073
1200
  constructor(capabilityService, pluginLoaderService, templateEngineService) {
1074
1201
  this.capabilityService = capabilityService;
1075
1202
  this.pluginLoaderService = pluginLoaderService;
@@ -1078,7 +1205,7 @@ var DebugController = class _DebugController {
1078
1205
  list(res) {
1079
1206
  try {
1080
1207
  const capabilities = this.capabilityService.listCapabilities();
1081
- res.status(import_common4.HttpStatus.OK).json({
1208
+ res.status(import_common5.HttpStatus.OK).json({
1082
1209
  status_code: ErrorCodes.SUCCESS,
1083
1210
  data: {
1084
1211
  capabilities: capabilities.map((c) => ({
@@ -1091,7 +1218,7 @@ var DebugController = class _DebugController {
1091
1218
  });
1092
1219
  } catch (error) {
1093
1220
  this.logger.error("Failed to list capabilities", error);
1094
- res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json({
1221
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json({
1095
1222
  status_code: ErrorCodes.EXECUTION_ERROR,
1096
1223
  error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
1097
1224
  });
@@ -1135,9 +1262,10 @@ var DebugController = class _DebugController {
1135
1262
  const action = await this.getActionName(config.pluginKey, config.formValue, body.action);
1136
1263
  const resolvedParams = this.templateEngineService.resolve(config.formValue, params, config.paramsSchema);
1137
1264
  const result = await this.capabilityService.loadWithConfig(config).call(action, params, {
1138
- isDebug: true
1265
+ isDebug: true,
1266
+ caller: "debug"
1139
1267
  });
1140
- res.status(import_common4.HttpStatus.OK).json({
1268
+ res.status(import_common5.HttpStatus.OK).json({
1141
1269
  status_code: ErrorCodes.SUCCESS,
1142
1270
  data: {
1143
1271
  output: result,
@@ -1152,7 +1280,7 @@ var DebugController = class _DebugController {
1152
1280
  });
1153
1281
  } catch (error) {
1154
1282
  this.logger.error("Debug execution failed", error);
1155
- res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1283
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1156
1284
  }
1157
1285
  }
1158
1286
  /**
@@ -1209,7 +1337,8 @@ var DebugController = class _DebugController {
1209
1337
  });
1210
1338
  const capability = this.capabilityService.loadWithConfig(config);
1211
1339
  const eventStream = capability.callStreamWithEvents(action, params, {
1212
- isDebug: true
1340
+ isDebug: true,
1341
+ caller: "debug"
1213
1342
  });
1214
1343
  let pendingChunk = null;
1215
1344
  for await (const event of eventStream) {
@@ -1308,46 +1437,46 @@ var DebugController = class _DebugController {
1308
1437
  }
1309
1438
  }
1310
1439
  };
1311
- _ts_decorate4([
1312
- (0, import_common4.Get)("list"),
1313
- _ts_param2(0, (0, import_common4.Res)()),
1314
- _ts_metadata2("design:type", Function),
1315
- _ts_metadata2("design:paramtypes", [
1440
+ _ts_decorate5([
1441
+ (0, import_common5.Get)("list"),
1442
+ _ts_param3(0, (0, import_common5.Res)()),
1443
+ _ts_metadata3("design:type", Function),
1444
+ _ts_metadata3("design:paramtypes", [
1316
1445
  typeof Response === "undefined" ? Object : Response
1317
1446
  ]),
1318
- _ts_metadata2("design:returntype", void 0)
1447
+ _ts_metadata3("design:returntype", void 0)
1319
1448
  ], DebugController.prototype, "list", null);
1320
- _ts_decorate4([
1321
- (0, import_common4.Post)("debug/:capability_id"),
1322
- _ts_param2(0, (0, import_common4.Param)("capability_id")),
1323
- _ts_param2(1, (0, import_common4.Body)()),
1324
- _ts_param2(2, (0, import_common4.Res)()),
1325
- _ts_metadata2("design:type", Function),
1326
- _ts_metadata2("design:paramtypes", [
1449
+ _ts_decorate5([
1450
+ (0, import_common5.Post)("debug/:capability_id"),
1451
+ _ts_param3(0, (0, import_common5.Param)("capability_id")),
1452
+ _ts_param3(1, (0, import_common5.Body)()),
1453
+ _ts_param3(2, (0, import_common5.Res)()),
1454
+ _ts_metadata3("design:type", Function),
1455
+ _ts_metadata3("design:paramtypes", [
1327
1456
  String,
1328
1457
  typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody,
1329
1458
  typeof Response === "undefined" ? Object : Response
1330
1459
  ]),
1331
- _ts_metadata2("design:returntype", Promise)
1460
+ _ts_metadata3("design:returntype", Promise)
1332
1461
  ], DebugController.prototype, "debug", null);
1333
- _ts_decorate4([
1334
- (0, import_common4.Post)("debug/:capability_id/stream"),
1335
- _ts_param2(0, (0, import_common4.Param)("capability_id")),
1336
- _ts_param2(1, (0, import_common4.Body)()),
1337
- _ts_param2(2, (0, import_common4.Res)()),
1338
- _ts_metadata2("design:type", Function),
1339
- _ts_metadata2("design:paramtypes", [
1462
+ _ts_decorate5([
1463
+ (0, import_common5.Post)("debug/:capability_id/stream"),
1464
+ _ts_param3(0, (0, import_common5.Param)("capability_id")),
1465
+ _ts_param3(1, (0, import_common5.Body)()),
1466
+ _ts_param3(2, (0, import_common5.Res)()),
1467
+ _ts_metadata3("design:type", Function),
1468
+ _ts_metadata3("design:paramtypes", [
1340
1469
  String,
1341
1470
  typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody,
1342
1471
  typeof Response === "undefined" ? Object : Response
1343
1472
  ]),
1344
- _ts_metadata2("design:returntype", Promise)
1473
+ _ts_metadata3("design:returntype", Promise)
1345
1474
  ], DebugController.prototype, "debugStream", null);
1346
- DebugController = _ts_decorate4([
1475
+ DebugController = _ts_decorate5([
1347
1476
  (0, import_swagger.ApiExcludeController)(),
1348
- (0, import_common4.Controller)("__innerapi__/capability"),
1349
- _ts_metadata2("design:type", Function),
1350
- _ts_metadata2("design:paramtypes", [
1477
+ (0, import_common5.Controller)("__innerapi__/capability"),
1478
+ _ts_metadata3("design:type", Function),
1479
+ _ts_metadata3("design:paramtypes", [
1351
1480
  typeof CapabilityService === "undefined" ? Object : CapabilityService,
1352
1481
  typeof PluginLoaderService === "undefined" ? Object : PluginLoaderService,
1353
1482
  typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService
@@ -1355,38 +1484,38 @@ DebugController = _ts_decorate4([
1355
1484
  ], DebugController);
1356
1485
 
1357
1486
  // src/controllers/webhook.controller.ts
1358
- var import_common5 = require("@nestjs/common");
1487
+ var import_common6 = require("@nestjs/common");
1359
1488
  var import_swagger2 = require("@nestjs/swagger");
1360
- function _ts_decorate5(decorators, target, key, desc) {
1489
+ function _ts_decorate6(decorators, target, key, desc) {
1361
1490
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1362
1491
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1363
1492
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1364
1493
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1365
1494
  }
1366
- __name(_ts_decorate5, "_ts_decorate");
1367
- function _ts_metadata3(k, v) {
1495
+ __name(_ts_decorate6, "_ts_decorate");
1496
+ function _ts_metadata4(k, v) {
1368
1497
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1369
1498
  }
1370
- __name(_ts_metadata3, "_ts_metadata");
1371
- function _ts_param3(paramIndex, decorator) {
1499
+ __name(_ts_metadata4, "_ts_metadata");
1500
+ function _ts_param4(paramIndex, decorator) {
1372
1501
  return function(target, key) {
1373
1502
  decorator(target, key, paramIndex);
1374
1503
  };
1375
1504
  }
1376
- __name(_ts_param3, "_ts_param");
1505
+ __name(_ts_param4, "_ts_param");
1377
1506
  var WebhookController = class _WebhookController {
1378
1507
  static {
1379
1508
  __name(this, "WebhookController");
1380
1509
  }
1381
1510
  capabilityService;
1382
- logger = new import_common5.Logger(_WebhookController.name);
1511
+ logger = new import_common6.Logger(_WebhookController.name);
1383
1512
  constructor(capabilityService) {
1384
1513
  this.capabilityService = capabilityService;
1385
1514
  }
1386
1515
  list(res) {
1387
1516
  try {
1388
1517
  const capabilities = this.capabilityService.listCapabilities();
1389
- res.status(import_common5.HttpStatus.OK).json({
1518
+ res.status(import_common6.HttpStatus.OK).json({
1390
1519
  status_code: ErrorCodes.SUCCESS,
1391
1520
  data: {
1392
1521
  capabilities: capabilities.map((c) => ({
@@ -1399,7 +1528,7 @@ var WebhookController = class _WebhookController {
1399
1528
  });
1400
1529
  } catch (error) {
1401
1530
  this.logger.error("Failed to list capabilities", error);
1402
- res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json({
1531
+ res.status(import_common6.HttpStatus.INTERNAL_SERVER_ERROR).json({
1403
1532
  status_code: ErrorCodes.EXECUTION_ERROR,
1404
1533
  error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
1405
1534
  });
@@ -1407,8 +1536,10 @@ var WebhookController = class _WebhookController {
1407
1536
  }
1408
1537
  async execute(capabilityId, body, res) {
1409
1538
  try {
1410
- const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
1411
- res.status(import_common5.HttpStatus.OK).json({
1539
+ const result = await this.capabilityService.load(capabilityId).call(body.action, body.params, {
1540
+ caller: "client"
1541
+ });
1542
+ res.status(import_common6.HttpStatus.OK).json({
1412
1543
  status_code: ErrorCodes.SUCCESS,
1413
1544
  data: {
1414
1545
  output: result
@@ -1416,7 +1547,7 @@ var WebhookController = class _WebhookController {
1416
1547
  });
1417
1548
  } catch (error) {
1418
1549
  this.logger.error("Capability execution failed", error);
1419
- res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1550
+ res.status(import_common6.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1420
1551
  }
1421
1552
  }
1422
1553
  /**
@@ -1468,7 +1599,9 @@ var WebhookController = class _WebhookController {
1468
1599
  input: stringifyForLog(body.params)
1469
1600
  });
1470
1601
  const capability = this.capabilityService.load(capabilityId);
1471
- const eventStream = capability.callStreamWithEvents(body.action, body.params);
1602
+ const eventStream = capability.callStreamWithEvents(body.action, body.params, {
1603
+ caller: "client"
1604
+ });
1472
1605
  let pendingChunk = null;
1473
1606
  for await (const event of eventStream) {
1474
1607
  switch (event.type) {
@@ -1566,60 +1699,60 @@ var WebhookController = class _WebhookController {
1566
1699
  }
1567
1700
  }
1568
1701
  };
1569
- _ts_decorate5([
1570
- (0, import_common5.Get)("list"),
1571
- _ts_param3(0, (0, import_common5.Res)()),
1572
- _ts_metadata3("design:type", Function),
1573
- _ts_metadata3("design:paramtypes", [
1702
+ _ts_decorate6([
1703
+ (0, import_common6.Get)("list"),
1704
+ _ts_param4(0, (0, import_common6.Res)()),
1705
+ _ts_metadata4("design:type", Function),
1706
+ _ts_metadata4("design:paramtypes", [
1574
1707
  typeof Response === "undefined" ? Object : Response
1575
1708
  ]),
1576
- _ts_metadata3("design:returntype", void 0)
1709
+ _ts_metadata4("design:returntype", void 0)
1577
1710
  ], WebhookController.prototype, "list", null);
1578
- _ts_decorate5([
1579
- (0, import_common5.Post)(":capability_id"),
1580
- _ts_param3(0, (0, import_common5.Param)("capability_id")),
1581
- _ts_param3(1, (0, import_common5.Body)()),
1582
- _ts_param3(2, (0, import_common5.Res)()),
1583
- _ts_metadata3("design:type", Function),
1584
- _ts_metadata3("design:paramtypes", [
1711
+ _ts_decorate6([
1712
+ (0, import_common6.Post)(":capability_id"),
1713
+ _ts_param4(0, (0, import_common6.Param)("capability_id")),
1714
+ _ts_param4(1, (0, import_common6.Body)()),
1715
+ _ts_param4(2, (0, import_common6.Res)()),
1716
+ _ts_metadata4("design:type", Function),
1717
+ _ts_metadata4("design:paramtypes", [
1585
1718
  String,
1586
1719
  typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody,
1587
1720
  typeof Response === "undefined" ? Object : Response
1588
1721
  ]),
1589
- _ts_metadata3("design:returntype", Promise)
1722
+ _ts_metadata4("design:returntype", Promise)
1590
1723
  ], WebhookController.prototype, "execute", null);
1591
- _ts_decorate5([
1592
- (0, import_common5.Post)(":capability_id/stream"),
1593
- _ts_param3(0, (0, import_common5.Param)("capability_id")),
1594
- _ts_param3(1, (0, import_common5.Body)()),
1595
- _ts_param3(2, (0, import_common5.Res)()),
1596
- _ts_metadata3("design:type", Function),
1597
- _ts_metadata3("design:paramtypes", [
1724
+ _ts_decorate6([
1725
+ (0, import_common6.Post)(":capability_id/stream"),
1726
+ _ts_param4(0, (0, import_common6.Param)("capability_id")),
1727
+ _ts_param4(1, (0, import_common6.Body)()),
1728
+ _ts_param4(2, (0, import_common6.Res)()),
1729
+ _ts_metadata4("design:type", Function),
1730
+ _ts_metadata4("design:paramtypes", [
1598
1731
  String,
1599
1732
  typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody,
1600
1733
  typeof Response === "undefined" ? Object : Response
1601
1734
  ]),
1602
- _ts_metadata3("design:returntype", Promise)
1735
+ _ts_metadata4("design:returntype", Promise)
1603
1736
  ], WebhookController.prototype, "executeStream", null);
1604
- WebhookController = _ts_decorate5([
1737
+ WebhookController = _ts_decorate6([
1605
1738
  (0, import_swagger2.ApiExcludeController)(),
1606
- (0, import_common5.Controller)("api/capability"),
1607
- _ts_metadata3("design:type", Function),
1608
- _ts_metadata3("design:paramtypes", [
1739
+ (0, import_common6.Controller)("api/capability"),
1740
+ _ts_metadata4("design:type", Function),
1741
+ _ts_metadata4("design:paramtypes", [
1609
1742
  typeof CapabilityService === "undefined" ? Object : CapabilityService
1610
1743
  ])
1611
1744
  ], WebhookController);
1612
1745
 
1613
1746
  // src/capability.module.ts
1614
- var import_common6 = require("@nestjs/common");
1615
- var import_nestjs_common2 = require("@lark-apaas/nestjs-common");
1616
- function _ts_decorate6(decorators, target, key, desc) {
1747
+ var import_common7 = require("@nestjs/common");
1748
+ var import_nestjs_common3 = require("@lark-apaas/nestjs-common");
1749
+ function _ts_decorate7(decorators, target, key, desc) {
1617
1750
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1618
1751
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1619
1752
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1620
1753
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1621
1754
  }
1622
- __name(_ts_decorate6, "_ts_decorate");
1755
+ __name(_ts_decorate7, "_ts_decorate");
1623
1756
  var CAPABILITY_OPTIONS = /* @__PURE__ */ Symbol("CAPABILITY_OPTIONS");
1624
1757
  var isDevelopment = process.env.NODE_ENV === "development";
1625
1758
  function getControllers() {
@@ -1640,7 +1773,7 @@ var CapabilityModule = class _CapabilityModule {
1640
1773
  return {
1641
1774
  module: _CapabilityModule,
1642
1775
  imports: [
1643
- import_nestjs_common2.CommonModule
1776
+ import_nestjs_common3.CommonModule
1644
1777
  ],
1645
1778
  controllers: getControllers(),
1646
1779
  providers: [
@@ -1650,43 +1783,48 @@ var CapabilityModule = class _CapabilityModule {
1650
1783
  },
1651
1784
  {
1652
1785
  provide: CapabilityService,
1653
- useFactory: /* @__PURE__ */ __name((requestContextService, httpClientFactory, pluginLoader, templateEngine, moduleOptions) => {
1654
- const service = new CapabilityService(requestContextService, httpClientFactory, pluginLoader, templateEngine);
1786
+ useFactory: /* @__PURE__ */ __name((requestContextService, httpClientFactory, pluginLoader, templateEngine, telemetryService, moduleOptions) => {
1787
+ const service = new CapabilityService(requestContextService, httpClientFactory, pluginLoader, templateEngine, telemetryService);
1655
1788
  if (moduleOptions) {
1656
1789
  service.setOptions(moduleOptions);
1657
1790
  }
1658
1791
  return service;
1659
1792
  }, "useFactory"),
1660
1793
  inject: [
1661
- import_nestjs_common2.RequestContextService,
1662
- import_nestjs_common2.HTTP_CLIENT_FACTORY,
1794
+ import_nestjs_common3.RequestContextService,
1795
+ import_nestjs_common3.HTTP_CLIENT_FACTORY,
1663
1796
  PluginLoaderService,
1664
1797
  TemplateEngineService,
1798
+ TelemetryService,
1665
1799
  CAPABILITY_OPTIONS
1666
1800
  ]
1667
1801
  },
1668
1802
  PluginLoaderService,
1669
- TemplateEngineService
1803
+ TemplateEngineService,
1804
+ TelemetryService
1670
1805
  ],
1671
1806
  exports: [
1672
- CapabilityService
1807
+ CapabilityService,
1808
+ TelemetryService
1673
1809
  ]
1674
1810
  };
1675
1811
  }
1676
1812
  };
1677
- CapabilityModule = _ts_decorate6([
1678
- (0, import_common6.Module)({
1813
+ CapabilityModule = _ts_decorate7([
1814
+ (0, import_common7.Module)({
1679
1815
  imports: [
1680
- import_nestjs_common2.CommonModule
1816
+ import_nestjs_common3.CommonModule
1681
1817
  ],
1682
1818
  controllers: getControllers(),
1683
1819
  providers: [
1684
1820
  CapabilityService,
1685
1821
  PluginLoaderService,
1686
- TemplateEngineService
1822
+ TemplateEngineService,
1823
+ TelemetryService
1687
1824
  ],
1688
1825
  exports: [
1689
- CapabilityService
1826
+ CapabilityService,
1827
+ TelemetryService
1690
1828
  ]
1691
1829
  })
1692
1830
  ], CapabilityModule);
@@ -1701,6 +1839,7 @@ CapabilityModule = _ts_decorate6([
1701
1839
  PluginLoadError,
1702
1840
  PluginLoaderService,
1703
1841
  PluginNotFoundError,
1842
+ TelemetryService,
1704
1843
  TemplateEngineService,
1705
1844
  WebhookController,
1706
1845
  migrationAdaptor