@copilotkit/runtime 1.6.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 (50) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +75 -0
  3. package/dist/{chunk-WUMAYJP3.mjs → chunk-D6YNY2XB.mjs} +2 -2
  4. package/dist/chunk-PTC5JN3P.mjs +1 -0
  5. package/dist/{chunk-DUW72ZZB.mjs → chunk-QTRO3GPV.mjs} +206 -70
  6. package/dist/chunk-QTRO3GPV.mjs.map +1 -0
  7. package/dist/{chunk-MPI4JZZR.mjs → chunk-QZ6X33MR.mjs} +2 -2
  8. package/dist/{chunk-2RP2NR4F.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 +206 -69
  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 +206 -69
  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 +4 -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 +4 -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 +4 -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 +4 -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/graphql/resolvers/copilot.resolver.ts +4 -0
  37. package/src/lib/index.ts +1 -0
  38. package/src/lib/logger.ts +48 -0
  39. package/src/lib/runtime/__tests__/remote-action-constructors.test.ts +45 -35
  40. package/src/lib/runtime/copilot-runtime.ts +176 -16
  41. package/src/lib/runtime/remote-action-constructors.ts +28 -68
  42. package/src/lib/runtime/remote-actions.ts +5 -5
  43. package/src/lib/streaming.ts +59 -0
  44. package/src/service-adapters/events.ts +3 -3
  45. package/dist/chunk-DFOKBSIS.mjs +0 -1
  46. package/dist/chunk-DUW72ZZB.mjs.map +0 -1
  47. /package/dist/{chunk-WUMAYJP3.mjs.map → chunk-D6YNY2XB.mjs.map} +0 -0
  48. /package/dist/{chunk-DFOKBSIS.mjs.map → chunk-PTC5JN3P.mjs.map} +0 -0
  49. /package/dist/{chunk-MPI4JZZR.mjs.map → chunk-QZ6X33MR.mjs.map} +0 -0
  50. /package/dist/{chunk-2RP2NR4F.mjs.map → chunk-RQS3BGAT.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @copilotkit/runtime
2
2
 
3
+ ## 1.7.0-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 1e934e4: - feat(runtime): Add LangFuse logging integration
8
+ - @copilotkit/shared@1.7.0-next.1
9
+
10
+ ## 1.7.0-next.0
11
+
12
+ ### Minor Changes
13
+
14
+ - 082db7a: - CrewAI support
15
+
16
+ ### Patch Changes
17
+
18
+ - 082db7a: - Added RemoteAgentAdapter and implemented CopilotKit (protocol & events).
19
+ - Integrated CrewAI (with a prototype, HITL, event system, and chat/demo tweaks).
20
+ - Updated docs and cleaned up code (fixing stdout issues, restricting Python version, and streamlining demos).
21
+ - @copilotkit/shared@1.7.0-next.0
22
+
3
23
  ## 1.6.0
4
24
 
5
25
  ### Minor Changes
@@ -24,6 +44,14 @@
24
44
  - Updated dependencies [090203d]
25
45
  - @copilotkit/shared@1.6.0
26
46
 
47
+ ## 1.6.0-next.12
48
+
49
+ ### Patch Changes
50
+
51
+ - 1bb9ca2: - fix(coagents): don't fail when LangSmith API key is missing
52
+ - fix(coagents): don't check for langsmithApiKey in resolveEndpointType
53
+ - @copilotkit/shared@1.6.0-next.12
54
+
27
55
  ## 1.6.0-next.11
28
56
 
29
57
  ### Patch Changes
package/README.md CHANGED
@@ -44,3 +44,78 @@
44
44
  # Documentation
45
45
 
46
46
  To get started with CopilotKit, please check out the [documentation](https://docs.copilotkit.ai).
47
+
48
+ ## LangFuse Logging Integration
49
+
50
+ CopilotKit now supports LangFuse logging integration to help you monitor, analyze, and debug your LLM requests and responses.
51
+
52
+ ### Setup
53
+
54
+ To enable LangFuse logging, you can configure it when initializing the CopilotRuntime:
55
+
56
+ ```typescript
57
+ import { CopilotRuntime, OpenAIAdapter } from "@copilotkit/runtime";
58
+ import { LangfuseClient } from "langfuse";
59
+
60
+ // Initialize your LangFuse client
61
+ const langfuse = new LangfuseClient({
62
+ publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
63
+ secretKey: process.env.LANGFUSE_SECRET_KEY!,
64
+ baseUrl: process.env.LANGFUSE_BASE_URL,
65
+ });
66
+
67
+ // Create a CopilotRuntime with LangFuse logging enabled
68
+ const runtime = new CopilotRuntime({
69
+ adapter: new OpenAIAdapter({ apiKey: process.env.OPENAI_API_KEY }),
70
+ logging: {
71
+ enabled: true,
72
+ progressive: true, // Set to false for buffered logging
73
+ logger: {
74
+ logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
75
+ logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
76
+ logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
77
+ },
78
+ },
79
+ });
80
+ ```
81
+
82
+ ### Configuration Options
83
+
84
+ The logging configuration accepts the following options:
85
+
86
+ - `enabled` (boolean): Enable or disable logging (default: false)
87
+ - `progressive` (boolean): When true, logs each chunk as it's streamed. When false, logs the complete response (default: true)
88
+ - `logger` (object): Contains callback functions for logging:
89
+ - `logRequest`: Called when an LLM request is made
90
+ - `logResponse`: Called when an LLM response is received
91
+ - `logError`: Called when an error occurs during an LLM request
92
+
93
+ ### Custom Logging Integrations
94
+
95
+ You can integrate with any logging service by implementing the logger interface:
96
+
97
+ ```typescript
98
+ const runtime = new CopilotRuntime({
99
+ adapter: new OpenAIAdapter({ apiKey: "YOUR_API_KEY" }),
100
+ logging: {
101
+ enabled: true,
102
+ progressive: false,
103
+ logger: {
104
+ logRequest: (data) => {
105
+ // Implement your custom logging logic
106
+ console.log("LLM Request:", JSON.stringify(data));
107
+ },
108
+ logResponse: (data) => {
109
+ // Implement your custom logging logic
110
+ console.log("LLM Response:", JSON.stringify(data));
111
+ },
112
+ logError: (error) => {
113
+ // Implement your custom error logging
114
+ console.error("LLM Error:", error);
115
+ },
116
+ },
117
+ },
118
+ });
119
+ ```
120
+
121
+ This allows you to send your logs to any system or service that you prefer.
@@ -2,7 +2,7 @@ import {
2
2
  getCommonConfig,
3
3
  getRuntimeInstanceTelemetryInfo,
4
4
  telemetry_client_default
5
- } from "./chunk-DUW72ZZB.mjs";
5
+ } from "./chunk-QTRO3GPV.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-WUMAYJP3.mjs.map
80
+ //# sourceMappingURL=chunk-D6YNY2XB.mjs.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-PTC5JN3P.mjs.map
@@ -39,7 +39,7 @@ var require_package = __commonJS({
39
39
  publishConfig: {
40
40
  access: "public"
41
41
  },
42
- version: "1.6.0",
42
+ version: "1.7.0-next.1",
43
43
  sideEffects: false,
44
44
  main: "./dist/index.js",
45
45
  module: "./dist/index.mjs",
@@ -1815,7 +1815,7 @@ async function streamEvents(controller, args) {
1815
1815
  let emitIntermediateStateUntilEnd = null;
1816
1816
  let shouldExit = false;
1817
1817
  let externalRunId = null;
1818
- const streamResponse2 = client.runs.stream(threadId, assistantId, payload);
1818
+ const streamResponse = client.runs.stream(threadId, assistantId, payload);
1819
1819
  const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
1820
1820
  let latestStateValues = {};
1821
1821
  let updatedState = state;
@@ -1825,7 +1825,7 @@ async function streamEvents(controller, args) {
1825
1825
  telemetry_client_default.capture("oss.runtime.agent_execution_stream_started", {
1826
1826
  hashedLgcKey: streamInfo.hashedLgcKey
1827
1827
  });
1828
- for await (const chunk of streamResponse2) {
1828
+ for await (const chunk of streamResponse) {
1829
1829
  if (![
1830
1830
  "events",
1831
1831
  "values",
@@ -2240,6 +2240,54 @@ __name(getSchemaKeys, "getSchemaKeys");
2240
2240
 
2241
2241
  // src/lib/runtime/remote-action-constructors.ts
2242
2242
  import { CopilotKitError, CopilotKitLowLevelError } from "@copilotkit/shared";
2243
+
2244
+ // src/lib/streaming.ts
2245
+ async function writeJsonLineResponseToEventStream(response, eventStream$) {
2246
+ const reader = response.getReader();
2247
+ const decoder = new TextDecoder();
2248
+ let buffer = [];
2249
+ function flushBuffer() {
2250
+ const currentBuffer = buffer.join("");
2251
+ if (currentBuffer.trim().length === 0) {
2252
+ return;
2253
+ }
2254
+ const parts = currentBuffer.split("\n");
2255
+ if (parts.length === 0) {
2256
+ return;
2257
+ }
2258
+ const lastPartIsComplete = currentBuffer.endsWith("\n");
2259
+ buffer = [];
2260
+ if (!lastPartIsComplete) {
2261
+ buffer.push(parts.pop());
2262
+ }
2263
+ parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
2264
+ eventStream$.next(JSON.parse(part));
2265
+ });
2266
+ }
2267
+ __name(flushBuffer, "flushBuffer");
2268
+ try {
2269
+ while (true) {
2270
+ const { done, value } = await reader.read();
2271
+ if (!done) {
2272
+ buffer.push(decoder.decode(value, {
2273
+ stream: true
2274
+ }));
2275
+ }
2276
+ flushBuffer();
2277
+ if (done) {
2278
+ break;
2279
+ }
2280
+ }
2281
+ } catch (error) {
2282
+ console.error("Error in stream", error);
2283
+ eventStream$.error(error);
2284
+ return;
2285
+ }
2286
+ eventStream$.complete();
2287
+ }
2288
+ __name(writeJsonLineResponseToEventStream, "writeJsonLineResponseToEventStream");
2289
+
2290
+ // src/lib/runtime/remote-action-constructors.ts
2243
2291
  import { CopilotKitApiDiscoveryError, ResolvedCopilotKitError } from "@copilotkit/shared";
2244
2292
  import { parseJson as parseJson2, tryMap } from "@copilotkit/shared";
2245
2293
  function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
@@ -2249,7 +2297,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
2249
2297
  parameters: [],
2250
2298
  handler: async (_args) => {
2251
2299
  },
2252
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
2300
+ remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
2253
2301
  logger2.debug({
2254
2302
  actionName: agent.name
2255
2303
  }, "Executing LangGraph Platform agent");
@@ -2293,7 +2341,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
2293
2341
  metaEvents
2294
2342
  });
2295
2343
  const eventSource = new RemoteLangGraphEventSource();
2296
- streamResponse(response, eventSource.eventStream$);
2344
+ writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
2297
2345
  return eventSource.processLangGraphEvents();
2298
2346
  } catch (error) {
2299
2347
  logger2.error({
@@ -2310,6 +2358,11 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
2310
2358
  ];
2311
2359
  }
2312
2360
  __name(constructLGCRemoteAction, "constructLGCRemoteAction");
2361
+ var RemoteAgentType;
2362
+ (function(RemoteAgentType2) {
2363
+ RemoteAgentType2["LangGraph"] = "langgraph";
2364
+ RemoteAgentType2["CrewAI"] = "crewai";
2365
+ })(RemoteAgentType || (RemoteAgentType = {}));
2313
2366
  function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
2314
2367
  const totalAgents = Array.isArray(json["agents"]) ? json["agents"].length : 0;
2315
2368
  const actions = json["actions"].map((action) => ({
@@ -2379,7 +2432,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
2379
2432
  parameters: [],
2380
2433
  handler: async (_args) => {
2381
2434
  },
2382
- langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
2435
+ remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
2383
2436
  logger2.debug({
2384
2437
  actionName: agent.name
2385
2438
  }, "Executing remote agent");
@@ -2439,9 +2492,17 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
2439
2492
  isRemoteEndpoint: true
2440
2493
  });
2441
2494
  }
2442
- const eventSource = new RemoteLangGraphEventSource();
2443
- streamResponse(response.body, eventSource.eventStream$);
2444
- return eventSource.processLangGraphEvents();
2495
+ if (agent.type === "langgraph") {
2496
+ const eventSource = new RemoteLangGraphEventSource();
2497
+ writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
2498
+ return eventSource.processLangGraphEvents();
2499
+ } else if (agent.type === "crewai") {
2500
+ const eventStream$ = new RuntimeEventSubject();
2501
+ writeJsonLineResponseToEventStream(response.body, eventStream$);
2502
+ return eventStream$;
2503
+ } else {
2504
+ throw new Error("Unsupported agent type");
2505
+ }
2445
2506
  } catch (error) {
2446
2507
  if (error instanceof CopilotKitError) {
2447
2508
  throw error;
@@ -2459,50 +2520,6 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
2459
2520
  ];
2460
2521
  }
2461
2522
  __name(constructRemoteActions, "constructRemoteActions");
2462
- async function streamResponse(response, eventStream$) {
2463
- const reader = response.getReader();
2464
- const decoder = new TextDecoder();
2465
- let buffer = [];
2466
- function flushBuffer() {
2467
- const currentBuffer = buffer.join("");
2468
- if (currentBuffer.trim().length === 0) {
2469
- return;
2470
- }
2471
- const parts = currentBuffer.split("\n");
2472
- if (parts.length === 0) {
2473
- return;
2474
- }
2475
- const lastPartIsComplete = currentBuffer.endsWith("\n");
2476
- buffer = [];
2477
- if (!lastPartIsComplete) {
2478
- buffer.push(parts.pop());
2479
- }
2480
- parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
2481
- eventStream$.next(JSON.parse(part));
2482
- });
2483
- }
2484
- __name(flushBuffer, "flushBuffer");
2485
- try {
2486
- while (true) {
2487
- const { done, value } = await reader.read();
2488
- if (!done) {
2489
- buffer.push(decoder.decode(value, {
2490
- stream: true
2491
- }));
2492
- }
2493
- flushBuffer();
2494
- if (done) {
2495
- break;
2496
- }
2497
- }
2498
- } catch (error) {
2499
- console.error("Error in stream", error);
2500
- eventStream$.error(error);
2501
- return;
2502
- }
2503
- eventStream$.complete();
2504
- }
2505
- __name(streamResponse, "streamResponse");
2506
2523
  function createHeaders(onBeforeRequest, graphqlContext) {
2507
2524
  const headers = {
2508
2525
  "Content-Type": "application/json"
@@ -2526,13 +2543,13 @@ var EndpointType;
2526
2543
  EndpointType2["CopilotKit"] = "copilotKit";
2527
2544
  EndpointType2["LangGraphPlatform"] = "langgraph-platform";
2528
2545
  })(EndpointType || (EndpointType = {}));
2529
- function isLangGraphAgentAction(action) {
2546
+ function isRemoteAgentAction(action) {
2530
2547
  if (!action) {
2531
2548
  return false;
2532
2549
  }
2533
- return typeof action.langGraphAgentHandler === "function";
2550
+ return typeof action.remoteAgentHandler === "function";
2534
2551
  }
2535
- __name(isLangGraphAgentAction, "isLangGraphAgentAction");
2552
+ __name(isRemoteAgentAction, "isRemoteAgentAction");
2536
2553
  async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
2537
2554
  logger2.debug({
2538
2555
  url
@@ -2695,6 +2712,7 @@ var CopilotRuntime = class {
2695
2712
  onBeforeRequest;
2696
2713
  onAfterRequest;
2697
2714
  delegateAgentProcessingToServiceAdapter;
2715
+ logging;
2698
2716
  constructor(params) {
2699
2717
  var _a, _b;
2700
2718
  if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints)) {
@@ -2711,11 +2729,14 @@ var CopilotRuntime = class {
2711
2729
  this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
2712
2730
  this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
2713
2731
  this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
2732
+ this.logging = params == null ? void 0 : params.logging;
2714
2733
  }
2715
2734
  async processRuntimeRequest(request) {
2716
- var _a;
2735
+ var _a, _b, _c, _d, _e;
2717
2736
  const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, url, extensions, agentSession, agentStates } = request;
2718
2737
  const eventSource = new RuntimeEventSource();
2738
+ const requestStartTime = Date.now();
2739
+ const streamedChunks = [];
2719
2740
  try {
2720
2741
  if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
2721
2742
  return await this.processAgentRequest(request);
@@ -2730,6 +2751,23 @@ please use an LLM adapter instead.`
2730
2751
  const messages = rawMessages.filter((message) => !message.agentStateMessage);
2731
2752
  const inputMessages = convertGqlInputToMessages(messages);
2732
2753
  const serverSideActions = await this.getServerSideActions(request);
2754
+ if ((_a = this.logging) == null ? void 0 : _a.enabled) {
2755
+ try {
2756
+ const requestData = {
2757
+ threadId,
2758
+ runId,
2759
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
2760
+ messages: inputMessages,
2761
+ actions: clientSideActionsInput,
2762
+ forwardedParameters,
2763
+ timestamp: requestStartTime,
2764
+ provider: this.detectProvider(serviceAdapter)
2765
+ };
2766
+ await this.logging.logger.logRequest(requestData);
2767
+ } catch (error) {
2768
+ console.error("Error logging LLM request:", error);
2769
+ }
2770
+ }
2733
2771
  const serverSideActionsInput = serverSideActions.map((action) => ({
2734
2772
  name: action.name,
2735
2773
  description: action.description,
@@ -2742,7 +2780,7 @@ please use an LLM adapter instead.`
2742
2780
  (action) => action.available !== ActionInputAvailability.remote
2743
2781
  )
2744
2782
  ]);
2745
- await ((_a = this.onBeforeRequest) == null ? void 0 : _a.call(this, {
2783
+ await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
2746
2784
  threadId,
2747
2785
  runId,
2748
2786
  inputMessages,
@@ -2773,6 +2811,69 @@ please use an LLM adapter instead.`
2773
2811
  });
2774
2812
  }).catch((_error) => {
2775
2813
  });
2814
+ if ((_c = this.logging) == null ? void 0 : _c.enabled) {
2815
+ try {
2816
+ outputMessagesPromise.then((outputMessages) => {
2817
+ var _a2;
2818
+ const responseData = {
2819
+ threadId: result.threadId,
2820
+ runId: result.runId,
2821
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
2822
+ // Use collected chunks for progressive mode or outputMessages for regular mode
2823
+ output: this.logging.progressive ? streamedChunks : outputMessages,
2824
+ latency: Date.now() - requestStartTime,
2825
+ timestamp: Date.now(),
2826
+ provider: this.detectProvider(serviceAdapter),
2827
+ // Indicate this is the final response
2828
+ isFinalResponse: true
2829
+ };
2830
+ try {
2831
+ (_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(responseData);
2832
+ } catch (logError) {
2833
+ console.error("Error logging LLM response:", logError);
2834
+ }
2835
+ }).catch((error) => {
2836
+ console.error("Failed to get output messages for logging:", error);
2837
+ });
2838
+ } catch (error) {
2839
+ console.error("Error setting up logging for LLM response:", error);
2840
+ }
2841
+ }
2842
+ if (((_d = this.logging) == null ? void 0 : _d.enabled) && this.logging.progressive) {
2843
+ const originalStream = eventSource.stream.bind(eventSource);
2844
+ eventSource.stream = async (callback) => {
2845
+ await originalStream(async (eventStream$) => {
2846
+ eventStream$.subscribe({
2847
+ next: (event) => {
2848
+ if (event.type === RuntimeEventTypes.TextMessageContent) {
2849
+ streamedChunks.push(event.content);
2850
+ try {
2851
+ const progressiveData = {
2852
+ threadId: threadId || "",
2853
+ runId,
2854
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
2855
+ output: event.content,
2856
+ latency: Date.now() - requestStartTime,
2857
+ timestamp: Date.now(),
2858
+ provider: this.detectProvider(serviceAdapter),
2859
+ isProgressiveChunk: true
2860
+ };
2861
+ Promise.resolve().then(() => {
2862
+ var _a2;
2863
+ (_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(progressiveData);
2864
+ }).catch((error) => {
2865
+ console.error("Error in progressive logging:", error);
2866
+ });
2867
+ } catch (error) {
2868
+ console.error("Error preparing progressive log data:", error);
2869
+ }
2870
+ }
2871
+ }
2872
+ });
2873
+ await callback(eventStream$);
2874
+ });
2875
+ };
2876
+ }
2776
2877
  return {
2777
2878
  threadId: nonEmptyThreadId,
2778
2879
  runId: result.runId,
@@ -2785,6 +2886,22 @@ please use an LLM adapter instead.`
2785
2886
  extensions: result.extensions
2786
2887
  };
2787
2888
  } catch (error) {
2889
+ if ((_e = this.logging) == null ? void 0 : _e.enabled) {
2890
+ try {
2891
+ const errorData = {
2892
+ threadId,
2893
+ runId,
2894
+ model: forwardedParameters == null ? void 0 : forwardedParameters.model,
2895
+ error: error instanceof Error ? error : String(error),
2896
+ timestamp: Date.now(),
2897
+ latency: Date.now() - requestStartTime,
2898
+ provider: this.detectProvider(serviceAdapter)
2899
+ };
2900
+ await this.logging.logger.logError(errorData);
2901
+ } catch (logError) {
2902
+ console.error("Error logging LLM error:", logError);
2903
+ }
2904
+ }
2788
2905
  if (error instanceof CopilotKitError3) {
2789
2906
  throw error;
2790
2907
  }
@@ -2794,7 +2911,6 @@ please use an LLM adapter instead.`
2794
2911
  }
2795
2912
  }
2796
2913
  async discoverAgentsFromEndpoints(graphqlContext) {
2797
- const headers = createHeaders(null, graphqlContext);
2798
2914
  const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
2799
2915
  const agents2 = await acc;
2800
2916
  if (endpoint.type === EndpointType.LangGraphPlatform) {
@@ -2820,11 +2936,12 @@ please use an LLM adapter instead.`
2820
2936
  ...endpointAgents
2821
2937
  ];
2822
2938
  }
2939
+ const cpkEndpoint = endpoint;
2823
2940
  const fetchUrl = `${endpoint.url}/info`;
2824
2941
  try {
2825
2942
  const response = await fetch(fetchUrl, {
2826
2943
  method: "POST",
2827
- headers,
2944
+ headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
2828
2945
  body: JSON.stringify({
2829
2946
  properties: graphqlContext.properties
2830
2947
  })
@@ -2870,7 +2987,6 @@ please use an LLM adapter instead.`
2870
2987
  if (!agentWithEndpoint) {
2871
2988
  throw new Error("Agent not found");
2872
2989
  }
2873
- const headers = createHeaders(null, graphqlContext);
2874
2990
  if (agentWithEndpoint.endpoint.type === EndpointType.LangGraphPlatform) {
2875
2991
  const propertyHeaders = graphqlContext.properties.authorization ? {
2876
2992
  authorization: `Bearer ${graphqlContext.properties.authorization}`
@@ -2905,11 +3021,12 @@ please use an LLM adapter instead.`
2905
3021
  };
2906
3022
  }
2907
3023
  } else if (agentWithEndpoint.endpoint.type === EndpointType.CopilotKit || !("type" in agentWithEndpoint.endpoint)) {
2908
- const fetchUrl = `${agentWithEndpoint.endpoint.url}/agents/state`;
3024
+ const cpkEndpoint = agentWithEndpoint.endpoint;
3025
+ const fetchUrl = `${cpkEndpoint.url}/agents/state`;
2909
3026
  try {
2910
3027
  const response = await fetch(fetchUrl, {
2911
3028
  method: "POST",
2912
- headers,
3029
+ headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
2913
3030
  body: JSON.stringify({
2914
3031
  properties: graphqlContext.properties,
2915
3032
  threadId,
@@ -2954,7 +3071,7 @@ please use an LLM adapter instead.`
2954
3071
  const threadId = threadIdFromRequest ?? agentSession.threadId;
2955
3072
  const serverSideActions = await this.getServerSideActions(request);
2956
3073
  const messages = convertGqlInputToMessages(rawMessages);
2957
- const currentAgent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
3074
+ const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
2958
3075
  if (!currentAgent) {
2959
3076
  throw new CopilotKitAgentDiscoveryError({
2960
3077
  agentName
@@ -2962,8 +3079,8 @@ please use an LLM adapter instead.`
2962
3079
  }
2963
3080
  const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
2964
3081
  // Case 1: Keep all regular (non-agent) actions
2965
- !isLangGraphAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
2966
- isLangGraphAgentAction(action) && action.name !== agentName
3082
+ !isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
3083
+ isRemoteAgentAction(action) && action.name !== agentName
2967
3084
  )).map((action) => ({
2968
3085
  name: action.name,
2969
3086
  description: action.description,
@@ -2981,7 +3098,7 @@ please use an LLM adapter instead.`
2981
3098
  }));
2982
3099
  try {
2983
3100
  const eventSource = new RuntimeEventSource();
2984
- const stream = await currentAgent.langGraphAgentHandler({
3101
+ const stream = await currentAgent.remoteAgentHandler({
2985
3102
  name: agentName,
2986
3103
  threadId,
2987
3104
  nodeName,
@@ -3055,6 +3172,21 @@ please use an LLM adapter instead.`
3055
3172
  ...remoteActions
3056
3173
  ];
3057
3174
  }
3175
+ // Add helper method to detect provider
3176
+ detectProvider(serviceAdapter) {
3177
+ const adapterName = serviceAdapter.constructor.name;
3178
+ if (adapterName.includes("OpenAI"))
3179
+ return "openai";
3180
+ if (adapterName.includes("Anthropic"))
3181
+ return "anthropic";
3182
+ if (adapterName.includes("Google"))
3183
+ return "google";
3184
+ if (adapterName.includes("Groq"))
3185
+ return "groq";
3186
+ if (adapterName.includes("LangChain"))
3187
+ return "langchain";
3188
+ return void 0;
3189
+ }
3058
3190
  };
3059
3191
  __name(CopilotRuntime, "CopilotRuntime");
3060
3192
  function flattenToolCallsNoDuplicates(toolsByPriority) {
@@ -3352,7 +3484,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
3352
3484
  return;
3353
3485
  }
3354
3486
  }
3355
- if (isLangGraphAgentAction(action)) {
3487
+ if (isRemoteAgentAction(action)) {
3356
3488
  const result = `${action.name} agent started`;
3357
3489
  const agentExecution = plainToInstance2(ActionExecutionMessage, {
3358
3490
  id: actionExecutionId,
@@ -3373,7 +3505,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
3373
3505
  actionName: action.name,
3374
3506
  result
3375
3507
  });
3376
- const stream = await action.langGraphAgentHandler({
3508
+ const stream = await action.remoteAgentHandler({
3377
3509
  name: action.name,
3378
3510
  threadId,
3379
3511
  actionInputsWithoutAgents,
@@ -3611,6 +3743,9 @@ var CopilotResolver = class {
3611
3743
  resolveOutputMessagesPromise = resolve;
3612
3744
  rejectOutputMessagesPromise = reject;
3613
3745
  });
3746
+ if (copilotCloudPublicApiKey) {
3747
+ ctx.properties["copilotCloudPublicApiKey"] = copilotCloudPublicApiKey;
3748
+ }
3614
3749
  logger2.debug("Processing");
3615
3750
  const { eventSource, threadId = randomId4(), runId, serverSideActions, actionInputsWithoutAgents, extensions } = await copilotRuntime.processRuntimeRequest({
3616
3751
  serviceAdapter,
@@ -4299,10 +4434,11 @@ export {
4299
4434
  copilotKitEndpoint,
4300
4435
  langGraphPlatformEndpoint,
4301
4436
  resolveEndpointType,
4437
+ createLogger,
4302
4438
  addCustomHeaderPlugin,
4303
4439
  createContext,
4304
4440
  buildSchema,
4305
4441
  getCommonConfig,
4306
4442
  copilotRuntimeNodeHttpEndpoint
4307
4443
  };
4308
- //# sourceMappingURL=chunk-DUW72ZZB.mjs.map
4444
+ //# sourceMappingURL=chunk-QTRO3GPV.mjs.map