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

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 (84) hide show
  1. package/CHANGELOG.md +0 -53
  2. package/dist/{chunk-XWBDEXDA.mjs → chunk-5BIEM2UU.mjs} +3 -4
  3. package/dist/{chunk-XWBDEXDA.mjs.map → chunk-5BIEM2UU.mjs.map} +1 -1
  4. package/dist/{chunk-PMIAGZGS.mjs → chunk-GSYE3DGY.mjs} +2414 -2928
  5. package/dist/chunk-GSYE3DGY.mjs.map +1 -0
  6. package/dist/{chunk-GS7DO47Q.mjs → chunk-IIXJVVTV.mjs} +78 -155
  7. package/dist/chunk-IIXJVVTV.mjs.map +1 -0
  8. package/dist/{chunk-TOBFVWZU.mjs → chunk-MIPAKFI5.mjs} +2 -2
  9. package/dist/{chunk-VBXBFZEL.mjs → chunk-N24X5I3C.mjs} +2 -2
  10. package/dist/{chunk-6RUTA76W.mjs → chunk-WFYPJXWX.mjs} +2 -2
  11. package/dist/{chunk-5OK4GLKL.mjs → chunk-XDBXF3Q6.mjs} +2 -19
  12. package/dist/chunk-XDBXF3Q6.mjs.map +1 -0
  13. package/dist/{groq-adapter-172a2ca4.d.ts → groq-adapter-25a2bd35.d.ts} +1 -1
  14. package/dist/index.d.ts +3 -4
  15. package/dist/index.js +2598 -3250
  16. package/dist/index.js.map +1 -1
  17. package/dist/index.mjs +8 -12
  18. package/dist/index.mjs.map +1 -1
  19. package/dist/lib/index.d.ts +4 -5
  20. package/dist/lib/index.js +2730 -3339
  21. package/dist/lib/index.js.map +1 -1
  22. package/dist/lib/index.mjs +8 -9
  23. package/dist/lib/integrations/index.d.ts +3 -3
  24. package/dist/lib/integrations/index.js +96 -160
  25. package/dist/lib/integrations/index.js.map +1 -1
  26. package/dist/lib/integrations/index.mjs +6 -7
  27. package/dist/lib/integrations/nest/index.d.ts +2 -2
  28. package/dist/lib/integrations/nest/index.js +96 -160
  29. package/dist/lib/integrations/nest/index.js.map +1 -1
  30. package/dist/lib/integrations/nest/index.mjs +4 -5
  31. package/dist/lib/integrations/node-express/index.d.ts +2 -2
  32. package/dist/lib/integrations/node-express/index.js +96 -160
  33. package/dist/lib/integrations/node-express/index.js.map +1 -1
  34. package/dist/lib/integrations/node-express/index.mjs +4 -5
  35. package/dist/lib/integrations/node-http/index.d.ts +2 -2
  36. package/dist/lib/integrations/node-http/index.js +96 -160
  37. package/dist/lib/integrations/node-http/index.js.map +1 -1
  38. package/dist/lib/integrations/node-http/index.mjs +3 -4
  39. package/dist/service-adapters/index.d.ts +4 -6
  40. package/dist/service-adapters/index.js +107 -225
  41. package/dist/service-adapters/index.js.map +1 -1
  42. package/dist/service-adapters/index.mjs +2 -6
  43. package/dist/{shared-bd953ebf.d.ts → shared-e272b15a.d.ts} +5 -45
  44. package/dist/utils/index.d.ts +1 -17
  45. package/dist/utils/index.js +2 -3
  46. package/dist/utils/index.js.map +1 -1
  47. package/dist/utils/index.mjs +1 -1
  48. package/package.json +2 -2
  49. package/src/agents/langgraph/event-source.ts +38 -36
  50. package/src/agents/langgraph/events.ts +1 -19
  51. package/src/graphql/resolvers/copilot.resolver.ts +45 -108
  52. package/src/graphql/resolvers/state.resolver.ts +3 -3
  53. package/src/lib/integrations/shared.ts +0 -43
  54. package/src/lib/runtime/copilot-runtime.ts +83 -412
  55. package/src/lib/runtime/langgraph/langgraph-agent.ts +0 -12
  56. package/src/lib/runtime/remote-action-constructors.ts +3 -28
  57. package/src/lib/runtime/remote-lg-action.ts +40 -130
  58. package/src/lib/streaming.ts +36 -125
  59. package/src/service-adapters/anthropic/anthropic-adapter.ts +8 -67
  60. package/src/service-adapters/anthropic/utils.ts +8 -3
  61. package/src/service-adapters/events.ts +81 -37
  62. package/src/service-adapters/groq/groq-adapter.ts +56 -66
  63. package/src/service-adapters/index.ts +0 -1
  64. package/src/service-adapters/openai/openai-adapter.ts +3 -18
  65. package/src/utils/failed-response-status-reasons.ts +1 -23
  66. package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +387 -172
  67. package/dist/chunk-5OK4GLKL.mjs.map +0 -1
  68. package/dist/chunk-AMUJQ6IR.mjs +0 -50
  69. package/dist/chunk-AMUJQ6IR.mjs.map +0 -1
  70. package/dist/chunk-GS7DO47Q.mjs.map +0 -1
  71. package/dist/chunk-PMIAGZGS.mjs.map +0 -1
  72. package/dist/service-adapters/shared/index.d.ts +0 -9
  73. package/dist/service-adapters/shared/index.js +0 -72
  74. package/dist/service-adapters/shared/index.js.map +0 -1
  75. package/dist/service-adapters/shared/index.mjs +0 -8
  76. package/dist/service-adapters/shared/index.mjs.map +0 -1
  77. package/src/lib/error-messages.ts +0 -200
  78. package/src/lib/runtime/__tests__/copilot-runtime-trace.test.ts +0 -169
  79. package/src/service-adapters/shared/error-utils.ts +0 -61
  80. package/src/service-adapters/shared/index.ts +0 -1
  81. package/dist/{chunk-TOBFVWZU.mjs.map → chunk-MIPAKFI5.mjs.map} +0 -0
  82. package/dist/{chunk-VBXBFZEL.mjs.map → chunk-N24X5I3C.mjs.map} +0 -0
  83. package/dist/{chunk-6RUTA76W.mjs.map → chunk-WFYPJXWX.mjs.map} +0 -0
  84. package/dist/{langserve-fc5cac89.d.ts → langserve-4a5c9217.d.ts} +7 -7
@@ -1,32 +1,37 @@
1
1
  import {
2
2
  Action,
3
+ randomId,
3
4
  CopilotKitError,
4
5
  CopilotKitErrorCode,
5
6
  CopilotKitLowLevelError,
6
- ensureStructuredError,
7
- randomId,
8
- Severity,
9
7
  } from "@copilotkit/shared";
10
- import { plainToInstance } from "class-transformer";
11
8
  import {
12
- catchError,
9
+ of,
13
10
  concat,
11
+ scan,
14
12
  concatMap,
15
- EMPTY,
16
- firstValueFrom,
17
- from,
18
- of,
19
13
  ReplaySubject,
20
- scan,
21
14
  Subject,
15
+ firstValueFrom,
16
+ from,
17
+ catchError,
18
+ EMPTY,
19
+ BehaviorSubject,
22
20
  } from "rxjs";
23
- import { ActionInput } from "../graphql/inputs/action.input";
24
- import { ActionExecutionMessage, ResultMessage, TextMessage } from "../graphql/types/converted";
21
+ import { streamLangChainResponse } from "./langchain/utils";
25
22
  import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
26
- import { isRemoteAgentAction } from "../lib/runtime/remote-actions";
27
- import { generateHelpfulErrorMessage } from "../lib/streaming";
28
23
  import telemetry from "../lib/telemetry-client";
29
- import { streamLangChainResponse } from "./langchain/utils";
24
+ import { isRemoteAgentAction } from "../lib/runtime/remote-actions";
25
+ import { ActionInput } from "../graphql/inputs/action.input";
26
+ import {
27
+ ActionExecutionMessage,
28
+ ResultMessage,
29
+ TextMessage,
30
+ Message,
31
+ } from "../graphql/types/converted";
32
+ import { plainToInstance } from "class-transformer";
33
+ import { MessageRole } from "../graphql/types/enums";
34
+ import { parseJson, tryMap } from "@copilotkit/shared";
30
35
 
31
36
  export enum RuntimeEventTypes {
32
37
  TextMessageStart = "TextMessageStart",
@@ -268,8 +273,10 @@ export class RuntimeEventSource {
268
273
  threadId: string;
269
274
  }) {
270
275
  this.callback(this.eventStream$).catch((error) => {
271
- // Convert streaming errors to structured errors, but preserve already structured ones
272
- const structuredError = ensureStructuredError(error, convertStreamingErrorToStructured);
276
+ console.error("Error in event source callback", error);
277
+
278
+ // Convert streaming errors to structured errors
279
+ const structuredError = convertStreamingErrorToStructured(error);
273
280
  this.eventStream$.error(structuredError);
274
281
  this.eventStream$.complete();
275
282
  });
@@ -323,16 +330,17 @@ export class RuntimeEventSource {
323
330
  eventWithState.actionExecutionId,
324
331
  actionInputsWithoutAgents,
325
332
  threadId,
326
- ).catch((error) => {});
333
+ ).catch((error) => {
334
+ console.error(error);
335
+ });
327
336
 
328
337
  telemetry.capture("oss.runtime.server_action_executed", {});
329
338
  return concat(of(eventWithState.event!), toolCallEventStream$).pipe(
330
339
  catchError((error) => {
331
- // Convert streaming errors to structured errors and send as action result, but preserve already structured ones
332
- const structuredError = ensureStructuredError(
333
- error,
334
- convertStreamingErrorToStructured,
335
- );
340
+ console.error("Error in tool call stream", error);
341
+
342
+ // Convert streaming errors to structured errors and send as action result
343
+ const structuredError = convertStreamingErrorToStructured(error);
336
344
  toolCallEventStream$.sendActionExecutionResult({
337
345
  actionExecutionId: eventWithState.actionExecutionId!,
338
346
  actionName: eventWithState.action!.name,
@@ -428,8 +436,10 @@ async function executeAction(
428
436
  from(stream).subscribe({
429
437
  next: (event) => eventStream$.next(event),
430
438
  error: (err) => {
431
- // Preserve already structured CopilotKit errors, only convert unstructured errors
432
- const structuredError = ensureStructuredError(err, convertStreamingErrorToStructured);
439
+ console.error("Error in stream", err);
440
+
441
+ // Convert streaming errors to structured errors
442
+ const structuredError = convertStreamingErrorToStructured(err);
433
443
  eventStream$.sendActionExecutionResult({
434
444
  actionExecutionId,
435
445
  actionName: action.name,
@@ -470,31 +480,65 @@ async function executeAction(
470
480
  }
471
481
 
472
482
  function convertStreamingErrorToStructured(error: any): CopilotKitError {
473
- // Determine a more helpful error message based on context
474
- let helpfulMessage = generateHelpfulErrorMessage(error, "event streaming connection");
475
-
476
- // For network-related errors, use CopilotKitLowLevelError to preserve the original error
483
+ // Handle network termination errors
477
484
  if (
478
- error?.message?.includes("fetch failed") ||
479
- error?.message?.includes("ECONNREFUSED") ||
480
- error?.message?.includes("ENOTFOUND") ||
481
- error?.message?.includes("ETIMEDOUT") ||
482
485
  error?.message?.includes("terminated") ||
483
486
  error?.cause?.code === "UND_ERR_SOCKET" ||
484
487
  error?.message?.includes("other side closed") ||
485
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")
486
503
  ) {
487
504
  return new CopilotKitLowLevelError({
488
505
  error: error instanceof Error ? error : new Error(String(error)),
489
506
  url: "event streaming connection",
490
- message: helpfulMessage,
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,
491
536
  });
492
537
  }
493
538
 
494
- // For all other errors, preserve the raw error in a basic CopilotKitError
539
+ // Default: convert unknown streaming errors
495
540
  return new CopilotKitError({
496
- message: helpfulMessage,
541
+ message: `Event streaming error: ${errorMessage}`,
497
542
  code: CopilotKitErrorCode.UNKNOWN,
498
- severity: Severity.CRITICAL,
499
543
  });
500
544
  }
@@ -27,7 +27,6 @@ import {
27
27
  limitMessagesToTokenCount,
28
28
  } from "../openai/utils";
29
29
  import { randomUUID } from "@copilotkit/shared";
30
- import { convertServiceAdapterError } from "../shared";
31
30
 
32
31
  const DEFAULT_MODEL = "llama-3.3-70b-versatile";
33
32
 
@@ -95,85 +94,76 @@ export class GroqAdapter implements CopilotServiceAdapter {
95
94
  function: { name: forwardedParameters.toolChoiceFunctionName },
96
95
  };
97
96
  }
98
- let stream;
99
- try {
100
- stream = await this.groq.chat.completions.create({
101
- model: model,
102
- stream: true,
103
- messages: openaiMessages as unknown as ChatCompletionMessageParam[],
104
- ...(tools.length > 0 && { tools }),
105
- ...(forwardedParameters?.maxTokens && {
106
- max_tokens: forwardedParameters.maxTokens,
107
- }),
108
- ...(forwardedParameters?.stop && { stop: forwardedParameters.stop }),
109
- ...(toolChoice && { tool_choice: toolChoice }),
110
- ...(this.disableParallelToolCalls && { parallel_tool_calls: false }),
111
- ...(forwardedParameters?.temperature && { temperature: forwardedParameters.temperature }),
112
- });
113
- } catch (error) {
114
- throw convertServiceAdapterError(error, "Groq");
115
- }
97
+ const stream = await this.groq.chat.completions.create({
98
+ model: model,
99
+ stream: true,
100
+ messages: openaiMessages as unknown as ChatCompletionMessageParam[],
101
+ ...(tools.length > 0 && { tools }),
102
+ ...(forwardedParameters?.maxTokens && {
103
+ max_tokens: forwardedParameters.maxTokens,
104
+ }),
105
+ ...(forwardedParameters?.stop && { stop: forwardedParameters.stop }),
106
+ ...(toolChoice && { tool_choice: toolChoice }),
107
+ ...(this.disableParallelToolCalls && { parallel_tool_calls: false }),
108
+ ...(forwardedParameters?.temperature && { temperature: forwardedParameters.temperature }),
109
+ });
116
110
 
117
111
  eventSource.stream(async (eventStream$) => {
118
112
  let mode: "function" | "message" | null = null;
119
113
  let currentMessageId: string;
120
114
  let currentToolCallId: string;
121
115
 
122
- try {
123
- for await (const chunk of stream) {
124
- const toolCall = chunk.choices[0].delta.tool_calls?.[0];
125
- const content = chunk.choices[0].delta.content;
126
-
127
- // When switching from message to function or vice versa,
128
- // send the respective end event.
129
- // If toolCall?.id is defined, it means a new tool call starts.
130
- if (mode === "message" && toolCall?.id) {
131
- mode = null;
132
- eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
133
- } else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
134
- mode = null;
135
- eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
136
- }
116
+ for await (const chunk of stream) {
117
+ const toolCall = chunk.choices[0].delta.tool_calls?.[0];
118
+ const content = chunk.choices[0].delta.content;
137
119
 
138
- // If we send a new message type, send the appropriate start event.
139
- if (mode === null) {
140
- if (toolCall?.id) {
141
- mode = "function";
142
- currentToolCallId = toolCall!.id;
143
- eventStream$.sendActionExecutionStart({
144
- actionExecutionId: currentToolCallId,
145
- actionName: toolCall!.function!.name,
146
- parentMessageId: chunk.id,
147
- });
148
- } else if (content) {
149
- mode = "message";
150
- currentMessageId = chunk.id;
151
- eventStream$.sendTextMessageStart({ messageId: currentMessageId });
152
- }
153
- }
120
+ // When switching from message to function or vice versa,
121
+ // send the respective end event.
122
+ // If toolCall?.id is defined, it means a new tool call starts.
123
+ if (mode === "message" && toolCall?.id) {
124
+ mode = null;
125
+ eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
126
+ } else if (mode === "function" && (toolCall === undefined || toolCall?.id)) {
127
+ mode = null;
128
+ eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
129
+ }
154
130
 
155
- // send the content events
156
- if (mode === "message" && content) {
157
- eventStream$.sendTextMessageContent({
158
- messageId: currentMessageId,
159
- content,
160
- });
161
- } else if (mode === "function" && toolCall?.function?.arguments) {
162
- eventStream$.sendActionExecutionArgs({
131
+ // If we send a new message type, send the appropriate start event.
132
+ if (mode === null) {
133
+ if (toolCall?.id) {
134
+ mode = "function";
135
+ currentToolCallId = toolCall!.id;
136
+ eventStream$.sendActionExecutionStart({
163
137
  actionExecutionId: currentToolCallId,
164
- args: toolCall.function.arguments,
138
+ actionName: toolCall!.function!.name,
139
+ parentMessageId: chunk.id,
165
140
  });
141
+ } else if (content) {
142
+ mode = "message";
143
+ currentMessageId = chunk.id;
144
+ eventStream$.sendTextMessageStart({ messageId: currentMessageId });
166
145
  }
167
146
  }
168
147
 
169
- // send the end events
170
- if (mode === "message") {
171
- eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
172
- } else if (mode === "function") {
173
- eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
148
+ // send the content events
149
+ if (mode === "message" && content) {
150
+ eventStream$.sendTextMessageContent({
151
+ messageId: currentMessageId,
152
+ content,
153
+ });
154
+ } else if (mode === "function" && toolCall?.function?.arguments) {
155
+ eventStream$.sendActionExecutionArgs({
156
+ actionExecutionId: currentToolCallId,
157
+ args: toolCall.function.arguments,
158
+ });
174
159
  }
175
- } catch (error) {
176
- throw convertServiceAdapterError(error, "Groq");
160
+ }
161
+
162
+ // send the end events
163
+ if (mode === "message") {
164
+ eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
165
+ } else if (mode === "function") {
166
+ eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
177
167
  }
178
168
 
179
169
  eventStream$.complete();
@@ -5,7 +5,6 @@ export type {
5
5
  } from "./service-adapter";
6
6
  export type { RemoteChainParameters } from "./langchain/langserve";
7
7
  export { RemoteChain } from "./langchain/langserve";
8
- export * from "./shared";
9
8
  export * from "./openai/openai-adapter";
10
9
  export * from "./langchain/langchain-adapter";
11
10
  export * from "./google/google-genai-adapter";
@@ -60,7 +60,6 @@ import {
60
60
  limitMessagesToTokenCount,
61
61
  } from "./utils";
62
62
  import { randomUUID } from "@copilotkit/shared";
63
- import { convertServiceAdapterError } from "../shared";
64
63
 
65
64
  const DEFAULT_MODEL = "gpt-4o";
66
65
 
@@ -129,8 +128,6 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
129
128
  const tools = actions.map(convertActionInputToOpenAITool);
130
129
  const threadId = threadIdFromRequest ?? randomUUID();
131
130
 
132
- console.log("messages", messages);
133
-
134
131
  // ALLOWLIST APPROACH: Only include tool_result messages that correspond to valid tool_calls
135
132
  // Step 1: Extract valid tool_call IDs
136
133
  const validToolUseIds = new Set<string>();
@@ -171,18 +168,6 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
171
168
  };
172
169
  }
173
170
 
174
- console.log("INPUT", {
175
- model: model,
176
- stream: true,
177
- messages: openaiMessages,
178
- ...(tools.length > 0 && { tools }),
179
- ...(forwardedParameters?.maxTokens && { max_tokens: forwardedParameters.maxTokens }),
180
- ...(forwardedParameters?.stop && { stop: forwardedParameters.stop }),
181
- ...(toolChoice && { tool_choice: toolChoice }),
182
- ...(this.disableParallelToolCalls && { parallel_tool_calls: false }),
183
- ...(forwardedParameters?.temperature && { temperature: forwardedParameters.temperature }),
184
- });
185
-
186
171
  try {
187
172
  const stream = this.openai.beta.chat.completions.stream({
188
173
  model: model,
@@ -259,15 +244,15 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
259
244
  eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
260
245
  }
261
246
  } catch (error) {
262
- console.error("[OpenAI] Error during API call:", error);
263
- throw convertServiceAdapterError(error, "OpenAI");
247
+ console.error("[OpenAI] Error processing stream:", error);
248
+ throw error;
264
249
  }
265
250
 
266
251
  eventStream$.complete();
267
252
  });
268
253
  } catch (error) {
269
254
  console.error("[OpenAI] Error during API call:", error);
270
- throw convertServiceAdapterError(error, "OpenAI");
255
+ throw error;
271
256
  }
272
257
 
273
258
  return {
@@ -37,34 +37,12 @@ export class UnknownErrorResponse extends FailedResponseStatus {
37
37
  reason = FailedResponseStatusReason.UNKNOWN_ERROR;
38
38
  declare details: {
39
39
  description?: string;
40
- originalError?: {
41
- code?: string;
42
- statusCode?: number;
43
- severity?: string;
44
- visibility?: string;
45
- originalErrorType?: string;
46
- extensions?: any;
47
- };
48
40
  };
49
41
 
50
- constructor({
51
- description,
52
- originalError,
53
- }: {
54
- description?: string;
55
- originalError?: {
56
- code?: string;
57
- statusCode?: number;
58
- severity?: string;
59
- visibility?: string;
60
- originalErrorType?: string;
61
- extensions?: any;
62
- };
63
- }) {
42
+ constructor({ description }: { description?: string }) {
64
43
  super();
65
44
  this.details = {
66
45
  description,
67
- originalError,
68
46
  };
69
47
  }
70
48
  }