@lark-apaas/nestjs-capability 0.0.1-alpha.3 → 0.0.1-alpha.6

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.cjs CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  CapabilityNotFoundError: () => CapabilityNotFoundError,
37
37
  CapabilityService: () => CapabilityService,
38
38
  DebugController: () => DebugController,
39
+ ErrorCodes: () => ErrorCodes,
39
40
  PluginLoadError: () => PluginLoadError,
40
41
  PluginLoaderService: () => PluginLoaderService,
41
42
  PluginNotFoundError: () => PluginNotFoundError,
@@ -44,6 +45,22 @@ __export(index_exports, {
44
45
  });
45
46
  module.exports = __toCommonJS(index_exports);
46
47
 
48
+ // src/interfaces/error-codes.ts
49
+ var ErrorCodes = {
50
+ /** 成功 */
51
+ SUCCESS: "0",
52
+ /** 能力不存在 */
53
+ CAPABILITY_NOT_FOUND: "k_ec_cap_001",
54
+ /** 插件不存在 */
55
+ PLUGIN_NOT_FOUND: "k_ec_cap_002",
56
+ /** Action 不存在 */
57
+ ACTION_NOT_FOUND: "k_ec_cap_003",
58
+ /** 参数验证失败 */
59
+ PARAMS_VALIDATION_ERROR: "k_ec_cap_004",
60
+ /** 执行失败 */
61
+ EXECUTION_ERROR: "k_ec_cap_005"
62
+ };
63
+
47
64
  // src/services/template-engine.service.ts
48
65
  var import_common = require("@nestjs/common");
49
66
  function _ts_decorate(decorators, target, key, desc) {
@@ -130,8 +147,8 @@ var PluginNotFoundError = class extends Error {
130
147
  static {
131
148
  __name(this, "PluginNotFoundError");
132
149
  }
133
- constructor(pluginID) {
134
- super(`Plugin not found: ${pluginID}`);
150
+ constructor(pluginKey) {
151
+ super(`Plugin not found: ${pluginKey}`);
135
152
  this.name = "PluginNotFoundError";
136
153
  }
137
154
  };
@@ -139,8 +156,8 @@ var PluginLoadError = class extends Error {
139
156
  static {
140
157
  __name(this, "PluginLoadError");
141
158
  }
142
- constructor(pluginID, reason) {
143
- super(`Failed to load plugin ${pluginID}: ${reason}`);
159
+ constructor(pluginKey, reason) {
160
+ super(`Failed to load plugin ${pluginKey}: ${reason}`);
144
161
  this.name = "PluginLoadError";
145
162
  }
146
163
  };
@@ -150,41 +167,41 @@ var PluginLoaderService = class _PluginLoaderService {
150
167
  }
151
168
  logger = new import_common2.Logger(_PluginLoaderService.name);
152
169
  pluginInstances = /* @__PURE__ */ new Map();
153
- async loadPlugin(pluginID) {
154
- const cached = this.pluginInstances.get(pluginID);
170
+ async loadPlugin(pluginKey) {
171
+ const cached = this.pluginInstances.get(pluginKey);
155
172
  if (cached) {
156
- this.logger.debug(`Using cached plugin instance: ${pluginID}`);
173
+ this.logger.debug(`Using cached plugin instance: ${pluginKey}`);
157
174
  return cached;
158
175
  }
159
- this.logger.log(`Loading plugin: ${pluginID}`);
176
+ this.logger.log(`Loading plugin: ${pluginKey}`);
160
177
  try {
161
- const pluginPackage = (await import(pluginID)).default;
178
+ const pluginPackage = (await import(pluginKey)).default;
162
179
  if (typeof pluginPackage.create !== "function") {
163
- throw new PluginLoadError(pluginID, "Plugin does not export create() function");
180
+ throw new PluginLoadError(pluginKey, "Plugin does not export create() function");
164
181
  }
165
182
  const instance = pluginPackage.create();
166
- this.pluginInstances.set(pluginID, instance);
167
- this.logger.log(`Plugin loaded successfully: ${pluginID}`);
183
+ this.pluginInstances.set(pluginKey, instance);
184
+ this.logger.log(`Plugin loaded successfully: ${pluginKey}`);
168
185
  return instance;
169
186
  } catch (error) {
170
187
  if (error.code === "MODULE_NOT_FOUND") {
171
- throw new PluginNotFoundError(pluginID);
188
+ throw new PluginNotFoundError(pluginKey);
172
189
  }
173
- throw new PluginLoadError(pluginID, error instanceof Error ? error.message : String(error));
190
+ throw new PluginLoadError(pluginKey, error instanceof Error ? error.message : String(error));
174
191
  }
175
192
  }
176
- isPluginInstalled(pluginID) {
193
+ isPluginInstalled(pluginKey) {
177
194
  try {
178
- require.resolve(pluginID);
195
+ require.resolve(pluginKey);
179
196
  return true;
180
197
  } catch {
181
198
  return false;
182
199
  }
183
200
  }
184
- clearCache(pluginID) {
185
- if (pluginID) {
186
- this.pluginInstances.delete(pluginID);
187
- this.logger.log(`Cleared cache for plugin: ${pluginID}`);
201
+ clearCache(pluginKey) {
202
+ if (pluginKey) {
203
+ this.pluginInstances.delete(pluginKey);
204
+ this.logger.log(`Cleared cache for plugin: ${pluginKey}`);
188
205
  } else {
189
206
  this.pluginInstances.clear();
190
207
  this.logger.log("Cleared all plugin caches");
@@ -230,8 +247,8 @@ var ActionNotFoundError = class extends Error {
230
247
  static {
231
248
  __name(this, "ActionNotFoundError");
232
249
  }
233
- constructor(pluginID, actionName) {
234
- super(`Action '${actionName}' not found in plugin ${pluginID}`);
250
+ constructor(pluginKey, actionName) {
251
+ super(`Action '${actionName}' not found in plugin ${pluginKey}`);
235
252
  this.name = "ActionNotFoundError";
236
253
  }
237
254
  };
@@ -311,6 +328,9 @@ var CapabilityService = class _CapabilityService {
311
328
  callStream: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
312
329
  return this.executeCallStream(config, actionName, input, contextOverride);
313
330
  }, "callStream"),
331
+ callStreamWithEvents: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
332
+ return this.executeCallStreamWithEvents(config, actionName, input, contextOverride);
333
+ }, "callStreamWithEvents"),
314
334
  isStream: /* @__PURE__ */ __name(async (actionName) => {
315
335
  return this.checkIsStream(config, actionName);
316
336
  }, "isStream")
@@ -320,9 +340,9 @@ var CapabilityService = class _CapabilityService {
320
340
  * 检查 action 是否为流式
321
341
  */
322
342
  async checkIsStream(config, actionName) {
323
- const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginID);
343
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
324
344
  if (!pluginInstance.hasAction(actionName)) {
325
- throw new ActionNotFoundError(config.pluginID, actionName);
345
+ throw new ActionNotFoundError(config.pluginKey, actionName);
326
346
  }
327
347
  return pluginInstance.isStreamAction?.(actionName) ?? false;
328
348
  }
@@ -334,9 +354,9 @@ var CapabilityService = class _CapabilityService {
334
354
  async executeCall(config, actionName, input, contextOverride) {
335
355
  const startTime = Date.now();
336
356
  try {
337
- const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginID);
357
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
338
358
  if (!pluginInstance.hasAction(actionName)) {
339
- throw new ActionNotFoundError(config.pluginID, actionName);
359
+ throw new ActionNotFoundError(config.pluginKey, actionName);
340
360
  }
341
361
  const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
342
362
  const context = this.buildActionContext(contextOverride);
@@ -345,7 +365,7 @@ var CapabilityService = class _CapabilityService {
345
365
  message: "Executing capability",
346
366
  capabilityId: config.id,
347
367
  action: actionName,
348
- pluginID: config.pluginID,
368
+ pluginKey: config.pluginKey,
349
369
  isStream
350
370
  });
351
371
  let result;
@@ -384,9 +404,9 @@ var CapabilityService = class _CapabilityService {
384
404
  async *executeCallStream(config, actionName, input, contextOverride) {
385
405
  const startTime = Date.now();
386
406
  try {
387
- const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginID);
407
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
388
408
  if (!pluginInstance.hasAction(actionName)) {
389
- throw new ActionNotFoundError(config.pluginID, actionName);
409
+ throw new ActionNotFoundError(config.pluginKey, actionName);
390
410
  }
391
411
  const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
392
412
  const context = this.buildActionContext(contextOverride);
@@ -395,7 +415,7 @@ var CapabilityService = class _CapabilityService {
395
415
  message: "Executing capability (stream)",
396
416
  capabilityId: config.id,
397
417
  action: actionName,
398
- pluginID: config.pluginID,
418
+ pluginKey: config.pluginKey,
399
419
  isStream
400
420
  });
401
421
  if (isStream && pluginInstance.runStream) {
@@ -421,6 +441,79 @@ var CapabilityService = class _CapabilityService {
421
441
  throw error;
422
442
  }
423
443
  }
444
+ /**
445
+ * 流式执行 capability,返回带事件协议的流
446
+ * - 优先使用 pluginInstance.runStreamWithEvents
447
+ * - 如果插件不支持,则包装 runStream/run 为 StreamEvent
448
+ */
449
+ async *executeCallStreamWithEvents(config, actionName, input, contextOverride) {
450
+ const startTime = Date.now();
451
+ let chunkCount = 0;
452
+ try {
453
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
454
+ if (!pluginInstance.hasAction(actionName)) {
455
+ throw new ActionNotFoundError(config.pluginKey, actionName);
456
+ }
457
+ const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
458
+ const context = this.buildActionContext(contextOverride);
459
+ this.logger.log({
460
+ message: "Executing capability (streamWithEvents)",
461
+ capabilityId: config.id,
462
+ action: actionName,
463
+ pluginKey: config.pluginKey
464
+ });
465
+ if (pluginInstance.runStreamWithEvents) {
466
+ yield* pluginInstance.runStreamWithEvents(actionName, context, resolvedParams);
467
+ } else {
468
+ const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
469
+ if (isStream && pluginInstance.runStream) {
470
+ for await (const chunk of pluginInstance.runStream(actionName, context, resolvedParams)) {
471
+ chunkCount++;
472
+ yield {
473
+ type: "data",
474
+ data: chunk
475
+ };
476
+ }
477
+ } else {
478
+ const result = await pluginInstance.run(actionName, context, resolvedParams);
479
+ chunkCount = 1;
480
+ yield {
481
+ type: "data",
482
+ data: result
483
+ };
484
+ }
485
+ yield {
486
+ type: "done",
487
+ metadata: {
488
+ chunks: chunkCount,
489
+ duration: Date.now() - startTime
490
+ }
491
+ };
492
+ }
493
+ this.logger.log({
494
+ message: "Capability streamWithEvents completed",
495
+ capabilityId: config.id,
496
+ action: actionName,
497
+ duration: Date.now() - startTime,
498
+ chunks: chunkCount
499
+ });
500
+ } catch (error) {
501
+ this.logger.error({
502
+ message: "Capability streamWithEvents execution failed",
503
+ capabilityId: config.id,
504
+ action: actionName,
505
+ error: error instanceof Error ? error.message : String(error),
506
+ duration: Date.now() - startTime
507
+ });
508
+ yield {
509
+ type: "error",
510
+ error: {
511
+ code: "EXECUTION_ERROR",
512
+ message: error instanceof Error ? error.message : String(error)
513
+ }
514
+ };
515
+ }
516
+ }
424
517
  buildActionContext(override) {
425
518
  return {
426
519
  logger: this.logger,
@@ -482,14 +575,15 @@ var DebugController = class {
482
575
  list() {
483
576
  const capabilities = this.capabilityService.listCapabilities();
484
577
  return {
485
- code: 0,
486
- message: "success",
487
- data: capabilities.map((c) => ({
488
- id: c.id,
489
- name: c.name,
490
- pluginID: c.pluginID,
491
- pluginVersion: c.pluginVersion
492
- }))
578
+ status_code: ErrorCodes.SUCCESS,
579
+ data: {
580
+ capabilities: capabilities.map((c) => ({
581
+ id: c.id,
582
+ name: c.name,
583
+ pluginID: c.pluginKey,
584
+ pluginVersion: c.pluginVersion
585
+ }))
586
+ }
493
587
  };
494
588
  }
495
589
  /**
@@ -514,16 +608,16 @@ var DebugController = class {
514
608
  * 获取 action 名称
515
609
  * 优先使用传入的 action,否则使用插件第一个 action
516
610
  */
517
- async getActionName(pluginID, bodyAction) {
611
+ async getActionName(pluginKey, bodyAction) {
518
612
  if (bodyAction) {
519
613
  return bodyAction;
520
614
  }
521
- const pluginInstance = await this.pluginLoaderService.loadPlugin(pluginID);
615
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(pluginKey);
522
616
  const actions = pluginInstance.listActions();
523
617
  if (actions.length === 0) {
524
618
  throw new import_common4.HttpException({
525
619
  code: 1,
526
- message: `Plugin ${pluginID} has no actions`,
620
+ message: `Plugin ${pluginKey} has no actions`,
527
621
  error: "NO_ACTIONS"
528
622
  }, import_common4.HttpStatus.BAD_REQUEST);
529
623
  }
@@ -533,68 +627,45 @@ var DebugController = class {
533
627
  const startTime = Date.now();
534
628
  const params = body.params ?? {};
535
629
  const config = this.getCapabilityConfig(capabilityId, body.capability);
536
- const action = await this.getActionName(config.pluginID, body.action);
630
+ const action = await this.getActionName(config.pluginKey, body.action);
537
631
  const resolvedParams = this.templateEngineService.resolve(config.formValue, params);
538
632
  try {
539
633
  const result = await this.capabilityService.loadWithConfig(config).call(action, params);
540
634
  return {
541
- code: 0,
542
- message: "success",
543
- data: result,
544
- debug: {
545
- capabilityConfig: config,
546
- resolvedParams,
547
- duration: Date.now() - startTime,
548
- pluginID: config.pluginID,
549
- action
635
+ status_code: ErrorCodes.SUCCESS,
636
+ data: {
637
+ output: result,
638
+ debug: {
639
+ capabilityConfig: config,
640
+ resolvedParams,
641
+ duration: Date.now() - startTime,
642
+ pluginID: config.pluginKey,
643
+ action
644
+ }
550
645
  }
551
646
  };
552
647
  } catch (error) {
553
- const duration = Date.now() - startTime;
554
648
  if (error instanceof CapabilityNotFoundError) {
555
649
  throw new import_common4.HttpException({
556
- code: 1,
557
- message: error.message,
558
- error: "CAPABILITY_NOT_FOUND",
559
- debug: {
560
- duration
561
- }
650
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
651
+ error_msg: `Capability not found: ${capabilityId}`
562
652
  }, import_common4.HttpStatus.NOT_FOUND);
563
653
  }
564
654
  if (error instanceof PluginNotFoundError) {
565
655
  throw new import_common4.HttpException({
566
- code: 1,
567
- message: error.message,
568
- error: "PLUGIN_NOT_FOUND",
569
- debug: {
570
- duration,
571
- pluginID: config.pluginID,
572
- action
573
- }
656
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
657
+ error_msg: `Plugin not found: ${config.pluginKey}`
574
658
  }, import_common4.HttpStatus.INTERNAL_SERVER_ERROR);
575
659
  }
576
660
  if (error instanceof ActionNotFoundError) {
577
661
  throw new import_common4.HttpException({
578
- code: 1,
579
- message: error.message,
580
- error: "ACTION_NOT_FOUND",
581
- debug: {
582
- duration,
583
- pluginID: config.pluginID,
584
- action
585
- }
662
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
663
+ error_msg: `Action '${action}' not found in plugin ${config.pluginKey}`
586
664
  }, import_common4.HttpStatus.BAD_REQUEST);
587
665
  }
588
666
  throw new import_common4.HttpException({
589
- code: 1,
590
- message: error instanceof Error ? error.message : String(error),
591
- error: "EXECUTION_ERROR",
592
- debug: {
593
- duration,
594
- pluginID: config.pluginID,
595
- action,
596
- resolvedParams
597
- }
667
+ status_code: ErrorCodes.EXECUTION_ERROR,
668
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
598
669
  }, import_common4.HttpStatus.INTERNAL_SERVER_ERROR);
599
670
  }
600
671
  }
@@ -605,35 +676,78 @@ var DebugController = class {
605
676
  res.setHeader("Connection", "keep-alive");
606
677
  try {
607
678
  const config = this.getCapabilityConfig(capabilityId, body.capability);
608
- const action = await this.getActionName(config.pluginID, body.action);
679
+ const action = await this.getActionName(config.pluginKey, body.action);
609
680
  const capability = this.capabilityService.loadWithConfig(config);
610
- const stream = capability.callStream(action, params);
611
- for await (const chunk of stream) {
612
- res.write(`data: ${JSON.stringify(chunk)}
681
+ const eventStream = capability.callStreamWithEvents(action, params);
682
+ for await (const event of eventStream) {
683
+ switch (event.type) {
684
+ case "data": {
685
+ const response = {
686
+ status_code: ErrorCodes.SUCCESS,
687
+ data: {
688
+ type: "content",
689
+ delta: {
690
+ content: event.data
691
+ }
692
+ }
693
+ };
694
+ res.write(`data: ${JSON.stringify(response)}
695
+
696
+ `);
697
+ break;
698
+ }
699
+ case "done": {
700
+ const response = {
701
+ status_code: ErrorCodes.SUCCESS,
702
+ data: {
703
+ type: "content",
704
+ delta: {
705
+ content: null
706
+ },
707
+ finished: true
708
+ }
709
+ };
710
+ res.write(`data: ${JSON.stringify(response)}
613
711
 
614
712
  `);
713
+ res.end();
714
+ return;
715
+ }
716
+ case "error": {
717
+ const response = {
718
+ status_code: ErrorCodes.SUCCESS,
719
+ data: {
720
+ type: "error",
721
+ error: {
722
+ code: 0,
723
+ message: event.error.message
724
+ }
725
+ }
726
+ };
727
+ res.write(`data: ${JSON.stringify(response)}
728
+
729
+ `);
730
+ res.end();
731
+ return;
732
+ }
733
+ }
615
734
  }
616
- res.write("data: [DONE]\n\n");
735
+ res.end();
617
736
  } catch (error) {
618
- const message = error instanceof Error ? error.message : String(error);
619
- let errorCode = "EXECUTION_ERROR";
620
- if (error instanceof CapabilityNotFoundError) {
621
- errorCode = "CAPABILITY_NOT_FOUND";
622
- } else if (error instanceof PluginNotFoundError) {
623
- errorCode = "PLUGIN_NOT_FOUND";
624
- } else if (error instanceof ActionNotFoundError) {
625
- errorCode = "ACTION_NOT_FOUND";
626
- } else if (error instanceof import_common4.HttpException) {
627
- const response = error.getResponse();
628
- errorCode = response.error ?? "EXECUTION_ERROR";
629
- }
630
- res.write(`data: ${JSON.stringify({
631
- error: message,
632
- code: errorCode
633
- })}
737
+ const errorMsg = error instanceof Error ? error.message : String(error);
738
+ const response = {
739
+ status_code: ErrorCodes.SUCCESS,
740
+ data: {
741
+ type: "error",
742
+ error: {
743
+ code: 0,
744
+ message: errorMsg
745
+ }
746
+ }
747
+ };
748
+ res.write(`data: ${JSON.stringify(response)}
634
749
 
635
750
  `);
636
- } finally {
637
751
  res.end();
638
752
  }
639
753
  }
@@ -642,7 +756,7 @@ _ts_decorate4([
642
756
  (0, import_common4.Get)("list"),
643
757
  _ts_metadata2("design:type", Function),
644
758
  _ts_metadata2("design:paramtypes", []),
645
- _ts_metadata2("design:returntype", typeof ListResponse === "undefined" ? Object : ListResponse)
759
+ _ts_metadata2("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
646
760
  ], DebugController.prototype, "list", null);
647
761
  _ts_decorate4([
648
762
  (0, import_common4.Post)("debug/:capability_id"),
@@ -708,51 +822,48 @@ var WebhookController = class {
708
822
  list() {
709
823
  const capabilities = this.capabilityService.listCapabilities();
710
824
  return {
711
- code: 0,
712
- message: "success",
713
- data: capabilities.map((c) => ({
714
- id: c.id,
715
- name: c.name,
716
- description: c.description,
717
- pluginID: c.pluginID,
718
- pluginVersion: c.pluginVersion
719
- }))
825
+ status_code: ErrorCodes.SUCCESS,
826
+ data: {
827
+ capabilities: capabilities.map((c) => ({
828
+ id: c.id,
829
+ name: c.name,
830
+ pluginID: c.pluginKey,
831
+ pluginVersion: c.pluginVersion
832
+ }))
833
+ }
720
834
  };
721
835
  }
722
836
  async execute(capabilityId, body) {
723
837
  try {
724
838
  const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
725
839
  return {
726
- code: 0,
727
- message: "success",
728
- data: result
840
+ status_code: ErrorCodes.SUCCESS,
841
+ data: {
842
+ output: result
843
+ }
729
844
  };
730
845
  } catch (error) {
731
846
  if (error instanceof CapabilityNotFoundError) {
732
847
  throw new import_common5.HttpException({
733
- code: 1,
734
- message: error.message,
735
- error: "CAPABILITY_NOT_FOUND"
848
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
849
+ error_msg: `Capability not found: ${capabilityId}`
736
850
  }, import_common5.HttpStatus.NOT_FOUND);
737
851
  }
738
852
  if (error instanceof PluginNotFoundError) {
739
853
  throw new import_common5.HttpException({
740
- code: 1,
741
- message: error.message,
742
- error: "PLUGIN_NOT_FOUND"
854
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
855
+ error_msg: `Plugin not found`
743
856
  }, import_common5.HttpStatus.INTERNAL_SERVER_ERROR);
744
857
  }
745
858
  if (error instanceof ActionNotFoundError) {
746
859
  throw new import_common5.HttpException({
747
- code: 1,
748
- message: error.message,
749
- error: "ACTION_NOT_FOUND"
860
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
861
+ error_msg: `Action '${body.action}' not found`
750
862
  }, import_common5.HttpStatus.BAD_REQUEST);
751
863
  }
752
864
  throw new import_common5.HttpException({
753
- code: 1,
754
- message: error instanceof Error ? error.message : String(error),
755
- error: "EXECUTION_ERROR"
865
+ status_code: ErrorCodes.EXECUTION_ERROR,
866
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
756
867
  }, import_common5.HttpStatus.INTERNAL_SERVER_ERROR);
757
868
  }
758
869
  }
@@ -762,30 +873,76 @@ var WebhookController = class {
762
873
  res.setHeader("Connection", "keep-alive");
763
874
  try {
764
875
  const capability = this.capabilityService.load(capabilityId);
765
- const stream = capability.callStream(body.action, body.params);
766
- for await (const chunk of stream) {
767
- res.write(`data: ${JSON.stringify(chunk)}
876
+ const eventStream = capability.callStreamWithEvents(body.action, body.params);
877
+ for await (const event of eventStream) {
878
+ switch (event.type) {
879
+ case "data": {
880
+ const response = {
881
+ status_code: ErrorCodes.SUCCESS,
882
+ data: {
883
+ type: "content",
884
+ delta: {
885
+ content: event.data
886
+ }
887
+ }
888
+ };
889
+ res.write(`data: ${JSON.stringify(response)}
768
890
 
769
891
  `);
892
+ break;
893
+ }
894
+ case "done": {
895
+ const response = {
896
+ status_code: ErrorCodes.SUCCESS,
897
+ data: {
898
+ type: "content",
899
+ delta: {
900
+ content: null
901
+ },
902
+ finished: true
903
+ }
904
+ };
905
+ res.write(`data: ${JSON.stringify(response)}
906
+
907
+ `);
908
+ res.end();
909
+ return;
910
+ }
911
+ case "error": {
912
+ const response = {
913
+ status_code: ErrorCodes.SUCCESS,
914
+ data: {
915
+ type: "error",
916
+ error: {
917
+ code: 0,
918
+ message: event.error.message
919
+ }
920
+ }
921
+ };
922
+ res.write(`data: ${JSON.stringify(response)}
923
+
924
+ `);
925
+ res.end();
926
+ return;
927
+ }
928
+ }
770
929
  }
771
- res.write("data: [DONE]\n\n");
930
+ res.end();
772
931
  } catch (error) {
773
- const message = error instanceof Error ? error.message : String(error);
774
- let errorCode = "EXECUTION_ERROR";
775
- if (error instanceof CapabilityNotFoundError) {
776
- errorCode = "CAPABILITY_NOT_FOUND";
777
- } else if (error instanceof PluginNotFoundError) {
778
- errorCode = "PLUGIN_NOT_FOUND";
779
- } else if (error instanceof ActionNotFoundError) {
780
- errorCode = "ACTION_NOT_FOUND";
781
- }
782
- res.write(`data: ${JSON.stringify({
783
- error: message,
784
- code: errorCode
785
- })}
932
+ const errorMsg = error instanceof Error ? error.message : String(error);
933
+ const response = {
934
+ status_code: ErrorCodes.SUCCESS,
935
+ data: {
936
+ type: "error",
937
+ error: {
938
+ code: 0,
939
+ message: errorMsg
940
+ }
941
+ }
942
+ };
943
+ res.write(`data: ${JSON.stringify(response)}
786
944
 
787
945
  `);
788
- } finally {
789
946
  res.end();
790
947
  }
791
948
  }
@@ -794,7 +951,7 @@ _ts_decorate5([
794
951
  (0, import_common5.Get)("list"),
795
952
  _ts_metadata3("design:type", Function),
796
953
  _ts_metadata3("design:paramtypes", []),
797
- _ts_metadata3("design:returntype", typeof ListResponse === "undefined" ? Object : ListResponse)
954
+ _ts_metadata3("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
798
955
  ], WebhookController.prototype, "list", null);
799
956
  _ts_decorate5([
800
957
  (0, import_common5.Post)(":capability_id"),
@@ -909,6 +1066,7 @@ CapabilityModule = _ts_decorate6([
909
1066
  CapabilityNotFoundError,
910
1067
  CapabilityService,
911
1068
  DebugController,
1069
+ ErrorCodes,
912
1070
  PluginLoadError,
913
1071
  PluginLoaderService,
914
1072
  PluginNotFoundError,