You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.3 KiB

6 months ago
import { useEffect, useRef, useState } from 'react';
6 months ago
import './App.css'
6 months ago
import { sendChatMessage } from './util/chat';
6 months ago
const BASE_URL='http://localhost:3333';
function App() {
const [history, setHistory] = useState([]);
const [processing, setProcessing] = useState(false);
const [prompt, setPrompt] = useState([]);
6 months ago
const refHistoryContainer= useRef(null);
const refPrompContainer= useRef(null);
6 months ago
6 months ago
function onSubmitToNode(event) {
6 months ago
event.preventDefault();
const input = event.target.elements.input.value;
console.log("Submitted:", input);
6 months ago
// setProcessing(true);
6 months ago
fetch(`${BASE_URL}/generate`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input:[
...history,
{
role:'user',
content:[{
type:'input_text',
text: input
}]
}
]
}),
}).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
response.json().then(data => {;
console.log(data);
// add to history
setHistory(prev => [...prev, {
role: 'user',
content: [{
type:'input_text',
text: input
}]
}, {
role: 'assistant',
content: [{
type:'output_text',
text: data.output_text
}]
}]);
setPrompt([
...prompt,
data.prompt,
]);
// clear input
event.target.elements.input.value = '';
6 months ago
setProcessing(false);
6 months ago
});
});
6 months ago
}
function onSubmit(event) {
event.preventDefault();
const input = event.target.elements.input.value;
console.log("Submitted:", input);
// setProcessing(true);
sendChatMessage([
...history,
{
role:'user',
content: input,
}
]).then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
let data=response;
console.log(data);
// add to history
setHistory(prev => [...prev, {
role: 'user',
content:input,
}, {
role: 'assistant',
content: data.output_text,
}]);
setPrompt([
...prompt,
data.prompt,
]);
// clear input
event.target.elements.input.value = '';
setProcessing(false);
});
6 months ago
}
6 months ago
useEffect(()=>{
refHistoryContainer.current.scrollTop = refHistoryContainer.current.scrollHeight;
},[history]);
useEffect(()=>{
refPrompContainer.current.scrollTop = refPrompContainer.current.scrollHeight;
},[prompt]);
6 months ago
return (
6 months ago
<main className='h-screen flex flex-col gap-8 justify-end p-8'>
<div ref={refPrompContainer} className='flex-1 flex flex-col gap-2 border-4 overflow-y-auto'>
6 months ago
{prompt?.length==0 ? (
<div className='p-2 border-b border-gray-200'>Promp will appear here...</div>
):(
prompt?.map((item, index) => (
6 months ago
<div key={index} className='p-2 border-b border-gray-500 bg-pink-200'>
6 months ago
<p className='text-lg'>{item}</p>
</div>
))
)}
</div>
6 months ago
<div ref={refHistoryContainer} className='flex-1 overflow-y-auto'>
<div className='flex flex-col justify-end gap-2'>
{history?.length==0? (
<div className='p-2'>History will appear here...</div>
):(
history.map((item, index) => (
<div key={index} className={`p-2 rounded border-4 ${item.role === 'user' ? 'bg-gray-100' : 'bg-yellow-100'}`}>
6 months ago
<p className='text-lg whitespace-pre-wrap'>{item.content}</p>
6 months ago
</div>
))
)}
</div>
6 months ago
</div>
<div className=''>
6 months ago
<form className='flex flex-col justify-center *:border-4 gap-4' onSubmit={onSubmit} autoComplete="off">
<input id="input" name="input" required className='self-stretch p-2' autoComplete="off"/>
6 months ago
<button type="submit" className='rounded-full uppercase' disabled={processing}>Send</button>
</form>
</div>
</main>
)
}
export default App