@copilotkit/runtime 0.37.0 → 0.38.0-mme-alpha.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 (88) 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-NFCPM5AM.mjs → chunk-6NZ4UMOD.mjs} +4 -4
  5. package/dist/chunk-6NZ4UMOD.mjs.map +1 -0
  6. package/dist/{chunk-2CCVVJDU.mjs → chunk-6OJ47NCG.mjs} +13 -15
  7. package/dist/chunk-6OJ47NCG.mjs.map +1 -0
  8. package/dist/chunk-BYB2LNMK.mjs +152 -0
  9. package/dist/chunk-BYB2LNMK.mjs.map +1 -0
  10. package/dist/{chunk-XPAUPJMW.mjs → chunk-EWS5TMDA.mjs} +411 -206
  11. package/dist/chunk-EWS5TMDA.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-BLTAUVRP.mjs → chunk-OZMCHYYR.mjs} +5 -3
  15. package/dist/{chunk-BLTAUVRP.mjs.map → chunk-OZMCHYYR.mjs.map} +1 -1
  16. package/dist/chunk-RHQLCJGG.mjs +7 -0
  17. package/dist/chunk-RHQLCJGG.mjs.map +1 -0
  18. package/dist/{chunk-5HGYI6EG.mjs → chunk-XI3HBDMA.mjs} +13 -5
  19. package/dist/chunk-XI3HBDMA.mjs.map +1 -0
  20. package/dist/{chunk-4UA4RB4C.mjs → chunk-XXYCNRFT.mjs} +12 -10
  21. package/dist/chunk-XXYCNRFT.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-d5ba24be.d.ts} +24 -23
  26. package/dist/index.d.ts +7 -4
  27. package/dist/index.js +486 -96
  28. package/dist/index.js.map +1 -1
  29. package/dist/index.mjs +16 -7
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/{langchain-adapter-9ce103f3.d.ts → langchain-adapter-54784d29.d.ts} +1 -1
  32. package/dist/{langserve-fd5066ee.d.ts → langserve-63794237.d.ts} +24 -7
  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 +480 -96
  40. package/dist/lib/index.js.map +1 -1
  41. package/dist/lib/index.mjs +9 -7
  42. package/dist/lib/integrations/index.d.ts +5 -3
  43. package/dist/lib/integrations/index.js +419 -80
  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 +409 -68
  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-d94f01da.d.ts +21 -0
  51. package/dist/service-adapters/index.d.ts +2 -2
  52. package/dist/service-adapters/index.js +54 -8
  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 +269 -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 +13 -37
  68. package/src/lib/integrations/nextjs/app-router.ts +9 -17
  69. package/src/lib/integrations/nextjs/pages-router.ts +9 -15
  70. package/src/lib/integrations/node-http/index.ts +6 -14
  71. package/src/lib/integrations/shared.ts +37 -17
  72. package/src/lib/logger.ts +28 -0
  73. package/src/service-adapters/events.ts +20 -2
  74. package/src/service-adapters/google/google-genai-adapter.ts +3 -0
  75. package/src/service-adapters/google/utils.ts +1 -1
  76. package/src/service-adapters/index.ts +1 -1
  77. package/src/service-adapters/langchain/langserve.ts +10 -4
  78. package/src/service-adapters/langchain/utils.ts +58 -9
  79. package/src/utils/failed-response-status-reasons.ts +48 -0
  80. package/src/utils/index.ts +1 -0
  81. package/dist/chunk-2CCVVJDU.mjs.map +0 -1
  82. package/dist/chunk-4UA4RB4C.mjs.map +0 -1
  83. package/dist/chunk-5HGYI6EG.mjs.map +0 -1
  84. package/dist/chunk-7IFP53C6.mjs.map +0 -1
  85. package/dist/chunk-NFCPM5AM.mjs.map +0 -1
  86. package/dist/chunk-XPAUPJMW.mjs.map +0 -1
  87. package/dist/pages-router-b6bc6c60.d.ts +0 -30
  88. 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
@@ -26,10 +36,10 @@ module.exports = __toCommonJS(node_http_exports);
26
36
  var import_graphql_yoga2 = require("graphql-yoga");
27
37
 
28
38
  // src/lib/integrations/shared.ts
29
- var import_type_graphql13 = require("type-graphql");
39
+ var import_type_graphql14 = require("type-graphql");
30
40
 
31
41
  // src/graphql/resolvers/copilot.resolver.ts
32
- var import_type_graphql12 = require("type-graphql");
42
+ var import_type_graphql13 = require("type-graphql");
33
43
  var import_rxjs2 = require("rxjs");
34
44
 
35
45
  // src/graphql/inputs/generate-copilot-response.input.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
@@ -761,7 +786,11 @@ var RuntimeEventTypes;
761
786
  })(RuntimeEventTypes || (RuntimeEventTypes = {}));
762
787
 
763
788
  // src/graphql/resolvers/copilot.resolver.ts
764
- var import_graphql_scalars = require("graphql-scalars");
789
+ var import_graphql_scalars2 = require("graphql-scalars");
790
+ var import_class_transformer = require("class-transformer");
791
+
792
+ // src/graphql/types/guardrails-result.type.ts
793
+ var import_type_graphql12 = require("type-graphql");
765
794
  function _ts_decorate11(decorators, target, key, desc) {
766
795
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
767
796
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
@@ -778,27 +807,182 @@ function _ts_metadata11(k, v) {
778
807
  return Reflect.metadata(k, v);
779
808
  }
780
809
  __name(_ts_metadata11, "_ts_metadata");
810
+ var GuardrailsResultStatus;
811
+ (function(GuardrailsResultStatus2) {
812
+ GuardrailsResultStatus2["ALLOWED"] = "allowed";
813
+ GuardrailsResultStatus2["DENIED"] = "denied";
814
+ })(GuardrailsResultStatus || (GuardrailsResultStatus = {}));
815
+ (0, import_type_graphql12.registerEnumType)(GuardrailsResultStatus, {
816
+ name: "GuardrailsResultStatus",
817
+ description: "The status of the guardrails check"
818
+ });
819
+ var GuardrailsResult = class {
820
+ status;
821
+ reason;
822
+ };
823
+ __name(GuardrailsResult, "GuardrailsResult");
824
+ _ts_decorate11([
825
+ (0, import_type_graphql12.Field)(() => GuardrailsResultStatus),
826
+ _ts_metadata11("design:type", String)
827
+ ], GuardrailsResult.prototype, "status", void 0);
828
+ _ts_decorate11([
829
+ (0, import_type_graphql12.Field)(() => String, {
830
+ nullable: true
831
+ }),
832
+ _ts_metadata11("design:type", String)
833
+ ], GuardrailsResult.prototype, "reason", void 0);
834
+ GuardrailsResult = _ts_decorate11([
835
+ (0, import_type_graphql12.ObjectType)()
836
+ ], GuardrailsResult);
837
+
838
+ // src/graphql/resolvers/copilot.resolver.ts
839
+ var import_graphql = require("graphql");
840
+
841
+ // src/utils/failed-response-status-reasons.ts
842
+ var GuardrailsValidationFailureResponse = class extends FailedResponseStatus {
843
+ reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
844
+ constructor({ guardrailsReason }) {
845
+ super();
846
+ this.details = {
847
+ guardrailsReason
848
+ };
849
+ }
850
+ };
851
+ __name(GuardrailsValidationFailureResponse, "GuardrailsValidationFailureResponse");
852
+ var MessageStreamInterruptedResponse = class extends FailedResponseStatus {
853
+ reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
854
+ constructor({ messageId }) {
855
+ super();
856
+ this.details = {
857
+ messageId,
858
+ description: "Check the message for mode details"
859
+ };
860
+ }
861
+ };
862
+ __name(MessageStreamInterruptedResponse, "MessageStreamInterruptedResponse");
863
+ var UnknownErrorResponse = class extends FailedResponseStatus {
864
+ reason = FailedResponseStatusReason.UNKNOWN_ERROR;
865
+ constructor({ description }) {
866
+ super();
867
+ this.details = {
868
+ description
869
+ };
870
+ }
871
+ };
872
+ __name(UnknownErrorResponse, "UnknownErrorResponse");
873
+
874
+ // src/graphql/resolvers/copilot.resolver.ts
875
+ function _ts_decorate12(decorators, target, key, desc) {
876
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
877
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
878
+ r = Reflect.decorate(decorators, target, key, desc);
879
+ else
880
+ for (var i = decorators.length - 1; i >= 0; i--)
881
+ if (d = decorators[i])
882
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
883
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
884
+ }
885
+ __name(_ts_decorate12, "_ts_decorate");
886
+ function _ts_metadata12(k, v) {
887
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
888
+ return Reflect.metadata(k, v);
889
+ }
890
+ __name(_ts_metadata12, "_ts_metadata");
781
891
  function _ts_param(paramIndex, decorator) {
782
892
  return function(target, key) {
783
893
  decorator(target, key, paramIndex);
784
894
  };
785
895
  }
786
896
  __name(_ts_param, "_ts_param");
897
+ var invokeGuardrails = /* @__PURE__ */ __name(async ({ baseUrl, copilotCloudPublicApiKey, data, logger: logger2, onResult }) => {
898
+ var _a;
899
+ if (data.messages.length && ((_a = data.messages[data.messages.length - 1].textMessage) == null ? void 0 : _a.role) === MessageRole.user) {
900
+ const messages = data.messages.filter((m) => m.textMessage !== void 0 && (m.textMessage.role === MessageRole.user || m.textMessage.role === MessageRole.assistant)).map((m) => ({
901
+ role: m.textMessage.role,
902
+ content: m.textMessage.content
903
+ }));
904
+ const lastMessage = messages[messages.length - 1];
905
+ const restOfMessages = messages.slice(0, -1);
906
+ const body = {
907
+ input: lastMessage.content,
908
+ validTopics: data.cloud.guardrails.inputValidationRules.allowList,
909
+ invalidTopics: data.cloud.guardrails.inputValidationRules.denyList,
910
+ messages: restOfMessages
911
+ };
912
+ const guardrailsResult = await fetch(`${baseUrl}/guardrails/validate`, {
913
+ method: "POST",
914
+ headers: {
915
+ "Content-Type": "application/json",
916
+ "X-CopilotCloud-Public-API-Key": copilotCloudPublicApiKey
917
+ },
918
+ body: JSON.stringify(body)
919
+ });
920
+ const responseBody = await guardrailsResult.json();
921
+ if (!guardrailsResult.ok) {
922
+ logger2.error({
923
+ error: responseBody
924
+ }, "Failed to invoke guardrails");
925
+ throw new import_graphql.GraphQLError("Failed to invoke guardrails");
926
+ }
927
+ onResult(responseBody);
928
+ } else {
929
+ onResult({
930
+ status: GuardrailsResultStatus.ALLOWED
931
+ });
932
+ }
933
+ }, "invokeGuardrails");
787
934
  var CopilotResolver = class {
788
935
  async hello() {
789
936
  return "Hello World";
790
937
  }
791
938
  async generateCopilotResponse(ctx, data, properties) {
939
+ let logger2 = ctx.logger.child({
940
+ component: "CopilotResolver.generateCopilotResponse"
941
+ });
942
+ logger2.debug({
943
+ data
944
+ }, "Generating Copilot response");
945
+ const copilotRuntime = ctx._copilotkit.runtime;
946
+ const serviceAdapter = ctx._copilotkit.serviceAdapter;
792
947
  if (properties) {
793
- ctx._copilotkit.properties = {
794
- ...ctx._copilotkit.properties,
948
+ logger2.debug("Properties provided, merging with context properties");
949
+ ctx.properties = {
950
+ ...ctx.properties,
795
951
  ...properties
796
952
  };
797
953
  }
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({
954
+ let copilotCloudPublicApiKey = null;
955
+ let copilotCloudBaseUrl;
956
+ if (data.cloud) {
957
+ logger2 = logger2.child({
958
+ cloud: true
959
+ });
960
+ logger2.debug("Cloud configuration provided, checking for public API key in headers");
961
+ const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
962
+ if (key) {
963
+ logger2.debug("Public API key found in headers");
964
+ copilotCloudPublicApiKey = key;
965
+ } else {
966
+ logger2.error("Public API key not found in headers");
967
+ throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
968
+ }
969
+ if (process.env.COPILOT_CLOUD_BASE_URL) {
970
+ copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
971
+ } else if (ctx._copilotkit.baseUrl) {
972
+ copilotCloudBaseUrl = ctx._copilotkit.baseUrl;
973
+ } else {
974
+ copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
975
+ }
976
+ logger2 = logger2.child({
977
+ copilotCloudBaseUrl
978
+ });
979
+ }
980
+ logger2.debug("Setting up subjects");
981
+ const responseStatus$ = new import_rxjs2.ReplaySubject();
982
+ const interruptStreaming$ = new import_rxjs2.ReplaySubject();
983
+ const guardrailsResult$ = new import_rxjs2.ReplaySubject();
984
+ logger2.debug("Processing");
985
+ const { eventSource, threadId = (0, import_nanoid.nanoid)(), runId, actions } = await copilotRuntime.process({
802
986
  serviceAdapter,
803
987
  messages: data.messages,
804
988
  actions: data.frontend.actions,
@@ -806,17 +990,55 @@ var CopilotResolver = class {
806
990
  runId: data.runId,
807
991
  publicApiKey: void 0
808
992
  });
993
+ logger2.debug("Event source created, creating response");
809
994
  const response = {
810
995
  threadId,
811
996
  runId,
812
- status: (0, import_rxjs2.firstValueFrom)(responseStatus),
997
+ status: (0, import_rxjs2.firstValueFrom)(responseStatus$),
813
998
  messages: new import_graphql_yoga.Repeater(async (pushMessage, stopStreamingMessages) => {
814
- const eventStream = eventSource.process(copilotRuntime.actions).pipe(
999
+ var _a, _b;
1000
+ logger2.debug("Messages repeater created");
1001
+ if ((_a = data.cloud) == null ? void 0 : _a.guardrails) {
1002
+ logger2 = logger2.child({
1003
+ guardrails: true
1004
+ });
1005
+ logger2.debug("Guardrails is enabled, validating input");
1006
+ invokeGuardrails({
1007
+ baseUrl: copilotCloudBaseUrl,
1008
+ copilotCloudPublicApiKey,
1009
+ data,
1010
+ logger: logger2,
1011
+ onResult: (result) => {
1012
+ logger2.debug({
1013
+ status: result.status
1014
+ }, "Guardrails validation done");
1015
+ guardrailsResult$.next(result);
1016
+ if (result.status === "denied") {
1017
+ responseStatus$.next(new GuardrailsValidationFailureResponse({
1018
+ guardrailsReason: result.reason
1019
+ }));
1020
+ interruptStreaming$.next({
1021
+ reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
1022
+ });
1023
+ }
1024
+ }
1025
+ });
1026
+ }
1027
+ let eventStreamSubscription;
1028
+ const eventStream = eventSource.process({
1029
+ serversideActions: copilotRuntime.actions,
1030
+ guardrailsResult$: ((_b = data.cloud) == null ? void 0 : _b.guardrails) ? guardrailsResult$ : null
1031
+ }).pipe(
815
1032
  // shareReplay() ensures that later subscribers will see the whole stream instead of
816
1033
  // just the events that were emitted after the subscriber was added.
817
- (0, import_rxjs2.shareReplay)()
1034
+ (0, import_rxjs2.shareReplay)(),
1035
+ (0, import_rxjs2.finalize)(() => {
1036
+ logger2.debug("Event stream finalized, stopping streaming messages");
1037
+ stopStreamingMessages();
1038
+ })
818
1039
  );
819
- eventStream.subscribe({
1040
+ logger2.debug("Event stream created, subscribing to event stream");
1041
+ eventStreamSubscription = eventStream.subscribe({
820
1042
  next: async (event) => {
821
1043
  switch (event.type) {
822
1044
  case RuntimeEventTypes.TextMessageStart:
@@ -827,23 +1049,59 @@ var CopilotResolver = class {
827
1049
  (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
828
1050
  );
829
1051
  const streamingTextStatus = new import_rxjs2.Subject();
1052
+ const messageId = (0, import_nanoid.nanoid)();
830
1053
  pushMessage({
831
- id: (0, import_nanoid.nanoid)(),
1054
+ id: messageId,
832
1055
  status: (0, import_rxjs2.firstValueFrom)(streamingTextStatus),
833
1056
  createdAt: /* @__PURE__ */ new Date(),
834
1057
  role: MessageRole.assistant,
835
1058
  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);
1059
+ logger2.debug("Text message content repeater created");
1060
+ let textSubscription;
1061
+ interruptStreaming$.pipe((0, import_rxjs2.shareReplay)(), (0, import_rxjs2.take)(1), (0, import_rxjs2.tap)(({ reason, messageId: messageId2 }) => {
1062
+ logger2.debug({
1063
+ reason,
1064
+ messageId: messageId2
1065
+ }, "Text streaming interrupted");
1066
+ streamingTextStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1067
+ reason
1068
+ }));
1069
+ responseStatus$.next(new MessageStreamInterruptedResponse({
1070
+ messageId: messageId2
1071
+ }));
1072
+ stopStreamingText();
1073
+ textSubscription.unsubscribe();
1074
+ })).subscribe();
1075
+ logger2.debug("Subscribing to text message content stream");
1076
+ textSubscription = textMessageContentStream.subscribe({
1077
+ next: async (e) => {
1078
+ if (e.type == RuntimeEventTypes.TextMessageContent) {
1079
+ await pushTextChunk(e.content);
1080
+ }
1081
+ },
1082
+ error: (err) => {
1083
+ logger2.error({
1084
+ err
1085
+ }, "Error in text message content stream");
1086
+ interruptStreaming$.next({
1087
+ reason: "Error streaming message content",
1088
+ messageId
1089
+ });
1090
+ stopStreamingText();
1091
+ textSubscription.unsubscribe();
1092
+ },
1093
+ complete: () => {
1094
+ logger2.debug("Text message content stream completed");
1095
+ streamingTextStatus.next(new SuccessMessageStatus());
1096
+ stopStreamingText();
1097
+ textSubscription.unsubscribe();
839
1098
  }
840
1099
  });
841
- stopStreamingText();
842
- streamingTextStatus.next(new SuccessMessageStatus());
843
1100
  })
844
1101
  });
845
1102
  break;
846
1103
  case RuntimeEventTypes.ActionExecutionStart:
1104
+ logger2.debug("Action execution start event received");
847
1105
  const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs2.skipWhile)((e) => e !== event), (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
848
1106
  const streamingArgumentsStatus = new import_rxjs2.Subject();
849
1107
  pushMessage({
@@ -853,17 +1111,36 @@ var CopilotResolver = class {
853
1111
  name: event.actionName,
854
1112
  scope: event.scope,
855
1113
  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);
1114
+ logger2.debug("Action execution argument stream created");
1115
+ let actionExecutionArgumentSubscription;
1116
+ actionExecutionArgumentSubscription = actionExecutionArgumentStream.subscribe({
1117
+ next: async (e) => {
1118
+ if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
1119
+ await pushArgumentsChunk(e.args);
1120
+ }
1121
+ },
1122
+ error: (err) => {
1123
+ logger2.error({
1124
+ err
1125
+ }, "Error in action execution argument stream");
1126
+ streamingArgumentsStatus.next((0, import_class_transformer.plainToInstance)(FailedMessageStatus, {
1127
+ reason: "An unknown error has occurred in the action execution argument stream"
1128
+ }));
1129
+ stopStreamingArguments();
1130
+ actionExecutionArgumentSubscription.unsubscribe();
1131
+ },
1132
+ complete: () => {
1133
+ logger2.debug("Action execution argument stream completed");
1134
+ streamingArgumentsStatus.next(new SuccessMessageStatus());
1135
+ stopStreamingArguments();
1136
+ actionExecutionArgumentSubscription.unsubscribe();
859
1137
  }
860
1138
  });
861
- stopStreamingArguments();
862
- streamingArgumentsStatus.next(new SuccessMessageStatus());
863
1139
  })
864
1140
  });
865
1141
  break;
866
1142
  case RuntimeEventTypes.ActionExecutionResult:
1143
+ logger2.debug("Action execution result event received");
867
1144
  pushMessage({
868
1145
  id: (0, import_nanoid.nanoid)(),
869
1146
  status: new SuccessMessageStatus(),
@@ -875,9 +1152,25 @@ var CopilotResolver = class {
875
1152
  break;
876
1153
  }
877
1154
  },
878
- error: (err) => console.error("Error in event source", err),
879
- complete: () => {
880
- responseStatus.next(new SuccessResponseStatus());
1155
+ error: (err) => {
1156
+ logger2.error({
1157
+ err
1158
+ }, "Error in event stream");
1159
+ responseStatus$.next(new UnknownErrorResponse({
1160
+ description: `An unknown error has occurred in the event stream`
1161
+ }));
1162
+ eventStreamSubscription.unsubscribe();
1163
+ stopStreamingMessages();
1164
+ },
1165
+ complete: async () => {
1166
+ var _a2;
1167
+ logger2.debug("Event stream completed");
1168
+ if ((_a2 = data.cloud) == null ? void 0 : _a2.guardrails) {
1169
+ logger2.debug("Guardrails is enabled, waiting for guardrails result");
1170
+ await (0, import_rxjs2.firstValueFrom)(guardrailsResult$);
1171
+ }
1172
+ responseStatus$.next(new SuccessResponseStatus());
1173
+ eventStreamSubscription.unsubscribe();
881
1174
  stopStreamingMessages();
882
1175
  }
883
1176
  });
@@ -887,77 +1180,125 @@ var CopilotResolver = class {
887
1180
  }
888
1181
  };
889
1182
  __name(CopilotResolver, "CopilotResolver");
890
- _ts_decorate11([
891
- (0, import_type_graphql12.Query)(() => String),
892
- _ts_metadata11("design:type", Function),
893
- _ts_metadata11("design:paramtypes", []),
894
- _ts_metadata11("design:returntype", Promise)
1183
+ _ts_decorate12([
1184
+ (0, import_type_graphql13.Query)(() => String),
1185
+ _ts_metadata12("design:type", Function),
1186
+ _ts_metadata12("design:paramtypes", []),
1187
+ _ts_metadata12("design:returntype", Promise)
895
1188
  ], CopilotResolver.prototype, "hello", null);
896
- _ts_decorate11([
897
- (0, import_type_graphql12.Mutation)(() => CopilotResponse),
898
- _ts_param(0, (0, import_type_graphql12.Ctx)()),
899
- _ts_param(1, (0, import_type_graphql12.Arg)("data")),
900
- _ts_param(2, (0, import_type_graphql12.Arg)("properties", () => import_graphql_scalars.GraphQLJSONObject, {
1189
+ _ts_decorate12([
1190
+ (0, import_type_graphql13.Mutation)(() => CopilotResponse),
1191
+ _ts_param(0, (0, import_type_graphql13.Ctx)()),
1192
+ _ts_param(1, (0, import_type_graphql13.Arg)("data")),
1193
+ _ts_param(2, (0, import_type_graphql13.Arg)("properties", () => import_graphql_scalars2.GraphQLJSONObject, {
901
1194
  nullable: true
902
1195
  })),
903
- _ts_metadata11("design:type", Function),
904
- _ts_metadata11("design:paramtypes", [
1196
+ _ts_metadata12("design:type", Function),
1197
+ _ts_metadata12("design:paramtypes", [
905
1198
  typeof GraphQLContext === "undefined" ? Object : GraphQLContext,
906
1199
  typeof GenerateCopilotResponseInput === "undefined" ? Object : GenerateCopilotResponseInput,
907
1200
  typeof CopilotRequestContextProperties === "undefined" ? Object : CopilotRequestContextProperties
908
1201
  ]),
909
- _ts_metadata11("design:returntype", Promise)
1202
+ _ts_metadata12("design:returntype", Promise)
910
1203
  ], CopilotResolver.prototype, "generateCopilotResponse", null);
911
- CopilotResolver = _ts_decorate11([
912
- (0, import_type_graphql12.Resolver)(() => CopilotResponse)
1204
+ CopilotResolver = _ts_decorate12([
1205
+ (0, import_type_graphql13.Resolver)(() => CopilotResponse)
913
1206
  ], CopilotResolver);
914
1207
 
915
1208
  // src/lib/integrations/shared.ts
916
1209
  var import_plugin_defer_stream = require("@graphql-yoga/plugin-defer-stream");
917
- async function createContext(initialContext, copilotKitContext) {
1210
+
1211
+ // src/lib/logger.ts
1212
+ var import_pino = __toESM(require("pino"));
1213
+ var import_pino_pretty = __toESM(require("pino-pretty"));
1214
+ function createLogger(options) {
1215
+ const { level, component } = options || {};
1216
+ const stream = (0, import_pino_pretty.default)({
1217
+ colorize: true
1218
+ });
1219
+ const logger2 = (0, import_pino.default)({
1220
+ level: process.env.LOG_LEVEL || level || "error",
1221
+ redact: {
1222
+ paths: [
1223
+ "pid",
1224
+ "hostname"
1225
+ ],
1226
+ remove: true
1227
+ }
1228
+ }, stream);
1229
+ if (component) {
1230
+ return logger2.child({
1231
+ component
1232
+ });
1233
+ } else {
1234
+ return logger2;
1235
+ }
1236
+ }
1237
+ __name(createLogger, "createLogger");
1238
+
1239
+ // src/lib/integrations/shared.ts
1240
+ var logger = createLogger();
1241
+ async function createContext(initialContext, copilotKitContext, contextLogger, properties = {}) {
1242
+ logger.debug({
1243
+ copilotKitContext
1244
+ }, "Creating GraphQL context");
918
1245
  const ctx = {
919
1246
  ...initialContext,
920
1247
  _copilotkit: {
921
1248
  ...copilotKitContext
922
- }
1249
+ },
1250
+ properties: {
1251
+ ...properties
1252
+ },
1253
+ logger: contextLogger
923
1254
  };
924
1255
  return ctx;
925
1256
  }
926
1257
  __name(createContext, "createContext");
927
1258
  function buildSchema(options = {}) {
928
- const schema = (0, import_type_graphql13.buildSchemaSync)({
1259
+ logger.debug("Building GraphQL schema...");
1260
+ const schema = (0, import_type_graphql14.buildSchemaSync)({
929
1261
  resolvers: [
930
1262
  CopilotResolver
931
1263
  ],
932
1264
  emitSchemaFile: options.emitSchemaFile
933
1265
  });
1266
+ logger.debug("GraphQL schema built successfully");
934
1267
  return schema;
935
1268
  }
936
1269
  __name(buildSchema, "buildSchema");
937
1270
  function getCommonConfig(options) {
1271
+ const logLevel = process.env.LOG_LEVEL || options.logLevel || "error";
1272
+ const logger2 = createLogger({
1273
+ level: logLevel,
1274
+ component: "getCommonConfig"
1275
+ });
1276
+ logger2.debug("Getting common config");
1277
+ const contextLogger = createLogger({
1278
+ level: logLevel
1279
+ });
938
1280
  return {
1281
+ logging: createLogger({
1282
+ component: "Yoga GraphQL",
1283
+ level: logLevel
1284
+ }),
939
1285
  schema: buildSchema(),
940
1286
  plugins: [
941
1287
  (0, import_plugin_defer_stream.useDeferStream)()
942
1288
  ],
943
- context: (ctx) => createContext(ctx, {
944
- runtime: options.runtime,
945
- serviceAdapter: options.serviceAdapter,
946
- properties: {}
947
- })
1289
+ context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
948
1290
  };
949
1291
  }
950
1292
  __name(getCommonConfig, "getCommonConfig");
951
1293
 
952
1294
  // src/lib/integrations/node-http/index.ts
953
- function copilotRuntimeNodeHttpEndpoint({ runtime, endpoint, serviceAdapter }) {
954
- const commonConfig = getCommonConfig({
955
- runtime,
956
- serviceAdapter
957
- });
1295
+ function copilotRuntimeNodeHttpEndpoint(options) {
1296
+ const commonConfig = getCommonConfig(options);
1297
+ const logger2 = commonConfig.logging;
1298
+ logger2.debug("Creating Node HTTP endpoint");
958
1299
  const yoga = (0, import_graphql_yoga2.createYoga)({
959
1300
  ...commonConfig,
960
- graphqlEndpoint: endpoint
1301
+ graphqlEndpoint: options.endpoint
961
1302
  });
962
1303
  return yoga;
963
1304
  }