@copilotkit/runtime 1.7.0-next.0 → 1.7.0-next.1

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 (43) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +75 -0
  3. package/dist/{chunk-PH24IU7T.mjs → chunk-D6YNY2XB.mjs} +2 -2
  4. package/dist/chunk-PTC5JN3P.mjs +1 -0
  5. package/dist/{chunk-34Y5DNNJ.mjs → chunk-QTRO3GPV.mjs} +120 -5
  6. package/dist/{chunk-34Y5DNNJ.mjs.map → chunk-QTRO3GPV.mjs.map} +1 -1
  7. package/dist/{chunk-2BN7NZNC.mjs → chunk-QZ6X33MR.mjs} +2 -2
  8. package/dist/{chunk-ZYFN76KV.mjs → chunk-RQS3BGAT.mjs} +2 -2
  9. package/dist/{copilot-runtime-15bfc4f4.d.ts → copilot-runtime-5103c7e7.d.ts} +66 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +120 -4
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +7 -5
  14. package/dist/index.mjs.map +1 -1
  15. package/dist/lib/index.d.ts +1 -1
  16. package/dist/lib/index.js +120 -4
  17. package/dist/lib/index.js.map +1 -1
  18. package/dist/lib/index.mjs +7 -5
  19. package/dist/lib/integrations/index.d.ts +2 -2
  20. package/dist/lib/integrations/index.js +1 -1
  21. package/dist/lib/integrations/index.js.map +1 -1
  22. package/dist/lib/integrations/index.mjs +4 -4
  23. package/dist/lib/integrations/nest/index.d.ts +1 -1
  24. package/dist/lib/integrations/nest/index.js +1 -1
  25. package/dist/lib/integrations/nest/index.js.map +1 -1
  26. package/dist/lib/integrations/nest/index.mjs +2 -2
  27. package/dist/lib/integrations/node-express/index.d.ts +1 -1
  28. package/dist/lib/integrations/node-express/index.js +1 -1
  29. package/dist/lib/integrations/node-express/index.js.map +1 -1
  30. package/dist/lib/integrations/node-express/index.mjs +2 -2
  31. package/dist/lib/integrations/node-http/index.d.ts +1 -1
  32. package/dist/lib/integrations/node-http/index.js +1 -1
  33. package/dist/lib/integrations/node-http/index.js.map +1 -1
  34. package/dist/lib/integrations/node-http/index.mjs +1 -1
  35. package/package.json +2 -2
  36. package/src/lib/index.ts +1 -0
  37. package/src/lib/logger.ts +48 -0
  38. package/src/lib/runtime/copilot-runtime.ts +162 -2
  39. package/dist/chunk-DFOKBSIS.mjs +0 -1
  40. /package/dist/{chunk-PH24IU7T.mjs.map → chunk-D6YNY2XB.mjs.map} +0 -0
  41. /package/dist/{chunk-DFOKBSIS.mjs.map → chunk-PTC5JN3P.mjs.map} +0 -0
  42. /package/dist/{chunk-2BN7NZNC.mjs.map → chunk-QZ6X33MR.mjs.map} +0 -0
  43. /package/dist/{chunk-ZYFN76KV.mjs.map → chunk-RQS3BGAT.mjs.map} +0 -0
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-34Y5DNNJ.mjs";
5
+ } from "./chunk-QTRO3GPV.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-2BN7NZNC.mjs.map
25
+ //# sourceMappingURL=chunk-QZ6X33MR.mjs.map
@@ -2,7 +2,7 @@ import {
2
2
  copilotRuntimeNodeHttpEndpoint,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-34Y5DNNJ.mjs";
5
+ } from "./chunk-QTRO3GPV.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-ZYFN76KV.mjs.map
25
+ //# sourceMappingURL=chunk-RQS3BGAT.mjs.map
@@ -7,6 +7,53 @@ import { YogaInitialContext, createYoga } from 'graphql-yoga';
7
7
  import { CopilotCloudOptions } from './lib/cloud/index.js';
8
8
 
9
9
  type LogLevel = "debug" | "info" | "warn" | "error";
10
+ type CopilotRuntimeLogger = ReturnType<typeof createLogger>;
11
+ declare function createLogger(options?: {
12
+ level?: LogLevel;
13
+ component?: string;
14
+ }): pino.Logger<never>;
15
+ interface LogLLMRequestData {
16
+ threadId?: string;
17
+ runId?: string;
18
+ model?: string;
19
+ messages: any[];
20
+ actions?: any[];
21
+ forwardedParameters?: any;
22
+ timestamp: number;
23
+ provider?: string;
24
+ [key: string]: any;
25
+ }
26
+ interface LogLLMResponseData {
27
+ threadId: string;
28
+ runId?: string;
29
+ model?: string;
30
+ output: any;
31
+ latency: number;
32
+ timestamp: number;
33
+ provider?: string;
34
+ isProgressiveChunk?: boolean;
35
+ isFinalResponse?: boolean;
36
+ [key: string]: any;
37
+ }
38
+ interface LogLLMErrorData {
39
+ threadId?: string;
40
+ runId?: string;
41
+ model?: string;
42
+ error: Error | string;
43
+ timestamp: number;
44
+ provider?: string;
45
+ [key: string]: any;
46
+ }
47
+ interface CopilotLoggerHooks {
48
+ logRequest: (data: LogLLMRequestData) => void | Promise<void>;
49
+ logResponse: (data: LogLLMResponseData) => void | Promise<void>;
50
+ logError: (data: LogLLMErrorData) => void | Promise<void>;
51
+ }
52
+ interface CopilotLoggingConfig {
53
+ enabled: boolean;
54
+ progressive: boolean;
55
+ logger: CopilotLoggerHooks;
56
+ }
10
57
 
11
58
  declare const logger: pino.Logger<never>;
12
59
  declare const addCustomHeaderPlugin: {
@@ -195,6 +242,22 @@ interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
195
242
  remoteEndpoints?: EndpointDefinition[];
196
243
  langserve?: RemoteChainParameters[];
197
244
  delegateAgentProcessingToServiceAdapter?: boolean;
245
+ /**
246
+ * Configuration for LLM request/response logging
247
+ *
248
+ * ```ts
249
+ * logging: {
250
+ * enabled: true, // Enable or disable logging
251
+ * progressive: true, // Set to false for buffered logging
252
+ * logger: {
253
+ * logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
254
+ * logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
255
+ * logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
256
+ * },
257
+ * }
258
+ * ```
259
+ */
260
+ logging?: CopilotLoggingConfig;
198
261
  }
199
262
  declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
200
263
  actions: ActionsConfiguration<T>;
@@ -203,16 +266,18 @@ declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
203
266
  private onBeforeRequest?;
204
267
  private onAfterRequest?;
205
268
  private delegateAgentProcessingToServiceAdapter;
269
+ private logging?;
206
270
  constructor(params?: CopilotRuntimeConstructorParams<T>);
207
271
  processRuntimeRequest(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse>;
208
272
  discoverAgentsFromEndpoints(graphqlContext: GraphQLContext): Promise<AgentWithEndpoint[]>;
209
273
  loadAgentState(graphqlContext: GraphQLContext, threadId: string, agentName: string): Promise<LoadAgentStateResponse>;
210
274
  private processAgentRequest;
211
275
  private getServerSideActions;
276
+ private detectProvider;
212
277
  }
213
278
  declare function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[];
214
279
  declare function copilotKitEndpoint(config: Omit<CopilotKitEndpoint, "type">): CopilotKitEndpoint;
215
280
  declare function langGraphPlatformEndpoint(config: Omit<LangGraphPlatformEndpoint, "type">): LangGraphPlatformEndpoint;
216
281
  declare function resolveEndpointType(endpoint: EndpointDefinition): EndpointType;
217
282
 
218
- export { CopilotRuntimeConstructorParams as C, GraphQLContext as G, CopilotRuntime as a, addCustomHeaderPlugin as b, copilotKitEndpoint as c, CopilotRequestContextProperties as d, CreateCopilotRuntimeServerOptions as e, flattenToolCallsNoDuplicates as f, createContext as g, buildSchema as h, CommonConfig as i, getCommonConfig as j, langGraphPlatformEndpoint as l, resolveEndpointType as r };
283
+ export { CopilotRuntimeConstructorParams as C, GraphQLContext as G, LogLevel as L, CopilotRuntime as a, addCustomHeaderPlugin as b, copilotKitEndpoint as c, CopilotRequestContextProperties as d, CreateCopilotRuntimeServerOptions as e, flattenToolCallsNoDuplicates as f, createContext as g, buildSchema as h, CommonConfig as i, getCommonConfig as j, CopilotRuntimeLogger as k, langGraphPlatformEndpoint as l, createLogger as m, LogLLMRequestData as n, LogLLMResponseData as o, LogLLMErrorData as p, CopilotLoggerHooks as q, resolveEndpointType as r, CopilotLoggingConfig as s };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { i as CommonConfig, d as CopilotRequestContextProperties, a as CopilotRuntime, C as CopilotRuntimeConstructorParams, e as CreateCopilotRuntimeServerOptions, G as GraphQLContext, b as addCustomHeaderPlugin, h as buildSchema, c as copilotKitEndpoint, g as createContext, f as flattenToolCallsNoDuplicates, j as getCommonConfig, l as langGraphPlatformEndpoint, r as resolveEndpointType } from './copilot-runtime-15bfc4f4.js';
1
+ export { i as CommonConfig, q as CopilotLoggerHooks, s as CopilotLoggingConfig, d as CopilotRequestContextProperties, a as CopilotRuntime, C as CopilotRuntimeConstructorParams, k as CopilotRuntimeLogger, e as CreateCopilotRuntimeServerOptions, G as GraphQLContext, p as LogLLMErrorData, n as LogLLMRequestData, o as LogLLMResponseData, L as LogLevel, b as addCustomHeaderPlugin, h as buildSchema, c as copilotKitEndpoint, g as createContext, m as createLogger, f as flattenToolCallsNoDuplicates, j as getCommonConfig, l as langGraphPlatformEndpoint, r as resolveEndpointType } from './copilot-runtime-5103c7e7.js';
2
2
  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-fb9aa3ab.js';
3
3
  export { CopilotRuntimeServerInstance, config, copilotRuntimeNextJSAppRouterEndpoint, copilotRuntimeNextJSPagesRouterEndpoint } from './lib/integrations/index.js';
4
4
  export { copilotRuntimeNodeHttpEndpoint } from './lib/integrations/node-http/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.7.0-next.0",
47
+ version: "1.7.0-next.1",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -150,6 +150,7 @@ __export(src_exports, {
150
150
  copilotRuntimeNodeExpressEndpoint: () => copilotRuntimeNodeExpressEndpoint,
151
151
  copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
152
152
  createContext: () => createContext,
153
+ createLogger: () => createLogger,
153
154
  flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
154
155
  getCommonConfig: () => getCommonConfig,
155
156
  langGraphPlatformEndpoint: () => langGraphPlatformEndpoint,
@@ -4008,6 +4009,7 @@ var CopilotRuntime = class {
4008
4009
  onBeforeRequest;
4009
4010
  onAfterRequest;
4010
4011
  delegateAgentProcessingToServiceAdapter;
4012
+ logging;
4011
4013
  constructor(params) {
4012
4014
  var _a, _b;
4013
4015
  if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints)) {
@@ -4024,11 +4026,14 @@ var CopilotRuntime = class {
4024
4026
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
4025
4027
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
4026
4028
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
4029
+ this.logging = params == null ? void 0 : params.logging;
4027
4030
  }
4028
4031
  async processRuntimeRequest(request) {
4029
- var _a;
4032
+ var _a, _b, _c, _d, _e;
4030
4033
  const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, url, extensions, agentSession, agentStates } = request;
4031
4034
  const eventSource = new RuntimeEventSource();
4035
+ const requestStartTime = Date.now();
4036
+ const streamedChunks = [];
4032
4037
  try {
4033
4038
  if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
4034
4039
  return await this.processAgentRequest(request);
@@ -4043,6 +4048,23 @@ please use an LLM adapter instead.`
4043
4048
  const messages = rawMessages.filter((message) => !message.agentStateMessage);
4044
4049
  const inputMessages = convertGqlInputToMessages(messages);
4045
4050
  const serverSideActions = await this.getServerSideActions(request);
4051
+ if ((_a = this.logging) == null ? void 0 : _a.enabled) {
4052
+ try {
4053
+ const requestData = {
4054
+ threadId,
4055
+ runId,
4056
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
4057
+ messages: inputMessages,
4058
+ actions: clientSideActionsInput,
4059
+ forwardedParameters,
4060
+ timestamp: requestStartTime,
4061
+ provider: this.detectProvider(serviceAdapter)
4062
+ };
4063
+ await this.logging.logger.logRequest(requestData);
4064
+ } catch (error) {
4065
+ console.error("Error logging LLM request:", error);
4066
+ }
4067
+ }
4046
4068
  const serverSideActionsInput = serverSideActions.map((action) => ({
4047
4069
  name: action.name,
4048
4070
  description: action.description,
@@ -4055,7 +4077,7 @@ please use an LLM adapter instead.`
4055
4077
  (action) => action.available !== ActionInputAvailability.remote
4056
4078
  )
4057
4079
  ]);
4058
- await ((_a = this.onBeforeRequest) == null ? void 0 : _a.call(this, {
4080
+ await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
4059
4081
  threadId,
4060
4082
  runId,
4061
4083
  inputMessages,
@@ -4086,6 +4108,69 @@ please use an LLM adapter instead.`
4086
4108
  });
4087
4109
  }).catch((_error) => {
4088
4110
  });
4111
+ if ((_c = this.logging) == null ? void 0 : _c.enabled) {
4112
+ try {
4113
+ outputMessagesPromise.then((outputMessages) => {
4114
+ var _a2;
4115
+ const responseData = {
4116
+ threadId: result.threadId,
4117
+ runId: result.runId,
4118
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
4119
+ // Use collected chunks for progressive mode or outputMessages for regular mode
4120
+ output: this.logging.progressive ? streamedChunks : outputMessages,
4121
+ latency: Date.now() - requestStartTime,
4122
+ timestamp: Date.now(),
4123
+ provider: this.detectProvider(serviceAdapter),
4124
+ // Indicate this is the final response
4125
+ isFinalResponse: true
4126
+ };
4127
+ try {
4128
+ (_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(responseData);
4129
+ } catch (logError) {
4130
+ console.error("Error logging LLM response:", logError);
4131
+ }
4132
+ }).catch((error) => {
4133
+ console.error("Failed to get output messages for logging:", error);
4134
+ });
4135
+ } catch (error) {
4136
+ console.error("Error setting up logging for LLM response:", error);
4137
+ }
4138
+ }
4139
+ if (((_d = this.logging) == null ? void 0 : _d.enabled) && this.logging.progressive) {
4140
+ const originalStream = eventSource.stream.bind(eventSource);
4141
+ eventSource.stream = async (callback) => {
4142
+ await originalStream(async (eventStream$) => {
4143
+ eventStream$.subscribe({
4144
+ next: (event) => {
4145
+ if (event.type === RuntimeEventTypes.TextMessageContent) {
4146
+ streamedChunks.push(event.content);
4147
+ try {
4148
+ const progressiveData = {
4149
+ threadId: threadId || "",
4150
+ runId,
4151
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
4152
+ output: event.content,
4153
+ latency: Date.now() - requestStartTime,
4154
+ timestamp: Date.now(),
4155
+ provider: this.detectProvider(serviceAdapter),
4156
+ isProgressiveChunk: true
4157
+ };
4158
+ Promise.resolve().then(() => {
4159
+ var _a2;
4160
+ (_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(progressiveData);
4161
+ }).catch((error) => {
4162
+ console.error("Error in progressive logging:", error);
4163
+ });
4164
+ } catch (error) {
4165
+ console.error("Error preparing progressive log data:", error);
4166
+ }
4167
+ }
4168
+ }
4169
+ });
4170
+ await callback(eventStream$);
4171
+ });
4172
+ };
4173
+ }
4089
4174
  return {
4090
4175
  threadId: nonEmptyThreadId,
4091
4176
  runId: result.runId,
@@ -4098,6 +4183,22 @@ please use an LLM adapter instead.`
4098
4183
  extensions: result.extensions
4099
4184
  };
4100
4185
  } catch (error) {
4186
+ if ((_e = this.logging) == null ? void 0 : _e.enabled) {
4187
+ try {
4188
+ const errorData = {
4189
+ threadId,
4190
+ runId,
4191
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
4192
+ error: error instanceof Error ? error : String(error),
4193
+ timestamp: Date.now(),
4194
+ latency: Date.now() - requestStartTime,
4195
+ provider: this.detectProvider(serviceAdapter)
4196
+ };
4197
+ await this.logging.logger.logError(errorData);
4198
+ } catch (logError) {
4199
+ console.error("Error logging LLM error:", logError);
4200
+ }
4201
+ }
4101
4202
  if (error instanceof import_shared20.CopilotKitError) {
4102
4203
  throw error;
4103
4204
  }
@@ -4183,7 +4284,6 @@ please use an LLM adapter instead.`
4183
4284
  if (!agentWithEndpoint) {
4184
4285
  throw new Error("Agent not found");
4185
4286
  }
4186
- const headers = createHeaders(null, graphqlContext);
4187
4287
  if (agentWithEndpoint.endpoint.type === EndpointType.LangGraphPlatform) {
4188
4288
  const propertyHeaders = graphqlContext.properties.authorization ? {
4189
4289
  authorization: `Bearer ${graphqlContext.properties.authorization}`
@@ -4369,6 +4469,21 @@ please use an LLM adapter instead.`
4369
4469
  ...remoteActions
4370
4470
  ];
4371
4471
  }
4472
+ // Add helper method to detect provider
4473
+ detectProvider(serviceAdapter) {
4474
+ const adapterName = serviceAdapter.constructor.name;
4475
+ if (adapterName.includes("OpenAI"))
4476
+ return "openai";
4477
+ if (adapterName.includes("Anthropic"))
4478
+ return "anthropic";
4479
+ if (adapterName.includes("Google"))
4480
+ return "google";
4481
+ if (adapterName.includes("Groq"))
4482
+ return "groq";
4483
+ if (adapterName.includes("LangChain"))
4484
+ return "langchain";
4485
+ return void 0;
4486
+ }
4372
4487
  };
4373
4488
  __name(CopilotRuntime, "CopilotRuntime");
4374
4489
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -6187,6 +6302,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
6187
6302
  copilotRuntimeNodeExpressEndpoint,
6188
6303
  copilotRuntimeNodeHttpEndpoint,
6189
6304
  createContext,
6305
+ createLogger,
6190
6306
  flattenToolCallsNoDuplicates,
6191
6307
  getCommonConfig,
6192
6308
  langGraphPlatformEndpoint,