@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;" />
88 lines (86 loc) • 3.18 kB
JavaScript
import {
useToast
} from "./chunk-EFL5OBKN.mjs";
import {
CopilotContext
} from "./chunk-YHT6CWIY.mjs";
// src/hooks/use-coagent-state-render.ts
import { useRef, useContext, useEffect } from "react";
import { randomId, CopilotKitAgentDiscoveryError } from "@copilotkit/shared";
function useCoAgentStateRender(action, dependencies) {
const {
setCoAgentStateRender,
removeCoAgentStateRender,
coAgentStateRenders,
chatComponentsCache,
availableAgents
} = useContext(CopilotContext);
const idRef = useRef(randomId());
const { setBannerError, addToast } = useToast();
useEffect(() => {
if ((availableAgents == null ? void 0 : availableAgents.length) && !availableAgents.some((a) => a.name === action.name)) {
const message = `(useCoAgentStateRender): Agent "${action.name}" not found. Make sure the agent exists and is properly configured.`;
const agentError = new CopilotKitAgentDiscoveryError({
agentName: action.name,
availableAgents: availableAgents.map((a) => ({ name: a.name, id: a.id }))
});
setBannerError(agentError);
}
}, [availableAgents]);
const key = `${action.name}-${action.nodeName || "global"}`;
if (dependencies === void 0) {
if (coAgentStateRenders[idRef.current]) {
coAgentStateRenders[idRef.current].handler = action.handler;
if (typeof action.render === "function") {
if (chatComponentsCache.current !== null) {
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
}
}
}
}
useEffect(() => {
const currentId = idRef.current;
const hasDuplicate = Object.entries(coAgentStateRenders).some(([id, otherAction]) => {
if (id === currentId)
return false;
if (otherAction.name !== action.name)
return false;
const hasNodeName = !!action.nodeName;
const hasOtherNodeName = !!otherAction.nodeName;
if (!hasNodeName && !hasOtherNodeName)
return true;
if (hasNodeName !== hasOtherNodeName)
return false;
return action.nodeName === otherAction.nodeName;
});
if (hasDuplicate) {
const message = action.nodeName ? `Found multiple state renders for agent ${action.name} and node ${action.nodeName}. State renders might get overridden` : `Found multiple state renders for agent ${action.name}. State renders might get overridden`;
addToast({
type: "warning",
message,
id: `dup-action-${action.name}`
});
}
}, [coAgentStateRenders]);
useEffect(() => {
setCoAgentStateRender(idRef.current, action);
if (chatComponentsCache.current !== null && action.render !== void 0) {
chatComponentsCache.current.coAgentStateRenders[key] = action.render;
}
return () => {
removeCoAgentStateRender(idRef.current);
};
}, [
setCoAgentStateRender,
removeCoAgentStateRender,
action.name,
// include render only if it's a string
typeof action.render === "string" ? action.render : void 0,
// dependencies set by the developer
...dependencies || []
]);
}
export {
useCoAgentStateRender
};
//# sourceMappingURL=chunk-2QZSAQTX.mjs.map