@copilotkit/react-core
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
230 lines (225 loc) • 9.15 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
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);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/hooks/use-copilot-runtime-client.ts
var use_copilot_runtime_client_exports = {};
__export(use_copilot_runtime_client_exports, {
useCopilotRuntimeClient: () => useCopilotRuntimeClient
});
module.exports = __toCommonJS(use_copilot_runtime_client_exports);
var import_runtime_client_gql = require("@copilotkit/runtime-client-gql");
// src/components/toast/toast-provider.tsx
var import_react = require("react");
var import_shared = require("@copilotkit/shared");
var import_jsx_runtime = require("react/jsx-runtime");
var ToastContext = (0, import_react.createContext)(void 0);
function useToast() {
const context = (0, import_react.useContext)(ToastContext);
if (!context) {
throw new Error("useToast must be used within a ToastProvider");
}
return context;
}
// src/hooks/use-copilot-runtime-client.ts
var import_react2 = require("react");
var import_shared2 = require("@copilotkit/shared");
// src/utils/dev-console.ts
function isLocalhost() {
if (typeof window === "undefined")
return false;
return window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname === "0.0.0.0";
}
function shouldShowDevConsole(showDevConsole) {
if (showDevConsole !== void 0) {
return showDevConsole;
}
return isLocalhost();
}
// src/hooks/use-copilot-runtime-client.ts
var useCopilotRuntimeClient = (options) => {
const { setBannerError } = useToast();
const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
const lastStructuredErrorRef = (0, import_react2.useRef)(null);
const traceUIError = (error, originalError) => __async(void 0, null, function* () {
if (!onError || !runtimeOptions.publicApiKey)
return;
try {
const errorEvent = {
type: "error",
timestamp: Date.now(),
context: {
source: "ui",
request: {
operation: "runtimeClient",
url: runtimeOptions.url,
startTime: Date.now()
},
technical: {
environment: "browser",
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0,
stackTrace: originalError instanceof Error ? originalError.stack : void 0
}
},
error
};
yield onError(errorEvent);
} catch (error2) {
console.error("Error in onError handler:", error2);
}
});
const runtimeClient = (0, import_react2.useMemo)(() => {
return new import_runtime_client_gql.CopilotRuntimeClient(__spreadProps(__spreadValues({}, runtimeOptions), {
handleGQLErrors: (error) => {
var _a2;
if ((_a2 = error.graphQLErrors) == null ? void 0 : _a2.length) {
const graphQLErrors = error.graphQLErrors;
const routeError = (gqlError) => {
const extensions = gqlError.extensions;
const visibility = extensions == null ? void 0 : extensions.visibility;
const isDev = shouldShowDevConsole(showDevConsole != null ? showDevConsole : false);
if (visibility === import_shared2.ErrorVisibility.SILENT) {
console.error("CopilotKit Silent Error:", gqlError.message);
return;
}
if (!isDev) {
console.error("CopilotKit Error (hidden in production):", gqlError.message);
return;
}
const now = Date.now();
const errorMessage = gqlError.message;
if (lastStructuredErrorRef.current && lastStructuredErrorRef.current.message === errorMessage && now - lastStructuredErrorRef.current.timestamp < 150) {
return;
}
lastStructuredErrorRef.current = { message: errorMessage, timestamp: now };
const ckError = createStructuredError(gqlError);
if (ckError) {
setBannerError(ckError);
traceUIError(ckError, gqlError);
} else {
const fallbackError = new import_shared2.CopilotKitError({
message: gqlError.message,
code: import_shared2.CopilotKitErrorCode.UNKNOWN
});
setBannerError(fallbackError);
traceUIError(fallbackError, gqlError);
}
};
graphQLErrors.forEach(routeError);
} else {
const isDev = shouldShowDevConsole(showDevConsole != null ? showDevConsole : false);
if (!isDev) {
console.error("CopilotKit Error (hidden in production):", error);
} else {
const fallbackError = new import_shared2.CopilotKitError({
message: (error == null ? void 0 : error.message) || String(error),
code: import_shared2.CopilotKitErrorCode.UNKNOWN
});
setBannerError(fallbackError);
traceUIError(fallbackError, error);
}
}
},
handleGQLWarning: (message) => {
console.warn(message);
const warningError = new import_shared2.CopilotKitError({
message,
code: import_shared2.CopilotKitErrorCode.UNKNOWN
});
setBannerError(warningError);
}
}));
}, [runtimeOptions, setBannerError, showDevConsole, onError]);
return runtimeClient;
};
function createStructuredError(gqlError) {
var _a, _b, _c;
const extensions = gqlError.extensions;
const originalError = extensions == null ? void 0 : extensions.originalError;
const message = (originalError == null ? void 0 : originalError.message) || gqlError.message;
const code = extensions == null ? void 0 : extensions.code;
if (code) {
return new import_shared2.CopilotKitError({ message, code });
}
if ((_a = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _a.includes("CopilotApiDiscoveryError")) {
return new import_shared2.CopilotKitApiDiscoveryError({ message });
}
if ((_b = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _b.includes("CopilotKitRemoteEndpointDiscoveryError")) {
return new import_shared2.CopilotKitRemoteEndpointDiscoveryError({ message });
}
if ((_c = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _c.includes("CopilotKitAgentDiscoveryError")) {
return new import_shared2.CopilotKitAgentDiscoveryError({
agentName: "",
availableAgents: []
});
}
return null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useCopilotRuntimeClient
});
//# sourceMappingURL=use-copilot-runtime-client.js.map