@copilotkit/runtime 1.4.6 → 1.4.8-coagents-v0-3.1

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +69 -5
  2. package/__snapshots__/schema/schema.graphql +7 -9
  3. package/dist/{chunk-OKQVDDJ2.mjs → chunk-2PK2SFRB.mjs} +220 -57
  4. package/dist/chunk-2PK2SFRB.mjs.map +1 -0
  5. package/dist/{chunk-37LZS4PV.mjs → chunk-BACNNHHI.mjs} +2 -2
  6. package/dist/{chunk-55FONM7Y.mjs → chunk-FVYNRYIB.mjs} +420 -296
  7. package/dist/chunk-FVYNRYIB.mjs.map +1 -0
  8. package/dist/{chunk-6KZNQI7Z.mjs → chunk-MQJNZYAH.mjs} +2 -2
  9. package/dist/{chunk-B74M7FXG.mjs → chunk-RFF5IIZJ.mjs} +3 -2
  10. package/dist/chunk-RFF5IIZJ.mjs.map +1 -0
  11. package/dist/{chunk-XA3KCJ6P.mjs → chunk-YFG3Q3YH.mjs} +2 -2
  12. package/dist/{copilot-runtime-12e7ac40.d.ts → copilot-runtime-b15b683d.d.ts} +2 -2
  13. package/dist/graphql/types/converted/index.d.ts +1 -1
  14. package/dist/graphql/types/converted/index.js +2 -1
  15. package/dist/graphql/types/converted/index.js.map +1 -1
  16. package/dist/graphql/types/converted/index.mjs +1 -1
  17. package/dist/{groq-adapter-24abe931.d.ts → groq-adapter-50aa9621.d.ts} +1 -1
  18. package/dist/{index-10b1c870.d.ts → index-ff3fbc33.d.ts} +7 -8
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +725 -437
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +6 -6
  23. package/dist/{langserve-f021ab9c.d.ts → langserve-8ec29cba.d.ts} +51 -12
  24. package/dist/lib/index.d.ts +4 -4
  25. package/dist/lib/index.js +703 -431
  26. package/dist/lib/index.js.map +1 -1
  27. package/dist/lib/index.mjs +6 -6
  28. package/dist/lib/integrations/index.d.ts +4 -4
  29. package/dist/lib/integrations/index.js +71 -30
  30. package/dist/lib/integrations/index.js.map +1 -1
  31. package/dist/lib/integrations/index.mjs +6 -6
  32. package/dist/lib/integrations/nest/index.d.ts +3 -3
  33. package/dist/lib/integrations/nest/index.js +71 -30
  34. package/dist/lib/integrations/nest/index.js.map +1 -1
  35. package/dist/lib/integrations/nest/index.mjs +4 -4
  36. package/dist/lib/integrations/node-express/index.d.ts +3 -3
  37. package/dist/lib/integrations/node-express/index.js +71 -30
  38. package/dist/lib/integrations/node-express/index.js.map +1 -1
  39. package/dist/lib/integrations/node-express/index.mjs +4 -4
  40. package/dist/lib/integrations/node-http/index.d.ts +3 -3
  41. package/dist/lib/integrations/node-http/index.js +71 -30
  42. package/dist/lib/integrations/node-http/index.js.map +1 -1
  43. package/dist/lib/integrations/node-http/index.mjs +3 -3
  44. package/dist/service-adapters/index.d.ts +4 -4
  45. package/dist/service-adapters/index.js +219 -56
  46. package/dist/service-adapters/index.js.map +1 -1
  47. package/dist/service-adapters/index.mjs +1 -1
  48. package/package.json +2 -2
  49. package/src/agents/langgraph/event-source.ts +140 -148
  50. package/src/agents/langgraph/events.ts +1 -1
  51. package/src/graphql/inputs/message.input.ts +15 -3
  52. package/src/graphql/resolvers/copilot.resolver.ts +32 -6
  53. package/src/graphql/types/converted/index.ts +4 -3
  54. package/src/graphql/types/copilot-response.type.ts +12 -3
  55. package/src/graphql/types/enums.ts +0 -11
  56. package/src/lib/runtime/remote-action-constructors.ts +4 -2
  57. package/src/lib/runtime/remote-actions.ts +1 -0
  58. package/src/lib/runtime/remote-lg-action.ts +161 -140
  59. package/src/service-adapters/anthropic/anthropic-adapter.ts +16 -6
  60. package/src/service-adapters/conversion.ts +2 -1
  61. package/src/service-adapters/events.ts +111 -52
  62. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +7 -3
  63. package/src/service-adapters/groq/groq-adapter.ts +23 -8
  64. package/src/service-adapters/langchain/utils.ts +49 -31
  65. package/src/service-adapters/openai/openai-adapter.ts +22 -9
  66. package/src/service-adapters/openai/openai-assistant-adapter.ts +22 -8
  67. package/src/service-adapters/unify/unify-adapter.ts +28 -11
  68. package/dist/chunk-55FONM7Y.mjs.map +0 -1
  69. package/dist/chunk-B74M7FXG.mjs.map +0 -1
  70. package/dist/chunk-OKQVDDJ2.mjs.map +0 -1
  71. /package/dist/{chunk-37LZS4PV.mjs.map → chunk-BACNNHHI.mjs.map} +0 -0
  72. /package/dist/{chunk-6KZNQI7Z.mjs.map → chunk-MQJNZYAH.mjs.map} +0 -0
  73. /package/dist/{chunk-XA3KCJ6P.mjs.map → chunk-YFG3Q3YH.mjs.map} +0 -0
package/dist/lib/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.4.6",
47
+ version: "1.4.8-coagents-v0-3.1",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -424,6 +424,8 @@ var OpenAIAdapter = class {
424
424
  eventSource.stream(async (eventStream$) => {
425
425
  var _a, _b;
426
426
  let mode = null;
427
+ let currentMessageId;
428
+ let currentToolCallId;
427
429
  for await (const chunk of stream) {
428
430
  if (chunk.choices.length === 0) {
429
431
  continue;
@@ -432,30 +434,52 @@ var OpenAIAdapter = class {
432
434
  const content = chunk.choices[0].delta.content;
433
435
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
434
436
  mode = null;
435
- eventStream$.sendTextMessageEnd();
437
+ eventStream$.sendTextMessageEnd({
438
+ messageId: currentMessageId
439
+ });
436
440
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
437
441
  mode = null;
438
- eventStream$.sendActionExecutionEnd();
442
+ eventStream$.sendActionExecutionEnd({
443
+ actionExecutionId: currentToolCallId
444
+ });
439
445
  }
440
446
  if (mode === null) {
441
447
  if (toolCall == null ? void 0 : toolCall.id) {
442
448
  mode = "function";
443
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
449
+ currentToolCallId = toolCall.id;
450
+ eventStream$.sendActionExecutionStart({
451
+ actionExecutionId: currentToolCallId,
452
+ parentMessageId: chunk.id,
453
+ actionName: toolCall.function.name
454
+ });
444
455
  } else if (content) {
445
456
  mode = "message";
446
- eventStream$.sendTextMessageStart(chunk.id);
457
+ currentMessageId = chunk.id;
458
+ eventStream$.sendTextMessageStart({
459
+ messageId: currentMessageId
460
+ });
447
461
  }
448
462
  }
449
463
  if (mode === "message" && content) {
450
- eventStream$.sendTextMessageContent(content);
464
+ eventStream$.sendTextMessageContent({
465
+ messageId: currentMessageId,
466
+ content
467
+ });
451
468
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
452
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
469
+ eventStream$.sendActionExecutionArgs({
470
+ actionExecutionId: currentToolCallId,
471
+ args: toolCall.function.arguments
472
+ });
453
473
  }
454
474
  }
455
475
  if (mode === "message") {
456
- eventStream$.sendTextMessageEnd();
476
+ eventStream$.sendTextMessageEnd({
477
+ messageId: currentMessageId
478
+ });
457
479
  } else if (mode === "function") {
458
- eventStream$.sendActionExecutionEnd();
480
+ eventStream$.sendActionExecutionEnd({
481
+ actionExecutionId: currentToolCallId
482
+ });
459
483
  }
460
484
  eventStream$.complete();
461
485
  });
@@ -523,17 +547,25 @@ function isBaseMessageChunk(message) {
523
547
  __name(isBaseMessageChunk, "isBaseMessageChunk");
524
548
  function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
525
549
  if (actionExecution) {
526
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
550
+ eventStream$.sendActionExecutionResult({
551
+ actionExecutionId: actionExecution.id,
552
+ actionName: actionExecution.name,
553
+ result: "Sending a message"
554
+ });
527
555
  }
528
556
  }
529
557
  __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
530
558
  async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
531
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
559
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
532
560
  if (typeof result === "string") {
533
561
  if (!actionExecution) {
534
562
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
535
563
  } else {
536
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
564
+ eventStream$.sendActionExecutionResult({
565
+ actionExecutionId: actionExecution.id,
566
+ actionName: actionExecution.name,
567
+ result
568
+ });
537
569
  }
538
570
  } else if (isAIMessage(result)) {
539
571
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -541,7 +573,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
541
573
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
542
574
  }
543
575
  for (const toolCall of result.tool_calls) {
544
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
576
+ eventStream$.sendActionExecution({
577
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
578
+ actionName: toolCall.name,
579
+ args: JSON.stringify(toolCall.args)
580
+ });
545
581
  }
546
582
  } else if (isBaseMessageChunk(result)) {
547
583
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -550,13 +586,18 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
550
586
  }
551
587
  if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
552
588
  for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
553
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
589
+ eventStream$.sendActionExecution({
590
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
591
+ actionName: toolCall.name,
592
+ args: JSON.stringify(toolCall.args)
593
+ });
554
594
  }
555
595
  }
556
596
  } else if (result && "getReader" in result) {
557
597
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
558
598
  let reader = result.getReader();
559
599
  let mode = null;
600
+ let currentMessageId;
560
601
  const toolCallDetails = {
561
602
  name: null,
562
603
  id: null,
@@ -595,10 +636,14 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
595
636
  }
596
637
  if (mode === "message" && (toolCallId || done)) {
597
638
  mode = null;
598
- eventStream$.sendTextMessageEnd();
639
+ eventStream$.sendTextMessageEnd({
640
+ messageId: currentMessageId
641
+ });
599
642
  } else if (mode === "function" && (!hasToolCall || done)) {
600
643
  mode = null;
601
- eventStream$.sendActionExecutionEnd();
644
+ eventStream$.sendActionExecutionEnd({
645
+ actionExecutionId: toolCallId
646
+ });
602
647
  }
603
648
  if (done) {
604
649
  break;
@@ -606,21 +651,40 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
606
651
  if (mode === null) {
607
652
  if (hasToolCall && toolCallId && toolCallName) {
608
653
  mode = "function";
609
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
654
+ eventStream$.sendActionExecutionStart({
655
+ actionExecutionId: toolCallId,
656
+ actionName: toolCallName,
657
+ parentMessageId: (_i = value.lc_kwargs) == null ? void 0 : _i.id
658
+ });
610
659
  } else if (content) {
611
660
  mode = "message";
612
- eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
661
+ currentMessageId = ((_j = value.lc_kwargs) == null ? void 0 : _j.id) || (0, import_shared2.randomId)();
662
+ eventStream$.sendTextMessageStart({
663
+ messageId: currentMessageId
664
+ });
613
665
  }
614
666
  }
615
667
  if (mode === "message" && content) {
616
- eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
668
+ eventStream$.sendTextMessageContent({
669
+ messageId: currentMessageId,
670
+ content: Array.isArray(content) ? ((_k = content[0]) == null ? void 0 : _k.text) ?? "" : content
671
+ });
617
672
  } else if (mode === "function" && toolCallArgs) {
618
673
  if (toolCallDetails.index !== toolCallDetails.prevIndex) {
619
- eventStream$.sendActionExecutionEnd();
620
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
674
+ eventStream$.sendActionExecutionEnd({
675
+ actionExecutionId: toolCallId
676
+ });
677
+ eventStream$.sendActionExecutionStart({
678
+ actionExecutionId: toolCallId,
679
+ actionName: toolCallName,
680
+ parentMessageId: (_l = value.lc_kwargs) == null ? void 0 : _l.id
681
+ });
621
682
  toolCallDetails.prevIndex = toolCallDetails.index;
622
683
  }
623
- eventStream$.sendActionExecutionArgs(toolCallArgs);
684
+ eventStream$.sendActionExecutionArgs({
685
+ actionExecutionId: toolCallId,
686
+ args: toolCallArgs
687
+ });
624
688
  }
625
689
  } catch (error) {
626
690
  console.error("Error reading from stream", error);
@@ -628,7 +692,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
628
692
  }
629
693
  }
630
694
  } else if (actionExecution) {
631
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
695
+ eventStream$.sendActionExecutionResult({
696
+ actionExecutionId: actionExecution.id,
697
+ actionName: actionExecution.name,
698
+ result: encodeResult(result)
699
+ });
632
700
  } else {
633
701
  throw new Error("Invalid return type from LangChain function.");
634
702
  }
@@ -808,21 +876,33 @@ var OpenAIAssistantAdapter = class {
808
876
  eventSource.stream(async (eventStream$) => {
809
877
  var _a, _b, _c, _d, _e, _f;
810
878
  let inFunctionCall = false;
879
+ let currentMessageId;
880
+ let currentToolCallId;
811
881
  for await (const chunk of stream) {
812
882
  switch (chunk.event) {
813
883
  case "thread.message.created":
814
884
  if (inFunctionCall) {
815
- eventStream$.sendActionExecutionEnd();
885
+ eventStream$.sendActionExecutionEnd({
886
+ actionExecutionId: currentToolCallId
887
+ });
816
888
  }
817
- eventStream$.sendTextMessageStart(chunk.data.id);
889
+ currentMessageId = chunk.data.id;
890
+ eventStream$.sendTextMessageStart({
891
+ messageId: currentMessageId
892
+ });
818
893
  break;
819
894
  case "thread.message.delta":
820
895
  if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
821
- eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
896
+ eventStream$.sendTextMessageContent({
897
+ messageId: currentMessageId,
898
+ content: (_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value
899
+ });
822
900
  }
823
901
  break;
824
902
  case "thread.message.completed":
825
- eventStream$.sendTextMessageEnd();
903
+ eventStream$.sendTextMessageEnd({
904
+ messageId: currentMessageId
905
+ });
826
906
  break;
827
907
  case "thread.run.step.delta":
828
908
  let toolCallId;
@@ -835,18 +915,30 @@ var OpenAIAssistantAdapter = class {
835
915
  }
836
916
  if (toolCallName && toolCallId) {
837
917
  if (inFunctionCall) {
838
- eventStream$.sendActionExecutionEnd();
918
+ eventStream$.sendActionExecutionEnd({
919
+ actionExecutionId: currentToolCallId
920
+ });
839
921
  }
840
922
  inFunctionCall = true;
841
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
923
+ currentToolCallId = toolCallId;
924
+ eventStream$.sendActionExecutionStart({
925
+ actionExecutionId: currentToolCallId,
926
+ parentMessageId: chunk.data.id,
927
+ actionName: toolCallName
928
+ });
842
929
  } else if (toolCallArgs) {
843
- eventStream$.sendActionExecutionArgs(toolCallArgs);
930
+ eventStream$.sendActionExecutionArgs({
931
+ actionExecutionId: currentToolCallId,
932
+ args: toolCallArgs
933
+ });
844
934
  }
845
935
  break;
846
936
  }
847
937
  }
848
938
  if (inFunctionCall) {
849
- eventStream$.sendActionExecutionEnd();
939
+ eventStream$.sendActionExecutionEnd({
940
+ actionExecutionId: currentToolCallId
941
+ });
850
942
  }
851
943
  eventStream$.complete();
852
944
  });
@@ -899,46 +991,77 @@ var UnifyAdapter = class {
899
991
  }
900
992
  });
901
993
  let model = null;
994
+ let currentMessageId;
995
+ let currentToolCallId;
902
996
  request.eventSource.stream(async (eventStream$) => {
903
997
  var _a, _b;
904
998
  let mode = null;
905
999
  for await (const chunk of stream) {
906
1000
  if (this.start) {
907
1001
  model = chunk.model;
908
- eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
909
- eventStream$.sendTextMessageContent(`Model used: ${model}
910
- `);
911
- eventStream$.sendTextMessageEnd();
1002
+ currentMessageId = (0, import_shared4.randomId)();
1003
+ eventStream$.sendTextMessageStart({
1004
+ messageId: currentMessageId
1005
+ });
1006
+ eventStream$.sendTextMessageContent({
1007
+ messageId: currentMessageId,
1008
+ content: `Model used: ${model}
1009
+ `
1010
+ });
1011
+ eventStream$.sendTextMessageEnd({
1012
+ messageId: currentMessageId
1013
+ });
912
1014
  this.start = false;
913
1015
  }
914
1016
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
915
1017
  const content = chunk.choices[0].delta.content;
916
1018
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
917
1019
  mode = null;
918
- eventStream$.sendTextMessageEnd();
1020
+ eventStream$.sendTextMessageEnd({
1021
+ messageId: currentMessageId
1022
+ });
919
1023
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
920
1024
  mode = null;
921
- eventStream$.sendActionExecutionEnd();
1025
+ eventStream$.sendActionExecutionEnd({
1026
+ actionExecutionId: currentToolCallId
1027
+ });
922
1028
  }
923
1029
  if (mode === null) {
924
1030
  if (toolCall == null ? void 0 : toolCall.id) {
925
1031
  mode = "function";
926
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1032
+ currentToolCallId = toolCall.id;
1033
+ eventStream$.sendActionExecutionStart({
1034
+ actionExecutionId: currentToolCallId,
1035
+ actionName: toolCall.function.name
1036
+ });
927
1037
  } else if (content) {
928
1038
  mode = "message";
929
- eventStream$.sendTextMessageStart(chunk.id);
1039
+ currentMessageId = chunk.id;
1040
+ eventStream$.sendTextMessageStart({
1041
+ messageId: currentMessageId
1042
+ });
930
1043
  }
931
1044
  }
932
1045
  if (mode === "message" && content) {
933
- eventStream$.sendTextMessageContent(content);
1046
+ eventStream$.sendTextMessageContent({
1047
+ messageId: currentMessageId,
1048
+ content
1049
+ });
934
1050
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
935
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1051
+ eventStream$.sendActionExecutionArgs({
1052
+ actionExecutionId: currentToolCallId,
1053
+ args: toolCall.function.arguments
1054
+ });
936
1055
  }
937
1056
  }
938
1057
  if (mode === "message") {
939
- eventStream$.sendTextMessageEnd();
1058
+ eventStream$.sendTextMessageEnd({
1059
+ messageId: currentMessageId
1060
+ });
940
1061
  } else if (mode === "function") {
941
- eventStream$.sendActionExecutionEnd();
1062
+ eventStream$.sendActionExecutionEnd({
1063
+ actionExecutionId: currentToolCallId
1064
+ });
942
1065
  }
943
1066
  eventStream$.complete();
944
1067
  });
@@ -1004,35 +1127,59 @@ var GroqAdapter = class {
1004
1127
  eventSource.stream(async (eventStream$) => {
1005
1128
  var _a, _b;
1006
1129
  let mode = null;
1130
+ let currentMessageId;
1131
+ let currentToolCallId;
1007
1132
  for await (const chunk of stream) {
1008
1133
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
1009
1134
  const content = chunk.choices[0].delta.content;
1010
1135
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
1011
1136
  mode = null;
1012
- eventStream$.sendTextMessageEnd();
1137
+ eventStream$.sendTextMessageEnd({
1138
+ messageId: currentMessageId
1139
+ });
1013
1140
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
1014
1141
  mode = null;
1015
- eventStream$.sendActionExecutionEnd();
1142
+ eventStream$.sendActionExecutionEnd({
1143
+ actionExecutionId: currentToolCallId
1144
+ });
1016
1145
  }
1017
1146
  if (mode === null) {
1018
1147
  if (toolCall == null ? void 0 : toolCall.id) {
1019
1148
  mode = "function";
1020
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1149
+ currentToolCallId = toolCall.id;
1150
+ eventStream$.sendActionExecutionStart({
1151
+ actionExecutionId: currentToolCallId,
1152
+ actionName: toolCall.function.name,
1153
+ parentMessageId: chunk.id
1154
+ });
1021
1155
  } else if (content) {
1022
1156
  mode = "message";
1023
- eventStream$.sendTextMessageStart(chunk.id);
1157
+ currentMessageId = chunk.id;
1158
+ eventStream$.sendTextMessageStart({
1159
+ messageId: currentMessageId
1160
+ });
1024
1161
  }
1025
1162
  }
1026
1163
  if (mode === "message" && content) {
1027
- eventStream$.sendTextMessageContent(content);
1164
+ eventStream$.sendTextMessageContent({
1165
+ messageId: currentMessageId,
1166
+ content
1167
+ });
1028
1168
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
1029
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1169
+ eventStream$.sendActionExecutionArgs({
1170
+ actionExecutionId: currentToolCallId,
1171
+ args: toolCall.function.arguments
1172
+ });
1030
1173
  }
1031
1174
  }
1032
1175
  if (mode === "message") {
1033
- eventStream$.sendTextMessageEnd();
1176
+ eventStream$.sendTextMessageEnd({
1177
+ messageId: currentMessageId
1178
+ });
1034
1179
  } else if (mode === "function") {
1035
- eventStream$.sendActionExecutionEnd();
1180
+ eventStream$.sendActionExecutionEnd({
1181
+ actionExecutionId: currentToolCallId
1182
+ });
1036
1183
  }
1037
1184
  eventStream$.complete();
1038
1185
  });
@@ -1093,163 +1240,167 @@ var RemoteLangGraphEventSource = class {
1093
1240
  }
1094
1241
  return shouldEmitToolCalls === toolCallName;
1095
1242
  }
1243
+ getCurrentContent(event) {
1244
+ var _a, _b, _c, _d, _e;
1245
+ const content = ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.content) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.content);
1246
+ if (!content) {
1247
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1248
+ for (const chunk of toolCallChunks) {
1249
+ if (chunk.args) {
1250
+ return chunk.args;
1251
+ }
1252
+ }
1253
+ }
1254
+ if (typeof content === "string") {
1255
+ return content;
1256
+ } else if (Array.isArray(content) && content.length > 0) {
1257
+ return content[0].text;
1258
+ }
1259
+ return null;
1260
+ }
1261
+ getCurrentMessageId(event) {
1262
+ var _a, _b, _c, _d, _e;
1263
+ return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.id) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.id);
1264
+ }
1265
+ getCurrentToolCallChunks(event) {
1266
+ var _a, _b, _c, _d, _e;
1267
+ return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.tool_call_chunks);
1268
+ }
1269
+ getResponseMetadata(event) {
1270
+ var _a, _b, _c, _d, _e;
1271
+ return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.response_metadata) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.response_metadata);
1272
+ }
1096
1273
  processLangGraphEvents() {
1097
1274
  let lastEventWithState = null;
1098
1275
  return this.eventStream$.pipe((0, import_rxjs.scan)((acc, event) => {
1099
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1100
1276
  if (event.event === LangGraphEventTypes.OnChatModelStream) {
1101
- const content = ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.content) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.content);
1102
- if (typeof content === "string") {
1103
- acc.content = content;
1104
- } else if (Array.isArray(content) && content.length > 0) {
1105
- acc.content = content[0].text;
1106
- } else {
1107
- acc.content = null;
1108
- }
1109
- const toolCallChunks = (
1110
- // @ts-expect-error -- LangGraph Platform implementation stores data outside of kwargs
1111
- ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) ?? ((_j = (_i = event.data) == null ? void 0 : _i.chunk) == null ? void 0 : _j.tool_call_chunks)
1112
- );
1113
- const toolCallMessageId = ((_m = (_l = (_k = event.data) == null ? void 0 : _k.chunk) == null ? void 0 : _l.kwargs) == null ? void 0 : _m.id) ?? ((_o = (_n = event.data) == null ? void 0 : _n.chunk) == null ? void 0 : _o.id);
1114
- if (toolCallChunks && toolCallChunks.length > 0) {
1115
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1116
- acc.toolCallMessageId = toolCallMessageId;
1117
- if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
1118
- acc.toolCallName = toolCallChunks[0].name;
1119
- }
1120
- if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
1121
- acc.toolCallId = toolCallChunks[0].id;
1122
- }
1123
- acc.prevMessageId = acc.messageId;
1124
- acc.messageId = toolCallMessageId;
1125
- } else if (acc.content && acc.content != "") {
1126
- acc.prevMessageId = acc.messageId;
1127
- acc.messageId = toolCallMessageId;
1128
- } else {
1129
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1130
- acc.prevMessageId = acc.messageId;
1131
- }
1132
- } else {
1133
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1134
- acc.toolCallMessageId = null;
1135
- acc.prevMessageId = acc.messageId;
1136
- acc.messageId = null;
1137
- acc.toolCallName = null;
1277
+ const prevMessageId = acc.lastMessageId;
1278
+ acc.currentContent = this.getCurrentContent(event);
1279
+ acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
1280
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1281
+ const responseMetadata = this.getResponseMetadata(event);
1282
+ acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
1283
+ acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
1284
+ acc.isToolCall = toolCallChunks && toolCallChunks.length > 0;
1285
+ acc.isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
1286
+ acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
1287
+ ({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
1288
+ name: acc.lastToolCallName,
1289
+ id: acc.lastToolCallId
1290
+ });
1138
1291
  }
1139
1292
  acc.event = event;
1140
1293
  lastEventWithState = acc;
1141
1294
  return acc;
1142
1295
  }, {
1143
1296
  event: null,
1144
- toolCallId: null,
1145
- toolCallMessageId: null,
1146
- prevToolCallMessageId: null,
1147
- messageId: null,
1148
- toolCallName: null,
1149
- prevMessageId: null,
1150
- content: null
1151
- }), (0, import_rxjs.mergeMap)((eventWithState) => {
1152
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1297
+ isMessageStart: false,
1298
+ isMessageEnd: false,
1299
+ isToolCallStart: false,
1300
+ isToolCallEnd: false,
1301
+ isToolCall: false,
1302
+ lastMessageId: null,
1303
+ lastToolCallId: null,
1304
+ lastToolCallName: null,
1305
+ currentContent: null
1306
+ }), (0, import_rxjs.mergeMap)((acc) => {
1153
1307
  const events = [];
1154
1308
  let shouldEmitMessages = true;
1155
- let shouldEmitToolCalls = false;
1156
- if (eventWithState.event.event == LangGraphEventTypes.OnChatModelStream) {
1157
- if ("copilotkit:emit-tool-calls" in (eventWithState.event.metadata || {})) {
1158
- shouldEmitToolCalls = eventWithState.event.metadata["copilotkit:emit-tool-calls"];
1309
+ let shouldEmitToolCalls = true;
1310
+ if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
1311
+ if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
1312
+ shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
1159
1313
  }
1160
- if ("copilotkit:emit-messages" in (eventWithState.event.metadata || {})) {
1161
- shouldEmitMessages = eventWithState.event.metadata["copilotkit:emit-messages"];
1314
+ if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
1315
+ shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
1162
1316
  }
1163
1317
  }
1164
- if (eventWithState.prevToolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId && this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1318
+ const responseMetadata = this.getResponseMetadata(acc.event);
1319
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls" && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1165
1320
  events.push({
1166
- type: RuntimeEventTypes.ActionExecutionEnd
1321
+ type: RuntimeEventTypes.ActionExecutionEnd,
1322
+ actionExecutionId: acc.lastToolCallId
1167
1323
  });
1168
1324
  }
1169
- if (eventWithState.prevMessageId !== null && eventWithState.prevMessageId !== eventWithState.messageId && shouldEmitMessages) {
1325
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
1170
1326
  events.push({
1171
- type: RuntimeEventTypes.TextMessageEnd
1327
+ type: RuntimeEventTypes.TextMessageEnd,
1328
+ messageId: acc.lastMessageId
1172
1329
  });
1173
1330
  }
1174
- switch (eventWithState.event.event) {
1331
+ switch (acc.event.event) {
1175
1332
  case LangGraphEventTypes.OnCustomEvent:
1176
- if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1333
+ if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1177
1334
  events.push({
1178
1335
  type: RuntimeEventTypes.TextMessageStart,
1179
- messageId: eventWithState.event.data.message_id
1336
+ messageId: acc.event.data.message_id
1180
1337
  });
1181
1338
  events.push({
1182
1339
  type: RuntimeEventTypes.TextMessageContent,
1183
- content: eventWithState.event.data.message
1340
+ messageId: acc.event.data.message_id,
1341
+ content: acc.event.data.message
1184
1342
  });
1185
1343
  events.push({
1186
- type: RuntimeEventTypes.TextMessageEnd
1344
+ type: RuntimeEventTypes.TextMessageEnd,
1345
+ messageId: acc.event.data.message_id
1187
1346
  });
1188
- } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1347
+ } else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1189
1348
  events.push({
1190
1349
  type: RuntimeEventTypes.ActionExecutionStart,
1191
- actionExecutionId: eventWithState.event.data.id,
1192
- actionName: eventWithState.event.data.name
1350
+ actionExecutionId: acc.event.data.id,
1351
+ actionName: acc.event.data.name,
1352
+ parentMessageId: acc.event.data.id
1193
1353
  });
1194
1354
  events.push({
1195
1355
  type: RuntimeEventTypes.ActionExecutionArgs,
1196
- args: JSON.stringify(eventWithState.event.data.args)
1356
+ actionExecutionId: acc.event.data.id,
1357
+ args: JSON.stringify(acc.event.data.args)
1197
1358
  });
1198
1359
  events.push({
1199
- type: RuntimeEventTypes.ActionExecutionEnd
1360
+ type: RuntimeEventTypes.ActionExecutionEnd,
1361
+ actionExecutionId: acc.event.data.id
1200
1362
  });
1201
1363
  }
1202
1364
  break;
1203
1365
  case LangGraphEventTypes.OnCopilotKitStateSync:
1204
1366
  events.push({
1205
1367
  type: RuntimeEventTypes.AgentStateMessage,
1206
- threadId: eventWithState.event.thread_id,
1207
- role: eventWithState.event.role,
1208
- agentName: eventWithState.event.agent_name,
1209
- nodeName: eventWithState.event.node_name,
1210
- runId: eventWithState.event.run_id,
1211
- active: eventWithState.event.active,
1212
- state: JSON.stringify(eventWithState.event.state),
1213
- running: eventWithState.event.running
1368
+ threadId: acc.event.thread_id,
1369
+ role: acc.event.role,
1370
+ agentName: acc.event.agent_name,
1371
+ nodeName: acc.event.node_name,
1372
+ runId: acc.event.run_id,
1373
+ active: acc.event.active,
1374
+ state: JSON.stringify(acc.event.state),
1375
+ running: acc.event.running
1214
1376
  });
1215
1377
  break;
1216
- case LangGraphEventTypes.OnToolEnd:
1217
- break;
1218
1378
  case LangGraphEventTypes.OnChatModelStream:
1219
- if (eventWithState.toolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId) {
1220
- if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1221
- events.push({
1222
- type: RuntimeEventTypes.ActionExecutionStart,
1223
- actionExecutionId: eventWithState.toolCallMessageId,
1224
- actionName: eventWithState.toolCallName,
1225
- scope: "client"
1226
- });
1227
- }
1228
- } else if (eventWithState.messageId !== null && eventWithState.prevMessageId !== eventWithState.messageId) {
1229
- if (shouldEmitMessages) {
1230
- events.push({
1231
- type: RuntimeEventTypes.TextMessageStart,
1232
- messageId: eventWithState.messageId
1233
- });
1234
- }
1379
+ if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1380
+ events.push({
1381
+ type: RuntimeEventTypes.ActionExecutionStart,
1382
+ actionExecutionId: acc.lastToolCallId,
1383
+ actionName: acc.lastToolCallName,
1384
+ parentMessageId: acc.lastMessageId
1385
+ });
1386
+ } else if (acc.isMessageStart && shouldEmitMessages) {
1387
+ events.push({
1388
+ type: RuntimeEventTypes.TextMessageStart,
1389
+ messageId: acc.lastMessageId
1390
+ });
1235
1391
  }
1236
- const args = ((_e = (_d = (_c = (_b = (_a = eventWithState.event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) == null ? void 0 : _d[0]) == null ? void 0 : _e.args) ?? // @ts-expect-error -- sdf
1237
- ((_i = (_h = (_g = (_f = eventWithState.event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.tool_call_chunks) == null ? void 0 : _h[0]) == null ? void 0 : _i.args);
1238
- const content = eventWithState.content;
1239
- if (args) {
1240
- if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1241
- events.push({
1242
- type: RuntimeEventTypes.ActionExecutionArgs,
1243
- args
1244
- });
1245
- }
1246
- } else if (eventWithState.messageId !== null && content) {
1247
- if (shouldEmitMessages) {
1248
- events.push({
1249
- type: RuntimeEventTypes.TextMessageContent,
1250
- content
1251
- });
1252
- }
1392
+ if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1393
+ events.push({
1394
+ type: RuntimeEventTypes.ActionExecutionArgs,
1395
+ actionExecutionId: acc.lastToolCallId,
1396
+ args: acc.currentContent
1397
+ });
1398
+ } else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
1399
+ events.push({
1400
+ type: RuntimeEventTypes.TextMessageContent,
1401
+ messageId: acc.lastMessageId,
1402
+ content: acc.currentContent
1403
+ });
1253
1404
  }
1254
1405
  break;
1255
1406
  }
@@ -1257,14 +1408,16 @@ var RemoteLangGraphEventSource = class {
1257
1408
  }), (0, import_rxjs.catchError)((error) => {
1258
1409
  console.error(error);
1259
1410
  const events = [];
1260
- if (lastEventWithState == null ? void 0 : lastEventWithState.messageId) {
1411
+ if ((lastEventWithState == null ? void 0 : lastEventWithState.lastMessageId) && !lastEventWithState.isToolCall) {
1261
1412
  events.push({
1262
- type: RuntimeEventTypes.TextMessageEnd
1413
+ type: RuntimeEventTypes.TextMessageEnd,
1414
+ messageId: lastEventWithState.lastMessageId
1263
1415
  });
1264
1416
  }
1265
- if (lastEventWithState == null ? void 0 : lastEventWithState.toolCallMessageId) {
1417
+ if (lastEventWithState == null ? void 0 : lastEventWithState.lastToolCallId) {
1266
1418
  events.push({
1267
- type: RuntimeEventTypes.ActionExecutionEnd
1419
+ type: RuntimeEventTypes.ActionExecutionEnd,
1420
+ actionExecutionId: lastEventWithState.lastToolCallId
1268
1421
  });
1269
1422
  }
1270
1423
  const messageId = (0, import_shared6.randomId)();
@@ -1274,10 +1427,12 @@ var RemoteLangGraphEventSource = class {
1274
1427
  });
1275
1428
  events.push({
1276
1429
  type: RuntimeEventTypes.TextMessageContent,
1430
+ messageId,
1277
1431
  content: "\u274C An error occurred. Please try again."
1278
1432
  });
1279
1433
  events.push({
1280
- type: RuntimeEventTypes.TextMessageEnd
1434
+ type: RuntimeEventTypes.TextMessageEnd,
1435
+ messageId
1281
1436
  });
1282
1437
  return events;
1283
1438
  }));
@@ -1299,12 +1454,6 @@ var MessageRole;
1299
1454
  MessageRole2["system"] = "system";
1300
1455
  MessageRole2["tool"] = "tool";
1301
1456
  })(MessageRole || (MessageRole = {}));
1302
- var ActionExecutionScope;
1303
- (function(ActionExecutionScope2) {
1304
- ActionExecutionScope2["server"] = "server";
1305
- ActionExecutionScope2["client"] = "client";
1306
- ActionExecutionScope2["passThrough"] = "passThrough";
1307
- })(ActionExecutionScope || (ActionExecutionScope = {}));
1308
1457
  var CopilotRequestType;
1309
1458
  (function(CopilotRequestType2) {
1310
1459
  CopilotRequestType2["Chat"] = "Chat";
@@ -1323,10 +1472,6 @@ var ActionInputAvailability;
1323
1472
  name: "MessageRole",
1324
1473
  description: "The role of the message"
1325
1474
  });
1326
- (0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
1327
- name: "ActionExecutionScope",
1328
- description: "The scope of the action"
1329
- });
1330
1475
  (0, import_type_graphql.registerEnumType)(CopilotRequestType, {
1331
1476
  name: "CopilotRequestType",
1332
1477
  description: "The type of Copilot request"
@@ -1383,7 +1528,7 @@ async function streamEvents(controller, args) {
1383
1528
  const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1384
1529
  let formattedMessages = [];
1385
1530
  try {
1386
- formattedMessages = formatMessages(messages);
1531
+ formattedMessages = copilotkitMessagesToLangChain(messages);
1387
1532
  } catch (e) {
1388
1533
  logger2.error(e, `Error event thrown: ${e.message}`);
1389
1534
  }
@@ -1536,7 +1681,8 @@ async function streamEvents(controller, args) {
1536
1681
  nodeName: isEndNode ? "__end__" : nodeName,
1537
1682
  state: state.values,
1538
1683
  running: !shouldExit,
1539
- active: false
1684
+ active: false,
1685
+ includeMessages: true
1540
1686
  }));
1541
1687
  return Promise.resolve();
1542
1688
  } catch (e) {
@@ -1549,13 +1695,20 @@ async function streamEvents(controller, args) {
1549
1695
  }
1550
1696
  }
1551
1697
  __name(streamEvents, "streamEvents");
1552
- function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
1553
- const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
1554
- if (key !== "messages") {
1555
- acc[key] = state[key];
1556
- }
1557
- return acc;
1558
- }, {});
1698
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active, includeMessages = false }) {
1699
+ if (!includeMessages) {
1700
+ state = Object.keys(state).reduce((acc, key) => {
1701
+ if (key !== "messages") {
1702
+ acc[key] = state[key];
1703
+ }
1704
+ return acc;
1705
+ }, {});
1706
+ } else {
1707
+ state = {
1708
+ ...state,
1709
+ messages: langchainMessagesToCopilotKit(state.messages || [])
1710
+ };
1711
+ }
1559
1712
  return JSON.stringify({
1560
1713
  event: LangGraphEventTypes.OnCopilotKitStateSync,
1561
1714
  thread_id: threadId,
@@ -1563,7 +1716,7 @@ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, runnin
1563
1716
  agent_name: agentName,
1564
1717
  node_name: nodeName,
1565
1718
  active,
1566
- state: stateWithoutMessages,
1719
+ state,
1567
1720
  running,
1568
1721
  role: "assistant"
1569
1722
  }) + "\n";
@@ -1639,134 +1792,159 @@ function langGraphDefaultMergeState(state, messages, actions, agentName) {
1639
1792
  if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1640
1793
  messages = messages.slice(1);
1641
1794
  }
1642
- const mergedMessages = state.messages || [];
1643
- const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
1644
- const existingToolCallResults = /* @__PURE__ */ new Set();
1645
- for (const message of mergedMessages) {
1646
- if ("tool_call_id" in message) {
1647
- existingToolCallResults.add(message.tool_call_id);
1795
+ const existingMessages = state.messages || [];
1796
+ const existingMessageIds = new Set(existingMessages.map((message) => message.id));
1797
+ const newMessages = messages.filter((message) => !existingMessageIds.has(message.id));
1798
+ return {
1799
+ ...state,
1800
+ messages: newMessages,
1801
+ copilotkit: {
1802
+ actions
1803
+ }
1804
+ };
1805
+ }
1806
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1807
+ function langchainMessagesToCopilotKit(messages) {
1808
+ const result = [];
1809
+ const tool_call_names = {};
1810
+ for (const message of messages) {
1811
+ if (message.type === "ai") {
1812
+ for (const tool_call of message.tool_calls) {
1813
+ tool_call_names[tool_call.id] = tool_call.name;
1814
+ }
1648
1815
  }
1649
1816
  }
1650
1817
  for (const message of messages) {
1651
- if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
1652
- continue;
1818
+ let content = message.content;
1819
+ if (content instanceof Array) {
1820
+ content = content[0];
1653
1821
  }
1654
- if ("name" in message && message.name === agentName) {
1655
- continue;
1822
+ if (content instanceof Object) {
1823
+ content = content.text;
1656
1824
  }
1657
- if (!existingMessageIds.has(message.id)) {
1658
- if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
1659
- console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
1660
- continue;
1661
- }
1662
- mergedMessages.push(message);
1663
- } else {
1664
- for (let i = 0; i < mergedMessages.length; i++) {
1665
- if (mergedMessages[i].id === message.id && message.role === "assistant") {
1666
- if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
1667
- message.tool_calls = mergedMessages[i]["tool_calls"];
1668
- message.additional_kwargs = mergedMessages[i].additional_kwargs;
1669
- }
1670
- mergedMessages[i] = message;
1825
+ if (message.type === "human") {
1826
+ result.push({
1827
+ role: "user",
1828
+ content,
1829
+ id: message.id
1830
+ });
1831
+ } else if (message.type === "system") {
1832
+ result.push({
1833
+ role: "system",
1834
+ content,
1835
+ id: message.id
1836
+ });
1837
+ } else if (message.type === "ai") {
1838
+ if (message.tool_calls && message.tool_calls.length > 0) {
1839
+ for (const tool_call of message.tool_calls) {
1840
+ result.push({
1841
+ id: tool_call.id,
1842
+ name: tool_call.name,
1843
+ arguments: tool_call.args,
1844
+ parentMessageId: message.id
1845
+ });
1671
1846
  }
1847
+ } else {
1848
+ result.push({
1849
+ role: "assistant",
1850
+ content,
1851
+ id: message.id,
1852
+ parentMessageId: message.id
1853
+ });
1672
1854
  }
1855
+ } else if (message.type === "tool") {
1856
+ const actionName = tool_call_names[message.tool_call_id] || message.name || "";
1857
+ result.push({
1858
+ actionExecutionId: message.tool_call_id,
1859
+ actionName,
1860
+ result: content,
1861
+ id: message.id
1862
+ });
1673
1863
  }
1674
1864
  }
1675
- for (let i = 0; i < mergedMessages.length - 1; i++) {
1676
- const currentMessage = mergedMessages[i];
1677
- const nextMessage = mergedMessages[i + 1];
1678
- if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
1679
- nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
1865
+ const resultsDict = {};
1866
+ for (const msg of result) {
1867
+ if (msg.actionExecutionId) {
1868
+ resultsDict[msg.actionExecutionId] = msg;
1680
1869
  }
1681
1870
  }
1682
- const correctedMessages = [];
1683
- for (let i = 0; i < mergedMessages.length; i++) {
1684
- const currentMessage = mergedMessages[i];
1685
- const nextMessage = mergedMessages[i + 1] || null;
1686
- const prevMessage = mergedMessages[i - 1] || null;
1687
- if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
1688
- if (!nextMessage) {
1689
- console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
1690
- continue;
1691
- }
1692
- if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
1693
- const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
1694
- if (toolMessage) {
1695
- console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
1696
- correctedMessages.push(currentMessage, toolMessage);
1697
- continue;
1698
- } else {
1699
- console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
1700
- continue;
1701
- }
1702
- }
1703
- correctedMessages.push(currentMessage);
1704
- continue;
1871
+ const reorderedResult = [];
1872
+ for (const msg of result) {
1873
+ if (!("actionExecutionId" in msg)) {
1874
+ reorderedResult.push(msg);
1705
1875
  }
1706
- if ("tool_call_id" in currentMessage) {
1707
- if (!prevMessage || !("tool_calls" in prevMessage)) {
1708
- console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
1709
- continue;
1710
- }
1711
- if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
1712
- console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
1713
- continue;
1876
+ if ("arguments" in msg) {
1877
+ const msgId = msg.id;
1878
+ if (msgId in resultsDict) {
1879
+ reorderedResult.push(resultsDict[msgId]);
1880
+ } else {
1881
+ console.warn("Tool call result message not found for id:", msgId);
1714
1882
  }
1715
- correctedMessages.push(currentMessage);
1716
- continue;
1717
1883
  }
1718
- correctedMessages.push(currentMessage);
1719
1884
  }
1720
- return {
1721
- ...state,
1722
- messages: correctedMessages,
1723
- copilotkit: {
1724
- actions
1725
- }
1726
- };
1885
+ return reorderedResult;
1727
1886
  }
1728
- __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1729
- function formatMessages(messages) {
1730
- return messages.map((message) => {
1731
- if (message.isTextMessage() && message.role === "assistant") {
1732
- return message;
1733
- }
1734
- if (message.isTextMessage() && message.role === "system") {
1735
- return message;
1736
- }
1737
- if (message.isTextMessage() && message.role === "user") {
1738
- return message;
1887
+ __name(langchainMessagesToCopilotKit, "langchainMessagesToCopilotKit");
1888
+ function copilotkitMessagesToLangChain(messages) {
1889
+ const result = [];
1890
+ const processedActionExecutions = /* @__PURE__ */ new Set();
1891
+ for (const message of messages) {
1892
+ if (message.isTextMessage()) {
1893
+ if (message.role === "user") {
1894
+ result.push({
1895
+ ...message,
1896
+ role: MessageRole.user
1897
+ });
1898
+ } else if (message.role === "system") {
1899
+ result.push({
1900
+ ...message,
1901
+ role: MessageRole.system
1902
+ });
1903
+ } else if (message.role === "assistant") {
1904
+ result.push({
1905
+ ...message,
1906
+ role: MessageRole.assistant
1907
+ });
1908
+ }
1909
+ continue;
1739
1910
  }
1740
1911
  if (message.isActionExecutionMessage()) {
1741
- const toolCall = {
1742
- name: message.name,
1743
- args: message.arguments,
1744
- id: message.id
1745
- };
1746
- return {
1747
- type: message.type,
1912
+ const messageId = message.parentMessageId ?? message.id;
1913
+ if (processedActionExecutions.has(messageId)) {
1914
+ continue;
1915
+ }
1916
+ processedActionExecutions.add(messageId);
1917
+ const relatedActionExecutions = messages.filter((m) => m.isActionExecutionMessage() && (m.parentMessageId && m.parentMessageId === messageId || m.id === messageId));
1918
+ const tool_calls = relatedActionExecutions.map((m) => ({
1919
+ name: m.name,
1920
+ args: m.arguments,
1921
+ id: m.id
1922
+ }));
1923
+ result.push({
1924
+ id: messageId,
1925
+ type: "ActionExecutionMessage",
1748
1926
  content: "",
1749
- tool_calls: [
1750
- toolCall
1751
- ],
1752
- role: MessageRole.assistant,
1753
- id: message.id
1754
- };
1927
+ tool_calls,
1928
+ role: MessageRole.assistant
1929
+ });
1930
+ continue;
1755
1931
  }
1756
1932
  if (message.isResultMessage()) {
1757
- return {
1933
+ result.push({
1758
1934
  type: message.type,
1759
1935
  content: message.result,
1760
1936
  id: message.id,
1761
1937
  tool_call_id: message.actionExecutionId,
1762
1938
  name: message.actionName,
1763
1939
  role: MessageRole.tool
1764
- };
1940
+ });
1941
+ continue;
1765
1942
  }
1766
1943
  throw new Error(`Unknown message type ${message.type}`);
1767
- });
1944
+ }
1945
+ return result;
1768
1946
  }
1769
- __name(formatMessages, "formatMessages");
1947
+ __name(copilotkitMessagesToLangChain, "copilotkitMessagesToLangChain");
1770
1948
 
1771
1949
  // src/lib/runtime/remote-action-constructors.ts
1772
1950
  function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
@@ -1776,7 +1954,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
1776
1954
  parameters: [],
1777
1955
  handler: async (_args) => {
1778
1956
  },
1779
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
1957
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
1780
1958
  var _a;
1781
1959
  logger2.debug({
1782
1960
  actionName: agent.name
@@ -1802,7 +1980,10 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
1802
1980
  agent,
1803
1981
  threadId,
1804
1982
  nodeName,
1805
- messages,
1983
+ messages: [
1984
+ ...messages,
1985
+ ...additionalMessages
1986
+ ],
1806
1987
  state,
1807
1988
  properties: graphqlContext.properties,
1808
1989
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -1884,7 +2065,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1884
2065
  parameters: [],
1885
2066
  handler: async (_args) => {
1886
2067
  },
1887
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2068
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
1888
2069
  var _a;
1889
2070
  logger2.debug({
1890
2071
  actionName: agent.name
@@ -1909,7 +2090,10 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1909
2090
  name,
1910
2091
  threadId,
1911
2092
  nodeName,
1912
- messages,
2093
+ messages: [
2094
+ ...messages,
2095
+ ...additionalMessages
2096
+ ],
1913
2097
  state,
1914
2098
  properties: graphqlContext.properties,
1915
2099
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -2152,7 +2336,94 @@ function getRuntimeInstanceTelemetryInfo(runtime) {
2152
2336
  __name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
2153
2337
  var telemetry_client_default = telemetryClient;
2154
2338
 
2339
+ // src/graphql/types/base/index.ts
2340
+ var import_type_graphql2 = require("type-graphql");
2341
+ function _ts_decorate(decorators, target, key, desc) {
2342
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2343
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2344
+ r = Reflect.decorate(decorators, target, key, desc);
2345
+ else
2346
+ for (var i = decorators.length - 1; i >= 0; i--)
2347
+ if (d = decorators[i])
2348
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2349
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2350
+ }
2351
+ __name(_ts_decorate, "_ts_decorate");
2352
+ function _ts_metadata(k, v) {
2353
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2354
+ return Reflect.metadata(k, v);
2355
+ }
2356
+ __name(_ts_metadata, "_ts_metadata");
2357
+ var BaseMessageInput = class {
2358
+ id;
2359
+ createdAt;
2360
+ };
2361
+ __name(BaseMessageInput, "BaseMessageInput");
2362
+ _ts_decorate([
2363
+ (0, import_type_graphql2.Field)(() => String),
2364
+ _ts_metadata("design:type", String)
2365
+ ], BaseMessageInput.prototype, "id", void 0);
2366
+ _ts_decorate([
2367
+ (0, import_type_graphql2.Field)(() => Date),
2368
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2369
+ ], BaseMessageInput.prototype, "createdAt", void 0);
2370
+ BaseMessageInput = _ts_decorate([
2371
+ (0, import_type_graphql2.InputType)()
2372
+ ], BaseMessageInput);
2373
+
2374
+ // src/graphql/types/converted/index.ts
2375
+ var Message = class extends BaseMessageInput {
2376
+ type;
2377
+ isTextMessage() {
2378
+ return this.type === "TextMessage";
2379
+ }
2380
+ isActionExecutionMessage() {
2381
+ return this.type === "ActionExecutionMessage";
2382
+ }
2383
+ isResultMessage() {
2384
+ return this.type === "ResultMessage";
2385
+ }
2386
+ isAgentStateMessage() {
2387
+ return this.type === "AgentStateMessage";
2388
+ }
2389
+ };
2390
+ __name(Message, "Message");
2391
+ var TextMessage = class extends Message {
2392
+ type = "TextMessage";
2393
+ content;
2394
+ role;
2395
+ parentMessageId;
2396
+ };
2397
+ __name(TextMessage, "TextMessage");
2398
+ var ActionExecutionMessage = class extends Message {
2399
+ type = "ActionExecutionMessage";
2400
+ name;
2401
+ arguments;
2402
+ parentMessageId;
2403
+ };
2404
+ __name(ActionExecutionMessage, "ActionExecutionMessage");
2405
+ var ResultMessage = class extends Message {
2406
+ type = "ResultMessage";
2407
+ actionExecutionId;
2408
+ actionName;
2409
+ result;
2410
+ };
2411
+ __name(ResultMessage, "ResultMessage");
2412
+ var AgentStateMessage = class extends Message {
2413
+ type = "AgentStateMessage";
2414
+ threadId;
2415
+ agentName;
2416
+ nodeName;
2417
+ runId;
2418
+ active;
2419
+ role;
2420
+ state;
2421
+ running;
2422
+ };
2423
+ __name(AgentStateMessage, "AgentStateMessage");
2424
+
2155
2425
  // src/service-adapters/events.ts
2426
+ var import_class_transformer = require("class-transformer");
2156
2427
  var RuntimeEventTypes;
2157
2428
  (function(RuntimeEventTypes2) {
2158
2429
  RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
@@ -2168,52 +2439,74 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2168
2439
  constructor() {
2169
2440
  super();
2170
2441
  }
2171
- sendTextMessageStart(messageId) {
2442
+ sendTextMessageStart({ messageId, parentMessageId }) {
2172
2443
  this.next({
2173
2444
  type: "TextMessageStart",
2174
- messageId
2445
+ messageId,
2446
+ parentMessageId
2175
2447
  });
2176
2448
  }
2177
- sendTextMessageContent(content) {
2449
+ sendTextMessageContent({ messageId, content }) {
2178
2450
  this.next({
2179
2451
  type: "TextMessageContent",
2180
- content
2452
+ content,
2453
+ messageId
2181
2454
  });
2182
2455
  }
2183
- sendTextMessageEnd() {
2456
+ sendTextMessageEnd({ messageId }) {
2184
2457
  this.next({
2185
- type: "TextMessageEnd"
2458
+ type: "TextMessageEnd",
2459
+ messageId
2186
2460
  });
2187
2461
  }
2188
2462
  sendTextMessage(messageId, content) {
2189
- this.sendTextMessageStart(messageId);
2190
- this.sendTextMessageContent(content);
2191
- this.sendTextMessageEnd();
2463
+ this.sendTextMessageStart({
2464
+ messageId
2465
+ });
2466
+ this.sendTextMessageContent({
2467
+ messageId,
2468
+ content
2469
+ });
2470
+ this.sendTextMessageEnd({
2471
+ messageId
2472
+ });
2192
2473
  }
2193
- sendActionExecutionStart(actionExecutionId, actionName) {
2474
+ sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId }) {
2194
2475
  this.next({
2195
2476
  type: "ActionExecutionStart",
2196
2477
  actionExecutionId,
2197
- actionName
2478
+ actionName,
2479
+ parentMessageId
2198
2480
  });
2199
2481
  }
2200
- sendActionExecutionArgs(args) {
2482
+ sendActionExecutionArgs({ actionExecutionId, args }) {
2201
2483
  this.next({
2202
2484
  type: "ActionExecutionArgs",
2203
- args
2485
+ args,
2486
+ actionExecutionId
2204
2487
  });
2205
2488
  }
2206
- sendActionExecutionEnd() {
2489
+ sendActionExecutionEnd({ actionExecutionId }) {
2207
2490
  this.next({
2208
- type: "ActionExecutionEnd"
2491
+ type: "ActionExecutionEnd",
2492
+ actionExecutionId
2209
2493
  });
2210
2494
  }
2211
- sendActionExecution(actionExecutionId, toolName, args) {
2212
- this.sendActionExecutionStart(actionExecutionId, toolName);
2213
- this.sendActionExecutionArgs(args);
2214
- this.sendActionExecutionEnd();
2495
+ sendActionExecution({ actionExecutionId, actionName, args, parentMessageId }) {
2496
+ this.sendActionExecutionStart({
2497
+ actionExecutionId,
2498
+ actionName,
2499
+ parentMessageId
2500
+ });
2501
+ this.sendActionExecutionArgs({
2502
+ actionExecutionId,
2503
+ args
2504
+ });
2505
+ this.sendActionExecutionEnd({
2506
+ actionExecutionId
2507
+ });
2215
2508
  }
2216
- sendActionExecutionResult(actionExecutionId, actionName, result) {
2509
+ sendActionExecutionResult({ actionExecutionId, actionName, result }) {
2217
2510
  this.next({
2218
2511
  type: "ActionExecutionResult",
2219
2512
  actionName,
@@ -2221,7 +2514,7 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2221
2514
  result
2222
2515
  });
2223
2516
  }
2224
- sendAgentStateMessage(threadId, agentName, nodeName, runId, active, role, state, running) {
2517
+ sendAgentStateMessage({ threadId, agentName, nodeName, runId, active, role, state, running }) {
2225
2518
  this.next({
2226
2519
  type: "AgentStateMessage",
2227
2520
  threadId,
@@ -2258,22 +2551,13 @@ var RuntimeEventSource = class {
2258
2551
  this.sendErrorMessageToChat();
2259
2552
  });
2260
2553
  return this.eventStream$.pipe(
2261
- // mark tools for server side execution
2262
- (0, import_rxjs2.map)((event) => {
2263
- if (event.type === "ActionExecutionStart") {
2264
- if (event.scope !== "passThrough") {
2265
- event.scope = serverSideActions.find((action) => action.name === event.actionName) ? "server" : "client";
2266
- }
2267
- }
2268
- return event;
2269
- }),
2270
2554
  // track state
2271
2555
  (0, import_rxjs2.scan)((acc, event) => {
2272
2556
  acc = {
2273
2557
  ...acc
2274
2558
  };
2275
2559
  if (event.type === "ActionExecutionStart") {
2276
- acc.callActionServerSide = event.scope === "server";
2560
+ acc.callActionServerSide = serverSideActions.find((action) => action.name === event.actionName) !== void 0;
2277
2561
  acc.args = "";
2278
2562
  acc.actionExecutionId = event.actionExecutionId;
2279
2563
  if (acc.callActionServerSide) {
@@ -2327,10 +2611,33 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2327
2611
  }
2328
2612
  }
2329
2613
  if (isLangGraphAgentAction(action)) {
2330
- eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
2614
+ const result = `${action.name} agent started`;
2615
+ const agentExecution = (0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2616
+ id: actionExecutionId,
2617
+ createdAt: /* @__PURE__ */ new Date(),
2618
+ name: action.name,
2619
+ arguments: JSON.parse(actionArguments),
2620
+ parentMessageId: actionExecutionId
2621
+ });
2622
+ const agentExecutionResult = (0, import_class_transformer.plainToInstance)(ResultMessage, {
2623
+ id: "result-" + actionExecutionId,
2624
+ createdAt: /* @__PURE__ */ new Date(),
2625
+ actionExecutionId,
2626
+ actionName: action.name,
2627
+ result
2628
+ });
2629
+ eventStream$.sendActionExecutionResult({
2630
+ actionExecutionId,
2631
+ actionName: action.name,
2632
+ result
2633
+ });
2331
2634
  const stream = await action.langGraphAgentHandler({
2332
2635
  name: action.name,
2333
- actionInputsWithoutAgents
2636
+ actionInputsWithoutAgents,
2637
+ additionalMessages: [
2638
+ agentExecution,
2639
+ agentExecutionResult
2640
+ ]
2334
2641
  });
2335
2642
  (0, import_rxjs2.from)(stream).subscribe({
2336
2643
  next: (event) => eventStream$.next(event),
@@ -2351,113 +2658,29 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2351
2658
  }
2352
2659
  __name(executeAction, "executeAction");
2353
2660
 
2354
- // src/graphql/types/base/index.ts
2355
- var import_type_graphql2 = require("type-graphql");
2356
- function _ts_decorate(decorators, target, key, desc) {
2357
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2358
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2359
- r = Reflect.decorate(decorators, target, key, desc);
2360
- else
2361
- for (var i = decorators.length - 1; i >= 0; i--)
2362
- if (d = decorators[i])
2363
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2364
- return c > 3 && r && Object.defineProperty(target, key, r), r;
2365
- }
2366
- __name(_ts_decorate, "_ts_decorate");
2367
- function _ts_metadata(k, v) {
2368
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2369
- return Reflect.metadata(k, v);
2370
- }
2371
- __name(_ts_metadata, "_ts_metadata");
2372
- var BaseMessageInput = class {
2373
- id;
2374
- createdAt;
2375
- };
2376
- __name(BaseMessageInput, "BaseMessageInput");
2377
- _ts_decorate([
2378
- (0, import_type_graphql2.Field)(() => String),
2379
- _ts_metadata("design:type", String)
2380
- ], BaseMessageInput.prototype, "id", void 0);
2381
- _ts_decorate([
2382
- (0, import_type_graphql2.Field)(() => Date),
2383
- _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2384
- ], BaseMessageInput.prototype, "createdAt", void 0);
2385
- BaseMessageInput = _ts_decorate([
2386
- (0, import_type_graphql2.InputType)()
2387
- ], BaseMessageInput);
2388
-
2389
- // src/graphql/types/converted/index.ts
2390
- var Message = class extends BaseMessageInput {
2391
- type;
2392
- isTextMessage() {
2393
- return this.type === "TextMessage";
2394
- }
2395
- isActionExecutionMessage() {
2396
- return this.type === "ActionExecutionMessage";
2397
- }
2398
- isResultMessage() {
2399
- return this.type === "ResultMessage";
2400
- }
2401
- isAgentStateMessage() {
2402
- return this.type === "AgentStateMessage";
2403
- }
2404
- };
2405
- __name(Message, "Message");
2406
- var TextMessage = class extends Message {
2407
- type = "TextMessage";
2408
- content;
2409
- role;
2410
- };
2411
- __name(TextMessage, "TextMessage");
2412
- var ActionExecutionMessage = class extends Message {
2413
- type = "ActionExecutionMessage";
2414
- name;
2415
- arguments;
2416
- scope;
2417
- };
2418
- __name(ActionExecutionMessage, "ActionExecutionMessage");
2419
- var ResultMessage = class extends Message {
2420
- type = "ResultMessage";
2421
- actionExecutionId;
2422
- actionName;
2423
- result;
2424
- };
2425
- __name(ResultMessage, "ResultMessage");
2426
- var AgentStateMessage = class extends Message {
2427
- type = "AgentStateMessage";
2428
- threadId;
2429
- agentName;
2430
- nodeName;
2431
- runId;
2432
- active;
2433
- role;
2434
- state;
2435
- running;
2436
- };
2437
- __name(AgentStateMessage, "AgentStateMessage");
2438
-
2439
2661
  // src/service-adapters/conversion.ts
2440
- var import_class_transformer = require("class-transformer");
2662
+ var import_class_transformer2 = require("class-transformer");
2441
2663
  function convertGqlInputToMessages(inputMessages) {
2442
2664
  const messages = [];
2443
2665
  for (const message of inputMessages) {
2444
2666
  if (message.textMessage) {
2445
- messages.push((0, import_class_transformer.plainToInstance)(TextMessage, {
2667
+ messages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
2446
2668
  id: message.id,
2447
2669
  createdAt: message.createdAt,
2448
2670
  role: message.textMessage.role,
2449
- content: message.textMessage.content
2671
+ content: message.textMessage.content,
2672
+ parentMessageId: message.textMessage.parentMessageId
2450
2673
  }));
2451
2674
  } else if (message.actionExecutionMessage) {
2452
- messages.push((0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2675
+ messages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
2453
2676
  id: message.id,
2454
2677
  createdAt: message.createdAt,
2455
2678
  name: message.actionExecutionMessage.name,
2456
2679
  arguments: JSON.parse(message.actionExecutionMessage.arguments),
2457
- scope: message.actionExecutionMessage.scope
2680
+ parentMessageId: message.actionExecutionMessage.parentMessageId
2458
2681
  }));
2459
2682
  } else if (message.resultMessage) {
2460
- messages.push((0, import_class_transformer.plainToInstance)(ResultMessage, {
2683
+ messages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
2461
2684
  id: message.id,
2462
2685
  createdAt: message.createdAt,
2463
2686
  actionExecutionId: message.resultMessage.actionExecutionId,
@@ -2465,7 +2688,7 @@ function convertGqlInputToMessages(inputMessages) {
2465
2688
  result: message.resultMessage.result
2466
2689
  }));
2467
2690
  } else if (message.agentStateMessage) {
2468
- messages.push((0, import_class_transformer.plainToInstance)(AgentStateMessage, {
2691
+ messages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
2469
2692
  id: message.id,
2470
2693
  threadId: message.agentStateMessage.threadId,
2471
2694
  createdAt: message.createdAt,
@@ -2774,6 +2997,7 @@ MessageInput = _ts_decorate2([
2774
2997
  ], MessageInput);
2775
2998
  var TextMessageInput = class {
2776
2999
  content;
3000
+ parentMessageId;
2777
3001
  role;
2778
3002
  };
2779
3003
  __name(TextMessageInput, "TextMessageInput");
@@ -2781,6 +3005,12 @@ _ts_decorate2([
2781
3005
  (0, import_type_graphql3.Field)(() => String),
2782
3006
  _ts_metadata2("design:type", String)
2783
3007
  ], TextMessageInput.prototype, "content", void 0);
3008
+ _ts_decorate2([
3009
+ (0, import_type_graphql3.Field)(() => String, {
3010
+ nullable: true
3011
+ }),
3012
+ _ts_metadata2("design:type", String)
3013
+ ], TextMessageInput.prototype, "parentMessageId", void 0);
2784
3014
  _ts_decorate2([
2785
3015
  (0, import_type_graphql3.Field)(() => MessageRole),
2786
3016
  _ts_metadata2("design:type", typeof MessageRole === "undefined" ? Object : MessageRole)
@@ -2791,6 +3021,7 @@ TextMessageInput = _ts_decorate2([
2791
3021
  var ActionExecutionMessageInput = class {
2792
3022
  name;
2793
3023
  arguments;
3024
+ parentMessageId;
2794
3025
  scope;
2795
3026
  };
2796
3027
  __name(ActionExecutionMessageInput, "ActionExecutionMessageInput");
@@ -2803,8 +3034,17 @@ _ts_decorate2([
2803
3034
  _ts_metadata2("design:type", String)
2804
3035
  ], ActionExecutionMessageInput.prototype, "arguments", void 0);
2805
3036
  _ts_decorate2([
2806
- (0, import_type_graphql3.Field)(() => ActionExecutionScope),
2807
- _ts_metadata2("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
3037
+ (0, import_type_graphql3.Field)(() => String, {
3038
+ nullable: true
3039
+ }),
3040
+ _ts_metadata2("design:type", String)
3041
+ ], ActionExecutionMessageInput.prototype, "parentMessageId", void 0);
3042
+ _ts_decorate2([
3043
+ (0, import_type_graphql3.Field)(() => String, {
3044
+ nullable: true,
3045
+ deprecationReason: "This field will be removed in a future version"
3046
+ }),
3047
+ _ts_metadata2("design:type", typeof String === "undefined" ? Object : String)
2808
3048
  ], ActionExecutionMessageInput.prototype, "scope", void 0);
2809
3049
  ActionExecutionMessageInput = _ts_decorate2([
2810
3050
  (0, import_type_graphql3.InputType)()
@@ -2812,6 +3052,7 @@ ActionExecutionMessageInput = _ts_decorate2([
2812
3052
  var ResultMessageInput = class {
2813
3053
  actionExecutionId;
2814
3054
  actionName;
3055
+ parentMessageId;
2815
3056
  result;
2816
3057
  };
2817
3058
  __name(ResultMessageInput, "ResultMessageInput");
@@ -2823,6 +3064,12 @@ _ts_decorate2([
2823
3064
  (0, import_type_graphql3.Field)(() => String),
2824
3065
  _ts_metadata2("design:type", String)
2825
3066
  ], ResultMessageInput.prototype, "actionName", void 0);
3067
+ _ts_decorate2([
3068
+ (0, import_type_graphql3.Field)(() => String, {
3069
+ nullable: true
3070
+ }),
3071
+ _ts_metadata2("design:type", String)
3072
+ ], ResultMessageInput.prototype, "parentMessageId", void 0);
2826
3073
  _ts_decorate2([
2827
3074
  (0, import_type_graphql3.Field)(() => String),
2828
3075
  _ts_metadata2("design:type", String)
@@ -3545,6 +3792,7 @@ BaseMessageOutput = _ts_decorate13([
3545
3792
  var TextMessageOutput = class {
3546
3793
  role;
3547
3794
  content;
3795
+ parentMessageId;
3548
3796
  };
3549
3797
  __name(TextMessageOutput, "TextMessageOutput");
3550
3798
  _ts_decorate13([
@@ -3557,6 +3805,12 @@ _ts_decorate13([
3557
3805
  ]),
3558
3806
  _ts_metadata13("design:type", Array)
3559
3807
  ], TextMessageOutput.prototype, "content", void 0);
3808
+ _ts_decorate13([
3809
+ (0, import_type_graphql14.Field)(() => String, {
3810
+ nullable: true
3811
+ }),
3812
+ _ts_metadata13("design:type", String)
3813
+ ], TextMessageOutput.prototype, "parentMessageId", void 0);
3560
3814
  TextMessageOutput = _ts_decorate13([
3561
3815
  (0, import_type_graphql14.ObjectType)({
3562
3816
  implements: BaseMessageOutput
@@ -3566,6 +3820,7 @@ var ActionExecutionMessageOutput = class {
3566
3820
  name;
3567
3821
  scope;
3568
3822
  arguments;
3823
+ parentMessageId;
3569
3824
  };
3570
3825
  __name(ActionExecutionMessageOutput, "ActionExecutionMessageOutput");
3571
3826
  _ts_decorate13([
@@ -3573,8 +3828,11 @@ _ts_decorate13([
3573
3828
  _ts_metadata13("design:type", String)
3574
3829
  ], ActionExecutionMessageOutput.prototype, "name", void 0);
3575
3830
  _ts_decorate13([
3576
- (0, import_type_graphql14.Field)(() => ActionExecutionScope),
3577
- _ts_metadata13("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
3831
+ (0, import_type_graphql14.Field)(() => String, {
3832
+ nullable: true,
3833
+ deprecationReason: "This field will be removed in a future version"
3834
+ }),
3835
+ _ts_metadata13("design:type", String)
3578
3836
  ], ActionExecutionMessageOutput.prototype, "scope", void 0);
3579
3837
  _ts_decorate13([
3580
3838
  (0, import_type_graphql14.Field)(() => [
@@ -3582,6 +3840,12 @@ _ts_decorate13([
3582
3840
  ]),
3583
3841
  _ts_metadata13("design:type", Array)
3584
3842
  ], ActionExecutionMessageOutput.prototype, "arguments", void 0);
3843
+ _ts_decorate13([
3844
+ (0, import_type_graphql14.Field)(() => String, {
3845
+ nullable: true
3846
+ }),
3847
+ _ts_metadata13("design:type", String)
3848
+ ], ActionExecutionMessageOutput.prototype, "parentMessageId", void 0);
3585
3849
  ActionExecutionMessageOutput = _ts_decorate13([
3586
3850
  (0, import_type_graphql14.ObjectType)({
3587
3851
  implements: BaseMessageOutput
@@ -3692,7 +3956,7 @@ CopilotResponse = _ts_decorate13([
3692
3956
  // src/graphql/resolvers/copilot.resolver.ts
3693
3957
  var import_graphql_yoga = require("graphql-yoga");
3694
3958
  var import_graphql_scalars2 = require("graphql-scalars");
3695
- var import_class_transformer2 = require("class-transformer");
3959
+ var import_class_transformer3 = require("class-transformer");
3696
3960
  var import_graphql = require("graphql");
3697
3961
 
3698
3962
  // src/utils/failed-response-status-reasons.ts
@@ -3890,7 +4154,7 @@ var CopilotResolver = class {
3890
4154
  reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
3891
4155
  });
3892
4156
  outputMessages = [
3893
- (0, import_class_transformer2.plainToInstance)(TextMessage, {
4157
+ (0, import_class_transformer3.plainToInstance)(TextMessage, {
3894
4158
  id: (0, import_shared10.randomId)(),
3895
4159
  createdAt: /* @__PURE__ */ new Date(),
3896
4160
  content: result.reason,
@@ -3939,12 +4203,15 @@ var CopilotResolver = class {
3939
4203
  // skip until this message start event
3940
4204
  (0, import_rxjs4.skipWhile)((e) => e !== event),
3941
4205
  // take until the message end event
3942
- (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
4206
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.TextMessageEnd && e.messageId == event.messageId)),
4207
+ // filter out any other message events or message ids
4208
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.TextMessageContent && e.messageId == event.messageId)
3943
4209
  );
3944
4210
  const streamingTextStatus = new import_rxjs4.Subject();
3945
4211
  const messageId = event.messageId;
3946
4212
  pushMessage({
3947
4213
  id: messageId,
4214
+ parentMessageId: event.parentMessageId,
3948
4215
  status: (0, import_rxjs4.firstValueFrom)(streamingTextStatus),
3949
4216
  createdAt: /* @__PURE__ */ new Date(),
3950
4217
  role: MessageRole.assistant,
@@ -3957,7 +4224,7 @@ var CopilotResolver = class {
3957
4224
  reason,
3958
4225
  messageId: messageId2
3959
4226
  }, "Text streaming interrupted");
3960
- streamingTextStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4227
+ streamingTextStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
3961
4228
  reason
3962
4229
  }));
3963
4230
  responseStatus$.next(new MessageStreamInterruptedResponse({
@@ -3990,7 +4257,7 @@ var CopilotResolver = class {
3990
4257
  streamingTextStatus.next(new SuccessMessageStatus());
3991
4258
  stopStreamingText();
3992
4259
  textSubscription == null ? void 0 : textSubscription.unsubscribe();
3993
- outputMessages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
4260
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(TextMessage, {
3994
4261
  id: messageId,
3995
4262
  createdAt: /* @__PURE__ */ new Date(),
3996
4263
  content: textChunks.join(""),
@@ -4003,14 +4270,20 @@ var CopilotResolver = class {
4003
4270
  break;
4004
4271
  case RuntimeEventTypes.ActionExecutionStart:
4005
4272
  logger2.debug("Action execution start event received");
4006
- const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs4.skipWhile)((e) => e !== event), (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
4273
+ const actionExecutionArgumentStream = eventStream.pipe(
4274
+ (0, import_rxjs4.skipWhile)((e) => e !== event),
4275
+ // take until the action execution end event
4276
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.ActionExecutionEnd && e.actionExecutionId == event.actionExecutionId)),
4277
+ // filter out any other action execution events or action execution ids
4278
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.ActionExecutionArgs && e.actionExecutionId == event.actionExecutionId)
4279
+ );
4007
4280
  const streamingArgumentsStatus = new import_rxjs4.Subject();
4008
4281
  pushMessage({
4009
4282
  id: event.actionExecutionId,
4283
+ parentMessageId: event.parentMessageId,
4010
4284
  status: (0, import_rxjs4.firstValueFrom)(streamingArgumentsStatus),
4011
4285
  createdAt: /* @__PURE__ */ new Date(),
4012
4286
  name: event.actionName,
4013
- scope: event.scope,
4014
4287
  arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
4015
4288
  logger2.debug("Action execution argument stream created");
4016
4289
  const argumentChunks = [];
@@ -4026,7 +4299,7 @@ var CopilotResolver = class {
4026
4299
  logger2.error({
4027
4300
  err
4028
4301
  }, "Error in action execution argument stream");
4029
- streamingArgumentsStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4302
+ streamingArgumentsStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
4030
4303
  reason: "An unknown error has occurred in the action execution argument stream"
4031
4304
  }));
4032
4305
  stopStreamingArguments();
@@ -4037,11 +4310,10 @@ var CopilotResolver = class {
4037
4310
  streamingArgumentsStatus.next(new SuccessMessageStatus());
4038
4311
  stopStreamingArguments();
4039
4312
  actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
4040
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
4313
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ActionExecutionMessage, {
4041
4314
  id: event.actionExecutionId,
4042
4315
  createdAt: /* @__PURE__ */ new Date(),
4043
4316
  name: event.actionName,
4044
- scope: event.scope,
4045
4317
  arguments: argumentChunks.join("")
4046
4318
  }));
4047
4319
  }
@@ -4054,15 +4326,15 @@ var CopilotResolver = class {
4054
4326
  result: event.result
4055
4327
  }, "Action execution result event received");
4056
4328
  pushMessage({
4057
- id: (0, import_shared10.randomId)(),
4329
+ id: "result-" + event.actionExecutionId,
4058
4330
  status: new SuccessMessageStatus(),
4059
4331
  createdAt: /* @__PURE__ */ new Date(),
4060
4332
  actionExecutionId: event.actionExecutionId,
4061
4333
  actionName: event.actionName,
4062
4334
  result: event.result
4063
4335
  });
4064
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
4065
- id: (0, import_shared10.randomId)(),
4336
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ResultMessage, {
4337
+ id: "result-" + event.actionExecutionId,
4066
4338
  createdAt: /* @__PURE__ */ new Date(),
4067
4339
  actionExecutionId: event.actionExecutionId,
4068
4340
  actionName: event.actionName,
@@ -4086,7 +4358,7 @@ var CopilotResolver = class {
4086
4358
  role: MessageRole.assistant,
4087
4359
  createdAt: /* @__PURE__ */ new Date()
4088
4360
  });
4089
- outputMessages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
4361
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(AgentStateMessage, {
4090
4362
  id: (0, import_shared10.randomId)(),
4091
4363
  threadId: event.threadId,
4092
4364
  agentName: event.agentName,