@copilotkit/runtime 1.4.8 → 1.5.0-coagents-v0-3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/__snapshots__/schema/schema.graphql +7 -9
  3. package/dist/{chunk-W7GP2EOT.mjs → chunk-4AYRDPWK.mjs} +239 -65
  4. package/dist/chunk-4AYRDPWK.mjs.map +1 -0
  5. package/dist/{chunk-LSLFIYKP.mjs → chunk-7BOO3OFP.mjs} +2 -2
  6. package/dist/{chunk-GGNRDS7N.mjs → chunk-GSTWFOZF.mjs} +2 -2
  7. package/dist/{chunk-TA5I5LYF.mjs → chunk-OMAAKIJQ.mjs} +2 -2
  8. package/dist/{chunk-R6AUAQK6.mjs → chunk-PXS4SBRF.mjs} +423 -299
  9. package/dist/chunk-PXS4SBRF.mjs.map +1 -0
  10. package/dist/{chunk-B74M7FXG.mjs → chunk-RFF5IIZJ.mjs} +3 -2
  11. package/dist/chunk-RFF5IIZJ.mjs.map +1 -0
  12. package/dist/{copilot-runtime-48d7c2fc.d.ts → copilot-runtime-2e46a7b6.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-ac43305a.d.ts → groq-adapter-7bf6824b.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 +747 -448
  21. package/dist/index.js.map +1 -1
  22. package/dist/index.mjs +6 -6
  23. package/dist/{langserve-b74e8606.d.ts → langserve-f318db89.d.ts} +51 -12
  24. package/dist/lib/index.d.ts +4 -4
  25. package/dist/lib/index.js +714 -439
  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 +238 -64
  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 +159 -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 +116 -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 +55 -32
  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-B74M7FXG.mjs.map +0 -1
  69. package/dist/chunk-R6AUAQK6.mjs.map +0 -1
  70. package/dist/chunk-W7GP2EOT.mjs.map +0 -1
  71. /package/dist/{chunk-LSLFIYKP.mjs.map → chunk-7BOO3OFP.mjs.map} +0 -0
  72. /package/dist/{chunk-GGNRDS7N.mjs.map → chunk-GSTWFOZF.mjs.map} +0 -0
  73. /package/dist/{chunk-TA5I5LYF.mjs.map → chunk-OMAAKIJQ.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.8",
47
+ version: "1.5.0-coagents-v0-3.0",
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,
@@ -570,9 +611,12 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
570
611
  let toolCallId = void 0;
571
612
  let toolCallArgs = void 0;
572
613
  let hasToolCall = false;
573
- let content = value == null ? void 0 : value.content;
614
+ let content = "";
615
+ if (value && value.content) {
616
+ content = Array.isArray(value.content) ? ((_d = value.content[0]) == null ? void 0 : _d.text) ?? "" : value.content;
617
+ }
574
618
  if (isAIMessageChunk(value)) {
575
- let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
619
+ let chunk = (_e = value.tool_call_chunks) == null ? void 0 : _e[0];
576
620
  toolCallArgs = chunk == null ? void 0 : chunk.args;
577
621
  hasToolCall = chunk != void 0;
578
622
  if (chunk == null ? void 0 : chunk.name)
@@ -587,18 +631,22 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
587
631
  toolCallName = toolCallDetails.name;
588
632
  toolCallId = toolCallDetails.id;
589
633
  } else if (isBaseMessageChunk(value)) {
590
- let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
591
- toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
634
+ let chunk = (_g = (_f = value.additional_kwargs) == null ? void 0 : _f.tool_calls) == null ? void 0 : _g[0];
635
+ toolCallName = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.name;
592
636
  toolCallId = chunk == null ? void 0 : chunk.id;
593
- toolCallArgs = (_h = chunk == null ? void 0 : chunk.function) == null ? void 0 : _h.arguments;
637
+ toolCallArgs = (_i = chunk == null ? void 0 : chunk.function) == null ? void 0 : _i.arguments;
594
638
  hasToolCall = (chunk == null ? void 0 : chunk.function) != void 0;
595
639
  }
596
640
  if (mode === "message" && (toolCallId || done)) {
597
641
  mode = null;
598
- eventStream$.sendTextMessageEnd();
642
+ eventStream$.sendTextMessageEnd({
643
+ messageId: currentMessageId
644
+ });
599
645
  } else if (mode === "function" && (!hasToolCall || done)) {
600
646
  mode = null;
601
- eventStream$.sendActionExecutionEnd();
647
+ eventStream$.sendActionExecutionEnd({
648
+ actionExecutionId: toolCallId
649
+ });
602
650
  }
603
651
  if (done) {
604
652
  break;
@@ -606,21 +654,40 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
606
654
  if (mode === null) {
607
655
  if (hasToolCall && toolCallId && toolCallName) {
608
656
  mode = "function";
609
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
657
+ eventStream$.sendActionExecutionStart({
658
+ actionExecutionId: toolCallId,
659
+ actionName: toolCallName,
660
+ parentMessageId: (_j = value.lc_kwargs) == null ? void 0 : _j.id
661
+ });
610
662
  } else if (content) {
611
663
  mode = "message";
612
- eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
664
+ currentMessageId = ((_k = value.lc_kwargs) == null ? void 0 : _k.id) || (0, import_shared2.randomId)();
665
+ eventStream$.sendTextMessageStart({
666
+ messageId: currentMessageId
667
+ });
613
668
  }
614
669
  }
615
670
  if (mode === "message" && content) {
616
- eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
671
+ eventStream$.sendTextMessageContent({
672
+ messageId: currentMessageId,
673
+ content
674
+ });
617
675
  } else if (mode === "function" && toolCallArgs) {
618
676
  if (toolCallDetails.index !== toolCallDetails.prevIndex) {
619
- eventStream$.sendActionExecutionEnd();
620
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
677
+ eventStream$.sendActionExecutionEnd({
678
+ actionExecutionId: toolCallId
679
+ });
680
+ eventStream$.sendActionExecutionStart({
681
+ actionExecutionId: toolCallId,
682
+ actionName: toolCallName,
683
+ parentMessageId: (_l = value.lc_kwargs) == null ? void 0 : _l.id
684
+ });
621
685
  toolCallDetails.prevIndex = toolCallDetails.index;
622
686
  }
623
- eventStream$.sendActionExecutionArgs(toolCallArgs);
687
+ eventStream$.sendActionExecutionArgs({
688
+ actionExecutionId: toolCallId,
689
+ args: toolCallArgs
690
+ });
624
691
  }
625
692
  } catch (error) {
626
693
  console.error("Error reading from stream", error);
@@ -628,7 +695,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
628
695
  }
629
696
  }
630
697
  } else if (actionExecution) {
631
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
698
+ eventStream$.sendActionExecutionResult({
699
+ actionExecutionId: actionExecution.id,
700
+ actionName: actionExecution.name,
701
+ result: encodeResult(result)
702
+ });
632
703
  } else {
633
704
  throw new Error("Invalid return type from LangChain function.");
634
705
  }
@@ -808,21 +879,33 @@ var OpenAIAssistantAdapter = class {
808
879
  eventSource.stream(async (eventStream$) => {
809
880
  var _a, _b, _c, _d, _e, _f;
810
881
  let inFunctionCall = false;
882
+ let currentMessageId;
883
+ let currentToolCallId;
811
884
  for await (const chunk of stream) {
812
885
  switch (chunk.event) {
813
886
  case "thread.message.created":
814
887
  if (inFunctionCall) {
815
- eventStream$.sendActionExecutionEnd();
888
+ eventStream$.sendActionExecutionEnd({
889
+ actionExecutionId: currentToolCallId
890
+ });
816
891
  }
817
- eventStream$.sendTextMessageStart(chunk.data.id);
892
+ currentMessageId = chunk.data.id;
893
+ eventStream$.sendTextMessageStart({
894
+ messageId: currentMessageId
895
+ });
818
896
  break;
819
897
  case "thread.message.delta":
820
898
  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);
899
+ eventStream$.sendTextMessageContent({
900
+ messageId: currentMessageId,
901
+ content: (_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value
902
+ });
822
903
  }
823
904
  break;
824
905
  case "thread.message.completed":
825
- eventStream$.sendTextMessageEnd();
906
+ eventStream$.sendTextMessageEnd({
907
+ messageId: currentMessageId
908
+ });
826
909
  break;
827
910
  case "thread.run.step.delta":
828
911
  let toolCallId;
@@ -835,18 +918,30 @@ var OpenAIAssistantAdapter = class {
835
918
  }
836
919
  if (toolCallName && toolCallId) {
837
920
  if (inFunctionCall) {
838
- eventStream$.sendActionExecutionEnd();
921
+ eventStream$.sendActionExecutionEnd({
922
+ actionExecutionId: currentToolCallId
923
+ });
839
924
  }
840
925
  inFunctionCall = true;
841
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
926
+ currentToolCallId = toolCallId;
927
+ eventStream$.sendActionExecutionStart({
928
+ actionExecutionId: currentToolCallId,
929
+ parentMessageId: chunk.data.id,
930
+ actionName: toolCallName
931
+ });
842
932
  } else if (toolCallArgs) {
843
- eventStream$.sendActionExecutionArgs(toolCallArgs);
933
+ eventStream$.sendActionExecutionArgs({
934
+ actionExecutionId: currentToolCallId,
935
+ args: toolCallArgs
936
+ });
844
937
  }
845
938
  break;
846
939
  }
847
940
  }
848
941
  if (inFunctionCall) {
849
- eventStream$.sendActionExecutionEnd();
942
+ eventStream$.sendActionExecutionEnd({
943
+ actionExecutionId: currentToolCallId
944
+ });
850
945
  }
851
946
  eventStream$.complete();
852
947
  });
@@ -899,46 +994,77 @@ var UnifyAdapter = class {
899
994
  }
900
995
  });
901
996
  let model = null;
997
+ let currentMessageId;
998
+ let currentToolCallId;
902
999
  request.eventSource.stream(async (eventStream$) => {
903
1000
  var _a, _b;
904
1001
  let mode = null;
905
1002
  for await (const chunk of stream) {
906
1003
  if (this.start) {
907
1004
  model = chunk.model;
908
- eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
909
- eventStream$.sendTextMessageContent(`Model used: ${model}
910
- `);
911
- eventStream$.sendTextMessageEnd();
1005
+ currentMessageId = (0, import_shared4.randomId)();
1006
+ eventStream$.sendTextMessageStart({
1007
+ messageId: currentMessageId
1008
+ });
1009
+ eventStream$.sendTextMessageContent({
1010
+ messageId: currentMessageId,
1011
+ content: `Model used: ${model}
1012
+ `
1013
+ });
1014
+ eventStream$.sendTextMessageEnd({
1015
+ messageId: currentMessageId
1016
+ });
912
1017
  this.start = false;
913
1018
  }
914
1019
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
915
1020
  const content = chunk.choices[0].delta.content;
916
1021
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
917
1022
  mode = null;
918
- eventStream$.sendTextMessageEnd();
1023
+ eventStream$.sendTextMessageEnd({
1024
+ messageId: currentMessageId
1025
+ });
919
1026
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
920
1027
  mode = null;
921
- eventStream$.sendActionExecutionEnd();
1028
+ eventStream$.sendActionExecutionEnd({
1029
+ actionExecutionId: currentToolCallId
1030
+ });
922
1031
  }
923
1032
  if (mode === null) {
924
1033
  if (toolCall == null ? void 0 : toolCall.id) {
925
1034
  mode = "function";
926
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1035
+ currentToolCallId = toolCall.id;
1036
+ eventStream$.sendActionExecutionStart({
1037
+ actionExecutionId: currentToolCallId,
1038
+ actionName: toolCall.function.name
1039
+ });
927
1040
  } else if (content) {
928
1041
  mode = "message";
929
- eventStream$.sendTextMessageStart(chunk.id);
1042
+ currentMessageId = chunk.id;
1043
+ eventStream$.sendTextMessageStart({
1044
+ messageId: currentMessageId
1045
+ });
930
1046
  }
931
1047
  }
932
1048
  if (mode === "message" && content) {
933
- eventStream$.sendTextMessageContent(content);
1049
+ eventStream$.sendTextMessageContent({
1050
+ messageId: currentMessageId,
1051
+ content
1052
+ });
934
1053
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
935
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1054
+ eventStream$.sendActionExecutionArgs({
1055
+ actionExecutionId: currentToolCallId,
1056
+ args: toolCall.function.arguments
1057
+ });
936
1058
  }
937
1059
  }
938
1060
  if (mode === "message") {
939
- eventStream$.sendTextMessageEnd();
1061
+ eventStream$.sendTextMessageEnd({
1062
+ messageId: currentMessageId
1063
+ });
940
1064
  } else if (mode === "function") {
941
- eventStream$.sendActionExecutionEnd();
1065
+ eventStream$.sendActionExecutionEnd({
1066
+ actionExecutionId: currentToolCallId
1067
+ });
942
1068
  }
943
1069
  eventStream$.complete();
944
1070
  });
@@ -1004,35 +1130,59 @@ var GroqAdapter = class {
1004
1130
  eventSource.stream(async (eventStream$) => {
1005
1131
  var _a, _b;
1006
1132
  let mode = null;
1133
+ let currentMessageId;
1134
+ let currentToolCallId;
1007
1135
  for await (const chunk of stream) {
1008
1136
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
1009
1137
  const content = chunk.choices[0].delta.content;
1010
1138
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
1011
1139
  mode = null;
1012
- eventStream$.sendTextMessageEnd();
1140
+ eventStream$.sendTextMessageEnd({
1141
+ messageId: currentMessageId
1142
+ });
1013
1143
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
1014
1144
  mode = null;
1015
- eventStream$.sendActionExecutionEnd();
1145
+ eventStream$.sendActionExecutionEnd({
1146
+ actionExecutionId: currentToolCallId
1147
+ });
1016
1148
  }
1017
1149
  if (mode === null) {
1018
1150
  if (toolCall == null ? void 0 : toolCall.id) {
1019
1151
  mode = "function";
1020
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1152
+ currentToolCallId = toolCall.id;
1153
+ eventStream$.sendActionExecutionStart({
1154
+ actionExecutionId: currentToolCallId,
1155
+ actionName: toolCall.function.name,
1156
+ parentMessageId: chunk.id
1157
+ });
1021
1158
  } else if (content) {
1022
1159
  mode = "message";
1023
- eventStream$.sendTextMessageStart(chunk.id);
1160
+ currentMessageId = chunk.id;
1161
+ eventStream$.sendTextMessageStart({
1162
+ messageId: currentMessageId
1163
+ });
1024
1164
  }
1025
1165
  }
1026
1166
  if (mode === "message" && content) {
1027
- eventStream$.sendTextMessageContent(content);
1167
+ eventStream$.sendTextMessageContent({
1168
+ messageId: currentMessageId,
1169
+ content
1170
+ });
1028
1171
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
1029
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1172
+ eventStream$.sendActionExecutionArgs({
1173
+ actionExecutionId: currentToolCallId,
1174
+ args: toolCall.function.arguments
1175
+ });
1030
1176
  }
1031
1177
  }
1032
1178
  if (mode === "message") {
1033
- eventStream$.sendTextMessageEnd();
1179
+ eventStream$.sendTextMessageEnd({
1180
+ messageId: currentMessageId
1181
+ });
1034
1182
  } else if (mode === "function") {
1035
- eventStream$.sendActionExecutionEnd();
1183
+ eventStream$.sendActionExecutionEnd({
1184
+ actionExecutionId: currentToolCallId
1185
+ });
1036
1186
  }
1037
1187
  eventStream$.complete();
1038
1188
  });
@@ -1093,163 +1243,167 @@ var RemoteLangGraphEventSource = class {
1093
1243
  }
1094
1244
  return shouldEmitToolCalls === toolCallName;
1095
1245
  }
1246
+ getCurrentContent(event) {
1247
+ var _a, _b, _c, _d, _e;
1248
+ 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);
1249
+ if (!content) {
1250
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1251
+ for (const chunk of toolCallChunks) {
1252
+ if (chunk.args) {
1253
+ return chunk.args;
1254
+ }
1255
+ }
1256
+ }
1257
+ if (typeof content === "string") {
1258
+ return content;
1259
+ } else if (Array.isArray(content) && content.length > 0) {
1260
+ return content[0].text;
1261
+ }
1262
+ return null;
1263
+ }
1264
+ getCurrentMessageId(event) {
1265
+ var _a, _b, _c, _d, _e;
1266
+ 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);
1267
+ }
1268
+ getCurrentToolCallChunks(event) {
1269
+ var _a, _b, _c, _d, _e;
1270
+ 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);
1271
+ }
1272
+ getResponseMetadata(event) {
1273
+ var _a, _b, _c, _d, _e;
1274
+ 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);
1275
+ }
1096
1276
  processLangGraphEvents() {
1097
1277
  let lastEventWithState = null;
1098
1278
  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
1279
  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;
1280
+ const prevMessageId = acc.lastMessageId;
1281
+ acc.currentContent = this.getCurrentContent(event);
1282
+ acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
1283
+ const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
1284
+ const responseMetadata = this.getResponseMetadata(event);
1285
+ acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
1286
+ acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
1287
+ acc.isToolCall = toolCallChunks && toolCallChunks.length > 0;
1288
+ acc.isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
1289
+ acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
1290
+ ({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
1291
+ name: acc.lastToolCallName,
1292
+ id: acc.lastToolCallId
1293
+ });
1138
1294
  }
1139
1295
  acc.event = event;
1140
1296
  lastEventWithState = acc;
1141
1297
  return acc;
1142
1298
  }, {
1143
1299
  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;
1300
+ isMessageStart: false,
1301
+ isMessageEnd: false,
1302
+ isToolCallStart: false,
1303
+ isToolCallEnd: false,
1304
+ isToolCall: false,
1305
+ lastMessageId: null,
1306
+ lastToolCallId: null,
1307
+ lastToolCallName: null,
1308
+ currentContent: null
1309
+ }), (0, import_rxjs.mergeMap)((acc) => {
1153
1310
  const events = [];
1154
1311
  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"];
1312
+ let shouldEmitToolCalls = true;
1313
+ if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
1314
+ if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
1315
+ shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
1159
1316
  }
1160
- if ("copilotkit:emit-messages" in (eventWithState.event.metadata || {})) {
1161
- shouldEmitMessages = eventWithState.event.metadata["copilotkit:emit-messages"];
1317
+ if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
1318
+ shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
1162
1319
  }
1163
1320
  }
1164
- if (eventWithState.prevToolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId && this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1321
+ const responseMetadata = this.getResponseMetadata(acc.event);
1322
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls" && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1165
1323
  events.push({
1166
- type: RuntimeEventTypes.ActionExecutionEnd
1324
+ type: RuntimeEventTypes.ActionExecutionEnd,
1325
+ actionExecutionId: acc.lastToolCallId
1167
1326
  });
1168
1327
  }
1169
- if (eventWithState.prevMessageId !== null && eventWithState.prevMessageId !== eventWithState.messageId && shouldEmitMessages) {
1328
+ if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
1170
1329
  events.push({
1171
- type: RuntimeEventTypes.TextMessageEnd
1330
+ type: RuntimeEventTypes.TextMessageEnd,
1331
+ messageId: acc.lastMessageId
1172
1332
  });
1173
1333
  }
1174
- switch (eventWithState.event.event) {
1334
+ switch (acc.event.event) {
1175
1335
  case LangGraphEventTypes.OnCustomEvent:
1176
- if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1336
+ if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
1177
1337
  events.push({
1178
1338
  type: RuntimeEventTypes.TextMessageStart,
1179
- messageId: eventWithState.event.data.message_id
1339
+ messageId: acc.event.data.message_id
1180
1340
  });
1181
1341
  events.push({
1182
1342
  type: RuntimeEventTypes.TextMessageContent,
1183
- content: eventWithState.event.data.message
1343
+ messageId: acc.event.data.message_id,
1344
+ content: acc.event.data.message
1184
1345
  });
1185
1346
  events.push({
1186
- type: RuntimeEventTypes.TextMessageEnd
1347
+ type: RuntimeEventTypes.TextMessageEnd,
1348
+ messageId: acc.event.data.message_id
1187
1349
  });
1188
- } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1350
+ } else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1189
1351
  events.push({
1190
1352
  type: RuntimeEventTypes.ActionExecutionStart,
1191
- actionExecutionId: eventWithState.event.data.id,
1192
- actionName: eventWithState.event.data.name
1353
+ actionExecutionId: acc.event.data.id,
1354
+ actionName: acc.event.data.name,
1355
+ parentMessageId: acc.event.data.id
1193
1356
  });
1194
1357
  events.push({
1195
1358
  type: RuntimeEventTypes.ActionExecutionArgs,
1196
- args: JSON.stringify(eventWithState.event.data.args)
1359
+ actionExecutionId: acc.event.data.id,
1360
+ args: JSON.stringify(acc.event.data.args)
1197
1361
  });
1198
1362
  events.push({
1199
- type: RuntimeEventTypes.ActionExecutionEnd
1363
+ type: RuntimeEventTypes.ActionExecutionEnd,
1364
+ actionExecutionId: acc.event.data.id
1200
1365
  });
1201
1366
  }
1202
1367
  break;
1203
1368
  case LangGraphEventTypes.OnCopilotKitStateSync:
1204
1369
  events.push({
1205
1370
  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
1371
+ threadId: acc.event.thread_id,
1372
+ role: acc.event.role,
1373
+ agentName: acc.event.agent_name,
1374
+ nodeName: acc.event.node_name,
1375
+ runId: acc.event.run_id,
1376
+ active: acc.event.active,
1377
+ state: JSON.stringify(acc.event.state),
1378
+ running: acc.event.running
1214
1379
  });
1215
1380
  break;
1216
- case LangGraphEventTypes.OnToolEnd:
1217
- break;
1218
1381
  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
- }
1382
+ if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1383
+ events.push({
1384
+ type: RuntimeEventTypes.ActionExecutionStart,
1385
+ actionExecutionId: acc.lastToolCallId,
1386
+ actionName: acc.lastToolCallName,
1387
+ parentMessageId: acc.lastMessageId
1388
+ });
1389
+ } else if (acc.isMessageStart && shouldEmitMessages) {
1390
+ events.push({
1391
+ type: RuntimeEventTypes.TextMessageStart,
1392
+ messageId: acc.lastMessageId
1393
+ });
1235
1394
  }
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
- }
1395
+ if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
1396
+ events.push({
1397
+ type: RuntimeEventTypes.ActionExecutionArgs,
1398
+ actionExecutionId: acc.lastToolCallId,
1399
+ args: acc.currentContent
1400
+ });
1401
+ } else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
1402
+ events.push({
1403
+ type: RuntimeEventTypes.TextMessageContent,
1404
+ messageId: acc.lastMessageId,
1405
+ content: acc.currentContent
1406
+ });
1253
1407
  }
1254
1408
  break;
1255
1409
  }
@@ -1257,14 +1411,16 @@ var RemoteLangGraphEventSource = class {
1257
1411
  }), (0, import_rxjs.catchError)((error) => {
1258
1412
  console.error(error);
1259
1413
  const events = [];
1260
- if (lastEventWithState == null ? void 0 : lastEventWithState.messageId) {
1414
+ if ((lastEventWithState == null ? void 0 : lastEventWithState.lastMessageId) && !lastEventWithState.isToolCall) {
1261
1415
  events.push({
1262
- type: RuntimeEventTypes.TextMessageEnd
1416
+ type: RuntimeEventTypes.TextMessageEnd,
1417
+ messageId: lastEventWithState.lastMessageId
1263
1418
  });
1264
1419
  }
1265
- if (lastEventWithState == null ? void 0 : lastEventWithState.toolCallMessageId) {
1420
+ if (lastEventWithState == null ? void 0 : lastEventWithState.lastToolCallId) {
1266
1421
  events.push({
1267
- type: RuntimeEventTypes.ActionExecutionEnd
1422
+ type: RuntimeEventTypes.ActionExecutionEnd,
1423
+ actionExecutionId: lastEventWithState.lastToolCallId
1268
1424
  });
1269
1425
  }
1270
1426
  const messageId = (0, import_shared6.randomId)();
@@ -1274,10 +1430,12 @@ var RemoteLangGraphEventSource = class {
1274
1430
  });
1275
1431
  events.push({
1276
1432
  type: RuntimeEventTypes.TextMessageContent,
1433
+ messageId,
1277
1434
  content: "\u274C An error occurred. Please try again."
1278
1435
  });
1279
1436
  events.push({
1280
- type: RuntimeEventTypes.TextMessageEnd
1437
+ type: RuntimeEventTypes.TextMessageEnd,
1438
+ messageId
1281
1439
  });
1282
1440
  return events;
1283
1441
  }));
@@ -1299,12 +1457,6 @@ var MessageRole;
1299
1457
  MessageRole2["system"] = "system";
1300
1458
  MessageRole2["tool"] = "tool";
1301
1459
  })(MessageRole || (MessageRole = {}));
1302
- var ActionExecutionScope;
1303
- (function(ActionExecutionScope2) {
1304
- ActionExecutionScope2["server"] = "server";
1305
- ActionExecutionScope2["client"] = "client";
1306
- ActionExecutionScope2["passThrough"] = "passThrough";
1307
- })(ActionExecutionScope || (ActionExecutionScope = {}));
1308
1460
  var CopilotRequestType;
1309
1461
  (function(CopilotRequestType2) {
1310
1462
  CopilotRequestType2["Chat"] = "Chat";
@@ -1323,10 +1475,6 @@ var ActionInputAvailability;
1323
1475
  name: "MessageRole",
1324
1476
  description: "The role of the message"
1325
1477
  });
1326
- (0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
1327
- name: "ActionExecutionScope",
1328
- description: "The scope of the action"
1329
- });
1330
1478
  (0, import_type_graphql.registerEnumType)(CopilotRequestType, {
1331
1479
  name: "CopilotRequestType",
1332
1480
  description: "The type of Copilot request"
@@ -1383,7 +1531,7 @@ async function streamEvents(controller, args) {
1383
1531
  const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
1384
1532
  let formattedMessages = [];
1385
1533
  try {
1386
- formattedMessages = formatMessages(messages);
1534
+ formattedMessages = copilotkitMessagesToLangChain(messages);
1387
1535
  } catch (e) {
1388
1536
  logger2.error(e, `Error event thrown: ${e.message}`);
1389
1537
  }
@@ -1536,7 +1684,8 @@ async function streamEvents(controller, args) {
1536
1684
  nodeName: isEndNode ? "__end__" : nodeName,
1537
1685
  state: state.values,
1538
1686
  running: !shouldExit,
1539
- active: false
1687
+ active: false,
1688
+ includeMessages: true
1540
1689
  }));
1541
1690
  return Promise.resolve();
1542
1691
  } catch (e) {
@@ -1549,13 +1698,20 @@ async function streamEvents(controller, args) {
1549
1698
  }
1550
1699
  }
1551
1700
  __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
- }, {});
1701
+ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active, includeMessages = false }) {
1702
+ if (!includeMessages) {
1703
+ state = Object.keys(state).reduce((acc, key) => {
1704
+ if (key !== "messages") {
1705
+ acc[key] = state[key];
1706
+ }
1707
+ return acc;
1708
+ }, {});
1709
+ } else {
1710
+ state = {
1711
+ ...state,
1712
+ messages: langchainMessagesToCopilotKit(state.messages || [])
1713
+ };
1714
+ }
1559
1715
  return JSON.stringify({
1560
1716
  event: LangGraphEventTypes.OnCopilotKitStateSync,
1561
1717
  thread_id: threadId,
@@ -1563,7 +1719,7 @@ function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, runnin
1563
1719
  agent_name: agentName,
1564
1720
  node_name: nodeName,
1565
1721
  active,
1566
- state: stateWithoutMessages,
1722
+ state,
1567
1723
  running,
1568
1724
  role: "assistant"
1569
1725
  }) + "\n";
@@ -1639,134 +1795,157 @@ function langGraphDefaultMergeState(state, messages, actions, agentName) {
1639
1795
  if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
1640
1796
  messages = messages.slice(1);
1641
1797
  }
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);
1798
+ const existingMessages = state.messages || [];
1799
+ const existingMessageIds = new Set(existingMessages.map((message) => message.id));
1800
+ const newMessages = messages.filter((message) => !existingMessageIds.has(message.id));
1801
+ return {
1802
+ ...state,
1803
+ messages: newMessages,
1804
+ copilotkit: {
1805
+ actions
1806
+ }
1807
+ };
1808
+ }
1809
+ __name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
1810
+ function langchainMessagesToCopilotKit(messages) {
1811
+ const result = [];
1812
+ const tool_call_names = {};
1813
+ for (const message of messages) {
1814
+ if (message.type === "ai") {
1815
+ for (const tool_call of message.tool_calls) {
1816
+ tool_call_names[tool_call.id] = tool_call.name;
1817
+ }
1648
1818
  }
1649
1819
  }
1650
1820
  for (const message of messages) {
1651
- if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
1652
- continue;
1821
+ let content = message.content;
1822
+ if (content instanceof Array) {
1823
+ content = content[0];
1653
1824
  }
1654
- if ("name" in message && message.name === agentName) {
1655
- continue;
1825
+ if (content instanceof Object) {
1826
+ content = content.text;
1656
1827
  }
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;
1828
+ if (message.type === "human") {
1829
+ result.push({
1830
+ role: "user",
1831
+ content,
1832
+ id: message.id
1833
+ });
1834
+ } else if (message.type === "system") {
1835
+ result.push({
1836
+ role: "system",
1837
+ content,
1838
+ id: message.id
1839
+ });
1840
+ } else if (message.type === "ai") {
1841
+ if (message.tool_calls && message.tool_calls.length > 0) {
1842
+ for (const tool_call of message.tool_calls) {
1843
+ result.push({
1844
+ id: tool_call.id,
1845
+ name: tool_call.name,
1846
+ arguments: tool_call.args,
1847
+ parentMessageId: message.id
1848
+ });
1671
1849
  }
1850
+ } else {
1851
+ result.push({
1852
+ role: "assistant",
1853
+ content,
1854
+ id: message.id,
1855
+ parentMessageId: message.id
1856
+ });
1672
1857
  }
1858
+ } else if (message.type === "tool") {
1859
+ const actionName = tool_call_names[message.tool_call_id] || message.name || "";
1860
+ result.push({
1861
+ actionExecutionId: message.tool_call_id,
1862
+ actionName,
1863
+ result: content,
1864
+ id: message.id
1865
+ });
1673
1866
  }
1674
1867
  }
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;
1868
+ const resultsDict = {};
1869
+ for (const msg of result) {
1870
+ if (msg.actionExecutionId) {
1871
+ resultsDict[msg.actionExecutionId] = msg;
1680
1872
  }
1681
1873
  }
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;
1874
+ const reorderedResult = [];
1875
+ for (const msg of result) {
1876
+ if (!("actionExecutionId" in msg)) {
1877
+ reorderedResult.push(msg);
1705
1878
  }
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;
1879
+ if ("arguments" in msg) {
1880
+ const msgId = msg.id;
1881
+ if (msgId in resultsDict) {
1882
+ reorderedResult.push(resultsDict[msgId]);
1714
1883
  }
1715
- correctedMessages.push(currentMessage);
1716
- continue;
1717
1884
  }
1718
- correctedMessages.push(currentMessage);
1719
1885
  }
1720
- return {
1721
- ...state,
1722
- messages: correctedMessages,
1723
- copilotkit: {
1724
- actions
1725
- }
1726
- };
1886
+ return reorderedResult;
1727
1887
  }
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;
1888
+ __name(langchainMessagesToCopilotKit, "langchainMessagesToCopilotKit");
1889
+ function copilotkitMessagesToLangChain(messages) {
1890
+ const result = [];
1891
+ const processedActionExecutions = /* @__PURE__ */ new Set();
1892
+ for (const message of messages) {
1893
+ if (message.isTextMessage()) {
1894
+ if (message.role === "user") {
1895
+ result.push({
1896
+ ...message,
1897
+ role: MessageRole.user
1898
+ });
1899
+ } else if (message.role === "system") {
1900
+ result.push({
1901
+ ...message,
1902
+ role: MessageRole.system
1903
+ });
1904
+ } else if (message.role === "assistant") {
1905
+ result.push({
1906
+ ...message,
1907
+ role: MessageRole.assistant
1908
+ });
1909
+ }
1910
+ continue;
1739
1911
  }
1740
1912
  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,
1913
+ const messageId = message.parentMessageId ?? message.id;
1914
+ if (processedActionExecutions.has(messageId)) {
1915
+ continue;
1916
+ }
1917
+ processedActionExecutions.add(messageId);
1918
+ const relatedActionExecutions = messages.filter((m) => m.isActionExecutionMessage() && (m.parentMessageId && m.parentMessageId === messageId || m.id === messageId));
1919
+ const tool_calls = relatedActionExecutions.map((m) => ({
1920
+ name: m.name,
1921
+ args: m.arguments,
1922
+ id: m.id
1923
+ }));
1924
+ result.push({
1925
+ id: messageId,
1926
+ type: "ActionExecutionMessage",
1748
1927
  content: "",
1749
- tool_calls: [
1750
- toolCall
1751
- ],
1752
- role: MessageRole.assistant,
1753
- id: message.id
1754
- };
1928
+ tool_calls,
1929
+ role: MessageRole.assistant
1930
+ });
1931
+ continue;
1755
1932
  }
1756
1933
  if (message.isResultMessage()) {
1757
- return {
1934
+ result.push({
1758
1935
  type: message.type,
1759
1936
  content: message.result,
1760
1937
  id: message.id,
1761
1938
  tool_call_id: message.actionExecutionId,
1762
1939
  name: message.actionName,
1763
1940
  role: MessageRole.tool
1764
- };
1941
+ });
1942
+ continue;
1765
1943
  }
1766
1944
  throw new Error(`Unknown message type ${message.type}`);
1767
- });
1945
+ }
1946
+ return result;
1768
1947
  }
1769
- __name(formatMessages, "formatMessages");
1948
+ __name(copilotkitMessagesToLangChain, "copilotkitMessagesToLangChain");
1770
1949
 
1771
1950
  // src/lib/runtime/remote-action-constructors.ts
1772
1951
  function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
@@ -1776,7 +1955,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
1776
1955
  parameters: [],
1777
1956
  handler: async (_args) => {
1778
1957
  },
1779
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
1958
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
1780
1959
  var _a;
1781
1960
  logger2.debug({
1782
1961
  actionName: agent.name
@@ -1802,7 +1981,10 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
1802
1981
  agent,
1803
1982
  threadId,
1804
1983
  nodeName,
1805
- messages,
1984
+ messages: [
1985
+ ...messages,
1986
+ ...additionalMessages
1987
+ ],
1806
1988
  state,
1807
1989
  properties: graphqlContext.properties,
1808
1990
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -1885,7 +2067,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1885
2067
  parameters: [],
1886
2068
  handler: async (_args) => {
1887
2069
  },
1888
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
2070
+ langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [] }) => {
1889
2071
  var _a;
1890
2072
  logger2.debug({
1891
2073
  actionName: agent.name
@@ -1910,7 +2092,10 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1910
2092
  name,
1911
2093
  threadId,
1912
2094
  nodeName,
1913
- messages,
2095
+ messages: [
2096
+ ...messages,
2097
+ ...additionalMessages
2098
+ ],
1914
2099
  state,
1915
2100
  properties: graphqlContext.properties,
1916
2101
  actions: actionInputsWithoutAgents.map((action) => ({
@@ -2153,7 +2338,94 @@ function getRuntimeInstanceTelemetryInfo(runtime) {
2153
2338
  __name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
2154
2339
  var telemetry_client_default = telemetryClient;
2155
2340
 
2341
+ // src/graphql/types/base/index.ts
2342
+ var import_type_graphql2 = require("type-graphql");
2343
+ function _ts_decorate(decorators, target, key, desc) {
2344
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2345
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2346
+ r = Reflect.decorate(decorators, target, key, desc);
2347
+ else
2348
+ for (var i = decorators.length - 1; i >= 0; i--)
2349
+ if (d = decorators[i])
2350
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2351
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2352
+ }
2353
+ __name(_ts_decorate, "_ts_decorate");
2354
+ function _ts_metadata(k, v) {
2355
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2356
+ return Reflect.metadata(k, v);
2357
+ }
2358
+ __name(_ts_metadata, "_ts_metadata");
2359
+ var BaseMessageInput = class {
2360
+ id;
2361
+ createdAt;
2362
+ };
2363
+ __name(BaseMessageInput, "BaseMessageInput");
2364
+ _ts_decorate([
2365
+ (0, import_type_graphql2.Field)(() => String),
2366
+ _ts_metadata("design:type", String)
2367
+ ], BaseMessageInput.prototype, "id", void 0);
2368
+ _ts_decorate([
2369
+ (0, import_type_graphql2.Field)(() => Date),
2370
+ _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2371
+ ], BaseMessageInput.prototype, "createdAt", void 0);
2372
+ BaseMessageInput = _ts_decorate([
2373
+ (0, import_type_graphql2.InputType)()
2374
+ ], BaseMessageInput);
2375
+
2376
+ // src/graphql/types/converted/index.ts
2377
+ var Message = class extends BaseMessageInput {
2378
+ type;
2379
+ isTextMessage() {
2380
+ return this.type === "TextMessage";
2381
+ }
2382
+ isActionExecutionMessage() {
2383
+ return this.type === "ActionExecutionMessage";
2384
+ }
2385
+ isResultMessage() {
2386
+ return this.type === "ResultMessage";
2387
+ }
2388
+ isAgentStateMessage() {
2389
+ return this.type === "AgentStateMessage";
2390
+ }
2391
+ };
2392
+ __name(Message, "Message");
2393
+ var TextMessage = class extends Message {
2394
+ type = "TextMessage";
2395
+ content;
2396
+ role;
2397
+ parentMessageId;
2398
+ };
2399
+ __name(TextMessage, "TextMessage");
2400
+ var ActionExecutionMessage = class extends Message {
2401
+ type = "ActionExecutionMessage";
2402
+ name;
2403
+ arguments;
2404
+ parentMessageId;
2405
+ };
2406
+ __name(ActionExecutionMessage, "ActionExecutionMessage");
2407
+ var ResultMessage = class extends Message {
2408
+ type = "ResultMessage";
2409
+ actionExecutionId;
2410
+ actionName;
2411
+ result;
2412
+ };
2413
+ __name(ResultMessage, "ResultMessage");
2414
+ var AgentStateMessage = class extends Message {
2415
+ type = "AgentStateMessage";
2416
+ threadId;
2417
+ agentName;
2418
+ nodeName;
2419
+ runId;
2420
+ active;
2421
+ role;
2422
+ state;
2423
+ running;
2424
+ };
2425
+ __name(AgentStateMessage, "AgentStateMessage");
2426
+
2156
2427
  // src/service-adapters/events.ts
2428
+ var import_class_transformer = require("class-transformer");
2157
2429
  var RuntimeEventTypes;
2158
2430
  (function(RuntimeEventTypes2) {
2159
2431
  RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
@@ -2169,52 +2441,74 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2169
2441
  constructor() {
2170
2442
  super();
2171
2443
  }
2172
- sendTextMessageStart(messageId) {
2444
+ sendTextMessageStart({ messageId, parentMessageId }) {
2173
2445
  this.next({
2174
2446
  type: "TextMessageStart",
2175
- messageId
2447
+ messageId,
2448
+ parentMessageId
2176
2449
  });
2177
2450
  }
2178
- sendTextMessageContent(content) {
2451
+ sendTextMessageContent({ messageId, content }) {
2179
2452
  this.next({
2180
2453
  type: "TextMessageContent",
2181
- content
2454
+ content,
2455
+ messageId
2182
2456
  });
2183
2457
  }
2184
- sendTextMessageEnd() {
2458
+ sendTextMessageEnd({ messageId }) {
2185
2459
  this.next({
2186
- type: "TextMessageEnd"
2460
+ type: "TextMessageEnd",
2461
+ messageId
2187
2462
  });
2188
2463
  }
2189
2464
  sendTextMessage(messageId, content) {
2190
- this.sendTextMessageStart(messageId);
2191
- this.sendTextMessageContent(content);
2192
- this.sendTextMessageEnd();
2465
+ this.sendTextMessageStart({
2466
+ messageId
2467
+ });
2468
+ this.sendTextMessageContent({
2469
+ messageId,
2470
+ content
2471
+ });
2472
+ this.sendTextMessageEnd({
2473
+ messageId
2474
+ });
2193
2475
  }
2194
- sendActionExecutionStart(actionExecutionId, actionName) {
2476
+ sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId }) {
2195
2477
  this.next({
2196
2478
  type: "ActionExecutionStart",
2197
2479
  actionExecutionId,
2198
- actionName
2480
+ actionName,
2481
+ parentMessageId
2199
2482
  });
2200
2483
  }
2201
- sendActionExecutionArgs(args) {
2484
+ sendActionExecutionArgs({ actionExecutionId, args }) {
2202
2485
  this.next({
2203
2486
  type: "ActionExecutionArgs",
2204
- args
2487
+ args,
2488
+ actionExecutionId
2205
2489
  });
2206
2490
  }
2207
- sendActionExecutionEnd() {
2491
+ sendActionExecutionEnd({ actionExecutionId }) {
2208
2492
  this.next({
2209
- type: "ActionExecutionEnd"
2493
+ type: "ActionExecutionEnd",
2494
+ actionExecutionId
2210
2495
  });
2211
2496
  }
2212
- sendActionExecution(actionExecutionId, toolName, args) {
2213
- this.sendActionExecutionStart(actionExecutionId, toolName);
2214
- this.sendActionExecutionArgs(args);
2215
- this.sendActionExecutionEnd();
2497
+ sendActionExecution({ actionExecutionId, actionName, args, parentMessageId }) {
2498
+ this.sendActionExecutionStart({
2499
+ actionExecutionId,
2500
+ actionName,
2501
+ parentMessageId
2502
+ });
2503
+ this.sendActionExecutionArgs({
2504
+ actionExecutionId,
2505
+ args
2506
+ });
2507
+ this.sendActionExecutionEnd({
2508
+ actionExecutionId
2509
+ });
2216
2510
  }
2217
- sendActionExecutionResult(actionExecutionId, actionName, result) {
2511
+ sendActionExecutionResult({ actionExecutionId, actionName, result }) {
2218
2512
  this.next({
2219
2513
  type: "ActionExecutionResult",
2220
2514
  actionName,
@@ -2222,7 +2516,7 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2222
2516
  result
2223
2517
  });
2224
2518
  }
2225
- sendAgentStateMessage(threadId, agentName, nodeName, runId, active, role, state, running) {
2519
+ sendAgentStateMessage({ threadId, agentName, nodeName, runId, active, role, state, running }) {
2226
2520
  this.next({
2227
2521
  type: "AgentStateMessage",
2228
2522
  threadId,
@@ -2259,27 +2553,19 @@ var RuntimeEventSource = class {
2259
2553
  this.sendErrorMessageToChat();
2260
2554
  });
2261
2555
  return this.eventStream$.pipe(
2262
- // mark tools for server side execution
2263
- (0, import_rxjs2.map)((event) => {
2264
- if (event.type === "ActionExecutionStart") {
2265
- if (event.scope !== "passThrough") {
2266
- event.scope = serverSideActions.find((action) => action.name === event.actionName) ? "server" : "client";
2267
- }
2268
- }
2269
- return event;
2270
- }),
2271
2556
  // track state
2272
2557
  (0, import_rxjs2.scan)((acc, event) => {
2273
2558
  acc = {
2274
2559
  ...acc
2275
2560
  };
2276
2561
  if (event.type === "ActionExecutionStart") {
2277
- acc.callActionServerSide = event.scope === "server";
2562
+ acc.callActionServerSide = serverSideActions.find((action) => action.name === event.actionName) !== void 0;
2278
2563
  acc.args = "";
2279
2564
  acc.actionExecutionId = event.actionExecutionId;
2280
2565
  if (acc.callActionServerSide) {
2281
2566
  acc.action = serverSideActions.find((action) => action.name === event.actionName);
2282
2567
  }
2568
+ acc.actionExecutionParentMessageId = event.parentMessageId;
2283
2569
  } else if (event.type === "ActionExecutionArgs") {
2284
2570
  acc.args += event.args;
2285
2571
  }
@@ -2290,12 +2576,13 @@ var RuntimeEventSource = class {
2290
2576
  callActionServerSide: false,
2291
2577
  args: "",
2292
2578
  actionExecutionId: null,
2293
- action: null
2579
+ action: null,
2580
+ actionExecutionParentMessageId: null
2294
2581
  }),
2295
2582
  (0, import_rxjs2.concatMap)((eventWithState) => {
2296
2583
  if (eventWithState.event.type === "ActionExecutionEnd" && eventWithState.callActionServerSide) {
2297
2584
  const toolCallEventStream$ = new RuntimeEventSubject();
2298
- executeAction(toolCallEventStream$, guardrailsResult$ ? guardrailsResult$ : null, eventWithState.action, eventWithState.args, eventWithState.actionExecutionId, actionInputsWithoutAgents).catch((error) => {
2585
+ executeAction(toolCallEventStream$, guardrailsResult$ ? guardrailsResult$ : null, eventWithState.action, eventWithState.args, eventWithState.actionExecutionParentMessageId, eventWithState.actionExecutionId, actionInputsWithoutAgents).catch((error) => {
2299
2586
  console.error(error);
2300
2587
  });
2301
2588
  telemetry_client_default.capture("oss.runtime.server_action_executed", {});
@@ -2308,7 +2595,7 @@ var RuntimeEventSource = class {
2308
2595
  }
2309
2596
  };
2310
2597
  __name(RuntimeEventSource, "RuntimeEventSource");
2311
- async function executeAction(eventStream$, guardrailsResult$, action, actionArguments, actionExecutionId, actionInputsWithoutAgents) {
2598
+ async function executeAction(eventStream$, guardrailsResult$, action, actionArguments, actionExecutionParentMessageId, actionExecutionId, actionInputsWithoutAgents) {
2312
2599
  var _a;
2313
2600
  if (guardrailsResult$) {
2314
2601
  const { status } = await (0, import_rxjs2.firstValueFrom)(guardrailsResult$);
@@ -2328,10 +2615,33 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2328
2615
  }
2329
2616
  }
2330
2617
  if (isLangGraphAgentAction(action)) {
2331
- eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
2618
+ const result = `${action.name} agent started`;
2619
+ const agentExecution = (0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2620
+ id: actionExecutionId,
2621
+ createdAt: /* @__PURE__ */ new Date(),
2622
+ name: action.name,
2623
+ arguments: JSON.parse(actionArguments),
2624
+ parentMessageId: actionExecutionParentMessageId ?? actionExecutionId
2625
+ });
2626
+ const agentExecutionResult = (0, import_class_transformer.plainToInstance)(ResultMessage, {
2627
+ id: "result-" + actionExecutionId,
2628
+ createdAt: /* @__PURE__ */ new Date(),
2629
+ actionExecutionId,
2630
+ actionName: action.name,
2631
+ result
2632
+ });
2633
+ eventStream$.sendActionExecutionResult({
2634
+ actionExecutionId,
2635
+ actionName: action.name,
2636
+ result
2637
+ });
2332
2638
  const stream = await action.langGraphAgentHandler({
2333
2639
  name: action.name,
2334
- actionInputsWithoutAgents
2640
+ actionInputsWithoutAgents,
2641
+ additionalMessages: [
2642
+ agentExecution,
2643
+ agentExecutionResult
2644
+ ]
2335
2645
  });
2336
2646
  (0, import_rxjs2.from)(stream).subscribe({
2337
2647
  next: (event) => eventStream$.next(event),
@@ -2352,113 +2662,29 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2352
2662
  }
2353
2663
  __name(executeAction, "executeAction");
2354
2664
 
2355
- // src/graphql/types/base/index.ts
2356
- var import_type_graphql2 = require("type-graphql");
2357
- function _ts_decorate(decorators, target, key, desc) {
2358
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2359
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
2360
- r = Reflect.decorate(decorators, target, key, desc);
2361
- else
2362
- for (var i = decorators.length - 1; i >= 0; i--)
2363
- if (d = decorators[i])
2364
- r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2365
- return c > 3 && r && Object.defineProperty(target, key, r), r;
2366
- }
2367
- __name(_ts_decorate, "_ts_decorate");
2368
- function _ts_metadata(k, v) {
2369
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
2370
- return Reflect.metadata(k, v);
2371
- }
2372
- __name(_ts_metadata, "_ts_metadata");
2373
- var BaseMessageInput = class {
2374
- id;
2375
- createdAt;
2376
- };
2377
- __name(BaseMessageInput, "BaseMessageInput");
2378
- _ts_decorate([
2379
- (0, import_type_graphql2.Field)(() => String),
2380
- _ts_metadata("design:type", String)
2381
- ], BaseMessageInput.prototype, "id", void 0);
2382
- _ts_decorate([
2383
- (0, import_type_graphql2.Field)(() => Date),
2384
- _ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
2385
- ], BaseMessageInput.prototype, "createdAt", void 0);
2386
- BaseMessageInput = _ts_decorate([
2387
- (0, import_type_graphql2.InputType)()
2388
- ], BaseMessageInput);
2389
-
2390
- // src/graphql/types/converted/index.ts
2391
- var Message = class extends BaseMessageInput {
2392
- type;
2393
- isTextMessage() {
2394
- return this.type === "TextMessage";
2395
- }
2396
- isActionExecutionMessage() {
2397
- return this.type === "ActionExecutionMessage";
2398
- }
2399
- isResultMessage() {
2400
- return this.type === "ResultMessage";
2401
- }
2402
- isAgentStateMessage() {
2403
- return this.type === "AgentStateMessage";
2404
- }
2405
- };
2406
- __name(Message, "Message");
2407
- var TextMessage = class extends Message {
2408
- type = "TextMessage";
2409
- content;
2410
- role;
2411
- };
2412
- __name(TextMessage, "TextMessage");
2413
- var ActionExecutionMessage = class extends Message {
2414
- type = "ActionExecutionMessage";
2415
- name;
2416
- arguments;
2417
- scope;
2418
- };
2419
- __name(ActionExecutionMessage, "ActionExecutionMessage");
2420
- var ResultMessage = class extends Message {
2421
- type = "ResultMessage";
2422
- actionExecutionId;
2423
- actionName;
2424
- result;
2425
- };
2426
- __name(ResultMessage, "ResultMessage");
2427
- var AgentStateMessage = class extends Message {
2428
- type = "AgentStateMessage";
2429
- threadId;
2430
- agentName;
2431
- nodeName;
2432
- runId;
2433
- active;
2434
- role;
2435
- state;
2436
- running;
2437
- };
2438
- __name(AgentStateMessage, "AgentStateMessage");
2439
-
2440
2665
  // src/service-adapters/conversion.ts
2441
- var import_class_transformer = require("class-transformer");
2666
+ var import_class_transformer2 = require("class-transformer");
2442
2667
  function convertGqlInputToMessages(inputMessages) {
2443
2668
  const messages = [];
2444
2669
  for (const message of inputMessages) {
2445
2670
  if (message.textMessage) {
2446
- messages.push((0, import_class_transformer.plainToInstance)(TextMessage, {
2671
+ messages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
2447
2672
  id: message.id,
2448
2673
  createdAt: message.createdAt,
2449
2674
  role: message.textMessage.role,
2450
- content: message.textMessage.content
2675
+ content: message.textMessage.content,
2676
+ parentMessageId: message.textMessage.parentMessageId
2451
2677
  }));
2452
2678
  } else if (message.actionExecutionMessage) {
2453
- messages.push((0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
2679
+ messages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
2454
2680
  id: message.id,
2455
2681
  createdAt: message.createdAt,
2456
2682
  name: message.actionExecutionMessage.name,
2457
2683
  arguments: JSON.parse(message.actionExecutionMessage.arguments),
2458
- scope: message.actionExecutionMessage.scope
2684
+ parentMessageId: message.actionExecutionMessage.parentMessageId
2459
2685
  }));
2460
2686
  } else if (message.resultMessage) {
2461
- messages.push((0, import_class_transformer.plainToInstance)(ResultMessage, {
2687
+ messages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
2462
2688
  id: message.id,
2463
2689
  createdAt: message.createdAt,
2464
2690
  actionExecutionId: message.resultMessage.actionExecutionId,
@@ -2466,7 +2692,7 @@ function convertGqlInputToMessages(inputMessages) {
2466
2692
  result: message.resultMessage.result
2467
2693
  }));
2468
2694
  } else if (message.agentStateMessage) {
2469
- messages.push((0, import_class_transformer.plainToInstance)(AgentStateMessage, {
2695
+ messages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
2470
2696
  id: message.id,
2471
2697
  threadId: message.agentStateMessage.threadId,
2472
2698
  createdAt: message.createdAt,
@@ -2769,6 +2995,7 @@ MessageInput = _ts_decorate2([
2769
2995
  ], MessageInput);
2770
2996
  var TextMessageInput = class {
2771
2997
  content;
2998
+ parentMessageId;
2772
2999
  role;
2773
3000
  };
2774
3001
  __name(TextMessageInput, "TextMessageInput");
@@ -2776,6 +3003,12 @@ _ts_decorate2([
2776
3003
  (0, import_type_graphql3.Field)(() => String),
2777
3004
  _ts_metadata2("design:type", String)
2778
3005
  ], TextMessageInput.prototype, "content", void 0);
3006
+ _ts_decorate2([
3007
+ (0, import_type_graphql3.Field)(() => String, {
3008
+ nullable: true
3009
+ }),
3010
+ _ts_metadata2("design:type", String)
3011
+ ], TextMessageInput.prototype, "parentMessageId", void 0);
2779
3012
  _ts_decorate2([
2780
3013
  (0, import_type_graphql3.Field)(() => MessageRole),
2781
3014
  _ts_metadata2("design:type", typeof MessageRole === "undefined" ? Object : MessageRole)
@@ -2786,6 +3019,7 @@ TextMessageInput = _ts_decorate2([
2786
3019
  var ActionExecutionMessageInput = class {
2787
3020
  name;
2788
3021
  arguments;
3022
+ parentMessageId;
2789
3023
  scope;
2790
3024
  };
2791
3025
  __name(ActionExecutionMessageInput, "ActionExecutionMessageInput");
@@ -2798,8 +3032,17 @@ _ts_decorate2([
2798
3032
  _ts_metadata2("design:type", String)
2799
3033
  ], ActionExecutionMessageInput.prototype, "arguments", void 0);
2800
3034
  _ts_decorate2([
2801
- (0, import_type_graphql3.Field)(() => ActionExecutionScope),
2802
- _ts_metadata2("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
3035
+ (0, import_type_graphql3.Field)(() => String, {
3036
+ nullable: true
3037
+ }),
3038
+ _ts_metadata2("design:type", String)
3039
+ ], ActionExecutionMessageInput.prototype, "parentMessageId", void 0);
3040
+ _ts_decorate2([
3041
+ (0, import_type_graphql3.Field)(() => String, {
3042
+ nullable: true,
3043
+ deprecationReason: "This field will be removed in a future version"
3044
+ }),
3045
+ _ts_metadata2("design:type", typeof String === "undefined" ? Object : String)
2803
3046
  ], ActionExecutionMessageInput.prototype, "scope", void 0);
2804
3047
  ActionExecutionMessageInput = _ts_decorate2([
2805
3048
  (0, import_type_graphql3.InputType)()
@@ -2807,6 +3050,7 @@ ActionExecutionMessageInput = _ts_decorate2([
2807
3050
  var ResultMessageInput = class {
2808
3051
  actionExecutionId;
2809
3052
  actionName;
3053
+ parentMessageId;
2810
3054
  result;
2811
3055
  };
2812
3056
  __name(ResultMessageInput, "ResultMessageInput");
@@ -2818,6 +3062,12 @@ _ts_decorate2([
2818
3062
  (0, import_type_graphql3.Field)(() => String),
2819
3063
  _ts_metadata2("design:type", String)
2820
3064
  ], ResultMessageInput.prototype, "actionName", void 0);
3065
+ _ts_decorate2([
3066
+ (0, import_type_graphql3.Field)(() => String, {
3067
+ nullable: true
3068
+ }),
3069
+ _ts_metadata2("design:type", String)
3070
+ ], ResultMessageInput.prototype, "parentMessageId", void 0);
2821
3071
  _ts_decorate2([
2822
3072
  (0, import_type_graphql3.Field)(() => String),
2823
3073
  _ts_metadata2("design:type", String)
@@ -3540,6 +3790,7 @@ BaseMessageOutput = _ts_decorate13([
3540
3790
  var TextMessageOutput = class {
3541
3791
  role;
3542
3792
  content;
3793
+ parentMessageId;
3543
3794
  };
3544
3795
  __name(TextMessageOutput, "TextMessageOutput");
3545
3796
  _ts_decorate13([
@@ -3552,6 +3803,12 @@ _ts_decorate13([
3552
3803
  ]),
3553
3804
  _ts_metadata13("design:type", Array)
3554
3805
  ], TextMessageOutput.prototype, "content", void 0);
3806
+ _ts_decorate13([
3807
+ (0, import_type_graphql14.Field)(() => String, {
3808
+ nullable: true
3809
+ }),
3810
+ _ts_metadata13("design:type", String)
3811
+ ], TextMessageOutput.prototype, "parentMessageId", void 0);
3555
3812
  TextMessageOutput = _ts_decorate13([
3556
3813
  (0, import_type_graphql14.ObjectType)({
3557
3814
  implements: BaseMessageOutput
@@ -3561,6 +3818,7 @@ var ActionExecutionMessageOutput = class {
3561
3818
  name;
3562
3819
  scope;
3563
3820
  arguments;
3821
+ parentMessageId;
3564
3822
  };
3565
3823
  __name(ActionExecutionMessageOutput, "ActionExecutionMessageOutput");
3566
3824
  _ts_decorate13([
@@ -3568,8 +3826,11 @@ _ts_decorate13([
3568
3826
  _ts_metadata13("design:type", String)
3569
3827
  ], ActionExecutionMessageOutput.prototype, "name", void 0);
3570
3828
  _ts_decorate13([
3571
- (0, import_type_graphql14.Field)(() => ActionExecutionScope),
3572
- _ts_metadata13("design:type", typeof ActionExecutionScope === "undefined" ? Object : ActionExecutionScope)
3829
+ (0, import_type_graphql14.Field)(() => String, {
3830
+ nullable: true,
3831
+ deprecationReason: "This field will be removed in a future version"
3832
+ }),
3833
+ _ts_metadata13("design:type", String)
3573
3834
  ], ActionExecutionMessageOutput.prototype, "scope", void 0);
3574
3835
  _ts_decorate13([
3575
3836
  (0, import_type_graphql14.Field)(() => [
@@ -3577,6 +3838,12 @@ _ts_decorate13([
3577
3838
  ]),
3578
3839
  _ts_metadata13("design:type", Array)
3579
3840
  ], ActionExecutionMessageOutput.prototype, "arguments", void 0);
3841
+ _ts_decorate13([
3842
+ (0, import_type_graphql14.Field)(() => String, {
3843
+ nullable: true
3844
+ }),
3845
+ _ts_metadata13("design:type", String)
3846
+ ], ActionExecutionMessageOutput.prototype, "parentMessageId", void 0);
3580
3847
  ActionExecutionMessageOutput = _ts_decorate13([
3581
3848
  (0, import_type_graphql14.ObjectType)({
3582
3849
  implements: BaseMessageOutput
@@ -3687,7 +3954,7 @@ CopilotResponse = _ts_decorate13([
3687
3954
  // src/graphql/resolvers/copilot.resolver.ts
3688
3955
  var import_graphql_yoga = require("graphql-yoga");
3689
3956
  var import_graphql_scalars2 = require("graphql-scalars");
3690
- var import_class_transformer2 = require("class-transformer");
3957
+ var import_class_transformer3 = require("class-transformer");
3691
3958
  var import_graphql = require("graphql");
3692
3959
 
3693
3960
  // src/utils/failed-response-status-reasons.ts
@@ -3885,7 +4152,7 @@ var CopilotResolver = class {
3885
4152
  reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
3886
4153
  });
3887
4154
  outputMessages = [
3888
- (0, import_class_transformer2.plainToInstance)(TextMessage, {
4155
+ (0, import_class_transformer3.plainToInstance)(TextMessage, {
3889
4156
  id: (0, import_shared10.randomId)(),
3890
4157
  createdAt: /* @__PURE__ */ new Date(),
3891
4158
  content: result.reason,
@@ -3934,12 +4201,15 @@ var CopilotResolver = class {
3934
4201
  // skip until this message start event
3935
4202
  (0, import_rxjs4.skipWhile)((e) => e !== event),
3936
4203
  // take until the message end event
3937
- (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
4204
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.TextMessageEnd && e.messageId == event.messageId)),
4205
+ // filter out any other message events or message ids
4206
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.TextMessageContent && e.messageId == event.messageId)
3938
4207
  );
3939
4208
  const streamingTextStatus = new import_rxjs4.Subject();
3940
4209
  const messageId = event.messageId;
3941
4210
  pushMessage({
3942
4211
  id: messageId,
4212
+ parentMessageId: event.parentMessageId,
3943
4213
  status: (0, import_rxjs4.firstValueFrom)(streamingTextStatus),
3944
4214
  createdAt: /* @__PURE__ */ new Date(),
3945
4215
  role: MessageRole.assistant,
@@ -3952,7 +4222,7 @@ var CopilotResolver = class {
3952
4222
  reason,
3953
4223
  messageId: messageId2
3954
4224
  }, "Text streaming interrupted");
3955
- streamingTextStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4225
+ streamingTextStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
3956
4226
  reason
3957
4227
  }));
3958
4228
  responseStatus$.next(new MessageStreamInterruptedResponse({
@@ -3985,7 +4255,7 @@ var CopilotResolver = class {
3985
4255
  streamingTextStatus.next(new SuccessMessageStatus());
3986
4256
  stopStreamingText();
3987
4257
  textSubscription == null ? void 0 : textSubscription.unsubscribe();
3988
- outputMessages.push((0, import_class_transformer2.plainToInstance)(TextMessage, {
4258
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(TextMessage, {
3989
4259
  id: messageId,
3990
4260
  createdAt: /* @__PURE__ */ new Date(),
3991
4261
  content: textChunks.join(""),
@@ -3998,14 +4268,20 @@ var CopilotResolver = class {
3998
4268
  break;
3999
4269
  case RuntimeEventTypes.ActionExecutionStart:
4000
4270
  logger2.debug("Action execution start event received");
4001
- const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs4.skipWhile)((e) => e !== event), (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
4271
+ const actionExecutionArgumentStream = eventStream.pipe(
4272
+ (0, import_rxjs4.skipWhile)((e) => e !== event),
4273
+ // take until the action execution end event
4274
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.ActionExecutionEnd && e.actionExecutionId == event.actionExecutionId)),
4275
+ // filter out any other action execution events or action execution ids
4276
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.ActionExecutionArgs && e.actionExecutionId == event.actionExecutionId)
4277
+ );
4002
4278
  const streamingArgumentsStatus = new import_rxjs4.Subject();
4003
4279
  pushMessage({
4004
4280
  id: event.actionExecutionId,
4281
+ parentMessageId: event.parentMessageId,
4005
4282
  status: (0, import_rxjs4.firstValueFrom)(streamingArgumentsStatus),
4006
4283
  createdAt: /* @__PURE__ */ new Date(),
4007
4284
  name: event.actionName,
4008
- scope: event.scope,
4009
4285
  arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
4010
4286
  logger2.debug("Action execution argument stream created");
4011
4287
  const argumentChunks = [];
@@ -4021,7 +4297,7 @@ var CopilotResolver = class {
4021
4297
  logger2.error({
4022
4298
  err
4023
4299
  }, "Error in action execution argument stream");
4024
- streamingArgumentsStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
4300
+ streamingArgumentsStatus.next((0, import_class_transformer3.plainToInstance)(FailedMessageStatus, {
4025
4301
  reason: "An unknown error has occurred in the action execution argument stream"
4026
4302
  }));
4027
4303
  stopStreamingArguments();
@@ -4032,11 +4308,10 @@ var CopilotResolver = class {
4032
4308
  streamingArgumentsStatus.next(new SuccessMessageStatus());
4033
4309
  stopStreamingArguments();
4034
4310
  actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
4035
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ActionExecutionMessage, {
4311
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ActionExecutionMessage, {
4036
4312
  id: event.actionExecutionId,
4037
4313
  createdAt: /* @__PURE__ */ new Date(),
4038
4314
  name: event.actionName,
4039
- scope: event.scope,
4040
4315
  arguments: argumentChunks.join("")
4041
4316
  }));
4042
4317
  }
@@ -4049,15 +4324,15 @@ var CopilotResolver = class {
4049
4324
  result: event.result
4050
4325
  }, "Action execution result event received");
4051
4326
  pushMessage({
4052
- id: (0, import_shared10.randomId)(),
4327
+ id: "result-" + event.actionExecutionId,
4053
4328
  status: new SuccessMessageStatus(),
4054
4329
  createdAt: /* @__PURE__ */ new Date(),
4055
4330
  actionExecutionId: event.actionExecutionId,
4056
4331
  actionName: event.actionName,
4057
4332
  result: event.result
4058
4333
  });
4059
- outputMessages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
4060
- id: (0, import_shared10.randomId)(),
4334
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(ResultMessage, {
4335
+ id: "result-" + event.actionExecutionId,
4061
4336
  createdAt: /* @__PURE__ */ new Date(),
4062
4337
  actionExecutionId: event.actionExecutionId,
4063
4338
  actionName: event.actionName,
@@ -4081,7 +4356,7 @@ var CopilotResolver = class {
4081
4356
  role: MessageRole.assistant,
4082
4357
  createdAt: /* @__PURE__ */ new Date()
4083
4358
  });
4084
- outputMessages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
4359
+ outputMessages.push((0, import_class_transformer3.plainToInstance)(AgentStateMessage, {
4085
4360
  id: (0, import_shared10.randomId)(),
4086
4361
  threadId: event.threadId,
4087
4362
  agentName: event.agentName,