From 5f0c1a9549d99923dfe99b62a8f916c899a29dc3 Mon Sep 17 00:00:00 2001 From: ernestocarocca <113439130+ernestocarocca@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:39:18 +0100 Subject: [PATCH] feat: update chat endpoint and enhance chat functionality in the UI --- src/api.ts | 2 +- src/app/page.tsx | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/api.ts b/src/api.ts index ebc9635..cdfec86 100644 --- a/src/api.ts +++ b/src/api.ts @@ -70,7 +70,7 @@ const aiMassage: FastifyPluginCallback = (fastify, opts, next) => { fastify.get('/chat', async (_, reply) => { await chat(reply, 'Hello'); }); - fastify.post('/chat/send', async (request, reply) => { + fastify.post('/chat/message', async (request, reply) => { const userMessage = request.body as string; await chat(reply, userMessage); }); diff --git a/src/app/page.tsx b/src/app/page.tsx index a356f5a..e4ada34 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,11 +1,38 @@ 'use client'; - +import { ChangeEvent, useState } from 'react'; import { Card, CardHeader, CardBody } from '@nextui-org/card'; import { Button, Spacer } from '@nextui-org/react'; import { IconArrowUp, IconTrash } from '@tabler/icons-react'; import { TextArea } from '@/components/Textearea'; export default function Page() { + const [response, setResponse] = useState(''); + const [loading, setLoading] = useState(false); + const [input, setInput] = useState(''); + const [error, setError] = useState(''); + + const handleInputChange = (event: ChangeEvent) => { + setInput(event.target.value); + }; + + const fetchChatCompletion = async () => { + setLoading(true); + + try { + const response = await fetch('http://localhost:8000/chat/message', { + method: 'POST', + body: JSON.stringify({ userMessage: input }) + }); + console.log('response', response); + if (response.ok) { + setResponse(await response.text()); + } + } catch (error) { + setError('Error processing request'); + } + setLoading(false); + }; + return (
@@ -18,15 +45,21 @@ export default function Page() {
- + + {loading &&

Loading...

} + {error &&

{error}

} + {response &&

{response}

} +