@copilotkit/runtime 1.9.2-next.3 → 1.9.2-next.4

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 (41) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/{chunk-XDBXF3Q6.mjs → chunk-5FNEYAWH.mjs} +2 -2
  3. package/dist/{chunk-N24X5I3C.mjs → chunk-6W7TKZNY.mjs} +2 -2
  4. package/dist/{chunk-MIPAKFI5.mjs → chunk-CJ44W4R2.mjs} +2 -2
  5. package/dist/{chunk-WFYPJXWX.mjs → chunk-NK2XARWT.mjs} +2 -2
  6. package/dist/{chunk-GSYE3DGY.mjs → chunk-ZUF3A5TW.mjs} +197 -16
  7. package/dist/chunk-ZUF3A5TW.mjs.map +1 -0
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.js +196 -15
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +5 -5
  12. package/dist/lib/index.d.ts +1 -1
  13. package/dist/lib/index.js +196 -15
  14. package/dist/lib/index.js.map +1 -1
  15. package/dist/lib/index.mjs +5 -5
  16. package/dist/lib/integrations/index.d.ts +2 -2
  17. package/dist/lib/integrations/index.js +19 -10
  18. package/dist/lib/integrations/index.js.map +1 -1
  19. package/dist/lib/integrations/index.mjs +4 -4
  20. package/dist/lib/integrations/nest/index.d.ts +1 -1
  21. package/dist/lib/integrations/nest/index.js +19 -10
  22. package/dist/lib/integrations/nest/index.js.map +1 -1
  23. package/dist/lib/integrations/nest/index.mjs +2 -2
  24. package/dist/lib/integrations/node-express/index.d.ts +1 -1
  25. package/dist/lib/integrations/node-express/index.js +19 -10
  26. package/dist/lib/integrations/node-express/index.js.map +1 -1
  27. package/dist/lib/integrations/node-express/index.mjs +2 -2
  28. package/dist/lib/integrations/node-http/index.d.ts +1 -1
  29. package/dist/lib/integrations/node-http/index.js +19 -10
  30. package/dist/lib/integrations/node-http/index.js.map +1 -1
  31. package/dist/lib/integrations/node-http/index.mjs +1 -1
  32. package/dist/{shared-e272b15a.d.ts → shared-ba062831.d.ts} +35 -1
  33. package/package.json +2 -2
  34. package/src/graphql/resolvers/copilot.resolver.ts +24 -5
  35. package/src/lib/runtime/__tests__/copilot-runtime-trace.test.ts +169 -0
  36. package/src/lib/runtime/copilot-runtime.ts +254 -6
  37. package/dist/chunk-GSYE3DGY.mjs.map +0 -1
  38. /package/dist/{chunk-XDBXF3Q6.mjs.map → chunk-5FNEYAWH.mjs.map} +0 -0
  39. /package/dist/{chunk-N24X5I3C.mjs.map → chunk-6W7TKZNY.mjs.map} +0 -0
  40. /package/dist/{chunk-MIPAKFI5.mjs.map → chunk-CJ44W4R2.mjs.map} +0 -0
  41. /package/dist/{chunk-WFYPJXWX.mjs.map → chunk-NK2XARWT.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @copilotkit/runtime
2
2
 
3
+ ## 1.9.2-next.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 9169ad7: - feat: add onTrace handler for runtime and UI error/event tracking
8
+ - 9169ad7: - feat: add onTrace handler for comprehensive debugging and observability - Add CopilotTraceEvent interfaces with rich debugging context, implement runtime-side tracing with publicApiKey gating, add UI-side error tracing, include comprehensive test coverage, and fix tsup build config to exclude test files
9
+ - fix: extract publicApiKey for all requests + trace GraphQL errors
10
+ - Updated dependencies [9169ad7]
11
+ - Updated dependencies [9169ad7]
12
+ - @copilotkit/shared@1.9.2-next.4
13
+
3
14
  ## 1.9.2-next.3
4
15
 
5
16
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  LangGraphEventTypes
3
- } from "./chunk-GSYE3DGY.mjs";
3
+ } from "./chunk-ZUF3A5TW.mjs";
4
4
  import {
5
5
  __name
6
6
  } from "./chunk-FHD4JECV.mjs";
@@ -138,4 +138,4 @@ export {
138
138
  CustomEventNames,
139
139
  LangGraphAgent
140
140
  };
141
- //# sourceMappingURL=chunk-XDBXF3Q6.mjs.map
141
+ //# sourceMappingURL=chunk-5FNEYAWH.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-GSYE3DGY.mjs";
5
+ } from "./chunk-ZUF3A5TW.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -22,4 +22,4 @@ __name(copilotRuntimeNodeExpressEndpoint, "copilotRuntimeNodeExpressEndpoint");
22
22
  export {
23
23
  copilotRuntimeNodeExpressEndpoint
24
24
  };
25
- //# sourceMappingURL=chunk-N24X5I3C.mjs.map
25
+ //# sourceMappingURL=chunk-6W7TKZNY.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-GSYE3DGY.mjs";
5
+ } from "./chunk-ZUF3A5TW.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -22,4 +22,4 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
22
22
  export {
23
23
  copilotRuntimeNestEndpoint
24
24
  };
25
- //# sourceMappingURL=chunk-MIPAKFI5.mjs.map
25
+ //# sourceMappingURL=chunk-CJ44W4R2.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  getCommonConfig,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-GSYE3DGY.mjs";
5
+ } from "./chunk-ZUF3A5TW.mjs";
6
6
  import {
7
7
  __name
8
8
  } from "./chunk-FHD4JECV.mjs";
@@ -77,4 +77,4 @@ export {
77
77
  config,
78
78
  copilotRuntimeNextJSPagesRouterEndpoint
79
79
  };
80
- //# sourceMappingURL=chunk-WFYPJXWX.mjs.map
80
+ //# sourceMappingURL=chunk-NK2XARWT.mjs.map
@@ -40,7 +40,7 @@ var require_package = __commonJS({
40
40
  publishConfig: {
41
41
  access: "public"
42
42
  },
43
- version: "1.9.2-next.3",
43
+ version: "1.9.2-next.4",
44
44
  sideEffects: false,
45
45
  main: "./dist/index.js",
46
46
  module: "./dist/index.mjs",
@@ -3219,6 +3219,8 @@ var CopilotRuntime = class {
3219
3219
  delegateAgentProcessingToServiceAdapter;
3220
3220
  observability;
3221
3221
  availableAgents;
3222
+ onTrace;
3223
+ hasWarnedAboutTracing = false;
3222
3224
  // +++ MCP Properties +++
3223
3225
  mcpServersConfig;
3224
3226
  mcpActionCache = /* @__PURE__ */ new Map();
@@ -3243,6 +3245,7 @@ var CopilotRuntime = class {
3243
3245
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
3244
3246
  this.observability = params == null ? void 0 : params.observability_c;
3245
3247
  this.agents = (params == null ? void 0 : params.agents) ?? {};
3248
+ this.onTrace = params == null ? void 0 : params.onTrace;
3246
3249
  this.mcpServersConfig = params == null ? void 0 : params.mcpServers;
3247
3250
  this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
3248
3251
  if (this.mcpServersConfig && this.mcpServersConfig.length > 0 && !this.createMCPClientImpl) {
@@ -3321,6 +3324,27 @@ var CopilotRuntime = class {
3321
3324
  const eventSource = new RuntimeEventSource();
3322
3325
  const requestStartTime = Date.now();
3323
3326
  const streamedChunks = [];
3327
+ await this.trace("request", {
3328
+ threadId,
3329
+ runId,
3330
+ source: "runtime",
3331
+ request: {
3332
+ operation: "processRuntimeRequest",
3333
+ method: "POST",
3334
+ url,
3335
+ startTime: requestStartTime
3336
+ },
3337
+ agent: agentSession ? {
3338
+ name: agentSession.agentName
3339
+ } : void 0,
3340
+ messages: {
3341
+ input: rawMessages,
3342
+ messageCount: rawMessages.length
3343
+ },
3344
+ technical: {
3345
+ environment: process.env.NODE_ENV
3346
+ }
3347
+ }, void 0, publicApiKey);
3324
3348
  try {
3325
3349
  if (Object.keys(this.agents).length && (agentSession == null ? void 0 : agentSession.agentName) && !this.delegateAgentProcessingToServiceAdapter) {
3326
3350
  this.agents = {
@@ -3490,11 +3514,35 @@ please use an LLM adapter instead.`
3490
3514
  console.error("Error logging LLM error:", logError);
3491
3515
  }
3492
3516
  }
3517
+ let structuredError;
3493
3518
  if (error instanceof CopilotKitError5) {
3494
- throw error;
3519
+ structuredError = error;
3520
+ } else {
3521
+ console.error("Error getting response:", error);
3522
+ structuredError = this.convertStreamingErrorToStructured(error);
3495
3523
  }
3496
- console.error("Error getting response:", error);
3497
- const structuredError = this.convertStreamingErrorToStructured(error);
3524
+ await this.trace("error", {
3525
+ threadId,
3526
+ runId,
3527
+ source: "runtime",
3528
+ request: {
3529
+ operation: "processRuntimeRequest",
3530
+ method: "POST",
3531
+ url,
3532
+ startTime: requestStartTime
3533
+ },
3534
+ response: {
3535
+ endTime: Date.now(),
3536
+ latency: Date.now() - requestStartTime
3537
+ },
3538
+ agent: agentSession ? {
3539
+ name: agentSession.agentName
3540
+ } : void 0,
3541
+ technical: {
3542
+ environment: process.env.NODE_ENV,
3543
+ stackTrace: error instanceof Error ? error.stack : void 0
3544
+ }
3545
+ }, structuredError, publicApiKey);
3498
3546
  throw structuredError;
3499
3547
  }
3500
3548
  }
@@ -3679,6 +3727,26 @@ please use an LLM adapter instead.`
3679
3727
  const requestStartTime = Date.now();
3680
3728
  const streamedChunks = [];
3681
3729
  const threadId = threadIdFromRequest ?? agentSession.threadId;
3730
+ await this.trace("agent_state", {
3731
+ threadId,
3732
+ source: "agent",
3733
+ request: {
3734
+ operation: "processAgentRequest",
3735
+ method: "POST",
3736
+ startTime: requestStartTime
3737
+ },
3738
+ agent: {
3739
+ name: agentName,
3740
+ nodeName
3741
+ },
3742
+ messages: {
3743
+ input: rawMessages,
3744
+ messageCount: rawMessages.length
3745
+ },
3746
+ technical: {
3747
+ environment: process.env.NODE_ENV
3748
+ }
3749
+ }, void 0, publicApiKey);
3682
3750
  const serverSideActions = await this.getServerSideActions(request);
3683
3751
  const messages = convertGqlInputToMessages(rawMessages);
3684
3752
  const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
@@ -3774,7 +3842,7 @@ please use an LLM adapter instead.`
3774
3842
  eventSource.stream(async (eventStream$) => {
3775
3843
  from(stream).subscribe({
3776
3844
  next: (event) => eventStream$.next(event),
3777
- error: (err) => {
3845
+ error: async (err) => {
3778
3846
  var _a2;
3779
3847
  console.error("Error in stream", err);
3780
3848
  if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
@@ -3796,6 +3864,27 @@ please use an LLM adapter instead.`
3796
3864
  }
3797
3865
  }
3798
3866
  const structuredError = this.convertStreamingErrorToStructured(err);
3867
+ await this.trace("error", {
3868
+ threadId,
3869
+ source: "agent",
3870
+ request: {
3871
+ operation: "processAgentRequest",
3872
+ method: "POST",
3873
+ startTime: requestStartTime
3874
+ },
3875
+ response: {
3876
+ endTime: Date.now(),
3877
+ latency: Date.now() - requestStartTime
3878
+ },
3879
+ agent: {
3880
+ name: agentName,
3881
+ nodeName
3882
+ },
3883
+ technical: {
3884
+ environment: process.env.NODE_ENV,
3885
+ stackTrace: err instanceof Error ? err.stack : void 0
3886
+ }
3887
+ }, structuredError, publicApiKey);
3799
3888
  eventStream$.error(structuredError);
3800
3889
  eventStream$.complete();
3801
3890
  },
@@ -3863,8 +3952,35 @@ please use an LLM adapter instead.`
3863
3952
  console.error("Error logging agent error:", logError);
3864
3953
  }
3865
3954
  }
3955
+ let structuredError;
3956
+ if (error instanceof CopilotKitError5) {
3957
+ structuredError = error;
3958
+ } else {
3959
+ structuredError = this.convertStreamingErrorToStructured(error);
3960
+ }
3961
+ await this.trace("error", {
3962
+ threadId,
3963
+ source: "agent",
3964
+ request: {
3965
+ operation: "processAgentRequest",
3966
+ method: "POST",
3967
+ startTime: requestStartTime
3968
+ },
3969
+ response: {
3970
+ endTime: Date.now(),
3971
+ latency: Date.now() - requestStartTime
3972
+ },
3973
+ agent: {
3974
+ name: agentName,
3975
+ nodeName
3976
+ },
3977
+ technical: {
3978
+ environment: process.env.NODE_ENV,
3979
+ stackTrace: error instanceof Error ? error.stack : void 0
3980
+ }
3981
+ }, structuredError, publicApiKey);
3866
3982
  console.error("Error getting response:", error);
3867
- throw error;
3983
+ throw structuredError;
3868
3984
  }
3869
3985
  }
3870
3986
  async getServerSideActions(request) {
@@ -3984,6 +4100,62 @@ please use an LLM adapter instead.`
3984
4100
  code: CopilotKitErrorCode3.UNKNOWN
3985
4101
  });
3986
4102
  }
4103
+ async trace(type, context, error, publicApiKey) {
4104
+ if (!this.onTrace)
4105
+ return;
4106
+ if (!publicApiKey) {
4107
+ if (!this.hasWarnedAboutTracing) {
4108
+ console.warn("CopilotKit: onTrace handler provided but requires publicApiKey for tracing to work. This is a CopilotKit Cloud feature. See: https://docs.copilotkit.ai/cloud");
4109
+ this.hasWarnedAboutTracing = true;
4110
+ }
4111
+ return;
4112
+ }
4113
+ try {
4114
+ const traceEvent = {
4115
+ type,
4116
+ timestamp: Date.now(),
4117
+ context,
4118
+ ...error && {
4119
+ error
4120
+ }
4121
+ };
4122
+ await this.onTrace(traceEvent);
4123
+ } catch (traceError) {
4124
+ console.error("Error in onTrace handler:", traceError);
4125
+ }
4126
+ }
4127
+ /**
4128
+ * Public method to trace GraphQL validation errors
4129
+ * This allows the GraphQL resolver to send validation errors through the trace system
4130
+ */
4131
+ async traceGraphQLError(error, context) {
4132
+ if (!this.onTrace)
4133
+ return;
4134
+ try {
4135
+ await this.onTrace({
4136
+ type: "error",
4137
+ timestamp: Date.now(),
4138
+ context: {
4139
+ source: "runtime",
4140
+ request: {
4141
+ operation: context.operation,
4142
+ startTime: Date.now()
4143
+ },
4144
+ technical: {
4145
+ environment: process.env.NODE_ENV
4146
+ },
4147
+ metadata: {
4148
+ errorType: "GraphQLValidationError",
4149
+ cloudConfigPresent: context.cloudConfigPresent,
4150
+ guardrailsEnabled: context.guardrailsEnabled
4151
+ }
4152
+ },
4153
+ error
4154
+ });
4155
+ } catch (traceError) {
4156
+ console.error("Error in onTrace handler:", traceError);
4157
+ }
4158
+ }
3987
4159
  };
3988
4160
  __name(CopilotRuntime, "CopilotRuntime");
3989
4161
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -4539,7 +4711,7 @@ var CopilotResolver = class {
4539
4711
  };
4540
4712
  }
4541
4713
  async generateCopilotResponse(ctx, data, properties) {
4542
- var _a, _b, _c, _d;
4714
+ var _a, _b, _c, _d, _e;
4543
4715
  telemetry_client_default.capture("oss.runtime.copilot_request_created", {
4544
4716
  "cloud.guardrails.enabled": ((_a = data.cloud) == null ? void 0 : _a.guardrails) !== void 0,
4545
4717
  requestType: data.metadata.requestType,
@@ -4570,23 +4742,32 @@ var CopilotResolver = class {
4570
4742
  const serviceAdapter = ctx._copilotkit.serviceAdapter;
4571
4743
  let copilotCloudPublicApiKey = null;
4572
4744
  let copilotCloudBaseUrl;
4745
+ const publicApiKeyFromHeaders = ctx.request.headers.get("x-copilotcloud-public-api-key");
4746
+ if (publicApiKeyFromHeaders) {
4747
+ copilotCloudPublicApiKey = publicApiKeyFromHeaders;
4748
+ }
4573
4749
  if (data.cloud) {
4574
4750
  logger2 = logger2.child({
4575
4751
  cloud: true
4576
4752
  });
4577
4753
  logger2.debug("Cloud configuration provided, checking for public API key in headers");
4578
- const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
4579
- if (key) {
4580
- logger2.debug("Public API key found in headers");
4581
- copilotCloudPublicApiKey = key;
4582
- } else {
4754
+ if (!copilotCloudPublicApiKey) {
4583
4755
  logger2.error("Public API key not found in headers");
4756
+ await copilotRuntime.traceGraphQLError({
4757
+ message: "X-CopilotCloud-Public-API-Key header is required",
4758
+ code: "MISSING_PUBLIC_API_KEY",
4759
+ type: "GraphQLError"
4760
+ }, {
4761
+ operation: "generateCopilotResponse",
4762
+ cloudConfigPresent: Boolean(data.cloud),
4763
+ guardrailsEnabled: Boolean((_b = data.cloud) == null ? void 0 : _b.guardrails)
4764
+ });
4584
4765
  throw new GraphQLError("X-CopilotCloud-Public-API-Key header is required");
4585
4766
  }
4586
4767
  if (process.env.COPILOT_CLOUD_BASE_URL) {
4587
4768
  copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
4588
- } else if ((_b = ctx._copilotkit.cloud) == null ? void 0 : _b.baseUrl) {
4589
- copilotCloudBaseUrl = (_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl;
4769
+ } else if ((_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl) {
4770
+ copilotCloudBaseUrl = (_d = ctx._copilotkit.cloud) == null ? void 0 : _d.baseUrl;
4590
4771
  } else {
4591
4772
  copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
4592
4773
  }
@@ -4628,7 +4809,7 @@ var CopilotResolver = class {
4628
4809
  logger2.debug("Event source created, creating response");
4629
4810
  const eventStream = eventSource.processRuntimeEvents({
4630
4811
  serverSideActions,
4631
- guardrailsResult$: ((_d = data.cloud) == null ? void 0 : _d.guardrails) ? guardrailsResult$ : null,
4812
+ guardrailsResult$: ((_e = data.cloud) == null ? void 0 : _e.guardrails) ? guardrailsResult$ : null,
4632
4813
  actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
4633
4814
  // TODO-AGENTS: do not exclude ALL server side actions
4634
4815
  (action) => !serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
@@ -5325,4 +5506,4 @@ export {
5325
5506
  getCommonConfig,
5326
5507
  copilotRuntimeNodeHttpEndpoint
5327
5508
  };
5328
- //# sourceMappingURL=chunk-GSYE3DGY.mjs.map
5509
+ //# sourceMappingURL=chunk-ZUF3A5TW.mjs.map