@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;" />
147 lines (145 loc) • 5.46 kB
JavaScript
import {
shouldShowDevConsole
} from "./chunk-ICIK2BSB.mjs";
import {
useToast
} from "./chunk-EFL5OBKN.mjs";
import {
__async,
__objRest,
__spreadProps,
__spreadValues
} from "./chunk-SKC7AJIV.mjs";
// src/hooks/use-copilot-runtime-client.ts
import {
CopilotRuntimeClient
} from "@copilotkit/runtime-client-gql";
import { useMemo, useRef } from "react";
import {
ErrorVisibility,
CopilotKitApiDiscoveryError,
CopilotKitRemoteEndpointDiscoveryError,
CopilotKitAgentDiscoveryError,
CopilotKitError,
CopilotKitErrorCode
} from "@copilotkit/shared";
var useCopilotRuntimeClient = (options) => {
const { setBannerError } = useToast();
const _a = options, { showDevConsole, onError } = _a, runtimeOptions = __objRest(_a, ["showDevConsole", "onError"]);
const lastStructuredErrorRef = useRef(null);
const traceUIError = (error, originalError) => __async(void 0, null, function* () {
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 = useMemo(() => {
return new 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 === 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 CopilotKitError({
message: gqlError.message,
code: 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 CopilotKitError({
message: (error == null ? void 0 : error.message) || String(error),
code: CopilotKitErrorCode.UNKNOWN
});
setBannerError(fallbackError);
traceUIError(fallbackError, error);
}
}
},
handleGQLWarning: (message) => {
console.warn(message);
const warningError = new CopilotKitError({
message,
code: 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 CopilotKitError({ message, code });
}
if ((_a = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _a.includes("CopilotApiDiscoveryError")) {
return new CopilotKitApiDiscoveryError({ message });
}
if ((_b = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _b.includes("CopilotKitRemoteEndpointDiscoveryError")) {
return new CopilotKitRemoteEndpointDiscoveryError({ message });
}
if ((_c = originalError == null ? void 0 : originalError.stack) == null ? void 0 : _c.includes("CopilotKitAgentDiscoveryError")) {
return new CopilotKitAgentDiscoveryError({
agentName: "",
availableAgents: []
});
}
return null;
}
export {
useCopilotRuntimeClient
};
//# sourceMappingURL=chunk-Q3MCVRO3.mjs.map