@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/node-http/index.ts
@@ -301,15 +311,10 @@ function _ts_metadata5(k, v) {
301
311
  }
302
312
  __name(_ts_metadata5, "_ts_metadata");
303
313
  var GuardrailsRuleInput = class {
304
- id;
305
314
  allowList = [];
306
315
  denyList = [];
307
316
  };
308
317
  __name(GuardrailsRuleInput, "GuardrailsRuleInput");
309
- _ts_decorate5([
310
- (0, import_type_graphql6.Field)(() => String),
311
- _ts_metadata5("design:type", String)
312
- ], GuardrailsRuleInput.prototype, "id", void 0);
313
318
  _ts_decorate5([
314
319
  (0, import_type_graphql6.Field)(() => [
315
320
  String
@@ -330,16 +335,17 @@ GuardrailsRuleInput = _ts_decorate5([
330
335
  (0, import_type_graphql6.InputType)()
331
336
  ], GuardrailsRuleInput);
332
337
  var GuardrailsInput = class {
333
- inputValidationRules = [];
338
+ inputValidationRules = {
339
+ allowList: [],
340
+ denyList: []
341
+ };
334
342
  };
335
343
  __name(GuardrailsInput, "GuardrailsInput");
336
344
  _ts_decorate5([
337
- (0, import_type_graphql6.Field)(() => [
338
- GuardrailsRuleInput
339
- ], {
345
+ (0, import_type_graphql6.Field)(() => GuardrailsRuleInput, {
340
346
  nullable: true
341
347
  }),
342
- _ts_metadata5("design:type", Array)
348
+ _ts_metadata5("design:type", typeof GuardrailsRuleInput === "undefined" ? Object : GuardrailsRuleInput)
343
349
  ], GuardrailsInput.prototype, "inputValidationRules", void 0);
344
350
  GuardrailsInput = _ts_decorate5([
345
351
  (0, import_type_graphql6.InputType)()
@@ -367,7 +373,9 @@ var CloudInput = class {
367
373
  };
368
374
  __name(CloudInput, "CloudInput");
369
375
  _ts_decorate6([
370
- (0, import_type_graphql7.Field)(() => GuardrailsInput),
376
+ (0, import_type_graphql7.Field)(() => GuardrailsInput, {
377
+ nullable: true
378
+ }),
371
379
  _ts_metadata6("design:type", typeof GuardrailsInput === "undefined" ? Object : GuardrailsInput)
372
380
  ], CloudInput.prototype, "guardrails", void 0);
373
381
  CloudInput = _ts_decorate6([
@@ -507,6 +515,7 @@ var MessageStatusUnion = (0, import_type_graphql9.createUnionType)({
507
515
  });
508
516
 
509
517
  // src/graphql/types/response-status.type.ts
518
+ var import_graphql_scalars = require("graphql-scalars");
510
519
  var import_type_graphql10 = require("type-graphql");
511
520
  function _ts_decorate9(decorators, target, key, desc) {
512
521
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -573,15 +582,31 @@ SuccessResponseStatus = _ts_decorate9([
573
582
  implements: BaseResponseStatus
574
583
  })
575
584
  ], SuccessResponseStatus);
585
+ var FailedResponseStatusReason;
586
+ (function(FailedResponseStatusReason2) {
587
+ FailedResponseStatusReason2["GUARDRAILS_VALIDATION_FAILED"] = "GUARDRAILS_VALIDATION_FAILED";
588
+ FailedResponseStatusReason2["MESSAGE_STREAM_INTERRUPTED"] = "MESSAGE_STREAM_INTERRUPTED";
589
+ FailedResponseStatusReason2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
590
+ })(FailedResponseStatusReason || (FailedResponseStatusReason = {}));
591
+ (0, import_type_graphql10.registerEnumType)(FailedResponseStatusReason, {
592
+ name: "FailedResponseStatusReason"
593
+ });
576
594
  var FailedResponseStatus = class extends BaseResponseStatus {
577
595
  code = "failed";
578
596
  reason;
597
+ details = null;
579
598
  };
580
599
  __name(FailedResponseStatus, "FailedResponseStatus");
581
600
  _ts_decorate9([
582
- (0, import_type_graphql10.Field)(() => String),
601
+ (0, import_type_graphql10.Field)(() => FailedResponseStatusReason),
583
602
  _ts_metadata9("design:type", String)
584
603
  ], FailedResponseStatus.prototype, "reason", void 0);
604
+ _ts_decorate9([
605
+ (0, import_type_graphql10.Field)(() => import_graphql_scalars.GraphQLJSON, {
606
+ nullable: true
607
+ }),
608
+ _ts_metadata9("design:type", typeof Record === "undefined" ? Object : Record)
609
+ ], FailedResponseStatus.prototype, "details", void 0);
585
610
  FailedResponseStatus = _ts_decorate9([
586
611
  (0, import_type_graphql10.ObjectType)({
587
612
  implements: BaseResponseStatus
@@ -749,6 +774,27 @@ var import_nanoid = require("nanoid");
749
774
 
750
775
  // src/service-adapters/events.ts
751
776
  var import_rxjs = require("rxjs");
777
+
778
+ // src/graphql/types/converted/index.ts
779
+ var TextMessage = class extends BaseMessage {
780
+ content;
781
+ role;
782
+ };
783
+ __name(TextMessage, "TextMessage");
784
+ var ActionExecutionMessage = class extends BaseMessage {
785
+ name;
786
+ arguments;
787
+ scope;
788
+ };
789
+ __name(ActionExecutionMessage, "ActionExecutionMessage");
790
+ var ResultMessage = class extends BaseMessage {
791
+ actionExecutionId;
792
+ actionName;
793
+ result;
794
+ };
795
+ __name(ResultMessage, "ResultMessage");
796
+
797
+ // src/service-adapters/events.ts
752
798
  var RuntimeEventTypes;
753
799
  (function(RuntimeEventTypes2) {
754
800
  RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
@@ -761,7 +807,44 @@ var RuntimeEventTypes;
761
807
  })(RuntimeEventTypes || (RuntimeEventTypes = {}));
762
808
 
763
809
  // src/graphql/resolvers/copilot.resolver.ts
764
- var import_graphql_scalars = require("graphql-scalars");
810
+ var import_graphql_scalars2 = require("graphql-scalars");
811
+ var import_class_transformer = require("class-transformer");
812
+ var import_graphql = require("graphql");
813
+
814
+ // src/utils/failed-response-status-reasons.ts
815
+ var GuardrailsValidationFailureResponse = class extends FailedResponseStatus {
816
+ reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
817
+ constructor({ guardrailsReason }) {
818
+ super();
819
+ this.details = {
820
+ guardrailsReason
821
+ };
822
+ }
823
+ };
824
+ __name(GuardrailsValidationFailureResponse, "GuardrailsValidationFailureResponse");
825
+ var MessageStreamInterruptedResponse = class extends FailedResponseStatus {
826
+ reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
827
+ constructor({ messageId }) {
828
+ super();
829
+ this.details = {
830
+ messageId,
831
+ description: "Check the message for mode details"
832
+ };
833
+ }
834
+ };
835
+ __name(MessageStreamInterruptedResponse, "MessageStreamInterruptedResponse");
836
+ var UnknownErrorResponse = class extends FailedResponseStatus {
837
+ reason = FailedResponseStatusReason.UNKNOWN_ERROR;
838
+ constructor({ description }) {
839
+ super();
840
+ this.details = {
841
+ description
842
+ };
843
+ }
844
+ };
845
+ __name(UnknownErrorResponse, "UnknownErrorResponse");
846
+
847
+ // src/graphql/resolvers/copilot.resolver.ts
765
848
  function _ts_decorate11(decorators, target, key, desc) {
766
849
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
767
850
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -784,39 +867,173 @@ function _ts_param(paramIndex, decorator) {
784
867
  };
785
868
  }
786
869
  __name(_ts_param, "_ts_param");
870
+ var invokeGuardrails = /* @__PURE__ */ __name(async ({ baseUrl, copilotCloudPublicApiKey, data, onResult, onError }) => {
871
+ var _a;
872
+ if (data.messages.length && ((_a = data.messages[data.messages.length - 1].textMessage) == null ? void 0 : _a.role) === MessageRole.user) {
873
+ const messages = data.messages.filter((m) => m.textMessage !== void 0 && (m.textMessage.role === MessageRole.user || m.textMessage.role === MessageRole.assistant)).map((m) => ({
874
+ role: m.textMessage.role,
875
+ content: m.textMessage.content
876
+ }));
877
+ const lastMessage = messages[messages.length - 1];
878
+ const restOfMessages = messages.slice(0, -1);
879
+ const body = {
880
+ input: lastMessage.content,
881
+ validTopics: data.cloud.guardrails.inputValidationRules.allowList,
882
+ invalidTopics: data.cloud.guardrails.inputValidationRules.denyList,
883
+ messages: restOfMessages
884
+ };
885
+ const guardrailsResult = await fetch(`${baseUrl}/guardrails/validate`, {
886
+ method: "POST",
887
+ headers: {
888
+ "Content-Type": "application/json",
889
+ "X-CopilotCloud-Public-API-Key": copilotCloudPublicApiKey
890
+ },
891
+ body: JSON.stringify(body)
892
+ });
893
+ if (guardrailsResult.ok) {
894
+ const resultJson = await guardrailsResult.json();
895
+ onResult(resultJson);
896
+ } else {
897
+ onError(await guardrailsResult.json());
898
+ }
899
+ }
900
+ }, "invokeGuardrails");
787
901
  var CopilotResolver = class {
788
902
  async hello() {
789
903
  return "Hello World";
790
904
  }
791
905
  async generateCopilotResponse(ctx, data, properties) {
906
+ let logger2 = ctx.logger.child({
907
+ component: "CopilotResolver.generateCopilotResponse"
908
+ });
909
+ logger2.debug({
910
+ data
911
+ }, "Generating Copilot response");
912
+ const copilotRuntime = ctx._copilotkit.runtime;
913
+ const serviceAdapter = ctx._copilotkit.serviceAdapter;
792
914
  if (properties) {
793
- ctx._copilotkit.properties = {
794
- ...ctx._copilotkit.properties,
915
+ logger2.debug("Properties provided, merging with context properties");
916
+ ctx.properties = {
917
+ ...ctx.properties,
795
918
  ...properties
796
919
  };
797
920
  }
798
- const copilotRuntime = ctx._copilotkit.runtime;
799
- const serviceAdapter = ctx._copilotkit.serviceAdapter;
800
- const responseStatus = new import_rxjs2.Subject();
801
- const { eventSource, threadId = (0, import_nanoid.nanoid)(), runId } = await copilotRuntime.process({
921
+ let copilotCloudPublicApiKey = null;
922
+ let copilotCloudBaseUrl;
923
+ if (data.cloud) {
924
+ logger2 = logger2.child({
925
+ cloud: true
926
+ });
927
+ logger2.debug("Cloud configuration provided, checking for public API key in headers");
928
+ const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
929
+ if (key) {
930
+ logger2.debug("Public API key found in headers");
931
+ copilotCloudPublicApiKey = key;
932
+ } else {
933
+ logger2.error("Public API key not found in headers");
934
+ throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
935
+ }
936
+ if (process.env.COPILOT_CLOUD_BASE_URL) {
937
+ copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
938
+ } else if (ctx._copilotkit.baseUrl) {
939
+ copilotCloudBaseUrl = ctx._copilotkit.baseUrl;
940
+ } else {
941
+ copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
942
+ }
943
+ logger2 = logger2.child({
944
+ copilotCloudBaseUrl
945
+ });
946
+ }
947
+ logger2.debug("Setting up subjects");
948
+ const responseStatus$ = new import_rxjs2.ReplaySubject();
949
+ const interruptStreaming$ = new import_rxjs2.ReplaySubject();
950
+ const guardrailsResult$ = new import_rxjs2.ReplaySubject();
951
+ let outputMessages = [];
952
+ let resolveOutputMessagesPromise;
953
+ let rejectOutputMessagesPromise;
954
+ const outputMessagesPromise = new Promise((resolve, reject) => {
955
+ resolveOutputMessagesPromise = resolve;
956
+ rejectOutputMessagesPromise = reject;
957
+ });
958
+ logger2.debug("Processing");
959
+ const { eventSource, threadId = (0, import_nanoid.nanoid)(), runId, actions } = await copilotRuntime.process({
802
960
  serviceAdapter,
803
961
  messages: data.messages,
804
962
  actions: data.frontend.actions,
805
963
  threadId: data.threadId,
806
964
  runId: data.runId,
807
- publicApiKey: void 0
965
+ publicApiKey: void 0,
966
+ properties: ctx.properties || {},
967
+ outputMessagesPromise
808
968
  });
969
+ logger2.debug("Event source created, creating response");
809
970
  const response = {
810
971
  threadId,
811
972
  runId,
812
- status: (0, import_rxjs2.firstValueFrom)(responseStatus),
973
+ status: (0, import_rxjs2.firstValueFrom)(responseStatus$),
813
974
  messages: new import_graphql_yoga.Repeater(async (pushMessage, stopStreamingMessages) => {
814
- const eventStream = eventSource.process(copilotRuntime.actions).pipe(
975
+ var _a, _b;
976
+ logger2.debug("Messages repeater created");
977
+ if ((_a = data.cloud) == null ? void 0 : _a.guardrails) {
978
+ logger2 = logger2.child({
979
+ guardrails: true
980
+ });
981
+ logger2.debug("Guardrails is enabled, validating input");
982
+ invokeGuardrails({
983
+ baseUrl: copilotCloudBaseUrl,
984
+ copilotCloudPublicApiKey,
985
+ data,
986
+ onResult: (result) => {
987
+ logger2.debug({
988
+ status: result.status
989
+ }, "Guardrails validation done");
990
+ guardrailsResult$.next(result);
991
+ if (result.status === "denied") {
992
+ responseStatus$.next(new GuardrailsValidationFailureResponse({
993
+ guardrailsReason: result.reason
994
+ }));
995
+ interruptStreaming$.next({
996
+ reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
997
+ });
998
+ outputMessages = [
999
+ (0, import_class_transformer.plainToInstance)(TextMessage, {
1000
+ id: (0, import_nanoid.nanoid)(),
1001
+ createdAt: /* @__PURE__ */ new Date(),
1002
+ content: result.reason,
1003
+ role: MessageRole.assistant
1004
+ })
1005
+ ];
1006
+ resolveOutputMessagesPromise(outputMessages);
1007
+ }
1008
+ },
1009
+ onError: (err) => {
1010
+ logger2.error({
1011
+ err
1012
+ }, "Error in guardrails validation");
1013
+ responseStatus$.next(new UnknownErrorResponse({
1014
+ description: `An unknown error has occurred in the guardrails validation`
1015
+ }));
1016
+ interruptStreaming$.next({
1017
+ reason: `Interrupted due to unknown error in guardrails validation`
1018
+ });
1019
+ rejectOutputMessagesPromise(err);
1020
+ }
1021
+ });
1022
+ }
1023
+ let eventStreamSubscription;
1024
+ const eventStream = eventSource.process({
1025
+ serversideActions: actions,
1026
+ guardrailsResult$: ((_b = data.cloud) == null ? void 0 : _b.guardrails) ? guardrailsResult$ : null
1027
+ }).pipe(
815
1028
  // shareReplay() ensures that later subscribers will see the whole stream instead of
816
1029
  // just the events that were emitted after the subscriber was added.
817
- (0, import_rxjs2.shareReplay)()
1030
+ (0, import_rxjs2.shareReplay)(),
1031
+ (0, import_rxjs2.finalize)(() => {
1032
+ logger2.debug("Event stream finalized");
1033
+ })
818
1034
  );
819
- eventStream.subscribe({
1035
+ logger2.debug("Event stream created, subscribing to event stream");
1036
+ eventStreamSubscription = eventStream.subscribe({
820
1037
  next: async (event) => {
821
1038
  switch (event.type) {
822
1039
  case RuntimeEventTypes.TextMessageStart:
@@ -827,23 +1044,67 @@ var CopilotResolver = class {
827
1044
  (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
828
1045
  );
829
1046
  const streamingTextStatus = new import_rxjs2.Subject();
1047
+ const messageId = (0, import_nanoid.nanoid)();
830
1048
  pushMessage({
831
- id: (0, import_nanoid.nanoid)(),
1049
+ id: messageId,
832
1050
  status: (0, import_rxjs2.firstValueFrom)(streamingTextStatus),
833
1051
  createdAt: /* @__PURE__ */ new Date(),
834
1052
  role: MessageRole.assistant,
835
1053
  content: new import_graphql_yoga.Repeater(async (pushTextChunk, stopStreamingText) => {
836
- await textMessageContentStream.forEach(async (e) => {
837
- if (e.type == RuntimeEventTypes.TextMessageContent) {
838
- await pushTextChunk(e.content);
1054
+ logger2.debug("Text message content repeater created");
1055
+ const textChunks = [];
1056
+ let textSubscription;
1057
+ interruptStreaming$.pipe((0, import_rxjs2.shareReplay)(), (0, import_rxjs2.take)(1), (0, import_rxjs2.tap)(({ reason, messageId: messageId2 }) => {
1058
+ logger2.debug({
1059
+ reason,
1060
+ messageId: messageId2
1061
+ }, "Text streaming interrupted");
1062
+ streamingTextStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1063
+ reason
1064
+ }));
1065
+ responseStatus$.next(new MessageStreamInterruptedResponse({
1066
+ messageId: messageId2
1067
+ }));
1068
+ stopStreamingText();
1069
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1070
+ })).subscribe();
1071
+ logger2.debug("Subscribing to text message content stream");
1072
+ textSubscription = textMessageContentStream.subscribe({
1073
+ next: async (e) => {
1074
+ if (e.type == RuntimeEventTypes.TextMessageContent) {
1075
+ await pushTextChunk(e.content);
1076
+ textChunks.push(e.content);
1077
+ }
1078
+ },
1079
+ error: (err) => {
1080
+ logger2.error({
1081
+ err
1082
+ }, "Error in text message content stream");
1083
+ interruptStreaming$.next({
1084
+ reason: "Error streaming message content",
1085
+ messageId
1086
+ });
1087
+ stopStreamingText();
1088
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1089
+ },
1090
+ complete: () => {
1091
+ logger2.debug("Text message content stream completed");
1092
+ streamingTextStatus.next(new SuccessMessageStatus());
1093
+ stopStreamingText();
1094
+ textSubscription == null ? void 0 : textSubscription.unsubscribe();
1095
+ outputMessages.push((0, import_class_transformer.plainToInstance)(TextMessage, {
1096
+ id: messageId,
1097
+ createdAt: /* @__PURE__ */ new Date(),
1098
+ content: textChunks.join(""),
1099
+ role: MessageRole.assistant
1100
+ }));
839
1101
  }
840
1102
  });
841
- stopStreamingText();
842
- streamingTextStatus.next(new SuccessMessageStatus());
843
1103
  })
844
1104
  });
845
1105
  break;
846
1106
  case RuntimeEventTypes.ActionExecutionStart:
1107
+ logger2.debug("Action execution start event received");
847
1108
  const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs2.skipWhile)((e) => e !== event), (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
848
1109
  const streamingArgumentsStatus = new import_rxjs2.Subject();
849
1110
  pushMessage({
@@ -853,17 +1114,47 @@ var CopilotResolver = class {
853
1114
  name: event.actionName,
854
1115
  scope: event.scope,
855
1116
  arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
856
- await actionExecutionArgumentStream.forEach(async (e) => {
857
- if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
858
- await pushArgumentsChunk(e.args);
1117
+ logger2.debug("Action execution argument stream created");
1118
+ const argumentChunks = [];
1119
+ let actionExecutionArgumentSubscription;
1120
+ actionExecutionArgumentSubscription = actionExecutionArgumentStream.subscribe({
1121
+ next: async (e) => {
1122
+ if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
1123
+ await pushArgumentsChunk(e.args);
1124
+ argumentChunks.push(e.args);
1125
+ }
1126
+ },
1127
+ error: (err) => {
1128
+ logger2.error({
1129
+ err
1130
+ }, "Error in action execution argument stream");
1131
+ streamingArgumentsStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1132
+ reason: "An unknown error has occurred in the action execution argument stream"
1133
+ }));
1134
+ stopStreamingArguments();
1135
+ actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
1136
+ },
1137
+ complete: () => {
1138
+ logger2.debug("Action execution argument stream completed");
1139
+ streamingArgumentsStatus.next(new SuccessMessageStatus());
1140
+ stopStreamingArguments();
1141
+ actionExecutionArgumentSubscription == null ? void 0 : actionExecutionArgumentSubscription.unsubscribe();
1142
+ outputMessages.push((0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
1143
+ id: event.actionExecutionId,
1144
+ createdAt: /* @__PURE__ */ new Date(),
1145
+ name: event.actionName,
1146
+ scope: event.scope,
1147
+ arguments: argumentChunks.join("")
1148
+ }));
859
1149
  }
860
1150
  });
861
- stopStreamingArguments();
862
- streamingArgumentsStatus.next(new SuccessMessageStatus());
863
1151
  })
864
1152
  });
865
1153
  break;
866
1154
  case RuntimeEventTypes.ActionExecutionResult:
1155
+ logger2.debug({
1156
+ result: event.result
1157
+ }, "Action execution result event received");
867
1158
  pushMessage({
868
1159
  id: (0, import_nanoid.nanoid)(),
869
1160
  status: new SuccessMessageStatus(),
@@ -872,13 +1163,38 @@ var CopilotResolver = class {
872
1163
  actionName: event.actionName,
873
1164
  result: event.result
874
1165
  });
1166
+ outputMessages.push((0, import_class_transformer.plainToInstance)(ResultMessage, {
1167
+ id: (0, import_nanoid.nanoid)(),
1168
+ createdAt: /* @__PURE__ */ new Date(),
1169
+ actionExecutionId: event.actionExecutionId,
1170
+ actionName: event.actionName,
1171
+ result: event.result
1172
+ }));
875
1173
  break;
876
1174
  }
877
1175
  },
878
- error: (err) => console.error("Error in event source", err),
879
- complete: () => {
880
- responseStatus.next(new SuccessResponseStatus());
1176
+ error: (err) => {
1177
+ logger2.error({
1178
+ err
1179
+ }, "Error in event stream");
1180
+ responseStatus$.next(new UnknownErrorResponse({
1181
+ description: `An unknown error has occurred in the event stream`
1182
+ }));
1183
+ eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
1184
+ stopStreamingMessages();
1185
+ rejectOutputMessagesPromise(err);
1186
+ },
1187
+ complete: async () => {
1188
+ var _a2;
1189
+ logger2.debug("Event stream completed");
1190
+ if ((_a2 = data.cloud) == null ? void 0 : _a2.guardrails) {
1191
+ logger2.debug("Guardrails is enabled, waiting for guardrails result");
1192
+ await (0, import_rxjs2.firstValueFrom)(guardrailsResult$);
1193
+ }
1194
+ responseStatus$.next(new SuccessResponseStatus());
1195
+ eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
881
1196
  stopStreamingMessages();
1197
+ resolveOutputMessagesPromise(outputMessages);
882
1198
  }
883
1199
  });
884
1200
  })
@@ -897,7 +1213,7 @@ _ts_decorate11([
897
1213
  (0, import_type_graphql12.Mutation)(() => CopilotResponse),
898
1214
  _ts_param(0, (0, import_type_graphql12.Ctx)()),
899
1215
  _ts_param(1, (0, import_type_graphql12.Arg)("data")),
900
- _ts_param(2, (0, import_type_graphql12.Arg)("properties", () => import_graphql_scalars.GraphQLJSONObject, {
1216
+ _ts_param(2, (0, import_type_graphql12.Arg)("properties", () => import_graphql_scalars2.GraphQLJSONObject, {
901
1217
  nullable: true
902
1218
  })),
903
1219
  _ts_metadata11("design:type", Function),
@@ -914,50 +1230,98 @@ CopilotResolver = _ts_decorate11([
914
1230
 
915
1231
  // src/lib/integrations/shared.ts
916
1232
  var import_plugin_defer_stream = require("@graphql-yoga/plugin-defer-stream");
917
- async function createContext(initialContext, copilotKitContext) {
1233
+
1234
+ // src/lib/logger.ts
1235
+ var import_pino = __toESM(require("pino"));
1236
+ var import_pino_pretty = __toESM(require("pino-pretty"));
1237
+ function createLogger(options) {
1238
+ const { level, component } = options || {};
1239
+ const stream = (0, import_pino_pretty.default)({
1240
+ colorize: true
1241
+ });
1242
+ const logger2 = (0, import_pino.default)({
1243
+ level: process.env.LOG_LEVEL || level || "error",
1244
+ redact: {
1245
+ paths: [
1246
+ "pid",
1247
+ "hostname"
1248
+ ],
1249
+ remove: true
1250
+ }
1251
+ }, stream);
1252
+ if (component) {
1253
+ return logger2.child({
1254
+ component
1255
+ });
1256
+ } else {
1257
+ return logger2;
1258
+ }
1259
+ }
1260
+ __name(createLogger, "createLogger");
1261
+
1262
+ // src/lib/integrations/shared.ts
1263
+ var logger = createLogger();
1264
+ async function createContext(initialContext, copilotKitContext, contextLogger, properties = {}) {
1265
+ logger.debug({
1266
+ copilotKitContext
1267
+ }, "Creating GraphQL context");
918
1268
  const ctx = {
919
1269
  ...initialContext,
920
1270
  _copilotkit: {
921
1271
  ...copilotKitContext
922
- }
1272
+ },
1273
+ properties: {
1274
+ ...properties
1275
+ },
1276
+ logger: contextLogger
923
1277
  };
924
1278
  return ctx;
925
1279
  }
926
1280
  __name(createContext, "createContext");
927
1281
  function buildSchema(options = {}) {
1282
+ logger.debug("Building GraphQL schema...");
928
1283
  const schema = (0, import_type_graphql13.buildSchemaSync)({
929
1284
  resolvers: [
930
1285
  CopilotResolver
931
1286
  ],
932
1287
  emitSchemaFile: options.emitSchemaFile
933
1288
  });
1289
+ logger.debug("GraphQL schema built successfully");
934
1290
  return schema;
935
1291
  }
936
1292
  __name(buildSchema, "buildSchema");
937
1293
  function getCommonConfig(options) {
1294
+ const logLevel = process.env.LOG_LEVEL || options.logLevel || "error";
1295
+ const logger2 = createLogger({
1296
+ level: logLevel,
1297
+ component: "getCommonConfig"
1298
+ });
1299
+ logger2.debug("Getting common config");
1300
+ const contextLogger = createLogger({
1301
+ level: logLevel
1302
+ });
938
1303
  return {
1304
+ logging: createLogger({
1305
+ component: "Yoga GraphQL",
1306
+ level: logLevel
1307
+ }),
939
1308
  schema: buildSchema(),
940
1309
  plugins: [
941
1310
  (0, import_plugin_defer_stream.useDeferStream)()
942
1311
  ],
943
- context: (ctx) => createContext(ctx, {
944
- runtime: options.runtime,
945
- serviceAdapter: options.serviceAdapter,
946
- properties: {}
947
- })
1312
+ context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
948
1313
  };
949
1314
  }
950
1315
  __name(getCommonConfig, "getCommonConfig");
951
1316
 
952
1317
  // src/lib/integrations/node-http/index.ts
953
- function copilotRuntimeNodeHttpEndpoint({ runtime, endpoint, serviceAdapter }) {
954
- const commonConfig = getCommonConfig({
955
- runtime,
956
- serviceAdapter
957
- });
1318
+ function copilotRuntimeNodeHttpEndpoint(options) {
1319
+ const commonConfig = getCommonConfig(options);
1320
+ const logger2 = commonConfig.logging;
1321
+ logger2.debug("Creating Node HTTP endpoint");
958
1322
  const yoga = (0, import_graphql_yoga2.createYoga)({
959
1323
  ...commonConfig,
960
- graphqlEndpoint: endpoint
1324
+ graphqlEndpoint: options.endpoint
961
1325
  });
962
1326
  return yoga;
963
1327
  }