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