UNPKG

@copilotkit/react-core

Version:

<div align="center"> <a href="https://copilotkit.ai" target="_blank"> <img src="https://github.com/copilotkit/copilotkit/raw/main/assets/banner.png" alt="CopilotKit Logo"> </a>

225 lines (216 loc) • 6.87 kB
import { defaultCopilotContextCategories } from "./chunk-OKBPP25E.mjs"; import { useCopilotContext } from "./chunk-PTSWFERP.mjs"; import { useCopilotMessagesContext } from "./chunk-DCTJZ742.mjs"; import { useChat } from "./chunk-FUO5LKSJ.mjs"; import { useAsyncCallback } from "./chunk-22ENANUU.mjs"; import { __async, __objRest, __spreadProps, __spreadValues } from "./chunk-SKC7AJIV.mjs"; // src/hooks/use-copilot-chat.ts import { useRef, useEffect, useCallback } from "react"; import { Role, TextMessage } from "@copilotkit/runtime-client-gql"; function useCopilotChat(_a = {}) { var _b = _a, { makeSystemMessage } = _b, options = __objRest(_b, [ "makeSystemMessage" ]); const { getContextString, getFunctionCallHandler, copilotApiConfig, isLoading, setIsLoading, chatInstructions, actions, coagentStatesRef, setCoagentStatesWithRef, coAgentStateRenders, agentSession, setAgentSession, forwardedParameters, agentLock, threadId, setThreadId, runId, setRunId, chatAbortControllerRef, extensions, setExtensions, langGraphInterruptAction, setLangGraphInterruptAction } = useCopilotContext(); const { messages, setMessages } = useCopilotMessagesContext(); const latestGetContextString = useUpdatedRef(getContextString); const deleteMessage = useCallback( (messageId) => { setMessages((prev) => prev.filter((message) => message.id !== messageId)); }, [setMessages] ); const makeSystemMessageCallback = useCallback(() => { const systemMessageMaker = makeSystemMessage || defaultSystemMessage; const contextString = latestGetContextString.current([], defaultCopilotContextCategories); return new TextMessage({ content: systemMessageMaker(contextString, chatInstructions), role: Role.System }); }, [getContextString, makeSystemMessage, chatInstructions]); const onCoAgentStateRender = useAsyncCallback( (args) => __async(this, null, function* () { var _a2; const { name, nodeName, state } = args; let action = Object.values(coAgentStateRenders).find( (action2) => action2.name === name && action2.nodeName === nodeName ); if (!action) { action = Object.values(coAgentStateRenders).find( (action2) => action2.name === name && !action2.nodeName ); } if (action) { yield (_a2 = action.handler) == null ? void 0 : _a2.call(action, { state, nodeName }); } }), [coAgentStateRenders] ); const { append, reload, stop, runChatCompletion } = useChat(__spreadProps(__spreadValues({}, options), { actions: Object.values(actions), copilotConfig: copilotApiConfig, initialMessages: options.initialMessages || [], onFunctionCall: getFunctionCallHandler(), onCoAgentStateRender, messages, setMessages, makeSystemMessageCallback, isLoading, setIsLoading, coagentStatesRef, setCoagentStatesWithRef, agentSession, setAgentSession, forwardedParameters, threadId, setThreadId, runId, setRunId, chatAbortControllerRef, agentLock, extensions, setExtensions, langGraphInterruptAction, setLangGraphInterruptAction })); const latestAppend = useUpdatedRef(append); const latestAppendFunc = useAsyncCallback( (message, options2) => __async(this, null, function* () { return yield latestAppend.current(message, options2); }), [latestAppend] ); const latestReload = useUpdatedRef(reload); const latestReloadFunc = useAsyncCallback(() => __async(this, null, function* () { return yield latestReload.current(); }), [latestReload]); const latestStop = useUpdatedRef(stop); const latestStopFunc = useCallback(() => { return latestStop.current(); }, [latestStop]); const latestDelete = useUpdatedRef(deleteMessage); const latestDeleteFunc = useCallback( (messageId) => { return latestDelete.current(messageId); }, [latestDelete] ); const latestSetMessages = useUpdatedRef(setMessages); const latestSetMessagesFunc = useCallback( (messages2) => { return latestSetMessages.current(messages2); }, [latestSetMessages] ); const latestRunChatCompletion = useUpdatedRef(runChatCompletion); const latestRunChatCompletionFunc = useAsyncCallback(() => __async(this, null, function* () { return yield latestRunChatCompletion.current(); }), [latestRunChatCompletion]); const reset = useCallback(() => { latestStopFunc(); setMessages([]); setRunId(null); setCoagentStatesWithRef({}); let initialAgentSession = null; if (agentLock) { initialAgentSession = { agentName: agentLock }; } setAgentSession(initialAgentSession); }, [ latestStopFunc, setMessages, setThreadId, setCoagentStatesWithRef, setAgentSession, agentLock ]); const latestReset = useUpdatedRef(reset); const latestResetFunc = useCallback(() => { return latestReset.current(); }, [latestReset]); return { visibleMessages: messages, appendMessage: latestAppendFunc, setMessages: latestSetMessagesFunc, reloadMessages: latestReloadFunc, stopGeneration: latestStopFunc, reset: latestResetFunc, deleteMessage: latestDeleteFunc, runChatCompletion: latestRunChatCompletionFunc, isLoading }; } function useUpdatedRef(value) { const ref = useRef(value); useEffect(() => { ref.current = value; }, [value]); return ref; } function defaultSystemMessage(contextString, additionalInstructions) { return ` Please act as an efficient, competent, conscientious, and industrious professional assistant. Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism. Always be polite and respectful, and prefer brevity over verbosity. The user has provided you with the following context: \`\`\` ${contextString} \`\`\` They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information. Please assist them as best you can. You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so. If you would like to call a function, call it without saying anything else. In case of a function error: - If this error stems from incorrect function parameters or syntax, you may retry with corrected arguments. - If the error's source is unclear or seems unrelated to your input, do not attempt further retries. ` + (additionalInstructions ? ` ${additionalInstructions}` : ""); } export { useCopilotChat, defaultSystemMessage }; //# sourceMappingURL=chunk-S3XGAWBE.mjs.map