@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;" />
102 lines (100 loc) • 3.04 kB
JavaScript
import {
ExclamationMarkIcon
} from "./chunk-O7ARI5CV.mjs";
import {
useToast
} from "./chunk-EFL5OBKN.mjs";
import {
__async
} from "./chunk-SKC7AJIV.mjs";
// src/components/error-boundary/error-utils.tsx
import { useCallback } from "react";
import ReactMarkdown from "react-markdown";
import { jsx, jsxs } from "react/jsx-runtime";
function ErrorToast({ errors }) {
const errorsToRender = errors.map((error, idx) => {
var _a, _b, _c;
const originalError = "extensions" in error ? (_a = error.extensions) == null ? void 0 : _a.originalError : {};
const message = (_b = originalError == null ? void 0 : originalError.message) != null ? _b : error.message;
const code = "extensions" in error ? (_c = error.extensions) == null ? void 0 : _c.code : null;
return /* @__PURE__ */ jsxs(
"div",
{
style: {
marginTop: idx === 0 ? 0 : 10,
marginBottom: 14
},
children: [
/* @__PURE__ */ jsx(ExclamationMarkIcon, { style: { marginBottom: 4 } }),
code && /* @__PURE__ */ jsxs(
"div",
{
style: {
fontWeight: "600",
marginBottom: 4
},
children: [
"Copilot Runtime Error:",
" ",
/* @__PURE__ */ jsx("span", { style: { fontFamily: "monospace", fontWeight: "normal" }, children: code })
]
}
),
/* @__PURE__ */ jsx(ReactMarkdown, { children: message })
]
},
idx
);
});
return /* @__PURE__ */ jsxs(
"div",
{
style: {
fontSize: "13px",
maxWidth: "600px"
},
children: [
errorsToRender,
/* @__PURE__ */ jsx("div", { style: { fontSize: "11px", opacity: 0.75 }, children: "NOTE: This error only displays during local development." })
]
}
);
}
function useErrorToast() {
const { addToast } = useToast();
return useCallback(
(error) => {
const errorId = error.map((err) => {
var _a, _b;
const message = "extensions" in err ? ((_b = (_a = err.extensions) == null ? void 0 : _a.originalError) == null ? void 0 : _b.message) || err.message : err.message;
const stack = err.stack || "";
return btoa(message + stack).slice(0, 32);
}).join("|");
addToast({
type: "error",
id: errorId,
// Toast libraries typically dedupe by id
message: /* @__PURE__ */ jsx(ErrorToast, { errors: error })
});
},
[addToast]
);
}
function useAsyncCallback(callback, deps) {
const addErrorToast = useErrorToast();
return useCallback((...args) => __async(this, null, function* () {
try {
return yield callback(...args);
} catch (error) {
console.error("Error in async callback:", error);
addErrorToast([error]);
throw error;
}
}), deps);
}
export {
ErrorToast,
useErrorToast,
useAsyncCallback
};
//# sourceMappingURL=chunk-N4WEHORG.mjs.map