UNPKG

@liveblocks/react-ui

Version:

A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.

72 lines (69 loc) 1.83 kB
import { jsx } from 'react/jsx-runtime'; import { kInternal } from '@liveblocks/core'; import { useClient } from '@liveblocks/react'; import { useSignal } from '@liveblocks/react/_private'; import { useCallback, useMemo } from 'react'; import { AiToolInvocationContext } from './contexts.js'; function StableRenderFn(props) { return props.renderFn(props.props); } function AiMessageToolInvocation({ message, part, copilotId }) { const client = useClient(); const ai = client[kInternal].ai; const tool = useSignal(ai.signals.getTool\u03A3(part.name, message.chatId)); const respond = useCallback( (result) => { if (message.status !== "awaiting-tool") { } else if (part.stage === "receiving") { } else if (part.stage === "executed") { console.log( `Ignoring respond(): tool '${part.name}' (${part.invocationId}) has already executed` ); } else { ai.setToolResult( message.chatId, message.id, part.invocationId, result ?? { data: {} }, { copilotId } ); } }, [ ai, message.chatId, message.id, message.status, part.invocationId, part.name, part.stage, copilotId ] ); const props = useMemo(() => { const { type: _, ...rest } = part; return { ...rest, respond, types: void 0, [kInternal]: { execute: tool?.execute } }; }, [part, respond, tool?.execute]); if (tool?.render === void 0) return null; return /* @__PURE__ */ jsx(AiToolInvocationContext.Provider, { value: props, children: /* @__PURE__ */ jsx(StableRenderFn, { renderFn: tool.render, props }) }); } export { AiMessageToolInvocation }; //# sourceMappingURL=tool-invocation.js.map