@cmnd-ai/chatbot-react 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Readme.md CHANGED
@@ -20,6 +20,7 @@ yarn add @cmnd-ai/chatbot-react
20
20
 
21
21
  ```javascript
22
22
  import { ChatProvider } from "@cmnd-ai/chatbot-react";
23
+ import "@cmnd-ai/chatbot-react/dist/styles/index.css"
23
24
 
24
25
  const App = () => {
25
26
  return (
@@ -1,9 +1,11 @@
1
1
  import React from "react";
2
2
  import { CmndChatBotProps } from "../CmndChatBot/index.js";
3
- import { CmndChatContext } from "../type.js";
3
+ import { CmndChatContext, CMNDChatMemory } from "../type.js";
4
4
  export declare const ChatProviderContext: React.Context<CmndChatContext | undefined>;
5
5
  export interface ChatProviderProps extends CmndChatBotProps {
6
6
  children?: React.ReactNode | ((props: CmndChatBotProps) => React.ReactNode);
7
7
  }
8
+ export declare const setCurrentConversationMemory: (memory: CMNDChatMemory) => Promise<import("axios").AxiosResponse<any, any> | undefined>;
9
+ export declare const deleteCurrentConversationMemory: (memoryKeyToDelete: string) => Promise<import("axios").AxiosResponse<any, any> | undefined>;
8
10
  declare function ChatProvider(props: ChatProviderProps): JSX.Element | null;
9
11
  export default ChatProvider;
@@ -4,9 +4,48 @@ import { MessageRole, } from "../type.js";
4
4
  import postUserConversation from "../services/postUserConversation.js";
5
5
  import Conversation from "../components/Conversation.js";
6
6
  import getChatBotById from "../services/getchatBotById.js";
7
+ import patchChatbotConversationMemory from "../services/patchChatbotConversationMemory/index.js";
8
+ import deleteChatbotConversationMemory from "../services/deleteChatbotConversationMemory/index.js";
9
+ let globalChatbotConversationId;
10
+ let globalChatbotProps;
7
11
  export const ChatProviderContext = React.createContext(undefined);
12
+ export const setCurrentConversationMemory = async (memory) => {
13
+ try {
14
+ if (!globalChatbotConversationId || !globalChatbotProps) {
15
+ return Promise.reject("setCurrentConversationMemory method is only available when user is interacting with an active chat thread.");
16
+ }
17
+ return await patchChatbotConversationMemory({
18
+ chatbotConversationId: globalChatbotConversationId,
19
+ memory,
20
+ baseUrl: globalChatbotProps.baseUrl,
21
+ chatbotId: globalChatbotProps.chatbotId,
22
+ organizationId: globalChatbotProps.organizationId,
23
+ });
24
+ }
25
+ catch (error) {
26
+ Promise.reject(error);
27
+ }
28
+ };
29
+ export const deleteCurrentConversationMemory = async (memoryKeyToDelete) => {
30
+ try {
31
+ if (!globalChatbotConversationId || !globalChatbotProps) {
32
+ return Promise.reject("deleteCurrentConversationMemory method is only available when user is interacting with an active chat thread.");
33
+ }
34
+ return await deleteChatbotConversationMemory({
35
+ chatbotConversationId: globalChatbotConversationId,
36
+ memoryKeyToDelete,
37
+ baseUrl: globalChatbotProps.baseUrl,
38
+ chatbotId: globalChatbotProps.chatbotId,
39
+ organizationId: globalChatbotProps.organizationId,
40
+ });
41
+ }
42
+ catch (error) {
43
+ Promise.reject(error);
44
+ }
45
+ };
8
46
  function ChatProvider(props) {
9
- const { chatbotId, organizationId, baseUrl, Components, UITools } = props;
47
+ globalChatbotProps = props;
48
+ const { chatbotId, organizationId, baseUrl, Components, UITools, initialMemory, } = props;
10
49
  const [loading, setLoading] = useState(true);
11
50
  const [messages, setMessages] = useState([]);
12
51
  const [error, setError] = useState(null);
@@ -17,6 +56,9 @@ function ChatProvider(props) {
17
56
  const messagesRef = useRef(null);
18
57
  const [chatbotConversationId, setChatbotConversationId] = useState(undefined);
19
58
  const [enabledTools, setEnabledTools] = useState([]);
59
+ useEffect(() => {
60
+ globalChatbotConversationId = chatbotConversationId;
61
+ }, [chatbotConversationId]);
20
62
  useEffect(() => {
21
63
  setLoading(true);
22
64
  getChatBotById(baseUrl, organizationId, chatbotId)
@@ -42,8 +84,17 @@ function ChatProvider(props) {
42
84
  const postSessionMessage = async (newMessages, onData) => {
43
85
  if (!organizationId || !chatbotId)
44
86
  return;
87
+ const payload = {
88
+ messages: newMessages,
89
+ };
90
+ if (chatbotConversationId) {
91
+ payload["chatbotConversationId"] = chatbotConversationId;
92
+ }
93
+ else {
94
+ payload["initialMemory"] = initialMemory;
95
+ }
45
96
  return postUserConversation({
46
- payload: newMessages,
97
+ payload,
47
98
  apikey: props.apiKey,
48
99
  chatbotId,
49
100
  baseUrl,
@@ -112,7 +163,8 @@ function ChatProvider(props) {
112
163
  setCanSendMessage,
113
164
  scrollToBottom,
114
165
  enabledTools,
166
+ chatbotConversationId,
115
167
  },
116
- }, children: error ? (_jsx("div", { children: "An error occured" })) : (_jsxs(_Fragment, { children: [props.children, _jsx(Conversation, { messages: messages, setMessages: setMessages, postSessionMessage: postSessionMessage, isChatLoading: isChatLoading, messagesRef: messagesRef, input: input, setInput: setInput, handleSendClick: handleSendClick, setChatbotConversationId: setChatbotConversationId, setIsChatLoading: setIsChatLoading, setCanSendMessage: setCanSendMessage, canSendMessage: canSendMessage, scrollToBottom: scrollToBottom, error: error, enabledTools: enabledTools, Components: Components, UITools: UITools, customStyles: props.customStyles })] })) }));
168
+ }, children: error ? (_jsx("div", { children: "An error occured" })) : (_jsxs(_Fragment, { children: [props.children, _jsx(Conversation, { messages: messages, setMessages: setMessages, postSessionMessage: postSessionMessage, isChatLoading: isChatLoading, messagesRef: messagesRef, input: input, setInput: setInput, handleSendClick: handleSendClick, setChatbotConversationId: setChatbotConversationId, setIsChatLoading: setIsChatLoading, setCanSendMessage: setCanSendMessage, canSendMessage: canSendMessage, scrollToBottom: scrollToBottom, error: error, enabledTools: enabledTools, Components: Components, UITools: UITools, customStyles: props.customStyles, chatbotConversationId: chatbotConversationId, setCurrentConversationMemory: setCurrentConversationMemory })] })) }));
117
169
  }
118
170
  export default ChatProvider;
@@ -1,5 +1,5 @@
1
1
  import React from "react";
2
- import { CmndChatContext, CustomStyles, UIFunctionArguments } from "../type.js";
2
+ import { CMNDChatMemory, CustomStyles, UIFunctionArguments } from "../type.js";
3
3
  import { ConversationProps } from "../components/Conversation.js";
4
4
  export interface CmndChatBotProps extends Pick<ConversationProps, 'Components'> {
5
5
  chatbotId: number;
@@ -8,6 +8,7 @@ export interface CmndChatBotProps extends Pick<ConversationProps, 'Components'>
8
8
  baseUrl: string;
9
9
  UITools?: Record<string, React.FC<UIFunctionArguments<any>>>;
10
10
  customStyles?: CustomStyles;
11
+ initialMemory?: CMNDChatMemory;
11
12
  }
12
- declare function CmndChatBot(): CmndChatContext;
13
+ declare function CmndChatBot(): import("../type.js").CmndChatContext;
13
14
  export default CmndChatBot;
@@ -116,7 +116,7 @@ const Chatbubble = ({ message, role, toolCallDetails, tools, postSessionMessage,
116
116
  return (_jsxs("div", { style: defaultStyle, className: `cmnd-chatbot-chat-bubble ${role}`, children: [_jsx("span", { style: overrideStyle({
117
117
  fontSize: "30px",
118
118
  color: "black",
119
- }, customStyles?.chatAvatarStyle), children: getChatAvatar(role) }), _jsxs("p", { style: chatBubbleCustomStyle, children: [role === MessageRole.USER || isLoadingBubble ? (_jsx("span", { children: message })) : (_jsx(ReactMarkdown, { className: "markdown", remarkPlugins: [remarkGfm], children: message?.toString() ?? "", remarkRehypeOptions: { passThrough: ["link"] }, components: {
119
+ }, customStyles?.chatAvatarStyle), children: getChatAvatar(role) }), _jsxs("span", { style: chatBubbleCustomStyle, children: [role === MessageRole.USER || isLoadingBubble ? (_jsx("span", { children: message })) : (_jsx(ReactMarkdown, { className: "markdown", remarkPlugins: [remarkGfm], children: message?.toString() ?? "", remarkRehypeOptions: { passThrough: ["link"] }, components: {
120
120
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
121
121
  // @ts-ignore
122
122
  code({ inline, className, children, ...props }) {
@@ -1,5 +1,5 @@
1
1
  import React, { Dispatch, SetStateAction } from "react";
2
- import { CustomStyles, InputFieldProps, SendButtonProps, UIFunctionArguments } from "../type.js";
2
+ import { CMNDChatMemory, CustomStyles, InputFieldProps, SendButtonProps, UIFunctionArguments } from "../type.js";
3
3
  export interface ConversationProps {
4
4
  messages: any[];
5
5
  setMessages: Dispatch<SetStateAction<any[]>>;
@@ -10,6 +10,7 @@ export interface ConversationProps {
10
10
  error: string | null;
11
11
  messagesRef: React.RefObject<HTMLDivElement>;
12
12
  setChatbotConversationId: Dispatch<React.SetStateAction<number | undefined>>;
13
+ chatbotConversationId: number | undefined;
13
14
  setIsChatLoading: Dispatch<SetStateAction<boolean>>;
14
15
  setCanSendMessage: Dispatch<SetStateAction<boolean>>;
15
16
  canSendMessage: boolean;
@@ -23,6 +24,7 @@ export interface ConversationProps {
23
24
  };
24
25
  UITools?: Record<string, React.FC<UIFunctionArguments<any>>>;
25
26
  customStyles?: CustomStyles;
27
+ setCurrentConversationMemory: (memory: CMNDChatMemory) => Promise<any>;
26
28
  }
27
29
  declare const Conversation: ({ messages, handleSendClick, isChatLoading, error, messagesRef, enabledTools, postSessionMessage, setMessages, setChatbotConversationId, setIsChatLoading, setCanSendMessage, scrollToBottom, canSendMessage, setInput, input, Components, UITools, customStyles, }: ConversationProps) => JSX.Element;
28
30
  export default Conversation;
@@ -0,0 +1,5 @@
1
+ declare const chatbot: {
2
+ patchChatbotConversationMemory: (organizationId: number, chatbotId: number, chatbotConversationId: number) => string;
3
+ deleteChatbotConversationMemory: (organizationId: number, chatbotId: number, chatbotConversationId: number, memoryKeyToDelete: string) => string;
4
+ };
5
+ export { chatbot };
@@ -0,0 +1,5 @@
1
+ const chatbot = {
2
+ patchChatbotConversationMemory: (organizationId, chatbotId, chatbotConversationId) => `/organizations/${organizationId}/chatbots/${chatbotId}/conversations/${chatbotConversationId}/memory`,
3
+ deleteChatbotConversationMemory: (organizationId, chatbotId, chatbotConversationId, memoryKeyToDelete) => `/organizations/${organizationId}/chatbots/${chatbotId}/conversations/${chatbotConversationId}/memory/${memoryKeyToDelete}`,
4
+ };
5
+ export { chatbot };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import "./styles/index.css";
2
1
  export { default as ChatProvider } from "./ChatProvider/index.js";
3
- export { default as useChatContext } from "./ChatProvider/useChatContext.js";
4
- export { CmndChatContext, InputFieldProps, SendButtonProps, CustomStyles } from "./type.js";
5
- export { default as CmndChatBot } from "./CmndChatBot/index.js";
2
+ export { CmndChatContext, InputFieldProps, SendButtonProps, CustomStyles, CMNDChatMemory, } from "./type.js";
3
+ export { setCurrentConversationMemory, deleteCurrentConversationMemory, } from "./ChatProvider/index.js";
package/dist/index.js CHANGED
@@ -1,4 +1,2 @@
1
- import "./styles/index.css";
2
1
  export { default as ChatProvider } from "./ChatProvider/index.js";
3
- export { default as useChatContext } from "./ChatProvider/useChatContext.js";
4
- export { default as CmndChatBot } from "./CmndChatBot/index.js";
2
+ export { setCurrentConversationMemory, deleteCurrentConversationMemory, } from "./ChatProvider/index.js";
@@ -0,0 +1,9 @@
1
+ interface IDeleteChatbotConversationMemory {
2
+ organizationId: number;
3
+ chatbotId: number;
4
+ chatbotConversationId: number;
5
+ memoryKeyToDelete: string;
6
+ baseUrl: string;
7
+ }
8
+ declare const deleteChatbotConversationMemory: ({ organizationId, chatbotId, chatbotConversationId, memoryKeyToDelete, baseUrl, }: IDeleteChatbotConversationMemory) => Promise<import("axios").AxiosResponse<any, any>>;
9
+ export default deleteChatbotConversationMemory;
@@ -0,0 +1,7 @@
1
+ import { chatbot } from "../../constants/endpoints.js";
2
+ import axios from "axios";
3
+ const deleteChatbotConversationMemory = ({ organizationId, chatbotId, chatbotConversationId, memoryKeyToDelete, baseUrl, }) => {
4
+ const endpoint = chatbot.deleteChatbotConversationMemory(organizationId, chatbotId, chatbotConversationId, memoryKeyToDelete);
5
+ return axios.delete(`${baseUrl}${endpoint}`);
6
+ };
7
+ export default deleteChatbotConversationMemory;
@@ -0,0 +1,11 @@
1
+ interface IPatchChatbotConversationMemory {
2
+ organizationId: number;
3
+ chatbotId: number;
4
+ chatbotConversationId: number;
5
+ memory: {
6
+ [key: string]: any;
7
+ };
8
+ baseUrl: string;
9
+ }
10
+ declare const patchChatbotConversationMemory: ({ organizationId, chatbotId, chatbotConversationId, memory, baseUrl, }: IPatchChatbotConversationMemory) => Promise<import("axios").AxiosResponse<any, any>>;
11
+ export default patchChatbotConversationMemory;
@@ -0,0 +1,9 @@
1
+ import axios from "axios";
2
+ import { chatbot } from "../../constants/endpoints.js";
3
+ const patchChatbotConversationMemory = ({ organizationId, chatbotId, chatbotConversationId, memory, baseUrl, }) => {
4
+ const endpoint = chatbot.patchChatbotConversationMemory(organizationId, chatbotId, chatbotConversationId);
5
+ return axios.patch(`${baseUrl}${endpoint}`, {
6
+ memory,
7
+ });
8
+ };
9
+ export default patchChatbotConversationMemory;
@@ -2,9 +2,7 @@ import axios from "axios";
2
2
  import processStream from "../ChatProvider/processStream/index.js";
3
3
  const postUserConversation = async ({ payload, apikey, chatbotId, baseUrl, onData, }) => {
4
4
  const endpoint = `${baseUrl}/chatbots/${chatbotId}/conversations`;
5
- const response = await axios.post(endpoint, {
6
- messages: payload,
7
- }, {
5
+ const response = await axios.post(endpoint, payload, {
8
6
  headers: {
9
7
  Accept: "text/event-stream",
10
8
  "accept-language": "en-US,en;q=0.9",
package/dist/type.d.ts CHANGED
@@ -109,4 +109,7 @@ export interface CustomStyles {
109
109
  botChatbubbleStyle?: CSSProperties;
110
110
  userChatbubbleStyle?: CSSProperties;
111
111
  }
112
+ export interface CMNDChatMemory {
113
+ [key: string]: any;
114
+ }
112
115
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmnd-ai/chatbot-react",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "main": "dist/index.js",
5
5
  "description": "",
6
6
  "type": "module",