@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;" />
83 lines (81 loc) • 2.36 kB
JavaScript
import {
StatusChecker
} from "./chunk-PMAFHQ7P.mjs";
import {
useErrorToast
} from "./chunk-N4WEHORG.mjs";
import {
UsageBanner,
getErrorActions
} from "./chunk-6ZLPNY7X.mjs";
// src/components/error-boundary/error-boundary.tsx
import React, { useEffect } from "react";
import { CopilotKitError } from "@copilotkit/shared";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var statusChecker = new StatusChecker();
var CopilotErrorBoundary = class extends React.Component {
constructor(props) {
super(props);
this.state = {
hasError: false
};
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidMount() {
if (this.props.publicApiKey) {
statusChecker.start(this.props.publicApiKey, (newStatus) => {
this.setState((prevState) => {
var _a;
if ((newStatus == null ? void 0 : newStatus.severity) !== ((_a = prevState.status) == null ? void 0 : _a.severity)) {
return { status: newStatus != null ? newStatus : void 0 };
}
return null;
});
});
}
}
componentWillUnmount() {
statusChecker.stop();
}
componentDidCatch(error, errorInfo) {
console.error("CopilotKit Error:", error, errorInfo);
}
render() {
var _a, _b, _c, _d;
if (this.state.hasError) {
if (this.state.error instanceof CopilotKitError) {
return /* @__PURE__ */ jsxs(Fragment, { children: [
this.props.children,
this.props.showUsageBanner && /* @__PURE__ */ jsx(
UsageBanner,
{
severity: (_b = (_a = this.state.status) == null ? void 0 : _a.severity) != null ? _b : this.state.error.severity,
message: (_d = (_c = this.state.status) == null ? void 0 : _c.message) != null ? _d : this.state.error.message,
actions: getErrorActions(this.state.error)
}
)
] });
}
throw this.state.error;
}
return this.props.children;
}
};
function ErrorToast({ error, children }) {
const addErrorToast = useErrorToast();
useEffect(() => {
if (error) {
addErrorToast([error]);
}
}, [error, addErrorToast]);
if (!error)
throw error;
return children;
}
export {
CopilotErrorBoundary,
ErrorToast
};
//# sourceMappingURL=chunk-N4VN2B5S.mjs.map