@smarter.sh/ui-chat 0.2.6 → 0.2.8

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.6",
3
+ "version": "0.2.8",
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",
@@ -49,14 +49,15 @@ function SmarterChat({
49
49
  apiUrl, // the URL of the Smarter chatbot API. example: https://smarter.3141-5926-5359.beta.api.smarter.sh/
50
50
  apiKey, // NOT USED. TO DELETE.
51
51
  toggleMetadata, // show/hide toggle button to show/hide the chat thread metadata
52
- csrfCookieName, // the Django CSRF cookie.
52
+ csrfCookieName = "csrftoken", // the Django CSRF cookie.
53
+ csrftoken, // the Django CSRF token. Passed from the Django template in the Smarter web console workbench.
53
54
  debugCookieName, // the Smarter chat debug cookie. Set here.
54
55
  debugCookieExpiration, // the Smarter chat debug cookie. Set here.
55
- sessionCookieName, // the Smarter chat session cookie. Set here, where the user creates a new chat session.
56
+ sessionCookieName = "session_key", // the Smarter chat session cookie. Set here, where the user creates a new chat session.
56
57
  sessionCookieExpiration, // the Smarter chat session cookie. Set here, where the user creates a new chat session.
57
- authSessionCookieName, // the Django session cookie. Set when the user logs in to the Smarter web console app.
58
+ authSessionCookieName = "sessionid", // the Django session cookie. Set when the user logs in to the Smarter web console app.
58
59
  showConsole = true, // show the server console log component
59
- cookieDomain, // the domain of the cookie. This is added to the cookie meta data, but it is not used.
60
+ cookieDomain, // the domain of the cookie. This is added to the cookie meta data to restrict the domain that this component will read cookie data.
60
61
  }) {
61
62
  const [configApiUrl, setConfigApiUrl] = useState(apiUrl);
62
63
 
@@ -89,10 +90,10 @@ function SmarterChat({
89
90
  const fileInputRef = useRef(null);
90
91
 
91
92
  // cookie management
92
- const csrfCookie = cookieMetaFactory(csrfCookieName, null, cookieDomain); // we read this but never set it.
93
- const authTokenCookie = cookieMetaFactory(authSessionCookieName, null, cookieDomain); // we read this but never set it.
94
- const sessionCookie = cookieMetaFactory(sessionCookieName, sessionCookieExpiration, cookieDomain);
95
- const debugCookie = cookieMetaFactory(debugCookieName, debugCookieExpiration, cookieDomain);
93
+ const csrfCookie = cookieMetaFactory(csrfCookieName, null, cookieDomain, csrftoken); // we read this but never set it.
94
+ const authTokenCookie = cookieMetaFactory(authSessionCookieName, null, cookieDomain, null); // we read this but never set it.
95
+ const sessionCookie = cookieMetaFactory(sessionCookieName, sessionCookieExpiration, cookieDomain, null);
96
+ const debugCookie = cookieMetaFactory(debugCookieName, debugCookieExpiration, cookieDomain, null);
96
97
  const cookies = {
97
98
  authTokenCookie: authTokenCookie, // the Django session cookie. Set when the user logs in to the Smarter web console app.
98
99
  // typically this is not required for the chat app when running inside the
@@ -29,21 +29,26 @@ const userAgent = "SmarterChat/1.0";
29
29
  const applicationJson = "application/json";
30
30
 
31
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
+ }
32
37
  let cookieValue = null;
33
- const expectedDomain = cookie.domain;
34
38
 
35
- // Check if the cookie is set for the expected domain. example: alpha.platform.smarter.sh
36
- if (window.location.hostname === expectedDomain && document.cookie && document.cookie !== "") {
37
- const cookies = document.cookie.split(";");
39
+ if (window.location.hostname.endsWith(cookie.domain) && document.cookie && document.cookie !== "") {
40
+ const cookies = document.cookie.split(";").map(cookie => cookie.trim());
38
41
  for (let i = 0; i < cookies.length; i++) {
39
- const thisCookie = cookies[i].trim();
40
- if (thisCookie.substring(0, cookie.name.length + 1) === cookie.name + "=") {
42
+ const thisCookie = cookies[i];
43
+ if (thisCookie.startsWith(`${cookie.name}=`)) {
41
44
  cookieValue = decodeURIComponent(thisCookie.substring(cookie.name.length + 1));
45
+ console.log("getCookie(): ", cookie.domain, cookie.name, cookieValue);
42
46
  break;
43
47
  }
44
48
  }
45
49
  }
46
50
 
51
+
47
52
  return cookieValue || defaultValue;
48
53
  }
49
54
 
@@ -94,6 +99,8 @@ function requestHeadersFactory(cookies) {
94
99
  return !trimmedCookie.startsWith(`${cookies.csrfCookie.name}=`);
95
100
  });
96
101
  const selectedCookies = cookiesArray.join("; ");
102
+
103
+ // example return value: "_ga=GA1.1.1244182935.1742308279; _ga_SK81M5HQYS=GS1.1.1742316653.3.1.1742320251.0.0.0"
97
104
  return selectedCookies;
98
105
  }
99
106
 
@@ -1,6 +1,6 @@
1
1
  import { MessageDirectionEnum, SenderRoleEnum, ValidMessageRolesEnum } from "./enums.js";
2
2
 
3
- export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain) {
3
+ export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain, cookieValue = null) {
4
4
  /*
5
5
  Create a cookie object.
6
6
  */
@@ -8,6 +8,7 @@ export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain) {
8
8
  name: cookieName,
9
9
  expiration: cookieExpiration,
10
10
  domain: cookieDomain,
11
+ value: cookieValue,
11
12
  };
12
13
  }
13
14