@lark-apaas/nestjs-capability 0.0.1-alpha.4 → 0.0.1-alpha.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.d.ts CHANGED
@@ -6,13 +6,151 @@ import { Response } from 'express';
6
6
  interface UserContext {
7
7
  userId: string;
8
8
  tenantId: string;
9
+ appId: string;
9
10
  }
10
11
  interface PluginActionContext {
11
12
  logger: Logger;
12
- httpClient: PlatformHttpClient;
13
+ platformHttpClient: PlatformHttpClient;
13
14
  userContext: UserContext;
14
15
  }
15
16
 
17
+ interface JSONSchema {
18
+ type?: string;
19
+ properties?: Record<string, JSONSchema>;
20
+ required?: string[];
21
+ items?: JSONSchema;
22
+ [key: string]: unknown;
23
+ }
24
+ interface CapabilityConfig {
25
+ id: string;
26
+ pluginKey: string;
27
+ pluginVersion: string;
28
+ name: string;
29
+ description: string;
30
+ paramsSchema: JSONSchema;
31
+ formValue: Record<string, unknown>;
32
+ createdAt: number;
33
+ updatedAt: number;
34
+ }
35
+
36
+ /**
37
+ * 成功响应基础结构
38
+ */
39
+ interface SuccessResponse<T> {
40
+ status_code: '0';
41
+ data: T;
42
+ }
43
+ /**
44
+ * 错误响应结构
45
+ */
46
+ interface ErrorResponse {
47
+ status_code: string;
48
+ error_msg: string;
49
+ }
50
+ /**
51
+ * Debug 信息
52
+ */
53
+ interface DebugInfo {
54
+ capabilityConfig: CapabilityConfig;
55
+ resolvedParams: unknown;
56
+ duration: number;
57
+ pluginID: string;
58
+ action: string;
59
+ }
60
+ /**
61
+ * 执行接口响应 data 结构
62
+ */
63
+ interface ExecuteResponseData {
64
+ output: unknown;
65
+ }
66
+ /**
67
+ * Debug 执行接口响应 data 结构
68
+ */
69
+ interface DebugExecuteResponseData {
70
+ output: unknown;
71
+ debug: DebugInfo;
72
+ }
73
+ /**
74
+ * 能力列表项
75
+ */
76
+ interface CapabilityListItem {
77
+ id: string;
78
+ name: string;
79
+ pluginID: string;
80
+ pluginVersion: string;
81
+ }
82
+ /**
83
+ * 列表接口响应 data 结构
84
+ */
85
+ interface ListResponseData {
86
+ capabilities: CapabilityListItem[];
87
+ }
88
+ /**
89
+ * 流式内容响应
90
+ */
91
+ interface StreamContentResponse {
92
+ status_code: '0';
93
+ data: {
94
+ type: 'content';
95
+ delta: {
96
+ content: unknown;
97
+ };
98
+ finished?: boolean;
99
+ };
100
+ }
101
+ /**
102
+ * 流式错误响应
103
+ */
104
+ interface StreamErrorResponse {
105
+ status_code: '0';
106
+ data: {
107
+ type: 'error';
108
+ error: {
109
+ code: number;
110
+ message: string;
111
+ };
112
+ };
113
+ }
114
+ /**
115
+ * 流式响应类型
116
+ */
117
+ type StreamResponse = StreamContentResponse | StreamErrorResponse;
118
+ /**
119
+ * 流完成元数据
120
+ */
121
+ interface StreamDoneMetadata {
122
+ /** chunk 总数 */
123
+ chunks: number;
124
+ /** 流持续时间 (ms) */
125
+ duration: number;
126
+ /** 聚合结果(可选) */
127
+ aggregated?: unknown;
128
+ }
129
+ /**
130
+ * 流错误信息
131
+ */
132
+ interface StreamError {
133
+ code: string;
134
+ message: string;
135
+ details?: unknown;
136
+ }
137
+ /**
138
+ * 流事件类型
139
+ * - data: 数据事件,包含单个 chunk
140
+ * - done: 完成事件,包含元数据
141
+ * - error: 错误事件,包含错误信息
142
+ */
143
+ type StreamEvent<T> = {
144
+ type: 'data';
145
+ data: T;
146
+ } | {
147
+ type: 'done';
148
+ metadata: StreamDoneMetadata;
149
+ } | {
150
+ type: 'error';
151
+ error: StreamError;
152
+ };
153
+
16
154
  interface ActionSchema {
17
155
  input: ZodSchema | ((config: unknown) => ZodSchema);
18
156
  output?: ZodSchema | ((input: unknown, config: unknown) => ZodSchema);
@@ -30,8 +168,10 @@ interface PluginInstance {
30
168
  getOutputSchema(actionName: string, input: unknown, config?: unknown): ZodSchema | undefined;
31
169
  /** 列出所有 action */
32
170
  listActions(): string[];
33
- /** 流式执行指定 action */
171
+ /** 流式执行指定 action,返回原始流 */
34
172
  runStream?(actionName: string, context: PluginActionContext, input: unknown): AsyncIterable<unknown>;
173
+ /** 流式执行指定 action,返回带事件协议的流(推荐) */
174
+ runStreamWithEvents?(actionName: string, context: PluginActionContext, input: unknown): AsyncIterable<StreamEvent<unknown>>;
35
175
  /** 检查 action 是否为流式 */
36
176
  isStreamAction?(actionName: string): boolean;
37
177
  /** 聚合流式结果(可选,插件自定义聚合逻辑) */
@@ -41,24 +181,24 @@ interface PluginPackage {
41
181
  create(): PluginInstance;
42
182
  }
43
183
 
44
- interface JSONSchema {
45
- type?: string;
46
- properties?: Record<string, JSONSchema>;
47
- required?: string[];
48
- items?: JSONSchema;
49
- [key: string]: unknown;
50
- }
51
- interface CapabilityConfig {
52
- id: string;
53
- pluginKey: string;
54
- pluginVersion: string;
55
- name: string;
56
- description: string;
57
- paramsSchema: JSONSchema;
58
- formValue: Record<string, unknown>;
59
- createdAt: number;
60
- updatedAt: number;
61
- }
184
+ /**
185
+ * Capability 模块错误码
186
+ */
187
+ declare const ErrorCodes: {
188
+ /** 成功 */
189
+ readonly SUCCESS: "0";
190
+ /** 能力不存在 */
191
+ readonly CAPABILITY_NOT_FOUND: "k_ec_cap_001";
192
+ /** 插件不存在 */
193
+ readonly PLUGIN_NOT_FOUND: "k_ec_cap_002";
194
+ /** Action 不存在 */
195
+ readonly ACTION_NOT_FOUND: "k_ec_cap_003";
196
+ /** 参数验证失败 */
197
+ readonly PARAMS_VALIDATION_ERROR: "k_ec_cap_004";
198
+ /** 执行失败 */
199
+ readonly EXECUTION_ERROR: "k_ec_cap_005";
200
+ };
201
+ type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
62
202
 
63
203
  /**
64
204
  * 模板引擎服务
@@ -115,11 +255,18 @@ interface CapabilityExecutor {
115
255
  */
116
256
  call(actionName: string, input: unknown, context?: Partial<PluginActionContext>): Promise<unknown>;
117
257
  /**
118
- * 流式调用 capability
258
+ * 流式调用 capability,返回原始流
119
259
  * - 返回原始 AsyncIterable
120
260
  * - 如果 action 是 unary,包装为单次 yield
121
261
  */
122
262
  callStream(actionName: string, input: unknown, context?: Partial<PluginActionContext>): AsyncIterable<unknown>;
263
+ /**
264
+ * 流式调用 capability,返回带事件协议的流(推荐)
265
+ * - 返回 StreamEvent 类型的 AsyncIterable
266
+ * - 支持 data/done/error 三种事件类型
267
+ * - Controller 层应优先使用此方法实现边收边发
268
+ */
269
+ callStreamWithEvents(actionName: string, input: unknown, context?: Partial<PluginActionContext>): AsyncIterable<StreamEvent<unknown>>;
123
270
  /**
124
271
  * 检查 action 是否为流式
125
272
  */
@@ -130,13 +277,13 @@ interface CapabilityModuleOptions {
130
277
  }
131
278
  declare class CapabilityService implements OnModuleInit {
132
279
  private readonly requestContextService;
133
- private readonly httpClient;
280
+ private readonly platformHttpClient;
134
281
  private readonly pluginLoaderService;
135
282
  private readonly templateEngineService;
136
283
  private readonly logger;
137
284
  private readonly capabilities;
138
285
  private capabilitiesDir;
139
- constructor(requestContextService: RequestContextService, httpClient: PlatformHttpClient, pluginLoaderService: PluginLoaderService, templateEngineService: TemplateEngineService);
286
+ constructor(requestContextService: RequestContextService, platformHttpClient: PlatformHttpClient, pluginLoaderService: PluginLoaderService, templateEngineService: TemplateEngineService);
140
287
  setCapabilitiesDir(dir: string): void;
141
288
  onModuleInit(): Promise<void>;
142
289
  private loadCapabilities;
@@ -165,6 +312,12 @@ declare class CapabilityService implements OnModuleInit {
165
312
  * - unary action: 包装为单次 yield
166
313
  */
167
314
  private executeCallStream;
315
+ /**
316
+ * 流式执行 capability,返回带事件协议的流
317
+ * - 优先使用 pluginInstance.runStreamWithEvents
318
+ * - 如果插件不支持,则包装 runStream/run 为 StreamEvent
319
+ */
320
+ private executeCallStreamWithEvents;
168
321
  private buildActionContext;
169
322
  private getUserContext;
170
323
  }
@@ -174,34 +327,13 @@ interface DebugRequestBody {
174
327
  params?: Record<string, unknown>;
175
328
  capability?: CapabilityConfig;
176
329
  }
177
- interface DebugResponse {
178
- code: number;
179
- message: string;
180
- data: unknown;
181
- debug?: {
182
- capabilityConfig: unknown;
183
- resolvedParams: unknown;
184
- duration: number;
185
- pluginKey: string;
186
- action: string;
187
- };
188
- }
189
- interface ListResponse$1 {
190
- code: number;
191
- message: string;
192
- data: Array<{
193
- id: string;
194
- name: string;
195
- pluginKey: string;
196
- pluginVersion: string;
197
- }>;
198
- }
199
330
  declare class DebugController {
200
331
  private readonly capabilityService;
201
332
  private readonly pluginLoaderService;
202
333
  private readonly templateEngineService;
334
+ private readonly logger;
203
335
  constructor(capabilityService: CapabilityService, pluginLoaderService: PluginLoaderService, templateEngineService: TemplateEngineService);
204
- list(): ListResponse$1;
336
+ list(): SuccessResponse<ListResponseData>;
205
337
  /**
206
338
  * 获取 capability 配置
207
339
  * 优先使用 body.capability,否则从服务获取
@@ -212,7 +344,7 @@ declare class DebugController {
212
344
  * 优先使用传入的 action,否则使用插件第一个 action
213
345
  */
214
346
  private getActionName;
215
- debug(capabilityId: string, body: DebugRequestBody): Promise<DebugResponse>;
347
+ debug(capabilityId: string, body: DebugRequestBody): Promise<SuccessResponse<DebugExecuteResponseData> | ErrorResponse>;
216
348
  debugStream(capabilityId: string, body: DebugRequestBody, res: Response): Promise<void>;
217
349
  }
218
350
 
@@ -220,28 +352,12 @@ interface ExecuteRequestBody {
220
352
  action: string;
221
353
  params: Record<string, unknown>;
222
354
  }
223
- interface ExecuteResponse {
224
- code: number;
225
- message: string;
226
- data: unknown;
227
- }
228
- interface CapabilityInfo {
229
- id: string;
230
- name: string;
231
- description: string;
232
- pluginKey: string;
233
- pluginVersion: string;
234
- }
235
- interface ListResponse {
236
- code: number;
237
- message: string;
238
- data: CapabilityInfo[];
239
- }
240
355
  declare class WebhookController {
241
356
  private readonly capabilityService;
357
+ private readonly logger;
242
358
  constructor(capabilityService: CapabilityService);
243
- list(): ListResponse;
244
- execute(capabilityId: string, body: ExecuteRequestBody): Promise<ExecuteResponse>;
359
+ list(): SuccessResponse<ListResponseData>;
360
+ execute(capabilityId: string, body: ExecuteRequestBody): Promise<SuccessResponse<ExecuteResponseData> | ErrorResponse>;
245
361
  executeStream(capabilityId: string, body: ExecuteRequestBody, res: Response): Promise<void>;
246
362
  }
247
363
 
@@ -249,4 +365,4 @@ declare class CapabilityModule {
249
365
  static forRoot(options?: CapabilityModuleOptions): DynamicModule;
250
366
  }
251
367
 
252
- export { ActionNotFoundError, type ActionSchema, type CapabilityConfig, type CapabilityExecutor, CapabilityModule, type CapabilityModuleOptions, CapabilityNotFoundError, CapabilityService, DebugController, type JSONSchema, type PluginActionContext, type PluginInstance, PluginLoadError, PluginLoaderService, PluginNotFoundError, type PluginPackage, TemplateEngineService, type UserContext, WebhookController };
368
+ 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 };