UNPKG

@copilotkit/shared

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

443 lines (430 loc) • 17.6 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/errors.ts var errors_exports = {}; __export(errors_exports, { BANNER_ERROR_NAMES: () => BANNER_ERROR_NAMES, COPILOT_CLOUD_ERROR_NAMES: () => COPILOT_CLOUD_ERROR_NAMES, ConfigurationError: () => ConfigurationError, CopilotKitAgentDiscoveryError: () => CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError: () => CopilotKitApiDiscoveryError, CopilotKitError: () => CopilotKitError, CopilotKitErrorCode: () => CopilotKitErrorCode, CopilotKitLowLevelError: () => CopilotKitLowLevelError, CopilotKitMisuseError: () => CopilotKitMisuseError, CopilotKitRemoteEndpointDiscoveryError: () => CopilotKitRemoteEndpointDiscoveryError, CopilotKitVersionMismatchError: () => CopilotKitVersionMismatchError, ERROR_CONFIG: () => ERROR_CONFIG, ERROR_NAMES: () => ERROR_NAMES, ErrorVisibility: () => ErrorVisibility, MissingPublicApiKeyError: () => MissingPublicApiKeyError, ResolvedCopilotKitError: () => ResolvedCopilotKitError, Severity: () => Severity, UpgradeRequiredError: () => UpgradeRequiredError, ensureStructuredError: () => ensureStructuredError, getPossibleVersionMismatch: () => getPossibleVersionMismatch, isStructuredCopilotKitError: () => isStructuredCopilotKitError }); module.exports = __toCommonJS(errors_exports); var import_graphql = require("graphql"); // package.json var version = "1.10.5"; // src/index.ts var COPILOTKIT_VERSION = version; // src/utils/errors.ts var Severity = /* @__PURE__ */ ((Severity2) => { Severity2["CRITICAL"] = "critical"; Severity2["WARNING"] = "warning"; Severity2["INFO"] = "info"; return Severity2; })(Severity || {}); var ErrorVisibility = /* @__PURE__ */ ((ErrorVisibility2) => { ErrorVisibility2["BANNER"] = "banner"; ErrorVisibility2["TOAST"] = "toast"; ErrorVisibility2["SILENT"] = "silent"; ErrorVisibility2["DEV_ONLY"] = "dev_only"; return ErrorVisibility2; })(ErrorVisibility || {}); var ERROR_NAMES = { COPILOT_ERROR: "CopilotError", COPILOT_API_DISCOVERY_ERROR: "CopilotApiDiscoveryError", COPILOT_REMOTE_ENDPOINT_DISCOVERY_ERROR: "CopilotKitRemoteEndpointDiscoveryError", COPILOT_KIT_AGENT_DISCOVERY_ERROR: "CopilotKitAgentDiscoveryError", COPILOT_KIT_LOW_LEVEL_ERROR: "CopilotKitLowLevelError", COPILOT_KIT_VERSION_MISMATCH_ERROR: "CopilotKitVersionMismatchError", RESOLVED_COPILOT_KIT_ERROR: "ResolvedCopilotKitError", CONFIGURATION_ERROR: "ConfigurationError", MISSING_PUBLIC_API_KEY_ERROR: "MissingPublicApiKeyError", UPGRADE_REQUIRED_ERROR: "UpgradeRequiredError" }; var BANNER_ERROR_NAMES = [ ERROR_NAMES.CONFIGURATION_ERROR, ERROR_NAMES.MISSING_PUBLIC_API_KEY_ERROR, ERROR_NAMES.UPGRADE_REQUIRED_ERROR, ERROR_NAMES.COPILOT_API_DISCOVERY_ERROR, ERROR_NAMES.COPILOT_REMOTE_ENDPOINT_DISCOVERY_ERROR, ERROR_NAMES.COPILOT_KIT_AGENT_DISCOVERY_ERROR ]; var COPILOT_CLOUD_ERROR_NAMES = BANNER_ERROR_NAMES; var CopilotKitErrorCode = /* @__PURE__ */ ((CopilotKitErrorCode2) => { CopilotKitErrorCode2["NETWORK_ERROR"] = "NETWORK_ERROR"; CopilotKitErrorCode2["NOT_FOUND"] = "NOT_FOUND"; CopilotKitErrorCode2["AGENT_NOT_FOUND"] = "AGENT_NOT_FOUND"; CopilotKitErrorCode2["API_NOT_FOUND"] = "API_NOT_FOUND"; CopilotKitErrorCode2["REMOTE_ENDPOINT_NOT_FOUND"] = "REMOTE_ENDPOINT_NOT_FOUND"; CopilotKitErrorCode2["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR"; CopilotKitErrorCode2["MISUSE"] = "MISUSE"; CopilotKitErrorCode2["UNKNOWN"] = "UNKNOWN"; CopilotKitErrorCode2["VERSION_MISMATCH"] = "VERSION_MISMATCH"; CopilotKitErrorCode2["CONFIGURATION_ERROR"] = "CONFIGURATION_ERROR"; CopilotKitErrorCode2["MISSING_PUBLIC_API_KEY_ERROR"] = "MISSING_PUBLIC_API_KEY_ERROR"; CopilotKitErrorCode2["UPGRADE_REQUIRED_ERROR"] = "UPGRADE_REQUIRED_ERROR"; return CopilotKitErrorCode2; })(CopilotKitErrorCode || {}); var BASE_URL = "https://docs.copilotkit.ai"; var getSeeMoreMarkdown = (link) => `See more: [${link}](${link})`; var ERROR_CONFIG = { ["NETWORK_ERROR" /* NETWORK_ERROR */]: { statusCode: 503, troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["NOT_FOUND" /* NOT_FOUND */]: { statusCode: 404, troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */]: { statusCode: 500, troubleshootingUrl: `${BASE_URL}/coagents/troubleshooting/common-issues#i-am-getting-agent-not-found-error`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["API_NOT_FOUND" /* API_NOT_FOUND */]: { statusCode: 404, troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-a-network-errors--api-not-found`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["REMOTE_ENDPOINT_NOT_FOUND" /* REMOTE_ENDPOINT_NOT_FOUND */]: { statusCode: 404, troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#i-am-getting-copilotkits-remote-endpoint-not-found-error`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["AUTHENTICATION_ERROR" /* AUTHENTICATION_ERROR */]: { statusCode: 401, troubleshootingUrl: `${BASE_URL}/troubleshooting/common-issues#authentication-errors`, visibility: "banner" /* BANNER */, severity: "critical" /* CRITICAL */ }, ["MISUSE" /* MISUSE */]: { statusCode: 400, troubleshootingUrl: null, visibility: "dev_only" /* DEV_ONLY */, severity: "warning" /* WARNING */ }, ["UNKNOWN" /* UNKNOWN */]: { statusCode: 500, visibility: "toast" /* TOAST */, severity: "critical" /* CRITICAL */ }, ["CONFIGURATION_ERROR" /* CONFIGURATION_ERROR */]: { statusCode: 400, troubleshootingUrl: null, severity: "warning" /* WARNING */, visibility: "banner" /* BANNER */ }, ["MISSING_PUBLIC_API_KEY_ERROR" /* MISSING_PUBLIC_API_KEY_ERROR */]: { statusCode: 400, troubleshootingUrl: null, severity: "critical" /* CRITICAL */, visibility: "banner" /* BANNER */ }, ["UPGRADE_REQUIRED_ERROR" /* UPGRADE_REQUIRED_ERROR */]: { statusCode: 402, troubleshootingUrl: null, severity: "warning" /* WARNING */, visibility: "banner" /* BANNER */ }, ["VERSION_MISMATCH" /* VERSION_MISMATCH */]: { statusCode: 400, troubleshootingUrl: null, visibility: "dev_only" /* DEV_ONLY */, severity: "info" /* INFO */ } }; var CopilotKitError = class extends import_graphql.GraphQLError { constructor({ message = "Unknown error occurred", code, severity, visibility }) { const name = ERROR_NAMES.COPILOT_ERROR; const config = ERROR_CONFIG[code]; const { statusCode } = config; const resolvedVisibility = visibility ?? config.visibility ?? "toast" /* TOAST */; const resolvedSeverity = severity ?? ("severity" in config ? config.severity : void 0); super(message, { extensions: { name, statusCode, code, visibility: resolvedVisibility, severity: resolvedSeverity, troubleshootingUrl: "troubleshootingUrl" in config ? config.troubleshootingUrl : null, originalError: { message, stack: new Error().stack } } }); this.code = code; this.name = name; this.statusCode = statusCode; this.severity = resolvedSeverity; this.visibility = resolvedVisibility; } }; var CopilotKitMisuseError = class extends CopilotKitError { constructor({ message, code = "MISUSE" /* MISUSE */ }) { const docsLink = "troubleshootingUrl" in ERROR_CONFIG[code] && ERROR_CONFIG[code].troubleshootingUrl ? getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl) : null; const finalMessage = docsLink ? `${message}. ${docsLink}` : message; super({ message: finalMessage, code }); this.name = ERROR_NAMES.COPILOT_API_DISCOVERY_ERROR; } }; var getVersionMismatchErrorMessage = ({ reactCoreVersion, runtimeVersion, runtimeClientGqlVersion }) => `Version mismatch detected: @copilotkit/runtime@${runtimeVersion ?? ""} is not compatible with @copilotkit/react-core@${reactCoreVersion} and @copilotkit/runtime-client-gql@${runtimeClientGqlVersion}. Please ensure all installed copilotkit packages are on the same version.`; var CopilotKitVersionMismatchError = class extends CopilotKitError { constructor({ reactCoreVersion, runtimeVersion, runtimeClientGqlVersion }) { const code = "VERSION_MISMATCH" /* VERSION_MISMATCH */; super({ message: getVersionMismatchErrorMessage({ reactCoreVersion, runtimeVersion, runtimeClientGqlVersion }), code }); this.name = ERROR_NAMES.COPILOT_KIT_VERSION_MISMATCH_ERROR; } }; var CopilotKitApiDiscoveryError = class extends CopilotKitError { constructor(params = {}) { const url = params.url ?? ""; let operationSuffix = ""; if (url == null ? void 0 : url.includes("/info")) operationSuffix = `when fetching CopilotKit info`; else if (url.includes("/actions/execute")) operationSuffix = `when attempting to execute actions.`; else if (url.includes("/agents/state")) operationSuffix = `when attempting to get agent state.`; else if (url.includes("/agents/execute")) operationSuffix = `when attempting to execute agent(s).`; const message = params.message ?? (params.url ? `Failed to find CopilotKit API endpoint at url ${params.url} ${operationSuffix}` : `Failed to find CopilotKit API endpoint.`); const code = params.code ?? "API_NOT_FOUND" /* API_NOT_FOUND */; const errorMessage = `${message}. ${getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl)}`; super({ message: errorMessage, code }); this.name = ERROR_NAMES.COPILOT_API_DISCOVERY_ERROR; } }; var CopilotKitRemoteEndpointDiscoveryError = class extends CopilotKitApiDiscoveryError { constructor(params) { const message = (params == null ? void 0 : params.message) ?? ((params == null ? void 0 : params.url) ? `Failed to find or contact remote endpoint at url ${params.url}` : "Failed to find or contact remote endpoint"); const code = "REMOTE_ENDPOINT_NOT_FOUND" /* REMOTE_ENDPOINT_NOT_FOUND */; super({ message, code }); this.name = ERROR_NAMES.COPILOT_REMOTE_ENDPOINT_DISCOVERY_ERROR; } }; var CopilotKitAgentDiscoveryError = class extends CopilotKitError { constructor(params) { const { agentName, availableAgents } = params; const code = "AGENT_NOT_FOUND" /* AGENT_NOT_FOUND */; const seeMore = getSeeMoreMarkdown(ERROR_CONFIG[code].troubleshootingUrl); let message; if (availableAgents.length) { const agentList = availableAgents.map((agent) => agent.name).join(", "); if (agentName) { message = `Agent '${agentName}' was not found. Available agents are: ${agentList}. Please verify the agent name in your configuration and ensure it matches one of the available agents. ${seeMore}`; } else { message = `The requested agent was not found. Available agents are: ${agentList}. Please verify the agent name in your configuration and ensure it matches one of the available agents. ${seeMore}`; } } else { message = `${agentName ? `Agent '${agentName}'` : "The requested agent"} was not found. Please set up at least one agent before proceeding. ${seeMore}`; } super({ message, code }); this.name = ERROR_NAMES.COPILOT_KIT_AGENT_DISCOVERY_ERROR; } }; var CopilotKitLowLevelError = class extends CopilotKitError { constructor({ error, url, message }) { let code = "NETWORK_ERROR" /* NETWORK_ERROR */; const errorCode = error.code; const errorMessage = message ?? resolveLowLevelErrorMessage({ errorCode, url }); super({ message: errorMessage, code }); this.name = ERROR_NAMES.COPILOT_KIT_LOW_LEVEL_ERROR; } }; var ResolvedCopilotKitError = class extends CopilotKitError { constructor({ status, message, code, isRemoteEndpoint, url }) { let resolvedCode = code; if (!resolvedCode) { switch (status) { case 400: throw new CopilotKitApiDiscoveryError({ message, url }); case 404: throw isRemoteEndpoint ? new CopilotKitRemoteEndpointDiscoveryError({ message, url }) : new CopilotKitApiDiscoveryError({ message, url }); default: resolvedCode = "UNKNOWN" /* UNKNOWN */; break; } } super({ message, code: resolvedCode }); this.name = ERROR_NAMES.RESOLVED_COPILOT_KIT_ERROR; } }; var ConfigurationError = class extends CopilotKitError { constructor(message) { super({ message, code: "CONFIGURATION_ERROR" /* CONFIGURATION_ERROR */ }); this.name = ERROR_NAMES.CONFIGURATION_ERROR; this.severity = "warning" /* WARNING */; } }; var MissingPublicApiKeyError = class extends ConfigurationError { constructor(message) { super(message); this.name = ERROR_NAMES.MISSING_PUBLIC_API_KEY_ERROR; this.severity = "critical" /* CRITICAL */; } }; var UpgradeRequiredError = class extends ConfigurationError { constructor(message) { super(message); this.name = ERROR_NAMES.UPGRADE_REQUIRED_ERROR; this.severity = "warning" /* WARNING */; } }; function isStructuredCopilotKitError(error) { var _a; return error instanceof CopilotKitError || error instanceof CopilotKitLowLevelError || (error == null ? void 0 : error.name) && error.name.includes("CopilotKit") || ((_a = error == null ? void 0 : error.extensions) == null ? void 0 : _a.code) !== void 0; } function ensureStructuredError(error, converter) { return isStructuredCopilotKitError(error) ? error : converter(error); } async function getPossibleVersionMismatch({ runtimeVersion, runtimeClientGqlVersion }) { if (!runtimeVersion || runtimeVersion === "" || !runtimeClientGqlVersion) return; if (COPILOTKIT_VERSION !== runtimeVersion || COPILOTKIT_VERSION !== runtimeClientGqlVersion || runtimeVersion !== runtimeClientGqlVersion) { return { runtimeVersion, runtimeClientGqlVersion, reactCoreVersion: COPILOTKIT_VERSION, message: getVersionMismatchErrorMessage({ runtimeVersion, runtimeClientGqlVersion, reactCoreVersion: COPILOTKIT_VERSION }) }; } return; } var resolveLowLevelErrorMessage = ({ errorCode, url }) => { const troubleshootingLink = ERROR_CONFIG["NETWORK_ERROR" /* NETWORK_ERROR */].troubleshootingUrl; const genericMessage = (description = `Failed to fetch from url ${url}.`) => `${description}. Possible reasons: - -The server may have an error preventing it from returning a response (Check the server logs for more info). - -The server might be down or unreachable - -There might be a network issue (e.g., DNS failure, connection timeout) - -The URL might be incorrect - -The server is not running on the specified port ${getSeeMoreMarkdown(troubleshootingLink)}`; if (url.includes("/info")) return genericMessage(`Failed to fetch CopilotKit agents/action information from url ${url}.`); if (url.includes("/actions/execute")) return genericMessage(`Fetch call to ${url} to execute actions failed.`); if (url.includes("/agents/state")) return genericMessage(`Fetch call to ${url} to get agent state failed.`); if (url.includes("/agents/execute")) return genericMessage(`Fetch call to ${url} to execute agent(s) failed.`); switch (errorCode) { case "ECONNREFUSED": return `Connection to ${url} was refused. Ensure the server is running and accessible. ${getSeeMoreMarkdown(troubleshootingLink)}`; case "ENOTFOUND": return `The server on ${url} could not be found. Check the URL or your network configuration. ${getSeeMoreMarkdown(ERROR_CONFIG["NOT_FOUND" /* NOT_FOUND */].troubleshootingUrl)}`; case "ETIMEDOUT": return `The connection to ${url} timed out. The server might be overloaded or taking too long to respond. ${getSeeMoreMarkdown(troubleshootingLink)}`; default: return; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BANNER_ERROR_NAMES, COPILOT_CLOUD_ERROR_NAMES, ConfigurationError, CopilotKitAgentDiscoveryError, CopilotKitApiDiscoveryError, CopilotKitError, CopilotKitErrorCode, CopilotKitLowLevelError, CopilotKitMisuseError, CopilotKitRemoteEndpointDiscoveryError, CopilotKitVersionMismatchError, ERROR_CONFIG, ERROR_NAMES, ErrorVisibility, MissingPublicApiKeyError, ResolvedCopilotKitError, Severity, UpgradeRequiredError, ensureStructuredError, getPossibleVersionMismatch, isStructuredCopilotKitError }); //# sourceMappingURL=errors.js.map