Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/10-Explainability-frame #11

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ The widget can be initialized in one of two ways:
* Add the following div to your website:
`<div id="chatWidgetContainer"></div>`
* Add the following script to your website:
`<script>initChatWidget()</script>`
`<script>ChatWidget()</script>`

There is configuration associated with the widget. You can pass it to the `initChatWidget` function as an object or to the `div` element as attributes.
There is configuration associated with the widget. You can pass it to the `ChatWidget` function as an object or to the `div` element as attributes.

| Attribute | Description | Default value |
| ------------------- | ----------------------- | ----------------------- |
Expand Down Expand Up @@ -55,7 +55,7 @@ Example usage:
serverUrl: "http://127.0.0.1:5000",
useFeedback: true,
useLogin: true,
});
}, "customContainerId");
</script>
```

Expand Down
5 changes: 4 additions & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<h1>Chat</h1>
<div id="root" style="margin: 0 100px;">
<h1>Chat</h1>
<p>Chat widget</p>
<div
id="chatWidgetContainer"
data-name="DevBot"
data-server-url="http://127.0.0.1:5000"
data-show-explanation
>
</div>
<p>Powered by <a href="">DevBot</a></p>
</div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
Expand Down
29 changes: 24 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { Config } from "./types";
import { useContext } from "react";
import { ConfigContext } from "./contexts/ConfigContext";
import { UserContext } from "./contexts/UserContext";
import ChatBox from "./components/ChatBox/ChatBox";
import LoginForm from "./components/LoginForm/LoginForm";
import ChatWidget from "./components/Widget/ChatWidget";
import ChatEmbedded from "./components/Embedded/ChatEmbedded";
import ExplainableBox from "./components/Explainability/ExplainableBox";

export default function App(props: Config) {
return props.useWidget ? (
<ChatWidget {...props} />
export default function App() {
const { config } = useContext(ConfigContext);
const { user } = useContext(UserContext);

const showExplanation = config.showExplanation && user?.username;
const content = !user ? <LoginForm /> : <ChatBox />;
return config.useWidget ? (
<ChatWidget>{content}</ChatWidget>
) : (
<ChatEmbedded {...props} />
<ChatEmbedded>
{showExplanation && (
<div className="col md-6">
<ExplainableBox />
</div>
)}
<div className={showExplanation ? "col md-6" : "col md-12"}>
{content}
</div>
</ChatEmbedded>
);
}
12 changes: 9 additions & 3 deletions src/components/ChatBox/ChatBox.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#chatBox .form-control {
.chat .form-control {
border-color: transparent;
}

#chatBox .form-control:focus {
.chat .form-control:focus {
border-color: transparent;
box-shadow: inset 0px 0px 0px 1px transparent;
}
Expand All @@ -13,4 +13,10 @@
flex: 1;
height: 1px;
background: #eee;
}
}

.chat.card-body{
overflow-y: auto;
display: flex;
flex-direction: column-reverse;
}
89 changes: 39 additions & 50 deletions src/components/ChatBox/ChatBox.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import "./ChatBox.css";

import React, { useState, useEffect, useRef, useCallback } from "react";
import React, {
useState,
useEffect,
useRef,
useCallback,
useContext,
} from "react";
import QuickReplyButton from "../QuickReply";

import {
MDBCard,
MDBCardHeader,
MDBCardBody,
MDBIcon,
MDBCardFooter,
} from "mdb-react-ui-kit";
import { useSocket } from "../../contexts/SocketContext";
import { MDBCardBody, MDBIcon, MDBCardFooter } from "mdb-react-ui-kit";
import { AgentChatMessage, UserChatMessage } from "../ChatMessage";
import { ChatMessage } from "../../types";
import useSocketConnection from "../socket_connector";
import { ConfigContext } from "../../contexts/ConfigContext";
import Frame from "../Frame/Frame";

export default function ChatBox({
name,
use_feedback,
serverUrl,
socketioPath,
}: {
name: string;
use_feedback: boolean;
serverUrl: string | undefined;
socketioPath: string | undefined;
}) {
export default function ChatBox() {
const { config } = useContext(ConfigContext);
const {
startConversation,
sendMessage,
quickReply,
onMessage,
onRestart,
giveFeedback,
} = useSocket();
const [chatMessages, setChatMessages] = useState<JSX.Element[]>([]);
const [chatButtons, setChatButtons] = useState<JSX.Element[]>([]);
const [inputValue, setInputValue] = useState<string>("");
const chatMessagesRef = useRef(chatMessages);
const inputRef = useRef<HTMLInputElement>(null);
const connector = useSocketConnection(serverUrl, socketioPath);

useEffect(() => {
startConversation();
}, [startConversation]);

const updateMessages = (message: JSX.Element) => {
chatMessagesRef.current = [...chatMessagesRef.current, message];
Expand All @@ -46,7 +49,7 @@ export default function ChatBox({
message={inputValue}
/>
);
connector.sendMessage({ message: inputValue });
sendMessage({ message: inputValue });
setInputValue("");
if (inputRef.current) {
inputRef.current.value = "";
Expand All @@ -61,9 +64,9 @@ export default function ChatBox({
message={message}
/>
);
connector.quickReply({ message: message });
quickReply({ message: message });
},
[connector, chatMessagesRef]
[chatMessagesRef, quickReply]
);

const handelMessage = useCallback(
Expand All @@ -75,14 +78,14 @@ export default function ChatBox({
updateMessages(
<AgentChatMessage
key={chatMessagesRef.current.length}
feedback={use_feedback ? connector.giveFeedback : null}
feedback={config.useFeedback ? giveFeedback : null}
message={message.text}
image_url={image_url}
/>
);
}
},
[connector, chatMessagesRef, use_feedback]
[giveFeedback, chatMessagesRef, config]
);

const handleButtons = useCallback(
Expand Down Expand Up @@ -111,37 +114,23 @@ export default function ChatBox({
);

useEffect(() => {
connector.onMessage((message: ChatMessage) => {
onMessage((message: ChatMessage) => {
handelMessage(message);
handleButtons(message);
});
}, [connector, handleButtons, handelMessage]);
}, [onMessage, handleButtons, handelMessage]);

useEffect(() => {
connector.onRestart(() => {
onRestart(() => {
setChatMessages([]);
setChatButtons([]);
});
}, [connector]);
}, [onRestart]);

return (
<div className="chat-widget-content">
<MDBCard
id="chatBox"
className="chat-widget-card"
style={{ borderRadius: "15px" }}
>
<MDBCardHeader
className="d-flex justify-content-between align-items-center p-3 bg-info text-white border-bottom-0"
style={{
borderTopLeftRadius: "15px",
borderTopRightRadius: "15px",
}}
>
<p className="mb-0 fw-bold">{name}</p>
</MDBCardHeader>

<MDBCardBody>
<Frame>
<>
<MDBCardBody className="chat">
<div className="card-body-messages">
{chatMessages}
<div className="d-flex flex-wrap justify-content-between">
Expand All @@ -164,7 +153,7 @@ export default function ChatBox({
</button>
</form>
</MDBCardFooter>
</MDBCard>
</div>
</>
</Frame>
);
}
7 changes: 2 additions & 5 deletions src/components/Embedded/ChatEmbedded.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.chat-widget-content .card-body{
overflow-y: auto;
max-height: 500px;
display: flex;
flex-direction: column-reverse;
.chat-widget-content .chat.card-body{
height: 501px;
}
15 changes: 3 additions & 12 deletions src/components/Embedded/ChatEmbedded.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import "./ChatEmbedded.css";
import React from "react";
import ChatBox from "../ChatBox/ChatBox";
import { Config } from "../../types";
import { ReactNode } from "react";

export default function ChatEmbedded(props: Config) {
return (
<ChatBox
name={props.name || "Chatbot"}
use_feedback={props.useFeedback || false}
serverUrl={props.serverUrl}
socketioPath={props.socketioPath}
/>
);
export default function ChatEmbedded({ children }: { children: ReactNode }) {
return <div className="row">{children}</div>;
}
3 changes: 3 additions & 0 deletions src/components/Explainability/ExplainableBox.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.chat-widget-content .exp.card-body{
height: 252px;
}
30 changes: 30 additions & 0 deletions src/components/Explainability/ExplainableBox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "./ExplainableBox.css";
import { useState } from "react";
import { MDBCardBody } from "mdb-react-ui-kit";
import Frame from "../Frame/Frame";

export default function ExplainableBox() {
const [slotValueExplanation, setSlotValueExplanation] = useState<
JSX.Element[]
>([]);
const [neuralExplanation, setNeuralExplanation] = useState<JSX.Element[]>([]);

return (
<>
<div className="row">
<div className="col md-12">
<Frame title="Explainable AI - Slot-Value">
<MDBCardBody className="exp">{slotValueExplanation}</MDBCardBody>
</Frame>
</div>
</div>
<div className="row">
<div className="col md-12">
<Frame title="Explainable AI - Neural Model">
<MDBCardBody className="exp">{neuralExplanation}</MDBCardBody>
</Frame>
</div>
</div>
</>
);
}
Empty file added src/components/Frame/Frame.css
Empty file.
37 changes: 37 additions & 0 deletions src/components/Frame/Frame.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "./Frame.css";

import { useContext } from "react";
import { MDBCard, MDBCardHeader } from "mdb-react-ui-kit";
import { ConfigContext } from "../../contexts/ConfigContext";
import { UserContext } from "../../contexts/UserContext";

export default function Frame({
children,
title,
}: {
children: JSX.Element;
title?: string;
}) {
const { config } = useContext(ConfigContext);
const { user } = useContext(UserContext);

return (
<div className="chat-widget-content">
<MDBCard className="chat-widget-card" style={{ borderRadius: "15px" }}>
<MDBCardHeader
className="d-flex justify-content-between align-items-center p-3 bg-info text-white border-bottom-0"
style={{
borderTopLeftRadius: "15px",
borderTopRightRadius: "15px",
}}
>
<p className="mb-0 fw-bold">{title ? title : config.name}</p>
{user?.username && !title && (
<p className="mb-0 fw-bold">{user?.username}</p>
)}
</MDBCardHeader>
{children}
</MDBCard>
</div>
);
}
20 changes: 20 additions & 0 deletions src/components/LoginForm/LoginForm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.login {
max-width: 400px;
}

.login .form-control {
border-color: transparent;
}

.login .form-control:focus {
border-color: transparent;
box-shadow: inset 0px 0px 0px 1px transparent;
}

.divider:after,
.divider:before {
content: "";
flex: 1;
height: 1px;
background: #eee;
}
Loading