@smarter.sh/ui-chat 0.2.4 → 0.2.6

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.4",
3
+ "version": "0.2.6",
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",
@@ -7,8 +7,8 @@
7
7
 
8
8
  // React stuff
9
9
  import React, { useRef, useState, useEffect } from "react";
10
- import PropTypes from 'prop-types';
11
- import ConfigPropTypes from '../../types/propTypes.js';
10
+ import PropTypes from "prop-types";
11
+ import ConfigPropTypes from "../../types/propTypes.js";
12
12
 
13
13
  import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
14
14
  import { faCheckCircle, faTimesCircle, faRocket } from "@fortawesome/free-solid-svg-icons";
@@ -29,18 +29,16 @@ import {
29
29
 
30
30
  // this repo
31
31
  import { ErrorModal } from "../ErrorModal/ErrorModal.jsx";
32
- import { Console} from "../Console/index.js";
32
+ import { Console } from "../Console/index.js";
33
33
 
34
34
  // This component
35
35
  import "./styles.css";
36
- import { ContainerLayout, ContentLayout, WorkbenchLayout, ChatAppLayout, ConsoleLayout} from "./Layout.js"
36
+ import { ContainerLayout, ContentLayout, WorkbenchLayout, ChatAppLayout, ConsoleLayout } from "./Layout.js";
37
37
  import { MessageDirectionEnum, SenderRoleEnum } from "./enums.js";
38
38
  import { setCookie, fetchConfig, fetchPrompt } from "./api.js";
39
39
  import { cookieMetaFactory, messageFactory, chatMessages2RequestMessages, chatInit } from "./utils.jsx";
40
40
  import { ErrorBoundary } from "./ErrorBoundary.jsx";
41
41
 
42
-
43
-
44
42
  const DEBUG_MODE = false;
45
43
 
46
44
  // The main chat component. This is the top-level component that
@@ -48,17 +46,18 @@ const DEBUG_MODE = false;
48
46
  // managing the chat message thread, sending messages to the backend
49
47
  // Api, and rendering the chat UI.
50
48
  function SmarterChat({
51
- apiUrl,
52
- apiKey,
53
- toggleMetadata,
54
- csrfCookieName,
55
- debugCookieName,
56
- debugCookieExpiration,
57
- sessionCookieName,
58
- sessionCookieExpiration,
59
- showConsole=true,
49
+ apiUrl, // the URL of the Smarter chatbot API. example: https://smarter.3141-5926-5359.beta.api.smarter.sh/
50
+ apiKey, // NOT USED. TO DELETE.
51
+ toggleMetadata, // show/hide toggle button to show/hide the chat thread metadata
52
+ csrfCookieName, // the Django CSRF cookie.
53
+ debugCookieName, // the Smarter chat debug cookie. Set here.
54
+ 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
+ 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
+ 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
60
  }) {
61
-
62
61
  const [configApiUrl, setConfigApiUrl] = useState(apiUrl);
63
62
 
64
63
  const [showMetadata, setShowMetadata] = useState(toggleMetadata);
@@ -90,15 +89,19 @@ function SmarterChat({
90
89
  const fileInputRef = useRef(null);
91
90
 
92
91
  // cookie management
93
- const csrfCookie = cookieMetaFactory(csrfCookieName, null); // we read this but never set it.
94
- const sessionCookie = cookieMetaFactory(sessionCookieName, sessionCookieExpiration);
95
- const debugCookie = cookieMetaFactory(debugCookieName, debugCookieExpiration);
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);
96
96
  const cookies = {
97
- csrfCookie: csrfCookie,
98
- sessionCookie: sessionCookie,
99
- debugCookie: debugCookie,
100
- };
97
+ authTokenCookie: authTokenCookie, // the Django session cookie. Set when the user logs in to the Smarter web console app.
98
+ // typically this is not required for the chat app when running inside the
99
+ // Smarter web console workbench, since it is already authenticated.
101
100
 
101
+ csrfCookie: csrfCookie, // the Django CSRF cookie. This is required for requests to the Smarter web console workbench.
102
+ sessionCookie: sessionCookie, // the Smarter chat session cookie. Set here, where the user creates a new chat session.
103
+ debugCookie: debugCookie, // the Smarter chat debug cookie. Set here. Controls browser console logging.
104
+ };
102
105
 
103
106
  const refetchConfig = async () => {
104
107
  const newConfig = await fetchConfig(configApiUrl, cookies);
@@ -108,7 +111,7 @@ function SmarterChat({
108
111
  console.log("fetchAndSetConfig() config:", newConfig);
109
112
  }
110
113
 
111
- PropTypes.checkPropTypes(ConfigPropTypes, newConfig, 'prop', 'SmarterChat');
114
+ PropTypes.checkPropTypes(ConfigPropTypes, newConfig, "prop", "SmarterChat");
112
115
  setConfig(newConfig);
113
116
  return newConfig;
114
117
  };
@@ -362,12 +365,20 @@ function SmarterChat({
362
365
  <ChatContainer style={chatContainerStyleOverrides}>
363
366
  <ConversationHeader>
364
367
  <ConversationHeader.Content
365
- userName={isReady ? <AppTitle title={title} isValid={isValid} isDeployed={isDeployed}/>: "Configuring workbench..."}
366
- info={isReady ? info: ""}
368
+ userName={
369
+ isReady ? (
370
+ <AppTitle title={title} isValid={isValid} isDeployed={isDeployed} />
371
+ ) : (
372
+ "Configuring workbench..."
373
+ )
374
+ }
375
+ info={isReady ? info : ""}
367
376
  />
368
377
  <ConversationHeader.Actions>
369
378
  <AddUserButton onClick={handleAddUserButtonClick} title="Start a new chat" />
370
- {toggleMetadata && <InfoButton onClick={handleInfoButtonClick} title="Toggle system meta data" />}
379
+ {toggleMetadata && (
380
+ <InfoButton onClick={handleInfoButtonClick} title="Toggle system meta data" />
381
+ )}
371
382
  </ConversationHeader.Actions>
372
383
  </ConversationHeader>
373
384
  <MessageList
@@ -401,9 +412,9 @@ function SmarterChat({
401
412
  </div>
402
413
  </ChatAppLayout>
403
414
  {showConsole && (
404
- <ConsoleLayout>
405
- <Console config={config} />
406
- </ConsoleLayout>
415
+ <ConsoleLayout>
416
+ <Console config={config} />
417
+ </ConsoleLayout>
407
418
  )}
408
419
  </WorkbenchLayout>
409
420
  </ContentLayout>
@@ -30,22 +30,20 @@ const applicationJson = "application/json";
30
30
 
31
31
  function getCookie(cookie, defaultValue = null) {
32
32
  let cookieValue = null;
33
- if (document.cookie && document.cookie !== "") {
33
+ const expectedDomain = cookie.domain;
34
+
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 !== "") {
34
37
  const cookies = document.cookie.split(";");
35
38
  for (let i = 0; i < cookies.length; i++) {
36
39
  const thisCookie = cookies[i].trim();
37
40
  if (thisCookie.substring(0, cookie.name.length + 1) === cookie.name + "=") {
38
41
  cookieValue = decodeURIComponent(thisCookie.substring(cookie.name.length + 1));
39
- if (developerMode) {
40
- console.log("getCookie(): found ", cookieValue, "for cookie", cookie.name);
41
- }
42
42
  break;
43
43
  }
44
44
  }
45
45
  }
46
- if (developerMode && !cookieValue) {
47
- console.warn("getCookie(): no value found for", cookie.name);
48
- }
46
+
49
47
  return cookieValue || defaultValue;
50
48
  }
51
49
 
@@ -101,9 +99,9 @@ function requestHeadersFactory(cookies) {
101
99
 
102
100
  const requestCookies = getRequestCookies(cookies);
103
101
  const csrftoken = getCookie(cookies.csrfCookie, "");
104
- const authToken = null; // FIX NOTE: add me.
102
+ const authToken = getCookie(cookies.authTokenCookie, "");
105
103
 
106
- return {
104
+ const requestHeaders = {
107
105
  Accept: applicationJson,
108
106
  "Content-Type": applicationJson,
109
107
  "X-CSRFToken": csrftoken,
@@ -112,6 +110,8 @@ function requestHeadersFactory(cookies) {
112
110
  Authorization: `Bearer ${authToken}`,
113
111
  "User-Agent": userAgent,
114
112
  };
113
+ console.log("requestHeadersFactory(): requestHeaders", requestHeaders);
114
+ return requestHeaders;
115
115
  }
116
116
 
117
117
  function requestInitFactory(headers, body) {
@@ -1,12 +1,13 @@
1
1
  import { MessageDirectionEnum, SenderRoleEnum, ValidMessageRolesEnum } from "./enums.js";
2
2
 
3
- export function cookieMetaFactory(cookieName, cookieExpiration) {
3
+ export function cookieMetaFactory(cookieName, cookieExpiration, cookieDomain) {
4
4
  /*
5
5
  Create a cookie object.
6
6
  */
7
7
  return {
8
8
  name: cookieName,
9
9
  expiration: cookieExpiration,
10
+ domain: cookieDomain,
10
11
  };
11
12
  }
12
13