@cmnd-ai/chatbot-react 1.1.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.
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ import { CmndChatBotProps } from "../CmndChatBot";
3
+ import { CmndChatContext } from "../type";
4
+ export declare const ChatProviderContext: React.Context<CmndChatContext | undefined>;
5
+ export interface ChatProviderProps extends Omit<CmndChatBotProps, "components"> {
6
+ children: React.ReactNode | ((props: Omit<CmndChatBotProps, "components">) => React.ReactNode);
7
+ }
8
+ declare function ChatProvider(props: ChatProviderProps): import("react/jsx-runtime").JSX.Element;
9
+ export default ChatProvider;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ChatProviderContext = void 0;
27
+ const jsx_runtime_1 = require("react/jsx-runtime");
28
+ const react_1 = __importStar(require("react"));
29
+ const constants_1 = require("../constants");
30
+ exports.ChatProviderContext = react_1.default.createContext(undefined);
31
+ function ChatProvider(props) {
32
+ const { chatbotId, organizationId } = props;
33
+ const [chats, setchats] = (0, react_1.useState)({
34
+ data: undefined,
35
+ error: null,
36
+ isLoading: false,
37
+ });
38
+ const [message, setmessage] = (0, react_1.useState)({
39
+ error: null,
40
+ isSending: false,
41
+ textValue: "",
42
+ });
43
+ const sendMessage = async () => {
44
+ const temp = { ...message };
45
+ try {
46
+ temp.error = null;
47
+ const requestBody = {
48
+ messages: [
49
+ {
50
+ hiddenFromUser: false,
51
+ message: message.textValue,
52
+ role: "user",
53
+ unuseful: false,
54
+ },
55
+ ],
56
+ };
57
+ const results = await postUserConversation({
58
+ orgId: organizationId,
59
+ payload: requestBody,
60
+ apikey: props.apiKey,
61
+ chatbotId,
62
+ });
63
+ if (!results.ok) {
64
+ throw new Error(`Failed to post conversation: ${results.statusText}`);
65
+ }
66
+ temp.textValue = "";
67
+ const data = await results.json();
68
+ setchats((prev) => ({
69
+ ...prev,
70
+ data: {
71
+ ...prev.data,
72
+ messages: [...(prev.data?.messages || [])].concat(...data.messages),
73
+ },
74
+ }));
75
+ }
76
+ catch (error) {
77
+ console.error("error while sending message: ", error);
78
+ temp.error = error;
79
+ }
80
+ temp.isSending = false;
81
+ setmessage(temp);
82
+ };
83
+ (0, react_1.useEffect)(() => {
84
+ if (message.isSending) {
85
+ sendMessage();
86
+ }
87
+ // eslint-disable-next-line react-hooks/exhaustive-deps
88
+ }, [message.isSending]);
89
+ const currentPendingMessage = message.isSending
90
+ ? message.textValue
91
+ : undefined;
92
+ return ((0, jsx_runtime_1.jsx)(exports.ChatProviderContext.Provider, { value: {
93
+ props: {
94
+ chats,
95
+ userInputData: {
96
+ ...message,
97
+ setTextValue(value) {
98
+ setmessage((prev) => ({ ...prev, textValue: value }));
99
+ },
100
+ submitMessage() {
101
+ setmessage((prev) => ({ ...prev, isSending: true }));
102
+ },
103
+ },
104
+ },
105
+ }, children: typeof props.children === "function" ? ((0, jsx_runtime_1.jsx)(props.children, { ...props }) // If it's a function, call it with props
106
+ ) : (props.children) // Otherwise, render it as React node
107
+ }));
108
+ }
109
+ exports.default = ChatProvider;
110
+ const postUserConversation = ({ orgId, payload, apikey, chatbotId, }) => {
111
+ return fetch(`${constants_1.baseUrl}/organizations/${orgId}/chatbots/${chatbotId}/conversations`, {
112
+ headers: {
113
+ accept: "application/json, text/plain, */*",
114
+ "accept-language": "en-US,en;q=0.9",
115
+ "content-type": "application/json;charset=UTF-8",
116
+ "x-api-key": apikey,
117
+ },
118
+ body: JSON.stringify(payload),
119
+ method: "POST",
120
+ });
121
+ };
@@ -0,0 +1,2 @@
1
+ declare const useChatContext: () => import("../type").CmndChatContext;
2
+ export default useChatContext;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const _1 = require(".");
5
+ const useChatContext = () => {
6
+ const context = (0, react_1.useContext)(_1.ChatProviderContext);
7
+ if (!context)
8
+ throw new Error("Cmnd chat context must be wrapped in a provider");
9
+ return context;
10
+ };
11
+ exports.default = useChatContext;
@@ -0,0 +1,14 @@
1
+ import React from "react";
2
+ import { CmndChatContext } from "../type";
3
+ export interface CmndChatBotProps {
4
+ chatbotId: number;
5
+ organizationId: number;
6
+ apiKey: string;
7
+ components: {
8
+ messages: (params: CmndChatContext) => React.ReactNode;
9
+ userInputBox: (params: CmndChatContext) => React.ReactNode;
10
+ error?: (params: CmndChatContext) => React.ReactNode;
11
+ };
12
+ }
13
+ declare function CmndChatBot(props: CmndChatBotProps): import("react/jsx-runtime").JSX.Element;
14
+ export default CmndChatBot;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ const useChatContext_1 = __importDefault(require("../ChatProvider/useChatContext"));
8
+ function CmndChatBot(props) {
9
+ const chatContext = (0, useChatContext_1.default)();
10
+ const { props: { chats }, } = chatContext;
11
+ if (chats.error && props.components.error) {
12
+ return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(props.components.error, { props: chatContext.props }) });
13
+ }
14
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(props.components.messages, { props: chatContext.props }), (0, jsx_runtime_1.jsx)(props.components.userInputBox, { props: chatContext.props })] }));
15
+ }
16
+ exports.default = CmndChatBot;
@@ -0,0 +1 @@
1
+ export declare const baseUrl: "https://api.cmnd.ai";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.baseUrl = void 0;
4
+ exports.baseUrl = "https://api.cmnd.ai";
@@ -0,0 +1,4 @@
1
+ export { default as ChatProvider } from "./ChatProvider";
2
+ export { default as useChatContext } from "./ChatProvider/useChatContext";
3
+ export { default as CmndChatBot } from "./CmndChatBot";
4
+ export * from "./constants";
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.CmndChatBot = exports.useChatContext = exports.ChatProvider = void 0;
21
+ var ChatProvider_1 = require("./ChatProvider");
22
+ Object.defineProperty(exports, "ChatProvider", { enumerable: true, get: function () { return __importDefault(ChatProvider_1).default; } });
23
+ var useChatContext_1 = require("./ChatProvider/useChatContext");
24
+ Object.defineProperty(exports, "useChatContext", { enumerable: true, get: function () { return __importDefault(useChatContext_1).default; } });
25
+ var CmndChatBot_1 = require("./CmndChatBot");
26
+ Object.defineProperty(exports, "CmndChatBot", { enumerable: true, get: function () { return __importDefault(CmndChatBot_1).default; } });
27
+ __exportStar(require("./constants"), exports);
package/dist/type.d.ts ADDED
@@ -0,0 +1,45 @@
1
+ export interface CmndChatContext {
2
+ props: {
3
+ chats: {
4
+ isLoading: boolean;
5
+ data: Conversation | undefined;
6
+ error: null | unknown;
7
+ };
8
+ userInputData: {
9
+ isSending: boolean;
10
+ textValue: string;
11
+ /** whether there is an error while sending the message */
12
+ error: null | unknown;
13
+ /** to change the value of the input text */
14
+ setTextValue: (value: string) => any;
15
+ /** function to call for submitting a message */
16
+ submitMessage: () => void;
17
+ /**Message currently being sent */
18
+ currentPendingMessage?: string;
19
+ };
20
+ };
21
+ }
22
+ export type ErrorOrData<T> = {
23
+ error: unknown;
24
+ } | T;
25
+ export interface Message {
26
+ id?: string;
27
+ role: "user" | "assistant";
28
+ message: string;
29
+ unuseful: boolean;
30
+ hiddenFromUser: boolean;
31
+ }
32
+ export interface Conversation {
33
+ conversationId: number;
34
+ conversationTitle: string;
35
+ messages: Message[];
36
+ model: string;
37
+ maxTokens: number;
38
+ costLimit: number;
39
+ systemPrompt: string;
40
+ enabledTools: any[];
41
+ /** Date string format */
42
+ createdAt: string;
43
+ /** Date string format */
44
+ updatedAt: string;
45
+ }
package/dist/type.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@cmnd-ai/chatbot-react",
3
+ "version": "1.1.0",
4
+ "main": "dist/index.js",
5
+ "description": "",
6
+ "files": [
7
+ "dist/",
8
+ "package.json",
9
+ "ReadMe.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "npm run build",
14
+ "semantic-release": "semantic-release"
15
+ },
16
+ "keywords": [],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "devDependencies": {
20
+ "@types/react": "^18.2.55",
21
+ "semantic-release": "^22.0.12",
22
+ "typescript": "^5.3.3"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ }
27
+ }