@copilotkit/runtime 1.4.0-pre-1-4-0.11 → 1.4.0-pre-1-4-0-alpha.10

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 (77) hide show
  1. package/CHANGELOG.md +33 -100
  2. package/dist/{chunk-BNQDVBQH.mjs → chunk-56IQ6PGC.mjs} +449 -75
  3. package/dist/chunk-56IQ6PGC.mjs.map +1 -0
  4. package/dist/chunk-DFOKBSIS.mjs +1 -0
  5. package/dist/chunk-DFOKBSIS.mjs.map +1 -0
  6. package/dist/{chunk-VBGS6IWV.mjs → chunk-RDHJQVWH.mjs} +483 -71
  7. package/dist/chunk-RDHJQVWH.mjs.map +1 -0
  8. package/dist/{chunk-V2YEM4Z5.mjs → chunk-SBWLAAB3.mjs} +4 -3
  9. package/dist/chunk-SBWLAAB3.mjs.map +1 -0
  10. package/dist/{chunk-677K33J7.mjs → chunk-SVL5LEKB.mjs} +4 -3
  11. package/dist/chunk-SVL5LEKB.mjs.map +1 -0
  12. package/dist/{chunk-3DNY5YTL.mjs → chunk-YUJSVJWS.mjs} +5 -4
  13. package/dist/chunk-YUJSVJWS.mjs.map +1 -0
  14. package/dist/{copilot-runtime-8d3f40c7.d.ts → copilot-runtime-dbe5fa02.d.ts} +4 -4
  15. package/dist/{groq-adapter-dbfba3eb.d.ts → groq-adapter-192d2413.d.ts} +1 -1
  16. package/dist/index.d.ts +3 -3
  17. package/dist/index.js +486 -169
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +22 -22
  20. package/dist/index.mjs.map +1 -1
  21. package/dist/{langserve-f00629d2.d.ts → langserve-878c62b9.d.ts} +46 -9
  22. package/dist/lib/index.d.ts +3 -3
  23. package/dist/lib/index.js +465 -163
  24. package/dist/lib/index.js.map +1 -1
  25. package/dist/lib/index.mjs +21 -21
  26. package/dist/lib/integrations/index.d.ts +3 -3
  27. package/dist/lib/integrations/index.js +161 -85
  28. package/dist/lib/integrations/index.js.map +1 -1
  29. package/dist/lib/integrations/index.mjs +5 -5
  30. package/dist/lib/integrations/nest/index.d.ts +2 -2
  31. package/dist/lib/integrations/nest/index.js +158 -82
  32. package/dist/lib/integrations/nest/index.js.map +1 -1
  33. package/dist/lib/integrations/nest/index.mjs +3 -3
  34. package/dist/lib/integrations/node-express/index.d.ts +2 -2
  35. package/dist/lib/integrations/node-express/index.js +158 -82
  36. package/dist/lib/integrations/node-express/index.js.map +1 -1
  37. package/dist/lib/integrations/node-express/index.mjs +3 -3
  38. package/dist/lib/integrations/node-http/index.d.ts +2 -2
  39. package/dist/lib/integrations/node-http/index.js +157 -81
  40. package/dist/lib/integrations/node-http/index.js.map +1 -1
  41. package/dist/lib/integrations/node-http/index.mjs +2 -2
  42. package/dist/service-adapters/index.d.ts +3 -3
  43. package/dist/service-adapters/index.js +243 -73
  44. package/dist/service-adapters/index.js.map +1 -1
  45. package/dist/service-adapters/index.mjs +1 -2
  46. package/package.json +4 -4
  47. package/src/agents/langgraph/event-source.ts +12 -0
  48. package/src/agents/langgraph/events.ts +2 -0
  49. package/src/graphql/resolvers/copilot.resolver.ts +28 -2
  50. package/src/lib/integrations/nest/index.ts +5 -2
  51. package/src/lib/integrations/nextjs/app-router.ts +5 -2
  52. package/src/lib/integrations/nextjs/pages-router.ts +5 -2
  53. package/src/lib/integrations/node-express/index.ts +5 -2
  54. package/src/lib/integrations/node-http/index.ts +5 -2
  55. package/src/lib/runtime/copilot-runtime.ts +48 -42
  56. package/src/lib/runtime/remote-action-constructors.ts +17 -3
  57. package/src/lib/runtime/remote-lg-cloud-action.ts +41 -15
  58. package/src/lib/telemetry-client.ts +43 -0
  59. package/src/service-adapters/anthropic/anthropic-adapter.ts +15 -6
  60. package/src/service-adapters/events.ts +86 -36
  61. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +7 -3
  62. package/src/service-adapters/google/google-genai-adapter.ts +2 -2
  63. package/src/service-adapters/groq/groq-adapter.ts +22 -8
  64. package/src/service-adapters/langchain/langchain-adapter.ts +22 -16
  65. package/src/service-adapters/langchain/utils.ts +47 -31
  66. package/src/service-adapters/openai/openai-adapter.ts +25 -8
  67. package/src/service-adapters/openai/openai-assistant-adapter.ts +21 -8
  68. package/src/service-adapters/unify/unify-adapter.ts +28 -11
  69. package/dist/chunk-3DNY5YTL.mjs.map +0 -1
  70. package/dist/chunk-677K33J7.mjs.map +0 -1
  71. package/dist/chunk-BNQDVBQH.mjs.map +0 -1
  72. package/dist/chunk-FL67XJAX.mjs +0 -288
  73. package/dist/chunk-FL67XJAX.mjs.map +0 -1
  74. package/dist/chunk-MXXPWWBF.mjs +0 -218
  75. package/dist/chunk-MXXPWWBF.mjs.map +0 -1
  76. package/dist/chunk-V2YEM4Z5.mjs.map +0 -1
  77. package/dist/chunk-VBGS6IWV.mjs.map +0 -1
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.0-pre-1-4-0.11",
47
+ version: "1.4.0-pre-1-4-0-alpha.10",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -142,12 +142,13 @@ __export(lib_exports, {
142
142
  createContext: () => createContext,
143
143
  flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
144
144
  getCommonConfig: () => getCommonConfig,
145
- langGraphCloudEndpoint: () => langGraphCloudEndpoint
145
+ langGraphCloudEndpoint: () => langGraphCloudEndpoint,
146
+ resolveEndpointType: () => resolveEndpointType
146
147
  });
147
148
  module.exports = __toCommonJS(lib_exports);
148
149
 
149
150
  // src/lib/runtime/copilot-runtime.ts
150
- var import_shared8 = require("@copilotkit/shared");
151
+ var import_shared9 = require("@copilotkit/shared");
151
152
 
152
153
  // src/service-adapters/langchain/langserve.ts
153
154
  var import_remote = require("langchain/runnables/remote");
@@ -423,35 +424,61 @@ var OpenAIAdapter = class {
423
424
  eventSource.stream(async (eventStream$) => {
424
425
  var _a, _b;
425
426
  let mode = null;
427
+ let currentMessageId;
428
+ let currentToolCallId;
426
429
  for await (const chunk of stream) {
430
+ if (chunk.choices.length === 0) {
431
+ continue;
432
+ }
427
433
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
428
434
  const content = chunk.choices[0].delta.content;
429
435
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
430
436
  mode = null;
431
- eventStream$.sendTextMessageEnd();
437
+ eventStream$.sendTextMessageEnd({
438
+ messageId: currentMessageId
439
+ });
432
440
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
433
441
  mode = null;
434
- eventStream$.sendActionExecutionEnd();
442
+ eventStream$.sendActionExecutionEnd({
443
+ actionExecutionId: currentToolCallId
444
+ });
435
445
  }
436
446
  if (mode === null) {
437
447
  if (toolCall == null ? void 0 : toolCall.id) {
438
448
  mode = "function";
439
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
449
+ currentToolCallId = toolCall.id;
450
+ eventStream$.sendActionExecutionStart({
451
+ actionExecutionId: currentToolCallId,
452
+ actionName: toolCall.function.name
453
+ });
440
454
  } else if (content) {
441
455
  mode = "message";
442
- eventStream$.sendTextMessageStart(chunk.id);
456
+ currentMessageId = chunk.id;
457
+ eventStream$.sendTextMessageStart({
458
+ messageId: currentMessageId
459
+ });
443
460
  }
444
461
  }
445
462
  if (mode === "message" && content) {
446
- eventStream$.sendTextMessageContent(content);
463
+ eventStream$.sendTextMessageContent({
464
+ messageId: currentMessageId,
465
+ content
466
+ });
447
467
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
448
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
468
+ eventStream$.sendActionExecutionArgs({
469
+ actionExecutionId: currentToolCallId,
470
+ args: toolCall.function.arguments
471
+ });
449
472
  }
450
473
  }
451
474
  if (mode === "message") {
452
- eventStream$.sendTextMessageEnd();
475
+ eventStream$.sendTextMessageEnd({
476
+ messageId: currentMessageId
477
+ });
453
478
  } else if (mode === "function") {
454
- eventStream$.sendActionExecutionEnd();
479
+ eventStream$.sendActionExecutionEnd({
480
+ actionExecutionId: currentToolCallId
481
+ });
455
482
  }
456
483
  eventStream$.complete();
457
484
  });
@@ -547,7 +574,11 @@ function isBaseMessageChunk(message) {
547
574
  __name(isBaseMessageChunk, "isBaseMessageChunk");
548
575
  function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
549
576
  if (actionExecution) {
550
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
577
+ eventStream$.sendActionExecutionResult({
578
+ actionExecutionId: actionExecution.id,
579
+ actionName: actionExecution.name,
580
+ result: "Sending a message"
581
+ });
551
582
  }
552
583
  }
553
584
  __name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
@@ -557,7 +588,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
557
588
  if (!actionExecution) {
558
589
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result);
559
590
  } else {
560
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
591
+ eventStream$.sendActionExecutionResult({
592
+ actionExecutionId: actionExecution.id,
593
+ actionName: actionExecution.name,
594
+ result
595
+ });
561
596
  }
562
597
  } else if (isAIMessage(result)) {
563
598
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -565,7 +600,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
565
600
  eventStream$.sendTextMessage((0, import_shared2.randomId)(), result.content);
566
601
  }
567
602
  for (const toolCall of result.tool_calls) {
568
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
603
+ eventStream$.sendActionExecution({
604
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
605
+ actionName: toolCall.name,
606
+ args: JSON.stringify(toolCall.args)
607
+ });
569
608
  }
570
609
  } else if (isBaseMessageChunk(result)) {
571
610
  maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
@@ -574,7 +613,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
574
613
  }
575
614
  if ((_b = result.lc_kwargs) == null ? void 0 : _b.tool_calls) {
576
615
  for (const toolCall of (_c = result.lc_kwargs) == null ? void 0 : _c.tool_calls) {
577
- eventStream$.sendActionExecution(toolCall.id || (0, import_shared2.randomId)(), toolCall.name, JSON.stringify(toolCall.args));
616
+ eventStream$.sendActionExecution({
617
+ actionExecutionId: toolCall.id || (0, import_shared2.randomId)(),
618
+ actionName: toolCall.name,
619
+ args: JSON.stringify(toolCall.args)
620
+ });
578
621
  }
579
622
  }
580
623
  } else if (result && "getReader" in result) {
@@ -592,6 +635,7 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
592
635
  const { done, value } = await reader.read();
593
636
  let toolCallName = void 0;
594
637
  let toolCallId = void 0;
638
+ let currentMessageId;
595
639
  let toolCallArgs = void 0;
596
640
  let hasToolCall = false;
597
641
  let content = value == null ? void 0 : value.content;
@@ -619,10 +663,14 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
619
663
  }
620
664
  if (mode === "message" && (toolCallId || done)) {
621
665
  mode = null;
622
- eventStream$.sendTextMessageEnd();
666
+ eventStream$.sendTextMessageEnd({
667
+ messageId: currentMessageId
668
+ });
623
669
  } else if (mode === "function" && (!hasToolCall || done)) {
624
670
  mode = null;
625
- eventStream$.sendActionExecutionEnd();
671
+ eventStream$.sendActionExecutionEnd({
672
+ actionExecutionId: toolCallId
673
+ });
626
674
  }
627
675
  if (done) {
628
676
  break;
@@ -630,21 +678,38 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
630
678
  if (mode === null) {
631
679
  if (hasToolCall && toolCallId && toolCallName) {
632
680
  mode = "function";
633
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
681
+ eventStream$.sendActionExecutionStart({
682
+ actionExecutionId: toolCallId,
683
+ actionName: toolCallName
684
+ });
634
685
  } else if (content) {
635
686
  mode = "message";
636
- eventStream$.sendTextMessageStart((0, import_shared2.randomId)());
687
+ currentMessageId = (0, import_shared2.randomId)();
688
+ eventStream$.sendTextMessageStart({
689
+ messageId: currentMessageId
690
+ });
637
691
  }
638
692
  }
639
693
  if (mode === "message" && content) {
640
- eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
694
+ eventStream$.sendTextMessageContent({
695
+ messageId: currentMessageId,
696
+ content: Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content
697
+ });
641
698
  } else if (mode === "function" && toolCallArgs) {
642
699
  if (toolCallDetails.index !== toolCallDetails.prevIndex) {
643
- eventStream$.sendActionExecutionEnd();
644
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
700
+ eventStream$.sendActionExecutionEnd({
701
+ actionExecutionId: toolCallId
702
+ });
703
+ eventStream$.sendActionExecutionStart({
704
+ actionExecutionId: toolCallId,
705
+ actionName: toolCallName
706
+ });
645
707
  toolCallDetails.prevIndex = toolCallDetails.index;
646
708
  }
647
- eventStream$.sendActionExecutionArgs(toolCallArgs);
709
+ eventStream$.sendActionExecutionArgs({
710
+ actionExecutionId: toolCallId,
711
+ args: toolCallArgs
712
+ });
648
713
  }
649
714
  } catch (error) {
650
715
  console.error("Error reading from stream", error);
@@ -652,7 +717,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
652
717
  }
653
718
  }
654
719
  } else if (actionExecution) {
655
- eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
720
+ eventStream$.sendActionExecutionResult({
721
+ actionExecutionId: actionExecution.id,
722
+ actionName: actionExecution.name,
723
+ result: encodeResult(result)
724
+ });
656
725
  } else {
657
726
  throw new Error("Invalid return type from LangChain function.");
658
727
  }
@@ -672,6 +741,7 @@ __name(encodeResult, "encodeResult");
672
741
 
673
742
  // src/service-adapters/langchain/langchain-adapter.ts
674
743
  var import_shared3 = require("@copilotkit/shared");
744
+ var import_promises = require("@langchain/core/callbacks/promises");
675
745
  var LangChainAdapter = class {
676
746
  options;
677
747
  /**
@@ -681,23 +751,28 @@ var LangChainAdapter = class {
681
751
  this.options = options;
682
752
  }
683
753
  async process(request) {
684
- const { eventSource, model, actions, messages, threadId, runId } = request;
685
- const result = await this.options.chainFn({
686
- messages: messages.map(convertMessageToLangChainMessage),
687
- tools: actions.map(convertActionInputToLangChainTool),
688
- model,
689
- threadId,
690
- runId
691
- });
692
- eventSource.stream(async (eventStream$) => {
693
- await streamLangChainResponse({
694
- result,
695
- eventStream$
754
+ try {
755
+ const { eventSource, model, actions, messages, runId } = request;
756
+ const threadId = request.threadId ?? (0, import_shared3.randomId)();
757
+ const result = await this.options.chainFn({
758
+ messages: messages.map(convertMessageToLangChainMessage),
759
+ tools: actions.map(convertActionInputToLangChainTool),
760
+ model,
761
+ threadId,
762
+ runId
696
763
  });
697
- });
698
- return {
699
- threadId: threadId || (0, import_shared3.randomId)()
700
- };
764
+ eventSource.stream(async (eventStream$) => {
765
+ await streamLangChainResponse({
766
+ result,
767
+ eventStream$
768
+ });
769
+ });
770
+ return {
771
+ threadId
772
+ };
773
+ } finally {
774
+ await (0, import_promises.awaitAllCallbacks)();
775
+ }
701
776
  }
702
777
  };
703
778
  __name(LangChainAdapter, "LangChainAdapter");
@@ -707,12 +782,16 @@ var import_google_gauth = require("@langchain/google-gauth");
707
782
  var GoogleGenerativeAIAdapter = class extends LangChainAdapter {
708
783
  constructor(options) {
709
784
  super({
710
- chainFn: async ({ messages, tools }) => {
785
+ chainFn: async ({ messages, tools, threadId }) => {
711
786
  const model = new import_google_gauth.ChatGoogle({
712
787
  modelName: (options == null ? void 0 : options.model) ?? "gemini-1.5-pro",
713
788
  apiVersion: "v1beta"
714
789
  }).bindTools(tools);
715
- return model.stream(messages);
790
+ return model.stream(messages, {
791
+ metadata: {
792
+ conversation_id: threadId
793
+ }
794
+ });
716
795
  }
717
796
  });
718
797
  }
@@ -822,21 +901,33 @@ var OpenAIAssistantAdapter = class {
822
901
  eventSource.stream(async (eventStream$) => {
823
902
  var _a, _b, _c, _d, _e, _f;
824
903
  let inFunctionCall = false;
904
+ let currentMessageId;
905
+ let currentToolCallId;
825
906
  for await (const chunk of stream) {
826
907
  switch (chunk.event) {
827
908
  case "thread.message.created":
828
909
  if (inFunctionCall) {
829
- eventStream$.sendActionExecutionEnd();
910
+ eventStream$.sendActionExecutionEnd({
911
+ actionExecutionId: currentToolCallId
912
+ });
830
913
  }
831
- eventStream$.sendTextMessageStart(chunk.data.id);
914
+ currentMessageId = chunk.data.id;
915
+ eventStream$.sendTextMessageStart({
916
+ messageId: currentMessageId
917
+ });
832
918
  break;
833
919
  case "thread.message.delta":
834
920
  if (((_a = chunk.data.delta.content) == null ? void 0 : _a[0].type) === "text") {
835
- eventStream$.sendTextMessageContent((_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value);
921
+ eventStream$.sendTextMessageContent({
922
+ messageId: currentMessageId,
923
+ content: (_b = chunk.data.delta.content) == null ? void 0 : _b[0].text.value
924
+ });
836
925
  }
837
926
  break;
838
927
  case "thread.message.completed":
839
- eventStream$.sendTextMessageEnd();
928
+ eventStream$.sendTextMessageEnd({
929
+ messageId: currentMessageId
930
+ });
840
931
  break;
841
932
  case "thread.run.step.delta":
842
933
  let toolCallId;
@@ -849,18 +940,29 @@ var OpenAIAssistantAdapter = class {
849
940
  }
850
941
  if (toolCallName && toolCallId) {
851
942
  if (inFunctionCall) {
852
- eventStream$.sendActionExecutionEnd();
943
+ eventStream$.sendActionExecutionEnd({
944
+ actionExecutionId: currentToolCallId
945
+ });
853
946
  }
854
947
  inFunctionCall = true;
855
- eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
948
+ currentToolCallId = toolCallId;
949
+ eventStream$.sendActionExecutionStart({
950
+ actionExecutionId: currentToolCallId,
951
+ actionName: toolCallName
952
+ });
856
953
  } else if (toolCallArgs) {
857
- eventStream$.sendActionExecutionArgs(toolCallArgs);
954
+ eventStream$.sendActionExecutionArgs({
955
+ actionExecutionId: currentToolCallId,
956
+ args: toolCallArgs
957
+ });
858
958
  }
859
959
  break;
860
960
  }
861
961
  }
862
962
  if (inFunctionCall) {
863
- eventStream$.sendActionExecutionEnd();
963
+ eventStream$.sendActionExecutionEnd({
964
+ actionExecutionId: currentToolCallId
965
+ });
864
966
  }
865
967
  eventStream$.complete();
866
968
  });
@@ -913,46 +1015,77 @@ var UnifyAdapter = class {
913
1015
  }
914
1016
  });
915
1017
  let model = null;
1018
+ let currentMessageId;
1019
+ let currentToolCallId;
916
1020
  request.eventSource.stream(async (eventStream$) => {
917
1021
  var _a, _b;
918
1022
  let mode = null;
919
1023
  for await (const chunk of stream) {
920
1024
  if (this.start) {
921
1025
  model = chunk.model;
922
- eventStream$.sendTextMessageStart((0, import_shared4.randomId)());
923
- eventStream$.sendTextMessageContent(`Model used: ${model}
924
- `);
925
- eventStream$.sendTextMessageEnd();
1026
+ currentMessageId = (0, import_shared4.randomId)();
1027
+ eventStream$.sendTextMessageStart({
1028
+ messageId: currentMessageId
1029
+ });
1030
+ eventStream$.sendTextMessageContent({
1031
+ messageId: currentMessageId,
1032
+ content: `Model used: ${model}
1033
+ `
1034
+ });
1035
+ eventStream$.sendTextMessageEnd({
1036
+ messageId: currentMessageId
1037
+ });
926
1038
  this.start = false;
927
1039
  }
928
1040
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
929
1041
  const content = chunk.choices[0].delta.content;
930
1042
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
931
1043
  mode = null;
932
- eventStream$.sendTextMessageEnd();
1044
+ eventStream$.sendTextMessageEnd({
1045
+ messageId: currentMessageId
1046
+ });
933
1047
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
934
1048
  mode = null;
935
- eventStream$.sendActionExecutionEnd();
1049
+ eventStream$.sendActionExecutionEnd({
1050
+ actionExecutionId: currentToolCallId
1051
+ });
936
1052
  }
937
1053
  if (mode === null) {
938
1054
  if (toolCall == null ? void 0 : toolCall.id) {
939
1055
  mode = "function";
940
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1056
+ currentToolCallId = toolCall.id;
1057
+ eventStream$.sendActionExecutionStart({
1058
+ actionExecutionId: currentToolCallId,
1059
+ actionName: toolCall.function.name
1060
+ });
941
1061
  } else if (content) {
942
1062
  mode = "message";
943
- eventStream$.sendTextMessageStart(chunk.id);
1063
+ currentMessageId = chunk.id;
1064
+ eventStream$.sendTextMessageStart({
1065
+ messageId: currentMessageId
1066
+ });
944
1067
  }
945
1068
  }
946
1069
  if (mode === "message" && content) {
947
- eventStream$.sendTextMessageContent(content);
1070
+ eventStream$.sendTextMessageContent({
1071
+ messageId: currentMessageId,
1072
+ content
1073
+ });
948
1074
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
949
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1075
+ eventStream$.sendActionExecutionArgs({
1076
+ actionExecutionId: currentToolCallId,
1077
+ args: toolCall.function.arguments
1078
+ });
950
1079
  }
951
1080
  }
952
1081
  if (mode === "message") {
953
- eventStream$.sendTextMessageEnd();
1082
+ eventStream$.sendTextMessageEnd({
1083
+ messageId: currentMessageId
1084
+ });
954
1085
  } else if (mode === "function") {
955
- eventStream$.sendActionExecutionEnd();
1086
+ eventStream$.sendActionExecutionEnd({
1087
+ actionExecutionId: currentToolCallId
1088
+ });
956
1089
  }
957
1090
  eventStream$.complete();
958
1091
  });
@@ -1018,35 +1151,58 @@ var GroqAdapter = class {
1018
1151
  eventSource.stream(async (eventStream$) => {
1019
1152
  var _a, _b;
1020
1153
  let mode = null;
1154
+ let currentMessageId;
1155
+ let currentToolCallId;
1021
1156
  for await (const chunk of stream) {
1022
1157
  const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
1023
1158
  const content = chunk.choices[0].delta.content;
1024
1159
  if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
1025
1160
  mode = null;
1026
- eventStream$.sendTextMessageEnd();
1161
+ eventStream$.sendTextMessageEnd({
1162
+ messageId: currentMessageId
1163
+ });
1027
1164
  } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
1028
1165
  mode = null;
1029
- eventStream$.sendActionExecutionEnd();
1166
+ eventStream$.sendActionExecutionEnd({
1167
+ actionExecutionId: currentToolCallId
1168
+ });
1030
1169
  }
1031
1170
  if (mode === null) {
1032
1171
  if (toolCall == null ? void 0 : toolCall.id) {
1033
1172
  mode = "function";
1034
- eventStream$.sendActionExecutionStart(toolCall.id, toolCall.function.name);
1173
+ currentToolCallId = toolCall.id;
1174
+ eventStream$.sendActionExecutionStart({
1175
+ actionExecutionId: currentToolCallId,
1176
+ actionName: toolCall.function.name
1177
+ });
1035
1178
  } else if (content) {
1036
1179
  mode = "message";
1037
- eventStream$.sendTextMessageStart(chunk.id);
1180
+ currentMessageId = chunk.id;
1181
+ eventStream$.sendTextMessageStart({
1182
+ messageId: currentMessageId
1183
+ });
1038
1184
  }
1039
1185
  }
1040
1186
  if (mode === "message" && content) {
1041
- eventStream$.sendTextMessageContent(content);
1187
+ eventStream$.sendTextMessageContent({
1188
+ messageId: currentMessageId,
1189
+ content
1190
+ });
1042
1191
  } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
1043
- eventStream$.sendActionExecutionArgs(toolCall.function.arguments);
1192
+ eventStream$.sendActionExecutionArgs({
1193
+ actionExecutionId: currentToolCallId,
1194
+ args: toolCall.function.arguments
1195
+ });
1044
1196
  }
1045
1197
  }
1046
1198
  if (mode === "message") {
1047
- eventStream$.sendTextMessageEnd();
1199
+ eventStream$.sendTextMessageEnd({
1200
+ messageId: currentMessageId
1201
+ });
1048
1202
  } else if (mode === "function") {
1049
- eventStream$.sendActionExecutionEnd();
1203
+ eventStream$.sendActionExecutionEnd({
1204
+ actionExecutionId: currentToolCallId
1205
+ });
1050
1206
  }
1051
1207
  eventStream$.complete();
1052
1208
  });
@@ -1058,16 +1214,14 @@ var GroqAdapter = class {
1058
1214
  __name(GroqAdapter, "GroqAdapter");
1059
1215
 
1060
1216
  // src/service-adapters/events.ts
1217
+ var import_shared8 = require("@copilotkit/shared");
1061
1218
  var import_rxjs2 = require("rxjs");
1062
1219
 
1063
1220
  // src/lib/telemetry-client.ts
1064
- var import_shared6 = require("@copilotkit/shared");
1065
- var packageJson = require_package();
1066
- var telemetryClient = new import_shared6.TelemetryClient({
1067
- packageName: packageJson.name,
1068
- packageVersion: packageJson.version
1069
- });
1070
- var telemetry_client_default = telemetryClient;
1221
+ var import_shared7 = require("@copilotkit/shared");
1222
+
1223
+ // src/lib/runtime/remote-action-constructors.ts
1224
+ var import_node_crypto2 = require("crypto");
1071
1225
 
1072
1226
  // src/agents/langgraph/event-source.ts
1073
1227
  var import_rxjs = require("rxjs");
@@ -1084,6 +1238,8 @@ var LangGraphEventTypes;
1084
1238
  LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
1085
1239
  LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
1086
1240
  LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
1241
+ LangGraphEventTypes2["OnCopilotKitEmitMessage"] = "on_copilotkit_emit_message";
1242
+ LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
1087
1243
  LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
1088
1244
  })(LangGraphEventTypes || (LangGraphEventTypes = {}));
1089
1245
  var CustomEventNames;
@@ -1095,7 +1251,7 @@ var CustomEventNames;
1095
1251
  })(CustomEventNames || (CustomEventNames = {}));
1096
1252
 
1097
1253
  // src/agents/langgraph/event-source.ts
1098
- var import_shared7 = require("@copilotkit/shared");
1254
+ var import_shared6 = require("@copilotkit/shared");
1099
1255
  var RemoteLangGraphEventSource = class {
1100
1256
  eventStream$ = new import_rxjs.ReplaySubject();
1101
1257
  shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
@@ -1177,12 +1333,14 @@ var RemoteLangGraphEventSource = class {
1177
1333
  }
1178
1334
  if (eventWithState.prevToolCallMessageId !== null && eventWithState.prevToolCallMessageId !== eventWithState.toolCallMessageId && this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1179
1335
  events.push({
1180
- type: RuntimeEventTypes.ActionExecutionEnd
1336
+ type: RuntimeEventTypes.ActionExecutionEnd,
1337
+ actionExecutionId: eventWithState.prevToolCallMessageId
1181
1338
  });
1182
1339
  }
1183
1340
  if (eventWithState.prevMessageId !== null && eventWithState.prevMessageId !== eventWithState.messageId && shouldEmitMessages) {
1184
1341
  events.push({
1185
- type: RuntimeEventTypes.TextMessageEnd
1342
+ type: RuntimeEventTypes.TextMessageEnd,
1343
+ messageId: eventWithState.prevMessageId
1186
1344
  });
1187
1345
  }
1188
1346
  switch (eventWithState.event.event) {
@@ -1194,10 +1352,12 @@ var RemoteLangGraphEventSource = class {
1194
1352
  });
1195
1353
  events.push({
1196
1354
  type: RuntimeEventTypes.TextMessageContent,
1355
+ messageId: eventWithState.event.data.message_id,
1197
1356
  content: eventWithState.event.data.message
1198
1357
  });
1199
1358
  events.push({
1200
- type: RuntimeEventTypes.TextMessageEnd
1359
+ type: RuntimeEventTypes.TextMessageEnd,
1360
+ messageId: eventWithState.event.data.message_id
1201
1361
  });
1202
1362
  } else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
1203
1363
  events.push({
@@ -1207,10 +1367,12 @@ var RemoteLangGraphEventSource = class {
1207
1367
  });
1208
1368
  events.push({
1209
1369
  type: RuntimeEventTypes.ActionExecutionArgs,
1370
+ actionExecutionId: eventWithState.event.data.id,
1210
1371
  args: JSON.stringify(eventWithState.event.data.args)
1211
1372
  });
1212
1373
  events.push({
1213
- type: RuntimeEventTypes.ActionExecutionEnd
1374
+ type: RuntimeEventTypes.ActionExecutionEnd,
1375
+ actionExecutionId: eventWithState.event.data.id
1214
1376
  });
1215
1377
  }
1216
1378
  break;
@@ -1254,6 +1416,7 @@ var RemoteLangGraphEventSource = class {
1254
1416
  if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
1255
1417
  events.push({
1256
1418
  type: RuntimeEventTypes.ActionExecutionArgs,
1419
+ actionExecutionId: eventWithState.toolCallMessageId,
1257
1420
  args
1258
1421
  });
1259
1422
  }
@@ -1261,6 +1424,7 @@ var RemoteLangGraphEventSource = class {
1261
1424
  if (shouldEmitMessages) {
1262
1425
  events.push({
1263
1426
  type: RuntimeEventTypes.TextMessageContent,
1427
+ messageId: eventWithState.messageId,
1264
1428
  content
1265
1429
  });
1266
1430
  }
@@ -1273,25 +1437,29 @@ var RemoteLangGraphEventSource = class {
1273
1437
  const events = [];
1274
1438
  if (lastEventWithState == null ? void 0 : lastEventWithState.messageId) {
1275
1439
  events.push({
1276
- type: RuntimeEventTypes.TextMessageEnd
1440
+ type: RuntimeEventTypes.TextMessageEnd,
1441
+ messageId: lastEventWithState.messageId
1277
1442
  });
1278
1443
  }
1279
1444
  if (lastEventWithState == null ? void 0 : lastEventWithState.toolCallMessageId) {
1280
1445
  events.push({
1281
- type: RuntimeEventTypes.ActionExecutionEnd
1446
+ type: RuntimeEventTypes.ActionExecutionEnd,
1447
+ actionExecutionId: lastEventWithState.toolCallMessageId
1282
1448
  });
1283
1449
  }
1284
- const messageId = (0, import_shared7.randomId)();
1450
+ const messageId = (0, import_shared6.randomId)();
1285
1451
  events.push({
1286
1452
  type: RuntimeEventTypes.TextMessageStart,
1287
1453
  messageId
1288
1454
  });
1289
1455
  events.push({
1290
1456
  type: RuntimeEventTypes.TextMessageContent,
1457
+ messageId,
1291
1458
  content: "\u274C An error occurred. Please try again."
1292
1459
  });
1293
1460
  events.push({
1294
- type: RuntimeEventTypes.TextMessageEnd
1461
+ type: RuntimeEventTypes.TextMessageEnd,
1462
+ messageId
1295
1463
  });
1296
1464
  return events;
1297
1465
  }));
@@ -1354,6 +1522,7 @@ async function execute(args) {
1354
1522
  }
1355
1523
  __name(execute, "execute");
1356
1524
  async function streamEvents(controller, args) {
1525
+ var _a, _b, _c, _d, _e, _f;
1357
1526
  const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
1358
1527
  let nodeName = initialNodeName;
1359
1528
  let state = initialState;
@@ -1427,7 +1596,14 @@ async function streamEvents(controller, args) {
1427
1596
  const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
1428
1597
  let latestStateValues = {};
1429
1598
  let updatedState = state;
1599
+ let manuallyEmittedState = null;
1600
+ let streamInfo = {
1601
+ hashedLgcKey: (0, import_node_crypto.createHash)("sha256").update(langsmithApiKey).digest("hex")
1602
+ };
1430
1603
  try {
1604
+ telemetry_client_default.capture("oss.runtime.agent_execution_stream_started", {
1605
+ hashedLgcKey: streamInfo.hashedLgcKey
1606
+ });
1431
1607
  for await (const chunk of streamResponse2) {
1432
1608
  if (![
1433
1609
  "events",
@@ -1449,26 +1625,37 @@ async function streamEvents(controller, args) {
1449
1625
  const runId = event.metadata.run_id;
1450
1626
  externalRunId = runId;
1451
1627
  const metadata = event.metadata;
1628
+ if (((_b = (_a = event.data) == null ? void 0 : _a.output) == null ? void 0 : _b.model) != null && ((_d = (_c = event.data) == null ? void 0 : _c.output) == null ? void 0 : _d.model) != "") {
1629
+ streamInfo.provider = (_f = (_e = event.data) == null ? void 0 : _e.output) == null ? void 0 : _f.model;
1630
+ }
1631
+ if (metadata.langgraph_host != null && metadata.langgraph_host != "") {
1632
+ streamInfo.langGraphHost = metadata.langgraph_host;
1633
+ }
1634
+ if (metadata.langgraph_version != null && metadata.langgraph_version != "") {
1635
+ streamInfo.langGraphVersion = metadata.langgraph_version;
1636
+ }
1452
1637
  shouldExit = shouldExit || eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitExit;
1453
1638
  const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
1454
1639
  const manuallyEmitIntermediateState = eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState;
1640
+ const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
1641
+ if (exitingNode) {
1642
+ manuallyEmittedState = null;
1643
+ }
1455
1644
  if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
1456
1645
  nodeName = currentNodeName;
1457
- if (eventType === LangGraphEventTypes.OnChainStart || eventType === LangGraphEventTypes.OnChainEnd) {
1458
- updatedState = latestStateValues;
1459
- }
1460
1646
  }
1647
+ updatedState = manuallyEmittedState ?? latestStateValues;
1461
1648
  if (!nodeName) {
1462
1649
  continue;
1463
1650
  }
1464
1651
  if (manuallyEmitIntermediateState) {
1465
- updatedState = event.data;
1652
+ manuallyEmittedState = event.data;
1466
1653
  emit(getStateSyncEvent({
1467
1654
  threadId,
1468
1655
  runId,
1469
1656
  agentName: agent.name,
1470
1657
  nodeName,
1471
- state: updatedState,
1658
+ state: manuallyEmittedState,
1472
1659
  running: true,
1473
1660
  active: true
1474
1661
  }));
@@ -1492,7 +1679,6 @@ async function streamEvents(controller, args) {
1492
1679
  if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
1493
1680
  emitIntermediateStateUntilEnd = null;
1494
1681
  }
1495
- const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
1496
1682
  if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
1497
1683
  state = updatedState;
1498
1684
  prevNodeName = nodeName;
@@ -1511,6 +1697,7 @@ async function streamEvents(controller, args) {
1511
1697
  state = await client.threads.getState(threadId);
1512
1698
  const isEndNode = state.next.length === 0;
1513
1699
  nodeName = Object.keys(state.metadata.writes)[0];
1700
+ telemetry_client_default.capture("oss.runtime.agent_execution_stream_ended", streamInfo);
1514
1701
  emit(getStateSyncEvent({
1515
1702
  threadId,
1516
1703
  runId: externalRunId,
@@ -1758,7 +1945,12 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
1758
1945
  logger2.debug({
1759
1946
  actionName: agent.name
1760
1947
  }, "Executing LangGraph Cloud agent");
1761
- telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
1948
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {
1949
+ agentExecution: true,
1950
+ type: "langgraph-cloud",
1951
+ agentsAmount: endpoint.agents.length,
1952
+ hashedLgcKey: (0, import_node_crypto2.createHash)("sha256").update(endpoint.langsmithApiKey).digest("hex")
1953
+ });
1762
1954
  let state = {};
1763
1955
  if (agentStates) {
1764
1956
  const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
@@ -1812,7 +2004,11 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1812
2004
  args
1813
2005
  }, "Executing remote action");
1814
2006
  const headers = createHeaders(onBeforeRequest, graphqlContext);
1815
- telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
2007
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {
2008
+ agentExecution: false,
2009
+ type: "self-hosted",
2010
+ agentsAmount: json["agents"].length
2011
+ });
1816
2012
  try {
1817
2013
  const response = await fetch(`${url}/actions/execute`, {
1818
2014
  method: "POST",
@@ -1858,7 +2054,11 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
1858
2054
  actionName: agent.name
1859
2055
  }, "Executing remote agent");
1860
2056
  const headers = createHeaders(onBeforeRequest, graphqlContext);
1861
- telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
2057
+ telemetry_client_default.capture("oss.runtime.remote_action_executed", {
2058
+ agentExecution: true,
2059
+ type: "self-hosted",
2060
+ agentsAmount: json["agents"].length
2061
+ });
1862
2062
  let state = {};
1863
2063
  if (agentStates) {
1864
2064
  const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
@@ -2069,6 +2269,53 @@ async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, m
2069
2269
  }
2070
2270
  __name(setupRemoteActions, "setupRemoteActions");
2071
2271
 
2272
+ // src/lib/telemetry-client.ts
2273
+ var import_node_crypto3 = require("crypto");
2274
+ var packageJson = require_package();
2275
+ var telemetryClient = new import_shared7.TelemetryClient({
2276
+ packageName: packageJson.name,
2277
+ packageVersion: packageJson.version
2278
+ });
2279
+ function getRuntimeInstanceTelemetryInfo(runtime) {
2280
+ const endpointsInfo = runtime.remoteEndpointDefinitions.reduce((acc, endpoint) => {
2281
+ let info = {
2282
+ ...acc
2283
+ };
2284
+ const endpointType = resolveEndpointType(endpoint);
2285
+ if (!info.endpointTypes.includes(endpointType)) {
2286
+ info = {
2287
+ ...info,
2288
+ endpointTypes: [
2289
+ ...info.endpointTypes,
2290
+ endpointType
2291
+ ]
2292
+ };
2293
+ }
2294
+ if (endpointType === EndpointType.LangGraphCloud) {
2295
+ const ep = endpoint;
2296
+ info = {
2297
+ ...info,
2298
+ agentsAmount: ep.agents.length,
2299
+ hashedKey: (0, import_node_crypto3.createHash)("sha256").update(ep.langsmithApiKey).digest("hex")
2300
+ };
2301
+ }
2302
+ return info;
2303
+ }, {
2304
+ endpointTypes: [],
2305
+ agentsAmount: null,
2306
+ hashedKey: null
2307
+ });
2308
+ return {
2309
+ actionsAmount: runtime.actions.length,
2310
+ endpointsAmount: runtime.remoteEndpointDefinitions.length,
2311
+ endpointTypes: endpointsInfo.endpointTypes,
2312
+ agentsAmount: endpointsInfo.agentsAmount,
2313
+ hashedLgcKey: endpointsInfo.hashedKey
2314
+ };
2315
+ }
2316
+ __name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
2317
+ var telemetry_client_default = telemetryClient;
2318
+
2072
2319
  // src/service-adapters/events.ts
2073
2320
  var RuntimeEventTypes;
2074
2321
  (function(RuntimeEventTypes2) {
@@ -2085,52 +2332,71 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2085
2332
  constructor() {
2086
2333
  super();
2087
2334
  }
2088
- sendTextMessageStart(messageId) {
2335
+ sendTextMessageStart({ messageId }) {
2089
2336
  this.next({
2090
2337
  type: "TextMessageStart",
2091
2338
  messageId
2092
2339
  });
2093
2340
  }
2094
- sendTextMessageContent(content) {
2341
+ sendTextMessageContent({ messageId, content }) {
2095
2342
  this.next({
2096
2343
  type: "TextMessageContent",
2097
- content
2344
+ content,
2345
+ messageId
2098
2346
  });
2099
2347
  }
2100
- sendTextMessageEnd() {
2348
+ sendTextMessageEnd({ messageId }) {
2101
2349
  this.next({
2102
- type: "TextMessageEnd"
2350
+ type: "TextMessageEnd",
2351
+ messageId
2103
2352
  });
2104
2353
  }
2105
2354
  sendTextMessage(messageId, content) {
2106
- this.sendTextMessageStart(messageId);
2107
- this.sendTextMessageContent(content);
2108
- this.sendTextMessageEnd();
2355
+ this.sendTextMessageStart({
2356
+ messageId
2357
+ });
2358
+ this.sendTextMessageContent({
2359
+ messageId,
2360
+ content
2361
+ });
2362
+ this.sendTextMessageEnd({
2363
+ messageId
2364
+ });
2109
2365
  }
2110
- sendActionExecutionStart(actionExecutionId, actionName) {
2366
+ sendActionExecutionStart({ actionExecutionId, actionName }) {
2111
2367
  this.next({
2112
2368
  type: "ActionExecutionStart",
2113
2369
  actionExecutionId,
2114
2370
  actionName
2115
2371
  });
2116
2372
  }
2117
- sendActionExecutionArgs(args) {
2373
+ sendActionExecutionArgs({ actionExecutionId, args }) {
2118
2374
  this.next({
2119
2375
  type: "ActionExecutionArgs",
2120
- args
2376
+ args,
2377
+ actionExecutionId
2121
2378
  });
2122
2379
  }
2123
- sendActionExecutionEnd() {
2380
+ sendActionExecutionEnd({ actionExecutionId }) {
2124
2381
  this.next({
2125
- type: "ActionExecutionEnd"
2382
+ type: "ActionExecutionEnd",
2383
+ actionExecutionId
2126
2384
  });
2127
2385
  }
2128
- sendActionExecution(actionExecutionId, toolName, args) {
2129
- this.sendActionExecutionStart(actionExecutionId, toolName);
2130
- this.sendActionExecutionArgs(args);
2131
- this.sendActionExecutionEnd();
2386
+ sendActionExecution({ actionExecutionId, actionName, args }) {
2387
+ this.sendActionExecutionStart({
2388
+ actionExecutionId,
2389
+ actionName
2390
+ });
2391
+ this.sendActionExecutionArgs({
2392
+ actionExecutionId,
2393
+ args
2394
+ });
2395
+ this.sendActionExecutionEnd({
2396
+ actionExecutionId
2397
+ });
2132
2398
  }
2133
- sendActionExecutionResult(actionExecutionId, actionName, result) {
2399
+ sendActionExecutionResult({ actionExecutionId, actionName, result }) {
2134
2400
  this.next({
2135
2401
  type: "ActionExecutionResult",
2136
2402
  actionName,
@@ -2138,7 +2404,7 @@ var RuntimeEventSubject = class extends import_rxjs2.ReplaySubject {
2138
2404
  result
2139
2405
  });
2140
2406
  }
2141
- sendAgentStateMessage(threadId, agentName, nodeName, runId, active, role, state, running) {
2407
+ sendAgentStateMessage({ threadId, agentName, nodeName, runId, active, role, state, running }) {
2142
2408
  this.next({
2143
2409
  type: "AgentStateMessage",
2144
2410
  threadId,
@@ -2159,9 +2425,20 @@ var RuntimeEventSource = class {
2159
2425
  async stream(callback) {
2160
2426
  this.callback = callback;
2161
2427
  }
2428
+ sendErrorMessageToChat() {
2429
+ const errorMessage = "\u274C An error occurred. Please try again.";
2430
+ if (!this.callback) {
2431
+ this.stream(async (eventStream$) => {
2432
+ eventStream$.sendTextMessage((0, import_shared8.randomId)(), errorMessage);
2433
+ });
2434
+ } else {
2435
+ this.eventStream$.sendTextMessage((0, import_shared8.randomId)(), errorMessage);
2436
+ }
2437
+ }
2162
2438
  processRuntimeEvents({ serverSideActions, guardrailsResult$, actionInputsWithoutAgents }) {
2163
2439
  this.callback(this.eventStream$).catch((error) => {
2164
2440
  console.error("Error in event source callback", error);
2441
+ this.sendErrorMessageToChat();
2165
2442
  });
2166
2443
  return this.eventStream$.pipe(
2167
2444
  // mark tools for server side execution
@@ -2233,7 +2510,11 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
2233
2510
  }
2234
2511
  }
2235
2512
  if (isLangGraphAgentAction(action)) {
2236
- eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
2513
+ eventStream$.sendActionExecutionResult({
2514
+ actionExecutionId,
2515
+ actionName: action.name,
2516
+ result: `${action.name} agent started`
2517
+ });
2237
2518
  const stream = await action.langGraphAgentHandler({
2238
2519
  name: action.name,
2239
2520
  actionInputsWithoutAgents
@@ -2404,37 +2685,37 @@ var CopilotRuntime = class {
2404
2685
  const remoteChain = new RemoteChain(chain);
2405
2686
  this.langserve.push(remoteChain.toAction());
2406
2687
  }
2407
- this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
2688
+ this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) ?? (params == null ? void 0 : params.remoteActions) ?? [];
2408
2689
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
2409
2690
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2410
2691
  }
2411
2692
  async processRuntimeRequest(request) {
2412
2693
  var _a;
2413
2694
  const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, agentSession, url } = request;
2414
- if (agentSession) {
2415
- return this.processAgentRequest(request);
2416
- }
2417
- const messages = rawMessages.filter((message) => !message.agentStateMessage);
2418
- const inputMessages = convertGqlInputToMessages(messages);
2419
- const serverSideActions = await this.getServerSideActions(request);
2420
- const serverSideActionsInput = serverSideActions.map((action) => ({
2421
- name: action.name,
2422
- description: action.description,
2423
- jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters))
2424
- }));
2425
- const actionInputs = flattenToolCallsNoDuplicates([
2426
- ...serverSideActionsInput,
2427
- ...clientSideActionsInput
2428
- ]);
2429
- await ((_a = this.onBeforeRequest) == null ? void 0 : _a.call(this, {
2430
- threadId,
2431
- runId,
2432
- inputMessages,
2433
- properties: graphqlContext.properties,
2434
- url
2435
- }));
2695
+ const eventSource = new RuntimeEventSource();
2436
2696
  try {
2437
- const eventSource = new RuntimeEventSource();
2697
+ if (agentSession) {
2698
+ return await this.processAgentRequest(request);
2699
+ }
2700
+ const messages = rawMessages.filter((message) => !message.agentStateMessage);
2701
+ const inputMessages = convertGqlInputToMessages(messages);
2702
+ const serverSideActions = await this.getServerSideActions(request);
2703
+ const serverSideActionsInput = serverSideActions.map((action) => ({
2704
+ name: action.name,
2705
+ description: action.description,
2706
+ jsonSchema: JSON.stringify((0, import_shared9.actionParametersToJsonSchema)(action.parameters))
2707
+ }));
2708
+ const actionInputs = flattenToolCallsNoDuplicates([
2709
+ ...serverSideActionsInput,
2710
+ ...clientSideActionsInput
2711
+ ]);
2712
+ await ((_a = this.onBeforeRequest) == null ? void 0 : _a.call(this, {
2713
+ threadId,
2714
+ runId,
2715
+ inputMessages,
2716
+ properties: graphqlContext.properties,
2717
+ url
2718
+ }));
2438
2719
  const result = await serviceAdapter.process({
2439
2720
  messages: inputMessages,
2440
2721
  actions: actionInputs,
@@ -2467,7 +2748,14 @@ var CopilotRuntime = class {
2467
2748
  };
2468
2749
  } catch (error) {
2469
2750
  console.error("Error getting response:", error);
2470
- throw error;
2751
+ eventSource.sendErrorMessageToChat();
2752
+ return {
2753
+ threadId: threadId || (0, import_shared9.randomId)(),
2754
+ runId: runId || (0, import_shared9.randomId)(),
2755
+ eventSource,
2756
+ serverSideActions: [],
2757
+ actionInputsWithoutAgents: []
2758
+ };
2471
2759
  }
2472
2760
  }
2473
2761
  async processAgentRequest(request) {
@@ -2483,7 +2771,7 @@ var CopilotRuntime = class {
2483
2771
  const serverSideActionsInput = serverSideActions.filter((action) => !isLangGraphAgentAction(action)).map((action) => ({
2484
2772
  name: action.name,
2485
2773
  description: action.description,
2486
- jsonSchema: JSON.stringify((0, import_shared8.actionParametersToJsonSchema)(action.parameters))
2774
+ jsonSchema: JSON.stringify((0, import_shared9.actionParametersToJsonSchema)(action.parameters))
2487
2775
  }));
2488
2776
  const actionInputsWithoutAgents = flattenToolCallsNoDuplicates([
2489
2777
  ...serverSideActionsInput,
@@ -2547,7 +2835,7 @@ var CopilotRuntime = class {
2547
2835
  }
2548
2836
  const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
2549
2837
  ...endpoint,
2550
- type: this.resolveEndpointType(endpoint)
2838
+ type: resolveEndpointType(endpoint)
2551
2839
  }));
2552
2840
  const remoteActions = await setupRemoteActions({
2553
2841
  remoteEndpointDefinitions,
@@ -2566,12 +2854,6 @@ var CopilotRuntime = class {
2566
2854
  ...remoteActions
2567
2855
  ];
2568
2856
  }
2569
- resolveEndpointType(endpoint) {
2570
- if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
2571
- return EndpointType.LangGraphCloud;
2572
- }
2573
- return endpoint.type;
2574
- }
2575
2857
  };
2576
2858
  __name(CopilotRuntime, "CopilotRuntime");
2577
2859
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -2600,6 +2882,17 @@ function langGraphCloudEndpoint(config2) {
2600
2882
  };
2601
2883
  }
2602
2884
  __name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
2885
+ function resolveEndpointType(endpoint) {
2886
+ if (!endpoint.type) {
2887
+ if ("langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
2888
+ return EndpointType.LangGraphCloud;
2889
+ } else {
2890
+ return EndpointType.CopilotKit;
2891
+ }
2892
+ }
2893
+ return endpoint.type;
2894
+ }
2895
+ __name(resolveEndpointType, "resolveEndpointType");
2603
2896
 
2604
2897
  // src/lib/integrations/shared.ts
2605
2898
  var import_type_graphql16 = require("type-graphql");
@@ -3613,7 +3906,7 @@ var UnknownErrorResponse = class extends FailedResponseStatus {
3613
3906
  __name(UnknownErrorResponse, "UnknownErrorResponse");
3614
3907
 
3615
3908
  // src/graphql/resolvers/copilot.resolver.ts
3616
- var import_shared9 = require("@copilotkit/shared");
3909
+ var import_shared10 = require("@copilotkit/shared");
3617
3910
  function _ts_decorate14(decorators, target, key, desc) {
3618
3911
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3619
3912
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -3730,7 +4023,7 @@ var CopilotResolver = class {
3730
4023
  rejectOutputMessagesPromise = reject;
3731
4024
  });
3732
4025
  logger2.debug("Processing");
3733
- const { eventSource, threadId = (0, import_shared9.randomId)(), runId, serverSideActions, actionInputsWithoutAgents } = await copilotRuntime.processRuntimeRequest({
4026
+ const { eventSource, threadId = (0, import_shared10.randomId)(), runId, serverSideActions, actionInputsWithoutAgents } = await copilotRuntime.processRuntimeRequest({
3734
4027
  serviceAdapter,
3735
4028
  messages: data.messages,
3736
4029
  actions: data.frontend.actions,
@@ -3775,7 +4068,7 @@ var CopilotResolver = class {
3775
4068
  });
3776
4069
  outputMessages = [
3777
4070
  (0, import_class_transformer2.plainToInstance)(TextMessage, {
3778
- id: (0, import_shared9.randomId)(),
4071
+ id: (0, import_shared10.randomId)(),
3779
4072
  createdAt: /* @__PURE__ */ new Date(),
3780
4073
  content: result.reason,
3781
4074
  role: MessageRole.assistant
@@ -3823,7 +4116,9 @@ var CopilotResolver = class {
3823
4116
  // skip until this message start event
3824
4117
  (0, import_rxjs4.skipWhile)((e) => e !== event),
3825
4118
  // take until the message end event
3826
- (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
4119
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.TextMessageEnd && e.messageId == event.messageId)),
4120
+ // filter out any other message events or message ids
4121
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.TextMessageContent && e.messageId == event.messageId)
3827
4122
  );
3828
4123
  const streamingTextStatus = new import_rxjs4.Subject();
3829
4124
  const messageId = event.messageId;
@@ -3887,7 +4182,13 @@ var CopilotResolver = class {
3887
4182
  break;
3888
4183
  case RuntimeEventTypes.ActionExecutionStart:
3889
4184
  logger2.debug("Action execution start event received");
3890
- const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs4.skipWhile)((e) => e !== event), (0, import_rxjs4.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
4185
+ const actionExecutionArgumentStream = eventStream.pipe(
4186
+ (0, import_rxjs4.skipWhile)((e) => e !== event),
4187
+ // take until the action execution end event
4188
+ (0, import_rxjs4.takeWhile)((e) => !(e.type === RuntimeEventTypes.ActionExecutionEnd && e.actionExecutionId == event.actionExecutionId)),
4189
+ // filter out any other action execution events or action execution ids
4190
+ (0, import_rxjs4.filter)((e) => e.type == RuntimeEventTypes.ActionExecutionArgs && e.actionExecutionId == event.actionExecutionId)
4191
+ );
3891
4192
  const streamingArgumentsStatus = new import_rxjs4.Subject();
3892
4193
  pushMessage({
3893
4194
  id: event.actionExecutionId,
@@ -3938,7 +4239,7 @@ var CopilotResolver = class {
3938
4239
  result: event.result
3939
4240
  }, "Action execution result event received");
3940
4241
  pushMessage({
3941
- id: (0, import_shared9.randomId)(),
4242
+ id: (0, import_shared10.randomId)(),
3942
4243
  status: new SuccessMessageStatus(),
3943
4244
  createdAt: /* @__PURE__ */ new Date(),
3944
4245
  actionExecutionId: event.actionExecutionId,
@@ -3946,7 +4247,7 @@ var CopilotResolver = class {
3946
4247
  result: event.result
3947
4248
  });
3948
4249
  outputMessages.push((0, import_class_transformer2.plainToInstance)(ResultMessage, {
3949
- id: (0, import_shared9.randomId)(),
4250
+ id: (0, import_shared10.randomId)(),
3950
4251
  createdAt: /* @__PURE__ */ new Date(),
3951
4252
  actionExecutionId: event.actionExecutionId,
3952
4253
  actionName: event.actionName,
@@ -3958,7 +4259,7 @@ var CopilotResolver = class {
3958
4259
  event
3959
4260
  }, "Agent message event received");
3960
4261
  pushMessage({
3961
- id: (0, import_shared9.randomId)(),
4262
+ id: (0, import_shared10.randomId)(),
3962
4263
  status: new SuccessMessageStatus(),
3963
4264
  threadId: event.threadId,
3964
4265
  agentName: event.agentName,
@@ -3971,7 +4272,7 @@ var CopilotResolver = class {
3971
4272
  createdAt: /* @__PURE__ */ new Date()
3972
4273
  });
3973
4274
  outputMessages.push((0, import_class_transformer2.plainToInstance)(AgentStateMessage, {
3974
- id: (0, import_shared9.randomId)(),
4275
+ id: (0, import_shared10.randomId)(),
3975
4276
  threadId: event.threadId,
3976
4277
  agentName: event.agentName,
3977
4278
  nodeName: event.nodeName,
@@ -4159,7 +4460,7 @@ function copilotRuntimeNextJSAppRouterEndpoint(options) {
4159
4460
  _copilotkit: options.properties._copilotkit
4160
4461
  });
4161
4462
  }
4162
- telemetry_client_default.capture("oss.runtime.instance_created", {});
4463
+ telemetry_client_default.capture("oss.runtime.instance_created", getRuntimeInstanceTelemetryInfo(options.runtime));
4163
4464
  const logger2 = commonConfig.logging;
4164
4465
  logger2.debug("Creating NextJS App Router endpoint");
4165
4466
  const yoga = (0, import_graphql_yoga2.createYoga)({
@@ -4198,7 +4499,7 @@ function copilotRuntimeNextJSPagesRouterEndpoint(options) {
4198
4499
  _copilotkit: options.properties._copilotkit
4199
4500
  });
4200
4501
  }
4201
- telemetry_client_default.capture("oss.runtime.instance_created", {});
4502
+ telemetry_client_default.capture("oss.runtime.instance_created", getRuntimeInstanceTelemetryInfo(options.runtime));
4202
4503
  const logger2 = commonConfig.logging;
4203
4504
  logger2.debug("Creating NextJS Pages Router endpoint");
4204
4505
  const yoga = (0, import_graphql_yoga3.createYoga)({
@@ -4224,7 +4525,7 @@ function copilotRuntimeNodeHttpEndpoint(options) {
4224
4525
  _copilotkit: options.properties._copilotkit
4225
4526
  });
4226
4527
  }
4227
- telemetry_client_default.capture("oss.runtime.instance_created", {});
4528
+ telemetry_client_default.capture("oss.runtime.instance_created", getRuntimeInstanceTelemetryInfo(options.runtime));
4228
4529
  const logger2 = commonConfig.logging;
4229
4530
  logger2.debug("Creating Node HTTP endpoint");
4230
4531
  const yoga = (0, import_graphql_yoga4.createYoga)({
@@ -4242,7 +4543,7 @@ function copilotRuntimeNodeExpressEndpoint(options) {
4242
4543
  framework: "node-express"
4243
4544
  }
4244
4545
  });
4245
- telemetry_client_default.capture("oss.runtime.instance_created", {});
4546
+ telemetry_client_default.capture("oss.runtime.instance_created", getRuntimeInstanceTelemetryInfo(options.runtime));
4246
4547
  return copilotRuntimeNodeHttpEndpoint(options);
4247
4548
  }
4248
4549
  __name(copilotRuntimeNodeExpressEndpoint, "copilotRuntimeNodeExpressEndpoint");
@@ -4254,7 +4555,7 @@ function copilotRuntimeNestEndpoint(options) {
4254
4555
  framework: "nest"
4255
4556
  }
4256
4557
  });
4257
- telemetry_client_default.capture("oss.runtime.instance_created", {});
4558
+ telemetry_client_default.capture("oss.runtime.instance_created", getRuntimeInstanceTelemetryInfo(options.runtime));
4258
4559
  return copilotRuntimeNodeHttpEndpoint(options);
4259
4560
  }
4260
4561
  __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
@@ -4278,6 +4579,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
4278
4579
  createContext,
4279
4580
  flattenToolCallsNoDuplicates,
4280
4581
  getCommonConfig,
4281
- langGraphCloudEndpoint
4582
+ langGraphCloudEndpoint,
4583
+ resolveEndpointType
4282
4584
  });
4283
4585
  //# sourceMappingURL=index.js.map