@lark-apaas/nestjs-capability 0.0.1-alpha.9 → 0.1.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Logger, OnModuleInit, DynamicModule } from '@nestjs/common';
1
+ import { Logger, OnModuleInit, OnModuleDestroy, DynamicModule } from '@nestjs/common';
2
2
  import { PlatformHttpClient, RequestContextService } from '@lark-apaas/nestjs-common';
3
3
  import { ZodSchema } from 'zod';
4
4
  import { Response } from 'express';
@@ -94,9 +94,7 @@ interface StreamContentResponse {
94
94
  status_code: '0';
95
95
  data: {
96
96
  type: 'content';
97
- delta: {
98
- content: unknown;
99
- };
97
+ delta: unknown;
100
98
  finished?: boolean;
101
99
  };
102
100
  }
@@ -230,6 +228,7 @@ declare class TemplateEngineService {
230
228
  }
231
229
 
232
230
  declare class PluginNotFoundError extends Error {
231
+ readonly pluginKey: string;
233
232
  constructor(pluginKey: string);
234
233
  }
235
234
  declare class PluginLoadError extends Error {
@@ -238,15 +237,40 @@ declare class PluginLoadError extends Error {
238
237
  declare class PluginLoaderService {
239
238
  private readonly logger;
240
239
  private readonly pluginInstances;
240
+ /** 记录每个插件的加载版本(时间戳),用于 ESM 缓存绕过 */
241
+ private readonly pluginVersions;
241
242
  loadPlugin(pluginKey: string): Promise<PluginInstance>;
242
243
  isPluginInstalled(pluginKey: string): boolean;
244
+ /**
245
+ * 清除插件缓存
246
+ * - 清除应用层 pluginInstances 缓存
247
+ * - 清除 Node.js CJS 模块缓存(require.cache)
248
+ * - 更新版本号,下次 import 时绕过 ESM 缓存
249
+ * @param pluginKey - 插件标识,不传则清除所有
250
+ */
243
251
  clearCache(pluginKey?: string): void;
252
+ /**
253
+ * 清除 CJS 模块缓存
254
+ */
255
+ private clearNodeModuleCache;
256
+ /**
257
+ * 递归清除子模块缓存
258
+ */
259
+ private clearModuleAndChildren;
260
+ /**
261
+ * 强制重新加载插件
262
+ * @param pluginKey - 插件标识
263
+ */
264
+ reloadPlugin(pluginKey: string): Promise<PluginInstance>;
244
265
  }
245
266
 
246
267
  declare class CapabilityNotFoundError extends Error {
268
+ readonly capabilityId: string;
247
269
  constructor(capabilityId: string);
248
270
  }
249
271
  declare class ActionNotFoundError extends Error {
272
+ readonly pluginKey: string;
273
+ readonly actionName: string;
250
274
  constructor(pluginKey: string, actionName: string);
251
275
  }
252
276
  interface CapabilityExecutor {
@@ -275,19 +299,60 @@ interface CapabilityExecutor {
275
299
  isStream(actionName: string): Promise<boolean>;
276
300
  }
277
301
  interface CapabilityModuleOptions {
302
+ /** 能力配置目录路径,默认 server/capabilities */
278
303
  capabilitiesDir?: string;
304
+ /** 是否启用文件监听(热更新),默认 false */
305
+ enableWatching?: boolean;
306
+ /** 文件变更防抖时间(ms),默认 300 */
307
+ watchDebounce?: number;
279
308
  }
280
- declare class CapabilityService implements OnModuleInit {
309
+ declare class CapabilityService implements OnModuleInit, OnModuleDestroy {
281
310
  private readonly requestContextService;
282
311
  private readonly platformHttpClient;
283
312
  private readonly pluginLoaderService;
284
313
  private readonly templateEngineService;
285
314
  private readonly logger;
286
315
  private readonly capabilities;
316
+ /** 文件路径到 capability id 的映射,用于文件删除时查找 */
317
+ private readonly filePathToId;
287
318
  private capabilitiesDir;
319
+ private fileWatcher;
320
+ private options;
288
321
  constructor(requestContextService: RequestContextService, platformHttpClient: PlatformHttpClient, pluginLoaderService: PluginLoaderService, templateEngineService: TemplateEngineService);
322
+ /**
323
+ * 设置模块配置
324
+ */
325
+ setOptions(options: CapabilityModuleOptions): void;
289
326
  setCapabilitiesDir(dir: string): void;
290
327
  onModuleInit(): Promise<void>;
328
+ onModuleDestroy(): Promise<void>;
329
+ /**
330
+ * 启动文件监听(沙箱环境自动调用)
331
+ */
332
+ startWatching(): void;
333
+ /**
334
+ * 停止文件监听
335
+ */
336
+ stopWatching(): void;
337
+ /**
338
+ * 重新加载所有能力配置
339
+ */
340
+ reloadAllCapabilities(): Promise<void>;
341
+ private handleFileAdd;
342
+ private handleFileChange;
343
+ private handleFileUnlink;
344
+ /**
345
+ * 从文件加载单个能力配置
346
+ */
347
+ private loadCapabilityFromFile;
348
+ /**
349
+ * 重新加载单个能力配置
350
+ */
351
+ private reloadCapabilityFromFile;
352
+ /**
353
+ * 根据文件路径移除能力配置
354
+ */
355
+ private removeCapabilityByFile;
291
356
  private loadCapabilities;
292
357
  listCapabilities(): CapabilityConfig[];
293
358
  getCapability(capabilityId: string): CapabilityConfig | null;
@@ -335,7 +400,7 @@ declare class DebugController {
335
400
  private readonly templateEngineService;
336
401
  private readonly logger;
337
402
  constructor(capabilityService: CapabilityService, pluginLoaderService: PluginLoaderService, templateEngineService: TemplateEngineService);
338
- list(): SuccessResponse<ListResponseData>;
403
+ list(res: Response): void;
339
404
  /**
340
405
  * 获取 capability 配置
341
406
  * 优先使用 body.capability,否则从服务获取
@@ -346,7 +411,11 @@ declare class DebugController {
346
411
  * 优先使用传入的 action,否则使用插件第一个 action
347
412
  */
348
413
  private getActionName;
349
- debug(capabilityId: string, body: DebugRequestBody): Promise<SuccessResponse<DebugExecuteResponseData> | ErrorResponse>;
414
+ debug(capabilityId: string, body: DebugRequestBody, res: Response): Promise<void>;
415
+ /**
416
+ * 构建错误响应
417
+ */
418
+ private buildErrorResponse;
350
419
  debugStream(capabilityId: string, body: DebugRequestBody, res: Response): Promise<void>;
351
420
  }
352
421
 
@@ -358,8 +427,12 @@ declare class WebhookController {
358
427
  private readonly capabilityService;
359
428
  private readonly logger;
360
429
  constructor(capabilityService: CapabilityService);
361
- list(): SuccessResponse<ListResponseData>;
362
- execute(capabilityId: string, body: ExecuteRequestBody): Promise<SuccessResponse<ExecuteResponseData> | ErrorResponse>;
430
+ list(res: Response): void;
431
+ execute(capabilityId: string, body: ExecuteRequestBody, res: Response): Promise<void>;
432
+ /**
433
+ * 构建错误响应
434
+ */
435
+ private buildErrorResponse;
363
436
  executeStream(capabilityId: string, body: ExecuteRequestBody, res: Response): Promise<void>;
364
437
  }
365
438
 
@@ -367,4 +440,68 @@ declare class CapabilityModule {
367
440
  static forRoot(options?: CapabilityModuleOptions): DynamicModule;
368
441
  }
369
442
 
370
- export { ActionNotFoundError, type ActionSchema, type CapabilityConfig, type CapabilityExecutor, type CapabilityListItem, CapabilityModule, type CapabilityModuleOptions, CapabilityNotFoundError, CapabilityService, DebugController, type DebugExecuteResponseData, type DebugInfo, type ErrorCode, ErrorCodes, type ErrorResponse, type ExecuteResponseData, type JSONSchema, type ListResponseData, type PluginActionContext, type PluginInstance, PluginLoadError, PluginLoaderService, PluginNotFoundError, type PluginPackage, type StreamContentResponse, type StreamDoneMetadata, type StreamError, type StreamErrorResponse, type StreamEvent, type StreamResponse, type SuccessResponse, TemplateEngineService, type UserContext, WebhookController };
443
+ /**
444
+ * Migration Adaptor
445
+ *
446
+ * 用于迁移老版本 capability 调用代码,将新版本的返回结果包装为老版本的 Response 结构
447
+ */
448
+ /**
449
+ * 失败输出结构
450
+ */
451
+ interface FailureOutput {
452
+ response: {
453
+ status: {
454
+ /** 协议层返回码(如 HTTP 状态码) */
455
+ protocolStatusCode: string;
456
+ /** 业务应用状态码 */
457
+ appStatusCode: string;
458
+ };
459
+ /** 返回体(JSON 字符串形式) */
460
+ body: string;
461
+ };
462
+ }
463
+ /**
464
+ * 老版本 capability 调用的 Response 结构
465
+ *
466
+ * 注意:output 使用 any 类型以简化迁移代码,避免类型断言
467
+ */
468
+ interface MigrationResponse {
469
+ /** 当 code=0 时表示执行成功,当 code!=0 时表示执行失败 */
470
+ code: number;
471
+ data?: {
472
+ /** 执行成功时的输出结果(any 类型,兼容迁移场景) */
473
+ output?: any;
474
+ /** 执行结果,'success' 或 'error' */
475
+ outcome?: 'success' | 'error';
476
+ /** 执行失败时的输出结果 */
477
+ failureOutput?: FailureOutput;
478
+ };
479
+ /** 发生错误时的错误信息 */
480
+ message?: string;
481
+ }
482
+ /**
483
+ * 迁移适配器
484
+ *
485
+ * 将 CapabilityService.load().call() 的返回结果包装为老版本的 Response 结构
486
+ *
487
+ * @example
488
+ * // 老代码
489
+ * const result = await generateTaskDescription(params);
490
+ *
491
+ * // 新代码
492
+ * const result = await migrationAdaptor(
493
+ * this.capabilityService.load('ai_generate_task_description').call('run', params)
494
+ * );
495
+ *
496
+ * // result 结构
497
+ * // {
498
+ * // code: 0,
499
+ * // data: {
500
+ * // output: { ... }, // 能力执行结果
501
+ * // outcome: 'success'
502
+ * // }
503
+ * // }
504
+ */
505
+ declare function migrationAdaptor(promise: Promise<unknown>): Promise<MigrationResponse>;
506
+
507
+ export { ActionNotFoundError, type ActionSchema, type CapabilityConfig, type CapabilityExecutor, type CapabilityListItem, CapabilityModule, type CapabilityModuleOptions, CapabilityNotFoundError, CapabilityService, DebugController, type DebugExecuteResponseData, type DebugInfo, type ErrorCode, ErrorCodes, type ErrorResponse, type ExecuteResponseData, type FailureOutput, type JSONSchema, type ListResponseData, type MigrationResponse, type PluginActionContext, type PluginInstance, PluginLoadError, PluginLoaderService, PluginNotFoundError, type PluginPackage, type StreamContentResponse, type StreamDoneMetadata, type StreamError, type StreamErrorResponse, type StreamEvent, type StreamResponse, type SuccessResponse, TemplateEngineService, type UserContext, WebhookController, migrationAdaptor };