@copilotkit/runtime 1.9.1 → 1.9.2-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 (47) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/{chunk-2CND6WGV.mjs → chunk-C3SWOFLO.mjs} +2 -2
  3. package/dist/{chunk-EVF3MXEK.mjs → chunk-KPFOAXRX.mjs} +2 -2
  4. package/dist/{chunk-RVLXQ2V5.mjs → chunk-RIPQZJB5.mjs} +2 -2
  5. package/dist/{chunk-GOPTDPPB.mjs → chunk-XGBY45FP.mjs} +265 -61
  6. package/dist/chunk-XGBY45FP.mjs.map +1 -0
  7. package/dist/{chunk-BHNTR222.mjs → chunk-YV3YXRMR.mjs} +2 -2
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.js +310 -106
  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 +303 -99
  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 +16 -7
  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 +16 -7
  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 +16 -7
  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 +16 -7
  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-0c31d7c5.d.ts → shared-e272b15a.d.ts} +1 -0
  33. package/package.json +2 -2
  34. package/src/agents/langgraph/event-source.ts +10 -1
  35. package/src/graphql/resolvers/copilot.resolver.ts +13 -0
  36. package/src/graphql/resolvers/state.resolver.ts +5 -7
  37. package/src/lib/runtime/copilot-runtime.ts +70 -6
  38. package/src/lib/runtime/remote-action-constructors.ts +37 -28
  39. package/src/lib/runtime/remote-lg-action.ts +45 -24
  40. package/src/lib/runtime/retry-utils.ts +96 -0
  41. package/src/lib/streaming.ts +54 -1
  42. package/src/service-adapters/events.ts +101 -6
  43. package/dist/chunk-GOPTDPPB.mjs.map +0 -1
  44. /package/dist/{chunk-2CND6WGV.mjs.map → chunk-C3SWOFLO.mjs.map} +0 -0
  45. /package/dist/{chunk-EVF3MXEK.mjs.map → chunk-KPFOAXRX.mjs.map} +0 -0
  46. /package/dist/{chunk-RVLXQ2V5.mjs.map → chunk-RIPQZJB5.mjs.map} +0 -0
  47. /package/dist/{chunk-BHNTR222.mjs.map → chunk-YV3YXRMR.mjs.map} +0 -0
@@ -1,4 +1,10 @@
1
- import { Action, randomId } from "@copilotkit/shared";
1
+ import {
2
+ Action,
3
+ randomId,
4
+ CopilotKitError,
5
+ CopilotKitErrorCode,
6
+ CopilotKitLowLevelError,
7
+ } from "@copilotkit/shared";
2
8
  import {
3
9
  of,
4
10
  concat,
@@ -10,14 +16,22 @@ import {
10
16
  from,
11
17
  catchError,
12
18
  EMPTY,
19
+ BehaviorSubject,
13
20
  } from "rxjs";
14
21
  import { streamLangChainResponse } from "./langchain/utils";
15
22
  import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
16
23
  import telemetry from "../lib/telemetry-client";
17
24
  import { isRemoteAgentAction } from "../lib/runtime/remote-actions";
18
25
  import { ActionInput } from "../graphql/inputs/action.input";
19
- import { ActionExecutionMessage, ResultMessage, TextMessage } from "../graphql/types/converted";
26
+ import {
27
+ ActionExecutionMessage,
28
+ ResultMessage,
29
+ TextMessage,
30
+ Message,
31
+ } from "../graphql/types/converted";
20
32
  import { plainToInstance } from "class-transformer";
33
+ import { MessageRole } from "../graphql/types/enums";
34
+ import { parseJson, tryMap } from "@copilotkit/shared";
21
35
 
22
36
  export enum RuntimeEventTypes {
23
37
  TextMessageStart = "TextMessageStart",
@@ -260,7 +274,10 @@ export class RuntimeEventSource {
260
274
  }) {
261
275
  this.callback(this.eventStream$).catch((error) => {
262
276
  console.error("Error in event source callback", error);
263
- this.sendErrorMessageToChat();
277
+
278
+ // Convert streaming errors to structured errors
279
+ const structuredError = convertStreamingErrorToStructured(error);
280
+ this.eventStream$.error(structuredError);
264
281
  this.eventStream$.complete();
265
282
  });
266
283
  return this.eventStream$.pipe(
@@ -321,7 +338,18 @@ export class RuntimeEventSource {
321
338
  return concat(of(eventWithState.event!), toolCallEventStream$).pipe(
322
339
  catchError((error) => {
323
340
  console.error("Error in tool call stream", error);
324
- this.sendErrorMessageToChat();
341
+
342
+ // Convert streaming errors to structured errors and send as action result
343
+ const structuredError = convertStreamingErrorToStructured(error);
344
+ toolCallEventStream$.sendActionExecutionResult({
345
+ actionExecutionId: eventWithState.actionExecutionId!,
346
+ actionName: eventWithState.action!.name,
347
+ error: {
348
+ code: structuredError.code,
349
+ message: structuredError.message,
350
+ },
351
+ });
352
+
325
353
  return EMPTY;
326
354
  }),
327
355
  );
@@ -409,12 +437,15 @@ async function executeAction(
409
437
  next: (event) => eventStream$.next(event),
410
438
  error: (err) => {
411
439
  console.error("Error in stream", err);
440
+
441
+ // Convert streaming errors to structured errors
442
+ const structuredError = convertStreamingErrorToStructured(err);
412
443
  eventStream$.sendActionExecutionResult({
413
444
  actionExecutionId,
414
445
  actionName: action.name,
415
446
  error: {
416
- code: "STREAM_ERROR",
417
- message: err.message,
447
+ code: structuredError.code,
448
+ message: structuredError.message,
418
449
  },
419
450
  });
420
451
  eventStream$.complete();
@@ -447,3 +478,67 @@ async function executeAction(
447
478
  }
448
479
  }
449
480
  }
481
+
482
+ function convertStreamingErrorToStructured(error: any): CopilotKitError {
483
+ // Handle network termination errors
484
+ if (
485
+ error?.message?.includes("terminated") ||
486
+ error?.cause?.code === "UND_ERR_SOCKET" ||
487
+ error?.message?.includes("other side closed") ||
488
+ error?.code === "UND_ERR_SOCKET"
489
+ ) {
490
+ return new CopilotKitError({
491
+ message:
492
+ "Connection to agent was unexpectedly terminated. This may be due to the agent service being restarted or network issues. Please try again.",
493
+ code: CopilotKitErrorCode.NETWORK_ERROR,
494
+ });
495
+ }
496
+
497
+ // Handle other network-related errors
498
+ if (
499
+ error?.message?.includes("fetch failed") ||
500
+ error?.message?.includes("ECONNREFUSED") ||
501
+ error?.message?.includes("ENOTFOUND") ||
502
+ error?.message?.includes("ETIMEDOUT")
503
+ ) {
504
+ return new CopilotKitLowLevelError({
505
+ error: error instanceof Error ? error : new Error(String(error)),
506
+ url: "event streaming connection",
507
+ message:
508
+ "Network error occurred during event streaming. Please check your connection and try again.",
509
+ });
510
+ }
511
+
512
+ // Handle abort/cancellation errors (these are usually normal)
513
+ if (
514
+ error?.message?.includes("aborted") ||
515
+ error?.message?.includes("canceled") ||
516
+ error?.message?.includes("signal is aborted")
517
+ ) {
518
+ return new CopilotKitError({
519
+ message: "Request was cancelled",
520
+ code: CopilotKitErrorCode.UNKNOWN,
521
+ });
522
+ }
523
+
524
+ // Handle API key errors (authentication/authorization issues)
525
+ const errorMessage = error?.message || String(error);
526
+ if (
527
+ errorMessage.includes("401") ||
528
+ errorMessage.toLowerCase().includes("api key") ||
529
+ errorMessage.toLowerCase().includes("unauthorized") ||
530
+ errorMessage.toLowerCase().includes("authentication") ||
531
+ errorMessage.toLowerCase().includes("incorrect api key")
532
+ ) {
533
+ return new CopilotKitError({
534
+ message: `Event streaming error: ${errorMessage}`,
535
+ code: CopilotKitErrorCode.MISSING_PUBLIC_API_KEY_ERROR,
536
+ });
537
+ }
538
+
539
+ // Default: convert unknown streaming errors
540
+ return new CopilotKitError({
541
+ message: `Event streaming error: ${errorMessage}`,
542
+ code: CopilotKitErrorCode.UNKNOWN,
543
+ });
544
+ }