@copilotkit/runtime 0.37.0 → 0.38.0-beta.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 (96) hide show
  1. package/.turbo/turbo-build.log +69 -54
  2. package/CHANGELOG.md +11 -0
  3. package/__snapshots__/schema/schema.graphql +15 -4
  4. package/dist/{chunk-2CCVVJDU.mjs → chunk-2PJG3NAC.mjs} +13 -15
  5. package/dist/chunk-2PJG3NAC.mjs.map +1 -0
  6. package/dist/{chunk-NFCPM5AM.mjs → chunk-6NZ4UMOD.mjs} +4 -4
  7. package/dist/chunk-6NZ4UMOD.mjs.map +1 -0
  8. package/dist/{chunk-XPAUPJMW.mjs → chunk-6YGDE3YI.mjs} +432 -220
  9. package/dist/chunk-6YGDE3YI.mjs.map +1 -0
  10. package/dist/chunk-BYB2LNMK.mjs +152 -0
  11. package/dist/chunk-BYB2LNMK.mjs.map +1 -0
  12. package/dist/{chunk-7IFP53C6.mjs → chunk-FRK6BXXV.mjs} +49 -11
  13. package/dist/chunk-FRK6BXXV.mjs.map +1 -0
  14. package/dist/{chunk-5HGYI6EG.mjs → chunk-JBDOA7MK.mjs} +34 -15
  15. package/dist/chunk-JBDOA7MK.mjs.map +1 -0
  16. package/dist/{chunk-4UA4RB4C.mjs → chunk-JIKPSUGQ.mjs} +45 -76
  17. package/dist/chunk-JIKPSUGQ.mjs.map +1 -0
  18. package/dist/{chunk-BLTAUVRP.mjs → chunk-OZMCHYYR.mjs} +5 -3
  19. package/dist/{chunk-BLTAUVRP.mjs.map → chunk-OZMCHYYR.mjs.map} +1 -1
  20. package/dist/chunk-RHQLCJGG.mjs +7 -0
  21. package/dist/chunk-RHQLCJGG.mjs.map +1 -0
  22. package/dist/failed-response-status-reasons-0ab19e06.d.ts +49 -0
  23. package/dist/graphql/types/base/index.mjs +2 -1
  24. package/dist/graphql/types/converted/index.mjs +3 -2
  25. package/dist/{index-f0875df3.d.ts → index-0e75acd2.d.ts} +86 -59
  26. package/dist/index.d.ts +7 -4
  27. package/dist/index.js +536 -169
  28. package/dist/index.js.map +1 -1
  29. package/dist/index.mjs +16 -13
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/{langchain-adapter-9ce103f3.d.ts → langchain-adapter-a02d1d38.d.ts} +4 -4
  32. package/dist/{langserve-fd5066ee.d.ts → langserve-75ebbc38.d.ts} +25 -9
  33. package/dist/lib/cloud/index.d.ts +6 -0
  34. package/dist/lib/cloud/index.js +18 -0
  35. package/dist/lib/cloud/index.js.map +1 -0
  36. package/dist/lib/cloud/index.mjs +1 -0
  37. package/dist/lib/cloud/index.mjs.map +1 -0
  38. package/dist/lib/index.d.ts +6 -4
  39. package/dist/lib/index.js +530 -169
  40. package/dist/lib/index.js.map +1 -1
  41. package/dist/lib/index.mjs +9 -13
  42. package/dist/lib/integrations/index.d.ts +5 -3
  43. package/dist/lib/integrations/index.js +426 -64
  44. package/dist/lib/integrations/index.js.map +1 -1
  45. package/dist/lib/integrations/index.mjs +7 -5
  46. package/dist/lib/integrations/node-http/index.d.ts +4 -2
  47. package/dist/lib/integrations/node-http/index.js +416 -52
  48. package/dist/lib/integrations/node-http/index.js.map +1 -1
  49. package/dist/lib/integrations/node-http/index.mjs +6 -4
  50. package/dist/pages-router-e81920d5.d.ts +21 -0
  51. package/dist/service-adapters/index.d.ts +2 -2
  52. package/dist/service-adapters/index.js +82 -25
  53. package/dist/service-adapters/index.js.map +1 -1
  54. package/dist/service-adapters/index.mjs +5 -4
  55. package/dist/utils/index.d.ts +1 -0
  56. package/dist/utils/index.js +174 -0
  57. package/dist/utils/index.js.map +1 -0
  58. package/dist/utils/index.mjs +12 -0
  59. package/dist/utils/index.mjs.map +1 -0
  60. package/package.json +6 -4
  61. package/src/graphql/inputs/cloud-guardrails.input.ts +2 -5
  62. package/src/graphql/inputs/cloud.input.ts +2 -2
  63. package/src/graphql/resolvers/copilot.resolver.ts +340 -30
  64. package/src/graphql/types/response-status.type.ts +16 -2
  65. package/src/index.ts +1 -0
  66. package/src/lib/cloud/index.ts +4 -0
  67. package/src/lib/copilot-runtime.ts +116 -70
  68. package/src/lib/index.ts +0 -1
  69. package/src/lib/integrations/nextjs/app-router.ts +9 -17
  70. package/src/lib/integrations/nextjs/pages-router.ts +9 -15
  71. package/src/lib/integrations/node-http/index.ts +6 -14
  72. package/src/lib/integrations/shared.ts +38 -18
  73. package/src/lib/logger.ts +28 -0
  74. package/src/service-adapters/events.ts +20 -2
  75. package/src/service-adapters/experimental/groq/groq-adapter.ts +3 -1
  76. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +3 -1
  77. package/src/service-adapters/google/google-genai-adapter.ts +6 -1
  78. package/src/service-adapters/google/utils.ts +1 -1
  79. package/src/service-adapters/index.ts +1 -1
  80. package/src/service-adapters/langchain/langchain-adapter.ts +8 -9
  81. package/src/service-adapters/langchain/langserve.ts +10 -4
  82. package/src/service-adapters/langchain/utils.ts +58 -9
  83. package/src/service-adapters/openai/openai-adapter.ts +8 -7
  84. package/src/service-adapters/openai/openai-assistant-adapter.ts +6 -8
  85. package/src/service-adapters/service-adapter.ts +1 -2
  86. package/src/utils/failed-response-status-reasons.ts +48 -0
  87. package/src/utils/index.ts +1 -0
  88. package/dist/chunk-2CCVVJDU.mjs.map +0 -1
  89. package/dist/chunk-4UA4RB4C.mjs.map +0 -1
  90. package/dist/chunk-5HGYI6EG.mjs.map +0 -1
  91. package/dist/chunk-7IFP53C6.mjs.map +0 -1
  92. package/dist/chunk-NFCPM5AM.mjs.map +0 -1
  93. package/dist/chunk-XPAUPJMW.mjs.map +0 -1
  94. package/dist/pages-router-b6bc6c60.d.ts +0 -30
  95. package/src/lib/copilot-cloud.ts +0 -63
  96. package/src/lib/guardrails.ts +0 -3
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
8
  var __export = (target, all) => {
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/lib/integrations/index.ts
@@ -306,15 +316,10 @@ function _ts_metadata5(k, v) {
306
316
  }
307
317
  __name(_ts_metadata5, "_ts_metadata");
308
318
  var GuardrailsRuleInput = class {
309
- id;
310
319
  allowList = [];
311
320
  denyList = [];
312
321
  };
313
322
  __name(GuardrailsRuleInput, "GuardrailsRuleInput");
314
- _ts_decorate5([
315
- (0, import_type_graphql6.Field)(() => String),
316
- _ts_metadata5("design:type", String)
317
- ], GuardrailsRuleInput.prototype, "id", void 0);
318
323
  _ts_decorate5([
319
324
  (0, import_type_graphql6.Field)(() => [
320
325
  String
@@ -335,16 +340,17 @@ GuardrailsRuleInput = _ts_decorate5([
335
340
  (0, import_type_graphql6.InputType)()
336
341
  ], GuardrailsRuleInput);
337
342
  var GuardrailsInput = class {
338
- inputValidationRules = [];
343
+ inputValidationRules = {
344
+ allowList: [],
345
+ denyList: []
346
+ };
339
347
  };
340
348
  __name(GuardrailsInput, "GuardrailsInput");
341
349
  _ts_decorate5([
342
- (0, import_type_graphql6.Field)(() => [
343
- GuardrailsRuleInput
344
- ], {
350
+ (0, import_type_graphql6.Field)(() => GuardrailsRuleInput, {
345
351
  nullable: true
346
352
  }),
347
- _ts_metadata5("design:type", Array)
353
+ _ts_metadata5("design:type", typeof GuardrailsRuleInput === "undefined" ? Object : GuardrailsRuleInput)
348
354
  ], GuardrailsInput.prototype, "inputValidationRules", void 0);
349
355
  GuardrailsInput = _ts_decorate5([
350
356
  (0, import_type_graphql6.InputType)()
@@ -372,7 +378,9 @@ var CloudInput = class {
372
378
  };
373
379
  __name(CloudInput, "CloudInput");
374
380
  _ts_decorate6([
375
- (0, import_type_graphql7.Field)(() => GuardrailsInput),
381
+ (0, import_type_graphql7.Field)(() => GuardrailsInput, {
382
+ nullable: true
383
+ }),
376
384
  _ts_metadata6("design:type", typeof GuardrailsInput === "undefined" ? Object : GuardrailsInput)
377
385
  ], CloudInput.prototype, "guardrails", void 0);
378
386
  CloudInput = _ts_decorate6([
@@ -512,6 +520,7 @@ var MessageStatusUnion = (0, import_type_graphql9.createUnionType)({
512
520
  });
513
521
 
514
522
  // src/graphql/types/response-status.type.ts
523
+ var import_graphql_scalars = require("graphql-scalars");
515
524
  var import_type_graphql10 = require("type-graphql");
516
525
  function _ts_decorate9(decorators, target, key, desc) {
517
526
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -578,15 +587,31 @@ SuccessResponseStatus = _ts_decorate9([
578
587
  implements: BaseResponseStatus
579
588
  })
580
589
  ], SuccessResponseStatus);
590
+ var FailedResponseStatusReason;
591
+ (function(FailedResponseStatusReason2) {
592
+ FailedResponseStatusReason2["GUARDRAILS_VALIDATION_FAILED"] = "GUARDRAILS_VALIDATION_FAILED";
593
+ FailedResponseStatusReason2["MESSAGE_STREAM_INTERRUPTED"] = "MESSAGE_STREAM_INTERRUPTED";
594
+ FailedResponseStatusReason2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
595
+ })(FailedResponseStatusReason || (FailedResponseStatusReason = {}));
596
+ (0, import_type_graphql10.registerEnumType)(FailedResponseStatusReason, {
597
+ name: "FailedResponseStatusReason"
598
+ });
581
599
  var FailedResponseStatus = class extends BaseResponseStatus {
582
600
  code = "failed";
583
601
  reason;
602
+ details = null;
584
603
  };
585
604
  __name(FailedResponseStatus, "FailedResponseStatus");
586
605
  _ts_decorate9([
587
- (0, import_type_graphql10.Field)(() => String),
606
+ (0, import_type_graphql10.Field)(() => FailedResponseStatusReason),
588
607
  _ts_metadata9("design:type", String)
589
608
  ], FailedResponseStatus.prototype, "reason", void 0);
609
+ _ts_decorate9([
610
+ (0, import_type_graphql10.Field)(() => import_graphql_scalars.GraphQLJSON, {
611
+ nullable: true
612
+ }),
613
+ _ts_metadata9("design:type", typeof Record === "undefined" ? Object : Record)
614
+ ], FailedResponseStatus.prototype, "details", void 0);
590
615
  FailedResponseStatus = _ts_decorate9([
591
616
  (0, import_type_graphql10.ObjectType)({
592
617
  implements: BaseResponseStatus
@@ -754,6 +779,27 @@ var import_nanoid = require("nanoid");
754
779
 
755
780
  // src/service-adapters/events.ts
756
781
  var import_rxjs = require("rxjs");
782
+
783
+ // src/graphql/types/converted/index.ts
784
+ var TextMessage = class extends BaseMessage {
785
+ content;
786
+ role;
787
+ };
788
+ __name(TextMessage, "TextMessage");
789
+ var ActionExecutionMessage = class extends BaseMessage {
790
+ name;
791
+ arguments;
792
+ scope;
793
+ };
794
+ __name(ActionExecutionMessage, "ActionExecutionMessage");
795
+ var ResultMessage = class extends BaseMessage {
796
+ actionExecutionId;
797
+ actionName;
798
+ result;
799
+ };
800
+ __name(ResultMessage, "ResultMessage");
801
+
802
+ // src/service-adapters/events.ts
757
803
  var RuntimeEventTypes;
758
804
  (function(RuntimeEventTypes2) {
759
805
  RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
@@ -766,7 +812,44 @@ var RuntimeEventTypes;
766
812
  })(RuntimeEventTypes || (RuntimeEventTypes = {}));
767
813
 
768
814
  // src/graphql/resolvers/copilot.resolver.ts
769
- var import_graphql_scalars = require("graphql-scalars");
815
+ var import_graphql_scalars2 = require("graphql-scalars");
816
+ var import_class_transformer = require("class-transformer");
817
+ var import_graphql = require("graphql");
818
+
819
+ // src/utils/failed-response-status-reasons.ts
820
+ var GuardrailsValidationFailureResponse = class extends FailedResponseStatus {
821
+ reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
822
+ constructor({ guardrailsReason }) {
823
+ super();
824
+ this.details = {
825
+ guardrailsReason
826
+ };
827
+ }
828
+ };
829
+ __name(GuardrailsValidationFailureResponse, "GuardrailsValidationFailureResponse");
830
+ var MessageStreamInterruptedResponse = class extends FailedResponseStatus {
831
+ reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
832
+ constructor({ messageId }) {
833
+ super();
834
+ this.details = {
835
+ messageId,
836
+ description: "Check the message for mode details"
837
+ };
838
+ }
839
+ };
840
+ __name(MessageStreamInterruptedResponse, "MessageStreamInterruptedResponse");
841
+ var UnknownErrorResponse = class extends FailedResponseStatus {
842
+ reason = FailedResponseStatusReason.UNKNOWN_ERROR;
843
+ constructor({ description }) {
844
+ super();
845
+ this.details = {
846
+ description
847
+ };
848
+ }
849
+ };
850
+ __name(UnknownErrorResponse, "UnknownErrorResponse");
851
+
852
+ // src/graphql/resolvers/copilot.resolver.ts
770
853
  function _ts_decorate11(decorators, target, key, desc) {
771
854
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
772
855
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -789,39 +872,173 @@ function _ts_param(paramIndex, decorator) {
789
872
  };
790
873
  }
791
874
  __name(_ts_param, "_ts_param");
875
+ var invokeGuardrails = /* @__PURE__ */ __name(async ({ baseUrl, copilotCloudPublicApiKey, data, onResult, onError }) => {
876
+ var _a;
877
+ if (data.messages.length && ((_a = data.messages[data.messages.length - 1].textMessage) == null ? void 0 : _a.role) === MessageRole.user) {
878
+ const messages = data.messages.filter((m) => m.textMessage !== void 0 && (m.textMessage.role === MessageRole.user || m.textMessage.role === MessageRole.assistant)).map((m) => ({
879
+ role: m.textMessage.role,
880
+ content: m.textMessage.content
881
+ }));
882
+ const lastMessage = messages[messages.length - 1];
883
+ const restOfMessages = messages.slice(0, -1);
884
+ const body = {
885
+ input: lastMessage.content,
886
+ validTopics: data.cloud.guardrails.inputValidationRules.allowList,
887
+ invalidTopics: data.cloud.guardrails.inputValidationRules.denyList,
888
+ messages: restOfMessages
889
+ };
890
+ const guardrailsResult = await fetch(`${baseUrl}/guardrails/validate`, {
891
+ method: "POST",
892
+ headers: {
893
+ "Content-Type": "application/json",
894
+ "X-CopilotCloud-Public-API-Key": copilotCloudPublicApiKey
895
+ },
896
+ body: JSON.stringify(body)
897
+ });
898
+ if (guardrailsResult.ok) {
899
+ const resultJson = await guardrailsResult.json();
900
+ onResult(resultJson);
901
+ } else {
902
+ onError(await guardrailsResult.json());
903
+ }
904
+ }
905
+ }, "invokeGuardrails");
792
906
  var CopilotResolver = class {
793
907
  async hello() {
794
908
  return "Hello World";
795
909
  }
796
910
  async generateCopilotResponse(ctx, data, properties) {
911
+ let logger2 = ctx.logger.child({
912
+ component: "CopilotResolver.generateCopilotResponse"
913
+ });
914
+ logger2.debug({
915
+ data
916
+ }, "Generating Copilot response");
917
+ const copilotRuntime = ctx._copilotkit.runtime;
918
+ const serviceAdapter = ctx._copilotkit.serviceAdapter;
797
919
  if (properties) {
798
- ctx._copilotkit.properties = {
799
- ...ctx._copilotkit.properties,
920
+ logger2.debug("Properties provided, merging with context properties");
921
+ ctx.properties = {
922
+ ...ctx.properties,
800
923
  ...properties
801
924
  };
802
925
  }
803
- const copilotRuntime = ctx._copilotkit.runtime;
804
- const serviceAdapter = ctx._copilotkit.serviceAdapter;
805
- const responseStatus = new import_rxjs2.Subject();
806
- const { eventSource, threadId = (0, import_nanoid.nanoid)(), runId } = await copilotRuntime.process({
926
+ let copilotCloudPublicApiKey = null;
927
+ let copilotCloudBaseUrl;
928
+ if (data.cloud) {
929
+ logger2 = logger2.child({
930
+ cloud: true
931
+ });
932
+ logger2.debug("Cloud configuration provided, checking for public API key in headers");
933
+ const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
934
+ if (key) {
935
+ logger2.debug("Public API key found in headers");
936
+ copilotCloudPublicApiKey = key;
937
+ } else {
938
+ logger2.error("Public API key not found in headers");
939
+ throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
940
+ }
941
+ if (process.env.COPILOT_CLOUD_BASE_URL) {
942
+ copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
943
+ } else if (ctx._copilotkit.baseUrl) {
944
+ copilotCloudBaseUrl = ctx._copilotkit.baseUrl;
945
+ } else {
946
+ copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
947
+ }
948
+ logger2 = logger2.child({
949
+ copilotCloudBaseUrl
950
+ });
951
+ }
952
+ logger2.debug("Setting up subjects");
953
+ const responseStatus$ = new import_rxjs2.ReplaySubject();
954
+ const interruptStreaming$ = new import_rxjs2.ReplaySubject();
955
+ const guardrailsResult$ = new import_rxjs2.ReplaySubject();
956
+ let outputMessages = [];
957
+ let resolveOutputMessagesPromise;
958
+ let rejectOutputMessagesPromise;
959
+ const outputMessagesPromise = new Promise((resolve, reject) => {
960
+ resolveOutputMessagesPromise = resolve;
961
+ rejectOutputMessagesPromise = reject;
962
+ });
963
+ logger2.debug("Processing");
964
+ const { eventSource, threadId = (0, import_nanoid.nanoid)(), runId, actions } = await copilotRuntime.process({
807
965
  serviceAdapter,
808
966
  messages: data.messages,
809
967
  actions: data.frontend.actions,
810
968
  threadId: data.threadId,
811
969
  runId: data.runId,
812
- publicApiKey: void 0
970
+ publicApiKey: void 0,
971
+ properties: ctx.properties || {},
972
+ outputMessagesPromise
813
973
  });
974
+ logger2.debug("Event source created, creating response");
814
975
  const response = {
815
976
  threadId,
816
977
  runId,
817
- status: (0, import_rxjs2.firstValueFrom)(responseStatus),
978
+ status: (0, import_rxjs2.firstValueFrom)(responseStatus$),
818
979
  messages: new import_graphql_yoga.Repeater(async (pushMessage, stopStreamingMessages) => {
819
- const eventStream = eventSource.process(copilotRuntime.actions).pipe(
980
+ var _a, _b;
981
+ logger2.debug("Messages repeater created");
982
+ if ((_a = data.cloud) == null ? void 0 : _a.guardrails) {
983
+ logger2 = logger2.child({
984
+ guardrails: true
985
+ });
986
+ logger2.debug("Guardrails is enabled, validating input");
987
+ invokeGuardrails({
988
+ baseUrl: copilotCloudBaseUrl,
989
+ copilotCloudPublicApiKey,
990
+ data,
991
+ onResult: (result) => {
992
+ logger2.debug({
993
+ status: result.status
994
+ }, "Guardrails validation done");
995
+ guardrailsResult$.next(result);
996
+ if (result.status === "denied") {
997
+ responseStatus$.next(new GuardrailsValidationFailureResponse({
998
+ guardrailsReason: result.reason
999
+ }));
1000
+ interruptStreaming$.next({
1001
+ reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
1002
+ });
1003
+ outputMessages = [
1004
+ (0, import_class_transformer.plainToInstance)(TextMessage, {
1005
+ id: (0, import_nanoid.nanoid)(),
1006
+ createdAt: /* @__PURE__ */ new Date(),
1007
+ content: result.reason,
1008
+ role: MessageRole.assistant
1009
+ })
1010
+ ];
1011
+ resolveOutputMessagesPromise(outputMessages);
1012
+ }
1013
+ },
1014
+ onError: (err) => {
1015
+ logger2.error({
1016
+ err
1017
+ }, "Error in guardrails validation");
1018
+ responseStatus$.next(new UnknownErrorResponse({
1019
+ description: `An unknown error has occurred in the guardrails validation`
1020
+ }));
1021
+ interruptStreaming$.next({
1022
+ reason: `Interrupted due to unknown error in guardrails validation`
1023
+ });
1024
+ rejectOutputMessagesPromise(err);
1025
+ }
1026
+ });
1027
+ }
1028
+ let eventStreamSubscription;
1029
+ const eventStream = eventSource.process({
1030
+ serversideActions: actions,
1031
+ guardrailsResult$: ((_b = data.cloud) == null ? void 0 : _b.guardrails) ? guardrailsResult$ : null
1032
+ }).pipe(
820
1033
  // shareReplay() ensures that later subscribers will see the whole stream instead of
821
1034
  // just the events that were emitted after the subscriber was added.
822
- (0, import_rxjs2.shareReplay)()
1035
+ (0, import_rxjs2.shareReplay)(),
1036
+ (0, import_rxjs2.finalize)(() => {
1037
+ logger2.debug("Event stream finalized");
1038
+ })
823
1039
  );
824
- eventStream.subscribe({
1040
+ logger2.debug("Event stream created, subscribing to event stream");
1041
+ eventStreamSubscription = eventStream.subscribe({
825
1042
  next: async (event) => {
826
1043
  switch (event.type) {
827
1044
  case RuntimeEventTypes.TextMessageStart:
@@ -832,23 +1049,67 @@ var CopilotResolver = class {
832
1049
  (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
833
1050
  );
834
1051
  const streamingTextStatus = new import_rxjs2.Subject();
1052
+ const messageId = (0, import_nanoid.nanoid)();
835
1053
  pushMessage({
836
- id: (0, import_nanoid.nanoid)(),
1054
+ id: messageId,
837
1055
  status: (0, import_rxjs2.firstValueFrom)(streamingTextStatus),
838
1056
  createdAt: /* @__PURE__ */ new Date(),
839
1057
  role: MessageRole.assistant,
840
1058
  content: new import_graphql_yoga.Repeater(async (pushTextChunk, stopStreamingText) => {
841
- await textMessageContentStream.forEach(async (e) => {
842
- if (e.type == RuntimeEventTypes.TextMessageContent) {
843
- await pushTextChunk(e.content);
1059
+ logger2.debug("Text message content repeater created");
1060
+ const textChunks = [];
1061
+ let textSubscription;
1062
+ interruptStreaming$.pipe((0, import_rxjs2.shareReplay)(), (0, import_rxjs2.take)(1), (0, import_rxjs2.tap)(({ reason, messageId: messageId2 }) => {
1063
+ logger2.debug({
1064
+ reason,
1065
+ messageId: messageId2
1066
+ }, "Text streaming interrupted");
1067
+ streamingTextStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1068
+ reason
1069
+ }));
1070
+ responseStatus$.next(new MessageStreamInterruptedResponse({
1071
+ messageId: messageId2
1072
+ }));
1073
+ stopStreamingText();
1074
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1075
+ })).subscribe();
1076
+ logger2.debug("Subscribing to text message content stream");
1077
+ textSubscription = textMessageContentStream.subscribe({
1078
+ next: async (e) => {
1079
+ if (e.type == RuntimeEventTypes.TextMessageContent) {
1080
+ await pushTextChunk(e.content);
1081
+ textChunks.push(e.content);
1082
+ }
1083
+ },
1084
+ error: (err) => {
1085
+ logger2.error({
1086
+ err
1087
+ }, "Error in text message content stream");
1088
+ interruptStreaming$.next({
1089
+ reason: "Error streaming message content",
1090
+ messageId
1091
+ });
1092
+ stopStreamingText();
1093
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1094
+ },
1095
+ complete: () => {
1096
+ logger2.debug("Text message content stream completed");
1097
+ streamingTextStatus.next(new SuccessMessageStatus());
1098
+ stopStreamingText();
1099
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1100
+ outputMessages.push((0, import_class_transformer.plainToInstance)(TextMessage, {
1101
+ id: messageId,
1102
+ createdAt: /* @__PURE__ */ new Date(),
1103
+ content: textChunks.join(""),
1104
+ role: MessageRole.assistant
1105
+ }));
844
1106
  }
845
1107
  });
846
- stopStreamingText();
847
- streamingTextStatus.next(new SuccessMessageStatus());
848
1108
  })
849
1109
  });
850
1110
  break;
851
1111
  case RuntimeEventTypes.ActionExecutionStart:
1112
+ logger2.debug("Action execution start event received");
852
1113
  const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs2.skipWhile)((e) => e !== event), (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
853
1114
  const streamingArgumentsStatus = new import_rxjs2.Subject();
854
1115
  pushMessage({
@@ -858,17 +1119,47 @@ var CopilotResolver = class {
858
1119
  name: event.actionName,
859
1120
  scope: event.scope,
860
1121
  arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
861
- await actionExecutionArgumentStream.forEach(async (e) => {
862
- if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
863
- await pushArgumentsChunk(e.args);
1122
+ logger2.debug("Action execution argument stream created");
1123
+ const argumentChunks = [];
1124
+ let actionExecutionArgumentSubscription;
1125
+ actionExecutionArgumentSubscription = actionExecutionArgumentStream.subscribe({
1126
+ next: async (e) => {
1127
+ if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
1128
+ await pushArgumentsChunk(e.args);
1129
+ argumentChunks.push(e.args);
1130
+ }
1131
+ },
1132
+ error: (err) => {
1133
+ logger2.error({
1134
+ err
1135
+ }, "Error in action execution argument stream");
1136
+ streamingArgumentsStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1137
+ reason: "An unknown error has occurred in the action execution argument stream"
1138
+ }));
1139
+ stopStreamingArguments();
1140
+ actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
1141
+ },
1142
+ complete: () => {
1143
+ logger2.debug("Action execution argument stream completed");
1144
+ streamingArgumentsStatus.next(new SuccessMessageStatus());
1145
+ stopStreamingArguments();
1146
+ actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
1147
+ outputMessages.push((0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
1148
+ id: event.actionExecutionId,
1149
+ createdAt: /* @__PURE__ */ new Date(),
1150
+ name: event.actionName,
1151
+ scope: event.scope,
1152
+ arguments: argumentChunks.join("")
1153
+ }));
864
1154
  }
865
1155
  });
866
- stopStreamingArguments();
867
- streamingArgumentsStatus.next(new SuccessMessageStatus());
868
1156
  })
869
1157
  });
870
1158
  break;
871
1159
  case RuntimeEventTypes.ActionExecutionResult:
1160
+ logger2.debug({
1161
+ result: event.result
1162
+ }, "Action execution result event received");
872
1163
  pushMessage({
873
1164
  id: (0, import_nanoid.nanoid)(),
874
1165
  status: new SuccessMessageStatus(),
@@ -877,13 +1168,38 @@ var CopilotResolver = class {
877
1168
  actionName: event.actionName,
878
1169
  result: event.result
879
1170
  });
1171
+ outputMessages.push((0, import_class_transformer.plainToInstance)(ResultMessage, {
1172
+ id: (0, import_nanoid.nanoid)(),
1173
+ createdAt: /* @__PURE__ */ new Date(),
1174
+ actionExecutionId: event.actionExecutionId,
1175
+ actionName: event.actionName,
1176
+ result: event.result
1177
+ }));
880
1178
  break;
881
1179
  }
882
1180
  },
883
- error: (err) => console.error("Error in event source", err),
884
- complete: () => {
885
- responseStatus.next(new SuccessResponseStatus());
1181
+ error: (err) => {
1182
+ logger2.error({
1183
+ err
1184
+ }, "Error in event stream");
1185
+ responseStatus$.next(new UnknownErrorResponse({
1186
+ description: `An unknown error has occurred in the event stream`
1187
+ }));
1188
+ eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
1189
+ stopStreamingMessages();
1190
+ rejectOutputMessagesPromise(err);
1191
+ },
1192
+ complete: async () => {
1193
+ var _a2;
1194
+ logger2.debug("Event stream completed");
1195
+ if ((_a2 = data.cloud) == null ? void 0 : _a2.guardrails) {
1196
+ logger2.debug("Guardrails is enabled, waiting for guardrails result");
1197
+ await (0, import_rxjs2.firstValueFrom)(guardrailsResult$);
1198
+ }
1199
+ responseStatus$.next(new SuccessResponseStatus());
1200
+ eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
886
1201
  stopStreamingMessages();
1202
+ resolveOutputMessagesPromise(outputMessages);
887
1203
  }
888
1204
  });
889
1205
  })
@@ -902,7 +1218,7 @@ _ts_decorate11([
902
1218
  (0, import_type_graphql12.Mutation)(() => CopilotResponse),
903
1219
  _ts_param(0, (0, import_type_graphql12.Ctx)()),
904
1220
  _ts_param(1, (0, import_type_graphql12.Arg)("data")),
905
- _ts_param(2, (0, import_type_graphql12.Arg)("properties", () => import_graphql_scalars.GraphQLJSONObject, {
1221
+ _ts_param(2, (0, import_type_graphql12.Arg)("properties", () => import_graphql_scalars2.GraphQLJSONObject, {
906
1222
  nullable: true
907
1223
  })),
908
1224
  _ts_metadata11("design:type", Function),
@@ -919,51 +1235,99 @@ CopilotResolver = _ts_decorate11([
919
1235
 
920
1236
  // src/lib/integrations/shared.ts
921
1237
  var import_plugin_defer_stream = require("@graphql-yoga/plugin-defer-stream");
922
- async function createContext(initialContext, copilotKitContext) {
1238
+
1239
+ // src/lib/logger.ts
1240
+ var import_pino = __toESM(require("pino"));
1241
+ var import_pino_pretty = __toESM(require("pino-pretty"));
1242
+ function createLogger(options) {
1243
+ const { level, component } = options || {};
1244
+ const stream = (0, import_pino_pretty.default)({
1245
+ colorize: true
1246
+ });
1247
+ const logger2 = (0, import_pino.default)({
1248
+ level: process.env.LOG_LEVEL || level || "error",
1249
+ redact: {
1250
+ paths: [
1251
+ "pid",
1252
+ "hostname"
1253
+ ],
1254
+ remove: true
1255
+ }
1256
+ }, stream);
1257
+ if (component) {
1258
+ return logger2.child({
1259
+ component
1260
+ });
1261
+ } else {
1262
+ return logger2;
1263
+ }
1264
+ }
1265
+ __name(createLogger, "createLogger");
1266
+
1267
+ // src/lib/integrations/shared.ts
1268
+ var logger = createLogger();
1269
+ async function createContext(initialContext, copilotKitContext, contextLogger, properties = {}) {
1270
+ logger.debug({
1271
+ copilotKitContext
1272
+ }, "Creating GraphQL context");
923
1273
  const ctx = {
924
1274
  ...initialContext,
925
1275
  _copilotkit: {
926
1276
  ...copilotKitContext
927
- }
1277
+ },
1278
+ properties: {
1279
+ ...properties
1280
+ },
1281
+ logger: contextLogger
928
1282
  };
929
1283
  return ctx;
930
1284
  }
931
1285
  __name(createContext, "createContext");
932
1286
  function buildSchema(options = {}) {
1287
+ logger.debug("Building GraphQL schema...");
933
1288
  const schema = (0, import_type_graphql13.buildSchemaSync)({
934
1289
  resolvers: [
935
1290
  CopilotResolver
936
1291
  ],
937
1292
  emitSchemaFile: options.emitSchemaFile
938
1293
  });
1294
+ logger.debug("GraphQL schema built successfully");
939
1295
  return schema;
940
1296
  }
941
1297
  __name(buildSchema, "buildSchema");
942
1298
  function getCommonConfig(options) {
1299
+ const logLevel = process.env.LOG_LEVEL || options.logLevel || "error";
1300
+ const logger2 = createLogger({
1301
+ level: logLevel,
1302
+ component: "getCommonConfig"
1303
+ });
1304
+ logger2.debug("Getting common config");
1305
+ const contextLogger = createLogger({
1306
+ level: logLevel
1307
+ });
943
1308
  return {
1309
+ logging: createLogger({
1310
+ component: "Yoga GraphQL",
1311
+ level: logLevel
1312
+ }),
944
1313
  schema: buildSchema(),
945
1314
  plugins: [
946
1315
  (0, import_plugin_defer_stream.useDeferStream)()
947
1316
  ],
948
- context: (ctx) => createContext(ctx, {
949
- runtime: options.runtime,
950
- serviceAdapter: options.serviceAdapter,
951
- properties: {}
952
- })
1317
+ context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
953
1318
  };
954
1319
  }
955
1320
  __name(getCommonConfig, "getCommonConfig");
956
1321
 
957
1322
  // src/lib/integrations/nextjs/app-router.ts
958
1323
  var import_graphql_yoga2 = require("graphql-yoga");
959
- function copilotRuntimeNextJSAppRouterEndpoint({ runtime, endpoint, serviceAdapter }) {
960
- const commonConfig = getCommonConfig({
961
- runtime,
962
- serviceAdapter
963
- });
1324
+ function copilotRuntimeNextJSAppRouterEndpoint(options) {
1325
+ const commonConfig = getCommonConfig(options);
1326
+ const logger2 = commonConfig.logging;
1327
+ logger2.debug("Creating NextJS App Router endpoint");
964
1328
  const yoga = (0, import_graphql_yoga2.createYoga)({
965
1329
  ...commonConfig,
966
- graphqlEndpoint: endpoint,
1330
+ graphqlEndpoint: options.endpoint,
967
1331
  fetchAPI: {
968
1332
  Response: globalThis.Response
969
1333
  }
@@ -984,14 +1348,13 @@ var config = {
984
1348
  bodyParser: false
985
1349
  }
986
1350
  };
987
- function copilotRuntimeNextJSPagesRouterEndpoint({ runtime, endpoint, serviceAdapter }) {
988
- const commonConfig = getCommonConfig({
989
- runtime,
990
- serviceAdapter
991
- });
1351
+ function copilotRuntimeNextJSPagesRouterEndpoint(options) {
1352
+ const commonConfig = getCommonConfig(options);
1353
+ const logger2 = commonConfig.logging;
1354
+ logger2.debug("Creating NextJS Pages Router endpoint");
992
1355
  const yoga = (0, import_graphql_yoga3.createYoga)({
993
1356
  ...commonConfig,
994
- graphqlEndpoint: endpoint
1357
+ graphqlEndpoint: options.endpoint
995
1358
  });
996
1359
  return yoga;
997
1360
  }
@@ -999,14 +1362,13 @@ __name(copilotRuntimeNextJSPagesRouterEndpoint, "copilotRuntimeNextJSPagesRouter
999
1362
 
1000
1363
  // src/lib/integrations/node-http/index.ts
1001
1364
  var import_graphql_yoga4 = require("graphql-yoga");
1002
- function copilotRuntimeNodeHttpEndpoint({ runtime, endpoint, serviceAdapter }) {
1003
- const commonConfig = getCommonConfig({
1004
- runtime,
1005
- serviceAdapter
1006
- });
1365
+ function copilotRuntimeNodeHttpEndpoint(options) {
1366
+ const commonConfig = getCommonConfig(options);
1367
+ const logger2 = commonConfig.logging;
1368
+ logger2.debug("Creating Node HTTP endpoint");
1007
1369
  const yoga = (0, import_graphql_yoga4.createYoga)({
1008
1370
  ...commonConfig,
1009
- graphqlEndpoint: endpoint
1371
+ graphqlEndpoint: options.endpoint
1010
1372
  });
1011
1373
  return yoga;
1012
1374
  }