@smarter.sh/ui-chat 0.2.8 → 0.2.10
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 +24 -11
- package/dist/smarter-chat-library.es.js +2920 -2919
- package/dist/smarter-chat-library.es.js.map +1 -1
- package/dist/smarter-chat-library.umd.js +35 -35
- package/dist/smarter-chat-library.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Console/Component.jsx +1 -1
- package/src/components/SmarterChat/Component.jsx +13 -14
- package/src/components/SmarterChat/api.js +21 -60
- package/src/components/SmarterChat/utils.jsx +0 -11
- package/src/components/shared/cookie.js +51 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smarter.sh/ui-chat",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "chatbot React.js component for the https://smarter.sh no-code, plugin based AI platform",
|
|
5
5
|
"homepage": "https://smarter.sh",
|
|
6
6
|
"main": "dist/smarter-chat-library.umd.js",
|
|
@@ -35,12 +35,11 @@ import { Console } from "../Console/index.js";
|
|
|
35
35
|
import "./styles.css";
|
|
36
36
|
import { ContainerLayout, ContentLayout, WorkbenchLayout, ChatAppLayout, ConsoleLayout } from "./Layout.js";
|
|
37
37
|
import { MessageDirectionEnum, SenderRoleEnum } from "./enums.js";
|
|
38
|
-
import {
|
|
39
|
-
import {
|
|
38
|
+
import { fetchConfig, fetchPrompt } from "./api.js";
|
|
39
|
+
import { setCookie, cookieMetaFactory } from "../shared/cookie.js"
|
|
40
|
+
import { messageFactory, chatMessages2RequestMessages, chatInit } from "./utils.jsx";
|
|
40
41
|
import { ErrorBoundary } from "./ErrorBoundary.jsx";
|
|
41
42
|
|
|
42
|
-
const DEBUG_MODE = false;
|
|
43
|
-
|
|
44
43
|
// The main chat component. This is the top-level component that
|
|
45
44
|
// is exported and used in the index.js file. It is responsible for
|
|
46
45
|
// managing the chat message thread, sending messages to the backend
|
|
@@ -50,7 +49,7 @@ function SmarterChat({
|
|
|
50
49
|
apiKey, // NOT USED. TO DELETE.
|
|
51
50
|
toggleMetadata, // show/hide toggle button to show/hide the chat thread metadata
|
|
52
51
|
csrfCookieName = "csrftoken", // the Django CSRF cookie.
|
|
53
|
-
csrftoken,
|
|
52
|
+
csrftoken, // the Django CSRF token. Passed from the Django template in the Smarter web console workbench.
|
|
54
53
|
debugCookieName, // the Smarter chat debug cookie. Set here.
|
|
55
54
|
debugCookieExpiration, // the Smarter chat debug cookie. Set here.
|
|
56
55
|
sessionCookieName = "session_key", // the Smarter chat session cookie. Set here, where the user creates a new chat session.
|
|
@@ -75,7 +74,7 @@ function SmarterChat({
|
|
|
75
74
|
const [isValid, setIsValid] = useState(false);
|
|
76
75
|
const [isDeployed, setIsDeployed] = useState(false);
|
|
77
76
|
|
|
78
|
-
const [debugMode, setDebugMode] = useState(
|
|
77
|
+
const [debugMode, setDebugMode] = useState(false);
|
|
79
78
|
const [messages, setMessages] = useState([]);
|
|
80
79
|
|
|
81
80
|
// future use
|
|
@@ -106,10 +105,11 @@ function SmarterChat({
|
|
|
106
105
|
|
|
107
106
|
const refetchConfig = async () => {
|
|
108
107
|
const newConfig = await fetchConfig(configApiUrl, cookies);
|
|
108
|
+
setDebugMode(newConfig?.debug_mode);
|
|
109
|
+
setCookie(cookies.debugCookie, debugMode);
|
|
109
110
|
|
|
110
|
-
if (
|
|
111
|
-
console.log("
|
|
112
|
-
console.log("fetchAndSetConfig() config:", newConfig);
|
|
111
|
+
if (debugMode) {
|
|
112
|
+
console.log("refetchConfig() config:", newConfig);
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
PropTypes.checkPropTypes(ConfigPropTypes, newConfig, "prop", "SmarterChat");
|
|
@@ -121,7 +121,9 @@ function SmarterChat({
|
|
|
121
121
|
try {
|
|
122
122
|
const newConfig = await refetchConfig();
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
if (debugMode) {
|
|
125
|
+
console.log("fetchAndSetConfig() config:", newConfig);
|
|
126
|
+
}
|
|
125
127
|
|
|
126
128
|
setPlaceholderText(newConfig.chatbot.app_placeholder);
|
|
127
129
|
setConfigApiUrl(newConfig.chatbot.url_chatbot);
|
|
@@ -154,7 +156,7 @@ function SmarterChat({
|
|
|
154
156
|
setIsReady(true);
|
|
155
157
|
setIsTyping(false);
|
|
156
158
|
|
|
157
|
-
if (
|
|
159
|
+
if (debugMode) {
|
|
158
160
|
console.log("fetchAndSetConfig() done!");
|
|
159
161
|
}
|
|
160
162
|
} catch (error) {
|
|
@@ -200,9 +202,6 @@ function SmarterChat({
|
|
|
200
202
|
}
|
|
201
203
|
if (["smarter", "system", "tool"].includes(message.sender)) {
|
|
202
204
|
// toggle backend messages
|
|
203
|
-
if (debugMode) {
|
|
204
|
-
//console.log("toggle message:", message);
|
|
205
|
-
}
|
|
206
205
|
return { ...message, display: newValue };
|
|
207
206
|
} else {
|
|
208
207
|
// always show user and assistant messages
|
|
@@ -22,65 +22,15 @@
|
|
|
22
22
|
v0.5.0: ./test/events/langchain.response.v0.5.0.json
|
|
23
23
|
-----------------------------------------------------------------------------*/
|
|
24
24
|
|
|
25
|
+
import { getCookie, setCookie } from "../shared/cookie";
|
|
26
|
+
|
|
25
27
|
// Set to true to enable local development mode,
|
|
26
28
|
// which will simulate the server-side API calls.
|
|
27
29
|
const developerMode = false;
|
|
30
|
+
let debugMode = developerMode;
|
|
28
31
|
const userAgent = "SmarterChat/1.0";
|
|
29
32
|
const applicationJson = "application/json";
|
|
30
33
|
|
|
31
|
-
function getCookie(cookie, defaultValue = null) {
|
|
32
|
-
console.log("getCookie(): cookie", cookie);
|
|
33
|
-
if (cookie.value !== null) {
|
|
34
|
-
console.log("getCookie(): ", cookie.domain, cookie.name, cookie.value);
|
|
35
|
-
return cookie.value;
|
|
36
|
-
}
|
|
37
|
-
let cookieValue = null;
|
|
38
|
-
|
|
39
|
-
if (window.location.hostname.endsWith(cookie.domain) && document.cookie && document.cookie !== "") {
|
|
40
|
-
const cookies = document.cookie.split(";").map(cookie => cookie.trim());
|
|
41
|
-
for (let i = 0; i < cookies.length; i++) {
|
|
42
|
-
const thisCookie = cookies[i];
|
|
43
|
-
if (thisCookie.startsWith(`${cookie.name}=`)) {
|
|
44
|
-
cookieValue = decodeURIComponent(thisCookie.substring(cookie.name.length + 1));
|
|
45
|
-
console.log("getCookie(): ", cookie.domain, cookie.name, cookieValue);
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return cookieValue || defaultValue;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function setCookie(cookie, value) {
|
|
56
|
-
const currentPath = window.location.pathname;
|
|
57
|
-
if (value) {
|
|
58
|
-
const expirationDate = new Date();
|
|
59
|
-
expirationDate.setTime(expirationDate.getTime() + cookie.expiration);
|
|
60
|
-
const expires = expirationDate.toUTCString();
|
|
61
|
-
const cookieData = `${cookie.name}=${value}; path=${currentPath}; SameSite=Lax; expires=${expires}`;
|
|
62
|
-
document.cookie = cookieData;
|
|
63
|
-
if (developerMode) {
|
|
64
|
-
console.log(
|
|
65
|
-
"setCookie(): ",
|
|
66
|
-
cookieData,
|
|
67
|
-
"now: ",
|
|
68
|
-
new Date().toUTCString(),
|
|
69
|
-
"expiration: ",
|
|
70
|
-
expirationDate.toUTCString(),
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
// Unset the cookie by setting its expiration date to the past
|
|
75
|
-
const expirationDate = new Date(0);
|
|
76
|
-
const expires = expirationDate.toUTCString();
|
|
77
|
-
const cookieData = `${cookie.name}=; path=${currentPath}; SameSite=Lax; expires=${expires}`;
|
|
78
|
-
document.cookie = cookieData;
|
|
79
|
-
if (developerMode) {
|
|
80
|
-
console.log("setCookie(): Unsetting cookie", cookieData);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
34
|
|
|
85
35
|
function promptRequestBodyFactory(messages, config) {
|
|
86
36
|
const body = {
|
|
@@ -91,7 +41,9 @@ function promptRequestBodyFactory(messages, config) {
|
|
|
91
41
|
}
|
|
92
42
|
|
|
93
43
|
function requestHeadersFactory(cookies) {
|
|
94
|
-
|
|
44
|
+
if (debugMode) {
|
|
45
|
+
console.log("requestHeadersFactory(): cookies", cookies);
|
|
46
|
+
}
|
|
95
47
|
function getRequestCookies(cookies) {
|
|
96
48
|
// Ensure that csrftoken is not included in the Cookie header.
|
|
97
49
|
const cookiesArray = document.cookie.split(";").filter((cookie) => {
|
|
@@ -117,7 +69,9 @@ function requestHeadersFactory(cookies) {
|
|
|
117
69
|
Authorization: `Bearer ${authToken}`,
|
|
118
70
|
"User-Agent": userAgent,
|
|
119
71
|
};
|
|
120
|
-
|
|
72
|
+
if (debugMode) {
|
|
73
|
+
console.log("requestHeadersFactory(): requestHeaders", requestHeaders);
|
|
74
|
+
}
|
|
121
75
|
return requestHeaders;
|
|
122
76
|
}
|
|
123
77
|
|
|
@@ -145,9 +99,9 @@ function urlFactory(apiUrl, endpoint, sessionKey) {
|
|
|
145
99
|
}
|
|
146
100
|
|
|
147
101
|
async function getJsonResponse(url, init, cookies) {
|
|
148
|
-
|
|
102
|
+
|
|
149
103
|
try {
|
|
150
|
-
if (debugMode
|
|
104
|
+
if (debugMode) {
|
|
151
105
|
console.log("getJsonResponse(): url: ", url, ", init: ", init, ", cookies: ", cookies);
|
|
152
106
|
}
|
|
153
107
|
const response = await fetch(url, init);
|
|
@@ -157,7 +111,7 @@ async function getJsonResponse(url, init, cookies) {
|
|
|
157
111
|
if (response.ok) {
|
|
158
112
|
const responseJson = await response.json(); // Convert the ReadableStream to a JSON object
|
|
159
113
|
const responseJsonData = await responseJson.data; // ditto
|
|
160
|
-
if (debugMode
|
|
114
|
+
if (debugMode) {
|
|
161
115
|
console.log("getJsonResponse(): response: ", responseJson);
|
|
162
116
|
}
|
|
163
117
|
return responseJsonData;
|
|
@@ -184,7 +138,11 @@ async function getJsonResponse(url, init, cookies) {
|
|
|
184
138
|
}
|
|
185
139
|
|
|
186
140
|
export async function fetchPrompt(config, messages, cookies) {
|
|
187
|
-
|
|
141
|
+
debugMode = config.debug_mode || developerMode;
|
|
142
|
+
|
|
143
|
+
if (debugMode) {
|
|
144
|
+
console.log("fetchPrompt(): config", config);
|
|
145
|
+
}
|
|
188
146
|
const apiUrl = config.chatbot.url_chatbot;
|
|
189
147
|
const sessionKey = getCookie(cookies.sessionCookie, "");
|
|
190
148
|
const url = urlFactory(apiUrl, null, sessionKey);
|
|
@@ -193,7 +151,9 @@ export async function fetchPrompt(config, messages, cookies) {
|
|
|
193
151
|
const init = requestInitFactory(headers, body);
|
|
194
152
|
const responseJson = await getJsonResponse(url, init, cookies);
|
|
195
153
|
if (responseJson && responseJson.body) {
|
|
196
|
-
|
|
154
|
+
if (debugMode) {
|
|
155
|
+
console.log("fetchPrompt(): parsing responseJson.body ");
|
|
156
|
+
}
|
|
197
157
|
const responseBody = await JSON.parse(responseJson.body);
|
|
198
158
|
return responseBody;
|
|
199
159
|
}
|
|
@@ -235,6 +195,7 @@ export async function fetchConfig(apiUrl, cookies) {
|
|
|
235
195
|
if (newConfig) {
|
|
236
196
|
setCookie(cookies.sessionCookie, newConfig.session_key);
|
|
237
197
|
setCookie(cookies.debugCookie, newConfig.debug_mode);
|
|
198
|
+
debugMode = newConfig.debug_mode || developerMode;
|
|
238
199
|
return newConfig;
|
|
239
200
|
}
|
|
240
201
|
return null;
|
|
@@ -1,16 +1,5 @@
|
|
|
1
1
|
import { MessageDirectionEnum, SenderRoleEnum, ValidMessageRolesEnum } from "./enums.js";
|
|
2
2
|
|
|
3
|
-
export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain, cookieValue = null) {
|
|
4
|
-
/*
|
|
5
|
-
Create a cookie object.
|
|
6
|
-
*/
|
|
7
|
-
return {
|
|
8
|
-
name: cookieName,
|
|
9
|
-
expiration: cookieExpiration,
|
|
10
|
-
domain: cookieDomain,
|
|
11
|
-
value: cookieValue,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
3
|
|
|
15
4
|
export function chatRestoreFromBackend(chat_history, last_response) {
|
|
16
5
|
/*
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*-----------------------------------------------------------------------------
|
|
2
|
+
Description: cookie management functions for the SmarterChat application.
|
|
3
|
+
-----------------------------------------------------------------------------*/
|
|
4
|
+
export function getCookie(cookie, defaultValue = null) {
|
|
5
|
+
if (cookie.value !== null) {
|
|
6
|
+
return cookie.value;
|
|
7
|
+
}
|
|
8
|
+
let cookieValue = null;
|
|
9
|
+
|
|
10
|
+
if (window.location.hostname.endsWith(cookie.domain) && document.cookie && document.cookie !== "") {
|
|
11
|
+
const cookies = document.cookie.split(";").map((cookie) => cookie.trim());
|
|
12
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
13
|
+
const thisCookie = cookies[i];
|
|
14
|
+
if (thisCookie.startsWith(`${cookie.name}=`)) {
|
|
15
|
+
cookieValue = decodeURIComponent(thisCookie.substring(cookie.name.length + 1));
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return cookieValue || defaultValue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function setCookie(cookie, value) {
|
|
25
|
+
const currentPath = window.location.pathname;
|
|
26
|
+
if (value) {
|
|
27
|
+
const expirationDate = new Date();
|
|
28
|
+
expirationDate.setTime(expirationDate.getTime() + cookie.expiration);
|
|
29
|
+
const expires = expirationDate.toUTCString();
|
|
30
|
+
const cookieData = `${cookie.name}=${value}; path=${currentPath}; SameSite=Lax; expires=${expires}`;
|
|
31
|
+
document.cookie = cookieData;
|
|
32
|
+
} else {
|
|
33
|
+
// Unset the cookie by setting its expiration date to the past
|
|
34
|
+
const expirationDate = new Date(0);
|
|
35
|
+
const expires = expirationDate.toUTCString();
|
|
36
|
+
const cookieData = `${cookie.name}=; path=${currentPath}; SameSite=Lax; expires=${expires}`;
|
|
37
|
+
document.cookie = cookieData;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain, cookieValue = null) {
|
|
42
|
+
/*
|
|
43
|
+
Create a cookie object.
|
|
44
|
+
*/
|
|
45
|
+
return {
|
|
46
|
+
name: cookieName,
|
|
47
|
+
expiration: cookieExpiration,
|
|
48
|
+
domain: cookieDomain,
|
|
49
|
+
value: cookieValue,
|
|
50
|
+
};
|
|
51
|
+
}
|