@lark-apaas/nestjs-capability 0.1.5 → 0.1.7

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
  /**
@@ -1026,42 +1152,44 @@ var CapabilityService = class _CapabilityService {
1026
1152
  return {
1027
1153
  appId: ctx?.appId ?? "",
1028
1154
  userId: ctx?.userId ?? "",
1029
- tenantId: ctx?.tenantId ?? ""
1155
+ tenantId: ctx?.tenantId ?? "",
1156
+ isSystemAccount: ctx?.isSystemAccount ?? false
1030
1157
  };
1031
1158
  }
1032
1159
  };
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,
1160
+ CapabilityService = _ts_decorate4([
1161
+ (0, import_common4.Injectable)(),
1162
+ _ts_param2(1, (0, import_common4.Inject)(import_nestjs_common2.HTTP_CLIENT_FACTORY)),
1163
+ _ts_metadata2("design:type", Function),
1164
+ _ts_metadata2("design:paramtypes", [
1165
+ typeof import_nestjs_common2.RequestContextService === "undefined" ? Object : import_nestjs_common2.RequestContextService,
1039
1166
  typeof HttpClientFactory === "undefined" ? Object : HttpClientFactory,
1040
1167
  typeof PluginLoaderService === "undefined" ? Object : PluginLoaderService,
1041
- typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService
1168
+ typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService,
1169
+ typeof TelemetryService === "undefined" ? Object : TelemetryService
1042
1170
  ])
1043
1171
  ], CapabilityService);
1044
1172
 
1045
1173
  // src/controllers/debug.controller.ts
1046
- var import_common4 = require("@nestjs/common");
1174
+ var import_common5 = require("@nestjs/common");
1047
1175
  var import_swagger = require("@nestjs/swagger");
1048
- function _ts_decorate4(decorators, target, key, desc) {
1176
+ function _ts_decorate5(decorators, target, key, desc) {
1049
1177
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1050
1178
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1051
1179
  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
1180
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1053
1181
  }
1054
- __name(_ts_decorate4, "_ts_decorate");
1055
- function _ts_metadata2(k, v) {
1182
+ __name(_ts_decorate5, "_ts_decorate");
1183
+ function _ts_metadata3(k, v) {
1056
1184
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1057
1185
  }
1058
- __name(_ts_metadata2, "_ts_metadata");
1059
- function _ts_param2(paramIndex, decorator) {
1186
+ __name(_ts_metadata3, "_ts_metadata");
1187
+ function _ts_param3(paramIndex, decorator) {
1060
1188
  return function(target, key) {
1061
1189
  decorator(target, key, paramIndex);
1062
1190
  };
1063
1191
  }
1064
- __name(_ts_param2, "_ts_param");
1192
+ __name(_ts_param3, "_ts_param");
1065
1193
  var DebugController = class _DebugController {
1066
1194
  static {
1067
1195
  __name(this, "DebugController");
@@ -1069,7 +1197,7 @@ var DebugController = class _DebugController {
1069
1197
  capabilityService;
1070
1198
  pluginLoaderService;
1071
1199
  templateEngineService;
1072
- logger = new import_common4.Logger(_DebugController.name);
1200
+ logger = new import_common5.Logger(_DebugController.name);
1073
1201
  constructor(capabilityService, pluginLoaderService, templateEngineService) {
1074
1202
  this.capabilityService = capabilityService;
1075
1203
  this.pluginLoaderService = pluginLoaderService;
@@ -1078,7 +1206,7 @@ var DebugController = class _DebugController {
1078
1206
  list(res) {
1079
1207
  try {
1080
1208
  const capabilities = this.capabilityService.listCapabilities();
1081
- res.status(import_common4.HttpStatus.OK).json({
1209
+ res.status(import_common5.HttpStatus.OK).json({
1082
1210
  status_code: ErrorCodes.SUCCESS,
1083
1211
  data: {
1084
1212
  capabilities: capabilities.map((c) => ({
@@ -1091,7 +1219,7 @@ var DebugController = class _DebugController {
1091
1219
  });
1092
1220
  } catch (error) {
1093
1221
  this.logger.error("Failed to list capabilities", error);
1094
- res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json({
1222
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json({
1095
1223
  status_code: ErrorCodes.EXECUTION_ERROR,
1096
1224
  error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
1097
1225
  });
@@ -1135,9 +1263,10 @@ var DebugController = class _DebugController {
1135
1263
  const action = await this.getActionName(config.pluginKey, config.formValue, body.action);
1136
1264
  const resolvedParams = this.templateEngineService.resolve(config.formValue, params, config.paramsSchema);
1137
1265
  const result = await this.capabilityService.loadWithConfig(config).call(action, params, {
1138
- isDebug: true
1266
+ isDebug: true,
1267
+ caller: "debug"
1139
1268
  });
1140
- res.status(import_common4.HttpStatus.OK).json({
1269
+ res.status(import_common5.HttpStatus.OK).json({
1141
1270
  status_code: ErrorCodes.SUCCESS,
1142
1271
  data: {
1143
1272
  output: result,
@@ -1152,7 +1281,7 @@ var DebugController = class _DebugController {
1152
1281
  });
1153
1282
  } catch (error) {
1154
1283
  this.logger.error("Debug execution failed", error);
1155
- res.status(import_common4.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1284
+ res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1156
1285
  }
1157
1286
  }
1158
1287
  /**
@@ -1209,7 +1338,8 @@ var DebugController = class _DebugController {
1209
1338
  });
1210
1339
  const capability = this.capabilityService.loadWithConfig(config);
1211
1340
  const eventStream = capability.callStreamWithEvents(action, params, {
1212
- isDebug: true
1341
+ isDebug: true,
1342
+ caller: "debug"
1213
1343
  });
1214
1344
  let pendingChunk = null;
1215
1345
  for await (const event of eventStream) {
@@ -1308,46 +1438,46 @@ var DebugController = class _DebugController {
1308
1438
  }
1309
1439
  }
1310
1440
  };
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", [
1441
+ _ts_decorate5([
1442
+ (0, import_common5.Get)("list"),
1443
+ _ts_param3(0, (0, import_common5.Res)()),
1444
+ _ts_metadata3("design:type", Function),
1445
+ _ts_metadata3("design:paramtypes", [
1316
1446
  typeof Response === "undefined" ? Object : Response
1317
1447
  ]),
1318
- _ts_metadata2("design:returntype", void 0)
1448
+ _ts_metadata3("design:returntype", void 0)
1319
1449
  ], 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", [
1450
+ _ts_decorate5([
1451
+ (0, import_common5.Post)("debug/:capability_id"),
1452
+ _ts_param3(0, (0, import_common5.Param)("capability_id")),
1453
+ _ts_param3(1, (0, import_common5.Body)()),
1454
+ _ts_param3(2, (0, import_common5.Res)()),
1455
+ _ts_metadata3("design:type", Function),
1456
+ _ts_metadata3("design:paramtypes", [
1327
1457
  String,
1328
1458
  typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody,
1329
1459
  typeof Response === "undefined" ? Object : Response
1330
1460
  ]),
1331
- _ts_metadata2("design:returntype", Promise)
1461
+ _ts_metadata3("design:returntype", Promise)
1332
1462
  ], 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", [
1463
+ _ts_decorate5([
1464
+ (0, import_common5.Post)("debug/:capability_id/stream"),
1465
+ _ts_param3(0, (0, import_common5.Param)("capability_id")),
1466
+ _ts_param3(1, (0, import_common5.Body)()),
1467
+ _ts_param3(2, (0, import_common5.Res)()),
1468
+ _ts_metadata3("design:type", Function),
1469
+ _ts_metadata3("design:paramtypes", [
1340
1470
  String,
1341
1471
  typeof DebugRequestBody === "undefined" ? Object : DebugRequestBody,
1342
1472
  typeof Response === "undefined" ? Object : Response
1343
1473
  ]),
1344
- _ts_metadata2("design:returntype", Promise)
1474
+ _ts_metadata3("design:returntype", Promise)
1345
1475
  ], DebugController.prototype, "debugStream", null);
1346
- DebugController = _ts_decorate4([
1476
+ DebugController = _ts_decorate5([
1347
1477
  (0, import_swagger.ApiExcludeController)(),
1348
- (0, import_common4.Controller)("__innerapi__/capability"),
1349
- _ts_metadata2("design:type", Function),
1350
- _ts_metadata2("design:paramtypes", [
1478
+ (0, import_common5.Controller)("__innerapi__/capability"),
1479
+ _ts_metadata3("design:type", Function),
1480
+ _ts_metadata3("design:paramtypes", [
1351
1481
  typeof CapabilityService === "undefined" ? Object : CapabilityService,
1352
1482
  typeof PluginLoaderService === "undefined" ? Object : PluginLoaderService,
1353
1483
  typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService
@@ -1355,38 +1485,38 @@ DebugController = _ts_decorate4([
1355
1485
  ], DebugController);
1356
1486
 
1357
1487
  // src/controllers/webhook.controller.ts
1358
- var import_common5 = require("@nestjs/common");
1488
+ var import_common6 = require("@nestjs/common");
1359
1489
  var import_swagger2 = require("@nestjs/swagger");
1360
- function _ts_decorate5(decorators, target, key, desc) {
1490
+ function _ts_decorate6(decorators, target, key, desc) {
1361
1491
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1362
1492
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1363
1493
  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
1494
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1365
1495
  }
1366
- __name(_ts_decorate5, "_ts_decorate");
1367
- function _ts_metadata3(k, v) {
1496
+ __name(_ts_decorate6, "_ts_decorate");
1497
+ function _ts_metadata4(k, v) {
1368
1498
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1369
1499
  }
1370
- __name(_ts_metadata3, "_ts_metadata");
1371
- function _ts_param3(paramIndex, decorator) {
1500
+ __name(_ts_metadata4, "_ts_metadata");
1501
+ function _ts_param4(paramIndex, decorator) {
1372
1502
  return function(target, key) {
1373
1503
  decorator(target, key, paramIndex);
1374
1504
  };
1375
1505
  }
1376
- __name(_ts_param3, "_ts_param");
1506
+ __name(_ts_param4, "_ts_param");
1377
1507
  var WebhookController = class _WebhookController {
1378
1508
  static {
1379
1509
  __name(this, "WebhookController");
1380
1510
  }
1381
1511
  capabilityService;
1382
- logger = new import_common5.Logger(_WebhookController.name);
1512
+ logger = new import_common6.Logger(_WebhookController.name);
1383
1513
  constructor(capabilityService) {
1384
1514
  this.capabilityService = capabilityService;
1385
1515
  }
1386
1516
  list(res) {
1387
1517
  try {
1388
1518
  const capabilities = this.capabilityService.listCapabilities();
1389
- res.status(import_common5.HttpStatus.OK).json({
1519
+ res.status(import_common6.HttpStatus.OK).json({
1390
1520
  status_code: ErrorCodes.SUCCESS,
1391
1521
  data: {
1392
1522
  capabilities: capabilities.map((c) => ({
@@ -1399,7 +1529,7 @@ var WebhookController = class _WebhookController {
1399
1529
  });
1400
1530
  } catch (error) {
1401
1531
  this.logger.error("Failed to list capabilities", error);
1402
- res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json({
1532
+ res.status(import_common6.HttpStatus.INTERNAL_SERVER_ERROR).json({
1403
1533
  status_code: ErrorCodes.EXECUTION_ERROR,
1404
1534
  error_msg: `Failed to list capabilities: ${error instanceof Error ? error.message : String(error)}`
1405
1535
  });
@@ -1407,8 +1537,10 @@ var WebhookController = class _WebhookController {
1407
1537
  }
1408
1538
  async execute(capabilityId, body, res) {
1409
1539
  try {
1410
- const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
1411
- res.status(import_common5.HttpStatus.OK).json({
1540
+ const result = await this.capabilityService.load(capabilityId).call(body.action, body.params, {
1541
+ caller: "client"
1542
+ });
1543
+ res.status(import_common6.HttpStatus.OK).json({
1412
1544
  status_code: ErrorCodes.SUCCESS,
1413
1545
  data: {
1414
1546
  output: result
@@ -1416,7 +1548,7 @@ var WebhookController = class _WebhookController {
1416
1548
  });
1417
1549
  } catch (error) {
1418
1550
  this.logger.error("Capability execution failed", error);
1419
- res.status(import_common5.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1551
+ res.status(import_common6.HttpStatus.INTERNAL_SERVER_ERROR).json(this.buildErrorResponse(error));
1420
1552
  }
1421
1553
  }
1422
1554
  /**
@@ -1468,7 +1600,9 @@ var WebhookController = class _WebhookController {
1468
1600
  input: stringifyForLog(body.params)
1469
1601
  });
1470
1602
  const capability = this.capabilityService.load(capabilityId);
1471
- const eventStream = capability.callStreamWithEvents(body.action, body.params);
1603
+ const eventStream = capability.callStreamWithEvents(body.action, body.params, {
1604
+ caller: "client"
1605
+ });
1472
1606
  let pendingChunk = null;
1473
1607
  for await (const event of eventStream) {
1474
1608
  switch (event.type) {
@@ -1566,60 +1700,60 @@ var WebhookController = class _WebhookController {
1566
1700
  }
1567
1701
  }
1568
1702
  };
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", [
1703
+ _ts_decorate6([
1704
+ (0, import_common6.Get)("list"),
1705
+ _ts_param4(0, (0, import_common6.Res)()),
1706
+ _ts_metadata4("design:type", Function),
1707
+ _ts_metadata4("design:paramtypes", [
1574
1708
  typeof Response === "undefined" ? Object : Response
1575
1709
  ]),
1576
- _ts_metadata3("design:returntype", void 0)
1710
+ _ts_metadata4("design:returntype", void 0)
1577
1711
  ], 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", [
1712
+ _ts_decorate6([
1713
+ (0, import_common6.Post)(":capability_id"),
1714
+ _ts_param4(0, (0, import_common6.Param)("capability_id")),
1715
+ _ts_param4(1, (0, import_common6.Body)()),
1716
+ _ts_param4(2, (0, import_common6.Res)()),
1717
+ _ts_metadata4("design:type", Function),
1718
+ _ts_metadata4("design:paramtypes", [
1585
1719
  String,
1586
1720
  typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody,
1587
1721
  typeof Response === "undefined" ? Object : Response
1588
1722
  ]),
1589
- _ts_metadata3("design:returntype", Promise)
1723
+ _ts_metadata4("design:returntype", Promise)
1590
1724
  ], 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", [
1725
+ _ts_decorate6([
1726
+ (0, import_common6.Post)(":capability_id/stream"),
1727
+ _ts_param4(0, (0, import_common6.Param)("capability_id")),
1728
+ _ts_param4(1, (0, import_common6.Body)()),
1729
+ _ts_param4(2, (0, import_common6.Res)()),
1730
+ _ts_metadata4("design:type", Function),
1731
+ _ts_metadata4("design:paramtypes", [
1598
1732
  String,
1599
1733
  typeof ExecuteRequestBody === "undefined" ? Object : ExecuteRequestBody,
1600
1734
  typeof Response === "undefined" ? Object : Response
1601
1735
  ]),
1602
- _ts_metadata3("design:returntype", Promise)
1736
+ _ts_metadata4("design:returntype", Promise)
1603
1737
  ], WebhookController.prototype, "executeStream", null);
1604
- WebhookController = _ts_decorate5([
1738
+ WebhookController = _ts_decorate6([
1605
1739
  (0, import_swagger2.ApiExcludeController)(),
1606
- (0, import_common5.Controller)("api/capability"),
1607
- _ts_metadata3("design:type", Function),
1608
- _ts_metadata3("design:paramtypes", [
1740
+ (0, import_common6.Controller)("api/capability"),
1741
+ _ts_metadata4("design:type", Function),
1742
+ _ts_metadata4("design:paramtypes", [
1609
1743
  typeof CapabilityService === "undefined" ? Object : CapabilityService
1610
1744
  ])
1611
1745
  ], WebhookController);
1612
1746
 
1613
1747
  // 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) {
1748
+ var import_common7 = require("@nestjs/common");
1749
+ var import_nestjs_common3 = require("@lark-apaas/nestjs-common");
1750
+ function _ts_decorate7(decorators, target, key, desc) {
1617
1751
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1618
1752
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1619
1753
  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
1754
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1621
1755
  }
1622
- __name(_ts_decorate6, "_ts_decorate");
1756
+ __name(_ts_decorate7, "_ts_decorate");
1623
1757
  var CAPABILITY_OPTIONS = /* @__PURE__ */ Symbol("CAPABILITY_OPTIONS");
1624
1758
  var isDevelopment = process.env.NODE_ENV === "development";
1625
1759
  function getControllers() {
@@ -1640,7 +1774,7 @@ var CapabilityModule = class _CapabilityModule {
1640
1774
  return {
1641
1775
  module: _CapabilityModule,
1642
1776
  imports: [
1643
- import_nestjs_common2.CommonModule
1777
+ import_nestjs_common3.CommonModule
1644
1778
  ],
1645
1779
  controllers: getControllers(),
1646
1780
  providers: [
@@ -1650,43 +1784,48 @@ var CapabilityModule = class _CapabilityModule {
1650
1784
  },
1651
1785
  {
1652
1786
  provide: CapabilityService,
1653
- useFactory: /* @__PURE__ */ __name((requestContextService, httpClientFactory, pluginLoader, templateEngine, moduleOptions) => {
1654
- const service = new CapabilityService(requestContextService, httpClientFactory, pluginLoader, templateEngine);
1787
+ useFactory: /* @__PURE__ */ __name((requestContextService, httpClientFactory, pluginLoader, templateEngine, telemetryService, moduleOptions) => {
1788
+ const service = new CapabilityService(requestContextService, httpClientFactory, pluginLoader, templateEngine, telemetryService);
1655
1789
  if (moduleOptions) {
1656
1790
  service.setOptions(moduleOptions);
1657
1791
  }
1658
1792
  return service;
1659
1793
  }, "useFactory"),
1660
1794
  inject: [
1661
- import_nestjs_common2.RequestContextService,
1662
- import_nestjs_common2.HTTP_CLIENT_FACTORY,
1795
+ import_nestjs_common3.RequestContextService,
1796
+ import_nestjs_common3.HTTP_CLIENT_FACTORY,
1663
1797
  PluginLoaderService,
1664
1798
  TemplateEngineService,
1799
+ TelemetryService,
1665
1800
  CAPABILITY_OPTIONS
1666
1801
  ]
1667
1802
  },
1668
1803
  PluginLoaderService,
1669
- TemplateEngineService
1804
+ TemplateEngineService,
1805
+ TelemetryService
1670
1806
  ],
1671
1807
  exports: [
1672
- CapabilityService
1808
+ CapabilityService,
1809
+ TelemetryService
1673
1810
  ]
1674
1811
  };
1675
1812
  }
1676
1813
  };
1677
- CapabilityModule = _ts_decorate6([
1678
- (0, import_common6.Module)({
1814
+ CapabilityModule = _ts_decorate7([
1815
+ (0, import_common7.Module)({
1679
1816
  imports: [
1680
- import_nestjs_common2.CommonModule
1817
+ import_nestjs_common3.CommonModule
1681
1818
  ],
1682
1819
  controllers: getControllers(),
1683
1820
  providers: [
1684
1821
  CapabilityService,
1685
1822
  PluginLoaderService,
1686
- TemplateEngineService
1823
+ TemplateEngineService,
1824
+ TelemetryService
1687
1825
  ],
1688
1826
  exports: [
1689
- CapabilityService
1827
+ CapabilityService,
1828
+ TelemetryService
1690
1829
  ]
1691
1830
  })
1692
1831
  ], CapabilityModule);
@@ -1701,6 +1840,7 @@ CapabilityModule = _ts_decorate6([
1701
1840
  PluginLoadError,
1702
1841
  PluginLoaderService,
1703
1842
  PluginNotFoundError,
1843
+ TelemetryService,
1704
1844
  TemplateEngineService,
1705
1845
  WebhookController,
1706
1846
  migrationAdaptor