@copilotkit/shared 1.5.12-next.5 → 1.5.12-next.7

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.
@@ -0,0 +1,261 @@
1
+ import { GraphQLError } from "graphql";
2
+
3
+ export const ERROR_NAMES = {
4
+ COPILOT_ERROR: "CopilotError",
5
+ COPILOT_API_DISCOVERY_ERROR: "CopilotApiDiscoveryError",
6
+ COPILOT_REMOTE_ENDPOINT_DISCOVERY_ERROR: "CopilotKitRemoteEndpointDiscoveryError",
7
+ COPILOT_KIT_AGENT_DISCOVERY_ERROR: "CopilotKitAgentDiscoveryError",
8
+ COPILOT_KIT_LOW_LEVEL_ERROR: "CopilotKitLowLevelError",
9
+ RESOLVED_COPILOT_KIT_ERROR: "ResolvedCopilotKitError",
10
+ } as const;
11
+
12
+ export enum CopilotKitErrorCode {
13
+ NETWORK_ERROR = "NETWORK_ERROR",
14
+ NOT_FOUND = "NOT_FOUND",
15
+ AGENT_NOT_FOUND = "AGENT_NOT_FOUND",
16
+ API_NOT_FOUND = "API_NOT_FOUND",
17
+ REMOTE_ENDPOINT_NOT_FOUND = "REMOTE_ENDPOINT_NOT_FOUND",
18
+ MISUSE = "MISUSE",
19
+ UNKNOWN = "UNKNOWN",
20
+ }
21
+
22
+ const BASE_URL = "https://docs.copilotkit.ai";
23
+
24
+ const getSeeMoreMarkdown = (link: string) => `See more: [${link}](${link})`;
25
+
26
+ export const ERROR_CONFIG = {
27
+ [CopilotKitErrorCode.NETWORK_ERROR]: {
28
+ statusCode: 503,
29
+ troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`,
30
+ },
31
+ [CopilotKitErrorCode.NOT_FOUND]: {
32
+ statusCode: 404,
33
+ troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`,
34
+ },
35
+ [CopilotKitErrorCode.AGENT_NOT_FOUND]: {
36
+ statusCode: 500,
37
+ troubleshootingUrl: `${BASE_URL}/coagents/troubleshooting/common-issues#i-am-getting-agent-not-found-error`,
38
+ },
39
+ [CopilotKitErrorCode.API_NOT_FOUND]: {
40
+ statusCode: 404,
41
+ troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`,
42
+ },
43
+ [CopilotKitErrorCode.REMOTE_ENDPOINT_NOT_FOUND]: {
44
+ statusCode: 404,
45
+ troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-copilotkits-remote-endpoint-not-found-error`,
46
+ },
47
+ [CopilotKitErrorCode.MISUSE]: {
48
+ statusCode: 400,
49
+ troubleshootingUrl: null,
50
+ },
51
+ [CopilotKitErrorCode.UNKNOWN]: {
52
+ statusCode: 500,
53
+ },
54
+ };
55
+
56
+ export class CopilotKitError extends GraphQLError {
57
+ code: CopilotKitErrorCode;
58
+ statusCode: number;
59
+
60
+ constructor({
61
+ message = "Unknown error occurred",
62
+ code,
63
+ }: {
64
+ message?: string;
65
+ code: CopilotKitErrorCode;
66
+ }) {
67
+ const name = ERROR_NAMES.COPILOT_ERROR;
68
+ const { statusCode } = ERROR_CONFIG[code];
69
+
70
+ super(message, {
71
+ extensions: {
72
+ name,
73
+ statusCode,
74
+ },
75
+ });
76
+ this.code = code;
77
+ this.name = name;
78
+ this.statusCode = statusCode;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Error thrown when we can identify wrong usage of our components.
84
+ * This helps us notify the developer before real errors can happen
85
+ *
86
+ * @extends CopilotKitError
87
+ */
88
+ export class CopilotKitMisuseError extends CopilotKitError {
89
+ constructor({
90
+ message,
91
+ code = CopilotKitErrorCode.MISUSE,
92
+ }: {
93
+ message: string;
94
+ code?: CopilotKitErrorCode;
95
+ }) {
96
+ const docsLink =
97
+ "troubleshootingUrl" in ERROR_CONFIG[code]
98
+ ? getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl as string)
99
+ : null;
100
+ const finalMessage = docsLink ? `${message}.\n\n${docsLink}` : message;
101
+ super({ message: finalMessage, code });
102
+ this.name = ERROR_NAMES.COPILOT_API_DISCOVERY_ERROR;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Error thrown when the CopilotKit API endpoint cannot be discovered or accessed.
108
+ * This typically occurs when:
109
+ * - The API endpoint URL is invalid or misconfigured
110
+ * - The API service is not running at the expected location
111
+ * - There are network/firewall issues preventing access
112
+ *
113
+ * @extends CopilotKitError
114
+ */
115
+ export class CopilotKitApiDiscoveryError extends CopilotKitError {
116
+ constructor(
117
+ params: {
118
+ message?: string;
119
+ code?: CopilotKitErrorCode.API_NOT_FOUND | CopilotKitErrorCode.REMOTE_ENDPOINT_NOT_FOUND;
120
+ } = {},
121
+ ) {
122
+ const message = params.message ?? "Failed to find CopilotKit API endpoint";
123
+ const code = params.code ?? CopilotKitErrorCode.API_NOT_FOUND;
124
+ const errorMessage = `${message}.\n\n${getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl)}`;
125
+ super({ message: errorMessage, code });
126
+ this.name = ERROR_NAMES.COPILOT_API_DISCOVERY_ERROR;
127
+ }
128
+ }
129
+
130
+ /**
131
+ * This error is used for endpoints specified in runtime's remote endpoints. If they cannot be contacted
132
+ * This typically occurs when:
133
+ * - The API endpoint URL is invalid or misconfigured
134
+ * - The API service is not running at the expected location
135
+ *
136
+ * @extends CopilotKitApiDiscoveryError
137
+ */
138
+ export class CopilotKitRemoteEndpointDiscoveryError extends CopilotKitApiDiscoveryError {
139
+ constructor(params?: { message?: string }) {
140
+ const message = params?.message ?? "Failed to find or contact remote endpoint";
141
+ const code = CopilotKitErrorCode.REMOTE_ENDPOINT_NOT_FOUND;
142
+ super({ message, code });
143
+ this.name = ERROR_NAMES.COPILOT_REMOTE_ENDPOINT_DISCOVERY_ERROR;
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Error thrown when a LangGraph agent cannot be found or accessed.
149
+ * This typically occurs when:
150
+ * - The specified agent name does not exist in the deployment
151
+ * - The agent configuration is invalid or missing
152
+ * - The agent service is not properly deployed or initialized
153
+ *
154
+ * @extends CopilotKitError
155
+ */
156
+ export class CopilotKitAgentDiscoveryError extends CopilotKitError {
157
+ constructor(params?: { agentName?: string }) {
158
+ const code = CopilotKitErrorCode.AGENT_NOT_FOUND;
159
+ const baseMessage = "Failed to find agent";
160
+ const configMessage = "Please verify the agent name exists and is properly configured.";
161
+ const finalMessage = params?.agentName
162
+ ? `${baseMessage} '${params.agentName}'. ${configMessage}\n\n${getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl)}`
163
+ : `${baseMessage}. ${configMessage}\n\n${getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl)}`;
164
+ super({ message: finalMessage || finalMessage, code });
165
+ this.name = ERROR_NAMES.COPILOT_KIT_AGENT_DISCOVERY_ERROR;
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Handles low-level networking errors that occur before a request reaches the server.
171
+ * These errors arise from issues in the underlying communication infrastructure rather than
172
+ * application-level logic or server responses. Typically used to handle "fetch failed" errors
173
+ * where no HTTP status code is available.
174
+ *
175
+ * Common scenarios include:
176
+ * - Connection failures (ECONNREFUSED) when server is down/unreachable
177
+ * - DNS resolution failures (ENOTFOUND) when domain can't be resolved
178
+ * - Timeouts (ETIMEDOUT) when request takes too long
179
+ * - Protocol/transport layer errors like SSL/TLS issues
180
+ */
181
+ export class CopilotKitLowLevelError extends CopilotKitError {
182
+ constructor({ error, url, message }: { error: Error; url: string; message?: string }) {
183
+ let code = CopilotKitErrorCode.NETWORK_ERROR;
184
+
185
+ const getMessageByCode = (errorCode?: string) => {
186
+ const troubleshootingLink = ERROR_CONFIG[code].troubleshootingUrl;
187
+ switch (errorCode) {
188
+ case "ECONNREFUSED":
189
+ return `Connection to ${url} was refused. Ensure the server is running and accessible.\n\n${getSeeMoreMarkdown(troubleshootingLink)}`;
190
+ case "ENOTFOUND":
191
+ return `The server on ${url} could not be found. Check the URL or your network configuration.\n\n${getSeeMoreMarkdown(ERROR_CONFIG[CopilotKitErrorCode.NOT_FOUND].troubleshootingUrl)}`;
192
+ case "ETIMEDOUT":
193
+ return `The connection to ${url} timed out. The server might be overloaded or taking too long to respond.\n\n${getSeeMoreMarkdown(troubleshootingLink)}`;
194
+ default:
195
+ return `Failed to fetch from url ${url}.
196
+
197
+ Possible reasons:
198
+ - -The server might be down or unreachable
199
+ - -There might be a network issue (e.g., DNS failure, connection timeout)
200
+ - -The URL might be incorrect
201
+ - -The server is not running on the specified port
202
+
203
+ ${getSeeMoreMarkdown(troubleshootingLink)}`;
204
+ }
205
+ };
206
+ // @ts-expect-error -- code may exist
207
+ const errorMessage = message ?? getMessageByCode(error.code as string);
208
+
209
+ super({ message: errorMessage, code });
210
+
211
+ this.name = ERROR_NAMES.COPILOT_KIT_LOW_LEVEL_ERROR;
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Generic catch-all error handler for HTTP responses from the CopilotKit API where a status code is available.
217
+ * Used when we receive an HTTP error status and wish to handle broad range of them
218
+ *
219
+ * This differs from CopilotKitLowLevelError in that:
220
+ * - ResolvedCopilotKitError: Server was reached and returned an HTTP status
221
+ * - CopilotKitLowLevelError: Error occurred before reaching server (e.g. network failure)
222
+ *
223
+ * @param status - The HTTP status code received from the API response
224
+ * @param message - Optional error message to include
225
+ * @param code - Optional specific CopilotKitErrorCode to override default behavior
226
+ *
227
+ * Default behavior:
228
+ * - 400 Bad Request: Maps to CopilotKitApiDiscoveryError
229
+ * - All other status codes: Maps to UNKNOWN error code if no specific code provided
230
+ */
231
+ export class ResolvedCopilotKitError extends CopilotKitError {
232
+ constructor({
233
+ status,
234
+ message,
235
+ code,
236
+ isRemoteEndpoint,
237
+ }: {
238
+ status: number;
239
+ message?: string;
240
+ code?: CopilotKitErrorCode;
241
+ isRemoteEndpoint?: boolean;
242
+ }) {
243
+ let resolvedCode = code;
244
+ if (!resolvedCode) {
245
+ switch (status) {
246
+ case 400:
247
+ throw new CopilotKitApiDiscoveryError({ message });
248
+ case 404:
249
+ throw isRemoteEndpoint
250
+ ? new CopilotKitRemoteEndpointDiscoveryError({ message })
251
+ : new CopilotKitApiDiscoveryError({ message });
252
+ default:
253
+ resolvedCode = CopilotKitErrorCode.UNKNOWN;
254
+ super({ message, code: resolvedCode });
255
+ }
256
+ } else {
257
+ super({ message, code: resolvedCode });
258
+ }
259
+ this.name = ERROR_NAMES.RESOLVED_COPILOT_KIT_ERROR;
260
+ }
261
+ }
@@ -1,2 +1,3 @@
1
+ export * from "./errors";
1
2
  export * from "./json-schema";
2
3
  export * from "./random-id";
@@ -1,5 +1,13 @@
1
- import { v4 as uuidv4 } from "uuid";
1
+ import { v4 as uuidv4, validate } from "uuid";
2
2
 
3
3
  export function randomId() {
4
4
  return "ck-" + uuidv4();
5
5
  }
6
+
7
+ export function randomUUID() {
8
+ return uuidv4();
9
+ }
10
+
11
+ export function isValidUUID(uuid: string) {
12
+ return validate(uuid);
13
+ }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=chunk-CYDWEPFL.mjs.map
@@ -1,10 +0,0 @@
1
- // src/utils/random-id.ts
2
- import { v4 as uuidv4 } from "uuid";
3
- function randomId() {
4
- return "ck-" + uuidv4();
5
- }
6
-
7
- export {
8
- randomId
9
- };
10
- //# sourceMappingURL=chunk-RIPX6APP.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/random-id.ts"],"sourcesContent":["import { v4 as uuidv4 } from \"uuid\";\n\nexport function randomId() {\n return \"ck-\" + uuidv4();\n}\n"],"mappings":";AAAA,SAAS,MAAM,cAAc;AAEtB,SAAS,WAAW;AACzB,SAAO,QAAQ,OAAO;AACxB;","names":[]}