-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.tsx
365 lines (323 loc) · 11.3 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import React, {useRef, useEffect, useCallback, useState} from 'react';
import {Configuration, OpenAIApi, ChatCompletionRequestMessage} from 'openai';
import {type ActionArgs} from '@remix-run/node';
import {Form, useActionData, useNavigation, Link, useSubmit, useLocation, useNavigate} from '@remix-run/react';
import context from '~/context';
import {Send as SendIcon} from '~/components/Icons';
import Message from '~/components/Message';
export interface ReturnedDataProps {
message?: string;
answer: string;
error?: string;
chatHistory: ChatCompletionRequestMessage[];
}
export interface ChatHistoryProps extends ChatCompletionRequestMessage {
error?: boolean,
}
/**
* API call executed server side
*/
export async function action({request}: ActionArgs): Promise<ReturnedDataProps> {
const body = await request.formData();
const message = body.get('message') as string;
const chatHistory = JSON.parse(body.get('chat-history') as string) || [];
// store your key in .env
const conf = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
try {
const openai = new OpenAIApi(conf);
const chat = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
...context,
...chatHistory,
{
role: 'user',
content: message,
},
],
});
const answer = chat.data.choices[0].message?.content;
return {
message: body.get('message') as string,
answer: answer as string,
chatHistory,
};
} catch (error: any) {
return {
message: body.get('message') as string,
answer: '',
error: error.message || 'Something went wrong! Please try again.',
chatHistory,
};
}
}
export default function IndexPage() {
const minTextareaRows = 1;
const maxTextareaRows = 3;
const data = useActionData<typeof action>();
const inputRef = useRef<HTMLTextAreaElement>(null);
const formRef = useRef<HTMLFormElement>(null);
const chatContainerRef = useRef<HTMLDivElement>(null);
const navigation = useNavigation();
const submit = useSubmit();
const [chatHistory, setChatHistory] = useState<ChatHistoryProps[]>([]);
const location = useLocation();
const navigate = useNavigate();
const isSubmitting = navigation.state === 'submitting';
/**
* Handles the change event of a textarea element and adjusts its height based on the content.
* Note: Using the ref to alter the rows rather than state since it's more performant / faster.
* @param event - The change event of the textarea element.
*/
const handleTextareaChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
if (!inputRef.current) {
return;
}
// reset required for when the user pressed backspace (otherwise it would stay at max rows)
inputRef.current.rows = minTextareaRows;
const lineHeight = parseInt(window.getComputedStyle(inputRef.current).lineHeight);
const paddingTop = parseInt(window.getComputedStyle(inputRef.current).paddingTop);
const paddingBottom = parseInt(window.getComputedStyle(inputRef.current).paddingBottom);
const scrollHeight = inputRef.current.scrollHeight - paddingTop - paddingBottom;
const currentRows = Math.floor(scrollHeight / lineHeight);
if (currentRows >= maxTextareaRows) {
inputRef.current.rows = maxTextareaRows;
inputRef.current.scrollTop = event.target.scrollHeight;
} else {
inputRef.current.rows = currentRows;
}
};
/**
* Adds a new message to the chat history
* @param data The message to add
*/
const pushChatHistory = useCallback((data: ChatHistoryProps) => {
setChatHistory(prevState => ([...prevState, data]));
}, [setChatHistory]);
/**
* Saves the user's message to the chat history
* @param content The user's message
*/
const saveUserMessage = (content: string) => {
pushChatHistory({
content,
role: 'user',
})
};
/**
* Ensure the user message is added to the chat on submit (button press)
* @param event Event from the form
*/
const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
const formData = new FormData(event.target as HTMLFormElement);
const message = formData.get('message');
saveUserMessage(message as string);
};
/**
* Submits the form when the user pressed enter but not shift + enter
* Also saves a mesasge to the the chat history
*
* @param event The keydown event
*/
const submitFormOnEnter = useCallback((event: React.KeyboardEvent<HTMLTextAreaElement>) => {
const value = (event.target as HTMLTextAreaElement).value;
if (event.key === 'Enter' && !event.shiftKey && value.trim().length > 2) {
saveUserMessage(value);
submit(formRef.current, {replace: true});
}
}, [submit, formRef, saveUserMessage]);
/**
* Scrolls to the bottom of the chat container
* TODO: Conisder reduced motion
* @param animationDuration The duration of the animation in milliseconds
*/
const scrollToBottom = useCallback((animationDuration: number = 300) => {
const body = document.body;
const html = document.documentElement;
const startTime = performance.now();
const step = (currentTime: number) => {
const targetScrollTop = Math.max(
body.scrollHeight,
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
);
const progress = (currentTime - startTime) / animationDuration;
window.scrollTo({ top: targetScrollTop });
if (progress < 1) {
window.requestAnimationFrame(step);
}
};
window.requestAnimationFrame(step);
}, []);
/**
* Sets the chat history when the page is loaded or the history is traversed (back / forward)
*/
const setChat = useCallback(() => {
if (location.state?.chatHistory) {
setChatHistory(location.state.chatHistory);
scrollToBottom();
}
}, [location, setChatHistory, scrollToBottom]);
/**
* Focuses the input element when the page is loaded and clears the
* input when the form is submitted
*/
useEffect(() => {
if (!inputRef.current) {
return;
}
if (navigation.state === 'submitting') {
inputRef.current.value = '';
inputRef.current.rows = 1;
} else {
inputRef.current.focus();
}
}, [navigation.state]);
/**
* Adds the API's response message to the chat history
* when the data comes back from the action method
*/
useEffect(() => {
if (data?.error) {
pushChatHistory({
content: data.error as string,
role: 'assistant',
error: true,
})
return;
}
if (data?.answer) {
const newAnswer = {
content: data.answer as string,
role: 'assistant',
};
pushChatHistory(newAnswer as ChatHistoryProps);
// push location to history
navigate('/', {
state: {
...location.state,
chatHistory: [...chatHistory, newAnswer],
},
})
}
}, [data, pushChatHistory, navigate]);
/**
* Clears the chat history when the page is loaded without any state
*/
useEffect(() => {
if (!location.state) {
setChatHistory([]);
scrollToBottom();
}
}, [location, setChatHistory, scrollToBottom]);
/**
* Sets the chat history when the page is loaded or the history is traversed (back / forward)
*/
useEffect(() => {
setChat();
}, [setChat]);
/**
* Scrolls to the bottom of the chat container when the chat history changes
*/
useEffect(() => {
if (!chatContainerRef.current) {
return;
}
if (chatHistory.length) {
scrollToBottom();
}
}, [chatHistory, scrollToBottom]);
return (
<main className="container mx-auto rounded-lg h-full grid grid-rows-layout p-4 pb-0 sm:p-8 sm:pb-0 max-w-full sm:max-w-auto">
<div className="chat-container" ref={chatContainerRef}>
{chatHistory.length === 0 && (
<div className="intro grid place-items-center h-full text-center">
<div className="intro-content">
<h1 className="text-4xl font-semibold">OpenAI base</h1>
<p className="mt-4">Ask anything 😊</p>
</div>
</div>
)}
{chatHistory.length > 0 && (
<div className="messages max-w-maxWidth mx-auto min-h-full grid place-content-end grid-cols-1 gap-4">
{chatHistory.map((chat, index) => (
<React.Fragment key={`message-${index}`}>
<Message
error={chat.error}
content={chat.content}
role={chat.role}
/>
</React.Fragment>
))}
{isSubmitting && (
<Message thinking role="assistant" content="" />
)}
</div>
)}
</div>
<div className="form-container p-4 sm:p-8 backdrop-blur-md sticky bottom-0">
<Form
aria-disabled={isSubmitting}
method="post"
ref={formRef}
onSubmit={handleFormSubmit}
replace
className="max-w-[500px] mx-auto"
>
<div className="input-wrap relative flex items-center">
<label htmlFor="message" className="absolute left[-9999px] w-px h-px overflow-hidden">Ask a question</label>
<textarea
id="message"
aria-disabled={isSubmitting}
ref={inputRef}
className="auto-growing-input m-0 appearance-none text-black placeholder:text-black resize-none text-sm md:text-base py-3 pl-5 pr-14 border border-slate-400 outline-none rounded-4xl w-full block leading-6 bg-white"
placeholder="Ask a question"
name="message"
onChange={handleTextareaChange}
required
rows={1}
onKeyDown={submitFormOnEnter}
minLength={2}
disabled={isSubmitting}
/>
<input
type="hidden"
value={JSON.stringify(chatHistory)} name="chat-history"
/>
<button
aria-label="Submit"
aria-disabled={isSubmitting}
className="absolute right-0 flex-shrink-0 items-center appearance-none text-black h-[50px] w-[50px] border-none cursor-pointer shadow-none rounded-4xl grid place-items-center group transition-colors disabled:cursor-not-allowed focus:outline-dark-blue"
type="submit"
disabled={isSubmitting}
>
<SendIcon />
</button>
</div>
</Form>
<p className="made-with text-xs text-center mt-4">
Made with ❤️ by <a target="_blank" href="http://joshuasanger.ca">Josh Sanger</a>
</p>
</div>
</main>
);
}
export function ErrorBoundary({error}: {error: Error}) {
return (
<main className="container mx-auto rounded-lg h-full grid grid-rows-layout p-4 pb-0 sm:p-8 sm:pb-0 max-w-full sm:max-w-auto">
<div className="chat-container">
<div className="intro grid place-items-center h-full text-center">
<div className="intro-content inline-block px-4 py-8 border border-error rounded-lg">
<h1 className="text-3xl font-semibold">Oops, something went wrong!</h1>
<p className="mt-4 text-error ">{error.message}</p>
<p className="mt-4"><Link to="/">Back to chat</Link></p>
</div>
</div>
</div>
</main>
);
}