@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/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",
@@ -430,6 +430,8 @@ var OpenAIAdapter = class {
430
430
  eventSource.stream(async (eventStream$) => {
431
431
  var _a, _b;
432
432
  let mode = null;
433
+ let currentMessageId;
434
+ let currentToolCallId;
433
435
  for await (const chunk of stream) {
434
436
  if (chunk.choices.length === 0) {
435
437
  continue;
@@ -438,30 +440,52 @@ var OpenAIAdapter = class {
438
440
  const content = chunk.choices[0].delta.content;
439
441
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
440
442
  mode = null;
441
- eventStream$.sendTextMessageEnd();
443
+ eventStream$.sendTextMessageEnd({
444
+ messageId: currentMessageId
445
+ });
442
446
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
443
447
  mode = null;
444
- eventStream$.sendActionExecutionEnd();
448
+ eventStream$.sendActionExecutionEnd({
449
+ actionExecutionId: currentToolCallId
450
+ });
445
451
  }
446
452
  if (mode === null) {
447
453
  if (toolCall == null ? void 0 : toolCall.id) {
448
454
  mode = "function";
449
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
455
+ currentToolCallId = toolCall.id;
456
+ eventStream$.sendActionExecutionStart({
457
+ actionExecutionId: currentToolCallId,
458
+ parentMessageId: chunk.id,
459
+ actionName: toolCall.function.name
460
+ });
450
461
  } else if (content) {
451
462
  mode = "message";
452
- eventStream$.sendTextMessageStart(chunk.id);
463
+ currentMessageId = chunk.id;
464
+ eventStream$.sendTextMessageStart({
465
+ messageId: currentMessageId
466
+ });
453
467
  }
454
468
  }
455
469
  if (mode === "message" && content) {
456
- eventStream$.sendTextMessageContent(content);
470
+ eventStream$.sendTextMessageContent({
471
+ messageId: currentMessageId,
472
+ content
473
+ });
457
474
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
458
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
475
+ eventStream$.sendActionExecutionArgs({
476
+ actionExecutionId: currentToolCallId,
477
+ args: toolCall.function.arguments
478
+ });
459
479
  }
460
480
  }
461
481
  if (mode === "message") {
462
- eventStream$.sendTextMessageEnd();
482
+ eventStream$.sendTextMessageEnd({
483
+ messageId: currentMessageId
484
+ });
463
485
  } else if (mode === "function") {
464
- eventStream$.sendActionExecutionEnd();
486
+ eventStream$.sendActionExecutionEnd({
487
+ actionExecutionId: currentToolCallId
488
+ });
465
489
  }
466
490
  eventStream$.complete();
467
491
  });
@@ -529,17 +553,25 @@ function isBaseMessageChunk(message) {
529
553
  __name(isBaseMessageChunk, "isBaseMessageChunk");
530
554
  function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
531
555
  if (actionExecution) {
532
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
556
+ eventStream$.sendActionExecutionResult({
557
+ actionExecutionId: actionExecution.id,
558
+ actionName: actionExecution.name,
559
+ result: "Sending a message"
560
+ });
533
561
  }
534
562
  }
535
563
  __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
536
564
  async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
537
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
565
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
538
566
  if (typeof result === "string") {
539
567
  if (!actionExecution) {
540
568
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
541
569
  } else {
542
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
570
+ eventStream$.sendActionExecutionResult({
571
+ actionExecutionId: actionExecution.id,
572
+ actionName: actionExecution.name,
573
+ result
574
+ });
543
575
  }
544
576
  } else if (isAIMessage(result)) {
545
577
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -547,7 +579,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
547
579
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
548
580
  }
549
581
  for (const toolCall of result.tool_calls) {
550
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
582
+ eventStream$.sendActionExecution({
583
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
584
+ actionName: toolCall.name,
585
+ args: JSON.stringify(toolCall.args)
586
+ });
551
587
  }
552
588
  } else if (isBaseMessageChunk(result)) {
553
589
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -556,13 +592,18 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
556
592
  }
557
593
  if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
558
594
  for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
559
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
595
+ eventStream$.sendActionExecution({
596
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
597
+ actionName: toolCall.name,
598
+ args: JSON.stringify(toolCall.args)
599
+ });
560
600
  }
561
601
  }
562
602
  } else if (result && "getReader" in result) {
563
603
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
564
604
  let reader = result.getReader();
565
605
  let mode = null;
606
+ let currentMessageId;
566
607
  const toolCallDetails = {
567
608
  name: null,
568
609
  id: null,
@@ -601,10 +642,14 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
601
642
  }
602
643
  if (mode === "message" && (toolCallId || done)) {
603
644
  mode = null;
604
- eventStream$.sendTextMessageEnd();
645
+ eventStream$.sendTextMessageEnd({
646
+ messageId: currentMessageId
647
+ });
605
648
  } else if (mode === "function" && (!hasToolCall || done)) {
606
649
  mode = null;
607
- eventStream$.sendActionExecutionEnd();
650
+ eventStream$.sendActionExecutionEnd({
651
+ actionExecutionId: toolCallId
652
+ });
608
653
  }
609
654
  if (done) {
610
655
  break;
@@ -612,21 +657,40 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
612
657
  if (mode === null) {
613
658
  if (hasToolCall && toolCallId && toolCallName) {
614
659
  mode = "function";
615
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
660
+ eventStream$.sendActionExecutionStart({
661
+ actionExecutionId: toolCallId,
662
+ actionName: toolCallName,
663
+ parentMessageId: (_i = value.lc_kwargs) == null ? void 0 : _i.id
664
+ });
616
665
  } else if (content) {
617
666
  mode = "message";
618
- eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
667
+ currentMessageId = ((_j = value.lc_kwargs) == null ? void 0 : _j.id) || (0, import_shared2.randomId)();
668
+ eventStream$.sendTextMessageStart({
669
+ messageId: currentMessageId
670
+ });
619
671
  }
620
672
  }
621
673
  if (mode === "message" && content) {
622
- eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
674
+ eventStream$.sendTextMessageContent({
675
+ messageId: currentMessageId,
676
+ content: Array.isArray(content) ? ((_k = content[0]) == null ? void 0 : _k.text) ?? "" : content
677
+ });
623
678
  } else if (mode === "function" && toolCallArgs) {
624
679
  if (toolCallDetails.index !== toolCallDetails.prevIndex) {
625
- eventStream$.sendActionExecutionEnd();
626
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
680
+ eventStream$.sendActionExecutionEnd({
681
+ actionExecutionId: toolCallId
682
+ });
683
+ eventStream$.sendActionExecutionStart({
684
+ actionExecutionId: toolCallId,
685
+ actionName: toolCallName,
686
+ parentMessageId: (_l = value.lc_kwargs) == null ? void 0 : _l.id
687
+ });
627
688
  toolCallDetails.prevIndex = toolCallDetails.index;
628
689
  }
629
- eventStream$.sendActionExecutionArgs(toolCallArgs);
690
+ eventStream$.sendActionExecutionArgs({
691
+ actionExecutionId: toolCallId,
692
+ args: toolCallArgs
693
+ });
630
694
  }
631
695
  } catch (error) {
632
696
  console.error("Error reading from stream", error);
@@ -634,7 +698,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
634
698
  }
635
699
  }
636
700
  } else if (actionExecution) {
637
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
701
+ eventStream$.sendActionExecutionResult({
702
+ actionExecutionId: actionExecution.id,
703
+ actionName: actionExecution.name,
704
+ result: encodeResult(result)
705
+ });
638
706
  } else {
639
707
  throw new Error("Invalid return type from LangChain function.");
640
708
  }
@@ -814,21 +882,33 @@ var OpenAIAssistantAdapter = class {
814
882
  eventSource.stream(async (eventStream$) => {
815
883
  var _a, _b, _c, _d, _e, _f;
816
884
  let inFunctionCall = false;
885
+ let currentMessageId;
886
+ let currentToolCallId;
817
887
  for await (const chunk of stream) {
818
888
  switch (chunk.event) {
819
889
  case "thread.message.created":
820
890
  if (inFunctionCall) {
821
- eventStream$.sendActionExecutionEnd();
891
+ eventStream$.sendActionExecutionEnd({
892
+ actionExecutionId: currentToolCallId
893
+ });
822
894
  }
823
- eventStream$.sendTextMessageStart(chunk.data.id);
895
+ currentMessageId = chunk.data.id;
896
+ eventStream$.sendTextMessageStart({
897
+ messageId: currentMessageId
898
+ });
824
899
  break;
825
900
  case "thread.message.delta":
826
901
  if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
827
- eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
902
+ eventStream$.sendTextMessageContent({
903
+ messageId: currentMessageId,
904
+ content: (_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value
905
+ });
828
906
  }
829
907
  break;
830
908
  case "thread.message.completed":
831
- eventStream$.sendTextMessageEnd();
909
+ eventStream$.sendTextMessageEnd({
910
+ messageId: currentMessageId
911
+ });
832
912
  break;
833
913
  case "thread.run.step.delta":
834
914
  let toolCallId;
@@ -841,18 +921,30 @@ var OpenAIAssistantAdapter = class {
841
921
  }
842
922
  if (toolCallName && toolCallId) {
843
923
  if (inFunctionCall) {
844
- eventStream$.sendActionExecutionEnd();
924
+ eventStream$.sendActionExecutionEnd({
925
+ actionExecutionId: currentToolCallId
926
+ });
845
927
  }
846
928
  inFunctionCall = true;
847
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
929
+ currentToolCallId = toolCallId;
930
+ eventStream$.sendActionExecutionStart({
931
+ actionExecutionId: currentToolCallId,
932
+ parentMessageId: chunk.data.id,
933
+ actionName: toolCallName
934
+ });
848
935
  } else if (toolCallArgs) {
849
- eventStream$.sendActionExecutionArgs(toolCallArgs);
936
+ eventStream$.sendActionExecutionArgs({
937
+ actionExecutionId: currentToolCallId,
938
+ args: toolCallArgs
939
+ });
850
940
  }
851
941
  break;
852
942
  }
853
943
  }
854
944
  if (inFunctionCall) {
855
- eventStream$.sendActionExecutionEnd();
945
+ eventStream$.sendActionExecutionEnd({
946
+ actionExecutionId: currentToolCallId
947
+ });
856
948
  }
857
949
  eventStream$.complete();
858
950
  });
@@ -905,46 +997,77 @@ var UnifyAdapter = class {
905
997
  }
906
998
  });
907
999
  let model = null;
1000
+ let currentMessageId;
1001
+ let currentToolCallId;
908
1002
  request.eventSource.stream(async (eventStream$) => {
909
1003
  var _a, _b;
910
1004
  let mode = null;
911
1005
  for await (const chunk of stream) {
912
1006
  if (this.start) {
913
1007
  model = chunk.model;
914
- eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
915
- eventStream$.sendTextMessageContent(`Model used: ${model}
916
- `);
917
- eventStream$.sendTextMessageEnd();
1008
+ currentMessageId = (0, import_shared4.randomId)();
1009
+ eventStream$.sendTextMessageStart({
1010
+ messageId: currentMessageId
1011
+ });
1012
+ eventStream$.sendTextMessageContent({
1013
+ messageId: currentMessageId,
1014
+ content: `Model used: ${model}
1015
+ `
1016
+ });
1017
+ eventStream$.sendTextMessageEnd({
1018
+ messageId: currentMessageId
1019
+ });
918
1020
  this.start = false;
919
1021
  }
920
1022
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
921
1023
  const content = chunk.choices[0].delta.content;
922
1024
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
923
1025
  mode = null;
924
- eventStream$.sendTextMessageEnd();
1026
+ eventStream$.sendTextMessageEnd({
1027
+ messageId: currentMessageId
1028
+ });
925
1029
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
926
1030
  mode = null;
927
- eventStream$.sendActionExecutionEnd();
1031
+ eventStream$.sendActionExecutionEnd({
1032
+ actionExecutionId: currentToolCallId
1033
+ });
928
1034
  }
929
1035
  if (mode === null) {
930
1036
  if (toolCall == null ? void 0 : toolCall.id) {
931
1037
  mode = "function";
932
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1038
+ currentToolCallId = toolCall.id;
1039
+ eventStream$.sendActionExecutionStart({
1040
+ actionExecutionId: currentToolCallId,
1041
+ actionName: toolCall.function.name
1042
+ });
933
1043
  } else if (content) {
934
1044
  mode = "message";
935
- eventStream$.sendTextMessageStart(chunk.id);
1045
+ currentMessageId = chunk.id;
1046
+ eventStream$.sendTextMessageStart({
1047
+ messageId: currentMessageId
1048
+ });
936
1049
  }
937
1050
  }
938
1051
  if (mode === "message" && content) {
939
- eventStream$.sendTextMessageContent(content);
1052
+ eventStream$.sendTextMessageContent({
1053
+ messageId: currentMessageId,
1054
+ content
1055
+ });
940
1056
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
941
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1057
+ eventStream$.sendActionExecutionArgs({
1058
+ actionExecutionId: currentToolCallId,
1059
+ args: toolCall.function.arguments
1060
+ });
942
1061
  }
943
1062
  }
944
1063
  if (mode === "message") {
945
- eventStream$.sendTextMessageEnd();
1064
+ eventStream$.sendTextMessageEnd({
1065
+ messageId: currentMessageId
1066
+ });
946
1067
  } else if (mode === "function") {
947
- eventStream$.sendActionExecutionEnd();
1068
+ eventStream$.sendActionExecutionEnd({
1069
+ actionExecutionId: currentToolCallId
1070
+ });
948
1071
  }
949
1072
  eventStream$.complete();
950
1073
  });
@@ -1010,35 +1133,59 @@ var GroqAdapter = class {
1010
1133
  eventSource.stream(async (eventStream$) => {
1011
1134
  var _a, _b;
1012
1135
  let mode = null;
1136
+ let currentMessageId;
1137
+ let currentToolCallId;
1013
1138
  for await (const chunk of stream) {
1014
1139
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
1015
1140
  const content = chunk.choices[0].delta.content;
1016
1141
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
1017
1142
  mode = null;
1018
- eventStream$.sendTextMessageEnd();
1143
+ eventStream$.sendTextMessageEnd({
1144
+ messageId: currentMessageId
1145
+ });
1019
1146
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
1020
1147
  mode = null;
1021
- eventStream$.sendActionExecutionEnd();
1148
+ eventStream$.sendActionExecutionEnd({
1149
+ actionExecutionId: currentToolCallId
1150
+ });
1022
1151
  }
1023
1152
  if (mode === null) {
1024
1153
  if (toolCall == null ? void 0 : toolCall.id) {
1025
1154
  mode = "function";
1026
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1155
+ currentToolCallId = toolCall.id;
1156
+ eventStream$.sendActionExecutionStart({
1157
+ actionExecutionId: currentToolCallId,
1158
+ actionName: toolCall.function.name,
1159
+ parentMessageId: chunk.id
1160
+ });
1027
1161
  } else if (content) {
1028
1162
  mode = "message";
1029
- eventStream$.sendTextMessageStart(chunk.id);
1163
+ currentMessageId = chunk.id;
1164
+ eventStream$.sendTextMessageStart({
1165
+ messageId: currentMessageId
1166
+ });
1030
1167
  }
1031
1168
  }
1032
1169
  if (mode === "message" && content) {
1033
- eventStream$.sendTextMessageContent(content);
1170
+ eventStream$.sendTextMessageContent({
1171
+ messageId: currentMessageId,
1172
+ content
1173
+ });
1034
1174
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
1035
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1175
+ eventStream$.sendActionExecutionArgs({
1176
+ actionExecutionId: currentToolCallId,
1177
+ args: toolCall.function.arguments
1178
+ });
1036
1179
  }
1037
1180
  }
1038
1181
  if (mode === "message") {
1039
- eventStream$.sendTextMessageEnd();
1182
+ eventStream$.sendTextMessageEnd({
1183
+ messageId: currentMessageId
1184
+ });
1040
1185
  } else if (mode === "function") {
1041
- eventStream$.sendActionExecutionEnd();
1186
+ eventStream$.sendActionExecutionEnd({
1187
+ actionExecutionId: currentToolCallId
1188
+ });
1042
1189
  }
1043
1190
  eventStream$.complete();
1044
1191
  });
@@ -1246,7 +1393,11 @@ var AnthropicAdapter = class {
1246
1393
  mode = "message";
1247
1394
  } else if (chunk.content_block.type === "tool_use") {
1248
1395
  currentToolCallId = chunk.content_block.id;
1249
- eventStream$.sendActionExecutionStart(currentToolCallId, chunk.content_block.name);
1396
+ eventStream$.sendActionExecutionStart({
1397
+ actionExecutionId: currentToolCallId,
1398
+ actionName: chunk.content_block.name,
1399
+ parentMessageId: currentMessageId
1400
+ });
1250
1401
  mode = "function";
1251
1402
  }
1252
1403
  } else if (chunk.type === "content_block_delta") {
@@ -1254,21 +1405,33 @@ var AnthropicAdapter = class {
1254
1405
  const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
1255
1406
  if (text.length > 0) {
1256
1407
  if (!didOutputText) {
1257
- eventStream$.sendTextMessageStart(currentMessageId);
1408
+ eventStream$.sendTextMessageStart({
1409
+ messageId: currentMessageId
1410
+ });
1258
1411
  didOutputText = true;
1259
1412
  }
1260
- eventStream$.sendTextMessageContent(text);
1413
+ eventStream$.sendTextMessageContent({
1414
+ messageId: currentMessageId,
1415
+ content: text
1416
+ });
1261
1417
  }
1262
1418
  } else if (chunk.delta.type === "input_json_delta") {
1263
- eventStream$.sendActionExecutionArgs(chunk.delta.partial_json);
1419
+ eventStream$.sendActionExecutionArgs({
1420
+ actionExecutionId: currentToolCallId,
1421
+ args: chunk.delta.partial_json
1422
+ });
1264
1423
  }
1265
1424
  } else if (chunk.type === "content_block_stop") {
1266
1425
  if (mode === "message") {
1267
1426
  if (didOutputText) {
1268
- eventStream$.sendTextMessageEnd();
1427
+ eventStream$.sendTextMessageEnd({
1428
+ messageId: currentMessageId
1429
+ });
1269
1430
  }
1270
1431
  } else if (mode === "function") {
1271
- eventStream$.sendActionExecutionEnd();
1432
+ eventStream$.sendActionExecutionEnd({
1433
+ actionExecutionId: currentToolCallId
1434
+ });
1272
1435
  }
1273
1436
  }
1274
1437
  }
@@ -1363,163 +1526,167 @@ var RemoteLangGraphEventSource = class {
1363
1526
  }
1364
1527
  return shouldEmitToolCalls === toolCallName;
1365
1528
  }
1529
+ getCurrentContent(event) {
1530
+ var _a, _b, _c, _d, _e;
1531
+ 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);
1532
+ if (!content) {
1533
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1534
+ for (const chunk of toolCallChunks) {
1535
+ if (chunk.args) {
1536
+ return chunk.args;
1537
+ }
1538
+ }
1539
+ }
1540
+ if (typeof content === "string") {
1541
+ return content;
1542
+ } else if (Array.isArray(content) && content.length > 0) {
1543
+ return content[0].text;
1544
+ }
1545
+ return null;
1546
+ }
1547
+ getCurrentMessageId(event) {
1548
+ var _a, _b, _c, _d, _e;
1549
+ 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);
1550
+ }
1551
+ getCurrentToolCallChunks(event) {
1552
+ var _a, _b, _c, _d, _e;
1553
+ 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);
1554
+ }
1555
+ getResponseMetadata(event) {
1556
+ var _a, _b, _c, _d, _e;
1557
+ 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);
1558
+ }
1366
1559
  processLangGraphEvents() {
1367
1560
  let lastEventWithState = null;
1368
1561
  return this.eventStream$.pipe((0, import_rxjs.scan)((acc, event) => {
1369
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1370
1562
  if (event.event === LangGraphEventTypes.OnChatModelStream) {
1371
- 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);
1372
- if (typeof content === "string") {
1373
- acc.content = content;
1374
- } else if (Array.isArray(content) && content.length > 0) {
1375
- acc.content = content[0].text;
1376
- } else {
1377
- acc.content = null;
1378
- }
1379
- const toolCallChunks = (
1380
- // @ts-expect-error -- LangGraph Platform implementation stores data outside of kwargs
1381
- ((_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)
1382
- );
1383
- 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);
1384
- if (toolCallChunks && toolCallChunks.length > 0) {
1385
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1386
- acc.toolCallMessageId = toolCallMessageId;
1387
- if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
1388
- acc.toolCallName = toolCallChunks[0].name;
1389
- }
1390
- if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
1391
- acc.toolCallId = toolCallChunks[0].id;
1392
- }
1393
- acc.prevMessageId = acc.messageId;
1394
- acc.messageId = toolCallMessageId;
1395
- } else if (acc.content && acc.content != "") {
1396
- acc.prevMessageId = acc.messageId;
1397
- acc.messageId = toolCallMessageId;
1398
- } else {
1399
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1400
- acc.prevMessageId = acc.messageId;
1401
- }
1402
- } else {
1403
- acc.prevToolCallMessageId = acc.toolCallMessageId;
1404
- acc.toolCallMessageId = null;
1405
- acc.prevMessageId = acc.messageId;
1406
- acc.messageId = null;
1407
- acc.toolCallName = null;
1563
+ const prevMessageId = acc.lastMessageId;
1564
+ acc.currentContent = this.getCurrentContent(event);
1565
+ acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
1566
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1567
+ const responseMetadata = this.getResponseMetadata(event);
1568
+ acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
1569
+ acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
1570
+ acc.isToolCall = toolCallChunks && toolCallChunks.length > 0;
1571
+ acc.isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
1572
+ acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
1573
+ ({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
1574
+ name: acc.lastToolCallName,
1575
+ id: acc.lastToolCallId
1576
+ });
1408
1577
  }
1409
1578
  acc.event = event;
1410
1579
  lastEventWithState = acc;
1411
1580
  return acc;
1412
1581
  }, {
1413
1582
  event: null,
1414
- toolCallId: null,
1415
- toolCallMessageId: null,
1416
- prevToolCallMessageId: null,
1417
- messageId: null,
1418
- toolCallName: null,
1419
- prevMessageId: null,
1420
- content: null
1421
- }), (0, import_rxjs.mergeMap)((eventWithState) => {
1422
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1583
+ isMessageStart: false,
1584
+ isMessageEnd: false,
1585
+ isToolCallStart: false,
1586
+ isToolCallEnd: false,
1587
+ isToolCall: false,
1588
+ lastMessageId: null,
1589
+ lastToolCallId: null,
1590
+ lastToolCallName: null,
1591
+ currentContent: null
1592
+ }), (0, import_rxjs.mergeMap)((acc) => {
1423
1593
  const events = [];
1424
1594
  let shouldEmitMessages = true;
1425
- let shouldEmitToolCalls = false;
1426
- if (eventWithState.event.event == LangGraphEventTypes.OnChatModelStream) {
1427
- if ("copilotkit:emit-tool-calls" in (eventWithState.event.metadata || {})) {
1428
- shouldEmitToolCalls = eventWithState.event.metadata["copilotkit:emit-tool-calls"];
1595
+ let shouldEmitToolCalls = true;
1596
+ if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
1597
+ if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
1598
+ shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
1429
1599
  }
1430
- if ("copilotkit:emit-messages" in (eventWithState.event.metadata || {})) {
1431
- shouldEmitMessages = eventWithState.event.metadata["copilotkit:emit-messages"];
1600
+ if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
1601
+ shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
1432
1602
  }
1433
1603
  }
1434
- if (eventWithState.prevToolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId && this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1604
+ const responseMetadata = this.getResponseMetadata(acc.event);
1605
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls" && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1435
1606
  events.push({
1436
- type: RuntimeEventTypes.ActionExecutionEnd
1607
+ type: RuntimeEventTypes.ActionExecutionEnd,
1608
+ actionExecutionId: acc.lastToolCallId
1437
1609
  });
1438
1610
  }
1439
- if (eventWithState.prevMessageId !== null && eventWithState.prevMessageId !== eventWithState.messageId && shouldEmitMessages) {
1611
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
1440
1612
  events.push({
1441
- type: RuntimeEventTypes.TextMessageEnd
1613
+ type: RuntimeEventTypes.TextMessageEnd,
1614
+ messageId: acc.lastMessageId
1442
1615
  });
1443
1616
  }
1444
- switch (eventWithState.event.event) {
1617
+ switch (acc.event.event) {
1445
1618
  case LangGraphEventTypes.OnCustomEvent:
1446
- if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1619
+ if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1447
1620
  events.push({
1448
1621
  type: RuntimeEventTypes.TextMessageStart,
1449
- messageId: eventWithState.event.data.message_id
1622
+ messageId: acc.event.data.message_id
1450
1623
  });
1451
1624
  events.push({
1452
1625
  type: RuntimeEventTypes.TextMessageContent,
1453
- content: eventWithState.event.data.message
1626
+ messageId: acc.event.data.message_id,
1627
+ content: acc.event.data.message
1454
1628
  });
1455
1629
  events.push({
1456
- type: RuntimeEventTypes.TextMessageEnd
1630
+ type: RuntimeEventTypes.TextMessageEnd,
1631
+ messageId: acc.event.data.message_id
1457
1632
  });
1458
- } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1633
+ } else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1459
1634
  events.push({
1460
1635
  type: RuntimeEventTypes.ActionExecutionStart,
1461
- actionExecutionId: eventWithState.event.data.id,
1462
- actionName: eventWithState.event.data.name
1636
+ actionExecutionId: acc.event.data.id,
1637
+ actionName: acc.event.data.name,
1638
+ parentMessageId: acc.event.data.id
1463
1639
  });
1464
1640
  events.push({
1465
1641
  type: RuntimeEventTypes.ActionExecutionArgs,
1466
- args: JSON.stringify(eventWithState.event.data.args)
1642
+ actionExecutionId: acc.event.data.id,
1643
+ args: JSON.stringify(acc.event.data.args)
1467
1644
  });
1468
1645
  events.push({
1469
- type: RuntimeEventTypes.ActionExecutionEnd
1646
+ type: RuntimeEventTypes.ActionExecutionEnd,
1647
+ actionExecutionId: acc.event.data.id
1470
1648
  });
1471
1649
  }
1472
1650
  break;
1473
1651
  case LangGraphEventTypes.OnCopilotKitStateSync:
1474
1652
  events.push({
1475
1653
  type: RuntimeEventTypes.AgentStateMessage,
1476
- threadId: eventWithState.event.thread_id,
1477
- role: eventWithState.event.role,
1478
- agentName: eventWithState.event.agent_name,
1479
- nodeName: eventWithState.event.node_name,
1480
- runId: eventWithState.event.run_id,
1481
- active: eventWithState.event.active,
1482
- state: JSON.stringify(eventWithState.event.state),
1483
- running: eventWithState.event.running
1654
+ threadId: acc.event.thread_id,
1655
+ role: acc.event.role,
1656
+ agentName: acc.event.agent_name,
1657
+ nodeName: acc.event.node_name,
1658
+ runId: acc.event.run_id,
1659
+ active: acc.event.active,
1660
+ state: JSON.stringify(acc.event.state),
1661
+ running: acc.event.running
1484
1662
  });
1485
1663
  break;
1486
- case LangGraphEventTypes.OnToolEnd:
1487
- break;
1488
1664
  case LangGraphEventTypes.OnChatModelStream:
1489
- if (eventWithState.toolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId) {
1490
- if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1491
- events.push({
1492
- type: RuntimeEventTypes.ActionExecutionStart,
1493
- actionExecutionId: eventWithState.toolCallMessageId,
1494
- actionName: eventWithState.toolCallName,
1495
- scope: "client"
1496
- });
1497
- }
1498
- } else if (eventWithState.messageId !== null && eventWithState.prevMessageId !== eventWithState.messageId) {
1499
- if (shouldEmitMessages) {
1500
- events.push({
1501
- type: RuntimeEventTypes.TextMessageStart,
1502
- messageId: eventWithState.messageId
1503
- });
1504
- }
1665
+ if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1666
+ events.push({
1667
+ type: RuntimeEventTypes.ActionExecutionStart,
1668
+ actionExecutionId: acc.lastToolCallId,
1669
+ actionName: acc.lastToolCallName,
1670
+ parentMessageId: acc.lastMessageId
1671
+ });
1672
+ } else if (acc.isMessageStart && shouldEmitMessages) {
1673
+ events.push({
1674
+ type: RuntimeEventTypes.TextMessageStart,
1675
+ messageId: acc.lastMessageId
1676
+ });
1505
1677
  }
1506
- 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
1507
- ((_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);
1508
- const content = eventWithState.content;
1509
- if (args) {
1510
- if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1511
- events.push({
1512
- type: RuntimeEventTypes.ActionExecutionArgs,
1513
- args
1514
- });
1515
- }
1516
- } else if (eventWithState.messageId !== null && content) {
1517
- if (shouldEmitMessages) {
1518
- events.push({
1519
- type: RuntimeEventTypes.TextMessageContent,
1520
- content
1521
- });
1522
- }
1678
+ if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1679
+ events.push({
1680
+ type: RuntimeEventTypes.ActionExecutionArgs,
1681
+ actionExecutionId: acc.lastToolCallId,
1682
+ args: acc.currentContent
1683
+ });
1684
+ } else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
1685
+ events.push({
1686
+ type: RuntimeEventTypes.TextMessageContent,
1687
+ messageId: acc.lastMessageId,
1688
+ content: acc.currentContent
1689
+ });
1523
1690
  }
1524
1691
  break;
1525
1692
  }
@@ -1527,14 +1694,16 @@ var RemoteLangGraphEventSource = class {
1527
1694
  }), (0, import_rxjs.catchError)((error) => {
1528
1695
  console.error(error);
1529
1696
  const events = [];
1530
- if (lastEventWithState == null ? void 0 : lastEventWithState.messageId) {
1697
+ if ((lastEventWithState == null ? void 0 : lastEventWithState.lastMessageId) && !lastEventWithState.isToolCall) {
1531
1698
  events.push({
1532
- type: RuntimeEventTypes.TextMessageEnd
1699
+ type: RuntimeEventTypes.TextMessageEnd,
1700
+ messageId: lastEventWithState.lastMessageId
1533
1701
  });
1534
1702
  }
1535
- if (lastEventWithState == null ? void 0 : lastEventWithState.toolCallMessageId) {
1703
+ if (lastEventWithState == null ? void 0 : lastEventWithState.lastToolCallId) {
1536
1704
  events.push({
1537
- type: RuntimeEventTypes.ActionExecutionEnd
1705
+ type: RuntimeEventTypes.ActionExecutionEnd,
1706
+ actionExecutionId: lastEventWithState.lastToolCallId
1538
1707
  });
1539
1708
  }
1540
1709
  const messageId = (0, import_shared7.randomId)();
@@ -1544,10 +1713,12 @@ var RemoteLangGraphEventSource = class {
1544
1713
  });
1545
1714
  events.push({
1546
1715
  type: RuntimeEventTypes.TextMessageContent,
1716
+ messageId,
1547
1717
  content: "\u274C An error occurred. Please try again."
1548
1718
  });
1549
1719
  events.push({
1550
- type: RuntimeEventTypes.TextMessageEnd
1720
+ type: RuntimeEventTypes.TextMessageEnd,
1721
+ messageId
1551
1722
  });
1552
1723
  return events;
1553
1724
  }));
@@ -1569,12 +1740,6 @@ var MessageRole;
1569
1740
  MessageRole2["system"] = "system";
1570
1741
  MessageRole2["tool"] = "tool";
1571
1742
  })(MessageRole || (MessageRole = {}));
1572
- var ActionExecutionScope;
1573
- (function(ActionExecutionScope2) {
1574
- ActionExecutionScope2["server"] = "server";
1575
- ActionExecutionScope2["client"] = "client";
1576
- ActionExecutionScope2["passThrough"] = "passThrough";
1577
- })(ActionExecutionScope || (ActionExecutionScope = {}));
1578
1743
  var CopilotRequestType;
1579
1744
  (function(CopilotRequestType2) {
1580
1745
  CopilotRequestType2["Chat"] = "Chat";
@@ -1593,10 +1758,6 @@ var ActionInputAvailability;
1593
1758
  name: "MessageRole",
1594
1759
  description: "The role of the message"
1595
1760
  });
1596
- (0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
1597
- name: "ActionExecutionScope",
1598
- description: "The scope of the action"
1599
- });
1600
1761
  (0, import_type_graphql.registerEnumType)(CopilotRequestType, {
1601
1762
  name: "CopilotRequestType",
1602
1763
  description: "The type of Copilot request"
@@ -1653,7 +1814,7 @@ async function streamEvents(controller, args) {
1653
1814
  const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1654
1815
  let formattedMessages = [];
1655
1816
  try {
1656
- formattedMessages = formatMessages(messages);
1817
+ formattedMessages = copilotkitMessagesToLangChain(messages);
1657
1818
  } catch (e) {
1658
1819
  logger2.error(e, `Error event thrown: ${e.message}`);
1659
1820
  }
@@ -1806,7 +1967,8 @@ async function streamEvents(controller, args) {
1806
1967
  nodeName: isEndNode ? "__end__" : nodeName,
1807
1968
  state: state.values,
1808
1969
  running: !shouldExit,
1809
- active: false
1970
+ active: false,
1971
+ includeMessages: true
1810
1972
  }));
1811
1973
  return Promise.resolve();
1812
1974
  } catch (e) {
@@ -1819,13 +1981,20 @@ async function streamEvents(controller, args) {
1819
1981
  }
1820
1982
  }
1821
1983
  __name(streamEvents, "streamEvents");
1822
- function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
1823
- const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
1824
- if (key !== "messages") {
1825
- acc[key] = state[key];
1826
- }
1827
- return acc;
1828
- }, {});
1984
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active, includeMessages = false }) {
1985
+ if (!includeMessages) {
1986
+ state = Object.keys(state).reduce((acc, key) => {
1987
+ if (key !== "messages") {
1988
+ acc[key] = state[key];
1989
+ }
1990
+ return acc;
1991
+ }, {});
1992
+ } else {
1993
+ state = {
1994
+ ...state,
1995
+ messages: langchainMessagesToCopilotKit(state.messages || [])
1996
+ };
1997
+ }
1829
1998
  return JSON.stringify({
1830
1999
  event: LangGraphEventTypes.OnCopilotKitStateSync,
1831
2000
  thread_id: threadId,
@@ -1833,7 +2002,7 @@ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, runnin
1833
2002
  agent_name: agentName,
1834
2003
  node_name: nodeName,
1835
2004
  active,
1836
- state: stateWithoutMessages,
2005
+ state,
1837
2006
  running,
1838
2007
  role: "assistant"
1839
2008
  }) + "\n";
@@ -1909,134 +2078,159 @@ function langGraphDefaultMergeState(state, messages, actions, agentName) {
1909
2078
  if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1910
2079
  messages = messages.slice(1);
1911
2080
  }
1912
- const mergedMessages = state.messages || [];
1913
- const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
1914
- const existingToolCallResults = /* @__PURE__ */ new Set();
1915
- for (const message of mergedMessages) {
1916
- if ("tool_call_id" in message) {
1917
- existingToolCallResults.add(message.tool_call_id);
2081
+ const existingMessages = state.messages || [];
2082
+ const existingMessageIds = new Set(existingMessages.map((message) => message.id));
2083
+ const newMessages = messages.filter((message) => !existingMessageIds.has(message.id));
2084
+ return {
2085
+ ...state,
2086
+ messages: newMessages,
2087
+ copilotkit: {
2088
+ actions
2089
+ }
2090
+ };
2091
+ }
2092
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
2093
+ function langchainMessagesToCopilotKit(messages) {
2094
+ const result = [];
2095
+ const tool_call_names = {};
2096
+ for (const message of messages) {
2097
+ if (message.type === "ai") {
2098
+ for (const tool_call of message.tool_calls) {
2099
+ tool_call_names[tool_call.id] = tool_call.name;
2100
+ }
1918
2101
  }
1919
2102
  }
1920
2103
  for (const message of messages) {
1921
- if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
1922
- continue;
2104
+ let content = message.content;
2105
+ if (content instanceof Array) {
2106
+ content = content[0];
1923
2107
  }
1924
- if ("name" in message && message.name === agentName) {
1925
- continue;
2108
+ if (content instanceof Object) {
2109
+ content = content.text;
1926
2110
  }
1927
- if (!existingMessageIds.has(message.id)) {
1928
- if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
1929
- console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
1930
- continue;
1931
- }
1932
- mergedMessages.push(message);
1933
- } else {
1934
- for (let i = 0; i < mergedMessages.length; i++) {
1935
- if (mergedMessages[i].id === message.id && message.role === "assistant") {
1936
- if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
1937
- message.tool_calls = mergedMessages[i]["tool_calls"];
1938
- message.additional_kwargs = mergedMessages[i].additional_kwargs;
1939
- }
1940
- mergedMessages[i] = message;
2111
+ if (message.type === "human") {
2112
+ result.push({
2113
+ role: "user",
2114
+ content,
2115
+ id: message.id
2116
+ });
2117
+ } else if (message.type === "system") {
2118
+ result.push({
2119
+ role: "system",
2120
+ content,
2121
+ id: message.id
2122
+ });
2123
+ } else if (message.type === "ai") {
2124
+ if (message.tool_calls && message.tool_calls.length > 0) {
2125
+ for (const tool_call of message.tool_calls) {
2126
+ result.push({
2127
+ id: tool_call.id,
2128
+ name: tool_call.name,
2129
+ arguments: tool_call.args,
2130
+ parentMessageId: message.id
2131
+ });
1941
2132
  }
2133
+ } else {
2134
+ result.push({
2135
+ role: "assistant",
2136
+ content,
2137
+ id: message.id,
2138
+ parentMessageId: message.id
2139
+ });
1942
2140
  }
2141
+ } else if (message.type === "tool") {
2142
+ const actionName = tool_call_names[message.tool_call_id] || message.name || "";
2143
+ result.push({
2144
+ actionExecutionId: message.tool_call_id,
2145
+ actionName,
2146
+ result: content,
2147
+ id: message.id
2148
+ });
1943
2149
  }
1944
2150
  }
1945
- for (let i = 0; i < mergedMessages.length - 1; i++) {
1946
- const currentMessage = mergedMessages[i];
1947
- const nextMessage = mergedMessages[i + 1];
1948
- if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
1949
- nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
2151
+ const resultsDict = {};
2152
+ for (const msg of result) {
2153
+ if (msg.actionExecutionId) {
2154
+ resultsDict[msg.actionExecutionId] = msg;
1950
2155
  }
1951
2156
  }
1952
- const correctedMessages = [];
1953
- for (let i = 0; i < mergedMessages.length; i++) {
1954
- const currentMessage = mergedMessages[i];
1955
- const nextMessage = mergedMessages[i + 1] || null;
1956
- const prevMessage = mergedMessages[i - 1] || null;
1957
- if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
1958
- if (!nextMessage) {
1959
- console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
1960
- continue;
1961
- }
1962
- if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
1963
- const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
1964
- if (toolMessage) {
1965
- console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
1966
- correctedMessages.push(currentMessage, toolMessage);
1967
- continue;
1968
- } else {
1969
- console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
1970
- continue;
1971
- }
1972
- }
1973
- correctedMessages.push(currentMessage);
1974
- continue;
2157
+ const reorderedResult = [];
2158
+ for (const msg of result) {
2159
+ if (!("actionExecutionId" in msg)) {
2160
+ reorderedResult.push(msg);
1975
2161
  }
1976
- if ("tool_call_id" in currentMessage) {
1977
- if (!prevMessage || !("tool_calls" in prevMessage)) {
1978
- console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
1979
- continue;
1980
- }
1981
- if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
1982
- console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
1983
- continue;
2162
+ if ("arguments" in msg) {
2163
+ const msgId = msg.id;
2164
+ if (msgId in resultsDict) {
2165
+ reorderedResult.push(resultsDict[msgId]);
2166
+ } else {
2167
+ console.warn("Tool call result message not found for id:", msgId);
1984
2168
  }
1985
- correctedMessages.push(currentMessage);
1986
- continue;
1987
2169
  }
1988
- correctedMessages.push(currentMessage);
1989
2170
  }
1990
- return {
1991
- ...state,
1992
- messages: correctedMessages,
1993
- copilotkit: {
1994
- actions
1995
- }
1996
- };
2171
+ return reorderedResult;
1997
2172
  }
1998
- __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1999
- function formatMessages(messages) {
2000
- return messages.map((message) => {
2001
- if (message.isTextMessage() && message.role === "assistant") {
2002
- return message;
2003
- }
2004
- if (message.isTextMessage() && message.role === "system") {
2005
- return message;
2006
- }
2007
- if (message.isTextMessage() && message.role === "user") {
2008
- return message;
2173
+ __name(langchainMessagesToCopilotKit, "langchainMessagesToCopilotKit");
2174
+ function copilotkitMessagesToLangChain(messages) {
2175
+ const result = [];
2176
+ const processedActionExecutions = /* @__PURE__ */ new Set();
2177
+ for (const message of messages) {
2178
+ if (message.isTextMessage()) {
2179
+ if (message.role === "user") {
2180
+ result.push({
2181
+ ...message,
2182
+ role: MessageRole.user
2183
+ });
2184
+ } else if (message.role === "system") {
2185
+ result.push({
2186
+ ...message,
2187
+ role: MessageRole.system
2188
+ });
2189
+ } else if (message.role === "assistant") {
2190
+ result.push({
2191
+ ...message,
2192
+ role: MessageRole.assistant
2193
+ });
2194
+ }
2195
+ continue;
2009
2196
  }
2010
2197
  if (message.isActionExecutionMessage()) {
2011
- const toolCall = {
2012
- name: message.name,
2013
- args: message.arguments,
2014
- id: message.id
2015
- };
2016
- return {
2017
- type: message.type,
2198
+ const messageId = message.parentMessageId ?? message.id;
2199
+ if (processedActionExecutions.has(messageId)) {
2200
+ continue;
2201
+ }
2202
+ processedActionExecutions.add(messageId);
2203
+ const relatedActionExecutions = messages.filter((m) => m.isActionExecutionMessage() && (m.parentMessageId && m.parentMessageId === messageId || m.id === messageId));
2204
+ const tool_calls = relatedActionExecutions.map((m) => ({
2205
+ name: m.name,
2206
+ args: m.arguments,
2207
+ id: m.id
2208
+ }));
2209
+ result.push({
2210
+ id: messageId,
2211
+ type: "ActionExecutionMessage",
2018
2212
  content: "",
2019
- tool_calls: [
2020
- toolCall
2021
- ],
2022
- role: MessageRole.assistant,
2023
- id: message.id
2024
- };
2213
+ tool_calls,
2214
+ role: MessageRole.assistant
2215
+ });
2216
+ continue;
2025
2217
  }
2026
2218
  if (message.isResultMessage()) {
2027
- return {
2219
+ result.push({
2028
2220
  type: message.type,
2029
2221
  content: message.result,
2030
2222
  id: message.id,
2031
2223
  tool_call_id: message.actionExecutionId,
2032
2224
  name: message.actionName,
2033
2225
  role: MessageRole.tool
2034
- };
2226
+ });
2227
+ continue;
2035
2228
  }
2036
2229
  throw new Error(`Unknown message type ${message.type}`);
2037
- });
2230
+ }
2231
+ return result;
2038
2232
  }
2039
- __name(formatMessages, "formatMessages");
2233
+ __name(copilotkitMessagesToLangChain, "copilotkitMessagesToLangChain");
2040
2234
 
2041
2235
  // src/lib/runtime/remote-action-constructors.ts
2042
2236
  function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
@@ -2046,7 +2240,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
2046
2240
  parameters: [],
2047
2241
  handler: async (_args) => {
2048
2242
  },
2049
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2243
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
2050
2244
  var _a;
2051
2245
  logger2.debug({
2052
2246
  actionName: agent.name
@@ -2072,7 +2266,10 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
2072
2266
  agent,
2073
2267
  threadId,
2074
2268
  nodeName,
2075
- messages,
2269
+ messages: [
2270
+ ...messages,
2271
+ ...additionalMessages
2272
+ ],
2076
2273
  state,
2077
2274
  properties: graphqlContext.properties,
2078
2275
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -2154,7 +2351,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
2154
2351
  parameters: [],
2155
2352
  handler: async (_args) => {
2156
2353
  },
2157
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2354
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
2158
2355
  var _a;
2159
2356
  logger2.debug({
2160
2357
  actionName: agent.name
@@ -2179,7 +2376,10 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
2179
2376
  name,
2180
2377
  threadId,
2181
2378
  nodeName,
2182
- messages,
2379
+ messages: [
2380
+ ...messages,
2381
+ ...additionalMessages
2382
+ ],
2183
2383
  state,
2184
2384
  properties: graphqlContext.properties,
2185
2385
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -2422,7 +2622,94 @@ function getRuntimeInstanceTelemetryInfo(runtime) {
2422
2622
  __name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
2423
2623
  var telemetry_client_default = telemetryClient;
2424
2624
 
2625
+ // src/graphql/types/base/index.ts
2626
+ var import_type_graphql2 = require("type-graphql");
2627
+ function _ts_decorate(decorators, target, key, desc) {
2628
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2629
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2630
+ r = Reflect.decorate(decorators, target, key, desc);
2631
+ else
2632
+ for (var i = decorators.length - 1; i >= 0; i--)
2633
+ if (d = decorators[i])
2634
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2635
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2636
+ }
2637
+ __name(_ts_decorate, "_ts_decorate");
2638
+ function _ts_metadata(k, v) {
2639
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2640
+ return Reflect.metadata(k, v);
2641
+ }
2642
+ __name(_ts_metadata, "_ts_metadata");
2643
+ var BaseMessageInput = class {
2644
+ id;
2645
+ createdAt;
2646
+ };
2647
+ __name(BaseMessageInput, "BaseMessageInput");
2648
+ _ts_decorate([
2649
+ (0, import_type_graphql2.Field)(() => String),
2650
+ _ts_metadata("design:type", String)
2651
+ ], BaseMessageInput.prototype, "id", void 0);
2652
+ _ts_decorate([
2653
+ (0, import_type_graphql2.Field)(() => Date),
2654
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2655
+ ], BaseMessageInput.prototype, "createdAt", void 0);
2656
+ BaseMessageInput = _ts_decorate([
2657
+ (0, import_type_graphql2.InputType)()
2658
+ ], BaseMessageInput);
2659
+
2660
+ // src/graphql/types/converted/index.ts
2661
+ var Message = class extends BaseMessageInput {
2662
+ type;
2663
+ isTextMessage() {
2664
+ return this.type === "TextMessage";
2665
+ }
2666
+ isActionExecutionMessage() {
2667
+ return this.type === "ActionExecutionMessage";
2668
+ }
2669
+ isResultMessage() {
2670
+ return this.type === "ResultMessage";
2671
+ }
2672
+ isAgentStateMessage() {
2673
+ return this.type === "AgentStateMessage";
2674
+ }
2675
+ };
2676
+ __name(Message, "Message");
2677
+ var TextMessage = class extends Message {
2678
+ type = "TextMessage";
2679
+ content;
2680
+ role;
2681
+ parentMessageId;
2682
+ };
2683
+ __name(TextMessage, "TextMessage");
2684
+ var ActionExecutionMessage = class extends Message {
2685
+ type = "ActionExecutionMessage";
2686
+ name;
2687
+ arguments;
2688
+ parentMessageId;
2689
+ };
2690
+ __name(ActionExecutionMessage, "ActionExecutionMessage");
2691
+ var ResultMessage = class extends Message {
2692
+ type = "ResultMessage";
2693
+ actionExecutionId;
2694
+ actionName;
2695
+ result;
2696
+ };
2697
+ __name(ResultMessage, "ResultMessage");
2698
+ var AgentStateMessage = class extends Message {
2699
+ type = "AgentStateMessage";
2700
+ threadId;
2701
+ agentName;
2702
+ nodeName;
2703
+ runId;
2704
+ active;
2705
+ role;
2706
+ state;
2707
+ running;
2708
+ };
2709
+ __name(AgentStateMessage, "AgentStateMessage");
2710
+
2425
2711
  // src/service-adapters/events.ts
2712
+ var import_class_transformer = require("class-transformer");
2426
2713
  var RuntimeEventTypes;
2427
2714
  (function(RuntimeEventTypes2) {
2428
2715
  RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
@@ -2438,52 +2725,74 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2438
2725
  constructor() {
2439
2726
  super();
2440
2727
  }
2441
- sendTextMessageStart(messageId) {
2728
+ sendTextMessageStart({ messageId, parentMessageId }) {
2442
2729
  this.next({
2443
2730
  type: "TextMessageStart",
2444
- messageId
2731
+ messageId,
2732
+ parentMessageId
2445
2733
  });
2446
2734
  }
2447
- sendTextMessageContent(content) {
2735
+ sendTextMessageContent({ messageId, content }) {
2448
2736
  this.next({
2449
2737
  type: "TextMessageContent",
2450
- content
2738
+ content,
2739
+ messageId
2451
2740
  });
2452
2741
  }
2453
- sendTextMessageEnd() {
2742
+ sendTextMessageEnd({ messageId }) {
2454
2743
  this.next({
2455
- type: "TextMessageEnd"
2744
+ type: "TextMessageEnd",
2745
+ messageId
2456
2746
  });
2457
2747
  }
2458
2748
  sendTextMessage(messageId, content) {
2459
- this.sendTextMessageStart(messageId);
2460
- this.sendTextMessageContent(content);
2461
- this.sendTextMessageEnd();
2749
+ this.sendTextMessageStart({
2750
+ messageId
2751
+ });
2752
+ this.sendTextMessageContent({
2753
+ messageId,
2754
+ content
2755
+ });
2756
+ this.sendTextMessageEnd({
2757
+ messageId
2758
+ });
2462
2759
  }
2463
- sendActionExecutionStart(actionExecutionId, actionName) {
2760
+ sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId }) {
2464
2761
  this.next({
2465
2762
  type: "ActionExecutionStart",
2466
2763
  actionExecutionId,
2467
- actionName
2764
+ actionName,
2765
+ parentMessageId
2468
2766
  });
2469
2767
  }
2470
- sendActionExecutionArgs(args) {
2768
+ sendActionExecutionArgs({ actionExecutionId, args }) {
2471
2769
  this.next({
2472
2770
  type: "ActionExecutionArgs",
2473
- args
2771
+ args,
2772
+ actionExecutionId
2474
2773
  });
2475
2774
  }
2476
- sendActionExecutionEnd() {
2775
+ sendActionExecutionEnd({ actionExecutionId }) {
2477
2776
  this.next({
2478
- type: "ActionExecutionEnd"
2777
+ type: "ActionExecutionEnd",
2778
+ actionExecutionId
2479
2779
  });
2480
2780
  }
2481
- sendActionExecution(actionExecutionId, toolName, args) {
2482
- this.sendActionExecutionStart(actionExecutionId, toolName);
2483
- this.sendActionExecutionArgs(args);
2484
- this.sendActionExecutionEnd();
2781
+ sendActionExecution({ actionExecutionId, actionName, args, parentMessageId }) {
2782
+ this.sendActionExecutionStart({
2783
+ actionExecutionId,
2784
+ actionName,
2785
+ parentMessageId
2786
+ });
2787
+ this.sendActionExecutionArgs({
2788
+ actionExecutionId,
2789
+ args
2790
+ });
2791
+ this.sendActionExecutionEnd({
2792
+ actionExecutionId
2793
+ });
2485
2794
  }
2486
- sendActionExecutionResult(actionExecutionId, actionName, result) {
2795
+ sendActionExecutionResult({ actionExecutionId, actionName, result }) {
2487
2796
  this.next({
2488
2797
  type: "ActionExecutionResult",
2489
2798
  actionName,
@@ -2491,7 +2800,7 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2491
2800
  result
2492
2801
  });
2493
2802
  }
2494
- sendAgentStateMessage(threadId, agentName, nodeName, runId, active, role, state, running) {
2803
+ sendAgentStateMessage({ threadId, agentName, nodeName, runId, active, role, state, running }) {
2495
2804
  this.next({
2496
2805
  type: "AgentStateMessage",
2497
2806
  threadId,
@@ -2528,22 +2837,13 @@ var RuntimeEventSource = class {
2528
2837
  this.sendErrorMessageToChat();
2529
2838
  });
2530
2839
  return this.eventStream$.pipe(
2531
- // mark tools for server side execution
2532
- (0, import_rxjs2.map)((event) => {
2533
- if (event.type === "ActionExecutionStart") {
2534
- if (event.scope !== "passThrough") {
2535
- event.scope = serverSideActions.find((action) => action.name === event.actionName) ? "server" : "client";
2536
- }
2537
- }
2538
- return event;
2539
- }),
2540
2840
  // track state
2541
2841
  (0, import_rxjs2.scan)((acc, event) => {
2542
2842
  acc = {
2543
2843
  ...acc
2544
2844
  };
2545
2845
  if (event.type === "ActionExecutionStart") {
2546
- acc.callActionServerSide = event.scope === "server";
2846
+ acc.callActionServerSide = serverSideActions.find((action) => action.name === event.actionName) !== void 0;
2547
2847
  acc.args = "";
2548
2848
  acc.actionExecutionId = event.actionExecutionId;
2549
2849
  if (acc.callActionServerSide) {
@@ -2597,10 +2897,33 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2597
2897
  }
2598
2898
  }
2599
2899
  if (isLangGraphAgentAction(action)) {
2600
- eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
2900
+ const result = `${action.name} agent started`;
2901
+ const agentExecution = (0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2902
+ id: actionExecutionId,
2903
+ createdAt: /* @__PURE__ */ new Date(),
2904
+ name: action.name,
2905
+ arguments: JSON.parse(actionArguments),
2906
+ parentMessageId: actionExecutionId
2907
+ });
2908
+ const agentExecutionResult = (0, import_class_transformer.plainToInstance)(ResultMessage, {
2909
+ id: "result-" + actionExecutionId,
2910
+ createdAt: /* @__PURE__ */ new Date(),
2911
+ actionExecutionId,
2912
+ actionName: action.name,
2913
+ result
2914
+ });
2915
+ eventStream$.sendActionExecutionResult({
2916
+ actionExecutionId,
2917
+ actionName: action.name,
2918
+ result
2919
+ });
2601
2920
  const stream = await action.langGraphAgentHandler({
2602
2921
  name: action.name,
2603
- actionInputsWithoutAgents
2922
+ actionInputsWithoutAgents,
2923
+ additionalMessages: [
2924
+ agentExecution,
2925
+ agentExecutionResult
2926
+ ]
2604
2927
  });
2605
2928
  (0, import_rxjs2.from)(stream).subscribe({
2606
2929
  next: (event) => eventStream$.next(event),
@@ -2621,113 +2944,29 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2621
2944
  }
2622
2945
  __name(executeAction, "executeAction");
2623
2946
 
2624
- // src/graphql/types/base/index.ts
2625
- var import_type_graphql2 = require("type-graphql");
2626
- function _ts_decorate(decorators, target, key, desc) {
2627
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2628
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2629
- r = Reflect.decorate(decorators, target, key, desc);
2630
- else
2631
- for (var i = decorators.length - 1; i >= 0; i--)
2632
- if (d = decorators[i])
2633
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2634
- return c > 3 && r && Object.defineProperty(target, key, r), r;
2635
- }
2636
- __name(_ts_decorate, "_ts_decorate");
2637
- function _ts_metadata(k, v) {
2638
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2639
- return Reflect.metadata(k, v);
2640
- }
2641
- __name(_ts_metadata, "_ts_metadata");
2642
- var BaseMessageInput = class {
2643
- id;
2644
- createdAt;
2645
- };
2646
- __name(BaseMessageInput, "BaseMessageInput");
2647
- _ts_decorate([
2648
- (0, import_type_graphql2.Field)(() => String),
2649
- _ts_metadata("design:type", String)
2650
- ], BaseMessageInput.prototype, "id", void 0);
2651
- _ts_decorate([
2652
- (0, import_type_graphql2.Field)(() => Date),
2653
- _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2654
- ], BaseMessageInput.prototype, "createdAt", void 0);
2655
- BaseMessageInput = _ts_decorate([
2656
- (0, import_type_graphql2.InputType)()
2657
- ], BaseMessageInput);
2658
-
2659
- // src/graphql/types/converted/index.ts
2660
- var Message = class extends BaseMessageInput {
2661
- type;
2662
- isTextMessage() {
2663
- return this.type === "TextMessage";
2664
- }
2665
- isActionExecutionMessage() {
2666
- return this.type === "ActionExecutionMessage";
2667
- }
2668
- isResultMessage() {
2669
- return this.type === "ResultMessage";
2670
- }
2671
- isAgentStateMessage() {
2672
- return this.type === "AgentStateMessage";
2673
- }
2674
- };
2675
- __name(Message, "Message");
2676
- var TextMessage = class extends Message {
2677
- type = "TextMessage";
2678
- content;
2679
- role;
2680
- };
2681
- __name(TextMessage, "TextMessage");
2682
- var ActionExecutionMessage = class extends Message {
2683
- type = "ActionExecutionMessage";
2684
- name;
2685
- arguments;
2686
- scope;
2687
- };
2688
- __name(ActionExecutionMessage, "ActionExecutionMessage");
2689
- var ResultMessage = class extends Message {
2690
- type = "ResultMessage";
2691
- actionExecutionId;
2692
- actionName;
2693
- result;
2694
- };
2695
- __name(ResultMessage, "ResultMessage");
2696
- var AgentStateMessage = class extends Message {
2697
- type = "AgentStateMessage";
2698
- threadId;
2699
- agentName;
2700
- nodeName;
2701
- runId;
2702
- active;
2703
- role;
2704
- state;
2705
- running;
2706
- };
2707
- __name(AgentStateMessage, "AgentStateMessage");
2708
-
2709
2947
  // src/service-adapters/conversion.ts
2710
- var import_class_transformer = require("class-transformer");
2948
+ var import_class_transformer2 = require("class-transformer");
2711
2949
  function convertGqlInputToMessages(inputMessages) {
2712
2950
  const messages = [];
2713
2951
  for (const message of inputMessages) {
2714
2952
  if (message.textMessage) {
2715
- messages.push((0, import_class_transformer.plainToInstance)(TextMessage, {
2953
+ messages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
2716
2954
  id: message.id,
2717
2955
  createdAt: message.createdAt,
2718
2956
  role: message.textMessage.role,
2719
- content: message.textMessage.content
2957
+ content: message.textMessage.content,
2958
+ parentMessageId: message.textMessage.parentMessageId
2720
2959
  }));
2721
2960
  } else if (message.actionExecutionMessage) {
2722
- messages.push((0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2961
+ messages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
2723
2962
  id: message.id,
2724
2963
  createdAt: message.createdAt,
2725
2964
  name: message.actionExecutionMessage.name,
2726
2965
  arguments: JSON.parse(message.actionExecutionMessage.arguments),
2727
- scope: message.actionExecutionMessage.scope
2966
+ parentMessageId: message.actionExecutionMessage.parentMessageId
2728
2967
  }));
2729
2968
  } else if (message.resultMessage) {
2730
- messages.push((0, import_class_transformer.plainToInstance)(ResultMessage, {
2969
+ messages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
2731
2970
  id: message.id,
2732
2971
  createdAt: message.createdAt,
2733
2972
  actionExecutionId: message.resultMessage.actionExecutionId,
@@ -2735,7 +2974,7 @@ function convertGqlInputToMessages(inputMessages) {
2735
2974
  result: message.resultMessage.result
2736
2975
  }));
2737
2976
  } else if (message.agentStateMessage) {
2738
- messages.push((0, import_class_transformer.plainToInstance)(AgentStateMessage, {
2977
+ messages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
2739
2978
  id: message.id,
2740
2979
  threadId: message.agentStateMessage.threadId,
2741
2980
  createdAt: message.createdAt,
@@ -3044,6 +3283,7 @@ MessageInput = _ts_decorate2([
3044
3283
  ], MessageInput);
3045
3284
  var TextMessageInput = class {
3046
3285
  content;
3286
+ parentMessageId;
3047
3287
  role;
3048
3288
  };
3049
3289
  __name(TextMessageInput, "TextMessageInput");
@@ -3051,6 +3291,12 @@ _ts_decorate2([
3051
3291
  (0, import_type_graphql3.Field)(() => String),
3052
3292
  _ts_metadata2("design:type", String)
3053
3293
  ], TextMessageInput.prototype, "content", void 0);
3294
+ _ts_decorate2([
3295
+ (0, import_type_graphql3.Field)(() => String, {
3296
+ nullable: true
3297
+ }),
3298
+ _ts_metadata2("design:type", String)
3299
+ ], TextMessageInput.prototype, "parentMessageId", void 0);
3054
3300
  _ts_decorate2([
3055
3301
  (0, import_type_graphql3.Field)(() => MessageRole),
3056
3302
  _ts_metadata2("design:type", typeof MessageRole === "undefined" ? Object : MessageRole)
@@ -3061,6 +3307,7 @@ TextMessageInput = _ts_decorate2([
3061
3307
  var ActionExecutionMessageInput = class {
3062
3308
  name;
3063
3309
  arguments;
3310
+ parentMessageId;
3064
3311
  scope;
3065
3312
  };
3066
3313
  __name(ActionExecutionMessageInput, "ActionExecutionMessageInput");
@@ -3073,8 +3320,17 @@ _ts_decorate2([
3073
3320
  _ts_metadata2("design:type", String)
3074
3321
  ], ActionExecutionMessageInput.prototype, "arguments", void 0);
3075
3322
  _ts_decorate2([
3076
- (0, import_type_graphql3.Field)(() => ActionExecutionScope),
3077
- _ts_metadata2("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
3323
+ (0, import_type_graphql3.Field)(() => String, {
3324
+ nullable: true
3325
+ }),
3326
+ _ts_metadata2("design:type", String)
3327
+ ], ActionExecutionMessageInput.prototype, "parentMessageId", void 0);
3328
+ _ts_decorate2([
3329
+ (0, import_type_graphql3.Field)(() => String, {
3330
+ nullable: true,
3331
+ deprecationReason: "This field will be removed in a future version"
3332
+ }),
3333
+ _ts_metadata2("design:type", typeof String === "undefined" ? Object : String)
3078
3334
  ], ActionExecutionMessageInput.prototype, "scope", void 0);
3079
3335
  ActionExecutionMessageInput = _ts_decorate2([
3080
3336
  (0, import_type_graphql3.InputType)()
@@ -3082,6 +3338,7 @@ ActionExecutionMessageInput = _ts_decorate2([
3082
3338
  var ResultMessageInput = class {
3083
3339
  actionExecutionId;
3084
3340
  actionName;
3341
+ parentMessageId;
3085
3342
  result;
3086
3343
  };
3087
3344
  __name(ResultMessageInput, "ResultMessageInput");
@@ -3093,6 +3350,12 @@ _ts_decorate2([
3093
3350
  (0, import_type_graphql3.Field)(() => String),
3094
3351
  _ts_metadata2("design:type", String)
3095
3352
  ], ResultMessageInput.prototype, "actionName", void 0);
3353
+ _ts_decorate2([
3354
+ (0, import_type_graphql3.Field)(() => String, {
3355
+ nullable: true
3356
+ }),
3357
+ _ts_metadata2("design:type", String)
3358
+ ], ResultMessageInput.prototype, "parentMessageId", void 0);
3096
3359
  _ts_decorate2([
3097
3360
  (0, import_type_graphql3.Field)(() => String),
3098
3361
  _ts_metadata2("design:type", String)
@@ -3815,6 +4078,7 @@ BaseMessageOutput = _ts_decorate13([
3815
4078
  var TextMessageOutput = class {
3816
4079
  role;
3817
4080
  content;
4081
+ parentMessageId;
3818
4082
  };
3819
4083
  __name(TextMessageOutput, "TextMessageOutput");
3820
4084
  _ts_decorate13([
@@ -3827,6 +4091,12 @@ _ts_decorate13([
3827
4091
  ]),
3828
4092
  _ts_metadata13("design:type", Array)
3829
4093
  ], TextMessageOutput.prototype, "content", void 0);
4094
+ _ts_decorate13([
4095
+ (0, import_type_graphql14.Field)(() => String, {
4096
+ nullable: true
4097
+ }),
4098
+ _ts_metadata13("design:type", String)
4099
+ ], TextMessageOutput.prototype, "parentMessageId", void 0);
3830
4100
  TextMessageOutput = _ts_decorate13([
3831
4101
  (0, import_type_graphql14.ObjectType)({
3832
4102
  implements: BaseMessageOutput
@@ -3836,6 +4106,7 @@ var ActionExecutionMessageOutput = class {
3836
4106
  name;
3837
4107
  scope;
3838
4108
  arguments;
4109
+ parentMessageId;
3839
4110
  };
3840
4111
  __name(ActionExecutionMessageOutput, "ActionExecutionMessageOutput");
3841
4112
  _ts_decorate13([
@@ -3843,8 +4114,11 @@ _ts_decorate13([
3843
4114
  _ts_metadata13("design:type", String)
3844
4115
  ], ActionExecutionMessageOutput.prototype, "name", void 0);
3845
4116
  _ts_decorate13([
3846
- (0, import_type_graphql14.Field)(() => ActionExecutionScope),
3847
- _ts_metadata13("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
4117
+ (0, import_type_graphql14.Field)(() => String, {
4118
+ nullable: true,
4119
+ deprecationReason: "This field will be removed in a future version"
4120
+ }),
4121
+ _ts_metadata13("design:type", String)
3848
4122
  ], ActionExecutionMessageOutput.prototype, "scope", void 0);
3849
4123
  _ts_decorate13([
3850
4124
  (0, import_type_graphql14.Field)(() => [
@@ -3852,6 +4126,12 @@ _ts_decorate13([
3852
4126
  ]),
3853
4127
  _ts_metadata13("design:type", Array)
3854
4128
  ], ActionExecutionMessageOutput.prototype, "arguments", void 0);
4129
+ _ts_decorate13([
4130
+ (0, import_type_graphql14.Field)(() => String, {
4131
+ nullable: true
4132
+ }),
4133
+ _ts_metadata13("design:type", String)
4134
+ ], ActionExecutionMessageOutput.prototype, "parentMessageId", void 0);
3855
4135
  ActionExecutionMessageOutput = _ts_decorate13([
3856
4136
  (0, import_type_graphql14.ObjectType)({
3857
4137
  implements: BaseMessageOutput
@@ -3962,7 +4242,7 @@ CopilotResponse = _ts_decorate13([
3962
4242
  // src/graphql/resolvers/copilot.resolver.ts
3963
4243
  var import_graphql_yoga = require("graphql-yoga");
3964
4244
  var import_graphql_scalars2 = require("graphql-scalars");
3965
- var import_class_transformer2 = require("class-transformer");
4245
+ var import_class_transformer3 = require("class-transformer");
3966
4246
  var import_graphql = require("graphql");
3967
4247
 
3968
4248
  // src/utils/failed-response-status-reasons.ts
@@ -4160,7 +4440,7 @@ var CopilotResolver = class {
4160
4440
  reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
4161
4441
  });
4162
4442
  outputMessages = [
4163
- (0, import_class_transformer2.plainToInstance)(TextMessage, {
4443
+ (0, import_class_transformer3.plainToInstance)(TextMessage, {
4164
4444
  id: (0, import_shared11.randomId)(),
4165
4445
  createdAt: /* @__PURE__ */ new Date(),
4166
4446
  content: result.reason,
@@ -4209,12 +4489,15 @@ var CopilotResolver = class {
4209
4489
  // skip until this message start event
4210
4490
  (0, import_rxjs4.skipWhile)((e) => e !== event),
4211
4491
  // take until the message end event
4212
- (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
4492
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.TextMessageEnd && e.messageId == event.messageId)),
4493
+ // filter out any other message events or message ids
4494
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.TextMessageContent && e.messageId == event.messageId)
4213
4495
  );
4214
4496
  const streamingTextStatus = new import_rxjs4.Subject();
4215
4497
  const messageId = event.messageId;
4216
4498
  pushMessage({
4217
4499
  id: messageId,
4500
+ parentMessageId: event.parentMessageId,
4218
4501
  status: (0, import_rxjs4.firstValueFrom)(streamingTextStatus),
4219
4502
  createdAt: /* @__PURE__ */ new Date(),
4220
4503
  role: MessageRole.assistant,
@@ -4227,7 +4510,7 @@ var CopilotResolver = class {
4227
4510
  reason,
4228
4511
  messageId: messageId2
4229
4512
  }, "Text streaming interrupted");
4230
- streamingTextStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4513
+ streamingTextStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
4231
4514
  reason
4232
4515
  }));
4233
4516
  responseStatus$.next(new MessageStreamInterruptedResponse({
@@ -4260,7 +4543,7 @@ var CopilotResolver = class {
4260
4543
  streamingTextStatus.next(new SuccessMessageStatus());
4261
4544
  stopStreamingText();
4262
4545
  textSubscription == null ? void 0 : textSubscription.unsubscribe();
4263
- outputMessages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
4546
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(TextMessage, {
4264
4547
  id: messageId,
4265
4548
  createdAt: /* @__PURE__ */ new Date(),
4266
4549
  content: textChunks.join(""),
@@ -4273,14 +4556,20 @@ var CopilotResolver = class {
4273
4556
  break;
4274
4557
  case RuntimeEventTypes.ActionExecutionStart:
4275
4558
  logger2.debug("Action execution start event received");
4276
- const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs4.skipWhile)((e) => e !== event), (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
4559
+ const actionExecutionArgumentStream = eventStream.pipe(
4560
+ (0, import_rxjs4.skipWhile)((e) => e !== event),
4561
+ // take until the action execution end event
4562
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.ActionExecutionEnd && e.actionExecutionId == event.actionExecutionId)),
4563
+ // filter out any other action execution events or action execution ids
4564
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.ActionExecutionArgs && e.actionExecutionId == event.actionExecutionId)
4565
+ );
4277
4566
  const streamingArgumentsStatus = new import_rxjs4.Subject();
4278
4567
  pushMessage({
4279
4568
  id: event.actionExecutionId,
4569
+ parentMessageId: event.parentMessageId,
4280
4570
  status: (0, import_rxjs4.firstValueFrom)(streamingArgumentsStatus),
4281
4571
  createdAt: /* @__PURE__ */ new Date(),
4282
4572
  name: event.actionName,
4283
- scope: event.scope,
4284
4573
  arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
4285
4574
  logger2.debug("Action execution argument stream created");
4286
4575
  const argumentChunks = [];
@@ -4296,7 +4585,7 @@ var CopilotResolver = class {
4296
4585
  logger2.error({
4297
4586
  err
4298
4587
  }, "Error in action execution argument stream");
4299
- streamingArgumentsStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4588
+ streamingArgumentsStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
4300
4589
  reason: "An unknown error has occurred in the action execution argument stream"
4301
4590
  }));
4302
4591
  stopStreamingArguments();
@@ -4307,11 +4596,10 @@ var CopilotResolver = class {
4307
4596
  streamingArgumentsStatus.next(new SuccessMessageStatus());
4308
4597
  stopStreamingArguments();
4309
4598
  actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
4310
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
4599
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ActionExecutionMessage, {
4311
4600
  id: event.actionExecutionId,
4312
4601
  createdAt: /* @__PURE__ */ new Date(),
4313
4602
  name: event.actionName,
4314
- scope: event.scope,
4315
4603
  arguments: argumentChunks.join("")
4316
4604
  }));
4317
4605
  }
@@ -4324,15 +4612,15 @@ var CopilotResolver = class {
4324
4612
  result: event.result
4325
4613
  }, "Action execution result event received");
4326
4614
  pushMessage({
4327
- id: (0, import_shared11.randomId)(),
4615
+ id: "result-" + event.actionExecutionId,
4328
4616
  status: new SuccessMessageStatus(),
4329
4617
  createdAt: /* @__PURE__ */ new Date(),
4330
4618
  actionExecutionId: event.actionExecutionId,
4331
4619
  actionName: event.actionName,
4332
4620
  result: event.result
4333
4621
  });
4334
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
4335
- id: (0, import_shared11.randomId)(),
4622
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ResultMessage, {
4623
+ id: "result-" + event.actionExecutionId,
4336
4624
  createdAt: /* @__PURE__ */ new Date(),
4337
4625
  actionExecutionId: event.actionExecutionId,
4338
4626
  actionName: event.actionName,
@@ -4356,7 +4644,7 @@ var CopilotResolver = class {
4356
4644
  role: MessageRole.assistant,
4357
4645
  createdAt: /* @__PURE__ */ new Date()
4358
4646
  });
4359
- outputMessages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
4647
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(AgentStateMessage, {
4360
4648
  id: (0, import_shared11.randomId)(),
4361
4649
  threadId: event.threadId,
4362
4650
  agentName: event.agentName,