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

Add payload size limit #3017

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions src/pages/sicp/subcomponents/chatbot/ChatBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
const [chatId, setChatId] = useState<string>();
const [messages, setMessages] = useState<ChatMessage[]>([INITIAL_MESSAGE]);
const [userInput, setUserInput] = useState('');
const [maxContentSize, setMaxContentSize] = useState(1000);
const tokens = useTokens();

const handleUserInput = (event: React.ChangeEvent<HTMLInputElement>) => {
Expand All @@ -56,7 +57,7 @@
.finally(() => {
setIsLoading(false);
});
}, [chatId, tokens, userInput]);
}, [chatId, tokens, userInput, maxContentSize]);

Check warning on line 60 in src/pages/sicp/subcomponents/chatbot/ChatBox.tsx

View workflow job for this annotation

GitHub Actions / lint (eslint)

React Hook useCallback has an unnecessary dependency: 'maxContentSize'. Either exclude it or remove the dependency array

const keyDown: React.KeyboardEventHandler<HTMLInputElement> = useCallback(
e => {
Expand All @@ -71,8 +72,11 @@
initChat(tokens, getSection(), getText()).then(resp => {
const message = resp.response;
const conversationId = resp.conversationId;
const maxMessageSize = resp.maxContentSize;
setMessages([message]);
setMaxContentSize(maxMessageSize);
setChatId(conversationId);
setUserInput('');
});
}, [getSection, getText, tokens]);

Expand Down Expand Up @@ -100,22 +104,29 @@
))}
{isLoading && <p>loading...</p>}
</div>
<input
type="text"
disabled={isLoading}
className={classes['user-input']}
placeholder={isLoading ? 'Waiting for response...' : 'Type your message here...'}
value={userInput}
onChange={handleUserInput}
onKeyDown={keyDown}
/>
<div className={classes['button-container']}>
<Button disabled={isLoading} className={classes['button-send']} onClick={sendMessage}>
Send
</Button>
<Button className={classes['button-clean']} onClick={resetChat}>
Clean
</Button>
<div className={classes['control-container']}>
<input
type="text"
disabled={isLoading}
className={classes['user-input']}
placeholder={isLoading ? 'Waiting for response...' : 'Type your message here...'}
value={userInput}
onChange={handleUserInput}
onKeyDown={keyDown}
maxLength={maxContentSize}
/>
<div className={classes['input-count-container']}>
<div className={classes['input-count']}>{`${userInput.length}/${maxContentSize}`}</div>
</div>

<div className={classes['button-container']}>
<Button disabled={isLoading} className={classes['button-send']} onClick={sendMessage}>
Send
</Button>
<Button className={classes['button-clean']} onClick={resetChat}>
Clean
</Button>
</div>
</div>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/pages/sicp/subcomponents/chatbot/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ChatMessage = {
type InitChatResponse = {
response: ChatMessage;
conversationId: string;
maxContentSize: number;
};

type ContinueChatResponse = {
Expand Down
47 changes: 36 additions & 11 deletions src/styles/Chatbot.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
z-index: 1000;
}

.chat-container {
position: relative;
width: 400px;
height: 450px;
border-radius: 5px;
background-color: #f1f1f1;
padding: 16px;
z-index: 1000;
}

.control-container {
display: flex;
flex-direction: column;
& > :first-child {
margin-top: 10px;
}
}

.bot-area {
position: relative;
}
Expand Down Expand Up @@ -50,16 +68,6 @@
border-radius: 50%;
}

.chat-container {
position: relative;
width: 400px;
height: 450px;
border-radius: 5px;
background-color: #f1f1f1;
padding: 16px;
z-index: 1000;
}

.chat-message {
height: 80%;
border: 1px solid #ddd;
Expand All @@ -82,11 +90,12 @@
background-color: #a3a3a4;
color: #fff;
font-size: 15px;
text-align: right;
text-align: left;
padding: 10px;
line-height: 1.5;
border-radius: 10px;
display: block;
margin-bottom: 5px;
}

.assistant {
Expand All @@ -98,6 +107,7 @@
line-height: 1.5;
border-radius: 10px;
display: block;
margin-bottom: 10px;
}

.button-container {
Expand Down Expand Up @@ -130,3 +140,18 @@
border-radius: 10px;
vertical-align: top;
}

.input-count-container {
justify-content: flex-end;
display: flex;
align-items: center;
height: 20px;
}
.input-count {
font-size: 10px;
color: #666;
margin: 0;
height: 100%;
display: flex;
align-items: center;
}
Loading