78 lines
3.6 KiB
JavaScript
78 lines
3.6 KiB
JavaScript
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
const require_CopilotChatConfigurationProvider = require('../providers/CopilotChatConfigurationProvider.cjs');
|
|
const require_CopilotKitProvider = require('../providers/CopilotKitProvider.cjs');
|
|
let react = require("react");
|
|
react = require_runtime.__toESM(react);
|
|
let _copilotkitnext_shared = require("@copilotkitnext/shared");
|
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
let _copilotkitnext_core = require("@copilotkitnext/core");
|
|
|
|
//#region src/hooks/use-render-tool-call.tsx
|
|
/**
|
|
* Memoized component that renders a single tool call.
|
|
* This prevents unnecessary re-renders when parent components update
|
|
* but the tool call data hasn't changed.
|
|
*/
|
|
const ToolCallRenderer = react.default.memo(function ToolCallRenderer({ toolCall, toolMessage, RenderComponent, isExecuting }) {
|
|
const args = (0, react.useMemo)(() => (0, _copilotkitnext_shared.partialJSONParse)(toolCall.function.arguments), [toolCall.function.arguments]);
|
|
const toolName = toolCall.function.name;
|
|
if (toolMessage) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
name: toolName,
|
|
args,
|
|
status: _copilotkitnext_core.ToolCallStatus.Complete,
|
|
result: toolMessage.content
|
|
});
|
|
else if (isExecuting) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
name: toolName,
|
|
args,
|
|
status: _copilotkitnext_core.ToolCallStatus.Executing,
|
|
result: void 0
|
|
});
|
|
else return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RenderComponent, {
|
|
name: toolName,
|
|
args,
|
|
status: _copilotkitnext_core.ToolCallStatus.InProgress,
|
|
result: void 0
|
|
});
|
|
}, (prevProps, nextProps) => {
|
|
if (prevProps.toolCall.id !== nextProps.toolCall.id) return false;
|
|
if (prevProps.toolCall.function.name !== nextProps.toolCall.function.name) return false;
|
|
if (prevProps.toolCall.function.arguments !== nextProps.toolCall.function.arguments) return false;
|
|
if (prevProps.toolMessage?.content !== nextProps.toolMessage?.content) return false;
|
|
if (prevProps.isExecuting !== nextProps.isExecuting) return false;
|
|
if (prevProps.RenderComponent !== nextProps.RenderComponent) return false;
|
|
return true;
|
|
});
|
|
/**
|
|
* Hook that returns a function to render tool calls based on the render functions
|
|
* defined in CopilotKitProvider.
|
|
*
|
|
* @returns A function that takes a tool call and optional tool message and returns the rendered component
|
|
*/
|
|
function useRenderToolCall() {
|
|
const { copilotkit, executingToolCallIds } = require_CopilotKitProvider.useCopilotKit();
|
|
const agentId = require_CopilotChatConfigurationProvider.useCopilotChatConfiguration()?.agentId ?? _copilotkitnext_shared.DEFAULT_AGENT_ID;
|
|
const renderToolCalls = (0, react.useSyncExternalStore)((callback) => {
|
|
return copilotkit.subscribe({ onRenderToolCallsChanged: callback }).unsubscribe;
|
|
}, () => copilotkit.renderToolCalls, () => copilotkit.renderToolCalls);
|
|
return (0, react.useCallback)(({ toolCall, toolMessage }) => {
|
|
const exactMatches = renderToolCalls.filter((rc) => rc.name === toolCall.function.name);
|
|
const renderConfig = exactMatches.find((rc) => rc.agentId === agentId) || exactMatches.find((rc) => !rc.agentId) || exactMatches[0] || renderToolCalls.find((rc) => rc.name === "*");
|
|
if (!renderConfig) return null;
|
|
const RenderComponent = renderConfig.render;
|
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ToolCallRenderer, {
|
|
toolCall,
|
|
toolMessage,
|
|
RenderComponent,
|
|
isExecuting: executingToolCallIds.has(toolCall.id)
|
|
}, toolCall.id);
|
|
}, [
|
|
renderToolCalls,
|
|
executingToolCallIds,
|
|
agentId
|
|
]);
|
|
}
|
|
|
|
//#endregion
|
|
exports.useRenderToolCall = useRenderToolCall;
|
|
//# sourceMappingURL=use-render-tool-call.cjs.map
|