@lark-apaas/nestjs-capability 0.1.4 → 0.1.5-alpha.0

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
@@ -419,8 +419,9 @@ interface PluginInstance {
419
419
 
420
420
  ```typescript
421
421
  interface PluginActionContext {
422
+ pluginKey: string; // 插件标识(如 @official/ai-chat)
422
423
  logger: Logger; // 日志记录器
423
- httpClient: PlatformHttpClient; // HTTP 客户端(平台鉴权)
424
+ platformHttpClient: PlatformHttpClient; // HTTP 客户端(自动携带 x-plugin-key header)
424
425
  userContext: {
425
426
  userId: string; // 用户 ID
426
427
  tenantId: string; // 租户 ID
@@ -430,6 +431,11 @@ interface PluginActionContext {
430
431
  }
431
432
  ```
432
433
 
434
+ **pluginKey 说明:**
435
+ - 每次插件调用都会传入当前插件的标识
436
+ - 插件的 HttpClient 会自动在请求头中携带 `x-plugin-key` header
437
+ - 用于计费追踪、日志关联等场景
438
+
433
439
  **isDebug 说明:**
434
440
  - `DebugController` 调用时 `isDebug = true`
435
441
  - `WebhookController` 或其他调用时 `isDebug = false`
package/dist/index.cjs CHANGED
@@ -538,7 +538,7 @@ var CapabilityService = class _CapabilityService {
538
538
  __name(this, "CapabilityService");
539
539
  }
540
540
  requestContextService;
541
- platformHttpClient;
541
+ httpClientFactory;
542
542
  pluginLoaderService;
543
543
  templateEngineService;
544
544
  logger = new import_common3.Logger(_CapabilityService.name);
@@ -548,9 +548,9 @@ var CapabilityService = class _CapabilityService {
548
548
  capabilitiesDir;
549
549
  fileWatcher = null;
550
550
  options = {};
551
- constructor(requestContextService, platformHttpClient, pluginLoaderService, templateEngineService) {
551
+ constructor(requestContextService, httpClientFactory, pluginLoaderService, templateEngineService) {
552
552
  this.requestContextService = requestContextService;
553
- this.platformHttpClient = platformHttpClient;
553
+ this.httpClientFactory = httpClientFactory;
554
554
  this.pluginLoaderService = pluginLoaderService;
555
555
  this.templateEngineService = templateEngineService;
556
556
  const isDev = process.env.NODE_ENV === "development";
@@ -790,7 +790,7 @@ var CapabilityService = class _CapabilityService {
790
790
  if (!pluginInstance.hasAction(actionName)) {
791
791
  throw new ActionNotFoundError(config.pluginKey, actionName);
792
792
  }
793
- const context = this.buildActionContext(contextOverride);
793
+ const context = this.buildActionContext(config.pluginKey, contextOverride);
794
794
  const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
795
795
  this.logger.log("Executing capability (call)", {
796
796
  ...loggerContext,
@@ -840,7 +840,7 @@ var CapabilityService = class _CapabilityService {
840
840
  if (!pluginInstance.hasAction(actionName)) {
841
841
  throw new ActionNotFoundError(config.pluginKey, actionName);
842
842
  }
843
- const context = this.buildActionContext(contextOverride);
843
+ const context = this.buildActionContext(config.pluginKey, contextOverride);
844
844
  const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
845
845
  this.logger.log("Executing capability (stream)", {
846
846
  ...loggerContext,
@@ -890,7 +890,7 @@ var CapabilityService = class _CapabilityService {
890
890
  if (!pluginInstance.hasAction(actionName)) {
891
891
  throw new ActionNotFoundError(config.pluginKey, actionName);
892
892
  }
893
- const context = this.buildActionContext(contextOverride);
893
+ const context = this.buildActionContext(config.pluginKey, contextOverride);
894
894
  const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
895
895
  this.logger.log("Executing capability (streamWithEvents)", {
896
896
  ...loggerContext,
@@ -963,14 +963,44 @@ var CapabilityService = class _CapabilityService {
963
963
  };
964
964
  }
965
965
  }
966
- buildActionContext(override) {
966
+ buildActionContext(pluginKey, override) {
967
967
  return {
968
+ pluginKey,
968
969
  logger: this.logger,
969
- platformHttpClient: this.platformHttpClient,
970
+ platformHttpClient: this.createPluginHttpClient(pluginKey),
970
971
  userContext: override?.userContext ?? this.getUserContext(),
971
972
  isDebug: override?.isDebug ?? false
972
973
  };
973
974
  }
975
+ /**
976
+ * 为指定插件创建独立的 HttpClient 实例
977
+ * 自动添加 x-plugin-key header
978
+ */
979
+ createPluginHttpClient(pluginKey) {
980
+ const client = this.httpClientFactory.create();
981
+ client.interceptors.request.use((config) => {
982
+ config.headers = {
983
+ ...config.headers,
984
+ "x-plugin-key": pluginKey
985
+ };
986
+ return config;
987
+ });
988
+ return this.wrapAsProtectedClient(client);
989
+ }
990
+ /**
991
+ * 将 HttpClient 包装为受保护的 PlatformHttpClient
992
+ * 只暴露请求方法,不暴露 interceptors
993
+ */
994
+ wrapAsProtectedClient(client) {
995
+ return {
996
+ request: /* @__PURE__ */ __name((config) => client.request(config), "request"),
997
+ get: /* @__PURE__ */ __name((url, config) => client.get(url, config), "get"),
998
+ post: /* @__PURE__ */ __name((url, data, config) => client.post(url, data, config), "post"),
999
+ put: /* @__PURE__ */ __name((url, data, config) => client.put(url, data, config), "put"),
1000
+ patch: /* @__PURE__ */ __name((url, data, config) => client.patch(url, data, config), "patch"),
1001
+ delete: /* @__PURE__ */ __name((url, config) => client.delete(url, config), "delete")
1002
+ };
1003
+ }
974
1004
  getUserContext() {
975
1005
  const ctx = this.requestContextService.getContext();
976
1006
  return {
@@ -982,11 +1012,11 @@ var CapabilityService = class _CapabilityService {
982
1012
  };
983
1013
  CapabilityService = _ts_decorate3([
984
1014
  (0, import_common3.Injectable)(),
985
- _ts_param(1, (0, import_common3.Inject)(import_nestjs_common.PLATFORM_HTTP_CLIENT)),
1015
+ _ts_param(1, (0, import_common3.Inject)(import_nestjs_common.HTTP_CLIENT_FACTORY)),
986
1016
  _ts_metadata("design:type", Function),
987
1017
  _ts_metadata("design:paramtypes", [
988
1018
  typeof import_nestjs_common.RequestContextService === "undefined" ? Object : import_nestjs_common.RequestContextService,
989
- typeof PlatformHttpClient === "undefined" ? Object : PlatformHttpClient,
1019
+ typeof HttpClientFactory === "undefined" ? Object : HttpClientFactory,
990
1020
  typeof PluginLoaderService === "undefined" ? Object : PluginLoaderService,
991
1021
  typeof TemplateEngineService === "undefined" ? Object : TemplateEngineService
992
1022
  ])
@@ -1576,8 +1606,8 @@ var CapabilityModule = class _CapabilityModule {
1576
1606
  },
1577
1607
  {
1578
1608
  provide: CapabilityService,
1579
- useFactory: /* @__PURE__ */ __name((requestContextService, httpClient, pluginLoader, templateEngine, moduleOptions) => {
1580
- const service = new CapabilityService(requestContextService, httpClient, pluginLoader, templateEngine);
1609
+ useFactory: /* @__PURE__ */ __name((requestContextService, httpClientFactory, pluginLoader, templateEngine, moduleOptions) => {
1610
+ const service = new CapabilityService(requestContextService, httpClientFactory, pluginLoader, templateEngine);
1581
1611
  if (moduleOptions) {
1582
1612
  service.setOptions(moduleOptions);
1583
1613
  }
@@ -1585,7 +1615,7 @@ var CapabilityModule = class _CapabilityModule {
1585
1615
  }, "useFactory"),
1586
1616
  inject: [
1587
1617
  import_nestjs_common2.RequestContextService,
1588
- import_nestjs_common2.PLATFORM_HTTP_CLIENT,
1618
+ import_nestjs_common2.HTTP_CLIENT_FACTORY,
1589
1619
  PluginLoaderService,
1590
1620
  TemplateEngineService,
1591
1621
  CAPABILITY_OPTIONS