@lark-apaas/nestjs-capability 0.0.1-alpha.4 → 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) {
@@ -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")
@@ -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
- pluginKey: c.pluginKey,
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
  /**
@@ -538,63 +632,40 @@ var DebugController = class {
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
- pluginKey: config.pluginKey,
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
- pluginKey: config.pluginKey,
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
- pluginKey: config.pluginKey,
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
- pluginKey: config.pluginKey,
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
  }
@@ -607,33 +678,76 @@ var DebugController = class {
607
678
  const config = this.getCapabilityConfig(capabilityId, body.capability);
608
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)}
711
+
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)}
613
728
 
614
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
- pluginKey: c.pluginKey,
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)}
890
+
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)}
768
923
 
769
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,