@lark-apaas/nestjs-capability 0.0.1-alpha.0 → 0.0.1-alpha.10

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
@@ -15,6 +15,7 @@ npm install @lark-apaas/nestjs-capability
15
15
  - 从 `server/capabilities/` 目录加载能力配置
16
16
  - 加载并实例化关联的插件
17
17
  - 多种调用方式(Node 调用、Debug 调用、前端调用)
18
+ - 流式调用支持(SSE)
18
19
  - 模板参数解析
19
20
 
20
21
  ## 快速开始
@@ -59,17 +60,56 @@ export class TaskService {
59
60
  }
60
61
  ```
61
62
 
63
+ ### 流式调用
64
+
65
+ 用于 LLM 对话等流式输出场景:
66
+
67
+ ```typescript
68
+ @Injectable()
69
+ export class ChatService {
70
+ constructor(private readonly capabilityService: CapabilityService) {}
71
+
72
+ async *chat(message: string): AsyncIterable<{ content: string }> {
73
+ const stream = this.capabilityService
74
+ .load('ai_chat')
75
+ .callStream('chat', { message });
76
+
77
+ for await (const chunk of stream) {
78
+ yield chunk as { content: string };
79
+ }
80
+ }
81
+ }
82
+ ```
83
+
84
+ 普通调用会自动聚合流式结果:
85
+
86
+ ```typescript
87
+ // 即使是流式 action,call() 也会返回聚合后的完整结果
88
+ const result = await this.capabilityService
89
+ .load('ai_chat')
90
+ .call('chat', { message: 'hello' });
91
+ ```
92
+
62
93
  ### 上下文覆盖
63
94
 
64
95
  ```typescript
96
+ // 覆盖 userContext
65
97
  const result = await this.capabilityService
66
98
  .load('notify_task_created')
67
99
  .call('run', inputParams, {
68
100
  userContext: {
69
101
  userId: 'custom-user-id',
70
102
  tenantId: 'custom-tenant-id',
103
+ appId: 'custom-app-id',
71
104
  },
72
105
  });
106
+
107
+ // 设置调试模式(插件可据此返回更详细的错误信息)
108
+ const debugResult = await this.capabilityService
109
+ .load('ai_chat')
110
+ .call('chat', inputParams, {
111
+ isDebug: true,
112
+ });
73
113
  ```
74
114
 
75
115
  ## 能力配置
@@ -128,16 +168,42 @@ POST /api/capability/:capability_id
128
168
  }
129
169
  ```
130
170
 
131
- ### Debug 调用(仅开发环境)
171
+ ### 前端流式调用
132
172
 
133
173
  ```
134
- POST /__innerapi__/capability/debug/:capability_id
174
+ POST /api/capability/:capability_id/stream
135
175
 
136
176
  请求体:
137
177
  {
138
- "action": "run",
178
+ "action": "chat",
139
179
  "params": {
180
+ "message": "hello"
181
+ }
182
+ }
183
+
184
+ 响应(SSE 格式):
185
+ data: {"content":"Hello"}
186
+ data: {"content":" World"}
187
+ data: [DONE]
188
+ ```
189
+
190
+ ### Debug 调用(仅开发环境)
191
+
192
+ ```
193
+ POST /__innerapi__/capability/debug/:capability_id
194
+
195
+ 请求体(所有字段可选):
196
+ {
197
+ "action": "run", // 可选,默认使用插件第一个 action
198
+ "params": { // 可选,用户输入参数
140
199
  "group_name": "测试群"
200
+ },
201
+ "capability": { // 可选,完整的 capability 配置(优先使用)
202
+ "id": "test",
203
+ "pluginID": "@test/plugin",
204
+ "pluginVersion": "1.0.0",
205
+ "name": "测试",
206
+ "formValue": {}
141
207
  }
142
208
  }
143
209
 
@@ -150,11 +216,30 @@ POST /__innerapi__/capability/debug/:capability_id
150
216
  "capabilityConfig": { ... },
151
217
  "resolvedParams": { ... },
152
218
  "duration": 123,
153
- "pluginID": "@xxx/plugin"
219
+ "pluginID": "@xxx/plugin",
220
+ "action": "run"
154
221
  }
155
222
  }
156
223
  ```
157
224
 
225
+ ### Debug 流式调用(仅开发环境)
226
+
227
+ ```
228
+ POST /__innerapi__/capability/debug/:capability_id/stream
229
+
230
+ 请求体(所有字段可选):
231
+ {
232
+ "action": "chat",
233
+ "params": { "message": "hello" },
234
+ "capability": { ... }
235
+ }
236
+
237
+ 响应(SSE 格式):
238
+ data: {"content":"Hello"}
239
+ data: {"content":" World"}
240
+ data: [DONE]
241
+ ```
242
+
158
243
  ### 列出所有能力(仅开发环境)
159
244
 
160
245
  ```
@@ -192,16 +277,30 @@ interface CapabilityService {
192
277
  // 加载能力并返回执行器
193
278
  load(capabilityId: string): CapabilityExecutor;
194
279
 
280
+ // 使用传入的配置加载能力执行器(用于 debug 场景)
281
+ loadWithConfig(config: CapabilityConfig): CapabilityExecutor;
282
+
195
283
  // 设置自定义能力目录
196
284
  setCapabilitiesDir(dir: string): void;
197
285
  }
198
286
 
199
287
  interface CapabilityExecutor {
288
+ // 调用能力(流式 action 会自动聚合结果)
200
289
  call(
201
290
  actionName: string,
202
291
  input: unknown,
203
292
  context?: Partial<PluginActionContext>
204
293
  ): Promise<unknown>;
294
+
295
+ // 流式调用能力(返回 AsyncIterable)
296
+ callStream(
297
+ actionName: string,
298
+ input: unknown,
299
+ context?: Partial<PluginActionContext>
300
+ ): AsyncIterable<unknown>;
301
+
302
+ // 检查 action 是否为流式
303
+ isStream(actionName: string): Promise<boolean>;
205
304
  }
206
305
  ```
207
306
 
@@ -257,10 +356,22 @@ interface CapabilityConfig {
257
356
 
258
357
  ```typescript
259
358
  interface PluginInstance {
359
+ // 执行 action(必需)
260
360
  run(actionName: string, context: PluginActionContext, input: unknown): Promise<unknown>;
361
+
362
+ // 流式执行 action(可选)
363
+ runStream?(actionName: string, context: PluginActionContext, input: unknown): AsyncIterable<unknown>;
364
+
365
+ // 检查是否为流式 action(可选)
366
+ isStreamAction?(actionName: string): boolean;
367
+
368
+ // 聚合流式结果(可选,默认返回 chunks 数组)
369
+ aggregate?(actionName: string, chunks: unknown[]): unknown;
370
+
371
+ // Action 相关
261
372
  hasAction(actionName: string): boolean;
262
- getActionSchema(actionName: string): ActionSchema | null;
263
373
  listActions(): string[];
374
+ getActionSchema(actionName: string): ActionSchema | null;
264
375
  getInputSchema(actionName: string, config?: unknown): ZodSchema | undefined;
265
376
  getOutputSchema(actionName: string, input: unknown, config?: unknown): ZodSchema | undefined;
266
377
  }
@@ -275,10 +386,17 @@ interface PluginActionContext {
275
386
  userContext: {
276
387
  userId: string; // 用户 ID
277
388
  tenantId: string; // 租户 ID
389
+ appId: string; // 应用 ID
278
390
  };
391
+ isDebug: boolean; // 是否为调试模式
279
392
  }
280
393
  ```
281
394
 
395
+ **isDebug 说明:**
396
+ - `DebugController` 调用时 `isDebug = true`
397
+ - `WebhookController` 或其他调用时 `isDebug = false`
398
+ - 插件可据此返回更详细的错误信息、调试日志等
399
+
282
400
  ## 错误类型
283
401
 
284
402
  | 错误 | 描述 |