Skip to content

Commit 0bd3a5e

Browse files
authored
Add payload size limit (#3017)
* Add payload_size_limit * Add input count and input limit * Add asymmetrical margin for ease of reading * Fix format
1 parent 2665d0d commit 0bd3a5e

File tree

3 files changed

+65
-28
lines changed

3 files changed

+65
-28
lines changed

src/pages/sicp/subcomponents/chatbot/ChatBox.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const ChatBox: React.FC<Props> = ({ getSection, getText }) => {
3232
const [chatId, setChatId] = useState<string>();
3333
const [messages, setMessages] = useState<ChatMessage[]>([INITIAL_MESSAGE]);
3434
const [userInput, setUserInput] = useState('');
35+
const [maxContentSize, setMaxContentSize] = useState(1000);
3536
const tokens = useTokens();
3637

3738
const handleUserInput = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -56,7 +57,7 @@ const ChatBox: React.FC<Props> = ({ getSection, getText }) => {
5657
.finally(() => {
5758
setIsLoading(false);
5859
});
59-
}, [chatId, tokens, userInput]);
60+
}, [chatId, tokens, userInput, maxContentSize]);
6061

6162
const keyDown: React.KeyboardEventHandler<HTMLInputElement> = useCallback(
6263
e => {
@@ -71,8 +72,11 @@ const ChatBox: React.FC<Props> = ({ getSection, getText }) => {
7172
initChat(tokens, getSection(), getText()).then(resp => {
7273
const message = resp.response;
7374
const conversationId = resp.conversationId;
75+
const maxMessageSize = resp.maxContentSize;
7476
setMessages([message]);
77+
setMaxContentSize(maxMessageSize);
7578
setChatId(conversationId);
79+
setUserInput('');
7680
});
7781
}, [getSection, getText, tokens]);
7882

@@ -100,22 +104,29 @@ const ChatBox: React.FC<Props> = ({ getSection, getText }) => {
100104
))}
101105
{isLoading && <p>loading...</p>}
102106
</div>
103-
<input
104-
type="text"
105-
disabled={isLoading}
106-
className={classes['user-input']}
107-
placeholder={isLoading ? 'Waiting for response...' : 'Type your message here...'}
108-
value={userInput}
109-
onChange={handleUserInput}
110-
onKeyDown={keyDown}
111-
/>
112-
<div className={classes['button-container']}>
113-
<Button disabled={isLoading} className={classes['button-send']} onClick={sendMessage}>
114-
Send
115-
</Button>
116-
<Button className={classes['button-clean']} onClick={resetChat}>
117-
Clean
118-
</Button>
107+
<div className={classes['control-container']}>
108+
<input
109+
type="text"
110+
disabled={isLoading}
111+
className={classes['user-input']}
112+
placeholder={isLoading ? 'Waiting for response...' : 'Type your message here...'}
113+
value={userInput}
114+
onChange={handleUserInput}
115+
onKeyDown={keyDown}
116+
maxLength={maxContentSize}
117+
/>
118+
<div className={classes['input-count-container']}>
119+
<div className={classes['input-count']}>{`${userInput.length}/${maxContentSize}`}</div>
120+
</div>
121+
122+
<div className={classes['button-container']}>
123+
<Button disabled={isLoading} className={classes['button-send']} onClick={sendMessage}>
124+
Send
125+
</Button>
126+
<Button className={classes['button-clean']} onClick={resetChat}>
127+
Clean
128+
</Button>
129+
</div>
119130
</div>
120131
</div>
121132
);

src/pages/sicp/subcomponents/chatbot/types.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ChatMessage = {
66
type InitChatResponse = {
77
response: ChatMessage;
88
conversationId: string;
9+
maxContentSize: number;
910
};
1011

1112
type ContinueChatResponse = {

src/styles/Chatbot.module.scss

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@
66
z-index: 1000;
77
}
88

9+
.chat-container {
10+
position: relative;
11+
width: 400px;
12+
height: 450px;
13+
border-radius: 5px;
14+
background-color: #f1f1f1;
15+
padding: 16px;
16+
z-index: 1000;
17+
}
18+
19+
.control-container {
20+
display: flex;
21+
flex-direction: column;
22+
& > :first-child {
23+
margin-top: 10px;
24+
}
25+
}
26+
927
.bot-area {
1028
position: relative;
1129
}
@@ -50,16 +68,6 @@
5068
border-radius: 50%;
5169
}
5270

53-
.chat-container {
54-
position: relative;
55-
width: 400px;
56-
height: 450px;
57-
border-radius: 5px;
58-
background-color: #f1f1f1;
59-
padding: 16px;
60-
z-index: 1000;
61-
}
62-
6371
.chat-message {
6472
height: 80%;
6573
border: 1px solid #ddd;
@@ -82,11 +90,12 @@
8290
background-color: #a3a3a4;
8391
color: #fff;
8492
font-size: 15px;
85-
text-align: right;
93+
text-align: left;
8694
padding: 10px;
8795
line-height: 1.5;
8896
border-radius: 10px;
8997
display: block;
98+
margin-bottom: 5px;
9099
}
91100

92101
.assistant {
@@ -98,6 +107,7 @@
98107
line-height: 1.5;
99108
border-radius: 10px;
100109
display: block;
110+
margin-bottom: 10px;
101111
}
102112

103113
.button-container {
@@ -130,3 +140,18 @@
130140
border-radius: 10px;
131141
vertical-align: top;
132142
}
143+
144+
.input-count-container {
145+
justify-content: flex-end;
146+
display: flex;
147+
align-items: center;
148+
height: 20px;
149+
}
150+
.input-count {
151+
font-size: 10px;
152+
color: #666;
153+
margin: 0;
154+
height: 100%;
155+
display: flex;
156+
align-items: center;
157+
}

0 commit comments

Comments
 (0)