feat: Implement main application shell with navigation and color scheme toggle

- Added Appshell component with responsive navbar and main content area
- Integrated ColorSchemeToggle for light/dark mode switching
- Created Welcome component with styled title and introductory text
- Developed ChatPage for LLM interaction with WebSocket support
- Implemented Biblioteca for managing notes with rich text editor
- Added LoginPage for user authentication with error handling
- Introduced MessageList and MessageBubble components for chat messages
- Styled components with CSS modules for consistent design
This commit is contained in:
2025-06-21 02:01:21 +02:00
parent 3d5deef0fb
commit aef8791151
101 changed files with 169 additions and 166 deletions
@@ -0,0 +1,57 @@
from fastapi import APIRouter
router = APIRouter()
@router.get("/bar")
def get_bar_chart():
return {
"xAxis": {"type": "category", "data": ["Ene", "Feb", "Mar", "Abr"]},
"yAxis": {"type": "value"},
"series": [{"data": [5, 20, 36, 10], "type": "bar"}]
}
@router.get("/line")
def get_line_chart():
return {
"xAxis": {"type": "category", "data": ["Semana 1", "Semana 2", "Semana 3", "Semana 4"]},
"yAxis": {"type": "value"},
"series": [{"data": [15, 25, 18, 30], "type": "line"}]
}
@router.get("/pie")
def get_pie_chart():
return {
"tooltip": {"trigger": "item"},
"legend": {"top": "5%", "left": "center"},
"series": [{
"name": "Accesos",
"type": "pie",
"radius": ["40%", "70%"],
"avoidLabelOverlap": False,
"itemStyle": {"borderRadius": 10, "borderColor": "#fff", "borderWidth": 2},
"label": {"show": False, "position": "center"},
"emphasis": {
"label": {"show": True, "fontSize": 16, "fontWeight": "bold"}
},
"labelLine": {"show": False},
"data": [
{"value": 1048, "name": "Search"},
{"value": 735, "name": "Direct"},
{"value": 580, "name": "Email"},
{"value": 484, "name": "Union Ads"},
{"value": 300, "name": "Video Ads"}
]
}]
}
@router.get("/scatter")
def get_scatter_chart():
return {
"xAxis": {},
"yAxis": {},
"series": [{
"symbolSize": 20,
"data": [[10, 8], [20, 20], [30, 10], [40, 30], [50, 15]],
"type": "scatter"
}]
}
@@ -0,0 +1,9 @@
# backend/api/endpoints/ping.py
from fastapi import APIRouter
router = APIRouter()
@router.get("/")
async def ping():
return {"message": "pong"}
@@ -0,0 +1,44 @@
# backend/domains/llm/agent_endpoints.py
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from fastapi.concurrency import run_in_threadpool
from backend.backend_domains.llms.llm_chat_srvc import construir_agente_llm, responder, responder_stream
from domains.Logger.logger_db import LoggerDB, logger
from entrypoint.init_db import db_credencial
LoggerDB(db_credencial, "logger_llm", created_by="sistema")
router = APIRouter()
agente = construir_agente_llm() # inicializa el agente una vez
# 📥 Schema para entrada de prompt
class ChatInput(BaseModel):
prompt: str
# ✅ Endpoint de respuesta simple
@router.post("/chat", summary="Enviar prompt y obtener respuesta completa del agente")
async def chat_endpoint(data: ChatInput):
try:
return await responder(data.prompt, agente)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.exception("[ERROR] Fallo durante respuesta del agente:")
raise HTTPException(status_code=500, detail="Error interno al procesar la solicitud.")
# 🔁 Endpoint de streaming
@router.post("/chat-stream", summary="Enviar prompt y recibir respuesta del agente en streaming")
async def chat_stream_endpoint(data: ChatInput):
try:
return StreamingResponse(
responder_stream(data.prompt, agente),
media_type="text/plain"
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.exception("[ERROR] Fallo durante respuesta en streaming:")
raise HTTPException(status_code=500, detail="Error interno en el agente.")
@@ -0,0 +1,84 @@
# src/services/agent_service.py
from domains.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo
from domains.ConexionSql.Postgres_conexion import PostgresConexion
from domains.ConexionApis.OpenAi_conexion import OpenAICliente
from domains.Llms.Modelos.Openai_model import ModeloOpenAI
from domains.Llms.Agente import AgenteAI
from domains.Llms.Memory.postgres_MemoryConv import MemoryConvPostgres
from domains.Llms.MCPs.McpClient import MCPClient
from domains.Llms.MCPs.McpClient_Registry import ClientRegistry
from entrypoint.init_db import db_credencial
from domains.Logger.logger_db import LoggerDB, logger
LoggerDB(db_credencial, "logger_llm", created_by="sistema")
from typing import AsyncGenerator
# 🔧 Inicialización única del agente
def construir_agente_llm() -> AgenteAI:
logger.info("[INICIO] Inicializando agente LLM...")
conexion = PostgresConexion(db_credencial)
# Paso 1: Obtener credencial
repo = OpenAICredencialRepo(conexion)
credencial = repo.get_by_id("OPAK20250513-61b29978b7604031014")
if not credencial:
raise ValueError("No se encontró la credencial OpenAI")
logger.debug(f"[OK] Credencial OpenAI cargada: {credencial.titulo}")
# Paso 2: Crear cliente
cliente = OpenAICliente(credencial)
# Paso 3: Instanciar modelo
modelo = ModeloOpenAI(
cliente=cliente,
model="gpt-4o",
temperature=1
)
# Paso 4: Memoria en PostgreSQL
memoria = MemoryConvPostgres(
credencial=db_credencial,
nombre_tabla="memoria_conversacion_pruebas",
k=10
)
# Paso 5: Herramientas MCP (ej. archivos)
archivos = MCPClient.from_http(
name="files",
url="http://127.0.0.1:4201/fs"
)
registry = ClientRegistry()
registry.add("files", archivos)
# Paso 6: Agente
agente = AgenteAI(
modelo=modelo,
nombre="Asistente Inteligente",
descripcion="",
system_prompt="",
rol="asistente",
objetivos=[],
max_iterations=0,
memoria=memoria,
mcp=registry
)
logger.success("[OK] Agente LLM listo.")
return agente
# ⚡ Función simple
async def responder(prompt: str, agente: AgenteAI) -> str:
logger.info(f"[Petición] Prompt recibido: {prompt[:50]}...")
respuesta = await agente.interactuar_en_bucle(prompt=prompt, stream=False)
logger.debug(f"[Respuesta] {respuesta[:100]}...")
return respuesta
# 🔁 Función en streaming
async def responder_stream(prompt: str, agente: AgenteAI) -> AsyncGenerator[str, None]:
logger.info(f"[Streaming] Prompt recibido: {prompt[:50]}...")
async for token in agente.interactuar_en_bucle(prompt=prompt, stream=True):
yield token
@@ -0,0 +1,35 @@
from fastapi import WebSocket, APIRouter, WebSocketDisconnect
from backend.backend_domains.llms.llm_chat_srvc import construir_agente_llm
from domains.Logger.logger_db import LoggerDB, logger
from entrypoint.init_db import db_credencial
import json
LoggerDB(db_credencial, "logger_llm_ws", created_by="sistema")
router = APIRouter()
agente = construir_agente_llm()
@router.websocket("/ws/chat")
async def chat_ws(websocket: WebSocket):
await websocket.accept()
try:
data = await websocket.receive_text()
parsed = json.loads(data)
prompt = parsed.get("prompt")
if not prompt:
await websocket.send_text("⚠️ Prompt vacío.")
await websocket.close()
return
# ✅ Solución: hacer await antes de iterar
respuesta_gen = await agente.interactuar_en_bucle(prompt=prompt, stream=True)
async for token in respuesta_gen:
await websocket.send_text(token)
await websocket.close()
except WebSocketDisconnect:
logger.info("🔌 WebSocket desconectado por el cliente.")
except Exception as e:
logger.exception("❌ Error en WebSocket:")
await websocket.close()
@@ -0,0 +1,113 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi import Path
from backend.backend_domains.text_manager.text_manager_schema import BibliotecaInput, NotaInput
from fastapi.concurrency import run_in_threadpool
from backend.db.conexion import get_conexion
from backend.backend_domains.text_manager.text_manager_srvc import *
from domains.ConexionSql.Postgres_conexion import PostgresConexion
from entrypoint.init_db import db_credencial
from domains.Logger.logger_db import LoggerDB, logger
LoggerDB(db_credencial, "logger_textos", created_by="sistema")
router = APIRouter()
@router.post("/biblioteca", summary="Crear una nueva biblioteca")
async def crear_biblioteca_endpoint(
data: BibliotecaInput,
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return await run_in_threadpool(
crear_biblioteca,
data.nombre_biblioteca,
conexion,
data.descripcion,
)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error interno al crear la biblioteca: {str(e)}")
@router.get("/list", summary="Listar todas las bibliotecas")
def listar_todas_bibliotecas(
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return listar_bibliotecas(conexion=conexion)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail="Error interno al listar las bibliotecas")
@router.post("/nota/{biblioteca_id}", summary="Agregar una nota a una biblioteca")
def agregar_nota(
biblioteca_id: str = Path(..., description="ID de la biblioteca a la que se agregará la nota"),
nota: NotaInput = ..., # viene del body
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return agregar_nota_a_biblioteca(
conexion=conexion,
biblioteca_id=biblioteca_id,
titulo=nota.titulo,
texto=nota.texto,
tags=nota.tags,
conexiones=nota.conexiones,
resumen=nota.resumen
)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail="Error interno al agregar la nota")
@router.get("/nota/list/{biblioteca_id}", summary="Listar todas las notas de una biblioteca")
def listar_notas(
biblioteca_id: str,
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return listar_notas_de_biblioteca(conexion, biblioteca_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail="Error interno al listar las notas")
@router.delete("/nota/{biblioteca_id}/{nota_id}", summary="Eliminar una nota por ID")
def eliminar_nota_endpoint(
biblioteca_id: str = Path(..., description="ID de la biblioteca"),
nota_id: str = Path(..., description="ID de la nota a eliminar"),
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return eliminar_nota(conexion=conexion, biblioteca_id=biblioteca_id, nota_id=nota_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Error interno al eliminar la nota")
@router.put("/nota/{biblioteca_id}/{nota_id}", summary="Actualizar una nota por ID")
def actualizar_nota_endpoint(
biblioteca_id: str = Path(..., description="ID de la biblioteca"),
nota_id: str = Path(..., description="ID de la nota a actualizar"),
nota: NotaInput = ..., # body
conexion: PostgresConexion = Depends(get_conexion)
):
try:
return actualizar_nota(conexion=conexion, biblioteca_id=biblioteca_id, nota_id=nota_id, nota_input=nota)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception:
raise HTTPException(status_code=500, detail="Error interno al actualizar la nota")
@@ -0,0 +1,15 @@
from pydantic import BaseModel
from typing import List, Optional
class NotaInput(BaseModel):
titulo: str
texto: str = ""
tags: Optional[List[str]] = []
conexiones: Optional[List[str]] = []
resumen: Optional[str] = ""
class BibliotecaInput(BaseModel):
nombre_biblioteca: str
descripcion: str
@@ -0,0 +1,188 @@
from domains.TextManager.biblioteca import Biblioteca
from domains.TextManager.biblioteca_mmr import BibliotecaRepo
from domains.Llms.Embedders.Openai_embedder import OpenAIEmbedder
from domains.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo
from domains.ConexionSql.Postgres_conexion import PostgresConexion
from domains.TextManager.nota import Nota
from domains.TextManager.notas_mmr import generar_tabla_nota_para_biblioteca, NotaRepo
from sqlalchemy import MetaData
from backend.backend_domains.text_manager.text_manager_schema import NotaInput
from entrypoint.init_db import db_credencial
from domains.Logger.logger_db import LoggerDB, logger
LoggerDB(db_credencial, "logger_textos", created_by="sistema")
def crear_biblioteca(nombre_biblioteca: str, conexion: PostgresConexion, descripcion: str = None):
logger.info("[INICIO] Creando biblioteca...")
try:
logger.info("[Paso 1] Obteniendo credencial...")
cred_repo = OpenAICredencialRepo(conexion)
credencial = cred_repo.get_by_id("OPAK20250513-61b29978b7604031014")
logger.debug(f"[OK] Credencial obtenida: {credencial.titulo if credencial else '❌ None'}")
logger.info("[Paso 2] Instanciando embedder...")
embedder = OpenAIEmbedder(credencial, model="text-embedding-3-large")
logger.debug("[OK] Embedder instanciado")
logger.info("[Paso 3] Instanciando biblioteca...")
biblioteca = Biblioteca(
nombre=nombre_biblioteca,
embedder=embedder,
descripcion=descripcion
)
logger.debug(f"[OK] Biblioteca instanciada con ID: {biblioteca.id}")
logger.info("[Paso 4] Guardando en base de datos...")
repo = BibliotecaRepo(conexion)
repo.add(biblioteca=biblioteca)
logger.success("[OK] Biblioteca guardada")
logger.info("[Paso 5] Generando modelo de notas...")
biblioteca.generar_modelo_notas(conexion)
logger.success("[OK] Modelo de notas generado")
logger.success("[FIN] Biblioteca creada correctamente")
return {
"mensaje": f"Biblioteca '{nombre_biblioteca}' creada con éxito.",
"id": biblioteca.id
}
except Exception as e:
logger.exception("[ERROR] Ocurrió una excepción:")
raise
def listar_bibliotecas(conexion: PostgresConexion) -> list[dict]:
repo = BibliotecaRepo(conexion)
bibliotecas: list[Biblioteca] = repo.get_all()
return [
{
"id": b.id,
"nombre": b.nombre,
"descripcion": b.descripcion,
"vector_dim": b.vector_dim
}
for b in bibliotecas
]
def agregar_nota_a_biblioteca(
conexion: PostgresConexion,
biblioteca_id: str,
titulo: str,
texto: str = "",
tags: list[str] = None,
conexiones: list[str] = None,
resumen: str = ""
):
repo_biblioteca = BibliotecaRepo(conexion)
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
if biblioteca is None:
raise ValueError(f"No se encontró la biblioteca con ID {biblioteca_id}")
nota = Nota(
titulo=titulo,
texto=texto,
tags=tags or [],
conexiones=conexiones or [],
resumen=resumen or "",
)
logger.debug(
f"[DEBUG] Nota creada: titulo='{nota.titulo}', "
f"texto_len={len(nota.texto)}, "
f"tags={len(nota.tags)}, "
f"conexiones={len(nota.conexiones)}, "
f"resumen_len={len(nota.resumen)}"
)
metadata = MetaData()
tabla, ModeloNota = generar_tabla_nota_para_biblioteca(
biblioteca.nombre,
biblioteca.vector_dim,
metadata
)
metadata.create_all(conexion.get_engine())
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
nota_id = repo_nota.add(nota)
resultado = {
"mensaje": f"Nota '{titulo}' agregada con éxito a la biblioteca '{biblioteca.nombre}'.",
"nota_id": nota_id
}
logger.success(f"[SUCCESS] {resultado['mensaje']}")
return resultado
def listar_notas_de_biblioteca(conexion: PostgresConexion, biblioteca_id: str) -> list[dict]:
repo_biblioteca = BibliotecaRepo(conexion)
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
if not biblioteca:
raise ValueError(f"No se encontró la biblioteca con ID: {biblioteca_id}")
metadata = MetaData()
tabla, ModeloNota = generar_tabla_nota_para_biblioteca(biblioteca.nombre, biblioteca.vector_dim, metadata)
metadata.create_all(conexion.get_engine())
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
notas = repo_nota.get_all()
return [
{
"id": n.id,
"titulo": n.titulo,
"tags": n.tags,
"texto": n.texto,
"resumen": n.resumen,
"conexiones": n.conexiones
}
for n in notas
]
def eliminar_nota(conexion: PostgresConexion, biblioteca_id: str, nota_id: str) -> dict:
repo_biblioteca = BibliotecaRepo(conexion)
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
if not biblioteca:
raise ValueError(f"No se encontró la biblioteca con ID: {biblioteca_id}")
metadata = MetaData()
_, ModeloNota = generar_tabla_nota_para_biblioteca(biblioteca.nombre, biblioteca.vector_dim, metadata)
metadata.create_all(conexion.get_engine())
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
fue_eliminada = repo_nota.delete_by_id(nota_id)
if fue_eliminada:
logger.success(f"Nota '{nota_id}' eliminada correctamente.")
return {"mensaje": f"Nota '{nota_id}' eliminada correctamente."}
else:
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
def actualizar_nota(conexion: PostgresConexion, biblioteca_id: str, nota_id: str, nota_input: NotaInput) -> dict:
repo_biblioteca = BibliotecaRepo(conexion)
biblioteca = repo_biblioteca.get_by_id(biblioteca_id)
if not biblioteca:
raise ValueError(f"No se encontró la biblioteca con ID: {biblioteca_id}")
metadata = MetaData()
_, ModeloNota = generar_tabla_nota_para_biblioteca(biblioteca.nombre, biblioteca.vector_dim, metadata)
metadata.create_all(conexion.get_engine())
repo_nota = NotaRepo(conexion.get_session(), ModeloNota)
nota_actualizada = Nota(
titulo=nota_input.titulo,
texto=nota_input.texto,
tags=nota_input.tags or [],
conexiones=nota_input.conexiones or [],
resumen=nota_input.resumen or ""
)
fue_actualizada = repo_nota.update(nota_id, nota_actualizada)
if fue_actualizada:
logger.success(f"Nota '{nota_id}' actualizada correctamente.")
return {"mensaje": f"Nota '{nota_id}' actualizada correctamente."}
else:
raise ValueError(f"No se encontró la nota con ID: {nota_id}")
@@ -0,0 +1,35 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from domains.Usuario.usuario_mmr import UsuarioRepo, Usuario, UsuarioModel
from backend.db.conexion import get_conexion
router = APIRouter()
@router.post("/usuarios/", response_model=dict)
def crear_usuario(nombre: str, email: str, db: Session = Depends(get_conexion)):
repo = UsuarioRepo(db)
usuario = Usuario(id=None, nombre=nombre, email=email)
usuario_id = repo.add(usuario)
return {"id": usuario_id}
@router.get("/usuarios/{usuario_id}", response_model=dict)
def obtener_usuario(usuario_id: int, db: Session = Depends(get_conexion)):
repo = UsuarioRepo(db)
usuario = repo.get_by_id(usuario_id)
if not usuario:
raise HTTPException(status_code=404, detail="Usuario no encontrado")
return {"id": usuario.id, "nombre": usuario.nombre, "email": usuario.email, "activo": usuario.activo}
@router.get("/usuarios/", response_model=list)
def listar_usuarios(db: Session = Depends(get_conexion)):
repo = UsuarioRepo(db)
usuarios = repo.get_all()
return [{"id": u.id, "nombre": u.nombre, "email": u.email, "activo": u.activo} for u in usuarios]
@router.delete("/usuarios/{usuario_id}", response_model=dict)
def eliminar_usuario(usuario_id: int, db: Session = Depends(get_conexion)):
repo = UsuarioRepo(db)
exito = repo.delete_by_id(usuario_id)
if not exito:
raise HTTPException(status_code=404, detail="Usuario no encontrado")
return {"ok": True}