supercompat 1.0.5 → 1.1.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/dist/index.cjs CHANGED
@@ -508,11 +508,11 @@ var serializeMessage = function(param) {
508
508
  // src/adapters/run/completionsRunAdapter/messages.ts
509
509
  var messages = function() {
510
510
  var _ref = _async_to_generator(function(param) {
511
- var run, getMessages2, _, _1, _2;
511
+ var run, getMessages2, messagesHistoryLength, _, _1, _2;
512
512
  return _ts_generator(this, function(_state) {
513
513
  switch(_state.label){
514
514
  case 0:
515
- run = param.run, getMessages2 = param.getMessages;
515
+ run = param.run, getMessages2 = param.getMessages, messagesHistoryLength = param.messagesHistoryLength;
516
516
  _1 = (_ = _to_consumable_array(run.instructions ? [
517
517
  {
518
518
  role: "system",
@@ -522,7 +522,9 @@ var messages = function() {
522
522
  _2 = (0, import_radash2.flat);
523
523
  return [
524
524
  4,
525
- getMessages2()
525
+ getMessages2({
526
+ messagesHistoryLength: messagesHistoryLength
527
+ })
526
528
  ];
527
529
  case 1:
528
530
  return [
@@ -614,177 +616,110 @@ var toolCallsData = function(param) {
614
616
  }
615
617
  return newToolCalls;
616
618
  };
617
- var completionsRunAdapter = function() {
618
- var _ref = _async_to_generator(function(param) {
619
- var client, run, onEvent2, getMessages2, opts, _tmp, providerResponse, e, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, delta, _delta_content, err;
620
- return _ts_generator(this, function(_state) {
621
- switch(_state.label){
622
- case 0:
623
- client = param.client, run = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages;
624
- if (run.status !== "queued") return [
625
- 2
626
- ];
627
- onEvent2({
628
- event: "thread.run.in_progress",
629
- data: _object_spread_props(_object_spread({}, run), {
630
- status: "in_progress"
631
- })
632
- });
633
- _tmp = {};
634
- return [
635
- 4,
636
- messages({
637
- run: run,
638
- getMessages: getMessages2
639
- })
640
- ];
641
- case 1:
642
- opts = _object_spread.apply(void 0, [
643
- (_tmp.messages = _state.sent(), _tmp.model = run.model, _tmp.stream = true, _tmp),
644
- (0, import_radash3.isEmpty)(run.tools) ? {} : {
645
- tools: run.tools
646
- }
647
- ]);
648
- console.dir({
649
- opts: opts
650
- }, {
651
- depth: null
652
- });
653
- _state.label = 2;
654
- case 2:
655
- _state.trys.push([
656
- 2,
657
- 4,
658
- ,
659
- 5
660
- ]);
661
- return [
662
- 4,
663
- client.chat.completions.create(opts)
664
- ];
665
- case 3:
666
- providerResponse = _state.sent();
667
- return [
668
- 3,
669
- 5
670
- ];
671
- case 4:
672
- e = _state.sent();
673
- console.error(e);
674
- return [
675
- 2,
619
+ var completionsRunAdapter = function(param) {
620
+ var _param_messagesHistoryLength = param.messagesHistoryLength, messagesHistoryLength = _param_messagesHistoryLength === void 0 ? 10 : _param_messagesHistoryLength;
621
+ return function() {
622
+ var _ref = _async_to_generator(function(param) {
623
+ var client, run, onEvent2, getMessages2, opts, _tmp, providerResponse, e, message, toolCallsRunStep, currentContent, currentToolCalls, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk, delta, _delta_content, err;
624
+ return _ts_generator(this, function(_state) {
625
+ switch(_state.label){
626
+ case 0:
627
+ client = param.client, run = param.run, onEvent2 = param.onEvent, getMessages2 = param.getMessages;
628
+ if (run.status !== "queued") return [
629
+ 2
630
+ ];
676
631
  onEvent2({
677
- event: "thread.run.failed",
632
+ event: "thread.run.in_progress",
678
633
  data: _object_spread_props(_object_spread({}, run), {
679
- failed_at: (0, import_dayjs.default)().unix(),
680
- status: "in_progress",
681
- last_error: e.message
682
- })
683
- })
684
- ];
685
- case 5:
686
- return [
687
- 4,
688
- onEvent2({
689
- event: "thread.message.created",
690
- data: {
691
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
692
- object: "thread.message",
693
- completed_at: null,
694
- run_id: run.id,
695
- created_at: (0, import_dayjs.default)().unix(),
696
- assistant_id: run.assistant_id,
697
- incomplete_at: null,
698
- incomplete_details: null,
699
- metadata: {},
700
- file_ids: [],
701
- thread_id: run.thread_id,
702
- content: [
703
- {
704
- text: {
705
- value: "",
706
- annotations: []
707
- },
708
- type: "text"
709
- }
710
- ],
711
- role: "assistant",
712
634
  status: "in_progress"
635
+ })
636
+ });
637
+ _tmp = {};
638
+ return [
639
+ 4,
640
+ messages({
641
+ run: run,
642
+ getMessages: getMessages2,
643
+ messagesHistoryLength: messagesHistoryLength
644
+ })
645
+ ];
646
+ case 1:
647
+ opts = _object_spread.apply(void 0, [
648
+ (_tmp.messages = _state.sent(), _tmp.model = run.model, _tmp.stream = true, _tmp),
649
+ (0, import_radash3.isEmpty)(run.tools) ? {} : {
650
+ tools: run.tools
713
651
  }
714
- })
715
- ];
716
- case 6:
717
- message = _state.sent();
718
- onEvent2({
719
- event: "thread.run.step.created",
720
- data: {
721
- id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
722
- object: "thread.run.step",
723
- run_id: run.id,
724
- assistant_id: run.assistant_id,
725
- thread_id: run.thread_id,
726
- type: "message_creation",
727
- status: "completed",
728
- completed_at: (0, import_dayjs.default)().unix(),
729
- created_at: (0, import_dayjs.default)().unix(),
730
- expired_at: null,
731
- last_error: null,
732
- metadata: {},
733
- failed_at: null,
734
- cancelled_at: null,
735
- usage: null,
736
- step_details: {
737
- type: "message_creation",
738
- message_creation: {
739
- message_id: message.id
652
+ ]);
653
+ console.dir({
654
+ opts: opts
655
+ }, {
656
+ depth: null
657
+ });
658
+ _state.label = 2;
659
+ case 2:
660
+ _state.trys.push([
661
+ 2,
662
+ 4,
663
+ ,
664
+ 5
665
+ ]);
666
+ return [
667
+ 4,
668
+ client.chat.completions.create(opts)
669
+ ];
670
+ case 3:
671
+ providerResponse = _state.sent();
672
+ return [
673
+ 3,
674
+ 5
675
+ ];
676
+ case 4:
677
+ e = _state.sent();
678
+ console.error(e);
679
+ return [
680
+ 2,
681
+ onEvent2({
682
+ event: "thread.run.failed",
683
+ data: _object_spread_props(_object_spread({}, run), {
684
+ failed_at: (0, import_dayjs.default)().unix(),
685
+ status: "in_progress",
686
+ last_error: e.message
687
+ })
688
+ })
689
+ ];
690
+ case 5:
691
+ return [
692
+ 4,
693
+ onEvent2({
694
+ event: "thread.message.created",
695
+ data: {
696
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
697
+ object: "thread.message",
698
+ completed_at: null,
699
+ run_id: run.id,
700
+ created_at: (0, import_dayjs.default)().unix(),
701
+ assistant_id: run.assistant_id,
702
+ incomplete_at: null,
703
+ incomplete_details: null,
704
+ metadata: {},
705
+ file_ids: [],
706
+ thread_id: run.thread_id,
707
+ content: [
708
+ {
709
+ text: {
710
+ value: "",
711
+ annotations: []
712
+ },
713
+ type: "text"
714
+ }
715
+ ],
716
+ role: "assistant",
717
+ status: "in_progress"
740
718
  }
741
- }
742
- }
743
- });
744
- currentContent = "";
745
- console.dir({
746
- providerResponse: providerResponse
747
- }, {
748
- depth: null
749
- });
750
- _iteratorAbruptCompletion = false, _didIteratorError = false;
751
- _state.label = 7;
752
- case 7:
753
- _state.trys.push([
754
- 7,
755
- 15,
756
- 16,
757
- 21
758
- ]);
759
- _iterator = _async_iterator(providerResponse);
760
- _state.label = 8;
761
- case 8:
762
- return [
763
- 4,
764
- _iterator.next()
765
- ];
766
- case 9:
767
- if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
768
- 3,
769
- 14
770
- ];
771
- _value = _step.value;
772
- chunk = _value;
773
- delta = chunk.choices[0].delta;
774
- if (delta.content) {
775
- ;
776
- currentContent = "".concat(currentContent).concat((_delta_content = delta.content) !== null && _delta_content !== void 0 ? _delta_content : "");
777
- }
778
- if (!delta.tool_calls) return [
779
- 3,
780
- 12
781
- ];
782
- if (!!toolCallsRunStep) return [
783
- 3,
784
- 11
785
- ];
786
- return [
787
- 4,
719
+ })
720
+ ];
721
+ case 6:
722
+ message = _state.sent();
788
723
  onEvent2({
789
724
  event: "thread.run.step.created",
790
725
  data: {
@@ -793,9 +728,9 @@ var completionsRunAdapter = function() {
793
728
  run_id: run.id,
794
729
  assistant_id: run.assistant_id,
795
730
  thread_id: run.thread_id,
796
- type: "tool_calls",
797
- status: "in_progress",
798
- completed_at: null,
731
+ type: "message_creation",
732
+ status: "completed",
733
+ completed_at: (0, import_dayjs.default)().unix(),
799
734
  created_at: (0, import_dayjs.default)().unix(),
800
735
  expired_at: null,
801
736
  last_error: null,
@@ -804,170 +739,241 @@ var completionsRunAdapter = function() {
804
739
  cancelled_at: null,
805
740
  usage: null,
806
741
  step_details: {
807
- type: "tool_calls",
808
- tool_calls: []
742
+ type: "message_creation",
743
+ message_creation: {
744
+ message_id: message.id
745
+ }
809
746
  }
810
747
  }
811
- })
812
- ];
813
- case 10:
814
- toolCallsRunStep = _state.sent();
815
- _state.label = 11;
816
- case 11:
817
- onEvent2({
818
- event: "thread.run.step.delta",
819
- data: {
820
- object: "thread.run.step.delta",
821
- run_id: run.id,
822
- id: toolCallsRunStep.id,
823
- delta: {
824
- step_details: {
748
+ });
749
+ currentContent = "";
750
+ console.dir({
751
+ providerResponse: providerResponse
752
+ }, {
753
+ depth: null
754
+ });
755
+ _iteratorAbruptCompletion = false, _didIteratorError = false;
756
+ _state.label = 7;
757
+ case 7:
758
+ _state.trys.push([
759
+ 7,
760
+ 15,
761
+ 16,
762
+ 21
763
+ ]);
764
+ _iterator = _async_iterator(providerResponse);
765
+ _state.label = 8;
766
+ case 8:
767
+ return [
768
+ 4,
769
+ _iterator.next()
770
+ ];
771
+ case 9:
772
+ if (!(_iteratorAbruptCompletion = !(_step = _state.sent()).done)) return [
773
+ 3,
774
+ 14
775
+ ];
776
+ _value = _step.value;
777
+ chunk = _value;
778
+ delta = chunk.choices[0].delta;
779
+ if (delta.content) {
780
+ ;
781
+ currentContent = "".concat(currentContent).concat((_delta_content = delta.content) !== null && _delta_content !== void 0 ? _delta_content : "");
782
+ }
783
+ if (!delta.tool_calls) return [
784
+ 3,
785
+ 12
786
+ ];
787
+ if (!!toolCallsRunStep) return [
788
+ 3,
789
+ 11
790
+ ];
791
+ return [
792
+ 4,
793
+ onEvent2({
794
+ event: "thread.run.step.created",
795
+ data: {
796
+ id: "THERE_IS_A_BUG_IN_SUPERCOMPAT_IF_YOU_SEE_THIS_ID",
797
+ object: "thread.run.step",
798
+ run_id: run.id,
799
+ assistant_id: run.assistant_id,
800
+ thread_id: run.thread_id,
825
801
  type: "tool_calls",
826
- tool_calls: delta.tool_calls.map(function(tc) {
827
- return _object_spread({
828
- id: (0, import_radash3.uid)(24),
829
- type: "function"
830
- }, tc);
831
- })
802
+ status: "in_progress",
803
+ completed_at: null,
804
+ created_at: (0, import_dayjs.default)().unix(),
805
+ expired_at: null,
806
+ last_error: null,
807
+ metadata: {},
808
+ failed_at: null,
809
+ cancelled_at: null,
810
+ usage: null,
811
+ step_details: {
812
+ type: "tool_calls",
813
+ tool_calls: []
814
+ }
832
815
  }
833
- }
834
- }
835
- });
836
- currentToolCalls = toolCallsData({
837
- prevToolCalls: currentToolCalls,
838
- delta: delta
839
- });
840
- _state.label = 12;
841
- case 12:
842
- if (delta.content) {
816
+ })
817
+ ];
818
+ case 10:
819
+ toolCallsRunStep = _state.sent();
820
+ _state.label = 11;
821
+ case 11:
843
822
  onEvent2({
844
- event: "thread.message.delta",
823
+ event: "thread.run.step.delta",
845
824
  data: {
846
- id: message.id,
825
+ object: "thread.run.step.delta",
826
+ run_id: run.id,
827
+ id: toolCallsRunStep.id,
847
828
  delta: {
848
- content: [
849
- {
850
- type: "text",
851
- index: 0,
852
- text: {
853
- value: delta.content
854
- }
855
- }
856
- ]
829
+ step_details: {
830
+ type: "tool_calls",
831
+ tool_calls: delta.tool_calls.map(function(tc) {
832
+ return _object_spread({
833
+ id: (0, import_radash3.uid)(24),
834
+ type: "function"
835
+ }, tc);
836
+ })
837
+ }
857
838
  }
858
839
  }
859
840
  });
860
- }
861
- _state.label = 13;
862
- case 13:
863
- _iteratorAbruptCompletion = false;
864
- return [
865
- 3,
866
- 8
867
- ];
868
- case 14:
869
- return [
870
- 3,
871
- 21
872
- ];
873
- case 15:
874
- err = _state.sent();
875
- _didIteratorError = true;
876
- _iteratorError = err;
877
- return [
878
- 3,
879
- 21
880
- ];
881
- case 16:
882
- _state.trys.push([
883
- 16,
884
- ,
885
- 19,
886
- 20
887
- ]);
888
- if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
889
- 3,
890
- 18
891
- ];
892
- return [
893
- 4,
894
- _iterator.return()
895
- ];
896
- case 17:
897
- _state.sent();
898
- _state.label = 18;
899
- case 18:
900
- return [
901
- 3,
902
- 20
903
- ];
904
- case 19:
905
- if (_didIteratorError) {
906
- throw _iteratorError;
907
- }
908
- return [
909
- 7
910
- ];
911
- case 20:
912
- return [
913
- 7
914
- ];
915
- case 21:
916
- return [
917
- 4,
918
- onEvent2({
919
- event: "thread.message.completed",
920
- data: _object_spread_props(_object_spread({}, message), {
921
- status: "completed",
922
- content: [
923
- {
924
- text: {
925
- value: currentContent,
926
- annotations: []
927
- },
928
- type: "text"
841
+ currentToolCalls = toolCallsData({
842
+ prevToolCalls: currentToolCalls,
843
+ delta: delta
844
+ });
845
+ _state.label = 12;
846
+ case 12:
847
+ if (delta.content) {
848
+ onEvent2({
849
+ event: "thread.message.delta",
850
+ data: {
851
+ id: message.id,
852
+ delta: {
853
+ content: [
854
+ {
855
+ type: "text",
856
+ index: 0,
857
+ text: {
858
+ value: delta.content
859
+ }
860
+ }
861
+ ]
929
862
  }
930
- ],
931
- tool_calls: currentToolCalls
932
- })
933
- })
934
- ];
935
- case 22:
936
- message = _state.sent();
937
- if ((0, import_radash3.isEmpty)(message.toolCalls)) {
863
+ }
864
+ });
865
+ }
866
+ _state.label = 13;
867
+ case 13:
868
+ _iteratorAbruptCompletion = false;
938
869
  return [
939
- 2,
870
+ 3,
871
+ 8
872
+ ];
873
+ case 14:
874
+ return [
875
+ 3,
876
+ 21
877
+ ];
878
+ case 15:
879
+ err = _state.sent();
880
+ _didIteratorError = true;
881
+ _iteratorError = err;
882
+ return [
883
+ 3,
884
+ 21
885
+ ];
886
+ case 16:
887
+ _state.trys.push([
888
+ 16,
889
+ ,
890
+ 19,
891
+ 20
892
+ ]);
893
+ if (!(_iteratorAbruptCompletion && _iterator.return != null)) return [
894
+ 3,
895
+ 18
896
+ ];
897
+ return [
898
+ 4,
899
+ _iterator.return()
900
+ ];
901
+ case 17:
902
+ _state.sent();
903
+ _state.label = 18;
904
+ case 18:
905
+ return [
906
+ 3,
907
+ 20
908
+ ];
909
+ case 19:
910
+ if (_didIteratorError) {
911
+ throw _iteratorError;
912
+ }
913
+ return [
914
+ 7
915
+ ];
916
+ case 20:
917
+ return [
918
+ 7
919
+ ];
920
+ case 21:
921
+ return [
922
+ 4,
940
923
  onEvent2({
941
- event: "thread.run.completed",
942
- data: _object_spread_props(_object_spread({}, run), {
924
+ event: "thread.message.completed",
925
+ data: _object_spread_props(_object_spread({}, message), {
943
926
  status: "completed",
944
- completed_at: (0, import_dayjs.default)().unix()
927
+ content: [
928
+ {
929
+ text: {
930
+ value: currentContent,
931
+ annotations: []
932
+ },
933
+ type: "text"
934
+ }
935
+ ],
936
+ tool_calls: currentToolCalls
945
937
  })
946
938
  })
947
939
  ];
948
- }
949
- return [
950
- 2,
951
- onEvent2({
952
- event: "thread.run.requires_action",
953
- data: _object_spread_props(_object_spread({}, run), {
954
- status: "requires_action",
955
- required_action: {
956
- type: "submit_tool_outputs",
957
- submit_tool_outputs: {
958
- tool_calls: message.toolCalls
940
+ case 22:
941
+ message = _state.sent();
942
+ if ((0, import_radash3.isEmpty)(message.toolCalls)) {
943
+ return [
944
+ 2,
945
+ onEvent2({
946
+ event: "thread.run.completed",
947
+ data: _object_spread_props(_object_spread({}, run), {
948
+ status: "completed",
949
+ completed_at: (0, import_dayjs.default)().unix()
950
+ })
951
+ })
952
+ ];
953
+ }
954
+ return [
955
+ 2,
956
+ onEvent2({
957
+ event: "thread.run.requires_action",
958
+ data: _object_spread_props(_object_spread({}, run), {
959
+ status: "requires_action",
960
+ required_action: {
961
+ type: "submit_tool_outputs",
962
+ submit_tool_outputs: {
963
+ tool_calls: message.toolCalls
964
+ }
959
965
  }
960
- }
966
+ })
961
967
  })
962
- })
963
- ];
964
- }
968
+ ];
969
+ }
970
+ });
965
971
  });
966
- });
967
- return function completionsRunAdapter(_) {
968
- return _ref.apply(this, arguments);
969
- };
970
- }();
972
+ return function(_) {
973
+ return _ref.apply(this, arguments);
974
+ };
975
+ }();
976
+ };
971
977
  // src/adapters/storage/prismaStorageAdapter/beta/threads/create.ts
972
978
  var import_dayjs3 = __toESM(require("dayjs"), 1);
973
979
  // src/adapters/storage/prismaStorageAdapter/beta/threads/serializeThread.ts
@@ -1634,53 +1640,59 @@ var onEvent = function(param) {
1634
1640
  // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/getMessages.ts
1635
1641
  var getMessages = function(param) {
1636
1642
  var prisma = param.prisma, run = param.run;
1637
- return /*#__PURE__*/ _async_to_generator(function() {
1638
- var messages3;
1639
- return _ts_generator(this, function(_state) {
1640
- switch(_state.label){
1641
- case 0:
1642
- return [
1643
- 4,
1644
- prisma.message.findMany({
1645
- where: {
1646
- threadId: run.threadId
1647
- },
1648
- include: {
1649
- run: {
1650
- include: {
1651
- runSteps: true
1643
+ return function() {
1644
+ var _ref = _async_to_generator(function(param) {
1645
+ var messagesHistoryLength, messages3;
1646
+ return _ts_generator(this, function(_state) {
1647
+ switch(_state.label){
1648
+ case 0:
1649
+ messagesHistoryLength = param.messagesHistoryLength;
1650
+ return [
1651
+ 4,
1652
+ prisma.message.findMany({
1653
+ where: {
1654
+ threadId: run.threadId
1655
+ },
1656
+ include: {
1657
+ run: {
1658
+ include: {
1659
+ runSteps: true
1660
+ }
1652
1661
  }
1662
+ },
1663
+ take: -messagesHistoryLength,
1664
+ orderBy: {
1665
+ createdAt: "asc"
1653
1666
  }
1654
- },
1655
- take: -10,
1656
- orderBy: {
1657
- createdAt: "asc"
1658
- }
1659
- })
1660
- ];
1661
- case 1:
1662
- messages3 = _state.sent();
1663
- return [
1664
- 2,
1665
- messages3.map(function(message) {
1666
- return _object_spread_props(_object_spread({}, serializeMessage2({
1667
- message: message
1668
- })), {
1669
- run: message.run ? _object_spread_props(_object_spread({}, serializeRun({
1670
- run: message.run
1667
+ })
1668
+ ];
1669
+ case 1:
1670
+ messages3 = _state.sent();
1671
+ return [
1672
+ 2,
1673
+ messages3.map(function(message) {
1674
+ return _object_spread_props(_object_spread({}, serializeMessage2({
1675
+ message: message
1671
1676
  })), {
1672
- runSteps: message.run.runSteps.map(function(runStep) {
1673
- return serializeRunStep({
1674
- runStep: runStep
1675
- });
1676
- })
1677
- }) : null
1678
- });
1679
- })
1680
- ];
1681
- }
1677
+ run: message.run ? _object_spread_props(_object_spread({}, serializeRun({
1678
+ run: message.run
1679
+ })), {
1680
+ runSteps: message.run.runSteps.map(function(runStep) {
1681
+ return serializeRunStep({
1682
+ runStep: runStep
1683
+ });
1684
+ })
1685
+ }) : null
1686
+ });
1687
+ })
1688
+ ];
1689
+ }
1690
+ });
1682
1691
  });
1683
- });
1692
+ return function(_) {
1693
+ return _ref.apply(this, arguments);
1694
+ };
1695
+ }();
1684
1696
  };
1685
1697
  // src/adapters/storage/prismaStorageAdapter/beta/threads/runs/createAndStream.ts
1686
1698
  var createAndStream = function(param) {