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

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 +17 -0
  2. package/dist/{chunk-N24X5I3C.mjs → chunk-5WRXYU4R.mjs} +2 -2
  3. package/dist/{chunk-XDBXF3Q6.mjs → chunk-BLBS3CQH.mjs} +2 -2
  4. package/dist/{chunk-MIPAKFI5.mjs → chunk-RK7WRNH6.mjs} +2 -2
  5. package/dist/{chunk-WFYPJXWX.mjs → chunk-T4U5EWYD.mjs} +2 -2
  6. package/dist/{chunk-GSYE3DGY.mjs → chunk-ZVJSUAD7.mjs} +197 -16
  7. package/dist/chunk-ZVJSUAD7.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-N24X5I3C.mjs.map → chunk-5WRXYU4R.mjs.map} +0 -0
  39. /package/dist/{chunk-XDBXF3Q6.mjs.map → chunk-BLBS3CQH.mjs.map} +0 -0
  40. /package/dist/{chunk-MIPAKFI5.mjs.map → chunk-RK7WRNH6.mjs.map} +0 -0
  41. /package/dist/{chunk-WFYPJXWX.mjs.map → chunk-T4U5EWYD.mjs.map} +0 -0
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { G as GoogleGenerativeAIAdapter, f as GroqAdapter, e as GroqAdapterParams, L as LangChainAdapter, a as OpenAIAdapter, O as OpenAIAdapterParams, c as OpenAIAssistantAdapter, b as OpenAIAssistantAdapterParams, d as UnifyAdapter, U as UnifyAdapterParams } from './groq-adapter-25a2bd35.js';
2
- export { e as CommonConfig, C as CopilotRequestContextProperties, k as CopilotRuntime, j as CopilotRuntimeConstructorParams, f as CopilotRuntimeLogger, i as CopilotRuntimeRequest, b as CreateCopilotRuntimeServerOptions, G as GraphQLContext, L as LogLevel, o as MCPClient, p as MCPEndpointConfig, M as MCPTool, a as addCustomHeaderPlugin, d as buildSchema, s as convertMCPToolsToActions, m as copilotKitEndpoint, c as createContext, h as createLogger, q as extractParametersFromSchema, l as flattenToolCallsNoDuplicates, t as generateMcpToolInstructions, g as getCommonConfig, n as langGraphPlatformEndpoint, r as resolveEndpointType } from './shared-e272b15a.js';
2
+ export { e as CommonConfig, C as CopilotRequestContextProperties, k as CopilotRuntime, j as CopilotRuntimeConstructorParams, f as CopilotRuntimeLogger, i as CopilotRuntimeRequest, b as CreateCopilotRuntimeServerOptions, G as GraphQLContext, L as LogLevel, o as MCPClient, p as MCPEndpointConfig, M as MCPTool, a as addCustomHeaderPlugin, d as buildSchema, s as convertMCPToolsToActions, m as copilotKitEndpoint, c as createContext, h as createLogger, q as extractParametersFromSchema, l as flattenToolCallsNoDuplicates, t as generateMcpToolInstructions, g as getCommonConfig, n as langGraphPlatformEndpoint, r as resolveEndpointType } from './shared-ba062831.js';
3
3
  export { CopilotRuntimeServerInstance, config, copilotRuntimeNextJSAppRouterEndpoint, copilotRuntimeNextJSPagesRouterEndpoint } from './lib/integrations/index.js';
4
4
  export { copilotRuntimeNodeHttpEndpoint } from './lib/integrations/node-http/index.js';
5
5
  export { copilotRuntimeNodeExpressEndpoint } from './lib/integrations/node-express/index.js';
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.9.2-next.3",
47
+ version: "1.9.2-next.5",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -5094,6 +5094,8 @@ var CopilotRuntime = class {
5094
5094
  delegateAgentProcessingToServiceAdapter;
5095
5095
  observability;
5096
5096
  availableAgents;
5097
+ onTrace;
5098
+ hasWarnedAboutTracing = false;
5097
5099
  // +++ MCP Properties +++
5098
5100
  mcpServersConfig;
5099
5101
  mcpActionCache = /* @__PURE__ */ new Map();
@@ -5118,6 +5120,7 @@ var CopilotRuntime = class {
5118
5120
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
5119
5121
  this.observability = params == null ? void 0 : params.observability_c;
5120
5122
  this.agents = (params == null ? void 0 : params.agents) ?? {};
5123
+ this.onTrace = params == null ? void 0 : params.onTrace;
5121
5124
  this.mcpServersConfig = params == null ? void 0 : params.mcpServers;
5122
5125
  this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
5123
5126
  if (this.mcpServersConfig && this.mcpServersConfig.length > 0 && !this.createMCPClientImpl) {
@@ -5196,6 +5199,27 @@ var CopilotRuntime = class {
5196
5199
  const eventSource = new RuntimeEventSource();
5197
5200
  const requestStartTime = Date.now();
5198
5201
  const streamedChunks = [];
5202
+ await this.trace("request", {
5203
+ threadId,
5204
+ runId,
5205
+ source: "runtime",
5206
+ request: {
5207
+ operation: "processRuntimeRequest",
5208
+ method: "POST",
5209
+ url,
5210
+ startTime: requestStartTime
5211
+ },
5212
+ agent: agentSession ? {
5213
+ name: agentSession.agentName
5214
+ } : void 0,
5215
+ messages: {
5216
+ input: rawMessages,
5217
+ messageCount: rawMessages.length
5218
+ },
5219
+ technical: {
5220
+ environment: process.env.NODE_ENV
5221
+ }
5222
+ }, void 0, publicApiKey);
5199
5223
  try {
5200
5224
  if (Object.keys(this.agents).length && (agentSession == null ? void 0 : agentSession.agentName) && !this.delegateAgentProcessingToServiceAdapter) {
5201
5225
  this.agents = {
@@ -5365,11 +5389,35 @@ please use an LLM adapter instead.`
5365
5389
  console.error("Error logging LLM error:", logError);
5366
5390
  }
5367
5391
  }
5392
+ let structuredError;
5368
5393
  if (error instanceof import_shared21.CopilotKitError) {
5369
- throw error;
5394
+ structuredError = error;
5395
+ } else {
5396
+ console.error("Error getting response:", error);
5397
+ structuredError = this.convertStreamingErrorToStructured(error);
5370
5398
  }
5371
- console.error("Error getting response:", error);
5372
- const structuredError = this.convertStreamingErrorToStructured(error);
5399
+ await this.trace("error", {
5400
+ threadId,
5401
+ runId,
5402
+ source: "runtime",
5403
+ request: {
5404
+ operation: "processRuntimeRequest",
5405
+ method: "POST",
5406
+ url,
5407
+ startTime: requestStartTime
5408
+ },
5409
+ response: {
5410
+ endTime: Date.now(),
5411
+ latency: Date.now() - requestStartTime
5412
+ },
5413
+ agent: agentSession ? {
5414
+ name: agentSession.agentName
5415
+ } : void 0,
5416
+ technical: {
5417
+ environment: process.env.NODE_ENV,
5418
+ stackTrace: error instanceof Error ? error.stack : void 0
5419
+ }
5420
+ }, structuredError, publicApiKey);
5373
5421
  throw structuredError;
5374
5422
  }
5375
5423
  }
@@ -5554,6 +5602,26 @@ please use an LLM adapter instead.`
5554
5602
  const requestStartTime = Date.now();
5555
5603
  const streamedChunks = [];
5556
5604
  const threadId = threadIdFromRequest ?? agentSession.threadId;
5605
+ await this.trace("agent_state", {
5606
+ threadId,
5607
+ source: "agent",
5608
+ request: {
5609
+ operation: "processAgentRequest",
5610
+ method: "POST",
5611
+ startTime: requestStartTime
5612
+ },
5613
+ agent: {
5614
+ name: agentName,
5615
+ nodeName
5616
+ },
5617
+ messages: {
5618
+ input: rawMessages,
5619
+ messageCount: rawMessages.length
5620
+ },
5621
+ technical: {
5622
+ environment: process.env.NODE_ENV
5623
+ }
5624
+ }, void 0, publicApiKey);
5557
5625
  const serverSideActions = await this.getServerSideActions(request);
5558
5626
  const messages = convertGqlInputToMessages(rawMessages);
5559
5627
  const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
@@ -5649,7 +5717,7 @@ please use an LLM adapter instead.`
5649
5717
  eventSource.stream(async (eventStream$) => {
5650
5718
  (0, import_rxjs2.from)(stream).subscribe({
5651
5719
  next: (event) => eventStream$.next(event),
5652
- error: (err) => {
5720
+ error: async (err) => {
5653
5721
  var _a2;
5654
5722
  console.error("Error in stream", err);
5655
5723
  if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
@@ -5671,6 +5739,27 @@ please use an LLM adapter instead.`
5671
5739
  }
5672
5740
  }
5673
5741
  const structuredError = this.convertStreamingErrorToStructured(err);
5742
+ await this.trace("error", {
5743
+ threadId,
5744
+ source: "agent",
5745
+ request: {
5746
+ operation: "processAgentRequest",
5747
+ method: "POST",
5748
+ startTime: requestStartTime
5749
+ },
5750
+ response: {
5751
+ endTime: Date.now(),
5752
+ latency: Date.now() - requestStartTime
5753
+ },
5754
+ agent: {
5755
+ name: agentName,
5756
+ nodeName
5757
+ },
5758
+ technical: {
5759
+ environment: process.env.NODE_ENV,
5760
+ stackTrace: err instanceof Error ? err.stack : void 0
5761
+ }
5762
+ }, structuredError, publicApiKey);
5674
5763
  eventStream$.error(structuredError);
5675
5764
  eventStream$.complete();
5676
5765
  },
@@ -5738,8 +5827,35 @@ please use an LLM adapter instead.`
5738
5827
  console.error("Error logging agent error:", logError);
5739
5828
  }
5740
5829
  }
5830
+ let structuredError;
5831
+ if (error instanceof import_shared21.CopilotKitError) {
5832
+ structuredError = error;
5833
+ } else {
5834
+ structuredError = this.convertStreamingErrorToStructured(error);
5835
+ }
5836
+ await this.trace("error", {
5837
+ threadId,
5838
+ source: "agent",
5839
+ request: {
5840
+ operation: "processAgentRequest",
5841
+ method: "POST",
5842
+ startTime: requestStartTime
5843
+ },
5844
+ response: {
5845
+ endTime: Date.now(),
5846
+ latency: Date.now() - requestStartTime
5847
+ },
5848
+ agent: {
5849
+ name: agentName,
5850
+ nodeName
5851
+ },
5852
+ technical: {
5853
+ environment: process.env.NODE_ENV,
5854
+ stackTrace: error instanceof Error ? error.stack : void 0
5855
+ }
5856
+ }, structuredError, publicApiKey);
5741
5857
  console.error("Error getting response:", error);
5742
- throw error;
5858
+ throw structuredError;
5743
5859
  }
5744
5860
  }
5745
5861
  async getServerSideActions(request) {
@@ -5859,6 +5975,62 @@ please use an LLM adapter instead.`
5859
5975
  code: import_shared21.CopilotKitErrorCode.UNKNOWN
5860
5976
  });
5861
5977
  }
5978
+ async trace(type, context, error, publicApiKey) {
5979
+ if (!this.onTrace)
5980
+ return;
5981
+ if (!publicApiKey) {
5982
+ if (!this.hasWarnedAboutTracing) {
5983
+ 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");
5984
+ this.hasWarnedAboutTracing = true;
5985
+ }
5986
+ return;
5987
+ }
5988
+ try {
5989
+ const traceEvent = {
5990
+ type,
5991
+ timestamp: Date.now(),
5992
+ context,
5993
+ ...error && {
5994
+ error
5995
+ }
5996
+ };
5997
+ await this.onTrace(traceEvent);
5998
+ } catch (traceError) {
5999
+ console.error("Error in onTrace handler:", traceError);
6000
+ }
6001
+ }
6002
+ /**
6003
+ * Public method to trace GraphQL validation errors
6004
+ * This allows the GraphQL resolver to send validation errors through the trace system
6005
+ */
6006
+ async traceGraphQLError(error, context) {
6007
+ if (!this.onTrace)
6008
+ return;
6009
+ try {
6010
+ await this.onTrace({
6011
+ type: "error",
6012
+ timestamp: Date.now(),
6013
+ context: {
6014
+ source: "runtime",
6015
+ request: {
6016
+ operation: context.operation,
6017
+ startTime: Date.now()
6018
+ },
6019
+ technical: {
6020
+ environment: process.env.NODE_ENV
6021
+ },
6022
+ metadata: {
6023
+ errorType: "GraphQLValidationError",
6024
+ cloudConfigPresent: context.cloudConfigPresent,
6025
+ guardrailsEnabled: context.guardrailsEnabled
6026
+ }
6027
+ },
6028
+ error
6029
+ });
6030
+ } catch (traceError) {
6031
+ console.error("Error in onTrace handler:", traceError);
6032
+ }
6033
+ }
5862
6034
  };
5863
6035
  __name(CopilotRuntime, "CopilotRuntime");
5864
6036
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -6449,7 +6621,7 @@ var CopilotResolver = class {
6449
6621
  };
6450
6622
  }
6451
6623
  async generateCopilotResponse(ctx, data, properties) {
6452
- var _a, _b, _c, _d;
6624
+ var _a, _b, _c, _d, _e;
6453
6625
  telemetry_client_default.capture("oss.runtime.copilot_request_created", {
6454
6626
  "cloud.guardrails.enabled": ((_a = data.cloud) == null ? void 0 : _a.guardrails) !== void 0,
6455
6627
  requestType: data.metadata.requestType,
@@ -6480,23 +6652,32 @@ var CopilotResolver = class {
6480
6652
  const serviceAdapter = ctx._copilotkit.serviceAdapter;
6481
6653
  let copilotCloudPublicApiKey = null;
6482
6654
  let copilotCloudBaseUrl;
6655
+ const publicApiKeyFromHeaders = ctx.request.headers.get("x-copilotcloud-public-api-key");
6656
+ if (publicApiKeyFromHeaders) {
6657
+ copilotCloudPublicApiKey = publicApiKeyFromHeaders;
6658
+ }
6483
6659
  if (data.cloud) {
6484
6660
  logger2 = logger2.child({
6485
6661
  cloud: true
6486
6662
  });
6487
6663
  logger2.debug("Cloud configuration provided, checking for public API key in headers");
6488
- const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
6489
- if (key) {
6490
- logger2.debug("Public API key found in headers");
6491
- copilotCloudPublicApiKey = key;
6492
- } else {
6664
+ if (!copilotCloudPublicApiKey) {
6493
6665
  logger2.error("Public API key not found in headers");
6666
+ await copilotRuntime.traceGraphQLError({
6667
+ message: "X-CopilotCloud-Public-API-Key header is required",
6668
+ code: "MISSING_PUBLIC_API_KEY",
6669
+ type: "GraphQLError"
6670
+ }, {
6671
+ operation: "generateCopilotResponse",
6672
+ cloudConfigPresent: Boolean(data.cloud),
6673
+ guardrailsEnabled: Boolean((_b = data.cloud) == null ? void 0 : _b.guardrails)
6674
+ });
6494
6675
  throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
6495
6676
  }
6496
6677
  if (process.env.COPILOT_CLOUD_BASE_URL) {
6497
6678
  copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
6498
- } else if ((_b = ctx._copilotkit.cloud) == null ? void 0 : _b.baseUrl) {
6499
- copilotCloudBaseUrl = (_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl;
6679
+ } else if ((_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl) {
6680
+ copilotCloudBaseUrl = (_d = ctx._copilotkit.cloud) == null ? void 0 : _d.baseUrl;
6500
6681
  } else {
6501
6682
  copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
6502
6683
  }
@@ -6538,7 +6719,7 @@ var CopilotResolver = class {
6538
6719
  logger2.debug("Event source created, creating response");
6539
6720
  const eventStream = eventSource.processRuntimeEvents({
6540
6721
  serverSideActions,
6541
- guardrailsResult$: ((_d = data.cloud) == null ? void 0 : _d.guardrails) ? guardrailsResult$ : null,
6722
+ guardrailsResult$: ((_e = data.cloud) == null ? void 0 : _e.guardrails) ? guardrailsResult$ : null,
6542
6723
  actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
6543
6724
  // TODO-AGENTS: do not exclude ALL server side actions
6544
6725
  (action) => !serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)