feat: Implement WebSocket support for chat functionality and refactor chat service

- Added WebSocket endpoint for real-time chat interactions.
- Refactored ChatPage component to utilize WebSocket for sending and receiving messages.
- Updated chat service to handle streaming responses from the LLM agent.
- Introduced error handling for WebSocket connections and message processing.
- Modified Editor_Test to include AppShellWithMenu for better layout.
- Adjusted file path in generar_tree.py for correct directory structure.
- Created llm_chat_endpoint_v1.py and llm_chat_srvc.py for handling chat requests and responses.
- Established logging for WebSocket interactions and errors.
This commit is contained in:
2025-06-17 00:19:36 +02:00
parent 6d6fab5634
commit 9ee8daa295
9 changed files with 233 additions and 1028 deletions
+31 -8
View File
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useState, useRef } from "react";
import { Container, Stack, Paper, ScrollArea, Title } from "@mantine/core";
import { ChatInput } from "./ChatInput";
import { MessageList } from "./MessageList";
@@ -8,19 +8,42 @@ export function ChatPage() {
const [messages, setMessages] = useState([
{ sender: "bot", content: "Hola, ¿en qué puedo ayudarte hoy?" },
]);
const wsRef = useRef<WebSocket | null>(null);
const handleSend = async (content: string) => {
const newMessages = [...messages, { sender: "user", content }];
setMessages(newMessages);
const response = await fetch("/api/chat", {
method: "POST",
body: JSON.stringify({ messages: newMessages }),
headers: { "Content-Type": "application/json" },
});
let currentResponse = "";
setMessages((prev) => [...prev, { sender: "bot", content: "" }]);
const data = await response.json();
setMessages([...newMessages, { sender: "bot", content: data.reply }]);
wsRef.current = new WebSocket("ws://localhost:8000/ws/chat");
wsRef.current.onopen = () => {
wsRef.current?.send(JSON.stringify({ prompt: content }));
};
wsRef.current.onmessage = (event) => {
const token = event.data;
currentResponse += token;
setMessages((prev) => {
const updated = [...prev];
updated[updated.length - 1] = { sender: "bot", content: currentResponse };
return updated;
});
};
wsRef.current.onerror = (err) => {
console.error("WebSocket error:", err);
setMessages((prev) => [
...prev.slice(0, -1),
{ sender: "bot", content: "⚠️ Error al comunicarse con el servidor." },
]);
};
wsRef.current.onclose = () => {
wsRef.current = null;
};
};
return (
@@ -2,6 +2,7 @@ import { RichTextEditor } from '@mantine/tiptap';
import { useEditor } from '@tiptap/react';
import StarterKit from '@tiptap/starter-kit';
import '@mantine/tiptap/styles.css';
import { AppShellWithMenu } from '../FitzStudio/Appshell/Appshell';
export default function EditorTest() {
const editor = useEditor({
@@ -10,8 +11,9 @@ export default function EditorTest() {
});
return (
<div style={{ padding: 40 }}>
{editor && (
{editor && ( <AppShellWithMenu>
<RichTextEditor editor={editor}>
<RichTextEditor.Toolbar sticky stickyOffset={0}>
<RichTextEditor.ControlsGroup>
@@ -21,7 +23,10 @@ export default function EditorTest() {
</RichTextEditor.Toolbar>
<RichTextEditor.Content />
</RichTextEditor>
</AppShellWithMenu>
)}
</div>
);
}