@smarter.sh/ui-chat 0.2.8 → 0.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smarter.sh/ui-chat",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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",
@@ -36,7 +36,7 @@ function Console({ config }) {
36
36
  // Lifecycle hooks
37
37
  useEffect(() => {
38
38
  if (config) {
39
- if (config.debug_mode) {
39
+ if (config?.debug_mode) {
40
40
  console.log('Console() component mounted');
41
41
  }
42
42
 
@@ -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 { setCookie, fetchConfig, fetchPrompt } from "./api.js";
38
+ import { fetchConfig, fetchPrompt } from "./api.js";
39
+ import { setCookie } from "../shared/cookie.js"
39
40
  import { cookieMetaFactory, 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, // the Django CSRF token. Passed from the Django template in the Smarter web console workbench.
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(DEBUG_MODE);
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 (newConfig?.debug_mode) {
111
- console.log("fetchAndSetConfig()...");
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
- console.log("fetchAndSetConfig() config:", newConfig);
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 (newConfig?.debug_mode) {
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
+ const debugMode = getCookie(cookies.debugCookie) === "true" || 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
- console.log("requestHeadersFactory(): cookies", cookies);
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
- console.log("requestHeadersFactory(): requestHeaders", requestHeaders);
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
- const debugMode = getCookie(cookies.debugCookie) === "true";
102
+
149
103
  try {
150
- if (debugMode || developerMode) {
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 || developerMode) {
114
+ if (debugMode) {
161
115
  console.log("getJsonResponse(): response: ", responseJson);
162
116
  }
163
117
  return responseJsonData;
@@ -184,7 +138,9 @@ async function getJsonResponse(url, init, cookies) {
184
138
  }
185
139
 
186
140
  export async function fetchPrompt(config, messages, cookies) {
187
- console.log("fetchPrompt(): config", config);
141
+ if (debugMode) {
142
+ console.log("fetchPrompt(): config", config);
143
+ }
188
144
  const apiUrl = config.chatbot.url_chatbot;
189
145
  const sessionKey = getCookie(cookies.sessionCookie, "");
190
146
  const url = urlFactory(apiUrl, null, sessionKey);
@@ -193,7 +149,9 @@ export async function fetchPrompt(config, messages, cookies) {
193
149
  const init = requestInitFactory(headers, body);
194
150
  const responseJson = await getJsonResponse(url, init, cookies);
195
151
  if (responseJson && responseJson.body) {
196
- console.log("fetchPrompt(): parsing responseJson.body ");
152
+ if (debugMode) {
153
+ console.log("fetchPrompt(): parsing responseJson.body ");
154
+ }
197
155
  const responseBody = await JSON.parse(responseJson.body);
198
156
  return responseBody;
199
157
  }
@@ -0,0 +1,39 @@
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
+ }