@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.js CHANGED
@@ -7,6 +7,22 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
7
7
  throw Error('Dynamic require of "' + x + '" is not supported');
8
8
  });
9
9
 
10
+ // src/interfaces/error-codes.ts
11
+ var ErrorCodes = {
12
+ /** 成功 */
13
+ SUCCESS: "0",
14
+ /** 能力不存在 */
15
+ CAPABILITY_NOT_FOUND: "k_ec_cap_001",
16
+ /** 插件不存在 */
17
+ PLUGIN_NOT_FOUND: "k_ec_cap_002",
18
+ /** Action 不存在 */
19
+ ACTION_NOT_FOUND: "k_ec_cap_003",
20
+ /** 参数验证失败 */
21
+ PARAMS_VALIDATION_ERROR: "k_ec_cap_004",
22
+ /** 执行失败 */
23
+ EXECUTION_ERROR: "k_ec_cap_005"
24
+ };
25
+
10
26
  // src/services/template-engine.service.ts
11
27
  import { Injectable } from "@nestjs/common";
12
28
  function _ts_decorate(decorators, target, key, desc) {
@@ -274,6 +290,9 @@ var CapabilityService = class _CapabilityService {
274
290
  callStream: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
275
291
  return this.executeCallStream(config, actionName, input, contextOverride);
276
292
  }, "callStream"),
293
+ callStreamWithEvents: /* @__PURE__ */ __name((actionName, input, contextOverride) => {
294
+ return this.executeCallStreamWithEvents(config, actionName, input, contextOverride);
295
+ }, "callStreamWithEvents"),
277
296
  isStream: /* @__PURE__ */ __name(async (actionName) => {
278
297
  return this.checkIsStream(config, actionName);
279
298
  }, "isStream")
@@ -384,6 +403,79 @@ var CapabilityService = class _CapabilityService {
384
403
  throw error;
385
404
  }
386
405
  }
406
+ /**
407
+ * 流式执行 capability,返回带事件协议的流
408
+ * - 优先使用 pluginInstance.runStreamWithEvents
409
+ * - 如果插件不支持,则包装 runStream/run 为 StreamEvent
410
+ */
411
+ async *executeCallStreamWithEvents(config, actionName, input, contextOverride) {
412
+ const startTime = Date.now();
413
+ let chunkCount = 0;
414
+ try {
415
+ const pluginInstance = await this.pluginLoaderService.loadPlugin(config.pluginKey);
416
+ if (!pluginInstance.hasAction(actionName)) {
417
+ throw new ActionNotFoundError(config.pluginKey, actionName);
418
+ }
419
+ const resolvedParams = this.templateEngineService.resolve(config.formValue, input);
420
+ const context = this.buildActionContext(contextOverride);
421
+ this.logger.log({
422
+ message: "Executing capability (streamWithEvents)",
423
+ capabilityId: config.id,
424
+ action: actionName,
425
+ pluginKey: config.pluginKey
426
+ });
427
+ if (pluginInstance.runStreamWithEvents) {
428
+ yield* pluginInstance.runStreamWithEvents(actionName, context, resolvedParams);
429
+ } else {
430
+ const isStream = pluginInstance.isStreamAction?.(actionName) ?? false;
431
+ if (isStream && pluginInstance.runStream) {
432
+ for await (const chunk of pluginInstance.runStream(actionName, context, resolvedParams)) {
433
+ chunkCount++;
434
+ yield {
435
+ type: "data",
436
+ data: chunk
437
+ };
438
+ }
439
+ } else {
440
+ const result = await pluginInstance.run(actionName, context, resolvedParams);
441
+ chunkCount = 1;
442
+ yield {
443
+ type: "data",
444
+ data: result
445
+ };
446
+ }
447
+ yield {
448
+ type: "done",
449
+ metadata: {
450
+ chunks: chunkCount,
451
+ duration: Date.now() - startTime
452
+ }
453
+ };
454
+ }
455
+ this.logger.log({
456
+ message: "Capability streamWithEvents completed",
457
+ capabilityId: config.id,
458
+ action: actionName,
459
+ duration: Date.now() - startTime,
460
+ chunks: chunkCount
461
+ });
462
+ } catch (error) {
463
+ this.logger.error({
464
+ message: "Capability streamWithEvents execution failed",
465
+ capabilityId: config.id,
466
+ action: actionName,
467
+ error: error instanceof Error ? error.message : String(error),
468
+ duration: Date.now() - startTime
469
+ });
470
+ yield {
471
+ type: "error",
472
+ error: {
473
+ code: "EXECUTION_ERROR",
474
+ message: error instanceof Error ? error.message : String(error)
475
+ }
476
+ };
477
+ }
478
+ }
387
479
  buildActionContext(override) {
388
480
  return {
389
481
  logger: this.logger,
@@ -445,14 +537,15 @@ var DebugController = class {
445
537
  list() {
446
538
  const capabilities = this.capabilityService.listCapabilities();
447
539
  return {
448
- code: 0,
449
- message: "success",
450
- data: capabilities.map((c) => ({
451
- id: c.id,
452
- name: c.name,
453
- pluginKey: c.pluginKey,
454
- pluginVersion: c.pluginVersion
455
- }))
540
+ status_code: ErrorCodes.SUCCESS,
541
+ data: {
542
+ capabilities: capabilities.map((c) => ({
543
+ id: c.id,
544
+ name: c.name,
545
+ pluginID: c.pluginKey,
546
+ pluginVersion: c.pluginVersion
547
+ }))
548
+ }
456
549
  };
457
550
  }
458
551
  /**
@@ -501,63 +594,40 @@ var DebugController = class {
501
594
  try {
502
595
  const result = await this.capabilityService.loadWithConfig(config).call(action, params);
503
596
  return {
504
- code: 0,
505
- message: "success",
506
- data: result,
507
- debug: {
508
- capabilityConfig: config,
509
- resolvedParams,
510
- duration: Date.now() - startTime,
511
- pluginKey: config.pluginKey,
512
- action
597
+ status_code: ErrorCodes.SUCCESS,
598
+ data: {
599
+ output: result,
600
+ debug: {
601
+ capabilityConfig: config,
602
+ resolvedParams,
603
+ duration: Date.now() - startTime,
604
+ pluginID: config.pluginKey,
605
+ action
606
+ }
513
607
  }
514
608
  };
515
609
  } catch (error) {
516
- const duration = Date.now() - startTime;
517
610
  if (error instanceof CapabilityNotFoundError) {
518
611
  throw new HttpException({
519
- code: 1,
520
- message: error.message,
521
- error: "CAPABILITY_NOT_FOUND",
522
- debug: {
523
- duration
524
- }
612
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
613
+ error_msg: `Capability not found: ${capabilityId}`
525
614
  }, HttpStatus.NOT_FOUND);
526
615
  }
527
616
  if (error instanceof PluginNotFoundError) {
528
617
  throw new HttpException({
529
- code: 1,
530
- message: error.message,
531
- error: "PLUGIN_NOT_FOUND",
532
- debug: {
533
- duration,
534
- pluginKey: config.pluginKey,
535
- action
536
- }
618
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
619
+ error_msg: `Plugin not found: ${config.pluginKey}`
537
620
  }, HttpStatus.INTERNAL_SERVER_ERROR);
538
621
  }
539
622
  if (error instanceof ActionNotFoundError) {
540
623
  throw new HttpException({
541
- code: 1,
542
- message: error.message,
543
- error: "ACTION_NOT_FOUND",
544
- debug: {
545
- duration,
546
- pluginKey: config.pluginKey,
547
- action
548
- }
624
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
625
+ error_msg: `Action '${action}' not found in plugin ${config.pluginKey}`
549
626
  }, HttpStatus.BAD_REQUEST);
550
627
  }
551
628
  throw new HttpException({
552
- code: 1,
553
- message: error instanceof Error ? error.message : String(error),
554
- error: "EXECUTION_ERROR",
555
- debug: {
556
- duration,
557
- pluginKey: config.pluginKey,
558
- action,
559
- resolvedParams
560
- }
629
+ status_code: ErrorCodes.EXECUTION_ERROR,
630
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
561
631
  }, HttpStatus.INTERNAL_SERVER_ERROR);
562
632
  }
563
633
  }
@@ -570,33 +640,76 @@ var DebugController = class {
570
640
  const config = this.getCapabilityConfig(capabilityId, body.capability);
571
641
  const action = await this.getActionName(config.pluginKey, body.action);
572
642
  const capability = this.capabilityService.loadWithConfig(config);
573
- const stream = capability.callStream(action, params);
574
- for await (const chunk of stream) {
575
- res.write(`data: ${JSON.stringify(chunk)}
643
+ const eventStream = capability.callStreamWithEvents(action, params);
644
+ for await (const event of eventStream) {
645
+ switch (event.type) {
646
+ case "data": {
647
+ const response = {
648
+ status_code: ErrorCodes.SUCCESS,
649
+ data: {
650
+ type: "content",
651
+ delta: {
652
+ content: event.data
653
+ }
654
+ }
655
+ };
656
+ res.write(`data: ${JSON.stringify(response)}
657
+
658
+ `);
659
+ break;
660
+ }
661
+ case "done": {
662
+ const response = {
663
+ status_code: ErrorCodes.SUCCESS,
664
+ data: {
665
+ type: "content",
666
+ delta: {
667
+ content: null
668
+ },
669
+ finished: true
670
+ }
671
+ };
672
+ res.write(`data: ${JSON.stringify(response)}
673
+
674
+ `);
675
+ res.end();
676
+ return;
677
+ }
678
+ case "error": {
679
+ const response = {
680
+ status_code: ErrorCodes.SUCCESS,
681
+ data: {
682
+ type: "error",
683
+ error: {
684
+ code: 0,
685
+ message: event.error.message
686
+ }
687
+ }
688
+ };
689
+ res.write(`data: ${JSON.stringify(response)}
576
690
 
577
691
  `);
692
+ res.end();
693
+ return;
694
+ }
695
+ }
578
696
  }
579
- res.write("data: [DONE]\n\n");
697
+ res.end();
580
698
  } catch (error) {
581
- const message = error instanceof Error ? error.message : String(error);
582
- let errorCode = "EXECUTION_ERROR";
583
- if (error instanceof CapabilityNotFoundError) {
584
- errorCode = "CAPABILITY_NOT_FOUND";
585
- } else if (error instanceof PluginNotFoundError) {
586
- errorCode = "PLUGIN_NOT_FOUND";
587
- } else if (error instanceof ActionNotFoundError) {
588
- errorCode = "ACTION_NOT_FOUND";
589
- } else if (error instanceof HttpException) {
590
- const response = error.getResponse();
591
- errorCode = response.error ?? "EXECUTION_ERROR";
592
- }
593
- res.write(`data: ${JSON.stringify({
594
- error: message,
595
- code: errorCode
596
- })}
699
+ const errorMsg = error instanceof Error ? error.message : String(error);
700
+ const response = {
701
+ status_code: ErrorCodes.SUCCESS,
702
+ data: {
703
+ type: "error",
704
+ error: {
705
+ code: 0,
706
+ message: errorMsg
707
+ }
708
+ }
709
+ };
710
+ res.write(`data: ${JSON.stringify(response)}
597
711
 
598
712
  `);
599
- } finally {
600
713
  res.end();
601
714
  }
602
715
  }
@@ -605,7 +718,7 @@ _ts_decorate4([
605
718
  Get("list"),
606
719
  _ts_metadata2("design:type", Function),
607
720
  _ts_metadata2("design:paramtypes", []),
608
- _ts_metadata2("design:returntype", typeof ListResponse === "undefined" ? Object : ListResponse)
721
+ _ts_metadata2("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
609
722
  ], DebugController.prototype, "list", null);
610
723
  _ts_decorate4([
611
724
  Post("debug/:capability_id"),
@@ -671,51 +784,48 @@ var WebhookController = class {
671
784
  list() {
672
785
  const capabilities = this.capabilityService.listCapabilities();
673
786
  return {
674
- code: 0,
675
- message: "success",
676
- data: capabilities.map((c) => ({
677
- id: c.id,
678
- name: c.name,
679
- description: c.description,
680
- pluginKey: c.pluginKey,
681
- pluginVersion: c.pluginVersion
682
- }))
787
+ status_code: ErrorCodes.SUCCESS,
788
+ data: {
789
+ capabilities: capabilities.map((c) => ({
790
+ id: c.id,
791
+ name: c.name,
792
+ pluginID: c.pluginKey,
793
+ pluginVersion: c.pluginVersion
794
+ }))
795
+ }
683
796
  };
684
797
  }
685
798
  async execute(capabilityId, body) {
686
799
  try {
687
800
  const result = await this.capabilityService.load(capabilityId).call(body.action, body.params);
688
801
  return {
689
- code: 0,
690
- message: "success",
691
- data: result
802
+ status_code: ErrorCodes.SUCCESS,
803
+ data: {
804
+ output: result
805
+ }
692
806
  };
693
807
  } catch (error) {
694
808
  if (error instanceof CapabilityNotFoundError) {
695
809
  throw new HttpException2({
696
- code: 1,
697
- message: error.message,
698
- error: "CAPABILITY_NOT_FOUND"
810
+ status_code: ErrorCodes.CAPABILITY_NOT_FOUND,
811
+ error_msg: `Capability not found: ${capabilityId}`
699
812
  }, HttpStatus2.NOT_FOUND);
700
813
  }
701
814
  if (error instanceof PluginNotFoundError) {
702
815
  throw new HttpException2({
703
- code: 1,
704
- message: error.message,
705
- error: "PLUGIN_NOT_FOUND"
816
+ status_code: ErrorCodes.PLUGIN_NOT_FOUND,
817
+ error_msg: `Plugin not found`
706
818
  }, HttpStatus2.INTERNAL_SERVER_ERROR);
707
819
  }
708
820
  if (error instanceof ActionNotFoundError) {
709
821
  throw new HttpException2({
710
- code: 1,
711
- message: error.message,
712
- error: "ACTION_NOT_FOUND"
822
+ status_code: ErrorCodes.ACTION_NOT_FOUND,
823
+ error_msg: `Action '${body.action}' not found`
713
824
  }, HttpStatus2.BAD_REQUEST);
714
825
  }
715
826
  throw new HttpException2({
716
- code: 1,
717
- message: error instanceof Error ? error.message : String(error),
718
- error: "EXECUTION_ERROR"
827
+ status_code: ErrorCodes.EXECUTION_ERROR,
828
+ error_msg: `Execution failed: ${error instanceof Error ? error.message : String(error)}`
719
829
  }, HttpStatus2.INTERNAL_SERVER_ERROR);
720
830
  }
721
831
  }
@@ -725,30 +835,76 @@ var WebhookController = class {
725
835
  res.setHeader("Connection", "keep-alive");
726
836
  try {
727
837
  const capability = this.capabilityService.load(capabilityId);
728
- const stream = capability.callStream(body.action, body.params);
729
- for await (const chunk of stream) {
730
- res.write(`data: ${JSON.stringify(chunk)}
838
+ const eventStream = capability.callStreamWithEvents(body.action, body.params);
839
+ for await (const event of eventStream) {
840
+ switch (event.type) {
841
+ case "data": {
842
+ const response = {
843
+ status_code: ErrorCodes.SUCCESS,
844
+ data: {
845
+ type: "content",
846
+ delta: {
847
+ content: event.data
848
+ }
849
+ }
850
+ };
851
+ res.write(`data: ${JSON.stringify(response)}
852
+
853
+ `);
854
+ break;
855
+ }
856
+ case "done": {
857
+ const response = {
858
+ status_code: ErrorCodes.SUCCESS,
859
+ data: {
860
+ type: "content",
861
+ delta: {
862
+ content: null
863
+ },
864
+ finished: true
865
+ }
866
+ };
867
+ res.write(`data: ${JSON.stringify(response)}
868
+
869
+ `);
870
+ res.end();
871
+ return;
872
+ }
873
+ case "error": {
874
+ const response = {
875
+ status_code: ErrorCodes.SUCCESS,
876
+ data: {
877
+ type: "error",
878
+ error: {
879
+ code: 0,
880
+ message: event.error.message
881
+ }
882
+ }
883
+ };
884
+ res.write(`data: ${JSON.stringify(response)}
731
885
 
732
886
  `);
887
+ res.end();
888
+ return;
889
+ }
890
+ }
733
891
  }
734
- res.write("data: [DONE]\n\n");
892
+ res.end();
735
893
  } catch (error) {
736
- const message = error instanceof Error ? error.message : String(error);
737
- let errorCode = "EXECUTION_ERROR";
738
- if (error instanceof CapabilityNotFoundError) {
739
- errorCode = "CAPABILITY_NOT_FOUND";
740
- } else if (error instanceof PluginNotFoundError) {
741
- errorCode = "PLUGIN_NOT_FOUND";
742
- } else if (error instanceof ActionNotFoundError) {
743
- errorCode = "ACTION_NOT_FOUND";
744
- }
745
- res.write(`data: ${JSON.stringify({
746
- error: message,
747
- code: errorCode
748
- })}
894
+ const errorMsg = error instanceof Error ? error.message : String(error);
895
+ const response = {
896
+ status_code: ErrorCodes.SUCCESS,
897
+ data: {
898
+ type: "error",
899
+ error: {
900
+ code: 0,
901
+ message: errorMsg
902
+ }
903
+ }
904
+ };
905
+ res.write(`data: ${JSON.stringify(response)}
749
906
 
750
907
  `);
751
- } finally {
752
908
  res.end();
753
909
  }
754
910
  }
@@ -757,7 +913,7 @@ _ts_decorate5([
757
913
  Get2("list"),
758
914
  _ts_metadata3("design:type", Function),
759
915
  _ts_metadata3("design:paramtypes", []),
760
- _ts_metadata3("design:returntype", typeof ListResponse === "undefined" ? Object : ListResponse)
916
+ _ts_metadata3("design:returntype", typeof SuccessResponse === "undefined" ? Object : SuccessResponse)
761
917
  ], WebhookController.prototype, "list", null);
762
918
  _ts_decorate5([
763
919
  Post2(":capability_id"),
@@ -871,6 +1027,7 @@ export {
871
1027
  CapabilityNotFoundError,
872
1028
  CapabilityService,
873
1029
  DebugController,
1030
+ ErrorCodes,
874
1031
  PluginLoadError,
875
1032
  PluginLoaderService,
876
1033
  PluginNotFoundError,