@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.mjs CHANGED
@@ -1,18 +1,18 @@
1
1
  import {
2
2
  CustomEventNames,
3
3
  LangGraphAgent
4
- } from "./chunk-XDBXF3Q6.mjs";
4
+ } from "./chunk-BLBS3CQH.mjs";
5
5
  import {
6
6
  config,
7
7
  copilotRuntimeNextJSAppRouterEndpoint,
8
8
  copilotRuntimeNextJSPagesRouterEndpoint
9
- } from "./chunk-WFYPJXWX.mjs";
9
+ } from "./chunk-T4U5EWYD.mjs";
10
10
  import {
11
11
  copilotRuntimeNestEndpoint
12
- } from "./chunk-MIPAKFI5.mjs";
12
+ } from "./chunk-RK7WRNH6.mjs";
13
13
  import {
14
14
  copilotRuntimeNodeExpressEndpoint
15
- } from "./chunk-N24X5I3C.mjs";
15
+ } from "./chunk-5WRXYU4R.mjs";
16
16
  import {
17
17
  CopilotRuntime,
18
18
  addCustomHeaderPlugin,
@@ -28,7 +28,7 @@ import {
28
28
  getCommonConfig,
29
29
  langGraphPlatformEndpoint,
30
30
  resolveEndpointType
31
- } from "./chunk-GSYE3DGY.mjs";
31
+ } from "./chunk-ZVJSUAD7.mjs";
32
32
  import {
33
33
  AnthropicAdapter,
34
34
  BedrockAdapter,
@@ -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 './integrations/index.js';
4
4
  export { copilotRuntimeNodeHttpEndpoint } from './integrations/node-http/index.js';
5
5
  export { copilotRuntimeNodeExpressEndpoint } from './integrations/node-express/index.js';
package/dist/lib/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",
@@ -4682,6 +4682,8 @@ var CopilotRuntime = class {
4682
4682
  delegateAgentProcessingToServiceAdapter;
4683
4683
  observability;
4684
4684
  availableAgents;
4685
+ onTrace;
4686
+ hasWarnedAboutTracing = false;
4685
4687
  // +++ MCP Properties +++
4686
4688
  mcpServersConfig;
4687
4689
  mcpActionCache = /* @__PURE__ */ new Map();
@@ -4706,6 +4708,7 @@ var CopilotRuntime = class {
4706
4708
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
4707
4709
  this.observability = params == null ? void 0 : params.observability_c;
4708
4710
  this.agents = (params == null ? void 0 : params.agents) ?? {};
4711
+ this.onTrace = params == null ? void 0 : params.onTrace;
4709
4712
  this.mcpServersConfig = params == null ? void 0 : params.mcpServers;
4710
4713
  this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
4711
4714
  if (this.mcpServersConfig && this.mcpServersConfig.length > 0 && !this.createMCPClientImpl) {
@@ -4784,6 +4787,27 @@ var CopilotRuntime = class {
4784
4787
  const eventSource = new RuntimeEventSource();
4785
4788
  const requestStartTime = Date.now();
4786
4789
  const streamedChunks = [];
4790
+ await this.trace("request", {
4791
+ threadId,
4792
+ runId,
4793
+ source: "runtime",
4794
+ request: {
4795
+ operation: "processRuntimeRequest",
4796
+ method: "POST",
4797
+ url,
4798
+ startTime: requestStartTime
4799
+ },
4800
+ agent: agentSession ? {
4801
+ name: agentSession.agentName
4802
+ } : void 0,
4803
+ messages: {
4804
+ input: rawMessages,
4805
+ messageCount: rawMessages.length
4806
+ },
4807
+ technical: {
4808
+ environment: process.env.NODE_ENV
4809
+ }
4810
+ }, void 0, publicApiKey);
4787
4811
  try {
4788
4812
  if (Object.keys(this.agents).length && (agentSession == null ? void 0 : agentSession.agentName) && !this.delegateAgentProcessingToServiceAdapter) {
4789
4813
  this.agents = {
@@ -4953,11 +4977,35 @@ please use an LLM adapter instead.`
4953
4977
  console.error("Error logging LLM error:", logError);
4954
4978
  }
4955
4979
  }
4980
+ let structuredError;
4956
4981
  if (error instanceof import_shared19.CopilotKitError) {
4957
- throw error;
4982
+ structuredError = error;
4983
+ } else {
4984
+ console.error("Error getting response:", error);
4985
+ structuredError = this.convertStreamingErrorToStructured(error);
4958
4986
  }
4959
- console.error("Error getting response:", error);
4960
- const structuredError = this.convertStreamingErrorToStructured(error);
4987
+ await this.trace("error", {
4988
+ threadId,
4989
+ runId,
4990
+ source: "runtime",
4991
+ request: {
4992
+ operation: "processRuntimeRequest",
4993
+ method: "POST",
4994
+ url,
4995
+ startTime: requestStartTime
4996
+ },
4997
+ response: {
4998
+ endTime: Date.now(),
4999
+ latency: Date.now() - requestStartTime
5000
+ },
5001
+ agent: agentSession ? {
5002
+ name: agentSession.agentName
5003
+ } : void 0,
5004
+ technical: {
5005
+ environment: process.env.NODE_ENV,
5006
+ stackTrace: error instanceof Error ? error.stack : void 0
5007
+ }
5008
+ }, structuredError, publicApiKey);
4961
5009
  throw structuredError;
4962
5010
  }
4963
5011
  }
@@ -5142,6 +5190,26 @@ please use an LLM adapter instead.`
5142
5190
  const requestStartTime = Date.now();
5143
5191
  const streamedChunks = [];
5144
5192
  const threadId = threadIdFromRequest ?? agentSession.threadId;
5193
+ await this.trace("agent_state", {
5194
+ threadId,
5195
+ source: "agent",
5196
+ request: {
5197
+ operation: "processAgentRequest",
5198
+ method: "POST",
5199
+ startTime: requestStartTime
5200
+ },
5201
+ agent: {
5202
+ name: agentName,
5203
+ nodeName
5204
+ },
5205
+ messages: {
5206
+ input: rawMessages,
5207
+ messageCount: rawMessages.length
5208
+ },
5209
+ technical: {
5210
+ environment: process.env.NODE_ENV
5211
+ }
5212
+ }, void 0, publicApiKey);
5145
5213
  const serverSideActions = await this.getServerSideActions(request);
5146
5214
  const messages = convertGqlInputToMessages(rawMessages);
5147
5215
  const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
@@ -5237,7 +5305,7 @@ please use an LLM adapter instead.`
5237
5305
  eventSource.stream(async (eventStream$) => {
5238
5306
  (0, import_rxjs2.from)(stream).subscribe({
5239
5307
  next: (event) => eventStream$.next(event),
5240
- error: (err) => {
5308
+ error: async (err) => {
5241
5309
  var _a2;
5242
5310
  console.error("Error in stream", err);
5243
5311
  if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
@@ -5259,6 +5327,27 @@ please use an LLM adapter instead.`
5259
5327
  }
5260
5328
  }
5261
5329
  const structuredError = this.convertStreamingErrorToStructured(err);
5330
+ await this.trace("error", {
5331
+ threadId,
5332
+ source: "agent",
5333
+ request: {
5334
+ operation: "processAgentRequest",
5335
+ method: "POST",
5336
+ startTime: requestStartTime
5337
+ },
5338
+ response: {
5339
+ endTime: Date.now(),
5340
+ latency: Date.now() - requestStartTime
5341
+ },
5342
+ agent: {
5343
+ name: agentName,
5344
+ nodeName
5345
+ },
5346
+ technical: {
5347
+ environment: process.env.NODE_ENV,
5348
+ stackTrace: err instanceof Error ? err.stack : void 0
5349
+ }
5350
+ }, structuredError, publicApiKey);
5262
5351
  eventStream$.error(structuredError);
5263
5352
  eventStream$.complete();
5264
5353
  },
@@ -5326,8 +5415,35 @@ please use an LLM adapter instead.`
5326
5415
  console.error("Error logging agent error:", logError);
5327
5416
  }
5328
5417
  }
5418
+ let structuredError;
5419
+ if (error instanceof import_shared19.CopilotKitError) {
5420
+ structuredError = error;
5421
+ } else {
5422
+ structuredError = this.convertStreamingErrorToStructured(error);
5423
+ }
5424
+ await this.trace("error", {
5425
+ threadId,
5426
+ source: "agent",
5427
+ request: {
5428
+ operation: "processAgentRequest",
5429
+ method: "POST",
5430
+ startTime: requestStartTime
5431
+ },
5432
+ response: {
5433
+ endTime: Date.now(),
5434
+ latency: Date.now() - requestStartTime
5435
+ },
5436
+ agent: {
5437
+ name: agentName,
5438
+ nodeName
5439
+ },
5440
+ technical: {
5441
+ environment: process.env.NODE_ENV,
5442
+ stackTrace: error instanceof Error ? error.stack : void 0
5443
+ }
5444
+ }, structuredError, publicApiKey);
5329
5445
  console.error("Error getting response:", error);
5330
- throw error;
5446
+ throw structuredError;
5331
5447
  }
5332
5448
  }
5333
5449
  async getServerSideActions(request) {
@@ -5447,6 +5563,62 @@ please use an LLM adapter instead.`
5447
5563
  code: import_shared19.CopilotKitErrorCode.UNKNOWN
5448
5564
  });
5449
5565
  }
5566
+ async trace(type, context, error, publicApiKey) {
5567
+ if (!this.onTrace)
5568
+ return;
5569
+ if (!publicApiKey) {
5570
+ if (!this.hasWarnedAboutTracing) {
5571
+ 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");
5572
+ this.hasWarnedAboutTracing = true;
5573
+ }
5574
+ return;
5575
+ }
5576
+ try {
5577
+ const traceEvent = {
5578
+ type,
5579
+ timestamp: Date.now(),
5580
+ context,
5581
+ ...error && {
5582
+ error
5583
+ }
5584
+ };
5585
+ await this.onTrace(traceEvent);
5586
+ } catch (traceError) {
5587
+ console.error("Error in onTrace handler:", traceError);
5588
+ }
5589
+ }
5590
+ /**
5591
+ * Public method to trace GraphQL validation errors
5592
+ * This allows the GraphQL resolver to send validation errors through the trace system
5593
+ */
5594
+ async traceGraphQLError(error, context) {
5595
+ if (!this.onTrace)
5596
+ return;
5597
+ try {
5598
+ await this.onTrace({
5599
+ type: "error",
5600
+ timestamp: Date.now(),
5601
+ context: {
5602
+ source: "runtime",
5603
+ request: {
5604
+ operation: context.operation,
5605
+ startTime: Date.now()
5606
+ },
5607
+ technical: {
5608
+ environment: process.env.NODE_ENV
5609
+ },
5610
+ metadata: {
5611
+ errorType: "GraphQLValidationError",
5612
+ cloudConfigPresent: context.cloudConfigPresent,
5613
+ guardrailsEnabled: context.guardrailsEnabled
5614
+ }
5615
+ },
5616
+ error
5617
+ });
5618
+ } catch (traceError) {
5619
+ console.error("Error in onTrace handler:", traceError);
5620
+ }
5621
+ }
5450
5622
  };
5451
5623
  __name(CopilotRuntime, "CopilotRuntime");
5452
5624
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -6037,7 +6209,7 @@ var CopilotResolver = class {
6037
6209
  };
6038
6210
  }
6039
6211
  async generateCopilotResponse(ctx, data, properties) {
6040
- var _a, _b, _c, _d;
6212
+ var _a, _b, _c, _d, _e;
6041
6213
  telemetry_client_default.capture("oss.runtime.copilot_request_created", {
6042
6214
  "cloud.guardrails.enabled": ((_a = data.cloud) == null ? void 0 : _a.guardrails) !== void 0,
6043
6215
  requestType: data.metadata.requestType,
@@ -6068,23 +6240,32 @@ var CopilotResolver = class {
6068
6240
  const serviceAdapter = ctx._copilotkit.serviceAdapter;
6069
6241
  let copilotCloudPublicApiKey = null;
6070
6242
  let copilotCloudBaseUrl;
6243
+ const publicApiKeyFromHeaders = ctx.request.headers.get("x-copilotcloud-public-api-key");
6244
+ if (publicApiKeyFromHeaders) {
6245
+ copilotCloudPublicApiKey = publicApiKeyFromHeaders;
6246
+ }
6071
6247
  if (data.cloud) {
6072
6248
  logger2 = logger2.child({
6073
6249
  cloud: true
6074
6250
  });
6075
6251
  logger2.debug("Cloud configuration provided, checking for public API key in headers");
6076
- const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
6077
- if (key) {
6078
- logger2.debug("Public API key found in headers");
6079
- copilotCloudPublicApiKey = key;
6080
- } else {
6252
+ if (!copilotCloudPublicApiKey) {
6081
6253
  logger2.error("Public API key not found in headers");
6254
+ await copilotRuntime.traceGraphQLError({
6255
+ message: "X-CopilotCloud-Public-API-Key header is required",
6256
+ code: "MISSING_PUBLIC_API_KEY",
6257
+ type: "GraphQLError"
6258
+ }, {
6259
+ operation: "generateCopilotResponse",
6260
+ cloudConfigPresent: Boolean(data.cloud),
6261
+ guardrailsEnabled: Boolean((_b = data.cloud) == null ? void 0 : _b.guardrails)
6262
+ });
6082
6263
  throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
6083
6264
  }
6084
6265
  if (process.env.COPILOT_CLOUD_BASE_URL) {
6085
6266
  copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
6086
- } else if ((_b = ctx._copilotkit.cloud) == null ? void 0 : _b.baseUrl) {
6087
- copilotCloudBaseUrl = (_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl;
6267
+ } else if ((_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl) {
6268
+ copilotCloudBaseUrl = (_d = ctx._copilotkit.cloud) == null ? void 0 : _d.baseUrl;
6088
6269
  } else {
6089
6270
  copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
6090
6271
  }
@@ -6126,7 +6307,7 @@ var CopilotResolver = class {
6126
6307
  logger2.debug("Event source created, creating response");
6127
6308
  const eventStream = eventSource.processRuntimeEvents({
6128
6309
  serverSideActions,
6129
- guardrailsResult$: ((_d = data.cloud) == null ? void 0 : _d.guardrails) ? guardrailsResult$ : null,
6310
+ guardrailsResult$: ((_e = data.cloud) == null ? void 0 : _e.guardrails) ? guardrailsResult$ : null,
6130
6311
  actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
6131
6312
  // TODO-AGENTS: do not exclude ALL server side actions
6132
6313
  (action) => !serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)