@lark-apaas/nestjs-capability 0.0.1-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/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Lark Technologies Pte. Ltd. and/or its affiliates
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
8
+ IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
9
+ INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
10
+ EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
11
+ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
12
+ DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
13
+ ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,307 @@
1
+ # @lark-apaas/nestjs-capability
2
+
3
+ 用于加载和执行能力(插件实例)的 NestJS 模块。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @lark-apaas/nestjs-capability
9
+ ```
10
+
11
+ ## 概述
12
+
13
+ 本模块提供以下功能:
14
+
15
+ - 从 `server/capabilities/` 目录加载能力配置
16
+ - 加载并实例化关联的插件
17
+ - 多种调用方式(Node 调用、Debug 调用、前端调用)
18
+ - 模板参数解析
19
+
20
+ ## 快速开始
21
+
22
+ ### 模块注册
23
+
24
+ ```typescript
25
+ // app.module.ts
26
+ import { CapabilityModule } from '@lark-apaas/nestjs-capability';
27
+ import * as path from 'path';
28
+
29
+ @Module({
30
+ imports: [
31
+ CapabilityModule.forRoot({
32
+ capabilitiesDir: path.join(__dirname, '../capabilities'),
33
+ }),
34
+ ],
35
+ })
36
+ export class AppModule {}
37
+ ```
38
+
39
+ ### Node 调用
40
+
41
+ ```typescript
42
+ import { Injectable } from '@nestjs/common';
43
+ import { CapabilityService } from '@lark-apaas/nestjs-capability';
44
+
45
+ @Injectable()
46
+ export class TaskService {
47
+ constructor(private readonly capabilityService: CapabilityService) {}
48
+
49
+ async onTaskCreated(task: Task) {
50
+ const result = await this.capabilityService
51
+ .load('create_feishu_group_for_task')
52
+ .call('run', {
53
+ group_name: `任务群 - ${task.name}`,
54
+ creator_id: task.creatorId,
55
+ });
56
+
57
+ return result;
58
+ }
59
+ }
60
+ ```
61
+
62
+ ### 上下文覆盖
63
+
64
+ ```typescript
65
+ const result = await this.capabilityService
66
+ .load('notify_task_created')
67
+ .call('run', inputParams, {
68
+ userContext: {
69
+ userId: 'custom-user-id',
70
+ tenantId: 'custom-tenant-id',
71
+ },
72
+ });
73
+ ```
74
+
75
+ ## 能力配置
76
+
77
+ 能力配置是位于 `server/capabilities/` 目录下的 JSON 文件:
78
+
79
+ ```json
80
+ {
81
+ "id": "create_feishu_group_for_task",
82
+ "pluginID": "@official-components/feishu-group-create",
83
+ "pluginVersion": "1.0.5",
84
+ "name": "任务创建时自动创建飞书群组",
85
+ "description": "在任务创建时,根据任务名称自动生成飞书群组",
86
+ "paramsSchema": {
87
+ "type": "object",
88
+ "properties": {
89
+ "group_name": { "type": "string" }
90
+ }
91
+ },
92
+ "formValue": {
93
+ "group_name": "{{input.group_name}}",
94
+ "members": ["123"]
95
+ },
96
+ "createdAt": 1764234360374,
97
+ "updatedAt": 1764234360374
98
+ }
99
+ ```
100
+
101
+ ### 模板语法
102
+
103
+ `formValue` 字段支持模板占位符:
104
+
105
+ - `{{input.xxx}}` - 引用用户输入参数
106
+ - `{{input.user.name}}` - 支持嵌套路径
107
+
108
+ ## HTTP API
109
+
110
+ ### 前端调用
111
+
112
+ ```
113
+ POST /api/capability/:capability_id
114
+
115
+ 请求体:
116
+ {
117
+ "action": "run",
118
+ "params": {
119
+ "group_name": "项目A群"
120
+ }
121
+ }
122
+
123
+ 响应:
124
+ {
125
+ "code": 0,
126
+ "message": "success",
127
+ "data": { ... }
128
+ }
129
+ ```
130
+
131
+ ### Debug 调用(仅开发环境)
132
+
133
+ ```
134
+ POST /__innerapi__/capability/debug/:capability_id
135
+
136
+ 请求体:
137
+ {
138
+ "action": "run",
139
+ "params": {
140
+ "group_name": "测试群"
141
+ }
142
+ }
143
+
144
+ 响应:
145
+ {
146
+ "code": 0,
147
+ "message": "success",
148
+ "data": { ... },
149
+ "debug": {
150
+ "capabilityConfig": { ... },
151
+ "resolvedParams": { ... },
152
+ "duration": 123,
153
+ "pluginID": "@xxx/plugin"
154
+ }
155
+ }
156
+ ```
157
+
158
+ ### 列出所有能力(仅开发环境)
159
+
160
+ ```
161
+ GET /__innerapi__/capability/list
162
+
163
+ 响应:
164
+ {
165
+ "code": 0,
166
+ "message": "success",
167
+ "data": [
168
+ {
169
+ "id": "create_feishu_group_for_task",
170
+ "name": "任务创建时自动创建飞书群组",
171
+ "pluginID": "@official-components/feishu-group-create",
172
+ "pluginVersion": "1.0.5"
173
+ }
174
+ ]
175
+ }
176
+ ```
177
+
178
+ ## 服务
179
+
180
+ ### CapabilityService
181
+
182
+ 用于加载和执行能力的核心服务。
183
+
184
+ ```typescript
185
+ interface CapabilityService {
186
+ // 列出所有已加载的能力
187
+ listCapabilities(): CapabilityConfig[];
188
+
189
+ // 获取指定能力
190
+ getCapability(capabilityId: string): CapabilityConfig | null;
191
+
192
+ // 加载能力并返回执行器
193
+ load(capabilityId: string): CapabilityExecutor;
194
+
195
+ // 设置自定义能力目录
196
+ setCapabilitiesDir(dir: string): void;
197
+ }
198
+
199
+ interface CapabilityExecutor {
200
+ call(
201
+ actionName: string,
202
+ input: unknown,
203
+ context?: Partial<PluginActionContext>
204
+ ): Promise<unknown>;
205
+ }
206
+ ```
207
+
208
+ ### PluginLoaderService
209
+
210
+ 用于加载和缓存插件实例的服务。
211
+
212
+ ```typescript
213
+ interface PluginLoaderService {
214
+ // 加载插件并创建实例(带缓存)
215
+ loadPlugin(pluginID: string): PluginInstance;
216
+
217
+ // 检查插件是否已安装
218
+ isPluginInstalled(pluginID: string): boolean;
219
+
220
+ // 清除插件缓存
221
+ clearCache(pluginID?: string): void;
222
+ }
223
+ ```
224
+
225
+ ### TemplateEngineService
226
+
227
+ 用于解析 formValue 模板的服务。
228
+
229
+ ```typescript
230
+ interface TemplateEngineService {
231
+ resolve(
232
+ template: Record<string, unknown>,
233
+ input: Record<string, unknown>
234
+ ): Record<string, unknown>;
235
+ }
236
+ ```
237
+
238
+ ## 接口定义
239
+
240
+ ### CapabilityConfig
241
+
242
+ ```typescript
243
+ interface CapabilityConfig {
244
+ id: string; // 能力唯一标识
245
+ pluginID: string; // 关联的插件 ID
246
+ pluginVersion: string; // 关联的插件版本
247
+ name: string; // 能力名称
248
+ description: string; // 能力描述
249
+ paramsSchema: JSONSchema; // 输入参数 JSON Schema
250
+ formValue: Record<string, unknown>; // 参数映射模板
251
+ createdAt: number; // 创建时间戳
252
+ updatedAt: number; // 更新时间戳
253
+ }
254
+ ```
255
+
256
+ ### PluginInstance
257
+
258
+ ```typescript
259
+ interface PluginInstance {
260
+ run(actionName: string, context: PluginActionContext, input: unknown): Promise<unknown>;
261
+ hasAction(actionName: string): boolean;
262
+ getActionSchema(actionName: string): ActionSchema | null;
263
+ listActions(): string[];
264
+ getInputSchema(actionName: string, config?: unknown): ZodSchema | undefined;
265
+ getOutputSchema(actionName: string, input: unknown, config?: unknown): ZodSchema | undefined;
266
+ }
267
+ ```
268
+
269
+ ### PluginActionContext
270
+
271
+ ```typescript
272
+ interface PluginActionContext {
273
+ logger: Logger; // 日志记录器
274
+ httpClient: PlatformHttpClient; // HTTP 客户端(平台鉴权)
275
+ userContext: {
276
+ userId: string; // 用户 ID
277
+ tenantId: string; // 租户 ID
278
+ };
279
+ }
280
+ ```
281
+
282
+ ## 错误类型
283
+
284
+ | 错误 | 描述 |
285
+ |------|------|
286
+ | `CapabilityNotFoundError` | 指定 ID 的能力不存在 |
287
+ | `PluginNotFoundError` | 插件未安装 |
288
+ | `PluginLoadError` | 插件加载或初始化失败 |
289
+ | `ActionNotFoundError` | 插件中不存在指定的 Action |
290
+
291
+ ## 模块配置
292
+
293
+ ```typescript
294
+ interface CapabilityModuleOptions {
295
+ // 能力配置目录路径(默认:server/capabilities)
296
+ capabilitiesDir?: string;
297
+ }
298
+ ```
299
+
300
+ ## 依赖
301
+
302
+ - `@lark-apaas/nestjs-common` - 提供 RequestContextService 和 PLATFORM_HTTP_CLIENT
303
+ - `@nestjs/common` - NestJS 框架
304
+
305
+ ## 许可证
306
+
307
+ MIT